2 * Job management routines for the CUPS scheduler.
4 * Copyright 2007-2017 by Apple Inc.
5 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * missing or damaged, see the license at "http://www.cups.org/".
15 * Include necessary headers...
20 #include <cups/backend.h>
23 # include <IOKit/pwr_mgt/IOPMLib.h>
24 # ifdef HAVE_IOKIT_PWR_MGT_IOPMLIBPRIVATE_H
25 # include <IOKit/pwr_mgt/IOPMLibPrivate.h>
26 # endif /* HAVE_IOKIT_PWR_MGT_IOPMLIBPRIVATE_H */
27 #endif /* __APPLE__ */
31 * Design Notes for Job Management
32 * -------------------------------
36 * pending Do nothing/check jobs
37 * pending-held Send SIGTERM to filters and backend
38 * processing Do nothing/start job
39 * stopped Send SIGKILL to filters and backend
40 * canceled Send SIGTERM to filters and backend
44 * Finalize clears the printer <-> job association, deletes the status
45 * buffer, closes all of the pipes, etc. and doesn't get run until all of
46 * the print processes are finished.
48 * UNLOADING OF JOBS (cupsdUnloadCompletedJobs)
50 * We unload the job attributes when they are not needed to reduce overall
51 * memory consumption. We don't unload jobs where job->state_value <
52 * IPP_JOB_STOPPED, job->printer != NULL, or job->access_time is recent.
54 * STARTING OF JOBS (start_job)
56 * When a job is started, a status buffer, several pipes, a security
57 * profile, and a backend process are created for the life of that job.
58 * These are shared for every file in a job. For remote print jobs, the
59 * IPP backend is provided with every file in the job and no filters are
62 * The job->printer member tracks which printer is printing a job, which
63 * can be different than the destination in job->dest for classes. The
64 * printer object also has a job pointer to track which job is being
67 * PRINTING OF JOB FILES (cupsdContinueJob)
69 * Each file in a job is filtered by 0 or more programs. After getting the
70 * list of filters needed and the total cost, the job is either passed or
71 * put back to the processing state until the current FilterLevel comes down
72 * enough to allow printing.
74 * If we can print, we build a string for the print options and run each of
75 * the filters, piping the output from one into the next.
77 * JOB STATUS UPDATES (update_job)
79 * The update_job function gets called whenever there are pending messages
80 * on the status pipe. These generally are updates to the marker-*,
81 * printer-state-message, or printer-state-reasons attributes. On EOF,
82 * finalize_job is called to clean up.
84 * FINALIZING JOBS (finalize_job)
86 * When all filters and the backend are done, we set the job state to
87 * completed (no errors), aborted (filter errors or abort-job policy),
88 * pending-held (auth required or retry-job policy), or pending
89 * (retry-current-job or stop-printer policies) as appropriate.
91 * Then we close the pipes and free the status buffers and profiles.
93 * JOB FILE COMPLETION (process_children in main.c)
95 * For multiple-file jobs, process_children (in main.c) sees that all
96 * filters have exited and calls in to print the next file if there are
97 * more files in the job, otherwise it waits for the backend to exit and
98 * update_job to do the cleanup.
106 static mime_filter_t gziptoany_filter
=
108 NULL
, /* Source type */
109 NULL
, /* Destination type */
111 "gziptoany" /* Filter program to run */
119 static int compare_active_jobs(void *first
, void *second
, void *data
);
120 static int compare_completed_jobs(void *first
, void *second
, void *data
);
121 static int compare_jobs(void *first
, void *second
, void *data
);
122 static void dump_job_history(cupsd_job_t
*job
);
123 static void finalize_job(cupsd_job_t
*job
, int set_job_state
);
124 static void free_job_history(cupsd_job_t
*job
);
125 static char *get_options(cupsd_job_t
*job
, int banner_page
, char *copies
,
126 size_t copies_size
, char *title
,
128 static size_t ipp_length(ipp_t
*ipp
);
129 static void load_job_cache(const char *filename
);
130 static void load_next_job_id(const char *filename
);
131 static void load_request_root(void);
132 static void remove_job_files(cupsd_job_t
*job
);
133 static void remove_job_history(cupsd_job_t
*job
);
134 static void set_time(cupsd_job_t
*job
, const char *name
);
135 static void start_job(cupsd_job_t
*job
, cupsd_printer_t
*printer
);
136 static void stop_job(cupsd_job_t
*job
, cupsd_jobaction_t action
);
137 static void unload_job(cupsd_job_t
*job
);
138 static void update_job(cupsd_job_t
*job
);
139 static void update_job_attrs(cupsd_job_t
*job
, int do_message
);
143 * 'cupsdAddJob()' - Add a new job to the job queue.
146 cupsd_job_t
* /* O - New job record */
147 cupsdAddJob(int priority
, /* I - Job priority */
148 const char *dest
) /* I - Job destination */
150 cupsd_job_t
*job
; /* New job record */
153 if ((job
= calloc(sizeof(cupsd_job_t
), 1)) == NULL
)
156 job
->id
= NextJobId
++;
157 job
->priority
= priority
;
158 job
->back_pipes
[0] = -1;
159 job
->back_pipes
[1] = -1;
160 job
->print_pipes
[0] = -1;
161 job
->print_pipes
[1] = -1;
162 job
->side_pipes
[0] = -1;
163 job
->side_pipes
[1] = -1;
164 job
->status_pipes
[0] = -1;
165 job
->status_pipes
[1] = -1;
167 cupsdSetString(&job
->dest
, dest
);
170 * Add the new job to the "all jobs" and "active jobs" lists...
173 cupsArrayAdd(Jobs
, job
);
174 cupsArrayAdd(ActiveJobs
, job
);
181 * 'cupsdCancelJobs()' - Cancel all jobs for the given destination/user.
185 cupsdCancelJobs(const char *dest
, /* I - Destination to cancel */
186 const char *username
, /* I - Username or NULL */
187 int purge
) /* I - Purge jobs? */
189 cupsd_job_t
*job
; /* Current job */
192 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
194 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
196 if ((!job
->dest
|| !job
->username
) && !cupsdLoadJob(job
))
199 if ((!dest
|| !strcmp(job
->dest
, dest
)) &&
200 (!username
|| !strcmp(job
->username
, username
)))
203 * Cancel all jobs matching this destination/user...
207 cupsdSetJobState(job
, IPP_JOB_CANCELED
, CUPSD_JOB_PURGE
,
208 "Job purged by user.");
209 else if (job
->state_value
< IPP_JOB_CANCELED
)
210 cupsdSetJobState(job
, IPP_JOB_CANCELED
, CUPSD_JOB_DEFAULT
,
211 "Job canceled by user.");
218 * 'cupsdCheckJobs()' - Check the pending jobs and start any if the destination
225 cupsd_job_t
*job
; /* Current job in queue */
226 cupsd_printer_t
*printer
, /* Printer destination */
227 *pclass
; /* Printer class destination */
228 ipp_attribute_t
*attr
; /* Job attribute */
229 time_t curtime
; /* Current time */
230 const char *reasons
; /* job-state-reasons value */
233 curtime
= time(NULL
);
235 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdCheckJobs: %d active jobs, sleeping=%d, ac-power=%d, reload=%d, curtime=%ld", cupsArrayCount(ActiveJobs
), Sleeping
, ACPower
, NeedReload
, (long)curtime
);
237 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
239 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
241 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
242 "cupsdCheckJobs: Job %d - dest=\"%s\", printer=%p, "
243 "state=%d, cancel_time=%ld, hold_until=%ld, kill_time=%ld, "
244 "pending_cost=%d, pending_timeout=%ld", job
->id
, job
->dest
,
245 job
->printer
, job
->state_value
, (long)job
->cancel_time
,
246 (long)job
->hold_until
, (long)job
->kill_time
,
247 job
->pending_cost
, (long)job
->pending_timeout
);
250 * Kill jobs if they are unresponsive...
253 if (job
->kill_time
&& job
->kill_time
<= curtime
)
256 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Stopping unresponsive job.");
258 stop_job(job
, CUPSD_JOB_FORCE
);
263 * Cancel stuck jobs...
266 if (job
->cancel_time
&& job
->cancel_time
<= curtime
)
268 int cancel_after
; /* job-cancel-after value */
270 attr
= ippFindAttribute(job
->attrs
, "job-cancel-after", IPP_TAG_INTEGER
);
271 cancel_after
= attr
? ippGetInteger(attr
, 0) : MaxJobTime
;
274 cupsdSetJobState(job
, IPP_JOB_CANCELED
, CUPSD_JOB_FORCE
, "Marking stuck job as completed after %d seconds.", cancel_after
);
276 cupsdSetJobState(job
, IPP_JOB_CANCELED
, CUPSD_JOB_DEFAULT
, "Canceling stuck job after %d seconds.", cancel_after
);
281 * Start held jobs if they are ready...
284 if (job
->state_value
== IPP_JOB_HELD
&&
286 job
->hold_until
< curtime
)
288 if (job
->pending_timeout
)
291 * This job is pending; check that we don't have an active Send-Document
292 * operation in progress on any of the client connections, then timeout
293 * the job so we can start printing...
296 cupsd_client_t
*con
; /* Current client connection */
298 for (con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
300 con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
302 con
->request
->request
.op
.operation_id
== IPP_SEND_DOCUMENT
)
308 if (cupsdTimeoutJob(job
))
312 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
313 "Job submission timed out.");
317 * Continue jobs that are waiting on the FilterLimit...
320 if (job
->pending_cost
> 0 &&
321 ((FilterLevel
+ job
->pending_cost
) < FilterLimit
|| FilterLevel
== 0))
322 cupsdContinueJob(job
);
325 * Skip jobs that where held-on-create
328 reasons
= ippGetString(job
->reasons
, 0, NULL
);
329 if (reasons
&& !strcmp(reasons
, "job-held-on-create"))
332 * Check whether the printer is still holding new jobs...
335 printer
= cupsdFindDest(job
->dest
);
337 if (printer
->holding_new_jobs
)
340 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
344 * Start pending jobs if the destination is available...
347 if (job
->state_value
== IPP_JOB_PENDING
&& !NeedReload
&&
348 (!Sleeping
|| ACPower
) && !DoingShutdown
&& !job
->printer
)
350 printer
= cupsdFindDest(job
->dest
);
353 while (printer
&& (printer
->type
& CUPS_PRINTER_CLASS
))
356 * If the class is remote, just pass it to the remote server...
361 if (pclass
->state
== IPP_PRINTER_STOPPED
)
363 else if (pclass
->type
& CUPS_PRINTER_REMOTE
)
366 printer
= cupsdFindAvailablePrinter(printer
->name
);
369 if (!printer
&& !pclass
)
372 * Whoa, the printer and/or class for this destination went away;
376 cupsdSetJobState(job
, IPP_JOB_ABORTED
, CUPSD_JOB_PURGE
,
377 "Job aborted because the destination printer/class "
383 * See if the printer is available or remote and not printing a job;
384 * if so, start the job...
390 * Add/update a job-printer-uri-actual attribute for this job
391 * so that we know which printer actually printed the job...
394 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri-actual", IPP_TAG_URI
)) != NULL
)
395 ippSetString(job
->attrs
, &attr
, 0, printer
->uri
);
397 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-printer-uri-actual", NULL
, printer
->uri
);
400 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
403 if (!printer
->job
&& printer
->state
== IPP_PRINTER_IDLE
)
409 cupsArraySave(ActiveJobs
);
410 start_job(job
, printer
);
411 cupsArrayRestore(ActiveJobs
);
420 * 'cupsdCleanJobs()' - Clean out old jobs.
426 cupsd_job_t
*job
; /* Current job */
427 time_t curtime
; /* Current time */
430 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
431 "cupsdCleanJobs: MaxJobs=%d, JobHistory=%d, JobFiles=%d",
432 MaxJobs
, JobHistory
, JobFiles
);
434 if (MaxJobs
<= 0 && JobHistory
== INT_MAX
&& JobFiles
== INT_MAX
)
437 curtime
= time(NULL
);
438 JobHistoryUpdate
= 0;
440 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
442 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
444 if (job
->state_value
>= IPP_JOB_CANCELED
&& !job
->printer
)
447 * Expire old jobs (or job files)...
450 if ((MaxJobs
> 0 && cupsArrayCount(Jobs
) >= MaxJobs
) ||
451 (job
->history_time
&& job
->history_time
<= curtime
))
453 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Removing from history.");
454 cupsdDeleteJob(job
, CUPSD_JOB_PURGE
);
456 else if (job
->file_time
&& job
->file_time
<= curtime
)
458 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Removing document files.");
459 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "curtime=%ld, job->file_time=%ld", (long)curtime
, (long)job
->file_time
);
460 remove_job_files(job
);
462 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
464 if (job
->history_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
465 JobHistoryUpdate
= job
->history_time
;
469 if (job
->history_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
470 JobHistoryUpdate
= job
->history_time
;
472 if (job
->file_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
473 JobHistoryUpdate
= job
->file_time
;
478 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdCleanJobs: JobHistoryUpdate=%ld",
479 (long)JobHistoryUpdate
);
484 * 'cupsdContinueJob()' - Continue printing with the next file in a job.
488 cupsdContinueJob(cupsd_job_t
*job
) /* I - Job */
490 int i
; /* Looping var */
491 int slot
; /* Pipe slot */
492 cups_array_t
*filters
= NULL
,/* Filters for job */
493 *prefilters
; /* Filters with prefilters */
494 mime_filter_t
*filter
, /* Current filter */
495 *prefilter
, /* Prefilter */
496 port_monitor
; /* Port monitor filter */
497 char scheme
[255]; /* Device URI scheme */
498 ipp_attribute_t
*attr
; /* Current attribute */
499 const char *ptr
, /* Pointer into value */
500 *abort_message
; /* Abort message */
501 ipp_jstate_t abort_state
= IPP_JOB_STOPPED
;
502 /* New job state on abort */
503 struct stat backinfo
; /* Backend file information */
504 int backroot
; /* Run backend as root? */
505 int pid
; /* Process ID of new filter process */
506 int banner_page
; /* 1 if banner page, 0 otherwise */
507 int filterfds
[2][2] = { { -1, -1 }, { -1, -1 } };
508 /* Pipes used between filters */
509 int envc
; /* Number of environment variables */
510 struct stat fileinfo
; /* Job file information */
511 int argc
= 0; /* Number of arguments */
512 char **argv
= NULL
, /* Filter command-line arguments */
513 filename
[1024], /* Job filename */
514 command
[1024], /* Full path to command */
515 jobid
[255], /* Job ID string */
517 /* Job title string */
518 copies
[255], /* # copies string */
519 *options
, /* Options string */
521 /* Environment variables */
522 charset
[255], /* CHARSET env variable */
523 class_name
[255],/* CLASS env variable */
524 classification
[1024],
525 /* CLASSIFICATION env variable */
527 /* CONTENT_TYPE env variable */
529 /* DEVICE_URI env variable */
530 final_content_type
[1024] = "",
531 /* FINAL_CONTENT_TYPE env variable */
532 lang
[255], /* LANG env variable */
535 /* APPLE_LANGUAGE env variable */
536 #endif /* __APPLE__ */
537 auth_info_required
[255],
538 /* AUTH_INFO_REQUIRED env variable */
539 ppd
[1024], /* PPD env variable */
541 /* PRINTER_INFO env variable */
542 printer_location
[255],
543 /* PRINTER_LOCATION env variable */
545 /* PRINTER env variable */
546 *printer_state_reasons
= NULL
,
547 /* PRINTER_STATE_REASONS env var */
549 /* RIP_MAX_CACHE env variable */
552 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
553 "cupsdContinueJob(job=%p(%d)): current_file=%d, num_files=%d",
554 job
, job
->id
, job
->current_file
, job
->num_files
);
557 * Figure out what filters are required to convert from
558 * the source to the destination type...
561 FilterLevel
-= job
->cost
;
564 job
->pending_cost
= 0;
566 memset(job
->filters
, 0, sizeof(job
->filters
));
568 if (job
->printer
->raw
)
571 * Remote jobs and raw queues go directly to the printer without
575 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Sending job to queue tagged as raw...");
580 * Local jobs get filtered...
583 mime_type_t
*dst
= job
->printer
->filetype
;
584 /* Destination file type */
586 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
587 job
->id
, job
->current_file
+ 1);
588 if (stat(filename
, &fileinfo
))
589 fileinfo
.st_size
= 0;
591 if (job
->retry_as_raster
)
594 * Need to figure out whether the printer supports image/pwg-raster or
595 * image/urf, and use the corresponding type...
598 char type
[MIME_MAX_TYPE
]; /* MIME media type for printer */
600 snprintf(type
, sizeof(type
), "%s/image/urf", job
->printer
->name
);
601 if ((dst
= mimeType(MimeDatabase
, "printer", type
)) == NULL
)
603 snprintf(type
, sizeof(type
), "%s/image/pwg-raster", job
->printer
->name
);
604 dst
= mimeType(MimeDatabase
, "printer", type
);
608 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Retrying job as \"%s\".", strchr(dst
->type
, '/') + 1);
610 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Unable to retry job using a supported raster format.");
613 filters
= mimeFilter2(MimeDatabase
, job
->filetypes
[job
->current_file
], (size_t)fileinfo
.st_size
, dst
, &(job
->cost
));
617 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
618 "Unable to convert file %d to printable format.",
621 abort_message
= "Aborting job because it cannot be printed.";
622 abort_state
= IPP_JOB_ABORTED
;
624 ippSetString(job
->attrs
, &job
->reasons
, 0, "document-unprintable-error");
629 * Figure out the final content type...
632 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "%d filters for job:",
633 cupsArrayCount(filters
));
634 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
636 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
637 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "%s (%s/%s to %s/%s, cost %d)",
639 filter
->src
? filter
->src
->super
: "???",
640 filter
->src
? filter
->src
->type
: "???",
641 filter
->dst
? filter
->dst
->super
: "???",
642 filter
->dst
? filter
->dst
->type
: "???",
645 if (!job
->printer
->remote
)
647 for (filter
= (mime_filter_t
*)cupsArrayLast(filters
);
648 filter
&& filter
->dst
;
649 filter
= (mime_filter_t
*)cupsArrayPrev(filters
))
650 if (strcmp(filter
->dst
->super
, "printer") ||
651 strcmp(filter
->dst
->type
, job
->printer
->name
))
654 if (filter
&& filter
->dst
)
656 if ((ptr
= strchr(filter
->dst
->type
, '/')) != NULL
)
657 snprintf(final_content_type
, sizeof(final_content_type
),
658 "FINAL_CONTENT_TYPE=%s", ptr
+ 1);
660 snprintf(final_content_type
, sizeof(final_content_type
),
661 "FINAL_CONTENT_TYPE=%s/%s", filter
->dst
->super
,
665 snprintf(final_content_type
, sizeof(final_content_type
),
666 "FINAL_CONTENT_TYPE=printer/%s", job
->printer
->name
);
670 * Remove NULL ("-") filters...
673 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
675 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
676 if (!strcmp(filter
->filter
, "-"))
677 cupsArrayRemove(filters
, filter
);
679 if (cupsArrayCount(filters
) == 0)
681 cupsArrayDelete(filters
);
686 * If this printer has any pre-filters, insert the required pre-filter
687 * in the filters array...
690 if (job
->printer
->prefiltertype
&& filters
)
692 prefilters
= cupsArrayNew(NULL
, NULL
);
694 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
696 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
698 if ((prefilter
= mimeFilterLookup(MimeDatabase
, filter
->src
,
699 job
->printer
->prefiltertype
)))
701 cupsArrayAdd(prefilters
, prefilter
);
702 job
->cost
+= prefilter
->cost
;
705 cupsArrayAdd(prefilters
, filter
);
708 cupsArrayDelete(filters
);
709 filters
= prefilters
;
714 * Set a minimum cost of 100 for all jobs so that FilterLimit
715 * works with raw queues and other low-cost paths.
722 * See if the filter cost is too high...
725 if ((FilterLevel
+ job
->cost
) > FilterLimit
&& FilterLevel
> 0 &&
729 * Don't print this job quite yet...
732 cupsArrayDelete(filters
);
734 cupsdLogJob(job
, CUPSD_LOG_INFO
,
735 "Holding because filter limit has been reached.");
736 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
737 "cupsdContinueJob: file=%d, cost=%d, level=%d, limit=%d",
738 job
->current_file
, job
->cost
, FilterLevel
,
741 job
->pending_cost
= job
->cost
;
746 FilterLevel
+= job
->cost
;
749 * Add decompression/raw filter as needed...
752 if ((job
->compressions
[job
->current_file
] && (!job
->printer
->remote
|| job
->num_files
== 1)) ||
753 (!job
->printer
->remote
&& job
->printer
->raw
&& job
->num_files
> 1))
756 * Add gziptoany filter to the front of the list...
760 filters
= cupsArrayNew(NULL
, NULL
);
762 if (!cupsArrayInsert(filters
, &gziptoany_filter
))
764 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
765 "Unable to add decompression filter - %s", strerror(errno
));
767 cupsArrayDelete(filters
);
769 abort_message
= "Stopping job because the scheduler ran out of memory.";
776 * Add port monitor, if any...
779 if (job
->printer
->port_monitor
)
782 * Add port monitor to the end of the list...
786 filters
= cupsArrayNew(NULL
, NULL
);
788 port_monitor
.src
= NULL
;
789 port_monitor
.dst
= NULL
;
790 port_monitor
.cost
= 0;
792 snprintf(port_monitor
.filter
, sizeof(port_monitor
.filter
),
793 "%s/monitor/%s", ServerBin
, job
->printer
->port_monitor
);
795 if (!cupsArrayAdd(filters
, &port_monitor
))
797 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
798 "Unable to add port monitor - %s", strerror(errno
));
800 abort_message
= "Stopping job because the scheduler ran out of memory.";
807 * Make sure we don't go over the "MAX_FILTERS" limit...
810 if (cupsArrayCount(filters
) > MAX_FILTERS
)
812 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
813 "Too many filters (%d > %d), unable to print.",
814 cupsArrayCount(filters
), MAX_FILTERS
);
816 abort_message
= "Aborting job because it needs too many filters to print.";
817 abort_state
= IPP_JOB_ABORTED
;
819 ippSetString(job
->attrs
, &job
->reasons
, 0, "document-unprintable-error");
825 * Determine if we are printing a banner page or not...
828 if (job
->job_sheets
== NULL
)
830 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "No job-sheets attribute.");
831 if ((job
->job_sheets
=
832 ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_ZERO
)) != NULL
)
833 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
834 "... but someone added one without setting job_sheets.");
836 else if (job
->job_sheets
->num_values
== 1)
837 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "job-sheets=%s",
838 job
->job_sheets
->values
[0].string
.text
);
840 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "job-sheets=%s,%s",
841 job
->job_sheets
->values
[0].string
.text
,
842 job
->job_sheets
->values
[1].string
.text
);
844 if (job
->printer
->type
& CUPS_PRINTER_REMOTE
)
846 else if (job
->job_sheets
== NULL
)
848 else if (_cups_strcasecmp(job
->job_sheets
->values
[0].string
.text
, "none") != 0 &&
849 job
->current_file
== 0)
851 else if (job
->job_sheets
->num_values
> 1 &&
852 _cups_strcasecmp(job
->job_sheets
->values
[1].string
.text
, "none") != 0 &&
853 job
->current_file
== (job
->num_files
- 1))
858 if ((options
= get_options(job
, banner_page
, copies
, sizeof(copies
), title
,
859 sizeof(title
))) == NULL
)
861 abort_message
= "Stopping job because the scheduler ran out of memory.";
867 * Build the command-line arguments for the filters. Each filter
868 * has 6 or 7 arguments:
876 * argv[6] = filename (optional; normally stdin)
878 * This allows legacy printer drivers that use the old System V
879 * printing interface to be used by CUPS.
881 * For remote jobs, we send all of the files in the argument list.
884 if (job
->printer
->remote
)
885 argc
= 6 + job
->num_files
;
889 if ((argv
= calloc((size_t)argc
+ 1, sizeof(char *))) == NULL
)
891 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Unable to allocate argument array - %s",
894 abort_message
= "Stopping job because the scheduler ran out of memory.";
899 sprintf(jobid
, "%d", job
->id
);
901 argv
[0] = job
->printer
->name
;
903 argv
[2] = job
->username
;
908 if (job
->printer
->remote
&& job
->num_files
> 1)
910 for (i
= 0; i
< job
->num_files
; i
++)
912 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
914 argv
[6 + i
] = strdup(filename
);
919 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
920 job
->id
, job
->current_file
+ 1);
921 argv
[6] = strdup(filename
);
924 for (i
= 0; argv
[i
]; i
++)
925 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "argv[%d]=\"%s\"", i
, argv
[i
]);
928 * Create environment variable strings for the filters...
931 attr
= ippFindAttribute(job
->attrs
, "attributes-natural-language",
935 strlcpy(apple_language
, "APPLE_LANGUAGE=", sizeof(apple_language
));
936 _cupsAppleLanguage(attr
->values
[0].string
.text
,
937 apple_language
+ 15, sizeof(apple_language
) - 15);
938 #endif /* __APPLE__ */
940 switch (strlen(attr
->values
[0].string
.text
))
944 * This is an unknown or badly formatted language code; use
945 * the POSIX locale...
948 strlcpy(lang
, "LANG=C", sizeof(lang
));
953 * Just the language code (ll)...
956 snprintf(lang
, sizeof(lang
), "LANG=%s.UTF-8",
957 attr
->values
[0].string
.text
);
962 * Language and country code (ll-cc)...
965 snprintf(lang
, sizeof(lang
), "LANG=%c%c_%c%c.UTF-8",
966 attr
->values
[0].string
.text
[0],
967 attr
->values
[0].string
.text
[1],
968 toupper(attr
->values
[0].string
.text
[3] & 255),
969 toupper(attr
->values
[0].string
.text
[4] & 255));
973 if ((attr
= ippFindAttribute(job
->attrs
, "document-format",
974 IPP_TAG_MIMETYPE
)) != NULL
&&
975 (ptr
= strstr(attr
->values
[0].string
.text
, "charset=")) != NULL
)
976 snprintf(charset
, sizeof(charset
), "CHARSET=%s", ptr
+ 8);
978 strlcpy(charset
, "CHARSET=utf-8", sizeof(charset
));
980 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s/%s",
981 job
->filetypes
[job
->current_file
]->super
,
982 job
->filetypes
[job
->current_file
]->type
);
983 snprintf(device_uri
, sizeof(device_uri
), "DEVICE_URI=%s",
984 job
->printer
->device_uri
);
985 snprintf(ppd
, sizeof(ppd
), "PPD=%s/ppd/%s.ppd", ServerRoot
,
987 snprintf(printer_info
, sizeof(printer_name
), "PRINTER_INFO=%s",
988 job
->printer
->info
? job
->printer
->info
: "");
989 snprintf(printer_location
, sizeof(printer_name
), "PRINTER_LOCATION=%s",
990 job
->printer
->location
? job
->printer
->location
: "");
991 snprintf(printer_name
, sizeof(printer_name
), "PRINTER=%s", job
->printer
->name
);
992 if (job
->printer
->num_reasons
> 0)
994 char *psrptr
; /* Pointer into PRINTER_STATE_REASONS */
995 size_t psrlen
; /* Size of PRINTER_STATE_REASONS */
997 for (psrlen
= 22, i
= 0; i
< job
->printer
->num_reasons
; i
++)
998 psrlen
+= strlen(job
->printer
->reasons
[i
]) + 1;
1000 if ((printer_state_reasons
= malloc(psrlen
)) != NULL
)
1003 * All of these strcpy's are safe because we allocated the psr string...
1006 strlcpy(printer_state_reasons
, "PRINTER_STATE_REASONS=", psrlen
);
1007 for (psrptr
= printer_state_reasons
+ 22, i
= 0;
1008 i
< job
->printer
->num_reasons
;
1013 strlcpy(psrptr
, job
->printer
->reasons
[i
], psrlen
- (size_t)(psrptr
- printer_state_reasons
));
1014 psrptr
+= strlen(psrptr
);
1018 snprintf(rip_max_cache
, sizeof(rip_max_cache
), "RIP_MAX_CACHE=%s", RIPCache
);
1020 if (job
->printer
->num_auth_info_required
== 1)
1021 snprintf(auth_info_required
, sizeof(auth_info_required
),
1022 "AUTH_INFO_REQUIRED=%s",
1023 job
->printer
->auth_info_required
[0]);
1024 else if (job
->printer
->num_auth_info_required
== 2)
1025 snprintf(auth_info_required
, sizeof(auth_info_required
),
1026 "AUTH_INFO_REQUIRED=%s,%s",
1027 job
->printer
->auth_info_required
[0],
1028 job
->printer
->auth_info_required
[1]);
1029 else if (job
->printer
->num_auth_info_required
== 3)
1030 snprintf(auth_info_required
, sizeof(auth_info_required
),
1031 "AUTH_INFO_REQUIRED=%s,%s,%s",
1032 job
->printer
->auth_info_required
[0],
1033 job
->printer
->auth_info_required
[1],
1034 job
->printer
->auth_info_required
[2]);
1035 else if (job
->printer
->num_auth_info_required
== 4)
1036 snprintf(auth_info_required
, sizeof(auth_info_required
),
1037 "AUTH_INFO_REQUIRED=%s,%s,%s,%s",
1038 job
->printer
->auth_info_required
[0],
1039 job
->printer
->auth_info_required
[1],
1040 job
->printer
->auth_info_required
[2],
1041 job
->printer
->auth_info_required
[3]);
1043 strlcpy(auth_info_required
, "AUTH_INFO_REQUIRED=none",
1044 sizeof(auth_info_required
));
1046 envc
= cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
1048 envp
[envc
++] = charset
;
1049 envp
[envc
++] = lang
;
1051 envp
[envc
++] = apple_language
;
1052 #endif /* __APPLE__ */
1053 envp
[envc
++] = ppd
;
1054 envp
[envc
++] = rip_max_cache
;
1055 envp
[envc
++] = content_type
;
1056 envp
[envc
++] = device_uri
;
1057 envp
[envc
++] = printer_info
;
1058 envp
[envc
++] = printer_location
;
1059 envp
[envc
++] = printer_name
;
1060 envp
[envc
++] = printer_state_reasons
? printer_state_reasons
:
1061 "PRINTER_STATE_REASONS=none";
1062 envp
[envc
++] = banner_page
? "CUPS_FILETYPE=job-sheet" :
1063 "CUPS_FILETYPE=document";
1065 if (final_content_type
[0])
1066 envp
[envc
++] = final_content_type
;
1068 if (Classification
&& !banner_page
)
1070 if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
1071 IPP_TAG_NAME
)) == NULL
)
1072 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
1074 else if (attr
->num_values
> 1 &&
1075 strcmp(attr
->values
[1].string
.text
, "none") != 0)
1076 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
1077 attr
->values
[1].string
.text
);
1079 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
1080 attr
->values
[0].string
.text
);
1082 envp
[envc
++] = classification
;
1085 if (job
->dtype
& CUPS_PRINTER_CLASS
)
1087 snprintf(class_name
, sizeof(class_name
), "CLASS=%s", job
->dest
);
1088 envp
[envc
++] = class_name
;
1091 envp
[envc
++] = auth_info_required
;
1094 i
< (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0]));
1096 if (job
->auth_env
[i
])
1097 envp
[envc
++] = job
->auth_env
[i
];
1102 envp
[envc
++] = job
->auth_uid
;
1106 for (i
= 0; i
< envc
; i
++)
1107 if (!strncmp(envp
[i
], "AUTH_", 5))
1108 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"AUTH_%c****\"", i
,
1110 else if (strncmp(envp
[i
], "DEVICE_URI=", 11))
1111 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"%s\"", i
, envp
[i
]);
1113 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"DEVICE_URI=%s\"", i
,
1114 job
->printer
->sanitized_device_uri
);
1116 if (job
->printer
->remote
)
1117 job
->current_file
= job
->num_files
;
1119 job
->current_file
++;
1122 * Now create processes for all of the filters...
1125 for (i
= 0, slot
= 0, filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
1127 i
++, filter
= (mime_filter_t
*)cupsArrayNext(filters
))
1129 if (filter
->filter
[0] != '/')
1130 snprintf(command
, sizeof(command
), "%s/filter/%s", ServerBin
,
1133 strlcpy(command
, filter
->filter
, sizeof(command
));
1135 if (i
< (cupsArrayCount(filters
) - 1))
1137 if (cupsdOpenPipe(filterfds
[slot
]))
1139 abort_message
= "Stopping job because the scheduler could not create "
1140 "the filter pipes.";
1147 if (job
->current_file
== 1 ||
1148 (job
->printer
->pc
&& job
->printer
->pc
->single_file
))
1150 if (strncmp(job
->printer
->device_uri
, "file:", 5) != 0)
1152 if (cupsdOpenPipe(job
->print_pipes
))
1154 abort_message
= "Stopping job because the scheduler could not "
1155 "create the backend pipes.";
1162 job
->print_pipes
[0] = -1;
1163 if (!strcmp(job
->printer
->device_uri
, "file:/dev/null") ||
1164 !strcmp(job
->printer
->device_uri
, "file:///dev/null"))
1165 job
->print_pipes
[1] = -1;
1168 if (!strncmp(job
->printer
->device_uri
, "file:/dev/", 10))
1169 job
->print_pipes
[1] = open(job
->printer
->device_uri
+ 5,
1171 else if (!strncmp(job
->printer
->device_uri
, "file:///dev/", 12))
1172 job
->print_pipes
[1] = open(job
->printer
->device_uri
+ 7,
1174 else if (!strncmp(job
->printer
->device_uri
, "file:///", 8))
1175 job
->print_pipes
[1] = open(job
->printer
->device_uri
+ 7,
1176 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
1178 job
->print_pipes
[1] = open(job
->printer
->device_uri
+ 5,
1179 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
1181 if (job
->print_pipes
[1] < 0)
1183 abort_message
= "Stopping job because the scheduler could not "
1184 "open the output file.";
1189 fcntl(job
->print_pipes
[1], F_SETFD
,
1190 fcntl(job
->print_pipes
[1], F_GETFD
) | FD_CLOEXEC
);
1195 filterfds
[slot
][0] = job
->print_pipes
[0];
1196 filterfds
[slot
][1] = job
->print_pipes
[1];
1199 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
1200 filterfds
[slot
][1], job
->status_pipes
[1],
1201 job
->back_pipes
[0], job
->side_pipes
[0], 0,
1202 job
->profile
, job
, job
->filters
+ i
);
1204 cupsdClosePipe(filterfds
[!slot
]);
1208 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Unable to start filter \"%s\" - %s.",
1209 filter
->filter
, strerror(errno
));
1211 abort_message
= "Stopping job because the scheduler could not execute a "
1217 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Started filter %s (PID %d)", command
,
1229 cupsArrayDelete(filters
);
1233 * Finally, pipe the final output into a backend process if needed...
1236 if (strncmp(job
->printer
->device_uri
, "file:", 5) != 0)
1238 if (job
->current_file
== 1 || job
->printer
->remote
||
1239 (job
->printer
->pc
&& job
->printer
->pc
->single_file
))
1241 sscanf(job
->printer
->device_uri
, "%254[^:]", scheme
);
1242 snprintf(command
, sizeof(command
), "%s/backend/%s", ServerBin
, scheme
);
1245 * See if the backend needs to run as root...
1250 else if (stat(command
, &backinfo
))
1253 backroot
= !(backinfo
.st_mode
& (S_IWGRP
| S_IRWXO
));
1255 argv
[0] = job
->printer
->sanitized_device_uri
;
1257 filterfds
[slot
][0] = -1;
1258 filterfds
[slot
][1] = -1;
1260 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
1261 filterfds
[slot
][1], job
->status_pipes
[1],
1262 job
->back_pipes
[1], job
->side_pipes
[1],
1263 backroot
, job
->bprofile
, job
, &(job
->backend
));
1267 abort_message
= "Stopping job because the sheduler could not execute "
1274 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Started backend %s (PID %d)",
1279 if (job
->current_file
== job
->num_files
||
1280 (job
->printer
->pc
&& job
->printer
->pc
->single_file
))
1281 cupsdClosePipe(job
->print_pipes
);
1283 if (job
->current_file
== job
->num_files
)
1285 cupsdClosePipe(job
->back_pipes
);
1286 cupsdClosePipe(job
->side_pipes
);
1288 close(job
->status_pipes
[1]);
1289 job
->status_pipes
[1] = -1;
1294 filterfds
[slot
][0] = -1;
1295 filterfds
[slot
][1] = -1;
1297 if (job
->current_file
== job
->num_files
||
1298 (job
->printer
->pc
&& job
->printer
->pc
->single_file
))
1299 cupsdClosePipe(job
->print_pipes
);
1301 if (job
->current_file
== job
->num_files
)
1303 close(job
->status_pipes
[1]);
1304 job
->status_pipes
[1] = -1;
1308 cupsdClosePipe(filterfds
[slot
]);
1310 for (i
= 6; i
< argc
; i
++)
1316 if (printer_state_reasons
)
1317 free(printer_state_reasons
);
1319 cupsdAddSelect(job
->status_buffer
->fd
, (cupsd_selfunc_t
)update_job
, NULL
,
1322 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
, "Job #%d started.",
1329 * If we get here, we need to abort the current job and close out all
1330 * files and pipes...
1335 FilterLevel
-= job
->cost
;
1338 for (slot
= 0; slot
< 2; slot
++)
1339 cupsdClosePipe(filterfds
[slot
]);
1341 cupsArrayDelete(filters
);
1345 for (i
= 6; i
< argc
; i
++)
1350 if (printer_state_reasons
)
1351 free(printer_state_reasons
);
1353 cupsdClosePipe(job
->print_pipes
);
1354 cupsdClosePipe(job
->back_pipes
);
1355 cupsdClosePipe(job
->side_pipes
);
1357 cupsdRemoveSelect(job
->status_pipes
[0]);
1358 cupsdClosePipe(job
->status_pipes
);
1359 cupsdStatBufDelete(job
->status_buffer
);
1360 job
->status_buffer
= NULL
;
1363 * Update the printer and job state.
1366 cupsdSetJobState(job
, abort_state
, CUPSD_JOB_DEFAULT
, "%s", abort_message
);
1367 cupsdSetPrinterState(job
->printer
, IPP_PRINTER_IDLE
, 0);
1368 update_job_attrs(job
, 0);
1371 free_job_history(job
);
1373 cupsArrayRemove(PrintingJobs
, job
);
1376 * Clear the printer <-> job association...
1379 job
->printer
->job
= NULL
;
1380 job
->printer
= NULL
;
1385 * 'cupsdDeleteJob()' - Free all memory used by a job.
1389 cupsdDeleteJob(cupsd_job_t
*job
, /* I - Job */
1390 cupsd_jobaction_t action
)/* I - Action */
1392 int i
; /* Looping var */
1396 finalize_job(job
, 1);
1398 if (action
== CUPSD_JOB_PURGE
)
1399 remove_job_history(job
);
1401 cupsdClearString(&job
->username
);
1402 cupsdClearString(&job
->dest
);
1404 i
< (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0]));
1406 cupsdClearString(job
->auth_env
+ i
);
1407 cupsdClearString(&job
->auth_uid
);
1409 if (action
== CUPSD_JOB_PURGE
)
1410 remove_job_files(job
);
1411 else if (job
->num_files
> 0)
1413 free(job
->compressions
);
1414 free(job
->filetypes
);
1420 free_job_history(job
);
1424 cupsArrayRemove(Jobs
, job
);
1425 cupsArrayRemove(ActiveJobs
, job
);
1426 cupsArrayRemove(PrintingJobs
, job
);
1433 * 'cupsdFreeAllJobs()' - Free all jobs from memory.
1437 cupsdFreeAllJobs(void)
1439 cupsd_job_t
*job
; /* Current job */
1447 cupsdStopAllJobs(CUPSD_JOB_FORCE
, 0);
1450 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1452 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1453 cupsdDeleteJob(job
, CUPSD_JOB_DEFAULT
);
1455 cupsdReleaseSignals();
1460 * 'cupsdFindJob()' - Find the specified job.
1463 cupsd_job_t
* /* O - Job data */
1464 cupsdFindJob(int id
) /* I - Job ID */
1466 cupsd_job_t key
; /* Search key */
1471 return ((cupsd_job_t
*)cupsArrayFind(Jobs
, &key
));
1476 * 'cupsdGetCompletedJobs()'- Generate a completed jobs list.
1479 cups_array_t
* /* O - Array of jobs */
1480 cupsdGetCompletedJobs(
1481 cupsd_printer_t
*p
) /* I - Printer */
1483 cups_array_t
*list
; /* Array of jobs */
1484 cupsd_job_t
*job
; /* Current job */
1487 list
= cupsArrayNew(compare_completed_jobs
, NULL
);
1489 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1491 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1492 if ((!p
|| !_cups_strcasecmp(p
->name
, job
->dest
)) && job
->state_value
>= IPP_JOB_STOPPED
&& job
->completed_time
)
1493 cupsArrayAdd(list
, job
);
1500 * 'cupsdGetPrinterJobCount()' - Get the number of pending, processing,
1501 * or held jobs in a printer or class.
1504 int /* O - Job count */
1505 cupsdGetPrinterJobCount(
1506 const char *dest
) /* I - Printer or class name */
1508 int count
; /* Job count */
1509 cupsd_job_t
*job
; /* Current job */
1512 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
1514 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
1515 if (job
->dest
&& !_cups_strcasecmp(job
->dest
, dest
))
1523 * 'cupsdGetUserJobCount()' - Get the number of pending, processing,
1524 * or held jobs for a user.
1527 int /* O - Job count */
1528 cupsdGetUserJobCount(
1529 const char *username
) /* I - Username */
1531 int count
; /* Job count */
1532 cupsd_job_t
*job
; /* Current job */
1535 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
1537 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
1538 if (!_cups_strcasecmp(job
->username
, username
))
1546 * 'cupsdLoadAllJobs()' - Load all jobs from disk.
1550 cupsdLoadAllJobs(void)
1552 char filename
[1024]; /* Full filename of job.cache file */
1553 struct stat fileinfo
; /* Information on job.cache file */
1554 cups_dir_t
*dir
; /* RequestRoot dir */
1555 cups_dentry_t
*dent
; /* Entry in RequestRoot */
1556 int load_cache
= 1; /* Load the job.cache file? */
1560 * Create the job arrays as needed...
1564 Jobs
= cupsArrayNew(compare_jobs
, NULL
);
1567 ActiveJobs
= cupsArrayNew(compare_active_jobs
, NULL
);
1570 PrintingJobs
= cupsArrayNew(compare_jobs
, NULL
);
1573 * See whether the job.cache file is older than the RequestRoot directory...
1576 snprintf(filename
, sizeof(filename
), "%s/job.cache", CacheDir
);
1578 if (stat(filename
, &fileinfo
))
1581 * No job.cache file...
1586 if (errno
!= ENOENT
)
1587 cupsdLogMessage(CUPSD_LOG_ERROR
,
1588 "Unable to get file information for \"%s\" - %s",
1589 filename
, strerror(errno
));
1591 else if ((dir
= cupsDirOpen(RequestRoot
)) == NULL
)
1594 * No spool directory...
1601 while ((dent
= cupsDirRead(dir
)) != NULL
)
1603 if (strlen(dent
->filename
) >= 6 && dent
->filename
[0] == 'c' && dent
->fileinfo
.st_mtime
> fileinfo
.st_mtime
)
1606 * Job history file is newer than job.cache file...
1618 * Load the most recent source for job data...
1624 * Load the job.cache file...
1627 load_job_cache(filename
);
1632 * Load the job history files...
1635 load_request_root();
1637 load_next_job_id(filename
);
1641 * Clean out old jobs as needed...
1644 if (MaxJobs
> 0 && cupsArrayCount(Jobs
) >= MaxJobs
)
1650 * 'cupsdLoadJob()' - Load a single job.
1653 int /* O - 1 on success, 0 on failure */
1654 cupsdLoadJob(cupsd_job_t
*job
) /* I - Job */
1656 int i
; /* Looping var */
1657 char jobfile
[1024]; /* Job filename */
1658 cups_file_t
*fp
; /* Job file */
1659 int fileid
; /* Current file ID */
1660 ipp_attribute_t
*attr
; /* Job attribute */
1661 const char *dest
; /* Destination name */
1662 cupsd_printer_t
*destptr
; /* Pointer to destination */
1663 mime_type_t
**filetypes
; /* New filetypes array */
1664 int *compressions
; /* New compressions array */
1669 if (job
->state_value
> IPP_JOB_STOPPED
)
1670 job
->access_time
= time(NULL
);
1675 if ((job
->attrs
= ippNew()) == NULL
)
1677 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Ran out of memory for job attributes.");
1682 * Load job attributes...
1685 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Loading attributes...");
1687 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, job
->id
);
1688 if ((fp
= cupsdOpenConfFile(jobfile
)) == NULL
)
1691 if (ippReadIO(fp
, (ipp_iocb_t
)cupsFileRead
, 1, NULL
, job
->attrs
) != IPP_DATA
)
1693 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1694 "Unable to read job control file \"%s\".", jobfile
);
1702 * Copy attribute data to the job object...
1705 if (!ippFindAttribute(job
->attrs
, "time-at-creation", IPP_TAG_INTEGER
))
1707 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1708 "Missing or bad time-at-creation attribute in control file.");
1712 if ((job
->state
= ippFindAttribute(job
->attrs
, "job-state",
1713 IPP_TAG_ENUM
)) == NULL
)
1715 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1716 "Missing or bad job-state attribute in control file.");
1720 job
->state_value
= (ipp_jstate_t
)job
->state
->values
[0].integer
;
1722 job
->history_time
= 0;
1724 if ((attr
= ippFindAttribute(job
->attrs
, "time-at-creation", IPP_TAG_INTEGER
)) != NULL
)
1725 job
->creation_time
= attr
->values
[0].integer
;
1727 if (job
->state_value
>= IPP_JOB_CANCELED
&& (attr
= ippFindAttribute(job
->attrs
, "time-at-completed", IPP_TAG_INTEGER
)) != NULL
)
1729 job
->completed_time
= attr
->values
[0].integer
;
1731 if (JobHistory
< INT_MAX
)
1732 job
->history_time
= attr
->values
[0].integer
+ JobHistory
;
1734 job
->history_time
= INT_MAX
;
1736 if (job
->history_time
< time(NULL
))
1737 goto error
; /* Expired, remove from history */
1739 if (job
->history_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
1740 JobHistoryUpdate
= job
->history_time
;
1742 if (JobFiles
< INT_MAX
)
1743 job
->file_time
= attr
->values
[0].integer
+ JobFiles
;
1745 job
->file_time
= INT_MAX
;
1747 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "cupsdLoadJob: job->file_time=%ld, time-at-completed=%ld, JobFiles=%d", (long)job
->file_time
, (long)attr
->values
[0].integer
, JobFiles
);
1749 if (job
->file_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
1750 JobHistoryUpdate
= job
->file_time
;
1752 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdLoadJob: JobHistoryUpdate=%ld",
1753 (long)JobHistoryUpdate
);
1758 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1759 IPP_TAG_URI
)) == NULL
)
1761 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1762 "No job-printer-uri attribute in control file.");
1766 if ((dest
= cupsdValidateDest(attr
->values
[0].string
.text
, &(job
->dtype
),
1769 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1770 "Unable to queue job for destination \"%s\".",
1771 attr
->values
[0].string
.text
);
1775 cupsdSetString(&job
->dest
, dest
);
1777 else if ((destptr
= cupsdFindDest(job
->dest
)) == NULL
)
1779 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1780 "Unable to queue job for destination \"%s\".",
1785 if ((job
->reasons
= ippFindAttribute(job
->attrs
, "job-state-reasons",
1786 IPP_TAG_KEYWORD
)) == NULL
)
1788 const char *reason
; /* job-state-reason keyword */
1790 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
1791 "Adding missing job-state-reasons attribute to control file.");
1793 switch (job
->state_value
)
1796 case IPP_JOB_PENDING
:
1797 if (destptr
->state
== IPP_PRINTER_STOPPED
)
1798 reason
= "printer-stopped";
1804 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1805 IPP_TAG_ZERO
)) != NULL
&&
1806 (attr
->value_tag
== IPP_TAG_NAME
||
1807 attr
->value_tag
== IPP_TAG_NAMELANG
||
1808 attr
->value_tag
== IPP_TAG_KEYWORD
) &&
1809 strcmp(attr
->values
[0].string
.text
, "no-hold"))
1810 reason
= "job-hold-until-specified";
1812 reason
= "job-incoming";
1815 case IPP_JOB_PROCESSING
:
1816 reason
= "job-printing";
1819 case IPP_JOB_STOPPED
:
1820 reason
= "job-stopped";
1823 case IPP_JOB_CANCELED
:
1824 reason
= "job-canceled-by-user";
1827 case IPP_JOB_ABORTED
:
1828 reason
= "aborted-by-system";
1831 case IPP_JOB_COMPLETED
:
1832 reason
= "job-completed-successfully";
1836 job
->reasons
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1837 "job-state-reasons", NULL
, reason
);
1839 else if (job
->state_value
== IPP_JOB_PENDING
)
1841 if (destptr
->state
== IPP_PRINTER_STOPPED
)
1842 ippSetString(job
->attrs
, &job
->reasons
, 0, "printer-stopped");
1844 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
1847 job
->impressions
= ippFindAttribute(job
->attrs
, "job-impressions-completed", IPP_TAG_INTEGER
);
1848 job
->sheets
= ippFindAttribute(job
->attrs
, "job-media-sheets-completed", IPP_TAG_INTEGER
);
1849 job
->job_sheets
= ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_NAME
);
1851 if (!job
->impressions
)
1852 job
->impressions
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-impressions-completed", 0);
1856 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
1857 IPP_TAG_INTEGER
)) == NULL
)
1859 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1860 "Missing or bad job-priority attribute in control file.");
1864 job
->priority
= attr
->values
[0].integer
;
1869 if ((attr
= ippFindAttribute(job
->attrs
, "job-originating-user-name",
1870 IPP_TAG_NAME
)) == NULL
)
1872 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1873 "Missing or bad job-originating-user-name "
1874 "attribute in control file.");
1878 cupsdSetString(&job
->username
, attr
->values
[0].string
.text
);
1883 if ((attr
= ippFindAttribute(job
->attrs
, "job-name", IPP_TAG_NAME
)) != NULL
)
1884 cupsdSetString(&job
->name
, attr
->values
[0].string
.text
);
1888 * Set the job hold-until time and state...
1891 if (job
->state_value
== IPP_JOB_HELD
)
1893 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1894 IPP_TAG_KEYWORD
)) == NULL
)
1895 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1898 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
, CUPSD_JOB_DEFAULT
);
1901 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1902 job
->state_value
= IPP_JOB_PENDING
;
1905 else if (job
->state_value
== IPP_JOB_PROCESSING
)
1907 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1908 job
->state_value
= IPP_JOB_PENDING
;
1911 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets", IPP_TAG_INTEGER
)) != NULL
)
1912 job
->koctets
= attr
->values
[0].integer
;
1914 if (!job
->num_files
)
1917 * Find all the d##### files...
1920 for (fileid
= 1; fileid
< 10000; fileid
++)
1922 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
1925 if (access(jobfile
, 0))
1928 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
1929 "Auto-typing document file \"%s\"...", jobfile
);
1931 if (fileid
> job
->num_files
)
1933 if (job
->num_files
== 0)
1935 compressions
= (int *)calloc((size_t)fileid
, sizeof(int));
1936 filetypes
= (mime_type_t
**)calloc((size_t)fileid
, sizeof(mime_type_t
*));
1940 compressions
= (int *)realloc(job
->compressions
, sizeof(int) * (size_t)fileid
);
1941 filetypes
= (mime_type_t
**)realloc(job
->filetypes
, sizeof(mime_type_t
*) * (size_t)fileid
);
1945 job
->compressions
= compressions
;
1948 job
->filetypes
= filetypes
;
1950 if (!compressions
|| !filetypes
)
1952 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1953 "Ran out of memory for job file types.");
1955 ippDelete(job
->attrs
);
1958 if (job
->compressions
)
1960 free(job
->compressions
);
1961 job
->compressions
= NULL
;
1966 free(job
->filetypes
);
1967 job
->filetypes
= NULL
;
1974 job
->num_files
= fileid
;
1977 job
->filetypes
[fileid
- 1] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
1978 job
->compressions
+ fileid
- 1);
1980 if (!job
->filetypes
[fileid
- 1])
1981 job
->filetypes
[fileid
- 1] = mimeType(MimeDatabase
, "application",
1987 * Load authentication information as needed...
1990 if (job
->state_value
< IPP_JOB_STOPPED
)
1992 snprintf(jobfile
, sizeof(jobfile
), "%s/a%05d", RequestRoot
, job
->id
);
1995 i
< (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0]));
1997 cupsdClearString(job
->auth_env
+ i
);
1998 cupsdClearString(&job
->auth_uid
);
2000 if ((fp
= cupsFileOpen(jobfile
, "r")) != NULL
)
2002 int bytes
, /* Size of auth data */
2003 linenum
= 1; /* Current line number */
2004 char line
[65536], /* Line from file */
2005 *value
, /* Value from line */
2006 data
[65536]; /* Decoded data */
2009 if (cupsFileGets(fp
, line
, sizeof(line
)) &&
2010 !strcmp(line
, "CUPSD-AUTH-V3"))
2013 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
2019 if (strcmp(line
, "negotiate") && strcmp(line
, "uid"))
2021 bytes
= sizeof(data
);
2022 httpDecode64_2(data
, &bytes
, value
);
2026 * Assign environment variables...
2029 if (!strcmp(line
, "uid"))
2031 cupsdSetStringf(&job
->auth_uid
, "AUTH_UID=%s", value
);
2034 else if (i
>= (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0])))
2037 if (!strcmp(line
, "username"))
2038 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_USERNAME=%s", data
);
2039 else if (!strcmp(line
, "domain"))
2040 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_DOMAIN=%s", data
);
2041 else if (!strcmp(line
, "password"))
2042 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_PASSWORD=%s", data
);
2043 else if (!strcmp(line
, "negotiate"))
2044 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_NEGOTIATE=%s", value
);
2056 job
->access_time
= time(NULL
);
2060 * If we get here then something bad happened...
2065 ippDelete(job
->attrs
);
2068 remove_job_history(job
);
2069 remove_job_files(job
);
2076 * 'cupsdMoveJob()' - Move the specified job to a different destination.
2080 cupsdMoveJob(cupsd_job_t
*job
, /* I - Job */
2081 cupsd_printer_t
*p
) /* I - Destination printer or class */
2083 ipp_attribute_t
*attr
; /* job-printer-uri attribute */
2084 const char *olddest
; /* Old destination */
2085 cupsd_printer_t
*oldp
; /* Old pointer */
2089 * Don't move completed jobs...
2092 if (job
->state_value
> IPP_JOB_STOPPED
)
2096 * Get the old destination...
2099 olddest
= job
->dest
;
2102 oldp
= job
->printer
;
2104 oldp
= cupsdFindDest(olddest
);
2107 * Change the destination information...
2110 if (job
->state_value
> IPP_JOB_HELD
)
2111 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
2112 "Stopping job prior to move.");
2114 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, oldp
, job
,
2115 "Job #%d moved from %s to %s.", job
->id
, olddest
,
2118 cupsdSetString(&job
->dest
, p
->name
);
2119 job
->dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_REMOTE
);
2121 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
2122 IPP_TAG_URI
)) != NULL
)
2123 ippSetString(job
->attrs
, &attr
, 0, p
->uri
);
2125 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, p
, job
,
2126 "Job #%d moved from %s to %s.", job
->id
, olddest
,
2130 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2135 * 'cupsdReleaseJob()' - Release the specified job.
2139 cupsdReleaseJob(cupsd_job_t
*job
) /* I - Job */
2141 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdReleaseJob(job=%p(%d))", job
,
2144 if (job
->state_value
== IPP_JOB_HELD
)
2147 * Add trailing banner as needed...
2150 if (job
->pending_timeout
)
2151 cupsdTimeoutJob(job
);
2153 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
2154 "Job released by user.");
2160 * 'cupsdRestartJob()' - Restart the specified job.
2164 cupsdRestartJob(cupsd_job_t
*job
) /* I - Job */
2166 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdRestartJob(job=%p(%d))", job
,
2169 if (job
->state_value
== IPP_JOB_STOPPED
|| job
->num_files
)
2170 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
2171 "Job restarted by user.");
2176 * 'cupsdSaveAllJobs()' - Save a summary of all jobs to disk.
2180 cupsdSaveAllJobs(void)
2182 int i
; /* Looping var */
2183 cups_file_t
*fp
; /* job.cache file */
2184 char filename
[1024], /* job.cache filename */
2185 temp
[1024]; /* Temporary string */
2186 cupsd_job_t
*job
; /* Current job */
2187 time_t curtime
; /* Current time */
2188 struct tm
*curdate
; /* Current date */
2191 snprintf(filename
, sizeof(filename
), "%s/job.cache", CacheDir
);
2192 if ((fp
= cupsdCreateConfFile(filename
, ConfigFilePerm
)) == NULL
)
2195 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving job.cache...");
2198 * Write a small header to the file...
2201 curtime
= time(NULL
);
2202 curdate
= localtime(&curtime
);
2203 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
2205 cupsFilePuts(fp
, "# Job cache file for " CUPS_SVERSION
"\n");
2206 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
2207 cupsFilePrintf(fp
, "NextJobId %d\n", NextJobId
);
2210 * Write each job known to the system...
2213 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
2215 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
2217 if (job
->printer
&& job
->printer
->temporary
)
2220 * Don't save jobs on temporary printers...
2226 cupsFilePrintf(fp
, "<Job %d>\n", job
->id
);
2227 cupsFilePrintf(fp
, "State %d\n", job
->state_value
);
2228 cupsFilePrintf(fp
, "Created %ld\n", (long)job
->creation_time
);
2229 if (job
->completed_time
)
2230 cupsFilePrintf(fp
, "Completed %ld\n", (long)job
->completed_time
);
2231 cupsFilePrintf(fp
, "Priority %d\n", job
->priority
);
2232 if (job
->hold_until
)
2233 cupsFilePrintf(fp
, "HoldUntil %ld\n", (long)job
->hold_until
);
2234 cupsFilePrintf(fp
, "Username %s\n", job
->username
);
2236 cupsFilePutConf(fp
, "Name", job
->name
);
2237 cupsFilePrintf(fp
, "Destination %s\n", job
->dest
);
2238 cupsFilePrintf(fp
, "DestType %d\n", job
->dtype
);
2239 cupsFilePrintf(fp
, "KOctets %d\n", job
->koctets
);
2240 cupsFilePrintf(fp
, "NumFiles %d\n", job
->num_files
);
2241 for (i
= 0; i
< job
->num_files
; i
++)
2242 cupsFilePrintf(fp
, "File %d %s/%s %d\n", i
+ 1, job
->filetypes
[i
]->super
,
2243 job
->filetypes
[i
]->type
, job
->compressions
[i
]);
2244 cupsFilePuts(fp
, "</Job>\n");
2247 cupsdCloseCreatedConfFile(fp
, filename
);
2252 * 'cupsdSaveJob()' - Save a job to disk.
2256 cupsdSaveJob(cupsd_job_t
*job
) /* I - Job */
2258 char filename
[1024]; /* Job control filename */
2259 cups_file_t
*fp
; /* Job file */
2262 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSaveJob(job=%p(%d)): job->attrs=%p",
2263 job
, job
->id
, job
->attrs
);
2265 if (job
->printer
&& job
->printer
->temporary
)
2268 * Don't save jobs on temporary printers...
2275 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
, job
->id
);
2277 if ((fp
= cupsdCreateConfFile(filename
, ConfigFilePerm
& 0600)) == NULL
)
2280 fchown(cupsFileNumber(fp
), RunUser
, Group
);
2282 job
->attrs
->state
= IPP_IDLE
;
2284 if (ippWriteIO(fp
, (ipp_iocb_t
)cupsFileWrite
, 1, NULL
,
2285 job
->attrs
) != IPP_DATA
)
2287 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Unable to write job control file.");
2292 if (!cupsdCloseCreatedConfFile(fp
, filename
))
2295 * Remove backup file and mark this job as clean...
2298 strlcat(filename
, ".O", sizeof(filename
));
2307 * 'cupsdSetJobHoldUntil()' - Set the hold time for a job.
2311 cupsdSetJobHoldUntil(cupsd_job_t
*job
, /* I - Job */
2312 const char *when
, /* I - When to resume */
2313 int update
)/* I - Update job-hold-until attr? */
2315 time_t curtime
; /* Current time */
2316 struct tm
*curdate
; /* Current date */
2317 int hour
; /* Hold hour */
2318 int minute
; /* Hold minute */
2319 int second
= 0; /* Hold second */
2322 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2323 "cupsdSetJobHoldUntil(job=%p(%d), when=\"%s\", update=%d)",
2324 job
, job
->id
, when
, update
);
2329 * Update the job-hold-until attribute...
2332 ipp_attribute_t
*attr
; /* job-hold-until attribute */
2334 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2335 IPP_TAG_KEYWORD
)) == NULL
)
2336 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2339 ippSetString(job
->attrs
, &attr
, 0, when
);
2341 attr
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
2342 "job-hold-until", NULL
, when
);
2346 if (isdigit(when
[0] & 255))
2347 attr
->value_tag
= IPP_TAG_NAME
;
2349 attr
->value_tag
= IPP_TAG_KEYWORD
;
2352 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2357 if (strcmp(when
, "no-hold"))
2358 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-hold-until-specified");
2360 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
2363 * Update the hold time...
2366 job
->cancel_time
= 0;
2368 if (!strcmp(when
, "indefinite") || !strcmp(when
, "auth-info-required"))
2371 * Hold indefinitely...
2374 job
->hold_until
= 0;
2376 if (MaxHoldTime
> 0)
2377 job
->cancel_time
= time(NULL
) + MaxHoldTime
;
2379 else if (!strcmp(when
, "day-time"))
2382 * Hold to 6am the next morning unless local time is < 6pm.
2385 curtime
= time(NULL
);
2386 curdate
= localtime(&curtime
);
2388 if (curdate
->tm_hour
< 18)
2389 job
->hold_until
= curtime
;
2391 job
->hold_until
= curtime
+
2392 ((29 - curdate
->tm_hour
) * 60 + 59 -
2393 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2395 else if (!strcmp(when
, "evening") || !strcmp(when
, "night"))
2398 * Hold to 6pm unless local time is > 6pm or < 6am.
2401 curtime
= time(NULL
);
2402 curdate
= localtime(&curtime
);
2404 if (curdate
->tm_hour
< 6 || curdate
->tm_hour
>= 18)
2405 job
->hold_until
= curtime
;
2407 job
->hold_until
= curtime
+
2408 ((17 - curdate
->tm_hour
) * 60 + 59 -
2409 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2411 else if (!strcmp(when
, "second-shift"))
2414 * Hold to 4pm unless local time is > 4pm.
2417 curtime
= time(NULL
);
2418 curdate
= localtime(&curtime
);
2420 if (curdate
->tm_hour
>= 16)
2421 job
->hold_until
= curtime
;
2423 job
->hold_until
= curtime
+
2424 ((15 - curdate
->tm_hour
) * 60 + 59 -
2425 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2427 else if (!strcmp(when
, "third-shift"))
2430 * Hold to 12am unless local time is < 8am.
2433 curtime
= time(NULL
);
2434 curdate
= localtime(&curtime
);
2436 if (curdate
->tm_hour
< 8)
2437 job
->hold_until
= curtime
;
2439 job
->hold_until
= curtime
+
2440 ((23 - curdate
->tm_hour
) * 60 + 59 -
2441 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2443 else if (!strcmp(when
, "weekend"))
2446 * Hold to weekend unless we are in the weekend.
2449 curtime
= time(NULL
);
2450 curdate
= localtime(&curtime
);
2452 if (curdate
->tm_wday
== 0 || curdate
->tm_wday
== 6)
2453 job
->hold_until
= curtime
;
2455 job
->hold_until
= curtime
+
2456 (((5 - curdate
->tm_wday
) * 24 +
2457 (17 - curdate
->tm_hour
)) * 60 + 59 -
2458 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2460 else if (sscanf(when
, "%d:%d:%d", &hour
, &minute
, &second
) >= 2)
2463 * Hold to specified GMT time (HH:MM or HH:MM:SS)...
2466 curtime
= time(NULL
);
2467 curdate
= gmtime(&curtime
);
2469 job
->hold_until
= curtime
+
2470 ((hour
- curdate
->tm_hour
) * 60 + minute
-
2471 curdate
->tm_min
) * 60 + second
- curdate
->tm_sec
;
2474 * Hold until next day as needed...
2477 if (job
->hold_until
< curtime
)
2478 job
->hold_until
+= 24 * 60 * 60;
2481 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetJobHoldUntil: hold_until=%d",
2482 (int)job
->hold_until
);
2487 * 'cupsdSetJobPriority()' - Set the priority of a job, moving it up/down in
2488 * the list as needed.
2492 cupsdSetJobPriority(
2493 cupsd_job_t
*job
, /* I - Job ID */
2494 int priority
) /* I - New priority (0 to 100) */
2496 ipp_attribute_t
*attr
; /* Job attribute */
2500 * Don't change completed jobs...
2503 if (job
->state_value
>= IPP_JOB_PROCESSING
)
2507 * Set the new priority and re-add the job into the active list...
2510 cupsArrayRemove(ActiveJobs
, job
);
2512 job
->priority
= priority
;
2514 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
2515 IPP_TAG_INTEGER
)) != NULL
)
2516 attr
->values
[0].integer
= priority
;
2518 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-priority",
2521 cupsArrayAdd(ActiveJobs
, job
);
2524 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2529 * 'cupsdSetJobState()' - Set the state of the specified print job.
2534 cupsd_job_t
*job
, /* I - Job to cancel */
2535 ipp_jstate_t newstate
, /* I - New job state */
2536 cupsd_jobaction_t action
, /* I - Action to take */
2537 const char *message
, /* I - Message to log */
2538 ...) /* I - Additional arguments as needed */
2540 int i
; /* Looping var */
2541 ipp_jstate_t oldstate
; /* Old state */
2542 char filename
[1024]; /* Job filename */
2543 ipp_attribute_t
*attr
; /* Job attribute */
2546 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2547 "cupsdSetJobState(job=%p(%d), state=%d, newstate=%d, "
2548 "action=%d, message=\"%s\")", job
, job
->id
, job
->state_value
,
2549 newstate
, action
, message
? message
: "(null)");
2553 * Make sure we have the job attributes...
2556 if (!cupsdLoadJob(job
))
2560 * Don't do anything if the state is unchanged and we aren't purging the
2564 oldstate
= job
->state_value
;
2565 if (newstate
== oldstate
&& action
!= CUPSD_JOB_PURGE
)
2569 * Stop any processes that are working on the current job...
2572 if (oldstate
== IPP_JOB_PROCESSING
)
2573 stop_job(job
, action
);
2576 * Set the new job state...
2579 job
->state_value
= newstate
;
2582 job
->state
->values
[0].integer
= newstate
;
2586 case IPP_JOB_PENDING
:
2588 * Update job-hold-until as needed...
2591 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2592 IPP_TAG_KEYWORD
)) == NULL
)
2593 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2597 ippSetValueTag(job
->attrs
, &attr
, IPP_TAG_KEYWORD
);
2598 ippSetString(job
->attrs
, &attr
, 0, "no-hold");
2604 case IPP_JOB_ABORTED
:
2605 case IPP_JOB_CANCELED
:
2606 case IPP_JOB_COMPLETED
:
2607 set_time(job
, "time-at-completed");
2608 ippSetString(job
->attrs
, &job
->reasons
, 0, "processing-to-stop-point");
2613 * Log message as needed...
2618 char buffer
[2048]; /* Message buffer */
2619 va_list ap
; /* Pointer to additional arguments */
2621 va_start(ap
, message
);
2622 vsnprintf(buffer
, sizeof(buffer
), message
, ap
);
2625 if (newstate
> IPP_JOB_STOPPED
)
2626 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
, "%s", buffer
);
2628 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
, "%s", buffer
);
2630 if (newstate
== IPP_JOB_STOPPED
|| newstate
== IPP_JOB_ABORTED
)
2631 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "%s", buffer
);
2633 cupsdLogJob(job
, CUPSD_LOG_INFO
, "%s", buffer
);
2637 * Handle post-state-change actions...
2642 case IPP_JOB_PROCESSING
:
2644 * Add the job to the "printing" list...
2647 if (!cupsArrayFind(PrintingJobs
, job
))
2648 cupsArrayAdd(PrintingJobs
, job
);
2651 * Set the processing time...
2654 set_time(job
, "time-at-processing");
2656 case IPP_JOB_PENDING
:
2658 case IPP_JOB_STOPPED
:
2660 * Make sure the job is in the active list...
2663 if (!cupsArrayFind(ActiveJobs
, job
))
2664 cupsArrayAdd(ActiveJobs
, job
);
2667 * Save the job state to disk...
2671 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2674 case IPP_JOB_ABORTED
:
2675 case IPP_JOB_CANCELED
:
2676 case IPP_JOB_COMPLETED
:
2677 if (newstate
== IPP_JOB_CANCELED
)
2680 * Remove the job from the active list if there are no processes still
2684 for (i
= 0; job
->filters
[i
] < 0; i
++);
2686 if (!job
->filters
[i
] && job
->backend
<= 0)
2687 cupsArrayRemove(ActiveJobs
, job
);
2692 * Otherwise just remove the job from the active list immediately...
2695 cupsArrayRemove(ActiveJobs
, job
);
2699 * Expire job subscriptions since the job is now "completed"...
2702 cupsdExpireSubscriptions(NULL
, job
);
2706 * If we are going to sleep and the PrintingJobs count is now 0, allow the
2707 * sleep to happen immediately...
2710 if (Sleeping
&& cupsArrayCount(PrintingJobs
) == 0)
2712 #endif /* __APPLE__ */
2715 * Remove any authentication data...
2718 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
2719 if (cupsdRemoveFile(filename
) && errno
!= ENOENT
)
2720 cupsdLogMessage(CUPSD_LOG_ERROR
,
2721 "Unable to remove authentication cache: %s",
2725 i
< (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0]));
2727 cupsdClearString(job
->auth_env
+ i
);
2729 cupsdClearString(&job
->auth_uid
);
2732 * Remove the print file for good if we aren't preserving jobs or
2736 if (!JobHistory
|| !JobFiles
|| action
== CUPSD_JOB_PURGE
)
2737 remove_job_files(job
);
2739 if (JobHistory
&& action
!= CUPSD_JOB_PURGE
)
2742 * Save job state info...
2746 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2748 else if (!job
->printer
)
2751 * Delete the job immediately if not actively printing...
2754 cupsdDeleteJob(job
, CUPSD_JOB_PURGE
);
2761 * Finalize the job immediately if we forced things...
2764 if (action
>= CUPSD_JOB_FORCE
&& job
&& job
->printer
)
2765 finalize_job(job
, 0);
2768 * Update the server "busy" state...
2771 cupsdSetBusyState();
2776 * 'cupsdStopAllJobs()' - Stop all print jobs.
2781 cupsd_jobaction_t action
, /* I - Action */
2782 int kill_delay
) /* I - Number of seconds before we kill */
2784 cupsd_job_t
*job
; /* Current job */
2787 DEBUG_puts("cupsdStopAllJobs()");
2789 for (job
= (cupsd_job_t
*)cupsArrayFirst(PrintingJobs
);
2791 job
= (cupsd_job_t
*)cupsArrayNext(PrintingJobs
))
2795 cupsdSetJobState(job
, IPP_JOB_COMPLETED
, CUPSD_JOB_FORCE
, NULL
);
2800 job
->kill_time
= time(NULL
) + kill_delay
;
2802 cupsdSetJobState(job
, IPP_JOB_PENDING
, action
, NULL
);
2809 * 'cupsdUnloadCompletedJobs()' - Flush completed job history from memory.
2813 cupsdUnloadCompletedJobs(void)
2815 cupsd_job_t
*job
; /* Current job */
2816 time_t expire
; /* Expiration time */
2819 expire
= time(NULL
) - 60;
2821 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
2823 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
2824 if (job
->attrs
&& job
->state_value
>= IPP_JOB_STOPPED
&& !job
->printer
&&
2825 job
->access_time
< expire
)
2837 * 'cupsdUpdateJobs()' - Update the history/file files for all jobs.
2841 cupsdUpdateJobs(void)
2843 cupsd_job_t
*job
; /* Current job */
2844 time_t curtime
; /* Current time */
2845 ipp_attribute_t
*attr
; /* time-at-completed attribute */
2848 curtime
= time(NULL
);
2849 JobHistoryUpdate
= 0;
2851 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
2853 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
2855 if (job
->state_value
>= IPP_JOB_CANCELED
&&
2856 (attr
= ippFindAttribute(job
->attrs
, "time-at-completed",
2857 IPP_TAG_INTEGER
)) != NULL
)
2860 * Update history/file expiration times...
2863 if (JobHistory
< INT_MAX
)
2864 job
->history_time
= attr
->values
[0].integer
+ JobHistory
;
2866 job
->history_time
= INT_MAX
;
2868 if (job
->history_time
< curtime
)
2870 cupsdDeleteJob(job
, CUPSD_JOB_PURGE
);
2874 if (job
->history_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
2875 JobHistoryUpdate
= job
->history_time
;
2877 if (JobFiles
< INT_MAX
)
2878 job
->file_time
= attr
->values
[0].integer
+ JobFiles
;
2880 job
->file_time
= INT_MAX
;
2882 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "cupsdUpdateJobs: job->file_time=%ld, time-at-completed=%ld, JobFiles=%d", (long)job
->file_time
, (long)attr
->values
[0].integer
, JobFiles
);
2884 if (job
->file_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
2885 JobHistoryUpdate
= job
->file_time
;
2889 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdUpdateJobs: JobHistoryUpdate=%ld",
2890 (long)JobHistoryUpdate
);
2895 * 'compare_active_jobs()' - Compare the job IDs and priorities of two jobs.
2898 static int /* O - Difference */
2899 compare_active_jobs(void *first
, /* I - First job */
2900 void *second
, /* I - Second job */
2901 void *data
) /* I - App data (not used) */
2903 int diff
; /* Difference */
2908 if ((diff
= ((cupsd_job_t
*)second
)->priority
-
2909 ((cupsd_job_t
*)first
)->priority
) != 0)
2912 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
2917 * 'compare_completed_jobs()' - Compare the job IDs and completion times of two jobs.
2920 static int /* O - Difference */
2921 compare_completed_jobs(void *first
, /* I - First job */
2922 void *second
, /* I - Second job */
2923 void *data
) /* I - App data (not used) */
2925 int diff
; /* Difference */
2930 if ((diff
= ((cupsd_job_t
*)second
)->completed_time
-
2931 ((cupsd_job_t
*)first
)->completed_time
) != 0)
2934 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
2939 * 'compare_jobs()' - Compare the job IDs of two jobs.
2942 static int /* O - Difference */
2943 compare_jobs(void *first
, /* I - First job */
2944 void *second
, /* I - Second job */
2945 void *data
) /* I - App data (not used) */
2949 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
2954 * 'dump_job_history()' - Dump any debug messages for a job.
2958 dump_job_history(cupsd_job_t
*job
) /* I - Job */
2960 int i
, /* Looping var */
2961 oldsize
; /* Current MaxLogSize */
2962 struct tm
*date
; /* Date/time value */
2963 cupsd_joblog_t
*message
; /* Current message */
2964 char temp
[2048], /* Log message */
2965 *ptr
, /* Pointer into log message */
2966 start
[256], /* Start time */
2967 end
[256]; /* End time */
2968 cupsd_printer_t
*printer
; /* Printer for job */
2972 * See if we have anything to dump...
2979 * Disable log rotation temporarily...
2982 oldsize
= MaxLogSize
;
2986 * Copy the debug messages to the log...
2989 message
= (cupsd_joblog_t
*)cupsArrayFirst(job
->history
);
2990 date
= localtime(&(message
->time
));
2991 strftime(start
, sizeof(start
), "%X", date
);
2993 message
= (cupsd_joblog_t
*)cupsArrayLast(job
->history
);
2994 date
= localtime(&(message
->time
));
2995 strftime(end
, sizeof(end
), "%X", date
);
2997 snprintf(temp
, sizeof(temp
),
2998 "[Job %d] The following messages were recorded from %s to %s",
2999 job
->id
, start
, end
);
3000 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, temp
);
3002 for (message
= (cupsd_joblog_t
*)cupsArrayFirst(job
->history
);
3004 message
= (cupsd_joblog_t
*)cupsArrayNext(job
->history
))
3005 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, message
->message
);
3007 snprintf(temp
, sizeof(temp
), "[Job %d] End of messages", job
->id
);
3008 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, temp
);
3011 * Log the printer state values...
3014 if ((printer
= job
->printer
) == NULL
)
3015 printer
= cupsdFindDest(job
->dest
);
3019 snprintf(temp
, sizeof(temp
), "[Job %d] printer-state=%d(%s)", job
->id
,
3021 printer
->state
== IPP_PRINTER_IDLE
? "idle" :
3022 printer
->state
== IPP_PRINTER_PROCESSING
? "processing" :
3024 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, temp
);
3026 snprintf(temp
, sizeof(temp
), "[Job %d] printer-state-message=\"%s\"",
3027 job
->id
, printer
->state_message
);
3028 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, temp
);
3030 snprintf(temp
, sizeof(temp
), "[Job %d] printer-state-reasons=", job
->id
);
3031 ptr
= temp
+ strlen(temp
);
3032 if (printer
->num_reasons
== 0)
3033 strlcpy(ptr
, "none", sizeof(temp
) - (size_t)(ptr
- temp
));
3037 i
< printer
->num_reasons
&& ptr
< (temp
+ sizeof(temp
) - 2);
3043 strlcpy(ptr
, printer
->reasons
[i
], sizeof(temp
) - (size_t)(ptr
- temp
));
3047 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, temp
);
3051 * Restore log file rotation...
3054 MaxLogSize
= oldsize
;
3057 * Free all messages...
3060 free_job_history(job
);
3065 * 'free_job_history()' - Free any log history.
3069 free_job_history(cupsd_job_t
*job
) /* I - Job */
3071 char *message
; /* Current message */
3077 for (message
= (char *)cupsArrayFirst(job
->history
);
3079 message
= (char *)cupsArrayNext(job
->history
))
3082 cupsArrayDelete(job
->history
);
3083 job
->history
= NULL
;
3088 * 'finalize_job()' - Cleanup after job filter processes and support data.
3092 finalize_job(cupsd_job_t
*job
, /* I - Job */
3093 int set_job_state
) /* I - 1 = set the job state */
3095 ipp_pstate_t printer_state
; /* New printer state value */
3096 ipp_jstate_t job_state
; /* New job state value */
3097 const char *message
; /* Message for job state */
3098 char buffer
[1024]; /* Buffer for formatted messages */
3101 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "finalize_job(job=%p(%d))", job
, job
->id
);
3104 * Clear the "connecting-to-device" and "cups-waiting-for-job-completed"
3105 * reasons, which are only valid when a printer is processing, along with any
3106 * remote printing job state...
3109 cupsdSetPrinterReasons(job
->printer
, "-connecting-to-device,"
3110 "cups-waiting-for-job-completed,"
3111 "cups-remote-pending,"
3112 "cups-remote-pending-held,"
3113 "cups-remote-processing,"
3114 "cups-remote-stopped,"
3115 "cups-remote-canceled,"
3116 "cups-remote-aborted,"
3117 "cups-remote-completed");
3120 * Similarly, clear the "offline-report" reason for non-USB devices since we
3121 * rarely have current information for network devices...
3124 if (strncmp(job
->printer
->device_uri
, "usb:", 4) &&
3125 strncmp(job
->printer
->device_uri
, "ippusb:", 7))
3126 cupsdSetPrinterReasons(job
->printer
, "-offline-report");
3129 * Free the security profile...
3132 cupsdDestroyProfile(job
->profile
);
3133 job
->profile
= NULL
;
3134 cupsdDestroyProfile(job
->bprofile
);
3135 job
->bprofile
= NULL
;
3138 * Clear the unresponsive job watchdog timers...
3141 job
->cancel_time
= 0;
3145 * Close pipes and status buffer...
3148 cupsdClosePipe(job
->print_pipes
);
3149 cupsdClosePipe(job
->back_pipes
);
3150 cupsdClosePipe(job
->side_pipes
);
3152 cupsdRemoveSelect(job
->status_pipes
[0]);
3153 cupsdClosePipe(job
->status_pipes
);
3154 cupsdStatBufDelete(job
->status_buffer
);
3155 job
->status_buffer
= NULL
;
3158 * Process the exit status...
3161 if (job
->printer
->state
== IPP_PRINTER_PROCESSING
)
3162 printer_state
= IPP_PRINTER_IDLE
;
3164 printer_state
= job
->printer
->state
;
3166 switch (job_state
= job
->state_value
)
3168 case IPP_JOB_PENDING
:
3169 message
= "Job paused.";
3173 message
= "Job held.";
3177 case IPP_JOB_PROCESSING
:
3178 case IPP_JOB_COMPLETED
:
3179 job_state
= IPP_JOB_COMPLETED
;
3180 message
= "Job completed.";
3183 ippSetString(job
->attrs
, &job
->reasons
, 0,
3184 "job-completed-successfully");
3187 case IPP_JOB_STOPPED
:
3188 message
= "Job stopped.";
3190 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-stopped");
3193 case IPP_JOB_CANCELED
:
3194 message
= "Job canceled.";
3196 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-canceled-by-user");
3199 case IPP_JOB_ABORTED
:
3200 message
= "Job aborted.";
3204 if (job
->status
< 0)
3207 * Backend had errors...
3210 int exit_code
; /* Exit code from backend */
3213 * Convert the status to an exit code. Due to the way the W* macros are
3214 * implemented on macOS (bug?), we have to store the exit status in a
3215 * variable first and then convert...
3218 exit_code
= -job
->status
;
3219 if (WIFEXITED(exit_code
))
3220 exit_code
= WEXITSTATUS(exit_code
);
3223 ippSetString(job
->attrs
, &job
->reasons
, 0, "cups-backend-crashed");
3224 exit_code
= job
->status
;
3227 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Backend returned status %d (%s)",
3229 exit_code
== CUPS_BACKEND_FAILED
? "failed" :
3230 exit_code
== CUPS_BACKEND_AUTH_REQUIRED
?
3231 "authentication required" :
3232 exit_code
== CUPS_BACKEND_HOLD
? "hold job" :
3233 exit_code
== CUPS_BACKEND_STOP
? "stop printer" :
3234 exit_code
== CUPS_BACKEND_CANCEL
? "cancel job" :
3235 exit_code
== CUPS_BACKEND_RETRY
? "retry job later" :
3236 exit_code
== CUPS_BACKEND_RETRY_CURRENT
? "retry job immediately" :
3237 exit_code
< 0 ? "crashed" : "unknown");
3240 * Do what needs to be done...
3246 case CUPS_BACKEND_FAILED
:
3248 * Backend failure, use the error-policy to determine how to
3252 if (job
->dtype
& CUPS_PRINTER_CLASS
)
3255 * Queued on a class - mark the job as pending and we'll retry on
3256 * another printer...
3259 if (job_state
== IPP_JOB_COMPLETED
)
3261 job_state
= IPP_JOB_PENDING
;
3262 message
= "Retrying job on another printer.";
3264 ippSetString(job
->attrs
, &job
->reasons
, 0,
3265 "resources-are-not-ready");
3268 else if (!strcmp(job
->printer
->error_policy
, "retry-current-job"))
3271 * The error policy is "retry-current-job" - mark the job as pending
3272 * and we'll retry on the same printer...
3275 if (job_state
== IPP_JOB_COMPLETED
)
3277 job_state
= IPP_JOB_PENDING
;
3278 message
= "Retrying job on same printer.";
3280 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
3283 else if ((job
->printer
->type
& CUPS_PRINTER_FAX
) ||
3284 !strcmp(job
->printer
->error_policy
, "retry-job"))
3286 if (job_state
== IPP_JOB_COMPLETED
)
3289 * The job was queued on a fax or the error policy is "retry-job" -
3290 * hold the job if the number of retries is less than the
3291 * JobRetryLimit, otherwise abort the job.
3296 if (job
->tries
> JobRetryLimit
&& JobRetryLimit
> 0)
3302 snprintf(buffer
, sizeof(buffer
),
3303 "Job aborted after %d unsuccessful attempts.",
3305 job_state
= IPP_JOB_ABORTED
;
3308 ippSetString(job
->attrs
, &job
->reasons
, 0, "aborted-by-system");
3313 * Try again in N seconds...
3316 snprintf(buffer
, sizeof(buffer
),
3317 "Job held for %d seconds since it could not be sent.",
3320 job
->hold_until
= time(NULL
) + JobRetryInterval
;
3321 job_state
= IPP_JOB_HELD
;
3324 ippSetString(job
->attrs
, &job
->reasons
, 0,
3325 "resources-are-not-ready");
3329 else if (!strcmp(job
->printer
->error_policy
, "abort-job") &&
3330 job_state
== IPP_JOB_COMPLETED
)
3332 job_state
= IPP_JOB_ABORTED
;
3333 message
= "Job aborted due to backend errors; please consult "
3334 "the error_log file for details.";
3336 ippSetString(job
->attrs
, &job
->reasons
, 0, "aborted-by-system");
3338 else if (job
->state_value
== IPP_JOB_PROCESSING
)
3340 job_state
= IPP_JOB_PENDING
;
3341 printer_state
= IPP_PRINTER_STOPPED
;
3342 message
= "Printer stopped due to backend errors; please "
3343 "consult the error_log file for details.";
3345 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
3349 case CUPS_BACKEND_CANCEL
:
3354 if (job_state
== IPP_JOB_COMPLETED
)
3356 job_state
= IPP_JOB_CANCELED
;
3357 message
= "Job canceled at printer.";
3359 ippSetString(job
->attrs
, &job
->reasons
, 0, "canceled-at-device");
3363 case CUPS_BACKEND_HOLD
:
3364 if (job_state
== IPP_JOB_COMPLETED
)
3370 const char *reason
= ippGetString(job
->reasons
, 0, NULL
);
3372 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "job-state-reasons=\"%s\"",
3375 if (!reason
|| strncmp(reason
, "account-", 8))
3377 cupsdSetJobHoldUntil(job
, "indefinite", 1);
3379 ippSetString(job
->attrs
, &job
->reasons
, 0,
3380 "job-hold-until-specified");
3381 message
= "Job held indefinitely due to backend errors; please "
3382 "consult the error_log file for details.";
3384 else if (!strcmp(reason
, "account-info-needed"))
3386 cupsdSetJobHoldUntil(job
, "indefinite", 0);
3388 message
= "Job held indefinitely - account information is "
3391 else if (!strcmp(reason
, "account-closed"))
3393 cupsdSetJobHoldUntil(job
, "indefinite", 0);
3395 message
= "Job held indefinitely - account has been closed.";
3397 else if (!strcmp(reason
, "account-limit-reached"))
3399 cupsdSetJobHoldUntil(job
, "indefinite", 0);
3401 message
= "Job held indefinitely - account limit has been "
3406 cupsdSetJobHoldUntil(job
, "indefinite", 0);
3408 message
= "Job held indefinitely - account authorization failed.";
3411 job_state
= IPP_JOB_HELD
;
3415 case CUPS_BACKEND_STOP
:
3417 * Stop the printer...
3420 printer_state
= IPP_PRINTER_STOPPED
;
3421 message
= "Printer stopped due to backend errors; please "
3422 "consult the error_log file for details.";
3424 if (job_state
== IPP_JOB_COMPLETED
)
3426 job_state
= IPP_JOB_PENDING
;
3428 ippSetString(job
->attrs
, &job
->reasons
, 0,
3429 "resources-are-not-ready");
3433 case CUPS_BACKEND_AUTH_REQUIRED
:
3435 * Hold the job for authentication...
3438 if (job_state
== IPP_JOB_COMPLETED
)
3440 cupsdSetJobHoldUntil(job
, "auth-info-required", 1);
3442 job_state
= IPP_JOB_HELD
;
3443 message
= "Job held for authentication.";
3445 if (strncmp(job
->reasons
->values
[0].string
.text
, "account-", 8))
3446 ippSetString(job
->attrs
, &job
->reasons
, 0,
3447 "cups-held-for-authentication");
3451 case CUPS_BACKEND_RETRY
:
3452 if (job_state
== IPP_JOB_COMPLETED
)
3455 * Hold the job if the number of retries is less than the
3456 * JobRetryLimit, otherwise abort the job.
3461 if (job
->tries
> JobRetryLimit
&& JobRetryLimit
> 0)
3467 snprintf(buffer
, sizeof(buffer
),
3468 "Job aborted after %d unsuccessful attempts.",
3470 job_state
= IPP_JOB_ABORTED
;
3473 ippSetString(job
->attrs
, &job
->reasons
, 0, "aborted-by-system");
3478 * Try again in N seconds...
3481 snprintf(buffer
, sizeof(buffer
),
3482 "Job held for %d seconds since it could not be sent.",
3485 job
->hold_until
= time(NULL
) + JobRetryInterval
;
3486 job_state
= IPP_JOB_HELD
;
3489 ippSetString(job
->attrs
, &job
->reasons
, 0,
3490 "resources-are-not-ready");
3495 case CUPS_BACKEND_RETRY_CURRENT
:
3497 * Mark the job as pending and retry on the same printer...
3500 if (job_state
== IPP_JOB_COMPLETED
)
3502 job_state
= IPP_JOB_PENDING
;
3503 message
= "Retrying job on same printer.";
3505 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
3510 else if (job
->status
> 0)
3513 * Filter had errors; stop job...
3516 if (job_state
== IPP_JOB_COMPLETED
)
3518 job_state
= IPP_JOB_STOPPED
;
3519 message
= "Job stopped due to filter errors; please consult the "
3520 "error_log file for details.";
3522 if (WIFSIGNALED(job
->status
))
3523 ippSetString(job
->attrs
, &job
->reasons
, 0, "cups-filter-crashed");
3525 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-completed-with-errors");
3530 * Update the printer and job state.
3533 if (set_job_state
&& job_state
!= job
->state_value
)
3534 cupsdSetJobState(job
, job_state
, CUPSD_JOB_DEFAULT
, "%s", message
);
3536 cupsdSetPrinterState(job
->printer
, printer_state
,
3537 printer_state
== IPP_PRINTER_STOPPED
);
3538 update_job_attrs(job
, 0);
3543 (job
->state_value
== IPP_JOB_ABORTED
||
3544 job
->state_value
== IPP_JOB_STOPPED
))
3545 dump_job_history(job
);
3547 free_job_history(job
);
3550 cupsArrayRemove(PrintingJobs
, job
);
3553 * Clear informational messages...
3556 if (job
->status_level
> CUPSD_LOG_ERROR
)
3557 job
->printer
->state_message
[0] = '\0';
3560 * Apply any PPD updates...
3563 if (job
->num_keywords
)
3565 if (cupsdUpdatePrinterPPD(job
->printer
, job
->num_keywords
, job
->keywords
))
3566 cupsdSetPrinterAttrs(job
->printer
);
3568 cupsFreeOptions(job
->num_keywords
, job
->keywords
);
3570 job
->num_keywords
= 0;
3571 job
->keywords
= NULL
;
3575 * Clear the printer <-> job association...
3578 job
->printer
->job
= NULL
;
3579 job
->printer
= NULL
;
3584 * 'get_options()' - Get a string containing the job options.
3587 static char * /* O - Options string */
3588 get_options(cupsd_job_t
*job
, /* I - Job */
3589 int banner_page
, /* I - Printing a banner page? */
3590 char *copies
, /* I - Copies buffer */
3591 size_t copies_size
, /* I - Size of copies buffer */
3592 char *title
, /* I - Title buffer */
3593 size_t title_size
) /* I - Size of title buffer */
3595 int i
; /* Looping var */
3596 size_t newlength
; /* New option buffer length */
3597 char *optptr
, /* Pointer to options */
3598 *valptr
; /* Pointer in value string */
3599 ipp_attribute_t
*attr
; /* Current attribute */
3600 _ppd_cache_t
*pc
; /* PPD cache and mapping data */
3601 int num_pwgppds
; /* Number of PWG->PPD options */
3602 cups_option_t
*pwgppds
, /* PWG->PPD options */
3603 *pwgppd
, /* Current PWG->PPD option */
3604 *preset
; /* Current preset option */
3605 int print_color_mode
,
3606 /* Output mode (if any) */
3607 print_quality
; /* Print quality (if any) */
3608 const char *ppd
; /* PPD option choice */
3609 int exact
; /* Did we get an exact match? */
3610 static char *options
= NULL
;/* Full list of options */
3611 static size_t optlength
= 0; /* Length of option buffer */
3615 * Building the options string is harder than it needs to be, but for the
3616 * moment we need to pass strings for command-line args and not IPP attribute
3619 * First build an options array for any PWG->PPD mapped option/choice pairs.
3622 pc
= job
->printer
->pc
;
3627 !ippFindAttribute(job
->attrs
, "com.apple.print.DocumentTicket.PMSpoolFormat", IPP_TAG_ZERO
) &&
3628 !ippFindAttribute(job
->attrs
, "APPrinterPreset", IPP_TAG_ZERO
) &&
3629 (ippFindAttribute(job
->attrs
, "print-color-mode", IPP_TAG_ZERO
) || ippFindAttribute(job
->attrs
, "print-quality", IPP_TAG_ZERO
) || ippFindAttribute(job
->attrs
, "cupsPrintQuality", IPP_TAG_ZERO
)))
3632 * Map print-color-mode and print-quality to a preset...
3635 if ((attr
= ippFindAttribute(job
->attrs
, "print-color-mode",
3636 IPP_TAG_KEYWORD
)) != NULL
&&
3637 !strcmp(attr
->values
[0].string
.text
, "monochrome"))
3638 print_color_mode
= _PWG_PRINT_COLOR_MODE_MONOCHROME
;
3640 print_color_mode
= _PWG_PRINT_COLOR_MODE_COLOR
;
3642 if ((attr
= ippFindAttribute(job
->attrs
, "print-quality", IPP_TAG_ENUM
)) != NULL
)
3644 ipp_quality_t pq
= (ipp_quality_t
)ippGetInteger(attr
, 0);
3646 if (pq
>= IPP_QUALITY_DRAFT
&& pq
<= IPP_QUALITY_HIGH
)
3647 print_quality
= attr
->values
[0].integer
- IPP_QUALITY_DRAFT
;
3649 print_quality
= _PWG_PRINT_QUALITY_NORMAL
;
3651 else if ((attr
= ippFindAttribute(job
->attrs
, "cupsPrintQuality", IPP_TAG_NAME
)) != NULL
)
3653 const char *pq
= ippGetString(attr
, 0, NULL
);
3655 if (!_cups_strcasecmp(pq
, "draft"))
3656 print_quality
= _PWG_PRINT_QUALITY_DRAFT
;
3657 else if (!_cups_strcasecmp(pq
, "high"))
3658 print_quality
= _PWG_PRINT_QUALITY_HIGH
;
3660 print_quality
= _PWG_PRINT_QUALITY_NORMAL
;
3662 if (!ippFindAttribute(job
->attrs
, "print-quality", IPP_TAG_ENUM
))
3664 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "Mapping cupsPrintQuality=%s to print-quality=%d", pq
, print_quality
+ IPP_QUALITY_DRAFT
);
3665 num_pwgppds
= cupsAddIntegerOption("print-quality", print_quality
+ IPP_QUALITY_DRAFT
, num_pwgppds
, &pwgppds
);
3670 print_quality
= _PWG_PRINT_QUALITY_NORMAL
;
3673 if (pc
->num_presets
[print_color_mode
][print_quality
] == 0)
3676 * Try to find a preset that works so that we maximize the chances of us
3677 * getting a good print using IPP attributes.
3680 if (pc
->num_presets
[print_color_mode
][_PWG_PRINT_QUALITY_NORMAL
] > 0)
3681 print_quality
= _PWG_PRINT_QUALITY_NORMAL
;
3682 else if (pc
->num_presets
[_PWG_PRINT_COLOR_MODE_COLOR
][print_quality
] > 0)
3683 print_color_mode
= _PWG_PRINT_COLOR_MODE_COLOR
;
3686 print_quality
= _PWG_PRINT_QUALITY_NORMAL
;
3687 print_color_mode
= _PWG_PRINT_COLOR_MODE_COLOR
;
3691 if (pc
->num_presets
[print_color_mode
][print_quality
] > 0)
3694 * Copy the preset options as long as the corresponding names are not
3695 * already defined in the IPP request...
3698 for (i
= pc
->num_presets
[print_color_mode
][print_quality
],
3699 preset
= pc
->presets
[print_color_mode
][print_quality
];
3703 if (!ippFindAttribute(job
->attrs
, preset
->name
, IPP_TAG_ZERO
))
3705 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "Adding preset option %s=%s", preset
->name
, preset
->value
);
3707 num_pwgppds
= cupsAddOption(preset
->name
, preset
->value
, num_pwgppds
, &pwgppds
);
3715 if ((attr
= ippFindAttribute(job
->attrs
, "print-quality", IPP_TAG_ENUM
)) != NULL
)
3717 int pq
= ippGetInteger(attr
, 0);
3718 static const char * const pqs
[] = { "Draft", "Normal", "High" };
3720 if (pq
>= IPP_QUALITY_DRAFT
&& pq
<= IPP_QUALITY_HIGH
)
3722 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "Mapping print-quality=%d to cupsPrintQuality=%s", pq
, pqs
[pq
- IPP_QUALITY_DRAFT
]);
3724 num_pwgppds
= cupsAddOption("cupsPrintQuality", pqs
[pq
- IPP_QUALITY_DRAFT
], num_pwgppds
, &pwgppds
);
3728 if (!ippFindAttribute(job
->attrs
, "InputSlot", IPP_TAG_ZERO
) &&
3729 !ippFindAttribute(job
->attrs
, "HPPaperSource", IPP_TAG_ZERO
))
3731 if ((ppd
= _ppdCacheGetInputSlot(pc
, job
->attrs
, NULL
)) != NULL
)
3732 num_pwgppds
= cupsAddOption(pc
->source_option
, ppd
, num_pwgppds
,
3735 if (!ippFindAttribute(job
->attrs
, "MediaType", IPP_TAG_ZERO
) &&
3736 (ppd
= _ppdCacheGetMediaType(pc
, job
->attrs
, NULL
)) != NULL
)
3738 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "Mapping media to MediaType=%s", ppd
);
3740 num_pwgppds
= cupsAddOption("MediaType", ppd
, num_pwgppds
, &pwgppds
);
3743 if (!ippFindAttribute(job
->attrs
, "PageRegion", IPP_TAG_ZERO
) &&
3744 !ippFindAttribute(job
->attrs
, "PageSize", IPP_TAG_ZERO
) &&
3745 (ppd
= _ppdCacheGetPageSize(pc
, job
->attrs
, NULL
, &exact
)) != NULL
)
3747 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "Mapping media to Pagesize=%s", ppd
);
3749 num_pwgppds
= cupsAddOption("PageSize", ppd
, num_pwgppds
, &pwgppds
);
3751 if (!ippFindAttribute(job
->attrs
, "media", IPP_TAG_ZERO
))
3753 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "Adding media=%s", ppd
);
3755 num_pwgppds
= cupsAddOption("media", ppd
, num_pwgppds
, &pwgppds
);
3759 if (!ippFindAttribute(job
->attrs
, "OutputBin", IPP_TAG_ZERO
) &&
3760 (attr
= ippFindAttribute(job
->attrs
, "output-bin",
3761 IPP_TAG_ZERO
)) != NULL
&&
3762 (attr
->value_tag
== IPP_TAG_KEYWORD
||
3763 attr
->value_tag
== IPP_TAG_NAME
) &&
3764 (ppd
= _ppdCacheGetOutputBin(pc
, attr
->values
[0].string
.text
)) != NULL
)
3767 * Map output-bin to OutputBin option...
3770 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "Mapping output-bin to OutputBin=%s", ppd
);
3772 num_pwgppds
= cupsAddOption("OutputBin", ppd
, num_pwgppds
, &pwgppds
);
3775 if (pc
->sides_option
&&
3776 !ippFindAttribute(job
->attrs
, pc
->sides_option
, IPP_TAG_ZERO
) &&
3777 (attr
= ippFindAttribute(job
->attrs
, "sides", IPP_TAG_KEYWORD
)) != NULL
)
3780 * Map sides to duplex option...
3783 if (!strcmp(attr
->values
[0].string
.text
, "one-sided"))
3785 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "Mapping sizes to Duplex=%s", pc
->sides_1sided
);
3787 num_pwgppds
= cupsAddOption(pc
->sides_option
, pc
->sides_1sided
, num_pwgppds
, &pwgppds
);
3789 else if (!strcmp(attr
->values
[0].string
.text
, "two-sided-long-edge"))
3791 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "Mapping sizes to Duplex=%s", pc
->sides_2sided_long
);
3793 num_pwgppds
= cupsAddOption(pc
->sides_option
, pc
->sides_2sided_long
, num_pwgppds
, &pwgppds
);
3795 else if (!strcmp(attr
->values
[0].string
.text
, "two-sided-short-edge"))
3797 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "Mapping sizes to Duplex=%s", pc
->sides_2sided_short
);
3799 num_pwgppds
= cupsAddOption(pc
->sides_option
, pc
->sides_2sided_short
, num_pwgppds
, &pwgppds
);
3804 * Map finishings values...
3807 num_pwgppds
= _ppdCacheGetFinishingOptions(pc
, job
->attrs
, IPP_FINISHINGS_NONE
, num_pwgppds
, &pwgppds
);
3809 for (i
= num_pwgppds
, pwgppd
= pwgppds
; i
> 0; i
--, pwgppd
++)
3810 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "After mapping finishings %s=%s", pwgppd
->name
, pwgppd
->value
);
3814 * Figure out how much room we need...
3817 newlength
= ipp_length(job
->attrs
);
3819 for (i
= num_pwgppds
, pwgppd
= pwgppds
; i
> 0; i
--, pwgppd
++)
3820 newlength
+= 1 + strlen(pwgppd
->name
) + 1 + strlen(pwgppd
->value
);
3823 * Then allocate/reallocate the option buffer as needed...
3826 if (newlength
== 0) /* This can never happen, but Clang */
3827 newlength
= 1; /* thinks it can... */
3829 if (newlength
> optlength
|| !options
)
3832 optptr
= malloc(newlength
);
3834 optptr
= realloc(options
, newlength
);
3838 cupsdLogJob(job
, CUPSD_LOG_CRIT
,
3839 "Unable to allocate " CUPS_LLFMT
" bytes for option buffer.",
3840 CUPS_LLCAST newlength
);
3845 optlength
= newlength
;
3849 * Now loop through the attributes and convert them to the textual
3850 * representation used by the filters...
3856 snprintf(title
, title_size
, "%s-%d", job
->printer
->name
, job
->id
);
3857 strlcpy(copies
, "1", copies_size
);
3859 for (attr
= job
->attrs
->attrs
; attr
!= NULL
; attr
= attr
->next
)
3861 if (!strcmp(attr
->name
, "copies") &&
3862 attr
->value_tag
== IPP_TAG_INTEGER
)
3865 * Don't use the # copies attribute if we are printing the job sheets...
3869 snprintf(copies
, copies_size
, "%d", attr
->values
[0].integer
);
3871 else if (!strcmp(attr
->name
, "job-name") &&
3872 (attr
->value_tag
== IPP_TAG_NAME
||
3873 attr
->value_tag
== IPP_TAG_NAMELANG
))
3874 strlcpy(title
, attr
->values
[0].string
.text
, title_size
);
3875 else if (attr
->group_tag
== IPP_TAG_JOB
)
3878 * Filter out other unwanted attributes...
3881 if (attr
->value_tag
== IPP_TAG_NOVALUE
||
3882 attr
->value_tag
== IPP_TAG_MIMETYPE
||
3883 attr
->value_tag
== IPP_TAG_NAMELANG
||
3884 attr
->value_tag
== IPP_TAG_TEXTLANG
||
3885 (attr
->value_tag
== IPP_TAG_URI
&& strcmp(attr
->name
, "job-uuid") &&
3886 strcmp(attr
->name
, "job-authorization-uri")) ||
3887 attr
->value_tag
== IPP_TAG_URISCHEME
||
3888 attr
->value_tag
== IPP_TAG_BEGIN_COLLECTION
) /* Not yet supported */
3891 if (!strcmp(attr
->name
, "job-hold-until") ||
3892 !strcmp(attr
->name
, "job-id") ||
3893 !strcmp(attr
->name
, "job-k-octets") ||
3894 !strcmp(attr
->name
, "job-media-sheets") ||
3895 !strcmp(attr
->name
, "job-media-sheets-completed") ||
3896 !strcmp(attr
->name
, "job-state") ||
3897 !strcmp(attr
->name
, "job-state-reasons"))
3900 if (!strncmp(attr
->name
, "job-", 4) &&
3901 strcmp(attr
->name
, "job-account-id") &&
3902 strcmp(attr
->name
, "job-accounting-user-id") &&
3903 strcmp(attr
->name
, "job-authorization-uri") &&
3904 strcmp(attr
->name
, "job-billing") &&
3905 strcmp(attr
->name
, "job-impressions") &&
3906 strcmp(attr
->name
, "job-originating-host-name") &&
3907 strcmp(attr
->name
, "job-password") &&
3908 strcmp(attr
->name
, "job-password-encryption") &&
3909 strcmp(attr
->name
, "job-uuid") &&
3910 !(job
->printer
->type
& CUPS_PRINTER_REMOTE
))
3913 if ((!strcmp(attr
->name
, "job-impressions") ||
3914 !strcmp(attr
->name
, "page-label") ||
3915 !strcmp(attr
->name
, "page-border") ||
3916 !strncmp(attr
->name
, "number-up", 9) ||
3917 !strcmp(attr
->name
, "page-ranges") ||
3918 !strcmp(attr
->name
, "page-set") ||
3919 !_cups_strcasecmp(attr
->name
, "AP_FIRSTPAGE_InputSlot") ||
3920 !_cups_strcasecmp(attr
->name
, "AP_FIRSTPAGE_ManualFeed") ||
3921 !_cups_strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
3922 "PMTotalSidesImaged..n.") ||
3923 !_cups_strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
3924 "PMTotalBeginPages..n.")) &&
3929 * Otherwise add them to the list...
3932 if (optptr
> options
)
3933 strlcat(optptr
, " ", optlength
- (size_t)(optptr
- options
));
3935 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
3937 strlcat(optptr
, attr
->name
, optlength
- (size_t)(optptr
- options
));
3938 strlcat(optptr
, "=", optlength
- (size_t)(optptr
- options
));
3941 for (i
= 0; i
< attr
->num_values
; i
++)
3944 strlcat(optptr
, ",", optlength
- (size_t)(optptr
- options
));
3946 optptr
+= strlen(optptr
);
3948 switch (attr
->value_tag
)
3950 case IPP_TAG_INTEGER
:
3952 snprintf(optptr
, optlength
- (size_t)(optptr
- options
),
3953 "%d", attr
->values
[i
].integer
);
3956 case IPP_TAG_BOOLEAN
:
3957 if (!attr
->values
[i
].boolean
)
3958 strlcat(optptr
, "no", optlength
- (size_t)(optptr
- options
));
3960 strlcat(optptr
, attr
->name
, optlength
- (size_t)(optptr
- options
));
3963 case IPP_TAG_RANGE
:
3964 if (attr
->values
[i
].range
.lower
== attr
->values
[i
].range
.upper
)
3965 snprintf(optptr
, optlength
- (size_t)(optptr
- options
) - 1,
3966 "%d", attr
->values
[i
].range
.lower
);
3968 snprintf(optptr
, optlength
- (size_t)(optptr
- options
) - 1,
3969 "%d-%d", attr
->values
[i
].range
.lower
,
3970 attr
->values
[i
].range
.upper
);
3973 case IPP_TAG_RESOLUTION
:
3974 snprintf(optptr
, optlength
- (size_t)(optptr
- options
) - 1,
3975 "%dx%d%s", attr
->values
[i
].resolution
.xres
,
3976 attr
->values
[i
].resolution
.yres
,
3977 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
3981 case IPP_TAG_STRING
:
3984 case IPP_TAG_KEYWORD
:
3985 case IPP_TAG_CHARSET
:
3986 case IPP_TAG_LANGUAGE
:
3988 for (valptr
= attr
->values
[i
].string
.text
; *valptr
;)
3990 if (strchr(" \t\n\\\'\"", *valptr
))
3992 *optptr
++ = *valptr
++;
3999 break; /* anti-compiler-warning-code */
4003 optptr
+= strlen(optptr
);
4008 * Finally loop through the PWG->PPD mapped options and add them...
4011 for (i
= num_pwgppds
, pwgppd
= pwgppds
; i
> 0; i
--, pwgppd
++)
4014 strlcpy(optptr
, pwgppd
->name
, optlength
- (size_t)(optptr
- options
));
4015 optptr
+= strlen(optptr
);
4017 strlcpy(optptr
, pwgppd
->value
, optlength
- (size_t)(optptr
- options
));
4018 optptr
+= strlen(optptr
);
4021 cupsFreeOptions(num_pwgppds
, pwgppds
);
4024 * Return the options string...
4032 * 'ipp_length()' - Compute the size of the buffer needed to hold
4033 * the textual IPP attributes.
4036 static size_t /* O - Size of attribute buffer */
4037 ipp_length(ipp_t
*ipp
) /* I - IPP request */
4039 size_t bytes
; /* Number of bytes */
4040 int i
; /* Looping var */
4041 ipp_attribute_t
*attr
; /* Current attribute */
4045 * Loop through all attributes...
4050 for (attr
= ipp
->attrs
; attr
!= NULL
; attr
= attr
->next
)
4053 * Skip attributes that won't be sent to filters...
4056 if (attr
->value_tag
== IPP_TAG_NOVALUE
||
4057 attr
->value_tag
== IPP_TAG_MIMETYPE
||
4058 attr
->value_tag
== IPP_TAG_NAMELANG
||
4059 attr
->value_tag
== IPP_TAG_TEXTLANG
||
4060 attr
->value_tag
== IPP_TAG_URI
||
4061 attr
->value_tag
== IPP_TAG_URISCHEME
)
4065 * Add space for a leading space and commas between each value.
4066 * For the first attribute, the leading space isn't used, so the
4067 * extra byte can be used as the nul terminator...
4070 bytes
++; /* " " separator */
4071 bytes
+= (size_t)attr
->num_values
; /* "," separators */
4074 * Boolean attributes appear as "foo,nofoo,foo,nofoo", while
4075 * other attributes appear as "foo=value1,value2,...,valueN".
4078 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
4079 bytes
+= strlen(attr
->name
);
4081 bytes
+= (size_t)attr
->num_values
* strlen(attr
->name
);
4084 * Now add the size required for each value in the attribute...
4087 switch (attr
->value_tag
)
4089 case IPP_TAG_INTEGER
:
4092 * Minimum value of a signed integer is -2147483647, or 11 digits.
4095 bytes
+= (size_t)attr
->num_values
* 11;
4098 case IPP_TAG_BOOLEAN
:
4100 * Add two bytes for each false ("no") value...
4103 for (i
= 0; i
< attr
->num_values
; i
++)
4104 if (!attr
->values
[i
].boolean
)
4108 case IPP_TAG_RANGE
:
4110 * A range is two signed integers separated by a hyphen, or
4111 * 23 characters max.
4114 bytes
+= (size_t)attr
->num_values
* 23;
4117 case IPP_TAG_RESOLUTION
:
4119 * A resolution is two signed integers separated by an "x" and
4120 * suffixed by the units, or 26 characters max.
4123 bytes
+= (size_t)attr
->num_values
* 26;
4126 case IPP_TAG_STRING
:
4129 case IPP_TAG_KEYWORD
:
4130 case IPP_TAG_CHARSET
:
4131 case IPP_TAG_LANGUAGE
:
4134 * Strings can contain characters that need quoting. We need
4135 * at least 2 * len + 2 characters to cover the quotes and
4136 * any backslashes in the string.
4139 for (i
= 0; i
< attr
->num_values
; i
++)
4140 bytes
+= 2 * strlen(attr
->values
[i
].string
.text
) + 2;
4144 break; /* anti-compiler-warning-code */
4153 * 'load_job_cache()' - Load jobs from the job.cache file.
4157 load_job_cache(const char *filename
) /* I - job.cache filename */
4159 cups_file_t
*fp
; /* job.cache file */
4160 char line
[1024], /* Line buffer */
4161 *value
; /* Value on line */
4162 int linenum
; /* Line number in file */
4163 cupsd_job_t
*job
; /* Current job */
4164 int jobid
; /* Job ID */
4165 char jobfile
[1024]; /* Job filename */
4169 * Open the job.cache file...
4172 if ((fp
= cupsdOpenConfFile(filename
)) == NULL
)
4174 load_request_root();
4179 * Read entries from the job cache file and create jobs as needed.
4182 cupsdLogMessage(CUPSD_LOG_INFO
, "Loading job cache file \"%s\"...",
4188 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
4190 if (!_cups_strcasecmp(line
, "NextJobId"))
4193 NextJobId
= atoi(value
);
4195 else if (!_cups_strcasecmp(line
, "<Job"))
4199 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing </Job> directive on line %d of %s.", linenum
, filename
);
4205 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing job ID on line %d of %s.", linenum
, filename
);
4209 jobid
= atoi(value
);
4213 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad job ID %d on line %d of %s.", jobid
, linenum
, filename
);
4217 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, jobid
);
4218 if (access(jobfile
, 0))
4220 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d.N", RequestRoot
, jobid
);
4221 if (access(jobfile
, 0))
4223 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Job %d] Files have gone away.",
4227 * job.cache file is out-of-date compared to spool directory; load
4232 load_request_root();
4237 job
= calloc(1, sizeof(cupsd_job_t
));
4240 cupsdLogMessage(CUPSD_LOG_EMERG
,
4241 "[Job %d] Unable to allocate memory for job.", jobid
);
4246 job
->back_pipes
[0] = -1;
4247 job
->back_pipes
[1] = -1;
4248 job
->print_pipes
[0] = -1;
4249 job
->print_pipes
[1] = -1;
4250 job
->side_pipes
[0] = -1;
4251 job
->side_pipes
[1] = -1;
4252 job
->status_pipes
[0] = -1;
4253 job
->status_pipes
[1] = -1;
4255 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Loading from cache...");
4259 cupsdLogMessage(CUPSD_LOG_ERROR
,
4260 "Missing <Job #> directive on line %d of %s.", linenum
, filename
);
4263 else if (!_cups_strcasecmp(line
, "</Job>"))
4265 cupsArrayAdd(Jobs
, job
);
4267 if (job
->state_value
<= IPP_JOB_STOPPED
&& cupsdLoadJob(job
))
4268 cupsArrayAdd(ActiveJobs
, job
);
4269 else if (job
->state_value
> IPP_JOB_STOPPED
)
4271 if (!job
->completed_time
|| !job
->creation_time
|| !job
->name
|| !job
->koctets
)
4282 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing value on line %d of %s.", linenum
, filename
);
4285 else if (!_cups_strcasecmp(line
, "State"))
4287 job
->state_value
= (ipp_jstate_t
)atoi(value
);
4289 if (job
->state_value
< IPP_JOB_PENDING
)
4290 job
->state_value
= IPP_JOB_PENDING
;
4291 else if (job
->state_value
> IPP_JOB_COMPLETED
)
4292 job
->state_value
= IPP_JOB_COMPLETED
;
4294 else if (!_cups_strcasecmp(line
, "Name"))
4296 cupsdSetString(&(job
->name
), value
);
4298 else if (!_cups_strcasecmp(line
, "Created"))
4300 job
->creation_time
= strtol(value
, NULL
, 10);
4302 else if (!_cups_strcasecmp(line
, "Completed"))
4304 job
->completed_time
= strtol(value
, NULL
, 10);
4306 else if (!_cups_strcasecmp(line
, "HoldUntil"))
4308 job
->hold_until
= strtol(value
, NULL
, 10);
4310 else if (!_cups_strcasecmp(line
, "Priority"))
4312 job
->priority
= atoi(value
);
4314 else if (!_cups_strcasecmp(line
, "Username"))
4316 cupsdSetString(&job
->username
, value
);
4318 else if (!_cups_strcasecmp(line
, "Destination"))
4320 cupsdSetString(&job
->dest
, value
);
4322 else if (!_cups_strcasecmp(line
, "DestType"))
4324 job
->dtype
= (cups_ptype_t
)atoi(value
);
4326 else if (!_cups_strcasecmp(line
, "KOctets"))
4328 job
->koctets
= atoi(value
);
4330 else if (!_cups_strcasecmp(line
, "NumFiles"))
4332 job
->num_files
= atoi(value
);
4334 if (job
->num_files
< 0)
4336 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad NumFiles value %d on line %d of %s.", job
->num_files
, linenum
, filename
);
4341 if (job
->num_files
> 0)
4343 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-001", RequestRoot
,
4345 if (access(jobfile
, 0))
4347 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Data files have gone away.");
4352 job
->filetypes
= calloc((size_t)job
->num_files
, sizeof(mime_type_t
*));
4353 job
->compressions
= calloc((size_t)job
->num_files
, sizeof(int));
4355 if (!job
->filetypes
|| !job
->compressions
)
4357 cupsdLogJob(job
, CUPSD_LOG_EMERG
,
4358 "Unable to allocate memory for %d files.",
4364 else if (!_cups_strcasecmp(line
, "File"))
4366 int number
, /* File number */
4367 compression
; /* Compression value */
4368 char super
[MIME_MAX_SUPER
], /* MIME super type */
4369 type
[MIME_MAX_TYPE
]; /* MIME type */
4372 if (sscanf(value
, "%d%*[ \t]%15[^/]/%255s%d", &number
, super
, type
,
4375 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File on line %d of %s.", linenum
, filename
);
4379 if (number
< 1 || number
> job
->num_files
)
4381 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File number %d on line %d of %s.", number
, linenum
, filename
);
4387 job
->compressions
[number
] = compression
;
4388 job
->filetypes
[number
] = mimeType(MimeDatabase
, super
, type
);
4390 if (!job
->filetypes
[number
])
4393 * If the original MIME type is unknown, auto-type it!
4396 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
4397 "Unknown MIME type %s/%s for file %d.",
4398 super
, type
, number
+ 1);
4400 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
4401 job
->id
, number
+ 1);
4402 job
->filetypes
[number
] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
4403 job
->compressions
+ number
);
4406 * If that didn't work, assume it is raw...
4409 if (!job
->filetypes
[number
])
4410 job
->filetypes
[number
] = mimeType(MimeDatabase
, "application",
4415 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unknown %s directive on line %d of %s.", line
, linenum
, filename
);
4420 cupsdLogMessage(CUPSD_LOG_ERROR
,
4421 "Missing </Job> directive on line %d of %s.", linenum
, filename
);
4422 cupsdDeleteJob(job
, CUPSD_JOB_PURGE
);
4430 * 'load_next_job_id()' - Load the NextJobId value from the job.cache file.
4434 load_next_job_id(const char *filename
) /* I - job.cache filename */
4436 cups_file_t
*fp
; /* job.cache file */
4437 char line
[1024], /* Line buffer */
4438 *value
; /* Value on line */
4439 int linenum
; /* Line number in file */
4440 int next_job_id
; /* NextJobId value from line */
4444 * Read the NextJobId directive from the job.cache file and use
4445 * the value (if any).
4448 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
4450 if (errno
!= ENOENT
)
4451 cupsdLogMessage(CUPSD_LOG_ERROR
,
4452 "Unable to open job cache file \"%s\": %s",
4453 filename
, strerror(errno
));
4458 cupsdLogMessage(CUPSD_LOG_INFO
,
4459 "Loading NextJobId from job cache file \"%s\"...", filename
);
4463 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
4465 if (!_cups_strcasecmp(line
, "NextJobId"))
4469 next_job_id
= atoi(value
);
4471 if (next_job_id
> NextJobId
)
4472 NextJobId
= next_job_id
;
4483 * 'load_request_root()' - Load jobs from the RequestRoot directory.
4487 load_request_root(void)
4489 cups_dir_t
*dir
; /* Directory */
4490 cups_dentry_t
*dent
; /* Directory entry */
4491 cupsd_job_t
*job
; /* New job */
4495 * Open the requests directory...
4498 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Scanning %s for jobs...", RequestRoot
);
4500 if ((dir
= cupsDirOpen(RequestRoot
)) == NULL
)
4502 cupsdLogMessage(CUPSD_LOG_ERROR
,
4503 "Unable to open spool directory \"%s\": %s",
4504 RequestRoot
, strerror(errno
));
4509 * Read all the c##### files...
4512 while ((dent
= cupsDirRead(dir
)) != NULL
)
4513 if (strlen(dent
->filename
) >= 6 && dent
->filename
[0] == 'c')
4516 * Allocate memory for the job...
4519 if ((job
= calloc(sizeof(cupsd_job_t
), 1)) == NULL
)
4521 cupsdLogMessage(CUPSD_LOG_ERROR
, "Ran out of memory for jobs.");
4527 * Assign the job ID...
4530 job
->id
= atoi(dent
->filename
+ 1);
4531 job
->back_pipes
[0] = -1;
4532 job
->back_pipes
[1] = -1;
4533 job
->print_pipes
[0] = -1;
4534 job
->print_pipes
[1] = -1;
4535 job
->side_pipes
[0] = -1;
4536 job
->side_pipes
[1] = -1;
4537 job
->status_pipes
[0] = -1;
4538 job
->status_pipes
[1] = -1;
4540 if (job
->id
>= NextJobId
)
4541 NextJobId
= job
->id
+ 1;
4547 if (cupsdLoadJob(job
))
4550 * Insert the job into the array, sorting by job priority and ID...
4553 cupsArrayAdd(Jobs
, job
);
4555 if (job
->state_value
<= IPP_JOB_STOPPED
)
4556 cupsArrayAdd(ActiveJobs
, job
);
4569 * 'remove_job_files()' - Remove the document files for a job.
4573 remove_job_files(cupsd_job_t
*job
) /* I - Job */
4575 int i
; /* Looping var */
4576 char filename
[1024]; /* Document filename */
4579 if (job
->num_files
<= 0)
4582 for (i
= 1; i
<= job
->num_files
; i
++)
4584 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
4586 cupsdUnlinkOrRemoveFile(filename
);
4589 free(job
->filetypes
);
4590 free(job
->compressions
);
4594 job
->filetypes
= NULL
;
4595 job
->compressions
= NULL
;
4597 LastEvent
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
4602 * 'remove_job_history()' - Remove the control file for a job.
4606 remove_job_history(cupsd_job_t
*job
) /* I - Job */
4608 char filename
[1024]; /* Control filename */
4612 * Remove the job info file...
4615 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
,
4617 cupsdUnlinkOrRemoveFile(filename
);
4619 LastEvent
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
4624 * 'set_time()' - Set one of the "time-at-xyz" attributes.
4628 set_time(cupsd_job_t
*job
, /* I - Job to update */
4629 const char *name
) /* I - Name of attribute */
4631 char date_name
[128]; /* date-time-at-xxx */
4632 ipp_attribute_t
*attr
; /* Time attribute */
4633 time_t curtime
; /* Current time */
4636 curtime
= time(NULL
);
4638 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "%s=%ld", name
, (long)curtime
);
4640 if ((attr
= ippFindAttribute(job
->attrs
, name
, IPP_TAG_ZERO
)) != NULL
)
4642 attr
->value_tag
= IPP_TAG_INTEGER
;
4643 attr
->values
[0].integer
= curtime
;
4646 snprintf(date_name
, sizeof(date_name
), "date-%s", name
);
4648 if ((attr
= ippFindAttribute(job
->attrs
, date_name
, IPP_TAG_ZERO
)) != NULL
)
4650 attr
->value_tag
= IPP_TAG_DATE
;
4651 ippSetDate(job
->attrs
, &attr
, 0, ippTimeToDate(curtime
));
4654 if (!strcmp(name
, "time-at-completed"))
4656 job
->completed_time
= curtime
;
4658 if (JobHistory
< INT_MAX
&& attr
)
4659 job
->history_time
= attr
->values
[0].integer
+ JobHistory
;
4661 job
->history_time
= INT_MAX
;
4663 if (job
->history_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
4664 JobHistoryUpdate
= job
->history_time
;
4666 if (JobFiles
< INT_MAX
&& attr
)
4667 job
->file_time
= curtime
+ JobFiles
;
4669 job
->file_time
= INT_MAX
;
4671 if (job
->file_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
4672 JobHistoryUpdate
= job
->file_time
;
4674 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_time: JobHistoryUpdate=%ld",
4675 (long)JobHistoryUpdate
);
4681 * 'start_job()' - Start a print job.
4685 start_job(cupsd_job_t
*job
, /* I - Job ID */
4686 cupsd_printer_t
*printer
) /* I - Printer to print job */
4688 const char *filename
; /* Support filename */
4689 ipp_attribute_t
*cancel_after
= ippFindAttribute(job
->attrs
,
4692 /* job-cancel-after attribute */
4695 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "start_job(job=%p(%d), printer=%p(%s))",
4696 job
, job
->id
, printer
, printer
->name
);
4699 * Make sure we have some files around before we try to print...
4702 if (job
->num_files
== 0)
4704 ippSetString(job
->attrs
, &job
->reasons
, 0, "aborted-by-system");
4705 cupsdSetJobState(job
, IPP_JOB_ABORTED
, CUPSD_JOB_DEFAULT
,
4706 "Aborting job because it has no files.");
4711 * Update the printer and job state to "processing"...
4714 if (!cupsdLoadJob(job
))
4717 if (!job
->printer_message
)
4718 job
->printer_message
= ippFindAttribute(job
->attrs
,
4719 "job-printer-state-message",
4721 if (job
->printer_message
)
4722 ippSetString(job
->attrs
, &job
->printer_message
, 0, "");
4724 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-printing");
4725 cupsdSetJobState(job
, IPP_JOB_PROCESSING
, CUPSD_JOB_DEFAULT
, NULL
);
4726 cupsdSetPrinterState(printer
, IPP_PRINTER_PROCESSING
, 0);
4727 cupsdSetPrinterReasons(printer
, "-cups-remote-pending,"
4728 "cups-remote-pending-held,"
4729 "cups-remote-processing,"
4730 "cups-remote-stopped,"
4731 "cups-remote-canceled,"
4732 "cups-remote-aborted,"
4733 "cups-remote-completed");
4736 job
->current_file
= 0;
4738 job
->history_time
= 0;
4740 job
->printer
= printer
;
4744 job
->cancel_time
= time(NULL
) + ippGetInteger(cancel_after
, 0);
4745 else if (MaxJobTime
> 0)
4746 job
->cancel_time
= time(NULL
) + MaxJobTime
;
4748 job
->cancel_time
= 0;
4751 * Check for support files...
4754 cupsdSetPrinterReasons(job
->printer
, "-cups-missing-filter-warning,"
4755 "cups-insecure-filter-warning");
4759 for (filename
= (const char *)cupsArrayFirst(printer
->pc
->support_files
);
4761 filename
= (const char *)cupsArrayNext(printer
->pc
->support_files
))
4763 if (_cupsFileCheck(filename
, _CUPS_FILE_CHECK_FILE
, !RunUser
,
4764 cupsdLogFCMessage
, printer
))
4770 * Setup the last exit status and security profiles...
4774 job
->profile
= cupsdCreateProfile(job
->id
, 0);
4775 job
->bprofile
= cupsdCreateProfile(job
->id
, 1);
4778 * Create the status pipes and buffer...
4781 if (cupsdOpenPipe(job
->status_pipes
))
4783 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
4784 "Unable to create job status pipes - %s.", strerror(errno
));
4786 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
4787 "Job stopped because the scheduler could not create the "
4788 "job status pipes.");
4790 cupsdDestroyProfile(job
->profile
);
4791 job
->profile
= NULL
;
4792 cupsdDestroyProfile(job
->bprofile
);
4793 job
->bprofile
= NULL
;
4797 job
->status_buffer
= cupsdStatBufNew(job
->status_pipes
[0], NULL
);
4798 job
->status_level
= CUPSD_LOG_INFO
;
4801 * Create the backchannel pipes and make them non-blocking...
4804 if (cupsdOpenPipe(job
->back_pipes
))
4806 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
4807 "Unable to create back-channel pipes - %s.", strerror(errno
));
4809 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
4810 "Job stopped because the scheduler could not create the "
4811 "back-channel pipes.");
4813 cupsdClosePipe(job
->status_pipes
);
4814 cupsdStatBufDelete(job
->status_buffer
);
4815 job
->status_buffer
= NULL
;
4817 cupsdDestroyProfile(job
->profile
);
4818 job
->profile
= NULL
;
4819 cupsdDestroyProfile(job
->bprofile
);
4820 job
->bprofile
= NULL
;
4824 fcntl(job
->back_pipes
[0], F_SETFL
,
4825 fcntl(job
->back_pipes
[0], F_GETFL
) | O_NONBLOCK
);
4826 fcntl(job
->back_pipes
[1], F_SETFL
,
4827 fcntl(job
->back_pipes
[1], F_GETFL
) | O_NONBLOCK
);
4830 * Create the side-channel pipes and make them non-blocking...
4833 if (socketpair(AF_LOCAL
, SOCK_STREAM
, 0, job
->side_pipes
))
4835 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
4836 "Unable to create side-channel pipes - %s.", strerror(errno
));
4838 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
4839 "Job stopped because the scheduler could not create the "
4840 "side-channel pipes.");
4842 cupsdClosePipe(job
->back_pipes
);
4844 cupsdClosePipe(job
->status_pipes
);
4845 cupsdStatBufDelete(job
->status_buffer
);
4846 job
->status_buffer
= NULL
;
4848 cupsdDestroyProfile(job
->profile
);
4849 job
->profile
= NULL
;
4850 cupsdDestroyProfile(job
->bprofile
);
4851 job
->bprofile
= NULL
;
4855 fcntl(job
->side_pipes
[0], F_SETFL
,
4856 fcntl(job
->side_pipes
[0], F_GETFL
) | O_NONBLOCK
);
4857 fcntl(job
->side_pipes
[1], F_SETFL
,
4858 fcntl(job
->side_pipes
[1], F_GETFL
) | O_NONBLOCK
);
4860 fcntl(job
->side_pipes
[0], F_SETFD
,
4861 fcntl(job
->side_pipes
[0], F_GETFD
) | FD_CLOEXEC
);
4862 fcntl(job
->side_pipes
[1], F_SETFD
,
4863 fcntl(job
->side_pipes
[1], F_GETFD
) | FD_CLOEXEC
);
4866 * Now start the first file in the job...
4869 cupsdContinueJob(job
);
4874 * 'stop_job()' - Stop a print job.
4878 stop_job(cupsd_job_t
*job
, /* I - Job */
4879 cupsd_jobaction_t action
) /* I - Action */
4881 int i
; /* Looping var */
4884 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "stop_job(job=%p(%d), action=%d)", job
,
4887 FilterLevel
-= job
->cost
;
4890 if (action
== CUPSD_JOB_DEFAULT
&& !job
->kill_time
&& job
->backend
> 0)
4891 job
->kill_time
= time(NULL
) + JobKillDelay
;
4892 else if (action
>= CUPSD_JOB_FORCE
)
4895 for (i
= 0; job
->filters
[i
]; i
++)
4896 if (job
->filters
[i
] > 0)
4898 cupsdEndProcess(job
->filters
[i
], action
>= CUPSD_JOB_FORCE
);
4900 if (action
>= CUPSD_JOB_FORCE
)
4901 job
->filters
[i
] = -job
->filters
[i
];
4904 if (job
->backend
> 0)
4906 cupsdEndProcess(job
->backend
, action
>= CUPSD_JOB_FORCE
);
4908 if (action
>= CUPSD_JOB_FORCE
)
4909 job
->backend
= -job
->backend
;
4912 if (action
>= CUPSD_JOB_FORCE
)
4915 * Clear job status...
4924 * 'unload_job()' - Unload a job from memory.
4928 unload_job(cupsd_job_t
*job
) /* I - Job */
4933 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Unloading...");
4935 ippDelete(job
->attrs
);
4939 job
->reasons
= NULL
;
4940 job
->impressions
= NULL
;
4942 job
->job_sheets
= NULL
;
4943 job
->printer_message
= NULL
;
4944 job
->printer_reasons
= NULL
;
4949 * 'update_job()' - Read a status update from a job's filters.
4953 update_job(cupsd_job_t
*job
) /* I - Job to check */
4955 int i
; /* Looping var */
4956 int copies
; /* Number of copies printed */
4957 char message
[CUPSD_SB_BUFFER_SIZE
],
4959 *ptr
; /* Pointer update... */
4960 int loglevel
, /* Log level for message */
4961 event
= 0; /* Events? */
4962 cupsd_printer_t
*printer
= job
->printer
;
4964 static const char * const levels
[] = /* Log levels */
4980 * Get the printer associated with this job; if the printer is stopped for
4981 * any reason then job->printer will be reset to NULL, so make sure we have
4982 * a valid pointer...
4985 while ((ptr
= cupsdStatBufUpdate(job
->status_buffer
, &loglevel
,
4986 message
, sizeof(message
))) != NULL
)
4989 * Process page and printer state messages as needed...
4992 if (loglevel
== CUPSD_LOG_PAGE
)
4995 * Page message; send the message to the page_log file and update the
4996 * job sheet count...
4999 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "PAGE: %s", message
);
5001 if (job
->impressions
)
5003 if (!_cups_strncasecmp(message
, "total ", 6))
5006 * Got a total count of pages from a backend or filter...
5009 copies
= atoi(message
+ 6);
5010 copies
-= ippGetInteger(job
->impressions
, 0); /* Just track the delta */
5012 else if (!sscanf(message
, "%*d%d", &copies
))
5015 ippSetInteger(job
->attrs
, &job
->impressions
, 0, ippGetInteger(job
->impressions
, 0) + copies
);
5017 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
5022 if (!_cups_strncasecmp(message
, "total ", 6))
5025 * Got a total count of pages from a backend or filter...
5028 copies
= atoi(message
+ 6);
5029 copies
-= ippGetInteger(job
->sheets
, 0); /* Just track the delta */
5031 else if (!sscanf(message
, "%*d%d", &copies
))
5034 ippSetInteger(job
->attrs
, &job
->sheets
, 0, ippGetInteger(job
->sheets
, 0) + copies
);
5036 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
5038 if (job
->printer
->page_limit
)
5039 cupsdUpdateQuota(job
->printer
, job
->username
, copies
, 0);
5042 cupsdLogPage(job
, message
);
5045 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
, "Printed %d page(s).", ippGetInteger(job
->sheets
, 0));
5047 else if (loglevel
== CUPSD_LOG_JOBSTATE
)
5050 * Support "keyword" to set job-state-reasons to the specified keyword.
5051 * This is sufficient for the current paid printing stuff.
5054 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "JOBSTATE: %s", message
);
5056 if (!strcmp(message
, "cups-retry-as-raster"))
5057 job
->retry_as_raster
= 1;
5059 ippSetString(job
->attrs
, &job
->reasons
, 0, message
);
5061 else if (loglevel
== CUPSD_LOG_STATE
)
5063 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "STATE: %s", message
);
5065 if (!strcmp(message
, "paused"))
5067 cupsdStopPrinter(job
->printer
, 1);
5070 else if (message
[0] && cupsdSetPrinterReasons(job
->printer
, message
))
5072 event
|= CUPSD_EVENT_PRINTER_STATE
;
5077 * Reset cancel time after connecting to the device...
5080 for (i
= 0; i
< job
->printer
->num_reasons
; i
++)
5081 if (!strcmp(job
->printer
->reasons
[i
], "connecting-to-device"))
5084 if (i
>= job
->printer
->num_reasons
)
5086 ipp_attribute_t
*cancel_after
= ippFindAttribute(job
->attrs
,
5089 /* job-cancel-after attribute */
5092 job
->cancel_time
= time(NULL
) + ippGetInteger(cancel_after
, 0);
5094 job
->cancel_time
= time(NULL
) + MaxJobTime
;
5099 update_job_attrs(job
, 0);
5101 else if (loglevel
== CUPSD_LOG_ATTR
)
5104 * Set attribute(s)...
5107 int num_attrs
; /* Number of attributes */
5108 cups_option_t
*attrs
; /* Attributes */
5109 const char *attr
; /* Attribute */
5111 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "ATTR: %s", message
);
5113 num_attrs
= cupsParseOptions(message
, 0, &attrs
);
5115 if ((attr
= cupsGetOption("auth-info-default", num_attrs
,
5118 job
->printer
->num_options
= cupsAddOption("auth-info", attr
,
5119 job
->printer
->num_options
,
5120 &(job
->printer
->options
));
5121 cupsdSetPrinterAttrs(job
->printer
);
5123 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
5126 if ((attr
= cupsGetOption("auth-info-required", num_attrs
,
5129 cupsdSetAuthInfoRequired(job
->printer
, attr
, NULL
);
5130 cupsdSetPrinterAttrs(job
->printer
);
5132 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
5135 if ((attr
= cupsGetOption("job-media-progress", num_attrs
,
5138 int progress
= atoi(attr
);
5141 if (progress
>= 0 && progress
<= 100)
5143 job
->progress
= progress
;
5146 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
5147 "Printing page %d, %d%%",
5148 job
->sheets
->values
[0].integer
, job
->progress
);
5152 if ((attr
= cupsGetOption("printer-alert", num_attrs
, attrs
)) != NULL
)
5154 cupsdSetString(&job
->printer
->alert
, attr
);
5155 event
|= CUPSD_EVENT_PRINTER_STATE
;
5158 if ((attr
= cupsGetOption("printer-alert-description", num_attrs
,
5161 cupsdSetString(&job
->printer
->alert_description
, attr
);
5162 event
|= CUPSD_EVENT_PRINTER_STATE
;
5165 if ((attr
= cupsGetOption("marker-colors", num_attrs
, attrs
)) != NULL
)
5167 cupsdSetPrinterAttr(job
->printer
, "marker-colors", (char *)attr
);
5168 job
->printer
->marker_time
= time(NULL
);
5169 event
|= CUPSD_EVENT_PRINTER_STATE
;
5170 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
5173 if ((attr
= cupsGetOption("marker-levels", num_attrs
, attrs
)) != NULL
)
5175 cupsdSetPrinterAttr(job
->printer
, "marker-levels", (char *)attr
);
5176 job
->printer
->marker_time
= time(NULL
);
5177 event
|= CUPSD_EVENT_PRINTER_STATE
;
5178 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
5181 if ((attr
= cupsGetOption("marker-low-levels", num_attrs
, attrs
)) != NULL
)
5183 cupsdSetPrinterAttr(job
->printer
, "marker-low-levels", (char *)attr
);
5184 job
->printer
->marker_time
= time(NULL
);
5185 event
|= CUPSD_EVENT_PRINTER_STATE
;
5186 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
5189 if ((attr
= cupsGetOption("marker-high-levels", num_attrs
, attrs
)) != NULL
)
5191 cupsdSetPrinterAttr(job
->printer
, "marker-high-levels", (char *)attr
);
5192 job
->printer
->marker_time
= time(NULL
);
5193 event
|= CUPSD_EVENT_PRINTER_STATE
;
5194 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
5197 if ((attr
= cupsGetOption("marker-message", num_attrs
, attrs
)) != NULL
)
5199 cupsdSetPrinterAttr(job
->printer
, "marker-message", (char *)attr
);
5200 job
->printer
->marker_time
= time(NULL
);
5201 event
|= CUPSD_EVENT_PRINTER_STATE
;
5202 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
5205 if ((attr
= cupsGetOption("marker-names", num_attrs
, attrs
)) != NULL
)
5207 cupsdSetPrinterAttr(job
->printer
, "marker-names", (char *)attr
);
5208 job
->printer
->marker_time
= time(NULL
);
5209 event
|= CUPSD_EVENT_PRINTER_STATE
;
5210 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
5213 if ((attr
= cupsGetOption("marker-types", num_attrs
, attrs
)) != NULL
)
5215 cupsdSetPrinterAttr(job
->printer
, "marker-types", (char *)attr
);
5216 job
->printer
->marker_time
= time(NULL
);
5217 event
|= CUPSD_EVENT_PRINTER_STATE
;
5218 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
5221 cupsFreeOptions(num_attrs
, attrs
);
5223 else if (loglevel
== CUPSD_LOG_PPD
)
5226 * Set attribute(s)...
5229 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "PPD: %s", message
);
5231 job
->num_keywords
= cupsParseOptions(message
, job
->num_keywords
,
5237 * Strip legacy message prefix...
5240 if (!strncmp(message
, "recoverable:", 12))
5243 while (isspace(*ptr
& 255))
5246 else if (!strncmp(message
, "recovered:", 10))
5249 while (isspace(*ptr
& 255))
5256 cupsdLogJob(job
, loglevel
== CUPSD_LOG_INFO
? CUPSD_LOG_DEBUG
: loglevel
, "%s", ptr
);
5258 if (loglevel
< CUPSD_LOG_DEBUG
&&
5259 strcmp(job
->printer
->state_message
, ptr
))
5261 strlcpy(job
->printer
->state_message
, ptr
,
5262 sizeof(job
->printer
->state_message
));
5264 event
|= CUPSD_EVENT_PRINTER_STATE
| CUPSD_EVENT_JOB_PROGRESS
;
5266 if (loglevel
<= job
->status_level
&& job
->status_level
> CUPSD_LOG_ERROR
)
5269 * Some messages show in the job-printer-state-message attribute...
5272 if (loglevel
!= CUPSD_LOG_NOTICE
)
5273 job
->status_level
= loglevel
;
5275 update_job_attrs(job
, 1);
5277 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
5278 "Set job-printer-state-message to \"%s\", "
5280 job
->printer_message
->values
[0].string
.text
,
5281 levels
[job
->status_level
]);
5286 if (!strchr(job
->status_buffer
->buffer
, '\n'))
5290 if (event
& CUPSD_EVENT_JOB_PROGRESS
)
5291 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
5292 "%s", job
->printer
->state_message
);
5293 if (event
& CUPSD_EVENT_PRINTER_STATE
)
5294 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, job
->printer
, NULL
,
5295 (job
->printer
->type
& CUPS_PRINTER_CLASS
) ?
5296 "Class \"%s\" state changed." :
5297 "Printer \"%s\" state changed.",
5298 job
->printer
->name
);
5301 if (ptr
== NULL
&& !job
->status_buffer
->bufused
)
5304 * See if all of the filters and the backend have returned their
5308 for (i
= 0; job
->filters
[i
] < 0; i
++);
5310 if (job
->filters
[i
])
5313 * EOF but we haven't collected the exit status of all filters...
5316 cupsdCheckProcess();
5320 if (job
->current_file
>= job
->num_files
&& job
->backend
> 0)
5323 * EOF but we haven't collected the exit status of the backend...
5326 cupsdCheckProcess();
5331 * Handle the end of job stuff...
5334 finalize_job(job
, 1);
5337 * Try printing another job...
5340 if (printer
->state
!= IPP_PRINTER_STOPPED
)
5347 * 'update_job_attrs()' - Update the job-printer-* attributes.
5351 update_job_attrs(cupsd_job_t
*job
, /* I - Job to update */
5352 int do_message
)/* I - 1 = copy job-printer-state message */
5354 int i
; /* Looping var */
5355 int num_reasons
; /* Actual number of reasons */
5356 const char * const *reasons
; /* Reasons */
5357 static const char *none
= "none"; /* "none" reason */
5361 * Get/create the job-printer-state-* attributes...
5364 if (!job
->printer_message
)
5366 if ((job
->printer_message
= ippFindAttribute(job
->attrs
,
5367 "job-printer-state-message",
5368 IPP_TAG_TEXT
)) == NULL
)
5369 job
->printer_message
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_TEXT
,
5370 "job-printer-state-message",
5374 if (!job
->printer_reasons
)
5375 job
->printer_reasons
= ippFindAttribute(job
->attrs
,
5376 "job-printer-state-reasons",
5380 * Copy or clear the printer-state-message value as needed...
5383 if (job
->state_value
!= IPP_JOB_PROCESSING
&&
5384 job
->status_level
== CUPSD_LOG_INFO
)
5386 ippSetString(job
->attrs
, &job
->printer_message
, 0, "");
5389 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
5391 else if (job
->printer
->state_message
[0] && do_message
)
5393 ippSetString(job
->attrs
, &job
->printer_message
, 0, job
->printer
->state_message
);
5396 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
5400 * ... and the printer-state-reasons value...
5403 if (job
->printer
->num_reasons
== 0)
5410 num_reasons
= job
->printer
->num_reasons
;
5411 reasons
= (const char * const *)job
->printer
->reasons
;
5414 if (!job
->printer_reasons
|| job
->printer_reasons
->num_values
!= num_reasons
)
5417 * Replace/create a job-printer-state-reasons attribute...
5420 ippDeleteAttribute(job
->attrs
, job
->printer_reasons
);
5422 job
->printer_reasons
= ippAddStrings(job
->attrs
,
5423 IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
5424 "job-printer-state-reasons",
5425 num_reasons
, NULL
, NULL
);
5430 * Don't bother clearing the reason strings if they are the same...
5433 for (i
= 0; i
< num_reasons
; i
++)
5434 if (strcmp(job
->printer_reasons
->values
[i
].string
.text
, reasons
[i
]))
5437 if (i
>= num_reasons
)
5441 * Not the same, so free the current strings...
5444 for (i
= 0; i
< num_reasons
; i
++)
5445 _cupsStrFree(job
->printer_reasons
->values
[i
].string
.text
);
5449 * Copy the reasons...
5452 for (i
= 0; i
< num_reasons
; i
++)
5453 job
->printer_reasons
->values
[i
].string
.text
= _cupsStrAlloc(reasons
[i
]);
5456 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);