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
, "HoldUntil %d\n", (int)job
->hold_until
);
1843 cupsFilePrintf(fp
, "Username %s\n", job
->username
);
1844 cupsFilePrintf(fp
, "Destination %s\n", job
->dest
);
1845 cupsFilePrintf(fp
, "DestType %d\n", job
->dtype
);
1846 cupsFilePrintf(fp
, "NumFiles %d\n", job
->num_files
);
1847 for (i
= 0; i
< job
->num_files
; i
++)
1848 cupsFilePrintf(fp
, "File %d %s/%s %d\n", i
+ 1, job
->filetypes
[i
]->super
,
1849 job
->filetypes
[i
]->type
, job
->compressions
[i
]);
1850 cupsFilePuts(fp
, "</Job>\n");
1858 * 'cupsdSaveJob()' - Save a job to disk.
1862 cupsdSaveJob(cupsd_job_t
*job
) /* I - Job */
1864 char filename
[1024]; /* Job control filename */
1865 cups_file_t
*fp
; /* Job file */
1868 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSaveJob(job=%p(%d)): job->attrs=%p",
1869 job
, job
->id
, job
->attrs
);
1871 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
, job
->id
);
1873 if ((fp
= cupsFileOpen(filename
, "w")) == NULL
)
1875 cupsdLogMessage(CUPSD_LOG_ERROR
,
1876 "[Job %d] Unable to create job control file \"%s\" - %s.",
1877 job
->id
, filename
, strerror(errno
));
1881 fchmod(cupsFileNumber(fp
), 0600);
1882 fchown(cupsFileNumber(fp
), RunUser
, Group
);
1884 job
->attrs
->state
= IPP_IDLE
;
1886 if (ippWriteIO(fp
, (ipp_iocb_t
)cupsFileWrite
, 1, NULL
,
1887 job
->attrs
) != IPP_DATA
)
1888 cupsdLogMessage(CUPSD_LOG_ERROR
,
1889 "[Job %d] Unable to write job control file!", job
->id
);
1898 * 'cupsdSetJobHoldUntil()' - Set the hold time for a job.
1902 cupsdSetJobHoldUntil(cupsd_job_t
*job
, /* I - Job */
1903 const char *when
, /* I - When to resume */
1904 int update
)/* I - Update job-hold-until attr? */
1906 time_t curtime
; /* Current time */
1907 struct tm
*curdate
; /* Current date */
1908 int hour
; /* Hold hour */
1909 int minute
; /* Hold minute */
1910 int second
= 0; /* Hold second */
1913 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1914 "cupsdSetJobHoldUntil(job=%p(%d), when=\"%s\", update=%d)",
1915 job
, job
->id
, when
, update
);
1920 * Update the job-hold-until attribute...
1923 ipp_attribute_t
*attr
; /* job-hold-until attribute */
1925 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1926 IPP_TAG_KEYWORD
)) == NULL
)
1927 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1930 cupsdSetString(&(attr
->values
[0].string
.text
), when
);
1932 attr
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1933 "job-hold-until", NULL
, when
);
1937 if (isdigit(when
[0] & 255))
1938 attr
->value_tag
= IPP_TAG_NAME
;
1940 attr
->value_tag
= IPP_TAG_KEYWORD
;
1943 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1948 * Update the hold time...
1951 if (!strcmp(when
, "indefinite") || !strcmp(when
, "auth-info-required"))
1954 * Hold indefinitely...
1957 job
->hold_until
= 0;
1959 else if (!strcmp(when
, "day-time"))
1962 * Hold to 6am the next morning unless local time is < 6pm.
1965 curtime
= time(NULL
);
1966 curdate
= localtime(&curtime
);
1968 if (curdate
->tm_hour
< 18)
1969 job
->hold_until
= curtime
;
1971 job
->hold_until
= curtime
+
1972 ((29 - curdate
->tm_hour
) * 60 + 59 -
1973 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1975 else if (!strcmp(when
, "evening") || !strcmp(when
, "night"))
1978 * Hold to 6pm unless local time is > 6pm or < 6am.
1981 curtime
= time(NULL
);
1982 curdate
= localtime(&curtime
);
1984 if (curdate
->tm_hour
< 6 || curdate
->tm_hour
>= 18)
1985 job
->hold_until
= curtime
;
1987 job
->hold_until
= curtime
+
1988 ((17 - curdate
->tm_hour
) * 60 + 59 -
1989 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1991 else if (!strcmp(when
, "second-shift"))
1994 * Hold to 4pm unless local time is > 4pm.
1997 curtime
= time(NULL
);
1998 curdate
= localtime(&curtime
);
2000 if (curdate
->tm_hour
>= 16)
2001 job
->hold_until
= curtime
;
2003 job
->hold_until
= curtime
+
2004 ((15 - curdate
->tm_hour
) * 60 + 59 -
2005 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2007 else if (!strcmp(when
, "third-shift"))
2010 * Hold to 12am unless local time is < 8am.
2013 curtime
= time(NULL
);
2014 curdate
= localtime(&curtime
);
2016 if (curdate
->tm_hour
< 8)
2017 job
->hold_until
= curtime
;
2019 job
->hold_until
= curtime
+
2020 ((23 - curdate
->tm_hour
) * 60 + 59 -
2021 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2023 else if (!strcmp(when
, "weekend"))
2026 * Hold to weekend unless we are in the weekend.
2029 curtime
= time(NULL
);
2030 curdate
= localtime(&curtime
);
2032 if (curdate
->tm_wday
== 0 || curdate
->tm_wday
== 6)
2033 job
->hold_until
= curtime
;
2035 job
->hold_until
= curtime
+
2036 (((5 - curdate
->tm_wday
) * 24 +
2037 (17 - curdate
->tm_hour
)) * 60 + 59 -
2038 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2040 else if (sscanf(when
, "%d:%d:%d", &hour
, &minute
, &second
) >= 2)
2043 * Hold to specified GMT time (HH:MM or HH:MM:SS)...
2046 curtime
= time(NULL
);
2047 curdate
= gmtime(&curtime
);
2049 job
->hold_until
= curtime
+
2050 ((hour
- curdate
->tm_hour
) * 60 + minute
-
2051 curdate
->tm_min
) * 60 + second
- curdate
->tm_sec
;
2054 * Hold until next day as needed...
2057 if (job
->hold_until
< curtime
)
2058 job
->hold_until
+= 24 * 60 * 60;
2061 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetJobHoldUntil: hold_until=%d",
2062 (int)job
->hold_until
);
2067 * 'cupsdSetJobPriority()' - Set the priority of a job, moving it up/down in
2068 * the list as needed.
2072 cupsdSetJobPriority(
2073 cupsd_job_t
*job
, /* I - Job ID */
2074 int priority
) /* I - New priority (0 to 100) */
2076 ipp_attribute_t
*attr
; /* Job attribute */
2080 * Don't change completed jobs...
2083 if (job
->state_value
>= IPP_JOB_PROCESSING
)
2087 * Set the new priority and re-add the job into the active list...
2090 cupsArrayRemove(ActiveJobs
, job
);
2092 job
->priority
= priority
;
2094 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
2095 IPP_TAG_INTEGER
)) != NULL
)
2096 attr
->values
[0].integer
= priority
;
2098 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-priority",
2101 cupsArrayAdd(ActiveJobs
, job
);
2104 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2109 * 'cupsdSetJobState()' - Set the state of the specified print job.
2114 cupsd_job_t
*job
, /* I - Job to cancel */
2115 ipp_jstate_t newstate
, /* I - New job state */
2116 cupsd_jobaction_t action
, /* I - Action to take */
2117 const char *message
, /* I - Message to log */
2118 ...) /* I - Additional arguments as needed */
2120 int i
; /* Looping var */
2121 ipp_jstate_t oldstate
; /* Old state */
2122 char filename
[1024]; /* Job filename */
2123 ipp_attribute_t
*attr
; /* Job attribute */
2126 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2127 "cupsdSetJobState(job=%p(%d), state=%d, newstate=%d, "
2128 "action=%d, message=\"%s\")", job
, job
->id
, job
->state_value
,
2129 newstate
, action
, message
? message
: "(null)");
2133 * Make sure we have the job attributes...
2136 if (!cupsdLoadJob(job
))
2140 * Don't do anything if the state is unchanged...
2143 if (newstate
== (oldstate
= job
->state_value
))
2147 * Stop any processes that are working on the current job...
2150 if (oldstate
== IPP_JOB_PROCESSING
)
2151 stop_job(job
, action
!= CUPSD_JOB_DEFAULT
);
2154 * Set the new job state...
2157 job
->state
->values
[0].integer
= newstate
;
2158 job
->state_value
= newstate
;
2162 case IPP_JOB_PENDING
:
2164 * Update job-hold-until as needed...
2167 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2168 IPP_TAG_KEYWORD
)) == NULL
)
2169 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2173 attr
->value_tag
= IPP_TAG_KEYWORD
;
2174 cupsdSetString(&(attr
->values
[0].string
.text
), "no-hold");
2180 case IPP_JOB_ABORTED
:
2181 case IPP_JOB_CANCELED
:
2182 case IPP_JOB_COMPLETED
:
2183 set_time(job
, "time-at-completed");
2188 * Log message as needed...
2193 char buffer
[2048]; /* Message buffer */
2194 va_list ap
; /* Pointer to additional arguments */
2196 va_start(ap
, message
);
2197 vsnprintf(buffer
, sizeof(buffer
), message
, ap
);
2200 if (newstate
> IPP_JOB_STOPPED
)
2201 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
, "%s", buffer
);
2203 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
, "%s", buffer
);
2205 if (newstate
== IPP_JOB_STOPPED
|| newstate
== IPP_JOB_ABORTED
)
2206 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "%s", buffer
);
2208 cupsdLogJob(job
, CUPSD_LOG_INFO
, "%s", buffer
);
2212 * Handle post-state-change actions...
2217 case IPP_JOB_PROCESSING
:
2219 * Add the job to the "printing" list...
2222 if (!cupsArrayFind(PrintingJobs
, job
))
2223 cupsArrayAdd(PrintingJobs
, job
);
2226 * Set the processing time...
2229 set_time(job
, "time-at-processing");
2231 case IPP_JOB_PENDING
:
2233 case IPP_JOB_STOPPED
:
2235 * Make sure the job is in the active list...
2238 if (!cupsArrayFind(ActiveJobs
, job
))
2239 cupsArrayAdd(ActiveJobs
, job
);
2242 * Save the job state to disk...
2246 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2249 case IPP_JOB_ABORTED
:
2250 case IPP_JOB_CANCELED
:
2251 case IPP_JOB_COMPLETED
:
2253 * Expire job subscriptions since the job is now "completed"...
2256 cupsdExpireSubscriptions(NULL
, job
);
2259 * Remove the job from the active list...
2262 cupsArrayRemove(ActiveJobs
, job
);
2266 * If we are going to sleep and the PrintingJobs count is now 0, allow the
2267 * sleep to happen immediately...
2270 if (Sleeping
&& cupsArrayCount(PrintingJobs
) == 0)
2272 #endif /* __APPLE__ */
2275 * Remove any authentication data...
2278 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
2279 if (cupsdRemoveFile(filename
) && errno
!= ENOENT
)
2280 cupsdLogMessage(CUPSD_LOG_ERROR
,
2281 "Unable to remove authentication cache: %s",
2284 cupsdClearString(&job
->auth_username
);
2285 cupsdClearString(&job
->auth_domain
);
2286 cupsdClearString(&job
->auth_password
);
2290 * Destroy the credential cache and clear the KRB5CCNAME env var string.
2295 krb5_cc_destroy(KerberosContext
, job
->ccache
);
2299 cupsdClearString(&job
->ccname
);
2300 #endif /* HAVE_GSSAPI */
2303 * Remove the print file for good if we aren't preserving jobs or
2307 if (!JobHistory
|| !JobFiles
|| action
== CUPSD_JOB_PURGE
)
2309 for (i
= 1; i
<= job
->num_files
; i
++)
2311 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
2316 if (job
->num_files
> 0)
2318 free(job
->filetypes
);
2319 free(job
->compressions
);
2322 job
->filetypes
= NULL
;
2323 job
->compressions
= NULL
;
2327 if (JobHistory
&& action
!= CUPSD_JOB_PURGE
)
2330 * Save job state info...
2334 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2336 else if (!job
->printer
)
2337 cupsdDeleteJob(job
, CUPSD_JOB_PURGE
);
2342 * Update the server "busy" state...
2345 cupsdSetBusyState();
2350 * 'cupsdStopAllJobs()' - Stop all print jobs.
2355 cupsd_jobaction_t action
) /* I - Action */
2357 cupsd_job_t
*job
; /* Current job */
2360 DEBUG_puts("cupsdStopAllJobs()");
2362 for (job
= (cupsd_job_t
*)cupsArrayFirst(PrintingJobs
);
2364 job
= (cupsd_job_t
*)cupsArrayNext(PrintingJobs
))
2365 cupsdSetJobState(job
, IPP_JOB_PENDING
, action
, NULL
);
2370 * 'cupsdUnloadCompletedJobs()' - Flush completed job history from memory.
2374 cupsdUnloadCompletedJobs(void)
2376 cupsd_job_t
*job
; /* Current job */
2377 time_t expire
; /* Expiration time */
2380 expire
= time(NULL
) - 60;
2382 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
2384 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
2385 if (job
->attrs
&& job
->state_value
>= IPP_JOB_STOPPED
&& !job
->printer
&&
2386 job
->access_time
< expire
)
2397 * 'compare_active_jobs()' - Compare the job IDs and priorities of two jobs.
2400 static int /* O - Difference */
2401 compare_active_jobs(void *first
, /* I - First job */
2402 void *second
, /* I - Second job */
2403 void *data
) /* I - App data (not used) */
2405 int diff
; /* Difference */
2408 if ((diff
= ((cupsd_job_t
*)second
)->priority
-
2409 ((cupsd_job_t
*)first
)->priority
) != 0)
2412 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
2417 * 'compare_jobs()' - Compare the job IDs of two jobs.
2420 static int /* O - Difference */
2421 compare_jobs(void *first
, /* I - First job */
2422 void *second
, /* I - Second job */
2423 void *data
) /* I - App data (not used) */
2425 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
2430 * 'finalize_job()' - Cleanup after job filter processes and support data.
2434 finalize_job(cupsd_job_t
*job
) /* I - Job */
2436 ipp_pstate_t printer_state
; /* New printer state value */
2437 ipp_jstate_t job_state
; /* New job state value */
2438 const char *message
; /* Message for job state */
2439 char buffer
[1024]; /* Buffer for formatted messages */
2442 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "finalize_job(job=%p(%d))", job
, job
->id
);
2445 * Clear the "connecting-to-device" reason, which is only valid when a
2446 * printer is processing...
2449 cupsdSetPrinterReasons(job
->printer
, "-connecting-to-device");
2452 * Similarly, clear the "offline-report" reason for non-USB devices since we
2453 * rarely have current information for network devices...
2456 if (strncmp(job
->printer
->device_uri
, "usb:", 4))
2457 cupsdSetPrinterReasons(job
->printer
, "-offline-report");
2460 * Free the security profile...
2463 cupsdDestroyProfile(job
->profile
);
2464 job
->profile
= NULL
;
2467 * Close pipes and status buffer...
2470 cupsdRemoveSelect(job
->status_buffer
->fd
);
2472 cupsdClosePipe(job
->print_pipes
);
2473 cupsdClosePipe(job
->back_pipes
);
2474 cupsdClosePipe(job
->side_pipes
);
2475 cupsdClosePipe(job
->status_pipes
);
2477 cupsdStatBufDelete(job
->status_buffer
);
2478 job
->status_buffer
= NULL
;
2481 * Process the exit status...
2484 if (job
->printer
->state
== IPP_PRINTER_PROCESSING
)
2485 printer_state
= IPP_PRINTER_IDLE
;
2487 printer_state
= job
->printer
->state
;
2489 switch (job_state
= job
->state_value
)
2491 case IPP_JOB_PENDING
:
2492 message
= "Job paused.";
2496 message
= "Job held.";
2500 case IPP_JOB_PROCESSING
:
2501 case IPP_JOB_COMPLETED
:
2502 job_state
= IPP_JOB_COMPLETED
;
2503 message
= "Job completed.";
2506 case IPP_JOB_STOPPED
:
2507 message
= "Job stopped.";
2510 case IPP_JOB_CANCELED
:
2511 message
= "Job canceled.";
2514 case IPP_JOB_ABORTED
:
2515 message
= "Job aborted.";
2519 if (job
->status
< 0)
2522 * Backend had errors...
2525 int exit_code
; /* Exit code from backend */
2529 * Convert the status to an exit code. Due to the way the W* macros are
2530 * implemented on MacOS X (bug?), we have to store the exit status in a
2531 * variable first and then convert...
2534 exit_code
= -job
->status
;
2535 if (WIFEXITED(exit_code
))
2536 exit_code
= WEXITSTATUS(exit_code
);
2538 exit_code
= job
->status
;
2540 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Backend returned status %d (%s)",
2542 exit_code
== CUPS_BACKEND_FAILED
? "failed" :
2543 exit_code
== CUPS_BACKEND_AUTH_REQUIRED
?
2544 "authentication required" :
2545 exit_code
== CUPS_BACKEND_HOLD
? "hold job" :
2546 exit_code
== CUPS_BACKEND_STOP
? "stop printer" :
2547 exit_code
== CUPS_BACKEND_CANCEL
? "cancel job" :
2548 exit_code
< 0 ? "crashed" : "unknown");
2551 * Do what needs to be done...
2557 case CUPS_BACKEND_FAILED
:
2559 * Backend failure, use the error-policy to determine how to
2563 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
2566 * Queued on a class - mark the job as pending and we'll retry on
2567 * another printer...
2570 if (job_state
== IPP_JOB_COMPLETED
)
2572 job_state
= IPP_JOB_PENDING
;
2573 message
= "Retrying job on another printer.";
2576 else if (!strcmp(job
->printer
->error_policy
, "retry-current-job"))
2579 * The error policy is "retry-current-job" - mark the job as pending
2580 * and we'll retry on the same printer...
2583 if (job_state
== IPP_JOB_COMPLETED
)
2585 job_state
= IPP_JOB_PENDING
;
2586 message
= "Retrying job on same printer.";
2589 else if ((job
->printer
->type
& CUPS_PRINTER_FAX
) ||
2590 !strcmp(job
->printer
->error_policy
, "retry-job"))
2592 if (job_state
== IPP_JOB_COMPLETED
)
2595 * The job was queued on a fax or the error policy is "retry-job" -
2596 * hold the job if the number of retries is less than the
2597 * JobRetryLimit, otherwise abort the job.
2602 if (job
->tries
>= JobRetryLimit
)
2608 snprintf(buffer
, sizeof(buffer
),
2609 "Job aborted after %d unsuccessful attempts.",
2611 job_state
= IPP_JOB_ABORTED
;
2617 * Try again in N seconds...
2620 set_hold_until(job
, time(NULL
) + JobRetryInterval
);
2622 snprintf(buffer
, sizeof(buffer
),
2623 "Job held for %d seconds since it could not be sent.",
2625 job_state
= IPP_JOB_HELD
;
2630 else if (!strcmp(job
->printer
->error_policy
, "abort-job") &&
2631 job_state
== IPP_JOB_COMPLETED
)
2633 job_state
= IPP_JOB_ABORTED
;
2634 message
= "Job aborted due to backend errors; please consult "
2635 "the error_log file for details.";
2639 printer_state
= IPP_PRINTER_STOPPED
;
2640 message
= "Printer stopped due to backend errors; please "
2641 "consult the error_log file for details.";
2643 if (job_state
== IPP_JOB_COMPLETED
)
2644 job_state
= IPP_JOB_PENDING
;
2648 case CUPS_BACKEND_CANCEL
:
2653 if (job_state
== IPP_JOB_COMPLETED
)
2655 job_state
= IPP_JOB_ABORTED
;
2656 message
= "Job aborted due to backend errors; please consult "
2657 "the error_log file for details.";
2661 case CUPS_BACKEND_HOLD
:
2662 if (job_state
== IPP_JOB_COMPLETED
)
2668 cupsdSetJobHoldUntil(job
, "indefinite", 1);
2670 job_state
= IPP_JOB_HELD
;
2671 message
= "Job held indefinitely due to backend errors; please "
2672 "consult the error_log file for details.";
2676 case CUPS_BACKEND_STOP
:
2678 * Stop the printer...
2681 printer_state
= IPP_PRINTER_STOPPED
;
2682 message
= "Printer stopped due to backend errors; please "
2683 "consult the error_log file for details.";
2685 if (job_state
== IPP_JOB_COMPLETED
)
2686 job_state
= IPP_JOB_PENDING
;
2689 case CUPS_BACKEND_AUTH_REQUIRED
:
2691 * Hold the job for authentication...
2694 if (job_state
== IPP_JOB_COMPLETED
)
2696 cupsdSetJobHoldUntil(job
, "auth-info-required", 1);
2698 job_state
= IPP_JOB_HELD
;
2699 message
= "Job held for authentication.";
2704 else if (job
->status
> 0)
2707 * Filter had errors; stop job...
2710 if (job_state
== IPP_JOB_COMPLETED
)
2712 job_state
= IPP_JOB_STOPPED
;
2713 message
= "Job stopped due to filter errors; please consult the "
2714 "error_log file for details.";
2719 * Update the printer and job state.
2722 cupsdSetJobState(job
, job_state
, CUPSD_JOB_DEFAULT
, "%s", message
);
2723 cupsdSetPrinterState(job
->printer
, printer_state
,
2724 printer_state
== IPP_PRINTER_STOPPED
);
2725 update_job_attrs(job
, 0);
2727 cupsArrayRemove(PrintingJobs
, job
);
2730 * Clear the printer <-> job association...
2733 job
->printer
->job
= NULL
;
2734 job
->printer
= NULL
;
2737 * Try printing another job...
2740 if (printer_state
!= IPP_PRINTER_STOPPED
)
2746 * 'get_options()' - Get a string containing the job options.
2749 static char * /* O - Options string */
2750 get_options(cupsd_job_t
*job
, /* I - Job */
2751 int banner_page
, /* I - Printing a banner page? */
2752 char *copies
, /* I - Copies buffer */
2753 size_t copies_size
, /* I - Size of copies buffer */
2754 char *title
, /* I - Title buffer */
2755 size_t title_size
) /* I - Size of title buffer */
2757 int i
; /* Looping var */
2758 char *optptr
, /* Pointer to options */
2759 *valptr
; /* Pointer in value string */
2760 ipp_attribute_t
*attr
; /* Current attribute */
2761 static char *options
= NULL
;/* Full list of options */
2762 static int optlength
= 0; /* Length of option buffer */
2766 * Building the options string is harder than it needs to be, but
2767 * for the moment we need to pass strings for command-line args and
2768 * not IPP attribute pointers... :)
2770 * First allocate/reallocate the option buffer as needed...
2773 i
= ipp_length(job
->attrs
);
2775 if (i
> optlength
|| !options
)
2780 optptr
= realloc(options
, i
);
2784 cupsdLogJob(job
, CUPSD_LOG_CRIT
,
2785 "Unable to allocate %d bytes for option buffer!", i
);
2794 * Now loop through the attributes and convert them to the textual
2795 * representation used by the filters...
2801 snprintf(title
, title_size
, "%s-%d", job
->printer
->name
, job
->id
);
2802 strlcpy(copies
, "1", copies_size
);
2804 for (attr
= job
->attrs
->attrs
; attr
!= NULL
; attr
= attr
->next
)
2806 if (!strcmp(attr
->name
, "copies") &&
2807 attr
->value_tag
== IPP_TAG_INTEGER
)
2810 * Don't use the # copies attribute if we are printing the job sheets...
2814 snprintf(copies
, copies_size
, "%d", attr
->values
[0].integer
);
2816 else if (!strcmp(attr
->name
, "job-name") &&
2817 (attr
->value_tag
== IPP_TAG_NAME
||
2818 attr
->value_tag
== IPP_TAG_NAMELANG
))
2819 strlcpy(title
, attr
->values
[0].string
.text
, title_size
);
2820 else if (attr
->group_tag
== IPP_TAG_JOB
)
2823 * Filter out other unwanted attributes...
2826 if (attr
->value_tag
== IPP_TAG_MIMETYPE
||
2827 attr
->value_tag
== IPP_TAG_NAMELANG
||
2828 attr
->value_tag
== IPP_TAG_TEXTLANG
||
2829 (attr
->value_tag
== IPP_TAG_URI
&& strcmp(attr
->name
, "job-uuid")) ||
2830 attr
->value_tag
== IPP_TAG_URISCHEME
||
2831 attr
->value_tag
== IPP_TAG_BEGIN_COLLECTION
) /* Not yet supported */
2834 if (!strncmp(attr
->name
, "time-", 5))
2837 if (!strncmp(attr
->name
, "job-", 4) &&
2838 strcmp(attr
->name
, "job-billing") &&
2839 strcmp(attr
->name
, "job-impressions") &&
2840 strcmp(attr
->name
, "job-originating-host-name") &&
2841 strcmp(attr
->name
, "job-uuid") &&
2842 !(job
->printer
->type
& CUPS_PRINTER_REMOTE
))
2845 if ((!strcmp(attr
->name
, "job-impressions") ||
2846 !strcmp(attr
->name
, "page-label") ||
2847 !strcmp(attr
->name
, "page-border") ||
2848 !strncmp(attr
->name
, "number-up", 9) ||
2849 !strcmp(attr
->name
, "page-ranges") ||
2850 !strcmp(attr
->name
, "page-set") ||
2851 !strcasecmp(attr
->name
, "AP_FIRSTPAGE_InputSlot") ||
2852 !strcasecmp(attr
->name
, "AP_FIRSTPAGE_ManualFeed") ||
2853 !strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
2854 "PMTotalSidesImaged..n.") ||
2855 !strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
2856 "PMTotalBeginPages..n.")) &&
2861 * Otherwise add them to the list...
2864 if (optptr
> options
)
2865 strlcat(optptr
, " ", optlength
- (optptr
- options
));
2867 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
2869 strlcat(optptr
, attr
->name
, optlength
- (optptr
- options
));
2870 strlcat(optptr
, "=", optlength
- (optptr
- options
));
2873 for (i
= 0; i
< attr
->num_values
; i
++)
2876 strlcat(optptr
, ",", optlength
- (optptr
- options
));
2878 optptr
+= strlen(optptr
);
2880 switch (attr
->value_tag
)
2882 case IPP_TAG_INTEGER
:
2884 snprintf(optptr
, optlength
- (optptr
- options
),
2885 "%d", attr
->values
[i
].integer
);
2888 case IPP_TAG_BOOLEAN
:
2889 if (!attr
->values
[i
].boolean
)
2890 strlcat(optptr
, "no", optlength
- (optptr
- options
));
2892 case IPP_TAG_NOVALUE
:
2893 strlcat(optptr
, attr
->name
,
2894 optlength
- (optptr
- options
));
2897 case IPP_TAG_RANGE
:
2898 if (attr
->values
[i
].range
.lower
== attr
->values
[i
].range
.upper
)
2899 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2900 "%d", attr
->values
[i
].range
.lower
);
2902 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2903 "%d-%d", attr
->values
[i
].range
.lower
,
2904 attr
->values
[i
].range
.upper
);
2907 case IPP_TAG_RESOLUTION
:
2908 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2909 "%dx%d%s", attr
->values
[i
].resolution
.xres
,
2910 attr
->values
[i
].resolution
.yres
,
2911 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
2915 case IPP_TAG_STRING
:
2918 case IPP_TAG_KEYWORD
:
2919 case IPP_TAG_CHARSET
:
2920 case IPP_TAG_LANGUAGE
:
2922 for (valptr
= attr
->values
[i
].string
.text
; *valptr
;)
2924 if (strchr(" \t\n\\\'\"", *valptr
))
2926 *optptr
++ = *valptr
++;
2933 break; /* anti-compiler-warning-code */
2937 optptr
+= strlen(optptr
);
2947 * 'ipp_length()' - Compute the size of the buffer needed to hold
2948 * the textual IPP attributes.
2951 static int /* O - Size of attribute buffer */
2952 ipp_length(ipp_t
*ipp
) /* I - IPP request */
2954 int bytes
; /* Number of bytes */
2955 int i
; /* Looping var */
2956 ipp_attribute_t
*attr
; /* Current attribute */
2960 * Loop through all attributes...
2965 for (attr
= ipp
->attrs
; attr
!= NULL
; attr
= attr
->next
)
2968 * Skip attributes that won't be sent to filters...
2971 if (attr
->value_tag
== IPP_TAG_MIMETYPE
||
2972 attr
->value_tag
== IPP_TAG_NAMELANG
||
2973 attr
->value_tag
== IPP_TAG_TEXTLANG
||
2974 attr
->value_tag
== IPP_TAG_URI
||
2975 attr
->value_tag
== IPP_TAG_URISCHEME
)
2978 if (strncmp(attr
->name
, "time-", 5) == 0)
2982 * Add space for a leading space and commas between each value.
2983 * For the first attribute, the leading space isn't used, so the
2984 * extra byte can be used as the nul terminator...
2987 bytes
++; /* " " separator */
2988 bytes
+= attr
->num_values
; /* "," separators */
2991 * Boolean attributes appear as "foo,nofoo,foo,nofoo", while
2992 * other attributes appear as "foo=value1,value2,...,valueN".
2995 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
2996 bytes
+= strlen(attr
->name
);
2998 bytes
+= attr
->num_values
* strlen(attr
->name
);
3001 * Now add the size required for each value in the attribute...
3004 switch (attr
->value_tag
)
3006 case IPP_TAG_INTEGER
:
3009 * Minimum value of a signed integer is -2147483647, or 11 digits.
3012 bytes
+= attr
->num_values
* 11;
3015 case IPP_TAG_BOOLEAN
:
3017 * Add two bytes for each false ("no") value...
3020 for (i
= 0; i
< attr
->num_values
; i
++)
3021 if (!attr
->values
[i
].boolean
)
3025 case IPP_TAG_RANGE
:
3027 * A range is two signed integers separated by a hyphen, or
3028 * 23 characters max.
3031 bytes
+= attr
->num_values
* 23;
3034 case IPP_TAG_RESOLUTION
:
3036 * A resolution is two signed integers separated by an "x" and
3037 * suffixed by the units, or 26 characters max.
3040 bytes
+= attr
->num_values
* 26;
3043 case IPP_TAG_STRING
:
3046 case IPP_TAG_KEYWORD
:
3047 case IPP_TAG_CHARSET
:
3048 case IPP_TAG_LANGUAGE
:
3051 * Strings can contain characters that need quoting. We need
3052 * at least 2 * len + 2 characters to cover the quotes and
3053 * any backslashes in the string.
3056 for (i
= 0; i
< attr
->num_values
; i
++)
3057 bytes
+= 2 * strlen(attr
->values
[i
].string
.text
) + 2;
3061 break; /* anti-compiler-warning-code */
3070 * 'load_job_cache()' - Load jobs from the job.cache file.
3074 load_job_cache(const char *filename
) /* I - job.cache filename */
3076 cups_file_t
*fp
; /* job.cache file */
3077 char line
[1024], /* Line buffer */
3078 *value
; /* Value on line */
3079 int linenum
; /* Line number in file */
3080 cupsd_job_t
*job
; /* Current job */
3081 int jobid
; /* Job ID */
3082 char jobfile
[1024]; /* Job filename */
3086 * Open the job.cache file...
3089 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
3091 if (errno
!= ENOENT
)
3092 cupsdLogMessage(CUPSD_LOG_ERROR
,
3093 "Unable to open job cache file \"%s\": %s",
3094 filename
, strerror(errno
));
3096 load_request_root();
3102 * Read entries from the job cache file and create jobs as needed.
3105 cupsdLogMessage(CUPSD_LOG_INFO
, "Loading job cache file \"%s\"...",
3111 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
3113 if (!strcasecmp(line
, "NextJobId"))
3116 NextJobId
= atoi(value
);
3118 else if (!strcasecmp(line
, "<Job"))
3122 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing </Job> directive on line %d!",
3129 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing job ID on line %d!", linenum
);
3133 jobid
= atoi(value
);
3137 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad job ID %d on line %d!", jobid
,
3142 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, jobid
);
3143 if (access(jobfile
, 0))
3145 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Job %d] Files have gone away!",
3150 job
= calloc(1, sizeof(cupsd_job_t
));
3153 cupsdLogMessage(CUPSD_LOG_EMERG
,
3154 "[Job %d] Unable to allocate memory for job!", jobid
);
3159 job
->back_pipes
[0] = -1;
3160 job
->back_pipes
[1] = -1;
3161 job
->print_pipes
[0] = -1;
3162 job
->print_pipes
[1] = -1;
3163 job
->side_pipes
[0] = -1;
3164 job
->side_pipes
[1] = -1;
3165 job
->status_pipes
[0] = -1;
3166 job
->status_pipes
[1] = -1;
3168 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Loading from cache...",
3173 cupsdLogMessage(CUPSD_LOG_ERROR
,
3174 "Missing <Job #> directive on line %d!", linenum
);
3177 else if (!strcasecmp(line
, "</Job>"))
3179 cupsArrayAdd(Jobs
, job
);
3181 if (job
->state_value
<= IPP_JOB_STOPPED
)
3183 cupsArrayAdd(ActiveJobs
, job
);
3191 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing value on line %d!", linenum
);
3194 else if (!strcasecmp(line
, "State"))
3196 job
->state_value
= (ipp_jstate_t
)atoi(value
);
3198 if (job
->state_value
< IPP_JOB_PENDING
)
3199 job
->state_value
= IPP_JOB_PENDING
;
3200 else if (job
->state_value
> IPP_JOB_COMPLETED
)
3201 job
->state_value
= IPP_JOB_COMPLETED
;
3203 else if (!strcasecmp(line
, "HoldUntil"))
3205 job
->hold_until
= atoi(value
);
3207 else if (!strcasecmp(line
, "Priority"))
3209 job
->priority
= atoi(value
);
3211 else if (!strcasecmp(line
, "Username"))
3213 cupsdSetString(&job
->username
, value
);
3215 else if (!strcasecmp(line
, "Destination"))
3217 cupsdSetString(&job
->dest
, value
);
3219 else if (!strcasecmp(line
, "DestType"))
3221 job
->dtype
= (cups_ptype_t
)atoi(value
);
3223 else if (!strcasecmp(line
, "NumFiles"))
3225 job
->num_files
= atoi(value
);
3227 if (job
->num_files
< 0)
3229 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad NumFiles value %d on line %d!",
3230 job
->num_files
, linenum
);
3235 if (job
->num_files
> 0)
3237 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-001", RequestRoot
,
3239 if (access(jobfile
, 0))
3241 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Data files have gone away!",
3247 job
->filetypes
= calloc(job
->num_files
, sizeof(mime_type_t
*));
3248 job
->compressions
= calloc(job
->num_files
, sizeof(int));
3250 if (!job
->filetypes
|| !job
->compressions
)
3252 cupsdLogMessage(CUPSD_LOG_EMERG
,
3253 "[Job %d] Unable to allocate memory for %d files!",
3254 job
->id
, job
->num_files
);
3259 else if (!strcasecmp(line
, "File"))
3261 int number
, /* File number */
3262 compression
; /* Compression value */
3263 char super
[MIME_MAX_SUPER
], /* MIME super type */
3264 type
[MIME_MAX_TYPE
]; /* MIME type */
3267 if (sscanf(value
, "%d%*[ \t]%15[^/]/%255s%d", &number
, super
, type
,
3270 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File on line %d!", linenum
);
3274 if (number
< 1 || number
> job
->num_files
)
3276 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File number %d on line %d!",
3283 job
->compressions
[number
] = compression
;
3284 job
->filetypes
[number
] = mimeType(MimeDatabase
, super
, type
);
3286 if (!job
->filetypes
[number
])
3289 * If the original MIME type is unknown, auto-type it!
3292 cupsdLogMessage(CUPSD_LOG_ERROR
,
3293 "[Job %d] Unknown MIME type %s/%s for file %d!",
3294 job
->id
, super
, type
, number
+ 1);
3296 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
3297 job
->id
, number
+ 1);
3298 job
->filetypes
[number
] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
3299 job
->compressions
+ number
);
3302 * If that didn't work, assume it is raw...
3305 if (!job
->filetypes
[number
])
3306 job
->filetypes
[number
] = mimeType(MimeDatabase
, "application",
3311 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unknown %s directive on line %d!",
3320 * 'load_next_job_id()' - Load the NextJobId value from the job.cache file.
3324 load_next_job_id(const char *filename
) /* I - job.cache filename */
3326 cups_file_t
*fp
; /* job.cache file */
3327 char line
[1024], /* Line buffer */
3328 *value
; /* Value on line */
3329 int linenum
; /* Line number in file */
3330 int next_job_id
; /* NextJobId value from line */
3334 * Read the NextJobId directive from the job.cache file and use
3335 * the value (if any).
3338 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
3340 if (errno
!= ENOENT
)
3341 cupsdLogMessage(CUPSD_LOG_ERROR
,
3342 "Unable to open job cache file \"%s\": %s",
3343 filename
, strerror(errno
));
3348 cupsdLogMessage(CUPSD_LOG_INFO
,
3349 "Loading NextJobId from job cache file \"%s\"...", filename
);
3353 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
3355 if (!strcasecmp(line
, "NextJobId"))
3359 next_job_id
= atoi(value
);
3361 if (next_job_id
> NextJobId
)
3362 NextJobId
= next_job_id
;
3373 * 'load_request_root()' - Load jobs from the RequestRoot directory.
3377 load_request_root(void)
3379 cups_dir_t
*dir
; /* Directory */
3380 cups_dentry_t
*dent
; /* Directory entry */
3381 cupsd_job_t
*job
; /* New job */
3385 * Open the requests directory...
3388 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Scanning %s for jobs...", RequestRoot
);
3390 if ((dir
= cupsDirOpen(RequestRoot
)) == NULL
)
3392 cupsdLogMessage(CUPSD_LOG_ERROR
,
3393 "Unable to open spool directory \"%s\": %s",
3394 RequestRoot
, strerror(errno
));
3399 * Read all the c##### files...
3402 while ((dent
= cupsDirRead(dir
)) != NULL
)
3403 if (strlen(dent
->filename
) >= 6 && dent
->filename
[0] == 'c')
3406 * Allocate memory for the job...
3409 if ((job
= calloc(sizeof(cupsd_job_t
), 1)) == NULL
)
3411 cupsdLogMessage(CUPSD_LOG_ERROR
, "Ran out of memory for jobs!");
3417 * Assign the job ID...
3420 job
->id
= atoi(dent
->filename
+ 1);
3421 job
->back_pipes
[0] = -1;
3422 job
->back_pipes
[1] = -1;
3423 job
->print_pipes
[0] = -1;
3424 job
->print_pipes
[1] = -1;
3425 job
->side_pipes
[0] = -1;
3426 job
->side_pipes
[1] = -1;
3427 job
->status_pipes
[0] = -1;
3428 job
->status_pipes
[1] = -1;
3430 if (job
->id
>= NextJobId
)
3431 NextJobId
= job
->id
+ 1;
3440 * Insert the job into the array, sorting by job priority and ID...
3443 cupsArrayAdd(Jobs
, job
);
3445 if (job
->state_value
<= IPP_JOB_STOPPED
)
3446 cupsArrayAdd(ActiveJobs
, job
);
3456 * 'set_hold_until()' - Set the hold time and update job-hold-until attribute.
3460 set_hold_until(cupsd_job_t
*job
, /* I - Job to update */
3461 time_t holdtime
) /* I - Hold until time */
3463 ipp_attribute_t
*attr
; /* job-hold-until attribute */
3464 struct tm
*holddate
; /* Hold date */
3465 char holdstr
[64]; /* Hold time */
3469 * Set the hold_until value and hold the job...
3472 cupsdLogMessage(CUPSD_LOG_DEBUG
, "set_hold_until: hold_until = %d",
3475 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
3476 job
->state_value
= IPP_JOB_HELD
;
3477 job
->hold_until
= holdtime
;
3480 * Update the job-hold-until attribute with a string representing GMT
3481 * time (HH:MM:SS)...
3484 holddate
= gmtime(&holdtime
);
3485 snprintf(holdstr
, sizeof(holdstr
), "%d:%d:%d", holddate
->tm_hour
,
3486 holddate
->tm_min
, holddate
->tm_sec
);
3488 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
3489 IPP_TAG_KEYWORD
)) == NULL
)
3490 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
3493 * Either add the attribute or update the value of the existing one
3497 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "job-hold-until",
3500 cupsdSetString(&attr
->values
[0].string
.text
, holdstr
);
3503 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
3508 * 'set_time()' - Set one of the "time-at-xyz" attributes.
3512 set_time(cupsd_job_t
*job
, /* I - Job to update */
3513 const char *name
) /* I - Name of attribute */
3515 ipp_attribute_t
*attr
; /* Time attribute */
3518 if ((attr
= ippFindAttribute(job
->attrs
, name
, IPP_TAG_ZERO
)) != NULL
)
3520 attr
->value_tag
= IPP_TAG_INTEGER
;
3521 attr
->values
[0].integer
= time(NULL
);
3527 * 'start_job()' - Start a print job.
3531 start_job(cupsd_job_t
*job
, /* I - Job ID */
3532 cupsd_printer_t
*printer
) /* I - Printer to print job */
3534 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "start_job(job=%p(%d), printer=%p(%s))",
3535 job
, job
->id
, printer
, printer
->name
);
3538 * Make sure we have some files around before we try to print...
3541 if (job
->num_files
== 0)
3543 cupsdSetJobState(job
, IPP_JOB_ABORTED
, CUPSD_JOB_DEFAULT
,
3544 "Aborting job because it has no files.");
3549 * Update the printer and job state to "processing"...
3552 if (!cupsdLoadJob(job
))
3555 cupsdSetJobState(job
, IPP_JOB_PROCESSING
, CUPSD_JOB_DEFAULT
, NULL
);
3556 cupsdSetPrinterState(printer
, IPP_PRINTER_PROCESSING
, 0);
3559 job
->current_file
= 0;
3561 job
->printer
= printer
;
3565 * Setup the last exit status and security profiles...
3569 job
->profile
= cupsdCreateProfile(job
->id
);
3572 * Create the status pipes and buffer...
3575 if (cupsdOpenPipe(job
->status_pipes
))
3577 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
3578 "Unable to create job status pipes - %s.", strerror(errno
));
3580 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
3581 "Job stopped because the scheduler could not create the "
3582 "job status pipes.");
3584 cupsdDestroyProfile(job
->profile
);
3585 job
->profile
= NULL
;
3589 job
->status_buffer
= cupsdStatBufNew(job
->status_pipes
[0], NULL
);
3590 job
->status_level
= CUPSD_LOG_INFO
;
3593 * Create the backchannel pipes and make them non-blocking...
3596 if (cupsdOpenPipe(job
->back_pipes
))
3598 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
3599 "Unable to create back-channel pipes - %s.", strerror(errno
));
3601 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
3602 "Job stopped because the scheduler could not create the "
3603 "back-channel pipes.");
3605 cupsdClosePipe(job
->status_pipes
);
3606 cupsdStatBufDelete(job
->status_buffer
);
3607 job
->status_buffer
= NULL
;
3609 cupsdDestroyProfile(job
->profile
);
3610 job
->profile
= NULL
;
3614 fcntl(job
->back_pipes
[0], F_SETFL
,
3615 fcntl(job
->back_pipes
[0], F_GETFL
) | O_NONBLOCK
);
3616 fcntl(job
->back_pipes
[1], F_SETFL
,
3617 fcntl(job
->back_pipes
[1], F_GETFL
) | O_NONBLOCK
);
3620 * Create the side-channel pipes and make them non-blocking...
3623 if (socketpair(AF_LOCAL
, SOCK_STREAM
, 0, job
->side_pipes
))
3625 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
3626 "Unable to create side-channel pipes - %s.", strerror(errno
));
3628 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
3629 "Job stopped because the scheduler could not create the "
3630 "side-channel pipes.");
3632 cupsdClosePipe(job
->back_pipes
);
3634 cupsdClosePipe(job
->status_pipes
);
3635 cupsdStatBufDelete(job
->status_buffer
);
3636 job
->status_buffer
= NULL
;
3638 cupsdDestroyProfile(job
->profile
);
3639 job
->profile
= NULL
;
3643 fcntl(job
->side_pipes
[0], F_SETFL
,
3644 fcntl(job
->side_pipes
[0], F_GETFL
) | O_NONBLOCK
);
3645 fcntl(job
->side_pipes
[1], F_SETFL
,
3646 fcntl(job
->side_pipes
[1], F_GETFL
) | O_NONBLOCK
);
3649 * Now start the first file in the job...
3652 cupsdContinueJob(job
);
3657 * 'stop_job()' - Stop a print job.
3661 stop_job(cupsd_job_t
*job
, /* I - Job */
3662 cupsd_jobaction_t action
) /* I - Action */
3664 int i
; /* Looping var */
3667 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "stop_job(job=%p(%d), action=%d)", job
,
3670 FilterLevel
-= job
->cost
;
3673 for (i
= 0; job
->filters
[i
]; i
++)
3674 if (job
->filters
[i
] > 0)
3675 cupsdEndProcess(job
->filters
[i
], action
== CUPSD_JOB_FORCE
);
3677 if (job
->backend
> 0)
3678 cupsdEndProcess(job
->backend
, action
== CUPSD_JOB_FORCE
);
3683 * 'unload_job()' - Unload a job from memory.
3687 unload_job(cupsd_job_t
*job
) /* I - Job */
3692 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Unloading...", job
->id
);
3694 ippDelete(job
->attrs
);
3699 job
->job_sheets
= NULL
;
3700 job
->printer_message
= NULL
;
3701 job
->printer_reasons
= NULL
;
3706 * 'update_job()' - Read a status update from a job's filters.
3710 update_job(cupsd_job_t
*job
) /* I - Job to check */
3712 int i
; /* Looping var */
3713 int copies
; /* Number of copies printed */
3714 char message
[1024], /* Message text */
3715 *ptr
; /* Pointer update... */
3716 int loglevel
, /* Log level for message */
3717 event
= 0; /* Events? */
3718 static const char * const levels
[] = /* Log levels */
3734 * Get the printer associated with this job; if the printer is stopped for
3735 * any reason then job->printer will be reset to NULL, so make sure we have
3736 * a valid pointer...
3739 while ((ptr
= cupsdStatBufUpdate(job
->status_buffer
, &loglevel
,
3740 message
, sizeof(message
))) != NULL
)
3743 * Process page and printer state messages as needed...
3746 if (loglevel
== CUPSD_LOG_PAGE
)
3749 * Page message; send the message to the page_log file and update the
3750 * job sheet count...
3753 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "PAGE: %s", message
);
3757 if (!strncasecmp(message
, "total ", 6))
3760 * Got a total count of pages from a backend or filter...
3763 copies
= atoi(message
+ 6);
3764 copies
-= job
->sheets
->values
[0].integer
; /* Just track the delta */
3766 else if (!sscanf(message
, "%*d%d", &copies
))
3769 job
->sheets
->values
[0].integer
+= copies
;
3771 if (job
->printer
->page_limit
)
3773 cupsd_quota_t
*q
= cupsdUpdateQuota(job
->printer
, job
->username
,
3777 if (AppleQuotas
&& q
->page_count
== -3)
3780 * Quota limit exceeded, cancel job in progress immediately...
3783 cupsdSetJobState(job
, IPP_JOB_CANCELED
, CUPSD_JOB_DEFAULT
,
3784 "Canceled job because pages exceed user %s "
3785 "quota limit on printer %s (%s).",
3786 job
->username
, job
->printer
->name
,
3787 job
->printer
->info
);
3792 #endif /* __APPLE__ */
3796 cupsdLogPage(job
, message
);
3799 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
3800 "Printed %d page(s).", job
->sheets
->values
[0].integer
);
3802 else if (loglevel
== CUPSD_LOG_STATE
)
3804 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "STATE: %s", message
);
3806 if (!strcmp(message
, "paused"))
3808 cupsdStopPrinter(job
->printer
, 1);
3813 cupsdSetPrinterReasons(job
->printer
, message
);
3814 cupsdAddPrinterHistory(job
->printer
);
3815 event
|= CUPSD_EVENT_PRINTER_STATE
;
3818 update_job_attrs(job
, 0);
3820 else if (loglevel
== CUPSD_LOG_ATTR
)
3823 * Set attribute(s)...
3826 int num_attrs
; /* Number of attributes */
3827 cups_option_t
*attrs
; /* Attributes */
3828 const char *attr
; /* Attribute */
3831 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "ATTR: %s", message
);
3833 num_attrs
= cupsParseOptions(message
, 0, &attrs
);
3835 if ((attr
= cupsGetOption("auth-info-required", num_attrs
,
3838 cupsdSetAuthInfoRequired(job
->printer
, attr
, NULL
);
3839 cupsdSetPrinterAttrs(job
->printer
);
3841 if (job
->printer
->type
& CUPS_PRINTER_DISCOVERED
)
3842 cupsdMarkDirty(CUPSD_DIRTY_REMOTE
);
3844 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3847 if ((attr
= cupsGetOption("job-media-progress", num_attrs
,
3850 int progress
= atoi(attr
);
3853 if (progress
>= 0 && progress
<= 100)
3855 job
->progress
= progress
;
3858 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
3859 "Printing page %d, %d%%",
3860 job
->sheets
->values
[0].integer
, job
->progress
);
3864 if ((attr
= cupsGetOption("printer-alert", num_attrs
, attrs
)) != NULL
)
3866 cupsdSetString(&job
->printer
->alert
, attr
);
3867 event
|= CUPSD_EVENT_PRINTER_STATE
;
3870 if ((attr
= cupsGetOption("printer-alert-description", num_attrs
,
3873 cupsdSetString(&job
->printer
->alert_description
, attr
);
3874 event
|= CUPSD_EVENT_PRINTER_STATE
;
3877 if ((attr
= cupsGetOption("marker-colors", num_attrs
, attrs
)) != NULL
)
3879 cupsdSetPrinterAttr(job
->printer
, "marker-colors", (char *)attr
);
3880 job
->printer
->marker_time
= time(NULL
);
3881 event
|= CUPSD_EVENT_PRINTER_STATE
;
3882 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3885 if ((attr
= cupsGetOption("marker-levels", num_attrs
, attrs
)) != NULL
)
3887 cupsdSetPrinterAttr(job
->printer
, "marker-levels", (char *)attr
);
3888 job
->printer
->marker_time
= time(NULL
);
3889 event
|= CUPSD_EVENT_PRINTER_STATE
;
3890 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3893 if ((attr
= cupsGetOption("marker-low-levels", num_attrs
, attrs
)) != NULL
)
3895 cupsdSetPrinterAttr(job
->printer
, "marker-low-levels", (char *)attr
);
3896 job
->printer
->marker_time
= time(NULL
);
3897 event
|= CUPSD_EVENT_PRINTER_STATE
;
3898 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3901 if ((attr
= cupsGetOption("marker-high-levels", num_attrs
, attrs
)) != NULL
)
3903 cupsdSetPrinterAttr(job
->printer
, "marker-high-levels", (char *)attr
);
3904 job
->printer
->marker_time
= time(NULL
);
3905 event
|= CUPSD_EVENT_PRINTER_STATE
;
3906 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3909 if ((attr
= cupsGetOption("marker-message", num_attrs
, attrs
)) != NULL
)
3911 cupsdSetPrinterAttr(job
->printer
, "marker-message", (char *)attr
);
3912 job
->printer
->marker_time
= time(NULL
);
3913 event
|= CUPSD_EVENT_PRINTER_STATE
;
3914 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3917 if ((attr
= cupsGetOption("marker-names", num_attrs
, attrs
)) != NULL
)
3919 cupsdSetPrinterAttr(job
->printer
, "marker-names", (char *)attr
);
3920 job
->printer
->marker_time
= time(NULL
);
3921 event
|= CUPSD_EVENT_PRINTER_STATE
;
3922 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3925 if ((attr
= cupsGetOption("marker-types", num_attrs
, attrs
)) != NULL
)
3927 cupsdSetPrinterAttr(job
->printer
, "marker-types", (char *)attr
);
3928 job
->printer
->marker_time
= time(NULL
);
3929 event
|= CUPSD_EVENT_PRINTER_STATE
;
3930 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3933 cupsFreeOptions(num_attrs
, attrs
);
3935 else if (loglevel
== CUPSD_LOG_PPD
)
3938 * Set attribute(s)...
3941 int num_keywords
; /* Number of keywords */
3942 cups_option_t
*keywords
; /* Keywords */
3945 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "PPD: %s", message
);
3947 num_keywords
= cupsParseOptions(message
, 0, &keywords
);
3949 if (cupsdUpdatePrinterPPD(job
->printer
, num_keywords
, keywords
))
3950 cupsdSetPrinterAttrs(job
->printer
);
3952 cupsFreeOptions(num_keywords
, keywords
);
3955 else if (!strncmp(message
, "recoverable:", 12))
3958 while (isspace(*ptr
& 255))
3963 cupsdSetPrinterReasons(job
->printer
,
3964 "+com.apple.print.recoverable-warning");
3965 cupsdSetString(&(job
->printer
->recoverable
), ptr
);
3966 cupsdAddPrinterHistory(job
->printer
);
3967 event
|= CUPSD_EVENT_PRINTER_STATE
;
3970 else if (!strncmp(message
, "recovered:", 10))
3972 cupsdSetPrinterReasons(job
->printer
,
3973 "-com.apple.print.recoverable-warning");
3976 while (isspace(*ptr
& 255))
3979 cupsdSetString(&(job
->printer
->recoverable
), ptr
);
3980 cupsdAddPrinterHistory(job
->printer
);
3981 event
|= CUPSD_EVENT_PRINTER_STATE
;
3983 #endif /* __APPLE__ */
3986 if (loglevel
!= CUPSD_LOG_INFO
&& loglevel
> LogLevel
)
3987 cupsdLogJob(job
, loglevel
, "%s", message
);
3989 if (loglevel
< CUPSD_LOG_DEBUG
)
3991 strlcpy(job
->printer
->state_message
, message
,
3992 sizeof(job
->printer
->state_message
));
3993 cupsdAddPrinterHistory(job
->printer
);
3995 event
|= CUPSD_EVENT_PRINTER_STATE
| CUPSD_EVENT_JOB_PROGRESS
;
3997 if (loglevel
<= job
->status_level
)
4000 * Some messages show in the job-printer-state-message attribute...
4003 if (loglevel
!= CUPSD_LOG_NOTICE
)
4004 job
->status_level
= loglevel
;
4006 update_job_attrs(job
, 1);
4008 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
4009 "Set job-printer-state-message to \"%s\", "
4011 job
->printer_message
->values
[0].string
.text
,
4012 levels
[job
->status_level
]);
4017 if (!strchr(job
->status_buffer
->buffer
, '\n'))
4021 if (event
& CUPSD_EVENT_PRINTER_STATE
)
4022 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, job
->printer
, NULL
,
4023 (job
->printer
->type
& CUPS_PRINTER_CLASS
) ?
4024 "Class \"%s\" state changed." :
4025 "Printer \"%s\" state changed.",
4026 job
->printer
->name
);
4028 if (event
& CUPSD_EVENT_JOB_PROGRESS
)
4029 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
4030 "%s", job
->printer
->state_message
);
4032 if (ptr
== NULL
&& !job
->status_buffer
->bufused
)
4035 * See if all of the filters and the backend have returned their
4039 for (i
= 0; job
->filters
[i
] < 0; i
++);
4041 if (job
->filters
[i
])
4044 * EOF but we haven't collected the exit status of all filters...
4047 cupsdCheckProcess();
4051 if (job
->current_file
>= job
->num_files
&& job
->backend
> 0)
4054 * EOF but we haven't collected the exit status of the backend...
4057 cupsdCheckProcess();
4062 * Handle the end of job stuff...
4068 * Check for new jobs...
4077 * 'update_job_attrs()' - Update the job-printer-* attributes.
4081 update_job_attrs(cupsd_job_t
*job
, /* I - Job to update */
4082 int do_message
)/* I - 1 = copy job-printer-state message */
4084 int i
; /* Looping var */
4085 int num_reasons
; /* Actual number of reasons */
4086 const char * const *reasons
; /* Reasons */
4087 static const char *none
= "none"; /* "none" reason */
4091 * Get/create the job-printer-state-* attributes...
4094 if (!job
->printer_message
)
4096 if ((job
->printer_message
= ippFindAttribute(job
->attrs
,
4097 "job-printer-state-message",
4098 IPP_TAG_TEXT
)) == NULL
)
4099 job
->printer_message
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_TEXT
,
4100 "job-printer-state-message",
4104 if (!job
->printer_reasons
)
4105 job
->printer_reasons
= ippFindAttribute(job
->attrs
,
4106 "job-printer-state-reasons",
4110 * Copy or clear the printer-state-message value as needed...
4113 if (job
->state_value
!= IPP_JOB_PROCESSING
&&
4114 job
->status_level
== CUPSD_LOG_INFO
)
4115 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
), "");
4116 else if (job
->printer
->state_message
[0] && do_message
)
4117 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
),
4118 job
->printer
->state_message
);
4121 * ... and the printer-state-reasons value...
4124 if (job
->printer
->num_reasons
== 0)
4131 num_reasons
= job
->printer
->num_reasons
;
4132 reasons
= (const char * const *)job
->printer
->reasons
;
4135 if (!job
->printer_reasons
|| job
->printer_reasons
->num_values
!= num_reasons
)
4138 * Replace/create a job-printer-state-reasons attribute...
4141 ippDeleteAttribute(job
->attrs
, job
->printer_reasons
);
4143 job
->printer_reasons
= ippAddStrings(job
->attrs
,
4144 IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
4145 "job-printer-state-reasons",
4146 num_reasons
, NULL
, NULL
);
4151 * Don't bother clearing the reason strings if they are the same...
4154 for (i
= 0; i
< num_reasons
; i
++)
4155 if (strcmp(job
->printer_reasons
->values
[i
].string
.text
, reasons
[i
]))
4158 if (i
>= num_reasons
)
4162 * Not the same, so free the current strings...
4165 for (i
= 0; i
< num_reasons
; i
++)
4166 _cupsStrFree(job
->printer_reasons
->values
[i
].string
.text
);
4170 * Copy the reasons...
4173 for (i
= 0; i
< num_reasons
; i
++)
4174 job
->printer_reasons
->values
[i
].string
.text
= _cupsStrAlloc(reasons
[i
]);
4179 * End of "$Id: job.c 7902 2008-09-03 14:20:17Z mike $".