2 * "$Id: job.c 7902 2008-09-03 14:20:17Z mike $"
4 * Job management routines for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2009 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
17 * cupsdAddJob() - Add a new job to the job queue.
18 * cupsdCancelJobs() - Cancel all jobs for the given
20 * cupsdCheckJobs() - Check the pending jobs and start any if the
21 * destination is available.
22 * cupsdCleanJobs() - Clean out old jobs.
23 * cupsdContinueJob() - Continue printing with the next file in a job.
24 * cupsdDeleteJob() - Free all memory used by a job.
25 * cupsdFreeAllJobs() - Free all jobs from memory.
26 * cupsdFindJob() - Find the specified job.
27 * cupsdGetPrinterJobCount() - Get the number of pending, processing, or held
28 * jobs in a printer or class.
29 * cupsdGetUserJobCount() - Get the number of pending, processing, or held
31 * cupsdLoadAllJobs() - Load all jobs from disk.
32 * cupsdLoadJob() - Load a single job.
33 * cupsdMoveJob() - Move the specified job to a different
35 * cupsdReleaseJob() - Release the specified job.
36 * cupsdRestartJob() - Restart the specified job.
37 * cupsdSaveAllJobs() - Save a summary of all jobs to disk.
38 * cupsdSaveJob() - Save a job to disk.
39 * cupsdSetJobHoldUntil() - Set the hold time for a job.
40 * cupsdSetJobPriority() - Set the priority of a job, moving it up/down
41 * in the list as needed.
42 * cupsdSetJobState() - Set the state of the specified print job.
43 * cupsdStopAllJobs() - Stop all print jobs.
44 * cupsdUnloadCompletedJobs() - Flush completed job history from memory.
45 * compare_active_jobs() - Compare the job IDs and priorities of two
47 * compare_jobs() - Compare the job IDs of two jobs.
48 * finalize_job() - Cleanup after job filter processes and support
50 * get_options() - Get a string containing the job options.
51 * ipp_length() - Compute the size of the buffer needed to hold
52 * the textual IPP attributes.
53 * load_job_cache() - Load jobs from the job.cache file.
54 * load_next_job_id() - Load the NextJobId value from the job.cache
56 * load_request_root() - Load jobs from the RequestRoot directory.
57 * set_hold_until() - Set the hold time and update job-hold-until
59 * set_time() - Set one of the "time-at-xyz" attributes.
60 * start_job() - Start a print job.
61 * stop_job() - Stop a print job.
62 * unload_job() - Unload a job from memory.
63 * update_job() - Read a status update from a job's filters.
64 * update_job_attrs() - Update the job-printer-* attributes.
68 * Include necessary headers...
73 #include <cups/backend.h>
78 * Design Notes for Job Management
79 * -------------------------------
83 * pending Do nothing/check jobs
84 * pending-held Send SIGTERM to filters and backend
85 * processing Do nothing/start job
86 * stopped Send SIGKILL to filters and backend
87 * canceled Send SIGTERM to filters and backend
91 * Finalize clears the printer <-> job association, deletes the status
92 * buffer, closes all of the pipes, etc. and doesn't get run until all of
93 * the print processes are finished.
95 * UNLOADING OF JOBS (cupsdUnloadCompletedJobs)
97 * We unload the job attributes when they are not needed to reduce overall
98 * memory consumption. We don't unload jobs where job->state_value <
99 * IPP_JOB_STOPPED, job->printer != NULL, or job->access_time is recent.
101 * STARTING OF JOBS (start_job)
103 * When a job is started, a status buffer, several pipes, a security
104 * profile, and a backend process are created for the life of that job.
105 * These are shared for every file in a job. For remote print jobs, the
106 * IPP backend is provided with every file in the job and no filters are
109 * The job->printer member tracks which printer is printing a job, which
110 * can be different than the destination in job->dest for classes. The
111 * printer object also has a job pointer to track which job is being
114 * PRINTING OF JOB FILES (cupsdContinueJob)
116 * Each file in a job is filtered by 0 or more programs. After getting the
117 * list of filters needed and the total cost, the job is either passed or
118 * put back to the processing state until the current FilterLevel comes down
119 * enough to allow printing.
121 * If we can print, we build a string for the print options and run each of
122 * the filters, piping the output from one into the next.
124 * JOB STATUS UPDATES (update_job)
126 * The update_job function gets called whenever there are pending messages
127 * on the status pipe. These generally are updates to the marker-*,
128 * printer-state-message, or printer-state-reasons attributes. On EOF,
129 * finalize_job is called to clean up.
131 * FINALIZING JOBS (finalize_job)
133 * When all filters and the backend are done, we set the job state to
134 * completed (no errors), aborted (filter errors or abort-job policy),
135 * pending-held (auth required or retry-job policy), or pending
136 * (retry-current-job or stop-printer policies) as appropriate.
138 * Then we close the pipes and free the status buffers and profiles.
140 * JOB FILE COMPLETION (process_children in main.c)
142 * For multiple-file jobs, process_children (in main.c) sees that all
143 * filters have exited and calls in to print the next file if there are
144 * more files in the job, otherwise it waits for the backend to exit and
145 * update_job to do the cleanup.
153 static mime_filter_t gziptoany_filter
=
155 NULL
, /* Source type */
156 NULL
, /* Destination type */
158 "gziptoany" /* Filter program to run */
166 static int compare_active_jobs(void *first
, void *second
, void *data
);
167 static int compare_jobs(void *first
, void *second
, void *data
);
168 static void finalize_job(cupsd_job_t
*job
);
169 static char *get_options(cupsd_job_t
*job
, int banner_page
, char *copies
,
170 size_t copies_size
, char *title
,
172 static int ipp_length(ipp_t
*ipp
);
173 static void load_job_cache(const char *filename
);
174 static void load_next_job_id(const char *filename
);
175 static void load_request_root(void);
176 static void set_hold_until(cupsd_job_t
*job
, time_t holdtime
);
177 static void set_time(cupsd_job_t
*job
, const char *name
);
178 static void start_job(cupsd_job_t
*job
, cupsd_printer_t
*printer
);
179 static void stop_job(cupsd_job_t
*job
, cupsd_jobaction_t action
);
180 static void unload_job(cupsd_job_t
*job
);
181 static void update_job(cupsd_job_t
*job
);
182 static void update_job_attrs(cupsd_job_t
*job
, int do_message
);
186 * 'cupsdAddJob()' - Add a new job to the job queue.
189 cupsd_job_t
* /* O - New job record */
190 cupsdAddJob(int priority
, /* I - Job priority */
191 const char *dest
) /* I - Job destination */
193 cupsd_job_t
*job
; /* New job record */
196 if ((job
= calloc(sizeof(cupsd_job_t
), 1)) == NULL
)
199 job
->id
= NextJobId
++;
200 job
->priority
= priority
;
201 job
->back_pipes
[0] = -1;
202 job
->back_pipes
[1] = -1;
203 job
->print_pipes
[0] = -1;
204 job
->print_pipes
[1] = -1;
205 job
->side_pipes
[0] = -1;
206 job
->side_pipes
[1] = -1;
207 job
->status_pipes
[0] = -1;
208 job
->status_pipes
[1] = -1;
210 cupsdSetString(&job
->dest
, dest
);
213 * Add the new job to the "all jobs" and "active jobs" lists...
216 cupsArrayAdd(Jobs
, job
);
217 cupsArrayAdd(ActiveJobs
, job
);
224 * 'cupsdCancelJobs()' - Cancel all jobs for the given destination/user.
228 cupsdCancelJobs(const char *dest
, /* I - Destination to cancel */
229 const char *username
, /* I - Username or NULL */
230 int purge
) /* I - Purge jobs? */
232 cupsd_job_t
*job
; /* Current job */
235 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
237 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
239 if ((!job
->dest
|| !job
->username
) && !cupsdLoadJob(job
))
242 if ((!dest
|| !strcmp(job
->dest
, dest
)) &&
243 (!username
|| !strcmp(job
->username
, username
)))
246 * Cancel all jobs matching this destination/user...
250 cupsdSetJobState(job
, IPP_JOB_CANCELED
, CUPSD_JOB_PURGE
,
251 "Job purged by user.");
253 cupsdSetJobState(job
, IPP_JOB_CANCELED
, CUPSD_JOB_DEFAULT
,
254 "Job canceled by user.");
263 * 'cupsdCheckJobs()' - Check the pending jobs and start any if the destination
270 cupsd_job_t
*job
; /* Current job in queue */
271 cupsd_printer_t
*printer
, /* Printer destination */
272 *pclass
; /* Printer class destination */
273 ipp_attribute_t
*attr
; /* Job attribute */
276 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
277 "cupsdCheckJobs: %d active jobs, sleeping=%d, reload=%d",
278 cupsArrayCount(ActiveJobs
), Sleeping
, NeedReload
);
280 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
282 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
285 * Start held jobs if they are ready...
288 if (job
->state_value
== IPP_JOB_HELD
&&
290 job
->hold_until
< time(NULL
))
292 if (job
->pending_timeout
)
295 * This job is pending; check that we don't have an active Send-Document
296 * operation in progress on any of the client connections, then timeout
297 * the job so we can start printing...
300 cupsd_client_t
*con
; /* Current client connection */
303 for (con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
305 con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
307 con
->request
->request
.op
.operation_id
== IPP_SEND_DOCUMENT
)
313 if (cupsdTimeoutJob(job
))
317 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
318 "Job submission timed out.");
322 * Start pending jobs if the destination is available...
325 if (job
->state_value
== IPP_JOB_PENDING
&& !NeedReload
&& !Sleeping
&&
328 printer
= cupsdFindDest(job
->dest
);
332 (printer
->type
& (CUPS_PRINTER_IMPLICIT
| CUPS_PRINTER_CLASS
)))
335 * If the class is remote, just pass it to the remote server...
340 if (pclass
->state
== IPP_PRINTER_STOPPED
)
342 else if (pclass
->type
& CUPS_PRINTER_REMOTE
)
345 printer
= cupsdFindAvailablePrinter(printer
->name
);
348 if (!printer
&& !pclass
)
351 * Whoa, the printer and/or class for this destination went away;
355 cupsdSetJobState(job
, IPP_JOB_ABORTED
, CUPSD_JOB_PURGE
,
356 "Job aborted because the destination printer/class "
359 else if (printer
&& !printer
->holding_new_jobs
)
362 * See if the printer is available or remote and not printing a job;
363 * if so, start the job...
369 * Add/update a job-actual-printer-uri attribute for this job
370 * so that we know which printer actually printed the job...
373 if ((attr
= ippFindAttribute(job
->attrs
, "job-actual-printer-uri",
374 IPP_TAG_URI
)) != NULL
)
375 cupsdSetString(&attr
->values
[0].string
.text
, printer
->uri
);
377 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
,
378 "job-actual-printer-uri", NULL
, printer
->uri
);
381 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
384 if ((!(printer
->type
& CUPS_PRINTER_DISCOVERED
) && /* Printer is local */
385 printer
->state
== IPP_PRINTER_IDLE
) || /* and idle, OR */
386 ((printer
->type
& CUPS_PRINTER_DISCOVERED
) && /* Printer is remote */
387 !printer
->job
)) /* and not printing */
393 start_job(job
, printer
);
402 * 'cupsdCleanJobs()' - Clean out old jobs.
408 cupsd_job_t
*job
; /* Current job */
414 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
415 job
&& cupsArrayCount(Jobs
) >= MaxJobs
;
416 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
417 if (job
->state_value
>= IPP_JOB_CANCELED
&& !job
->printer
)
418 cupsdDeleteJob(job
, CUPSD_JOB_PURGE
);
423 * 'cupsdContinueJob()' - Continue printing with the next file in a job.
427 cupsdContinueJob(cupsd_job_t
*job
) /* I - Job */
429 int i
; /* Looping var */
430 int slot
; /* Pipe slot */
431 cups_array_t
*filters
, /* Filters for job */
432 *prefilters
; /* Filters with prefilters */
433 mime_filter_t
*filter
, /* Current filter */
434 *prefilter
, /* Prefilter */
435 port_monitor
; /* Port monitor filter */
436 char scheme
[255]; /* Device URI scheme */
437 ipp_attribute_t
*attr
; /* Current attribute */
438 const char *ptr
, /* Pointer into value */
439 *abort_message
; /* Abort message */
440 struct stat backinfo
; /* Backend file information */
441 int backroot
; /* Run backend as root? */
442 int pid
; /* Process ID of new filter process */
443 int banner_page
; /* 1 if banner page, 0 otherwise */
444 int filterfds
[2][2];/* Pipes used between filters */
445 int envc
; /* Number of environment variables */
446 char **argv
, /* Filter command-line arguments */
447 filename
[1024], /* Job filename */
448 command
[1024], /* Full path to command */
449 jobid
[255], /* Job ID string */
451 /* Job title string */
452 copies
[255], /* # copies string */
453 *options
, /* Options string */
455 /* Environment variables */
456 charset
[255], /* CHARSET env variable */
457 class_name
[255],/* CLASS env variable */
458 classification
[1024],
459 /* CLASSIFICATION env variable */
461 /* CONTENT_TYPE env variable */
463 /* DEVICE_URI env variable */
464 final_content_type
[1024],
465 /* FINAL_CONTENT_TYPE env variable */
466 lang
[255], /* LANG env variable */
469 /* APPLE_LANGUAGE env variable */
470 #endif /* __APPLE__ */
471 ppd
[1024], /* PPD env variable */
473 /* PRINTER_INFO env variable */
474 printer_location
[255],
475 /* PRINTER_LOCATION env variable */
477 /* PRINTER env variable */
479 /* RIP_MAX_CACHE env variable */
482 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
483 "cupsdContinueJob(job=%p(%d)): current_file=%d, num_files=%d",
484 job
, job
->id
, job
->current_file
, job
->num_files
);
487 * Figure out what filters are required to convert from
488 * the source to the destination type...
491 FilterLevel
-= job
->cost
;
495 if (job
->printer
->raw
)
498 * Remote jobs and raw queues go directly to the printer without
502 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Sending job to queue tagged as raw...");
509 * Local jobs get filtered...
512 filters
= mimeFilter(MimeDatabase
, job
->filetypes
[job
->current_file
],
513 job
->printer
->filetype
, &(job
->cost
));
517 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
518 "Unable to convert file %d to printable format!",
521 job
->current_file
++;
523 if (job
->current_file
== job
->num_files
)
524 cupsdSetJobState(job
, IPP_JOB_ABORTED
, CUPSD_JOB_DEFAULT
,
525 "Aborting job because it cannot be printed.");
531 * Remove NULL ("-") filters...
534 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
536 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
537 if (!strcmp(filter
->filter
, "-"))
538 cupsArrayRemove(filters
, filter
);
540 if (cupsArrayCount(filters
) == 0)
542 cupsArrayDelete(filters
);
547 * If this printer has any pre-filters, insert the required pre-filter
548 * in the filters array...
551 if (job
->printer
->prefiltertype
&& filters
)
553 prefilters
= cupsArrayNew(NULL
, NULL
);
555 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
557 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
559 if ((prefilter
= mimeFilterLookup(MimeDatabase
, filter
->src
,
560 job
->printer
->prefiltertype
)))
562 cupsArrayAdd(prefilters
, prefilter
);
563 job
->cost
+= prefilter
->cost
;
566 cupsArrayAdd(prefilters
, filter
);
569 cupsArrayDelete(filters
);
570 filters
= prefilters
;
575 * Set a minimum cost of 100 for all jobs so that FilterLimit
576 * works with raw queues and other low-cost paths.
583 * See if the filter cost is too high...
586 if ((FilterLevel
+ job
->cost
) > FilterLimit
&& FilterLevel
> 0 &&
590 * Don't print this job quite yet...
593 cupsArrayDelete(filters
);
595 cupsdLogJob(job
, CUPSD_LOG_INFO
,
596 "Holding because filter limit has been reached.");
597 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
598 "cupsdContinueJob: file=%d, cost=%d, level=%d, limit=%d",
599 job
->current_file
, job
->cost
, FilterLevel
,
604 FilterLevel
+= job
->cost
;
607 * Add decompression/raw filter as needed...
610 if ((!job
->printer
->raw
&& job
->compressions
[job
->current_file
]) ||
611 (!filters
&& !job
->printer
->remote
&&
612 (job
->num_files
> 1 || !strncmp(job
->printer
->device_uri
, "file:", 5))))
615 * Add gziptoany filter to the front of the list...
619 filters
= cupsArrayNew(NULL
, NULL
);
621 if (!cupsArrayInsert(filters
, &gziptoany_filter
))
623 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
624 "Unable to add decompression filter - %s", strerror(errno
));
626 cupsArrayDelete(filters
);
628 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
629 "Stopping job because the scheduler ran out of "
632 FilterLevel
-= job
->cost
;
638 * Add port monitor, if any...
641 if (job
->printer
->port_monitor
)
644 * Add port monitor to the end of the list...
648 filters
= cupsArrayNew(NULL
, NULL
);
650 port_monitor
.src
= NULL
;
651 port_monitor
.dst
= NULL
;
652 port_monitor
.cost
= 0;
654 snprintf(port_monitor
.filter
, sizeof(port_monitor
.filter
),
655 "%s/monitor/%s", ServerBin
, job
->printer
->port_monitor
);
657 if (!cupsArrayAdd(filters
, &port_monitor
))
659 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
660 "Unable to add port monitor - %s", strerror(errno
));
662 cupsArrayDelete(filters
);
664 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
665 "Stopping job because the scheduler ran out of "
668 FilterLevel
-= job
->cost
;
674 * Make sure we don't go over the "MAX_FILTERS" limit...
677 if (cupsArrayCount(filters
) > MAX_FILTERS
)
679 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
680 "Too many filters (%d > %d), unable to print!",
681 cupsArrayCount(filters
), MAX_FILTERS
);
683 cupsArrayDelete(filters
);
684 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
685 "Stopping job because it needs too many filters to "
688 FilterLevel
-= job
->cost
;
693 * Determine if we are printing a banner page or not...
696 if (job
->job_sheets
== NULL
)
698 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "No job-sheets attribute.");
699 if ((job
->job_sheets
=
700 ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_ZERO
)) != NULL
)
701 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
702 "... but someone added one without setting job_sheets!");
704 else if (job
->job_sheets
->num_values
== 1)
705 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "job-sheets=%s",
706 job
->job_sheets
->values
[0].string
.text
);
708 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "job-sheets=%s,%s",
709 job
->job_sheets
->values
[0].string
.text
,
710 job
->job_sheets
->values
[1].string
.text
);
712 if (job
->printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
))
714 else if (job
->job_sheets
== NULL
)
716 else if (strcasecmp(job
->job_sheets
->values
[0].string
.text
, "none") != 0 &&
717 job
->current_file
== 0)
719 else if (job
->job_sheets
->num_values
> 1 &&
720 strcasecmp(job
->job_sheets
->values
[1].string
.text
, "none") != 0 &&
721 job
->current_file
== (job
->num_files
- 1))
726 if ((options
= get_options(job
, banner_page
, copies
, sizeof(copies
), title
,
727 sizeof(title
))) == NULL
)
729 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
730 "Stopping job because the scheduler ran out of memory.");
731 cupsArrayDelete(filters
);
733 FilterLevel
-= job
->cost
;
738 * Build the command-line arguments for the filters. Each filter
739 * has 6 or 7 arguments:
747 * argv[6] = filename (optional; normally stdin)
749 * This allows legacy printer drivers that use the old System V
750 * printing interface to be used by CUPS.
752 * For remote jobs, we send all of the files in the argument list.
755 if (job
->printer
->remote
)
756 argv
= calloc(7 + job
->num_files
, sizeof(char *));
758 argv
= calloc(8, sizeof(char *));
762 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Unable to allocate argument array - %s",
764 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
765 "Stopping job because the scheduler ran out of memory.");
766 cupsArrayDelete(filters
);
768 FilterLevel
-= job
->cost
;
772 sprintf(jobid
, "%d", job
->id
);
774 argv
[0] = job
->printer
->name
;
776 argv
[2] = job
->username
;
781 if (job
->printer
->remote
&& job
->num_files
> 1)
783 for (i
= 0; i
< job
->num_files
; i
++)
785 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
787 argv
[6 + i
] = strdup(filename
);
792 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
793 job
->id
, job
->current_file
+ 1);
797 for (i
= 0; argv
[i
]; i
++)
798 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "argv[%d]=\"%s\"", i
, argv
[i
]);
801 * Create environment variable strings for the filters...
804 attr
= ippFindAttribute(job
->attrs
, "attributes-natural-language",
808 strcpy(apple_language
, "APPLE_LANGUAGE=");
809 _cupsAppleLanguage(attr
->values
[0].string
.text
,
810 apple_language
+ 15, sizeof(apple_language
) - 15);
811 #endif /* __APPLE__ */
813 switch (strlen(attr
->values
[0].string
.text
))
817 * This is an unknown or badly formatted language code; use
818 * the POSIX locale...
821 strcpy(lang
, "LANG=C");
826 * Just the language code (ll)...
829 snprintf(lang
, sizeof(lang
), "LANG=%s.UTF-8",
830 attr
->values
[0].string
.text
);
835 * Language and country code (ll-cc)...
838 snprintf(lang
, sizeof(lang
), "LANG=%c%c_%c%c.UTF-8",
839 attr
->values
[0].string
.text
[0],
840 attr
->values
[0].string
.text
[1],
841 toupper(attr
->values
[0].string
.text
[3] & 255),
842 toupper(attr
->values
[0].string
.text
[4] & 255));
846 if ((attr
= ippFindAttribute(job
->attrs
, "document-format",
847 IPP_TAG_MIMETYPE
)) != NULL
&&
848 (ptr
= strstr(attr
->values
[0].string
.text
, "charset=")) != NULL
)
849 snprintf(charset
, sizeof(charset
), "CHARSET=%s", ptr
+ 8);
851 strlcpy(charset
, "CHARSET=utf-8", sizeof(charset
));
853 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s/%s",
854 job
->filetypes
[job
->current_file
]->super
,
855 job
->filetypes
[job
->current_file
]->type
);
856 snprintf(device_uri
, sizeof(device_uri
), "DEVICE_URI=%s",
857 job
->printer
->device_uri
);
858 snprintf(ppd
, sizeof(ppd
), "PPD=%s/ppd/%s.ppd", ServerRoot
,
860 snprintf(printer_info
, sizeof(printer_name
), "PRINTER_INFO=%s",
861 job
->printer
->info
? job
->printer
->info
: "");
862 snprintf(printer_location
, sizeof(printer_name
), "PRINTER_LOCATION=%s",
863 job
->printer
->location
? job
->printer
->location
: "");
864 snprintf(printer_name
, sizeof(printer_name
), "PRINTER=%s", job
->printer
->name
);
865 snprintf(rip_max_cache
, sizeof(rip_max_cache
), "RIP_MAX_CACHE=%s", RIPCache
);
867 envc
= cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
869 envp
[envc
++] = charset
;
870 envp
[envc
++] = lang
;
872 envp
[envc
++] = apple_language
;
873 #endif /* __APPLE__ */
875 envp
[envc
++] = rip_max_cache
;
876 envp
[envc
++] = content_type
;
877 envp
[envc
++] = device_uri
;
878 envp
[envc
++] = printer_info
;
879 envp
[envc
++] = printer_location
;
880 envp
[envc
++] = printer_name
;
881 envp
[envc
++] = banner_page
? "CUPS_FILETYPE=job-sheet" :
882 "CUPS_FILETYPE=document";
884 if (!job
->printer
->remote
&& !job
->printer
->raw
)
886 filter
= (mime_filter_t
*)cupsArrayLast(filters
);
888 if (job
->printer
->port_monitor
)
889 filter
= (mime_filter_t
*)cupsArrayPrev(filters
);
891 if (filter
&& filter
->dst
)
893 snprintf(final_content_type
, sizeof(final_content_type
),
894 "FINAL_CONTENT_TYPE=%s/%s",
895 filter
->dst
->super
, filter
->dst
->type
);
896 envp
[envc
++] = final_content_type
;
900 if (Classification
&& !banner_page
)
902 if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
903 IPP_TAG_NAME
)) == NULL
)
904 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
906 else if (attr
->num_values
> 1 &&
907 strcmp(attr
->values
[1].string
.text
, "none") != 0)
908 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
909 attr
->values
[1].string
.text
);
911 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
912 attr
->values
[0].string
.text
);
914 envp
[envc
++] = classification
;
917 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
919 snprintf(class_name
, sizeof(class_name
), "CLASS=%s", job
->dest
);
920 envp
[envc
++] = class_name
;
923 if (job
->auth_username
)
924 envp
[envc
++] = job
->auth_username
;
925 if (job
->auth_domain
)
926 envp
[envc
++] = job
->auth_domain
;
927 if (job
->auth_password
)
928 envp
[envc
++] = job
->auth_password
;
932 envp
[envc
++] = job
->ccname
;
933 #endif /* HAVE_GSSAPI */
937 for (i
= 0; i
< envc
; i
++)
938 if (!strncmp(envp
[i
], "AUTH_", 5))
939 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"AUTH_%c****\"", i
,
941 else if (strncmp(envp
[i
], "DEVICE_URI=", 11))
942 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"%s\"", i
, envp
[i
]);
944 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"DEVICE_URI=%s\"", i
,
945 job
->printer
->sanitized_device_uri
);
947 if (job
->printer
->remote
)
948 job
->current_file
= job
->num_files
;
950 job
->current_file
++;
953 * Now create processes for all of the filters...
956 filterfds
[0][0] = -1;
957 filterfds
[0][1] = -1;
958 filterfds
[1][0] = -1;
959 filterfds
[1][1] = -1;
961 memset(job
->filters
, 0, sizeof(job
->filters
));
963 for (i
= 0, slot
= 0, filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
965 i
++, filter
= (mime_filter_t
*)cupsArrayNext(filters
))
967 if (filter
->filter
[0] != '/')
968 snprintf(command
, sizeof(command
), "%s/filter/%s", ServerBin
,
971 strlcpy(command
, filter
->filter
, sizeof(command
));
973 if (i
< (cupsArrayCount(filters
) - 1))
975 if (cupsdOpenPipe(filterfds
[slot
]))
977 abort_message
= "Stopping job because the scheduler could not create "
985 if (job
->current_file
== 1)
987 if (strncmp(job
->printer
->device_uri
, "file:", 5) != 0)
989 if (cupsdOpenPipe(job
->print_pipes
))
991 abort_message
= "Stopping job because the scheduler could not "
992 "create the backend pipes.";
999 job
->print_pipes
[0] = -1;
1000 if (!strcmp(job
->printer
->device_uri
, "file:/dev/null") ||
1001 !strcmp(job
->printer
->device_uri
, "file:///dev/null"))
1002 job
->print_pipes
[1] = -1;
1005 if (!strncmp(job
->printer
->device_uri
, "file:/dev/", 10))
1006 job
->print_pipes
[1] = open(job
->printer
->device_uri
+ 5,
1008 else if (!strncmp(job
->printer
->device_uri
, "file:///dev/", 12))
1009 job
->print_pipes
[1] = open(job
->printer
->device_uri
+ 7,
1011 else if (!strncmp(job
->printer
->device_uri
, "file:///", 8))
1012 job
->print_pipes
[1] = open(job
->printer
->device_uri
+ 7,
1013 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
1015 job
->print_pipes
[1] = open(job
->printer
->device_uri
+ 5,
1016 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
1018 if (job
->print_pipes
[1] < 0)
1020 abort_message
= "Stopping job because the scheduler could not "
1021 "open the output file.";
1026 fcntl(job
->print_pipes
[1], F_SETFD
,
1027 fcntl(job
->print_pipes
[1], F_GETFD
) | FD_CLOEXEC
);
1032 filterfds
[slot
][0] = job
->print_pipes
[0];
1033 filterfds
[slot
][1] = job
->print_pipes
[1];
1036 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
1037 filterfds
[slot
][1], job
->status_pipes
[1],
1038 job
->back_pipes
[0], job
->side_pipes
[0], 0,
1039 job
->profile
, job
->id
, job
->filters
+ i
);
1041 cupsdClosePipe(filterfds
[!slot
]);
1045 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Unable to start filter \"%s\" - %s.",
1046 filter
->filter
, strerror(errno
));
1048 abort_message
= "Stopped job because the scheduler could not execute a "
1054 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Started filter %s (PID %d)", command
,
1061 cupsArrayDelete(filters
);
1065 * Finally, pipe the final output into a backend process if needed...
1068 if (strncmp(job
->printer
->device_uri
, "file:", 5) != 0)
1070 if (job
->current_file
== 1 || job
->printer
->remote
)
1072 sscanf(job
->printer
->device_uri
, "%254[^:]", scheme
);
1073 snprintf(command
, sizeof(command
), "%s/backend/%s", ServerBin
, scheme
);
1076 * See if the backend needs to run as root...
1081 else if (stat(command
, &backinfo
))
1084 backroot
= !(backinfo
.st_mode
& (S_IRWXG
| S_IRWXO
));
1086 argv
[0] = job
->printer
->sanitized_device_uri
;
1088 filterfds
[slot
][0] = -1;
1089 filterfds
[slot
][1] = -1;
1091 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
1092 filterfds
[slot
][1], job
->status_pipes
[1],
1093 job
->back_pipes
[1], job
->side_pipes
[1],
1094 backroot
, job
->profile
, job
->id
, &(job
->backend
));
1098 abort_message
= "Stopping job because the sheduler could not execute "
1105 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Started backend %s (PID %d)",
1110 if (job
->current_file
== job
->num_files
)
1112 cupsdClosePipe(job
->print_pipes
);
1113 cupsdClosePipe(job
->back_pipes
);
1114 cupsdClosePipe(job
->side_pipes
);
1116 close(job
->status_pipes
[1]);
1117 job
->status_pipes
[1] = -1;
1122 filterfds
[slot
][0] = -1;
1123 filterfds
[slot
][1] = -1;
1125 if (job
->current_file
== job
->num_files
)
1127 cupsdClosePipe(job
->print_pipes
);
1129 close(job
->status_pipes
[1]);
1130 job
->status_pipes
[1] = -1;
1134 cupsdClosePipe(filterfds
[slot
]);
1136 if (job
->printer
->remote
&& job
->num_files
> 1)
1138 for (i
= 0; i
< job
->num_files
; i
++)
1144 cupsdAddSelect(job
->status_buffer
->fd
, (cupsd_selfunc_t
)update_job
, NULL
,
1147 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
, "Job #%d started.",
1154 * If we get here, we need to abort the current job and close out all
1155 * files and pipes...
1160 for (slot
= 0; slot
< 2; slot
++)
1161 cupsdClosePipe(filterfds
[slot
]);
1163 cupsdClosePipe(job
->status_pipes
);
1164 cupsdStatBufDelete(job
->status_buffer
);
1165 job
->status_buffer
= NULL
;
1167 cupsArrayDelete(filters
);
1169 if (job
->printer
->remote
&& job
->num_files
> 1)
1171 for (i
= 0; i
< job
->num_files
; i
++)
1177 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
, "%s",
1183 * 'cupsdDeleteJob()' - Free all memory used by a job.
1187 cupsdDeleteJob(cupsd_job_t
*job
, /* I - Job */
1188 cupsd_jobaction_t action
)/* I - Action */
1193 if (action
== CUPSD_JOB_PURGE
)
1196 * Remove the job info file...
1199 char filename
[1024]; /* Job filename */
1201 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
,
1206 cupsdClearString(&job
->username
);
1207 cupsdClearString(&job
->dest
);
1208 cupsdClearString(&job
->auth_username
);
1209 cupsdClearString(&job
->auth_domain
);
1210 cupsdClearString(&job
->auth_password
);
1214 * Destroy the credential cache and clear the KRB5CCNAME env var string.
1219 krb5_cc_destroy(KerberosContext
, job
->ccache
);
1223 cupsdClearString(&job
->ccname
);
1224 #endif /* HAVE_GSSAPI */
1226 if (job
->num_files
> 0)
1228 free(job
->compressions
);
1229 free(job
->filetypes
);
1236 cupsArrayRemove(Jobs
, job
);
1237 cupsArrayRemove(ActiveJobs
, job
);
1238 cupsArrayRemove(PrintingJobs
, job
);
1245 * 'cupsdFreeAllJobs()' - Free all jobs from memory.
1249 cupsdFreeAllJobs(void)
1251 cupsd_job_t
*job
; /* Current job */
1259 cupsdStopAllJobs(1);
1262 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1264 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1265 cupsdDeleteJob(job
, CUPSD_JOB_DEFAULT
);
1267 cupsdReleaseSignals();
1272 * 'cupsdFindJob()' - Find the specified job.
1275 cupsd_job_t
* /* O - Job data */
1276 cupsdFindJob(int id
) /* I - Job ID */
1278 cupsd_job_t key
; /* Search key */
1283 return ((cupsd_job_t
*)cupsArrayFind(Jobs
, &key
));
1288 * 'cupsdGetPrinterJobCount()' - Get the number of pending, processing,
1289 * or held jobs in a printer or class.
1292 int /* O - Job count */
1293 cupsdGetPrinterJobCount(
1294 const char *dest
) /* I - Printer or class name */
1296 int count
; /* Job count */
1297 cupsd_job_t
*job
; /* Current job */
1300 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
1302 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
1303 if (job
->dest
&& !strcasecmp(job
->dest
, dest
))
1311 * 'cupsdGetUserJobCount()' - Get the number of pending, processing,
1312 * or held jobs for a user.
1315 int /* O - Job count */
1316 cupsdGetUserJobCount(
1317 const char *username
) /* I - Username */
1319 int count
; /* Job count */
1320 cupsd_job_t
*job
; /* Current job */
1323 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
1325 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
1326 if (!strcasecmp(job
->username
, username
))
1334 * 'cupsdLoadAllJobs()' - Load all jobs from disk.
1338 cupsdLoadAllJobs(void)
1340 char filename
[1024]; /* Full filename of job.cache file */
1341 struct stat fileinfo
, /* Information on job.cache file */
1342 dirinfo
; /* Information on RequestRoot dir */
1347 * Create the job arrays as needed...
1351 Jobs
= cupsArrayNew(compare_jobs
, NULL
);
1354 ActiveJobs
= cupsArrayNew(compare_active_jobs
, NULL
);
1357 PrintingJobs
= cupsArrayNew(compare_jobs
, NULL
);
1360 * See whether the job.cache file is older than the RequestRoot directory...
1363 snprintf(filename
, sizeof(filename
), "%s/job.cache", CacheDir
);
1365 if (stat(filename
, &fileinfo
))
1367 fileinfo
.st_mtime
= 0;
1369 if (errno
!= ENOENT
)
1370 cupsdLogMessage(CUPSD_LOG_ERROR
,
1371 "Unable to get file information for \"%s\" - %s",
1372 filename
, strerror(errno
));
1375 if (stat(RequestRoot
, &dirinfo
))
1377 dirinfo
.st_mtime
= 0;
1379 if (errno
!= ENOENT
)
1380 cupsdLogMessage(CUPSD_LOG_ERROR
,
1381 "Unable to get directory information for \"%s\" - %s",
1382 RequestRoot
, strerror(errno
));
1386 * Load the most recent source for job data...
1389 if (dirinfo
.st_mtime
> fileinfo
.st_mtime
)
1391 load_request_root();
1393 load_next_job_id(filename
);
1396 load_job_cache(filename
);
1399 * Clean out old jobs as needed...
1402 if (MaxJobs
> 0 && cupsArrayCount(Jobs
) >= MaxJobs
)
1408 * 'cupsdLoadJob()' - Load a single job.
1411 int /* O - 1 on success, 0 on failure */
1412 cupsdLoadJob(cupsd_job_t
*job
) /* I - Job */
1414 char jobfile
[1024]; /* Job filename */
1415 cups_file_t
*fp
; /* Job file */
1416 int fileid
; /* Current file ID */
1417 ipp_attribute_t
*attr
; /* Job attribute */
1418 const char *dest
; /* Destination name */
1419 cupsd_printer_t
*destptr
; /* Pointer to destination */
1420 mime_type_t
**filetypes
; /* New filetypes array */
1421 int *compressions
; /* New compressions array */
1426 if (job
->state_value
> IPP_JOB_STOPPED
)
1427 job
->access_time
= time(NULL
);
1432 if ((job
->attrs
= ippNew()) == NULL
)
1434 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Ran out of memory for job attributes!");
1439 * Load job attributes...
1442 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Loading attributes...", job
->id
);
1444 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, job
->id
);
1445 if ((fp
= cupsFileOpen(jobfile
, "r")) == NULL
)
1447 cupsdLogMessage(CUPSD_LOG_ERROR
,
1448 "[Job %d] Unable to open job control file \"%s\" - %s!",
1449 job
->id
, jobfile
, strerror(errno
));
1453 if (ippReadIO(fp
, (ipp_iocb_t
)cupsFileRead
, 1, NULL
, job
->attrs
) != IPP_DATA
)
1455 cupsdLogMessage(CUPSD_LOG_ERROR
,
1456 "[Job %d] Unable to read job control file \"%s\"!", job
->id
,
1465 * Copy attribute data to the job object...
1468 if (!ippFindAttribute(job
->attrs
, "time-at-creation", IPP_TAG_INTEGER
))
1470 cupsdLogMessage(CUPSD_LOG_ERROR
,
1471 "[Job %d] Missing or bad time-at-creation attribute in "
1472 "control file!", job
->id
);
1476 if ((job
->state
= ippFindAttribute(job
->attrs
, "job-state",
1477 IPP_TAG_ENUM
)) == NULL
)
1479 cupsdLogMessage(CUPSD_LOG_ERROR
,
1480 "[Job %d] Missing or bad job-state attribute in control "
1485 job
->state_value
= (ipp_jstate_t
)job
->state
->values
[0].integer
;
1489 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1490 IPP_TAG_URI
)) == NULL
)
1492 cupsdLogMessage(CUPSD_LOG_ERROR
,
1493 "[Job %d] No job-printer-uri attribute in control file!",
1498 if ((dest
= cupsdValidateDest(attr
->values
[0].string
.text
, &(job
->dtype
),
1501 cupsdLogMessage(CUPSD_LOG_ERROR
,
1502 "[Job %d] Unable to queue job for destination \"%s\"!",
1503 job
->id
, attr
->values
[0].string
.text
);
1507 cupsdSetString(&job
->dest
, dest
);
1509 else if ((destptr
= cupsdFindDest(job
->dest
)) == NULL
)
1511 cupsdLogMessage(CUPSD_LOG_ERROR
,
1512 "[Job %d] Unable to queue job for destination \"%s\"!",
1513 job
->id
, job
->dest
);
1517 job
->sheets
= ippFindAttribute(job
->attrs
, "job-media-sheets-completed",
1519 job
->job_sheets
= ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_NAME
);
1523 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
1524 IPP_TAG_INTEGER
)) == NULL
)
1526 cupsdLogMessage(CUPSD_LOG_ERROR
,
1527 "[Job %d] Missing or bad job-priority attribute in "
1528 "control file!", job
->id
);
1532 job
->priority
= attr
->values
[0].integer
;
1537 if ((attr
= ippFindAttribute(job
->attrs
, "job-originating-user-name",
1538 IPP_TAG_NAME
)) == NULL
)
1540 cupsdLogMessage(CUPSD_LOG_ERROR
,
1541 "[Job %d] Missing or bad job-originating-user-name "
1542 "attribute in control file!", job
->id
);
1546 cupsdSetString(&job
->username
, attr
->values
[0].string
.text
);
1550 * Set the job hold-until time and state...
1553 if (job
->state_value
== IPP_JOB_HELD
)
1555 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1556 IPP_TAG_KEYWORD
)) == NULL
)
1557 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1560 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
, CUPSD_JOB_DEFAULT
);
1563 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1564 job
->state_value
= IPP_JOB_PENDING
;
1567 else if (job
->state_value
== IPP_JOB_PROCESSING
)
1569 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1570 job
->state_value
= IPP_JOB_PENDING
;
1573 if (!job
->num_files
)
1576 * Find all the d##### files...
1579 for (fileid
= 1; fileid
< 10000; fileid
++)
1581 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
1584 if (access(jobfile
, 0))
1587 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1588 "[Job %d] Auto-typing document file \"%s\"...", job
->id
,
1591 if (fileid
> job
->num_files
)
1593 if (job
->num_files
== 0)
1595 compressions
= (int *)calloc(fileid
, sizeof(int));
1596 filetypes
= (mime_type_t
**)calloc(fileid
, sizeof(mime_type_t
*));
1600 compressions
= (int *)realloc(job
->compressions
,
1601 sizeof(int) * fileid
);
1602 filetypes
= (mime_type_t
**)realloc(job
->filetypes
,
1603 sizeof(mime_type_t
*) *
1607 if (!compressions
|| !filetypes
)
1609 cupsdLogMessage(CUPSD_LOG_ERROR
,
1610 "[Job %d] Ran out of memory for job file types!",
1615 job
->compressions
= compressions
;
1616 job
->filetypes
= filetypes
;
1617 job
->num_files
= fileid
;
1620 job
->filetypes
[fileid
- 1] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
1621 job
->compressions
+ fileid
- 1);
1623 if (!job
->filetypes
[fileid
- 1])
1624 job
->filetypes
[fileid
- 1] = mimeType(MimeDatabase
, "application",
1630 * Load authentication information as needed...
1633 if (job
->state_value
< IPP_JOB_STOPPED
)
1635 snprintf(jobfile
, sizeof(jobfile
), "%s/a%05d", RequestRoot
, job
->id
);
1637 cupsdClearString(&job
->auth_username
);
1638 cupsdClearString(&job
->auth_domain
);
1639 cupsdClearString(&job
->auth_password
);
1641 if ((fp
= cupsFileOpen(jobfile
, "r")) != NULL
)
1643 int i
, /* Looping var */
1644 bytes
; /* Size of auth data */
1645 char line
[255], /* Line from file */
1646 data
[255]; /* Decoded data */
1650 i
< destptr
->num_auth_info_required
&&
1651 cupsFileGets(fp
, line
, sizeof(line
));
1654 bytes
= sizeof(data
);
1655 httpDecode64_2(data
, &bytes
, line
);
1657 if (!strcmp(destptr
->auth_info_required
[i
], "username"))
1658 cupsdSetStringf(&job
->auth_username
, "AUTH_USERNAME=%s", data
);
1659 else if (!strcmp(destptr
->auth_info_required
[i
], "domain"))
1660 cupsdSetStringf(&job
->auth_domain
, "AUTH_DOMAIN=%s", data
);
1661 else if (!strcmp(destptr
->auth_info_required
[i
], "password"))
1662 cupsdSetStringf(&job
->auth_password
, "AUTH_PASSWORD=%s", data
);
1669 job
->access_time
= time(NULL
);
1673 * If we get here then something bad happened...
1678 ippDelete(job
->attrs
);
1687 * 'cupsdMoveJob()' - Move the specified job to a different destination.
1691 cupsdMoveJob(cupsd_job_t
*job
, /* I - Job */
1692 cupsd_printer_t
*p
) /* I - Destination printer or class */
1694 ipp_attribute_t
*attr
; /* job-printer-uri attribute */
1695 const char *olddest
; /* Old destination */
1696 cupsd_printer_t
*oldp
; /* Old pointer */
1700 * Don't move completed jobs...
1703 if (job
->state_value
> IPP_JOB_STOPPED
)
1707 * Get the old destination...
1710 olddest
= job
->dest
;
1713 oldp
= job
->printer
;
1715 oldp
= cupsdFindDest(olddest
);
1718 * Change the destination information...
1721 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
1722 "Stopping job prior to move.");
1724 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, oldp
, job
,
1725 "Job #%d moved from %s to %s.", job
->id
, olddest
,
1728 cupsdSetString(&job
->dest
, p
->name
);
1729 job
->dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_REMOTE
|
1730 CUPS_PRINTER_IMPLICIT
);
1732 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1733 IPP_TAG_URI
)) != NULL
)
1734 cupsdSetString(&(attr
->values
[0].string
.text
), p
->uri
);
1736 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, p
, job
,
1737 "Job #%d moved from %s to %s.", job
->id
, olddest
,
1741 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1746 * 'cupsdReleaseJob()' - Release the specified job.
1750 cupsdReleaseJob(cupsd_job_t
*job
) /* I - Job */
1752 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdReleaseJob(job=%p(%d))", job
,
1755 if (job
->state_value
== IPP_JOB_HELD
)
1758 * Add trailing banner as needed...
1761 if (job
->pending_timeout
)
1762 cupsdTimeoutJob(job
);
1764 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
1765 "Job released by user.");
1771 * 'cupsdRestartJob()' - Restart the specified job.
1775 cupsdRestartJob(cupsd_job_t
*job
) /* I - Job */
1777 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdRestartJob(job=%p(%d))", job
,
1780 if (job
->state_value
== IPP_JOB_STOPPED
|| job
->num_files
)
1781 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
1782 "Job restarted by user.");
1787 * 'cupsdSaveAllJobs()' - Save a summary of all jobs to disk.
1791 cupsdSaveAllJobs(void)
1793 int i
; /* Looping var */
1794 cups_file_t
*fp
; /* Job cache file */
1795 char temp
[1024]; /* Temporary string */
1796 cupsd_job_t
*job
; /* Current job */
1797 time_t curtime
; /* Current time */
1798 struct tm
*curdate
; /* Current date */
1801 snprintf(temp
, sizeof(temp
), "%s/job.cache", CacheDir
);
1802 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
1804 cupsdLogMessage(CUPSD_LOG_ERROR
,
1805 "Unable to create job cache file \"%s\" - %s",
1806 temp
, strerror(errno
));
1810 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving job cache file \"%s\"...", temp
);
1813 * Restrict access to the file...
1816 fchown(cupsFileNumber(fp
), getuid(), Group
);
1817 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
1820 * Write a small header to the file...
1823 curtime
= time(NULL
);
1824 curdate
= localtime(&curtime
);
1825 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
1827 cupsFilePuts(fp
, "# Job cache file for " CUPS_SVERSION
"\n");
1828 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
1829 cupsFilePrintf(fp
, "NextJobId %d\n", NextJobId
);
1832 * Write each job known to the system...
1835 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1837 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1839 cupsFilePrintf(fp
, "<Job %d>\n", job
->id
);
1840 cupsFilePrintf(fp
, "State %d\n", job
->state_value
);
1841 cupsFilePrintf(fp
, "Priority %d\n", job
->priority
);
1842 cupsFilePrintf(fp
, "Username %s\n", job
->username
);
1843 cupsFilePrintf(fp
, "Destination %s\n", job
->dest
);
1844 cupsFilePrintf(fp
, "DestType %d\n", job
->dtype
);
1845 cupsFilePrintf(fp
, "NumFiles %d\n", job
->num_files
);
1846 for (i
= 0; i
< job
->num_files
; i
++)
1847 cupsFilePrintf(fp
, "File %d %s/%s %d\n", i
+ 1, job
->filetypes
[i
]->super
,
1848 job
->filetypes
[i
]->type
, job
->compressions
[i
]);
1849 cupsFilePuts(fp
, "</Job>\n");
1857 * 'cupsdSaveJob()' - Save a job to disk.
1861 cupsdSaveJob(cupsd_job_t
*job
) /* I - Job */
1863 char filename
[1024]; /* Job control filename */
1864 cups_file_t
*fp
; /* Job file */
1867 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSaveJob(job=%p(%d)): job->attrs=%p",
1868 job
, job
->id
, job
->attrs
);
1870 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
, job
->id
);
1872 if ((fp
= cupsFileOpen(filename
, "w")) == NULL
)
1874 cupsdLogMessage(CUPSD_LOG_ERROR
,
1875 "[Job %d] Unable to create job control file \"%s\" - %s.",
1876 job
->id
, filename
, strerror(errno
));
1880 fchmod(cupsFileNumber(fp
), 0600);
1881 fchown(cupsFileNumber(fp
), RunUser
, Group
);
1883 job
->attrs
->state
= IPP_IDLE
;
1885 if (ippWriteIO(fp
, (ipp_iocb_t
)cupsFileWrite
, 1, NULL
,
1886 job
->attrs
) != IPP_DATA
)
1887 cupsdLogMessage(CUPSD_LOG_ERROR
,
1888 "[Job %d] Unable to write job control file!", job
->id
);
1897 * 'cupsdSetJobHoldUntil()' - Set the hold time for a job.
1901 cupsdSetJobHoldUntil(cupsd_job_t
*job
, /* I - Job */
1902 const char *when
, /* I - When to resume */
1903 int update
)/* I - Update job-hold-until attr? */
1905 time_t curtime
; /* Current time */
1906 struct tm
*curdate
; /* Current date */
1907 int hour
; /* Hold hour */
1908 int minute
; /* Hold minute */
1909 int second
= 0; /* Hold second */
1912 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1913 "cupsdSetJobHoldUntil(job=%p(%d), when=\"%s\", update=%d)",
1914 job
, job
->id
, when
, update
);
1919 * Update the job-hold-until attribute...
1922 ipp_attribute_t
*attr
; /* job-hold-until attribute */
1924 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1925 IPP_TAG_KEYWORD
)) == NULL
)
1926 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1929 cupsdSetString(&(attr
->values
[0].string
.text
), when
);
1931 attr
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1932 "job-hold-until", NULL
, when
);
1936 if (isdigit(when
[0] & 255))
1937 attr
->value_tag
= IPP_TAG_NAME
;
1939 attr
->value_tag
= IPP_TAG_KEYWORD
;
1942 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1947 * Update the hold time...
1950 if (!strcmp(when
, "indefinite") || !strcmp(when
, "auth-info-required"))
1953 * Hold indefinitely...
1956 job
->hold_until
= 0;
1958 else if (!strcmp(when
, "day-time"))
1961 * Hold to 6am the next morning unless local time is < 6pm.
1964 curtime
= time(NULL
);
1965 curdate
= localtime(&curtime
);
1967 if (curdate
->tm_hour
< 18)
1968 job
->hold_until
= curtime
;
1970 job
->hold_until
= curtime
+
1971 ((29 - curdate
->tm_hour
) * 60 + 59 -
1972 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1974 else if (!strcmp(when
, "evening") || !strcmp(when
, "night"))
1977 * Hold to 6pm unless local time is > 6pm or < 6am.
1980 curtime
= time(NULL
);
1981 curdate
= localtime(&curtime
);
1983 if (curdate
->tm_hour
< 6 || curdate
->tm_hour
>= 18)
1984 job
->hold_until
= curtime
;
1986 job
->hold_until
= curtime
+
1987 ((17 - curdate
->tm_hour
) * 60 + 59 -
1988 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1990 else if (!strcmp(when
, "second-shift"))
1993 * Hold to 4pm unless local time is > 4pm.
1996 curtime
= time(NULL
);
1997 curdate
= localtime(&curtime
);
1999 if (curdate
->tm_hour
>= 16)
2000 job
->hold_until
= curtime
;
2002 job
->hold_until
= curtime
+
2003 ((15 - curdate
->tm_hour
) * 60 + 59 -
2004 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2006 else if (!strcmp(when
, "third-shift"))
2009 * Hold to 12am unless local time is < 8am.
2012 curtime
= time(NULL
);
2013 curdate
= localtime(&curtime
);
2015 if (curdate
->tm_hour
< 8)
2016 job
->hold_until
= curtime
;
2018 job
->hold_until
= curtime
+
2019 ((23 - curdate
->tm_hour
) * 60 + 59 -
2020 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2022 else if (!strcmp(when
, "weekend"))
2025 * Hold to weekend unless we are in the weekend.
2028 curtime
= time(NULL
);
2029 curdate
= localtime(&curtime
);
2031 if (curdate
->tm_wday
== 0 || curdate
->tm_wday
== 6)
2032 job
->hold_until
= curtime
;
2034 job
->hold_until
= curtime
+
2035 (((5 - curdate
->tm_wday
) * 24 +
2036 (17 - curdate
->tm_hour
)) * 60 + 59 -
2037 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2039 else if (sscanf(when
, "%d:%d:%d", &hour
, &minute
, &second
) >= 2)
2042 * Hold to specified GMT time (HH:MM or HH:MM:SS)...
2045 curtime
= time(NULL
);
2046 curdate
= gmtime(&curtime
);
2048 job
->hold_until
= curtime
+
2049 ((hour
- curdate
->tm_hour
) * 60 + minute
-
2050 curdate
->tm_min
) * 60 + second
- curdate
->tm_sec
;
2053 * Hold until next day as needed...
2056 if (job
->hold_until
< curtime
)
2057 job
->hold_until
+= 24 * 60 * 60;
2060 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetJobHoldUntil: hold_until=%d",
2061 (int)job
->hold_until
);
2066 * 'cupsdSetJobPriority()' - Set the priority of a job, moving it up/down in
2067 * the list as needed.
2071 cupsdSetJobPriority(
2072 cupsd_job_t
*job
, /* I - Job ID */
2073 int priority
) /* I - New priority (0 to 100) */
2075 ipp_attribute_t
*attr
; /* Job attribute */
2079 * Don't change completed jobs...
2082 if (job
->state_value
>= IPP_JOB_PROCESSING
)
2086 * Set the new priority and re-add the job into the active list...
2089 cupsArrayRemove(ActiveJobs
, job
);
2091 job
->priority
= priority
;
2093 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
2094 IPP_TAG_INTEGER
)) != NULL
)
2095 attr
->values
[0].integer
= priority
;
2097 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-priority",
2100 cupsArrayAdd(ActiveJobs
, job
);
2103 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2108 * 'cupsdSetJobState()' - Set the state of the specified print job.
2113 cupsd_job_t
*job
, /* I - Job to cancel */
2114 ipp_jstate_t newstate
, /* I - New job state */
2115 cupsd_jobaction_t action
, /* I - Action to take */
2116 const char *message
, /* I - Message to log */
2117 ...) /* I - Additional arguments as needed */
2119 int i
; /* Looping var */
2120 ipp_jstate_t oldstate
; /* Old state */
2121 char filename
[1024]; /* Job filename */
2122 ipp_attribute_t
*attr
; /* Job attribute */
2125 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2126 "cupsdSetJobState(job=%p(%d), state=%d, newstate=%d, "
2127 "action=%d, message=\"%s\")", job
, job
->id
, job
->state_value
,
2128 newstate
, action
, message
? message
: "(null)");
2132 * Make sure we have the job attributes...
2135 if (!cupsdLoadJob(job
))
2139 * Don't do anything if the state is unchanged...
2142 if (newstate
== (oldstate
= job
->state_value
))
2146 * Stop any processes that are working on the current job...
2149 if (oldstate
== IPP_JOB_PROCESSING
)
2150 stop_job(job
, action
!= CUPSD_JOB_DEFAULT
);
2153 * Set the new job state...
2156 job
->state
->values
[0].integer
= newstate
;
2157 job
->state_value
= newstate
;
2161 case IPP_JOB_PENDING
:
2163 * Update job-hold-until as needed...
2166 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2167 IPP_TAG_KEYWORD
)) == NULL
)
2168 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2172 attr
->value_tag
= IPP_TAG_KEYWORD
;
2173 cupsdSetString(&(attr
->values
[0].string
.text
), "no-hold");
2179 case IPP_JOB_ABORTED
:
2180 case IPP_JOB_CANCELED
:
2181 case IPP_JOB_COMPLETED
:
2182 set_time(job
, "time-at-completed");
2187 * Log message as needed...
2192 char buffer
[2048]; /* Message buffer */
2193 va_list ap
; /* Pointer to additional arguments */
2195 va_start(ap
, message
);
2196 vsnprintf(buffer
, sizeof(buffer
), message
, ap
);
2199 if (newstate
> IPP_JOB_STOPPED
)
2200 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
, "%s", buffer
);
2202 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
, "%s", buffer
);
2204 if (newstate
== IPP_JOB_STOPPED
|| newstate
== IPP_JOB_ABORTED
)
2205 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "%s", buffer
);
2207 cupsdLogJob(job
, CUPSD_LOG_INFO
, "%s", buffer
);
2211 * Handle post-state-change actions...
2216 case IPP_JOB_PROCESSING
:
2218 * Add the job to the "printing" list...
2221 if (!cupsArrayFind(PrintingJobs
, job
))
2222 cupsArrayAdd(PrintingJobs
, job
);
2225 * Set the processing time...
2228 set_time(job
, "time-at-processing");
2230 case IPP_JOB_PENDING
:
2232 case IPP_JOB_STOPPED
:
2234 * Make sure the job is in the active list...
2237 if (!cupsArrayFind(ActiveJobs
, job
))
2238 cupsArrayAdd(ActiveJobs
, job
);
2241 * Save the job state to disk...
2245 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2248 case IPP_JOB_ABORTED
:
2249 case IPP_JOB_CANCELED
:
2250 case IPP_JOB_COMPLETED
:
2252 * Expire job subscriptions since the job is now "completed"...
2255 cupsdExpireSubscriptions(NULL
, job
);
2258 * Remove the job from the active list...
2261 cupsArrayRemove(ActiveJobs
, job
);
2265 * If we are going to sleep and the PrintingJobs count is now 0, allow the
2266 * sleep to happen immediately...
2269 if (Sleeping
&& cupsArrayCount(PrintingJobs
) == 0)
2271 #endif /* __APPLE__ */
2274 * Remove any authentication data...
2277 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
2278 if (cupsdRemoveFile(filename
) && errno
!= ENOENT
)
2279 cupsdLogMessage(CUPSD_LOG_ERROR
,
2280 "Unable to remove authentication cache: %s",
2283 cupsdClearString(&job
->auth_username
);
2284 cupsdClearString(&job
->auth_domain
);
2285 cupsdClearString(&job
->auth_password
);
2289 * Destroy the credential cache and clear the KRB5CCNAME env var string.
2294 krb5_cc_destroy(KerberosContext
, job
->ccache
);
2298 cupsdClearString(&job
->ccname
);
2299 #endif /* HAVE_GSSAPI */
2302 * Remove the print file for good if we aren't preserving jobs or
2306 if (!JobHistory
|| !JobFiles
|| action
== CUPSD_JOB_PURGE
)
2308 for (i
= 1; i
<= job
->num_files
; i
++)
2310 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
2315 if (job
->num_files
> 0)
2317 free(job
->filetypes
);
2318 free(job
->compressions
);
2321 job
->filetypes
= NULL
;
2322 job
->compressions
= NULL
;
2326 if (JobHistory
&& action
!= CUPSD_JOB_PURGE
)
2329 * Save job state info...
2333 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2335 else if (!job
->printer
)
2336 cupsdDeleteJob(job
, CUPSD_JOB_PURGE
);
2341 * Update the server "busy" state...
2344 cupsdSetBusyState();
2349 * 'cupsdStopAllJobs()' - Stop all print jobs.
2354 cupsd_jobaction_t action
) /* I - Action */
2356 cupsd_job_t
*job
; /* Current job */
2359 DEBUG_puts("cupsdStopAllJobs()");
2361 for (job
= (cupsd_job_t
*)cupsArrayFirst(PrintingJobs
);
2363 job
= (cupsd_job_t
*)cupsArrayNext(PrintingJobs
))
2364 cupsdSetJobState(job
, IPP_JOB_PENDING
, action
, NULL
);
2369 * 'cupsdUnloadCompletedJobs()' - Flush completed job history from memory.
2373 cupsdUnloadCompletedJobs(void)
2375 cupsd_job_t
*job
; /* Current job */
2376 time_t expire
; /* Expiration time */
2379 expire
= time(NULL
) - 60;
2381 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
2383 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
2384 if (job
->attrs
&& job
->state_value
>= IPP_JOB_STOPPED
&& !job
->printer
&&
2385 job
->access_time
< expire
)
2396 * 'compare_active_jobs()' - Compare the job IDs and priorities of two jobs.
2399 static int /* O - Difference */
2400 compare_active_jobs(void *first
, /* I - First job */
2401 void *second
, /* I - Second job */
2402 void *data
) /* I - App data (not used) */
2404 int diff
; /* Difference */
2407 if ((diff
= ((cupsd_job_t
*)second
)->priority
-
2408 ((cupsd_job_t
*)first
)->priority
) != 0)
2411 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
2416 * 'compare_jobs()' - Compare the job IDs of two jobs.
2419 static int /* O - Difference */
2420 compare_jobs(void *first
, /* I - First job */
2421 void *second
, /* I - Second job */
2422 void *data
) /* I - App data (not used) */
2424 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
2429 * 'finalize_job()' - Cleanup after job filter processes and support data.
2433 finalize_job(cupsd_job_t
*job
) /* I - Job */
2435 ipp_pstate_t printer_state
; /* New printer state value */
2436 ipp_jstate_t job_state
; /* New job state value */
2437 const char *message
; /* Message for job state */
2438 char buffer
[1024]; /* Buffer for formatted messages */
2441 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "finalize_job(job=%p(%d))", job
, job
->id
);
2444 * Clear the "connecting-to-device" reason, which is only valid when a
2445 * printer is processing...
2448 cupsdSetPrinterReasons(job
->printer
, "-connecting-to-device");
2451 * Similarly, clear the "offline-report" reason for non-USB devices since we
2452 * rarely have current information for network devices...
2455 if (strncmp(job
->printer
->device_uri
, "usb:", 4))
2456 cupsdSetPrinterReasons(job
->printer
, "-offline-report");
2459 * Free the security profile...
2462 cupsdDestroyProfile(job
->profile
);
2463 job
->profile
= NULL
;
2466 * Close pipes and status buffer...
2469 cupsdRemoveSelect(job
->status_buffer
->fd
);
2471 cupsdClosePipe(job
->print_pipes
);
2472 cupsdClosePipe(job
->back_pipes
);
2473 cupsdClosePipe(job
->side_pipes
);
2474 cupsdClosePipe(job
->status_pipes
);
2476 cupsdStatBufDelete(job
->status_buffer
);
2477 job
->status_buffer
= NULL
;
2480 * Process the exit status...
2483 if (job
->printer
->state
== IPP_PRINTER_PROCESSING
)
2484 printer_state
= IPP_PRINTER_IDLE
;
2486 printer_state
= job
->printer
->state
;
2488 switch (job_state
= job
->state_value
)
2490 case IPP_JOB_PENDING
:
2491 message
= "Job paused.";
2495 message
= "Job held.";
2499 case IPP_JOB_PROCESSING
:
2500 case IPP_JOB_COMPLETED
:
2501 job_state
= IPP_JOB_COMPLETED
;
2502 message
= "Job completed.";
2505 case IPP_JOB_STOPPED
:
2506 message
= "Job stopped.";
2509 case IPP_JOB_CANCELED
:
2510 message
= "Job canceled.";
2513 case IPP_JOB_ABORTED
:
2514 message
= "Job aborted.";
2518 if (job
->status
< 0)
2521 * Backend had errors...
2524 int exit_code
; /* Exit code from backend */
2528 * Convert the status to an exit code. Due to the way the W* macros are
2529 * implemented on MacOS X (bug?), we have to store the exit status in a
2530 * variable first and then convert...
2533 exit_code
= -job
->status
;
2534 if (WIFEXITED(exit_code
))
2535 exit_code
= WEXITSTATUS(exit_code
);
2537 exit_code
= job
->status
;
2539 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Backend returned status %d (%s)",
2541 exit_code
== CUPS_BACKEND_FAILED
? "failed" :
2542 exit_code
== CUPS_BACKEND_AUTH_REQUIRED
?
2543 "authentication required" :
2544 exit_code
== CUPS_BACKEND_HOLD
? "hold job" :
2545 exit_code
== CUPS_BACKEND_STOP
? "stop printer" :
2546 exit_code
== CUPS_BACKEND_CANCEL
? "cancel job" :
2547 exit_code
< 0 ? "crashed" : "unknown");
2550 * Do what needs to be done...
2556 case CUPS_BACKEND_FAILED
:
2558 * Backend failure, use the error-policy to determine how to
2562 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
2565 * Queued on a class - mark the job as pending and we'll retry on
2566 * another printer...
2569 job_state
= IPP_JOB_PENDING
;
2570 message
= "Retrying job on another printer.";
2572 else if (!strcmp(job
->printer
->error_policy
, "retry-current-job"))
2575 * The error policy is "retry-current-job" - mark the job as pending
2576 * and we'll retry on the same printer...
2579 job_state
= IPP_JOB_PENDING
;
2580 message
= "Retrying job on same printer.";
2582 else if ((job
->printer
->type
& CUPS_PRINTER_FAX
) ||
2583 !strcmp(job
->printer
->error_policy
, "retry-job"))
2586 * The job was queued on a fax or the error policy is "retry-job" -
2587 * hold the job if the number of retries is less than the
2588 * JobRetryLimit, otherwise abort the job.
2593 if (job
->tries
>= JobRetryLimit
)
2599 snprintf(buffer
, sizeof(buffer
),
2600 "Job aborted after %d unsuccessful attempts.",
2602 job_state
= IPP_JOB_ABORTED
;
2608 * Try again in N seconds...
2611 set_hold_until(job
, time(NULL
) + JobRetryInterval
);
2613 snprintf(buffer
, sizeof(buffer
),
2614 "Job held for %d seconds since it could not be sent.",
2616 job_state
= IPP_JOB_HELD
;
2620 else if (!strcmp(job
->printer
->error_policy
, "abort-job"))
2622 job_state
= IPP_JOB_ABORTED
;
2623 message
= "Job aborted due to backend errors; please consult "
2624 "the error_log file for details.";
2628 printer_state
= IPP_PRINTER_STOPPED
;
2629 job_state
= IPP_JOB_PENDING
;
2630 message
= "Printer stopped due to backend errors; please "
2631 "consult the error_log file for details.";
2635 case CUPS_BACKEND_CANCEL
:
2640 job_state
= IPP_JOB_ABORTED
;
2641 message
= "Job aborted due to backend errors; please consult "
2642 "the error_log file for details.";
2645 case CUPS_BACKEND_HOLD
:
2650 cupsdSetJobHoldUntil(job
, "indefinite", 1);
2652 job_state
= IPP_JOB_HELD
;
2653 message
= "Job held indefinitely due to backend errors; please "
2654 "consult the error_log file for details.";
2657 case CUPS_BACKEND_STOP
:
2659 * Stop the printer...
2662 printer_state
= IPP_PRINTER_STOPPED
;
2663 job_state
= IPP_JOB_PENDING
;
2664 message
= "Printer stopped due to backend errors; please "
2665 "consult the error_log file for details.";
2668 case CUPS_BACKEND_AUTH_REQUIRED
:
2670 * Hold the job for authentication...
2673 cupsdSetJobHoldUntil(job
, "auth-info-required", 1);
2675 job_state
= IPP_JOB_HELD
;
2676 message
= "Job held for authentication.";
2680 else if (job
->status
> 0)
2683 * Filter had errors; stop job...
2686 job_state
= IPP_JOB_STOPPED
;
2687 message
= "Job stopped due to filter errors; please consult the "
2688 "error_log file for details.";
2692 * Update the printer and job state.
2695 cupsdSetJobState(job
, job_state
, CUPSD_JOB_DEFAULT
, "%s", message
);
2696 cupsdSetPrinterState(job
->printer
, printer_state
,
2697 printer_state
== IPP_PRINTER_STOPPED
);
2698 update_job_attrs(job
, 0);
2700 cupsArrayRemove(PrintingJobs
, job
);
2703 * Clear the printer <-> job association...
2706 job
->printer
->job
= NULL
;
2707 job
->printer
= NULL
;
2710 * Try printing another job...
2713 if (printer_state
!= IPP_PRINTER_STOPPED
)
2719 * 'get_options()' - Get a string containing the job options.
2722 static char * /* O - Options string */
2723 get_options(cupsd_job_t
*job
, /* I - Job */
2724 int banner_page
, /* I - Printing a banner page? */
2725 char *copies
, /* I - Copies buffer */
2726 size_t copies_size
, /* I - Size of copies buffer */
2727 char *title
, /* I - Title buffer */
2728 size_t title_size
) /* I - Size of title buffer */
2730 int i
; /* Looping var */
2731 char *optptr
, /* Pointer to options */
2732 *valptr
; /* Pointer in value string */
2733 ipp_attribute_t
*attr
; /* Current attribute */
2734 static char *options
= NULL
;/* Full list of options */
2735 static int optlength
= 0; /* Length of option buffer */
2739 * Building the options string is harder than it needs to be, but
2740 * for the moment we need to pass strings for command-line args and
2741 * not IPP attribute pointers... :)
2743 * First allocate/reallocate the option buffer as needed...
2746 i
= ipp_length(job
->attrs
);
2748 if (i
> optlength
|| !options
)
2753 optptr
= realloc(options
, i
);
2757 cupsdLogJob(job
, CUPSD_LOG_CRIT
,
2758 "Unable to allocate %d bytes for option buffer!", i
);
2767 * Now loop through the attributes and convert them to the textual
2768 * representation used by the filters...
2774 snprintf(title
, title_size
, "%s-%d", job
->printer
->name
, job
->id
);
2775 strlcpy(copies
, "1", copies_size
);
2777 for (attr
= job
->attrs
->attrs
; attr
!= NULL
; attr
= attr
->next
)
2779 if (!strcmp(attr
->name
, "copies") &&
2780 attr
->value_tag
== IPP_TAG_INTEGER
)
2783 * Don't use the # copies attribute if we are printing the job sheets...
2787 snprintf(copies
, copies_size
, "%d", attr
->values
[0].integer
);
2789 else if (!strcmp(attr
->name
, "job-name") &&
2790 (attr
->value_tag
== IPP_TAG_NAME
||
2791 attr
->value_tag
== IPP_TAG_NAMELANG
))
2792 strlcpy(title
, attr
->values
[0].string
.text
, title_size
);
2793 else if (attr
->group_tag
== IPP_TAG_JOB
)
2796 * Filter out other unwanted attributes...
2799 if (attr
->value_tag
== IPP_TAG_MIMETYPE
||
2800 attr
->value_tag
== IPP_TAG_NAMELANG
||
2801 attr
->value_tag
== IPP_TAG_TEXTLANG
||
2802 (attr
->value_tag
== IPP_TAG_URI
&& strcmp(attr
->name
, "job-uuid")) ||
2803 attr
->value_tag
== IPP_TAG_URISCHEME
||
2804 attr
->value_tag
== IPP_TAG_BEGIN_COLLECTION
) /* Not yet supported */
2807 if (!strncmp(attr
->name
, "time-", 5))
2810 if (!strncmp(attr
->name
, "job-", 4) &&
2811 strcmp(attr
->name
, "job-billing") &&
2812 strcmp(attr
->name
, "job-impressions") &&
2813 strcmp(attr
->name
, "job-originating-host-name") &&
2814 strcmp(attr
->name
, "job-uuid") &&
2815 !(job
->printer
->type
& CUPS_PRINTER_REMOTE
))
2818 if ((!strcmp(attr
->name
, "job-impressions") ||
2819 !strcmp(attr
->name
, "page-label") ||
2820 !strcmp(attr
->name
, "page-border") ||
2821 !strncmp(attr
->name
, "number-up", 9) ||
2822 !strcmp(attr
->name
, "page-ranges") ||
2823 !strcmp(attr
->name
, "page-set") ||
2824 !strcasecmp(attr
->name
, "AP_FIRSTPAGE_InputSlot") ||
2825 !strcasecmp(attr
->name
, "AP_FIRSTPAGE_ManualFeed") ||
2826 !strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
2827 "PMTotalSidesImaged..n.") ||
2828 !strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
2829 "PMTotalBeginPages..n.")) &&
2834 * Otherwise add them to the list...
2837 if (optptr
> options
)
2838 strlcat(optptr
, " ", optlength
- (optptr
- options
));
2840 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
2842 strlcat(optptr
, attr
->name
, optlength
- (optptr
- options
));
2843 strlcat(optptr
, "=", optlength
- (optptr
- options
));
2846 for (i
= 0; i
< attr
->num_values
; i
++)
2849 strlcat(optptr
, ",", optlength
- (optptr
- options
));
2851 optptr
+= strlen(optptr
);
2853 switch (attr
->value_tag
)
2855 case IPP_TAG_INTEGER
:
2857 snprintf(optptr
, optlength
- (optptr
- options
),
2858 "%d", attr
->values
[i
].integer
);
2861 case IPP_TAG_BOOLEAN
:
2862 if (!attr
->values
[i
].boolean
)
2863 strlcat(optptr
, "no", optlength
- (optptr
- options
));
2865 case IPP_TAG_NOVALUE
:
2866 strlcat(optptr
, attr
->name
,
2867 optlength
- (optptr
- options
));
2870 case IPP_TAG_RANGE
:
2871 if (attr
->values
[i
].range
.lower
== attr
->values
[i
].range
.upper
)
2872 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2873 "%d", attr
->values
[i
].range
.lower
);
2875 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2876 "%d-%d", attr
->values
[i
].range
.lower
,
2877 attr
->values
[i
].range
.upper
);
2880 case IPP_TAG_RESOLUTION
:
2881 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2882 "%dx%d%s", attr
->values
[i
].resolution
.xres
,
2883 attr
->values
[i
].resolution
.yres
,
2884 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
2888 case IPP_TAG_STRING
:
2891 case IPP_TAG_KEYWORD
:
2892 case IPP_TAG_CHARSET
:
2893 case IPP_TAG_LANGUAGE
:
2895 for (valptr
= attr
->values
[i
].string
.text
; *valptr
;)
2897 if (strchr(" \t\n\\\'\"", *valptr
))
2899 *optptr
++ = *valptr
++;
2906 break; /* anti-compiler-warning-code */
2910 optptr
+= strlen(optptr
);
2920 * 'ipp_length()' - Compute the size of the buffer needed to hold
2921 * the textual IPP attributes.
2924 static int /* O - Size of attribute buffer */
2925 ipp_length(ipp_t
*ipp
) /* I - IPP request */
2927 int bytes
; /* Number of bytes */
2928 int i
; /* Looping var */
2929 ipp_attribute_t
*attr
; /* Current attribute */
2933 * Loop through all attributes...
2938 for (attr
= ipp
->attrs
; attr
!= NULL
; attr
= attr
->next
)
2941 * Skip attributes that won't be sent to filters...
2944 if (attr
->value_tag
== IPP_TAG_MIMETYPE
||
2945 attr
->value_tag
== IPP_TAG_NAMELANG
||
2946 attr
->value_tag
== IPP_TAG_TEXTLANG
||
2947 attr
->value_tag
== IPP_TAG_URI
||
2948 attr
->value_tag
== IPP_TAG_URISCHEME
)
2951 if (strncmp(attr
->name
, "time-", 5) == 0)
2955 * Add space for a leading space and commas between each value.
2956 * For the first attribute, the leading space isn't used, so the
2957 * extra byte can be used as the nul terminator...
2960 bytes
++; /* " " separator */
2961 bytes
+= attr
->num_values
; /* "," separators */
2964 * Boolean attributes appear as "foo,nofoo,foo,nofoo", while
2965 * other attributes appear as "foo=value1,value2,...,valueN".
2968 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
2969 bytes
+= strlen(attr
->name
);
2971 bytes
+= attr
->num_values
* strlen(attr
->name
);
2974 * Now add the size required for each value in the attribute...
2977 switch (attr
->value_tag
)
2979 case IPP_TAG_INTEGER
:
2982 * Minimum value of a signed integer is -2147483647, or 11 digits.
2985 bytes
+= attr
->num_values
* 11;
2988 case IPP_TAG_BOOLEAN
:
2990 * Add two bytes for each false ("no") value...
2993 for (i
= 0; i
< attr
->num_values
; i
++)
2994 if (!attr
->values
[i
].boolean
)
2998 case IPP_TAG_RANGE
:
3000 * A range is two signed integers separated by a hyphen, or
3001 * 23 characters max.
3004 bytes
+= attr
->num_values
* 23;
3007 case IPP_TAG_RESOLUTION
:
3009 * A resolution is two signed integers separated by an "x" and
3010 * suffixed by the units, or 26 characters max.
3013 bytes
+= attr
->num_values
* 26;
3016 case IPP_TAG_STRING
:
3019 case IPP_TAG_KEYWORD
:
3020 case IPP_TAG_CHARSET
:
3021 case IPP_TAG_LANGUAGE
:
3024 * Strings can contain characters that need quoting. We need
3025 * at least 2 * len + 2 characters to cover the quotes and
3026 * any backslashes in the string.
3029 for (i
= 0; i
< attr
->num_values
; i
++)
3030 bytes
+= 2 * strlen(attr
->values
[i
].string
.text
) + 2;
3034 break; /* anti-compiler-warning-code */
3043 * 'load_job_cache()' - Load jobs from the job.cache file.
3047 load_job_cache(const char *filename
) /* I - job.cache filename */
3049 cups_file_t
*fp
; /* job.cache file */
3050 char line
[1024], /* Line buffer */
3051 *value
; /* Value on line */
3052 int linenum
; /* Line number in file */
3053 cupsd_job_t
*job
; /* Current job */
3054 int jobid
; /* Job ID */
3055 char jobfile
[1024]; /* Job filename */
3059 * Open the job.cache file...
3062 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
3064 if (errno
!= ENOENT
)
3065 cupsdLogMessage(CUPSD_LOG_ERROR
,
3066 "Unable to open job cache file \"%s\": %s",
3067 filename
, strerror(errno
));
3069 load_request_root();
3075 * Read entries from the job cache file and create jobs as needed.
3078 cupsdLogMessage(CUPSD_LOG_INFO
, "Loading job cache file \"%s\"...",
3084 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
3086 if (!strcasecmp(line
, "NextJobId"))
3089 NextJobId
= atoi(value
);
3091 else if (!strcasecmp(line
, "<Job"))
3095 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing </Job> directive on line %d!",
3102 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing job ID on line %d!", linenum
);
3106 jobid
= atoi(value
);
3110 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad job ID %d on line %d!", jobid
,
3115 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, jobid
);
3116 if (access(jobfile
, 0))
3118 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Job %d] Files have gone away!",
3123 job
= calloc(1, sizeof(cupsd_job_t
));
3126 cupsdLogMessage(CUPSD_LOG_EMERG
,
3127 "[Job %d] Unable to allocate memory for job!", jobid
);
3132 job
->back_pipes
[0] = -1;
3133 job
->back_pipes
[1] = -1;
3134 job
->print_pipes
[0] = -1;
3135 job
->print_pipes
[1] = -1;
3136 job
->side_pipes
[0] = -1;
3137 job
->side_pipes
[1] = -1;
3138 job
->status_pipes
[0] = -1;
3139 job
->status_pipes
[1] = -1;
3141 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Loading from cache...",
3146 cupsdLogMessage(CUPSD_LOG_ERROR
,
3147 "Missing <Job #> directive on line %d!", linenum
);
3150 else if (!strcasecmp(line
, "</Job>"))
3152 cupsArrayAdd(Jobs
, job
);
3154 if (job
->state_value
<= IPP_JOB_STOPPED
)
3156 cupsArrayAdd(ActiveJobs
, job
);
3164 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing value on line %d!", linenum
);
3167 else if (!strcasecmp(line
, "State"))
3169 job
->state_value
= (ipp_jstate_t
)atoi(value
);
3171 if (job
->state_value
< IPP_JOB_PENDING
)
3172 job
->state_value
= IPP_JOB_PENDING
;
3173 else if (job
->state_value
> IPP_JOB_COMPLETED
)
3174 job
->state_value
= IPP_JOB_COMPLETED
;
3176 else if (!strcasecmp(line
, "Priority"))
3178 job
->priority
= atoi(value
);
3180 else if (!strcasecmp(line
, "Username"))
3182 cupsdSetString(&job
->username
, value
);
3184 else if (!strcasecmp(line
, "Destination"))
3186 cupsdSetString(&job
->dest
, value
);
3188 else if (!strcasecmp(line
, "DestType"))
3190 job
->dtype
= (cups_ptype_t
)atoi(value
);
3192 else if (!strcasecmp(line
, "NumFiles"))
3194 job
->num_files
= atoi(value
);
3196 if (job
->num_files
< 0)
3198 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad NumFiles value %d on line %d!",
3199 job
->num_files
, linenum
);
3204 if (job
->num_files
> 0)
3206 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-001", RequestRoot
,
3208 if (access(jobfile
, 0))
3210 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Data files have gone away!",
3216 job
->filetypes
= calloc(job
->num_files
, sizeof(mime_type_t
*));
3217 job
->compressions
= calloc(job
->num_files
, sizeof(int));
3219 if (!job
->filetypes
|| !job
->compressions
)
3221 cupsdLogMessage(CUPSD_LOG_EMERG
,
3222 "[Job %d] Unable to allocate memory for %d files!",
3223 job
->id
, job
->num_files
);
3228 else if (!strcasecmp(line
, "File"))
3230 int number
, /* File number */
3231 compression
; /* Compression value */
3232 char super
[MIME_MAX_SUPER
], /* MIME super type */
3233 type
[MIME_MAX_TYPE
]; /* MIME type */
3236 if (sscanf(value
, "%d%*[ \t]%15[^/]/%255s%d", &number
, super
, type
,
3239 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File on line %d!", linenum
);
3243 if (number
< 1 || number
> job
->num_files
)
3245 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File number %d on line %d!",
3252 job
->compressions
[number
] = compression
;
3253 job
->filetypes
[number
] = mimeType(MimeDatabase
, super
, type
);
3255 if (!job
->filetypes
[number
])
3258 * If the original MIME type is unknown, auto-type it!
3261 cupsdLogMessage(CUPSD_LOG_ERROR
,
3262 "[Job %d] Unknown MIME type %s/%s for file %d!",
3263 job
->id
, super
, type
, number
+ 1);
3265 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
3266 job
->id
, number
+ 1);
3267 job
->filetypes
[number
] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
3268 job
->compressions
+ number
);
3271 * If that didn't work, assume it is raw...
3274 if (!job
->filetypes
[number
])
3275 job
->filetypes
[number
] = mimeType(MimeDatabase
, "application",
3280 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unknown %s directive on line %d!",
3289 * 'load_next_job_id()' - Load the NextJobId value from the job.cache file.
3293 load_next_job_id(const char *filename
) /* I - job.cache filename */
3295 cups_file_t
*fp
; /* job.cache file */
3296 char line
[1024], /* Line buffer */
3297 *value
; /* Value on line */
3298 int linenum
; /* Line number in file */
3299 int next_job_id
; /* NextJobId value from line */
3303 * Read the NextJobId directive from the job.cache file and use
3304 * the value (if any).
3307 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
3309 if (errno
!= ENOENT
)
3310 cupsdLogMessage(CUPSD_LOG_ERROR
,
3311 "Unable to open job cache file \"%s\": %s",
3312 filename
, strerror(errno
));
3317 cupsdLogMessage(CUPSD_LOG_INFO
,
3318 "Loading NextJobId from job cache file \"%s\"...", filename
);
3322 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
3324 if (!strcasecmp(line
, "NextJobId"))
3328 next_job_id
= atoi(value
);
3330 if (next_job_id
> NextJobId
)
3331 NextJobId
= next_job_id
;
3342 * 'load_request_root()' - Load jobs from the RequestRoot directory.
3346 load_request_root(void)
3348 cups_dir_t
*dir
; /* Directory */
3349 cups_dentry_t
*dent
; /* Directory entry */
3350 cupsd_job_t
*job
; /* New job */
3354 * Open the requests directory...
3357 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Scanning %s for jobs...", RequestRoot
);
3359 if ((dir
= cupsDirOpen(RequestRoot
)) == NULL
)
3361 cupsdLogMessage(CUPSD_LOG_ERROR
,
3362 "Unable to open spool directory \"%s\": %s",
3363 RequestRoot
, strerror(errno
));
3368 * Read all the c##### files...
3371 while ((dent
= cupsDirRead(dir
)) != NULL
)
3372 if (strlen(dent
->filename
) >= 6 && dent
->filename
[0] == 'c')
3375 * Allocate memory for the job...
3378 if ((job
= calloc(sizeof(cupsd_job_t
), 1)) == NULL
)
3380 cupsdLogMessage(CUPSD_LOG_ERROR
, "Ran out of memory for jobs!");
3386 * Assign the job ID...
3389 job
->id
= atoi(dent
->filename
+ 1);
3390 job
->back_pipes
[0] = -1;
3391 job
->back_pipes
[1] = -1;
3392 job
->print_pipes
[0] = -1;
3393 job
->print_pipes
[1] = -1;
3394 job
->side_pipes
[0] = -1;
3395 job
->side_pipes
[1] = -1;
3396 job
->status_pipes
[0] = -1;
3397 job
->status_pipes
[1] = -1;
3399 if (job
->id
>= NextJobId
)
3400 NextJobId
= job
->id
+ 1;
3409 * Insert the job into the array, sorting by job priority and ID...
3412 cupsArrayAdd(Jobs
, job
);
3414 if (job
->state_value
<= IPP_JOB_STOPPED
)
3415 cupsArrayAdd(ActiveJobs
, job
);
3425 * 'set_hold_until()' - Set the hold time and update job-hold-until attribute.
3429 set_hold_until(cupsd_job_t
*job
, /* I - Job to update */
3430 time_t holdtime
) /* I - Hold until time */
3432 ipp_attribute_t
*attr
; /* job-hold-until attribute */
3433 struct tm
*holddate
; /* Hold date */
3434 char holdstr
[64]; /* Hold time */
3438 * Set the hold_until value and hold the job...
3441 cupsdLogMessage(CUPSD_LOG_DEBUG
, "set_hold_until: hold_until = %d",
3444 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
3445 job
->state_value
= IPP_JOB_HELD
;
3446 job
->hold_until
= holdtime
;
3449 * Update the job-hold-until attribute with a string representing GMT
3450 * time (HH:MM:SS)...
3453 holddate
= gmtime(&holdtime
);
3454 snprintf(holdstr
, sizeof(holdstr
), "%d:%d:%d", holddate
->tm_hour
,
3455 holddate
->tm_min
, holddate
->tm_sec
);
3457 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
3458 IPP_TAG_KEYWORD
)) == NULL
)
3459 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
3462 * Either add the attribute or update the value of the existing one
3466 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "job-hold-until",
3469 cupsdSetString(&attr
->values
[0].string
.text
, holdstr
);
3472 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
3477 * 'set_time()' - Set one of the "time-at-xyz" attributes.
3481 set_time(cupsd_job_t
*job
, /* I - Job to update */
3482 const char *name
) /* I - Name of attribute */
3484 ipp_attribute_t
*attr
; /* Time attribute */
3487 if ((attr
= ippFindAttribute(job
->attrs
, name
, IPP_TAG_ZERO
)) != NULL
)
3489 attr
->value_tag
= IPP_TAG_INTEGER
;
3490 attr
->values
[0].integer
= time(NULL
);
3496 * 'start_job()' - Start a print job.
3500 start_job(cupsd_job_t
*job
, /* I - Job ID */
3501 cupsd_printer_t
*printer
) /* I - Printer to print job */
3503 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "start_job(job=%p(%d), printer=%p(%s))",
3504 job
, job
->id
, printer
, printer
->name
);
3507 * Make sure we have some files around before we try to print...
3510 if (job
->num_files
== 0)
3512 cupsdSetJobState(job
, IPP_JOB_ABORTED
, CUPSD_JOB_DEFAULT
,
3513 "Aborting job because it has no files.");
3518 * Update the printer and job state to "processing"...
3521 if (!cupsdLoadJob(job
))
3524 cupsdSetJobState(job
, IPP_JOB_PROCESSING
, CUPSD_JOB_DEFAULT
, NULL
);
3525 cupsdSetPrinterState(printer
, IPP_PRINTER_PROCESSING
, 0);
3528 job
->current_file
= 0;
3530 job
->printer
= printer
;
3534 * Setup the last exit status and security profiles...
3538 job
->profile
= cupsdCreateProfile(job
->id
);
3541 * Create the status pipes and buffer...
3544 if (cupsdOpenPipe(job
->status_pipes
))
3546 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
3547 "Unable to create job status pipes - %s.", strerror(errno
));
3549 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
3550 "Job stopped because the scheduler could not create the "
3551 "job status pipes.");
3553 cupsdDestroyProfile(job
->profile
);
3554 job
->profile
= NULL
;
3558 job
->status_buffer
= cupsdStatBufNew(job
->status_pipes
[0], NULL
);
3559 job
->status_level
= CUPSD_LOG_INFO
;
3562 * Create the backchannel pipes and make them non-blocking...
3565 if (cupsdOpenPipe(job
->back_pipes
))
3567 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
3568 "Unable to create back-channel pipes - %s.", strerror(errno
));
3570 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
3571 "Job stopped because the scheduler could not create the "
3572 "back-channel pipes.");
3574 cupsdClosePipe(job
->status_pipes
);
3575 cupsdStatBufDelete(job
->status_buffer
);
3576 job
->status_buffer
= NULL
;
3578 cupsdDestroyProfile(job
->profile
);
3579 job
->profile
= NULL
;
3583 fcntl(job
->back_pipes
[0], F_SETFL
,
3584 fcntl(job
->back_pipes
[0], F_GETFL
) | O_NONBLOCK
);
3585 fcntl(job
->back_pipes
[1], F_SETFL
,
3586 fcntl(job
->back_pipes
[1], F_GETFL
) | O_NONBLOCK
);
3589 * Create the side-channel pipes and make them non-blocking...
3592 if (socketpair(AF_LOCAL
, SOCK_STREAM
, 0, job
->side_pipes
))
3594 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
3595 "Unable to create side-channel pipes - %s.", strerror(errno
));
3597 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
3598 "Job stopped because the scheduler could not create the "
3599 "side-channel pipes.");
3601 cupsdClosePipe(job
->back_pipes
);
3603 cupsdClosePipe(job
->status_pipes
);
3604 cupsdStatBufDelete(job
->status_buffer
);
3605 job
->status_buffer
= NULL
;
3607 cupsdDestroyProfile(job
->profile
);
3608 job
->profile
= NULL
;
3612 fcntl(job
->side_pipes
[0], F_SETFL
,
3613 fcntl(job
->side_pipes
[0], F_GETFL
) | O_NONBLOCK
);
3614 fcntl(job
->side_pipes
[1], F_SETFL
,
3615 fcntl(job
->side_pipes
[1], F_GETFL
) | O_NONBLOCK
);
3618 * Now start the first file in the job...
3621 cupsdContinueJob(job
);
3626 * 'stop_job()' - Stop a print job.
3630 stop_job(cupsd_job_t
*job
, /* I - Job */
3631 cupsd_jobaction_t action
) /* I - Action */
3633 int i
; /* Looping var */
3636 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "stop_job(job=%p(%d), action=%d)", job
,
3639 FilterLevel
-= job
->cost
;
3642 for (i
= 0; job
->filters
[i
]; i
++)
3643 if (job
->filters
[i
] > 0)
3644 cupsdEndProcess(job
->filters
[i
], action
== CUPSD_JOB_FORCE
);
3646 if (job
->backend
> 0)
3647 cupsdEndProcess(job
->backend
, action
== CUPSD_JOB_FORCE
);
3652 * 'unload_job()' - Unload a job from memory.
3656 unload_job(cupsd_job_t
*job
) /* I - Job */
3661 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Unloading...", job
->id
);
3663 ippDelete(job
->attrs
);
3668 job
->job_sheets
= NULL
;
3669 job
->printer_message
= NULL
;
3670 job
->printer_reasons
= NULL
;
3675 * 'update_job()' - Read a status update from a job's filters.
3679 update_job(cupsd_job_t
*job
) /* I - Job to check */
3681 int i
; /* Looping var */
3682 int copies
; /* Number of copies printed */
3683 char message
[1024], /* Message text */
3684 *ptr
; /* Pointer update... */
3685 int loglevel
, /* Log level for message */
3686 event
= 0; /* Events? */
3687 static const char * const levels
[] = /* Log levels */
3703 * Get the printer associated with this job; if the printer is stopped for
3704 * any reason then job->printer will be reset to NULL, so make sure we have
3705 * a valid pointer...
3708 while ((ptr
= cupsdStatBufUpdate(job
->status_buffer
, &loglevel
,
3709 message
, sizeof(message
))) != NULL
)
3712 * Process page and printer state messages as needed...
3715 if (loglevel
== CUPSD_LOG_PAGE
)
3718 * Page message; send the message to the page_log file and update the
3719 * job sheet count...
3722 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "PAGE: %s", message
);
3726 if (!strncasecmp(message
, "total ", 6))
3729 * Got a total count of pages from a backend or filter...
3732 copies
= atoi(message
+ 6);
3733 copies
-= job
->sheets
->values
[0].integer
; /* Just track the delta */
3735 else if (!sscanf(message
, "%*d%d", &copies
))
3738 job
->sheets
->values
[0].integer
+= copies
;
3740 if (job
->printer
->page_limit
)
3742 cupsd_quota_t
*q
= cupsdUpdateQuota(job
->printer
, job
->username
,
3746 if (AppleQuotas
&& q
->page_count
== -3)
3749 * Quota limit exceeded, cancel job in progress immediately...
3752 cupsdSetJobState(job
, IPP_JOB_CANCELED
, CUPSD_JOB_DEFAULT
,
3753 "Canceled job because pages exceed user %s "
3754 "quota limit on printer %s (%s).",
3755 job
->username
, job
->printer
->name
,
3756 job
->printer
->info
);
3761 #endif /* __APPLE__ */
3765 cupsdLogPage(job
, message
);
3768 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
3769 "Printed %d page(s).", job
->sheets
->values
[0].integer
);
3771 else if (loglevel
== CUPSD_LOG_STATE
)
3773 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "STATE: %s", message
);
3775 if (!strcmp(message
, "paused"))
3777 cupsdStopPrinter(job
->printer
, 1);
3782 cupsdSetPrinterReasons(job
->printer
, message
);
3783 cupsdAddPrinterHistory(job
->printer
);
3784 event
|= CUPSD_EVENT_PRINTER_STATE
;
3787 update_job_attrs(job
, 0);
3789 else if (loglevel
== CUPSD_LOG_ATTR
)
3792 * Set attribute(s)...
3795 int num_attrs
; /* Number of attributes */
3796 cups_option_t
*attrs
; /* Attributes */
3797 const char *attr
; /* Attribute */
3800 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "ATTR: %s", message
);
3802 num_attrs
= cupsParseOptions(message
, 0, &attrs
);
3804 if ((attr
= cupsGetOption("auth-info-required", num_attrs
,
3807 cupsdSetAuthInfoRequired(job
->printer
, attr
, NULL
);
3808 cupsdSetPrinterAttrs(job
->printer
);
3810 if (job
->printer
->type
& CUPS_PRINTER_DISCOVERED
)
3811 cupsdMarkDirty(CUPSD_DIRTY_REMOTE
);
3813 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3816 if ((attr
= cupsGetOption("job-media-progress", num_attrs
,
3819 int progress
= atoi(attr
);
3822 if (progress
>= 0 && progress
<= 100)
3824 job
->progress
= progress
;
3827 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
3828 "Printing page %d, %d%%",
3829 job
->sheets
->values
[0].integer
, job
->progress
);
3833 if ((attr
= cupsGetOption("printer-alert", num_attrs
, attrs
)) != NULL
)
3835 cupsdSetString(&job
->printer
->alert
, attr
);
3836 event
|= CUPSD_EVENT_PRINTER_STATE
;
3839 if ((attr
= cupsGetOption("printer-alert-description", num_attrs
,
3842 cupsdSetString(&job
->printer
->alert_description
, attr
);
3843 event
|= CUPSD_EVENT_PRINTER_STATE
;
3846 if ((attr
= cupsGetOption("marker-colors", num_attrs
, attrs
)) != NULL
)
3848 cupsdSetPrinterAttr(job
->printer
, "marker-colors", (char *)attr
);
3849 job
->printer
->marker_time
= time(NULL
);
3850 event
|= CUPSD_EVENT_PRINTER_STATE
;
3851 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3854 if ((attr
= cupsGetOption("marker-levels", num_attrs
, attrs
)) != NULL
)
3856 cupsdSetPrinterAttr(job
->printer
, "marker-levels", (char *)attr
);
3857 job
->printer
->marker_time
= time(NULL
);
3858 event
|= CUPSD_EVENT_PRINTER_STATE
;
3859 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3862 if ((attr
= cupsGetOption("marker-low-levels", num_attrs
, attrs
)) != NULL
)
3864 cupsdSetPrinterAttr(job
->printer
, "marker-low-levels", (char *)attr
);
3865 job
->printer
->marker_time
= time(NULL
);
3866 event
|= CUPSD_EVENT_PRINTER_STATE
;
3867 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3870 if ((attr
= cupsGetOption("marker-high-levels", num_attrs
, attrs
)) != NULL
)
3872 cupsdSetPrinterAttr(job
->printer
, "marker-high-levels", (char *)attr
);
3873 job
->printer
->marker_time
= time(NULL
);
3874 event
|= CUPSD_EVENT_PRINTER_STATE
;
3875 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3878 if ((attr
= cupsGetOption("marker-message", num_attrs
, attrs
)) != NULL
)
3880 cupsdSetPrinterAttr(job
->printer
, "marker-message", (char *)attr
);
3881 job
->printer
->marker_time
= time(NULL
);
3882 event
|= CUPSD_EVENT_PRINTER_STATE
;
3883 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3886 if ((attr
= cupsGetOption("marker-names", num_attrs
, attrs
)) != NULL
)
3888 cupsdSetPrinterAttr(job
->printer
, "marker-names", (char *)attr
);
3889 job
->printer
->marker_time
= time(NULL
);
3890 event
|= CUPSD_EVENT_PRINTER_STATE
;
3891 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3894 if ((attr
= cupsGetOption("marker-types", num_attrs
, attrs
)) != NULL
)
3896 cupsdSetPrinterAttr(job
->printer
, "marker-types", (char *)attr
);
3897 job
->printer
->marker_time
= time(NULL
);
3898 event
|= CUPSD_EVENT_PRINTER_STATE
;
3899 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3902 cupsFreeOptions(num_attrs
, attrs
);
3904 else if (loglevel
== CUPSD_LOG_PPD
)
3907 * Set attribute(s)...
3910 int num_keywords
; /* Number of keywords */
3911 cups_option_t
*keywords
; /* Keywords */
3914 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "PPD: %s", message
);
3916 num_keywords
= cupsParseOptions(message
, 0, &keywords
);
3918 if (cupsdUpdatePrinterPPD(job
->printer
, num_keywords
, keywords
))
3919 cupsdSetPrinterAttrs(job
->printer
);
3921 cupsFreeOptions(num_keywords
, keywords
);
3924 else if (!strncmp(message
, "recoverable:", 12))
3927 while (isspace(*ptr
& 255))
3932 cupsdSetPrinterReasons(job
->printer
,
3933 "+com.apple.print.recoverable-warning");
3934 cupsdSetString(&(job
->printer
->recoverable
), ptr
);
3935 cupsdAddPrinterHistory(job
->printer
);
3936 event
|= CUPSD_EVENT_PRINTER_STATE
;
3939 else if (!strncmp(message
, "recovered:", 10))
3941 cupsdSetPrinterReasons(job
->printer
,
3942 "-com.apple.print.recoverable-warning");
3945 while (isspace(*ptr
& 255))
3948 cupsdSetString(&(job
->printer
->recoverable
), ptr
);
3949 cupsdAddPrinterHistory(job
->printer
);
3950 event
|= CUPSD_EVENT_PRINTER_STATE
;
3952 #endif /* __APPLE__ */
3955 if (loglevel
!= CUPSD_LOG_INFO
&& loglevel
> LogLevel
)
3956 cupsdLogJob(job
, loglevel
, "%d %s", loglevel
, message
);
3958 if (loglevel
< CUPSD_LOG_DEBUG
)
3960 strlcpy(job
->printer
->state_message
, message
,
3961 sizeof(job
->printer
->state_message
));
3962 cupsdAddPrinterHistory(job
->printer
);
3964 event
|= CUPSD_EVENT_PRINTER_STATE
;
3966 if (loglevel
<= job
->status_level
)
3969 * Some messages show in the job-printer-state-message attribute...
3972 if (loglevel
!= CUPSD_LOG_NOTICE
)
3973 job
->status_level
= loglevel
;
3975 update_job_attrs(job
, 1);
3977 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
3978 "Set job-printer-state-message to \"%s\", "
3980 job
->printer_message
->values
[0].string
.text
,
3981 levels
[job
->status_level
]);
3986 if (!strchr(job
->status_buffer
->buffer
, '\n'))
3990 if (event
& CUPSD_EVENT_PRINTER_STATE
)
3991 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, job
->printer
, NULL
,
3992 (job
->printer
->type
& CUPS_PRINTER_CLASS
) ?
3993 "Class \"%s\" state changed." :
3994 "Printer \"%s\" state changed.",
3995 job
->printer
->name
);
3997 if (ptr
== NULL
&& !job
->status_buffer
->bufused
)
4000 * See if all of the filters and the backend have returned their
4004 for (i
= 0; job
->filters
[i
] < 0; i
++);
4006 if (job
->filters
[i
])
4009 * EOF but we haven't collected the exit status of all filters...
4012 cupsdCheckProcess();
4016 if (job
->current_file
>= job
->num_files
&& job
->backend
> 0)
4019 * EOF but we haven't collected the exit status of the backend...
4022 cupsdCheckProcess();
4027 * Handle the end of job stuff...
4033 * Check for new jobs...
4042 * 'update_job_attrs()' - Update the job-printer-* attributes.
4046 update_job_attrs(cupsd_job_t
*job
, /* I - Job to update */
4047 int do_message
)/* I - 1 = copy job-printer-state message */
4049 int i
; /* Looping var */
4050 int num_reasons
; /* Actual number of reasons */
4051 const char * const *reasons
; /* Reasons */
4052 static const char *none
= "none"; /* "none" reason */
4056 * Get/create the job-printer-state-* attributes...
4059 if (!job
->printer_message
)
4061 if ((job
->printer_message
= ippFindAttribute(job
->attrs
,
4062 "job-printer-state-message",
4063 IPP_TAG_TEXT
)) == NULL
)
4064 job
->printer_message
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_TEXT
,
4065 "job-printer-state-message",
4069 if (!job
->printer_reasons
)
4070 job
->printer_reasons
= ippFindAttribute(job
->attrs
,
4071 "job-printer-state-reasons",
4075 * Copy or clear the printer-state-message value as needed...
4078 if (job
->state_value
!= IPP_JOB_PROCESSING
&&
4079 job
->status_level
== CUPSD_LOG_INFO
)
4080 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
), "");
4081 else if (job
->printer
->state_message
[0] && do_message
)
4082 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
),
4083 job
->printer
->state_message
);
4086 * ... and the printer-state-reasons value...
4089 if (job
->printer
->num_reasons
== 0)
4096 num_reasons
= job
->printer
->num_reasons
;
4097 reasons
= (const char * const *)job
->printer
->reasons
;
4100 if (!job
->printer_reasons
|| job
->printer_reasons
->num_values
!= num_reasons
)
4103 * Replace/create a job-printer-state-reasons attribute...
4106 ippDeleteAttribute(job
->attrs
, job
->printer_reasons
);
4108 job
->printer_reasons
= ippAddStrings(job
->attrs
,
4109 IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
4110 "job-printer-state-reasons",
4111 num_reasons
, NULL
, NULL
);
4116 * Don't bother clearing the reason strings if they are the same...
4119 for (i
= 0; i
< num_reasons
; i
++)
4120 if (strcmp(job
->printer_reasons
->values
[i
].string
.text
, reasons
[i
]))
4123 if (i
>= num_reasons
)
4127 * Not the same, so free the current strings...
4130 for (i
= 0; i
< num_reasons
; i
++)
4131 _cupsStrFree(job
->printer_reasons
->values
[i
].string
.text
);
4135 * Copy the reasons...
4138 for (i
= 0; i
< num_reasons
; i
++)
4139 job
->printer_reasons
->values
[i
].string
.text
= _cupsStrAlloc(reasons
[i
]);
4144 * End of "$Id: job.c 7902 2008-09-03 14:20:17Z mike $".