4 * Job management routines for the CUPS scheduler.
6 * Copyright 2007-2012 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
25 * cupsdDeleteJob() - Free all memory used by a job.
26 * cupsdFreeAllJobs() - Free all jobs from memory.
27 * cupsdFindJob() - Find the specified job.
28 * cupsdGetPrinterJobCount() - Get the number of pending, processing, or
29 * held jobs in a printer or class.
30 * cupsdGetUserJobCount() - Get the number of pending, processing, or
31 * held jobs for a user.
32 * cupsdLoadAllJobs() - Load all jobs from disk.
33 * cupsdLoadJob() - Load a single job.
34 * cupsdMoveJob() - Move the specified job to a different
36 * cupsdReleaseJob() - Release the specified job.
37 * cupsdRestartJob() - Restart the specified job.
38 * cupsdSaveAllJobs() - Save a summary of all jobs to disk.
39 * cupsdSaveJob() - Save a job to disk.
40 * cupsdSetJobHoldUntil() - Set the hold time for a job.
41 * cupsdSetJobPriority() - Set the priority of a job, moving it up/down
42 * in the list as needed.
43 * cupsdSetJobState() - Set the state of the specified print job.
44 * cupsdStopAllJobs() - Stop all print jobs.
45 * cupsdUnloadCompletedJobs() - Flush completed job history from memory.
46 * cupsdUpdateJobs() - Update the history/file files for all jobs.
47 * compare_active_jobs() - Compare the job IDs and priorities of two
49 * compare_jobs() - Compare the job IDs of two jobs.
50 * dump_job_history() - Dump any debug messages for a job.
51 * free_job_history() - Free any log history.
52 * finalize_job() - Cleanup after job filter processes and
54 * get_options() - Get a string containing the job options.
55 * ipp_length() - Compute the size of the buffer needed to hold
56 * the textual IPP attributes.
57 * load_job_cache() - Load jobs from the job.cache file.
58 * load_next_job_id() - Load the NextJobId value from the job.cache
60 * load_request_root() - Load jobs from the RequestRoot directory.
61 * remove_job_files() - Remove the document files for a job.
62 * remove_job_history() - Remove the control file for a job.
63 * set_time() - Set one of the "time-at-xyz" attributes.
64 * start_job() - Start a print job.
65 * stop_job() - Stop a print job.
66 * unload_job() - Unload a job from memory.
67 * update_job() - Read a status update from a job's filters.
68 * update_job_attrs() - Update the job-printer-* attributes.
72 * Include necessary headers...
77 #include <cups/backend.h>
80 # include <IOKit/pwr_mgt/IOPMLib.h>
81 # ifdef HAVE_IOKIT_PWR_MGT_IOPMLIBPRIVATE_H
82 # include <IOKit/pwr_mgt/IOPMLibPrivate.h>
83 # endif /* HAVE_IOKIT_PWR_MGT_IOPMLIBPRIVATE_H */
84 #endif /* __APPLE__ */
88 * Design Notes for Job Management
89 * -------------------------------
93 * pending Do nothing/check jobs
94 * pending-held Send SIGTERM to filters and backend
95 * processing Do nothing/start job
96 * stopped Send SIGKILL to filters and backend
97 * canceled Send SIGTERM to filters and backend
101 * Finalize clears the printer <-> job association, deletes the status
102 * buffer, closes all of the pipes, etc. and doesn't get run until all of
103 * the print processes are finished.
105 * UNLOADING OF JOBS (cupsdUnloadCompletedJobs)
107 * We unload the job attributes when they are not needed to reduce overall
108 * memory consumption. We don't unload jobs where job->state_value <
109 * IPP_JOB_STOPPED, job->printer != NULL, or job->access_time is recent.
111 * STARTING OF JOBS (start_job)
113 * When a job is started, a status buffer, several pipes, a security
114 * profile, and a backend process are created for the life of that job.
115 * These are shared for every file in a job. For remote print jobs, the
116 * IPP backend is provided with every file in the job and no filters are
119 * The job->printer member tracks which printer is printing a job, which
120 * can be different than the destination in job->dest for classes. The
121 * printer object also has a job pointer to track which job is being
124 * PRINTING OF JOB FILES (cupsdContinueJob)
126 * Each file in a job is filtered by 0 or more programs. After getting the
127 * list of filters needed and the total cost, the job is either passed or
128 * put back to the processing state until the current FilterLevel comes down
129 * enough to allow printing.
131 * If we can print, we build a string for the print options and run each of
132 * the filters, piping the output from one into the next.
134 * JOB STATUS UPDATES (update_job)
136 * The update_job function gets called whenever there are pending messages
137 * on the status pipe. These generally are updates to the marker-*,
138 * printer-state-message, or printer-state-reasons attributes. On EOF,
139 * finalize_job is called to clean up.
141 * FINALIZING JOBS (finalize_job)
143 * When all filters and the backend are done, we set the job state to
144 * completed (no errors), aborted (filter errors or abort-job policy),
145 * pending-held (auth required or retry-job policy), or pending
146 * (retry-current-job or stop-printer policies) as appropriate.
148 * Then we close the pipes and free the status buffers and profiles.
150 * JOB FILE COMPLETION (process_children in main.c)
152 * For multiple-file jobs, process_children (in main.c) sees that all
153 * filters have exited and calls in to print the next file if there are
154 * more files in the job, otherwise it waits for the backend to exit and
155 * update_job to do the cleanup.
163 static mime_filter_t gziptoany_filter
=
165 NULL
, /* Source type */
166 NULL
, /* Destination type */
168 "gziptoany" /* Filter program to run */
176 static int compare_active_jobs(void *first
, void *second
, void *data
);
177 static int compare_jobs(void *first
, void *second
, void *data
);
178 static void dump_job_history(cupsd_job_t
*job
);
179 static void finalize_job(cupsd_job_t
*job
, int set_job_state
);
180 static void free_job_history(cupsd_job_t
*job
);
181 static char *get_options(cupsd_job_t
*job
, int banner_page
, char *copies
,
182 size_t copies_size
, char *title
,
184 static size_t ipp_length(ipp_t
*ipp
);
185 static void load_job_cache(const char *filename
);
186 static void load_next_job_id(const char *filename
);
187 static void load_request_root(void);
188 static void remove_job_files(cupsd_job_t
*job
);
189 static void remove_job_history(cupsd_job_t
*job
);
190 static void set_time(cupsd_job_t
*job
, const char *name
);
191 static void start_job(cupsd_job_t
*job
, cupsd_printer_t
*printer
);
192 static void stop_job(cupsd_job_t
*job
, cupsd_jobaction_t action
);
193 static void unload_job(cupsd_job_t
*job
);
194 static void update_job(cupsd_job_t
*job
);
195 static void update_job_attrs(cupsd_job_t
*job
, int do_message
);
199 * 'cupsdAddJob()' - Add a new job to the job queue.
202 cupsd_job_t
* /* O - New job record */
203 cupsdAddJob(int priority
, /* I - Job priority */
204 const char *dest
) /* I - Job destination */
206 cupsd_job_t
*job
; /* New job record */
209 if ((job
= calloc(sizeof(cupsd_job_t
), 1)) == NULL
)
212 job
->id
= NextJobId
++;
213 job
->priority
= priority
;
214 job
->back_pipes
[0] = -1;
215 job
->back_pipes
[1] = -1;
216 job
->print_pipes
[0] = -1;
217 job
->print_pipes
[1] = -1;
218 job
->side_pipes
[0] = -1;
219 job
->side_pipes
[1] = -1;
220 job
->status_pipes
[0] = -1;
221 job
->status_pipes
[1] = -1;
223 cupsdSetString(&job
->dest
, dest
);
226 * Add the new job to the "all jobs" and "active jobs" lists...
229 cupsArrayAdd(Jobs
, job
);
230 cupsArrayAdd(ActiveJobs
, job
);
237 * 'cupsdCancelJobs()' - Cancel all jobs for the given destination/user.
241 cupsdCancelJobs(const char *dest
, /* I - Destination to cancel */
242 const char *username
, /* I - Username or NULL */
243 int purge
) /* I - Purge jobs? */
245 cupsd_job_t
*job
; /* Current job */
248 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
250 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
252 if ((!job
->dest
|| !job
->username
) && !cupsdLoadJob(job
))
255 if ((!dest
|| !strcmp(job
->dest
, dest
)) &&
256 (!username
|| !strcmp(job
->username
, username
)))
259 * Cancel all jobs matching this destination/user...
263 cupsdSetJobState(job
, IPP_JOB_CANCELED
, CUPSD_JOB_PURGE
,
264 "Job purged by user.");
265 else if (job
->state_value
< IPP_JOB_CANCELED
)
266 cupsdSetJobState(job
, IPP_JOB_CANCELED
, CUPSD_JOB_DEFAULT
,
267 "Job canceled by user.");
276 * 'cupsdCheckJobs()' - Check the pending jobs and start any if the destination
283 cupsd_job_t
*job
; /* Current job in queue */
284 cupsd_printer_t
*printer
, /* Printer destination */
285 *pclass
; /* Printer class destination */
286 ipp_attribute_t
*attr
; /* Job attribute */
287 time_t curtime
; /* Current time */
290 curtime
= time(NULL
);
292 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
293 "cupsdCheckJobs: %d active jobs, sleeping=%d, reload=%d, "
294 "curtime=%ld", cupsArrayCount(ActiveJobs
), Sleeping
,
295 NeedReload
, (long)curtime
);
297 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
299 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
301 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
302 "cupsdCheckJobs: Job %d - dest=\"%s\", printer=%p, "
303 "state=%d, cancel_time=%ld, hold_until=%ld, kill_time=%ld, "
304 "pending_cost=%d, pending_timeout=%ld", job
->id
, job
->dest
,
305 job
->printer
, job
->state_value
, (long)job
->cancel_time
,
306 (long)job
->hold_until
, (long)job
->kill_time
,
307 job
->pending_cost
, (long)job
->pending_timeout
);
310 * Kill jobs if they are unresponsive...
313 if (job
->kill_time
&& job
->kill_time
<= curtime
)
315 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Stopping unresponsive job.");
317 stop_job(job
, CUPSD_JOB_FORCE
);
322 * Cancel stuck jobs...
325 if (job
->cancel_time
&& job
->cancel_time
<= curtime
)
327 cupsdSetJobState(job
, IPP_JOB_CANCELED
, CUPSD_JOB_DEFAULT
,
328 "Canceling stuck job after %d seconds.", MaxJobTime
);
333 * Start held jobs if they are ready...
336 if (job
->state_value
== IPP_JOB_HELD
&&
338 job
->hold_until
< curtime
)
340 if (job
->pending_timeout
)
343 * This job is pending; check that we don't have an active Send-Document
344 * operation in progress on any of the client connections, then timeout
345 * the job so we can start printing...
348 cupsd_client_t
*con
; /* Current client connection */
350 for (con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
352 con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
354 con
->request
->request
.op
.operation_id
== IPP_SEND_DOCUMENT
)
360 if (cupsdTimeoutJob(job
))
364 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
365 "Job submission timed out.");
369 * Continue jobs that are waiting on the FilterLimit...
372 if (job
->pending_cost
> 0 &&
373 ((FilterLevel
+ job
->pending_cost
) < FilterLimit
|| FilterLevel
== 0))
374 cupsdContinueJob(job
);
377 * Start pending jobs if the destination is available...
380 if (job
->state_value
== IPP_JOB_PENDING
&& !NeedReload
&&
381 #ifndef kIOPMAssertionTypeDenySystemSleep
383 #endif /* !kIOPMAssertionTypeDenySystemSleep */
384 !DoingShutdown
&& !job
->printer
)
386 printer
= cupsdFindDest(job
->dest
);
389 while (printer
&& (printer
->type
& CUPS_PRINTER_CLASS
))
392 * If the class is remote, just pass it to the remote server...
397 if (pclass
->state
== IPP_PRINTER_STOPPED
)
399 else if (pclass
->type
& CUPS_PRINTER_REMOTE
)
402 printer
= cupsdFindAvailablePrinter(printer
->name
);
405 if (!printer
&& !pclass
)
408 * Whoa, the printer and/or class for this destination went away;
412 cupsdSetJobState(job
, IPP_JOB_ABORTED
, CUPSD_JOB_PURGE
,
413 "Job aborted because the destination printer/class "
416 else if (printer
&& !printer
->holding_new_jobs
)
419 * See if the printer is available or remote and not printing a job;
420 * if so, start the job...
426 * Add/update a job-actual-printer-uri attribute for this job
427 * so that we know which printer actually printed the job...
430 if ((attr
= ippFindAttribute(job
->attrs
, "job-actual-printer-uri",
431 IPP_TAG_URI
)) != NULL
)
432 cupsdSetString(&attr
->values
[0].string
.text
, printer
->uri
);
434 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
,
435 "job-actual-printer-uri", NULL
, printer
->uri
);
438 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
441 if (!printer
->job
&& printer
->state
== IPP_PRINTER_IDLE
)
447 start_job(job
, printer
);
456 * 'cupsdCleanJobs()' - Clean out old jobs.
462 cupsd_job_t
*job
; /* Current job */
463 time_t curtime
; /* Current time */
466 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
467 "cupsdCleanJobs: MaxJobs=%d, JobHistory=%d, JobFiles=%d",
468 MaxJobs
, JobHistory
, JobFiles
);
470 if (MaxJobs
<= 0 && JobHistory
== INT_MAX
&& JobFiles
== INT_MAX
)
473 curtime
= time(NULL
);
474 JobHistoryUpdate
= 0;
476 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
478 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
480 if (job
->state_value
>= IPP_JOB_CANCELED
&& !job
->printer
)
483 * Expire old jobs (or job files)...
486 if ((MaxJobs
> 0 && cupsArrayCount(Jobs
) >= MaxJobs
) ||
487 (job
->history_time
&& job
->history_time
<= curtime
))
489 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Removing from history.");
490 cupsdDeleteJob(job
, CUPSD_JOB_PURGE
);
492 else if (job
->file_time
&& job
->file_time
<= curtime
)
494 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Removing document files.");
495 remove_job_files(job
);
497 if (job
->history_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
498 JobHistoryUpdate
= job
->history_time
;
502 if (job
->history_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
503 JobHistoryUpdate
= job
->history_time
;
505 if (job
->file_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
506 JobHistoryUpdate
= job
->file_time
;
511 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdCleanJobs: JobHistoryUpdate=%ld",
512 (long)JobHistoryUpdate
);
517 * 'cupsdContinueJob()' - Continue printing with the next file in a job.
521 cupsdContinueJob(cupsd_job_t
*job
) /* I - Job */
523 int i
; /* Looping var */
524 int slot
; /* Pipe slot */
525 cups_array_t
*filters
= NULL
,/* Filters for job */
526 *prefilters
; /* Filters with prefilters */
527 mime_filter_t
*filter
, /* Current filter */
528 *prefilter
, /* Prefilter */
529 port_monitor
; /* Port monitor filter */
530 char scheme
[255]; /* Device URI scheme */
531 ipp_attribute_t
*attr
; /* Current attribute */
532 const char *ptr
, /* Pointer into value */
533 *abort_message
; /* Abort message */
534 ipp_jstate_t abort_state
= IPP_JOB_STOPPED
;
535 /* New job state on abort */
536 struct stat backinfo
; /* Backend file information */
537 int backroot
; /* Run backend as root? */
538 int pid
; /* Process ID of new filter process */
539 int banner_page
; /* 1 if banner page, 0 otherwise */
540 int filterfds
[2][2] = { { -1, -1 }, { -1, -1 } };
541 /* Pipes used between filters */
542 int envc
; /* Number of environment variables */
543 struct stat fileinfo
; /* Job file information */
544 char **argv
= NULL
, /* Filter command-line arguments */
545 filename
[1024], /* Job filename */
546 command
[1024], /* Full path to command */
547 jobid
[255], /* Job ID string */
549 /* Job title string */
550 copies
[255], /* # copies string */
551 *options
, /* Options string */
553 /* Environment variables */
554 charset
[255], /* CHARSET env variable */
555 class_name
[255],/* CLASS env variable */
556 classification
[1024],
557 /* CLASSIFICATION env variable */
559 /* CONTENT_TYPE env variable */
561 /* DEVICE_URI env variable */
562 final_content_type
[1024] = "",
563 /* FINAL_CONTENT_TYPE env variable */
564 lang
[255], /* LANG env variable */
567 /* APPLE_LANGUAGE env variable */
568 #endif /* __APPLE__ */
569 auth_info_required
[255],
570 /* AUTH_INFO_REQUIRED env variable */
571 ppd
[1024], /* PPD env variable */
573 /* PRINTER_INFO env variable */
574 printer_location
[255],
575 /* PRINTER_LOCATION env variable */
577 /* PRINTER env variable */
578 *printer_state_reasons
= NULL
,
579 /* PRINTER_STATE_REASONS env var */
581 /* RIP_MAX_CACHE env variable */
584 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
585 "cupsdContinueJob(job=%p(%d)): current_file=%d, num_files=%d",
586 job
, job
->id
, job
->current_file
, job
->num_files
);
589 * Figure out what filters are required to convert from
590 * the source to the destination type...
593 FilterLevel
-= job
->cost
;
596 job
->pending_cost
= 0;
598 memset(job
->filters
, 0, sizeof(job
->filters
));
600 if (job
->printer
->raw
)
603 * Remote jobs and raw queues go directly to the printer without
607 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Sending job to queue tagged as raw...");
612 * Local jobs get filtered...
615 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
616 job
->id
, job
->current_file
+ 1);
617 if (stat(filename
, &fileinfo
))
618 fileinfo
.st_size
= 0;
620 filters
= mimeFilter2(MimeDatabase
, job
->filetypes
[job
->current_file
],
621 fileinfo
.st_size
, job
->printer
->filetype
,
626 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
627 "Unable to convert file %d to printable format.",
630 abort_message
= "Aborting job because it cannot be printed.";
631 abort_state
= IPP_JOB_ABORTED
;
633 ippSetString(job
->attrs
, &job
->reasons
, 0, "document-unprintable-error");
638 * Figure out the final content type...
641 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "%d filters for job:",
642 cupsArrayCount(filters
));
643 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
645 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
646 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "%s (%s/%s to %s/%s, cost %d)",
648 filter
->src
? filter
->src
->super
: "???",
649 filter
->src
? filter
->src
->type
: "???",
650 filter
->dst
? filter
->dst
->super
: "???",
651 filter
->dst
? filter
->dst
->type
: "???",
654 if (!job
->printer
->remote
)
656 for (filter
= (mime_filter_t
*)cupsArrayLast(filters
);
657 filter
&& filter
->dst
;
658 filter
= (mime_filter_t
*)cupsArrayPrev(filters
))
659 if (strcmp(filter
->dst
->super
, "printer") ||
660 strcmp(filter
->dst
->type
, job
->printer
->name
))
663 if (filter
&& filter
->dst
)
665 if ((ptr
= strchr(filter
->dst
->type
, '/')) != NULL
)
666 snprintf(final_content_type
, sizeof(final_content_type
),
667 "FINAL_CONTENT_TYPE=%s", ptr
+ 1);
669 snprintf(final_content_type
, sizeof(final_content_type
),
670 "FINAL_CONTENT_TYPE=%s/%s", filter
->dst
->super
,
674 snprintf(final_content_type
, sizeof(final_content_type
),
675 "FINAL_CONTENT_TYPE=printer/%s", job
->printer
->name
);
679 * Remove NULL ("-") filters...
682 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
684 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
685 if (!strcmp(filter
->filter
, "-"))
686 cupsArrayRemove(filters
, filter
);
688 if (cupsArrayCount(filters
) == 0)
690 cupsArrayDelete(filters
);
695 * If this printer has any pre-filters, insert the required pre-filter
696 * in the filters array...
699 if (job
->printer
->prefiltertype
&& filters
)
701 prefilters
= cupsArrayNew(NULL
, NULL
);
703 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
705 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
707 if ((prefilter
= mimeFilterLookup(MimeDatabase
, filter
->src
,
708 job
->printer
->prefiltertype
)))
710 cupsArrayAdd(prefilters
, prefilter
);
711 job
->cost
+= prefilter
->cost
;
714 cupsArrayAdd(prefilters
, filter
);
717 cupsArrayDelete(filters
);
718 filters
= prefilters
;
723 * Set a minimum cost of 100 for all jobs so that FilterLimit
724 * works with raw queues and other low-cost paths.
731 * See if the filter cost is too high...
734 if ((FilterLevel
+ job
->cost
) > FilterLimit
&& FilterLevel
> 0 &&
738 * Don't print this job quite yet...
741 cupsArrayDelete(filters
);
743 cupsdLogJob(job
, CUPSD_LOG_INFO
,
744 "Holding because filter limit has been reached.");
745 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
746 "cupsdContinueJob: file=%d, cost=%d, level=%d, limit=%d",
747 job
->current_file
, job
->cost
, FilterLevel
,
750 job
->pending_cost
= job
->cost
;
755 FilterLevel
+= job
->cost
;
758 * Add decompression/raw filter as needed...
761 if ((!job
->printer
->raw
&& job
->compressions
[job
->current_file
]) ||
762 (!filters
&& !job
->printer
->remote
&&
763 (job
->num_files
> 1 || !strncmp(job
->printer
->device_uri
, "file:", 5))))
766 * Add gziptoany filter to the front of the list...
770 filters
= cupsArrayNew(NULL
, NULL
);
772 if (!cupsArrayInsert(filters
, &gziptoany_filter
))
774 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
775 "Unable to add decompression filter - %s", strerror(errno
));
777 cupsArrayDelete(filters
);
779 abort_message
= "Stopping job because the scheduler ran out of memory.";
786 * Add port monitor, if any...
789 if (job
->printer
->port_monitor
)
792 * Add port monitor to the end of the list...
796 filters
= cupsArrayNew(NULL
, NULL
);
798 port_monitor
.src
= NULL
;
799 port_monitor
.dst
= NULL
;
800 port_monitor
.cost
= 0;
802 snprintf(port_monitor
.filter
, sizeof(port_monitor
.filter
),
803 "%s/monitor/%s", ServerBin
, job
->printer
->port_monitor
);
805 if (!cupsArrayAdd(filters
, &port_monitor
))
807 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
808 "Unable to add port monitor - %s", strerror(errno
));
810 abort_message
= "Stopping job because the scheduler ran out of memory.";
817 * Make sure we don't go over the "MAX_FILTERS" limit...
820 if (cupsArrayCount(filters
) > MAX_FILTERS
)
822 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
823 "Too many filters (%d > %d), unable to print.",
824 cupsArrayCount(filters
), MAX_FILTERS
);
826 abort_message
= "Aborting job because it needs too many filters to print.";
827 abort_state
= IPP_JOB_ABORTED
;
829 ippSetString(job
->attrs
, &job
->reasons
, 0, "document-unprintable-error");
835 * Determine if we are printing a banner page or not...
838 if (job
->job_sheets
== NULL
)
840 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "No job-sheets attribute.");
841 if ((job
->job_sheets
=
842 ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_ZERO
)) != NULL
)
843 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
844 "... but someone added one without setting job_sheets.");
846 else if (job
->job_sheets
->num_values
== 1)
847 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "job-sheets=%s",
848 job
->job_sheets
->values
[0].string
.text
);
850 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "job-sheets=%s,%s",
851 job
->job_sheets
->values
[0].string
.text
,
852 job
->job_sheets
->values
[1].string
.text
);
854 if (job
->printer
->type
& CUPS_PRINTER_REMOTE
)
856 else if (job
->job_sheets
== NULL
)
858 else if (_cups_strcasecmp(job
->job_sheets
->values
[0].string
.text
, "none") != 0 &&
859 job
->current_file
== 0)
861 else if (job
->job_sheets
->num_values
> 1 &&
862 _cups_strcasecmp(job
->job_sheets
->values
[1].string
.text
, "none") != 0 &&
863 job
->current_file
== (job
->num_files
- 1))
868 if ((options
= get_options(job
, banner_page
, copies
, sizeof(copies
), title
,
869 sizeof(title
))) == NULL
)
871 abort_message
= "Stopping job because the scheduler ran out of memory.";
877 * Build the command-line arguments for the filters. Each filter
878 * has 6 or 7 arguments:
886 * argv[6] = filename (optional; normally stdin)
888 * This allows legacy printer drivers that use the old System V
889 * printing interface to be used by CUPS.
891 * For remote jobs, we send all of the files in the argument list.
894 if (job
->printer
->remote
)
895 argv
= calloc(7 + job
->num_files
, sizeof(char *));
897 argv
= calloc(8, sizeof(char *));
901 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Unable to allocate argument array - %s",
904 abort_message
= "Stopping job because the scheduler ran out of memory.";
909 sprintf(jobid
, "%d", job
->id
);
911 argv
[0] = job
->printer
->name
;
913 argv
[2] = job
->username
;
918 if (job
->printer
->remote
&& job
->num_files
> 1)
920 for (i
= 0; i
< job
->num_files
; i
++)
922 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
924 argv
[6 + i
] = strdup(filename
);
929 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
930 job
->id
, job
->current_file
+ 1);
934 for (i
= 0; argv
[i
]; i
++)
935 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "argv[%d]=\"%s\"", i
, argv
[i
]);
938 * Create environment variable strings for the filters...
941 attr
= ippFindAttribute(job
->attrs
, "attributes-natural-language",
945 strlcpy(apple_language
, "APPLE_LANGUAGE=", sizeof(apple_language
));
946 _cupsAppleLanguage(attr
->values
[0].string
.text
,
947 apple_language
+ 15, sizeof(apple_language
) - 15);
948 #endif /* __APPLE__ */
950 switch (strlen(attr
->values
[0].string
.text
))
954 * This is an unknown or badly formatted language code; use
955 * the POSIX locale...
958 strlcpy(lang
, "LANG=C", sizeof(lang
));
963 * Just the language code (ll)...
966 snprintf(lang
, sizeof(lang
), "LANG=%s.UTF-8",
967 attr
->values
[0].string
.text
);
972 * Language and country code (ll-cc)...
975 snprintf(lang
, sizeof(lang
), "LANG=%c%c_%c%c.UTF-8",
976 attr
->values
[0].string
.text
[0],
977 attr
->values
[0].string
.text
[1],
978 toupper(attr
->values
[0].string
.text
[3] & 255),
979 toupper(attr
->values
[0].string
.text
[4] & 255));
983 if ((attr
= ippFindAttribute(job
->attrs
, "document-format",
984 IPP_TAG_MIMETYPE
)) != NULL
&&
985 (ptr
= strstr(attr
->values
[0].string
.text
, "charset=")) != NULL
)
986 snprintf(charset
, sizeof(charset
), "CHARSET=%s", ptr
+ 8);
988 strlcpy(charset
, "CHARSET=utf-8", sizeof(charset
));
990 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s/%s",
991 job
->filetypes
[job
->current_file
]->super
,
992 job
->filetypes
[job
->current_file
]->type
);
993 snprintf(device_uri
, sizeof(device_uri
), "DEVICE_URI=%s",
994 job
->printer
->device_uri
);
995 snprintf(ppd
, sizeof(ppd
), "PPD=%s/ppd/%s.ppd", ServerRoot
,
997 snprintf(printer_info
, sizeof(printer_name
), "PRINTER_INFO=%s",
998 job
->printer
->info
? job
->printer
->info
: "");
999 snprintf(printer_location
, sizeof(printer_name
), "PRINTER_LOCATION=%s",
1000 job
->printer
->location
? job
->printer
->location
: "");
1001 snprintf(printer_name
, sizeof(printer_name
), "PRINTER=%s", job
->printer
->name
);
1002 if (job
->printer
->num_reasons
> 0)
1004 char *psrptr
; /* Pointer into PRINTER_STATE_REASONS */
1005 size_t psrlen
; /* Size of PRINTER_STATE_REASONS */
1007 for (psrlen
= 22, i
= 0; i
< job
->printer
->num_reasons
; i
++)
1008 psrlen
+= strlen(job
->printer
->reasons
[i
]) + 1;
1010 if ((printer_state_reasons
= malloc(psrlen
)) != NULL
)
1013 * All of these strcpy's are safe because we allocated the psr string...
1016 strlcpy(printer_state_reasons
, "PRINTER_STATE_REASONS=", psrlen
);
1017 for (psrptr
= printer_state_reasons
+ 22, i
= 0;
1018 i
< job
->printer
->num_reasons
;
1023 strlcpy(psrptr
, job
->printer
->reasons
[i
],
1024 psrlen
- (psrptr
- printer_state_reasons
));
1025 psrptr
+= strlen(psrptr
);
1029 snprintf(rip_max_cache
, sizeof(rip_max_cache
), "RIP_MAX_CACHE=%s", RIPCache
);
1031 if (job
->printer
->num_auth_info_required
== 1)
1032 snprintf(auth_info_required
, sizeof(auth_info_required
),
1033 "AUTH_INFO_REQUIRED=%s",
1034 job
->printer
->auth_info_required
[0]);
1035 else if (job
->printer
->num_auth_info_required
== 2)
1036 snprintf(auth_info_required
, sizeof(auth_info_required
),
1037 "AUTH_INFO_REQUIRED=%s,%s",
1038 job
->printer
->auth_info_required
[0],
1039 job
->printer
->auth_info_required
[1]);
1040 else if (job
->printer
->num_auth_info_required
== 3)
1041 snprintf(auth_info_required
, sizeof(auth_info_required
),
1042 "AUTH_INFO_REQUIRED=%s,%s,%s",
1043 job
->printer
->auth_info_required
[0],
1044 job
->printer
->auth_info_required
[1],
1045 job
->printer
->auth_info_required
[2]);
1046 else if (job
->printer
->num_auth_info_required
== 4)
1047 snprintf(auth_info_required
, sizeof(auth_info_required
),
1048 "AUTH_INFO_REQUIRED=%s,%s,%s,%s",
1049 job
->printer
->auth_info_required
[0],
1050 job
->printer
->auth_info_required
[1],
1051 job
->printer
->auth_info_required
[2],
1052 job
->printer
->auth_info_required
[3]);
1054 strlcpy(auth_info_required
, "AUTH_INFO_REQUIRED=none",
1055 sizeof(auth_info_required
));
1057 envc
= cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
1059 envp
[envc
++] = charset
;
1060 envp
[envc
++] = lang
;
1062 envp
[envc
++] = apple_language
;
1063 #endif /* __APPLE__ */
1064 envp
[envc
++] = ppd
;
1065 envp
[envc
++] = rip_max_cache
;
1066 envp
[envc
++] = content_type
;
1067 envp
[envc
++] = device_uri
;
1068 envp
[envc
++] = printer_info
;
1069 envp
[envc
++] = printer_location
;
1070 envp
[envc
++] = printer_name
;
1071 envp
[envc
++] = printer_state_reasons
? printer_state_reasons
:
1072 "PRINTER_STATE_REASONS=none";
1073 envp
[envc
++] = banner_page
? "CUPS_FILETYPE=job-sheet" :
1074 "CUPS_FILETYPE=document";
1076 if (final_content_type
[0])
1077 envp
[envc
++] = final_content_type
;
1079 if (Classification
&& !banner_page
)
1081 if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
1082 IPP_TAG_NAME
)) == NULL
)
1083 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
1085 else if (attr
->num_values
> 1 &&
1086 strcmp(attr
->values
[1].string
.text
, "none") != 0)
1087 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
1088 attr
->values
[1].string
.text
);
1090 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
1091 attr
->values
[0].string
.text
);
1093 envp
[envc
++] = classification
;
1096 if (job
->dtype
& CUPS_PRINTER_CLASS
)
1098 snprintf(class_name
, sizeof(class_name
), "CLASS=%s", job
->dest
);
1099 envp
[envc
++] = class_name
;
1102 envp
[envc
++] = auth_info_required
;
1105 i
< (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0]));
1107 if (job
->auth_env
[i
])
1108 envp
[envc
++] = job
->auth_env
[i
];
1113 envp
[envc
++] = job
->auth_uid
;
1117 for (i
= 0; i
< envc
; i
++)
1118 if (!strncmp(envp
[i
], "AUTH_", 5))
1119 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"AUTH_%c****\"", i
,
1121 else if (strncmp(envp
[i
], "DEVICE_URI=", 11))
1122 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"%s\"", i
, envp
[i
]);
1124 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"DEVICE_URI=%s\"", i
,
1125 job
->printer
->sanitized_device_uri
);
1127 if (job
->printer
->remote
)
1128 job
->current_file
= job
->num_files
;
1130 job
->current_file
++;
1133 * Now create processes for all of the filters...
1136 for (i
= 0, slot
= 0, filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
1138 i
++, filter
= (mime_filter_t
*)cupsArrayNext(filters
))
1140 if (filter
->filter
[0] != '/')
1141 snprintf(command
, sizeof(command
), "%s/filter/%s", ServerBin
,
1144 strlcpy(command
, filter
->filter
, sizeof(command
));
1146 if (i
< (cupsArrayCount(filters
) - 1))
1148 if (cupsdOpenPipe(filterfds
[slot
]))
1150 abort_message
= "Stopping job because the scheduler could not create "
1151 "the filter pipes.";
1158 if (job
->current_file
== 1 ||
1159 (job
->printer
->pc
&& job
->printer
->pc
->single_file
))
1161 if (strncmp(job
->printer
->device_uri
, "file:", 5) != 0)
1163 if (cupsdOpenPipe(job
->print_pipes
))
1165 abort_message
= "Stopping job because the scheduler could not "
1166 "create the backend pipes.";
1173 job
->print_pipes
[0] = -1;
1174 if (!strcmp(job
->printer
->device_uri
, "file:/dev/null") ||
1175 !strcmp(job
->printer
->device_uri
, "file:///dev/null"))
1176 job
->print_pipes
[1] = -1;
1179 if (!strncmp(job
->printer
->device_uri
, "file:/dev/", 10))
1180 job
->print_pipes
[1] = open(job
->printer
->device_uri
+ 5,
1182 else if (!strncmp(job
->printer
->device_uri
, "file:///dev/", 12))
1183 job
->print_pipes
[1] = open(job
->printer
->device_uri
+ 7,
1185 else if (!strncmp(job
->printer
->device_uri
, "file:///", 8))
1186 job
->print_pipes
[1] = open(job
->printer
->device_uri
+ 7,
1187 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
1189 job
->print_pipes
[1] = open(job
->printer
->device_uri
+ 5,
1190 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
1192 if (job
->print_pipes
[1] < 0)
1194 abort_message
= "Stopping job because the scheduler could not "
1195 "open the output file.";
1200 fcntl(job
->print_pipes
[1], F_SETFD
,
1201 fcntl(job
->print_pipes
[1], F_GETFD
) | FD_CLOEXEC
);
1206 filterfds
[slot
][0] = job
->print_pipes
[0];
1207 filterfds
[slot
][1] = job
->print_pipes
[1];
1210 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
1211 filterfds
[slot
][1], job
->status_pipes
[1],
1212 job
->back_pipes
[0], job
->side_pipes
[0], 0,
1213 job
->profile
, job
, job
->filters
+ i
);
1215 cupsdClosePipe(filterfds
[!slot
]);
1219 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Unable to start filter \"%s\" - %s.",
1220 filter
->filter
, strerror(errno
));
1222 abort_message
= "Stopping job because the scheduler could not execute a "
1228 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Started filter %s (PID %d)", command
,
1235 cupsArrayDelete(filters
);
1239 * Finally, pipe the final output into a backend process if needed...
1242 if (strncmp(job
->printer
->device_uri
, "file:", 5) != 0)
1244 if (job
->current_file
== 1 || job
->printer
->remote
||
1245 (job
->printer
->pc
&& job
->printer
->pc
->single_file
))
1247 sscanf(job
->printer
->device_uri
, "%254[^:]", scheme
);
1248 snprintf(command
, sizeof(command
), "%s/backend/%s", ServerBin
, scheme
);
1251 * See if the backend needs to run as root...
1256 else if (stat(command
, &backinfo
))
1259 backroot
= !(backinfo
.st_mode
& (S_IRWXG
| S_IRWXO
));
1261 argv
[0] = job
->printer
->sanitized_device_uri
;
1263 filterfds
[slot
][0] = -1;
1264 filterfds
[slot
][1] = -1;
1266 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
1267 filterfds
[slot
][1], job
->status_pipes
[1],
1268 job
->back_pipes
[1], job
->side_pipes
[1],
1269 backroot
, job
->profile
, job
, &(job
->backend
));
1273 abort_message
= "Stopping job because the sheduler could not execute "
1280 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Started backend %s (PID %d)",
1285 if (job
->current_file
== job
->num_files
||
1286 (job
->printer
->pc
&& job
->printer
->pc
->single_file
))
1287 cupsdClosePipe(job
->print_pipes
);
1289 if (job
->current_file
== job
->num_files
)
1291 cupsdClosePipe(job
->back_pipes
);
1292 cupsdClosePipe(job
->side_pipes
);
1294 close(job
->status_pipes
[1]);
1295 job
->status_pipes
[1] = -1;
1300 filterfds
[slot
][0] = -1;
1301 filterfds
[slot
][1] = -1;
1303 if (job
->current_file
== job
->num_files
||
1304 (job
->printer
->pc
&& job
->printer
->pc
->single_file
))
1305 cupsdClosePipe(job
->print_pipes
);
1307 if (job
->current_file
== job
->num_files
)
1309 close(job
->status_pipes
[1]);
1310 job
->status_pipes
[1] = -1;
1314 cupsdClosePipe(filterfds
[slot
]);
1316 if (job
->printer
->remote
&& job
->num_files
> 1)
1318 for (i
= 0; i
< job
->num_files
; i
++)
1323 if (printer_state_reasons
)
1324 free(printer_state_reasons
);
1326 cupsdAddSelect(job
->status_buffer
->fd
, (cupsd_selfunc_t
)update_job
, NULL
,
1329 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
, "Job #%d started.",
1336 * If we get here, we need to abort the current job and close out all
1337 * files and pipes...
1342 FilterLevel
-= job
->cost
;
1345 for (slot
= 0; slot
< 2; slot
++)
1346 cupsdClosePipe(filterfds
[slot
]);
1348 cupsArrayDelete(filters
);
1352 if (job
->printer
->remote
&& job
->num_files
> 1)
1354 for (i
= 0; i
< job
->num_files
; i
++)
1361 if (printer_state_reasons
)
1362 free(printer_state_reasons
);
1364 cupsdClosePipe(job
->print_pipes
);
1365 cupsdClosePipe(job
->back_pipes
);
1366 cupsdClosePipe(job
->side_pipes
);
1368 cupsdRemoveSelect(job
->status_pipes
[0]);
1369 cupsdClosePipe(job
->status_pipes
);
1370 cupsdStatBufDelete(job
->status_buffer
);
1371 job
->status_buffer
= NULL
;
1374 * Update the printer and job state.
1377 cupsdSetJobState(job
, abort_state
, CUPSD_JOB_DEFAULT
, "%s", abort_message
);
1378 cupsdSetPrinterState(job
->printer
, IPP_PRINTER_IDLE
, 0);
1379 update_job_attrs(job
, 0);
1382 free_job_history(job
);
1384 cupsArrayRemove(PrintingJobs
, job
);
1387 * Clear the printer <-> job association...
1390 job
->printer
->job
= NULL
;
1391 job
->printer
= NULL
;
1396 * 'cupsdDeleteJob()' - Free all memory used by a job.
1400 cupsdDeleteJob(cupsd_job_t
*job
, /* I - Job */
1401 cupsd_jobaction_t action
)/* I - Action */
1403 int i
; /* Looping var */
1407 finalize_job(job
, 1);
1409 if (action
== CUPSD_JOB_PURGE
)
1410 remove_job_history(job
);
1412 cupsdClearString(&job
->username
);
1413 cupsdClearString(&job
->dest
);
1415 i
< (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0]));
1417 cupsdClearString(job
->auth_env
+ i
);
1418 cupsdClearString(&job
->auth_uid
);
1420 if (action
== CUPSD_JOB_PURGE
)
1421 remove_job_files(job
);
1422 else if (job
->num_files
> 0)
1424 free(job
->compressions
);
1425 free(job
->filetypes
);
1431 free_job_history(job
);
1435 cupsArrayRemove(Jobs
, job
);
1436 cupsArrayRemove(ActiveJobs
, job
);
1437 cupsArrayRemove(PrintingJobs
, job
);
1444 * 'cupsdFreeAllJobs()' - Free all jobs from memory.
1448 cupsdFreeAllJobs(void)
1450 cupsd_job_t
*job
; /* Current job */
1458 cupsdStopAllJobs(CUPSD_JOB_FORCE
, 0);
1461 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1463 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1464 cupsdDeleteJob(job
, CUPSD_JOB_DEFAULT
);
1466 cupsdReleaseSignals();
1471 * 'cupsdFindJob()' - Find the specified job.
1474 cupsd_job_t
* /* O - Job data */
1475 cupsdFindJob(int id
) /* I - Job ID */
1477 cupsd_job_t key
; /* Search key */
1482 return ((cupsd_job_t
*)cupsArrayFind(Jobs
, &key
));
1487 * 'cupsdGetPrinterJobCount()' - Get the number of pending, processing,
1488 * or held jobs in a printer or class.
1491 int /* O - Job count */
1492 cupsdGetPrinterJobCount(
1493 const char *dest
) /* I - Printer or class name */
1495 int count
; /* Job count */
1496 cupsd_job_t
*job
; /* Current job */
1499 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
1501 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
1502 if (job
->dest
&& !_cups_strcasecmp(job
->dest
, dest
))
1510 * 'cupsdGetUserJobCount()' - Get the number of pending, processing,
1511 * or held jobs for a user.
1514 int /* O - Job count */
1515 cupsdGetUserJobCount(
1516 const char *username
) /* I - Username */
1518 int count
; /* Job count */
1519 cupsd_job_t
*job
; /* Current job */
1522 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
1524 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
1525 if (!_cups_strcasecmp(job
->username
, username
))
1533 * 'cupsdLoadAllJobs()' - Load all jobs from disk.
1537 cupsdLoadAllJobs(void)
1539 char filename
[1024]; /* Full filename of job.cache file */
1540 struct stat fileinfo
, /* Information on job.cache file */
1541 dirinfo
; /* Information on RequestRoot dir */
1546 * Create the job arrays as needed...
1550 Jobs
= cupsArrayNew(compare_jobs
, NULL
);
1553 ActiveJobs
= cupsArrayNew(compare_active_jobs
, NULL
);
1556 PrintingJobs
= cupsArrayNew(compare_jobs
, NULL
);
1559 * See whether the job.cache file is older than the RequestRoot directory...
1562 snprintf(filename
, sizeof(filename
), "%s/job.cache", CacheDir
);
1564 if (stat(filename
, &fileinfo
))
1566 fileinfo
.st_mtime
= 0;
1568 if (errno
!= ENOENT
)
1569 cupsdLogMessage(CUPSD_LOG_ERROR
,
1570 "Unable to get file information for \"%s\" - %s",
1571 filename
, strerror(errno
));
1574 if (stat(RequestRoot
, &dirinfo
))
1576 dirinfo
.st_mtime
= 0;
1578 if (errno
!= ENOENT
)
1579 cupsdLogMessage(CUPSD_LOG_ERROR
,
1580 "Unable to get directory information for \"%s\" - %s",
1581 RequestRoot
, strerror(errno
));
1585 * Load the most recent source for job data...
1588 if (dirinfo
.st_mtime
> fileinfo
.st_mtime
)
1590 load_request_root();
1592 load_next_job_id(filename
);
1595 load_job_cache(filename
);
1598 * Clean out old jobs as needed...
1601 if (MaxJobs
> 0 && cupsArrayCount(Jobs
) >= MaxJobs
)
1607 * 'cupsdLoadJob()' - Load a single job.
1610 int /* O - 1 on success, 0 on failure */
1611 cupsdLoadJob(cupsd_job_t
*job
) /* I - Job */
1613 int i
; /* Looping var */
1614 char jobfile
[1024]; /* Job filename */
1615 cups_file_t
*fp
; /* Job file */
1616 int fileid
; /* Current file ID */
1617 ipp_attribute_t
*attr
; /* Job attribute */
1618 const char *dest
; /* Destination name */
1619 cupsd_printer_t
*destptr
; /* Pointer to destination */
1620 mime_type_t
**filetypes
; /* New filetypes array */
1621 int *compressions
; /* New compressions array */
1626 if (job
->state_value
> IPP_JOB_STOPPED
)
1627 job
->access_time
= time(NULL
);
1632 if ((job
->attrs
= ippNew()) == NULL
)
1634 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Ran out of memory for job attributes.");
1639 * Load job attributes...
1642 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Loading attributes...");
1644 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, job
->id
);
1645 if ((fp
= cupsdOpenConfFile(jobfile
)) == NULL
)
1648 if (ippReadIO(fp
, (ipp_iocb_t
)cupsFileRead
, 1, NULL
, job
->attrs
) != IPP_DATA
)
1650 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1651 "Unable to read job control file \"%s\".", jobfile
);
1659 * Copy attribute data to the job object...
1662 if (!ippFindAttribute(job
->attrs
, "time-at-creation", IPP_TAG_INTEGER
))
1664 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1665 "Missing or bad time-at-creation attribute in control file.");
1669 if ((job
->state
= ippFindAttribute(job
->attrs
, "job-state",
1670 IPP_TAG_ENUM
)) == NULL
)
1672 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1673 "Missing or bad job-state attribute in control file.");
1677 job
->state_value
= (ipp_jstate_t
)job
->state
->values
[0].integer
;
1679 job
->history_time
= 0;
1681 if (job
->state_value
>= IPP_JOB_CANCELED
&&
1682 (attr
= ippFindAttribute(job
->attrs
, "time-at-completed",
1683 IPP_TAG_INTEGER
)) != NULL
)
1685 if (JobHistory
< INT_MAX
)
1686 job
->history_time
= attr
->values
[0].integer
+ JobHistory
;
1688 job
->history_time
= INT_MAX
;
1690 if (job
->history_time
< time(NULL
))
1691 goto error
; /* Expired, remove from history */
1693 if (job
->history_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
1694 JobHistoryUpdate
= job
->history_time
;
1696 if (JobFiles
< INT_MAX
)
1697 job
->file_time
= attr
->values
[0].integer
+ JobFiles
;
1699 job
->file_time
= INT_MAX
;
1701 if (job
->file_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
1702 JobHistoryUpdate
= job
->file_time
;
1704 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdLoadJob: JobHistoryUpdate=%ld",
1705 (long)JobHistoryUpdate
);
1710 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1711 IPP_TAG_URI
)) == NULL
)
1713 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1714 "No job-printer-uri attribute in control file.");
1718 if ((dest
= cupsdValidateDest(attr
->values
[0].string
.text
, &(job
->dtype
),
1721 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1722 "Unable to queue job for destination \"%s\".",
1723 attr
->values
[0].string
.text
);
1727 cupsdSetString(&job
->dest
, dest
);
1729 else if ((destptr
= cupsdFindDest(job
->dest
)) == NULL
)
1731 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1732 "Unable to queue job for destination \"%s\".",
1737 if ((job
->reasons
= ippFindAttribute(job
->attrs
, "job-state-reasons",
1738 IPP_TAG_KEYWORD
)) == NULL
)
1740 const char *reason
; /* job-state-reason keyword */
1742 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
1743 "Adding missing job-state-reasons attribute to control file.");
1745 switch (job
->state_value
)
1748 case IPP_JOB_PENDING
:
1749 if (destptr
->state
== IPP_PRINTER_STOPPED
)
1750 reason
= "printer-stopped";
1756 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1757 IPP_TAG_ZERO
)) != NULL
&&
1758 (attr
->value_tag
== IPP_TAG_NAME
||
1759 attr
->value_tag
== IPP_TAG_NAMELANG
||
1760 attr
->value_tag
== IPP_TAG_KEYWORD
) &&
1761 strcmp(attr
->values
[0].string
.text
, "no-hold"))
1762 reason
= "job-hold-until-specified";
1764 reason
= "job-incoming";
1767 case IPP_JOB_PROCESSING
:
1768 reason
= "job-printing";
1771 case IPP_JOB_STOPPED
:
1772 reason
= "job-stopped";
1775 case IPP_JOB_CANCELED
:
1776 reason
= "job-canceled-by-user";
1779 case IPP_JOB_ABORTED
:
1780 reason
= "aborted-by-system";
1783 case IPP_JOB_COMPLETED
:
1784 reason
= "job-completed-successfully";
1788 job
->reasons
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1789 "job-state-reasons", NULL
, reason
);
1791 else if (job
->state_value
== IPP_JOB_PENDING
)
1793 if (destptr
->state
== IPP_PRINTER_STOPPED
)
1794 ippSetString(job
->attrs
, &job
->reasons
, 0, "printer-stopped");
1796 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
1799 job
->sheets
= ippFindAttribute(job
->attrs
, "job-media-sheets-completed",
1801 job
->job_sheets
= ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_NAME
);
1805 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
1806 IPP_TAG_INTEGER
)) == NULL
)
1808 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1809 "Missing or bad job-priority attribute in control file.");
1813 job
->priority
= attr
->values
[0].integer
;
1818 if ((attr
= ippFindAttribute(job
->attrs
, "job-originating-user-name",
1819 IPP_TAG_NAME
)) == NULL
)
1821 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1822 "Missing or bad job-originating-user-name "
1823 "attribute in control file.");
1827 cupsdSetString(&job
->username
, attr
->values
[0].string
.text
);
1831 * Set the job hold-until time and state...
1834 if (job
->state_value
== IPP_JOB_HELD
)
1836 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1837 IPP_TAG_KEYWORD
)) == NULL
)
1838 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1841 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
, CUPSD_JOB_DEFAULT
);
1844 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1845 job
->state_value
= IPP_JOB_PENDING
;
1848 else if (job
->state_value
== IPP_JOB_PROCESSING
)
1850 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1851 job
->state_value
= IPP_JOB_PENDING
;
1854 if (!job
->num_files
)
1857 * Find all the d##### files...
1860 for (fileid
= 1; fileid
< 10000; fileid
++)
1862 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
1865 if (access(jobfile
, 0))
1868 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
1869 "Auto-typing document file \"%s\"...", jobfile
);
1871 if (fileid
> job
->num_files
)
1873 if (job
->num_files
== 0)
1875 compressions
= (int *)calloc(fileid
, sizeof(int));
1876 filetypes
= (mime_type_t
**)calloc(fileid
, sizeof(mime_type_t
*));
1880 compressions
= (int *)realloc(job
->compressions
,
1881 sizeof(int) * fileid
);
1882 filetypes
= (mime_type_t
**)realloc(job
->filetypes
,
1883 sizeof(mime_type_t
*) *
1888 job
->compressions
= compressions
;
1891 job
->filetypes
= filetypes
;
1893 if (!compressions
|| !filetypes
)
1895 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1896 "Ran out of memory for job file types.");
1898 ippDelete(job
->attrs
);
1901 if (job
->compressions
)
1903 free(job
->compressions
);
1904 job
->compressions
= NULL
;
1909 free(job
->filetypes
);
1910 job
->filetypes
= NULL
;
1917 job
->num_files
= fileid
;
1920 job
->filetypes
[fileid
- 1] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
1921 job
->compressions
+ fileid
- 1);
1923 if (!job
->filetypes
[fileid
- 1])
1924 job
->filetypes
[fileid
- 1] = mimeType(MimeDatabase
, "application",
1930 * Load authentication information as needed...
1933 if (job
->state_value
< IPP_JOB_STOPPED
)
1935 snprintf(jobfile
, sizeof(jobfile
), "%s/a%05d", RequestRoot
, job
->id
);
1938 i
< (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0]));
1940 cupsdClearString(job
->auth_env
+ i
);
1941 cupsdClearString(&job
->auth_uid
);
1943 if ((fp
= cupsFileOpen(jobfile
, "r")) != NULL
)
1945 int bytes
, /* Size of auth data */
1946 linenum
= 1; /* Current line number */
1947 char line
[65536], /* Line from file */
1948 *value
, /* Value from line */
1949 data
[65536]; /* Decoded data */
1952 if (cupsFileGets(fp
, line
, sizeof(line
)) &&
1953 !strcmp(line
, "CUPSD-AUTH-V3"))
1956 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
1962 if (strcmp(line
, "negotiate") && strcmp(line
, "uid"))
1964 bytes
= sizeof(data
);
1965 httpDecode64_2(data
, &bytes
, value
);
1969 * Assign environment variables...
1972 if (!strcmp(line
, "uid"))
1974 cupsdSetStringf(&job
->auth_uid
, "AUTH_UID=%s", value
);
1977 else if (i
>= (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0])))
1980 if (!strcmp(line
, "username"))
1981 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_USERNAME=%s", data
);
1982 else if (!strcmp(line
, "domain"))
1983 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_DOMAIN=%s", data
);
1984 else if (!strcmp(line
, "password"))
1985 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_PASSWORD=%s", data
);
1986 else if (!strcmp(line
, "negotiate"))
1987 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_NEGOTIATE=%s", value
);
1999 job
->access_time
= time(NULL
);
2003 * If we get here then something bad happened...
2008 ippDelete(job
->attrs
);
2011 remove_job_history(job
);
2012 remove_job_files(job
);
2019 * 'cupsdMoveJob()' - Move the specified job to a different destination.
2023 cupsdMoveJob(cupsd_job_t
*job
, /* I - Job */
2024 cupsd_printer_t
*p
) /* I - Destination printer or class */
2026 ipp_attribute_t
*attr
; /* job-printer-uri attribute */
2027 const char *olddest
; /* Old destination */
2028 cupsd_printer_t
*oldp
; /* Old pointer */
2032 * Don't move completed jobs...
2035 if (job
->state_value
> IPP_JOB_STOPPED
)
2039 * Get the old destination...
2042 olddest
= job
->dest
;
2045 oldp
= job
->printer
;
2047 oldp
= cupsdFindDest(olddest
);
2050 * Change the destination information...
2053 if (job
->state_value
> IPP_JOB_HELD
)
2054 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
2055 "Stopping job prior to move.");
2057 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, oldp
, job
,
2058 "Job #%d moved from %s to %s.", job
->id
, olddest
,
2061 cupsdSetString(&job
->dest
, p
->name
);
2062 job
->dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_REMOTE
);
2064 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
2065 IPP_TAG_URI
)) != NULL
)
2066 cupsdSetString(&(attr
->values
[0].string
.text
), p
->uri
);
2068 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, p
, job
,
2069 "Job #%d moved from %s to %s.", job
->id
, olddest
,
2073 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2078 * 'cupsdReleaseJob()' - Release the specified job.
2082 cupsdReleaseJob(cupsd_job_t
*job
) /* I - Job */
2084 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdReleaseJob(job=%p(%d))", job
,
2087 if (job
->state_value
== IPP_JOB_HELD
)
2090 * Add trailing banner as needed...
2093 if (job
->pending_timeout
)
2094 cupsdTimeoutJob(job
);
2096 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
2097 "Job released by user.");
2103 * 'cupsdRestartJob()' - Restart the specified job.
2107 cupsdRestartJob(cupsd_job_t
*job
) /* I - Job */
2109 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdRestartJob(job=%p(%d))", job
,
2112 if (job
->state_value
== IPP_JOB_STOPPED
|| job
->num_files
)
2113 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
2114 "Job restarted by user.");
2119 * 'cupsdSaveAllJobs()' - Save a summary of all jobs to disk.
2123 cupsdSaveAllJobs(void)
2125 int i
; /* Looping var */
2126 cups_file_t
*fp
; /* job.cache file */
2127 char filename
[1024], /* job.cache filename */
2128 temp
[1024]; /* Temporary string */
2129 cupsd_job_t
*job
; /* Current job */
2130 time_t curtime
; /* Current time */
2131 struct tm
*curdate
; /* Current date */
2134 snprintf(filename
, sizeof(filename
), "%s/job.cache", CacheDir
);
2135 if ((fp
= cupsdCreateConfFile(filename
, ConfigFilePerm
)) == NULL
)
2138 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving job.cache...");
2141 * Write a small header to the file...
2144 curtime
= time(NULL
);
2145 curdate
= localtime(&curtime
);
2146 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
2148 cupsFilePuts(fp
, "# Job cache file for " CUPS_SVERSION
"\n");
2149 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
2150 cupsFilePrintf(fp
, "NextJobId %d\n", NextJobId
);
2153 * Write each job known to the system...
2156 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
2158 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
2160 cupsFilePrintf(fp
, "<Job %d>\n", job
->id
);
2161 cupsFilePrintf(fp
, "State %d\n", job
->state_value
);
2162 cupsFilePrintf(fp
, "Priority %d\n", job
->priority
);
2163 cupsFilePrintf(fp
, "HoldUntil %d\n", (int)job
->hold_until
);
2164 cupsFilePrintf(fp
, "Username %s\n", job
->username
);
2165 cupsFilePrintf(fp
, "Destination %s\n", job
->dest
);
2166 cupsFilePrintf(fp
, "DestType %d\n", job
->dtype
);
2167 cupsFilePrintf(fp
, "NumFiles %d\n", job
->num_files
);
2168 for (i
= 0; i
< job
->num_files
; i
++)
2169 cupsFilePrintf(fp
, "File %d %s/%s %d\n", i
+ 1, job
->filetypes
[i
]->super
,
2170 job
->filetypes
[i
]->type
, job
->compressions
[i
]);
2171 cupsFilePuts(fp
, "</Job>\n");
2174 cupsdCloseCreatedConfFile(fp
, filename
);
2179 * 'cupsdSaveJob()' - Save a job to disk.
2183 cupsdSaveJob(cupsd_job_t
*job
) /* I - Job */
2185 char filename
[1024]; /* Job control filename */
2186 cups_file_t
*fp
; /* Job file */
2189 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSaveJob(job=%p(%d)): job->attrs=%p",
2190 job
, job
->id
, job
->attrs
);
2192 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
, job
->id
);
2194 if ((fp
= cupsdCreateConfFile(filename
, ConfigFilePerm
& 0600)) == NULL
)
2197 fchown(cupsFileNumber(fp
), RunUser
, Group
);
2199 job
->attrs
->state
= IPP_IDLE
;
2201 if (ippWriteIO(fp
, (ipp_iocb_t
)cupsFileWrite
, 1, NULL
,
2202 job
->attrs
) != IPP_DATA
)
2204 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Unable to write job control file.");
2209 if (!cupsdCloseCreatedConfFile(fp
, filename
))
2212 * Remove backup file and mark this job as clean...
2215 strlcat(filename
, ".O", sizeof(filename
));
2224 * 'cupsdSetJobHoldUntil()' - Set the hold time for a job.
2228 cupsdSetJobHoldUntil(cupsd_job_t
*job
, /* I - Job */
2229 const char *when
, /* I - When to resume */
2230 int update
)/* I - Update job-hold-until attr? */
2232 time_t curtime
; /* Current time */
2233 struct tm
*curdate
; /* Current date */
2234 int hour
; /* Hold hour */
2235 int minute
; /* Hold minute */
2236 int second
= 0; /* Hold second */
2239 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2240 "cupsdSetJobHoldUntil(job=%p(%d), when=\"%s\", update=%d)",
2241 job
, job
->id
, when
, update
);
2246 * Update the job-hold-until attribute...
2249 ipp_attribute_t
*attr
; /* job-hold-until attribute */
2251 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2252 IPP_TAG_KEYWORD
)) == NULL
)
2253 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2256 cupsdSetString(&(attr
->values
[0].string
.text
), when
);
2258 attr
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
2259 "job-hold-until", NULL
, when
);
2263 if (isdigit(when
[0] & 255))
2264 attr
->value_tag
= IPP_TAG_NAME
;
2266 attr
->value_tag
= IPP_TAG_KEYWORD
;
2269 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2272 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-hold-until-specified");
2276 * Update the hold time...
2279 job
->cancel_time
= 0;
2281 if (!strcmp(when
, "indefinite") || !strcmp(when
, "auth-info-required"))
2284 * Hold indefinitely...
2287 job
->hold_until
= 0;
2289 if (MaxHoldTime
> 0)
2290 job
->cancel_time
= time(NULL
) + MaxHoldTime
;
2292 else if (!strcmp(when
, "day-time"))
2295 * Hold to 6am the next morning unless local time is < 6pm.
2298 curtime
= time(NULL
);
2299 curdate
= localtime(&curtime
);
2301 if (curdate
->tm_hour
< 18)
2302 job
->hold_until
= curtime
;
2304 job
->hold_until
= curtime
+
2305 ((29 - curdate
->tm_hour
) * 60 + 59 -
2306 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2308 else if (!strcmp(when
, "evening") || !strcmp(when
, "night"))
2311 * Hold to 6pm unless local time is > 6pm or < 6am.
2314 curtime
= time(NULL
);
2315 curdate
= localtime(&curtime
);
2317 if (curdate
->tm_hour
< 6 || curdate
->tm_hour
>= 18)
2318 job
->hold_until
= curtime
;
2320 job
->hold_until
= curtime
+
2321 ((17 - curdate
->tm_hour
) * 60 + 59 -
2322 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2324 else if (!strcmp(when
, "second-shift"))
2327 * Hold to 4pm unless local time is > 4pm.
2330 curtime
= time(NULL
);
2331 curdate
= localtime(&curtime
);
2333 if (curdate
->tm_hour
>= 16)
2334 job
->hold_until
= curtime
;
2336 job
->hold_until
= curtime
+
2337 ((15 - curdate
->tm_hour
) * 60 + 59 -
2338 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2340 else if (!strcmp(when
, "third-shift"))
2343 * Hold to 12am unless local time is < 8am.
2346 curtime
= time(NULL
);
2347 curdate
= localtime(&curtime
);
2349 if (curdate
->tm_hour
< 8)
2350 job
->hold_until
= curtime
;
2352 job
->hold_until
= curtime
+
2353 ((23 - curdate
->tm_hour
) * 60 + 59 -
2354 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2356 else if (!strcmp(when
, "weekend"))
2359 * Hold to weekend unless we are in the weekend.
2362 curtime
= time(NULL
);
2363 curdate
= localtime(&curtime
);
2365 if (curdate
->tm_wday
== 0 || curdate
->tm_wday
== 6)
2366 job
->hold_until
= curtime
;
2368 job
->hold_until
= curtime
+
2369 (((5 - curdate
->tm_wday
) * 24 +
2370 (17 - curdate
->tm_hour
)) * 60 + 59 -
2371 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2373 else if (sscanf(when
, "%d:%d:%d", &hour
, &minute
, &second
) >= 2)
2376 * Hold to specified GMT time (HH:MM or HH:MM:SS)...
2379 curtime
= time(NULL
);
2380 curdate
= gmtime(&curtime
);
2382 job
->hold_until
= curtime
+
2383 ((hour
- curdate
->tm_hour
) * 60 + minute
-
2384 curdate
->tm_min
) * 60 + second
- curdate
->tm_sec
;
2387 * Hold until next day as needed...
2390 if (job
->hold_until
< curtime
)
2391 job
->hold_until
+= 24 * 60 * 60;
2394 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetJobHoldUntil: hold_until=%d",
2395 (int)job
->hold_until
);
2400 * 'cupsdSetJobPriority()' - Set the priority of a job, moving it up/down in
2401 * the list as needed.
2405 cupsdSetJobPriority(
2406 cupsd_job_t
*job
, /* I - Job ID */
2407 int priority
) /* I - New priority (0 to 100) */
2409 ipp_attribute_t
*attr
; /* Job attribute */
2413 * Don't change completed jobs...
2416 if (job
->state_value
>= IPP_JOB_PROCESSING
)
2420 * Set the new priority and re-add the job into the active list...
2423 cupsArrayRemove(ActiveJobs
, job
);
2425 job
->priority
= priority
;
2427 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
2428 IPP_TAG_INTEGER
)) != NULL
)
2429 attr
->values
[0].integer
= priority
;
2431 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-priority",
2434 cupsArrayAdd(ActiveJobs
, job
);
2437 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2442 * 'cupsdSetJobState()' - Set the state of the specified print job.
2447 cupsd_job_t
*job
, /* I - Job to cancel */
2448 ipp_jstate_t newstate
, /* I - New job state */
2449 cupsd_jobaction_t action
, /* I - Action to take */
2450 const char *message
, /* I - Message to log */
2451 ...) /* I - Additional arguments as needed */
2453 int i
; /* Looping var */
2454 ipp_jstate_t oldstate
; /* Old state */
2455 char filename
[1024]; /* Job filename */
2456 ipp_attribute_t
*attr
; /* Job attribute */
2459 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2460 "cupsdSetJobState(job=%p(%d), state=%d, newstate=%d, "
2461 "action=%d, message=\"%s\")", job
, job
->id
, job
->state_value
,
2462 newstate
, action
, message
? message
: "(null)");
2466 * Make sure we have the job attributes...
2469 if (!cupsdLoadJob(job
))
2473 * Don't do anything if the state is unchanged and we aren't purging the
2477 oldstate
= job
->state_value
;
2478 if (newstate
== oldstate
&& action
!= CUPSD_JOB_PURGE
)
2482 * Stop any processes that are working on the current job...
2485 if (oldstate
== IPP_JOB_PROCESSING
)
2486 stop_job(job
, action
);
2489 * Set the new job state...
2492 job
->state_value
= newstate
;
2495 job
->state
->values
[0].integer
= newstate
;
2499 case IPP_JOB_PENDING
:
2501 * Update job-hold-until as needed...
2504 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2505 IPP_TAG_KEYWORD
)) == NULL
)
2506 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2510 attr
->value_tag
= IPP_TAG_KEYWORD
;
2511 cupsdSetString(&(attr
->values
[0].string
.text
), "no-hold");
2517 case IPP_JOB_ABORTED
:
2518 case IPP_JOB_CANCELED
:
2519 case IPP_JOB_COMPLETED
:
2520 set_time(job
, "time-at-completed");
2521 ippSetString(job
->attrs
, &job
->reasons
, 0, "processing-to-stop-point");
2526 * Log message as needed...
2531 char buffer
[2048]; /* Message buffer */
2532 va_list ap
; /* Pointer to additional arguments */
2534 va_start(ap
, message
);
2535 vsnprintf(buffer
, sizeof(buffer
), message
, ap
);
2538 if (newstate
> IPP_JOB_STOPPED
)
2539 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
, "%s", buffer
);
2541 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
, "%s", buffer
);
2543 if (newstate
== IPP_JOB_STOPPED
|| newstate
== IPP_JOB_ABORTED
)
2544 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "%s", buffer
);
2546 cupsdLogJob(job
, CUPSD_LOG_INFO
, "%s", buffer
);
2550 * Handle post-state-change actions...
2555 case IPP_JOB_PROCESSING
:
2557 * Add the job to the "printing" list...
2560 if (!cupsArrayFind(PrintingJobs
, job
))
2561 cupsArrayAdd(PrintingJobs
, job
);
2564 * Set the processing time...
2567 set_time(job
, "time-at-processing");
2569 case IPP_JOB_PENDING
:
2571 case IPP_JOB_STOPPED
:
2573 * Make sure the job is in the active list...
2576 if (!cupsArrayFind(ActiveJobs
, job
))
2577 cupsArrayAdd(ActiveJobs
, job
);
2580 * Save the job state to disk...
2584 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2587 case IPP_JOB_ABORTED
:
2588 case IPP_JOB_CANCELED
:
2589 case IPP_JOB_COMPLETED
:
2590 if (newstate
== IPP_JOB_CANCELED
)
2593 * Remove the job from the active list if there are no processes still
2597 for (i
= 0; job
->filters
[i
] < 0; i
++);
2599 if (!job
->filters
[i
] && job
->backend
<= 0)
2600 cupsArrayRemove(ActiveJobs
, job
);
2605 * Otherwise just remove the job from the active list immediately...
2608 cupsArrayRemove(ActiveJobs
, job
);
2612 * Expire job subscriptions since the job is now "completed"...
2615 cupsdExpireSubscriptions(NULL
, job
);
2619 * If we are going to sleep and the PrintingJobs count is now 0, allow the
2620 * sleep to happen immediately...
2623 if (Sleeping
&& cupsArrayCount(PrintingJobs
) == 0)
2625 #endif /* __APPLE__ */
2628 * Remove any authentication data...
2631 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
2632 if (cupsdRemoveFile(filename
) && errno
!= ENOENT
)
2633 cupsdLogMessage(CUPSD_LOG_ERROR
,
2634 "Unable to remove authentication cache: %s",
2638 i
< (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0]));
2640 cupsdClearString(job
->auth_env
+ i
);
2642 cupsdClearString(&job
->auth_uid
);
2645 * Remove the print file for good if we aren't preserving jobs or
2649 if (!JobHistory
|| !JobFiles
|| action
== CUPSD_JOB_PURGE
)
2650 remove_job_files(job
);
2652 if (JobHistory
&& action
!= CUPSD_JOB_PURGE
)
2655 * Save job state info...
2659 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2661 else if (!job
->printer
)
2664 * Delete the job immediately if not actively printing...
2667 cupsdDeleteJob(job
, CUPSD_JOB_PURGE
);
2674 * Finalize the job immediately if we forced things...
2677 if (action
>= CUPSD_JOB_FORCE
&& job
&& job
->printer
)
2678 finalize_job(job
, 0);
2681 * Update the server "busy" state...
2684 cupsdSetBusyState();
2689 * 'cupsdStopAllJobs()' - Stop all print jobs.
2694 cupsd_jobaction_t action
, /* I - Action */
2695 int kill_delay
) /* I - Number of seconds before we kill */
2697 cupsd_job_t
*job
; /* Current job */
2700 DEBUG_puts("cupsdStopAllJobs()");
2702 for (job
= (cupsd_job_t
*)cupsArrayFirst(PrintingJobs
);
2704 job
= (cupsd_job_t
*)cupsArrayNext(PrintingJobs
))
2707 job
->kill_time
= time(NULL
) + kill_delay
;
2709 cupsdSetJobState(job
, IPP_JOB_PENDING
, action
, NULL
);
2715 * 'cupsdUnloadCompletedJobs()' - Flush completed job history from memory.
2719 cupsdUnloadCompletedJobs(void)
2721 cupsd_job_t
*job
; /* Current job */
2722 time_t expire
; /* Expiration time */
2725 expire
= time(NULL
) - 60;
2727 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
2729 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
2730 if (job
->attrs
&& job
->state_value
>= IPP_JOB_STOPPED
&& !job
->printer
&&
2731 job
->access_time
< expire
)
2742 * 'cupsdUpdateJobs()' - Update the history/file files for all jobs.
2746 cupsdUpdateJobs(void)
2748 cupsd_job_t
*job
; /* Current job */
2749 time_t curtime
; /* Current time */
2750 ipp_attribute_t
*attr
; /* time-at-completed attribute */
2753 curtime
= time(NULL
);
2754 JobHistoryUpdate
= 0;
2756 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
2758 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
2760 if (job
->state_value
>= IPP_JOB_CANCELED
&&
2761 (attr
= ippFindAttribute(job
->attrs
, "time-at-completed",
2762 IPP_TAG_INTEGER
)) != NULL
)
2765 * Update history/file expiration times...
2768 if (JobHistory
< INT_MAX
)
2769 job
->history_time
= attr
->values
[0].integer
+ JobHistory
;
2771 job
->history_time
= INT_MAX
;
2773 if (job
->history_time
< curtime
)
2775 cupsdDeleteJob(job
, CUPSD_JOB_PURGE
);
2779 if (job
->history_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
2780 JobHistoryUpdate
= job
->history_time
;
2782 if (JobFiles
< INT_MAX
)
2783 job
->file_time
= attr
->values
[0].integer
+ JobFiles
;
2785 job
->file_time
= INT_MAX
;
2787 if (job
->file_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
2788 JobHistoryUpdate
= job
->file_time
;
2792 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdUpdateAllJobs: JobHistoryUpdate=%ld",
2793 (long)JobHistoryUpdate
);
2798 * 'compare_active_jobs()' - Compare the job IDs and priorities of two jobs.
2801 static int /* O - Difference */
2802 compare_active_jobs(void *first
, /* I - First job */
2803 void *second
, /* I - Second job */
2804 void *data
) /* I - App data (not used) */
2806 int diff
; /* Difference */
2811 if ((diff
= ((cupsd_job_t
*)second
)->priority
-
2812 ((cupsd_job_t
*)first
)->priority
) != 0)
2815 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
2820 * 'compare_jobs()' - Compare the job IDs of two jobs.
2823 static int /* O - Difference */
2824 compare_jobs(void *first
, /* I - First job */
2825 void *second
, /* I - Second job */
2826 void *data
) /* I - App data (not used) */
2830 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
2835 * 'dump_job_history()' - Dump any debug messages for a job.
2839 dump_job_history(cupsd_job_t
*job
) /* I - Job */
2841 int i
, /* Looping var */
2842 oldsize
; /* Current MaxLogSize */
2843 struct tm
*date
; /* Date/time value */
2844 cupsd_joblog_t
*message
; /* Current message */
2845 char temp
[2048], /* Log message */
2846 *ptr
, /* Pointer into log message */
2847 start
[256], /* Start time */
2848 end
[256]; /* End time */
2849 cupsd_printer_t
*printer
; /* Printer for job */
2853 * See if we have anything to dump...
2860 * Disable log rotation temporarily...
2863 oldsize
= MaxLogSize
;
2867 * Copy the debug messages to the log...
2870 message
= (cupsd_joblog_t
*)cupsArrayFirst(job
->history
);
2871 date
= localtime(&(message
->time
));
2872 strftime(start
, sizeof(start
), "%X", date
);
2874 message
= (cupsd_joblog_t
*)cupsArrayLast(job
->history
);
2875 date
= localtime(&(message
->time
));
2876 strftime(end
, sizeof(end
), "%X", date
);
2878 snprintf(temp
, sizeof(temp
),
2879 "[Job %d] The following messages were recorded from %s to %s",
2880 job
->id
, start
, end
);
2881 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, temp
);
2883 for (message
= (cupsd_joblog_t
*)cupsArrayFirst(job
->history
);
2885 message
= (cupsd_joblog_t
*)cupsArrayNext(job
->history
))
2886 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, message
->message
);
2888 snprintf(temp
, sizeof(temp
), "[Job %d] End of messages", job
->id
);
2889 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, temp
);
2892 * Log the printer state values...
2895 if ((printer
= job
->printer
) == NULL
)
2896 printer
= cupsdFindDest(job
->dest
);
2900 snprintf(temp
, sizeof(temp
), "[Job %d] printer-state=%d(%s)", job
->id
,
2902 printer
->state
== IPP_PRINTER_IDLE
? "idle" :
2903 printer
->state
== IPP_PRINTER_PROCESSING
? "processing" :
2905 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, temp
);
2907 snprintf(temp
, sizeof(temp
), "[Job %d] printer-state-message=\"%s\"",
2908 job
->id
, printer
->state_message
);
2909 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, temp
);
2911 snprintf(temp
, sizeof(temp
), "[Job %d] printer-state-reasons=", job
->id
);
2912 ptr
= temp
+ strlen(temp
);
2913 if (printer
->num_reasons
== 0)
2914 strlcpy(ptr
, "none", sizeof(temp
) - (ptr
- temp
));
2918 i
< printer
->num_reasons
&& ptr
< (temp
+ sizeof(temp
) - 2);
2924 strlcpy(ptr
, printer
->reasons
[i
], sizeof(temp
) - (ptr
- temp
));
2928 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, temp
);
2932 * Restore log file rotation...
2935 MaxLogSize
= oldsize
;
2938 * Free all messages...
2941 free_job_history(job
);
2946 * 'free_job_history()' - Free any log history.
2950 free_job_history(cupsd_job_t
*job
) /* I - Job */
2952 char *message
; /* Current message */
2958 for (message
= (char *)cupsArrayFirst(job
->history
);
2960 message
= (char *)cupsArrayNext(job
->history
))
2963 cupsArrayDelete(job
->history
);
2964 job
->history
= NULL
;
2969 * 'finalize_job()' - Cleanup after job filter processes and support data.
2973 finalize_job(cupsd_job_t
*job
, /* I - Job */
2974 int set_job_state
) /* I - 1 = set the job state */
2976 ipp_pstate_t printer_state
; /* New printer state value */
2977 ipp_jstate_t job_state
; /* New job state value */
2978 const char *message
; /* Message for job state */
2979 char buffer
[1024]; /* Buffer for formatted messages */
2982 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "finalize_job(job=%p(%d))", job
, job
->id
);
2985 * Clear the "connecting-to-device" reason, which is only valid when a printer
2986 * is processing, along with any remote printing job state...
2989 cupsdSetPrinterReasons(job
->printer
, "-connecting-to-device,"
2990 "cups-remote-pending,"
2991 "cups-remote-pending-held,"
2992 "cups-remote-processing,"
2993 "cups-remote-stopped,"
2994 "cups-remote-canceled,"
2995 "cups-remote-aborted,"
2996 "cups-remote-completed");
2999 * Similarly, clear the "offline-report" reason for non-USB devices since we
3000 * rarely have current information for network devices...
3003 if (strncmp(job
->printer
->device_uri
, "usb:", 4) &&
3004 strncmp(job
->printer
->device_uri
, "ippusb:", 7))
3005 cupsdSetPrinterReasons(job
->printer
, "-offline-report");
3008 * Free the security profile...
3011 cupsdDestroyProfile(job
->profile
);
3012 job
->profile
= NULL
;
3015 * Clear the unresponsive job watchdog timers...
3018 job
->cancel_time
= 0;
3022 * Close pipes and status buffer...
3025 cupsdClosePipe(job
->print_pipes
);
3026 cupsdClosePipe(job
->back_pipes
);
3027 cupsdClosePipe(job
->side_pipes
);
3029 cupsdRemoveSelect(job
->status_pipes
[0]);
3030 cupsdClosePipe(job
->status_pipes
);
3031 cupsdStatBufDelete(job
->status_buffer
);
3032 job
->status_buffer
= NULL
;
3035 * Process the exit status...
3038 if (job
->printer
->state
== IPP_PRINTER_PROCESSING
)
3039 printer_state
= IPP_PRINTER_IDLE
;
3041 printer_state
= job
->printer
->state
;
3043 switch (job_state
= job
->state_value
)
3045 case IPP_JOB_PENDING
:
3046 message
= "Job paused.";
3050 message
= "Job held.";
3054 case IPP_JOB_PROCESSING
:
3055 case IPP_JOB_COMPLETED
:
3056 job_state
= IPP_JOB_COMPLETED
;
3057 message
= "Job completed.";
3060 ippSetString(job
->attrs
, &job
->reasons
, 0,
3061 "job-completed-successfully");
3064 case IPP_JOB_STOPPED
:
3065 message
= "Job stopped.";
3067 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-stopped");
3070 case IPP_JOB_CANCELED
:
3071 message
= "Job canceled.";
3073 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-canceled-by-user");
3076 case IPP_JOB_ABORTED
:
3077 message
= "Job aborted.";
3081 if (job
->status
< 0)
3084 * Backend had errors...
3087 int exit_code
; /* Exit code from backend */
3090 * Convert the status to an exit code. Due to the way the W* macros are
3091 * implemented on MacOS X (bug?), we have to store the exit status in a
3092 * variable first and then convert...
3095 exit_code
= -job
->status
;
3096 if (WIFEXITED(exit_code
))
3097 exit_code
= WEXITSTATUS(exit_code
);
3100 ippSetString(job
->attrs
, &job
->reasons
, 0, "cups-backend-crashed");
3101 exit_code
= job
->status
;
3104 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Backend returned status %d (%s)",
3106 exit_code
== CUPS_BACKEND_FAILED
? "failed" :
3107 exit_code
== CUPS_BACKEND_AUTH_REQUIRED
?
3108 "authentication required" :
3109 exit_code
== CUPS_BACKEND_HOLD
? "hold job" :
3110 exit_code
== CUPS_BACKEND_STOP
? "stop printer" :
3111 exit_code
== CUPS_BACKEND_CANCEL
? "cancel job" :
3112 exit_code
== CUPS_BACKEND_RETRY
? "retry job later" :
3113 exit_code
== CUPS_BACKEND_RETRY_CURRENT
? "retry job immediately" :
3114 exit_code
< 0 ? "crashed" : "unknown");
3117 * Do what needs to be done...
3123 case CUPS_BACKEND_FAILED
:
3125 * Backend failure, use the error-policy to determine how to
3129 if (job
->dtype
& CUPS_PRINTER_CLASS
)
3132 * Queued on a class - mark the job as pending and we'll retry on
3133 * another printer...
3136 if (job_state
== IPP_JOB_COMPLETED
)
3138 job_state
= IPP_JOB_PENDING
;
3139 message
= "Retrying job on another printer.";
3141 ippSetString(job
->attrs
, &job
->reasons
, 0,
3142 "resources-are-not-ready");
3145 else if (!strcmp(job
->printer
->error_policy
, "retry-current-job"))
3148 * The error policy is "retry-current-job" - mark the job as pending
3149 * and we'll retry on the same printer...
3152 if (job_state
== IPP_JOB_COMPLETED
)
3154 job_state
= IPP_JOB_PENDING
;
3155 message
= "Retrying job on same printer.";
3157 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
3160 else if ((job
->printer
->type
& CUPS_PRINTER_FAX
) ||
3161 !strcmp(job
->printer
->error_policy
, "retry-job"))
3163 if (job_state
== IPP_JOB_COMPLETED
)
3166 * The job was queued on a fax or the error policy is "retry-job" -
3167 * hold the job if the number of retries is less than the
3168 * JobRetryLimit, otherwise abort the job.
3173 if (job
->tries
> JobRetryLimit
&& JobRetryLimit
> 0)
3179 snprintf(buffer
, sizeof(buffer
),
3180 "Job aborted after %d unsuccessful attempts.",
3182 job_state
= IPP_JOB_ABORTED
;
3185 ippSetString(job
->attrs
, &job
->reasons
, 0, "aborted-by-system");
3190 * Try again in N seconds...
3193 snprintf(buffer
, sizeof(buffer
),
3194 "Job held for %d seconds since it could not be sent.",
3197 job
->hold_until
= time(NULL
) + JobRetryInterval
;
3198 job_state
= IPP_JOB_HELD
;
3201 ippSetString(job
->attrs
, &job
->reasons
, 0,
3202 "resources-are-not-ready");
3206 else if (!strcmp(job
->printer
->error_policy
, "abort-job") &&
3207 job_state
== IPP_JOB_COMPLETED
)
3209 job_state
= IPP_JOB_ABORTED
;
3210 message
= "Job aborted due to backend errors; please consult "
3211 "the error_log file for details.";
3213 ippSetString(job
->attrs
, &job
->reasons
, 0, "aborted-by-system");
3215 else if (job
->state_value
== IPP_JOB_PROCESSING
)
3217 job_state
= IPP_JOB_PENDING
;
3218 printer_state
= IPP_PRINTER_STOPPED
;
3219 message
= "Printer stopped due to backend errors; please "
3220 "consult the error_log file for details.";
3222 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
3226 case CUPS_BACKEND_CANCEL
:
3231 if (job_state
== IPP_JOB_COMPLETED
)
3233 job_state
= IPP_JOB_CANCELED
;
3234 message
= "Job canceled at printer.";
3236 ippSetString(job
->attrs
, &job
->reasons
, 0, "canceled-at-device");
3240 case CUPS_BACKEND_HOLD
:
3241 if (job_state
== IPP_JOB_COMPLETED
)
3247 const char *reason
= ippGetString(job
->reasons
, 0, NULL
);
3249 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "job-state-reasons=\"%s\"",
3252 if (!reason
|| strncmp(reason
, "account-", 8))
3254 cupsdSetJobHoldUntil(job
, "indefinite", 1);
3256 ippSetString(job
->attrs
, &job
->reasons
, 0,
3257 "job-hold-until-specified");
3258 message
= "Job held indefinitely due to backend errors; please "
3259 "consult the error_log file for details.";
3261 else if (!strcmp(reason
, "account-info-needed"))
3263 cupsdSetJobHoldUntil(job
, "indefinite", 0);
3265 message
= "Job held indefinitely - account information is "
3268 else if (!strcmp(reason
, "account-closed"))
3270 cupsdSetJobHoldUntil(job
, "indefinite", 0);
3272 message
= "Job held indefinitely - account has been closed.";
3274 else if (!strcmp(reason
, "account-limit-reached"))
3276 cupsdSetJobHoldUntil(job
, "indefinite", 0);
3278 message
= "Job held indefinitely - account limit has been "
3283 cupsdSetJobHoldUntil(job
, "indefinite", 0);
3285 message
= "Job held indefinitely - account authorization failed.";
3288 job_state
= IPP_JOB_HELD
;
3292 case CUPS_BACKEND_STOP
:
3294 * Stop the printer...
3297 printer_state
= IPP_PRINTER_STOPPED
;
3298 message
= "Printer stopped due to backend errors; please "
3299 "consult the error_log file for details.";
3301 if (job_state
== IPP_JOB_COMPLETED
)
3303 job_state
= IPP_JOB_PENDING
;
3305 ippSetString(job
->attrs
, &job
->reasons
, 0,
3306 "resources-are-not-ready");
3310 case CUPS_BACKEND_AUTH_REQUIRED
:
3312 * Hold the job for authentication...
3315 if (job_state
== IPP_JOB_COMPLETED
)
3317 cupsdSetJobHoldUntil(job
, "auth-info-required", 1);
3319 job_state
= IPP_JOB_HELD
;
3320 message
= "Job held for authentication.";
3322 if (strncmp(job
->reasons
->values
[0].string
.text
, "account-", 8))
3323 ippSetString(job
->attrs
, &job
->reasons
, 0,
3324 "cups-held-for-authentication");
3328 case CUPS_BACKEND_RETRY
:
3329 if (job_state
== IPP_JOB_COMPLETED
)
3332 * Hold the job if the number of retries is less than the
3333 * JobRetryLimit, otherwise abort the job.
3338 if (job
->tries
> JobRetryLimit
&& JobRetryLimit
> 0)
3344 snprintf(buffer
, sizeof(buffer
),
3345 "Job aborted after %d unsuccessful attempts.",
3347 job_state
= IPP_JOB_ABORTED
;
3350 ippSetString(job
->attrs
, &job
->reasons
, 0, "aborted-by-system");
3355 * Try again in N seconds...
3358 snprintf(buffer
, sizeof(buffer
),
3359 "Job held for %d seconds since it could not be sent.",
3362 job
->hold_until
= time(NULL
) + JobRetryInterval
;
3363 job_state
= IPP_JOB_HELD
;
3366 ippSetString(job
->attrs
, &job
->reasons
, 0,
3367 "resources-are-not-ready");
3372 case CUPS_BACKEND_RETRY_CURRENT
:
3374 * Mark the job as pending and retry on the same printer...
3377 if (job_state
== IPP_JOB_COMPLETED
)
3379 job_state
= IPP_JOB_PENDING
;
3380 message
= "Retrying job on same printer.";
3382 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
3387 else if (job
->status
> 0)
3390 * Filter had errors; stop job...
3393 if (job_state
== IPP_JOB_COMPLETED
)
3395 job_state
= IPP_JOB_STOPPED
;
3396 message
= "Job stopped due to filter errors; please consult the "
3397 "error_log file for details.";
3399 if (WIFSIGNALED(job
->status
))
3400 ippSetString(job
->attrs
, &job
->reasons
, 0, "cups-filter-crashed");
3402 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-completed-with-errors");
3407 * Update the printer and job state.
3410 if (set_job_state
&& job_state
!= job
->state_value
)
3411 cupsdSetJobState(job
, job_state
, CUPSD_JOB_DEFAULT
, "%s", message
);
3413 cupsdSetPrinterState(job
->printer
, printer_state
,
3414 printer_state
== IPP_PRINTER_STOPPED
);
3415 update_job_attrs(job
, 0);
3420 (job
->state_value
== IPP_JOB_ABORTED
||
3421 job
->state_value
== IPP_JOB_STOPPED
))
3422 dump_job_history(job
);
3424 free_job_history(job
);
3427 cupsArrayRemove(PrintingJobs
, job
);
3430 * Apply any PPD updates...
3433 if (job
->num_keywords
)
3435 if (cupsdUpdatePrinterPPD(job
->printer
, job
->num_keywords
, job
->keywords
))
3436 cupsdSetPrinterAttrs(job
->printer
);
3438 cupsFreeOptions(job
->num_keywords
, job
->keywords
);
3440 job
->num_keywords
= 0;
3441 job
->keywords
= NULL
;
3445 * Clear the printer <-> job association...
3448 job
->printer
->job
= NULL
;
3449 job
->printer
= NULL
;
3452 * Try printing another job...
3455 if (printer_state
!= IPP_PRINTER_STOPPED
)
3461 * 'get_options()' - Get a string containing the job options.
3464 static char * /* O - Options string */
3465 get_options(cupsd_job_t
*job
, /* I - Job */
3466 int banner_page
, /* I - Printing a banner page? */
3467 char *copies
, /* I - Copies buffer */
3468 size_t copies_size
, /* I - Size of copies buffer */
3469 char *title
, /* I - Title buffer */
3470 size_t title_size
) /* I - Size of title buffer */
3472 int i
; /* Looping var */
3473 size_t newlength
; /* New option buffer length */
3474 char *optptr
, /* Pointer to options */
3475 *valptr
; /* Pointer in value string */
3476 ipp_attribute_t
*attr
; /* Current attribute */
3477 _ppd_cache_t
*pc
; /* PPD cache and mapping data */
3478 int num_pwgppds
; /* Number of PWG->PPD options */
3479 cups_option_t
*pwgppds
, /* PWG->PPD options */
3480 *pwgppd
, /* Current PWG->PPD option */
3481 *preset
; /* Current preset option */
3482 int print_color_mode
,
3483 /* Output mode (if any) */
3484 print_quality
; /* Print quality (if any) */
3485 const char *ppd
; /* PPD option choice */
3486 int exact
; /* Did we get an exact match? */
3487 static char *options
= NULL
;/* Full list of options */
3488 static size_t optlength
= 0; /* Length of option buffer */
3492 * Building the options string is harder than it needs to be, but for the
3493 * moment we need to pass strings for command-line args and not IPP attribute
3496 * First build an options array for any PWG->PPD mapped option/choice pairs.
3499 pc
= job
->printer
->pc
;
3504 !ippFindAttribute(job
->attrs
,
3505 "com.apple.print.DocumentTicket.PMSpoolFormat",
3507 !ippFindAttribute(job
->attrs
, "APPrinterPreset", IPP_TAG_ZERO
) &&
3508 (ippFindAttribute(job
->attrs
, "print-color-mode", IPP_TAG_ZERO
) ||
3509 ippFindAttribute(job
->attrs
, "print-quality", IPP_TAG_ZERO
)))
3512 * Map print-color-mode and print-quality to a preset...
3515 if ((attr
= ippFindAttribute(job
->attrs
, "print-color-mode",
3516 IPP_TAG_KEYWORD
)) != NULL
&&
3517 !strcmp(attr
->values
[0].string
.text
, "monochrome"))
3518 print_color_mode
= _PWG_PRINT_COLOR_MODE_MONOCHROME
;
3520 print_color_mode
= _PWG_PRINT_COLOR_MODE_COLOR
;
3522 if ((attr
= ippFindAttribute(job
->attrs
, "print-quality",
3523 IPP_TAG_ENUM
)) != NULL
&&
3524 attr
->values
[0].integer
>= IPP_QUALITY_DRAFT
&&
3525 attr
->values
[0].integer
<= IPP_QUALITY_HIGH
)
3526 print_quality
= attr
->values
[0].integer
- IPP_QUALITY_DRAFT
;
3528 print_quality
= _PWG_PRINT_QUALITY_NORMAL
;
3530 if (pc
->num_presets
[print_color_mode
][print_quality
] == 0)
3533 * Try to find a preset that works so that we maximize the chances of us
3534 * getting a good print using IPP attributes.
3537 if (pc
->num_presets
[print_color_mode
][_PWG_PRINT_QUALITY_NORMAL
] > 0)
3538 print_quality
= _PWG_PRINT_QUALITY_NORMAL
;
3539 else if (pc
->num_presets
[_PWG_PRINT_COLOR_MODE_COLOR
][print_quality
] > 0)
3540 print_color_mode
= _PWG_PRINT_COLOR_MODE_COLOR
;
3543 print_quality
= _PWG_PRINT_QUALITY_NORMAL
;
3544 print_color_mode
= _PWG_PRINT_COLOR_MODE_COLOR
;
3548 if (pc
->num_presets
[print_color_mode
][print_quality
] > 0)
3551 * Copy the preset options as long as the corresponding names are not
3552 * already defined in the IPP request...
3555 for (i
= pc
->num_presets
[print_color_mode
][print_quality
],
3556 preset
= pc
->presets
[print_color_mode
][print_quality
];
3560 if (!ippFindAttribute(job
->attrs
, preset
->name
, IPP_TAG_ZERO
))
3561 num_pwgppds
= cupsAddOption(preset
->name
, preset
->value
, num_pwgppds
,
3569 if (!ippFindAttribute(job
->attrs
, "InputSlot", IPP_TAG_ZERO
) &&
3570 !ippFindAttribute(job
->attrs
, "HPPaperSource", IPP_TAG_ZERO
))
3572 if ((ppd
= _ppdCacheGetInputSlot(pc
, job
->attrs
, NULL
)) != NULL
)
3573 num_pwgppds
= cupsAddOption(pc
->source_option
, ppd
, num_pwgppds
,
3576 if (!ippFindAttribute(job
->attrs
, "MediaType", IPP_TAG_ZERO
) &&
3577 (ppd
= _ppdCacheGetMediaType(pc
, job
->attrs
, NULL
)) != NULL
)
3578 num_pwgppds
= cupsAddOption("MediaType", ppd
, num_pwgppds
, &pwgppds
);
3580 if (!ippFindAttribute(job
->attrs
, "PageRegion", IPP_TAG_ZERO
) &&
3581 !ippFindAttribute(job
->attrs
, "PageSize", IPP_TAG_ZERO
) &&
3582 (ppd
= _ppdCacheGetPageSize(pc
, job
->attrs
, NULL
, &exact
)) != NULL
)
3584 num_pwgppds
= cupsAddOption("PageSize", ppd
, num_pwgppds
, &pwgppds
);
3586 if (!ippFindAttribute(job
->attrs
, "media", IPP_TAG_ZERO
))
3587 num_pwgppds
= cupsAddOption("media", ppd
, num_pwgppds
, &pwgppds
);
3590 if (!ippFindAttribute(job
->attrs
, "OutputBin", IPP_TAG_ZERO
) &&
3591 (attr
= ippFindAttribute(job
->attrs
, "output-bin",
3592 IPP_TAG_ZERO
)) != NULL
&&
3593 (attr
->value_tag
== IPP_TAG_KEYWORD
||
3594 attr
->value_tag
== IPP_TAG_NAME
) &&
3595 (ppd
= _ppdCacheGetOutputBin(pc
, attr
->values
[0].string
.text
)) != NULL
)
3598 * Map output-bin to OutputBin option...
3601 num_pwgppds
= cupsAddOption("OutputBin", ppd
, num_pwgppds
, &pwgppds
);
3604 if (pc
->sides_option
&&
3605 !ippFindAttribute(job
->attrs
, pc
->sides_option
, IPP_TAG_ZERO
) &&
3606 (attr
= ippFindAttribute(job
->attrs
, "sides", IPP_TAG_KEYWORD
)) != NULL
)
3609 * Map sides to duplex option...
3612 if (!strcmp(attr
->values
[0].string
.text
, "one-sided"))
3613 num_pwgppds
= cupsAddOption(pc
->sides_option
, pc
->sides_1sided
,
3614 num_pwgppds
, &pwgppds
);
3615 else if (!strcmp(attr
->values
[0].string
.text
, "two-sided-long-edge"))
3616 num_pwgppds
= cupsAddOption(pc
->sides_option
, pc
->sides_2sided_long
,
3617 num_pwgppds
, &pwgppds
);
3618 else if (!strcmp(attr
->values
[0].string
.text
, "two-sided-short-edge"))
3619 num_pwgppds
= cupsAddOption(pc
->sides_option
, pc
->sides_2sided_short
,
3620 num_pwgppds
, &pwgppds
);
3624 * Map finishings values...
3627 num_pwgppds
= _ppdCacheGetFinishingOptions(pc
, job
->attrs
,
3628 IPP_FINISHINGS_NONE
, num_pwgppds
,
3633 * Figure out how much room we need...
3636 newlength
= ipp_length(job
->attrs
);
3638 for (i
= num_pwgppds
, pwgppd
= pwgppds
; i
> 0; i
--, pwgppd
++)
3639 newlength
+= 1 + strlen(pwgppd
->name
) + 1 + strlen(pwgppd
->value
);
3642 * Then allocate/reallocate the option buffer as needed...
3645 if (newlength
== 0) /* This can never happen, but Clang */
3646 newlength
= 1; /* thinks it can... */
3648 if (newlength
> optlength
|| !options
)
3651 optptr
= malloc(newlength
);
3653 optptr
= realloc(options
, newlength
);
3657 cupsdLogJob(job
, CUPSD_LOG_CRIT
,
3658 "Unable to allocate " CUPS_LLFMT
" bytes for option buffer.",
3659 CUPS_LLCAST newlength
);
3664 optlength
= newlength
;
3668 * Now loop through the attributes and convert them to the textual
3669 * representation used by the filters...
3675 snprintf(title
, title_size
, "%s-%d", job
->printer
->name
, job
->id
);
3676 strlcpy(copies
, "1", copies_size
);
3678 for (attr
= job
->attrs
->attrs
; attr
!= NULL
; attr
= attr
->next
)
3680 if (!strcmp(attr
->name
, "copies") &&
3681 attr
->value_tag
== IPP_TAG_INTEGER
)
3684 * Don't use the # copies attribute if we are printing the job sheets...
3688 snprintf(copies
, copies_size
, "%d", attr
->values
[0].integer
);
3690 else if (!strcmp(attr
->name
, "job-name") &&
3691 (attr
->value_tag
== IPP_TAG_NAME
||
3692 attr
->value_tag
== IPP_TAG_NAMELANG
))
3693 strlcpy(title
, attr
->values
[0].string
.text
, title_size
);
3694 else if (attr
->group_tag
== IPP_TAG_JOB
)
3697 * Filter out other unwanted attributes...
3700 if (attr
->value_tag
== IPP_TAG_NOVALUE
||
3701 attr
->value_tag
== IPP_TAG_MIMETYPE
||
3702 attr
->value_tag
== IPP_TAG_NAMELANG
||
3703 attr
->value_tag
== IPP_TAG_TEXTLANG
||
3704 (attr
->value_tag
== IPP_TAG_URI
&& strcmp(attr
->name
, "job-uuid") &&
3705 strcmp(attr
->name
, "job-authorization-uri")) ||
3706 attr
->value_tag
== IPP_TAG_URISCHEME
||
3707 attr
->value_tag
== IPP_TAG_BEGIN_COLLECTION
) /* Not yet supported */
3710 if (!strcmp(attr
->name
, "job-hold-until") ||
3711 !strcmp(attr
->name
, "job-id") ||
3712 !strcmp(attr
->name
, "job-k-octets") ||
3713 !strcmp(attr
->name
, "job-media-sheets") ||
3714 !strcmp(attr
->name
, "job-media-sheets-completed") ||
3715 !strcmp(attr
->name
, "job-state") ||
3716 !strcmp(attr
->name
, "job-state-reasons"))
3719 if (!strncmp(attr
->name
, "job-", 4) &&
3720 strcmp(attr
->name
, "job-account-id") &&
3721 strcmp(attr
->name
, "job-accounting-user-id") &&
3722 strcmp(attr
->name
, "job-authorization-uri") &&
3723 strcmp(attr
->name
, "job-billing") &&
3724 strcmp(attr
->name
, "job-impressions") &&
3725 strcmp(attr
->name
, "job-originating-host-name") &&
3726 strcmp(attr
->name
, "job-password") &&
3727 strcmp(attr
->name
, "job-password-encryption") &&
3728 strcmp(attr
->name
, "job-uuid") &&
3729 !(job
->printer
->type
& CUPS_PRINTER_REMOTE
))
3732 if ((!strcmp(attr
->name
, "job-impressions") ||
3733 !strcmp(attr
->name
, "page-label") ||
3734 !strcmp(attr
->name
, "page-border") ||
3735 !strncmp(attr
->name
, "number-up", 9) ||
3736 !strcmp(attr
->name
, "page-ranges") ||
3737 !strcmp(attr
->name
, "page-set") ||
3738 !_cups_strcasecmp(attr
->name
, "AP_FIRSTPAGE_InputSlot") ||
3739 !_cups_strcasecmp(attr
->name
, "AP_FIRSTPAGE_ManualFeed") ||
3740 !_cups_strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
3741 "PMTotalSidesImaged..n.") ||
3742 !_cups_strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
3743 "PMTotalBeginPages..n.")) &&
3748 * Otherwise add them to the list...
3751 if (optptr
> options
)
3752 strlcat(optptr
, " ", optlength
- (optptr
- options
));
3754 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
3756 strlcat(optptr
, attr
->name
, optlength
- (optptr
- options
));
3757 strlcat(optptr
, "=", optlength
- (optptr
- options
));
3760 for (i
= 0; i
< attr
->num_values
; i
++)
3763 strlcat(optptr
, ",", optlength
- (optptr
- options
));
3765 optptr
+= strlen(optptr
);
3767 switch (attr
->value_tag
)
3769 case IPP_TAG_INTEGER
:
3771 snprintf(optptr
, optlength
- (optptr
- options
),
3772 "%d", attr
->values
[i
].integer
);
3775 case IPP_TAG_BOOLEAN
:
3776 if (!attr
->values
[i
].boolean
)
3777 strlcat(optptr
, "no", optlength
- (optptr
- options
));
3779 strlcat(optptr
, attr
->name
,
3780 optlength
- (optptr
- options
));
3783 case IPP_TAG_RANGE
:
3784 if (attr
->values
[i
].range
.lower
== attr
->values
[i
].range
.upper
)
3785 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
3786 "%d", attr
->values
[i
].range
.lower
);
3788 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
3789 "%d-%d", attr
->values
[i
].range
.lower
,
3790 attr
->values
[i
].range
.upper
);
3793 case IPP_TAG_RESOLUTION
:
3794 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
3795 "%dx%d%s", attr
->values
[i
].resolution
.xres
,
3796 attr
->values
[i
].resolution
.yres
,
3797 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
3801 case IPP_TAG_STRING
:
3804 case IPP_TAG_KEYWORD
:
3805 case IPP_TAG_CHARSET
:
3806 case IPP_TAG_LANGUAGE
:
3808 for (valptr
= attr
->values
[i
].string
.text
; *valptr
;)
3810 if (strchr(" \t\n\\\'\"", *valptr
))
3812 *optptr
++ = *valptr
++;
3819 break; /* anti-compiler-warning-code */
3823 optptr
+= strlen(optptr
);
3828 * Finally loop through the PWG->PPD mapped options and add them...
3831 for (i
= num_pwgppds
, pwgppd
= pwgppds
; i
> 0; i
--, pwgppd
++)
3834 strlcpy(optptr
, pwgppd
->name
, optlength
- (optptr
- options
));
3835 optptr
+= strlen(optptr
);
3837 strlcpy(optptr
, pwgppd
->value
, optlength
- (optptr
- options
));
3838 optptr
+= strlen(optptr
);
3841 cupsFreeOptions(num_pwgppds
, pwgppds
);
3844 * Return the options string...
3852 * 'ipp_length()' - Compute the size of the buffer needed to hold
3853 * the textual IPP attributes.
3856 static size_t /* O - Size of attribute buffer */
3857 ipp_length(ipp_t
*ipp
) /* I - IPP request */
3859 size_t bytes
; /* Number of bytes */
3860 int i
; /* Looping var */
3861 ipp_attribute_t
*attr
; /* Current attribute */
3865 * Loop through all attributes...
3870 for (attr
= ipp
->attrs
; attr
!= NULL
; attr
= attr
->next
)
3873 * Skip attributes that won't be sent to filters...
3876 if (attr
->value_tag
== IPP_TAG_NOVALUE
||
3877 attr
->value_tag
== IPP_TAG_MIMETYPE
||
3878 attr
->value_tag
== IPP_TAG_NAMELANG
||
3879 attr
->value_tag
== IPP_TAG_TEXTLANG
||
3880 attr
->value_tag
== IPP_TAG_URI
||
3881 attr
->value_tag
== IPP_TAG_URISCHEME
)
3885 * Add space for a leading space and commas between each value.
3886 * For the first attribute, the leading space isn't used, so the
3887 * extra byte can be used as the nul terminator...
3890 bytes
++; /* " " separator */
3891 bytes
+= attr
->num_values
; /* "," separators */
3894 * Boolean attributes appear as "foo,nofoo,foo,nofoo", while
3895 * other attributes appear as "foo=value1,value2,...,valueN".
3898 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
3899 bytes
+= strlen(attr
->name
);
3901 bytes
+= attr
->num_values
* strlen(attr
->name
);
3904 * Now add the size required for each value in the attribute...
3907 switch (attr
->value_tag
)
3909 case IPP_TAG_INTEGER
:
3912 * Minimum value of a signed integer is -2147483647, or 11 digits.
3915 bytes
+= attr
->num_values
* 11;
3918 case IPP_TAG_BOOLEAN
:
3920 * Add two bytes for each false ("no") value...
3923 for (i
= 0; i
< attr
->num_values
; i
++)
3924 if (!attr
->values
[i
].boolean
)
3928 case IPP_TAG_RANGE
:
3930 * A range is two signed integers separated by a hyphen, or
3931 * 23 characters max.
3934 bytes
+= attr
->num_values
* 23;
3937 case IPP_TAG_RESOLUTION
:
3939 * A resolution is two signed integers separated by an "x" and
3940 * suffixed by the units, or 26 characters max.
3943 bytes
+= attr
->num_values
* 26;
3946 case IPP_TAG_STRING
:
3949 case IPP_TAG_KEYWORD
:
3950 case IPP_TAG_CHARSET
:
3951 case IPP_TAG_LANGUAGE
:
3954 * Strings can contain characters that need quoting. We need
3955 * at least 2 * len + 2 characters to cover the quotes and
3956 * any backslashes in the string.
3959 for (i
= 0; i
< attr
->num_values
; i
++)
3960 bytes
+= 2 * strlen(attr
->values
[i
].string
.text
) + 2;
3964 break; /* anti-compiler-warning-code */
3973 * 'load_job_cache()' - Load jobs from the job.cache file.
3977 load_job_cache(const char *filename
) /* I - job.cache filename */
3979 cups_file_t
*fp
; /* job.cache file */
3980 char line
[1024], /* Line buffer */
3981 *value
; /* Value on line */
3982 int linenum
; /* Line number in file */
3983 cupsd_job_t
*job
; /* Current job */
3984 int jobid
; /* Job ID */
3985 char jobfile
[1024]; /* Job filename */
3989 * Open the job.cache file...
3992 if ((fp
= cupsdOpenConfFile(filename
)) == NULL
)
3994 load_request_root();
3999 * Read entries from the job cache file and create jobs as needed.
4002 cupsdLogMessage(CUPSD_LOG_INFO
, "Loading job cache file \"%s\"...",
4008 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
4010 if (!_cups_strcasecmp(line
, "NextJobId"))
4013 NextJobId
= atoi(value
);
4015 else if (!_cups_strcasecmp(line
, "<Job"))
4019 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing </Job> directive on line %d.",
4026 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing job ID on line %d.", linenum
);
4030 jobid
= atoi(value
);
4034 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad job ID %d on line %d.", jobid
,
4039 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, jobid
);
4040 if (access(jobfile
, 0))
4042 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d.N", RequestRoot
, jobid
);
4043 if (access(jobfile
, 0))
4045 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Job %d] Files have gone away.",
4051 job
= calloc(1, sizeof(cupsd_job_t
));
4054 cupsdLogMessage(CUPSD_LOG_EMERG
,
4055 "[Job %d] Unable to allocate memory for job.", jobid
);
4060 job
->back_pipes
[0] = -1;
4061 job
->back_pipes
[1] = -1;
4062 job
->print_pipes
[0] = -1;
4063 job
->print_pipes
[1] = -1;
4064 job
->side_pipes
[0] = -1;
4065 job
->side_pipes
[1] = -1;
4066 job
->status_pipes
[0] = -1;
4067 job
->status_pipes
[1] = -1;
4069 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Loading from cache...");
4073 cupsdLogMessage(CUPSD_LOG_ERROR
,
4074 "Missing <Job #> directive on line %d.", linenum
);
4077 else if (!_cups_strcasecmp(line
, "</Job>"))
4079 cupsArrayAdd(Jobs
, job
);
4081 if (job
->state_value
<= IPP_JOB_STOPPED
&& cupsdLoadJob(job
))
4082 cupsArrayAdd(ActiveJobs
, job
);
4088 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing value on line %d.", linenum
);
4091 else if (!_cups_strcasecmp(line
, "State"))
4093 job
->state_value
= (ipp_jstate_t
)atoi(value
);
4095 if (job
->state_value
< IPP_JOB_PENDING
)
4096 job
->state_value
= IPP_JOB_PENDING
;
4097 else if (job
->state_value
> IPP_JOB_COMPLETED
)
4098 job
->state_value
= IPP_JOB_COMPLETED
;
4100 else if (!_cups_strcasecmp(line
, "HoldUntil"))
4102 job
->hold_until
= atoi(value
);
4104 else if (!_cups_strcasecmp(line
, "Priority"))
4106 job
->priority
= atoi(value
);
4108 else if (!_cups_strcasecmp(line
, "Username"))
4110 cupsdSetString(&job
->username
, value
);
4112 else if (!_cups_strcasecmp(line
, "Destination"))
4114 cupsdSetString(&job
->dest
, value
);
4116 else if (!_cups_strcasecmp(line
, "DestType"))
4118 job
->dtype
= (cups_ptype_t
)atoi(value
);
4120 else if (!_cups_strcasecmp(line
, "NumFiles"))
4122 job
->num_files
= atoi(value
);
4124 if (job
->num_files
< 0)
4126 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad NumFiles value %d on line %d.",
4127 job
->num_files
, linenum
);
4132 if (job
->num_files
> 0)
4134 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-001", RequestRoot
,
4136 if (access(jobfile
, 0))
4138 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Data files have gone away.");
4143 job
->filetypes
= calloc(job
->num_files
, sizeof(mime_type_t
*));
4144 job
->compressions
= calloc(job
->num_files
, sizeof(int));
4146 if (!job
->filetypes
|| !job
->compressions
)
4148 cupsdLogJob(job
, CUPSD_LOG_EMERG
,
4149 "Unable to allocate memory for %d files.",
4155 else if (!_cups_strcasecmp(line
, "File"))
4157 int number
, /* File number */
4158 compression
; /* Compression value */
4159 char super
[MIME_MAX_SUPER
], /* MIME super type */
4160 type
[MIME_MAX_TYPE
]; /* MIME type */
4163 if (sscanf(value
, "%d%*[ \t]%15[^/]/%255s%d", &number
, super
, type
,
4166 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File on line %d.", linenum
);
4170 if (number
< 1 || number
> job
->num_files
)
4172 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File number %d on line %d.",
4179 job
->compressions
[number
] = compression
;
4180 job
->filetypes
[number
] = mimeType(MimeDatabase
, super
, type
);
4182 if (!job
->filetypes
[number
])
4185 * If the original MIME type is unknown, auto-type it!
4188 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
4189 "Unknown MIME type %s/%s for file %d.",
4190 super
, type
, number
+ 1);
4192 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
4193 job
->id
, number
+ 1);
4194 job
->filetypes
[number
] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
4195 job
->compressions
+ number
);
4198 * If that didn't work, assume it is raw...
4201 if (!job
->filetypes
[number
])
4202 job
->filetypes
[number
] = mimeType(MimeDatabase
, "application",
4207 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unknown %s directive on line %d.",
4213 cupsdLogMessage(CUPSD_LOG_ERROR
,
4214 "Missing </Job> directive on line %d.", linenum
);
4215 cupsdDeleteJob(job
, CUPSD_JOB_PURGE
);
4223 * 'load_next_job_id()' - Load the NextJobId value from the job.cache file.
4227 load_next_job_id(const char *filename
) /* I - job.cache filename */
4229 cups_file_t
*fp
; /* job.cache file */
4230 char line
[1024], /* Line buffer */
4231 *value
; /* Value on line */
4232 int linenum
; /* Line number in file */
4233 int next_job_id
; /* NextJobId value from line */
4237 * Read the NextJobId directive from the job.cache file and use
4238 * the value (if any).
4241 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
4243 if (errno
!= ENOENT
)
4244 cupsdLogMessage(CUPSD_LOG_ERROR
,
4245 "Unable to open job cache file \"%s\": %s",
4246 filename
, strerror(errno
));
4251 cupsdLogMessage(CUPSD_LOG_INFO
,
4252 "Loading NextJobId from job cache file \"%s\"...", filename
);
4256 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
4258 if (!_cups_strcasecmp(line
, "NextJobId"))
4262 next_job_id
= atoi(value
);
4264 if (next_job_id
> NextJobId
)
4265 NextJobId
= next_job_id
;
4276 * 'load_request_root()' - Load jobs from the RequestRoot directory.
4280 load_request_root(void)
4282 cups_dir_t
*dir
; /* Directory */
4283 cups_dentry_t
*dent
; /* Directory entry */
4284 cupsd_job_t
*job
; /* New job */
4288 * Open the requests directory...
4291 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Scanning %s for jobs...", RequestRoot
);
4293 if ((dir
= cupsDirOpen(RequestRoot
)) == NULL
)
4295 cupsdLogMessage(CUPSD_LOG_ERROR
,
4296 "Unable to open spool directory \"%s\": %s",
4297 RequestRoot
, strerror(errno
));
4302 * Read all the c##### files...
4305 while ((dent
= cupsDirRead(dir
)) != NULL
)
4306 if (strlen(dent
->filename
) >= 6 && dent
->filename
[0] == 'c')
4309 * Allocate memory for the job...
4312 if ((job
= calloc(sizeof(cupsd_job_t
), 1)) == NULL
)
4314 cupsdLogMessage(CUPSD_LOG_ERROR
, "Ran out of memory for jobs.");
4320 * Assign the job ID...
4323 job
->id
= atoi(dent
->filename
+ 1);
4324 job
->back_pipes
[0] = -1;
4325 job
->back_pipes
[1] = -1;
4326 job
->print_pipes
[0] = -1;
4327 job
->print_pipes
[1] = -1;
4328 job
->side_pipes
[0] = -1;
4329 job
->side_pipes
[1] = -1;
4330 job
->status_pipes
[0] = -1;
4331 job
->status_pipes
[1] = -1;
4333 if (job
->id
>= NextJobId
)
4334 NextJobId
= job
->id
+ 1;
4340 if (cupsdLoadJob(job
))
4343 * Insert the job into the array, sorting by job priority and ID...
4346 cupsArrayAdd(Jobs
, job
);
4348 if (job
->state_value
<= IPP_JOB_STOPPED
)
4349 cupsArrayAdd(ActiveJobs
, job
);
4362 * 'remove_job_files()' - Remove the document files for a job.
4366 remove_job_files(cupsd_job_t
*job
) /* I - Job */
4368 int i
; /* Looping var */
4369 char filename
[1024]; /* Document filename */
4372 if (job
->num_files
<= 0)
4375 for (i
= 1; i
<= job
->num_files
; i
++)
4377 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
4379 cupsdUnlinkOrRemoveFile(filename
);
4382 free(job
->filetypes
);
4383 free(job
->compressions
);
4387 job
->filetypes
= NULL
;
4388 job
->compressions
= NULL
;
4390 LastEvent
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
4395 * 'remove_job_history()' - Remove the control file for a job.
4399 remove_job_history(cupsd_job_t
*job
) /* I - Job */
4401 char filename
[1024]; /* Control filename */
4405 * Remove the job info file...
4408 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
,
4410 cupsdUnlinkOrRemoveFile(filename
);
4412 LastEvent
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
4417 * 'set_time()' - Set one of the "time-at-xyz" attributes.
4421 set_time(cupsd_job_t
*job
, /* I - Job to update */
4422 const char *name
) /* I - Name of attribute */
4424 ipp_attribute_t
*attr
; /* Time attribute */
4425 time_t curtime
; /* Current time */
4428 curtime
= time(NULL
);
4430 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "%s=%ld", name
, (long)curtime
);
4432 if ((attr
= ippFindAttribute(job
->attrs
, name
, IPP_TAG_ZERO
)) != NULL
)
4434 attr
->value_tag
= IPP_TAG_INTEGER
;
4435 attr
->values
[0].integer
= curtime
;
4438 if (!strcmp(name
, "time-at-completed"))
4440 if (JobHistory
< INT_MAX
&& attr
)
4441 job
->history_time
= attr
->values
[0].integer
+ JobHistory
;
4443 job
->history_time
= INT_MAX
;
4445 if (job
->history_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
4446 JobHistoryUpdate
= job
->history_time
;
4448 if (JobFiles
< INT_MAX
&& attr
)
4449 job
->file_time
= attr
->values
[0].integer
+ JobFiles
;
4451 job
->file_time
= INT_MAX
;
4453 if (job
->file_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
4454 JobHistoryUpdate
= job
->file_time
;
4456 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_time: JobHistoryUpdate=%ld",
4457 (long)JobHistoryUpdate
);
4463 * 'start_job()' - Start a print job.
4467 start_job(cupsd_job_t
*job
, /* I - Job ID */
4468 cupsd_printer_t
*printer
) /* I - Printer to print job */
4470 const char *filename
; /* Support filename */
4471 ipp_attribute_t
*cancel_after
= ippFindAttribute(job
->attrs
,
4474 /* job-cancel-after attribute */
4477 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "start_job(job=%p(%d), printer=%p(%s))",
4478 job
, job
->id
, printer
, printer
->name
);
4481 * Make sure we have some files around before we try to print...
4484 if (job
->num_files
== 0)
4486 ippSetString(job
->attrs
, &job
->reasons
, 0, "aborted-by-system");
4487 cupsdSetJobState(job
, IPP_JOB_ABORTED
, CUPSD_JOB_DEFAULT
,
4488 "Aborting job because it has no files.");
4493 * Update the printer and job state to "processing"...
4496 if (!cupsdLoadJob(job
))
4499 if (!job
->printer_message
)
4500 job
->printer_message
= ippFindAttribute(job
->attrs
,
4501 "job-printer-state-message",
4503 if (job
->printer_message
)
4504 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
), "");
4506 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-printing");
4507 cupsdSetJobState(job
, IPP_JOB_PROCESSING
, CUPSD_JOB_DEFAULT
, NULL
);
4508 cupsdSetPrinterState(printer
, IPP_PRINTER_PROCESSING
, 0);
4509 cupsdSetPrinterReasons(printer
, "-cups-remote-pending,"
4510 "cups-remote-pending-held,"
4511 "cups-remote-processing,"
4512 "cups-remote-stopped,"
4513 "cups-remote-canceled,"
4514 "cups-remote-aborted,"
4515 "cups-remote-completed");
4518 job
->current_file
= 0;
4520 job
->history_time
= 0;
4522 job
->printer
= printer
;
4526 job
->cancel_time
= time(NULL
) + ippGetInteger(cancel_after
, 0);
4527 else if (MaxJobTime
> 0)
4528 job
->cancel_time
= time(NULL
) + MaxJobTime
;
4530 job
->cancel_time
= 0;
4533 * Check for support files...
4536 cupsdSetPrinterReasons(job
->printer
, "-cups-missing-filter-warning,"
4537 "cups-insecure-filter-warning");
4541 for (filename
= (const char *)cupsArrayFirst(printer
->pc
->support_files
);
4543 filename
= (const char *)cupsArrayNext(printer
->pc
->support_files
))
4545 if (_cupsFileCheck(filename
, _CUPS_FILE_CHECK_FILE
, !RunUser
,
4546 cupsdLogFCMessage
, printer
))
4552 * Setup the last exit status and security profiles...
4556 job
->profile
= cupsdCreateProfile(job
->id
);
4559 * Create the status pipes and buffer...
4562 if (cupsdOpenPipe(job
->status_pipes
))
4564 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
4565 "Unable to create job status pipes - %s.", strerror(errno
));
4567 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
4568 "Job stopped because the scheduler could not create the "
4569 "job status pipes.");
4571 cupsdDestroyProfile(job
->profile
);
4572 job
->profile
= NULL
;
4576 job
->status_buffer
= cupsdStatBufNew(job
->status_pipes
[0], NULL
);
4577 job
->status_level
= CUPSD_LOG_INFO
;
4580 * Create the backchannel pipes and make them non-blocking...
4583 if (cupsdOpenPipe(job
->back_pipes
))
4585 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
4586 "Unable to create back-channel pipes - %s.", strerror(errno
));
4588 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
4589 "Job stopped because the scheduler could not create the "
4590 "back-channel pipes.");
4592 cupsdClosePipe(job
->status_pipes
);
4593 cupsdStatBufDelete(job
->status_buffer
);
4594 job
->status_buffer
= NULL
;
4596 cupsdDestroyProfile(job
->profile
);
4597 job
->profile
= NULL
;
4601 fcntl(job
->back_pipes
[0], F_SETFL
,
4602 fcntl(job
->back_pipes
[0], F_GETFL
) | O_NONBLOCK
);
4603 fcntl(job
->back_pipes
[1], F_SETFL
,
4604 fcntl(job
->back_pipes
[1], F_GETFL
) | O_NONBLOCK
);
4607 * Create the side-channel pipes and make them non-blocking...
4610 if (socketpair(AF_LOCAL
, SOCK_STREAM
, 0, job
->side_pipes
))
4612 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
4613 "Unable to create side-channel pipes - %s.", strerror(errno
));
4615 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
4616 "Job stopped because the scheduler could not create the "
4617 "side-channel pipes.");
4619 cupsdClosePipe(job
->back_pipes
);
4621 cupsdClosePipe(job
->status_pipes
);
4622 cupsdStatBufDelete(job
->status_buffer
);
4623 job
->status_buffer
= NULL
;
4625 cupsdDestroyProfile(job
->profile
);
4626 job
->profile
= NULL
;
4630 fcntl(job
->side_pipes
[0], F_SETFL
,
4631 fcntl(job
->side_pipes
[0], F_GETFL
) | O_NONBLOCK
);
4632 fcntl(job
->side_pipes
[1], F_SETFL
,
4633 fcntl(job
->side_pipes
[1], F_GETFL
) | O_NONBLOCK
);
4635 fcntl(job
->side_pipes
[0], F_SETFD
,
4636 fcntl(job
->side_pipes
[0], F_GETFD
) | FD_CLOEXEC
);
4637 fcntl(job
->side_pipes
[1], F_SETFD
,
4638 fcntl(job
->side_pipes
[1], F_GETFD
) | FD_CLOEXEC
);
4641 * Now start the first file in the job...
4644 cupsdContinueJob(job
);
4649 * 'stop_job()' - Stop a print job.
4653 stop_job(cupsd_job_t
*job
, /* I - Job */
4654 cupsd_jobaction_t action
) /* I - Action */
4656 int i
; /* Looping var */
4659 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "stop_job(job=%p(%d), action=%d)", job
,
4662 FilterLevel
-= job
->cost
;
4665 if (action
== CUPSD_JOB_DEFAULT
&& !job
->kill_time
)
4666 job
->kill_time
= time(NULL
) + JobKillDelay
;
4667 else if (action
>= CUPSD_JOB_FORCE
)
4670 for (i
= 0; job
->filters
[i
]; i
++)
4671 if (job
->filters
[i
] > 0)
4673 cupsdEndProcess(job
->filters
[i
], action
>= CUPSD_JOB_FORCE
);
4675 if (action
>= CUPSD_JOB_FORCE
)
4676 job
->filters
[i
] = -job
->filters
[i
];
4679 if (job
->backend
> 0)
4681 cupsdEndProcess(job
->backend
, action
>= CUPSD_JOB_FORCE
);
4683 if (action
>= CUPSD_JOB_FORCE
)
4684 job
->backend
= -job
->backend
;
4687 if (action
>= CUPSD_JOB_FORCE
)
4690 * Clear job status...
4699 * 'unload_job()' - Unload a job from memory.
4703 unload_job(cupsd_job_t
*job
) /* I - Job */
4708 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Unloading...");
4710 ippDelete(job
->attrs
);
4714 job
->reasons
= NULL
;
4716 job
->job_sheets
= NULL
;
4717 job
->printer_message
= NULL
;
4718 job
->printer_reasons
= NULL
;
4723 * 'update_job()' - Read a status update from a job's filters.
4727 update_job(cupsd_job_t
*job
) /* I - Job to check */
4729 int i
; /* Looping var */
4730 int copies
; /* Number of copies printed */
4731 char message
[CUPSD_SB_BUFFER_SIZE
],
4733 *ptr
; /* Pointer update... */
4734 int loglevel
, /* Log level for message */
4735 event
= 0; /* Events? */
4736 static const char * const levels
[] = /* Log levels */
4752 * Get the printer associated with this job; if the printer is stopped for
4753 * any reason then job->printer will be reset to NULL, so make sure we have
4754 * a valid pointer...
4757 while ((ptr
= cupsdStatBufUpdate(job
->status_buffer
, &loglevel
,
4758 message
, sizeof(message
))) != NULL
)
4761 * Process page and printer state messages as needed...
4764 if (loglevel
== CUPSD_LOG_PAGE
)
4767 * Page message; send the message to the page_log file and update the
4768 * job sheet count...
4771 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "PAGE: %s", message
);
4775 if (!_cups_strncasecmp(message
, "total ", 6))
4778 * Got a total count of pages from a backend or filter...
4781 copies
= atoi(message
+ 6);
4782 copies
-= job
->sheets
->values
[0].integer
; /* Just track the delta */
4784 else if (!sscanf(message
, "%*d%d", &copies
))
4787 job
->sheets
->values
[0].integer
+= copies
;
4789 if (job
->printer
->page_limit
)
4790 cupsdUpdateQuota(job
->printer
, job
->username
, copies
, 0);
4793 cupsdLogPage(job
, message
);
4796 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
4797 "Printed %d page(s).", job
->sheets
->values
[0].integer
);
4799 else if (loglevel
== CUPSD_LOG_JOBSTATE
)
4802 * Support "keyword" to set job-state-reasons to the specified keyword.
4803 * This is sufficient for the current paid printing stuff.
4806 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "JOBSTATE: %s", message
);
4808 ippSetString(job
->attrs
, &job
->reasons
, 0, message
);
4810 else if (loglevel
== CUPSD_LOG_STATE
)
4812 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "STATE: %s", message
);
4814 if (!strcmp(message
, "paused"))
4816 cupsdStopPrinter(job
->printer
, 1);
4819 else if (message
[0] && cupsdSetPrinterReasons(job
->printer
, message
))
4821 event
|= CUPSD_EVENT_PRINTER_STATE
;
4826 * Reset cancel time after connecting to the device...
4829 ipp_attribute_t
*cancel_after
= ippFindAttribute(job
->attrs
,
4832 /* job-cancel-after attribute */
4834 for (i
= 0; i
< job
->printer
->num_reasons
; i
++)
4835 if (!strcmp(job
->printer
->reasons
[i
], "connecting-to-device"))
4838 if (i
>= job
->printer
->num_reasons
)
4841 job
->cancel_time
= time(NULL
) + ippGetInteger(cancel_after
, 0);
4843 job
->cancel_time
= time(NULL
) + MaxJobTime
;
4848 update_job_attrs(job
, 0);
4850 else if (loglevel
== CUPSD_LOG_ATTR
)
4853 * Set attribute(s)...
4856 int num_attrs
; /* Number of attributes */
4857 cups_option_t
*attrs
; /* Attributes */
4858 const char *attr
; /* Attribute */
4860 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "ATTR: %s", message
);
4862 num_attrs
= cupsParseOptions(message
, 0, &attrs
);
4864 if ((attr
= cupsGetOption("auth-info-default", num_attrs
,
4867 job
->printer
->num_options
= cupsAddOption("auth-info", attr
,
4868 job
->printer
->num_options
,
4869 &(job
->printer
->options
));
4870 cupsdSetPrinterAttrs(job
->printer
);
4872 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4875 if ((attr
= cupsGetOption("auth-info-required", num_attrs
,
4878 cupsdSetAuthInfoRequired(job
->printer
, attr
, NULL
);
4879 cupsdSetPrinterAttrs(job
->printer
);
4881 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4884 if ((attr
= cupsGetOption("job-media-progress", num_attrs
,
4887 int progress
= atoi(attr
);
4890 if (progress
>= 0 && progress
<= 100)
4892 job
->progress
= progress
;
4895 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
4896 "Printing page %d, %d%%",
4897 job
->sheets
->values
[0].integer
, job
->progress
);
4901 if ((attr
= cupsGetOption("printer-alert", num_attrs
, attrs
)) != NULL
)
4903 cupsdSetString(&job
->printer
->alert
, attr
);
4904 event
|= CUPSD_EVENT_PRINTER_STATE
;
4907 if ((attr
= cupsGetOption("printer-alert-description", num_attrs
,
4910 cupsdSetString(&job
->printer
->alert_description
, attr
);
4911 event
|= CUPSD_EVENT_PRINTER_STATE
;
4914 if ((attr
= cupsGetOption("marker-colors", num_attrs
, attrs
)) != NULL
)
4916 cupsdSetPrinterAttr(job
->printer
, "marker-colors", (char *)attr
);
4917 job
->printer
->marker_time
= time(NULL
);
4918 event
|= CUPSD_EVENT_PRINTER_STATE
;
4919 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4922 if ((attr
= cupsGetOption("marker-levels", num_attrs
, attrs
)) != NULL
)
4924 cupsdSetPrinterAttr(job
->printer
, "marker-levels", (char *)attr
);
4925 job
->printer
->marker_time
= time(NULL
);
4926 event
|= CUPSD_EVENT_PRINTER_STATE
;
4927 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4930 if ((attr
= cupsGetOption("marker-low-levels", num_attrs
, attrs
)) != NULL
)
4932 cupsdSetPrinterAttr(job
->printer
, "marker-low-levels", (char *)attr
);
4933 job
->printer
->marker_time
= time(NULL
);
4934 event
|= CUPSD_EVENT_PRINTER_STATE
;
4935 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4938 if ((attr
= cupsGetOption("marker-high-levels", num_attrs
, attrs
)) != NULL
)
4940 cupsdSetPrinterAttr(job
->printer
, "marker-high-levels", (char *)attr
);
4941 job
->printer
->marker_time
= time(NULL
);
4942 event
|= CUPSD_EVENT_PRINTER_STATE
;
4943 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4946 if ((attr
= cupsGetOption("marker-message", num_attrs
, attrs
)) != NULL
)
4948 cupsdSetPrinterAttr(job
->printer
, "marker-message", (char *)attr
);
4949 job
->printer
->marker_time
= time(NULL
);
4950 event
|= CUPSD_EVENT_PRINTER_STATE
;
4951 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4954 if ((attr
= cupsGetOption("marker-names", num_attrs
, attrs
)) != NULL
)
4956 cupsdSetPrinterAttr(job
->printer
, "marker-names", (char *)attr
);
4957 job
->printer
->marker_time
= time(NULL
);
4958 event
|= CUPSD_EVENT_PRINTER_STATE
;
4959 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4962 if ((attr
= cupsGetOption("marker-types", num_attrs
, attrs
)) != NULL
)
4964 cupsdSetPrinterAttr(job
->printer
, "marker-types", (char *)attr
);
4965 job
->printer
->marker_time
= time(NULL
);
4966 event
|= CUPSD_EVENT_PRINTER_STATE
;
4967 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4970 cupsFreeOptions(num_attrs
, attrs
);
4972 else if (loglevel
== CUPSD_LOG_PPD
)
4975 * Set attribute(s)...
4978 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "PPD: %s", message
);
4980 job
->num_keywords
= cupsParseOptions(message
, job
->num_keywords
,
4986 * Strip legacy message prefix...
4989 if (!strncmp(message
, "recoverable:", 12))
4992 while (isspace(*ptr
& 255))
4995 else if (!strncmp(message
, "recovered:", 10))
4998 while (isspace(*ptr
& 255))
5005 cupsdLogJob(job
, loglevel
, "%s", ptr
);
5007 if (loglevel
< CUPSD_LOG_DEBUG
&&
5008 strcmp(job
->printer
->state_message
, ptr
))
5010 strlcpy(job
->printer
->state_message
, ptr
,
5011 sizeof(job
->printer
->state_message
));
5013 event
|= CUPSD_EVENT_PRINTER_STATE
| CUPSD_EVENT_JOB_PROGRESS
;
5015 if (loglevel
<= job
->status_level
&& job
->status_level
> CUPSD_LOG_ERROR
)
5018 * Some messages show in the job-printer-state-message attribute...
5021 if (loglevel
!= CUPSD_LOG_NOTICE
)
5022 job
->status_level
= loglevel
;
5024 update_job_attrs(job
, 1);
5026 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
5027 "Set job-printer-state-message to \"%s\", "
5029 job
->printer_message
->values
[0].string
.text
,
5030 levels
[job
->status_level
]);
5035 if (!strchr(job
->status_buffer
->buffer
, '\n'))
5039 if (event
& CUPSD_EVENT_JOB_PROGRESS
)
5040 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
5041 "%s", job
->printer
->state_message
);
5042 if (event
& CUPSD_EVENT_PRINTER_STATE
)
5043 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, job
->printer
, NULL
,
5044 (job
->printer
->type
& CUPS_PRINTER_CLASS
) ?
5045 "Class \"%s\" state changed." :
5046 "Printer \"%s\" state changed.",
5047 job
->printer
->name
);
5050 if (ptr
== NULL
&& !job
->status_buffer
->bufused
)
5053 * See if all of the filters and the backend have returned their
5057 for (i
= 0; job
->filters
[i
] < 0; i
++);
5059 if (job
->filters
[i
])
5062 * EOF but we haven't collected the exit status of all filters...
5065 cupsdCheckProcess();
5069 if (job
->current_file
>= job
->num_files
&& job
->backend
> 0)
5072 * EOF but we haven't collected the exit status of the backend...
5075 cupsdCheckProcess();
5080 * Handle the end of job stuff...
5083 finalize_job(job
, 1);
5086 * Check for new jobs...
5095 * 'update_job_attrs()' - Update the job-printer-* attributes.
5099 update_job_attrs(cupsd_job_t
*job
, /* I - Job to update */
5100 int do_message
)/* I - 1 = copy job-printer-state message */
5102 int i
; /* Looping var */
5103 int num_reasons
; /* Actual number of reasons */
5104 const char * const *reasons
; /* Reasons */
5105 static const char *none
= "none"; /* "none" reason */
5109 * Get/create the job-printer-state-* attributes...
5112 if (!job
->printer_message
)
5114 if ((job
->printer_message
= ippFindAttribute(job
->attrs
,
5115 "job-printer-state-message",
5116 IPP_TAG_TEXT
)) == NULL
)
5117 job
->printer_message
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_TEXT
,
5118 "job-printer-state-message",
5122 if (!job
->printer_reasons
)
5123 job
->printer_reasons
= ippFindAttribute(job
->attrs
,
5124 "job-printer-state-reasons",
5128 * Copy or clear the printer-state-message value as needed...
5131 if (job
->state_value
!= IPP_JOB_PROCESSING
&&
5132 job
->status_level
== CUPSD_LOG_INFO
)
5134 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
), "");
5137 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
5139 else if (job
->printer
->state_message
[0] && do_message
)
5141 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
),
5142 job
->printer
->state_message
);
5145 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
5149 * ... and the printer-state-reasons value...
5152 if (job
->printer
->num_reasons
== 0)
5159 num_reasons
= job
->printer
->num_reasons
;
5160 reasons
= (const char * const *)job
->printer
->reasons
;
5163 if (!job
->printer_reasons
|| job
->printer_reasons
->num_values
!= num_reasons
)
5166 * Replace/create a job-printer-state-reasons attribute...
5169 ippDeleteAttribute(job
->attrs
, job
->printer_reasons
);
5171 job
->printer_reasons
= ippAddStrings(job
->attrs
,
5172 IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
5173 "job-printer-state-reasons",
5174 num_reasons
, NULL
, NULL
);
5179 * Don't bother clearing the reason strings if they are the same...
5182 for (i
= 0; i
< num_reasons
; i
++)
5183 if (strcmp(job
->printer_reasons
->values
[i
].string
.text
, reasons
[i
]))
5186 if (i
>= num_reasons
)
5190 * Not the same, so free the current strings...
5193 for (i
= 0; i
< num_reasons
; i
++)
5194 _cupsStrFree(job
->printer_reasons
->values
[i
].string
.text
);
5198 * Copy the reasons...
5201 for (i
= 0; i
< num_reasons
; i
++)
5202 job
->printer_reasons
->values
[i
].string
.text
= _cupsStrAlloc(reasons
[i
]);
5205 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);