2 * Job management routines for the CUPS scheduler.
4 * Copyright 2007-2016 by Apple Inc.
5 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * missing or damaged, see the license at "http://www.cups.org/".
15 * Include necessary headers...
20 #include <cups/backend.h>
23 # include <IOKit/pwr_mgt/IOPMLib.h>
24 # ifdef HAVE_IOKIT_PWR_MGT_IOPMLIBPRIVATE_H
25 # include <IOKit/pwr_mgt/IOPMLibPrivate.h>
26 # endif /* HAVE_IOKIT_PWR_MGT_IOPMLIBPRIVATE_H */
27 #endif /* __APPLE__ */
31 * Design Notes for Job Management
32 * -------------------------------
36 * pending Do nothing/check jobs
37 * pending-held Send SIGTERM to filters and backend
38 * processing Do nothing/start job
39 * stopped Send SIGKILL to filters and backend
40 * canceled Send SIGTERM to filters and backend
44 * Finalize clears the printer <-> job association, deletes the status
45 * buffer, closes all of the pipes, etc. and doesn't get run until all of
46 * the print processes are finished.
48 * UNLOADING OF JOBS (cupsdUnloadCompletedJobs)
50 * We unload the job attributes when they are not needed to reduce overall
51 * memory consumption. We don't unload jobs where job->state_value <
52 * IPP_JOB_STOPPED, job->printer != NULL, or job->access_time is recent.
54 * STARTING OF JOBS (start_job)
56 * When a job is started, a status buffer, several pipes, a security
57 * profile, and a backend process are created for the life of that job.
58 * These are shared for every file in a job. For remote print jobs, the
59 * IPP backend is provided with every file in the job and no filters are
62 * The job->printer member tracks which printer is printing a job, which
63 * can be different than the destination in job->dest for classes. The
64 * printer object also has a job pointer to track which job is being
67 * PRINTING OF JOB FILES (cupsdContinueJob)
69 * Each file in a job is filtered by 0 or more programs. After getting the
70 * list of filters needed and the total cost, the job is either passed or
71 * put back to the processing state until the current FilterLevel comes down
72 * enough to allow printing.
74 * If we can print, we build a string for the print options and run each of
75 * the filters, piping the output from one into the next.
77 * JOB STATUS UPDATES (update_job)
79 * The update_job function gets called whenever there are pending messages
80 * on the status pipe. These generally are updates to the marker-*,
81 * printer-state-message, or printer-state-reasons attributes. On EOF,
82 * finalize_job is called to clean up.
84 * FINALIZING JOBS (finalize_job)
86 * When all filters and the backend are done, we set the job state to
87 * completed (no errors), aborted (filter errors or abort-job policy),
88 * pending-held (auth required or retry-job policy), or pending
89 * (retry-current-job or stop-printer policies) as appropriate.
91 * Then we close the pipes and free the status buffers and profiles.
93 * JOB FILE COMPLETION (process_children in main.c)
95 * For multiple-file jobs, process_children (in main.c) sees that all
96 * filters have exited and calls in to print the next file if there are
97 * more files in the job, otherwise it waits for the backend to exit and
98 * update_job to do the cleanup.
106 static mime_filter_t gziptoany_filter
=
108 NULL
, /* Source type */
109 NULL
, /* Destination type */
111 "gziptoany" /* Filter program to run */
119 static int compare_active_jobs(void *first
, void *second
, void *data
);
120 static int compare_completed_jobs(void *first
, void *second
, void *data
);
121 static int compare_jobs(void *first
, void *second
, void *data
);
122 static void dump_job_history(cupsd_job_t
*job
);
123 static void finalize_job(cupsd_job_t
*job
, int set_job_state
);
124 static void free_job_history(cupsd_job_t
*job
);
125 static char *get_options(cupsd_job_t
*job
, int banner_page
, char *copies
,
126 size_t copies_size
, char *title
,
128 static size_t ipp_length(ipp_t
*ipp
);
129 static void load_job_cache(const char *filename
);
130 static void load_next_job_id(const char *filename
);
131 static void load_request_root(void);
132 static void remove_job_files(cupsd_job_t
*job
);
133 static void remove_job_history(cupsd_job_t
*job
);
134 static void set_time(cupsd_job_t
*job
, const char *name
);
135 static void start_job(cupsd_job_t
*job
, cupsd_printer_t
*printer
);
136 static void stop_job(cupsd_job_t
*job
, cupsd_jobaction_t action
);
137 static void unload_job(cupsd_job_t
*job
);
138 static void update_job(cupsd_job_t
*job
);
139 static void update_job_attrs(cupsd_job_t
*job
, int do_message
);
143 * 'cupsdAddJob()' - Add a new job to the job queue.
146 cupsd_job_t
* /* O - New job record */
147 cupsdAddJob(int priority
, /* I - Job priority */
148 const char *dest
) /* I - Job destination */
150 cupsd_job_t
*job
; /* New job record */
153 if ((job
= calloc(sizeof(cupsd_job_t
), 1)) == NULL
)
156 job
->id
= NextJobId
++;
157 job
->priority
= priority
;
158 job
->back_pipes
[0] = -1;
159 job
->back_pipes
[1] = -1;
160 job
->print_pipes
[0] = -1;
161 job
->print_pipes
[1] = -1;
162 job
->side_pipes
[0] = -1;
163 job
->side_pipes
[1] = -1;
164 job
->status_pipes
[0] = -1;
165 job
->status_pipes
[1] = -1;
167 cupsdSetString(&job
->dest
, dest
);
170 * Add the new job to the "all jobs" and "active jobs" lists...
173 cupsArrayAdd(Jobs
, job
);
174 cupsArrayAdd(ActiveJobs
, job
);
181 * 'cupsdCancelJobs()' - Cancel all jobs for the given destination/user.
185 cupsdCancelJobs(const char *dest
, /* I - Destination to cancel */
186 const char *username
, /* I - Username or NULL */
187 int purge
) /* I - Purge jobs? */
189 cupsd_job_t
*job
; /* Current job */
192 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
194 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
196 if ((!job
->dest
|| !job
->username
) && !cupsdLoadJob(job
))
199 if ((!dest
|| !strcmp(job
->dest
, dest
)) &&
200 (!username
|| !strcmp(job
->username
, username
)))
203 * Cancel all jobs matching this destination/user...
207 cupsdSetJobState(job
, IPP_JOB_CANCELED
, CUPSD_JOB_PURGE
,
208 "Job purged by user.");
209 else if (job
->state_value
< IPP_JOB_CANCELED
)
210 cupsdSetJobState(job
, IPP_JOB_CANCELED
, CUPSD_JOB_DEFAULT
,
211 "Job canceled by user.");
218 * 'cupsdCheckJobs()' - Check the pending jobs and start any if the destination
225 cupsd_job_t
*job
; /* Current job in queue */
226 cupsd_printer_t
*printer
, /* Printer destination */
227 *pclass
; /* Printer class destination */
228 ipp_attribute_t
*attr
; /* Job attribute */
229 time_t curtime
; /* Current time */
230 const char *reasons
; /* job-state-reasons value */
233 curtime
= time(NULL
);
235 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdCheckJobs: %d active jobs, sleeping=%d, ac-power=%d, reload=%d, curtime=%ld", cupsArrayCount(ActiveJobs
), Sleeping
, ACPower
, NeedReload
, (long)curtime
);
237 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
239 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
241 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
242 "cupsdCheckJobs: Job %d - dest=\"%s\", printer=%p, "
243 "state=%d, cancel_time=%ld, hold_until=%ld, kill_time=%ld, "
244 "pending_cost=%d, pending_timeout=%ld", job
->id
, job
->dest
,
245 job
->printer
, job
->state_value
, (long)job
->cancel_time
,
246 (long)job
->hold_until
, (long)job
->kill_time
,
247 job
->pending_cost
, (long)job
->pending_timeout
);
250 * Kill jobs if they are unresponsive...
253 if (job
->kill_time
&& job
->kill_time
<= curtime
)
256 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Stopping unresponsive job.");
258 stop_job(job
, CUPSD_JOB_FORCE
);
263 * Cancel stuck jobs...
266 if (job
->cancel_time
&& job
->cancel_time
<= curtime
)
268 int cancel_after
; /* job-cancel-after value */
270 attr
= ippFindAttribute(job
->attrs
, "job-cancel-after", IPP_TAG_INTEGER
);
271 cancel_after
= attr
? ippGetInteger(attr
, 0) : MaxJobTime
;
274 cupsdSetJobState(job
, IPP_JOB_CANCELED
, CUPSD_JOB_FORCE
, "Marking stuck job as completed after %d seconds.", cancel_after
);
276 cupsdSetJobState(job
, IPP_JOB_CANCELED
, CUPSD_JOB_DEFAULT
, "Canceling stuck job after %d seconds.", cancel_after
);
281 * Start held jobs if they are ready...
284 if (job
->state_value
== IPP_JOB_HELD
&&
286 job
->hold_until
< curtime
)
288 if (job
->pending_timeout
)
291 * This job is pending; check that we don't have an active Send-Document
292 * operation in progress on any of the client connections, then timeout
293 * the job so we can start printing...
296 cupsd_client_t
*con
; /* Current client connection */
298 for (con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
300 con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
302 con
->request
->request
.op
.operation_id
== IPP_SEND_DOCUMENT
)
308 if (cupsdTimeoutJob(job
))
312 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
313 "Job submission timed out.");
317 * Continue jobs that are waiting on the FilterLimit...
320 if (job
->pending_cost
> 0 &&
321 ((FilterLevel
+ job
->pending_cost
) < FilterLimit
|| FilterLevel
== 0))
322 cupsdContinueJob(job
);
325 * Skip jobs that where held-on-create
328 reasons
= ippGetString(job
->reasons
, 0, NULL
);
329 if (reasons
&& !strcmp(reasons
, "job-held-on-create"))
332 * Check whether the printer is still holding new jobs...
335 printer
= cupsdFindDest(job
->dest
);
337 if (printer
->holding_new_jobs
)
340 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
344 * Start pending jobs if the destination is available...
347 if (job
->state_value
== IPP_JOB_PENDING
&& !NeedReload
&&
348 (!Sleeping
|| ACPower
) && !DoingShutdown
&& !job
->printer
)
350 printer
= cupsdFindDest(job
->dest
);
353 while (printer
&& (printer
->type
& CUPS_PRINTER_CLASS
))
356 * If the class is remote, just pass it to the remote server...
361 if (pclass
->state
== IPP_PRINTER_STOPPED
)
363 else if (pclass
->type
& CUPS_PRINTER_REMOTE
)
366 printer
= cupsdFindAvailablePrinter(printer
->name
);
369 if (!printer
&& !pclass
)
372 * Whoa, the printer and/or class for this destination went away;
376 cupsdSetJobState(job
, IPP_JOB_ABORTED
, CUPSD_JOB_PURGE
,
377 "Job aborted because the destination printer/class "
383 * See if the printer is available or remote and not printing a job;
384 * if so, start the job...
390 * Add/update a job-printer-uri-actual attribute for this job
391 * so that we know which printer actually printed the job...
394 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri-actual", IPP_TAG_URI
)) != NULL
)
395 ippSetString(job
->attrs
, &attr
, 0, printer
->uri
);
397 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-printer-uri-actual", NULL
, printer
->uri
);
400 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
403 if (!printer
->job
&& printer
->state
== IPP_PRINTER_IDLE
)
409 cupsArraySave(ActiveJobs
);
410 start_job(job
, printer
);
411 cupsArrayRestore(ActiveJobs
);
420 * 'cupsdCleanJobs()' - Clean out old jobs.
426 cupsd_job_t
*job
; /* Current job */
427 time_t curtime
; /* Current time */
430 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
431 "cupsdCleanJobs: MaxJobs=%d, JobHistory=%d, JobFiles=%d",
432 MaxJobs
, JobHistory
, JobFiles
);
434 if (MaxJobs
<= 0 && JobHistory
== INT_MAX
&& JobFiles
== INT_MAX
)
437 curtime
= time(NULL
);
438 JobHistoryUpdate
= 0;
440 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
442 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
444 if (job
->state_value
>= IPP_JOB_CANCELED
&& !job
->printer
)
447 * Expire old jobs (or job files)...
450 if ((MaxJobs
> 0 && cupsArrayCount(Jobs
) >= MaxJobs
) ||
451 (job
->history_time
&& job
->history_time
<= curtime
))
453 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Removing from history.");
454 cupsdDeleteJob(job
, CUPSD_JOB_PURGE
);
456 else if (job
->file_time
&& job
->file_time
<= curtime
)
458 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Removing document files.");
459 remove_job_files(job
);
461 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
463 if (job
->history_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
464 JobHistoryUpdate
= job
->history_time
;
468 if (job
->history_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
469 JobHistoryUpdate
= job
->history_time
;
471 if (job
->file_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
472 JobHistoryUpdate
= job
->file_time
;
477 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdCleanJobs: JobHistoryUpdate=%ld",
478 (long)JobHistoryUpdate
);
483 * 'cupsdContinueJob()' - Continue printing with the next file in a job.
487 cupsdContinueJob(cupsd_job_t
*job
) /* I - Job */
489 int i
; /* Looping var */
490 int slot
; /* Pipe slot */
491 cups_array_t
*filters
= NULL
,/* Filters for job */
492 *prefilters
; /* Filters with prefilters */
493 mime_filter_t
*filter
, /* Current filter */
494 *prefilter
, /* Prefilter */
495 port_monitor
; /* Port monitor filter */
496 char scheme
[255]; /* Device URI scheme */
497 ipp_attribute_t
*attr
; /* Current attribute */
498 const char *ptr
, /* Pointer into value */
499 *abort_message
; /* Abort message */
500 ipp_jstate_t abort_state
= IPP_JOB_STOPPED
;
501 /* New job state on abort */
502 struct stat backinfo
; /* Backend file information */
503 int backroot
; /* Run backend as root? */
504 int pid
; /* Process ID of new filter process */
505 int banner_page
; /* 1 if banner page, 0 otherwise */
506 int filterfds
[2][2] = { { -1, -1 }, { -1, -1 } };
507 /* Pipes used between filters */
508 int envc
; /* Number of environment variables */
509 struct stat fileinfo
; /* Job file information */
510 int argc
= 0; /* Number of arguments */
511 char **argv
= NULL
, /* Filter command-line arguments */
512 filename
[1024], /* Job filename */
513 command
[1024], /* Full path to command */
514 jobid
[255], /* Job ID string */
516 /* Job title string */
517 copies
[255], /* # copies string */
518 *options
, /* Options string */
520 /* Environment variables */
521 charset
[255], /* CHARSET env variable */
522 class_name
[255],/* CLASS env variable */
523 classification
[1024],
524 /* CLASSIFICATION env variable */
526 /* CONTENT_TYPE env variable */
528 /* DEVICE_URI env variable */
529 final_content_type
[1024] = "",
530 /* FINAL_CONTENT_TYPE env variable */
531 lang
[255], /* LANG env variable */
534 /* APPLE_LANGUAGE env variable */
535 #endif /* __APPLE__ */
536 auth_info_required
[255],
537 /* AUTH_INFO_REQUIRED env variable */
538 ppd
[1024], /* PPD env variable */
540 /* PRINTER_INFO env variable */
541 printer_location
[255],
542 /* PRINTER_LOCATION env variable */
544 /* PRINTER env variable */
545 *printer_state_reasons
= NULL
,
546 /* PRINTER_STATE_REASONS env var */
548 /* RIP_MAX_CACHE env variable */
551 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
552 "cupsdContinueJob(job=%p(%d)): current_file=%d, num_files=%d",
553 job
, job
->id
, job
->current_file
, job
->num_files
);
556 * Figure out what filters are required to convert from
557 * the source to the destination type...
560 FilterLevel
-= job
->cost
;
563 job
->pending_cost
= 0;
565 memset(job
->filters
, 0, sizeof(job
->filters
));
567 if (job
->printer
->raw
)
570 * Remote jobs and raw queues go directly to the printer without
574 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Sending job to queue tagged as raw...");
579 * Local jobs get filtered...
582 mime_type_t
*dst
= job
->printer
->filetype
;
583 /* Destination file type */
585 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
586 job
->id
, job
->current_file
+ 1);
587 if (stat(filename
, &fileinfo
))
588 fileinfo
.st_size
= 0;
590 if (job
->retry_as_raster
)
593 * Need to figure out whether the printer supports image/pwg-raster or
594 * image/urf, and use the corresponding type...
597 char type
[MIME_MAX_TYPE
]; /* MIME media type for printer */
599 snprintf(type
, sizeof(type
), "%s/image/urf", job
->printer
->name
);
600 if ((dst
= mimeType(MimeDatabase
, "printer", type
)) == NULL
)
602 snprintf(type
, sizeof(type
), "%s/image/pwg-raster", job
->printer
->name
);
603 dst
= mimeType(MimeDatabase
, "printer", type
);
607 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Retrying job as \"%s\".", strchr(dst
->type
, '/') + 1);
609 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Unable to retry job using a supported raster format.");
612 filters
= mimeFilter2(MimeDatabase
, job
->filetypes
[job
->current_file
], (size_t)fileinfo
.st_size
, dst
, &(job
->cost
));
616 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
617 "Unable to convert file %d to printable format.",
620 abort_message
= "Aborting job because it cannot be printed.";
621 abort_state
= IPP_JOB_ABORTED
;
623 ippSetString(job
->attrs
, &job
->reasons
, 0, "document-unprintable-error");
628 * Figure out the final content type...
631 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "%d filters for job:",
632 cupsArrayCount(filters
));
633 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
635 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
636 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "%s (%s/%s to %s/%s, cost %d)",
638 filter
->src
? filter
->src
->super
: "???",
639 filter
->src
? filter
->src
->type
: "???",
640 filter
->dst
? filter
->dst
->super
: "???",
641 filter
->dst
? filter
->dst
->type
: "???",
644 if (!job
->printer
->remote
)
646 for (filter
= (mime_filter_t
*)cupsArrayLast(filters
);
647 filter
&& filter
->dst
;
648 filter
= (mime_filter_t
*)cupsArrayPrev(filters
))
649 if (strcmp(filter
->dst
->super
, "printer") ||
650 strcmp(filter
->dst
->type
, job
->printer
->name
))
653 if (filter
&& filter
->dst
)
655 if ((ptr
= strchr(filter
->dst
->type
, '/')) != NULL
)
656 snprintf(final_content_type
, sizeof(final_content_type
),
657 "FINAL_CONTENT_TYPE=%s", ptr
+ 1);
659 snprintf(final_content_type
, sizeof(final_content_type
),
660 "FINAL_CONTENT_TYPE=%s/%s", filter
->dst
->super
,
664 snprintf(final_content_type
, sizeof(final_content_type
),
665 "FINAL_CONTENT_TYPE=printer/%s", job
->printer
->name
);
669 * Remove NULL ("-") filters...
672 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
674 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
675 if (!strcmp(filter
->filter
, "-"))
676 cupsArrayRemove(filters
, filter
);
678 if (cupsArrayCount(filters
) == 0)
680 cupsArrayDelete(filters
);
685 * If this printer has any pre-filters, insert the required pre-filter
686 * in the filters array...
689 if (job
->printer
->prefiltertype
&& filters
)
691 prefilters
= cupsArrayNew(NULL
, NULL
);
693 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
695 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
697 if ((prefilter
= mimeFilterLookup(MimeDatabase
, filter
->src
,
698 job
->printer
->prefiltertype
)))
700 cupsArrayAdd(prefilters
, prefilter
);
701 job
->cost
+= prefilter
->cost
;
704 cupsArrayAdd(prefilters
, filter
);
707 cupsArrayDelete(filters
);
708 filters
= prefilters
;
713 * Set a minimum cost of 100 for all jobs so that FilterLimit
714 * works with raw queues and other low-cost paths.
721 * See if the filter cost is too high...
724 if ((FilterLevel
+ job
->cost
) > FilterLimit
&& FilterLevel
> 0 &&
728 * Don't print this job quite yet...
731 cupsArrayDelete(filters
);
733 cupsdLogJob(job
, CUPSD_LOG_INFO
,
734 "Holding because filter limit has been reached.");
735 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
736 "cupsdContinueJob: file=%d, cost=%d, level=%d, limit=%d",
737 job
->current_file
, job
->cost
, FilterLevel
,
740 job
->pending_cost
= job
->cost
;
745 FilterLevel
+= job
->cost
;
748 * Add decompression/raw filter as needed...
751 if ((job
->compressions
[job
->current_file
] && (!job
->printer
->remote
|| job
->num_files
== 1)) ||
752 (!job
->printer
->remote
&& job
->printer
->raw
&& job
->num_files
> 1))
755 * Add gziptoany filter to the front of the list...
759 filters
= cupsArrayNew(NULL
, NULL
);
761 if (!cupsArrayInsert(filters
, &gziptoany_filter
))
763 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
764 "Unable to add decompression filter - %s", strerror(errno
));
766 cupsArrayDelete(filters
);
768 abort_message
= "Stopping job because the scheduler ran out of memory.";
775 * Add port monitor, if any...
778 if (job
->printer
->port_monitor
)
781 * Add port monitor to the end of the list...
785 filters
= cupsArrayNew(NULL
, NULL
);
787 port_monitor
.src
= NULL
;
788 port_monitor
.dst
= NULL
;
789 port_monitor
.cost
= 0;
791 snprintf(port_monitor
.filter
, sizeof(port_monitor
.filter
),
792 "%s/monitor/%s", ServerBin
, job
->printer
->port_monitor
);
794 if (!cupsArrayAdd(filters
, &port_monitor
))
796 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
797 "Unable to add port monitor - %s", strerror(errno
));
799 abort_message
= "Stopping job because the scheduler ran out of memory.";
806 * Make sure we don't go over the "MAX_FILTERS" limit...
809 if (cupsArrayCount(filters
) > MAX_FILTERS
)
811 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
812 "Too many filters (%d > %d), unable to print.",
813 cupsArrayCount(filters
), MAX_FILTERS
);
815 abort_message
= "Aborting job because it needs too many filters to print.";
816 abort_state
= IPP_JOB_ABORTED
;
818 ippSetString(job
->attrs
, &job
->reasons
, 0, "document-unprintable-error");
824 * Determine if we are printing a banner page or not...
827 if (job
->job_sheets
== NULL
)
829 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "No job-sheets attribute.");
830 if ((job
->job_sheets
=
831 ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_ZERO
)) != NULL
)
832 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
833 "... but someone added one without setting job_sheets.");
835 else if (job
->job_sheets
->num_values
== 1)
836 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "job-sheets=%s",
837 job
->job_sheets
->values
[0].string
.text
);
839 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "job-sheets=%s,%s",
840 job
->job_sheets
->values
[0].string
.text
,
841 job
->job_sheets
->values
[1].string
.text
);
843 if (job
->printer
->type
& CUPS_PRINTER_REMOTE
)
845 else if (job
->job_sheets
== NULL
)
847 else if (_cups_strcasecmp(job
->job_sheets
->values
[0].string
.text
, "none") != 0 &&
848 job
->current_file
== 0)
850 else if (job
->job_sheets
->num_values
> 1 &&
851 _cups_strcasecmp(job
->job_sheets
->values
[1].string
.text
, "none") != 0 &&
852 job
->current_file
== (job
->num_files
- 1))
857 if ((options
= get_options(job
, banner_page
, copies
, sizeof(copies
), title
,
858 sizeof(title
))) == NULL
)
860 abort_message
= "Stopping job because the scheduler ran out of memory.";
866 * Build the command-line arguments for the filters. Each filter
867 * has 6 or 7 arguments:
875 * argv[6] = filename (optional; normally stdin)
877 * This allows legacy printer drivers that use the old System V
878 * printing interface to be used by CUPS.
880 * For remote jobs, we send all of the files in the argument list.
883 if (job
->printer
->remote
)
884 argc
= 6 + job
->num_files
;
888 if ((argv
= calloc((size_t)argc
+ 1, sizeof(char *))) == NULL
)
890 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Unable to allocate argument array - %s",
893 abort_message
= "Stopping job because the scheduler ran out of memory.";
898 sprintf(jobid
, "%d", job
->id
);
900 argv
[0] = job
->printer
->name
;
902 argv
[2] = job
->username
;
907 if (job
->printer
->remote
&& job
->num_files
> 1)
909 for (i
= 0; i
< job
->num_files
; i
++)
911 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
913 argv
[6 + i
] = strdup(filename
);
918 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
919 job
->id
, job
->current_file
+ 1);
920 argv
[6] = strdup(filename
);
923 for (i
= 0; argv
[i
]; i
++)
924 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "argv[%d]=\"%s\"", i
, argv
[i
]);
927 * Create environment variable strings for the filters...
930 attr
= ippFindAttribute(job
->attrs
, "attributes-natural-language",
934 strlcpy(apple_language
, "APPLE_LANGUAGE=", sizeof(apple_language
));
935 _cupsAppleLanguage(attr
->values
[0].string
.text
,
936 apple_language
+ 15, sizeof(apple_language
) - 15);
937 #endif /* __APPLE__ */
939 switch (strlen(attr
->values
[0].string
.text
))
943 * This is an unknown or badly formatted language code; use
944 * the POSIX locale...
947 strlcpy(lang
, "LANG=C", sizeof(lang
));
952 * Just the language code (ll)...
955 snprintf(lang
, sizeof(lang
), "LANG=%s.UTF-8",
956 attr
->values
[0].string
.text
);
961 * Language and country code (ll-cc)...
964 snprintf(lang
, sizeof(lang
), "LANG=%c%c_%c%c.UTF-8",
965 attr
->values
[0].string
.text
[0],
966 attr
->values
[0].string
.text
[1],
967 toupper(attr
->values
[0].string
.text
[3] & 255),
968 toupper(attr
->values
[0].string
.text
[4] & 255));
972 if ((attr
= ippFindAttribute(job
->attrs
, "document-format",
973 IPP_TAG_MIMETYPE
)) != NULL
&&
974 (ptr
= strstr(attr
->values
[0].string
.text
, "charset=")) != NULL
)
975 snprintf(charset
, sizeof(charset
), "CHARSET=%s", ptr
+ 8);
977 strlcpy(charset
, "CHARSET=utf-8", sizeof(charset
));
979 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s/%s",
980 job
->filetypes
[job
->current_file
]->super
,
981 job
->filetypes
[job
->current_file
]->type
);
982 snprintf(device_uri
, sizeof(device_uri
), "DEVICE_URI=%s",
983 job
->printer
->device_uri
);
984 snprintf(ppd
, sizeof(ppd
), "PPD=%s/ppd/%s.ppd", ServerRoot
,
986 snprintf(printer_info
, sizeof(printer_name
), "PRINTER_INFO=%s",
987 job
->printer
->info
? job
->printer
->info
: "");
988 snprintf(printer_location
, sizeof(printer_name
), "PRINTER_LOCATION=%s",
989 job
->printer
->location
? job
->printer
->location
: "");
990 snprintf(printer_name
, sizeof(printer_name
), "PRINTER=%s", job
->printer
->name
);
991 if (job
->printer
->num_reasons
> 0)
993 char *psrptr
; /* Pointer into PRINTER_STATE_REASONS */
994 size_t psrlen
; /* Size of PRINTER_STATE_REASONS */
996 for (psrlen
= 22, i
= 0; i
< job
->printer
->num_reasons
; i
++)
997 psrlen
+= strlen(job
->printer
->reasons
[i
]) + 1;
999 if ((printer_state_reasons
= malloc(psrlen
)) != NULL
)
1002 * All of these strcpy's are safe because we allocated the psr string...
1005 strlcpy(printer_state_reasons
, "PRINTER_STATE_REASONS=", psrlen
);
1006 for (psrptr
= printer_state_reasons
+ 22, i
= 0;
1007 i
< job
->printer
->num_reasons
;
1012 strlcpy(psrptr
, job
->printer
->reasons
[i
], psrlen
- (size_t)(psrptr
- printer_state_reasons
));
1013 psrptr
+= strlen(psrptr
);
1017 snprintf(rip_max_cache
, sizeof(rip_max_cache
), "RIP_MAX_CACHE=%s", RIPCache
);
1019 if (job
->printer
->num_auth_info_required
== 1)
1020 snprintf(auth_info_required
, sizeof(auth_info_required
),
1021 "AUTH_INFO_REQUIRED=%s",
1022 job
->printer
->auth_info_required
[0]);
1023 else if (job
->printer
->num_auth_info_required
== 2)
1024 snprintf(auth_info_required
, sizeof(auth_info_required
),
1025 "AUTH_INFO_REQUIRED=%s,%s",
1026 job
->printer
->auth_info_required
[0],
1027 job
->printer
->auth_info_required
[1]);
1028 else if (job
->printer
->num_auth_info_required
== 3)
1029 snprintf(auth_info_required
, sizeof(auth_info_required
),
1030 "AUTH_INFO_REQUIRED=%s,%s,%s",
1031 job
->printer
->auth_info_required
[0],
1032 job
->printer
->auth_info_required
[1],
1033 job
->printer
->auth_info_required
[2]);
1034 else if (job
->printer
->num_auth_info_required
== 4)
1035 snprintf(auth_info_required
, sizeof(auth_info_required
),
1036 "AUTH_INFO_REQUIRED=%s,%s,%s,%s",
1037 job
->printer
->auth_info_required
[0],
1038 job
->printer
->auth_info_required
[1],
1039 job
->printer
->auth_info_required
[2],
1040 job
->printer
->auth_info_required
[3]);
1042 strlcpy(auth_info_required
, "AUTH_INFO_REQUIRED=none",
1043 sizeof(auth_info_required
));
1045 envc
= cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
1047 envp
[envc
++] = charset
;
1048 envp
[envc
++] = lang
;
1050 envp
[envc
++] = apple_language
;
1051 #endif /* __APPLE__ */
1052 envp
[envc
++] = ppd
;
1053 envp
[envc
++] = rip_max_cache
;
1054 envp
[envc
++] = content_type
;
1055 envp
[envc
++] = device_uri
;
1056 envp
[envc
++] = printer_info
;
1057 envp
[envc
++] = printer_location
;
1058 envp
[envc
++] = printer_name
;
1059 envp
[envc
++] = printer_state_reasons
? printer_state_reasons
:
1060 "PRINTER_STATE_REASONS=none";
1061 envp
[envc
++] = banner_page
? "CUPS_FILETYPE=job-sheet" :
1062 "CUPS_FILETYPE=document";
1064 if (final_content_type
[0])
1065 envp
[envc
++] = final_content_type
;
1067 if (Classification
&& !banner_page
)
1069 if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
1070 IPP_TAG_NAME
)) == NULL
)
1071 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
1073 else if (attr
->num_values
> 1 &&
1074 strcmp(attr
->values
[1].string
.text
, "none") != 0)
1075 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
1076 attr
->values
[1].string
.text
);
1078 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
1079 attr
->values
[0].string
.text
);
1081 envp
[envc
++] = classification
;
1084 if (job
->dtype
& CUPS_PRINTER_CLASS
)
1086 snprintf(class_name
, sizeof(class_name
), "CLASS=%s", job
->dest
);
1087 envp
[envc
++] = class_name
;
1090 envp
[envc
++] = auth_info_required
;
1093 i
< (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0]));
1095 if (job
->auth_env
[i
])
1096 envp
[envc
++] = job
->auth_env
[i
];
1101 envp
[envc
++] = job
->auth_uid
;
1105 for (i
= 0; i
< envc
; i
++)
1106 if (!strncmp(envp
[i
], "AUTH_", 5))
1107 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"AUTH_%c****\"", i
,
1109 else if (strncmp(envp
[i
], "DEVICE_URI=", 11))
1110 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"%s\"", i
, envp
[i
]);
1112 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"DEVICE_URI=%s\"", i
,
1113 job
->printer
->sanitized_device_uri
);
1115 if (job
->printer
->remote
)
1116 job
->current_file
= job
->num_files
;
1118 job
->current_file
++;
1121 * Now create processes for all of the filters...
1124 for (i
= 0, slot
= 0, filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
1126 i
++, filter
= (mime_filter_t
*)cupsArrayNext(filters
))
1128 if (filter
->filter
[0] != '/')
1129 snprintf(command
, sizeof(command
), "%s/filter/%s", ServerBin
,
1132 strlcpy(command
, filter
->filter
, sizeof(command
));
1134 if (i
< (cupsArrayCount(filters
) - 1))
1136 if (cupsdOpenPipe(filterfds
[slot
]))
1138 abort_message
= "Stopping job because the scheduler could not create "
1139 "the filter pipes.";
1146 if (job
->current_file
== 1 ||
1147 (job
->printer
->pc
&& job
->printer
->pc
->single_file
))
1149 if (strncmp(job
->printer
->device_uri
, "file:", 5) != 0)
1151 if (cupsdOpenPipe(job
->print_pipes
))
1153 abort_message
= "Stopping job because the scheduler could not "
1154 "create the backend pipes.";
1161 job
->print_pipes
[0] = -1;
1162 if (!strcmp(job
->printer
->device_uri
, "file:/dev/null") ||
1163 !strcmp(job
->printer
->device_uri
, "file:///dev/null"))
1164 job
->print_pipes
[1] = -1;
1167 if (!strncmp(job
->printer
->device_uri
, "file:/dev/", 10))
1168 job
->print_pipes
[1] = open(job
->printer
->device_uri
+ 5,
1170 else if (!strncmp(job
->printer
->device_uri
, "file:///dev/", 12))
1171 job
->print_pipes
[1] = open(job
->printer
->device_uri
+ 7,
1173 else if (!strncmp(job
->printer
->device_uri
, "file:///", 8))
1174 job
->print_pipes
[1] = open(job
->printer
->device_uri
+ 7,
1175 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
1177 job
->print_pipes
[1] = open(job
->printer
->device_uri
+ 5,
1178 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
1180 if (job
->print_pipes
[1] < 0)
1182 abort_message
= "Stopping job because the scheduler could not "
1183 "open the output file.";
1188 fcntl(job
->print_pipes
[1], F_SETFD
,
1189 fcntl(job
->print_pipes
[1], F_GETFD
) | FD_CLOEXEC
);
1194 filterfds
[slot
][0] = job
->print_pipes
[0];
1195 filterfds
[slot
][1] = job
->print_pipes
[1];
1198 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
1199 filterfds
[slot
][1], job
->status_pipes
[1],
1200 job
->back_pipes
[0], job
->side_pipes
[0], 0,
1201 job
->profile
, job
, job
->filters
+ i
);
1203 cupsdClosePipe(filterfds
[!slot
]);
1207 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Unable to start filter \"%s\" - %s.",
1208 filter
->filter
, strerror(errno
));
1210 abort_message
= "Stopping job because the scheduler could not execute a "
1216 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Started filter %s (PID %d)", command
,
1228 cupsArrayDelete(filters
);
1232 * Finally, pipe the final output into a backend process if needed...
1235 if (strncmp(job
->printer
->device_uri
, "file:", 5) != 0)
1237 if (job
->current_file
== 1 || job
->printer
->remote
||
1238 (job
->printer
->pc
&& job
->printer
->pc
->single_file
))
1240 sscanf(job
->printer
->device_uri
, "%254[^:]", scheme
);
1241 snprintf(command
, sizeof(command
), "%s/backend/%s", ServerBin
, scheme
);
1244 * See if the backend needs to run as root...
1249 else if (stat(command
, &backinfo
))
1252 backroot
= !(backinfo
.st_mode
& (S_IWGRP
| S_IRWXO
));
1254 argv
[0] = job
->printer
->sanitized_device_uri
;
1256 filterfds
[slot
][0] = -1;
1257 filterfds
[slot
][1] = -1;
1259 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
1260 filterfds
[slot
][1], job
->status_pipes
[1],
1261 job
->back_pipes
[1], job
->side_pipes
[1],
1262 backroot
, job
->bprofile
, job
, &(job
->backend
));
1266 abort_message
= "Stopping job because the sheduler could not execute "
1273 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Started backend %s (PID %d)",
1278 if (job
->current_file
== job
->num_files
||
1279 (job
->printer
->pc
&& job
->printer
->pc
->single_file
))
1280 cupsdClosePipe(job
->print_pipes
);
1282 if (job
->current_file
== job
->num_files
)
1284 cupsdClosePipe(job
->back_pipes
);
1285 cupsdClosePipe(job
->side_pipes
);
1287 close(job
->status_pipes
[1]);
1288 job
->status_pipes
[1] = -1;
1293 filterfds
[slot
][0] = -1;
1294 filterfds
[slot
][1] = -1;
1296 if (job
->current_file
== job
->num_files
||
1297 (job
->printer
->pc
&& job
->printer
->pc
->single_file
))
1298 cupsdClosePipe(job
->print_pipes
);
1300 if (job
->current_file
== job
->num_files
)
1302 close(job
->status_pipes
[1]);
1303 job
->status_pipes
[1] = -1;
1307 cupsdClosePipe(filterfds
[slot
]);
1309 for (i
= 6; i
< argc
; i
++)
1315 if (printer_state_reasons
)
1316 free(printer_state_reasons
);
1318 cupsdAddSelect(job
->status_buffer
->fd
, (cupsd_selfunc_t
)update_job
, NULL
,
1321 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
, "Job #%d started.",
1328 * If we get here, we need to abort the current job and close out all
1329 * files and pipes...
1334 FilterLevel
-= job
->cost
;
1337 for (slot
= 0; slot
< 2; slot
++)
1338 cupsdClosePipe(filterfds
[slot
]);
1340 cupsArrayDelete(filters
);
1344 for (i
= 6; i
< argc
; i
++)
1349 if (printer_state_reasons
)
1350 free(printer_state_reasons
);
1352 cupsdClosePipe(job
->print_pipes
);
1353 cupsdClosePipe(job
->back_pipes
);
1354 cupsdClosePipe(job
->side_pipes
);
1356 cupsdRemoveSelect(job
->status_pipes
[0]);
1357 cupsdClosePipe(job
->status_pipes
);
1358 cupsdStatBufDelete(job
->status_buffer
);
1359 job
->status_buffer
= NULL
;
1362 * Update the printer and job state.
1365 cupsdSetJobState(job
, abort_state
, CUPSD_JOB_DEFAULT
, "%s", abort_message
);
1366 cupsdSetPrinterState(job
->printer
, IPP_PRINTER_IDLE
, 0);
1367 update_job_attrs(job
, 0);
1370 free_job_history(job
);
1372 cupsArrayRemove(PrintingJobs
, job
);
1375 * Clear the printer <-> job association...
1378 job
->printer
->job
= NULL
;
1379 job
->printer
= NULL
;
1384 * 'cupsdDeleteJob()' - Free all memory used by a job.
1388 cupsdDeleteJob(cupsd_job_t
*job
, /* I - Job */
1389 cupsd_jobaction_t action
)/* I - Action */
1391 int i
; /* Looping var */
1395 finalize_job(job
, 1);
1397 if (action
== CUPSD_JOB_PURGE
)
1398 remove_job_history(job
);
1400 cupsdClearString(&job
->username
);
1401 cupsdClearString(&job
->dest
);
1403 i
< (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0]));
1405 cupsdClearString(job
->auth_env
+ i
);
1406 cupsdClearString(&job
->auth_uid
);
1408 if (action
== CUPSD_JOB_PURGE
)
1409 remove_job_files(job
);
1410 else if (job
->num_files
> 0)
1412 free(job
->compressions
);
1413 free(job
->filetypes
);
1419 free_job_history(job
);
1423 cupsArrayRemove(Jobs
, job
);
1424 cupsArrayRemove(ActiveJobs
, job
);
1425 cupsArrayRemove(PrintingJobs
, job
);
1432 * 'cupsdFreeAllJobs()' - Free all jobs from memory.
1436 cupsdFreeAllJobs(void)
1438 cupsd_job_t
*job
; /* Current job */
1446 cupsdStopAllJobs(CUPSD_JOB_FORCE
, 0);
1449 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1451 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1452 cupsdDeleteJob(job
, CUPSD_JOB_DEFAULT
);
1454 cupsdReleaseSignals();
1459 * 'cupsdFindJob()' - Find the specified job.
1462 cupsd_job_t
* /* O - Job data */
1463 cupsdFindJob(int id
) /* I - Job ID */
1465 cupsd_job_t key
; /* Search key */
1470 return ((cupsd_job_t
*)cupsArrayFind(Jobs
, &key
));
1475 * 'cupsdGetCompletedJobs()'- Generate a completed jobs list.
1478 cups_array_t
* /* O - Array of jobs */
1479 cupsdGetCompletedJobs(
1480 cupsd_printer_t
*p
) /* I - Printer */
1482 cups_array_t
*list
; /* Array of jobs */
1483 cupsd_job_t
*job
; /* Current job */
1486 list
= cupsArrayNew(compare_completed_jobs
, NULL
);
1488 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1490 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1491 if ((!p
|| !_cups_strcasecmp(p
->name
, job
->dest
)) && job
->state_value
>= IPP_JOB_STOPPED
&& job
->completed_time
)
1492 cupsArrayAdd(list
, job
);
1499 * 'cupsdGetPrinterJobCount()' - Get the number of pending, processing,
1500 * or held jobs in a printer or class.
1503 int /* O - Job count */
1504 cupsdGetPrinterJobCount(
1505 const char *dest
) /* I - Printer or class name */
1507 int count
; /* Job count */
1508 cupsd_job_t
*job
; /* Current job */
1511 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
1513 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
1514 if (job
->dest
&& !_cups_strcasecmp(job
->dest
, dest
))
1522 * 'cupsdGetUserJobCount()' - Get the number of pending, processing,
1523 * or held jobs for a user.
1526 int /* O - Job count */
1527 cupsdGetUserJobCount(
1528 const char *username
) /* I - Username */
1530 int count
; /* Job count */
1531 cupsd_job_t
*job
; /* Current job */
1534 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
1536 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
1537 if (!_cups_strcasecmp(job
->username
, username
))
1545 * 'cupsdLoadAllJobs()' - Load all jobs from disk.
1549 cupsdLoadAllJobs(void)
1551 char filename
[1024]; /* Full filename of job.cache file */
1552 struct stat fileinfo
; /* Information on job.cache file */
1553 cups_dir_t
*dir
; /* RequestRoot dir */
1554 cups_dentry_t
*dent
; /* Entry in RequestRoot */
1555 int load_cache
= 1; /* Load the job.cache file? */
1559 * Create the job arrays as needed...
1563 Jobs
= cupsArrayNew(compare_jobs
, NULL
);
1566 ActiveJobs
= cupsArrayNew(compare_active_jobs
, NULL
);
1569 PrintingJobs
= cupsArrayNew(compare_jobs
, NULL
);
1572 * See whether the job.cache file is older than the RequestRoot directory...
1575 snprintf(filename
, sizeof(filename
), "%s/job.cache", CacheDir
);
1577 if (stat(filename
, &fileinfo
))
1580 * No job.cache file...
1585 if (errno
!= ENOENT
)
1586 cupsdLogMessage(CUPSD_LOG_ERROR
,
1587 "Unable to get file information for \"%s\" - %s",
1588 filename
, strerror(errno
));
1590 else if ((dir
= cupsDirOpen(RequestRoot
)) == NULL
)
1593 * No spool directory...
1600 while ((dent
= cupsDirRead(dir
)) != NULL
)
1602 if (strlen(dent
->filename
) >= 6 && dent
->filename
[0] == 'c' && dent
->fileinfo
.st_mtime
> fileinfo
.st_mtime
)
1605 * Job history file is newer than job.cache file...
1617 * Load the most recent source for job data...
1623 * Load the job.cache file...
1626 load_job_cache(filename
);
1631 * Load the job history files...
1634 load_request_root();
1636 load_next_job_id(filename
);
1640 * Clean out old jobs as needed...
1643 if (MaxJobs
> 0 && cupsArrayCount(Jobs
) >= MaxJobs
)
1649 * 'cupsdLoadJob()' - Load a single job.
1652 int /* O - 1 on success, 0 on failure */
1653 cupsdLoadJob(cupsd_job_t
*job
) /* I - Job */
1655 int i
; /* Looping var */
1656 char jobfile
[1024]; /* Job filename */
1657 cups_file_t
*fp
; /* Job file */
1658 int fileid
; /* Current file ID */
1659 ipp_attribute_t
*attr
; /* Job attribute */
1660 const char *dest
; /* Destination name */
1661 cupsd_printer_t
*destptr
; /* Pointer to destination */
1662 mime_type_t
**filetypes
; /* New filetypes array */
1663 int *compressions
; /* New compressions array */
1668 if (job
->state_value
> IPP_JOB_STOPPED
)
1669 job
->access_time
= time(NULL
);
1674 if ((job
->attrs
= ippNew()) == NULL
)
1676 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Ran out of memory for job attributes.");
1681 * Load job attributes...
1684 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Loading attributes...");
1686 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, job
->id
);
1687 if ((fp
= cupsdOpenConfFile(jobfile
)) == NULL
)
1690 if (ippReadIO(fp
, (ipp_iocb_t
)cupsFileRead
, 1, NULL
, job
->attrs
) != IPP_DATA
)
1692 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1693 "Unable to read job control file \"%s\".", jobfile
);
1701 * Copy attribute data to the job object...
1704 if (!ippFindAttribute(job
->attrs
, "time-at-creation", IPP_TAG_INTEGER
))
1706 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1707 "Missing or bad time-at-creation attribute in control file.");
1711 if ((job
->state
= ippFindAttribute(job
->attrs
, "job-state",
1712 IPP_TAG_ENUM
)) == NULL
)
1714 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1715 "Missing or bad job-state attribute in control file.");
1719 job
->state_value
= (ipp_jstate_t
)job
->state
->values
[0].integer
;
1721 job
->history_time
= 0;
1723 if ((attr
= ippFindAttribute(job
->attrs
, "time-at-creation", IPP_TAG_INTEGER
)) != NULL
)
1724 job
->creation_time
= attr
->values
[0].integer
;
1726 if (job
->state_value
>= IPP_JOB_CANCELED
&& (attr
= ippFindAttribute(job
->attrs
, "time-at-completed", IPP_TAG_INTEGER
)) != NULL
)
1728 job
->completed_time
= attr
->values
[0].integer
;
1730 if (JobHistory
< INT_MAX
)
1731 job
->history_time
= attr
->values
[0].integer
+ JobHistory
;
1733 job
->history_time
= INT_MAX
;
1735 if (job
->history_time
< time(NULL
))
1736 goto error
; /* Expired, remove from history */
1738 if (job
->history_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
1739 JobHistoryUpdate
= job
->history_time
;
1741 if (JobFiles
< INT_MAX
)
1742 job
->file_time
= attr
->values
[0].integer
+ JobFiles
;
1744 job
->file_time
= INT_MAX
;
1746 if (job
->file_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
1747 JobHistoryUpdate
= job
->file_time
;
1749 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdLoadJob: JobHistoryUpdate=%ld",
1750 (long)JobHistoryUpdate
);
1755 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1756 IPP_TAG_URI
)) == NULL
)
1758 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1759 "No job-printer-uri attribute in control file.");
1763 if ((dest
= cupsdValidateDest(attr
->values
[0].string
.text
, &(job
->dtype
),
1766 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1767 "Unable to queue job for destination \"%s\".",
1768 attr
->values
[0].string
.text
);
1772 cupsdSetString(&job
->dest
, dest
);
1774 else if ((destptr
= cupsdFindDest(job
->dest
)) == NULL
)
1776 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1777 "Unable to queue job for destination \"%s\".",
1782 if ((job
->reasons
= ippFindAttribute(job
->attrs
, "job-state-reasons",
1783 IPP_TAG_KEYWORD
)) == NULL
)
1785 const char *reason
; /* job-state-reason keyword */
1787 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
1788 "Adding missing job-state-reasons attribute to control file.");
1790 switch (job
->state_value
)
1793 case IPP_JOB_PENDING
:
1794 if (destptr
->state
== IPP_PRINTER_STOPPED
)
1795 reason
= "printer-stopped";
1801 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1802 IPP_TAG_ZERO
)) != NULL
&&
1803 (attr
->value_tag
== IPP_TAG_NAME
||
1804 attr
->value_tag
== IPP_TAG_NAMELANG
||
1805 attr
->value_tag
== IPP_TAG_KEYWORD
) &&
1806 strcmp(attr
->values
[0].string
.text
, "no-hold"))
1807 reason
= "job-hold-until-specified";
1809 reason
= "job-incoming";
1812 case IPP_JOB_PROCESSING
:
1813 reason
= "job-printing";
1816 case IPP_JOB_STOPPED
:
1817 reason
= "job-stopped";
1820 case IPP_JOB_CANCELED
:
1821 reason
= "job-canceled-by-user";
1824 case IPP_JOB_ABORTED
:
1825 reason
= "aborted-by-system";
1828 case IPP_JOB_COMPLETED
:
1829 reason
= "job-completed-successfully";
1833 job
->reasons
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1834 "job-state-reasons", NULL
, reason
);
1836 else if (job
->state_value
== IPP_JOB_PENDING
)
1838 if (destptr
->state
== IPP_PRINTER_STOPPED
)
1839 ippSetString(job
->attrs
, &job
->reasons
, 0, "printer-stopped");
1841 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
1844 job
->impressions
= ippFindAttribute(job
->attrs
, "job-impressions-completed", IPP_TAG_INTEGER
);
1845 job
->sheets
= ippFindAttribute(job
->attrs
, "job-media-sheets-completed", IPP_TAG_INTEGER
);
1846 job
->job_sheets
= ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_NAME
);
1848 if (!job
->impressions
)
1849 job
->impressions
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-impressions-completed", 0);
1853 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
1854 IPP_TAG_INTEGER
)) == NULL
)
1856 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1857 "Missing or bad job-priority attribute in control file.");
1861 job
->priority
= attr
->values
[0].integer
;
1866 if ((attr
= ippFindAttribute(job
->attrs
, "job-originating-user-name",
1867 IPP_TAG_NAME
)) == NULL
)
1869 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1870 "Missing or bad job-originating-user-name "
1871 "attribute in control file.");
1875 cupsdSetString(&job
->username
, attr
->values
[0].string
.text
);
1880 if ((attr
= ippFindAttribute(job
->attrs
, "job-name", IPP_TAG_NAME
)) != NULL
)
1881 cupsdSetString(&job
->name
, attr
->values
[0].string
.text
);
1885 * Set the job hold-until time and state...
1888 if (job
->state_value
== IPP_JOB_HELD
)
1890 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1891 IPP_TAG_KEYWORD
)) == NULL
)
1892 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1895 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
, CUPSD_JOB_DEFAULT
);
1898 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1899 job
->state_value
= IPP_JOB_PENDING
;
1902 else if (job
->state_value
== IPP_JOB_PROCESSING
)
1904 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1905 job
->state_value
= IPP_JOB_PENDING
;
1908 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets", IPP_TAG_INTEGER
)) != NULL
)
1909 job
->koctets
= attr
->values
[0].integer
;
1911 if (!job
->num_files
)
1914 * Find all the d##### files...
1917 for (fileid
= 1; fileid
< 10000; fileid
++)
1919 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
1922 if (access(jobfile
, 0))
1925 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
1926 "Auto-typing document file \"%s\"...", jobfile
);
1928 if (fileid
> job
->num_files
)
1930 if (job
->num_files
== 0)
1932 compressions
= (int *)calloc((size_t)fileid
, sizeof(int));
1933 filetypes
= (mime_type_t
**)calloc((size_t)fileid
, sizeof(mime_type_t
*));
1937 compressions
= (int *)realloc(job
->compressions
, sizeof(int) * (size_t)fileid
);
1938 filetypes
= (mime_type_t
**)realloc(job
->filetypes
, sizeof(mime_type_t
*) * (size_t)fileid
);
1942 job
->compressions
= compressions
;
1945 job
->filetypes
= filetypes
;
1947 if (!compressions
|| !filetypes
)
1949 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1950 "Ran out of memory for job file types.");
1952 ippDelete(job
->attrs
);
1955 if (job
->compressions
)
1957 free(job
->compressions
);
1958 job
->compressions
= NULL
;
1963 free(job
->filetypes
);
1964 job
->filetypes
= NULL
;
1971 job
->num_files
= fileid
;
1974 job
->filetypes
[fileid
- 1] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
1975 job
->compressions
+ fileid
- 1);
1977 if (!job
->filetypes
[fileid
- 1])
1978 job
->filetypes
[fileid
- 1] = mimeType(MimeDatabase
, "application",
1984 * Load authentication information as needed...
1987 if (job
->state_value
< IPP_JOB_STOPPED
)
1989 snprintf(jobfile
, sizeof(jobfile
), "%s/a%05d", RequestRoot
, job
->id
);
1992 i
< (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0]));
1994 cupsdClearString(job
->auth_env
+ i
);
1995 cupsdClearString(&job
->auth_uid
);
1997 if ((fp
= cupsFileOpen(jobfile
, "r")) != NULL
)
1999 int bytes
, /* Size of auth data */
2000 linenum
= 1; /* Current line number */
2001 char line
[65536], /* Line from file */
2002 *value
, /* Value from line */
2003 data
[65536]; /* Decoded data */
2006 if (cupsFileGets(fp
, line
, sizeof(line
)) &&
2007 !strcmp(line
, "CUPSD-AUTH-V3"))
2010 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
2016 if (strcmp(line
, "negotiate") && strcmp(line
, "uid"))
2018 bytes
= sizeof(data
);
2019 httpDecode64_2(data
, &bytes
, value
);
2023 * Assign environment variables...
2026 if (!strcmp(line
, "uid"))
2028 cupsdSetStringf(&job
->auth_uid
, "AUTH_UID=%s", value
);
2031 else if (i
>= (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0])))
2034 if (!strcmp(line
, "username"))
2035 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_USERNAME=%s", data
);
2036 else if (!strcmp(line
, "domain"))
2037 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_DOMAIN=%s", data
);
2038 else if (!strcmp(line
, "password"))
2039 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_PASSWORD=%s", data
);
2040 else if (!strcmp(line
, "negotiate"))
2041 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_NEGOTIATE=%s", value
);
2053 job
->access_time
= time(NULL
);
2057 * If we get here then something bad happened...
2062 ippDelete(job
->attrs
);
2065 remove_job_history(job
);
2066 remove_job_files(job
);
2073 * 'cupsdMoveJob()' - Move the specified job to a different destination.
2077 cupsdMoveJob(cupsd_job_t
*job
, /* I - Job */
2078 cupsd_printer_t
*p
) /* I - Destination printer or class */
2080 ipp_attribute_t
*attr
; /* job-printer-uri attribute */
2081 const char *olddest
; /* Old destination */
2082 cupsd_printer_t
*oldp
; /* Old pointer */
2086 * Don't move completed jobs...
2089 if (job
->state_value
> IPP_JOB_STOPPED
)
2093 * Get the old destination...
2096 olddest
= job
->dest
;
2099 oldp
= job
->printer
;
2101 oldp
= cupsdFindDest(olddest
);
2104 * Change the destination information...
2107 if (job
->state_value
> IPP_JOB_HELD
)
2108 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
2109 "Stopping job prior to move.");
2111 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, oldp
, job
,
2112 "Job #%d moved from %s to %s.", job
->id
, olddest
,
2115 cupsdSetString(&job
->dest
, p
->name
);
2116 job
->dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_REMOTE
);
2118 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
2119 IPP_TAG_URI
)) != NULL
)
2120 ippSetString(job
->attrs
, &attr
, 0, p
->uri
);
2122 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, p
, job
,
2123 "Job #%d moved from %s to %s.", job
->id
, olddest
,
2127 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2132 * 'cupsdReleaseJob()' - Release the specified job.
2136 cupsdReleaseJob(cupsd_job_t
*job
) /* I - Job */
2138 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdReleaseJob(job=%p(%d))", job
,
2141 if (job
->state_value
== IPP_JOB_HELD
)
2144 * Add trailing banner as needed...
2147 if (job
->pending_timeout
)
2148 cupsdTimeoutJob(job
);
2150 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
2151 "Job released by user.");
2157 * 'cupsdRestartJob()' - Restart the specified job.
2161 cupsdRestartJob(cupsd_job_t
*job
) /* I - Job */
2163 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdRestartJob(job=%p(%d))", job
,
2166 if (job
->state_value
== IPP_JOB_STOPPED
|| job
->num_files
)
2167 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
2168 "Job restarted by user.");
2173 * 'cupsdSaveAllJobs()' - Save a summary of all jobs to disk.
2177 cupsdSaveAllJobs(void)
2179 int i
; /* Looping var */
2180 cups_file_t
*fp
; /* job.cache file */
2181 char filename
[1024], /* job.cache filename */
2182 temp
[1024]; /* Temporary string */
2183 cupsd_job_t
*job
; /* Current job */
2184 time_t curtime
; /* Current time */
2185 struct tm
*curdate
; /* Current date */
2188 snprintf(filename
, sizeof(filename
), "%s/job.cache", CacheDir
);
2189 if ((fp
= cupsdCreateConfFile(filename
, ConfigFilePerm
)) == NULL
)
2192 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving job.cache...");
2195 * Write a small header to the file...
2198 curtime
= time(NULL
);
2199 curdate
= localtime(&curtime
);
2200 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
2202 cupsFilePuts(fp
, "# Job cache file for " CUPS_SVERSION
"\n");
2203 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
2204 cupsFilePrintf(fp
, "NextJobId %d\n", NextJobId
);
2207 * Write each job known to the system...
2210 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
2212 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
2214 if (job
->printer
&& job
->printer
->temporary
)
2217 * Don't save jobs on temporary printers...
2223 cupsFilePrintf(fp
, "<Job %d>\n", job
->id
);
2224 cupsFilePrintf(fp
, "State %d\n", job
->state_value
);
2225 cupsFilePrintf(fp
, "Created %ld\n", (long)job
->creation_time
);
2226 if (job
->completed_time
)
2227 cupsFilePrintf(fp
, "Completed %ld\n", (long)job
->completed_time
);
2228 cupsFilePrintf(fp
, "Priority %d\n", job
->priority
);
2229 if (job
->hold_until
)
2230 cupsFilePrintf(fp
, "HoldUntil %ld\n", (long)job
->hold_until
);
2231 cupsFilePrintf(fp
, "Username %s\n", job
->username
);
2233 cupsFilePutConf(fp
, "Name", job
->name
);
2234 cupsFilePrintf(fp
, "Destination %s\n", job
->dest
);
2235 cupsFilePrintf(fp
, "DestType %d\n", job
->dtype
);
2236 cupsFilePrintf(fp
, "KOctets %d\n", job
->koctets
);
2237 cupsFilePrintf(fp
, "NumFiles %d\n", job
->num_files
);
2238 for (i
= 0; i
< job
->num_files
; i
++)
2239 cupsFilePrintf(fp
, "File %d %s/%s %d\n", i
+ 1, job
->filetypes
[i
]->super
,
2240 job
->filetypes
[i
]->type
, job
->compressions
[i
]);
2241 cupsFilePuts(fp
, "</Job>\n");
2244 cupsdCloseCreatedConfFile(fp
, filename
);
2249 * 'cupsdSaveJob()' - Save a job to disk.
2253 cupsdSaveJob(cupsd_job_t
*job
) /* I - Job */
2255 char filename
[1024]; /* Job control filename */
2256 cups_file_t
*fp
; /* Job file */
2259 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSaveJob(job=%p(%d)): job->attrs=%p",
2260 job
, job
->id
, job
->attrs
);
2262 if (job
->printer
&& job
->printer
->temporary
)
2265 * Don't save jobs on temporary printers...
2272 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
, job
->id
);
2274 if ((fp
= cupsdCreateConfFile(filename
, ConfigFilePerm
& 0600)) == NULL
)
2277 fchown(cupsFileNumber(fp
), RunUser
, Group
);
2279 job
->attrs
->state
= IPP_IDLE
;
2281 if (ippWriteIO(fp
, (ipp_iocb_t
)cupsFileWrite
, 1, NULL
,
2282 job
->attrs
) != IPP_DATA
)
2284 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Unable to write job control file.");
2289 if (!cupsdCloseCreatedConfFile(fp
, filename
))
2292 * Remove backup file and mark this job as clean...
2295 strlcat(filename
, ".O", sizeof(filename
));
2304 * 'cupsdSetJobHoldUntil()' - Set the hold time for a job.
2308 cupsdSetJobHoldUntil(cupsd_job_t
*job
, /* I - Job */
2309 const char *when
, /* I - When to resume */
2310 int update
)/* I - Update job-hold-until attr? */
2312 time_t curtime
; /* Current time */
2313 struct tm
*curdate
; /* Current date */
2314 int hour
; /* Hold hour */
2315 int minute
; /* Hold minute */
2316 int second
= 0; /* Hold second */
2319 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2320 "cupsdSetJobHoldUntil(job=%p(%d), when=\"%s\", update=%d)",
2321 job
, job
->id
, when
, update
);
2326 * Update the job-hold-until attribute...
2329 ipp_attribute_t
*attr
; /* job-hold-until attribute */
2331 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2332 IPP_TAG_KEYWORD
)) == NULL
)
2333 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2336 ippSetString(job
->attrs
, &attr
, 0, when
);
2338 attr
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
2339 "job-hold-until", NULL
, when
);
2343 if (isdigit(when
[0] & 255))
2344 attr
->value_tag
= IPP_TAG_NAME
;
2346 attr
->value_tag
= IPP_TAG_KEYWORD
;
2349 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2354 if (strcmp(when
, "no-hold"))
2355 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-hold-until-specified");
2357 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
2360 * Update the hold time...
2363 job
->cancel_time
= 0;
2365 if (!strcmp(when
, "indefinite") || !strcmp(when
, "auth-info-required"))
2368 * Hold indefinitely...
2371 job
->hold_until
= 0;
2373 if (MaxHoldTime
> 0)
2374 job
->cancel_time
= time(NULL
) + MaxHoldTime
;
2376 else if (!strcmp(when
, "day-time"))
2379 * Hold to 6am the next morning unless local time is < 6pm.
2382 curtime
= time(NULL
);
2383 curdate
= localtime(&curtime
);
2385 if (curdate
->tm_hour
< 18)
2386 job
->hold_until
= curtime
;
2388 job
->hold_until
= curtime
+
2389 ((29 - curdate
->tm_hour
) * 60 + 59 -
2390 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2392 else if (!strcmp(when
, "evening") || !strcmp(when
, "night"))
2395 * Hold to 6pm unless local time is > 6pm or < 6am.
2398 curtime
= time(NULL
);
2399 curdate
= localtime(&curtime
);
2401 if (curdate
->tm_hour
< 6 || curdate
->tm_hour
>= 18)
2402 job
->hold_until
= curtime
;
2404 job
->hold_until
= curtime
+
2405 ((17 - curdate
->tm_hour
) * 60 + 59 -
2406 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2408 else if (!strcmp(when
, "second-shift"))
2411 * Hold to 4pm unless local time is > 4pm.
2414 curtime
= time(NULL
);
2415 curdate
= localtime(&curtime
);
2417 if (curdate
->tm_hour
>= 16)
2418 job
->hold_until
= curtime
;
2420 job
->hold_until
= curtime
+
2421 ((15 - curdate
->tm_hour
) * 60 + 59 -
2422 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2424 else if (!strcmp(when
, "third-shift"))
2427 * Hold to 12am unless local time is < 8am.
2430 curtime
= time(NULL
);
2431 curdate
= localtime(&curtime
);
2433 if (curdate
->tm_hour
< 8)
2434 job
->hold_until
= curtime
;
2436 job
->hold_until
= curtime
+
2437 ((23 - curdate
->tm_hour
) * 60 + 59 -
2438 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2440 else if (!strcmp(when
, "weekend"))
2443 * Hold to weekend unless we are in the weekend.
2446 curtime
= time(NULL
);
2447 curdate
= localtime(&curtime
);
2449 if (curdate
->tm_wday
== 0 || curdate
->tm_wday
== 6)
2450 job
->hold_until
= curtime
;
2452 job
->hold_until
= curtime
+
2453 (((5 - curdate
->tm_wday
) * 24 +
2454 (17 - curdate
->tm_hour
)) * 60 + 59 -
2455 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2457 else if (sscanf(when
, "%d:%d:%d", &hour
, &minute
, &second
) >= 2)
2460 * Hold to specified GMT time (HH:MM or HH:MM:SS)...
2463 curtime
= time(NULL
);
2464 curdate
= gmtime(&curtime
);
2466 job
->hold_until
= curtime
+
2467 ((hour
- curdate
->tm_hour
) * 60 + minute
-
2468 curdate
->tm_min
) * 60 + second
- curdate
->tm_sec
;
2471 * Hold until next day as needed...
2474 if (job
->hold_until
< curtime
)
2475 job
->hold_until
+= 24 * 60 * 60;
2478 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetJobHoldUntil: hold_until=%d",
2479 (int)job
->hold_until
);
2484 * 'cupsdSetJobPriority()' - Set the priority of a job, moving it up/down in
2485 * the list as needed.
2489 cupsdSetJobPriority(
2490 cupsd_job_t
*job
, /* I - Job ID */
2491 int priority
) /* I - New priority (0 to 100) */
2493 ipp_attribute_t
*attr
; /* Job attribute */
2497 * Don't change completed jobs...
2500 if (job
->state_value
>= IPP_JOB_PROCESSING
)
2504 * Set the new priority and re-add the job into the active list...
2507 cupsArrayRemove(ActiveJobs
, job
);
2509 job
->priority
= priority
;
2511 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
2512 IPP_TAG_INTEGER
)) != NULL
)
2513 attr
->values
[0].integer
= priority
;
2515 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-priority",
2518 cupsArrayAdd(ActiveJobs
, job
);
2521 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2526 * 'cupsdSetJobState()' - Set the state of the specified print job.
2531 cupsd_job_t
*job
, /* I - Job to cancel */
2532 ipp_jstate_t newstate
, /* I - New job state */
2533 cupsd_jobaction_t action
, /* I - Action to take */
2534 const char *message
, /* I - Message to log */
2535 ...) /* I - Additional arguments as needed */
2537 int i
; /* Looping var */
2538 ipp_jstate_t oldstate
; /* Old state */
2539 char filename
[1024]; /* Job filename */
2540 ipp_attribute_t
*attr
; /* Job attribute */
2543 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2544 "cupsdSetJobState(job=%p(%d), state=%d, newstate=%d, "
2545 "action=%d, message=\"%s\")", job
, job
->id
, job
->state_value
,
2546 newstate
, action
, message
? message
: "(null)");
2550 * Make sure we have the job attributes...
2553 if (!cupsdLoadJob(job
))
2557 * Don't do anything if the state is unchanged and we aren't purging the
2561 oldstate
= job
->state_value
;
2562 if (newstate
== oldstate
&& action
!= CUPSD_JOB_PURGE
)
2566 * Stop any processes that are working on the current job...
2569 if (oldstate
== IPP_JOB_PROCESSING
)
2570 stop_job(job
, action
);
2573 * Set the new job state...
2576 job
->state_value
= newstate
;
2579 job
->state
->values
[0].integer
= newstate
;
2583 case IPP_JOB_PENDING
:
2585 * Update job-hold-until as needed...
2588 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2589 IPP_TAG_KEYWORD
)) == NULL
)
2590 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2594 ippSetValueTag(job
->attrs
, &attr
, IPP_TAG_KEYWORD
);
2595 ippSetString(job
->attrs
, &attr
, 0, "no-hold");
2601 case IPP_JOB_ABORTED
:
2602 case IPP_JOB_CANCELED
:
2603 case IPP_JOB_COMPLETED
:
2604 set_time(job
, "time-at-completed");
2605 ippSetString(job
->attrs
, &job
->reasons
, 0, "processing-to-stop-point");
2610 * Log message as needed...
2615 char buffer
[2048]; /* Message buffer */
2616 va_list ap
; /* Pointer to additional arguments */
2618 va_start(ap
, message
);
2619 vsnprintf(buffer
, sizeof(buffer
), message
, ap
);
2622 if (newstate
> IPP_JOB_STOPPED
)
2623 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
, "%s", buffer
);
2625 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
, "%s", buffer
);
2627 if (newstate
== IPP_JOB_STOPPED
|| newstate
== IPP_JOB_ABORTED
)
2628 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "%s", buffer
);
2630 cupsdLogJob(job
, CUPSD_LOG_INFO
, "%s", buffer
);
2634 * Handle post-state-change actions...
2639 case IPP_JOB_PROCESSING
:
2641 * Add the job to the "printing" list...
2644 if (!cupsArrayFind(PrintingJobs
, job
))
2645 cupsArrayAdd(PrintingJobs
, job
);
2648 * Set the processing time...
2651 set_time(job
, "time-at-processing");
2653 case IPP_JOB_PENDING
:
2655 case IPP_JOB_STOPPED
:
2657 * Make sure the job is in the active list...
2660 if (!cupsArrayFind(ActiveJobs
, job
))
2661 cupsArrayAdd(ActiveJobs
, job
);
2664 * Save the job state to disk...
2668 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2671 case IPP_JOB_ABORTED
:
2672 case IPP_JOB_CANCELED
:
2673 case IPP_JOB_COMPLETED
:
2674 if (newstate
== IPP_JOB_CANCELED
)
2677 * Remove the job from the active list if there are no processes still
2681 for (i
= 0; job
->filters
[i
] < 0; i
++);
2683 if (!job
->filters
[i
] && job
->backend
<= 0)
2684 cupsArrayRemove(ActiveJobs
, job
);
2689 * Otherwise just remove the job from the active list immediately...
2692 cupsArrayRemove(ActiveJobs
, job
);
2696 * Expire job subscriptions since the job is now "completed"...
2699 cupsdExpireSubscriptions(NULL
, job
);
2703 * If we are going to sleep and the PrintingJobs count is now 0, allow the
2704 * sleep to happen immediately...
2707 if (Sleeping
&& cupsArrayCount(PrintingJobs
) == 0)
2709 #endif /* __APPLE__ */
2712 * Remove any authentication data...
2715 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
2716 if (cupsdRemoveFile(filename
) && errno
!= ENOENT
)
2717 cupsdLogMessage(CUPSD_LOG_ERROR
,
2718 "Unable to remove authentication cache: %s",
2722 i
< (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0]));
2724 cupsdClearString(job
->auth_env
+ i
);
2726 cupsdClearString(&job
->auth_uid
);
2729 * Remove the print file for good if we aren't preserving jobs or
2733 if (!JobHistory
|| !JobFiles
|| action
== CUPSD_JOB_PURGE
)
2734 remove_job_files(job
);
2736 if (JobHistory
&& action
!= CUPSD_JOB_PURGE
)
2739 * Save job state info...
2743 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2745 else if (!job
->printer
)
2748 * Delete the job immediately if not actively printing...
2751 cupsdDeleteJob(job
, CUPSD_JOB_PURGE
);
2758 * Finalize the job immediately if we forced things...
2761 if (action
>= CUPSD_JOB_FORCE
&& job
&& job
->printer
)
2762 finalize_job(job
, 0);
2765 * Update the server "busy" state...
2768 cupsdSetBusyState();
2773 * 'cupsdStopAllJobs()' - Stop all print jobs.
2778 cupsd_jobaction_t action
, /* I - Action */
2779 int kill_delay
) /* I - Number of seconds before we kill */
2781 cupsd_job_t
*job
; /* Current job */
2784 DEBUG_puts("cupsdStopAllJobs()");
2786 for (job
= (cupsd_job_t
*)cupsArrayFirst(PrintingJobs
);
2788 job
= (cupsd_job_t
*)cupsArrayNext(PrintingJobs
))
2792 cupsdSetJobState(job
, IPP_JOB_COMPLETED
, CUPSD_JOB_FORCE
, NULL
);
2797 job
->kill_time
= time(NULL
) + kill_delay
;
2799 cupsdSetJobState(job
, IPP_JOB_PENDING
, action
, NULL
);
2806 * 'cupsdUnloadCompletedJobs()' - Flush completed job history from memory.
2810 cupsdUnloadCompletedJobs(void)
2812 cupsd_job_t
*job
; /* Current job */
2813 time_t expire
; /* Expiration time */
2816 expire
= time(NULL
) - 60;
2818 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
2820 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
2821 if (job
->attrs
&& job
->state_value
>= IPP_JOB_STOPPED
&& !job
->printer
&&
2822 job
->access_time
< expire
)
2834 * 'cupsdUpdateJobs()' - Update the history/file files for all jobs.
2838 cupsdUpdateJobs(void)
2840 cupsd_job_t
*job
; /* Current job */
2841 time_t curtime
; /* Current time */
2842 ipp_attribute_t
*attr
; /* time-at-completed attribute */
2845 curtime
= time(NULL
);
2846 JobHistoryUpdate
= 0;
2848 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
2850 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
2852 if (job
->state_value
>= IPP_JOB_CANCELED
&&
2853 (attr
= ippFindAttribute(job
->attrs
, "time-at-completed",
2854 IPP_TAG_INTEGER
)) != NULL
)
2857 * Update history/file expiration times...
2860 if (JobHistory
< INT_MAX
)
2861 job
->history_time
= attr
->values
[0].integer
+ JobHistory
;
2863 job
->history_time
= INT_MAX
;
2865 if (job
->history_time
< curtime
)
2867 cupsdDeleteJob(job
, CUPSD_JOB_PURGE
);
2871 if (job
->history_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
2872 JobHistoryUpdate
= job
->history_time
;
2874 if (JobFiles
< INT_MAX
)
2875 job
->file_time
= attr
->values
[0].integer
+ JobFiles
;
2877 job
->file_time
= INT_MAX
;
2879 if (job
->file_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
2880 JobHistoryUpdate
= job
->file_time
;
2884 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdUpdateAllJobs: JobHistoryUpdate=%ld",
2885 (long)JobHistoryUpdate
);
2890 * 'compare_active_jobs()' - Compare the job IDs and priorities of two jobs.
2893 static int /* O - Difference */
2894 compare_active_jobs(void *first
, /* I - First job */
2895 void *second
, /* I - Second job */
2896 void *data
) /* I - App data (not used) */
2898 int diff
; /* Difference */
2903 if ((diff
= ((cupsd_job_t
*)second
)->priority
-
2904 ((cupsd_job_t
*)first
)->priority
) != 0)
2907 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
2912 * 'compare_completed_jobs()' - Compare the job IDs and completion times of two jobs.
2915 static int /* O - Difference */
2916 compare_completed_jobs(void *first
, /* I - First job */
2917 void *second
, /* I - Second job */
2918 void *data
) /* I - App data (not used) */
2920 int diff
; /* Difference */
2925 if ((diff
= ((cupsd_job_t
*)second
)->completed_time
-
2926 ((cupsd_job_t
*)first
)->completed_time
) != 0)
2929 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
2934 * 'compare_jobs()' - Compare the job IDs of two jobs.
2937 static int /* O - Difference */
2938 compare_jobs(void *first
, /* I - First job */
2939 void *second
, /* I - Second job */
2940 void *data
) /* I - App data (not used) */
2944 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
2949 * 'dump_job_history()' - Dump any debug messages for a job.
2953 dump_job_history(cupsd_job_t
*job
) /* I - Job */
2955 int i
, /* Looping var */
2956 oldsize
; /* Current MaxLogSize */
2957 struct tm
*date
; /* Date/time value */
2958 cupsd_joblog_t
*message
; /* Current message */
2959 char temp
[2048], /* Log message */
2960 *ptr
, /* Pointer into log message */
2961 start
[256], /* Start time */
2962 end
[256]; /* End time */
2963 cupsd_printer_t
*printer
; /* Printer for job */
2967 * See if we have anything to dump...
2974 * Disable log rotation temporarily...
2977 oldsize
= MaxLogSize
;
2981 * Copy the debug messages to the log...
2984 message
= (cupsd_joblog_t
*)cupsArrayFirst(job
->history
);
2985 date
= localtime(&(message
->time
));
2986 strftime(start
, sizeof(start
), "%X", date
);
2988 message
= (cupsd_joblog_t
*)cupsArrayLast(job
->history
);
2989 date
= localtime(&(message
->time
));
2990 strftime(end
, sizeof(end
), "%X", date
);
2992 snprintf(temp
, sizeof(temp
),
2993 "[Job %d] The following messages were recorded from %s to %s",
2994 job
->id
, start
, end
);
2995 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, temp
);
2997 for (message
= (cupsd_joblog_t
*)cupsArrayFirst(job
->history
);
2999 message
= (cupsd_joblog_t
*)cupsArrayNext(job
->history
))
3000 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, message
->message
);
3002 snprintf(temp
, sizeof(temp
), "[Job %d] End of messages", job
->id
);
3003 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, temp
);
3006 * Log the printer state values...
3009 if ((printer
= job
->printer
) == NULL
)
3010 printer
= cupsdFindDest(job
->dest
);
3014 snprintf(temp
, sizeof(temp
), "[Job %d] printer-state=%d(%s)", job
->id
,
3016 printer
->state
== IPP_PRINTER_IDLE
? "idle" :
3017 printer
->state
== IPP_PRINTER_PROCESSING
? "processing" :
3019 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, temp
);
3021 snprintf(temp
, sizeof(temp
), "[Job %d] printer-state-message=\"%s\"",
3022 job
->id
, printer
->state_message
);
3023 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, temp
);
3025 snprintf(temp
, sizeof(temp
), "[Job %d] printer-state-reasons=", job
->id
);
3026 ptr
= temp
+ strlen(temp
);
3027 if (printer
->num_reasons
== 0)
3028 strlcpy(ptr
, "none", sizeof(temp
) - (size_t)(ptr
- temp
));
3032 i
< printer
->num_reasons
&& ptr
< (temp
+ sizeof(temp
) - 2);
3038 strlcpy(ptr
, printer
->reasons
[i
], sizeof(temp
) - (size_t)(ptr
- temp
));
3042 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, temp
);
3046 * Restore log file rotation...
3049 MaxLogSize
= oldsize
;
3052 * Free all messages...
3055 free_job_history(job
);
3060 * 'free_job_history()' - Free any log history.
3064 free_job_history(cupsd_job_t
*job
) /* I - Job */
3066 char *message
; /* Current message */
3072 for (message
= (char *)cupsArrayFirst(job
->history
);
3074 message
= (char *)cupsArrayNext(job
->history
))
3077 cupsArrayDelete(job
->history
);
3078 job
->history
= NULL
;
3083 * 'finalize_job()' - Cleanup after job filter processes and support data.
3087 finalize_job(cupsd_job_t
*job
, /* I - Job */
3088 int set_job_state
) /* I - 1 = set the job state */
3090 ipp_pstate_t printer_state
; /* New printer state value */
3091 ipp_jstate_t job_state
; /* New job state value */
3092 const char *message
; /* Message for job state */
3093 char buffer
[1024]; /* Buffer for formatted messages */
3096 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "finalize_job(job=%p(%d))", job
, job
->id
);
3099 * Clear the "connecting-to-device" and "cups-waiting-for-job-completed"
3100 * reasons, which are only valid when a printer is processing, along with any
3101 * remote printing job state...
3104 cupsdSetPrinterReasons(job
->printer
, "-connecting-to-device,"
3105 "cups-waiting-for-job-completed,"
3106 "cups-remote-pending,"
3107 "cups-remote-pending-held,"
3108 "cups-remote-processing,"
3109 "cups-remote-stopped,"
3110 "cups-remote-canceled,"
3111 "cups-remote-aborted,"
3112 "cups-remote-completed");
3115 * Similarly, clear the "offline-report" reason for non-USB devices since we
3116 * rarely have current information for network devices...
3119 if (strncmp(job
->printer
->device_uri
, "usb:", 4) &&
3120 strncmp(job
->printer
->device_uri
, "ippusb:", 7))
3121 cupsdSetPrinterReasons(job
->printer
, "-offline-report");
3124 * Free the security profile...
3127 cupsdDestroyProfile(job
->profile
);
3128 job
->profile
= NULL
;
3129 cupsdDestroyProfile(job
->bprofile
);
3130 job
->bprofile
= NULL
;
3133 * Clear the unresponsive job watchdog timers...
3136 job
->cancel_time
= 0;
3140 * Close pipes and status buffer...
3143 cupsdClosePipe(job
->print_pipes
);
3144 cupsdClosePipe(job
->back_pipes
);
3145 cupsdClosePipe(job
->side_pipes
);
3147 cupsdRemoveSelect(job
->status_pipes
[0]);
3148 cupsdClosePipe(job
->status_pipes
);
3149 cupsdStatBufDelete(job
->status_buffer
);
3150 job
->status_buffer
= NULL
;
3153 * Process the exit status...
3156 if (job
->printer
->state
== IPP_PRINTER_PROCESSING
)
3157 printer_state
= IPP_PRINTER_IDLE
;
3159 printer_state
= job
->printer
->state
;
3161 switch (job_state
= job
->state_value
)
3163 case IPP_JOB_PENDING
:
3164 message
= "Job paused.";
3168 message
= "Job held.";
3172 case IPP_JOB_PROCESSING
:
3173 case IPP_JOB_COMPLETED
:
3174 job_state
= IPP_JOB_COMPLETED
;
3175 message
= "Job completed.";
3178 ippSetString(job
->attrs
, &job
->reasons
, 0,
3179 "job-completed-successfully");
3182 case IPP_JOB_STOPPED
:
3183 message
= "Job stopped.";
3185 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-stopped");
3188 case IPP_JOB_CANCELED
:
3189 message
= "Job canceled.";
3191 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-canceled-by-user");
3194 case IPP_JOB_ABORTED
:
3195 message
= "Job aborted.";
3199 if (job
->status
< 0)
3202 * Backend had errors...
3205 int exit_code
; /* Exit code from backend */
3208 * Convert the status to an exit code. Due to the way the W* macros are
3209 * implemented on macOS (bug?), we have to store the exit status in a
3210 * variable first and then convert...
3213 exit_code
= -job
->status
;
3214 if (WIFEXITED(exit_code
))
3215 exit_code
= WEXITSTATUS(exit_code
);
3218 ippSetString(job
->attrs
, &job
->reasons
, 0, "cups-backend-crashed");
3219 exit_code
= job
->status
;
3222 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Backend returned status %d (%s)",
3224 exit_code
== CUPS_BACKEND_FAILED
? "failed" :
3225 exit_code
== CUPS_BACKEND_AUTH_REQUIRED
?
3226 "authentication required" :
3227 exit_code
== CUPS_BACKEND_HOLD
? "hold job" :
3228 exit_code
== CUPS_BACKEND_STOP
? "stop printer" :
3229 exit_code
== CUPS_BACKEND_CANCEL
? "cancel job" :
3230 exit_code
== CUPS_BACKEND_RETRY
? "retry job later" :
3231 exit_code
== CUPS_BACKEND_RETRY_CURRENT
? "retry job immediately" :
3232 exit_code
< 0 ? "crashed" : "unknown");
3235 * Do what needs to be done...
3241 case CUPS_BACKEND_FAILED
:
3243 * Backend failure, use the error-policy to determine how to
3247 if (job
->dtype
& CUPS_PRINTER_CLASS
)
3250 * Queued on a class - mark the job as pending and we'll retry on
3251 * another printer...
3254 if (job_state
== IPP_JOB_COMPLETED
)
3256 job_state
= IPP_JOB_PENDING
;
3257 message
= "Retrying job on another printer.";
3259 ippSetString(job
->attrs
, &job
->reasons
, 0,
3260 "resources-are-not-ready");
3263 else if (!strcmp(job
->printer
->error_policy
, "retry-current-job"))
3266 * The error policy is "retry-current-job" - mark the job as pending
3267 * and we'll retry on the same printer...
3270 if (job_state
== IPP_JOB_COMPLETED
)
3272 job_state
= IPP_JOB_PENDING
;
3273 message
= "Retrying job on same printer.";
3275 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
3278 else if ((job
->printer
->type
& CUPS_PRINTER_FAX
) ||
3279 !strcmp(job
->printer
->error_policy
, "retry-job"))
3281 if (job_state
== IPP_JOB_COMPLETED
)
3284 * The job was queued on a fax or the error policy is "retry-job" -
3285 * hold the job if the number of retries is less than the
3286 * JobRetryLimit, otherwise abort the job.
3291 if (job
->tries
> JobRetryLimit
&& JobRetryLimit
> 0)
3297 snprintf(buffer
, sizeof(buffer
),
3298 "Job aborted after %d unsuccessful attempts.",
3300 job_state
= IPP_JOB_ABORTED
;
3303 ippSetString(job
->attrs
, &job
->reasons
, 0, "aborted-by-system");
3308 * Try again in N seconds...
3311 snprintf(buffer
, sizeof(buffer
),
3312 "Job held for %d seconds since it could not be sent.",
3315 job
->hold_until
= time(NULL
) + JobRetryInterval
;
3316 job_state
= IPP_JOB_HELD
;
3319 ippSetString(job
->attrs
, &job
->reasons
, 0,
3320 "resources-are-not-ready");
3324 else if (!strcmp(job
->printer
->error_policy
, "abort-job") &&
3325 job_state
== IPP_JOB_COMPLETED
)
3327 job_state
= IPP_JOB_ABORTED
;
3328 message
= "Job aborted due to backend errors; please consult "
3329 "the error_log file for details.";
3331 ippSetString(job
->attrs
, &job
->reasons
, 0, "aborted-by-system");
3333 else if (job
->state_value
== IPP_JOB_PROCESSING
)
3335 job_state
= IPP_JOB_PENDING
;
3336 printer_state
= IPP_PRINTER_STOPPED
;
3337 message
= "Printer stopped due to backend errors; please "
3338 "consult the error_log file for details.";
3340 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
3344 case CUPS_BACKEND_CANCEL
:
3349 if (job_state
== IPP_JOB_COMPLETED
)
3351 job_state
= IPP_JOB_CANCELED
;
3352 message
= "Job canceled at printer.";
3354 ippSetString(job
->attrs
, &job
->reasons
, 0, "canceled-at-device");
3358 case CUPS_BACKEND_HOLD
:
3359 if (job_state
== IPP_JOB_COMPLETED
)
3365 const char *reason
= ippGetString(job
->reasons
, 0, NULL
);
3367 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "job-state-reasons=\"%s\"",
3370 if (!reason
|| strncmp(reason
, "account-", 8))
3372 cupsdSetJobHoldUntil(job
, "indefinite", 1);
3374 ippSetString(job
->attrs
, &job
->reasons
, 0,
3375 "job-hold-until-specified");
3376 message
= "Job held indefinitely due to backend errors; please "
3377 "consult the error_log file for details.";
3379 else if (!strcmp(reason
, "account-info-needed"))
3381 cupsdSetJobHoldUntil(job
, "indefinite", 0);
3383 message
= "Job held indefinitely - account information is "
3386 else if (!strcmp(reason
, "account-closed"))
3388 cupsdSetJobHoldUntil(job
, "indefinite", 0);
3390 message
= "Job held indefinitely - account has been closed.";
3392 else if (!strcmp(reason
, "account-limit-reached"))
3394 cupsdSetJobHoldUntil(job
, "indefinite", 0);
3396 message
= "Job held indefinitely - account limit has been "
3401 cupsdSetJobHoldUntil(job
, "indefinite", 0);
3403 message
= "Job held indefinitely - account authorization failed.";
3406 job_state
= IPP_JOB_HELD
;
3410 case CUPS_BACKEND_STOP
:
3412 * Stop the printer...
3415 printer_state
= IPP_PRINTER_STOPPED
;
3416 message
= "Printer stopped due to backend errors; please "
3417 "consult the error_log file for details.";
3419 if (job_state
== IPP_JOB_COMPLETED
)
3421 job_state
= IPP_JOB_PENDING
;
3423 ippSetString(job
->attrs
, &job
->reasons
, 0,
3424 "resources-are-not-ready");
3428 case CUPS_BACKEND_AUTH_REQUIRED
:
3430 * Hold the job for authentication...
3433 if (job_state
== IPP_JOB_COMPLETED
)
3435 cupsdSetJobHoldUntil(job
, "auth-info-required", 1);
3437 job_state
= IPP_JOB_HELD
;
3438 message
= "Job held for authentication.";
3440 if (strncmp(job
->reasons
->values
[0].string
.text
, "account-", 8))
3441 ippSetString(job
->attrs
, &job
->reasons
, 0,
3442 "cups-held-for-authentication");
3446 case CUPS_BACKEND_RETRY
:
3447 if (job_state
== IPP_JOB_COMPLETED
)
3450 * Hold the job if the number of retries is less than the
3451 * JobRetryLimit, otherwise abort the job.
3456 if (job
->tries
> JobRetryLimit
&& JobRetryLimit
> 0)
3462 snprintf(buffer
, sizeof(buffer
),
3463 "Job aborted after %d unsuccessful attempts.",
3465 job_state
= IPP_JOB_ABORTED
;
3468 ippSetString(job
->attrs
, &job
->reasons
, 0, "aborted-by-system");
3473 * Try again in N seconds...
3476 snprintf(buffer
, sizeof(buffer
),
3477 "Job held for %d seconds since it could not be sent.",
3480 job
->hold_until
= time(NULL
) + JobRetryInterval
;
3481 job_state
= IPP_JOB_HELD
;
3484 ippSetString(job
->attrs
, &job
->reasons
, 0,
3485 "resources-are-not-ready");
3490 case CUPS_BACKEND_RETRY_CURRENT
:
3492 * Mark the job as pending and retry on the same printer...
3495 if (job_state
== IPP_JOB_COMPLETED
)
3497 job_state
= IPP_JOB_PENDING
;
3498 message
= "Retrying job on same printer.";
3500 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
3505 else if (job
->status
> 0)
3508 * Filter had errors; stop job...
3511 if (job_state
== IPP_JOB_COMPLETED
)
3513 job_state
= IPP_JOB_STOPPED
;
3514 message
= "Job stopped due to filter errors; please consult the "
3515 "error_log file for details.";
3517 if (WIFSIGNALED(job
->status
))
3518 ippSetString(job
->attrs
, &job
->reasons
, 0, "cups-filter-crashed");
3520 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-completed-with-errors");
3525 * Update the printer and job state.
3528 if (set_job_state
&& job_state
!= job
->state_value
)
3529 cupsdSetJobState(job
, job_state
, CUPSD_JOB_DEFAULT
, "%s", message
);
3531 cupsdSetPrinterState(job
->printer
, printer_state
,
3532 printer_state
== IPP_PRINTER_STOPPED
);
3533 update_job_attrs(job
, 0);
3538 (job
->state_value
== IPP_JOB_ABORTED
||
3539 job
->state_value
== IPP_JOB_STOPPED
))
3540 dump_job_history(job
);
3542 free_job_history(job
);
3545 cupsArrayRemove(PrintingJobs
, job
);
3548 * Clear informational messages...
3551 if (job
->status_level
> CUPSD_LOG_ERROR
)
3552 job
->printer
->state_message
[0] = '\0';
3555 * Apply any PPD updates...
3558 if (job
->num_keywords
)
3560 if (cupsdUpdatePrinterPPD(job
->printer
, job
->num_keywords
, job
->keywords
))
3561 cupsdSetPrinterAttrs(job
->printer
);
3563 cupsFreeOptions(job
->num_keywords
, job
->keywords
);
3565 job
->num_keywords
= 0;
3566 job
->keywords
= NULL
;
3570 * Clear the printer <-> job association...
3573 job
->printer
->job
= NULL
;
3574 job
->printer
= NULL
;
3579 * 'get_options()' - Get a string containing the job options.
3582 static char * /* O - Options string */
3583 get_options(cupsd_job_t
*job
, /* I - Job */
3584 int banner_page
, /* I - Printing a banner page? */
3585 char *copies
, /* I - Copies buffer */
3586 size_t copies_size
, /* I - Size of copies buffer */
3587 char *title
, /* I - Title buffer */
3588 size_t title_size
) /* I - Size of title buffer */
3590 int i
; /* Looping var */
3591 size_t newlength
; /* New option buffer length */
3592 char *optptr
, /* Pointer to options */
3593 *valptr
; /* Pointer in value string */
3594 ipp_attribute_t
*attr
; /* Current attribute */
3595 _ppd_cache_t
*pc
; /* PPD cache and mapping data */
3596 int num_pwgppds
; /* Number of PWG->PPD options */
3597 cups_option_t
*pwgppds
, /* PWG->PPD options */
3598 *pwgppd
, /* Current PWG->PPD option */
3599 *preset
; /* Current preset option */
3600 int print_color_mode
,
3601 /* Output mode (if any) */
3602 print_quality
; /* Print quality (if any) */
3603 const char *ppd
; /* PPD option choice */
3604 int exact
; /* Did we get an exact match? */
3605 static char *options
= NULL
;/* Full list of options */
3606 static size_t optlength
= 0; /* Length of option buffer */
3610 * Building the options string is harder than it needs to be, but for the
3611 * moment we need to pass strings for command-line args and not IPP attribute
3614 * First build an options array for any PWG->PPD mapped option/choice pairs.
3617 pc
= job
->printer
->pc
;
3622 !ippFindAttribute(job
->attrs
,
3623 "com.apple.print.DocumentTicket.PMSpoolFormat",
3625 !ippFindAttribute(job
->attrs
, "APPrinterPreset", IPP_TAG_ZERO
) &&
3626 (ippFindAttribute(job
->attrs
, "print-color-mode", IPP_TAG_ZERO
) ||
3627 ippFindAttribute(job
->attrs
, "print-quality", IPP_TAG_ZERO
)))
3630 * Map print-color-mode and print-quality to a preset...
3633 if ((attr
= ippFindAttribute(job
->attrs
, "print-color-mode",
3634 IPP_TAG_KEYWORD
)) != NULL
&&
3635 !strcmp(attr
->values
[0].string
.text
, "monochrome"))
3636 print_color_mode
= _PWG_PRINT_COLOR_MODE_MONOCHROME
;
3638 print_color_mode
= _PWG_PRINT_COLOR_MODE_COLOR
;
3640 if ((attr
= ippFindAttribute(job
->attrs
, "print-quality",
3641 IPP_TAG_ENUM
)) != NULL
&&
3642 attr
->values
[0].integer
>= IPP_QUALITY_DRAFT
&&
3643 attr
->values
[0].integer
<= IPP_QUALITY_HIGH
)
3644 print_quality
= attr
->values
[0].integer
- IPP_QUALITY_DRAFT
;
3646 print_quality
= _PWG_PRINT_QUALITY_NORMAL
;
3648 if (pc
->num_presets
[print_color_mode
][print_quality
] == 0)
3651 * Try to find a preset that works so that we maximize the chances of us
3652 * getting a good print using IPP attributes.
3655 if (pc
->num_presets
[print_color_mode
][_PWG_PRINT_QUALITY_NORMAL
] > 0)
3656 print_quality
= _PWG_PRINT_QUALITY_NORMAL
;
3657 else if (pc
->num_presets
[_PWG_PRINT_COLOR_MODE_COLOR
][print_quality
] > 0)
3658 print_color_mode
= _PWG_PRINT_COLOR_MODE_COLOR
;
3661 print_quality
= _PWG_PRINT_QUALITY_NORMAL
;
3662 print_color_mode
= _PWG_PRINT_COLOR_MODE_COLOR
;
3666 if (pc
->num_presets
[print_color_mode
][print_quality
] > 0)
3669 * Copy the preset options as long as the corresponding names are not
3670 * already defined in the IPP request...
3673 for (i
= pc
->num_presets
[print_color_mode
][print_quality
],
3674 preset
= pc
->presets
[print_color_mode
][print_quality
];
3678 if (!ippFindAttribute(job
->attrs
, preset
->name
, IPP_TAG_ZERO
))
3679 num_pwgppds
= cupsAddOption(preset
->name
, preset
->value
, num_pwgppds
,
3687 if (!ippFindAttribute(job
->attrs
, "InputSlot", IPP_TAG_ZERO
) &&
3688 !ippFindAttribute(job
->attrs
, "HPPaperSource", IPP_TAG_ZERO
))
3690 if ((ppd
= _ppdCacheGetInputSlot(pc
, job
->attrs
, NULL
)) != NULL
)
3691 num_pwgppds
= cupsAddOption(pc
->source_option
, ppd
, num_pwgppds
,
3694 if (!ippFindAttribute(job
->attrs
, "MediaType", IPP_TAG_ZERO
) &&
3695 (ppd
= _ppdCacheGetMediaType(pc
, job
->attrs
, NULL
)) != NULL
)
3696 num_pwgppds
= cupsAddOption("MediaType", ppd
, num_pwgppds
, &pwgppds
);
3698 if (!ippFindAttribute(job
->attrs
, "PageRegion", IPP_TAG_ZERO
) &&
3699 !ippFindAttribute(job
->attrs
, "PageSize", IPP_TAG_ZERO
) &&
3700 (ppd
= _ppdCacheGetPageSize(pc
, job
->attrs
, NULL
, &exact
)) != NULL
)
3702 num_pwgppds
= cupsAddOption("PageSize", ppd
, num_pwgppds
, &pwgppds
);
3704 if (!ippFindAttribute(job
->attrs
, "media", IPP_TAG_ZERO
))
3705 num_pwgppds
= cupsAddOption("media", ppd
, num_pwgppds
, &pwgppds
);
3708 if (!ippFindAttribute(job
->attrs
, "OutputBin", IPP_TAG_ZERO
) &&
3709 (attr
= ippFindAttribute(job
->attrs
, "output-bin",
3710 IPP_TAG_ZERO
)) != NULL
&&
3711 (attr
->value_tag
== IPP_TAG_KEYWORD
||
3712 attr
->value_tag
== IPP_TAG_NAME
) &&
3713 (ppd
= _ppdCacheGetOutputBin(pc
, attr
->values
[0].string
.text
)) != NULL
)
3716 * Map output-bin to OutputBin option...
3719 num_pwgppds
= cupsAddOption("OutputBin", ppd
, num_pwgppds
, &pwgppds
);
3722 if (pc
->sides_option
&&
3723 !ippFindAttribute(job
->attrs
, pc
->sides_option
, IPP_TAG_ZERO
) &&
3724 (attr
= ippFindAttribute(job
->attrs
, "sides", IPP_TAG_KEYWORD
)) != NULL
)
3727 * Map sides to duplex option...
3730 if (!strcmp(attr
->values
[0].string
.text
, "one-sided"))
3731 num_pwgppds
= cupsAddOption(pc
->sides_option
, pc
->sides_1sided
,
3732 num_pwgppds
, &pwgppds
);
3733 else if (!strcmp(attr
->values
[0].string
.text
, "two-sided-long-edge"))
3734 num_pwgppds
= cupsAddOption(pc
->sides_option
, pc
->sides_2sided_long
,
3735 num_pwgppds
, &pwgppds
);
3736 else if (!strcmp(attr
->values
[0].string
.text
, "two-sided-short-edge"))
3737 num_pwgppds
= cupsAddOption(pc
->sides_option
, pc
->sides_2sided_short
,
3738 num_pwgppds
, &pwgppds
);
3742 * Map finishings values...
3745 num_pwgppds
= _ppdCacheGetFinishingOptions(pc
, job
->attrs
,
3746 IPP_FINISHINGS_NONE
, num_pwgppds
,
3751 * Figure out how much room we need...
3754 newlength
= ipp_length(job
->attrs
);
3756 for (i
= num_pwgppds
, pwgppd
= pwgppds
; i
> 0; i
--, pwgppd
++)
3757 newlength
+= 1 + strlen(pwgppd
->name
) + 1 + strlen(pwgppd
->value
);
3760 * Then allocate/reallocate the option buffer as needed...
3763 if (newlength
== 0) /* This can never happen, but Clang */
3764 newlength
= 1; /* thinks it can... */
3766 if (newlength
> optlength
|| !options
)
3769 optptr
= malloc(newlength
);
3771 optptr
= realloc(options
, newlength
);
3775 cupsdLogJob(job
, CUPSD_LOG_CRIT
,
3776 "Unable to allocate " CUPS_LLFMT
" bytes for option buffer.",
3777 CUPS_LLCAST newlength
);
3782 optlength
= newlength
;
3786 * Now loop through the attributes and convert them to the textual
3787 * representation used by the filters...
3793 snprintf(title
, title_size
, "%s-%d", job
->printer
->name
, job
->id
);
3794 strlcpy(copies
, "1", copies_size
);
3796 for (attr
= job
->attrs
->attrs
; attr
!= NULL
; attr
= attr
->next
)
3798 if (!strcmp(attr
->name
, "copies") &&
3799 attr
->value_tag
== IPP_TAG_INTEGER
)
3802 * Don't use the # copies attribute if we are printing the job sheets...
3806 snprintf(copies
, copies_size
, "%d", attr
->values
[0].integer
);
3808 else if (!strcmp(attr
->name
, "job-name") &&
3809 (attr
->value_tag
== IPP_TAG_NAME
||
3810 attr
->value_tag
== IPP_TAG_NAMELANG
))
3811 strlcpy(title
, attr
->values
[0].string
.text
, title_size
);
3812 else if (attr
->group_tag
== IPP_TAG_JOB
)
3815 * Filter out other unwanted attributes...
3818 if (attr
->value_tag
== IPP_TAG_NOVALUE
||
3819 attr
->value_tag
== IPP_TAG_MIMETYPE
||
3820 attr
->value_tag
== IPP_TAG_NAMELANG
||
3821 attr
->value_tag
== IPP_TAG_TEXTLANG
||
3822 (attr
->value_tag
== IPP_TAG_URI
&& strcmp(attr
->name
, "job-uuid") &&
3823 strcmp(attr
->name
, "job-authorization-uri")) ||
3824 attr
->value_tag
== IPP_TAG_URISCHEME
||
3825 attr
->value_tag
== IPP_TAG_BEGIN_COLLECTION
) /* Not yet supported */
3828 if (!strcmp(attr
->name
, "job-hold-until") ||
3829 !strcmp(attr
->name
, "job-id") ||
3830 !strcmp(attr
->name
, "job-k-octets") ||
3831 !strcmp(attr
->name
, "job-media-sheets") ||
3832 !strcmp(attr
->name
, "job-media-sheets-completed") ||
3833 !strcmp(attr
->name
, "job-state") ||
3834 !strcmp(attr
->name
, "job-state-reasons"))
3837 if (!strncmp(attr
->name
, "job-", 4) &&
3838 strcmp(attr
->name
, "job-account-id") &&
3839 strcmp(attr
->name
, "job-accounting-user-id") &&
3840 strcmp(attr
->name
, "job-authorization-uri") &&
3841 strcmp(attr
->name
, "job-billing") &&
3842 strcmp(attr
->name
, "job-impressions") &&
3843 strcmp(attr
->name
, "job-originating-host-name") &&
3844 strcmp(attr
->name
, "job-password") &&
3845 strcmp(attr
->name
, "job-password-encryption") &&
3846 strcmp(attr
->name
, "job-uuid") &&
3847 !(job
->printer
->type
& CUPS_PRINTER_REMOTE
))
3850 if ((!strcmp(attr
->name
, "job-impressions") ||
3851 !strcmp(attr
->name
, "page-label") ||
3852 !strcmp(attr
->name
, "page-border") ||
3853 !strncmp(attr
->name
, "number-up", 9) ||
3854 !strcmp(attr
->name
, "page-ranges") ||
3855 !strcmp(attr
->name
, "page-set") ||
3856 !_cups_strcasecmp(attr
->name
, "AP_FIRSTPAGE_InputSlot") ||
3857 !_cups_strcasecmp(attr
->name
, "AP_FIRSTPAGE_ManualFeed") ||
3858 !_cups_strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
3859 "PMTotalSidesImaged..n.") ||
3860 !_cups_strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
3861 "PMTotalBeginPages..n.")) &&
3866 * Otherwise add them to the list...
3869 if (optptr
> options
)
3870 strlcat(optptr
, " ", optlength
- (size_t)(optptr
- options
));
3872 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
3874 strlcat(optptr
, attr
->name
, optlength
- (size_t)(optptr
- options
));
3875 strlcat(optptr
, "=", optlength
- (size_t)(optptr
- options
));
3878 for (i
= 0; i
< attr
->num_values
; i
++)
3881 strlcat(optptr
, ",", optlength
- (size_t)(optptr
- options
));
3883 optptr
+= strlen(optptr
);
3885 switch (attr
->value_tag
)
3887 case IPP_TAG_INTEGER
:
3889 snprintf(optptr
, optlength
- (size_t)(optptr
- options
),
3890 "%d", attr
->values
[i
].integer
);
3893 case IPP_TAG_BOOLEAN
:
3894 if (!attr
->values
[i
].boolean
)
3895 strlcat(optptr
, "no", optlength
- (size_t)(optptr
- options
));
3897 strlcat(optptr
, attr
->name
, optlength
- (size_t)(optptr
- options
));
3900 case IPP_TAG_RANGE
:
3901 if (attr
->values
[i
].range
.lower
== attr
->values
[i
].range
.upper
)
3902 snprintf(optptr
, optlength
- (size_t)(optptr
- options
) - 1,
3903 "%d", attr
->values
[i
].range
.lower
);
3905 snprintf(optptr
, optlength
- (size_t)(optptr
- options
) - 1,
3906 "%d-%d", attr
->values
[i
].range
.lower
,
3907 attr
->values
[i
].range
.upper
);
3910 case IPP_TAG_RESOLUTION
:
3911 snprintf(optptr
, optlength
- (size_t)(optptr
- options
) - 1,
3912 "%dx%d%s", attr
->values
[i
].resolution
.xres
,
3913 attr
->values
[i
].resolution
.yres
,
3914 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
3918 case IPP_TAG_STRING
:
3921 case IPP_TAG_KEYWORD
:
3922 case IPP_TAG_CHARSET
:
3923 case IPP_TAG_LANGUAGE
:
3925 for (valptr
= attr
->values
[i
].string
.text
; *valptr
;)
3927 if (strchr(" \t\n\\\'\"", *valptr
))
3929 *optptr
++ = *valptr
++;
3936 break; /* anti-compiler-warning-code */
3940 optptr
+= strlen(optptr
);
3945 * Finally loop through the PWG->PPD mapped options and add them...
3948 for (i
= num_pwgppds
, pwgppd
= pwgppds
; i
> 0; i
--, pwgppd
++)
3951 strlcpy(optptr
, pwgppd
->name
, optlength
- (size_t)(optptr
- options
));
3952 optptr
+= strlen(optptr
);
3954 strlcpy(optptr
, pwgppd
->value
, optlength
- (size_t)(optptr
- options
));
3955 optptr
+= strlen(optptr
);
3958 cupsFreeOptions(num_pwgppds
, pwgppds
);
3961 * Return the options string...
3969 * 'ipp_length()' - Compute the size of the buffer needed to hold
3970 * the textual IPP attributes.
3973 static size_t /* O - Size of attribute buffer */
3974 ipp_length(ipp_t
*ipp
) /* I - IPP request */
3976 size_t bytes
; /* Number of bytes */
3977 int i
; /* Looping var */
3978 ipp_attribute_t
*attr
; /* Current attribute */
3982 * Loop through all attributes...
3987 for (attr
= ipp
->attrs
; attr
!= NULL
; attr
= attr
->next
)
3990 * Skip attributes that won't be sent to filters...
3993 if (attr
->value_tag
== IPP_TAG_NOVALUE
||
3994 attr
->value_tag
== IPP_TAG_MIMETYPE
||
3995 attr
->value_tag
== IPP_TAG_NAMELANG
||
3996 attr
->value_tag
== IPP_TAG_TEXTLANG
||
3997 attr
->value_tag
== IPP_TAG_URI
||
3998 attr
->value_tag
== IPP_TAG_URISCHEME
)
4002 * Add space for a leading space and commas between each value.
4003 * For the first attribute, the leading space isn't used, so the
4004 * extra byte can be used as the nul terminator...
4007 bytes
++; /* " " separator */
4008 bytes
+= (size_t)attr
->num_values
; /* "," separators */
4011 * Boolean attributes appear as "foo,nofoo,foo,nofoo", while
4012 * other attributes appear as "foo=value1,value2,...,valueN".
4015 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
4016 bytes
+= strlen(attr
->name
);
4018 bytes
+= (size_t)attr
->num_values
* strlen(attr
->name
);
4021 * Now add the size required for each value in the attribute...
4024 switch (attr
->value_tag
)
4026 case IPP_TAG_INTEGER
:
4029 * Minimum value of a signed integer is -2147483647, or 11 digits.
4032 bytes
+= (size_t)attr
->num_values
* 11;
4035 case IPP_TAG_BOOLEAN
:
4037 * Add two bytes for each false ("no") value...
4040 for (i
= 0; i
< attr
->num_values
; i
++)
4041 if (!attr
->values
[i
].boolean
)
4045 case IPP_TAG_RANGE
:
4047 * A range is two signed integers separated by a hyphen, or
4048 * 23 characters max.
4051 bytes
+= (size_t)attr
->num_values
* 23;
4054 case IPP_TAG_RESOLUTION
:
4056 * A resolution is two signed integers separated by an "x" and
4057 * suffixed by the units, or 26 characters max.
4060 bytes
+= (size_t)attr
->num_values
* 26;
4063 case IPP_TAG_STRING
:
4066 case IPP_TAG_KEYWORD
:
4067 case IPP_TAG_CHARSET
:
4068 case IPP_TAG_LANGUAGE
:
4071 * Strings can contain characters that need quoting. We need
4072 * at least 2 * len + 2 characters to cover the quotes and
4073 * any backslashes in the string.
4076 for (i
= 0; i
< attr
->num_values
; i
++)
4077 bytes
+= 2 * strlen(attr
->values
[i
].string
.text
) + 2;
4081 break; /* anti-compiler-warning-code */
4090 * 'load_job_cache()' - Load jobs from the job.cache file.
4094 load_job_cache(const char *filename
) /* I - job.cache filename */
4096 cups_file_t
*fp
; /* job.cache file */
4097 char line
[1024], /* Line buffer */
4098 *value
; /* Value on line */
4099 int linenum
; /* Line number in file */
4100 cupsd_job_t
*job
; /* Current job */
4101 int jobid
; /* Job ID */
4102 char jobfile
[1024]; /* Job filename */
4106 * Open the job.cache file...
4109 if ((fp
= cupsdOpenConfFile(filename
)) == NULL
)
4111 load_request_root();
4116 * Read entries from the job cache file and create jobs as needed.
4119 cupsdLogMessage(CUPSD_LOG_INFO
, "Loading job cache file \"%s\"...",
4125 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
4127 if (!_cups_strcasecmp(line
, "NextJobId"))
4130 NextJobId
= atoi(value
);
4132 else if (!_cups_strcasecmp(line
, "<Job"))
4136 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing </Job> directive on line %d of %s.", linenum
, filename
);
4142 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing job ID on line %d of %s.", linenum
, filename
);
4146 jobid
= atoi(value
);
4150 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad job ID %d on line %d of %s.", jobid
, linenum
, filename
);
4154 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, jobid
);
4155 if (access(jobfile
, 0))
4157 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d.N", RequestRoot
, jobid
);
4158 if (access(jobfile
, 0))
4160 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Job %d] Files have gone away.",
4164 * job.cache file is out-of-date compared to spool directory; load
4169 load_request_root();
4174 job
= calloc(1, sizeof(cupsd_job_t
));
4177 cupsdLogMessage(CUPSD_LOG_EMERG
,
4178 "[Job %d] Unable to allocate memory for job.", jobid
);
4183 job
->back_pipes
[0] = -1;
4184 job
->back_pipes
[1] = -1;
4185 job
->print_pipes
[0] = -1;
4186 job
->print_pipes
[1] = -1;
4187 job
->side_pipes
[0] = -1;
4188 job
->side_pipes
[1] = -1;
4189 job
->status_pipes
[0] = -1;
4190 job
->status_pipes
[1] = -1;
4192 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Loading from cache...");
4196 cupsdLogMessage(CUPSD_LOG_ERROR
,
4197 "Missing <Job #> directive on line %d of %s.", linenum
, filename
);
4200 else if (!_cups_strcasecmp(line
, "</Job>"))
4202 cupsArrayAdd(Jobs
, job
);
4204 if (job
->state_value
<= IPP_JOB_STOPPED
&& cupsdLoadJob(job
))
4205 cupsArrayAdd(ActiveJobs
, job
);
4206 else if (job
->state_value
> IPP_JOB_STOPPED
)
4208 if (!job
->completed_time
|| !job
->creation_time
|| !job
->name
|| !job
->koctets
)
4219 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing value on line %d of %s.", linenum
, filename
);
4222 else if (!_cups_strcasecmp(line
, "State"))
4224 job
->state_value
= (ipp_jstate_t
)atoi(value
);
4226 if (job
->state_value
< IPP_JOB_PENDING
)
4227 job
->state_value
= IPP_JOB_PENDING
;
4228 else if (job
->state_value
> IPP_JOB_COMPLETED
)
4229 job
->state_value
= IPP_JOB_COMPLETED
;
4231 else if (!_cups_strcasecmp(line
, "Name"))
4233 cupsdSetString(&(job
->name
), value
);
4235 else if (!_cups_strcasecmp(line
, "Created"))
4237 job
->creation_time
= strtol(value
, NULL
, 10);
4239 else if (!_cups_strcasecmp(line
, "Completed"))
4241 job
->completed_time
= strtol(value
, NULL
, 10);
4243 else if (!_cups_strcasecmp(line
, "HoldUntil"))
4245 job
->hold_until
= strtol(value
, NULL
, 10);
4247 else if (!_cups_strcasecmp(line
, "Priority"))
4249 job
->priority
= atoi(value
);
4251 else if (!_cups_strcasecmp(line
, "Username"))
4253 cupsdSetString(&job
->username
, value
);
4255 else if (!_cups_strcasecmp(line
, "Destination"))
4257 cupsdSetString(&job
->dest
, value
);
4259 else if (!_cups_strcasecmp(line
, "DestType"))
4261 job
->dtype
= (cups_ptype_t
)atoi(value
);
4263 else if (!_cups_strcasecmp(line
, "KOctets"))
4265 job
->koctets
= atoi(value
);
4267 else if (!_cups_strcasecmp(line
, "NumFiles"))
4269 job
->num_files
= atoi(value
);
4271 if (job
->num_files
< 0)
4273 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad NumFiles value %d on line %d of %s.", job
->num_files
, linenum
, filename
);
4278 if (job
->num_files
> 0)
4280 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-001", RequestRoot
,
4282 if (access(jobfile
, 0))
4284 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Data files have gone away.");
4289 job
->filetypes
= calloc((size_t)job
->num_files
, sizeof(mime_type_t
*));
4290 job
->compressions
= calloc((size_t)job
->num_files
, sizeof(int));
4292 if (!job
->filetypes
|| !job
->compressions
)
4294 cupsdLogJob(job
, CUPSD_LOG_EMERG
,
4295 "Unable to allocate memory for %d files.",
4301 else if (!_cups_strcasecmp(line
, "File"))
4303 int number
, /* File number */
4304 compression
; /* Compression value */
4305 char super
[MIME_MAX_SUPER
], /* MIME super type */
4306 type
[MIME_MAX_TYPE
]; /* MIME type */
4309 if (sscanf(value
, "%d%*[ \t]%15[^/]/%255s%d", &number
, super
, type
,
4312 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File on line %d of %s.", linenum
, filename
);
4316 if (number
< 1 || number
> job
->num_files
)
4318 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File number %d on line %d of %s.", number
, linenum
, filename
);
4324 job
->compressions
[number
] = compression
;
4325 job
->filetypes
[number
] = mimeType(MimeDatabase
, super
, type
);
4327 if (!job
->filetypes
[number
])
4330 * If the original MIME type is unknown, auto-type it!
4333 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
4334 "Unknown MIME type %s/%s for file %d.",
4335 super
, type
, number
+ 1);
4337 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
4338 job
->id
, number
+ 1);
4339 job
->filetypes
[number
] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
4340 job
->compressions
+ number
);
4343 * If that didn't work, assume it is raw...
4346 if (!job
->filetypes
[number
])
4347 job
->filetypes
[number
] = mimeType(MimeDatabase
, "application",
4352 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unknown %s directive on line %d of %s.", line
, linenum
, filename
);
4357 cupsdLogMessage(CUPSD_LOG_ERROR
,
4358 "Missing </Job> directive on line %d of %s.", linenum
, filename
);
4359 cupsdDeleteJob(job
, CUPSD_JOB_PURGE
);
4367 * 'load_next_job_id()' - Load the NextJobId value from the job.cache file.
4371 load_next_job_id(const char *filename
) /* I - job.cache filename */
4373 cups_file_t
*fp
; /* job.cache file */
4374 char line
[1024], /* Line buffer */
4375 *value
; /* Value on line */
4376 int linenum
; /* Line number in file */
4377 int next_job_id
; /* NextJobId value from line */
4381 * Read the NextJobId directive from the job.cache file and use
4382 * the value (if any).
4385 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
4387 if (errno
!= ENOENT
)
4388 cupsdLogMessage(CUPSD_LOG_ERROR
,
4389 "Unable to open job cache file \"%s\": %s",
4390 filename
, strerror(errno
));
4395 cupsdLogMessage(CUPSD_LOG_INFO
,
4396 "Loading NextJobId from job cache file \"%s\"...", filename
);
4400 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
4402 if (!_cups_strcasecmp(line
, "NextJobId"))
4406 next_job_id
= atoi(value
);
4408 if (next_job_id
> NextJobId
)
4409 NextJobId
= next_job_id
;
4420 * 'load_request_root()' - Load jobs from the RequestRoot directory.
4424 load_request_root(void)
4426 cups_dir_t
*dir
; /* Directory */
4427 cups_dentry_t
*dent
; /* Directory entry */
4428 cupsd_job_t
*job
; /* New job */
4432 * Open the requests directory...
4435 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Scanning %s for jobs...", RequestRoot
);
4437 if ((dir
= cupsDirOpen(RequestRoot
)) == NULL
)
4439 cupsdLogMessage(CUPSD_LOG_ERROR
,
4440 "Unable to open spool directory \"%s\": %s",
4441 RequestRoot
, strerror(errno
));
4446 * Read all the c##### files...
4449 while ((dent
= cupsDirRead(dir
)) != NULL
)
4450 if (strlen(dent
->filename
) >= 6 && dent
->filename
[0] == 'c')
4453 * Allocate memory for the job...
4456 if ((job
= calloc(sizeof(cupsd_job_t
), 1)) == NULL
)
4458 cupsdLogMessage(CUPSD_LOG_ERROR
, "Ran out of memory for jobs.");
4464 * Assign the job ID...
4467 job
->id
= atoi(dent
->filename
+ 1);
4468 job
->back_pipes
[0] = -1;
4469 job
->back_pipes
[1] = -1;
4470 job
->print_pipes
[0] = -1;
4471 job
->print_pipes
[1] = -1;
4472 job
->side_pipes
[0] = -1;
4473 job
->side_pipes
[1] = -1;
4474 job
->status_pipes
[0] = -1;
4475 job
->status_pipes
[1] = -1;
4477 if (job
->id
>= NextJobId
)
4478 NextJobId
= job
->id
+ 1;
4484 if (cupsdLoadJob(job
))
4487 * Insert the job into the array, sorting by job priority and ID...
4490 cupsArrayAdd(Jobs
, job
);
4492 if (job
->state_value
<= IPP_JOB_STOPPED
)
4493 cupsArrayAdd(ActiveJobs
, job
);
4506 * 'remove_job_files()' - Remove the document files for a job.
4510 remove_job_files(cupsd_job_t
*job
) /* I - Job */
4512 int i
; /* Looping var */
4513 char filename
[1024]; /* Document filename */
4516 if (job
->num_files
<= 0)
4519 for (i
= 1; i
<= job
->num_files
; i
++)
4521 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
4523 cupsdUnlinkOrRemoveFile(filename
);
4526 free(job
->filetypes
);
4527 free(job
->compressions
);
4531 job
->filetypes
= NULL
;
4532 job
->compressions
= NULL
;
4534 LastEvent
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
4539 * 'remove_job_history()' - Remove the control file for a job.
4543 remove_job_history(cupsd_job_t
*job
) /* I - Job */
4545 char filename
[1024]; /* Control filename */
4549 * Remove the job info file...
4552 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
,
4554 cupsdUnlinkOrRemoveFile(filename
);
4556 LastEvent
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
4561 * 'set_time()' - Set one of the "time-at-xyz" attributes.
4565 set_time(cupsd_job_t
*job
, /* I - Job to update */
4566 const char *name
) /* I - Name of attribute */
4568 char date_name
[128]; /* date-time-at-xxx */
4569 ipp_attribute_t
*attr
; /* Time attribute */
4570 time_t curtime
; /* Current time */
4573 curtime
= time(NULL
);
4575 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "%s=%ld", name
, (long)curtime
);
4577 if ((attr
= ippFindAttribute(job
->attrs
, name
, IPP_TAG_ZERO
)) != NULL
)
4579 attr
->value_tag
= IPP_TAG_INTEGER
;
4580 attr
->values
[0].integer
= curtime
;
4583 snprintf(date_name
, sizeof(date_name
), "date-%s", name
);
4585 if ((attr
= ippFindAttribute(job
->attrs
, date_name
, IPP_TAG_ZERO
)) != NULL
)
4587 attr
->value_tag
= IPP_TAG_DATE
;
4588 ippSetDate(job
->attrs
, &attr
, 0, ippTimeToDate(curtime
));
4591 if (!strcmp(name
, "time-at-completed"))
4593 job
->completed_time
= curtime
;
4595 if (JobHistory
< INT_MAX
&& attr
)
4596 job
->history_time
= attr
->values
[0].integer
+ JobHistory
;
4598 job
->history_time
= INT_MAX
;
4600 if (job
->history_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
4601 JobHistoryUpdate
= job
->history_time
;
4603 if (JobFiles
< INT_MAX
&& attr
)
4604 job
->file_time
= attr
->values
[0].integer
+ JobFiles
;
4606 job
->file_time
= INT_MAX
;
4608 if (job
->file_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
4609 JobHistoryUpdate
= job
->file_time
;
4611 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_time: JobHistoryUpdate=%ld",
4612 (long)JobHistoryUpdate
);
4618 * 'start_job()' - Start a print job.
4622 start_job(cupsd_job_t
*job
, /* I - Job ID */
4623 cupsd_printer_t
*printer
) /* I - Printer to print job */
4625 const char *filename
; /* Support filename */
4626 ipp_attribute_t
*cancel_after
= ippFindAttribute(job
->attrs
,
4629 /* job-cancel-after attribute */
4632 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "start_job(job=%p(%d), printer=%p(%s))",
4633 job
, job
->id
, printer
, printer
->name
);
4636 * Make sure we have some files around before we try to print...
4639 if (job
->num_files
== 0)
4641 ippSetString(job
->attrs
, &job
->reasons
, 0, "aborted-by-system");
4642 cupsdSetJobState(job
, IPP_JOB_ABORTED
, CUPSD_JOB_DEFAULT
,
4643 "Aborting job because it has no files.");
4648 * Update the printer and job state to "processing"...
4651 if (!cupsdLoadJob(job
))
4654 if (!job
->printer_message
)
4655 job
->printer_message
= ippFindAttribute(job
->attrs
,
4656 "job-printer-state-message",
4658 if (job
->printer_message
)
4659 ippSetString(job
->attrs
, &job
->printer_message
, 0, "");
4661 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-printing");
4662 cupsdSetJobState(job
, IPP_JOB_PROCESSING
, CUPSD_JOB_DEFAULT
, NULL
);
4663 cupsdSetPrinterState(printer
, IPP_PRINTER_PROCESSING
, 0);
4664 cupsdSetPrinterReasons(printer
, "-cups-remote-pending,"
4665 "cups-remote-pending-held,"
4666 "cups-remote-processing,"
4667 "cups-remote-stopped,"
4668 "cups-remote-canceled,"
4669 "cups-remote-aborted,"
4670 "cups-remote-completed");
4673 job
->current_file
= 0;
4675 job
->history_time
= 0;
4677 job
->printer
= printer
;
4681 job
->cancel_time
= time(NULL
) + ippGetInteger(cancel_after
, 0);
4682 else if (MaxJobTime
> 0)
4683 job
->cancel_time
= time(NULL
) + MaxJobTime
;
4685 job
->cancel_time
= 0;
4688 * Check for support files...
4691 cupsdSetPrinterReasons(job
->printer
, "-cups-missing-filter-warning,"
4692 "cups-insecure-filter-warning");
4696 for (filename
= (const char *)cupsArrayFirst(printer
->pc
->support_files
);
4698 filename
= (const char *)cupsArrayNext(printer
->pc
->support_files
))
4700 if (_cupsFileCheck(filename
, _CUPS_FILE_CHECK_FILE
, !RunUser
,
4701 cupsdLogFCMessage
, printer
))
4707 * Setup the last exit status and security profiles...
4711 job
->profile
= cupsdCreateProfile(job
->id
, 0);
4712 job
->bprofile
= cupsdCreateProfile(job
->id
, 1);
4715 * Create the status pipes and buffer...
4718 if (cupsdOpenPipe(job
->status_pipes
))
4720 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
4721 "Unable to create job status pipes - %s.", strerror(errno
));
4723 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
4724 "Job stopped because the scheduler could not create the "
4725 "job status pipes.");
4727 cupsdDestroyProfile(job
->profile
);
4728 job
->profile
= NULL
;
4729 cupsdDestroyProfile(job
->bprofile
);
4730 job
->bprofile
= NULL
;
4734 job
->status_buffer
= cupsdStatBufNew(job
->status_pipes
[0], NULL
);
4735 job
->status_level
= CUPSD_LOG_INFO
;
4738 * Create the backchannel pipes and make them non-blocking...
4741 if (cupsdOpenPipe(job
->back_pipes
))
4743 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
4744 "Unable to create back-channel pipes - %s.", strerror(errno
));
4746 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
4747 "Job stopped because the scheduler could not create the "
4748 "back-channel pipes.");
4750 cupsdClosePipe(job
->status_pipes
);
4751 cupsdStatBufDelete(job
->status_buffer
);
4752 job
->status_buffer
= NULL
;
4754 cupsdDestroyProfile(job
->profile
);
4755 job
->profile
= NULL
;
4756 cupsdDestroyProfile(job
->bprofile
);
4757 job
->bprofile
= NULL
;
4761 fcntl(job
->back_pipes
[0], F_SETFL
,
4762 fcntl(job
->back_pipes
[0], F_GETFL
) | O_NONBLOCK
);
4763 fcntl(job
->back_pipes
[1], F_SETFL
,
4764 fcntl(job
->back_pipes
[1], F_GETFL
) | O_NONBLOCK
);
4767 * Create the side-channel pipes and make them non-blocking...
4770 if (socketpair(AF_LOCAL
, SOCK_STREAM
, 0, job
->side_pipes
))
4772 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
4773 "Unable to create side-channel pipes - %s.", strerror(errno
));
4775 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
4776 "Job stopped because the scheduler could not create the "
4777 "side-channel pipes.");
4779 cupsdClosePipe(job
->back_pipes
);
4781 cupsdClosePipe(job
->status_pipes
);
4782 cupsdStatBufDelete(job
->status_buffer
);
4783 job
->status_buffer
= NULL
;
4785 cupsdDestroyProfile(job
->profile
);
4786 job
->profile
= NULL
;
4787 cupsdDestroyProfile(job
->bprofile
);
4788 job
->bprofile
= NULL
;
4792 fcntl(job
->side_pipes
[0], F_SETFL
,
4793 fcntl(job
->side_pipes
[0], F_GETFL
) | O_NONBLOCK
);
4794 fcntl(job
->side_pipes
[1], F_SETFL
,
4795 fcntl(job
->side_pipes
[1], F_GETFL
) | O_NONBLOCK
);
4797 fcntl(job
->side_pipes
[0], F_SETFD
,
4798 fcntl(job
->side_pipes
[0], F_GETFD
) | FD_CLOEXEC
);
4799 fcntl(job
->side_pipes
[1], F_SETFD
,
4800 fcntl(job
->side_pipes
[1], F_GETFD
) | FD_CLOEXEC
);
4803 * Now start the first file in the job...
4806 cupsdContinueJob(job
);
4811 * 'stop_job()' - Stop a print job.
4815 stop_job(cupsd_job_t
*job
, /* I - Job */
4816 cupsd_jobaction_t action
) /* I - Action */
4818 int i
; /* Looping var */
4821 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "stop_job(job=%p(%d), action=%d)", job
,
4824 FilterLevel
-= job
->cost
;
4827 if (action
== CUPSD_JOB_DEFAULT
&& !job
->kill_time
&& job
->backend
> 0)
4828 job
->kill_time
= time(NULL
) + JobKillDelay
;
4829 else if (action
>= CUPSD_JOB_FORCE
)
4832 for (i
= 0; job
->filters
[i
]; i
++)
4833 if (job
->filters
[i
] > 0)
4835 cupsdEndProcess(job
->filters
[i
], action
>= CUPSD_JOB_FORCE
);
4837 if (action
>= CUPSD_JOB_FORCE
)
4838 job
->filters
[i
] = -job
->filters
[i
];
4841 if (job
->backend
> 0)
4843 cupsdEndProcess(job
->backend
, action
>= CUPSD_JOB_FORCE
);
4845 if (action
>= CUPSD_JOB_FORCE
)
4846 job
->backend
= -job
->backend
;
4849 if (action
>= CUPSD_JOB_FORCE
)
4852 * Clear job status...
4861 * 'unload_job()' - Unload a job from memory.
4865 unload_job(cupsd_job_t
*job
) /* I - Job */
4870 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Unloading...");
4872 ippDelete(job
->attrs
);
4876 job
->reasons
= NULL
;
4877 job
->impressions
= NULL
;
4879 job
->job_sheets
= NULL
;
4880 job
->printer_message
= NULL
;
4881 job
->printer_reasons
= NULL
;
4886 * 'update_job()' - Read a status update from a job's filters.
4890 update_job(cupsd_job_t
*job
) /* I - Job to check */
4892 int i
; /* Looping var */
4893 int copies
; /* Number of copies printed */
4894 char message
[CUPSD_SB_BUFFER_SIZE
],
4896 *ptr
; /* Pointer update... */
4897 int loglevel
, /* Log level for message */
4898 event
= 0; /* Events? */
4899 cupsd_printer_t
*printer
= job
->printer
;
4901 static const char * const levels
[] = /* Log levels */
4917 * Get the printer associated with this job; if the printer is stopped for
4918 * any reason then job->printer will be reset to NULL, so make sure we have
4919 * a valid pointer...
4922 while ((ptr
= cupsdStatBufUpdate(job
->status_buffer
, &loglevel
,
4923 message
, sizeof(message
))) != NULL
)
4926 * Process page and printer state messages as needed...
4929 if (loglevel
== CUPSD_LOG_PAGE
)
4932 * Page message; send the message to the page_log file and update the
4933 * job sheet count...
4936 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "PAGE: %s", message
);
4938 if (job
->impressions
)
4940 if (!_cups_strncasecmp(message
, "total ", 6))
4943 * Got a total count of pages from a backend or filter...
4946 copies
= atoi(message
+ 6);
4947 copies
-= ippGetInteger(job
->impressions
, 0); /* Just track the delta */
4949 else if (!sscanf(message
, "%*d%d", &copies
))
4952 ippSetInteger(job
->attrs
, &job
->impressions
, 0, ippGetInteger(job
->impressions
, 0) + copies
);
4954 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
4959 if (!_cups_strncasecmp(message
, "total ", 6))
4962 * Got a total count of pages from a backend or filter...
4965 copies
= atoi(message
+ 6);
4966 copies
-= ippGetInteger(job
->sheets
, 0); /* Just track the delta */
4968 else if (!sscanf(message
, "%*d%d", &copies
))
4971 ippSetInteger(job
->attrs
, &job
->sheets
, 0, ippGetInteger(job
->sheets
, 0) + copies
);
4973 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
4975 if (job
->printer
->page_limit
)
4976 cupsdUpdateQuota(job
->printer
, job
->username
, copies
, 0);
4979 cupsdLogPage(job
, message
);
4982 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
, "Printed %d page(s).", ippGetInteger(job
->sheets
, 0));
4984 else if (loglevel
== CUPSD_LOG_JOBSTATE
)
4987 * Support "keyword" to set job-state-reasons to the specified keyword.
4988 * This is sufficient for the current paid printing stuff.
4991 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "JOBSTATE: %s", message
);
4993 if (!strcmp(message
, "cups-retry-as-raster"))
4994 job
->retry_as_raster
= 1;
4996 ippSetString(job
->attrs
, &job
->reasons
, 0, message
);
4998 else if (loglevel
== CUPSD_LOG_STATE
)
5000 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "STATE: %s", message
);
5002 if (!strcmp(message
, "paused"))
5004 cupsdStopPrinter(job
->printer
, 1);
5007 else if (message
[0] && cupsdSetPrinterReasons(job
->printer
, message
))
5009 event
|= CUPSD_EVENT_PRINTER_STATE
;
5014 * Reset cancel time after connecting to the device...
5017 for (i
= 0; i
< job
->printer
->num_reasons
; i
++)
5018 if (!strcmp(job
->printer
->reasons
[i
], "connecting-to-device"))
5021 if (i
>= job
->printer
->num_reasons
)
5023 ipp_attribute_t
*cancel_after
= ippFindAttribute(job
->attrs
,
5026 /* job-cancel-after attribute */
5029 job
->cancel_time
= time(NULL
) + ippGetInteger(cancel_after
, 0);
5031 job
->cancel_time
= time(NULL
) + MaxJobTime
;
5036 update_job_attrs(job
, 0);
5038 else if (loglevel
== CUPSD_LOG_ATTR
)
5041 * Set attribute(s)...
5044 int num_attrs
; /* Number of attributes */
5045 cups_option_t
*attrs
; /* Attributes */
5046 const char *attr
; /* Attribute */
5048 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "ATTR: %s", message
);
5050 num_attrs
= cupsParseOptions(message
, 0, &attrs
);
5052 if ((attr
= cupsGetOption("auth-info-default", num_attrs
,
5055 job
->printer
->num_options
= cupsAddOption("auth-info", attr
,
5056 job
->printer
->num_options
,
5057 &(job
->printer
->options
));
5058 cupsdSetPrinterAttrs(job
->printer
);
5060 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
5063 if ((attr
= cupsGetOption("auth-info-required", num_attrs
,
5066 cupsdSetAuthInfoRequired(job
->printer
, attr
, NULL
);
5067 cupsdSetPrinterAttrs(job
->printer
);
5069 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
5072 if ((attr
= cupsGetOption("job-media-progress", num_attrs
,
5075 int progress
= atoi(attr
);
5078 if (progress
>= 0 && progress
<= 100)
5080 job
->progress
= progress
;
5083 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
5084 "Printing page %d, %d%%",
5085 job
->sheets
->values
[0].integer
, job
->progress
);
5089 if ((attr
= cupsGetOption("printer-alert", num_attrs
, attrs
)) != NULL
)
5091 cupsdSetString(&job
->printer
->alert
, attr
);
5092 event
|= CUPSD_EVENT_PRINTER_STATE
;
5095 if ((attr
= cupsGetOption("printer-alert-description", num_attrs
,
5098 cupsdSetString(&job
->printer
->alert_description
, attr
);
5099 event
|= CUPSD_EVENT_PRINTER_STATE
;
5102 if ((attr
= cupsGetOption("marker-colors", num_attrs
, attrs
)) != NULL
)
5104 cupsdSetPrinterAttr(job
->printer
, "marker-colors", (char *)attr
);
5105 job
->printer
->marker_time
= time(NULL
);
5106 event
|= CUPSD_EVENT_PRINTER_STATE
;
5107 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
5110 if ((attr
= cupsGetOption("marker-levels", num_attrs
, attrs
)) != NULL
)
5112 cupsdSetPrinterAttr(job
->printer
, "marker-levels", (char *)attr
);
5113 job
->printer
->marker_time
= time(NULL
);
5114 event
|= CUPSD_EVENT_PRINTER_STATE
;
5115 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
5118 if ((attr
= cupsGetOption("marker-low-levels", num_attrs
, attrs
)) != NULL
)
5120 cupsdSetPrinterAttr(job
->printer
, "marker-low-levels", (char *)attr
);
5121 job
->printer
->marker_time
= time(NULL
);
5122 event
|= CUPSD_EVENT_PRINTER_STATE
;
5123 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
5126 if ((attr
= cupsGetOption("marker-high-levels", num_attrs
, attrs
)) != NULL
)
5128 cupsdSetPrinterAttr(job
->printer
, "marker-high-levels", (char *)attr
);
5129 job
->printer
->marker_time
= time(NULL
);
5130 event
|= CUPSD_EVENT_PRINTER_STATE
;
5131 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
5134 if ((attr
= cupsGetOption("marker-message", num_attrs
, attrs
)) != NULL
)
5136 cupsdSetPrinterAttr(job
->printer
, "marker-message", (char *)attr
);
5137 job
->printer
->marker_time
= time(NULL
);
5138 event
|= CUPSD_EVENT_PRINTER_STATE
;
5139 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
5142 if ((attr
= cupsGetOption("marker-names", num_attrs
, attrs
)) != NULL
)
5144 cupsdSetPrinterAttr(job
->printer
, "marker-names", (char *)attr
);
5145 job
->printer
->marker_time
= time(NULL
);
5146 event
|= CUPSD_EVENT_PRINTER_STATE
;
5147 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
5150 if ((attr
= cupsGetOption("marker-types", num_attrs
, attrs
)) != NULL
)
5152 cupsdSetPrinterAttr(job
->printer
, "marker-types", (char *)attr
);
5153 job
->printer
->marker_time
= time(NULL
);
5154 event
|= CUPSD_EVENT_PRINTER_STATE
;
5155 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
5158 cupsFreeOptions(num_attrs
, attrs
);
5160 else if (loglevel
== CUPSD_LOG_PPD
)
5163 * Set attribute(s)...
5166 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "PPD: %s", message
);
5168 job
->num_keywords
= cupsParseOptions(message
, job
->num_keywords
,
5174 * Strip legacy message prefix...
5177 if (!strncmp(message
, "recoverable:", 12))
5180 while (isspace(*ptr
& 255))
5183 else if (!strncmp(message
, "recovered:", 10))
5186 while (isspace(*ptr
& 255))
5193 cupsdLogJob(job
, loglevel
== CUPSD_LOG_INFO
? CUPSD_LOG_DEBUG
: loglevel
, "%s", ptr
);
5195 if (loglevel
< CUPSD_LOG_DEBUG
&&
5196 strcmp(job
->printer
->state_message
, ptr
))
5198 strlcpy(job
->printer
->state_message
, ptr
,
5199 sizeof(job
->printer
->state_message
));
5201 event
|= CUPSD_EVENT_PRINTER_STATE
| CUPSD_EVENT_JOB_PROGRESS
;
5203 if (loglevel
<= job
->status_level
&& job
->status_level
> CUPSD_LOG_ERROR
)
5206 * Some messages show in the job-printer-state-message attribute...
5209 if (loglevel
!= CUPSD_LOG_NOTICE
)
5210 job
->status_level
= loglevel
;
5212 update_job_attrs(job
, 1);
5214 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
5215 "Set job-printer-state-message to \"%s\", "
5217 job
->printer_message
->values
[0].string
.text
,
5218 levels
[job
->status_level
]);
5223 if (!strchr(job
->status_buffer
->buffer
, '\n'))
5227 if (event
& CUPSD_EVENT_JOB_PROGRESS
)
5228 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
5229 "%s", job
->printer
->state_message
);
5230 if (event
& CUPSD_EVENT_PRINTER_STATE
)
5231 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, job
->printer
, NULL
,
5232 (job
->printer
->type
& CUPS_PRINTER_CLASS
) ?
5233 "Class \"%s\" state changed." :
5234 "Printer \"%s\" state changed.",
5235 job
->printer
->name
);
5238 if (ptr
== NULL
&& !job
->status_buffer
->bufused
)
5241 * See if all of the filters and the backend have returned their
5245 for (i
= 0; job
->filters
[i
] < 0; i
++);
5247 if (job
->filters
[i
])
5250 * EOF but we haven't collected the exit status of all filters...
5253 cupsdCheckProcess();
5257 if (job
->current_file
>= job
->num_files
&& job
->backend
> 0)
5260 * EOF but we haven't collected the exit status of the backend...
5263 cupsdCheckProcess();
5268 * Handle the end of job stuff...
5271 finalize_job(job
, 1);
5274 * Try printing another job...
5277 if (printer
->state
!= IPP_PRINTER_STOPPED
)
5284 * 'update_job_attrs()' - Update the job-printer-* attributes.
5288 update_job_attrs(cupsd_job_t
*job
, /* I - Job to update */
5289 int do_message
)/* I - 1 = copy job-printer-state message */
5291 int i
; /* Looping var */
5292 int num_reasons
; /* Actual number of reasons */
5293 const char * const *reasons
; /* Reasons */
5294 static const char *none
= "none"; /* "none" reason */
5298 * Get/create the job-printer-state-* attributes...
5301 if (!job
->printer_message
)
5303 if ((job
->printer_message
= ippFindAttribute(job
->attrs
,
5304 "job-printer-state-message",
5305 IPP_TAG_TEXT
)) == NULL
)
5306 job
->printer_message
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_TEXT
,
5307 "job-printer-state-message",
5311 if (!job
->printer_reasons
)
5312 job
->printer_reasons
= ippFindAttribute(job
->attrs
,
5313 "job-printer-state-reasons",
5317 * Copy or clear the printer-state-message value as needed...
5320 if (job
->state_value
!= IPP_JOB_PROCESSING
&&
5321 job
->status_level
== CUPSD_LOG_INFO
)
5323 ippSetString(job
->attrs
, &job
->printer_message
, 0, "");
5326 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
5328 else if (job
->printer
->state_message
[0] && do_message
)
5330 ippSetString(job
->attrs
, &job
->printer_message
, 0, job
->printer
->state_message
);
5333 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
5337 * ... and the printer-state-reasons value...
5340 if (job
->printer
->num_reasons
== 0)
5347 num_reasons
= job
->printer
->num_reasons
;
5348 reasons
= (const char * const *)job
->printer
->reasons
;
5351 if (!job
->printer_reasons
|| job
->printer_reasons
->num_values
!= num_reasons
)
5354 * Replace/create a job-printer-state-reasons attribute...
5357 ippDeleteAttribute(job
->attrs
, job
->printer_reasons
);
5359 job
->printer_reasons
= ippAddStrings(job
->attrs
,
5360 IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
5361 "job-printer-state-reasons",
5362 num_reasons
, NULL
, NULL
);
5367 * Don't bother clearing the reason strings if they are the same...
5370 for (i
= 0; i
< num_reasons
; i
++)
5371 if (strcmp(job
->printer_reasons
->values
[i
].string
.text
, reasons
[i
]))
5374 if (i
>= num_reasons
)
5378 * Not the same, so free the current strings...
5381 for (i
= 0; i
< num_reasons
; i
++)
5382 _cupsStrFree(job
->printer_reasons
->values
[i
].string
.text
);
5386 * Copy the reasons...
5389 for (i
= 0; i
< num_reasons
; i
++)
5390 job
->printer_reasons
->values
[i
].string
.text
= _cupsStrAlloc(reasons
[i
]);
5393 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);