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 */
274 time_t curtime
; /* Current time */
277 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
278 "cupsdCheckJobs: %d active jobs, sleeping=%d, reload=%d",
279 cupsArrayCount(ActiveJobs
), Sleeping
, NeedReload
);
281 curtime
= time(NULL
);
283 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
285 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
288 * Kill jobs if they are unresponsive...
291 if (job
->kill_time
&& job
->kill_time
<= curtime
)
293 stop_job(job
, CUPSD_JOB_FORCE
);
298 * Start held jobs if they are ready...
301 if (job
->state_value
== IPP_JOB_HELD
&&
303 job
->hold_until
< curtime
)
305 if (job
->pending_timeout
)
308 * This job is pending; check that we don't have an active Send-Document
309 * operation in progress on any of the client connections, then timeout
310 * the job so we can start printing...
313 cupsd_client_t
*con
; /* Current client connection */
316 for (con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
318 con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
320 con
->request
->request
.op
.operation_id
== IPP_SEND_DOCUMENT
)
326 if (cupsdTimeoutJob(job
))
330 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
331 "Job submission timed out.");
335 * Start pending jobs if the destination is available...
338 if (job
->state_value
== IPP_JOB_PENDING
&& !NeedReload
&& !Sleeping
&&
341 printer
= cupsdFindDest(job
->dest
);
345 (printer
->type
& (CUPS_PRINTER_IMPLICIT
| CUPS_PRINTER_CLASS
)))
348 * If the class is remote, just pass it to the remote server...
353 if (pclass
->state
== IPP_PRINTER_STOPPED
)
355 else if (pclass
->type
& CUPS_PRINTER_REMOTE
)
358 printer
= cupsdFindAvailablePrinter(printer
->name
);
361 if (!printer
&& !pclass
)
364 * Whoa, the printer and/or class for this destination went away;
368 cupsdSetJobState(job
, IPP_JOB_ABORTED
, CUPSD_JOB_PURGE
,
369 "Job aborted because the destination printer/class "
372 else if (printer
&& !printer
->holding_new_jobs
)
375 * See if the printer is available or remote and not printing a job;
376 * if so, start the job...
382 * Add/update a job-actual-printer-uri attribute for this job
383 * so that we know which printer actually printed the job...
386 if ((attr
= ippFindAttribute(job
->attrs
, "job-actual-printer-uri",
387 IPP_TAG_URI
)) != NULL
)
388 cupsdSetString(&attr
->values
[0].string
.text
, printer
->uri
);
390 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
,
391 "job-actual-printer-uri", NULL
, printer
->uri
);
394 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
397 if ((!(printer
->type
& CUPS_PRINTER_DISCOVERED
) && /* Printer is local */
398 printer
->state
== IPP_PRINTER_IDLE
) || /* and idle, OR */
399 ((printer
->type
& CUPS_PRINTER_DISCOVERED
) && /* Printer is remote */
400 !printer
->job
)) /* and not printing */
406 start_job(job
, printer
);
415 * 'cupsdCleanJobs()' - Clean out old jobs.
421 cupsd_job_t
*job
; /* Current job */
427 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
428 job
&& cupsArrayCount(Jobs
) >= MaxJobs
;
429 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
430 if (job
->state_value
>= IPP_JOB_CANCELED
&& !job
->printer
)
431 cupsdDeleteJob(job
, CUPSD_JOB_PURGE
);
436 * 'cupsdContinueJob()' - Continue printing with the next file in a job.
440 cupsdContinueJob(cupsd_job_t
*job
) /* I - Job */
442 int i
; /* Looping var */
443 int slot
; /* Pipe slot */
444 cups_array_t
*filters
, /* Filters for job */
445 *prefilters
; /* Filters with prefilters */
446 mime_filter_t
*filter
, /* Current filter */
447 *prefilter
, /* Prefilter */
448 port_monitor
; /* Port monitor filter */
449 char scheme
[255]; /* Device URI scheme */
450 ipp_attribute_t
*attr
; /* Current attribute */
451 const char *ptr
, /* Pointer into value */
452 *abort_message
; /* Abort message */
453 struct stat backinfo
; /* Backend file information */
454 int backroot
; /* Run backend as root? */
455 int pid
; /* Process ID of new filter process */
456 int banner_page
; /* 1 if banner page, 0 otherwise */
457 int filterfds
[2][2];/* Pipes used between filters */
458 int envc
; /* Number of environment variables */
459 char **argv
, /* Filter command-line arguments */
460 filename
[1024], /* Job filename */
461 command
[1024], /* Full path to command */
462 jobid
[255], /* Job ID string */
464 /* Job title string */
465 copies
[255], /* # copies string */
466 *options
, /* Options string */
468 /* Environment variables */
469 charset
[255], /* CHARSET env variable */
470 class_name
[255],/* CLASS env variable */
471 classification
[1024],
472 /* CLASSIFICATION env variable */
474 /* CONTENT_TYPE env variable */
476 /* DEVICE_URI env variable */
477 final_content_type
[1024],
478 /* FINAL_CONTENT_TYPE env variable */
479 lang
[255], /* LANG env variable */
482 /* APPLE_LANGUAGE env variable */
483 #endif /* __APPLE__ */
484 ppd
[1024], /* PPD env variable */
486 /* PRINTER_INFO env variable */
487 printer_location
[255],
488 /* PRINTER_LOCATION env variable */
490 /* PRINTER env variable */
492 /* RIP_MAX_CACHE env variable */
495 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
496 "cupsdContinueJob(job=%p(%d)): current_file=%d, num_files=%d",
497 job
, job
->id
, job
->current_file
, job
->num_files
);
500 * Figure out what filters are required to convert from
501 * the source to the destination type...
504 FilterLevel
-= job
->cost
;
508 if (job
->printer
->raw
)
511 * Remote jobs and raw queues go directly to the printer without
515 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Sending job to queue tagged as raw...");
522 * Local jobs get filtered...
525 filters
= mimeFilter(MimeDatabase
, job
->filetypes
[job
->current_file
],
526 job
->printer
->filetype
, &(job
->cost
));
530 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
531 "Unable to convert file %d to printable format!",
534 job
->current_file
++;
536 if (job
->current_file
== job
->num_files
)
537 cupsdSetJobState(job
, IPP_JOB_ABORTED
, CUPSD_JOB_DEFAULT
,
538 "Aborting job because it cannot be printed.");
544 * Remove NULL ("-") filters...
547 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
549 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
550 if (!strcmp(filter
->filter
, "-"))
551 cupsArrayRemove(filters
, filter
);
553 if (cupsArrayCount(filters
) == 0)
555 cupsArrayDelete(filters
);
560 * If this printer has any pre-filters, insert the required pre-filter
561 * in the filters array...
564 if (job
->printer
->prefiltertype
&& filters
)
566 prefilters
= cupsArrayNew(NULL
, NULL
);
568 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
570 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
572 if ((prefilter
= mimeFilterLookup(MimeDatabase
, filter
->src
,
573 job
->printer
->prefiltertype
)))
575 cupsArrayAdd(prefilters
, prefilter
);
576 job
->cost
+= prefilter
->cost
;
579 cupsArrayAdd(prefilters
, filter
);
582 cupsArrayDelete(filters
);
583 filters
= prefilters
;
588 * Set a minimum cost of 100 for all jobs so that FilterLimit
589 * works with raw queues and other low-cost paths.
596 * See if the filter cost is too high...
599 if ((FilterLevel
+ job
->cost
) > FilterLimit
&& FilterLevel
> 0 &&
603 * Don't print this job quite yet...
606 cupsArrayDelete(filters
);
608 cupsdLogJob(job
, CUPSD_LOG_INFO
,
609 "Holding because filter limit has been reached.");
610 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
611 "cupsdContinueJob: file=%d, cost=%d, level=%d, limit=%d",
612 job
->current_file
, job
->cost
, FilterLevel
,
617 FilterLevel
+= job
->cost
;
620 * Add decompression/raw filter as needed...
623 if ((!job
->printer
->raw
&& job
->compressions
[job
->current_file
]) ||
624 (!filters
&& !job
->printer
->remote
&&
625 (job
->num_files
> 1 || !strncmp(job
->printer
->device_uri
, "file:", 5))))
628 * Add gziptoany filter to the front of the list...
632 filters
= cupsArrayNew(NULL
, NULL
);
634 if (!cupsArrayInsert(filters
, &gziptoany_filter
))
636 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
637 "Unable to add decompression filter - %s", strerror(errno
));
639 cupsArrayDelete(filters
);
641 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
642 "Stopping job because the scheduler ran out of "
645 FilterLevel
-= job
->cost
;
651 * Add port monitor, if any...
654 if (job
->printer
->port_monitor
)
657 * Add port monitor to the end of the list...
661 filters
= cupsArrayNew(NULL
, NULL
);
663 port_monitor
.src
= NULL
;
664 port_monitor
.dst
= NULL
;
665 port_monitor
.cost
= 0;
667 snprintf(port_monitor
.filter
, sizeof(port_monitor
.filter
),
668 "%s/monitor/%s", ServerBin
, job
->printer
->port_monitor
);
670 if (!cupsArrayAdd(filters
, &port_monitor
))
672 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
673 "Unable to add port monitor - %s", strerror(errno
));
675 cupsArrayDelete(filters
);
677 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
678 "Stopping job because the scheduler ran out of "
681 FilterLevel
-= job
->cost
;
687 * Make sure we don't go over the "MAX_FILTERS" limit...
690 if (cupsArrayCount(filters
) > MAX_FILTERS
)
692 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
693 "Too many filters (%d > %d), unable to print!",
694 cupsArrayCount(filters
), MAX_FILTERS
);
696 cupsArrayDelete(filters
);
697 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
698 "Stopping job because it needs too many filters to "
701 FilterLevel
-= job
->cost
;
706 * Determine if we are printing a banner page or not...
709 if (job
->job_sheets
== NULL
)
711 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "No job-sheets attribute.");
712 if ((job
->job_sheets
=
713 ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_ZERO
)) != NULL
)
714 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
715 "... but someone added one without setting job_sheets!");
717 else if (job
->job_sheets
->num_values
== 1)
718 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "job-sheets=%s",
719 job
->job_sheets
->values
[0].string
.text
);
721 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "job-sheets=%s,%s",
722 job
->job_sheets
->values
[0].string
.text
,
723 job
->job_sheets
->values
[1].string
.text
);
725 if (job
->printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
))
727 else if (job
->job_sheets
== NULL
)
729 else if (strcasecmp(job
->job_sheets
->values
[0].string
.text
, "none") != 0 &&
730 job
->current_file
== 0)
732 else if (job
->job_sheets
->num_values
> 1 &&
733 strcasecmp(job
->job_sheets
->values
[1].string
.text
, "none") != 0 &&
734 job
->current_file
== (job
->num_files
- 1))
739 if ((options
= get_options(job
, banner_page
, copies
, sizeof(copies
), title
,
740 sizeof(title
))) == NULL
)
742 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
743 "Stopping job because the scheduler ran out of memory.");
744 cupsArrayDelete(filters
);
746 FilterLevel
-= job
->cost
;
751 * Build the command-line arguments for the filters. Each filter
752 * has 6 or 7 arguments:
760 * argv[6] = filename (optional; normally stdin)
762 * This allows legacy printer drivers that use the old System V
763 * printing interface to be used by CUPS.
765 * For remote jobs, we send all of the files in the argument list.
768 if (job
->printer
->remote
)
769 argv
= calloc(7 + job
->num_files
, sizeof(char *));
771 argv
= calloc(8, sizeof(char *));
775 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Unable to allocate argument array - %s",
777 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
778 "Stopping job because the scheduler ran out of memory.");
779 cupsArrayDelete(filters
);
781 FilterLevel
-= job
->cost
;
785 sprintf(jobid
, "%d", job
->id
);
787 argv
[0] = job
->printer
->name
;
789 argv
[2] = job
->username
;
794 if (job
->printer
->remote
&& job
->num_files
> 1)
796 for (i
= 0; i
< job
->num_files
; i
++)
798 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
800 argv
[6 + i
] = strdup(filename
);
805 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
806 job
->id
, job
->current_file
+ 1);
810 for (i
= 0; argv
[i
]; i
++)
811 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "argv[%d]=\"%s\"", i
, argv
[i
]);
814 * Create environment variable strings for the filters...
817 attr
= ippFindAttribute(job
->attrs
, "attributes-natural-language",
821 strcpy(apple_language
, "APPLE_LANGUAGE=");
822 _cupsAppleLanguage(attr
->values
[0].string
.text
,
823 apple_language
+ 15, sizeof(apple_language
) - 15);
824 #endif /* __APPLE__ */
826 switch (strlen(attr
->values
[0].string
.text
))
830 * This is an unknown or badly formatted language code; use
831 * the POSIX locale...
834 strcpy(lang
, "LANG=C");
839 * Just the language code (ll)...
842 snprintf(lang
, sizeof(lang
), "LANG=%s.UTF-8",
843 attr
->values
[0].string
.text
);
848 * Language and country code (ll-cc)...
851 snprintf(lang
, sizeof(lang
), "LANG=%c%c_%c%c.UTF-8",
852 attr
->values
[0].string
.text
[0],
853 attr
->values
[0].string
.text
[1],
854 toupper(attr
->values
[0].string
.text
[3] & 255),
855 toupper(attr
->values
[0].string
.text
[4] & 255));
859 if ((attr
= ippFindAttribute(job
->attrs
, "document-format",
860 IPP_TAG_MIMETYPE
)) != NULL
&&
861 (ptr
= strstr(attr
->values
[0].string
.text
, "charset=")) != NULL
)
862 snprintf(charset
, sizeof(charset
), "CHARSET=%s", ptr
+ 8);
864 strlcpy(charset
, "CHARSET=utf-8", sizeof(charset
));
866 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s/%s",
867 job
->filetypes
[job
->current_file
]->super
,
868 job
->filetypes
[job
->current_file
]->type
);
869 snprintf(device_uri
, sizeof(device_uri
), "DEVICE_URI=%s",
870 job
->printer
->device_uri
);
871 snprintf(ppd
, sizeof(ppd
), "PPD=%s/ppd/%s.ppd", ServerRoot
,
873 snprintf(printer_info
, sizeof(printer_name
), "PRINTER_INFO=%s",
874 job
->printer
->info
? job
->printer
->info
: "");
875 snprintf(printer_location
, sizeof(printer_name
), "PRINTER_LOCATION=%s",
876 job
->printer
->location
? job
->printer
->location
: "");
877 snprintf(printer_name
, sizeof(printer_name
), "PRINTER=%s", job
->printer
->name
);
878 snprintf(rip_max_cache
, sizeof(rip_max_cache
), "RIP_MAX_CACHE=%s", RIPCache
);
880 envc
= cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
882 envp
[envc
++] = charset
;
883 envp
[envc
++] = lang
;
885 envp
[envc
++] = apple_language
;
886 #endif /* __APPLE__ */
888 envp
[envc
++] = rip_max_cache
;
889 envp
[envc
++] = content_type
;
890 envp
[envc
++] = device_uri
;
891 envp
[envc
++] = printer_info
;
892 envp
[envc
++] = printer_location
;
893 envp
[envc
++] = printer_name
;
894 envp
[envc
++] = banner_page
? "CUPS_FILETYPE=job-sheet" :
895 "CUPS_FILETYPE=document";
897 if (!job
->printer
->remote
&& !job
->printer
->raw
)
899 filter
= (mime_filter_t
*)cupsArrayLast(filters
);
901 if (job
->printer
->port_monitor
)
902 filter
= (mime_filter_t
*)cupsArrayPrev(filters
);
904 if (filter
&& filter
->dst
)
906 snprintf(final_content_type
, sizeof(final_content_type
),
907 "FINAL_CONTENT_TYPE=%s/%s",
908 filter
->dst
->super
, filter
->dst
->type
);
909 envp
[envc
++] = final_content_type
;
913 if (Classification
&& !banner_page
)
915 if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
916 IPP_TAG_NAME
)) == NULL
)
917 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
919 else if (attr
->num_values
> 1 &&
920 strcmp(attr
->values
[1].string
.text
, "none") != 0)
921 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
922 attr
->values
[1].string
.text
);
924 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
925 attr
->values
[0].string
.text
);
927 envp
[envc
++] = classification
;
930 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
932 snprintf(class_name
, sizeof(class_name
), "CLASS=%s", job
->dest
);
933 envp
[envc
++] = class_name
;
936 if (job
->auth_username
)
937 envp
[envc
++] = job
->auth_username
;
938 if (job
->auth_domain
)
939 envp
[envc
++] = job
->auth_domain
;
940 if (job
->auth_password
)
941 envp
[envc
++] = job
->auth_password
;
945 envp
[envc
++] = job
->ccname
;
946 #endif /* HAVE_GSSAPI */
950 for (i
= 0; i
< envc
; i
++)
951 if (!strncmp(envp
[i
], "AUTH_", 5))
952 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"AUTH_%c****\"", i
,
954 else if (strncmp(envp
[i
], "DEVICE_URI=", 11))
955 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"%s\"", i
, envp
[i
]);
957 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"DEVICE_URI=%s\"", i
,
958 job
->printer
->sanitized_device_uri
);
960 if (job
->printer
->remote
)
961 job
->current_file
= job
->num_files
;
963 job
->current_file
++;
966 * Now create processes for all of the filters...
969 filterfds
[0][0] = -1;
970 filterfds
[0][1] = -1;
971 filterfds
[1][0] = -1;
972 filterfds
[1][1] = -1;
974 memset(job
->filters
, 0, sizeof(job
->filters
));
976 for (i
= 0, slot
= 0, filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
978 i
++, filter
= (mime_filter_t
*)cupsArrayNext(filters
))
980 if (filter
->filter
[0] != '/')
981 snprintf(command
, sizeof(command
), "%s/filter/%s", ServerBin
,
984 strlcpy(command
, filter
->filter
, sizeof(command
));
986 if (i
< (cupsArrayCount(filters
) - 1))
988 if (cupsdOpenPipe(filterfds
[slot
]))
990 abort_message
= "Stopping job because the scheduler could not create "
998 if (job
->current_file
== 1)
1000 if (strncmp(job
->printer
->device_uri
, "file:", 5) != 0)
1002 if (cupsdOpenPipe(job
->print_pipes
))
1004 abort_message
= "Stopping job because the scheduler could not "
1005 "create the backend pipes.";
1012 job
->print_pipes
[0] = -1;
1013 if (!strcmp(job
->printer
->device_uri
, "file:/dev/null") ||
1014 !strcmp(job
->printer
->device_uri
, "file:///dev/null"))
1015 job
->print_pipes
[1] = -1;
1018 if (!strncmp(job
->printer
->device_uri
, "file:/dev/", 10))
1019 job
->print_pipes
[1] = open(job
->printer
->device_uri
+ 5,
1021 else if (!strncmp(job
->printer
->device_uri
, "file:///dev/", 12))
1022 job
->print_pipes
[1] = open(job
->printer
->device_uri
+ 7,
1024 else if (!strncmp(job
->printer
->device_uri
, "file:///", 8))
1025 job
->print_pipes
[1] = open(job
->printer
->device_uri
+ 7,
1026 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
1028 job
->print_pipes
[1] = open(job
->printer
->device_uri
+ 5,
1029 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
1031 if (job
->print_pipes
[1] < 0)
1033 abort_message
= "Stopping job because the scheduler could not "
1034 "open the output file.";
1039 fcntl(job
->print_pipes
[1], F_SETFD
,
1040 fcntl(job
->print_pipes
[1], F_GETFD
) | FD_CLOEXEC
);
1045 filterfds
[slot
][0] = job
->print_pipes
[0];
1046 filterfds
[slot
][1] = job
->print_pipes
[1];
1049 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
1050 filterfds
[slot
][1], job
->status_pipes
[1],
1051 job
->back_pipes
[0], job
->side_pipes
[0], 0,
1052 job
->profile
, job
->id
, job
->filters
+ i
);
1054 cupsdClosePipe(filterfds
[!slot
]);
1058 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Unable to start filter \"%s\" - %s.",
1059 filter
->filter
, strerror(errno
));
1061 abort_message
= "Stopped job because the scheduler could not execute a "
1067 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Started filter %s (PID %d)", command
,
1074 cupsArrayDelete(filters
);
1078 * Finally, pipe the final output into a backend process if needed...
1081 if (strncmp(job
->printer
->device_uri
, "file:", 5) != 0)
1083 if (job
->current_file
== 1 || job
->printer
->remote
)
1085 sscanf(job
->printer
->device_uri
, "%254[^:]", scheme
);
1086 snprintf(command
, sizeof(command
), "%s/backend/%s", ServerBin
, scheme
);
1089 * See if the backend needs to run as root...
1094 else if (stat(command
, &backinfo
))
1097 backroot
= !(backinfo
.st_mode
& (S_IRWXG
| S_IRWXO
));
1099 argv
[0] = job
->printer
->sanitized_device_uri
;
1101 filterfds
[slot
][0] = -1;
1102 filterfds
[slot
][1] = -1;
1104 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
1105 filterfds
[slot
][1], job
->status_pipes
[1],
1106 job
->back_pipes
[1], job
->side_pipes
[1],
1107 backroot
, job
->profile
, job
->id
, &(job
->backend
));
1111 abort_message
= "Stopping job because the sheduler could not execute "
1118 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Started backend %s (PID %d)",
1123 if (job
->current_file
== job
->num_files
)
1125 cupsdClosePipe(job
->print_pipes
);
1126 cupsdClosePipe(job
->back_pipes
);
1127 cupsdClosePipe(job
->side_pipes
);
1129 close(job
->status_pipes
[1]);
1130 job
->status_pipes
[1] = -1;
1135 filterfds
[slot
][0] = -1;
1136 filterfds
[slot
][1] = -1;
1138 if (job
->current_file
== job
->num_files
)
1140 cupsdClosePipe(job
->print_pipes
);
1142 close(job
->status_pipes
[1]);
1143 job
->status_pipes
[1] = -1;
1147 cupsdClosePipe(filterfds
[slot
]);
1149 if (job
->printer
->remote
&& job
->num_files
> 1)
1151 for (i
= 0; i
< job
->num_files
; i
++)
1157 cupsdAddSelect(job
->status_buffer
->fd
, (cupsd_selfunc_t
)update_job
, NULL
,
1160 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
, "Job #%d started.",
1167 * If we get here, we need to abort the current job and close out all
1168 * files and pipes...
1173 for (slot
= 0; slot
< 2; slot
++)
1174 cupsdClosePipe(filterfds
[slot
]);
1176 cupsdClosePipe(job
->status_pipes
);
1177 cupsdStatBufDelete(job
->status_buffer
);
1178 job
->status_buffer
= NULL
;
1180 cupsArrayDelete(filters
);
1182 if (job
->printer
->remote
&& job
->num_files
> 1)
1184 for (i
= 0; i
< job
->num_files
; i
++)
1190 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
, "%s",
1196 * 'cupsdDeleteJob()' - Free all memory used by a job.
1200 cupsdDeleteJob(cupsd_job_t
*job
, /* I - Job */
1201 cupsd_jobaction_t action
)/* I - Action */
1206 if (action
== CUPSD_JOB_PURGE
)
1209 * Remove the job info file...
1212 char filename
[1024]; /* Job filename */
1214 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
,
1219 cupsdClearString(&job
->username
);
1220 cupsdClearString(&job
->dest
);
1221 cupsdClearString(&job
->auth_username
);
1222 cupsdClearString(&job
->auth_domain
);
1223 cupsdClearString(&job
->auth_password
);
1227 * Destroy the credential cache and clear the KRB5CCNAME env var string.
1232 krb5_cc_destroy(KerberosContext
, job
->ccache
);
1236 cupsdClearString(&job
->ccname
);
1237 #endif /* HAVE_GSSAPI */
1239 if (job
->num_files
> 0)
1241 free(job
->compressions
);
1242 free(job
->filetypes
);
1249 cupsArrayRemove(Jobs
, job
);
1250 cupsArrayRemove(ActiveJobs
, job
);
1251 cupsArrayRemove(PrintingJobs
, job
);
1258 * 'cupsdFreeAllJobs()' - Free all jobs from memory.
1262 cupsdFreeAllJobs(void)
1264 cupsd_job_t
*job
; /* Current job */
1272 cupsdStopAllJobs(CUPSD_JOB_FORCE
, 0);
1275 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1277 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1278 cupsdDeleteJob(job
, CUPSD_JOB_DEFAULT
);
1280 cupsdReleaseSignals();
1285 * 'cupsdFindJob()' - Find the specified job.
1288 cupsd_job_t
* /* O - Job data */
1289 cupsdFindJob(int id
) /* I - Job ID */
1291 cupsd_job_t key
; /* Search key */
1296 return ((cupsd_job_t
*)cupsArrayFind(Jobs
, &key
));
1301 * 'cupsdGetPrinterJobCount()' - Get the number of pending, processing,
1302 * or held jobs in a printer or class.
1305 int /* O - Job count */
1306 cupsdGetPrinterJobCount(
1307 const char *dest
) /* I - Printer or class name */
1309 int count
; /* Job count */
1310 cupsd_job_t
*job
; /* Current job */
1313 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
1315 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
1316 if (job
->dest
&& !strcasecmp(job
->dest
, dest
))
1324 * 'cupsdGetUserJobCount()' - Get the number of pending, processing,
1325 * or held jobs for a user.
1328 int /* O - Job count */
1329 cupsdGetUserJobCount(
1330 const char *username
) /* I - Username */
1332 int count
; /* Job count */
1333 cupsd_job_t
*job
; /* Current job */
1336 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
1338 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
1339 if (!strcasecmp(job
->username
, username
))
1347 * 'cupsdLoadAllJobs()' - Load all jobs from disk.
1351 cupsdLoadAllJobs(void)
1353 char filename
[1024]; /* Full filename of job.cache file */
1354 struct stat fileinfo
, /* Information on job.cache file */
1355 dirinfo
; /* Information on RequestRoot dir */
1360 * Create the job arrays as needed...
1364 Jobs
= cupsArrayNew(compare_jobs
, NULL
);
1367 ActiveJobs
= cupsArrayNew(compare_active_jobs
, NULL
);
1370 PrintingJobs
= cupsArrayNew(compare_jobs
, NULL
);
1373 * See whether the job.cache file is older than the RequestRoot directory...
1376 snprintf(filename
, sizeof(filename
), "%s/job.cache", CacheDir
);
1378 if (stat(filename
, &fileinfo
))
1380 fileinfo
.st_mtime
= 0;
1382 if (errno
!= ENOENT
)
1383 cupsdLogMessage(CUPSD_LOG_ERROR
,
1384 "Unable to get file information for \"%s\" - %s",
1385 filename
, strerror(errno
));
1388 if (stat(RequestRoot
, &dirinfo
))
1390 dirinfo
.st_mtime
= 0;
1392 if (errno
!= ENOENT
)
1393 cupsdLogMessage(CUPSD_LOG_ERROR
,
1394 "Unable to get directory information for \"%s\" - %s",
1395 RequestRoot
, strerror(errno
));
1399 * Load the most recent source for job data...
1402 if (dirinfo
.st_mtime
> fileinfo
.st_mtime
)
1404 load_request_root();
1406 load_next_job_id(filename
);
1409 load_job_cache(filename
);
1412 * Clean out old jobs as needed...
1415 if (MaxJobs
> 0 && cupsArrayCount(Jobs
) >= MaxJobs
)
1421 * 'cupsdLoadJob()' - Load a single job.
1424 int /* O - 1 on success, 0 on failure */
1425 cupsdLoadJob(cupsd_job_t
*job
) /* I - Job */
1427 char jobfile
[1024]; /* Job filename */
1428 cups_file_t
*fp
; /* Job file */
1429 int fileid
; /* Current file ID */
1430 ipp_attribute_t
*attr
; /* Job attribute */
1431 const char *dest
; /* Destination name */
1432 cupsd_printer_t
*destptr
; /* Pointer to destination */
1433 mime_type_t
**filetypes
; /* New filetypes array */
1434 int *compressions
; /* New compressions array */
1439 if (job
->state_value
> IPP_JOB_STOPPED
)
1440 job
->access_time
= time(NULL
);
1445 if ((job
->attrs
= ippNew()) == NULL
)
1447 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Ran out of memory for job attributes!");
1452 * Load job attributes...
1455 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Loading attributes...", job
->id
);
1457 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, job
->id
);
1458 if ((fp
= cupsFileOpen(jobfile
, "r")) == NULL
)
1460 cupsdLogMessage(CUPSD_LOG_ERROR
,
1461 "[Job %d] Unable to open job control file \"%s\" - %s!",
1462 job
->id
, jobfile
, strerror(errno
));
1466 if (ippReadIO(fp
, (ipp_iocb_t
)cupsFileRead
, 1, NULL
, job
->attrs
) != IPP_DATA
)
1468 cupsdLogMessage(CUPSD_LOG_ERROR
,
1469 "[Job %d] Unable to read job control file \"%s\"!", job
->id
,
1478 * Copy attribute data to the job object...
1481 if (!ippFindAttribute(job
->attrs
, "time-at-creation", IPP_TAG_INTEGER
))
1483 cupsdLogMessage(CUPSD_LOG_ERROR
,
1484 "[Job %d] Missing or bad time-at-creation attribute in "
1485 "control file!", job
->id
);
1489 if ((job
->state
= ippFindAttribute(job
->attrs
, "job-state",
1490 IPP_TAG_ENUM
)) == NULL
)
1492 cupsdLogMessage(CUPSD_LOG_ERROR
,
1493 "[Job %d] Missing or bad job-state attribute in control "
1498 job
->state_value
= (ipp_jstate_t
)job
->state
->values
[0].integer
;
1502 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1503 IPP_TAG_URI
)) == NULL
)
1505 cupsdLogMessage(CUPSD_LOG_ERROR
,
1506 "[Job %d] No job-printer-uri attribute in control file!",
1511 if ((dest
= cupsdValidateDest(attr
->values
[0].string
.text
, &(job
->dtype
),
1514 cupsdLogMessage(CUPSD_LOG_ERROR
,
1515 "[Job %d] Unable to queue job for destination \"%s\"!",
1516 job
->id
, attr
->values
[0].string
.text
);
1520 cupsdSetString(&job
->dest
, dest
);
1522 else if ((destptr
= cupsdFindDest(job
->dest
)) == NULL
)
1524 cupsdLogMessage(CUPSD_LOG_ERROR
,
1525 "[Job %d] Unable to queue job for destination \"%s\"!",
1526 job
->id
, job
->dest
);
1530 job
->sheets
= ippFindAttribute(job
->attrs
, "job-media-sheets-completed",
1532 job
->job_sheets
= ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_NAME
);
1536 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
1537 IPP_TAG_INTEGER
)) == NULL
)
1539 cupsdLogMessage(CUPSD_LOG_ERROR
,
1540 "[Job %d] Missing or bad job-priority attribute in "
1541 "control file!", job
->id
);
1545 job
->priority
= attr
->values
[0].integer
;
1550 if ((attr
= ippFindAttribute(job
->attrs
, "job-originating-user-name",
1551 IPP_TAG_NAME
)) == NULL
)
1553 cupsdLogMessage(CUPSD_LOG_ERROR
,
1554 "[Job %d] Missing or bad job-originating-user-name "
1555 "attribute in control file!", job
->id
);
1559 cupsdSetString(&job
->username
, attr
->values
[0].string
.text
);
1563 * Set the job hold-until time and state...
1566 if (job
->state_value
== IPP_JOB_HELD
)
1568 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1569 IPP_TAG_KEYWORD
)) == NULL
)
1570 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1573 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
, CUPSD_JOB_DEFAULT
);
1576 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1577 job
->state_value
= IPP_JOB_PENDING
;
1580 else if (job
->state_value
== IPP_JOB_PROCESSING
)
1582 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1583 job
->state_value
= IPP_JOB_PENDING
;
1586 if (!job
->num_files
)
1589 * Find all the d##### files...
1592 for (fileid
= 1; fileid
< 10000; fileid
++)
1594 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
1597 if (access(jobfile
, 0))
1600 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1601 "[Job %d] Auto-typing document file \"%s\"...", job
->id
,
1604 if (fileid
> job
->num_files
)
1606 if (job
->num_files
== 0)
1608 compressions
= (int *)calloc(fileid
, sizeof(int));
1609 filetypes
= (mime_type_t
**)calloc(fileid
, sizeof(mime_type_t
*));
1613 compressions
= (int *)realloc(job
->compressions
,
1614 sizeof(int) * fileid
);
1615 filetypes
= (mime_type_t
**)realloc(job
->filetypes
,
1616 sizeof(mime_type_t
*) *
1620 if (!compressions
|| !filetypes
)
1622 cupsdLogMessage(CUPSD_LOG_ERROR
,
1623 "[Job %d] Ran out of memory for job file types!",
1628 job
->compressions
= compressions
;
1629 job
->filetypes
= filetypes
;
1630 job
->num_files
= fileid
;
1633 job
->filetypes
[fileid
- 1] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
1634 job
->compressions
+ fileid
- 1);
1636 if (!job
->filetypes
[fileid
- 1])
1637 job
->filetypes
[fileid
- 1] = mimeType(MimeDatabase
, "application",
1643 * Load authentication information as needed...
1646 if (job
->state_value
< IPP_JOB_STOPPED
)
1648 snprintf(jobfile
, sizeof(jobfile
), "%s/a%05d", RequestRoot
, job
->id
);
1650 cupsdClearString(&job
->auth_username
);
1651 cupsdClearString(&job
->auth_domain
);
1652 cupsdClearString(&job
->auth_password
);
1654 if ((fp
= cupsFileOpen(jobfile
, "r")) != NULL
)
1656 int i
, /* Looping var */
1657 bytes
; /* Size of auth data */
1658 char line
[255], /* Line from file */
1659 data
[255]; /* Decoded data */
1663 i
< destptr
->num_auth_info_required
&&
1664 cupsFileGets(fp
, line
, sizeof(line
));
1667 bytes
= sizeof(data
);
1668 httpDecode64_2(data
, &bytes
, line
);
1670 if (!strcmp(destptr
->auth_info_required
[i
], "username"))
1671 cupsdSetStringf(&job
->auth_username
, "AUTH_USERNAME=%s", data
);
1672 else if (!strcmp(destptr
->auth_info_required
[i
], "domain"))
1673 cupsdSetStringf(&job
->auth_domain
, "AUTH_DOMAIN=%s", data
);
1674 else if (!strcmp(destptr
->auth_info_required
[i
], "password"))
1675 cupsdSetStringf(&job
->auth_password
, "AUTH_PASSWORD=%s", data
);
1682 job
->access_time
= time(NULL
);
1686 * If we get here then something bad happened...
1691 ippDelete(job
->attrs
);
1700 * 'cupsdMoveJob()' - Move the specified job to a different destination.
1704 cupsdMoveJob(cupsd_job_t
*job
, /* I - Job */
1705 cupsd_printer_t
*p
) /* I - Destination printer or class */
1707 ipp_attribute_t
*attr
; /* job-printer-uri attribute */
1708 const char *olddest
; /* Old destination */
1709 cupsd_printer_t
*oldp
; /* Old pointer */
1713 * Don't move completed jobs...
1716 if (job
->state_value
> IPP_JOB_STOPPED
)
1720 * Get the old destination...
1723 olddest
= job
->dest
;
1726 oldp
= job
->printer
;
1728 oldp
= cupsdFindDest(olddest
);
1731 * Change the destination information...
1734 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
1735 "Stopping job prior to move.");
1737 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, oldp
, job
,
1738 "Job #%d moved from %s to %s.", job
->id
, olddest
,
1741 cupsdSetString(&job
->dest
, p
->name
);
1742 job
->dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_REMOTE
|
1743 CUPS_PRINTER_IMPLICIT
);
1745 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1746 IPP_TAG_URI
)) != NULL
)
1747 cupsdSetString(&(attr
->values
[0].string
.text
), p
->uri
);
1749 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, p
, job
,
1750 "Job #%d moved from %s to %s.", job
->id
, olddest
,
1754 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1759 * 'cupsdReleaseJob()' - Release the specified job.
1763 cupsdReleaseJob(cupsd_job_t
*job
) /* I - Job */
1765 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdReleaseJob(job=%p(%d))", job
,
1768 if (job
->state_value
== IPP_JOB_HELD
)
1771 * Add trailing banner as needed...
1774 if (job
->pending_timeout
)
1775 cupsdTimeoutJob(job
);
1777 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
1778 "Job released by user.");
1784 * 'cupsdRestartJob()' - Restart the specified job.
1788 cupsdRestartJob(cupsd_job_t
*job
) /* I - Job */
1790 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdRestartJob(job=%p(%d))", job
,
1793 if (job
->state_value
== IPP_JOB_STOPPED
|| job
->num_files
)
1794 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
1795 "Job restarted by user.");
1800 * 'cupsdSaveAllJobs()' - Save a summary of all jobs to disk.
1804 cupsdSaveAllJobs(void)
1806 int i
; /* Looping var */
1807 cups_file_t
*fp
; /* Job cache file */
1808 char temp
[1024]; /* Temporary string */
1809 cupsd_job_t
*job
; /* Current job */
1810 time_t curtime
; /* Current time */
1811 struct tm
*curdate
; /* Current date */
1814 snprintf(temp
, sizeof(temp
), "%s/job.cache", CacheDir
);
1815 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
1817 cupsdLogMessage(CUPSD_LOG_ERROR
,
1818 "Unable to create job cache file \"%s\" - %s",
1819 temp
, strerror(errno
));
1823 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving job cache file \"%s\"...", temp
);
1826 * Restrict access to the file...
1829 fchown(cupsFileNumber(fp
), getuid(), Group
);
1830 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
1833 * Write a small header to the file...
1836 curtime
= time(NULL
);
1837 curdate
= localtime(&curtime
);
1838 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
1840 cupsFilePuts(fp
, "# Job cache file for " CUPS_SVERSION
"\n");
1841 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
1842 cupsFilePrintf(fp
, "NextJobId %d\n", NextJobId
);
1845 * Write each job known to the system...
1848 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1850 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1852 cupsFilePrintf(fp
, "<Job %d>\n", job
->id
);
1853 cupsFilePrintf(fp
, "State %d\n", job
->state_value
);
1854 cupsFilePrintf(fp
, "Priority %d\n", job
->priority
);
1855 cupsFilePrintf(fp
, "HoldUntil %d\n", (int)job
->hold_until
);
1856 cupsFilePrintf(fp
, "Username %s\n", job
->username
);
1857 cupsFilePrintf(fp
, "Destination %s\n", job
->dest
);
1858 cupsFilePrintf(fp
, "DestType %d\n", job
->dtype
);
1859 cupsFilePrintf(fp
, "NumFiles %d\n", job
->num_files
);
1860 for (i
= 0; i
< job
->num_files
; i
++)
1861 cupsFilePrintf(fp
, "File %d %s/%s %d\n", i
+ 1, job
->filetypes
[i
]->super
,
1862 job
->filetypes
[i
]->type
, job
->compressions
[i
]);
1863 cupsFilePuts(fp
, "</Job>\n");
1871 * 'cupsdSaveJob()' - Save a job to disk.
1875 cupsdSaveJob(cupsd_job_t
*job
) /* I - Job */
1877 char filename
[1024]; /* Job control filename */
1878 cups_file_t
*fp
; /* Job file */
1881 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSaveJob(job=%p(%d)): job->attrs=%p",
1882 job
, job
->id
, job
->attrs
);
1884 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
, job
->id
);
1886 if ((fp
= cupsFileOpen(filename
, "w")) == NULL
)
1888 cupsdLogMessage(CUPSD_LOG_ERROR
,
1889 "[Job %d] Unable to create job control file \"%s\" - %s.",
1890 job
->id
, filename
, strerror(errno
));
1894 fchmod(cupsFileNumber(fp
), 0600);
1895 fchown(cupsFileNumber(fp
), RunUser
, Group
);
1897 job
->attrs
->state
= IPP_IDLE
;
1899 if (ippWriteIO(fp
, (ipp_iocb_t
)cupsFileWrite
, 1, NULL
,
1900 job
->attrs
) != IPP_DATA
)
1901 cupsdLogMessage(CUPSD_LOG_ERROR
,
1902 "[Job %d] Unable to write job control file!", job
->id
);
1911 * 'cupsdSetJobHoldUntil()' - Set the hold time for a job.
1915 cupsdSetJobHoldUntil(cupsd_job_t
*job
, /* I - Job */
1916 const char *when
, /* I - When to resume */
1917 int update
)/* I - Update job-hold-until attr? */
1919 time_t curtime
; /* Current time */
1920 struct tm
*curdate
; /* Current date */
1921 int hour
; /* Hold hour */
1922 int minute
; /* Hold minute */
1923 int second
= 0; /* Hold second */
1926 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1927 "cupsdSetJobHoldUntil(job=%p(%d), when=\"%s\", update=%d)",
1928 job
, job
->id
, when
, update
);
1933 * Update the job-hold-until attribute...
1936 ipp_attribute_t
*attr
; /* job-hold-until attribute */
1938 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1939 IPP_TAG_KEYWORD
)) == NULL
)
1940 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1943 cupsdSetString(&(attr
->values
[0].string
.text
), when
);
1945 attr
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1946 "job-hold-until", NULL
, when
);
1950 if (isdigit(when
[0] & 255))
1951 attr
->value_tag
= IPP_TAG_NAME
;
1953 attr
->value_tag
= IPP_TAG_KEYWORD
;
1956 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1961 * Update the hold time...
1964 if (!strcmp(when
, "indefinite") || !strcmp(when
, "auth-info-required"))
1967 * Hold indefinitely...
1970 job
->hold_until
= 0;
1972 else if (!strcmp(when
, "day-time"))
1975 * Hold to 6am the next morning unless local time is < 6pm.
1978 curtime
= time(NULL
);
1979 curdate
= localtime(&curtime
);
1981 if (curdate
->tm_hour
< 18)
1982 job
->hold_until
= curtime
;
1984 job
->hold_until
= curtime
+
1985 ((29 - curdate
->tm_hour
) * 60 + 59 -
1986 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1988 else if (!strcmp(when
, "evening") || !strcmp(when
, "night"))
1991 * Hold to 6pm unless local time is > 6pm or < 6am.
1994 curtime
= time(NULL
);
1995 curdate
= localtime(&curtime
);
1997 if (curdate
->tm_hour
< 6 || curdate
->tm_hour
>= 18)
1998 job
->hold_until
= curtime
;
2000 job
->hold_until
= curtime
+
2001 ((17 - curdate
->tm_hour
) * 60 + 59 -
2002 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2004 else if (!strcmp(when
, "second-shift"))
2007 * Hold to 4pm unless local time is > 4pm.
2010 curtime
= time(NULL
);
2011 curdate
= localtime(&curtime
);
2013 if (curdate
->tm_hour
>= 16)
2014 job
->hold_until
= curtime
;
2016 job
->hold_until
= curtime
+
2017 ((15 - curdate
->tm_hour
) * 60 + 59 -
2018 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2020 else if (!strcmp(when
, "third-shift"))
2023 * Hold to 12am unless local time is < 8am.
2026 curtime
= time(NULL
);
2027 curdate
= localtime(&curtime
);
2029 if (curdate
->tm_hour
< 8)
2030 job
->hold_until
= curtime
;
2032 job
->hold_until
= curtime
+
2033 ((23 - curdate
->tm_hour
) * 60 + 59 -
2034 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2036 else if (!strcmp(when
, "weekend"))
2039 * Hold to weekend unless we are in the weekend.
2042 curtime
= time(NULL
);
2043 curdate
= localtime(&curtime
);
2045 if (curdate
->tm_wday
== 0 || curdate
->tm_wday
== 6)
2046 job
->hold_until
= curtime
;
2048 job
->hold_until
= curtime
+
2049 (((5 - curdate
->tm_wday
) * 24 +
2050 (17 - curdate
->tm_hour
)) * 60 + 59 -
2051 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2053 else if (sscanf(when
, "%d:%d:%d", &hour
, &minute
, &second
) >= 2)
2056 * Hold to specified GMT time (HH:MM or HH:MM:SS)...
2059 curtime
= time(NULL
);
2060 curdate
= gmtime(&curtime
);
2062 job
->hold_until
= curtime
+
2063 ((hour
- curdate
->tm_hour
) * 60 + minute
-
2064 curdate
->tm_min
) * 60 + second
- curdate
->tm_sec
;
2067 * Hold until next day as needed...
2070 if (job
->hold_until
< curtime
)
2071 job
->hold_until
+= 24 * 60 * 60;
2074 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetJobHoldUntil: hold_until=%d",
2075 (int)job
->hold_until
);
2080 * 'cupsdSetJobPriority()' - Set the priority of a job, moving it up/down in
2081 * the list as needed.
2085 cupsdSetJobPriority(
2086 cupsd_job_t
*job
, /* I - Job ID */
2087 int priority
) /* I - New priority (0 to 100) */
2089 ipp_attribute_t
*attr
; /* Job attribute */
2093 * Don't change completed jobs...
2096 if (job
->state_value
>= IPP_JOB_PROCESSING
)
2100 * Set the new priority and re-add the job into the active list...
2103 cupsArrayRemove(ActiveJobs
, job
);
2105 job
->priority
= priority
;
2107 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
2108 IPP_TAG_INTEGER
)) != NULL
)
2109 attr
->values
[0].integer
= priority
;
2111 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-priority",
2114 cupsArrayAdd(ActiveJobs
, job
);
2117 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2122 * 'cupsdSetJobState()' - Set the state of the specified print job.
2127 cupsd_job_t
*job
, /* I - Job to cancel */
2128 ipp_jstate_t newstate
, /* I - New job state */
2129 cupsd_jobaction_t action
, /* I - Action to take */
2130 const char *message
, /* I - Message to log */
2131 ...) /* I - Additional arguments as needed */
2133 int i
; /* Looping var */
2134 ipp_jstate_t oldstate
; /* Old state */
2135 char filename
[1024]; /* Job filename */
2136 ipp_attribute_t
*attr
; /* Job attribute */
2139 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2140 "cupsdSetJobState(job=%p(%d), state=%d, newstate=%d, "
2141 "action=%d, message=\"%s\")", job
, job
->id
, job
->state_value
,
2142 newstate
, action
, message
? message
: "(null)");
2146 * Make sure we have the job attributes...
2149 if (!cupsdLoadJob(job
))
2153 * Don't do anything if the state is unchanged...
2156 if (newstate
== (oldstate
= job
->state_value
))
2160 * Stop any processes that are working on the current job...
2163 if (oldstate
== IPP_JOB_PROCESSING
)
2164 stop_job(job
, action
!= CUPSD_JOB_DEFAULT
);
2167 * Set the new job state...
2170 job
->state
->values
[0].integer
= newstate
;
2171 job
->state_value
= newstate
;
2175 case IPP_JOB_PENDING
:
2177 * Update job-hold-until as needed...
2180 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2181 IPP_TAG_KEYWORD
)) == NULL
)
2182 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2186 attr
->value_tag
= IPP_TAG_KEYWORD
;
2187 cupsdSetString(&(attr
->values
[0].string
.text
), "no-hold");
2193 case IPP_JOB_ABORTED
:
2194 case IPP_JOB_CANCELED
:
2195 case IPP_JOB_COMPLETED
:
2196 set_time(job
, "time-at-completed");
2201 * Log message as needed...
2206 char buffer
[2048]; /* Message buffer */
2207 va_list ap
; /* Pointer to additional arguments */
2209 va_start(ap
, message
);
2210 vsnprintf(buffer
, sizeof(buffer
), message
, ap
);
2213 if (newstate
> IPP_JOB_STOPPED
)
2214 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
, "%s", buffer
);
2216 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
, "%s", buffer
);
2218 if (newstate
== IPP_JOB_STOPPED
|| newstate
== IPP_JOB_ABORTED
)
2219 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "%s", buffer
);
2221 cupsdLogJob(job
, CUPSD_LOG_INFO
, "%s", buffer
);
2225 * Handle post-state-change actions...
2230 case IPP_JOB_PROCESSING
:
2232 * Add the job to the "printing" list...
2235 if (!cupsArrayFind(PrintingJobs
, job
))
2236 cupsArrayAdd(PrintingJobs
, job
);
2239 * Set the processing time...
2242 set_time(job
, "time-at-processing");
2244 case IPP_JOB_PENDING
:
2246 case IPP_JOB_STOPPED
:
2248 * Make sure the job is in the active list...
2251 if (!cupsArrayFind(ActiveJobs
, job
))
2252 cupsArrayAdd(ActiveJobs
, job
);
2255 * Save the job state to disk...
2259 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2262 case IPP_JOB_ABORTED
:
2263 case IPP_JOB_CANCELED
:
2264 case IPP_JOB_COMPLETED
:
2266 * Expire job subscriptions since the job is now "completed"...
2269 cupsdExpireSubscriptions(NULL
, job
);
2272 * Remove the job from the active list...
2275 cupsArrayRemove(ActiveJobs
, job
);
2279 * If we are going to sleep and the PrintingJobs count is now 0, allow the
2280 * sleep to happen immediately...
2283 if (Sleeping
&& cupsArrayCount(PrintingJobs
) == 0)
2285 #endif /* __APPLE__ */
2288 * Remove any authentication data...
2291 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
2292 if (cupsdRemoveFile(filename
) && errno
!= ENOENT
)
2293 cupsdLogMessage(CUPSD_LOG_ERROR
,
2294 "Unable to remove authentication cache: %s",
2297 cupsdClearString(&job
->auth_username
);
2298 cupsdClearString(&job
->auth_domain
);
2299 cupsdClearString(&job
->auth_password
);
2303 * Destroy the credential cache and clear the KRB5CCNAME env var string.
2308 krb5_cc_destroy(KerberosContext
, job
->ccache
);
2312 cupsdClearString(&job
->ccname
);
2313 #endif /* HAVE_GSSAPI */
2316 * Remove the print file for good if we aren't preserving jobs or
2320 if (!JobHistory
|| !JobFiles
|| action
== CUPSD_JOB_PURGE
)
2322 for (i
= 1; i
<= job
->num_files
; i
++)
2324 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
2329 if (job
->num_files
> 0)
2331 free(job
->filetypes
);
2332 free(job
->compressions
);
2335 job
->filetypes
= NULL
;
2336 job
->compressions
= NULL
;
2340 if (JobHistory
&& action
!= CUPSD_JOB_PURGE
)
2343 * Save job state info...
2347 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2349 else if (!job
->printer
)
2350 cupsdDeleteJob(job
, CUPSD_JOB_PURGE
);
2355 * Update the server "busy" state...
2358 cupsdSetBusyState();
2363 * 'cupsdStopAllJobs()' - Stop all print jobs.
2368 cupsd_jobaction_t action
, /* I - Action */
2369 int kill_delay
) /* I - Number of seconds before we kill */
2371 cupsd_job_t
*job
; /* Current job */
2374 DEBUG_puts("cupsdStopAllJobs()");
2376 for (job
= (cupsd_job_t
*)cupsArrayFirst(PrintingJobs
);
2378 job
= (cupsd_job_t
*)cupsArrayNext(PrintingJobs
))
2381 job
->kill_time
= time(NULL
) + kill_delay
;
2383 cupsdSetJobState(job
, IPP_JOB_PENDING
, action
, NULL
);
2389 * 'cupsdUnloadCompletedJobs()' - Flush completed job history from memory.
2393 cupsdUnloadCompletedJobs(void)
2395 cupsd_job_t
*job
; /* Current job */
2396 time_t expire
; /* Expiration time */
2399 expire
= time(NULL
) - 60;
2401 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
2403 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
2404 if (job
->attrs
&& job
->state_value
>= IPP_JOB_STOPPED
&& !job
->printer
&&
2405 job
->access_time
< expire
)
2416 * 'compare_active_jobs()' - Compare the job IDs and priorities of two jobs.
2419 static int /* O - Difference */
2420 compare_active_jobs(void *first
, /* I - First job */
2421 void *second
, /* I - Second job */
2422 void *data
) /* I - App data (not used) */
2424 int diff
; /* Difference */
2427 if ((diff
= ((cupsd_job_t
*)second
)->priority
-
2428 ((cupsd_job_t
*)first
)->priority
) != 0)
2431 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
2436 * 'compare_jobs()' - Compare the job IDs of two jobs.
2439 static int /* O - Difference */
2440 compare_jobs(void *first
, /* I - First job */
2441 void *second
, /* I - Second job */
2442 void *data
) /* I - App data (not used) */
2444 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
2449 * 'finalize_job()' - Cleanup after job filter processes and support data.
2453 finalize_job(cupsd_job_t
*job
) /* I - Job */
2455 ipp_pstate_t printer_state
; /* New printer state value */
2456 ipp_jstate_t job_state
; /* New job state value */
2457 const char *message
; /* Message for job state */
2458 char buffer
[1024]; /* Buffer for formatted messages */
2461 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "finalize_job(job=%p(%d))", job
, job
->id
);
2464 * Clear the "connecting-to-device" reason, which is only valid when a
2465 * printer is processing...
2468 cupsdSetPrinterReasons(job
->printer
, "-connecting-to-device");
2471 * Similarly, clear the "offline-report" reason for non-USB devices since we
2472 * rarely have current information for network devices...
2475 if (strncmp(job
->printer
->device_uri
, "usb:", 4))
2476 cupsdSetPrinterReasons(job
->printer
, "-offline-report");
2479 * Free the security profile...
2482 cupsdDestroyProfile(job
->profile
);
2483 job
->profile
= NULL
;
2486 * Close pipes and status buffer...
2489 cupsdRemoveSelect(job
->status_buffer
->fd
);
2491 cupsdClosePipe(job
->print_pipes
);
2492 cupsdClosePipe(job
->back_pipes
);
2493 cupsdClosePipe(job
->side_pipes
);
2494 cupsdClosePipe(job
->status_pipes
);
2496 cupsdStatBufDelete(job
->status_buffer
);
2497 job
->status_buffer
= NULL
;
2500 * Process the exit status...
2503 if (job
->printer
->state
== IPP_PRINTER_PROCESSING
)
2504 printer_state
= IPP_PRINTER_IDLE
;
2506 printer_state
= job
->printer
->state
;
2508 switch (job_state
= job
->state_value
)
2510 case IPP_JOB_PENDING
:
2511 message
= "Job paused.";
2515 message
= "Job held.";
2519 case IPP_JOB_PROCESSING
:
2520 case IPP_JOB_COMPLETED
:
2521 job_state
= IPP_JOB_COMPLETED
;
2522 message
= "Job completed.";
2525 case IPP_JOB_STOPPED
:
2526 message
= "Job stopped.";
2529 case IPP_JOB_CANCELED
:
2530 message
= "Job canceled.";
2533 case IPP_JOB_ABORTED
:
2534 message
= "Job aborted.";
2538 if (job
->status
< 0)
2541 * Backend had errors...
2544 int exit_code
; /* Exit code from backend */
2548 * Convert the status to an exit code. Due to the way the W* macros are
2549 * implemented on MacOS X (bug?), we have to store the exit status in a
2550 * variable first and then convert...
2553 exit_code
= -job
->status
;
2554 if (WIFEXITED(exit_code
))
2555 exit_code
= WEXITSTATUS(exit_code
);
2557 exit_code
= job
->status
;
2559 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Backend returned status %d (%s)",
2561 exit_code
== CUPS_BACKEND_FAILED
? "failed" :
2562 exit_code
== CUPS_BACKEND_AUTH_REQUIRED
?
2563 "authentication required" :
2564 exit_code
== CUPS_BACKEND_HOLD
? "hold job" :
2565 exit_code
== CUPS_BACKEND_STOP
? "stop printer" :
2566 exit_code
== CUPS_BACKEND_CANCEL
? "cancel job" :
2567 exit_code
< 0 ? "crashed" : "unknown");
2570 * Do what needs to be done...
2576 case CUPS_BACKEND_FAILED
:
2578 * Backend failure, use the error-policy to determine how to
2582 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
2585 * Queued on a class - mark the job as pending and we'll retry on
2586 * another printer...
2589 if (job_state
== IPP_JOB_COMPLETED
)
2591 job_state
= IPP_JOB_PENDING
;
2592 message
= "Retrying job on another printer.";
2595 else if (!strcmp(job
->printer
->error_policy
, "retry-current-job"))
2598 * The error policy is "retry-current-job" - mark the job as pending
2599 * and we'll retry on the same printer...
2602 if (job_state
== IPP_JOB_COMPLETED
)
2604 job_state
= IPP_JOB_PENDING
;
2605 message
= "Retrying job on same printer.";
2608 else if ((job
->printer
->type
& CUPS_PRINTER_FAX
) ||
2609 !strcmp(job
->printer
->error_policy
, "retry-job"))
2611 if (job_state
== IPP_JOB_COMPLETED
)
2614 * The job was queued on a fax or the error policy is "retry-job" -
2615 * hold the job if the number of retries is less than the
2616 * JobRetryLimit, otherwise abort the job.
2621 if (job
->tries
>= JobRetryLimit
)
2627 snprintf(buffer
, sizeof(buffer
),
2628 "Job aborted after %d unsuccessful attempts.",
2630 job_state
= IPP_JOB_ABORTED
;
2636 * Try again in N seconds...
2639 set_hold_until(job
, time(NULL
) + JobRetryInterval
);
2641 snprintf(buffer
, sizeof(buffer
),
2642 "Job held for %d seconds since it could not be sent.",
2644 job_state
= IPP_JOB_HELD
;
2649 else if (!strcmp(job
->printer
->error_policy
, "abort-job") &&
2650 job_state
== IPP_JOB_COMPLETED
)
2652 job_state
= IPP_JOB_ABORTED
;
2653 message
= "Job aborted due to backend errors; please consult "
2654 "the error_log file for details.";
2658 printer_state
= IPP_PRINTER_STOPPED
;
2659 message
= "Printer stopped due to backend errors; please "
2660 "consult the error_log file for details.";
2662 if (job_state
== IPP_JOB_COMPLETED
)
2663 job_state
= IPP_JOB_PENDING
;
2667 case CUPS_BACKEND_CANCEL
:
2672 if (job_state
== IPP_JOB_COMPLETED
)
2674 job_state
= IPP_JOB_ABORTED
;
2675 message
= "Job aborted due to backend errors; please consult "
2676 "the error_log file for details.";
2680 case CUPS_BACKEND_HOLD
:
2681 if (job_state
== IPP_JOB_COMPLETED
)
2687 cupsdSetJobHoldUntil(job
, "indefinite", 1);
2689 job_state
= IPP_JOB_HELD
;
2690 message
= "Job held indefinitely due to backend errors; please "
2691 "consult the error_log file for details.";
2695 case CUPS_BACKEND_STOP
:
2697 * Stop the printer...
2700 printer_state
= IPP_PRINTER_STOPPED
;
2701 message
= "Printer stopped due to backend errors; please "
2702 "consult the error_log file for details.";
2704 if (job_state
== IPP_JOB_COMPLETED
)
2705 job_state
= IPP_JOB_PENDING
;
2708 case CUPS_BACKEND_AUTH_REQUIRED
:
2710 * Hold the job for authentication...
2713 if (job_state
== IPP_JOB_COMPLETED
)
2715 cupsdSetJobHoldUntil(job
, "auth-info-required", 1);
2717 job_state
= IPP_JOB_HELD
;
2718 message
= "Job held for authentication.";
2723 else if (job
->status
> 0)
2726 * Filter had errors; stop job...
2729 if (job_state
== IPP_JOB_COMPLETED
)
2731 job_state
= IPP_JOB_STOPPED
;
2732 message
= "Job stopped due to filter errors; please consult the "
2733 "error_log file for details.";
2738 * Update the printer and job state.
2741 cupsdSetJobState(job
, job_state
, CUPSD_JOB_DEFAULT
, "%s", message
);
2742 cupsdSetPrinterState(job
->printer
, printer_state
,
2743 printer_state
== IPP_PRINTER_STOPPED
);
2744 update_job_attrs(job
, 0);
2746 cupsArrayRemove(PrintingJobs
, job
);
2749 * Clear the printer <-> job association...
2752 job
->printer
->job
= NULL
;
2753 job
->printer
= NULL
;
2756 * Try printing another job...
2759 if (printer_state
!= IPP_PRINTER_STOPPED
)
2765 * 'get_options()' - Get a string containing the job options.
2768 static char * /* O - Options string */
2769 get_options(cupsd_job_t
*job
, /* I - Job */
2770 int banner_page
, /* I - Printing a banner page? */
2771 char *copies
, /* I - Copies buffer */
2772 size_t copies_size
, /* I - Size of copies buffer */
2773 char *title
, /* I - Title buffer */
2774 size_t title_size
) /* I - Size of title buffer */
2776 int i
; /* Looping var */
2777 char *optptr
, /* Pointer to options */
2778 *valptr
; /* Pointer in value string */
2779 ipp_attribute_t
*attr
; /* Current attribute */
2780 static char *options
= NULL
;/* Full list of options */
2781 static int optlength
= 0; /* Length of option buffer */
2785 * Building the options string is harder than it needs to be, but
2786 * for the moment we need to pass strings for command-line args and
2787 * not IPP attribute pointers... :)
2789 * First allocate/reallocate the option buffer as needed...
2792 i
= ipp_length(job
->attrs
);
2794 if (i
> optlength
|| !options
)
2799 optptr
= realloc(options
, i
);
2803 cupsdLogJob(job
, CUPSD_LOG_CRIT
,
2804 "Unable to allocate %d bytes for option buffer!", i
);
2813 * Now loop through the attributes and convert them to the textual
2814 * representation used by the filters...
2820 snprintf(title
, title_size
, "%s-%d", job
->printer
->name
, job
->id
);
2821 strlcpy(copies
, "1", copies_size
);
2823 for (attr
= job
->attrs
->attrs
; attr
!= NULL
; attr
= attr
->next
)
2825 if (!strcmp(attr
->name
, "copies") &&
2826 attr
->value_tag
== IPP_TAG_INTEGER
)
2829 * Don't use the # copies attribute if we are printing the job sheets...
2833 snprintf(copies
, copies_size
, "%d", attr
->values
[0].integer
);
2835 else if (!strcmp(attr
->name
, "job-name") &&
2836 (attr
->value_tag
== IPP_TAG_NAME
||
2837 attr
->value_tag
== IPP_TAG_NAMELANG
))
2838 strlcpy(title
, attr
->values
[0].string
.text
, title_size
);
2839 else if (attr
->group_tag
== IPP_TAG_JOB
)
2842 * Filter out other unwanted attributes...
2845 if (attr
->value_tag
== IPP_TAG_MIMETYPE
||
2846 attr
->value_tag
== IPP_TAG_NAMELANG
||
2847 attr
->value_tag
== IPP_TAG_TEXTLANG
||
2848 (attr
->value_tag
== IPP_TAG_URI
&& strcmp(attr
->name
, "job-uuid")) ||
2849 attr
->value_tag
== IPP_TAG_URISCHEME
||
2850 attr
->value_tag
== IPP_TAG_BEGIN_COLLECTION
) /* Not yet supported */
2853 if (!strncmp(attr
->name
, "time-", 5))
2856 if (!strncmp(attr
->name
, "job-", 4) &&
2857 strcmp(attr
->name
, "job-billing") &&
2858 strcmp(attr
->name
, "job-impressions") &&
2859 strcmp(attr
->name
, "job-originating-host-name") &&
2860 strcmp(attr
->name
, "job-uuid") &&
2861 !(job
->printer
->type
& CUPS_PRINTER_REMOTE
))
2864 if ((!strcmp(attr
->name
, "job-impressions") ||
2865 !strcmp(attr
->name
, "page-label") ||
2866 !strcmp(attr
->name
, "page-border") ||
2867 !strncmp(attr
->name
, "number-up", 9) ||
2868 !strcmp(attr
->name
, "page-ranges") ||
2869 !strcmp(attr
->name
, "page-set") ||
2870 !strcasecmp(attr
->name
, "AP_FIRSTPAGE_InputSlot") ||
2871 !strcasecmp(attr
->name
, "AP_FIRSTPAGE_ManualFeed") ||
2872 !strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
2873 "PMTotalSidesImaged..n.") ||
2874 !strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
2875 "PMTotalBeginPages..n.")) &&
2880 * Otherwise add them to the list...
2883 if (optptr
> options
)
2884 strlcat(optptr
, " ", optlength
- (optptr
- options
));
2886 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
2888 strlcat(optptr
, attr
->name
, optlength
- (optptr
- options
));
2889 strlcat(optptr
, "=", optlength
- (optptr
- options
));
2892 for (i
= 0; i
< attr
->num_values
; i
++)
2895 strlcat(optptr
, ",", optlength
- (optptr
- options
));
2897 optptr
+= strlen(optptr
);
2899 switch (attr
->value_tag
)
2901 case IPP_TAG_INTEGER
:
2903 snprintf(optptr
, optlength
- (optptr
- options
),
2904 "%d", attr
->values
[i
].integer
);
2907 case IPP_TAG_BOOLEAN
:
2908 if (!attr
->values
[i
].boolean
)
2909 strlcat(optptr
, "no", optlength
- (optptr
- options
));
2911 case IPP_TAG_NOVALUE
:
2912 strlcat(optptr
, attr
->name
,
2913 optlength
- (optptr
- options
));
2916 case IPP_TAG_RANGE
:
2917 if (attr
->values
[i
].range
.lower
== attr
->values
[i
].range
.upper
)
2918 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2919 "%d", attr
->values
[i
].range
.lower
);
2921 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2922 "%d-%d", attr
->values
[i
].range
.lower
,
2923 attr
->values
[i
].range
.upper
);
2926 case IPP_TAG_RESOLUTION
:
2927 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2928 "%dx%d%s", attr
->values
[i
].resolution
.xres
,
2929 attr
->values
[i
].resolution
.yres
,
2930 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
2934 case IPP_TAG_STRING
:
2937 case IPP_TAG_KEYWORD
:
2938 case IPP_TAG_CHARSET
:
2939 case IPP_TAG_LANGUAGE
:
2941 for (valptr
= attr
->values
[i
].string
.text
; *valptr
;)
2943 if (strchr(" \t\n\\\'\"", *valptr
))
2945 *optptr
++ = *valptr
++;
2952 break; /* anti-compiler-warning-code */
2956 optptr
+= strlen(optptr
);
2966 * 'ipp_length()' - Compute the size of the buffer needed to hold
2967 * the textual IPP attributes.
2970 static int /* O - Size of attribute buffer */
2971 ipp_length(ipp_t
*ipp
) /* I - IPP request */
2973 int bytes
; /* Number of bytes */
2974 int i
; /* Looping var */
2975 ipp_attribute_t
*attr
; /* Current attribute */
2979 * Loop through all attributes...
2984 for (attr
= ipp
->attrs
; attr
!= NULL
; attr
= attr
->next
)
2987 * Skip attributes that won't be sent to filters...
2990 if (attr
->value_tag
== IPP_TAG_MIMETYPE
||
2991 attr
->value_tag
== IPP_TAG_NAMELANG
||
2992 attr
->value_tag
== IPP_TAG_TEXTLANG
||
2993 attr
->value_tag
== IPP_TAG_URI
||
2994 attr
->value_tag
== IPP_TAG_URISCHEME
)
2997 if (strncmp(attr
->name
, "time-", 5) == 0)
3001 * Add space for a leading space and commas between each value.
3002 * For the first attribute, the leading space isn't used, so the
3003 * extra byte can be used as the nul terminator...
3006 bytes
++; /* " " separator */
3007 bytes
+= attr
->num_values
; /* "," separators */
3010 * Boolean attributes appear as "foo,nofoo,foo,nofoo", while
3011 * other attributes appear as "foo=value1,value2,...,valueN".
3014 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
3015 bytes
+= strlen(attr
->name
);
3017 bytes
+= attr
->num_values
* strlen(attr
->name
);
3020 * Now add the size required for each value in the attribute...
3023 switch (attr
->value_tag
)
3025 case IPP_TAG_INTEGER
:
3028 * Minimum value of a signed integer is -2147483647, or 11 digits.
3031 bytes
+= attr
->num_values
* 11;
3034 case IPP_TAG_BOOLEAN
:
3036 * Add two bytes for each false ("no") value...
3039 for (i
= 0; i
< attr
->num_values
; i
++)
3040 if (!attr
->values
[i
].boolean
)
3044 case IPP_TAG_RANGE
:
3046 * A range is two signed integers separated by a hyphen, or
3047 * 23 characters max.
3050 bytes
+= attr
->num_values
* 23;
3053 case IPP_TAG_RESOLUTION
:
3055 * A resolution is two signed integers separated by an "x" and
3056 * suffixed by the units, or 26 characters max.
3059 bytes
+= attr
->num_values
* 26;
3062 case IPP_TAG_STRING
:
3065 case IPP_TAG_KEYWORD
:
3066 case IPP_TAG_CHARSET
:
3067 case IPP_TAG_LANGUAGE
:
3070 * Strings can contain characters that need quoting. We need
3071 * at least 2 * len + 2 characters to cover the quotes and
3072 * any backslashes in the string.
3075 for (i
= 0; i
< attr
->num_values
; i
++)
3076 bytes
+= 2 * strlen(attr
->values
[i
].string
.text
) + 2;
3080 break; /* anti-compiler-warning-code */
3089 * 'load_job_cache()' - Load jobs from the job.cache file.
3093 load_job_cache(const char *filename
) /* I - job.cache filename */
3095 cups_file_t
*fp
; /* job.cache file */
3096 char line
[1024], /* Line buffer */
3097 *value
; /* Value on line */
3098 int linenum
; /* Line number in file */
3099 cupsd_job_t
*job
; /* Current job */
3100 int jobid
; /* Job ID */
3101 char jobfile
[1024]; /* Job filename */
3105 * Open the job.cache file...
3108 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
3110 if (errno
!= ENOENT
)
3111 cupsdLogMessage(CUPSD_LOG_ERROR
,
3112 "Unable to open job cache file \"%s\": %s",
3113 filename
, strerror(errno
));
3115 load_request_root();
3121 * Read entries from the job cache file and create jobs as needed.
3124 cupsdLogMessage(CUPSD_LOG_INFO
, "Loading job cache file \"%s\"...",
3130 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
3132 if (!strcasecmp(line
, "NextJobId"))
3135 NextJobId
= atoi(value
);
3137 else if (!strcasecmp(line
, "<Job"))
3141 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing </Job> directive on line %d!",
3148 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing job ID on line %d!", linenum
);
3152 jobid
= atoi(value
);
3156 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad job ID %d on line %d!", jobid
,
3161 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, jobid
);
3162 if (access(jobfile
, 0))
3164 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Job %d] Files have gone away!",
3169 job
= calloc(1, sizeof(cupsd_job_t
));
3172 cupsdLogMessage(CUPSD_LOG_EMERG
,
3173 "[Job %d] Unable to allocate memory for job!", jobid
);
3178 job
->back_pipes
[0] = -1;
3179 job
->back_pipes
[1] = -1;
3180 job
->print_pipes
[0] = -1;
3181 job
->print_pipes
[1] = -1;
3182 job
->side_pipes
[0] = -1;
3183 job
->side_pipes
[1] = -1;
3184 job
->status_pipes
[0] = -1;
3185 job
->status_pipes
[1] = -1;
3187 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Loading from cache...",
3192 cupsdLogMessage(CUPSD_LOG_ERROR
,
3193 "Missing <Job #> directive on line %d!", linenum
);
3196 else if (!strcasecmp(line
, "</Job>"))
3198 cupsArrayAdd(Jobs
, job
);
3200 if (job
->state_value
<= IPP_JOB_STOPPED
)
3202 cupsArrayAdd(ActiveJobs
, job
);
3210 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing value on line %d!", linenum
);
3213 else if (!strcasecmp(line
, "State"))
3215 job
->state_value
= (ipp_jstate_t
)atoi(value
);
3217 if (job
->state_value
< IPP_JOB_PENDING
)
3218 job
->state_value
= IPP_JOB_PENDING
;
3219 else if (job
->state_value
> IPP_JOB_COMPLETED
)
3220 job
->state_value
= IPP_JOB_COMPLETED
;
3222 else if (!strcasecmp(line
, "HoldUntil"))
3224 job
->hold_until
= atoi(value
);
3226 else if (!strcasecmp(line
, "Priority"))
3228 job
->priority
= atoi(value
);
3230 else if (!strcasecmp(line
, "Username"))
3232 cupsdSetString(&job
->username
, value
);
3234 else if (!strcasecmp(line
, "Destination"))
3236 cupsdSetString(&job
->dest
, value
);
3238 else if (!strcasecmp(line
, "DestType"))
3240 job
->dtype
= (cups_ptype_t
)atoi(value
);
3242 else if (!strcasecmp(line
, "NumFiles"))
3244 job
->num_files
= atoi(value
);
3246 if (job
->num_files
< 0)
3248 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad NumFiles value %d on line %d!",
3249 job
->num_files
, linenum
);
3254 if (job
->num_files
> 0)
3256 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-001", RequestRoot
,
3258 if (access(jobfile
, 0))
3260 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Data files have gone away!",
3266 job
->filetypes
= calloc(job
->num_files
, sizeof(mime_type_t
*));
3267 job
->compressions
= calloc(job
->num_files
, sizeof(int));
3269 if (!job
->filetypes
|| !job
->compressions
)
3271 cupsdLogMessage(CUPSD_LOG_EMERG
,
3272 "[Job %d] Unable to allocate memory for %d files!",
3273 job
->id
, job
->num_files
);
3278 else if (!strcasecmp(line
, "File"))
3280 int number
, /* File number */
3281 compression
; /* Compression value */
3282 char super
[MIME_MAX_SUPER
], /* MIME super type */
3283 type
[MIME_MAX_TYPE
]; /* MIME type */
3286 if (sscanf(value
, "%d%*[ \t]%15[^/]/%255s%d", &number
, super
, type
,
3289 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File on line %d!", linenum
);
3293 if (number
< 1 || number
> job
->num_files
)
3295 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File number %d on line %d!",
3302 job
->compressions
[number
] = compression
;
3303 job
->filetypes
[number
] = mimeType(MimeDatabase
, super
, type
);
3305 if (!job
->filetypes
[number
])
3308 * If the original MIME type is unknown, auto-type it!
3311 cupsdLogMessage(CUPSD_LOG_ERROR
,
3312 "[Job %d] Unknown MIME type %s/%s for file %d!",
3313 job
->id
, super
, type
, number
+ 1);
3315 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
3316 job
->id
, number
+ 1);
3317 job
->filetypes
[number
] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
3318 job
->compressions
+ number
);
3321 * If that didn't work, assume it is raw...
3324 if (!job
->filetypes
[number
])
3325 job
->filetypes
[number
] = mimeType(MimeDatabase
, "application",
3330 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unknown %s directive on line %d!",
3339 * 'load_next_job_id()' - Load the NextJobId value from the job.cache file.
3343 load_next_job_id(const char *filename
) /* I - job.cache filename */
3345 cups_file_t
*fp
; /* job.cache file */
3346 char line
[1024], /* Line buffer */
3347 *value
; /* Value on line */
3348 int linenum
; /* Line number in file */
3349 int next_job_id
; /* NextJobId value from line */
3353 * Read the NextJobId directive from the job.cache file and use
3354 * the value (if any).
3357 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
3359 if (errno
!= ENOENT
)
3360 cupsdLogMessage(CUPSD_LOG_ERROR
,
3361 "Unable to open job cache file \"%s\": %s",
3362 filename
, strerror(errno
));
3367 cupsdLogMessage(CUPSD_LOG_INFO
,
3368 "Loading NextJobId from job cache file \"%s\"...", filename
);
3372 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
3374 if (!strcasecmp(line
, "NextJobId"))
3378 next_job_id
= atoi(value
);
3380 if (next_job_id
> NextJobId
)
3381 NextJobId
= next_job_id
;
3392 * 'load_request_root()' - Load jobs from the RequestRoot directory.
3396 load_request_root(void)
3398 cups_dir_t
*dir
; /* Directory */
3399 cups_dentry_t
*dent
; /* Directory entry */
3400 cupsd_job_t
*job
; /* New job */
3404 * Open the requests directory...
3407 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Scanning %s for jobs...", RequestRoot
);
3409 if ((dir
= cupsDirOpen(RequestRoot
)) == NULL
)
3411 cupsdLogMessage(CUPSD_LOG_ERROR
,
3412 "Unable to open spool directory \"%s\": %s",
3413 RequestRoot
, strerror(errno
));
3418 * Read all the c##### files...
3421 while ((dent
= cupsDirRead(dir
)) != NULL
)
3422 if (strlen(dent
->filename
) >= 6 && dent
->filename
[0] == 'c')
3425 * Allocate memory for the job...
3428 if ((job
= calloc(sizeof(cupsd_job_t
), 1)) == NULL
)
3430 cupsdLogMessage(CUPSD_LOG_ERROR
, "Ran out of memory for jobs!");
3436 * Assign the job ID...
3439 job
->id
= atoi(dent
->filename
+ 1);
3440 job
->back_pipes
[0] = -1;
3441 job
->back_pipes
[1] = -1;
3442 job
->print_pipes
[0] = -1;
3443 job
->print_pipes
[1] = -1;
3444 job
->side_pipes
[0] = -1;
3445 job
->side_pipes
[1] = -1;
3446 job
->status_pipes
[0] = -1;
3447 job
->status_pipes
[1] = -1;
3449 if (job
->id
>= NextJobId
)
3450 NextJobId
= job
->id
+ 1;
3459 * Insert the job into the array, sorting by job priority and ID...
3462 cupsArrayAdd(Jobs
, job
);
3464 if (job
->state_value
<= IPP_JOB_STOPPED
)
3465 cupsArrayAdd(ActiveJobs
, job
);
3475 * 'set_hold_until()' - Set the hold time and update job-hold-until attribute.
3479 set_hold_until(cupsd_job_t
*job
, /* I - Job to update */
3480 time_t holdtime
) /* I - Hold until time */
3482 ipp_attribute_t
*attr
; /* job-hold-until attribute */
3483 struct tm
*holddate
; /* Hold date */
3484 char holdstr
[64]; /* Hold time */
3488 * Set the hold_until value and hold the job...
3491 cupsdLogMessage(CUPSD_LOG_DEBUG
, "set_hold_until: hold_until = %d",
3494 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
3495 job
->state_value
= IPP_JOB_HELD
;
3496 job
->hold_until
= holdtime
;
3499 * Update the job-hold-until attribute with a string representing GMT
3500 * time (HH:MM:SS)...
3503 holddate
= gmtime(&holdtime
);
3504 snprintf(holdstr
, sizeof(holdstr
), "%d:%d:%d", holddate
->tm_hour
,
3505 holddate
->tm_min
, holddate
->tm_sec
);
3507 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
3508 IPP_TAG_KEYWORD
)) == NULL
)
3509 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
3512 * Either add the attribute or update the value of the existing one
3516 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "job-hold-until",
3519 cupsdSetString(&attr
->values
[0].string
.text
, holdstr
);
3522 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
3527 * 'set_time()' - Set one of the "time-at-xyz" attributes.
3531 set_time(cupsd_job_t
*job
, /* I - Job to update */
3532 const char *name
) /* I - Name of attribute */
3534 ipp_attribute_t
*attr
; /* Time attribute */
3537 if ((attr
= ippFindAttribute(job
->attrs
, name
, IPP_TAG_ZERO
)) != NULL
)
3539 attr
->value_tag
= IPP_TAG_INTEGER
;
3540 attr
->values
[0].integer
= time(NULL
);
3546 * 'start_job()' - Start a print job.
3550 start_job(cupsd_job_t
*job
, /* I - Job ID */
3551 cupsd_printer_t
*printer
) /* I - Printer to print job */
3553 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "start_job(job=%p(%d), printer=%p(%s))",
3554 job
, job
->id
, printer
, printer
->name
);
3557 * Make sure we have some files around before we try to print...
3560 if (job
->num_files
== 0)
3562 cupsdSetJobState(job
, IPP_JOB_ABORTED
, CUPSD_JOB_DEFAULT
,
3563 "Aborting job because it has no files.");
3568 * Update the printer and job state to "processing"...
3571 if (!cupsdLoadJob(job
))
3574 cupsdSetJobState(job
, IPP_JOB_PROCESSING
, CUPSD_JOB_DEFAULT
, NULL
);
3575 cupsdSetPrinterState(printer
, IPP_PRINTER_PROCESSING
, 0);
3578 job
->current_file
= 0;
3580 job
->printer
= printer
;
3584 * Setup the last exit status and security profiles...
3588 job
->profile
= cupsdCreateProfile(job
->id
);
3591 * Create the status pipes and buffer...
3594 if (cupsdOpenPipe(job
->status_pipes
))
3596 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
3597 "Unable to create job status pipes - %s.", strerror(errno
));
3599 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
3600 "Job stopped because the scheduler could not create the "
3601 "job status pipes.");
3603 cupsdDestroyProfile(job
->profile
);
3604 job
->profile
= NULL
;
3608 job
->status_buffer
= cupsdStatBufNew(job
->status_pipes
[0], NULL
);
3609 job
->status_level
= CUPSD_LOG_INFO
;
3612 * Create the backchannel pipes and make them non-blocking...
3615 if (cupsdOpenPipe(job
->back_pipes
))
3617 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
3618 "Unable to create back-channel pipes - %s.", strerror(errno
));
3620 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
3621 "Job stopped because the scheduler could not create the "
3622 "back-channel pipes.");
3624 cupsdClosePipe(job
->status_pipes
);
3625 cupsdStatBufDelete(job
->status_buffer
);
3626 job
->status_buffer
= NULL
;
3628 cupsdDestroyProfile(job
->profile
);
3629 job
->profile
= NULL
;
3633 fcntl(job
->back_pipes
[0], F_SETFL
,
3634 fcntl(job
->back_pipes
[0], F_GETFL
) | O_NONBLOCK
);
3635 fcntl(job
->back_pipes
[1], F_SETFL
,
3636 fcntl(job
->back_pipes
[1], F_GETFL
) | O_NONBLOCK
);
3639 * Create the side-channel pipes and make them non-blocking...
3642 if (socketpair(AF_LOCAL
, SOCK_STREAM
, 0, job
->side_pipes
))
3644 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
3645 "Unable to create side-channel pipes - %s.", strerror(errno
));
3647 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
3648 "Job stopped because the scheduler could not create the "
3649 "side-channel pipes.");
3651 cupsdClosePipe(job
->back_pipes
);
3653 cupsdClosePipe(job
->status_pipes
);
3654 cupsdStatBufDelete(job
->status_buffer
);
3655 job
->status_buffer
= NULL
;
3657 cupsdDestroyProfile(job
->profile
);
3658 job
->profile
= NULL
;
3662 fcntl(job
->side_pipes
[0], F_SETFL
,
3663 fcntl(job
->side_pipes
[0], F_GETFL
) | O_NONBLOCK
);
3664 fcntl(job
->side_pipes
[1], F_SETFL
,
3665 fcntl(job
->side_pipes
[1], F_GETFL
) | O_NONBLOCK
);
3668 * Now start the first file in the job...
3671 cupsdContinueJob(job
);
3676 * 'stop_job()' - Stop a print job.
3680 stop_job(cupsd_job_t
*job
, /* I - Job */
3681 cupsd_jobaction_t action
) /* I - Action */
3683 int i
; /* Looping var */
3686 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "stop_job(job=%p(%d), action=%d)", job
,
3689 FilterLevel
-= job
->cost
;
3692 if (action
== CUPSD_JOB_DEFAULT
&& !job
->kill_time
)
3693 job
->kill_time
= time(NULL
) + JobKillDelay
;
3694 else if (action
== CUPSD_JOB_FORCE
)
3697 for (i
= 0; job
->filters
[i
]; i
++)
3698 if (job
->filters
[i
] > 0)
3699 cupsdEndProcess(job
->filters
[i
], action
== CUPSD_JOB_FORCE
);
3701 if (job
->backend
> 0)
3702 cupsdEndProcess(job
->backend
, action
== CUPSD_JOB_FORCE
);
3707 * 'unload_job()' - Unload a job from memory.
3711 unload_job(cupsd_job_t
*job
) /* I - Job */
3716 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Unloading...", job
->id
);
3718 ippDelete(job
->attrs
);
3723 job
->job_sheets
= NULL
;
3724 job
->printer_message
= NULL
;
3725 job
->printer_reasons
= NULL
;
3730 * 'update_job()' - Read a status update from a job's filters.
3734 update_job(cupsd_job_t
*job
) /* I - Job to check */
3736 int i
; /* Looping var */
3737 int copies
; /* Number of copies printed */
3738 char message
[1024], /* Message text */
3739 *ptr
; /* Pointer update... */
3740 int loglevel
, /* Log level for message */
3741 event
= 0; /* Events? */
3742 static const char * const levels
[] = /* Log levels */
3758 * Get the printer associated with this job; if the printer is stopped for
3759 * any reason then job->printer will be reset to NULL, so make sure we have
3760 * a valid pointer...
3763 while ((ptr
= cupsdStatBufUpdate(job
->status_buffer
, &loglevel
,
3764 message
, sizeof(message
))) != NULL
)
3767 * Process page and printer state messages as needed...
3770 if (loglevel
== CUPSD_LOG_PAGE
)
3773 * Page message; send the message to the page_log file and update the
3774 * job sheet count...
3777 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "PAGE: %s", message
);
3781 if (!strncasecmp(message
, "total ", 6))
3784 * Got a total count of pages from a backend or filter...
3787 copies
= atoi(message
+ 6);
3788 copies
-= job
->sheets
->values
[0].integer
; /* Just track the delta */
3790 else if (!sscanf(message
, "%*d%d", &copies
))
3793 job
->sheets
->values
[0].integer
+= copies
;
3795 if (job
->printer
->page_limit
)
3797 cupsd_quota_t
*q
= cupsdUpdateQuota(job
->printer
, job
->username
,
3801 if (AppleQuotas
&& q
->page_count
== -3)
3804 * Quota limit exceeded, cancel job in progress immediately...
3807 cupsdSetJobState(job
, IPP_JOB_CANCELED
, CUPSD_JOB_DEFAULT
,
3808 "Canceled job because pages exceed user %s "
3809 "quota limit on printer %s (%s).",
3810 job
->username
, job
->printer
->name
,
3811 job
->printer
->info
);
3816 #endif /* __APPLE__ */
3820 cupsdLogPage(job
, message
);
3823 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
3824 "Printed %d page(s).", job
->sheets
->values
[0].integer
);
3826 else if (loglevel
== CUPSD_LOG_STATE
)
3828 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "STATE: %s", message
);
3830 if (!strcmp(message
, "paused"))
3832 cupsdStopPrinter(job
->printer
, 1);
3837 cupsdSetPrinterReasons(job
->printer
, message
);
3838 cupsdAddPrinterHistory(job
->printer
);
3839 event
|= CUPSD_EVENT_PRINTER_STATE
;
3842 update_job_attrs(job
, 0);
3844 else if (loglevel
== CUPSD_LOG_ATTR
)
3847 * Set attribute(s)...
3850 int num_attrs
; /* Number of attributes */
3851 cups_option_t
*attrs
; /* Attributes */
3852 const char *attr
; /* Attribute */
3855 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "ATTR: %s", message
);
3857 num_attrs
= cupsParseOptions(message
, 0, &attrs
);
3859 if ((attr
= cupsGetOption("auth-info-required", num_attrs
,
3862 cupsdSetAuthInfoRequired(job
->printer
, attr
, NULL
);
3863 cupsdSetPrinterAttrs(job
->printer
);
3865 if (job
->printer
->type
& CUPS_PRINTER_DISCOVERED
)
3866 cupsdMarkDirty(CUPSD_DIRTY_REMOTE
);
3868 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3871 if ((attr
= cupsGetOption("job-media-progress", num_attrs
,
3874 int progress
= atoi(attr
);
3877 if (progress
>= 0 && progress
<= 100)
3879 job
->progress
= progress
;
3882 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
3883 "Printing page %d, %d%%",
3884 job
->sheets
->values
[0].integer
, job
->progress
);
3888 if ((attr
= cupsGetOption("printer-alert", num_attrs
, attrs
)) != NULL
)
3890 cupsdSetString(&job
->printer
->alert
, attr
);
3891 event
|= CUPSD_EVENT_PRINTER_STATE
;
3894 if ((attr
= cupsGetOption("printer-alert-description", num_attrs
,
3897 cupsdSetString(&job
->printer
->alert_description
, attr
);
3898 event
|= CUPSD_EVENT_PRINTER_STATE
;
3901 if ((attr
= cupsGetOption("marker-colors", num_attrs
, attrs
)) != NULL
)
3903 cupsdSetPrinterAttr(job
->printer
, "marker-colors", (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-levels", num_attrs
, attrs
)) != NULL
)
3911 cupsdSetPrinterAttr(job
->printer
, "marker-levels", (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-low-levels", num_attrs
, attrs
)) != NULL
)
3919 cupsdSetPrinterAttr(job
->printer
, "marker-low-levels", (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-high-levels", num_attrs
, attrs
)) != NULL
)
3927 cupsdSetPrinterAttr(job
->printer
, "marker-high-levels", (char *)attr
);
3928 job
->printer
->marker_time
= time(NULL
);
3929 event
|= CUPSD_EVENT_PRINTER_STATE
;
3930 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3933 if ((attr
= cupsGetOption("marker-message", num_attrs
, attrs
)) != NULL
)
3935 cupsdSetPrinterAttr(job
->printer
, "marker-message", (char *)attr
);
3936 job
->printer
->marker_time
= time(NULL
);
3937 event
|= CUPSD_EVENT_PRINTER_STATE
;
3938 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3941 if ((attr
= cupsGetOption("marker-names", num_attrs
, attrs
)) != NULL
)
3943 cupsdSetPrinterAttr(job
->printer
, "marker-names", (char *)attr
);
3944 job
->printer
->marker_time
= time(NULL
);
3945 event
|= CUPSD_EVENT_PRINTER_STATE
;
3946 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3949 if ((attr
= cupsGetOption("marker-types", num_attrs
, attrs
)) != NULL
)
3951 cupsdSetPrinterAttr(job
->printer
, "marker-types", (char *)attr
);
3952 job
->printer
->marker_time
= time(NULL
);
3953 event
|= CUPSD_EVENT_PRINTER_STATE
;
3954 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3957 cupsFreeOptions(num_attrs
, attrs
);
3959 else if (loglevel
== CUPSD_LOG_PPD
)
3962 * Set attribute(s)...
3965 int num_keywords
; /* Number of keywords */
3966 cups_option_t
*keywords
; /* Keywords */
3969 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "PPD: %s", message
);
3971 num_keywords
= cupsParseOptions(message
, 0, &keywords
);
3973 if (cupsdUpdatePrinterPPD(job
->printer
, num_keywords
, keywords
))
3974 cupsdSetPrinterAttrs(job
->printer
);
3976 cupsFreeOptions(num_keywords
, keywords
);
3979 else if (!strncmp(message
, "recoverable:", 12))
3982 while (isspace(*ptr
& 255))
3987 cupsdSetPrinterReasons(job
->printer
,
3988 "+com.apple.print.recoverable-warning");
3989 cupsdSetString(&(job
->printer
->recoverable
), ptr
);
3990 cupsdAddPrinterHistory(job
->printer
);
3991 event
|= CUPSD_EVENT_PRINTER_STATE
;
3994 else if (!strncmp(message
, "recovered:", 10))
3996 cupsdSetPrinterReasons(job
->printer
,
3997 "-com.apple.print.recoverable-warning");
4000 while (isspace(*ptr
& 255))
4003 cupsdSetString(&(job
->printer
->recoverable
), ptr
);
4004 cupsdAddPrinterHistory(job
->printer
);
4005 event
|= CUPSD_EVENT_PRINTER_STATE
;
4007 #endif /* __APPLE__ */
4010 if (loglevel
!= CUPSD_LOG_INFO
&& loglevel
> LogLevel
)
4011 cupsdLogJob(job
, loglevel
, "%s", message
);
4013 if (loglevel
< CUPSD_LOG_DEBUG
)
4015 strlcpy(job
->printer
->state_message
, message
,
4016 sizeof(job
->printer
->state_message
));
4017 cupsdAddPrinterHistory(job
->printer
);
4019 event
|= CUPSD_EVENT_PRINTER_STATE
| CUPSD_EVENT_JOB_PROGRESS
;
4021 if (loglevel
<= job
->status_level
)
4024 * Some messages show in the job-printer-state-message attribute...
4027 if (loglevel
!= CUPSD_LOG_NOTICE
)
4028 job
->status_level
= loglevel
;
4030 update_job_attrs(job
, 1);
4032 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
4033 "Set job-printer-state-message to \"%s\", "
4035 job
->printer_message
->values
[0].string
.text
,
4036 levels
[job
->status_level
]);
4041 if (!strchr(job
->status_buffer
->buffer
, '\n'))
4045 if (event
& CUPSD_EVENT_PRINTER_STATE
)
4046 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, job
->printer
, NULL
,
4047 (job
->printer
->type
& CUPS_PRINTER_CLASS
) ?
4048 "Class \"%s\" state changed." :
4049 "Printer \"%s\" state changed.",
4050 job
->printer
->name
);
4052 if (event
& CUPSD_EVENT_JOB_PROGRESS
)
4053 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
4054 "%s", job
->printer
->state_message
);
4056 if (ptr
== NULL
&& !job
->status_buffer
->bufused
)
4059 * See if all of the filters and the backend have returned their
4063 for (i
= 0; job
->filters
[i
] < 0; i
++);
4065 if (job
->filters
[i
])
4068 * EOF but we haven't collected the exit status of all filters...
4071 cupsdCheckProcess();
4075 if (job
->current_file
>= job
->num_files
&& job
->backend
> 0)
4078 * EOF but we haven't collected the exit status of the backend...
4081 cupsdCheckProcess();
4086 * Handle the end of job stuff...
4092 * Check for new jobs...
4101 * 'update_job_attrs()' - Update the job-printer-* attributes.
4105 update_job_attrs(cupsd_job_t
*job
, /* I - Job to update */
4106 int do_message
)/* I - 1 = copy job-printer-state message */
4108 int i
; /* Looping var */
4109 int num_reasons
; /* Actual number of reasons */
4110 const char * const *reasons
; /* Reasons */
4111 static const char *none
= "none"; /* "none" reason */
4115 * Get/create the job-printer-state-* attributes...
4118 if (!job
->printer_message
)
4120 if ((job
->printer_message
= ippFindAttribute(job
->attrs
,
4121 "job-printer-state-message",
4122 IPP_TAG_TEXT
)) == NULL
)
4123 job
->printer_message
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_TEXT
,
4124 "job-printer-state-message",
4128 if (!job
->printer_reasons
)
4129 job
->printer_reasons
= ippFindAttribute(job
->attrs
,
4130 "job-printer-state-reasons",
4134 * Copy or clear the printer-state-message value as needed...
4137 if (job
->state_value
!= IPP_JOB_PROCESSING
&&
4138 job
->status_level
== CUPSD_LOG_INFO
)
4139 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
), "");
4140 else if (job
->printer
->state_message
[0] && do_message
)
4141 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
),
4142 job
->printer
->state_message
);
4145 * ... and the printer-state-reasons value...
4148 if (job
->printer
->num_reasons
== 0)
4155 num_reasons
= job
->printer
->num_reasons
;
4156 reasons
= (const char * const *)job
->printer
->reasons
;
4159 if (!job
->printer_reasons
|| job
->printer_reasons
->num_values
!= num_reasons
)
4162 * Replace/create a job-printer-state-reasons attribute...
4165 ippDeleteAttribute(job
->attrs
, job
->printer_reasons
);
4167 job
->printer_reasons
= ippAddStrings(job
->attrs
,
4168 IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
4169 "job-printer-state-reasons",
4170 num_reasons
, NULL
, NULL
);
4175 * Don't bother clearing the reason strings if they are the same...
4178 for (i
= 0; i
< num_reasons
; i
++)
4179 if (strcmp(job
->printer_reasons
->values
[i
].string
.text
, reasons
[i
]))
4182 if (i
>= num_reasons
)
4186 * Not the same, so free the current strings...
4189 for (i
= 0; i
< num_reasons
; i
++)
4190 _cupsStrFree(job
->printer_reasons
->values
[i
].string
.text
);
4194 * Copy the reasons...
4197 for (i
= 0; i
< num_reasons
; i
++)
4198 job
->printer_reasons
->values
[i
].string
.text
= _cupsStrAlloc(reasons
[i
]);
4203 * End of "$Id: job.c 7902 2008-09-03 14:20:17Z mike $".