4 * Job management routines for the CUPS scheduler.
6 * Copyright 2007-2014 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
,
239 "cupsdCheckJobs: %d active jobs, sleeping=%d, reload=%d, "
240 "curtime=%ld", cupsArrayCount(ActiveJobs
), Sleeping
,
241 NeedReload
, (long)curtime
);
243 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
245 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
247 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
248 "cupsdCheckJobs: Job %d - dest=\"%s\", printer=%p, "
249 "state=%d, cancel_time=%ld, hold_until=%ld, kill_time=%ld, "
250 "pending_cost=%d, pending_timeout=%ld", job
->id
, job
->dest
,
251 job
->printer
, job
->state_value
, (long)job
->cancel_time
,
252 (long)job
->hold_until
, (long)job
->kill_time
,
253 job
->pending_cost
, (long)job
->pending_timeout
);
256 * Kill jobs if they are unresponsive...
259 if (job
->kill_time
&& job
->kill_time
<= curtime
)
262 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Stopping unresponsive job.");
264 stop_job(job
, CUPSD_JOB_FORCE
);
269 * Cancel stuck jobs...
272 if (job
->cancel_time
&& job
->cancel_time
<= curtime
)
274 int cancel_after
; /* job-cancel-after value */
276 attr
= ippFindAttribute(job
->attrs
, "job-cancel-after", IPP_TAG_INTEGER
);
277 cancel_after
= attr
? ippGetInteger(attr
, 0) : MaxJobTime
;
280 cupsdSetJobState(job
, IPP_JOB_CANCELED
, CUPSD_JOB_FORCE
, "Marking stuck job as completed after %d seconds.", cancel_after
);
282 cupsdSetJobState(job
, IPP_JOB_CANCELED
, CUPSD_JOB_DEFAULT
, "Canceling stuck job after %d seconds.", cancel_after
);
287 * Start held jobs if they are ready...
290 if (job
->state_value
== IPP_JOB_HELD
&&
292 job
->hold_until
< curtime
)
294 if (job
->pending_timeout
)
297 * This job is pending; check that we don't have an active Send-Document
298 * operation in progress on any of the client connections, then timeout
299 * the job so we can start printing...
302 cupsd_client_t
*con
; /* Current client connection */
304 for (con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
306 con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
308 con
->request
->request
.op
.operation_id
== IPP_SEND_DOCUMENT
)
314 if (cupsdTimeoutJob(job
))
318 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
319 "Job submission timed out.");
323 * Continue jobs that are waiting on the FilterLimit...
326 if (job
->pending_cost
> 0 &&
327 ((FilterLevel
+ job
->pending_cost
) < FilterLimit
|| FilterLevel
== 0))
328 cupsdContinueJob(job
);
331 * Start pending jobs if the destination is available...
334 if (job
->state_value
== IPP_JOB_PENDING
&& !NeedReload
&&
335 !Sleeping
&& !DoingShutdown
&& !job
->printer
)
337 printer
= cupsdFindDest(job
->dest
);
340 while (printer
&& (printer
->type
& CUPS_PRINTER_CLASS
))
343 * If the class is remote, just pass it to the remote server...
348 if (pclass
->state
== IPP_PRINTER_STOPPED
)
350 else if (pclass
->type
& CUPS_PRINTER_REMOTE
)
353 printer
= cupsdFindAvailablePrinter(printer
->name
);
356 if (!printer
&& !pclass
)
359 * Whoa, the printer and/or class for this destination went away;
363 cupsdSetJobState(job
, IPP_JOB_ABORTED
, CUPSD_JOB_PURGE
,
364 "Job aborted because the destination printer/class "
367 else if (printer
&& !printer
->holding_new_jobs
)
370 * See if the printer is available or remote and not printing a job;
371 * if so, start the job...
377 * Add/update a job-actual-printer-uri attribute for this job
378 * so that we know which printer actually printed the job...
381 if ((attr
= ippFindAttribute(job
->attrs
, "job-actual-printer-uri",
382 IPP_TAG_URI
)) != NULL
)
383 cupsdSetString(&attr
->values
[0].string
.text
, printer
->uri
);
385 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
,
386 "job-actual-printer-uri", NULL
, printer
->uri
);
389 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
392 if (!printer
->job
&& printer
->state
== IPP_PRINTER_IDLE
)
398 start_job(job
, printer
);
407 * 'cupsdCleanJobs()' - Clean out old jobs.
413 cupsd_job_t
*job
; /* Current job */
414 time_t curtime
; /* Current time */
417 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
418 "cupsdCleanJobs: MaxJobs=%d, JobHistory=%d, JobFiles=%d",
419 MaxJobs
, JobHistory
, JobFiles
);
421 if (MaxJobs
<= 0 && JobHistory
== INT_MAX
&& JobFiles
== INT_MAX
)
424 curtime
= time(NULL
);
425 JobHistoryUpdate
= 0;
427 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
429 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
431 if (job
->state_value
>= IPP_JOB_CANCELED
&& !job
->printer
)
434 * Expire old jobs (or job files)...
437 if ((MaxJobs
> 0 && cupsArrayCount(Jobs
) >= MaxJobs
) ||
438 (job
->history_time
&& job
->history_time
<= curtime
))
440 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Removing from history.");
441 cupsdDeleteJob(job
, CUPSD_JOB_PURGE
);
443 else if (job
->file_time
&& job
->file_time
<= curtime
)
445 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Removing document files.");
446 remove_job_files(job
);
448 if (job
->history_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
449 JobHistoryUpdate
= job
->history_time
;
453 if (job
->history_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
454 JobHistoryUpdate
= job
->history_time
;
456 if (job
->file_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
457 JobHistoryUpdate
= job
->file_time
;
462 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdCleanJobs: JobHistoryUpdate=%ld",
463 (long)JobHistoryUpdate
);
468 * 'cupsdContinueJob()' - Continue printing with the next file in a job.
472 cupsdContinueJob(cupsd_job_t
*job
) /* I - Job */
474 int i
; /* Looping var */
475 int slot
; /* Pipe slot */
476 cups_array_t
*filters
= NULL
,/* Filters for job */
477 *prefilters
; /* Filters with prefilters */
478 mime_filter_t
*filter
, /* Current filter */
479 *prefilter
, /* Prefilter */
480 port_monitor
; /* Port monitor filter */
481 char scheme
[255]; /* Device URI scheme */
482 ipp_attribute_t
*attr
; /* Current attribute */
483 const char *ptr
, /* Pointer into value */
484 *abort_message
; /* Abort message */
485 ipp_jstate_t abort_state
= IPP_JOB_STOPPED
;
486 /* New job state on abort */
487 struct stat backinfo
; /* Backend file information */
488 int backroot
; /* Run backend as root? */
489 int pid
; /* Process ID of new filter process */
490 int banner_page
; /* 1 if banner page, 0 otherwise */
491 int filterfds
[2][2] = { { -1, -1 }, { -1, -1 } };
492 /* Pipes used between filters */
493 int envc
; /* Number of environment variables */
494 struct stat fileinfo
; /* Job file information */
495 int argc
; /* Number of arguments */
496 char **argv
= NULL
, /* Filter command-line arguments */
497 filename
[1024], /* Job filename */
498 command
[1024], /* Full path to command */
499 jobid
[255], /* Job ID string */
501 /* Job title string */
502 copies
[255], /* # copies string */
503 *options
, /* Options string */
505 /* Environment variables */
506 charset
[255], /* CHARSET env variable */
507 class_name
[255],/* CLASS env variable */
508 classification
[1024],
509 /* CLASSIFICATION env variable */
511 /* CONTENT_TYPE env variable */
513 /* DEVICE_URI env variable */
514 final_content_type
[1024] = "",
515 /* FINAL_CONTENT_TYPE env variable */
516 lang
[255], /* LANG env variable */
519 /* APPLE_LANGUAGE env variable */
520 #endif /* __APPLE__ */
521 auth_info_required
[255],
522 /* AUTH_INFO_REQUIRED env variable */
523 ppd
[1024], /* PPD env variable */
525 /* PRINTER_INFO env variable */
526 printer_location
[255],
527 /* PRINTER_LOCATION env variable */
529 /* PRINTER env variable */
530 *printer_state_reasons
= NULL
,
531 /* PRINTER_STATE_REASONS env var */
533 /* RIP_MAX_CACHE env variable */
536 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
537 "cupsdContinueJob(job=%p(%d)): current_file=%d, num_files=%d",
538 job
, job
->id
, job
->current_file
, job
->num_files
);
541 * Figure out what filters are required to convert from
542 * the source to the destination type...
545 FilterLevel
-= job
->cost
;
548 job
->pending_cost
= 0;
550 memset(job
->filters
, 0, sizeof(job
->filters
));
552 if (job
->printer
->raw
)
555 * Remote jobs and raw queues go directly to the printer without
559 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Sending job to queue tagged as raw...");
564 * Local jobs get filtered...
567 mime_type_t
*dst
= job
->printer
->filetype
;
568 /* Destination file type */
570 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
571 job
->id
, job
->current_file
+ 1);
572 if (stat(filename
, &fileinfo
))
573 fileinfo
.st_size
= 0;
575 if (job
->retry_as_raster
)
578 * Need to figure out whether the printer supports image/pwg-raster or
579 * image/urf, and use the corresponding type...
582 char type
[MIME_MAX_TYPE
]; /* MIME media type for printer */
584 snprintf(type
, sizeof(type
), "%s/image/urf", job
->printer
->name
);
585 if ((dst
= mimeType(MimeDatabase
, "printer", type
)) == NULL
)
587 snprintf(type
, sizeof(type
), "%s/image/pwg-raster", job
->printer
->name
);
588 dst
= mimeType(MimeDatabase
, "printer", type
);
592 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Retrying job as \"%s\".", strchr(dst
->type
, '/') + 1);
594 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Unable to retry job using a supported raster format.");
597 filters
= mimeFilter2(MimeDatabase
, job
->filetypes
[job
->current_file
], (size_t)fileinfo
.st_size
, dst
, &(job
->cost
));
601 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
602 "Unable to convert file %d to printable format.",
605 abort_message
= "Aborting job because it cannot be printed.";
606 abort_state
= IPP_JOB_ABORTED
;
608 ippSetString(job
->attrs
, &job
->reasons
, 0, "document-unprintable-error");
613 * Figure out the final content type...
616 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "%d filters for job:",
617 cupsArrayCount(filters
));
618 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
620 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
621 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "%s (%s/%s to %s/%s, cost %d)",
623 filter
->src
? filter
->src
->super
: "???",
624 filter
->src
? filter
->src
->type
: "???",
625 filter
->dst
? filter
->dst
->super
: "???",
626 filter
->dst
? filter
->dst
->type
: "???",
629 if (!job
->printer
->remote
)
631 for (filter
= (mime_filter_t
*)cupsArrayLast(filters
);
632 filter
&& filter
->dst
;
633 filter
= (mime_filter_t
*)cupsArrayPrev(filters
))
634 if (strcmp(filter
->dst
->super
, "printer") ||
635 strcmp(filter
->dst
->type
, job
->printer
->name
))
638 if (filter
&& filter
->dst
)
640 if ((ptr
= strchr(filter
->dst
->type
, '/')) != NULL
)
641 snprintf(final_content_type
, sizeof(final_content_type
),
642 "FINAL_CONTENT_TYPE=%s", ptr
+ 1);
644 snprintf(final_content_type
, sizeof(final_content_type
),
645 "FINAL_CONTENT_TYPE=%s/%s", filter
->dst
->super
,
649 snprintf(final_content_type
, sizeof(final_content_type
),
650 "FINAL_CONTENT_TYPE=printer/%s", job
->printer
->name
);
654 * Remove NULL ("-") filters...
657 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
659 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
660 if (!strcmp(filter
->filter
, "-"))
661 cupsArrayRemove(filters
, filter
);
663 if (cupsArrayCount(filters
) == 0)
665 cupsArrayDelete(filters
);
670 * If this printer has any pre-filters, insert the required pre-filter
671 * in the filters array...
674 if (job
->printer
->prefiltertype
&& filters
)
676 prefilters
= cupsArrayNew(NULL
, NULL
);
678 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
680 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
682 if ((prefilter
= mimeFilterLookup(MimeDatabase
, filter
->src
,
683 job
->printer
->prefiltertype
)))
685 cupsArrayAdd(prefilters
, prefilter
);
686 job
->cost
+= prefilter
->cost
;
689 cupsArrayAdd(prefilters
, filter
);
692 cupsArrayDelete(filters
);
693 filters
= prefilters
;
698 * Set a minimum cost of 100 for all jobs so that FilterLimit
699 * works with raw queues and other low-cost paths.
706 * See if the filter cost is too high...
709 if ((FilterLevel
+ job
->cost
) > FilterLimit
&& FilterLevel
> 0 &&
713 * Don't print this job quite yet...
716 cupsArrayDelete(filters
);
718 cupsdLogJob(job
, CUPSD_LOG_INFO
,
719 "Holding because filter limit has been reached.");
720 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
721 "cupsdContinueJob: file=%d, cost=%d, level=%d, limit=%d",
722 job
->current_file
, job
->cost
, FilterLevel
,
725 job
->pending_cost
= job
->cost
;
730 FilterLevel
+= job
->cost
;
733 * Add decompression/raw filter as needed...
736 if (job
->compressions
[job
->current_file
] &&
737 (!job
->printer
->remote
|| job
->num_files
== 1))
740 * Add gziptoany filter to the front of the list...
744 filters
= cupsArrayNew(NULL
, NULL
);
746 if (!cupsArrayInsert(filters
, &gziptoany_filter
))
748 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
749 "Unable to add decompression filter - %s", strerror(errno
));
751 cupsArrayDelete(filters
);
753 abort_message
= "Stopping job because the scheduler ran out of memory.";
760 * Add port monitor, if any...
763 if (job
->printer
->port_monitor
)
766 * Add port monitor to the end of the list...
770 filters
= cupsArrayNew(NULL
, NULL
);
772 port_monitor
.src
= NULL
;
773 port_monitor
.dst
= NULL
;
774 port_monitor
.cost
= 0;
776 snprintf(port_monitor
.filter
, sizeof(port_monitor
.filter
),
777 "%s/monitor/%s", ServerBin
, job
->printer
->port_monitor
);
779 if (!cupsArrayAdd(filters
, &port_monitor
))
781 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
782 "Unable to add port monitor - %s", strerror(errno
));
784 abort_message
= "Stopping job because the scheduler ran out of memory.";
791 * Make sure we don't go over the "MAX_FILTERS" limit...
794 if (cupsArrayCount(filters
) > MAX_FILTERS
)
796 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
797 "Too many filters (%d > %d), unable to print.",
798 cupsArrayCount(filters
), MAX_FILTERS
);
800 abort_message
= "Aborting job because it needs too many filters to print.";
801 abort_state
= IPP_JOB_ABORTED
;
803 ippSetString(job
->attrs
, &job
->reasons
, 0, "document-unprintable-error");
809 * Determine if we are printing a banner page or not...
812 if (job
->job_sheets
== NULL
)
814 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "No job-sheets attribute.");
815 if ((job
->job_sheets
=
816 ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_ZERO
)) != NULL
)
817 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
818 "... but someone added one without setting job_sheets.");
820 else if (job
->job_sheets
->num_values
== 1)
821 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "job-sheets=%s",
822 job
->job_sheets
->values
[0].string
.text
);
824 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "job-sheets=%s,%s",
825 job
->job_sheets
->values
[0].string
.text
,
826 job
->job_sheets
->values
[1].string
.text
);
828 if (job
->printer
->type
& CUPS_PRINTER_REMOTE
)
830 else if (job
->job_sheets
== NULL
)
832 else if (_cups_strcasecmp(job
->job_sheets
->values
[0].string
.text
, "none") != 0 &&
833 job
->current_file
== 0)
835 else if (job
->job_sheets
->num_values
> 1 &&
836 _cups_strcasecmp(job
->job_sheets
->values
[1].string
.text
, "none") != 0 &&
837 job
->current_file
== (job
->num_files
- 1))
842 if ((options
= get_options(job
, banner_page
, copies
, sizeof(copies
), title
,
843 sizeof(title
))) == NULL
)
845 abort_message
= "Stopping job because the scheduler ran out of memory.";
851 * Build the command-line arguments for the filters. Each filter
852 * has 6 or 7 arguments:
860 * argv[6] = filename (optional; normally stdin)
862 * This allows legacy printer drivers that use the old System V
863 * printing interface to be used by CUPS.
865 * For remote jobs, we send all of the files in the argument list.
868 if (job
->printer
->remote
)
869 argc
= 6 + job
->num_files
;
873 if ((argv
= calloc((size_t)argc
+ 1, sizeof(char *))) == NULL
)
875 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Unable to allocate argument array - %s",
878 abort_message
= "Stopping job because the scheduler ran out of memory.";
883 sprintf(jobid
, "%d", job
->id
);
885 argv
[0] = job
->printer
->name
;
887 argv
[2] = job
->username
;
892 if (job
->printer
->remote
&& job
->num_files
> 1)
894 for (i
= 0; i
< job
->num_files
; i
++)
896 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
898 argv
[6 + i
] = strdup(filename
);
903 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
904 job
->id
, job
->current_file
+ 1);
905 argv
[6] = strdup(filename
);
908 for (i
= 0; argv
[i
]; i
++)
909 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "argv[%d]=\"%s\"", i
, argv
[i
]);
912 * Create environment variable strings for the filters...
915 attr
= ippFindAttribute(job
->attrs
, "attributes-natural-language",
919 strlcpy(apple_language
, "APPLE_LANGUAGE=", sizeof(apple_language
));
920 _cupsAppleLanguage(attr
->values
[0].string
.text
,
921 apple_language
+ 15, sizeof(apple_language
) - 15);
922 #endif /* __APPLE__ */
924 switch (strlen(attr
->values
[0].string
.text
))
928 * This is an unknown or badly formatted language code; use
929 * the POSIX locale...
932 strlcpy(lang
, "LANG=C", sizeof(lang
));
937 * Just the language code (ll)...
940 snprintf(lang
, sizeof(lang
), "LANG=%s.UTF-8",
941 attr
->values
[0].string
.text
);
946 * Language and country code (ll-cc)...
949 snprintf(lang
, sizeof(lang
), "LANG=%c%c_%c%c.UTF-8",
950 attr
->values
[0].string
.text
[0],
951 attr
->values
[0].string
.text
[1],
952 toupper(attr
->values
[0].string
.text
[3] & 255),
953 toupper(attr
->values
[0].string
.text
[4] & 255));
957 if ((attr
= ippFindAttribute(job
->attrs
, "document-format",
958 IPP_TAG_MIMETYPE
)) != NULL
&&
959 (ptr
= strstr(attr
->values
[0].string
.text
, "charset=")) != NULL
)
960 snprintf(charset
, sizeof(charset
), "CHARSET=%s", ptr
+ 8);
962 strlcpy(charset
, "CHARSET=utf-8", sizeof(charset
));
964 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s/%s",
965 job
->filetypes
[job
->current_file
]->super
,
966 job
->filetypes
[job
->current_file
]->type
);
967 snprintf(device_uri
, sizeof(device_uri
), "DEVICE_URI=%s",
968 job
->printer
->device_uri
);
969 snprintf(ppd
, sizeof(ppd
), "PPD=%s/ppd/%s.ppd", ServerRoot
,
971 snprintf(printer_info
, sizeof(printer_name
), "PRINTER_INFO=%s",
972 job
->printer
->info
? job
->printer
->info
: "");
973 snprintf(printer_location
, sizeof(printer_name
), "PRINTER_LOCATION=%s",
974 job
->printer
->location
? job
->printer
->location
: "");
975 snprintf(printer_name
, sizeof(printer_name
), "PRINTER=%s", job
->printer
->name
);
976 if (job
->printer
->num_reasons
> 0)
978 char *psrptr
; /* Pointer into PRINTER_STATE_REASONS */
979 size_t psrlen
; /* Size of PRINTER_STATE_REASONS */
981 for (psrlen
= 22, i
= 0; i
< job
->printer
->num_reasons
; i
++)
982 psrlen
+= strlen(job
->printer
->reasons
[i
]) + 1;
984 if ((printer_state_reasons
= malloc(psrlen
)) != NULL
)
987 * All of these strcpy's are safe because we allocated the psr string...
990 strlcpy(printer_state_reasons
, "PRINTER_STATE_REASONS=", psrlen
);
991 for (psrptr
= printer_state_reasons
+ 22, i
= 0;
992 i
< job
->printer
->num_reasons
;
997 strlcpy(psrptr
, job
->printer
->reasons
[i
], psrlen
- (size_t)(psrptr
- printer_state_reasons
));
998 psrptr
+= strlen(psrptr
);
1002 snprintf(rip_max_cache
, sizeof(rip_max_cache
), "RIP_MAX_CACHE=%s", RIPCache
);
1004 if (job
->printer
->num_auth_info_required
== 1)
1005 snprintf(auth_info_required
, sizeof(auth_info_required
),
1006 "AUTH_INFO_REQUIRED=%s",
1007 job
->printer
->auth_info_required
[0]);
1008 else if (job
->printer
->num_auth_info_required
== 2)
1009 snprintf(auth_info_required
, sizeof(auth_info_required
),
1010 "AUTH_INFO_REQUIRED=%s,%s",
1011 job
->printer
->auth_info_required
[0],
1012 job
->printer
->auth_info_required
[1]);
1013 else if (job
->printer
->num_auth_info_required
== 3)
1014 snprintf(auth_info_required
, sizeof(auth_info_required
),
1015 "AUTH_INFO_REQUIRED=%s,%s,%s",
1016 job
->printer
->auth_info_required
[0],
1017 job
->printer
->auth_info_required
[1],
1018 job
->printer
->auth_info_required
[2]);
1019 else if (job
->printer
->num_auth_info_required
== 4)
1020 snprintf(auth_info_required
, sizeof(auth_info_required
),
1021 "AUTH_INFO_REQUIRED=%s,%s,%s,%s",
1022 job
->printer
->auth_info_required
[0],
1023 job
->printer
->auth_info_required
[1],
1024 job
->printer
->auth_info_required
[2],
1025 job
->printer
->auth_info_required
[3]);
1027 strlcpy(auth_info_required
, "AUTH_INFO_REQUIRED=none",
1028 sizeof(auth_info_required
));
1030 envc
= cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
1032 envp
[envc
++] = charset
;
1033 envp
[envc
++] = lang
;
1035 envp
[envc
++] = apple_language
;
1036 #endif /* __APPLE__ */
1037 envp
[envc
++] = ppd
;
1038 envp
[envc
++] = rip_max_cache
;
1039 envp
[envc
++] = content_type
;
1040 envp
[envc
++] = device_uri
;
1041 envp
[envc
++] = printer_info
;
1042 envp
[envc
++] = printer_location
;
1043 envp
[envc
++] = printer_name
;
1044 envp
[envc
++] = printer_state_reasons
? printer_state_reasons
:
1045 "PRINTER_STATE_REASONS=none";
1046 envp
[envc
++] = banner_page
? "CUPS_FILETYPE=job-sheet" :
1047 "CUPS_FILETYPE=document";
1049 if (final_content_type
[0])
1050 envp
[envc
++] = final_content_type
;
1052 if (Classification
&& !banner_page
)
1054 if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
1055 IPP_TAG_NAME
)) == NULL
)
1056 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
1058 else if (attr
->num_values
> 1 &&
1059 strcmp(attr
->values
[1].string
.text
, "none") != 0)
1060 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
1061 attr
->values
[1].string
.text
);
1063 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
1064 attr
->values
[0].string
.text
);
1066 envp
[envc
++] = classification
;
1069 if (job
->dtype
& CUPS_PRINTER_CLASS
)
1071 snprintf(class_name
, sizeof(class_name
), "CLASS=%s", job
->dest
);
1072 envp
[envc
++] = class_name
;
1075 envp
[envc
++] = auth_info_required
;
1078 i
< (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0]));
1080 if (job
->auth_env
[i
])
1081 envp
[envc
++] = job
->auth_env
[i
];
1086 envp
[envc
++] = job
->auth_uid
;
1090 for (i
= 0; i
< envc
; i
++)
1091 if (!strncmp(envp
[i
], "AUTH_", 5))
1092 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"AUTH_%c****\"", i
,
1094 else if (strncmp(envp
[i
], "DEVICE_URI=", 11))
1095 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"%s\"", i
, envp
[i
]);
1097 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"DEVICE_URI=%s\"", i
,
1098 job
->printer
->sanitized_device_uri
);
1100 if (job
->printer
->remote
)
1101 job
->current_file
= job
->num_files
;
1103 job
->current_file
++;
1106 * Now create processes for all of the filters...
1109 for (i
= 0, slot
= 0, filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
1111 i
++, filter
= (mime_filter_t
*)cupsArrayNext(filters
))
1113 if (filter
->filter
[0] != '/')
1114 snprintf(command
, sizeof(command
), "%s/filter/%s", ServerBin
,
1117 strlcpy(command
, filter
->filter
, sizeof(command
));
1119 if (i
< (cupsArrayCount(filters
) - 1))
1121 if (cupsdOpenPipe(filterfds
[slot
]))
1123 abort_message
= "Stopping job because the scheduler could not create "
1124 "the filter pipes.";
1131 if (job
->current_file
== 1 ||
1132 (job
->printer
->pc
&& job
->printer
->pc
->single_file
))
1134 if (strncmp(job
->printer
->device_uri
, "file:", 5) != 0)
1136 if (cupsdOpenPipe(job
->print_pipes
))
1138 abort_message
= "Stopping job because the scheduler could not "
1139 "create the backend pipes.";
1146 job
->print_pipes
[0] = -1;
1147 if (!strcmp(job
->printer
->device_uri
, "file:/dev/null") ||
1148 !strcmp(job
->printer
->device_uri
, "file:///dev/null"))
1149 job
->print_pipes
[1] = -1;
1152 if (!strncmp(job
->printer
->device_uri
, "file:/dev/", 10))
1153 job
->print_pipes
[1] = open(job
->printer
->device_uri
+ 5,
1155 else if (!strncmp(job
->printer
->device_uri
, "file:///dev/", 12))
1156 job
->print_pipes
[1] = open(job
->printer
->device_uri
+ 7,
1158 else if (!strncmp(job
->printer
->device_uri
, "file:///", 8))
1159 job
->print_pipes
[1] = open(job
->printer
->device_uri
+ 7,
1160 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
1162 job
->print_pipes
[1] = open(job
->printer
->device_uri
+ 5,
1163 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
1165 if (job
->print_pipes
[1] < 0)
1167 abort_message
= "Stopping job because the scheduler could not "
1168 "open the output file.";
1173 fcntl(job
->print_pipes
[1], F_SETFD
,
1174 fcntl(job
->print_pipes
[1], F_GETFD
) | FD_CLOEXEC
);
1179 filterfds
[slot
][0] = job
->print_pipes
[0];
1180 filterfds
[slot
][1] = job
->print_pipes
[1];
1183 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
1184 filterfds
[slot
][1], job
->status_pipes
[1],
1185 job
->back_pipes
[0], job
->side_pipes
[0], 0,
1186 job
->profile
, job
, job
->filters
+ i
);
1188 cupsdClosePipe(filterfds
[!slot
]);
1192 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Unable to start filter \"%s\" - %s.",
1193 filter
->filter
, strerror(errno
));
1195 abort_message
= "Stopping job because the scheduler could not execute a "
1201 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Started filter %s (PID %d)", command
,
1213 cupsArrayDelete(filters
);
1217 * Finally, pipe the final output into a backend process if needed...
1220 if (strncmp(job
->printer
->device_uri
, "file:", 5) != 0)
1222 if (job
->current_file
== 1 || job
->printer
->remote
||
1223 (job
->printer
->pc
&& job
->printer
->pc
->single_file
))
1225 sscanf(job
->printer
->device_uri
, "%254[^:]", scheme
);
1226 snprintf(command
, sizeof(command
), "%s/backend/%s", ServerBin
, scheme
);
1229 * See if the backend needs to run as root...
1234 else if (stat(command
, &backinfo
))
1237 backroot
= !(backinfo
.st_mode
& (S_IWGRP
| S_IRWXO
));
1239 argv
[0] = job
->printer
->sanitized_device_uri
;
1241 filterfds
[slot
][0] = -1;
1242 filterfds
[slot
][1] = -1;
1244 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
1245 filterfds
[slot
][1], job
->status_pipes
[1],
1246 job
->back_pipes
[1], job
->side_pipes
[1],
1247 backroot
, job
->bprofile
, job
, &(job
->backend
));
1251 abort_message
= "Stopping job because the sheduler could not execute "
1258 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Started backend %s (PID %d)",
1263 if (job
->current_file
== job
->num_files
||
1264 (job
->printer
->pc
&& job
->printer
->pc
->single_file
))
1265 cupsdClosePipe(job
->print_pipes
);
1267 if (job
->current_file
== job
->num_files
)
1269 cupsdClosePipe(job
->back_pipes
);
1270 cupsdClosePipe(job
->side_pipes
);
1272 close(job
->status_pipes
[1]);
1273 job
->status_pipes
[1] = -1;
1278 filterfds
[slot
][0] = -1;
1279 filterfds
[slot
][1] = -1;
1281 if (job
->current_file
== job
->num_files
||
1282 (job
->printer
->pc
&& job
->printer
->pc
->single_file
))
1283 cupsdClosePipe(job
->print_pipes
);
1285 if (job
->current_file
== job
->num_files
)
1287 close(job
->status_pipes
[1]);
1288 job
->status_pipes
[1] = -1;
1292 cupsdClosePipe(filterfds
[slot
]);
1294 for (i
= 6; i
< argc
; i
++)
1300 if (printer_state_reasons
)
1301 free(printer_state_reasons
);
1303 cupsdAddSelect(job
->status_buffer
->fd
, (cupsd_selfunc_t
)update_job
, NULL
,
1306 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
, "Job #%d started.",
1313 * If we get here, we need to abort the current job and close out all
1314 * files and pipes...
1319 FilterLevel
-= job
->cost
;
1322 for (slot
= 0; slot
< 2; slot
++)
1323 cupsdClosePipe(filterfds
[slot
]);
1325 cupsArrayDelete(filters
);
1329 for (i
= 6; i
< argc
; i
++)
1334 if (printer_state_reasons
)
1335 free(printer_state_reasons
);
1337 cupsdClosePipe(job
->print_pipes
);
1338 cupsdClosePipe(job
->back_pipes
);
1339 cupsdClosePipe(job
->side_pipes
);
1341 cupsdRemoveSelect(job
->status_pipes
[0]);
1342 cupsdClosePipe(job
->status_pipes
);
1343 cupsdStatBufDelete(job
->status_buffer
);
1344 job
->status_buffer
= NULL
;
1347 * Update the printer and job state.
1350 cupsdSetJobState(job
, abort_state
, CUPSD_JOB_DEFAULT
, "%s", abort_message
);
1351 cupsdSetPrinterState(job
->printer
, IPP_PRINTER_IDLE
, 0);
1352 update_job_attrs(job
, 0);
1355 free_job_history(job
);
1357 cupsArrayRemove(PrintingJobs
, job
);
1360 * Clear the printer <-> job association...
1363 job
->printer
->job
= NULL
;
1364 job
->printer
= NULL
;
1369 * 'cupsdDeleteJob()' - Free all memory used by a job.
1373 cupsdDeleteJob(cupsd_job_t
*job
, /* I - Job */
1374 cupsd_jobaction_t action
)/* I - Action */
1376 int i
; /* Looping var */
1380 finalize_job(job
, 1);
1382 if (action
== CUPSD_JOB_PURGE
)
1383 remove_job_history(job
);
1385 cupsdClearString(&job
->username
);
1386 cupsdClearString(&job
->dest
);
1388 i
< (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0]));
1390 cupsdClearString(job
->auth_env
+ i
);
1391 cupsdClearString(&job
->auth_uid
);
1393 if (action
== CUPSD_JOB_PURGE
)
1394 remove_job_files(job
);
1395 else if (job
->num_files
> 0)
1397 free(job
->compressions
);
1398 free(job
->filetypes
);
1404 free_job_history(job
);
1408 cupsArrayRemove(Jobs
, job
);
1409 cupsArrayRemove(ActiveJobs
, job
);
1410 cupsArrayRemove(PrintingJobs
, job
);
1417 * 'cupsdFreeAllJobs()' - Free all jobs from memory.
1421 cupsdFreeAllJobs(void)
1423 cupsd_job_t
*job
; /* Current job */
1431 cupsdStopAllJobs(CUPSD_JOB_FORCE
, 0);
1434 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1436 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1437 cupsdDeleteJob(job
, CUPSD_JOB_DEFAULT
);
1439 cupsdReleaseSignals();
1444 * 'cupsdFindJob()' - Find the specified job.
1447 cupsd_job_t
* /* O - Job data */
1448 cupsdFindJob(int id
) /* I - Job ID */
1450 cupsd_job_t key
; /* Search key */
1455 return ((cupsd_job_t
*)cupsArrayFind(Jobs
, &key
));
1460 * 'cupsdGetCompletedJobs()'- Generate a completed jobs list.
1463 cups_array_t
* /* O - Array of jobs */
1464 cupsdGetCompletedJobs(
1465 cupsd_printer_t
*p
) /* I - Printer */
1467 cups_array_t
*list
; /* Array of jobs */
1468 cupsd_job_t
*job
; /* Current job */
1471 list
= cupsArrayNew(compare_completed_jobs
, NULL
);
1473 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1475 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1476 if ((!p
|| !_cups_strcasecmp(p
->name
, job
->dest
)) && job
->state_value
>= IPP_JOB_STOPPED
&& job
->completed_time
)
1477 cupsArrayAdd(list
, job
);
1484 * 'cupsdGetPrinterJobCount()' - Get the number of pending, processing,
1485 * or held jobs in a printer or class.
1488 int /* O - Job count */
1489 cupsdGetPrinterJobCount(
1490 const char *dest
) /* I - Printer or class name */
1492 int count
; /* Job count */
1493 cupsd_job_t
*job
; /* Current job */
1496 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
1498 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
1499 if (job
->dest
&& !_cups_strcasecmp(job
->dest
, dest
))
1507 * 'cupsdGetUserJobCount()' - Get the number of pending, processing,
1508 * or held jobs for a user.
1511 int /* O - Job count */
1512 cupsdGetUserJobCount(
1513 const char *username
) /* I - Username */
1515 int count
; /* Job count */
1516 cupsd_job_t
*job
; /* Current job */
1519 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
1521 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
1522 if (!_cups_strcasecmp(job
->username
, username
))
1530 * 'cupsdLoadAllJobs()' - Load all jobs from disk.
1534 cupsdLoadAllJobs(void)
1536 char filename
[1024]; /* Full filename of job.cache file */
1537 struct stat fileinfo
, /* Information on job.cache file */
1538 dirinfo
; /* Information on RequestRoot dir */
1543 * Create the job arrays as needed...
1547 Jobs
= cupsArrayNew(compare_jobs
, NULL
);
1550 ActiveJobs
= cupsArrayNew(compare_active_jobs
, NULL
);
1553 PrintingJobs
= cupsArrayNew(compare_jobs
, NULL
);
1556 * See whether the job.cache file is older than the RequestRoot directory...
1559 snprintf(filename
, sizeof(filename
), "%s/job.cache", CacheDir
);
1561 if (stat(filename
, &fileinfo
))
1563 fileinfo
.st_mtime
= 0;
1565 if (errno
!= ENOENT
)
1566 cupsdLogMessage(CUPSD_LOG_ERROR
,
1567 "Unable to get file information for \"%s\" - %s",
1568 filename
, strerror(errno
));
1571 if (stat(RequestRoot
, &dirinfo
))
1573 dirinfo
.st_mtime
= 0;
1575 if (errno
!= ENOENT
)
1576 cupsdLogMessage(CUPSD_LOG_ERROR
,
1577 "Unable to get directory information for \"%s\" - %s",
1578 RequestRoot
, strerror(errno
));
1582 * Load the most recent source for job data...
1585 if (dirinfo
.st_mtime
> fileinfo
.st_mtime
)
1587 load_request_root();
1589 load_next_job_id(filename
);
1592 load_job_cache(filename
);
1595 * Clean out old jobs as needed...
1598 if (MaxJobs
> 0 && cupsArrayCount(Jobs
) >= MaxJobs
)
1604 * 'cupsdLoadJob()' - Load a single job.
1607 int /* O - 1 on success, 0 on failure */
1608 cupsdLoadJob(cupsd_job_t
*job
) /* I - Job */
1610 int i
; /* Looping var */
1611 char jobfile
[1024]; /* Job filename */
1612 cups_file_t
*fp
; /* Job file */
1613 int fileid
; /* Current file ID */
1614 ipp_attribute_t
*attr
; /* Job attribute */
1615 const char *dest
; /* Destination name */
1616 cupsd_printer_t
*destptr
; /* Pointer to destination */
1617 mime_type_t
**filetypes
; /* New filetypes array */
1618 int *compressions
; /* New compressions array */
1623 if (job
->state_value
> IPP_JOB_STOPPED
)
1624 job
->access_time
= time(NULL
);
1629 if ((job
->attrs
= ippNew()) == NULL
)
1631 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Ran out of memory for job attributes.");
1636 * Load job attributes...
1639 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Loading attributes...");
1641 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, job
->id
);
1642 if ((fp
= cupsdOpenConfFile(jobfile
)) == NULL
)
1645 if (ippReadIO(fp
, (ipp_iocb_t
)cupsFileRead
, 1, NULL
, job
->attrs
) != IPP_DATA
)
1647 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1648 "Unable to read job control file \"%s\".", jobfile
);
1656 * Copy attribute data to the job object...
1659 if (!ippFindAttribute(job
->attrs
, "time-at-creation", IPP_TAG_INTEGER
))
1661 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1662 "Missing or bad time-at-creation attribute in control file.");
1666 if ((job
->state
= ippFindAttribute(job
->attrs
, "job-state",
1667 IPP_TAG_ENUM
)) == NULL
)
1669 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1670 "Missing or bad job-state attribute in control file.");
1674 job
->state_value
= (ipp_jstate_t
)job
->state
->values
[0].integer
;
1676 job
->history_time
= 0;
1678 if ((attr
= ippFindAttribute(job
->attrs
, "time-at-creation", IPP_TAG_INTEGER
)) != NULL
)
1679 job
->creation_time
= attr
->values
[0].integer
;
1681 if (job
->state_value
>= IPP_JOB_CANCELED
&& (attr
= ippFindAttribute(job
->attrs
, "time-at-completed", IPP_TAG_INTEGER
)) != NULL
)
1683 job
->completed_time
= attr
->values
[0].integer
;
1685 if (JobHistory
< INT_MAX
)
1686 job
->history_time
= attr
->values
[0].integer
+ JobHistory
;
1688 job
->history_time
= INT_MAX
;
1690 if (job
->history_time
< time(NULL
))
1691 goto error
; /* Expired, remove from history */
1693 if (job
->history_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
1694 JobHistoryUpdate
= job
->history_time
;
1696 if (JobFiles
< INT_MAX
)
1697 job
->file_time
= attr
->values
[0].integer
+ JobFiles
;
1699 job
->file_time
= INT_MAX
;
1701 if (job
->file_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
1702 JobHistoryUpdate
= job
->file_time
;
1704 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdLoadJob: JobHistoryUpdate=%ld",
1705 (long)JobHistoryUpdate
);
1710 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1711 IPP_TAG_URI
)) == NULL
)
1713 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1714 "No job-printer-uri attribute in control file.");
1718 if ((dest
= cupsdValidateDest(attr
->values
[0].string
.text
, &(job
->dtype
),
1721 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1722 "Unable to queue job for destination \"%s\".",
1723 attr
->values
[0].string
.text
);
1727 cupsdSetString(&job
->dest
, dest
);
1729 else if ((destptr
= cupsdFindDest(job
->dest
)) == NULL
)
1731 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1732 "Unable to queue job for destination \"%s\".",
1737 if ((job
->reasons
= ippFindAttribute(job
->attrs
, "job-state-reasons",
1738 IPP_TAG_KEYWORD
)) == NULL
)
1740 const char *reason
; /* job-state-reason keyword */
1742 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
1743 "Adding missing job-state-reasons attribute to control file.");
1745 switch (job
->state_value
)
1748 case IPP_JOB_PENDING
:
1749 if (destptr
->state
== IPP_PRINTER_STOPPED
)
1750 reason
= "printer-stopped";
1756 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1757 IPP_TAG_ZERO
)) != NULL
&&
1758 (attr
->value_tag
== IPP_TAG_NAME
||
1759 attr
->value_tag
== IPP_TAG_NAMELANG
||
1760 attr
->value_tag
== IPP_TAG_KEYWORD
) &&
1761 strcmp(attr
->values
[0].string
.text
, "no-hold"))
1762 reason
= "job-hold-until-specified";
1764 reason
= "job-incoming";
1767 case IPP_JOB_PROCESSING
:
1768 reason
= "job-printing";
1771 case IPP_JOB_STOPPED
:
1772 reason
= "job-stopped";
1775 case IPP_JOB_CANCELED
:
1776 reason
= "job-canceled-by-user";
1779 case IPP_JOB_ABORTED
:
1780 reason
= "aborted-by-system";
1783 case IPP_JOB_COMPLETED
:
1784 reason
= "job-completed-successfully";
1788 job
->reasons
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1789 "job-state-reasons", NULL
, reason
);
1791 else if (job
->state_value
== IPP_JOB_PENDING
)
1793 if (destptr
->state
== IPP_PRINTER_STOPPED
)
1794 ippSetString(job
->attrs
, &job
->reasons
, 0, "printer-stopped");
1796 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
1799 job
->sheets
= ippFindAttribute(job
->attrs
, "job-media-sheets-completed",
1801 job
->job_sheets
= ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_NAME
);
1805 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
1806 IPP_TAG_INTEGER
)) == NULL
)
1808 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1809 "Missing or bad job-priority attribute in control file.");
1813 job
->priority
= attr
->values
[0].integer
;
1818 if ((attr
= ippFindAttribute(job
->attrs
, "job-originating-user-name",
1819 IPP_TAG_NAME
)) == NULL
)
1821 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1822 "Missing or bad job-originating-user-name "
1823 "attribute in control file.");
1827 cupsdSetString(&job
->username
, attr
->values
[0].string
.text
);
1832 if ((attr
= ippFindAttribute(job
->attrs
, "job-name", IPP_TAG_NAME
)) != NULL
)
1833 cupsdSetString(&job
->name
, attr
->values
[0].string
.text
);
1837 * Set the job hold-until time and state...
1840 if (job
->state_value
== IPP_JOB_HELD
)
1842 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1843 IPP_TAG_KEYWORD
)) == NULL
)
1844 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1847 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
, CUPSD_JOB_DEFAULT
);
1850 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1851 job
->state_value
= IPP_JOB_PENDING
;
1854 else if (job
->state_value
== IPP_JOB_PROCESSING
)
1856 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1857 job
->state_value
= IPP_JOB_PENDING
;
1860 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets", IPP_TAG_INTEGER
)) != NULL
)
1861 job
->koctets
= attr
->values
[0].integer
;
1863 if (!job
->num_files
)
1866 * Find all the d##### files...
1869 for (fileid
= 1; fileid
< 10000; fileid
++)
1871 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
1874 if (access(jobfile
, 0))
1877 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
1878 "Auto-typing document file \"%s\"...", jobfile
);
1880 if (fileid
> job
->num_files
)
1882 if (job
->num_files
== 0)
1884 compressions
= (int *)calloc((size_t)fileid
, sizeof(int));
1885 filetypes
= (mime_type_t
**)calloc((size_t)fileid
, sizeof(mime_type_t
*));
1889 compressions
= (int *)realloc(job
->compressions
, sizeof(int) * (size_t)fileid
);
1890 filetypes
= (mime_type_t
**)realloc(job
->filetypes
, sizeof(mime_type_t
*) * (size_t)fileid
);
1894 job
->compressions
= compressions
;
1897 job
->filetypes
= filetypes
;
1899 if (!compressions
|| !filetypes
)
1901 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1902 "Ran out of memory for job file types.");
1904 ippDelete(job
->attrs
);
1907 if (job
->compressions
)
1909 free(job
->compressions
);
1910 job
->compressions
= NULL
;
1915 free(job
->filetypes
);
1916 job
->filetypes
= NULL
;
1923 job
->num_files
= fileid
;
1926 job
->filetypes
[fileid
- 1] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
1927 job
->compressions
+ fileid
- 1);
1929 if (!job
->filetypes
[fileid
- 1])
1930 job
->filetypes
[fileid
- 1] = mimeType(MimeDatabase
, "application",
1936 * Load authentication information as needed...
1939 if (job
->state_value
< IPP_JOB_STOPPED
)
1941 snprintf(jobfile
, sizeof(jobfile
), "%s/a%05d", RequestRoot
, job
->id
);
1944 i
< (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0]));
1946 cupsdClearString(job
->auth_env
+ i
);
1947 cupsdClearString(&job
->auth_uid
);
1949 if ((fp
= cupsFileOpen(jobfile
, "r")) != NULL
)
1951 int bytes
, /* Size of auth data */
1952 linenum
= 1; /* Current line number */
1953 char line
[65536], /* Line from file */
1954 *value
, /* Value from line */
1955 data
[65536]; /* Decoded data */
1958 if (cupsFileGets(fp
, line
, sizeof(line
)) &&
1959 !strcmp(line
, "CUPSD-AUTH-V3"))
1962 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
1968 if (strcmp(line
, "negotiate") && strcmp(line
, "uid"))
1970 bytes
= sizeof(data
);
1971 httpDecode64_2(data
, &bytes
, value
);
1975 * Assign environment variables...
1978 if (!strcmp(line
, "uid"))
1980 cupsdSetStringf(&job
->auth_uid
, "AUTH_UID=%s", value
);
1983 else if (i
>= (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0])))
1986 if (!strcmp(line
, "username"))
1987 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_USERNAME=%s", data
);
1988 else if (!strcmp(line
, "domain"))
1989 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_DOMAIN=%s", data
);
1990 else if (!strcmp(line
, "password"))
1991 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_PASSWORD=%s", data
);
1992 else if (!strcmp(line
, "negotiate"))
1993 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_NEGOTIATE=%s", value
);
2005 job
->access_time
= time(NULL
);
2009 * If we get here then something bad happened...
2014 ippDelete(job
->attrs
);
2017 remove_job_history(job
);
2018 remove_job_files(job
);
2025 * 'cupsdMoveJob()' - Move the specified job to a different destination.
2029 cupsdMoveJob(cupsd_job_t
*job
, /* I - Job */
2030 cupsd_printer_t
*p
) /* I - Destination printer or class */
2032 ipp_attribute_t
*attr
; /* job-printer-uri attribute */
2033 const char *olddest
; /* Old destination */
2034 cupsd_printer_t
*oldp
; /* Old pointer */
2038 * Don't move completed jobs...
2041 if (job
->state_value
> IPP_JOB_STOPPED
)
2045 * Get the old destination...
2048 olddest
= job
->dest
;
2051 oldp
= job
->printer
;
2053 oldp
= cupsdFindDest(olddest
);
2056 * Change the destination information...
2059 if (job
->state_value
> IPP_JOB_HELD
)
2060 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
2061 "Stopping job prior to move.");
2063 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, oldp
, job
,
2064 "Job #%d moved from %s to %s.", job
->id
, olddest
,
2067 cupsdSetString(&job
->dest
, p
->name
);
2068 job
->dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_REMOTE
);
2070 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
2071 IPP_TAG_URI
)) != NULL
)
2072 cupsdSetString(&(attr
->values
[0].string
.text
), p
->uri
);
2074 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, p
, job
,
2075 "Job #%d moved from %s to %s.", job
->id
, olddest
,
2079 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2084 * 'cupsdReleaseJob()' - Release the specified job.
2088 cupsdReleaseJob(cupsd_job_t
*job
) /* I - Job */
2090 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdReleaseJob(job=%p(%d))", job
,
2093 if (job
->state_value
== IPP_JOB_HELD
)
2096 * Add trailing banner as needed...
2099 if (job
->pending_timeout
)
2100 cupsdTimeoutJob(job
);
2102 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
2103 "Job released by user.");
2109 * 'cupsdRestartJob()' - Restart the specified job.
2113 cupsdRestartJob(cupsd_job_t
*job
) /* I - Job */
2115 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdRestartJob(job=%p(%d))", job
,
2118 if (job
->state_value
== IPP_JOB_STOPPED
|| job
->num_files
)
2119 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
2120 "Job restarted by user.");
2125 * 'cupsdSaveAllJobs()' - Save a summary of all jobs to disk.
2129 cupsdSaveAllJobs(void)
2131 int i
; /* Looping var */
2132 cups_file_t
*fp
; /* job.cache file */
2133 char filename
[1024], /* job.cache filename */
2134 temp
[1024]; /* Temporary string */
2135 cupsd_job_t
*job
; /* Current job */
2136 time_t curtime
; /* Current time */
2137 struct tm
*curdate
; /* Current date */
2140 snprintf(filename
, sizeof(filename
), "%s/job.cache", CacheDir
);
2141 if ((fp
= cupsdCreateConfFile(filename
, ConfigFilePerm
)) == NULL
)
2144 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving job.cache...");
2147 * Write a small header to the file...
2150 curtime
= time(NULL
);
2151 curdate
= localtime(&curtime
);
2152 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
2154 cupsFilePuts(fp
, "# Job cache file for " CUPS_SVERSION
"\n");
2155 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
2156 cupsFilePrintf(fp
, "NextJobId %d\n", NextJobId
);
2159 * Write each job known to the system...
2162 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
2164 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
2166 cupsFilePrintf(fp
, "<Job %d>\n", job
->id
);
2167 cupsFilePrintf(fp
, "State %d\n", job
->state_value
);
2168 cupsFilePrintf(fp
, "Created %ld\n", (long)job
->creation_time
);
2169 if (job
->completed_time
)
2170 cupsFilePrintf(fp
, "Completed %ld\n", (long)job
->completed_time
);
2171 cupsFilePrintf(fp
, "Priority %d\n", job
->priority
);
2172 if (job
->hold_until
)
2173 cupsFilePrintf(fp
, "HoldUntil %ld\n", (long)job
->hold_until
);
2174 cupsFilePrintf(fp
, "Username %s\n", job
->username
);
2176 cupsFilePutConf(fp
, "Name", job
->name
);
2177 cupsFilePrintf(fp
, "Destination %s\n", job
->dest
);
2178 cupsFilePrintf(fp
, "DestType %d\n", job
->dtype
);
2179 cupsFilePrintf(fp
, "KOctets %d\n", job
->koctets
);
2180 cupsFilePrintf(fp
, "NumFiles %d\n", job
->num_files
);
2181 for (i
= 0; i
< job
->num_files
; i
++)
2182 cupsFilePrintf(fp
, "File %d %s/%s %d\n", i
+ 1, job
->filetypes
[i
]->super
,
2183 job
->filetypes
[i
]->type
, job
->compressions
[i
]);
2184 cupsFilePuts(fp
, "</Job>\n");
2187 cupsdCloseCreatedConfFile(fp
, filename
);
2192 * 'cupsdSaveJob()' - Save a job to disk.
2196 cupsdSaveJob(cupsd_job_t
*job
) /* I - Job */
2198 char filename
[1024]; /* Job control filename */
2199 cups_file_t
*fp
; /* Job file */
2202 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSaveJob(job=%p(%d)): job->attrs=%p",
2203 job
, job
->id
, job
->attrs
);
2205 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
, job
->id
);
2207 if ((fp
= cupsdCreateConfFile(filename
, ConfigFilePerm
& 0600)) == NULL
)
2210 fchown(cupsFileNumber(fp
), RunUser
, Group
);
2212 job
->attrs
->state
= IPP_IDLE
;
2214 if (ippWriteIO(fp
, (ipp_iocb_t
)cupsFileWrite
, 1, NULL
,
2215 job
->attrs
) != IPP_DATA
)
2217 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Unable to write job control file.");
2222 if (!cupsdCloseCreatedConfFile(fp
, filename
))
2225 * Remove backup file and mark this job as clean...
2228 strlcat(filename
, ".O", sizeof(filename
));
2237 * 'cupsdSetJobHoldUntil()' - Set the hold time for a job.
2241 cupsdSetJobHoldUntil(cupsd_job_t
*job
, /* I - Job */
2242 const char *when
, /* I - When to resume */
2243 int update
)/* I - Update job-hold-until attr? */
2245 time_t curtime
; /* Current time */
2246 struct tm
*curdate
; /* Current date */
2247 int hour
; /* Hold hour */
2248 int minute
; /* Hold minute */
2249 int second
= 0; /* Hold second */
2252 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2253 "cupsdSetJobHoldUntil(job=%p(%d), when=\"%s\", update=%d)",
2254 job
, job
->id
, when
, update
);
2259 * Update the job-hold-until attribute...
2262 ipp_attribute_t
*attr
; /* job-hold-until attribute */
2264 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2265 IPP_TAG_KEYWORD
)) == NULL
)
2266 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2269 cupsdSetString(&(attr
->values
[0].string
.text
), when
);
2271 attr
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
2272 "job-hold-until", NULL
, when
);
2276 if (isdigit(when
[0] & 255))
2277 attr
->value_tag
= IPP_TAG_NAME
;
2279 attr
->value_tag
= IPP_TAG_KEYWORD
;
2282 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2287 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-hold-until-specified");
2290 * Update the hold time...
2293 job
->cancel_time
= 0;
2295 if (!strcmp(when
, "indefinite") || !strcmp(when
, "auth-info-required"))
2298 * Hold indefinitely...
2301 job
->hold_until
= 0;
2303 if (MaxHoldTime
> 0)
2304 job
->cancel_time
= time(NULL
) + MaxHoldTime
;
2306 else if (!strcmp(when
, "day-time"))
2309 * Hold to 6am the next morning unless local time is < 6pm.
2312 curtime
= time(NULL
);
2313 curdate
= localtime(&curtime
);
2315 if (curdate
->tm_hour
< 18)
2316 job
->hold_until
= curtime
;
2318 job
->hold_until
= curtime
+
2319 ((29 - curdate
->tm_hour
) * 60 + 59 -
2320 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2322 else if (!strcmp(when
, "evening") || !strcmp(when
, "night"))
2325 * Hold to 6pm unless local time is > 6pm or < 6am.
2328 curtime
= time(NULL
);
2329 curdate
= localtime(&curtime
);
2331 if (curdate
->tm_hour
< 6 || curdate
->tm_hour
>= 18)
2332 job
->hold_until
= curtime
;
2334 job
->hold_until
= curtime
+
2335 ((17 - curdate
->tm_hour
) * 60 + 59 -
2336 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2338 else if (!strcmp(when
, "second-shift"))
2341 * Hold to 4pm unless local time is > 4pm.
2344 curtime
= time(NULL
);
2345 curdate
= localtime(&curtime
);
2347 if (curdate
->tm_hour
>= 16)
2348 job
->hold_until
= curtime
;
2350 job
->hold_until
= curtime
+
2351 ((15 - curdate
->tm_hour
) * 60 + 59 -
2352 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2354 else if (!strcmp(when
, "third-shift"))
2357 * Hold to 12am unless local time is < 8am.
2360 curtime
= time(NULL
);
2361 curdate
= localtime(&curtime
);
2363 if (curdate
->tm_hour
< 8)
2364 job
->hold_until
= curtime
;
2366 job
->hold_until
= curtime
+
2367 ((23 - curdate
->tm_hour
) * 60 + 59 -
2368 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2370 else if (!strcmp(when
, "weekend"))
2373 * Hold to weekend unless we are in the weekend.
2376 curtime
= time(NULL
);
2377 curdate
= localtime(&curtime
);
2379 if (curdate
->tm_wday
== 0 || curdate
->tm_wday
== 6)
2380 job
->hold_until
= curtime
;
2382 job
->hold_until
= curtime
+
2383 (((5 - curdate
->tm_wday
) * 24 +
2384 (17 - curdate
->tm_hour
)) * 60 + 59 -
2385 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2387 else if (sscanf(when
, "%d:%d:%d", &hour
, &minute
, &second
) >= 2)
2390 * Hold to specified GMT time (HH:MM or HH:MM:SS)...
2393 curtime
= time(NULL
);
2394 curdate
= gmtime(&curtime
);
2396 job
->hold_until
= curtime
+
2397 ((hour
- curdate
->tm_hour
) * 60 + minute
-
2398 curdate
->tm_min
) * 60 + second
- curdate
->tm_sec
;
2401 * Hold until next day as needed...
2404 if (job
->hold_until
< curtime
)
2405 job
->hold_until
+= 24 * 60 * 60;
2408 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetJobHoldUntil: hold_until=%d",
2409 (int)job
->hold_until
);
2414 * 'cupsdSetJobPriority()' - Set the priority of a job, moving it up/down in
2415 * the list as needed.
2419 cupsdSetJobPriority(
2420 cupsd_job_t
*job
, /* I - Job ID */
2421 int priority
) /* I - New priority (0 to 100) */
2423 ipp_attribute_t
*attr
; /* Job attribute */
2427 * Don't change completed jobs...
2430 if (job
->state_value
>= IPP_JOB_PROCESSING
)
2434 * Set the new priority and re-add the job into the active list...
2437 cupsArrayRemove(ActiveJobs
, job
);
2439 job
->priority
= priority
;
2441 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
2442 IPP_TAG_INTEGER
)) != NULL
)
2443 attr
->values
[0].integer
= priority
;
2445 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-priority",
2448 cupsArrayAdd(ActiveJobs
, job
);
2451 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2456 * 'cupsdSetJobState()' - Set the state of the specified print job.
2461 cupsd_job_t
*job
, /* I - Job to cancel */
2462 ipp_jstate_t newstate
, /* I - New job state */
2463 cupsd_jobaction_t action
, /* I - Action to take */
2464 const char *message
, /* I - Message to log */
2465 ...) /* I - Additional arguments as needed */
2467 int i
; /* Looping var */
2468 ipp_jstate_t oldstate
; /* Old state */
2469 char filename
[1024]; /* Job filename */
2470 ipp_attribute_t
*attr
; /* Job attribute */
2473 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2474 "cupsdSetJobState(job=%p(%d), state=%d, newstate=%d, "
2475 "action=%d, message=\"%s\")", job
, job
->id
, job
->state_value
,
2476 newstate
, action
, message
? message
: "(null)");
2480 * Make sure we have the job attributes...
2483 if (!cupsdLoadJob(job
))
2487 * Don't do anything if the state is unchanged and we aren't purging the
2491 oldstate
= job
->state_value
;
2492 if (newstate
== oldstate
&& action
!= CUPSD_JOB_PURGE
)
2496 * Stop any processes that are working on the current job...
2499 if (oldstate
== IPP_JOB_PROCESSING
)
2500 stop_job(job
, action
);
2503 * Set the new job state...
2506 job
->state_value
= newstate
;
2509 job
->state
->values
[0].integer
= newstate
;
2513 case IPP_JOB_PENDING
:
2515 * Update job-hold-until as needed...
2518 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2519 IPP_TAG_KEYWORD
)) == NULL
)
2520 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2524 attr
->value_tag
= IPP_TAG_KEYWORD
;
2525 cupsdSetString(&(attr
->values
[0].string
.text
), "no-hold");
2531 case IPP_JOB_ABORTED
:
2532 case IPP_JOB_CANCELED
:
2533 case IPP_JOB_COMPLETED
:
2534 set_time(job
, "time-at-completed");
2535 ippSetString(job
->attrs
, &job
->reasons
, 0, "processing-to-stop-point");
2540 * Log message as needed...
2545 char buffer
[2048]; /* Message buffer */
2546 va_list ap
; /* Pointer to additional arguments */
2548 va_start(ap
, message
);
2549 vsnprintf(buffer
, sizeof(buffer
), message
, ap
);
2552 if (newstate
> IPP_JOB_STOPPED
)
2553 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
, "%s", buffer
);
2555 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
, "%s", buffer
);
2557 if (newstate
== IPP_JOB_STOPPED
|| newstate
== IPP_JOB_ABORTED
)
2558 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "%s", buffer
);
2560 cupsdLogJob(job
, CUPSD_LOG_INFO
, "%s", buffer
);
2564 * Handle post-state-change actions...
2569 case IPP_JOB_PROCESSING
:
2571 * Add the job to the "printing" list...
2574 if (!cupsArrayFind(PrintingJobs
, job
))
2575 cupsArrayAdd(PrintingJobs
, job
);
2578 * Set the processing time...
2581 set_time(job
, "time-at-processing");
2583 case IPP_JOB_PENDING
:
2585 case IPP_JOB_STOPPED
:
2587 * Make sure the job is in the active list...
2590 if (!cupsArrayFind(ActiveJobs
, job
))
2591 cupsArrayAdd(ActiveJobs
, job
);
2594 * Save the job state to disk...
2598 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2601 case IPP_JOB_ABORTED
:
2602 case IPP_JOB_CANCELED
:
2603 case IPP_JOB_COMPLETED
:
2604 if (newstate
== IPP_JOB_CANCELED
)
2607 * Remove the job from the active list if there are no processes still
2611 for (i
= 0; job
->filters
[i
] < 0; i
++);
2613 if (!job
->filters
[i
] && job
->backend
<= 0)
2614 cupsArrayRemove(ActiveJobs
, job
);
2619 * Otherwise just remove the job from the active list immediately...
2622 cupsArrayRemove(ActiveJobs
, job
);
2626 * Expire job subscriptions since the job is now "completed"...
2629 cupsdExpireSubscriptions(NULL
, job
);
2633 * If we are going to sleep and the PrintingJobs count is now 0, allow the
2634 * sleep to happen immediately...
2637 if (Sleeping
&& cupsArrayCount(PrintingJobs
) == 0)
2639 #endif /* __APPLE__ */
2642 * Remove any authentication data...
2645 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
2646 if (cupsdRemoveFile(filename
) && errno
!= ENOENT
)
2647 cupsdLogMessage(CUPSD_LOG_ERROR
,
2648 "Unable to remove authentication cache: %s",
2652 i
< (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0]));
2654 cupsdClearString(job
->auth_env
+ i
);
2656 cupsdClearString(&job
->auth_uid
);
2659 * Remove the print file for good if we aren't preserving jobs or
2663 if (!JobHistory
|| !JobFiles
|| action
== CUPSD_JOB_PURGE
)
2664 remove_job_files(job
);
2666 if (JobHistory
&& action
!= CUPSD_JOB_PURGE
)
2669 * Save job state info...
2673 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2675 else if (!job
->printer
)
2678 * Delete the job immediately if not actively printing...
2681 cupsdDeleteJob(job
, CUPSD_JOB_PURGE
);
2688 * Finalize the job immediately if we forced things...
2691 if (action
>= CUPSD_JOB_FORCE
&& job
&& job
->printer
)
2692 finalize_job(job
, 0);
2695 * Update the server "busy" state...
2698 cupsdSetBusyState();
2703 * 'cupsdStopAllJobs()' - Stop all print jobs.
2708 cupsd_jobaction_t action
, /* I - Action */
2709 int kill_delay
) /* I - Number of seconds before we kill */
2711 cupsd_job_t
*job
; /* Current job */
2714 DEBUG_puts("cupsdStopAllJobs()");
2716 for (job
= (cupsd_job_t
*)cupsArrayFirst(PrintingJobs
);
2718 job
= (cupsd_job_t
*)cupsArrayNext(PrintingJobs
))
2722 cupsdSetJobState(job
, IPP_JOB_COMPLETED
, CUPSD_JOB_FORCE
, NULL
);
2727 job
->kill_time
= time(NULL
) + kill_delay
;
2729 cupsdSetJobState(job
, IPP_JOB_PENDING
, action
, NULL
);
2736 * 'cupsdUnloadCompletedJobs()' - Flush completed job history from memory.
2740 cupsdUnloadCompletedJobs(void)
2742 cupsd_job_t
*job
; /* Current job */
2743 time_t expire
; /* Expiration time */
2746 expire
= time(NULL
) - 60;
2748 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
2750 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
2751 if (job
->attrs
&& job
->state_value
>= IPP_JOB_STOPPED
&& !job
->printer
&&
2752 job
->access_time
< expire
)
2763 * 'cupsdUpdateJobs()' - Update the history/file files for all jobs.
2767 cupsdUpdateJobs(void)
2769 cupsd_job_t
*job
; /* Current job */
2770 time_t curtime
; /* Current time */
2771 ipp_attribute_t
*attr
; /* time-at-completed attribute */
2774 curtime
= time(NULL
);
2775 JobHistoryUpdate
= 0;
2777 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
2779 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
2781 if (job
->state_value
>= IPP_JOB_CANCELED
&&
2782 (attr
= ippFindAttribute(job
->attrs
, "time-at-completed",
2783 IPP_TAG_INTEGER
)) != NULL
)
2786 * Update history/file expiration times...
2789 if (JobHistory
< INT_MAX
)
2790 job
->history_time
= attr
->values
[0].integer
+ JobHistory
;
2792 job
->history_time
= INT_MAX
;
2794 if (job
->history_time
< curtime
)
2796 cupsdDeleteJob(job
, CUPSD_JOB_PURGE
);
2800 if (job
->history_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
2801 JobHistoryUpdate
= job
->history_time
;
2803 if (JobFiles
< INT_MAX
)
2804 job
->file_time
= attr
->values
[0].integer
+ JobFiles
;
2806 job
->file_time
= INT_MAX
;
2808 if (job
->file_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
2809 JobHistoryUpdate
= job
->file_time
;
2813 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdUpdateAllJobs: JobHistoryUpdate=%ld",
2814 (long)JobHistoryUpdate
);
2819 * 'compare_active_jobs()' - Compare the job IDs and priorities of two jobs.
2822 static int /* O - Difference */
2823 compare_active_jobs(void *first
, /* I - First job */
2824 void *second
, /* I - Second job */
2825 void *data
) /* I - App data (not used) */
2827 int diff
; /* Difference */
2832 if ((diff
= ((cupsd_job_t
*)second
)->priority
-
2833 ((cupsd_job_t
*)first
)->priority
) != 0)
2836 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
2841 * 'compare_completed_jobs()' - Compare the job IDs and completion times of two jobs.
2844 static int /* O - Difference */
2845 compare_completed_jobs(void *first
, /* I - First job */
2846 void *second
, /* I - Second job */
2847 void *data
) /* I - App data (not used) */
2849 int diff
; /* Difference */
2854 if ((diff
= ((cupsd_job_t
*)second
)->completed_time
-
2855 ((cupsd_job_t
*)first
)->completed_time
) != 0)
2858 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
2863 * 'compare_jobs()' - Compare the job IDs of two jobs.
2866 static int /* O - Difference */
2867 compare_jobs(void *first
, /* I - First job */
2868 void *second
, /* I - Second job */
2869 void *data
) /* I - App data (not used) */
2873 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
2878 * 'dump_job_history()' - Dump any debug messages for a job.
2882 dump_job_history(cupsd_job_t
*job
) /* I - Job */
2884 int i
, /* Looping var */
2885 oldsize
; /* Current MaxLogSize */
2886 struct tm
*date
; /* Date/time value */
2887 cupsd_joblog_t
*message
; /* Current message */
2888 char temp
[2048], /* Log message */
2889 *ptr
, /* Pointer into log message */
2890 start
[256], /* Start time */
2891 end
[256]; /* End time */
2892 cupsd_printer_t
*printer
; /* Printer for job */
2896 * See if we have anything to dump...
2903 * Disable log rotation temporarily...
2906 oldsize
= MaxLogSize
;
2910 * Copy the debug messages to the log...
2913 message
= (cupsd_joblog_t
*)cupsArrayFirst(job
->history
);
2914 date
= localtime(&(message
->time
));
2915 strftime(start
, sizeof(start
), "%X", date
);
2917 message
= (cupsd_joblog_t
*)cupsArrayLast(job
->history
);
2918 date
= localtime(&(message
->time
));
2919 strftime(end
, sizeof(end
), "%X", date
);
2921 snprintf(temp
, sizeof(temp
),
2922 "[Job %d] The following messages were recorded from %s to %s",
2923 job
->id
, start
, end
);
2924 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, temp
);
2926 for (message
= (cupsd_joblog_t
*)cupsArrayFirst(job
->history
);
2928 message
= (cupsd_joblog_t
*)cupsArrayNext(job
->history
))
2929 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, message
->message
);
2931 snprintf(temp
, sizeof(temp
), "[Job %d] End of messages", job
->id
);
2932 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, temp
);
2935 * Log the printer state values...
2938 if ((printer
= job
->printer
) == NULL
)
2939 printer
= cupsdFindDest(job
->dest
);
2943 snprintf(temp
, sizeof(temp
), "[Job %d] printer-state=%d(%s)", job
->id
,
2945 printer
->state
== IPP_PRINTER_IDLE
? "idle" :
2946 printer
->state
== IPP_PRINTER_PROCESSING
? "processing" :
2948 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, temp
);
2950 snprintf(temp
, sizeof(temp
), "[Job %d] printer-state-message=\"%s\"",
2951 job
->id
, printer
->state_message
);
2952 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, temp
);
2954 snprintf(temp
, sizeof(temp
), "[Job %d] printer-state-reasons=", job
->id
);
2955 ptr
= temp
+ strlen(temp
);
2956 if (printer
->num_reasons
== 0)
2957 strlcpy(ptr
, "none", sizeof(temp
) - (size_t)(ptr
- temp
));
2961 i
< printer
->num_reasons
&& ptr
< (temp
+ sizeof(temp
) - 2);
2967 strlcpy(ptr
, printer
->reasons
[i
], sizeof(temp
) - (size_t)(ptr
- temp
));
2971 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, temp
);
2975 * Restore log file rotation...
2978 MaxLogSize
= oldsize
;
2981 * Free all messages...
2984 free_job_history(job
);
2989 * 'free_job_history()' - Free any log history.
2993 free_job_history(cupsd_job_t
*job
) /* I - Job */
2995 char *message
; /* Current message */
3001 for (message
= (char *)cupsArrayFirst(job
->history
);
3003 message
= (char *)cupsArrayNext(job
->history
))
3006 cupsArrayDelete(job
->history
);
3007 job
->history
= NULL
;
3012 * 'finalize_job()' - Cleanup after job filter processes and support data.
3016 finalize_job(cupsd_job_t
*job
, /* I - Job */
3017 int set_job_state
) /* I - 1 = set the job state */
3019 ipp_pstate_t printer_state
; /* New printer state value */
3020 ipp_jstate_t job_state
; /* New job state value */
3021 const char *message
; /* Message for job state */
3022 char buffer
[1024]; /* Buffer for formatted messages */
3025 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "finalize_job(job=%p(%d))", job
, job
->id
);
3028 * Clear the "connecting-to-device" and "cups-waiting-for-job-completed"
3029 * reasons, which are only valid when a printer is processing, along with any
3030 * remote printing job state...
3033 cupsdSetPrinterReasons(job
->printer
, "-connecting-to-device,"
3034 "cups-waiting-for-job-completed,"
3035 "cups-remote-pending,"
3036 "cups-remote-pending-held,"
3037 "cups-remote-processing,"
3038 "cups-remote-stopped,"
3039 "cups-remote-canceled,"
3040 "cups-remote-aborted,"
3041 "cups-remote-completed");
3044 * Similarly, clear the "offline-report" reason for non-USB devices since we
3045 * rarely have current information for network devices...
3048 if (strncmp(job
->printer
->device_uri
, "usb:", 4) &&
3049 strncmp(job
->printer
->device_uri
, "ippusb:", 7))
3050 cupsdSetPrinterReasons(job
->printer
, "-offline-report");
3053 * Free the security profile...
3056 cupsdDestroyProfile(job
->profile
);
3057 job
->profile
= NULL
;
3058 cupsdDestroyProfile(job
->bprofile
);
3059 job
->bprofile
= NULL
;
3062 * Clear the unresponsive job watchdog timers...
3065 job
->cancel_time
= 0;
3069 * Close pipes and status buffer...
3072 cupsdClosePipe(job
->print_pipes
);
3073 cupsdClosePipe(job
->back_pipes
);
3074 cupsdClosePipe(job
->side_pipes
);
3076 cupsdRemoveSelect(job
->status_pipes
[0]);
3077 cupsdClosePipe(job
->status_pipes
);
3078 cupsdStatBufDelete(job
->status_buffer
);
3079 job
->status_buffer
= NULL
;
3082 * Process the exit status...
3085 if (job
->printer
->state
== IPP_PRINTER_PROCESSING
)
3086 printer_state
= IPP_PRINTER_IDLE
;
3088 printer_state
= job
->printer
->state
;
3090 switch (job_state
= job
->state_value
)
3092 case IPP_JOB_PENDING
:
3093 message
= "Job paused.";
3097 message
= "Job held.";
3101 case IPP_JOB_PROCESSING
:
3102 case IPP_JOB_COMPLETED
:
3103 job_state
= IPP_JOB_COMPLETED
;
3104 message
= "Job completed.";
3107 ippSetString(job
->attrs
, &job
->reasons
, 0,
3108 "job-completed-successfully");
3111 case IPP_JOB_STOPPED
:
3112 message
= "Job stopped.";
3114 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-stopped");
3117 case IPP_JOB_CANCELED
:
3118 message
= "Job canceled.";
3120 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-canceled-by-user");
3123 case IPP_JOB_ABORTED
:
3124 message
= "Job aborted.";
3128 if (job
->status
< 0)
3131 * Backend had errors...
3134 int exit_code
; /* Exit code from backend */
3137 * Convert the status to an exit code. Due to the way the W* macros are
3138 * implemented on MacOS X (bug?), we have to store the exit status in a
3139 * variable first and then convert...
3142 exit_code
= -job
->status
;
3143 if (WIFEXITED(exit_code
))
3144 exit_code
= WEXITSTATUS(exit_code
);
3147 ippSetString(job
->attrs
, &job
->reasons
, 0, "cups-backend-crashed");
3148 exit_code
= job
->status
;
3151 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Backend returned status %d (%s)",
3153 exit_code
== CUPS_BACKEND_FAILED
? "failed" :
3154 exit_code
== CUPS_BACKEND_AUTH_REQUIRED
?
3155 "authentication required" :
3156 exit_code
== CUPS_BACKEND_HOLD
? "hold job" :
3157 exit_code
== CUPS_BACKEND_STOP
? "stop printer" :
3158 exit_code
== CUPS_BACKEND_CANCEL
? "cancel job" :
3159 exit_code
== CUPS_BACKEND_RETRY
? "retry job later" :
3160 exit_code
== CUPS_BACKEND_RETRY_CURRENT
? "retry job immediately" :
3161 exit_code
< 0 ? "crashed" : "unknown");
3164 * Do what needs to be done...
3170 case CUPS_BACKEND_FAILED
:
3172 * Backend failure, use the error-policy to determine how to
3176 if (job
->dtype
& CUPS_PRINTER_CLASS
)
3179 * Queued on a class - mark the job as pending and we'll retry on
3180 * another printer...
3183 if (job_state
== IPP_JOB_COMPLETED
)
3185 job_state
= IPP_JOB_PENDING
;
3186 message
= "Retrying job on another printer.";
3188 ippSetString(job
->attrs
, &job
->reasons
, 0,
3189 "resources-are-not-ready");
3192 else if (!strcmp(job
->printer
->error_policy
, "retry-current-job"))
3195 * The error policy is "retry-current-job" - mark the job as pending
3196 * and we'll retry on the same printer...
3199 if (job_state
== IPP_JOB_COMPLETED
)
3201 job_state
= IPP_JOB_PENDING
;
3202 message
= "Retrying job on same printer.";
3204 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
3207 else if ((job
->printer
->type
& CUPS_PRINTER_FAX
) ||
3208 !strcmp(job
->printer
->error_policy
, "retry-job"))
3210 if (job_state
== IPP_JOB_COMPLETED
)
3213 * The job was queued on a fax or the error policy is "retry-job" -
3214 * hold the job if the number of retries is less than the
3215 * JobRetryLimit, otherwise abort the job.
3220 if (job
->tries
> JobRetryLimit
&& JobRetryLimit
> 0)
3226 snprintf(buffer
, sizeof(buffer
),
3227 "Job aborted after %d unsuccessful attempts.",
3229 job_state
= IPP_JOB_ABORTED
;
3232 ippSetString(job
->attrs
, &job
->reasons
, 0, "aborted-by-system");
3237 * Try again in N seconds...
3240 snprintf(buffer
, sizeof(buffer
),
3241 "Job held for %d seconds since it could not be sent.",
3244 job
->hold_until
= time(NULL
) + JobRetryInterval
;
3245 job_state
= IPP_JOB_HELD
;
3248 ippSetString(job
->attrs
, &job
->reasons
, 0,
3249 "resources-are-not-ready");
3253 else if (!strcmp(job
->printer
->error_policy
, "abort-job") &&
3254 job_state
== IPP_JOB_COMPLETED
)
3256 job_state
= IPP_JOB_ABORTED
;
3257 message
= "Job aborted due to backend errors; please consult "
3258 "the error_log file for details.";
3260 ippSetString(job
->attrs
, &job
->reasons
, 0, "aborted-by-system");
3262 else if (job
->state_value
== IPP_JOB_PROCESSING
)
3264 job_state
= IPP_JOB_PENDING
;
3265 printer_state
= IPP_PRINTER_STOPPED
;
3266 message
= "Printer stopped due to backend errors; please "
3267 "consult the error_log file for details.";
3269 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
3273 case CUPS_BACKEND_CANCEL
:
3278 if (job_state
== IPP_JOB_COMPLETED
)
3280 job_state
= IPP_JOB_CANCELED
;
3281 message
= "Job canceled at printer.";
3283 ippSetString(job
->attrs
, &job
->reasons
, 0, "canceled-at-device");
3287 case CUPS_BACKEND_HOLD
:
3288 if (job_state
== IPP_JOB_COMPLETED
)
3294 const char *reason
= ippGetString(job
->reasons
, 0, NULL
);
3296 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "job-state-reasons=\"%s\"",
3299 if (!reason
|| strncmp(reason
, "account-", 8))
3301 cupsdSetJobHoldUntil(job
, "indefinite", 1);
3303 ippSetString(job
->attrs
, &job
->reasons
, 0,
3304 "job-hold-until-specified");
3305 message
= "Job held indefinitely due to backend errors; please "
3306 "consult the error_log file for details.";
3308 else if (!strcmp(reason
, "account-info-needed"))
3310 cupsdSetJobHoldUntil(job
, "indefinite", 0);
3312 message
= "Job held indefinitely - account information is "
3315 else if (!strcmp(reason
, "account-closed"))
3317 cupsdSetJobHoldUntil(job
, "indefinite", 0);
3319 message
= "Job held indefinitely - account has been closed.";
3321 else if (!strcmp(reason
, "account-limit-reached"))
3323 cupsdSetJobHoldUntil(job
, "indefinite", 0);
3325 message
= "Job held indefinitely - account limit has been "
3330 cupsdSetJobHoldUntil(job
, "indefinite", 0);
3332 message
= "Job held indefinitely - account authorization failed.";
3335 job_state
= IPP_JOB_HELD
;
3339 case CUPS_BACKEND_STOP
:
3341 * Stop the printer...
3344 printer_state
= IPP_PRINTER_STOPPED
;
3345 message
= "Printer stopped due to backend errors; please "
3346 "consult the error_log file for details.";
3348 if (job_state
== IPP_JOB_COMPLETED
)
3350 job_state
= IPP_JOB_PENDING
;
3352 ippSetString(job
->attrs
, &job
->reasons
, 0,
3353 "resources-are-not-ready");
3357 case CUPS_BACKEND_AUTH_REQUIRED
:
3359 * Hold the job for authentication...
3362 if (job_state
== IPP_JOB_COMPLETED
)
3364 cupsdSetJobHoldUntil(job
, "auth-info-required", 1);
3366 job_state
= IPP_JOB_HELD
;
3367 message
= "Job held for authentication.";
3369 if (strncmp(job
->reasons
->values
[0].string
.text
, "account-", 8))
3370 ippSetString(job
->attrs
, &job
->reasons
, 0,
3371 "cups-held-for-authentication");
3375 case CUPS_BACKEND_RETRY
:
3376 if (job_state
== IPP_JOB_COMPLETED
)
3379 * Hold the job if the number of retries is less than the
3380 * JobRetryLimit, otherwise abort the job.
3385 if (job
->tries
> JobRetryLimit
&& JobRetryLimit
> 0)
3391 snprintf(buffer
, sizeof(buffer
),
3392 "Job aborted after %d unsuccessful attempts.",
3394 job_state
= IPP_JOB_ABORTED
;
3397 ippSetString(job
->attrs
, &job
->reasons
, 0, "aborted-by-system");
3402 * Try again in N seconds...
3405 snprintf(buffer
, sizeof(buffer
),
3406 "Job held for %d seconds since it could not be sent.",
3409 job
->hold_until
= time(NULL
) + JobRetryInterval
;
3410 job_state
= IPP_JOB_HELD
;
3413 ippSetString(job
->attrs
, &job
->reasons
, 0,
3414 "resources-are-not-ready");
3419 case CUPS_BACKEND_RETRY_CURRENT
:
3421 * Mark the job as pending and retry on the same printer...
3424 if (job_state
== IPP_JOB_COMPLETED
)
3426 job_state
= IPP_JOB_PENDING
;
3427 message
= "Retrying job on same printer.";
3429 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
3434 else if (job
->status
> 0)
3437 * Filter had errors; stop job...
3440 if (job_state
== IPP_JOB_COMPLETED
)
3442 job_state
= IPP_JOB_STOPPED
;
3443 message
= "Job stopped due to filter errors; please consult the "
3444 "error_log file for details.";
3446 if (WIFSIGNALED(job
->status
))
3447 ippSetString(job
->attrs
, &job
->reasons
, 0, "cups-filter-crashed");
3449 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-completed-with-errors");
3454 * Update the printer and job state.
3457 if (set_job_state
&& job_state
!= job
->state_value
)
3458 cupsdSetJobState(job
, job_state
, CUPSD_JOB_DEFAULT
, "%s", message
);
3460 cupsdSetPrinterState(job
->printer
, printer_state
,
3461 printer_state
== IPP_PRINTER_STOPPED
);
3462 update_job_attrs(job
, 0);
3467 (job
->state_value
== IPP_JOB_ABORTED
||
3468 job
->state_value
== IPP_JOB_STOPPED
))
3469 dump_job_history(job
);
3471 free_job_history(job
);
3474 cupsArrayRemove(PrintingJobs
, job
);
3477 * Apply any PPD updates...
3480 if (job
->num_keywords
)
3482 if (cupsdUpdatePrinterPPD(job
->printer
, job
->num_keywords
, job
->keywords
))
3483 cupsdSetPrinterAttrs(job
->printer
);
3485 cupsFreeOptions(job
->num_keywords
, job
->keywords
);
3487 job
->num_keywords
= 0;
3488 job
->keywords
= NULL
;
3492 * Clear the printer <-> job association...
3495 job
->printer
->job
= NULL
;
3496 job
->printer
= NULL
;
3499 * Try printing another job...
3502 if (printer_state
!= IPP_PRINTER_STOPPED
)
3508 * 'get_options()' - Get a string containing the job options.
3511 static char * /* O - Options string */
3512 get_options(cupsd_job_t
*job
, /* I - Job */
3513 int banner_page
, /* I - Printing a banner page? */
3514 char *copies
, /* I - Copies buffer */
3515 size_t copies_size
, /* I - Size of copies buffer */
3516 char *title
, /* I - Title buffer */
3517 size_t title_size
) /* I - Size of title buffer */
3519 int i
; /* Looping var */
3520 size_t newlength
; /* New option buffer length */
3521 char *optptr
, /* Pointer to options */
3522 *valptr
; /* Pointer in value string */
3523 ipp_attribute_t
*attr
; /* Current attribute */
3524 _ppd_cache_t
*pc
; /* PPD cache and mapping data */
3525 int num_pwgppds
; /* Number of PWG->PPD options */
3526 cups_option_t
*pwgppds
, /* PWG->PPD options */
3527 *pwgppd
, /* Current PWG->PPD option */
3528 *preset
; /* Current preset option */
3529 int print_color_mode
,
3530 /* Output mode (if any) */
3531 print_quality
; /* Print quality (if any) */
3532 const char *ppd
; /* PPD option choice */
3533 int exact
; /* Did we get an exact match? */
3534 static char *options
= NULL
;/* Full list of options */
3535 static size_t optlength
= 0; /* Length of option buffer */
3539 * Building the options string is harder than it needs to be, but for the
3540 * moment we need to pass strings for command-line args and not IPP attribute
3543 * First build an options array for any PWG->PPD mapped option/choice pairs.
3546 pc
= job
->printer
->pc
;
3551 !ippFindAttribute(job
->attrs
,
3552 "com.apple.print.DocumentTicket.PMSpoolFormat",
3554 !ippFindAttribute(job
->attrs
, "APPrinterPreset", IPP_TAG_ZERO
) &&
3555 (ippFindAttribute(job
->attrs
, "print-color-mode", IPP_TAG_ZERO
) ||
3556 ippFindAttribute(job
->attrs
, "print-quality", IPP_TAG_ZERO
)))
3559 * Map print-color-mode and print-quality to a preset...
3562 if ((attr
= ippFindAttribute(job
->attrs
, "print-color-mode",
3563 IPP_TAG_KEYWORD
)) != NULL
&&
3564 !strcmp(attr
->values
[0].string
.text
, "monochrome"))
3565 print_color_mode
= _PWG_PRINT_COLOR_MODE_MONOCHROME
;
3567 print_color_mode
= _PWG_PRINT_COLOR_MODE_COLOR
;
3569 if ((attr
= ippFindAttribute(job
->attrs
, "print-quality",
3570 IPP_TAG_ENUM
)) != NULL
&&
3571 attr
->values
[0].integer
>= IPP_QUALITY_DRAFT
&&
3572 attr
->values
[0].integer
<= IPP_QUALITY_HIGH
)
3573 print_quality
= attr
->values
[0].integer
- IPP_QUALITY_DRAFT
;
3575 print_quality
= _PWG_PRINT_QUALITY_NORMAL
;
3577 if (pc
->num_presets
[print_color_mode
][print_quality
] == 0)
3580 * Try to find a preset that works so that we maximize the chances of us
3581 * getting a good print using IPP attributes.
3584 if (pc
->num_presets
[print_color_mode
][_PWG_PRINT_QUALITY_NORMAL
] > 0)
3585 print_quality
= _PWG_PRINT_QUALITY_NORMAL
;
3586 else if (pc
->num_presets
[_PWG_PRINT_COLOR_MODE_COLOR
][print_quality
] > 0)
3587 print_color_mode
= _PWG_PRINT_COLOR_MODE_COLOR
;
3590 print_quality
= _PWG_PRINT_QUALITY_NORMAL
;
3591 print_color_mode
= _PWG_PRINT_COLOR_MODE_COLOR
;
3595 if (pc
->num_presets
[print_color_mode
][print_quality
] > 0)
3598 * Copy the preset options as long as the corresponding names are not
3599 * already defined in the IPP request...
3602 for (i
= pc
->num_presets
[print_color_mode
][print_quality
],
3603 preset
= pc
->presets
[print_color_mode
][print_quality
];
3607 if (!ippFindAttribute(job
->attrs
, preset
->name
, IPP_TAG_ZERO
))
3608 num_pwgppds
= cupsAddOption(preset
->name
, preset
->value
, num_pwgppds
,
3616 if (!ippFindAttribute(job
->attrs
, "InputSlot", IPP_TAG_ZERO
) &&
3617 !ippFindAttribute(job
->attrs
, "HPPaperSource", IPP_TAG_ZERO
))
3619 if ((ppd
= _ppdCacheGetInputSlot(pc
, job
->attrs
, NULL
)) != NULL
)
3620 num_pwgppds
= cupsAddOption(pc
->source_option
, ppd
, num_pwgppds
,
3623 if (!ippFindAttribute(job
->attrs
, "MediaType", IPP_TAG_ZERO
) &&
3624 (ppd
= _ppdCacheGetMediaType(pc
, job
->attrs
, NULL
)) != NULL
)
3625 num_pwgppds
= cupsAddOption("MediaType", ppd
, num_pwgppds
, &pwgppds
);
3627 if (!ippFindAttribute(job
->attrs
, "PageRegion", IPP_TAG_ZERO
) &&
3628 !ippFindAttribute(job
->attrs
, "PageSize", IPP_TAG_ZERO
) &&
3629 (ppd
= _ppdCacheGetPageSize(pc
, job
->attrs
, NULL
, &exact
)) != NULL
)
3631 num_pwgppds
= cupsAddOption("PageSize", ppd
, num_pwgppds
, &pwgppds
);
3633 if (!ippFindAttribute(job
->attrs
, "media", IPP_TAG_ZERO
))
3634 num_pwgppds
= cupsAddOption("media", ppd
, num_pwgppds
, &pwgppds
);
3637 if (!ippFindAttribute(job
->attrs
, "OutputBin", IPP_TAG_ZERO
) &&
3638 (attr
= ippFindAttribute(job
->attrs
, "output-bin",
3639 IPP_TAG_ZERO
)) != NULL
&&
3640 (attr
->value_tag
== IPP_TAG_KEYWORD
||
3641 attr
->value_tag
== IPP_TAG_NAME
) &&
3642 (ppd
= _ppdCacheGetOutputBin(pc
, attr
->values
[0].string
.text
)) != NULL
)
3645 * Map output-bin to OutputBin option...
3648 num_pwgppds
= cupsAddOption("OutputBin", ppd
, num_pwgppds
, &pwgppds
);
3651 if (pc
->sides_option
&&
3652 !ippFindAttribute(job
->attrs
, pc
->sides_option
, IPP_TAG_ZERO
) &&
3653 (attr
= ippFindAttribute(job
->attrs
, "sides", IPP_TAG_KEYWORD
)) != NULL
)
3656 * Map sides to duplex option...
3659 if (!strcmp(attr
->values
[0].string
.text
, "one-sided"))
3660 num_pwgppds
= cupsAddOption(pc
->sides_option
, pc
->sides_1sided
,
3661 num_pwgppds
, &pwgppds
);
3662 else if (!strcmp(attr
->values
[0].string
.text
, "two-sided-long-edge"))
3663 num_pwgppds
= cupsAddOption(pc
->sides_option
, pc
->sides_2sided_long
,
3664 num_pwgppds
, &pwgppds
);
3665 else if (!strcmp(attr
->values
[0].string
.text
, "two-sided-short-edge"))
3666 num_pwgppds
= cupsAddOption(pc
->sides_option
, pc
->sides_2sided_short
,
3667 num_pwgppds
, &pwgppds
);
3671 * Map finishings values...
3674 num_pwgppds
= _ppdCacheGetFinishingOptions(pc
, job
->attrs
,
3675 IPP_FINISHINGS_NONE
, num_pwgppds
,
3680 * Figure out how much room we need...
3683 newlength
= ipp_length(job
->attrs
);
3685 for (i
= num_pwgppds
, pwgppd
= pwgppds
; i
> 0; i
--, pwgppd
++)
3686 newlength
+= 1 + strlen(pwgppd
->name
) + 1 + strlen(pwgppd
->value
);
3689 * Then allocate/reallocate the option buffer as needed...
3692 if (newlength
== 0) /* This can never happen, but Clang */
3693 newlength
= 1; /* thinks it can... */
3695 if (newlength
> optlength
|| !options
)
3698 optptr
= malloc(newlength
);
3700 optptr
= realloc(options
, newlength
);
3704 cupsdLogJob(job
, CUPSD_LOG_CRIT
,
3705 "Unable to allocate " CUPS_LLFMT
" bytes for option buffer.",
3706 CUPS_LLCAST newlength
);
3711 optlength
= newlength
;
3715 * Now loop through the attributes and convert them to the textual
3716 * representation used by the filters...
3722 snprintf(title
, title_size
, "%s-%d", job
->printer
->name
, job
->id
);
3723 strlcpy(copies
, "1", copies_size
);
3725 for (attr
= job
->attrs
->attrs
; attr
!= NULL
; attr
= attr
->next
)
3727 if (!strcmp(attr
->name
, "copies") &&
3728 attr
->value_tag
== IPP_TAG_INTEGER
)
3731 * Don't use the # copies attribute if we are printing the job sheets...
3735 snprintf(copies
, copies_size
, "%d", attr
->values
[0].integer
);
3737 else if (!strcmp(attr
->name
, "job-name") &&
3738 (attr
->value_tag
== IPP_TAG_NAME
||
3739 attr
->value_tag
== IPP_TAG_NAMELANG
))
3740 strlcpy(title
, attr
->values
[0].string
.text
, title_size
);
3741 else if (attr
->group_tag
== IPP_TAG_JOB
)
3744 * Filter out other unwanted attributes...
3747 if (attr
->value_tag
== IPP_TAG_NOVALUE
||
3748 attr
->value_tag
== IPP_TAG_MIMETYPE
||
3749 attr
->value_tag
== IPP_TAG_NAMELANG
||
3750 attr
->value_tag
== IPP_TAG_TEXTLANG
||
3751 (attr
->value_tag
== IPP_TAG_URI
&& strcmp(attr
->name
, "job-uuid") &&
3752 strcmp(attr
->name
, "job-authorization-uri")) ||
3753 attr
->value_tag
== IPP_TAG_URISCHEME
||
3754 attr
->value_tag
== IPP_TAG_BEGIN_COLLECTION
) /* Not yet supported */
3757 if (!strcmp(attr
->name
, "job-hold-until") ||
3758 !strcmp(attr
->name
, "job-id") ||
3759 !strcmp(attr
->name
, "job-k-octets") ||
3760 !strcmp(attr
->name
, "job-media-sheets") ||
3761 !strcmp(attr
->name
, "job-media-sheets-completed") ||
3762 !strcmp(attr
->name
, "job-state") ||
3763 !strcmp(attr
->name
, "job-state-reasons"))
3766 if (!strncmp(attr
->name
, "job-", 4) &&
3767 strcmp(attr
->name
, "job-account-id") &&
3768 strcmp(attr
->name
, "job-accounting-user-id") &&
3769 strcmp(attr
->name
, "job-authorization-uri") &&
3770 strcmp(attr
->name
, "job-billing") &&
3771 strcmp(attr
->name
, "job-impressions") &&
3772 strcmp(attr
->name
, "job-originating-host-name") &&
3773 strcmp(attr
->name
, "job-password") &&
3774 strcmp(attr
->name
, "job-password-encryption") &&
3775 strcmp(attr
->name
, "job-uuid") &&
3776 !(job
->printer
->type
& CUPS_PRINTER_REMOTE
))
3779 if ((!strcmp(attr
->name
, "job-impressions") ||
3780 !strcmp(attr
->name
, "page-label") ||
3781 !strcmp(attr
->name
, "page-border") ||
3782 !strncmp(attr
->name
, "number-up", 9) ||
3783 !strcmp(attr
->name
, "page-ranges") ||
3784 !strcmp(attr
->name
, "page-set") ||
3785 !_cups_strcasecmp(attr
->name
, "AP_FIRSTPAGE_InputSlot") ||
3786 !_cups_strcasecmp(attr
->name
, "AP_FIRSTPAGE_ManualFeed") ||
3787 !_cups_strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
3788 "PMTotalSidesImaged..n.") ||
3789 !_cups_strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
3790 "PMTotalBeginPages..n.")) &&
3795 * Otherwise add them to the list...
3798 if (optptr
> options
)
3799 strlcat(optptr
, " ", optlength
- (size_t)(optptr
- options
));
3801 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
3803 strlcat(optptr
, attr
->name
, optlength
- (size_t)(optptr
- options
));
3804 strlcat(optptr
, "=", optlength
- (size_t)(optptr
- options
));
3807 for (i
= 0; i
< attr
->num_values
; i
++)
3810 strlcat(optptr
, ",", optlength
- (size_t)(optptr
- options
));
3812 optptr
+= strlen(optptr
);
3814 switch (attr
->value_tag
)
3816 case IPP_TAG_INTEGER
:
3818 snprintf(optptr
, optlength
- (size_t)(optptr
- options
),
3819 "%d", attr
->values
[i
].integer
);
3822 case IPP_TAG_BOOLEAN
:
3823 if (!attr
->values
[i
].boolean
)
3824 strlcat(optptr
, "no", optlength
- (size_t)(optptr
- options
));
3826 strlcat(optptr
, attr
->name
, optlength
- (size_t)(optptr
- options
));
3829 case IPP_TAG_RANGE
:
3830 if (attr
->values
[i
].range
.lower
== attr
->values
[i
].range
.upper
)
3831 snprintf(optptr
, optlength
- (size_t)(optptr
- options
) - 1,
3832 "%d", attr
->values
[i
].range
.lower
);
3834 snprintf(optptr
, optlength
- (size_t)(optptr
- options
) - 1,
3835 "%d-%d", attr
->values
[i
].range
.lower
,
3836 attr
->values
[i
].range
.upper
);
3839 case IPP_TAG_RESOLUTION
:
3840 snprintf(optptr
, optlength
- (size_t)(optptr
- options
) - 1,
3841 "%dx%d%s", attr
->values
[i
].resolution
.xres
,
3842 attr
->values
[i
].resolution
.yres
,
3843 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
3847 case IPP_TAG_STRING
:
3850 case IPP_TAG_KEYWORD
:
3851 case IPP_TAG_CHARSET
:
3852 case IPP_TAG_LANGUAGE
:
3854 for (valptr
= attr
->values
[i
].string
.text
; *valptr
;)
3856 if (strchr(" \t\n\\\'\"", *valptr
))
3858 *optptr
++ = *valptr
++;
3865 break; /* anti-compiler-warning-code */
3869 optptr
+= strlen(optptr
);
3874 * Finally loop through the PWG->PPD mapped options and add them...
3877 for (i
= num_pwgppds
, pwgppd
= pwgppds
; i
> 0; i
--, pwgppd
++)
3880 strlcpy(optptr
, pwgppd
->name
, optlength
- (size_t)(optptr
- options
));
3881 optptr
+= strlen(optptr
);
3883 strlcpy(optptr
, pwgppd
->value
, optlength
- (size_t)(optptr
- options
));
3884 optptr
+= strlen(optptr
);
3887 cupsFreeOptions(num_pwgppds
, pwgppds
);
3890 * Return the options string...
3898 * 'ipp_length()' - Compute the size of the buffer needed to hold
3899 * the textual IPP attributes.
3902 static size_t /* O - Size of attribute buffer */
3903 ipp_length(ipp_t
*ipp
) /* I - IPP request */
3905 size_t bytes
; /* Number of bytes */
3906 int i
; /* Looping var */
3907 ipp_attribute_t
*attr
; /* Current attribute */
3911 * Loop through all attributes...
3916 for (attr
= ipp
->attrs
; attr
!= NULL
; attr
= attr
->next
)
3919 * Skip attributes that won't be sent to filters...
3922 if (attr
->value_tag
== IPP_TAG_NOVALUE
||
3923 attr
->value_tag
== IPP_TAG_MIMETYPE
||
3924 attr
->value_tag
== IPP_TAG_NAMELANG
||
3925 attr
->value_tag
== IPP_TAG_TEXTLANG
||
3926 attr
->value_tag
== IPP_TAG_URI
||
3927 attr
->value_tag
== IPP_TAG_URISCHEME
)
3931 * Add space for a leading space and commas between each value.
3932 * For the first attribute, the leading space isn't used, so the
3933 * extra byte can be used as the nul terminator...
3936 bytes
++; /* " " separator */
3937 bytes
+= (size_t)attr
->num_values
; /* "," separators */
3940 * Boolean attributes appear as "foo,nofoo,foo,nofoo", while
3941 * other attributes appear as "foo=value1,value2,...,valueN".
3944 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
3945 bytes
+= strlen(attr
->name
);
3947 bytes
+= (size_t)attr
->num_values
* strlen(attr
->name
);
3950 * Now add the size required for each value in the attribute...
3953 switch (attr
->value_tag
)
3955 case IPP_TAG_INTEGER
:
3958 * Minimum value of a signed integer is -2147483647, or 11 digits.
3961 bytes
+= (size_t)attr
->num_values
* 11;
3964 case IPP_TAG_BOOLEAN
:
3966 * Add two bytes for each false ("no") value...
3969 for (i
= 0; i
< attr
->num_values
; i
++)
3970 if (!attr
->values
[i
].boolean
)
3974 case IPP_TAG_RANGE
:
3976 * A range is two signed integers separated by a hyphen, or
3977 * 23 characters max.
3980 bytes
+= (size_t)attr
->num_values
* 23;
3983 case IPP_TAG_RESOLUTION
:
3985 * A resolution is two signed integers separated by an "x" and
3986 * suffixed by the units, or 26 characters max.
3989 bytes
+= (size_t)attr
->num_values
* 26;
3992 case IPP_TAG_STRING
:
3995 case IPP_TAG_KEYWORD
:
3996 case IPP_TAG_CHARSET
:
3997 case IPP_TAG_LANGUAGE
:
4000 * Strings can contain characters that need quoting. We need
4001 * at least 2 * len + 2 characters to cover the quotes and
4002 * any backslashes in the string.
4005 for (i
= 0; i
< attr
->num_values
; i
++)
4006 bytes
+= 2 * strlen(attr
->values
[i
].string
.text
) + 2;
4010 break; /* anti-compiler-warning-code */
4019 * 'load_job_cache()' - Load jobs from the job.cache file.
4023 load_job_cache(const char *filename
) /* I - job.cache filename */
4025 cups_file_t
*fp
; /* job.cache file */
4026 char line
[1024], /* Line buffer */
4027 *value
; /* Value on line */
4028 int linenum
; /* Line number in file */
4029 cupsd_job_t
*job
; /* Current job */
4030 int jobid
; /* Job ID */
4031 char jobfile
[1024]; /* Job filename */
4035 * Open the job.cache file...
4038 if ((fp
= cupsdOpenConfFile(filename
)) == NULL
)
4040 load_request_root();
4045 * Read entries from the job cache file and create jobs as needed.
4048 cupsdLogMessage(CUPSD_LOG_INFO
, "Loading job cache file \"%s\"...",
4054 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
4056 if (!_cups_strcasecmp(line
, "NextJobId"))
4059 NextJobId
= atoi(value
);
4061 else if (!_cups_strcasecmp(line
, "<Job"))
4065 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing </Job> directive on line %d.",
4072 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing job ID on line %d.", linenum
);
4076 jobid
= atoi(value
);
4080 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad job ID %d on line %d.", jobid
,
4085 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, jobid
);
4086 if (access(jobfile
, 0))
4088 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d.N", RequestRoot
, jobid
);
4089 if (access(jobfile
, 0))
4091 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Job %d] Files have gone away.",
4097 job
= calloc(1, sizeof(cupsd_job_t
));
4100 cupsdLogMessage(CUPSD_LOG_EMERG
,
4101 "[Job %d] Unable to allocate memory for job.", jobid
);
4106 job
->back_pipes
[0] = -1;
4107 job
->back_pipes
[1] = -1;
4108 job
->print_pipes
[0] = -1;
4109 job
->print_pipes
[1] = -1;
4110 job
->side_pipes
[0] = -1;
4111 job
->side_pipes
[1] = -1;
4112 job
->status_pipes
[0] = -1;
4113 job
->status_pipes
[1] = -1;
4115 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Loading from cache...");
4119 cupsdLogMessage(CUPSD_LOG_ERROR
,
4120 "Missing <Job #> directive on line %d.", linenum
);
4123 else if (!_cups_strcasecmp(line
, "</Job>"))
4125 cupsArrayAdd(Jobs
, job
);
4127 if (job
->state_value
<= IPP_JOB_STOPPED
&& cupsdLoadJob(job
))
4128 cupsArrayAdd(ActiveJobs
, job
);
4129 else if (job
->state_value
> IPP_JOB_STOPPED
)
4131 if (!job
->completed_time
|| !job
->creation_time
|| !job
->name
|| !job
->koctets
)
4142 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing value on line %d.", linenum
);
4145 else if (!_cups_strcasecmp(line
, "State"))
4147 job
->state_value
= (ipp_jstate_t
)atoi(value
);
4149 if (job
->state_value
< IPP_JOB_PENDING
)
4150 job
->state_value
= IPP_JOB_PENDING
;
4151 else if (job
->state_value
> IPP_JOB_COMPLETED
)
4152 job
->state_value
= IPP_JOB_COMPLETED
;
4154 else if (!_cups_strcasecmp(line
, "Name"))
4156 cupsdSetString(&(job
->name
), value
);
4158 else if (!_cups_strcasecmp(line
, "Created"))
4160 job
->creation_time
= strtol(value
, NULL
, 10);
4162 else if (!_cups_strcasecmp(line
, "Completed"))
4164 job
->completed_time
= strtol(value
, NULL
, 10);
4166 else if (!_cups_strcasecmp(line
, "HoldUntil"))
4168 job
->hold_until
= strtol(value
, NULL
, 10);
4170 else if (!_cups_strcasecmp(line
, "Priority"))
4172 job
->priority
= atoi(value
);
4174 else if (!_cups_strcasecmp(line
, "Username"))
4176 cupsdSetString(&job
->username
, value
);
4178 else if (!_cups_strcasecmp(line
, "Destination"))
4180 cupsdSetString(&job
->dest
, value
);
4182 else if (!_cups_strcasecmp(line
, "DestType"))
4184 job
->dtype
= (cups_ptype_t
)atoi(value
);
4186 else if (!_cups_strcasecmp(line
, "KOctets"))
4188 job
->koctets
= atoi(value
);
4190 else if (!_cups_strcasecmp(line
, "NumFiles"))
4192 job
->num_files
= atoi(value
);
4194 if (job
->num_files
< 0)
4196 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad NumFiles value %d on line %d.",
4197 job
->num_files
, linenum
);
4202 if (job
->num_files
> 0)
4204 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-001", RequestRoot
,
4206 if (access(jobfile
, 0))
4208 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Data files have gone away.");
4213 job
->filetypes
= calloc((size_t)job
->num_files
, sizeof(mime_type_t
*));
4214 job
->compressions
= calloc((size_t)job
->num_files
, sizeof(int));
4216 if (!job
->filetypes
|| !job
->compressions
)
4218 cupsdLogJob(job
, CUPSD_LOG_EMERG
,
4219 "Unable to allocate memory for %d files.",
4225 else if (!_cups_strcasecmp(line
, "File"))
4227 int number
, /* File number */
4228 compression
; /* Compression value */
4229 char super
[MIME_MAX_SUPER
], /* MIME super type */
4230 type
[MIME_MAX_TYPE
]; /* MIME type */
4233 if (sscanf(value
, "%d%*[ \t]%15[^/]/%255s%d", &number
, super
, type
,
4236 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File on line %d.", linenum
);
4240 if (number
< 1 || number
> job
->num_files
)
4242 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File number %d on line %d.",
4249 job
->compressions
[number
] = compression
;
4250 job
->filetypes
[number
] = mimeType(MimeDatabase
, super
, type
);
4252 if (!job
->filetypes
[number
])
4255 * If the original MIME type is unknown, auto-type it!
4258 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
4259 "Unknown MIME type %s/%s for file %d.",
4260 super
, type
, number
+ 1);
4262 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
4263 job
->id
, number
+ 1);
4264 job
->filetypes
[number
] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
4265 job
->compressions
+ number
);
4268 * If that didn't work, assume it is raw...
4271 if (!job
->filetypes
[number
])
4272 job
->filetypes
[number
] = mimeType(MimeDatabase
, "application",
4277 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unknown %s directive on line %d.",
4283 cupsdLogMessage(CUPSD_LOG_ERROR
,
4284 "Missing </Job> directive on line %d.", linenum
);
4285 cupsdDeleteJob(job
, CUPSD_JOB_PURGE
);
4293 * 'load_next_job_id()' - Load the NextJobId value from the job.cache file.
4297 load_next_job_id(const char *filename
) /* I - job.cache filename */
4299 cups_file_t
*fp
; /* job.cache file */
4300 char line
[1024], /* Line buffer */
4301 *value
; /* Value on line */
4302 int linenum
; /* Line number in file */
4303 int next_job_id
; /* NextJobId value from line */
4307 * Read the NextJobId directive from the job.cache file and use
4308 * the value (if any).
4311 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
4313 if (errno
!= ENOENT
)
4314 cupsdLogMessage(CUPSD_LOG_ERROR
,
4315 "Unable to open job cache file \"%s\": %s",
4316 filename
, strerror(errno
));
4321 cupsdLogMessage(CUPSD_LOG_INFO
,
4322 "Loading NextJobId from job cache file \"%s\"...", filename
);
4326 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
4328 if (!_cups_strcasecmp(line
, "NextJobId"))
4332 next_job_id
= atoi(value
);
4334 if (next_job_id
> NextJobId
)
4335 NextJobId
= next_job_id
;
4346 * 'load_request_root()' - Load jobs from the RequestRoot directory.
4350 load_request_root(void)
4352 cups_dir_t
*dir
; /* Directory */
4353 cups_dentry_t
*dent
; /* Directory entry */
4354 cupsd_job_t
*job
; /* New job */
4358 * Open the requests directory...
4361 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Scanning %s for jobs...", RequestRoot
);
4363 if ((dir
= cupsDirOpen(RequestRoot
)) == NULL
)
4365 cupsdLogMessage(CUPSD_LOG_ERROR
,
4366 "Unable to open spool directory \"%s\": %s",
4367 RequestRoot
, strerror(errno
));
4372 * Read all the c##### files...
4375 while ((dent
= cupsDirRead(dir
)) != NULL
)
4376 if (strlen(dent
->filename
) >= 6 && dent
->filename
[0] == 'c')
4379 * Allocate memory for the job...
4382 if ((job
= calloc(sizeof(cupsd_job_t
), 1)) == NULL
)
4384 cupsdLogMessage(CUPSD_LOG_ERROR
, "Ran out of memory for jobs.");
4390 * Assign the job ID...
4393 job
->id
= atoi(dent
->filename
+ 1);
4394 job
->back_pipes
[0] = -1;
4395 job
->back_pipes
[1] = -1;
4396 job
->print_pipes
[0] = -1;
4397 job
->print_pipes
[1] = -1;
4398 job
->side_pipes
[0] = -1;
4399 job
->side_pipes
[1] = -1;
4400 job
->status_pipes
[0] = -1;
4401 job
->status_pipes
[1] = -1;
4403 if (job
->id
>= NextJobId
)
4404 NextJobId
= job
->id
+ 1;
4410 if (cupsdLoadJob(job
))
4413 * Insert the job into the array, sorting by job priority and ID...
4416 cupsArrayAdd(Jobs
, job
);
4418 if (job
->state_value
<= IPP_JOB_STOPPED
)
4419 cupsArrayAdd(ActiveJobs
, job
);
4432 * 'remove_job_files()' - Remove the document files for a job.
4436 remove_job_files(cupsd_job_t
*job
) /* I - Job */
4438 int i
; /* Looping var */
4439 char filename
[1024]; /* Document filename */
4442 if (job
->num_files
<= 0)
4445 for (i
= 1; i
<= job
->num_files
; i
++)
4447 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
4449 cupsdUnlinkOrRemoveFile(filename
);
4452 free(job
->filetypes
);
4453 free(job
->compressions
);
4457 job
->filetypes
= NULL
;
4458 job
->compressions
= NULL
;
4460 LastEvent
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
4465 * 'remove_job_history()' - Remove the control file for a job.
4469 remove_job_history(cupsd_job_t
*job
) /* I - Job */
4471 char filename
[1024]; /* Control filename */
4475 * Remove the job info file...
4478 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
,
4480 cupsdUnlinkOrRemoveFile(filename
);
4482 LastEvent
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
4487 * 'set_time()' - Set one of the "time-at-xyz" attributes.
4491 set_time(cupsd_job_t
*job
, /* I - Job to update */
4492 const char *name
) /* I - Name of attribute */
4494 ipp_attribute_t
*attr
; /* Time attribute */
4495 time_t curtime
; /* Current time */
4498 curtime
= time(NULL
);
4500 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "%s=%ld", name
, (long)curtime
);
4502 if ((attr
= ippFindAttribute(job
->attrs
, name
, IPP_TAG_ZERO
)) != NULL
)
4504 attr
->value_tag
= IPP_TAG_INTEGER
;
4505 attr
->values
[0].integer
= curtime
;
4508 if (!strcmp(name
, "time-at-completed"))
4510 job
->completed_time
= curtime
;
4512 if (JobHistory
< INT_MAX
&& attr
)
4513 job
->history_time
= attr
->values
[0].integer
+ JobHistory
;
4515 job
->history_time
= INT_MAX
;
4517 if (job
->history_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
4518 JobHistoryUpdate
= job
->history_time
;
4520 if (JobFiles
< INT_MAX
&& attr
)
4521 job
->file_time
= attr
->values
[0].integer
+ JobFiles
;
4523 job
->file_time
= INT_MAX
;
4525 if (job
->file_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
4526 JobHistoryUpdate
= job
->file_time
;
4528 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_time: JobHistoryUpdate=%ld",
4529 (long)JobHistoryUpdate
);
4535 * 'start_job()' - Start a print job.
4539 start_job(cupsd_job_t
*job
, /* I - Job ID */
4540 cupsd_printer_t
*printer
) /* I - Printer to print job */
4542 const char *filename
; /* Support filename */
4543 ipp_attribute_t
*cancel_after
= ippFindAttribute(job
->attrs
,
4546 /* job-cancel-after attribute */
4549 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "start_job(job=%p(%d), printer=%p(%s))",
4550 job
, job
->id
, printer
, printer
->name
);
4553 * Make sure we have some files around before we try to print...
4556 if (job
->num_files
== 0)
4558 ippSetString(job
->attrs
, &job
->reasons
, 0, "aborted-by-system");
4559 cupsdSetJobState(job
, IPP_JOB_ABORTED
, CUPSD_JOB_DEFAULT
,
4560 "Aborting job because it has no files.");
4565 * Update the printer and job state to "processing"...
4568 if (!cupsdLoadJob(job
))
4571 if (!job
->printer_message
)
4572 job
->printer_message
= ippFindAttribute(job
->attrs
,
4573 "job-printer-state-message",
4575 if (job
->printer_message
)
4576 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
), "");
4578 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-printing");
4579 cupsdSetJobState(job
, IPP_JOB_PROCESSING
, CUPSD_JOB_DEFAULT
, NULL
);
4580 cupsdSetPrinterState(printer
, IPP_PRINTER_PROCESSING
, 0);
4581 cupsdSetPrinterReasons(printer
, "-cups-remote-pending,"
4582 "cups-remote-pending-held,"
4583 "cups-remote-processing,"
4584 "cups-remote-stopped,"
4585 "cups-remote-canceled,"
4586 "cups-remote-aborted,"
4587 "cups-remote-completed");
4590 job
->current_file
= 0;
4592 job
->history_time
= 0;
4594 job
->printer
= printer
;
4598 job
->cancel_time
= time(NULL
) + ippGetInteger(cancel_after
, 0);
4599 else if (MaxJobTime
> 0)
4600 job
->cancel_time
= time(NULL
) + MaxJobTime
;
4602 job
->cancel_time
= 0;
4605 * Check for support files...
4608 cupsdSetPrinterReasons(job
->printer
, "-cups-missing-filter-warning,"
4609 "cups-insecure-filter-warning");
4613 for (filename
= (const char *)cupsArrayFirst(printer
->pc
->support_files
);
4615 filename
= (const char *)cupsArrayNext(printer
->pc
->support_files
))
4617 if (_cupsFileCheck(filename
, _CUPS_FILE_CHECK_FILE
, !RunUser
,
4618 cupsdLogFCMessage
, printer
))
4624 * Setup the last exit status and security profiles...
4628 job
->profile
= cupsdCreateProfile(job
->id
, 0);
4629 job
->bprofile
= cupsdCreateProfile(job
->id
, 1);
4632 * Create the status pipes and buffer...
4635 if (cupsdOpenPipe(job
->status_pipes
))
4637 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
4638 "Unable to create job status pipes - %s.", strerror(errno
));
4640 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
4641 "Job stopped because the scheduler could not create the "
4642 "job status pipes.");
4644 cupsdDestroyProfile(job
->profile
);
4645 job
->profile
= NULL
;
4646 cupsdDestroyProfile(job
->bprofile
);
4647 job
->bprofile
= NULL
;
4651 job
->status_buffer
= cupsdStatBufNew(job
->status_pipes
[0], NULL
);
4652 job
->status_level
= CUPSD_LOG_INFO
;
4655 * Create the backchannel pipes and make them non-blocking...
4658 if (cupsdOpenPipe(job
->back_pipes
))
4660 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
4661 "Unable to create back-channel pipes - %s.", strerror(errno
));
4663 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
4664 "Job stopped because the scheduler could not create the "
4665 "back-channel pipes.");
4667 cupsdClosePipe(job
->status_pipes
);
4668 cupsdStatBufDelete(job
->status_buffer
);
4669 job
->status_buffer
= NULL
;
4671 cupsdDestroyProfile(job
->profile
);
4672 job
->profile
= NULL
;
4673 cupsdDestroyProfile(job
->bprofile
);
4674 job
->bprofile
= NULL
;
4678 fcntl(job
->back_pipes
[0], F_SETFL
,
4679 fcntl(job
->back_pipes
[0], F_GETFL
) | O_NONBLOCK
);
4680 fcntl(job
->back_pipes
[1], F_SETFL
,
4681 fcntl(job
->back_pipes
[1], F_GETFL
) | O_NONBLOCK
);
4684 * Create the side-channel pipes and make them non-blocking...
4687 if (socketpair(AF_LOCAL
, SOCK_STREAM
, 0, job
->side_pipes
))
4689 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
4690 "Unable to create side-channel pipes - %s.", strerror(errno
));
4692 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
4693 "Job stopped because the scheduler could not create the "
4694 "side-channel pipes.");
4696 cupsdClosePipe(job
->back_pipes
);
4698 cupsdClosePipe(job
->status_pipes
);
4699 cupsdStatBufDelete(job
->status_buffer
);
4700 job
->status_buffer
= NULL
;
4702 cupsdDestroyProfile(job
->profile
);
4703 job
->profile
= NULL
;
4704 cupsdDestroyProfile(job
->bprofile
);
4705 job
->bprofile
= NULL
;
4709 fcntl(job
->side_pipes
[0], F_SETFL
,
4710 fcntl(job
->side_pipes
[0], F_GETFL
) | O_NONBLOCK
);
4711 fcntl(job
->side_pipes
[1], F_SETFL
,
4712 fcntl(job
->side_pipes
[1], F_GETFL
) | O_NONBLOCK
);
4714 fcntl(job
->side_pipes
[0], F_SETFD
,
4715 fcntl(job
->side_pipes
[0], F_GETFD
) | FD_CLOEXEC
);
4716 fcntl(job
->side_pipes
[1], F_SETFD
,
4717 fcntl(job
->side_pipes
[1], F_GETFD
) | FD_CLOEXEC
);
4720 * Now start the first file in the job...
4723 cupsdContinueJob(job
);
4728 * 'stop_job()' - Stop a print job.
4732 stop_job(cupsd_job_t
*job
, /* I - Job */
4733 cupsd_jobaction_t action
) /* I - Action */
4735 int i
; /* Looping var */
4738 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "stop_job(job=%p(%d), action=%d)", job
,
4741 FilterLevel
-= job
->cost
;
4744 if (action
== CUPSD_JOB_DEFAULT
&& !job
->kill_time
)
4745 job
->kill_time
= time(NULL
) + JobKillDelay
;
4746 else if (action
>= CUPSD_JOB_FORCE
)
4749 for (i
= 0; job
->filters
[i
]; i
++)
4750 if (job
->filters
[i
] > 0)
4752 cupsdEndProcess(job
->filters
[i
], action
>= CUPSD_JOB_FORCE
);
4754 if (action
>= CUPSD_JOB_FORCE
)
4755 job
->filters
[i
] = -job
->filters
[i
];
4758 if (job
->backend
> 0)
4760 cupsdEndProcess(job
->backend
, action
>= CUPSD_JOB_FORCE
);
4762 if (action
>= CUPSD_JOB_FORCE
)
4763 job
->backend
= -job
->backend
;
4766 if (action
>= CUPSD_JOB_FORCE
)
4769 * Clear job status...
4778 * 'unload_job()' - Unload a job from memory.
4782 unload_job(cupsd_job_t
*job
) /* I - Job */
4787 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Unloading...");
4789 ippDelete(job
->attrs
);
4793 job
->reasons
= NULL
;
4795 job
->job_sheets
= NULL
;
4796 job
->printer_message
= NULL
;
4797 job
->printer_reasons
= NULL
;
4802 * 'update_job()' - Read a status update from a job's filters.
4806 update_job(cupsd_job_t
*job
) /* I - Job to check */
4808 int i
; /* Looping var */
4809 int copies
; /* Number of copies printed */
4810 char message
[CUPSD_SB_BUFFER_SIZE
],
4812 *ptr
; /* Pointer update... */
4813 int loglevel
, /* Log level for message */
4814 event
= 0; /* Events? */
4815 static const char * const levels
[] = /* Log levels */
4831 * Get the printer associated with this job; if the printer is stopped for
4832 * any reason then job->printer will be reset to NULL, so make sure we have
4833 * a valid pointer...
4836 while ((ptr
= cupsdStatBufUpdate(job
->status_buffer
, &loglevel
,
4837 message
, sizeof(message
))) != NULL
)
4840 * Process page and printer state messages as needed...
4843 if (loglevel
== CUPSD_LOG_PAGE
)
4846 * Page message; send the message to the page_log file and update the
4847 * job sheet count...
4850 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "PAGE: %s", message
);
4854 if (!_cups_strncasecmp(message
, "total ", 6))
4857 * Got a total count of pages from a backend or filter...
4860 copies
= atoi(message
+ 6);
4861 copies
-= job
->sheets
->values
[0].integer
; /* Just track the delta */
4863 else if (!sscanf(message
, "%*d%d", &copies
))
4866 job
->sheets
->values
[0].integer
+= copies
;
4868 if (job
->printer
->page_limit
)
4869 cupsdUpdateQuota(job
->printer
, job
->username
, copies
, 0);
4872 cupsdLogPage(job
, message
);
4875 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
4876 "Printed %d page(s).", job
->sheets
->values
[0].integer
);
4878 else if (loglevel
== CUPSD_LOG_JOBSTATE
)
4881 * Support "keyword" to set job-state-reasons to the specified keyword.
4882 * This is sufficient for the current paid printing stuff.
4885 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "JOBSTATE: %s", message
);
4887 if (!strcmp(message
, "cups-retry-as-raster"))
4888 job
->retry_as_raster
= 1;
4890 ippSetString(job
->attrs
, &job
->reasons
, 0, message
);
4892 else if (loglevel
== CUPSD_LOG_STATE
)
4894 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "STATE: %s", message
);
4896 if (!strcmp(message
, "paused"))
4898 cupsdStopPrinter(job
->printer
, 1);
4901 else if (message
[0] && cupsdSetPrinterReasons(job
->printer
, message
))
4903 event
|= CUPSD_EVENT_PRINTER_STATE
;
4908 * Reset cancel time after connecting to the device...
4911 for (i
= 0; i
< job
->printer
->num_reasons
; i
++)
4912 if (!strcmp(job
->printer
->reasons
[i
], "connecting-to-device"))
4915 if (i
>= job
->printer
->num_reasons
)
4917 ipp_attribute_t
*cancel_after
= ippFindAttribute(job
->attrs
,
4920 /* job-cancel-after attribute */
4923 job
->cancel_time
= time(NULL
) + ippGetInteger(cancel_after
, 0);
4925 job
->cancel_time
= time(NULL
) + MaxJobTime
;
4930 update_job_attrs(job
, 0);
4932 else if (loglevel
== CUPSD_LOG_ATTR
)
4935 * Set attribute(s)...
4938 int num_attrs
; /* Number of attributes */
4939 cups_option_t
*attrs
; /* Attributes */
4940 const char *attr
; /* Attribute */
4942 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "ATTR: %s", message
);
4944 num_attrs
= cupsParseOptions(message
, 0, &attrs
);
4946 if ((attr
= cupsGetOption("auth-info-default", num_attrs
,
4949 job
->printer
->num_options
= cupsAddOption("auth-info", attr
,
4950 job
->printer
->num_options
,
4951 &(job
->printer
->options
));
4952 cupsdSetPrinterAttrs(job
->printer
);
4954 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4957 if ((attr
= cupsGetOption("auth-info-required", num_attrs
,
4960 cupsdSetAuthInfoRequired(job
->printer
, attr
, NULL
);
4961 cupsdSetPrinterAttrs(job
->printer
);
4963 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4966 if ((attr
= cupsGetOption("job-media-progress", num_attrs
,
4969 int progress
= atoi(attr
);
4972 if (progress
>= 0 && progress
<= 100)
4974 job
->progress
= progress
;
4977 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
4978 "Printing page %d, %d%%",
4979 job
->sheets
->values
[0].integer
, job
->progress
);
4983 if ((attr
= cupsGetOption("printer-alert", num_attrs
, attrs
)) != NULL
)
4985 cupsdSetString(&job
->printer
->alert
, attr
);
4986 event
|= CUPSD_EVENT_PRINTER_STATE
;
4989 if ((attr
= cupsGetOption("printer-alert-description", num_attrs
,
4992 cupsdSetString(&job
->printer
->alert_description
, attr
);
4993 event
|= CUPSD_EVENT_PRINTER_STATE
;
4996 if ((attr
= cupsGetOption("marker-colors", num_attrs
, attrs
)) != NULL
)
4998 cupsdSetPrinterAttr(job
->printer
, "marker-colors", (char *)attr
);
4999 job
->printer
->marker_time
= time(NULL
);
5000 event
|= CUPSD_EVENT_PRINTER_STATE
;
5001 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
5004 if ((attr
= cupsGetOption("marker-levels", num_attrs
, attrs
)) != NULL
)
5006 cupsdSetPrinterAttr(job
->printer
, "marker-levels", (char *)attr
);
5007 job
->printer
->marker_time
= time(NULL
);
5008 event
|= CUPSD_EVENT_PRINTER_STATE
;
5009 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
5012 if ((attr
= cupsGetOption("marker-low-levels", num_attrs
, attrs
)) != NULL
)
5014 cupsdSetPrinterAttr(job
->printer
, "marker-low-levels", (char *)attr
);
5015 job
->printer
->marker_time
= time(NULL
);
5016 event
|= CUPSD_EVENT_PRINTER_STATE
;
5017 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
5020 if ((attr
= cupsGetOption("marker-high-levels", num_attrs
, attrs
)) != NULL
)
5022 cupsdSetPrinterAttr(job
->printer
, "marker-high-levels", (char *)attr
);
5023 job
->printer
->marker_time
= time(NULL
);
5024 event
|= CUPSD_EVENT_PRINTER_STATE
;
5025 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
5028 if ((attr
= cupsGetOption("marker-message", num_attrs
, attrs
)) != NULL
)
5030 cupsdSetPrinterAttr(job
->printer
, "marker-message", (char *)attr
);
5031 job
->printer
->marker_time
= time(NULL
);
5032 event
|= CUPSD_EVENT_PRINTER_STATE
;
5033 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
5036 if ((attr
= cupsGetOption("marker-names", num_attrs
, attrs
)) != NULL
)
5038 cupsdSetPrinterAttr(job
->printer
, "marker-names", (char *)attr
);
5039 job
->printer
->marker_time
= time(NULL
);
5040 event
|= CUPSD_EVENT_PRINTER_STATE
;
5041 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
5044 if ((attr
= cupsGetOption("marker-types", num_attrs
, attrs
)) != NULL
)
5046 cupsdSetPrinterAttr(job
->printer
, "marker-types", (char *)attr
);
5047 job
->printer
->marker_time
= time(NULL
);
5048 event
|= CUPSD_EVENT_PRINTER_STATE
;
5049 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
5052 cupsFreeOptions(num_attrs
, attrs
);
5054 else if (loglevel
== CUPSD_LOG_PPD
)
5057 * Set attribute(s)...
5060 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "PPD: %s", message
);
5062 job
->num_keywords
= cupsParseOptions(message
, job
->num_keywords
,
5068 * Strip legacy message prefix...
5071 if (!strncmp(message
, "recoverable:", 12))
5074 while (isspace(*ptr
& 255))
5077 else if (!strncmp(message
, "recovered:", 10))
5080 while (isspace(*ptr
& 255))
5087 cupsdLogJob(job
, loglevel
, "%s", ptr
);
5089 if (loglevel
< CUPSD_LOG_DEBUG
&&
5090 strcmp(job
->printer
->state_message
, ptr
))
5092 strlcpy(job
->printer
->state_message
, ptr
,
5093 sizeof(job
->printer
->state_message
));
5095 event
|= CUPSD_EVENT_PRINTER_STATE
| CUPSD_EVENT_JOB_PROGRESS
;
5097 if (loglevel
<= job
->status_level
&& job
->status_level
> CUPSD_LOG_ERROR
)
5100 * Some messages show in the job-printer-state-message attribute...
5103 if (loglevel
!= CUPSD_LOG_NOTICE
)
5104 job
->status_level
= loglevel
;
5106 update_job_attrs(job
, 1);
5108 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
5109 "Set job-printer-state-message to \"%s\", "
5111 job
->printer_message
->values
[0].string
.text
,
5112 levels
[job
->status_level
]);
5117 if (!strchr(job
->status_buffer
->buffer
, '\n'))
5121 if (event
& CUPSD_EVENT_JOB_PROGRESS
)
5122 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
5123 "%s", job
->printer
->state_message
);
5124 if (event
& CUPSD_EVENT_PRINTER_STATE
)
5125 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, job
->printer
, NULL
,
5126 (job
->printer
->type
& CUPS_PRINTER_CLASS
) ?
5127 "Class \"%s\" state changed." :
5128 "Printer \"%s\" state changed.",
5129 job
->printer
->name
);
5132 if (ptr
== NULL
&& !job
->status_buffer
->bufused
)
5135 * See if all of the filters and the backend have returned their
5139 for (i
= 0; job
->filters
[i
] < 0; i
++);
5141 if (job
->filters
[i
])
5144 * EOF but we haven't collected the exit status of all filters...
5147 cupsdCheckProcess();
5151 if (job
->current_file
>= job
->num_files
&& job
->backend
> 0)
5154 * EOF but we haven't collected the exit status of the backend...
5157 cupsdCheckProcess();
5162 * Handle the end of job stuff...
5165 finalize_job(job
, 1);
5168 * Check for new jobs...
5177 * 'update_job_attrs()' - Update the job-printer-* attributes.
5181 update_job_attrs(cupsd_job_t
*job
, /* I - Job to update */
5182 int do_message
)/* I - 1 = copy job-printer-state message */
5184 int i
; /* Looping var */
5185 int num_reasons
; /* Actual number of reasons */
5186 const char * const *reasons
; /* Reasons */
5187 static const char *none
= "none"; /* "none" reason */
5191 * Get/create the job-printer-state-* attributes...
5194 if (!job
->printer_message
)
5196 if ((job
->printer_message
= ippFindAttribute(job
->attrs
,
5197 "job-printer-state-message",
5198 IPP_TAG_TEXT
)) == NULL
)
5199 job
->printer_message
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_TEXT
,
5200 "job-printer-state-message",
5204 if (!job
->printer_reasons
)
5205 job
->printer_reasons
= ippFindAttribute(job
->attrs
,
5206 "job-printer-state-reasons",
5210 * Copy or clear the printer-state-message value as needed...
5213 if (job
->state_value
!= IPP_JOB_PROCESSING
&&
5214 job
->status_level
== CUPSD_LOG_INFO
)
5216 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
), "");
5219 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
5221 else if (job
->printer
->state_message
[0] && do_message
)
5223 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
),
5224 job
->printer
->state_message
);
5227 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
5231 * ... and the printer-state-reasons value...
5234 if (job
->printer
->num_reasons
== 0)
5241 num_reasons
= job
->printer
->num_reasons
;
5242 reasons
= (const char * const *)job
->printer
->reasons
;
5245 if (!job
->printer_reasons
|| job
->printer_reasons
->num_values
!= num_reasons
)
5248 * Replace/create a job-printer-state-reasons attribute...
5251 ippDeleteAttribute(job
->attrs
, job
->printer_reasons
);
5253 job
->printer_reasons
= ippAddStrings(job
->attrs
,
5254 IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
5255 "job-printer-state-reasons",
5256 num_reasons
, NULL
, NULL
);
5261 * Don't bother clearing the reason strings if they are the same...
5264 for (i
= 0; i
< num_reasons
; i
++)
5265 if (strcmp(job
->printer_reasons
->values
[i
].string
.text
, reasons
[i
]))
5268 if (i
>= num_reasons
)
5272 * Not the same, so free the current strings...
5275 for (i
= 0; i
< num_reasons
; i
++)
5276 _cupsStrFree(job
->printer_reasons
->values
[i
].string
.text
);
5280 * Copy the reasons...
5283 for (i
= 0; i
< num_reasons
; i
++)
5284 job
->printer_reasons
->values
[i
].string
.text
= _cupsStrAlloc(reasons
[i
]);
5287 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);