4 * Job management routines for the CUPS scheduler.
6 * Copyright 2007-2015 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
17 * Include necessary headers...
22 #include <cups/backend.h>
25 # include <IOKit/pwr_mgt/IOPMLib.h>
26 # ifdef HAVE_IOKIT_PWR_MGT_IOPMLIBPRIVATE_H
27 # include <IOKit/pwr_mgt/IOPMLibPrivate.h>
28 # endif /* HAVE_IOKIT_PWR_MGT_IOPMLIBPRIVATE_H */
29 #endif /* __APPLE__ */
33 * Design Notes for Job Management
34 * -------------------------------
38 * pending Do nothing/check jobs
39 * pending-held Send SIGTERM to filters and backend
40 * processing Do nothing/start job
41 * stopped Send SIGKILL to filters and backend
42 * canceled Send SIGTERM to filters and backend
46 * Finalize clears the printer <-> job association, deletes the status
47 * buffer, closes all of the pipes, etc. and doesn't get run until all of
48 * the print processes are finished.
50 * UNLOADING OF JOBS (cupsdUnloadCompletedJobs)
52 * We unload the job attributes when they are not needed to reduce overall
53 * memory consumption. We don't unload jobs where job->state_value <
54 * IPP_JOB_STOPPED, job->printer != NULL, or job->access_time is recent.
56 * STARTING OF JOBS (start_job)
58 * When a job is started, a status buffer, several pipes, a security
59 * profile, and a backend process are created for the life of that job.
60 * These are shared for every file in a job. For remote print jobs, the
61 * IPP backend is provided with every file in the job and no filters are
64 * The job->printer member tracks which printer is printing a job, which
65 * can be different than the destination in job->dest for classes. The
66 * printer object also has a job pointer to track which job is being
69 * PRINTING OF JOB FILES (cupsdContinueJob)
71 * Each file in a job is filtered by 0 or more programs. After getting the
72 * list of filters needed and the total cost, the job is either passed or
73 * put back to the processing state until the current FilterLevel comes down
74 * enough to allow printing.
76 * If we can print, we build a string for the print options and run each of
77 * the filters, piping the output from one into the next.
79 * JOB STATUS UPDATES (update_job)
81 * The update_job function gets called whenever there are pending messages
82 * on the status pipe. These generally are updates to the marker-*,
83 * printer-state-message, or printer-state-reasons attributes. On EOF,
84 * finalize_job is called to clean up.
86 * FINALIZING JOBS (finalize_job)
88 * When all filters and the backend are done, we set the job state to
89 * completed (no errors), aborted (filter errors or abort-job policy),
90 * pending-held (auth required or retry-job policy), or pending
91 * (retry-current-job or stop-printer policies) as appropriate.
93 * Then we close the pipes and free the status buffers and profiles.
95 * JOB FILE COMPLETION (process_children in main.c)
97 * For multiple-file jobs, process_children (in main.c) sees that all
98 * filters have exited and calls in to print the next file if there are
99 * more files in the job, otherwise it waits for the backend to exit and
100 * update_job to do the cleanup.
108 static mime_filter_t gziptoany_filter
=
110 NULL
, /* Source type */
111 NULL
, /* Destination type */
113 "gziptoany" /* Filter program to run */
121 static int compare_active_jobs(void *first
, void *second
, void *data
);
122 static int compare_completed_jobs(void *first
, void *second
, void *data
);
123 static int compare_jobs(void *first
, void *second
, void *data
);
124 static void dump_job_history(cupsd_job_t
*job
);
125 static void finalize_job(cupsd_job_t
*job
, int set_job_state
);
126 static void free_job_history(cupsd_job_t
*job
);
127 static char *get_options(cupsd_job_t
*job
, int banner_page
, char *copies
,
128 size_t copies_size
, char *title
,
130 static size_t ipp_length(ipp_t
*ipp
);
131 static void load_job_cache(const char *filename
);
132 static void load_next_job_id(const char *filename
);
133 static void load_request_root(void);
134 static void remove_job_files(cupsd_job_t
*job
);
135 static void remove_job_history(cupsd_job_t
*job
);
136 static void set_time(cupsd_job_t
*job
, const char *name
);
137 static void start_job(cupsd_job_t
*job
, cupsd_printer_t
*printer
);
138 static void stop_job(cupsd_job_t
*job
, cupsd_jobaction_t action
);
139 static void unload_job(cupsd_job_t
*job
);
140 static void update_job(cupsd_job_t
*job
);
141 static void update_job_attrs(cupsd_job_t
*job
, int do_message
);
145 * 'cupsdAddJob()' - Add a new job to the job queue.
148 cupsd_job_t
* /* O - New job record */
149 cupsdAddJob(int priority
, /* I - Job priority */
150 const char *dest
) /* I - Job destination */
152 cupsd_job_t
*job
; /* New job record */
155 if ((job
= calloc(sizeof(cupsd_job_t
), 1)) == NULL
)
158 job
->id
= NextJobId
++;
159 job
->priority
= priority
;
160 job
->back_pipes
[0] = -1;
161 job
->back_pipes
[1] = -1;
162 job
->print_pipes
[0] = -1;
163 job
->print_pipes
[1] = -1;
164 job
->side_pipes
[0] = -1;
165 job
->side_pipes
[1] = -1;
166 job
->status_pipes
[0] = -1;
167 job
->status_pipes
[1] = -1;
169 cupsdSetString(&job
->dest
, dest
);
172 * Add the new job to the "all jobs" and "active jobs" lists...
175 cupsArrayAdd(Jobs
, job
);
176 cupsArrayAdd(ActiveJobs
, job
);
183 * 'cupsdCancelJobs()' - Cancel all jobs for the given destination/user.
187 cupsdCancelJobs(const char *dest
, /* I - Destination to cancel */
188 const char *username
, /* I - Username or NULL */
189 int purge
) /* I - Purge jobs? */
191 cupsd_job_t
*job
; /* Current job */
194 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
196 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
198 if ((!job
->dest
|| !job
->username
) && !cupsdLoadJob(job
))
201 if ((!dest
|| !strcmp(job
->dest
, dest
)) &&
202 (!username
|| !strcmp(job
->username
, username
)))
205 * Cancel all jobs matching this destination/user...
209 cupsdSetJobState(job
, IPP_JOB_CANCELED
, CUPSD_JOB_PURGE
,
210 "Job purged by user.");
211 else if (job
->state_value
< IPP_JOB_CANCELED
)
212 cupsdSetJobState(job
, IPP_JOB_CANCELED
, CUPSD_JOB_DEFAULT
,
213 "Job canceled by user.");
222 * 'cupsdCheckJobs()' - Check the pending jobs and start any if the destination
229 cupsd_job_t
*job
; /* Current job in queue */
230 cupsd_printer_t
*printer
, /* Printer destination */
231 *pclass
; /* Printer class destination */
232 ipp_attribute_t
*attr
; /* Job attribute */
233 time_t curtime
; /* Current time */
236 curtime
= time(NULL
);
238 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdCheckJobs: %d active jobs, sleeping=%d, ac-power=%d, reload=%d, curtime=%ld", cupsArrayCount(ActiveJobs
), Sleeping
, ACPower
, NeedReload
, (long)curtime
);
240 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
242 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
244 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
245 "cupsdCheckJobs: Job %d - dest=\"%s\", printer=%p, "
246 "state=%d, cancel_time=%ld, hold_until=%ld, kill_time=%ld, "
247 "pending_cost=%d, pending_timeout=%ld", job
->id
, job
->dest
,
248 job
->printer
, job
->state_value
, (long)job
->cancel_time
,
249 (long)job
->hold_until
, (long)job
->kill_time
,
250 job
->pending_cost
, (long)job
->pending_timeout
);
253 * Kill jobs if they are unresponsive...
256 if (job
->kill_time
&& job
->kill_time
<= curtime
)
259 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Stopping unresponsive job.");
261 stop_job(job
, CUPSD_JOB_FORCE
);
266 * Cancel stuck jobs...
269 if (job
->cancel_time
&& job
->cancel_time
<= curtime
)
271 int cancel_after
; /* job-cancel-after value */
273 attr
= ippFindAttribute(job
->attrs
, "job-cancel-after", IPP_TAG_INTEGER
);
274 cancel_after
= attr
? ippGetInteger(attr
, 0) : MaxJobTime
;
277 cupsdSetJobState(job
, IPP_JOB_CANCELED
, CUPSD_JOB_FORCE
, "Marking stuck job as completed after %d seconds.", cancel_after
);
279 cupsdSetJobState(job
, IPP_JOB_CANCELED
, CUPSD_JOB_DEFAULT
, "Canceling stuck job after %d seconds.", cancel_after
);
284 * Start held jobs if they are ready...
287 if (job
->state_value
== IPP_JOB_HELD
&&
289 job
->hold_until
< curtime
)
291 if (job
->pending_timeout
)
294 * This job is pending; check that we don't have an active Send-Document
295 * operation in progress on any of the client connections, then timeout
296 * the job so we can start printing...
299 cupsd_client_t
*con
; /* Current client connection */
301 for (con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
303 con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
305 con
->request
->request
.op
.operation_id
== IPP_SEND_DOCUMENT
)
311 if (cupsdTimeoutJob(job
))
315 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
316 "Job submission timed out.");
320 * Continue jobs that are waiting on the FilterLimit...
323 if (job
->pending_cost
> 0 &&
324 ((FilterLevel
+ job
->pending_cost
) < FilterLimit
|| FilterLevel
== 0))
325 cupsdContinueJob(job
);
328 * Start pending jobs if the destination is available...
331 if (job
->state_value
== IPP_JOB_PENDING
&& !NeedReload
&&
332 (!Sleeping
|| ACPower
) && !DoingShutdown
&& !job
->printer
)
334 printer
= cupsdFindDest(job
->dest
);
337 while (printer
&& (printer
->type
& CUPS_PRINTER_CLASS
))
340 * If the class is remote, just pass it to the remote server...
345 if (pclass
->state
== IPP_PRINTER_STOPPED
)
347 else if (pclass
->type
& CUPS_PRINTER_REMOTE
)
350 printer
= cupsdFindAvailablePrinter(printer
->name
);
353 if (!printer
&& !pclass
)
356 * Whoa, the printer and/or class for this destination went away;
360 cupsdSetJobState(job
, IPP_JOB_ABORTED
, CUPSD_JOB_PURGE
,
361 "Job aborted because the destination printer/class "
364 else if (printer
&& !printer
->holding_new_jobs
)
367 * See if the printer is available or remote and not printing a job;
368 * if so, start the job...
374 * Add/update a job-actual-printer-uri attribute for this job
375 * so that we know which printer actually printed the job...
378 if ((attr
= ippFindAttribute(job
->attrs
, "job-actual-printer-uri",
379 IPP_TAG_URI
)) != NULL
)
380 cupsdSetString(&attr
->values
[0].string
.text
, printer
->uri
);
382 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
,
383 "job-actual-printer-uri", NULL
, printer
->uri
);
386 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
389 if (!printer
->job
&& printer
->state
== IPP_PRINTER_IDLE
)
395 start_job(job
, printer
);
404 * 'cupsdCleanJobs()' - Clean out old jobs.
410 cupsd_job_t
*job
; /* Current job */
411 time_t curtime
; /* Current time */
414 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
415 "cupsdCleanJobs: MaxJobs=%d, JobHistory=%d, JobFiles=%d",
416 MaxJobs
, JobHistory
, JobFiles
);
418 if (MaxJobs
<= 0 && JobHistory
== INT_MAX
&& JobFiles
== INT_MAX
)
421 curtime
= time(NULL
);
422 JobHistoryUpdate
= 0;
424 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
426 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
428 if (job
->state_value
>= IPP_JOB_CANCELED
&& !job
->printer
)
431 * Expire old jobs (or job files)...
434 if ((MaxJobs
> 0 && cupsArrayCount(Jobs
) >= MaxJobs
) ||
435 (job
->history_time
&& job
->history_time
<= curtime
))
437 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Removing from history.");
438 cupsdDeleteJob(job
, CUPSD_JOB_PURGE
);
440 else if (job
->file_time
&& job
->file_time
<= curtime
)
442 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Removing document files.");
443 remove_job_files(job
);
445 if (job
->history_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
446 JobHistoryUpdate
= job
->history_time
;
450 if (job
->history_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
451 JobHistoryUpdate
= job
->history_time
;
453 if (job
->file_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
454 JobHistoryUpdate
= job
->file_time
;
459 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdCleanJobs: JobHistoryUpdate=%ld",
460 (long)JobHistoryUpdate
);
465 * 'cupsdContinueJob()' - Continue printing with the next file in a job.
469 cupsdContinueJob(cupsd_job_t
*job
) /* I - Job */
471 int i
; /* Looping var */
472 int slot
; /* Pipe slot */
473 cups_array_t
*filters
= NULL
,/* Filters for job */
474 *prefilters
; /* Filters with prefilters */
475 mime_filter_t
*filter
, /* Current filter */
476 *prefilter
, /* Prefilter */
477 port_monitor
; /* Port monitor filter */
478 char scheme
[255]; /* Device URI scheme */
479 ipp_attribute_t
*attr
; /* Current attribute */
480 const char *ptr
, /* Pointer into value */
481 *abort_message
; /* Abort message */
482 ipp_jstate_t abort_state
= IPP_JOB_STOPPED
;
483 /* New job state on abort */
484 struct stat backinfo
; /* Backend file information */
485 int backroot
; /* Run backend as root? */
486 int pid
; /* Process ID of new filter process */
487 int banner_page
; /* 1 if banner page, 0 otherwise */
488 int filterfds
[2][2] = { { -1, -1 }, { -1, -1 } };
489 /* Pipes used between filters */
490 int envc
; /* Number of environment variables */
491 struct stat fileinfo
; /* Job file information */
492 int argc
= 0; /* Number of arguments */
493 char **argv
= NULL
, /* Filter command-line arguments */
494 filename
[1024], /* Job filename */
495 command
[1024], /* Full path to command */
496 jobid
[255], /* Job ID string */
498 /* Job title string */
499 copies
[255], /* # copies string */
500 *options
, /* Options string */
502 /* Environment variables */
503 charset
[255], /* CHARSET env variable */
504 class_name
[255],/* CLASS env variable */
505 classification
[1024],
506 /* CLASSIFICATION env variable */
508 /* CONTENT_TYPE env variable */
510 /* DEVICE_URI env variable */
511 final_content_type
[1024] = "",
512 /* FINAL_CONTENT_TYPE env variable */
513 lang
[255], /* LANG env variable */
516 /* APPLE_LANGUAGE env variable */
517 #endif /* __APPLE__ */
518 auth_info_required
[255],
519 /* AUTH_INFO_REQUIRED env variable */
520 ppd
[1024], /* PPD env variable */
522 /* PRINTER_INFO env variable */
523 printer_location
[255],
524 /* PRINTER_LOCATION env variable */
526 /* PRINTER env variable */
527 *printer_state_reasons
= NULL
,
528 /* PRINTER_STATE_REASONS env var */
530 /* RIP_MAX_CACHE env variable */
533 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
534 "cupsdContinueJob(job=%p(%d)): current_file=%d, num_files=%d",
535 job
, job
->id
, job
->current_file
, job
->num_files
);
538 * Figure out what filters are required to convert from
539 * the source to the destination type...
542 FilterLevel
-= job
->cost
;
545 job
->pending_cost
= 0;
547 memset(job
->filters
, 0, sizeof(job
->filters
));
549 if (job
->printer
->raw
)
552 * Remote jobs and raw queues go directly to the printer without
556 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Sending job to queue tagged as raw...");
561 * Local jobs get filtered...
564 mime_type_t
*dst
= job
->printer
->filetype
;
565 /* Destination file type */
567 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
568 job
->id
, job
->current_file
+ 1);
569 if (stat(filename
, &fileinfo
))
570 fileinfo
.st_size
= 0;
572 if (job
->retry_as_raster
)
575 * Need to figure out whether the printer supports image/pwg-raster or
576 * image/urf, and use the corresponding type...
579 char type
[MIME_MAX_TYPE
]; /* MIME media type for printer */
581 snprintf(type
, sizeof(type
), "%s/image/urf", job
->printer
->name
);
582 if ((dst
= mimeType(MimeDatabase
, "printer", type
)) == NULL
)
584 snprintf(type
, sizeof(type
), "%s/image/pwg-raster", job
->printer
->name
);
585 dst
= mimeType(MimeDatabase
, "printer", type
);
589 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Retrying job as \"%s\".", strchr(dst
->type
, '/') + 1);
591 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Unable to retry job using a supported raster format.");
594 filters
= mimeFilter2(MimeDatabase
, job
->filetypes
[job
->current_file
], (size_t)fileinfo
.st_size
, dst
, &(job
->cost
));
598 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
599 "Unable to convert file %d to printable format.",
602 abort_message
= "Aborting job because it cannot be printed.";
603 abort_state
= IPP_JOB_ABORTED
;
605 ippSetString(job
->attrs
, &job
->reasons
, 0, "document-unprintable-error");
610 * Figure out the final content type...
613 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "%d filters for job:",
614 cupsArrayCount(filters
));
615 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
617 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
618 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "%s (%s/%s to %s/%s, cost %d)",
620 filter
->src
? filter
->src
->super
: "???",
621 filter
->src
? filter
->src
->type
: "???",
622 filter
->dst
? filter
->dst
->super
: "???",
623 filter
->dst
? filter
->dst
->type
: "???",
626 if (!job
->printer
->remote
)
628 for (filter
= (mime_filter_t
*)cupsArrayLast(filters
);
629 filter
&& filter
->dst
;
630 filter
= (mime_filter_t
*)cupsArrayPrev(filters
))
631 if (strcmp(filter
->dst
->super
, "printer") ||
632 strcmp(filter
->dst
->type
, job
->printer
->name
))
635 if (filter
&& filter
->dst
)
637 if ((ptr
= strchr(filter
->dst
->type
, '/')) != NULL
)
638 snprintf(final_content_type
, sizeof(final_content_type
),
639 "FINAL_CONTENT_TYPE=%s", ptr
+ 1);
641 snprintf(final_content_type
, sizeof(final_content_type
),
642 "FINAL_CONTENT_TYPE=%s/%s", filter
->dst
->super
,
646 snprintf(final_content_type
, sizeof(final_content_type
),
647 "FINAL_CONTENT_TYPE=printer/%s", job
->printer
->name
);
651 * Remove NULL ("-") filters...
654 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
656 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
657 if (!strcmp(filter
->filter
, "-"))
658 cupsArrayRemove(filters
, filter
);
660 if (cupsArrayCount(filters
) == 0)
662 cupsArrayDelete(filters
);
667 * If this printer has any pre-filters, insert the required pre-filter
668 * in the filters array...
671 if (job
->printer
->prefiltertype
&& filters
)
673 prefilters
= cupsArrayNew(NULL
, NULL
);
675 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
677 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
679 if ((prefilter
= mimeFilterLookup(MimeDatabase
, filter
->src
,
680 job
->printer
->prefiltertype
)))
682 cupsArrayAdd(prefilters
, prefilter
);
683 job
->cost
+= prefilter
->cost
;
686 cupsArrayAdd(prefilters
, filter
);
689 cupsArrayDelete(filters
);
690 filters
= prefilters
;
695 * Set a minimum cost of 100 for all jobs so that FilterLimit
696 * works with raw queues and other low-cost paths.
703 * See if the filter cost is too high...
706 if ((FilterLevel
+ job
->cost
) > FilterLimit
&& FilterLevel
> 0 &&
710 * Don't print this job quite yet...
713 cupsArrayDelete(filters
);
715 cupsdLogJob(job
, CUPSD_LOG_INFO
,
716 "Holding because filter limit has been reached.");
717 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
718 "cupsdContinueJob: file=%d, cost=%d, level=%d, limit=%d",
719 job
->current_file
, job
->cost
, FilterLevel
,
722 job
->pending_cost
= job
->cost
;
727 FilterLevel
+= job
->cost
;
730 * Add decompression/raw filter as needed...
733 if (job
->compressions
[job
->current_file
] &&
734 (!job
->printer
->remote
|| job
->num_files
== 1))
737 * Add gziptoany filter to the front of the list...
741 filters
= cupsArrayNew(NULL
, NULL
);
743 if (!cupsArrayInsert(filters
, &gziptoany_filter
))
745 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
746 "Unable to add decompression filter - %s", strerror(errno
));
748 cupsArrayDelete(filters
);
750 abort_message
= "Stopping job because the scheduler ran out of memory.";
757 * Add port monitor, if any...
760 if (job
->printer
->port_monitor
)
763 * Add port monitor to the end of the list...
767 filters
= cupsArrayNew(NULL
, NULL
);
769 port_monitor
.src
= NULL
;
770 port_monitor
.dst
= NULL
;
771 port_monitor
.cost
= 0;
773 snprintf(port_monitor
.filter
, sizeof(port_monitor
.filter
),
774 "%s/monitor/%s", ServerBin
, job
->printer
->port_monitor
);
776 if (!cupsArrayAdd(filters
, &port_monitor
))
778 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
779 "Unable to add port monitor - %s", strerror(errno
));
781 abort_message
= "Stopping job because the scheduler ran out of memory.";
788 * Make sure we don't go over the "MAX_FILTERS" limit...
791 if (cupsArrayCount(filters
) > MAX_FILTERS
)
793 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
794 "Too many filters (%d > %d), unable to print.",
795 cupsArrayCount(filters
), MAX_FILTERS
);
797 abort_message
= "Aborting job because it needs too many filters to print.";
798 abort_state
= IPP_JOB_ABORTED
;
800 ippSetString(job
->attrs
, &job
->reasons
, 0, "document-unprintable-error");
806 * Determine if we are printing a banner page or not...
809 if (job
->job_sheets
== NULL
)
811 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "No job-sheets attribute.");
812 if ((job
->job_sheets
=
813 ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_ZERO
)) != NULL
)
814 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
815 "... but someone added one without setting job_sheets.");
817 else if (job
->job_sheets
->num_values
== 1)
818 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "job-sheets=%s",
819 job
->job_sheets
->values
[0].string
.text
);
821 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "job-sheets=%s,%s",
822 job
->job_sheets
->values
[0].string
.text
,
823 job
->job_sheets
->values
[1].string
.text
);
825 if (job
->printer
->type
& CUPS_PRINTER_REMOTE
)
827 else if (job
->job_sheets
== NULL
)
829 else if (_cups_strcasecmp(job
->job_sheets
->values
[0].string
.text
, "none") != 0 &&
830 job
->current_file
== 0)
832 else if (job
->job_sheets
->num_values
> 1 &&
833 _cups_strcasecmp(job
->job_sheets
->values
[1].string
.text
, "none") != 0 &&
834 job
->current_file
== (job
->num_files
- 1))
839 if ((options
= get_options(job
, banner_page
, copies
, sizeof(copies
), title
,
840 sizeof(title
))) == NULL
)
842 abort_message
= "Stopping job because the scheduler ran out of memory.";
848 * Build the command-line arguments for the filters. Each filter
849 * has 6 or 7 arguments:
857 * argv[6] = filename (optional; normally stdin)
859 * This allows legacy printer drivers that use the old System V
860 * printing interface to be used by CUPS.
862 * For remote jobs, we send all of the files in the argument list.
865 if (job
->printer
->remote
)
866 argc
= 6 + job
->num_files
;
870 if ((argv
= calloc((size_t)argc
+ 1, sizeof(char *))) == NULL
)
872 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Unable to allocate argument array - %s",
875 abort_message
= "Stopping job because the scheduler ran out of memory.";
880 sprintf(jobid
, "%d", job
->id
);
882 argv
[0] = job
->printer
->name
;
884 argv
[2] = job
->username
;
889 if (job
->printer
->remote
&& job
->num_files
> 1)
891 for (i
= 0; i
< job
->num_files
; i
++)
893 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
895 argv
[6 + i
] = strdup(filename
);
900 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
901 job
->id
, job
->current_file
+ 1);
902 argv
[6] = strdup(filename
);
905 for (i
= 0; argv
[i
]; i
++)
906 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "argv[%d]=\"%s\"", i
, argv
[i
]);
909 * Create environment variable strings for the filters...
912 attr
= ippFindAttribute(job
->attrs
, "attributes-natural-language",
916 strlcpy(apple_language
, "APPLE_LANGUAGE=", sizeof(apple_language
));
917 _cupsAppleLanguage(attr
->values
[0].string
.text
,
918 apple_language
+ 15, sizeof(apple_language
) - 15);
919 #endif /* __APPLE__ */
921 switch (strlen(attr
->values
[0].string
.text
))
925 * This is an unknown or badly formatted language code; use
926 * the POSIX locale...
929 strlcpy(lang
, "LANG=C", sizeof(lang
));
934 * Just the language code (ll)...
937 snprintf(lang
, sizeof(lang
), "LANG=%s.UTF-8",
938 attr
->values
[0].string
.text
);
943 * Language and country code (ll-cc)...
946 snprintf(lang
, sizeof(lang
), "LANG=%c%c_%c%c.UTF-8",
947 attr
->values
[0].string
.text
[0],
948 attr
->values
[0].string
.text
[1],
949 toupper(attr
->values
[0].string
.text
[3] & 255),
950 toupper(attr
->values
[0].string
.text
[4] & 255));
954 if ((attr
= ippFindAttribute(job
->attrs
, "document-format",
955 IPP_TAG_MIMETYPE
)) != NULL
&&
956 (ptr
= strstr(attr
->values
[0].string
.text
, "charset=")) != NULL
)
957 snprintf(charset
, sizeof(charset
), "CHARSET=%s", ptr
+ 8);
959 strlcpy(charset
, "CHARSET=utf-8", sizeof(charset
));
961 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s/%s",
962 job
->filetypes
[job
->current_file
]->super
,
963 job
->filetypes
[job
->current_file
]->type
);
964 snprintf(device_uri
, sizeof(device_uri
), "DEVICE_URI=%s",
965 job
->printer
->device_uri
);
966 snprintf(ppd
, sizeof(ppd
), "PPD=%s/ppd/%s.ppd", ServerRoot
,
968 snprintf(printer_info
, sizeof(printer_name
), "PRINTER_INFO=%s",
969 job
->printer
->info
? job
->printer
->info
: "");
970 snprintf(printer_location
, sizeof(printer_name
), "PRINTER_LOCATION=%s",
971 job
->printer
->location
? job
->printer
->location
: "");
972 snprintf(printer_name
, sizeof(printer_name
), "PRINTER=%s", job
->printer
->name
);
973 if (job
->printer
->num_reasons
> 0)
975 char *psrptr
; /* Pointer into PRINTER_STATE_REASONS */
976 size_t psrlen
; /* Size of PRINTER_STATE_REASONS */
978 for (psrlen
= 22, i
= 0; i
< job
->printer
->num_reasons
; i
++)
979 psrlen
+= strlen(job
->printer
->reasons
[i
]) + 1;
981 if ((printer_state_reasons
= malloc(psrlen
)) != NULL
)
984 * All of these strcpy's are safe because we allocated the psr string...
987 strlcpy(printer_state_reasons
, "PRINTER_STATE_REASONS=", psrlen
);
988 for (psrptr
= printer_state_reasons
+ 22, i
= 0;
989 i
< job
->printer
->num_reasons
;
994 strlcpy(psrptr
, job
->printer
->reasons
[i
], psrlen
- (size_t)(psrptr
- printer_state_reasons
));
995 psrptr
+= strlen(psrptr
);
999 snprintf(rip_max_cache
, sizeof(rip_max_cache
), "RIP_MAX_CACHE=%s", RIPCache
);
1001 if (job
->printer
->num_auth_info_required
== 1)
1002 snprintf(auth_info_required
, sizeof(auth_info_required
),
1003 "AUTH_INFO_REQUIRED=%s",
1004 job
->printer
->auth_info_required
[0]);
1005 else if (job
->printer
->num_auth_info_required
== 2)
1006 snprintf(auth_info_required
, sizeof(auth_info_required
),
1007 "AUTH_INFO_REQUIRED=%s,%s",
1008 job
->printer
->auth_info_required
[0],
1009 job
->printer
->auth_info_required
[1]);
1010 else if (job
->printer
->num_auth_info_required
== 3)
1011 snprintf(auth_info_required
, sizeof(auth_info_required
),
1012 "AUTH_INFO_REQUIRED=%s,%s,%s",
1013 job
->printer
->auth_info_required
[0],
1014 job
->printer
->auth_info_required
[1],
1015 job
->printer
->auth_info_required
[2]);
1016 else if (job
->printer
->num_auth_info_required
== 4)
1017 snprintf(auth_info_required
, sizeof(auth_info_required
),
1018 "AUTH_INFO_REQUIRED=%s,%s,%s,%s",
1019 job
->printer
->auth_info_required
[0],
1020 job
->printer
->auth_info_required
[1],
1021 job
->printer
->auth_info_required
[2],
1022 job
->printer
->auth_info_required
[3]);
1024 strlcpy(auth_info_required
, "AUTH_INFO_REQUIRED=none",
1025 sizeof(auth_info_required
));
1027 envc
= cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
1029 envp
[envc
++] = charset
;
1030 envp
[envc
++] = lang
;
1032 envp
[envc
++] = apple_language
;
1033 #endif /* __APPLE__ */
1034 envp
[envc
++] = ppd
;
1035 envp
[envc
++] = rip_max_cache
;
1036 envp
[envc
++] = content_type
;
1037 envp
[envc
++] = device_uri
;
1038 envp
[envc
++] = printer_info
;
1039 envp
[envc
++] = printer_location
;
1040 envp
[envc
++] = printer_name
;
1041 envp
[envc
++] = printer_state_reasons
? printer_state_reasons
:
1042 "PRINTER_STATE_REASONS=none";
1043 envp
[envc
++] = banner_page
? "CUPS_FILETYPE=job-sheet" :
1044 "CUPS_FILETYPE=document";
1046 if (final_content_type
[0])
1047 envp
[envc
++] = final_content_type
;
1049 if (Classification
&& !banner_page
)
1051 if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
1052 IPP_TAG_NAME
)) == NULL
)
1053 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
1055 else if (attr
->num_values
> 1 &&
1056 strcmp(attr
->values
[1].string
.text
, "none") != 0)
1057 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
1058 attr
->values
[1].string
.text
);
1060 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
1061 attr
->values
[0].string
.text
);
1063 envp
[envc
++] = classification
;
1066 if (job
->dtype
& CUPS_PRINTER_CLASS
)
1068 snprintf(class_name
, sizeof(class_name
), "CLASS=%s", job
->dest
);
1069 envp
[envc
++] = class_name
;
1072 envp
[envc
++] = auth_info_required
;
1075 i
< (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0]));
1077 if (job
->auth_env
[i
])
1078 envp
[envc
++] = job
->auth_env
[i
];
1083 envp
[envc
++] = job
->auth_uid
;
1087 for (i
= 0; i
< envc
; i
++)
1088 if (!strncmp(envp
[i
], "AUTH_", 5))
1089 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"AUTH_%c****\"", i
,
1091 else if (strncmp(envp
[i
], "DEVICE_URI=", 11))
1092 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"%s\"", i
, envp
[i
]);
1094 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"DEVICE_URI=%s\"", i
,
1095 job
->printer
->sanitized_device_uri
);
1097 if (job
->printer
->remote
)
1098 job
->current_file
= job
->num_files
;
1100 job
->current_file
++;
1103 * Now create processes for all of the filters...
1106 for (i
= 0, slot
= 0, filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
1108 i
++, filter
= (mime_filter_t
*)cupsArrayNext(filters
))
1110 if (filter
->filter
[0] != '/')
1111 snprintf(command
, sizeof(command
), "%s/filter/%s", ServerBin
,
1114 strlcpy(command
, filter
->filter
, sizeof(command
));
1116 if (i
< (cupsArrayCount(filters
) - 1))
1118 if (cupsdOpenPipe(filterfds
[slot
]))
1120 abort_message
= "Stopping job because the scheduler could not create "
1121 "the filter pipes.";
1128 if (job
->current_file
== 1 ||
1129 (job
->printer
->pc
&& job
->printer
->pc
->single_file
))
1131 if (strncmp(job
->printer
->device_uri
, "file:", 5) != 0)
1133 if (cupsdOpenPipe(job
->print_pipes
))
1135 abort_message
= "Stopping job because the scheduler could not "
1136 "create the backend pipes.";
1143 job
->print_pipes
[0] = -1;
1144 if (!strcmp(job
->printer
->device_uri
, "file:/dev/null") ||
1145 !strcmp(job
->printer
->device_uri
, "file:///dev/null"))
1146 job
->print_pipes
[1] = -1;
1149 if (!strncmp(job
->printer
->device_uri
, "file:/dev/", 10))
1150 job
->print_pipes
[1] = open(job
->printer
->device_uri
+ 5,
1152 else if (!strncmp(job
->printer
->device_uri
, "file:///dev/", 12))
1153 job
->print_pipes
[1] = open(job
->printer
->device_uri
+ 7,
1155 else if (!strncmp(job
->printer
->device_uri
, "file:///", 8))
1156 job
->print_pipes
[1] = open(job
->printer
->device_uri
+ 7,
1157 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
1159 job
->print_pipes
[1] = open(job
->printer
->device_uri
+ 5,
1160 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
1162 if (job
->print_pipes
[1] < 0)
1164 abort_message
= "Stopping job because the scheduler could not "
1165 "open the output file.";
1170 fcntl(job
->print_pipes
[1], F_SETFD
,
1171 fcntl(job
->print_pipes
[1], F_GETFD
) | FD_CLOEXEC
);
1176 filterfds
[slot
][0] = job
->print_pipes
[0];
1177 filterfds
[slot
][1] = job
->print_pipes
[1];
1180 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
1181 filterfds
[slot
][1], job
->status_pipes
[1],
1182 job
->back_pipes
[0], job
->side_pipes
[0], 0,
1183 job
->profile
, job
, job
->filters
+ i
);
1185 cupsdClosePipe(filterfds
[!slot
]);
1189 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Unable to start filter \"%s\" - %s.",
1190 filter
->filter
, strerror(errno
));
1192 abort_message
= "Stopping job because the scheduler could not execute a "
1198 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Started filter %s (PID %d)", command
,
1210 cupsArrayDelete(filters
);
1214 * Finally, pipe the final output into a backend process if needed...
1217 if (strncmp(job
->printer
->device_uri
, "file:", 5) != 0)
1219 if (job
->current_file
== 1 || job
->printer
->remote
||
1220 (job
->printer
->pc
&& job
->printer
->pc
->single_file
))
1222 sscanf(job
->printer
->device_uri
, "%254[^:]", scheme
);
1223 snprintf(command
, sizeof(command
), "%s/backend/%s", ServerBin
, scheme
);
1226 * See if the backend needs to run as root...
1231 else if (stat(command
, &backinfo
))
1234 backroot
= !(backinfo
.st_mode
& (S_IWGRP
| S_IRWXO
));
1236 argv
[0] = job
->printer
->sanitized_device_uri
;
1238 filterfds
[slot
][0] = -1;
1239 filterfds
[slot
][1] = -1;
1241 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
1242 filterfds
[slot
][1], job
->status_pipes
[1],
1243 job
->back_pipes
[1], job
->side_pipes
[1],
1244 backroot
, job
->bprofile
, job
, &(job
->backend
));
1248 abort_message
= "Stopping job because the sheduler could not execute "
1255 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Started backend %s (PID %d)",
1260 if (job
->current_file
== job
->num_files
||
1261 (job
->printer
->pc
&& job
->printer
->pc
->single_file
))
1262 cupsdClosePipe(job
->print_pipes
);
1264 if (job
->current_file
== job
->num_files
)
1266 cupsdClosePipe(job
->back_pipes
);
1267 cupsdClosePipe(job
->side_pipes
);
1269 close(job
->status_pipes
[1]);
1270 job
->status_pipes
[1] = -1;
1275 filterfds
[slot
][0] = -1;
1276 filterfds
[slot
][1] = -1;
1278 if (job
->current_file
== job
->num_files
||
1279 (job
->printer
->pc
&& job
->printer
->pc
->single_file
))
1280 cupsdClosePipe(job
->print_pipes
);
1282 if (job
->current_file
== job
->num_files
)
1284 close(job
->status_pipes
[1]);
1285 job
->status_pipes
[1] = -1;
1289 cupsdClosePipe(filterfds
[slot
]);
1291 for (i
= 6; i
< argc
; i
++)
1297 if (printer_state_reasons
)
1298 free(printer_state_reasons
);
1300 cupsdAddSelect(job
->status_buffer
->fd
, (cupsd_selfunc_t
)update_job
, NULL
,
1303 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
, "Job #%d started.",
1310 * If we get here, we need to abort the current job and close out all
1311 * files and pipes...
1316 FilterLevel
-= job
->cost
;
1319 for (slot
= 0; slot
< 2; slot
++)
1320 cupsdClosePipe(filterfds
[slot
]);
1322 cupsArrayDelete(filters
);
1326 for (i
= 6; i
< argc
; i
++)
1331 if (printer_state_reasons
)
1332 free(printer_state_reasons
);
1334 cupsdClosePipe(job
->print_pipes
);
1335 cupsdClosePipe(job
->back_pipes
);
1336 cupsdClosePipe(job
->side_pipes
);
1338 cupsdRemoveSelect(job
->status_pipes
[0]);
1339 cupsdClosePipe(job
->status_pipes
);
1340 cupsdStatBufDelete(job
->status_buffer
);
1341 job
->status_buffer
= NULL
;
1344 * Update the printer and job state.
1347 cupsdSetJobState(job
, abort_state
, CUPSD_JOB_DEFAULT
, "%s", abort_message
);
1348 cupsdSetPrinterState(job
->printer
, IPP_PRINTER_IDLE
, 0);
1349 update_job_attrs(job
, 0);
1352 free_job_history(job
);
1354 cupsArrayRemove(PrintingJobs
, job
);
1357 * Clear the printer <-> job association...
1360 job
->printer
->job
= NULL
;
1361 job
->printer
= NULL
;
1366 * 'cupsdDeleteJob()' - Free all memory used by a job.
1370 cupsdDeleteJob(cupsd_job_t
*job
, /* I - Job */
1371 cupsd_jobaction_t action
)/* I - Action */
1373 int i
; /* Looping var */
1377 finalize_job(job
, 1);
1379 if (action
== CUPSD_JOB_PURGE
)
1380 remove_job_history(job
);
1382 cupsdClearString(&job
->username
);
1383 cupsdClearString(&job
->dest
);
1385 i
< (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0]));
1387 cupsdClearString(job
->auth_env
+ i
);
1388 cupsdClearString(&job
->auth_uid
);
1390 if (action
== CUPSD_JOB_PURGE
)
1391 remove_job_files(job
);
1392 else if (job
->num_files
> 0)
1394 free(job
->compressions
);
1395 free(job
->filetypes
);
1401 free_job_history(job
);
1405 cupsArrayRemove(Jobs
, job
);
1406 cupsArrayRemove(ActiveJobs
, job
);
1407 cupsArrayRemove(PrintingJobs
, job
);
1414 * 'cupsdFreeAllJobs()' - Free all jobs from memory.
1418 cupsdFreeAllJobs(void)
1420 cupsd_job_t
*job
; /* Current job */
1428 cupsdStopAllJobs(CUPSD_JOB_FORCE
, 0);
1431 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1433 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1434 cupsdDeleteJob(job
, CUPSD_JOB_DEFAULT
);
1436 cupsdReleaseSignals();
1441 * 'cupsdFindJob()' - Find the specified job.
1444 cupsd_job_t
* /* O - Job data */
1445 cupsdFindJob(int id
) /* I - Job ID */
1447 cupsd_job_t key
; /* Search key */
1452 return ((cupsd_job_t
*)cupsArrayFind(Jobs
, &key
));
1457 * 'cupsdGetCompletedJobs()'- Generate a completed jobs list.
1460 cups_array_t
* /* O - Array of jobs */
1461 cupsdGetCompletedJobs(
1462 cupsd_printer_t
*p
) /* I - Printer */
1464 cups_array_t
*list
; /* Array of jobs */
1465 cupsd_job_t
*job
; /* Current job */
1468 list
= cupsArrayNew(compare_completed_jobs
, NULL
);
1470 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1472 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1473 if ((!p
|| !_cups_strcasecmp(p
->name
, job
->dest
)) && job
->state_value
>= IPP_JOB_STOPPED
&& job
->completed_time
)
1474 cupsArrayAdd(list
, job
);
1481 * 'cupsdGetPrinterJobCount()' - Get the number of pending, processing,
1482 * or held jobs in a printer or class.
1485 int /* O - Job count */
1486 cupsdGetPrinterJobCount(
1487 const char *dest
) /* I - Printer or class name */
1489 int count
; /* Job count */
1490 cupsd_job_t
*job
; /* Current job */
1493 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
1495 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
1496 if (job
->dest
&& !_cups_strcasecmp(job
->dest
, dest
))
1504 * 'cupsdGetUserJobCount()' - Get the number of pending, processing,
1505 * or held jobs for a user.
1508 int /* O - Job count */
1509 cupsdGetUserJobCount(
1510 const char *username
) /* I - Username */
1512 int count
; /* Job count */
1513 cupsd_job_t
*job
; /* Current job */
1516 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
1518 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
1519 if (!_cups_strcasecmp(job
->username
, username
))
1527 * 'cupsdLoadAllJobs()' - Load all jobs from disk.
1531 cupsdLoadAllJobs(void)
1533 char filename
[1024]; /* Full filename of job.cache file */
1534 struct stat fileinfo
; /* Information on job.cache file */
1535 cups_dir_t
*dir
; /* RequestRoot dir */
1536 cups_dentry_t
*dent
; /* Entry in RequestRoot */
1537 int load_cache
= 1; /* Load the job.cache file? */
1541 * Create the job arrays as needed...
1545 Jobs
= cupsArrayNew(compare_jobs
, NULL
);
1548 ActiveJobs
= cupsArrayNew(compare_active_jobs
, NULL
);
1551 PrintingJobs
= cupsArrayNew(compare_jobs
, NULL
);
1554 * See whether the job.cache file is older than the RequestRoot directory...
1557 snprintf(filename
, sizeof(filename
), "%s/job.cache", CacheDir
);
1559 if (stat(filename
, &fileinfo
))
1562 * No job.cache file...
1567 if (errno
!= ENOENT
)
1568 cupsdLogMessage(CUPSD_LOG_ERROR
,
1569 "Unable to get file information for \"%s\" - %s",
1570 filename
, strerror(errno
));
1572 else if ((dir
= cupsDirOpen(RequestRoot
)) == NULL
)
1575 * No spool directory...
1582 while ((dent
= cupsDirRead(dir
)) != NULL
)
1584 if (strlen(dent
->filename
) >= 6 && dent
->filename
[0] == 'c' && dent
->fileinfo
.st_mtime
> fileinfo
.st_mtime
)
1587 * Job history file is newer than job.cache file...
1599 * Load the most recent source for job data...
1605 * Load the job.cache file...
1608 load_job_cache(filename
);
1613 * Load the job history files...
1616 load_request_root();
1618 load_next_job_id(filename
);
1622 * Clean out old jobs as needed...
1625 if (MaxJobs
> 0 && cupsArrayCount(Jobs
) >= MaxJobs
)
1631 * 'cupsdLoadJob()' - Load a single job.
1634 int /* O - 1 on success, 0 on failure */
1635 cupsdLoadJob(cupsd_job_t
*job
) /* I - Job */
1637 int i
; /* Looping var */
1638 char jobfile
[1024]; /* Job filename */
1639 cups_file_t
*fp
; /* Job file */
1640 int fileid
; /* Current file ID */
1641 ipp_attribute_t
*attr
; /* Job attribute */
1642 const char *dest
; /* Destination name */
1643 cupsd_printer_t
*destptr
; /* Pointer to destination */
1644 mime_type_t
**filetypes
; /* New filetypes array */
1645 int *compressions
; /* New compressions array */
1650 if (job
->state_value
> IPP_JOB_STOPPED
)
1651 job
->access_time
= time(NULL
);
1656 if ((job
->attrs
= ippNew()) == NULL
)
1658 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Ran out of memory for job attributes.");
1663 * Load job attributes...
1666 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Loading attributes...");
1668 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, job
->id
);
1669 if ((fp
= cupsdOpenConfFile(jobfile
)) == NULL
)
1672 if (ippReadIO(fp
, (ipp_iocb_t
)cupsFileRead
, 1, NULL
, job
->attrs
) != IPP_DATA
)
1674 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1675 "Unable to read job control file \"%s\".", jobfile
);
1683 * Copy attribute data to the job object...
1686 if (!ippFindAttribute(job
->attrs
, "time-at-creation", IPP_TAG_INTEGER
))
1688 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1689 "Missing or bad time-at-creation attribute in control file.");
1693 if ((job
->state
= ippFindAttribute(job
->attrs
, "job-state",
1694 IPP_TAG_ENUM
)) == NULL
)
1696 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1697 "Missing or bad job-state attribute in control file.");
1701 job
->state_value
= (ipp_jstate_t
)job
->state
->values
[0].integer
;
1703 job
->history_time
= 0;
1705 if ((attr
= ippFindAttribute(job
->attrs
, "time-at-creation", IPP_TAG_INTEGER
)) != NULL
)
1706 job
->creation_time
= attr
->values
[0].integer
;
1708 if (job
->state_value
>= IPP_JOB_CANCELED
&& (attr
= ippFindAttribute(job
->attrs
, "time-at-completed", IPP_TAG_INTEGER
)) != NULL
)
1710 job
->completed_time
= attr
->values
[0].integer
;
1712 if (JobHistory
< INT_MAX
)
1713 job
->history_time
= attr
->values
[0].integer
+ JobHistory
;
1715 job
->history_time
= INT_MAX
;
1717 if (job
->history_time
< time(NULL
))
1718 goto error
; /* Expired, remove from history */
1720 if (job
->history_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
1721 JobHistoryUpdate
= job
->history_time
;
1723 if (JobFiles
< INT_MAX
)
1724 job
->file_time
= attr
->values
[0].integer
+ JobFiles
;
1726 job
->file_time
= INT_MAX
;
1728 if (job
->file_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
1729 JobHistoryUpdate
= job
->file_time
;
1731 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdLoadJob: JobHistoryUpdate=%ld",
1732 (long)JobHistoryUpdate
);
1737 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1738 IPP_TAG_URI
)) == NULL
)
1740 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1741 "No job-printer-uri attribute in control file.");
1745 if ((dest
= cupsdValidateDest(attr
->values
[0].string
.text
, &(job
->dtype
),
1748 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1749 "Unable to queue job for destination \"%s\".",
1750 attr
->values
[0].string
.text
);
1754 cupsdSetString(&job
->dest
, dest
);
1756 else if ((destptr
= cupsdFindDest(job
->dest
)) == NULL
)
1758 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1759 "Unable to queue job for destination \"%s\".",
1764 if ((job
->reasons
= ippFindAttribute(job
->attrs
, "job-state-reasons",
1765 IPP_TAG_KEYWORD
)) == NULL
)
1767 const char *reason
; /* job-state-reason keyword */
1769 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
1770 "Adding missing job-state-reasons attribute to control file.");
1772 switch (job
->state_value
)
1775 case IPP_JOB_PENDING
:
1776 if (destptr
->state
== IPP_PRINTER_STOPPED
)
1777 reason
= "printer-stopped";
1783 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1784 IPP_TAG_ZERO
)) != NULL
&&
1785 (attr
->value_tag
== IPP_TAG_NAME
||
1786 attr
->value_tag
== IPP_TAG_NAMELANG
||
1787 attr
->value_tag
== IPP_TAG_KEYWORD
) &&
1788 strcmp(attr
->values
[0].string
.text
, "no-hold"))
1789 reason
= "job-hold-until-specified";
1791 reason
= "job-incoming";
1794 case IPP_JOB_PROCESSING
:
1795 reason
= "job-printing";
1798 case IPP_JOB_STOPPED
:
1799 reason
= "job-stopped";
1802 case IPP_JOB_CANCELED
:
1803 reason
= "job-canceled-by-user";
1806 case IPP_JOB_ABORTED
:
1807 reason
= "aborted-by-system";
1810 case IPP_JOB_COMPLETED
:
1811 reason
= "job-completed-successfully";
1815 job
->reasons
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1816 "job-state-reasons", NULL
, reason
);
1818 else if (job
->state_value
== IPP_JOB_PENDING
)
1820 if (destptr
->state
== IPP_PRINTER_STOPPED
)
1821 ippSetString(job
->attrs
, &job
->reasons
, 0, "printer-stopped");
1823 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
1826 job
->sheets
= ippFindAttribute(job
->attrs
, "job-media-sheets-completed",
1828 job
->job_sheets
= ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_NAME
);
1832 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
1833 IPP_TAG_INTEGER
)) == NULL
)
1835 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1836 "Missing or bad job-priority attribute in control file.");
1840 job
->priority
= attr
->values
[0].integer
;
1845 if ((attr
= ippFindAttribute(job
->attrs
, "job-originating-user-name",
1846 IPP_TAG_NAME
)) == NULL
)
1848 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1849 "Missing or bad job-originating-user-name "
1850 "attribute in control file.");
1854 cupsdSetString(&job
->username
, attr
->values
[0].string
.text
);
1859 if ((attr
= ippFindAttribute(job
->attrs
, "job-name", IPP_TAG_NAME
)) != NULL
)
1860 cupsdSetString(&job
->name
, attr
->values
[0].string
.text
);
1864 * Set the job hold-until time and state...
1867 if (job
->state_value
== IPP_JOB_HELD
)
1869 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1870 IPP_TAG_KEYWORD
)) == NULL
)
1871 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1874 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
, CUPSD_JOB_DEFAULT
);
1877 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1878 job
->state_value
= IPP_JOB_PENDING
;
1881 else if (job
->state_value
== IPP_JOB_PROCESSING
)
1883 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1884 job
->state_value
= IPP_JOB_PENDING
;
1887 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets", IPP_TAG_INTEGER
)) != NULL
)
1888 job
->koctets
= attr
->values
[0].integer
;
1890 if (!job
->num_files
)
1893 * Find all the d##### files...
1896 for (fileid
= 1; fileid
< 10000; fileid
++)
1898 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
1901 if (access(jobfile
, 0))
1904 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
1905 "Auto-typing document file \"%s\"...", jobfile
);
1907 if (fileid
> job
->num_files
)
1909 if (job
->num_files
== 0)
1911 compressions
= (int *)calloc((size_t)fileid
, sizeof(int));
1912 filetypes
= (mime_type_t
**)calloc((size_t)fileid
, sizeof(mime_type_t
*));
1916 compressions
= (int *)realloc(job
->compressions
, sizeof(int) * (size_t)fileid
);
1917 filetypes
= (mime_type_t
**)realloc(job
->filetypes
, sizeof(mime_type_t
*) * (size_t)fileid
);
1921 job
->compressions
= compressions
;
1924 job
->filetypes
= filetypes
;
1926 if (!compressions
|| !filetypes
)
1928 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1929 "Ran out of memory for job file types.");
1931 ippDelete(job
->attrs
);
1934 if (job
->compressions
)
1936 free(job
->compressions
);
1937 job
->compressions
= NULL
;
1942 free(job
->filetypes
);
1943 job
->filetypes
= NULL
;
1950 job
->num_files
= fileid
;
1953 job
->filetypes
[fileid
- 1] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
1954 job
->compressions
+ fileid
- 1);
1956 if (!job
->filetypes
[fileid
- 1])
1957 job
->filetypes
[fileid
- 1] = mimeType(MimeDatabase
, "application",
1963 * Load authentication information as needed...
1966 if (job
->state_value
< IPP_JOB_STOPPED
)
1968 snprintf(jobfile
, sizeof(jobfile
), "%s/a%05d", RequestRoot
, job
->id
);
1971 i
< (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0]));
1973 cupsdClearString(job
->auth_env
+ i
);
1974 cupsdClearString(&job
->auth_uid
);
1976 if ((fp
= cupsFileOpen(jobfile
, "r")) != NULL
)
1978 int bytes
, /* Size of auth data */
1979 linenum
= 1; /* Current line number */
1980 char line
[65536], /* Line from file */
1981 *value
, /* Value from line */
1982 data
[65536]; /* Decoded data */
1985 if (cupsFileGets(fp
, line
, sizeof(line
)) &&
1986 !strcmp(line
, "CUPSD-AUTH-V3"))
1989 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
1995 if (strcmp(line
, "negotiate") && strcmp(line
, "uid"))
1997 bytes
= sizeof(data
);
1998 httpDecode64_2(data
, &bytes
, value
);
2002 * Assign environment variables...
2005 if (!strcmp(line
, "uid"))
2007 cupsdSetStringf(&job
->auth_uid
, "AUTH_UID=%s", value
);
2010 else if (i
>= (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0])))
2013 if (!strcmp(line
, "username"))
2014 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_USERNAME=%s", data
);
2015 else if (!strcmp(line
, "domain"))
2016 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_DOMAIN=%s", data
);
2017 else if (!strcmp(line
, "password"))
2018 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_PASSWORD=%s", data
);
2019 else if (!strcmp(line
, "negotiate"))
2020 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_NEGOTIATE=%s", value
);
2032 job
->access_time
= time(NULL
);
2036 * If we get here then something bad happened...
2041 ippDelete(job
->attrs
);
2044 remove_job_history(job
);
2045 remove_job_files(job
);
2052 * 'cupsdMoveJob()' - Move the specified job to a different destination.
2056 cupsdMoveJob(cupsd_job_t
*job
, /* I - Job */
2057 cupsd_printer_t
*p
) /* I - Destination printer or class */
2059 ipp_attribute_t
*attr
; /* job-printer-uri attribute */
2060 const char *olddest
; /* Old destination */
2061 cupsd_printer_t
*oldp
; /* Old pointer */
2065 * Don't move completed jobs...
2068 if (job
->state_value
> IPP_JOB_STOPPED
)
2072 * Get the old destination...
2075 olddest
= job
->dest
;
2078 oldp
= job
->printer
;
2080 oldp
= cupsdFindDest(olddest
);
2083 * Change the destination information...
2086 if (job
->state_value
> IPP_JOB_HELD
)
2087 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
2088 "Stopping job prior to move.");
2090 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, oldp
, job
,
2091 "Job #%d moved from %s to %s.", job
->id
, olddest
,
2094 cupsdSetString(&job
->dest
, p
->name
);
2095 job
->dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_REMOTE
);
2097 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
2098 IPP_TAG_URI
)) != NULL
)
2099 cupsdSetString(&(attr
->values
[0].string
.text
), p
->uri
);
2101 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, p
, job
,
2102 "Job #%d moved from %s to %s.", job
->id
, olddest
,
2106 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2111 * 'cupsdReleaseJob()' - Release the specified job.
2115 cupsdReleaseJob(cupsd_job_t
*job
) /* I - Job */
2117 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdReleaseJob(job=%p(%d))", job
,
2120 if (job
->state_value
== IPP_JOB_HELD
)
2123 * Add trailing banner as needed...
2126 if (job
->pending_timeout
)
2127 cupsdTimeoutJob(job
);
2129 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
2130 "Job released by user.");
2136 * 'cupsdRestartJob()' - Restart the specified job.
2140 cupsdRestartJob(cupsd_job_t
*job
) /* I - Job */
2142 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdRestartJob(job=%p(%d))", job
,
2145 if (job
->state_value
== IPP_JOB_STOPPED
|| job
->num_files
)
2146 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
2147 "Job restarted by user.");
2152 * 'cupsdSaveAllJobs()' - Save a summary of all jobs to disk.
2156 cupsdSaveAllJobs(void)
2158 int i
; /* Looping var */
2159 cups_file_t
*fp
; /* job.cache file */
2160 char filename
[1024], /* job.cache filename */
2161 temp
[1024]; /* Temporary string */
2162 cupsd_job_t
*job
; /* Current job */
2163 time_t curtime
; /* Current time */
2164 struct tm
*curdate
; /* Current date */
2167 snprintf(filename
, sizeof(filename
), "%s/job.cache", CacheDir
);
2168 if ((fp
= cupsdCreateConfFile(filename
, ConfigFilePerm
)) == NULL
)
2171 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving job.cache...");
2174 * Write a small header to the file...
2177 curtime
= time(NULL
);
2178 curdate
= localtime(&curtime
);
2179 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
2181 cupsFilePuts(fp
, "# Job cache file for " CUPS_SVERSION
"\n");
2182 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
2183 cupsFilePrintf(fp
, "NextJobId %d\n", NextJobId
);
2186 * Write each job known to the system...
2189 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
2191 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
2193 cupsFilePrintf(fp
, "<Job %d>\n", job
->id
);
2194 cupsFilePrintf(fp
, "State %d\n", job
->state_value
);
2195 cupsFilePrintf(fp
, "Created %ld\n", (long)job
->creation_time
);
2196 if (job
->completed_time
)
2197 cupsFilePrintf(fp
, "Completed %ld\n", (long)job
->completed_time
);
2198 cupsFilePrintf(fp
, "Priority %d\n", job
->priority
);
2199 if (job
->hold_until
)
2200 cupsFilePrintf(fp
, "HoldUntil %ld\n", (long)job
->hold_until
);
2201 cupsFilePrintf(fp
, "Username %s\n", job
->username
);
2203 cupsFilePutConf(fp
, "Name", job
->name
);
2204 cupsFilePrintf(fp
, "Destination %s\n", job
->dest
);
2205 cupsFilePrintf(fp
, "DestType %d\n", job
->dtype
);
2206 cupsFilePrintf(fp
, "KOctets %d\n", job
->koctets
);
2207 cupsFilePrintf(fp
, "NumFiles %d\n", job
->num_files
);
2208 for (i
= 0; i
< job
->num_files
; i
++)
2209 cupsFilePrintf(fp
, "File %d %s/%s %d\n", i
+ 1, job
->filetypes
[i
]->super
,
2210 job
->filetypes
[i
]->type
, job
->compressions
[i
]);
2211 cupsFilePuts(fp
, "</Job>\n");
2214 cupsdCloseCreatedConfFile(fp
, filename
);
2219 * 'cupsdSaveJob()' - Save a job to disk.
2223 cupsdSaveJob(cupsd_job_t
*job
) /* I - Job */
2225 char filename
[1024]; /* Job control filename */
2226 cups_file_t
*fp
; /* Job file */
2229 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSaveJob(job=%p(%d)): job->attrs=%p",
2230 job
, job
->id
, job
->attrs
);
2232 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
, job
->id
);
2234 if ((fp
= cupsdCreateConfFile(filename
, ConfigFilePerm
& 0600)) == NULL
)
2237 fchown(cupsFileNumber(fp
), RunUser
, Group
);
2239 job
->attrs
->state
= IPP_IDLE
;
2241 if (ippWriteIO(fp
, (ipp_iocb_t
)cupsFileWrite
, 1, NULL
,
2242 job
->attrs
) != IPP_DATA
)
2244 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Unable to write job control file.");
2249 if (!cupsdCloseCreatedConfFile(fp
, filename
))
2252 * Remove backup file and mark this job as clean...
2255 strlcat(filename
, ".O", sizeof(filename
));
2264 * 'cupsdSetJobHoldUntil()' - Set the hold time for a job.
2268 cupsdSetJobHoldUntil(cupsd_job_t
*job
, /* I - Job */
2269 const char *when
, /* I - When to resume */
2270 int update
)/* I - Update job-hold-until attr? */
2272 time_t curtime
; /* Current time */
2273 struct tm
*curdate
; /* Current date */
2274 int hour
; /* Hold hour */
2275 int minute
; /* Hold minute */
2276 int second
= 0; /* Hold second */
2279 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2280 "cupsdSetJobHoldUntil(job=%p(%d), when=\"%s\", update=%d)",
2281 job
, job
->id
, when
, update
);
2286 * Update the job-hold-until attribute...
2289 ipp_attribute_t
*attr
; /* job-hold-until attribute */
2291 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2292 IPP_TAG_KEYWORD
)) == NULL
)
2293 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2296 cupsdSetString(&(attr
->values
[0].string
.text
), when
);
2298 attr
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
2299 "job-hold-until", NULL
, when
);
2303 if (isdigit(when
[0] & 255))
2304 attr
->value_tag
= IPP_TAG_NAME
;
2306 attr
->value_tag
= IPP_TAG_KEYWORD
;
2309 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2314 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-hold-until-specified");
2317 * Update the hold time...
2320 job
->cancel_time
= 0;
2322 if (!strcmp(when
, "indefinite") || !strcmp(when
, "auth-info-required"))
2325 * Hold indefinitely...
2328 job
->hold_until
= 0;
2330 if (MaxHoldTime
> 0)
2331 job
->cancel_time
= time(NULL
) + MaxHoldTime
;
2333 else if (!strcmp(when
, "day-time"))
2336 * Hold to 6am the next morning unless local time is < 6pm.
2339 curtime
= time(NULL
);
2340 curdate
= localtime(&curtime
);
2342 if (curdate
->tm_hour
< 18)
2343 job
->hold_until
= curtime
;
2345 job
->hold_until
= curtime
+
2346 ((29 - curdate
->tm_hour
) * 60 + 59 -
2347 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2349 else if (!strcmp(when
, "evening") || !strcmp(when
, "night"))
2352 * Hold to 6pm unless local time is > 6pm or < 6am.
2355 curtime
= time(NULL
);
2356 curdate
= localtime(&curtime
);
2358 if (curdate
->tm_hour
< 6 || curdate
->tm_hour
>= 18)
2359 job
->hold_until
= curtime
;
2361 job
->hold_until
= curtime
+
2362 ((17 - curdate
->tm_hour
) * 60 + 59 -
2363 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2365 else if (!strcmp(when
, "second-shift"))
2368 * Hold to 4pm unless local time is > 4pm.
2371 curtime
= time(NULL
);
2372 curdate
= localtime(&curtime
);
2374 if (curdate
->tm_hour
>= 16)
2375 job
->hold_until
= curtime
;
2377 job
->hold_until
= curtime
+
2378 ((15 - curdate
->tm_hour
) * 60 + 59 -
2379 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2381 else if (!strcmp(when
, "third-shift"))
2384 * Hold to 12am unless local time is < 8am.
2387 curtime
= time(NULL
);
2388 curdate
= localtime(&curtime
);
2390 if (curdate
->tm_hour
< 8)
2391 job
->hold_until
= curtime
;
2393 job
->hold_until
= curtime
+
2394 ((23 - curdate
->tm_hour
) * 60 + 59 -
2395 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2397 else if (!strcmp(when
, "weekend"))
2400 * Hold to weekend unless we are in the weekend.
2403 curtime
= time(NULL
);
2404 curdate
= localtime(&curtime
);
2406 if (curdate
->tm_wday
== 0 || curdate
->tm_wday
== 6)
2407 job
->hold_until
= curtime
;
2409 job
->hold_until
= curtime
+
2410 (((5 - curdate
->tm_wday
) * 24 +
2411 (17 - curdate
->tm_hour
)) * 60 + 59 -
2412 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2414 else if (sscanf(when
, "%d:%d:%d", &hour
, &minute
, &second
) >= 2)
2417 * Hold to specified GMT time (HH:MM or HH:MM:SS)...
2420 curtime
= time(NULL
);
2421 curdate
= gmtime(&curtime
);
2423 job
->hold_until
= curtime
+
2424 ((hour
- curdate
->tm_hour
) * 60 + minute
-
2425 curdate
->tm_min
) * 60 + second
- curdate
->tm_sec
;
2428 * Hold until next day as needed...
2431 if (job
->hold_until
< curtime
)
2432 job
->hold_until
+= 24 * 60 * 60;
2435 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetJobHoldUntil: hold_until=%d",
2436 (int)job
->hold_until
);
2441 * 'cupsdSetJobPriority()' - Set the priority of a job, moving it up/down in
2442 * the list as needed.
2446 cupsdSetJobPriority(
2447 cupsd_job_t
*job
, /* I - Job ID */
2448 int priority
) /* I - New priority (0 to 100) */
2450 ipp_attribute_t
*attr
; /* Job attribute */
2454 * Don't change completed jobs...
2457 if (job
->state_value
>= IPP_JOB_PROCESSING
)
2461 * Set the new priority and re-add the job into the active list...
2464 cupsArrayRemove(ActiveJobs
, job
);
2466 job
->priority
= priority
;
2468 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
2469 IPP_TAG_INTEGER
)) != NULL
)
2470 attr
->values
[0].integer
= priority
;
2472 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-priority",
2475 cupsArrayAdd(ActiveJobs
, job
);
2478 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2483 * 'cupsdSetJobState()' - Set the state of the specified print job.
2488 cupsd_job_t
*job
, /* I - Job to cancel */
2489 ipp_jstate_t newstate
, /* I - New job state */
2490 cupsd_jobaction_t action
, /* I - Action to take */
2491 const char *message
, /* I - Message to log */
2492 ...) /* I - Additional arguments as needed */
2494 int i
; /* Looping var */
2495 ipp_jstate_t oldstate
; /* Old state */
2496 char filename
[1024]; /* Job filename */
2497 ipp_attribute_t
*attr
; /* Job attribute */
2500 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2501 "cupsdSetJobState(job=%p(%d), state=%d, newstate=%d, "
2502 "action=%d, message=\"%s\")", job
, job
->id
, job
->state_value
,
2503 newstate
, action
, message
? message
: "(null)");
2507 * Make sure we have the job attributes...
2510 if (!cupsdLoadJob(job
))
2514 * Don't do anything if the state is unchanged and we aren't purging the
2518 oldstate
= job
->state_value
;
2519 if (newstate
== oldstate
&& action
!= CUPSD_JOB_PURGE
)
2523 * Stop any processes that are working on the current job...
2526 if (oldstate
== IPP_JOB_PROCESSING
)
2527 stop_job(job
, action
);
2530 * Set the new job state...
2533 job
->state_value
= newstate
;
2536 job
->state
->values
[0].integer
= newstate
;
2540 case IPP_JOB_PENDING
:
2542 * Update job-hold-until as needed...
2545 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2546 IPP_TAG_KEYWORD
)) == NULL
)
2547 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2551 attr
->value_tag
= IPP_TAG_KEYWORD
;
2552 cupsdSetString(&(attr
->values
[0].string
.text
), "no-hold");
2558 case IPP_JOB_ABORTED
:
2559 case IPP_JOB_CANCELED
:
2560 case IPP_JOB_COMPLETED
:
2561 set_time(job
, "time-at-completed");
2562 ippSetString(job
->attrs
, &job
->reasons
, 0, "processing-to-stop-point");
2567 * Log message as needed...
2572 char buffer
[2048]; /* Message buffer */
2573 va_list ap
; /* Pointer to additional arguments */
2575 va_start(ap
, message
);
2576 vsnprintf(buffer
, sizeof(buffer
), message
, ap
);
2579 if (newstate
> IPP_JOB_STOPPED
)
2580 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
, "%s", buffer
);
2582 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
, "%s", buffer
);
2584 if (newstate
== IPP_JOB_STOPPED
|| newstate
== IPP_JOB_ABORTED
)
2585 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "%s", buffer
);
2587 cupsdLogJob(job
, CUPSD_LOG_INFO
, "%s", buffer
);
2591 * Handle post-state-change actions...
2596 case IPP_JOB_PROCESSING
:
2598 * Add the job to the "printing" list...
2601 if (!cupsArrayFind(PrintingJobs
, job
))
2602 cupsArrayAdd(PrintingJobs
, job
);
2605 * Set the processing time...
2608 set_time(job
, "time-at-processing");
2610 case IPP_JOB_PENDING
:
2612 case IPP_JOB_STOPPED
:
2614 * Make sure the job is in the active list...
2617 if (!cupsArrayFind(ActiveJobs
, job
))
2618 cupsArrayAdd(ActiveJobs
, job
);
2621 * Save the job state to disk...
2625 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2628 case IPP_JOB_ABORTED
:
2629 case IPP_JOB_CANCELED
:
2630 case IPP_JOB_COMPLETED
:
2631 if (newstate
== IPP_JOB_CANCELED
)
2634 * Remove the job from the active list if there are no processes still
2638 for (i
= 0; job
->filters
[i
] < 0; i
++);
2640 if (!job
->filters
[i
] && job
->backend
<= 0)
2641 cupsArrayRemove(ActiveJobs
, job
);
2646 * Otherwise just remove the job from the active list immediately...
2649 cupsArrayRemove(ActiveJobs
, job
);
2653 * Expire job subscriptions since the job is now "completed"...
2656 cupsdExpireSubscriptions(NULL
, job
);
2660 * If we are going to sleep and the PrintingJobs count is now 0, allow the
2661 * sleep to happen immediately...
2664 if (Sleeping
&& cupsArrayCount(PrintingJobs
) == 0)
2666 #endif /* __APPLE__ */
2669 * Remove any authentication data...
2672 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
2673 if (cupsdRemoveFile(filename
) && errno
!= ENOENT
)
2674 cupsdLogMessage(CUPSD_LOG_ERROR
,
2675 "Unable to remove authentication cache: %s",
2679 i
< (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0]));
2681 cupsdClearString(job
->auth_env
+ i
);
2683 cupsdClearString(&job
->auth_uid
);
2686 * Remove the print file for good if we aren't preserving jobs or
2690 if (!JobHistory
|| !JobFiles
|| action
== CUPSD_JOB_PURGE
)
2691 remove_job_files(job
);
2693 if (JobHistory
&& action
!= CUPSD_JOB_PURGE
)
2696 * Save job state info...
2700 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2702 else if (!job
->printer
)
2705 * Delete the job immediately if not actively printing...
2708 cupsdDeleteJob(job
, CUPSD_JOB_PURGE
);
2715 * Finalize the job immediately if we forced things...
2718 if (action
>= CUPSD_JOB_FORCE
&& job
&& job
->printer
)
2719 finalize_job(job
, 0);
2722 * Update the server "busy" state...
2725 cupsdSetBusyState();
2730 * 'cupsdStopAllJobs()' - Stop all print jobs.
2735 cupsd_jobaction_t action
, /* I - Action */
2736 int kill_delay
) /* I - Number of seconds before we kill */
2738 cupsd_job_t
*job
; /* Current job */
2741 DEBUG_puts("cupsdStopAllJobs()");
2743 for (job
= (cupsd_job_t
*)cupsArrayFirst(PrintingJobs
);
2745 job
= (cupsd_job_t
*)cupsArrayNext(PrintingJobs
))
2749 cupsdSetJobState(job
, IPP_JOB_COMPLETED
, CUPSD_JOB_FORCE
, NULL
);
2754 job
->kill_time
= time(NULL
) + kill_delay
;
2756 cupsdSetJobState(job
, IPP_JOB_PENDING
, action
, NULL
);
2763 * 'cupsdUnloadCompletedJobs()' - Flush completed job history from memory.
2767 cupsdUnloadCompletedJobs(void)
2769 cupsd_job_t
*job
; /* Current job */
2770 time_t expire
; /* Expiration time */
2773 expire
= time(NULL
) - 60;
2775 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
2777 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
2778 if (job
->attrs
&& job
->state_value
>= IPP_JOB_STOPPED
&& !job
->printer
&&
2779 job
->access_time
< expire
)
2790 * 'cupsdUpdateJobs()' - Update the history/file files for all jobs.
2794 cupsdUpdateJobs(void)
2796 cupsd_job_t
*job
; /* Current job */
2797 time_t curtime
; /* Current time */
2798 ipp_attribute_t
*attr
; /* time-at-completed attribute */
2801 curtime
= time(NULL
);
2802 JobHistoryUpdate
= 0;
2804 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
2806 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
2808 if (job
->state_value
>= IPP_JOB_CANCELED
&&
2809 (attr
= ippFindAttribute(job
->attrs
, "time-at-completed",
2810 IPP_TAG_INTEGER
)) != NULL
)
2813 * Update history/file expiration times...
2816 if (JobHistory
< INT_MAX
)
2817 job
->history_time
= attr
->values
[0].integer
+ JobHistory
;
2819 job
->history_time
= INT_MAX
;
2821 if (job
->history_time
< curtime
)
2823 cupsdDeleteJob(job
, CUPSD_JOB_PURGE
);
2827 if (job
->history_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
2828 JobHistoryUpdate
= job
->history_time
;
2830 if (JobFiles
< INT_MAX
)
2831 job
->file_time
= attr
->values
[0].integer
+ JobFiles
;
2833 job
->file_time
= INT_MAX
;
2835 if (job
->file_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
2836 JobHistoryUpdate
= job
->file_time
;
2840 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdUpdateAllJobs: JobHistoryUpdate=%ld",
2841 (long)JobHistoryUpdate
);
2846 * 'compare_active_jobs()' - Compare the job IDs and priorities of two jobs.
2849 static int /* O - Difference */
2850 compare_active_jobs(void *first
, /* I - First job */
2851 void *second
, /* I - Second job */
2852 void *data
) /* I - App data (not used) */
2854 int diff
; /* Difference */
2859 if ((diff
= ((cupsd_job_t
*)second
)->priority
-
2860 ((cupsd_job_t
*)first
)->priority
) != 0)
2863 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
2868 * 'compare_completed_jobs()' - Compare the job IDs and completion times of two jobs.
2871 static int /* O - Difference */
2872 compare_completed_jobs(void *first
, /* I - First job */
2873 void *second
, /* I - Second job */
2874 void *data
) /* I - App data (not used) */
2876 int diff
; /* Difference */
2881 if ((diff
= ((cupsd_job_t
*)second
)->completed_time
-
2882 ((cupsd_job_t
*)first
)->completed_time
) != 0)
2885 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
2890 * 'compare_jobs()' - Compare the job IDs of two jobs.
2893 static int /* O - Difference */
2894 compare_jobs(void *first
, /* I - First job */
2895 void *second
, /* I - Second job */
2896 void *data
) /* I - App data (not used) */
2900 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
2905 * 'dump_job_history()' - Dump any debug messages for a job.
2909 dump_job_history(cupsd_job_t
*job
) /* I - Job */
2911 int i
, /* Looping var */
2912 oldsize
; /* Current MaxLogSize */
2913 struct tm
*date
; /* Date/time value */
2914 cupsd_joblog_t
*message
; /* Current message */
2915 char temp
[2048], /* Log message */
2916 *ptr
, /* Pointer into log message */
2917 start
[256], /* Start time */
2918 end
[256]; /* End time */
2919 cupsd_printer_t
*printer
; /* Printer for job */
2923 * See if we have anything to dump...
2930 * Disable log rotation temporarily...
2933 oldsize
= MaxLogSize
;
2937 * Copy the debug messages to the log...
2940 message
= (cupsd_joblog_t
*)cupsArrayFirst(job
->history
);
2941 date
= localtime(&(message
->time
));
2942 strftime(start
, sizeof(start
), "%X", date
);
2944 message
= (cupsd_joblog_t
*)cupsArrayLast(job
->history
);
2945 date
= localtime(&(message
->time
));
2946 strftime(end
, sizeof(end
), "%X", date
);
2948 snprintf(temp
, sizeof(temp
),
2949 "[Job %d] The following messages were recorded from %s to %s",
2950 job
->id
, start
, end
);
2951 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, temp
);
2953 for (message
= (cupsd_joblog_t
*)cupsArrayFirst(job
->history
);
2955 message
= (cupsd_joblog_t
*)cupsArrayNext(job
->history
))
2956 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, message
->message
);
2958 snprintf(temp
, sizeof(temp
), "[Job %d] End of messages", job
->id
);
2959 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, temp
);
2962 * Log the printer state values...
2965 if ((printer
= job
->printer
) == NULL
)
2966 printer
= cupsdFindDest(job
->dest
);
2970 snprintf(temp
, sizeof(temp
), "[Job %d] printer-state=%d(%s)", job
->id
,
2972 printer
->state
== IPP_PRINTER_IDLE
? "idle" :
2973 printer
->state
== IPP_PRINTER_PROCESSING
? "processing" :
2975 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, temp
);
2977 snprintf(temp
, sizeof(temp
), "[Job %d] printer-state-message=\"%s\"",
2978 job
->id
, printer
->state_message
);
2979 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, temp
);
2981 snprintf(temp
, sizeof(temp
), "[Job %d] printer-state-reasons=", job
->id
);
2982 ptr
= temp
+ strlen(temp
);
2983 if (printer
->num_reasons
== 0)
2984 strlcpy(ptr
, "none", sizeof(temp
) - (size_t)(ptr
- temp
));
2988 i
< printer
->num_reasons
&& ptr
< (temp
+ sizeof(temp
) - 2);
2994 strlcpy(ptr
, printer
->reasons
[i
], sizeof(temp
) - (size_t)(ptr
- temp
));
2998 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, temp
);
3002 * Restore log file rotation...
3005 MaxLogSize
= oldsize
;
3008 * Free all messages...
3011 free_job_history(job
);
3016 * 'free_job_history()' - Free any log history.
3020 free_job_history(cupsd_job_t
*job
) /* I - Job */
3022 char *message
; /* Current message */
3028 for (message
= (char *)cupsArrayFirst(job
->history
);
3030 message
= (char *)cupsArrayNext(job
->history
))
3033 cupsArrayDelete(job
->history
);
3034 job
->history
= NULL
;
3039 * 'finalize_job()' - Cleanup after job filter processes and support data.
3043 finalize_job(cupsd_job_t
*job
, /* I - Job */
3044 int set_job_state
) /* I - 1 = set the job state */
3046 ipp_pstate_t printer_state
; /* New printer state value */
3047 ipp_jstate_t job_state
; /* New job state value */
3048 const char *message
; /* Message for job state */
3049 char buffer
[1024]; /* Buffer for formatted messages */
3052 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "finalize_job(job=%p(%d))", job
, job
->id
);
3055 * Clear the "connecting-to-device" and "cups-waiting-for-job-completed"
3056 * reasons, which are only valid when a printer is processing, along with any
3057 * remote printing job state...
3060 cupsdSetPrinterReasons(job
->printer
, "-connecting-to-device,"
3061 "cups-waiting-for-job-completed,"
3062 "cups-remote-pending,"
3063 "cups-remote-pending-held,"
3064 "cups-remote-processing,"
3065 "cups-remote-stopped,"
3066 "cups-remote-canceled,"
3067 "cups-remote-aborted,"
3068 "cups-remote-completed");
3071 * Similarly, clear the "offline-report" reason for non-USB devices since we
3072 * rarely have current information for network devices...
3075 if (strncmp(job
->printer
->device_uri
, "usb:", 4) &&
3076 strncmp(job
->printer
->device_uri
, "ippusb:", 7))
3077 cupsdSetPrinterReasons(job
->printer
, "-offline-report");
3080 * Free the security profile...
3083 cupsdDestroyProfile(job
->profile
);
3084 job
->profile
= NULL
;
3085 cupsdDestroyProfile(job
->bprofile
);
3086 job
->bprofile
= NULL
;
3089 * Clear the unresponsive job watchdog timers...
3092 job
->cancel_time
= 0;
3096 * Close pipes and status buffer...
3099 cupsdClosePipe(job
->print_pipes
);
3100 cupsdClosePipe(job
->back_pipes
);
3101 cupsdClosePipe(job
->side_pipes
);
3103 cupsdRemoveSelect(job
->status_pipes
[0]);
3104 cupsdClosePipe(job
->status_pipes
);
3105 cupsdStatBufDelete(job
->status_buffer
);
3106 job
->status_buffer
= NULL
;
3109 * Process the exit status...
3112 if (job
->printer
->state
== IPP_PRINTER_PROCESSING
)
3113 printer_state
= IPP_PRINTER_IDLE
;
3115 printer_state
= job
->printer
->state
;
3117 switch (job_state
= job
->state_value
)
3119 case IPP_JOB_PENDING
:
3120 message
= "Job paused.";
3124 message
= "Job held.";
3128 case IPP_JOB_PROCESSING
:
3129 case IPP_JOB_COMPLETED
:
3130 job_state
= IPP_JOB_COMPLETED
;
3131 message
= "Job completed.";
3134 ippSetString(job
->attrs
, &job
->reasons
, 0,
3135 "job-completed-successfully");
3138 case IPP_JOB_STOPPED
:
3139 message
= "Job stopped.";
3141 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-stopped");
3144 case IPP_JOB_CANCELED
:
3145 message
= "Job canceled.";
3147 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-canceled-by-user");
3150 case IPP_JOB_ABORTED
:
3151 message
= "Job aborted.";
3155 if (job
->status
< 0)
3158 * Backend had errors...
3161 int exit_code
; /* Exit code from backend */
3164 * Convert the status to an exit code. Due to the way the W* macros are
3165 * implemented on MacOS X (bug?), we have to store the exit status in a
3166 * variable first and then convert...
3169 exit_code
= -job
->status
;
3170 if (WIFEXITED(exit_code
))
3171 exit_code
= WEXITSTATUS(exit_code
);
3174 ippSetString(job
->attrs
, &job
->reasons
, 0, "cups-backend-crashed");
3175 exit_code
= job
->status
;
3178 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Backend returned status %d (%s)",
3180 exit_code
== CUPS_BACKEND_FAILED
? "failed" :
3181 exit_code
== CUPS_BACKEND_AUTH_REQUIRED
?
3182 "authentication required" :
3183 exit_code
== CUPS_BACKEND_HOLD
? "hold job" :
3184 exit_code
== CUPS_BACKEND_STOP
? "stop printer" :
3185 exit_code
== CUPS_BACKEND_CANCEL
? "cancel job" :
3186 exit_code
== CUPS_BACKEND_RETRY
? "retry job later" :
3187 exit_code
== CUPS_BACKEND_RETRY_CURRENT
? "retry job immediately" :
3188 exit_code
< 0 ? "crashed" : "unknown");
3191 * Do what needs to be done...
3197 case CUPS_BACKEND_FAILED
:
3199 * Backend failure, use the error-policy to determine how to
3203 if (job
->dtype
& CUPS_PRINTER_CLASS
)
3206 * Queued on a class - mark the job as pending and we'll retry on
3207 * another printer...
3210 if (job_state
== IPP_JOB_COMPLETED
)
3212 job_state
= IPP_JOB_PENDING
;
3213 message
= "Retrying job on another printer.";
3215 ippSetString(job
->attrs
, &job
->reasons
, 0,
3216 "resources-are-not-ready");
3219 else if (!strcmp(job
->printer
->error_policy
, "retry-current-job"))
3222 * The error policy is "retry-current-job" - mark the job as pending
3223 * and we'll retry on the same printer...
3226 if (job_state
== IPP_JOB_COMPLETED
)
3228 job_state
= IPP_JOB_PENDING
;
3229 message
= "Retrying job on same printer.";
3231 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
3234 else if ((job
->printer
->type
& CUPS_PRINTER_FAX
) ||
3235 !strcmp(job
->printer
->error_policy
, "retry-job"))
3237 if (job_state
== IPP_JOB_COMPLETED
)
3240 * The job was queued on a fax or the error policy is "retry-job" -
3241 * hold the job if the number of retries is less than the
3242 * JobRetryLimit, otherwise abort the job.
3247 if (job
->tries
> JobRetryLimit
&& JobRetryLimit
> 0)
3253 snprintf(buffer
, sizeof(buffer
),
3254 "Job aborted after %d unsuccessful attempts.",
3256 job_state
= IPP_JOB_ABORTED
;
3259 ippSetString(job
->attrs
, &job
->reasons
, 0, "aborted-by-system");
3264 * Try again in N seconds...
3267 snprintf(buffer
, sizeof(buffer
),
3268 "Job held for %d seconds since it could not be sent.",
3271 job
->hold_until
= time(NULL
) + JobRetryInterval
;
3272 job_state
= IPP_JOB_HELD
;
3275 ippSetString(job
->attrs
, &job
->reasons
, 0,
3276 "resources-are-not-ready");
3280 else if (!strcmp(job
->printer
->error_policy
, "abort-job") &&
3281 job_state
== IPP_JOB_COMPLETED
)
3283 job_state
= IPP_JOB_ABORTED
;
3284 message
= "Job aborted due to backend errors; please consult "
3285 "the error_log file for details.";
3287 ippSetString(job
->attrs
, &job
->reasons
, 0, "aborted-by-system");
3289 else if (job
->state_value
== IPP_JOB_PROCESSING
)
3291 job_state
= IPP_JOB_PENDING
;
3292 printer_state
= IPP_PRINTER_STOPPED
;
3293 message
= "Printer stopped due to backend errors; please "
3294 "consult the error_log file for details.";
3296 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
3300 case CUPS_BACKEND_CANCEL
:
3305 if (job_state
== IPP_JOB_COMPLETED
)
3307 job_state
= IPP_JOB_CANCELED
;
3308 message
= "Job canceled at printer.";
3310 ippSetString(job
->attrs
, &job
->reasons
, 0, "canceled-at-device");
3314 case CUPS_BACKEND_HOLD
:
3315 if (job_state
== IPP_JOB_COMPLETED
)
3321 const char *reason
= ippGetString(job
->reasons
, 0, NULL
);
3323 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "job-state-reasons=\"%s\"",
3326 if (!reason
|| strncmp(reason
, "account-", 8))
3328 cupsdSetJobHoldUntil(job
, "indefinite", 1);
3330 ippSetString(job
->attrs
, &job
->reasons
, 0,
3331 "job-hold-until-specified");
3332 message
= "Job held indefinitely due to backend errors; please "
3333 "consult the error_log file for details.";
3335 else if (!strcmp(reason
, "account-info-needed"))
3337 cupsdSetJobHoldUntil(job
, "indefinite", 0);
3339 message
= "Job held indefinitely - account information is "
3342 else if (!strcmp(reason
, "account-closed"))
3344 cupsdSetJobHoldUntil(job
, "indefinite", 0);
3346 message
= "Job held indefinitely - account has been closed.";
3348 else if (!strcmp(reason
, "account-limit-reached"))
3350 cupsdSetJobHoldUntil(job
, "indefinite", 0);
3352 message
= "Job held indefinitely - account limit has been "
3357 cupsdSetJobHoldUntil(job
, "indefinite", 0);
3359 message
= "Job held indefinitely - account authorization failed.";
3362 job_state
= IPP_JOB_HELD
;
3366 case CUPS_BACKEND_STOP
:
3368 * Stop the printer...
3371 printer_state
= IPP_PRINTER_STOPPED
;
3372 message
= "Printer stopped due to backend errors; please "
3373 "consult the error_log file for details.";
3375 if (job_state
== IPP_JOB_COMPLETED
)
3377 job_state
= IPP_JOB_PENDING
;
3379 ippSetString(job
->attrs
, &job
->reasons
, 0,
3380 "resources-are-not-ready");
3384 case CUPS_BACKEND_AUTH_REQUIRED
:
3386 * Hold the job for authentication...
3389 if (job_state
== IPP_JOB_COMPLETED
)
3391 cupsdSetJobHoldUntil(job
, "auth-info-required", 1);
3393 job_state
= IPP_JOB_HELD
;
3394 message
= "Job held for authentication.";
3396 if (strncmp(job
->reasons
->values
[0].string
.text
, "account-", 8))
3397 ippSetString(job
->attrs
, &job
->reasons
, 0,
3398 "cups-held-for-authentication");
3402 case CUPS_BACKEND_RETRY
:
3403 if (job_state
== IPP_JOB_COMPLETED
)
3406 * Hold the job if the number of retries is less than the
3407 * JobRetryLimit, otherwise abort the job.
3412 if (job
->tries
> JobRetryLimit
&& JobRetryLimit
> 0)
3418 snprintf(buffer
, sizeof(buffer
),
3419 "Job aborted after %d unsuccessful attempts.",
3421 job_state
= IPP_JOB_ABORTED
;
3424 ippSetString(job
->attrs
, &job
->reasons
, 0, "aborted-by-system");
3429 * Try again in N seconds...
3432 snprintf(buffer
, sizeof(buffer
),
3433 "Job held for %d seconds since it could not be sent.",
3436 job
->hold_until
= time(NULL
) + JobRetryInterval
;
3437 job_state
= IPP_JOB_HELD
;
3440 ippSetString(job
->attrs
, &job
->reasons
, 0,
3441 "resources-are-not-ready");
3446 case CUPS_BACKEND_RETRY_CURRENT
:
3448 * Mark the job as pending and retry on the same printer...
3451 if (job_state
== IPP_JOB_COMPLETED
)
3453 job_state
= IPP_JOB_PENDING
;
3454 message
= "Retrying job on same printer.";
3456 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
3461 else if (job
->status
> 0)
3464 * Filter had errors; stop job...
3467 if (job_state
== IPP_JOB_COMPLETED
)
3469 job_state
= IPP_JOB_STOPPED
;
3470 message
= "Job stopped due to filter errors; please consult the "
3471 "error_log file for details.";
3473 if (WIFSIGNALED(job
->status
))
3474 ippSetString(job
->attrs
, &job
->reasons
, 0, "cups-filter-crashed");
3476 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-completed-with-errors");
3481 * Update the printer and job state.
3484 if (set_job_state
&& job_state
!= job
->state_value
)
3485 cupsdSetJobState(job
, job_state
, CUPSD_JOB_DEFAULT
, "%s", message
);
3487 cupsdSetPrinterState(job
->printer
, printer_state
,
3488 printer_state
== IPP_PRINTER_STOPPED
);
3489 update_job_attrs(job
, 0);
3494 (job
->state_value
== IPP_JOB_ABORTED
||
3495 job
->state_value
== IPP_JOB_STOPPED
))
3496 dump_job_history(job
);
3498 free_job_history(job
);
3501 cupsArrayRemove(PrintingJobs
, job
);
3504 * Apply any PPD updates...
3507 if (job
->num_keywords
)
3509 if (cupsdUpdatePrinterPPD(job
->printer
, job
->num_keywords
, job
->keywords
))
3510 cupsdSetPrinterAttrs(job
->printer
);
3512 cupsFreeOptions(job
->num_keywords
, job
->keywords
);
3514 job
->num_keywords
= 0;
3515 job
->keywords
= NULL
;
3519 * Clear the printer <-> job association...
3522 job
->printer
->job
= NULL
;
3523 job
->printer
= NULL
;
3526 * Try printing another job...
3529 if (printer_state
!= IPP_PRINTER_STOPPED
)
3535 * 'get_options()' - Get a string containing the job options.
3538 static char * /* O - Options string */
3539 get_options(cupsd_job_t
*job
, /* I - Job */
3540 int banner_page
, /* I - Printing a banner page? */
3541 char *copies
, /* I - Copies buffer */
3542 size_t copies_size
, /* I - Size of copies buffer */
3543 char *title
, /* I - Title buffer */
3544 size_t title_size
) /* I - Size of title buffer */
3546 int i
; /* Looping var */
3547 size_t newlength
; /* New option buffer length */
3548 char *optptr
, /* Pointer to options */
3549 *valptr
; /* Pointer in value string */
3550 ipp_attribute_t
*attr
; /* Current attribute */
3551 _ppd_cache_t
*pc
; /* PPD cache and mapping data */
3552 int num_pwgppds
; /* Number of PWG->PPD options */
3553 cups_option_t
*pwgppds
, /* PWG->PPD options */
3554 *pwgppd
, /* Current PWG->PPD option */
3555 *preset
; /* Current preset option */
3556 int print_color_mode
,
3557 /* Output mode (if any) */
3558 print_quality
; /* Print quality (if any) */
3559 const char *ppd
; /* PPD option choice */
3560 int exact
; /* Did we get an exact match? */
3561 static char *options
= NULL
;/* Full list of options */
3562 static size_t optlength
= 0; /* Length of option buffer */
3566 * Building the options string is harder than it needs to be, but for the
3567 * moment we need to pass strings for command-line args and not IPP attribute
3570 * First build an options array for any PWG->PPD mapped option/choice pairs.
3573 pc
= job
->printer
->pc
;
3578 !ippFindAttribute(job
->attrs
,
3579 "com.apple.print.DocumentTicket.PMSpoolFormat",
3581 !ippFindAttribute(job
->attrs
, "APPrinterPreset", IPP_TAG_ZERO
) &&
3582 (ippFindAttribute(job
->attrs
, "print-color-mode", IPP_TAG_ZERO
) ||
3583 ippFindAttribute(job
->attrs
, "print-quality", IPP_TAG_ZERO
)))
3586 * Map print-color-mode and print-quality to a preset...
3589 if ((attr
= ippFindAttribute(job
->attrs
, "print-color-mode",
3590 IPP_TAG_KEYWORD
)) != NULL
&&
3591 !strcmp(attr
->values
[0].string
.text
, "monochrome"))
3592 print_color_mode
= _PWG_PRINT_COLOR_MODE_MONOCHROME
;
3594 print_color_mode
= _PWG_PRINT_COLOR_MODE_COLOR
;
3596 if ((attr
= ippFindAttribute(job
->attrs
, "print-quality",
3597 IPP_TAG_ENUM
)) != NULL
&&
3598 attr
->values
[0].integer
>= IPP_QUALITY_DRAFT
&&
3599 attr
->values
[0].integer
<= IPP_QUALITY_HIGH
)
3600 print_quality
= attr
->values
[0].integer
- IPP_QUALITY_DRAFT
;
3602 print_quality
= _PWG_PRINT_QUALITY_NORMAL
;
3604 if (pc
->num_presets
[print_color_mode
][print_quality
] == 0)
3607 * Try to find a preset that works so that we maximize the chances of us
3608 * getting a good print using IPP attributes.
3611 if (pc
->num_presets
[print_color_mode
][_PWG_PRINT_QUALITY_NORMAL
] > 0)
3612 print_quality
= _PWG_PRINT_QUALITY_NORMAL
;
3613 else if (pc
->num_presets
[_PWG_PRINT_COLOR_MODE_COLOR
][print_quality
] > 0)
3614 print_color_mode
= _PWG_PRINT_COLOR_MODE_COLOR
;
3617 print_quality
= _PWG_PRINT_QUALITY_NORMAL
;
3618 print_color_mode
= _PWG_PRINT_COLOR_MODE_COLOR
;
3622 if (pc
->num_presets
[print_color_mode
][print_quality
] > 0)
3625 * Copy the preset options as long as the corresponding names are not
3626 * already defined in the IPP request...
3629 for (i
= pc
->num_presets
[print_color_mode
][print_quality
],
3630 preset
= pc
->presets
[print_color_mode
][print_quality
];
3634 if (!ippFindAttribute(job
->attrs
, preset
->name
, IPP_TAG_ZERO
))
3635 num_pwgppds
= cupsAddOption(preset
->name
, preset
->value
, num_pwgppds
,
3643 if (!ippFindAttribute(job
->attrs
, "InputSlot", IPP_TAG_ZERO
) &&
3644 !ippFindAttribute(job
->attrs
, "HPPaperSource", IPP_TAG_ZERO
))
3646 if ((ppd
= _ppdCacheGetInputSlot(pc
, job
->attrs
, NULL
)) != NULL
)
3647 num_pwgppds
= cupsAddOption(pc
->source_option
, ppd
, num_pwgppds
,
3650 if (!ippFindAttribute(job
->attrs
, "MediaType", IPP_TAG_ZERO
) &&
3651 (ppd
= _ppdCacheGetMediaType(pc
, job
->attrs
, NULL
)) != NULL
)
3652 num_pwgppds
= cupsAddOption("MediaType", ppd
, num_pwgppds
, &pwgppds
);
3654 if (!ippFindAttribute(job
->attrs
, "PageRegion", IPP_TAG_ZERO
) &&
3655 !ippFindAttribute(job
->attrs
, "PageSize", IPP_TAG_ZERO
) &&
3656 (ppd
= _ppdCacheGetPageSize(pc
, job
->attrs
, NULL
, &exact
)) != NULL
)
3658 num_pwgppds
= cupsAddOption("PageSize", ppd
, num_pwgppds
, &pwgppds
);
3660 if (!ippFindAttribute(job
->attrs
, "media", IPP_TAG_ZERO
))
3661 num_pwgppds
= cupsAddOption("media", ppd
, num_pwgppds
, &pwgppds
);
3664 if (!ippFindAttribute(job
->attrs
, "OutputBin", IPP_TAG_ZERO
) &&
3665 (attr
= ippFindAttribute(job
->attrs
, "output-bin",
3666 IPP_TAG_ZERO
)) != NULL
&&
3667 (attr
->value_tag
== IPP_TAG_KEYWORD
||
3668 attr
->value_tag
== IPP_TAG_NAME
) &&
3669 (ppd
= _ppdCacheGetOutputBin(pc
, attr
->values
[0].string
.text
)) != NULL
)
3672 * Map output-bin to OutputBin option...
3675 num_pwgppds
= cupsAddOption("OutputBin", ppd
, num_pwgppds
, &pwgppds
);
3678 if (pc
->sides_option
&&
3679 !ippFindAttribute(job
->attrs
, pc
->sides_option
, IPP_TAG_ZERO
) &&
3680 (attr
= ippFindAttribute(job
->attrs
, "sides", IPP_TAG_KEYWORD
)) != NULL
)
3683 * Map sides to duplex option...
3686 if (!strcmp(attr
->values
[0].string
.text
, "one-sided"))
3687 num_pwgppds
= cupsAddOption(pc
->sides_option
, pc
->sides_1sided
,
3688 num_pwgppds
, &pwgppds
);
3689 else if (!strcmp(attr
->values
[0].string
.text
, "two-sided-long-edge"))
3690 num_pwgppds
= cupsAddOption(pc
->sides_option
, pc
->sides_2sided_long
,
3691 num_pwgppds
, &pwgppds
);
3692 else if (!strcmp(attr
->values
[0].string
.text
, "two-sided-short-edge"))
3693 num_pwgppds
= cupsAddOption(pc
->sides_option
, pc
->sides_2sided_short
,
3694 num_pwgppds
, &pwgppds
);
3698 * Map finishings values...
3701 num_pwgppds
= _ppdCacheGetFinishingOptions(pc
, job
->attrs
,
3702 IPP_FINISHINGS_NONE
, num_pwgppds
,
3707 * Figure out how much room we need...
3710 newlength
= ipp_length(job
->attrs
);
3712 for (i
= num_pwgppds
, pwgppd
= pwgppds
; i
> 0; i
--, pwgppd
++)
3713 newlength
+= 1 + strlen(pwgppd
->name
) + 1 + strlen(pwgppd
->value
);
3716 * Then allocate/reallocate the option buffer as needed...
3719 if (newlength
== 0) /* This can never happen, but Clang */
3720 newlength
= 1; /* thinks it can... */
3722 if (newlength
> optlength
|| !options
)
3725 optptr
= malloc(newlength
);
3727 optptr
= realloc(options
, newlength
);
3731 cupsdLogJob(job
, CUPSD_LOG_CRIT
,
3732 "Unable to allocate " CUPS_LLFMT
" bytes for option buffer.",
3733 CUPS_LLCAST newlength
);
3738 optlength
= newlength
;
3742 * Now loop through the attributes and convert them to the textual
3743 * representation used by the filters...
3749 snprintf(title
, title_size
, "%s-%d", job
->printer
->name
, job
->id
);
3750 strlcpy(copies
, "1", copies_size
);
3752 for (attr
= job
->attrs
->attrs
; attr
!= NULL
; attr
= attr
->next
)
3754 if (!strcmp(attr
->name
, "copies") &&
3755 attr
->value_tag
== IPP_TAG_INTEGER
)
3758 * Don't use the # copies attribute if we are printing the job sheets...
3762 snprintf(copies
, copies_size
, "%d", attr
->values
[0].integer
);
3764 else if (!strcmp(attr
->name
, "job-name") &&
3765 (attr
->value_tag
== IPP_TAG_NAME
||
3766 attr
->value_tag
== IPP_TAG_NAMELANG
))
3767 strlcpy(title
, attr
->values
[0].string
.text
, title_size
);
3768 else if (attr
->group_tag
== IPP_TAG_JOB
)
3771 * Filter out other unwanted attributes...
3774 if (attr
->value_tag
== IPP_TAG_NOVALUE
||
3775 attr
->value_tag
== IPP_TAG_MIMETYPE
||
3776 attr
->value_tag
== IPP_TAG_NAMELANG
||
3777 attr
->value_tag
== IPP_TAG_TEXTLANG
||
3778 (attr
->value_tag
== IPP_TAG_URI
&& strcmp(attr
->name
, "job-uuid") &&
3779 strcmp(attr
->name
, "job-authorization-uri")) ||
3780 attr
->value_tag
== IPP_TAG_URISCHEME
||
3781 attr
->value_tag
== IPP_TAG_BEGIN_COLLECTION
) /* Not yet supported */
3784 if (!strcmp(attr
->name
, "job-hold-until") ||
3785 !strcmp(attr
->name
, "job-id") ||
3786 !strcmp(attr
->name
, "job-k-octets") ||
3787 !strcmp(attr
->name
, "job-media-sheets") ||
3788 !strcmp(attr
->name
, "job-media-sheets-completed") ||
3789 !strcmp(attr
->name
, "job-state") ||
3790 !strcmp(attr
->name
, "job-state-reasons"))
3793 if (!strncmp(attr
->name
, "job-", 4) &&
3794 strcmp(attr
->name
, "job-account-id") &&
3795 strcmp(attr
->name
, "job-accounting-user-id") &&
3796 strcmp(attr
->name
, "job-authorization-uri") &&
3797 strcmp(attr
->name
, "job-billing") &&
3798 strcmp(attr
->name
, "job-impressions") &&
3799 strcmp(attr
->name
, "job-originating-host-name") &&
3800 strcmp(attr
->name
, "job-password") &&
3801 strcmp(attr
->name
, "job-password-encryption") &&
3802 strcmp(attr
->name
, "job-uuid") &&
3803 !(job
->printer
->type
& CUPS_PRINTER_REMOTE
))
3806 if ((!strcmp(attr
->name
, "job-impressions") ||
3807 !strcmp(attr
->name
, "page-label") ||
3808 !strcmp(attr
->name
, "page-border") ||
3809 !strncmp(attr
->name
, "number-up", 9) ||
3810 !strcmp(attr
->name
, "page-ranges") ||
3811 !strcmp(attr
->name
, "page-set") ||
3812 !_cups_strcasecmp(attr
->name
, "AP_FIRSTPAGE_InputSlot") ||
3813 !_cups_strcasecmp(attr
->name
, "AP_FIRSTPAGE_ManualFeed") ||
3814 !_cups_strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
3815 "PMTotalSidesImaged..n.") ||
3816 !_cups_strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
3817 "PMTotalBeginPages..n.")) &&
3822 * Otherwise add them to the list...
3825 if (optptr
> options
)
3826 strlcat(optptr
, " ", optlength
- (size_t)(optptr
- options
));
3828 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
3830 strlcat(optptr
, attr
->name
, optlength
- (size_t)(optptr
- options
));
3831 strlcat(optptr
, "=", optlength
- (size_t)(optptr
- options
));
3834 for (i
= 0; i
< attr
->num_values
; i
++)
3837 strlcat(optptr
, ",", optlength
- (size_t)(optptr
- options
));
3839 optptr
+= strlen(optptr
);
3841 switch (attr
->value_tag
)
3843 case IPP_TAG_INTEGER
:
3845 snprintf(optptr
, optlength
- (size_t)(optptr
- options
),
3846 "%d", attr
->values
[i
].integer
);
3849 case IPP_TAG_BOOLEAN
:
3850 if (!attr
->values
[i
].boolean
)
3851 strlcat(optptr
, "no", optlength
- (size_t)(optptr
- options
));
3853 strlcat(optptr
, attr
->name
, optlength
- (size_t)(optptr
- options
));
3856 case IPP_TAG_RANGE
:
3857 if (attr
->values
[i
].range
.lower
== attr
->values
[i
].range
.upper
)
3858 snprintf(optptr
, optlength
- (size_t)(optptr
- options
) - 1,
3859 "%d", attr
->values
[i
].range
.lower
);
3861 snprintf(optptr
, optlength
- (size_t)(optptr
- options
) - 1,
3862 "%d-%d", attr
->values
[i
].range
.lower
,
3863 attr
->values
[i
].range
.upper
);
3866 case IPP_TAG_RESOLUTION
:
3867 snprintf(optptr
, optlength
- (size_t)(optptr
- options
) - 1,
3868 "%dx%d%s", attr
->values
[i
].resolution
.xres
,
3869 attr
->values
[i
].resolution
.yres
,
3870 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
3874 case IPP_TAG_STRING
:
3877 case IPP_TAG_KEYWORD
:
3878 case IPP_TAG_CHARSET
:
3879 case IPP_TAG_LANGUAGE
:
3881 for (valptr
= attr
->values
[i
].string
.text
; *valptr
;)
3883 if (strchr(" \t\n\\\'\"", *valptr
))
3885 *optptr
++ = *valptr
++;
3892 break; /* anti-compiler-warning-code */
3896 optptr
+= strlen(optptr
);
3901 * Finally loop through the PWG->PPD mapped options and add them...
3904 for (i
= num_pwgppds
, pwgppd
= pwgppds
; i
> 0; i
--, pwgppd
++)
3907 strlcpy(optptr
, pwgppd
->name
, optlength
- (size_t)(optptr
- options
));
3908 optptr
+= strlen(optptr
);
3910 strlcpy(optptr
, pwgppd
->value
, optlength
- (size_t)(optptr
- options
));
3911 optptr
+= strlen(optptr
);
3914 cupsFreeOptions(num_pwgppds
, pwgppds
);
3917 * Return the options string...
3925 * 'ipp_length()' - Compute the size of the buffer needed to hold
3926 * the textual IPP attributes.
3929 static size_t /* O - Size of attribute buffer */
3930 ipp_length(ipp_t
*ipp
) /* I - IPP request */
3932 size_t bytes
; /* Number of bytes */
3933 int i
; /* Looping var */
3934 ipp_attribute_t
*attr
; /* Current attribute */
3938 * Loop through all attributes...
3943 for (attr
= ipp
->attrs
; attr
!= NULL
; attr
= attr
->next
)
3946 * Skip attributes that won't be sent to filters...
3949 if (attr
->value_tag
== IPP_TAG_NOVALUE
||
3950 attr
->value_tag
== IPP_TAG_MIMETYPE
||
3951 attr
->value_tag
== IPP_TAG_NAMELANG
||
3952 attr
->value_tag
== IPP_TAG_TEXTLANG
||
3953 attr
->value_tag
== IPP_TAG_URI
||
3954 attr
->value_tag
== IPP_TAG_URISCHEME
)
3958 * Add space for a leading space and commas between each value.
3959 * For the first attribute, the leading space isn't used, so the
3960 * extra byte can be used as the nul terminator...
3963 bytes
++; /* " " separator */
3964 bytes
+= (size_t)attr
->num_values
; /* "," separators */
3967 * Boolean attributes appear as "foo,nofoo,foo,nofoo", while
3968 * other attributes appear as "foo=value1,value2,...,valueN".
3971 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
3972 bytes
+= strlen(attr
->name
);
3974 bytes
+= (size_t)attr
->num_values
* strlen(attr
->name
);
3977 * Now add the size required for each value in the attribute...
3980 switch (attr
->value_tag
)
3982 case IPP_TAG_INTEGER
:
3985 * Minimum value of a signed integer is -2147483647, or 11 digits.
3988 bytes
+= (size_t)attr
->num_values
* 11;
3991 case IPP_TAG_BOOLEAN
:
3993 * Add two bytes for each false ("no") value...
3996 for (i
= 0; i
< attr
->num_values
; i
++)
3997 if (!attr
->values
[i
].boolean
)
4001 case IPP_TAG_RANGE
:
4003 * A range is two signed integers separated by a hyphen, or
4004 * 23 characters max.
4007 bytes
+= (size_t)attr
->num_values
* 23;
4010 case IPP_TAG_RESOLUTION
:
4012 * A resolution is two signed integers separated by an "x" and
4013 * suffixed by the units, or 26 characters max.
4016 bytes
+= (size_t)attr
->num_values
* 26;
4019 case IPP_TAG_STRING
:
4022 case IPP_TAG_KEYWORD
:
4023 case IPP_TAG_CHARSET
:
4024 case IPP_TAG_LANGUAGE
:
4027 * Strings can contain characters that need quoting. We need
4028 * at least 2 * len + 2 characters to cover the quotes and
4029 * any backslashes in the string.
4032 for (i
= 0; i
< attr
->num_values
; i
++)
4033 bytes
+= 2 * strlen(attr
->values
[i
].string
.text
) + 2;
4037 break; /* anti-compiler-warning-code */
4046 * 'load_job_cache()' - Load jobs from the job.cache file.
4050 load_job_cache(const char *filename
) /* I - job.cache filename */
4052 cups_file_t
*fp
; /* job.cache file */
4053 char line
[1024], /* Line buffer */
4054 *value
; /* Value on line */
4055 int linenum
; /* Line number in file */
4056 cupsd_job_t
*job
; /* Current job */
4057 int jobid
; /* Job ID */
4058 char jobfile
[1024]; /* Job filename */
4062 * Open the job.cache file...
4065 if ((fp
= cupsdOpenConfFile(filename
)) == NULL
)
4067 load_request_root();
4072 * Read entries from the job cache file and create jobs as needed.
4075 cupsdLogMessage(CUPSD_LOG_INFO
, "Loading job cache file \"%s\"...",
4081 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
4083 if (!_cups_strcasecmp(line
, "NextJobId"))
4086 NextJobId
= atoi(value
);
4088 else if (!_cups_strcasecmp(line
, "<Job"))
4092 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing </Job> directive on line %d of %s.", linenum
, filename
);
4098 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing job ID on line %d of %s.", linenum
, filename
);
4102 jobid
= atoi(value
);
4106 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad job ID %d on line %d of %s.", jobid
, linenum
, filename
);
4110 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, jobid
);
4111 if (access(jobfile
, 0))
4113 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d.N", RequestRoot
, jobid
);
4114 if (access(jobfile
, 0))
4116 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Job %d] Files have gone away.",
4122 job
= calloc(1, sizeof(cupsd_job_t
));
4125 cupsdLogMessage(CUPSD_LOG_EMERG
,
4126 "[Job %d] Unable to allocate memory for job.", jobid
);
4131 job
->back_pipes
[0] = -1;
4132 job
->back_pipes
[1] = -1;
4133 job
->print_pipes
[0] = -1;
4134 job
->print_pipes
[1] = -1;
4135 job
->side_pipes
[0] = -1;
4136 job
->side_pipes
[1] = -1;
4137 job
->status_pipes
[0] = -1;
4138 job
->status_pipes
[1] = -1;
4140 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Loading from cache...");
4144 cupsdLogMessage(CUPSD_LOG_ERROR
,
4145 "Missing <Job #> directive on line %d of %s.", linenum
, filename
);
4148 else if (!_cups_strcasecmp(line
, "</Job>"))
4150 cupsArrayAdd(Jobs
, job
);
4152 if (job
->state_value
<= IPP_JOB_STOPPED
&& cupsdLoadJob(job
))
4153 cupsArrayAdd(ActiveJobs
, job
);
4154 else if (job
->state_value
> IPP_JOB_STOPPED
)
4156 if (!job
->completed_time
|| !job
->creation_time
|| !job
->name
|| !job
->koctets
)
4167 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing value on line %d of %s.", linenum
, filename
);
4170 else if (!_cups_strcasecmp(line
, "State"))
4172 job
->state_value
= (ipp_jstate_t
)atoi(value
);
4174 if (job
->state_value
< IPP_JOB_PENDING
)
4175 job
->state_value
= IPP_JOB_PENDING
;
4176 else if (job
->state_value
> IPP_JOB_COMPLETED
)
4177 job
->state_value
= IPP_JOB_COMPLETED
;
4179 else if (!_cups_strcasecmp(line
, "Name"))
4181 cupsdSetString(&(job
->name
), value
);
4183 else if (!_cups_strcasecmp(line
, "Created"))
4185 job
->creation_time
= strtol(value
, NULL
, 10);
4187 else if (!_cups_strcasecmp(line
, "Completed"))
4189 job
->completed_time
= strtol(value
, NULL
, 10);
4191 else if (!_cups_strcasecmp(line
, "HoldUntil"))
4193 job
->hold_until
= strtol(value
, NULL
, 10);
4195 else if (!_cups_strcasecmp(line
, "Priority"))
4197 job
->priority
= atoi(value
);
4199 else if (!_cups_strcasecmp(line
, "Username"))
4201 cupsdSetString(&job
->username
, value
);
4203 else if (!_cups_strcasecmp(line
, "Destination"))
4205 cupsdSetString(&job
->dest
, value
);
4207 else if (!_cups_strcasecmp(line
, "DestType"))
4209 job
->dtype
= (cups_ptype_t
)atoi(value
);
4211 else if (!_cups_strcasecmp(line
, "KOctets"))
4213 job
->koctets
= atoi(value
);
4215 else if (!_cups_strcasecmp(line
, "NumFiles"))
4217 job
->num_files
= atoi(value
);
4219 if (job
->num_files
< 0)
4221 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad NumFiles value %d on line %d of %s.", job
->num_files
, linenum
, filename
);
4226 if (job
->num_files
> 0)
4228 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-001", RequestRoot
,
4230 if (access(jobfile
, 0))
4232 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Data files have gone away.");
4237 job
->filetypes
= calloc((size_t)job
->num_files
, sizeof(mime_type_t
*));
4238 job
->compressions
= calloc((size_t)job
->num_files
, sizeof(int));
4240 if (!job
->filetypes
|| !job
->compressions
)
4242 cupsdLogJob(job
, CUPSD_LOG_EMERG
,
4243 "Unable to allocate memory for %d files.",
4249 else if (!_cups_strcasecmp(line
, "File"))
4251 int number
, /* File number */
4252 compression
; /* Compression value */
4253 char super
[MIME_MAX_SUPER
], /* MIME super type */
4254 type
[MIME_MAX_TYPE
]; /* MIME type */
4257 if (sscanf(value
, "%d%*[ \t]%15[^/]/%255s%d", &number
, super
, type
,
4260 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File on line %d of %s.", linenum
, filename
);
4264 if (number
< 1 || number
> job
->num_files
)
4266 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File number %d on line %d of %s.", number
, linenum
, filename
);
4272 job
->compressions
[number
] = compression
;
4273 job
->filetypes
[number
] = mimeType(MimeDatabase
, super
, type
);
4275 if (!job
->filetypes
[number
])
4278 * If the original MIME type is unknown, auto-type it!
4281 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
4282 "Unknown MIME type %s/%s for file %d.",
4283 super
, type
, number
+ 1);
4285 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
4286 job
->id
, number
+ 1);
4287 job
->filetypes
[number
] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
4288 job
->compressions
+ number
);
4291 * If that didn't work, assume it is raw...
4294 if (!job
->filetypes
[number
])
4295 job
->filetypes
[number
] = mimeType(MimeDatabase
, "application",
4300 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unknown %s directive on line %d of %s.", line
, linenum
, filename
);
4305 cupsdLogMessage(CUPSD_LOG_ERROR
,
4306 "Missing </Job> directive on line %d of %s.", linenum
, filename
);
4307 cupsdDeleteJob(job
, CUPSD_JOB_PURGE
);
4315 * 'load_next_job_id()' - Load the NextJobId value from the job.cache file.
4319 load_next_job_id(const char *filename
) /* I - job.cache filename */
4321 cups_file_t
*fp
; /* job.cache file */
4322 char line
[1024], /* Line buffer */
4323 *value
; /* Value on line */
4324 int linenum
; /* Line number in file */
4325 int next_job_id
; /* NextJobId value from line */
4329 * Read the NextJobId directive from the job.cache file and use
4330 * the value (if any).
4333 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
4335 if (errno
!= ENOENT
)
4336 cupsdLogMessage(CUPSD_LOG_ERROR
,
4337 "Unable to open job cache file \"%s\": %s",
4338 filename
, strerror(errno
));
4343 cupsdLogMessage(CUPSD_LOG_INFO
,
4344 "Loading NextJobId from job cache file \"%s\"...", filename
);
4348 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
4350 if (!_cups_strcasecmp(line
, "NextJobId"))
4354 next_job_id
= atoi(value
);
4356 if (next_job_id
> NextJobId
)
4357 NextJobId
= next_job_id
;
4368 * 'load_request_root()' - Load jobs from the RequestRoot directory.
4372 load_request_root(void)
4374 cups_dir_t
*dir
; /* Directory */
4375 cups_dentry_t
*dent
; /* Directory entry */
4376 cupsd_job_t
*job
; /* New job */
4380 * Open the requests directory...
4383 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Scanning %s for jobs...", RequestRoot
);
4385 if ((dir
= cupsDirOpen(RequestRoot
)) == NULL
)
4387 cupsdLogMessage(CUPSD_LOG_ERROR
,
4388 "Unable to open spool directory \"%s\": %s",
4389 RequestRoot
, strerror(errno
));
4394 * Read all the c##### files...
4397 while ((dent
= cupsDirRead(dir
)) != NULL
)
4398 if (strlen(dent
->filename
) >= 6 && dent
->filename
[0] == 'c')
4401 * Allocate memory for the job...
4404 if ((job
= calloc(sizeof(cupsd_job_t
), 1)) == NULL
)
4406 cupsdLogMessage(CUPSD_LOG_ERROR
, "Ran out of memory for jobs.");
4412 * Assign the job ID...
4415 job
->id
= atoi(dent
->filename
+ 1);
4416 job
->back_pipes
[0] = -1;
4417 job
->back_pipes
[1] = -1;
4418 job
->print_pipes
[0] = -1;
4419 job
->print_pipes
[1] = -1;
4420 job
->side_pipes
[0] = -1;
4421 job
->side_pipes
[1] = -1;
4422 job
->status_pipes
[0] = -1;
4423 job
->status_pipes
[1] = -1;
4425 if (job
->id
>= NextJobId
)
4426 NextJobId
= job
->id
+ 1;
4432 if (cupsdLoadJob(job
))
4435 * Insert the job into the array, sorting by job priority and ID...
4438 cupsArrayAdd(Jobs
, job
);
4440 if (job
->state_value
<= IPP_JOB_STOPPED
)
4441 cupsArrayAdd(ActiveJobs
, job
);
4454 * 'remove_job_files()' - Remove the document files for a job.
4458 remove_job_files(cupsd_job_t
*job
) /* I - Job */
4460 int i
; /* Looping var */
4461 char filename
[1024]; /* Document filename */
4464 if (job
->num_files
<= 0)
4467 for (i
= 1; i
<= job
->num_files
; i
++)
4469 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
4471 cupsdUnlinkOrRemoveFile(filename
);
4474 free(job
->filetypes
);
4475 free(job
->compressions
);
4479 job
->filetypes
= NULL
;
4480 job
->compressions
= NULL
;
4482 LastEvent
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
4487 * 'remove_job_history()' - Remove the control file for a job.
4491 remove_job_history(cupsd_job_t
*job
) /* I - Job */
4493 char filename
[1024]; /* Control filename */
4497 * Remove the job info file...
4500 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
,
4502 cupsdUnlinkOrRemoveFile(filename
);
4504 LastEvent
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
4509 * 'set_time()' - Set one of the "time-at-xyz" attributes.
4513 set_time(cupsd_job_t
*job
, /* I - Job to update */
4514 const char *name
) /* I - Name of attribute */
4516 ipp_attribute_t
*attr
; /* Time attribute */
4517 time_t curtime
; /* Current time */
4520 curtime
= time(NULL
);
4522 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "%s=%ld", name
, (long)curtime
);
4524 if ((attr
= ippFindAttribute(job
->attrs
, name
, IPP_TAG_ZERO
)) != NULL
)
4526 attr
->value_tag
= IPP_TAG_INTEGER
;
4527 attr
->values
[0].integer
= curtime
;
4530 if (!strcmp(name
, "time-at-completed"))
4532 job
->completed_time
= curtime
;
4534 if (JobHistory
< INT_MAX
&& attr
)
4535 job
->history_time
= attr
->values
[0].integer
+ JobHistory
;
4537 job
->history_time
= INT_MAX
;
4539 if (job
->history_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
4540 JobHistoryUpdate
= job
->history_time
;
4542 if (JobFiles
< INT_MAX
&& attr
)
4543 job
->file_time
= attr
->values
[0].integer
+ JobFiles
;
4545 job
->file_time
= INT_MAX
;
4547 if (job
->file_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
4548 JobHistoryUpdate
= job
->file_time
;
4550 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_time: JobHistoryUpdate=%ld",
4551 (long)JobHistoryUpdate
);
4557 * 'start_job()' - Start a print job.
4561 start_job(cupsd_job_t
*job
, /* I - Job ID */
4562 cupsd_printer_t
*printer
) /* I - Printer to print job */
4564 const char *filename
; /* Support filename */
4565 ipp_attribute_t
*cancel_after
= ippFindAttribute(job
->attrs
,
4568 /* job-cancel-after attribute */
4571 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "start_job(job=%p(%d), printer=%p(%s))",
4572 job
, job
->id
, printer
, printer
->name
);
4575 * Make sure we have some files around before we try to print...
4578 if (job
->num_files
== 0)
4580 ippSetString(job
->attrs
, &job
->reasons
, 0, "aborted-by-system");
4581 cupsdSetJobState(job
, IPP_JOB_ABORTED
, CUPSD_JOB_DEFAULT
,
4582 "Aborting job because it has no files.");
4587 * Update the printer and job state to "processing"...
4590 if (!cupsdLoadJob(job
))
4593 if (!job
->printer_message
)
4594 job
->printer_message
= ippFindAttribute(job
->attrs
,
4595 "job-printer-state-message",
4597 if (job
->printer_message
)
4598 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
), "");
4600 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-printing");
4601 cupsdSetJobState(job
, IPP_JOB_PROCESSING
, CUPSD_JOB_DEFAULT
, NULL
);
4602 cupsdSetPrinterState(printer
, IPP_PRINTER_PROCESSING
, 0);
4603 cupsdSetPrinterReasons(printer
, "-cups-remote-pending,"
4604 "cups-remote-pending-held,"
4605 "cups-remote-processing,"
4606 "cups-remote-stopped,"
4607 "cups-remote-canceled,"
4608 "cups-remote-aborted,"
4609 "cups-remote-completed");
4612 job
->current_file
= 0;
4614 job
->history_time
= 0;
4616 job
->printer
= printer
;
4620 job
->cancel_time
= time(NULL
) + ippGetInteger(cancel_after
, 0);
4621 else if (MaxJobTime
> 0)
4622 job
->cancel_time
= time(NULL
) + MaxJobTime
;
4624 job
->cancel_time
= 0;
4627 * Check for support files...
4630 cupsdSetPrinterReasons(job
->printer
, "-cups-missing-filter-warning,"
4631 "cups-insecure-filter-warning");
4635 for (filename
= (const char *)cupsArrayFirst(printer
->pc
->support_files
);
4637 filename
= (const char *)cupsArrayNext(printer
->pc
->support_files
))
4639 if (_cupsFileCheck(filename
, _CUPS_FILE_CHECK_FILE
, !RunUser
,
4640 cupsdLogFCMessage
, printer
))
4646 * Setup the last exit status and security profiles...
4650 job
->profile
= cupsdCreateProfile(job
->id
, 0);
4651 job
->bprofile
= cupsdCreateProfile(job
->id
, 1);
4654 * Create the status pipes and buffer...
4657 if (cupsdOpenPipe(job
->status_pipes
))
4659 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
4660 "Unable to create job status pipes - %s.", strerror(errno
));
4662 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
4663 "Job stopped because the scheduler could not create the "
4664 "job status pipes.");
4666 cupsdDestroyProfile(job
->profile
);
4667 job
->profile
= NULL
;
4668 cupsdDestroyProfile(job
->bprofile
);
4669 job
->bprofile
= NULL
;
4673 job
->status_buffer
= cupsdStatBufNew(job
->status_pipes
[0], NULL
);
4674 job
->status_level
= CUPSD_LOG_INFO
;
4677 * Create the backchannel pipes and make them non-blocking...
4680 if (cupsdOpenPipe(job
->back_pipes
))
4682 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
4683 "Unable to create back-channel pipes - %s.", strerror(errno
));
4685 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
4686 "Job stopped because the scheduler could not create the "
4687 "back-channel pipes.");
4689 cupsdClosePipe(job
->status_pipes
);
4690 cupsdStatBufDelete(job
->status_buffer
);
4691 job
->status_buffer
= NULL
;
4693 cupsdDestroyProfile(job
->profile
);
4694 job
->profile
= NULL
;
4695 cupsdDestroyProfile(job
->bprofile
);
4696 job
->bprofile
= NULL
;
4700 fcntl(job
->back_pipes
[0], F_SETFL
,
4701 fcntl(job
->back_pipes
[0], F_GETFL
) | O_NONBLOCK
);
4702 fcntl(job
->back_pipes
[1], F_SETFL
,
4703 fcntl(job
->back_pipes
[1], F_GETFL
) | O_NONBLOCK
);
4706 * Create the side-channel pipes and make them non-blocking...
4709 if (socketpair(AF_LOCAL
, SOCK_STREAM
, 0, job
->side_pipes
))
4711 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
4712 "Unable to create side-channel pipes - %s.", strerror(errno
));
4714 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
4715 "Job stopped because the scheduler could not create the "
4716 "side-channel pipes.");
4718 cupsdClosePipe(job
->back_pipes
);
4720 cupsdClosePipe(job
->status_pipes
);
4721 cupsdStatBufDelete(job
->status_buffer
);
4722 job
->status_buffer
= NULL
;
4724 cupsdDestroyProfile(job
->profile
);
4725 job
->profile
= NULL
;
4726 cupsdDestroyProfile(job
->bprofile
);
4727 job
->bprofile
= NULL
;
4731 fcntl(job
->side_pipes
[0], F_SETFL
,
4732 fcntl(job
->side_pipes
[0], F_GETFL
) | O_NONBLOCK
);
4733 fcntl(job
->side_pipes
[1], F_SETFL
,
4734 fcntl(job
->side_pipes
[1], F_GETFL
) | O_NONBLOCK
);
4736 fcntl(job
->side_pipes
[0], F_SETFD
,
4737 fcntl(job
->side_pipes
[0], F_GETFD
) | FD_CLOEXEC
);
4738 fcntl(job
->side_pipes
[1], F_SETFD
,
4739 fcntl(job
->side_pipes
[1], F_GETFD
) | FD_CLOEXEC
);
4742 * Now start the first file in the job...
4745 cupsdContinueJob(job
);
4750 * 'stop_job()' - Stop a print job.
4754 stop_job(cupsd_job_t
*job
, /* I - Job */
4755 cupsd_jobaction_t action
) /* I - Action */
4757 int i
; /* Looping var */
4760 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "stop_job(job=%p(%d), action=%d)", job
,
4763 FilterLevel
-= job
->cost
;
4766 if (action
== CUPSD_JOB_DEFAULT
&& !job
->kill_time
&& job
->backend
> 0)
4767 job
->kill_time
= time(NULL
) + JobKillDelay
;
4768 else if (action
>= CUPSD_JOB_FORCE
)
4771 for (i
= 0; job
->filters
[i
]; i
++)
4772 if (job
->filters
[i
] > 0)
4774 cupsdEndProcess(job
->filters
[i
], action
>= CUPSD_JOB_FORCE
);
4776 if (action
>= CUPSD_JOB_FORCE
)
4777 job
->filters
[i
] = -job
->filters
[i
];
4780 if (job
->backend
> 0)
4782 cupsdEndProcess(job
->backend
, action
>= CUPSD_JOB_FORCE
);
4784 if (action
>= CUPSD_JOB_FORCE
)
4785 job
->backend
= -job
->backend
;
4788 if (action
>= CUPSD_JOB_FORCE
)
4791 * Clear job status...
4800 * 'unload_job()' - Unload a job from memory.
4804 unload_job(cupsd_job_t
*job
) /* I - Job */
4809 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Unloading...");
4811 ippDelete(job
->attrs
);
4815 job
->reasons
= NULL
;
4817 job
->job_sheets
= NULL
;
4818 job
->printer_message
= NULL
;
4819 job
->printer_reasons
= NULL
;
4824 * 'update_job()' - Read a status update from a job's filters.
4828 update_job(cupsd_job_t
*job
) /* I - Job to check */
4830 int i
; /* Looping var */
4831 int copies
; /* Number of copies printed */
4832 char message
[CUPSD_SB_BUFFER_SIZE
],
4834 *ptr
; /* Pointer update... */
4835 int loglevel
, /* Log level for message */
4836 event
= 0; /* Events? */
4837 static const char * const levels
[] = /* Log levels */
4853 * Get the printer associated with this job; if the printer is stopped for
4854 * any reason then job->printer will be reset to NULL, so make sure we have
4855 * a valid pointer...
4858 while ((ptr
= cupsdStatBufUpdate(job
->status_buffer
, &loglevel
,
4859 message
, sizeof(message
))) != NULL
)
4862 * Process page and printer state messages as needed...
4865 if (loglevel
== CUPSD_LOG_PAGE
)
4868 * Page message; send the message to the page_log file and update the
4869 * job sheet count...
4872 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "PAGE: %s", message
);
4876 if (!_cups_strncasecmp(message
, "total ", 6))
4879 * Got a total count of pages from a backend or filter...
4882 copies
= atoi(message
+ 6);
4883 copies
-= job
->sheets
->values
[0].integer
; /* Just track the delta */
4885 else if (!sscanf(message
, "%*d%d", &copies
))
4888 job
->sheets
->values
[0].integer
+= copies
;
4890 if (job
->printer
->page_limit
)
4891 cupsdUpdateQuota(job
->printer
, job
->username
, copies
, 0);
4894 cupsdLogPage(job
, message
);
4897 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
4898 "Printed %d page(s).", job
->sheets
->values
[0].integer
);
4900 else if (loglevel
== CUPSD_LOG_JOBSTATE
)
4903 * Support "keyword" to set job-state-reasons to the specified keyword.
4904 * This is sufficient for the current paid printing stuff.
4907 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "JOBSTATE: %s", message
);
4909 if (!strcmp(message
, "cups-retry-as-raster"))
4910 job
->retry_as_raster
= 1;
4912 ippSetString(job
->attrs
, &job
->reasons
, 0, message
);
4914 else if (loglevel
== CUPSD_LOG_STATE
)
4916 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "STATE: %s", message
);
4918 if (!strcmp(message
, "paused"))
4920 cupsdStopPrinter(job
->printer
, 1);
4923 else if (message
[0] && cupsdSetPrinterReasons(job
->printer
, message
))
4925 event
|= CUPSD_EVENT_PRINTER_STATE
;
4930 * Reset cancel time after connecting to the device...
4933 for (i
= 0; i
< job
->printer
->num_reasons
; i
++)
4934 if (!strcmp(job
->printer
->reasons
[i
], "connecting-to-device"))
4937 if (i
>= job
->printer
->num_reasons
)
4939 ipp_attribute_t
*cancel_after
= ippFindAttribute(job
->attrs
,
4942 /* job-cancel-after attribute */
4945 job
->cancel_time
= time(NULL
) + ippGetInteger(cancel_after
, 0);
4947 job
->cancel_time
= time(NULL
) + MaxJobTime
;
4952 update_job_attrs(job
, 0);
4954 else if (loglevel
== CUPSD_LOG_ATTR
)
4957 * Set attribute(s)...
4960 int num_attrs
; /* Number of attributes */
4961 cups_option_t
*attrs
; /* Attributes */
4962 const char *attr
; /* Attribute */
4964 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "ATTR: %s", message
);
4966 num_attrs
= cupsParseOptions(message
, 0, &attrs
);
4968 if ((attr
= cupsGetOption("auth-info-default", num_attrs
,
4971 job
->printer
->num_options
= cupsAddOption("auth-info", attr
,
4972 job
->printer
->num_options
,
4973 &(job
->printer
->options
));
4974 cupsdSetPrinterAttrs(job
->printer
);
4976 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4979 if ((attr
= cupsGetOption("auth-info-required", num_attrs
,
4982 cupsdSetAuthInfoRequired(job
->printer
, attr
, NULL
);
4983 cupsdSetPrinterAttrs(job
->printer
);
4985 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4988 if ((attr
= cupsGetOption("job-media-progress", num_attrs
,
4991 int progress
= atoi(attr
);
4994 if (progress
>= 0 && progress
<= 100)
4996 job
->progress
= progress
;
4999 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
5000 "Printing page %d, %d%%",
5001 job
->sheets
->values
[0].integer
, job
->progress
);
5005 if ((attr
= cupsGetOption("printer-alert", num_attrs
, attrs
)) != NULL
)
5007 cupsdSetString(&job
->printer
->alert
, attr
);
5008 event
|= CUPSD_EVENT_PRINTER_STATE
;
5011 if ((attr
= cupsGetOption("printer-alert-description", num_attrs
,
5014 cupsdSetString(&job
->printer
->alert_description
, attr
);
5015 event
|= CUPSD_EVENT_PRINTER_STATE
;
5018 if ((attr
= cupsGetOption("marker-colors", num_attrs
, attrs
)) != NULL
)
5020 cupsdSetPrinterAttr(job
->printer
, "marker-colors", (char *)attr
);
5021 job
->printer
->marker_time
= time(NULL
);
5022 event
|= CUPSD_EVENT_PRINTER_STATE
;
5023 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
5026 if ((attr
= cupsGetOption("marker-levels", num_attrs
, attrs
)) != NULL
)
5028 cupsdSetPrinterAttr(job
->printer
, "marker-levels", (char *)attr
);
5029 job
->printer
->marker_time
= time(NULL
);
5030 event
|= CUPSD_EVENT_PRINTER_STATE
;
5031 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
5034 if ((attr
= cupsGetOption("marker-low-levels", num_attrs
, attrs
)) != NULL
)
5036 cupsdSetPrinterAttr(job
->printer
, "marker-low-levels", (char *)attr
);
5037 job
->printer
->marker_time
= time(NULL
);
5038 event
|= CUPSD_EVENT_PRINTER_STATE
;
5039 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
5042 if ((attr
= cupsGetOption("marker-high-levels", num_attrs
, attrs
)) != NULL
)
5044 cupsdSetPrinterAttr(job
->printer
, "marker-high-levels", (char *)attr
);
5045 job
->printer
->marker_time
= time(NULL
);
5046 event
|= CUPSD_EVENT_PRINTER_STATE
;
5047 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
5050 if ((attr
= cupsGetOption("marker-message", num_attrs
, attrs
)) != NULL
)
5052 cupsdSetPrinterAttr(job
->printer
, "marker-message", (char *)attr
);
5053 job
->printer
->marker_time
= time(NULL
);
5054 event
|= CUPSD_EVENT_PRINTER_STATE
;
5055 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
5058 if ((attr
= cupsGetOption("marker-names", num_attrs
, attrs
)) != NULL
)
5060 cupsdSetPrinterAttr(job
->printer
, "marker-names", (char *)attr
);
5061 job
->printer
->marker_time
= time(NULL
);
5062 event
|= CUPSD_EVENT_PRINTER_STATE
;
5063 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
5066 if ((attr
= cupsGetOption("marker-types", num_attrs
, attrs
)) != NULL
)
5068 cupsdSetPrinterAttr(job
->printer
, "marker-types", (char *)attr
);
5069 job
->printer
->marker_time
= time(NULL
);
5070 event
|= CUPSD_EVENT_PRINTER_STATE
;
5071 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
5074 cupsFreeOptions(num_attrs
, attrs
);
5076 else if (loglevel
== CUPSD_LOG_PPD
)
5079 * Set attribute(s)...
5082 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "PPD: %s", message
);
5084 job
->num_keywords
= cupsParseOptions(message
, job
->num_keywords
,
5090 * Strip legacy message prefix...
5093 if (!strncmp(message
, "recoverable:", 12))
5096 while (isspace(*ptr
& 255))
5099 else if (!strncmp(message
, "recovered:", 10))
5102 while (isspace(*ptr
& 255))
5109 cupsdLogJob(job
, loglevel
, "%s", ptr
);
5111 if (loglevel
< CUPSD_LOG_DEBUG
&&
5112 strcmp(job
->printer
->state_message
, ptr
))
5114 strlcpy(job
->printer
->state_message
, ptr
,
5115 sizeof(job
->printer
->state_message
));
5117 event
|= CUPSD_EVENT_PRINTER_STATE
| CUPSD_EVENT_JOB_PROGRESS
;
5119 if (loglevel
<= job
->status_level
&& job
->status_level
> CUPSD_LOG_ERROR
)
5122 * Some messages show in the job-printer-state-message attribute...
5125 if (loglevel
!= CUPSD_LOG_NOTICE
)
5126 job
->status_level
= loglevel
;
5128 update_job_attrs(job
, 1);
5130 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
5131 "Set job-printer-state-message to \"%s\", "
5133 job
->printer_message
->values
[0].string
.text
,
5134 levels
[job
->status_level
]);
5139 if (!strchr(job
->status_buffer
->buffer
, '\n'))
5143 if (event
& CUPSD_EVENT_JOB_PROGRESS
)
5144 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
5145 "%s", job
->printer
->state_message
);
5146 if (event
& CUPSD_EVENT_PRINTER_STATE
)
5147 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, job
->printer
, NULL
,
5148 (job
->printer
->type
& CUPS_PRINTER_CLASS
) ?
5149 "Class \"%s\" state changed." :
5150 "Printer \"%s\" state changed.",
5151 job
->printer
->name
);
5154 if (ptr
== NULL
&& !job
->status_buffer
->bufused
)
5157 * See if all of the filters and the backend have returned their
5161 for (i
= 0; job
->filters
[i
] < 0; i
++);
5163 if (job
->filters
[i
])
5166 * EOF but we haven't collected the exit status of all filters...
5169 cupsdCheckProcess();
5173 if (job
->current_file
>= job
->num_files
&& job
->backend
> 0)
5176 * EOF but we haven't collected the exit status of the backend...
5179 cupsdCheckProcess();
5184 * Handle the end of job stuff...
5187 finalize_job(job
, 1);
5190 * Check for new jobs...
5199 * 'update_job_attrs()' - Update the job-printer-* attributes.
5203 update_job_attrs(cupsd_job_t
*job
, /* I - Job to update */
5204 int do_message
)/* I - 1 = copy job-printer-state message */
5206 int i
; /* Looping var */
5207 int num_reasons
; /* Actual number of reasons */
5208 const char * const *reasons
; /* Reasons */
5209 static const char *none
= "none"; /* "none" reason */
5213 * Get/create the job-printer-state-* attributes...
5216 if (!job
->printer_message
)
5218 if ((job
->printer_message
= ippFindAttribute(job
->attrs
,
5219 "job-printer-state-message",
5220 IPP_TAG_TEXT
)) == NULL
)
5221 job
->printer_message
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_TEXT
,
5222 "job-printer-state-message",
5226 if (!job
->printer_reasons
)
5227 job
->printer_reasons
= ippFindAttribute(job
->attrs
,
5228 "job-printer-state-reasons",
5232 * Copy or clear the printer-state-message value as needed...
5235 if (job
->state_value
!= IPP_JOB_PROCESSING
&&
5236 job
->status_level
== CUPSD_LOG_INFO
)
5238 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
), "");
5241 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
5243 else if (job
->printer
->state_message
[0] && do_message
)
5245 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
),
5246 job
->printer
->state_message
);
5249 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
5253 * ... and the printer-state-reasons value...
5256 if (job
->printer
->num_reasons
== 0)
5263 num_reasons
= job
->printer
->num_reasons
;
5264 reasons
= (const char * const *)job
->printer
->reasons
;
5267 if (!job
->printer_reasons
|| job
->printer_reasons
->num_values
!= num_reasons
)
5270 * Replace/create a job-printer-state-reasons attribute...
5273 ippDeleteAttribute(job
->attrs
, job
->printer_reasons
);
5275 job
->printer_reasons
= ippAddStrings(job
->attrs
,
5276 IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
5277 "job-printer-state-reasons",
5278 num_reasons
, NULL
, NULL
);
5283 * Don't bother clearing the reason strings if they are the same...
5286 for (i
= 0; i
< num_reasons
; i
++)
5287 if (strcmp(job
->printer_reasons
->values
[i
].string
.text
, reasons
[i
]))
5290 if (i
>= num_reasons
)
5294 * Not the same, so free the current strings...
5297 for (i
= 0; i
< num_reasons
; i
++)
5298 _cupsStrFree(job
->printer_reasons
->values
[i
].string
.text
);
5302 * Copy the reasons...
5305 for (i
= 0; i
< num_reasons
; i
++)
5306 job
->printer_reasons
->values
[i
].string
.text
= _cupsStrAlloc(reasons
[i
]);
5309 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);