4 * Job management routines for the CUPS scheduler.
6 * Copyright 2007-2014 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
17 * Include necessary headers...
22 #include <cups/backend.h>
25 # include <IOKit/pwr_mgt/IOPMLib.h>
26 # ifdef HAVE_IOKIT_PWR_MGT_IOPMLIBPRIVATE_H
27 # include <IOKit/pwr_mgt/IOPMLibPrivate.h>
28 # endif /* HAVE_IOKIT_PWR_MGT_IOPMLIBPRIVATE_H */
29 #endif /* __APPLE__ */
33 * Design Notes for Job Management
34 * -------------------------------
38 * pending Do nothing/check jobs
39 * pending-held Send SIGTERM to filters and backend
40 * processing Do nothing/start job
41 * stopped Send SIGKILL to filters and backend
42 * canceled Send SIGTERM to filters and backend
46 * Finalize clears the printer <-> job association, deletes the status
47 * buffer, closes all of the pipes, etc. and doesn't get run until all of
48 * the print processes are finished.
50 * UNLOADING OF JOBS (cupsdUnloadCompletedJobs)
52 * We unload the job attributes when they are not needed to reduce overall
53 * memory consumption. We don't unload jobs where job->state_value <
54 * IPP_JOB_STOPPED, job->printer != NULL, or job->access_time is recent.
56 * STARTING OF JOBS (start_job)
58 * When a job is started, a status buffer, several pipes, a security
59 * profile, and a backend process are created for the life of that job.
60 * These are shared for every file in a job. For remote print jobs, the
61 * IPP backend is provided with every file in the job and no filters are
64 * The job->printer member tracks which printer is printing a job, which
65 * can be different than the destination in job->dest for classes. The
66 * printer object also has a job pointer to track which job is being
69 * PRINTING OF JOB FILES (cupsdContinueJob)
71 * Each file in a job is filtered by 0 or more programs. After getting the
72 * list of filters needed and the total cost, the job is either passed or
73 * put back to the processing state until the current FilterLevel comes down
74 * enough to allow printing.
76 * If we can print, we build a string for the print options and run each of
77 * the filters, piping the output from one into the next.
79 * JOB STATUS UPDATES (update_job)
81 * The update_job function gets called whenever there are pending messages
82 * on the status pipe. These generally are updates to the marker-*,
83 * printer-state-message, or printer-state-reasons attributes. On EOF,
84 * finalize_job is called to clean up.
86 * FINALIZING JOBS (finalize_job)
88 * When all filters and the backend are done, we set the job state to
89 * completed (no errors), aborted (filter errors or abort-job policy),
90 * pending-held (auth required or retry-job policy), or pending
91 * (retry-current-job or stop-printer policies) as appropriate.
93 * Then we close the pipes and free the status buffers and profiles.
95 * JOB FILE COMPLETION (process_children in main.c)
97 * For multiple-file jobs, process_children (in main.c) sees that all
98 * filters have exited and calls in to print the next file if there are
99 * more files in the job, otherwise it waits for the backend to exit and
100 * update_job to do the cleanup.
108 static mime_filter_t gziptoany_filter
=
110 NULL
, /* Source type */
111 NULL
, /* Destination type */
113 "gziptoany" /* Filter program to run */
121 static int compare_active_jobs(void *first
, void *second
, void *data
);
122 static int compare_jobs(void *first
, void *second
, void *data
);
123 static void dump_job_history(cupsd_job_t
*job
);
124 static void finalize_job(cupsd_job_t
*job
, int set_job_state
);
125 static void free_job_history(cupsd_job_t
*job
);
126 static char *get_options(cupsd_job_t
*job
, int banner_page
, char *copies
,
127 size_t copies_size
, char *title
,
129 static size_t ipp_length(ipp_t
*ipp
);
130 static void load_job_cache(const char *filename
);
131 static void load_next_job_id(const char *filename
);
132 static void load_request_root(void);
133 static void remove_job_files(cupsd_job_t
*job
);
134 static void remove_job_history(cupsd_job_t
*job
);
135 static void set_time(cupsd_job_t
*job
, const char *name
);
136 static void start_job(cupsd_job_t
*job
, cupsd_printer_t
*printer
);
137 static void stop_job(cupsd_job_t
*job
, cupsd_jobaction_t action
);
138 static void unload_job(cupsd_job_t
*job
);
139 static void update_job(cupsd_job_t
*job
);
140 static void update_job_attrs(cupsd_job_t
*job
, int do_message
);
144 * 'cupsdAddJob()' - Add a new job to the job queue.
147 cupsd_job_t
* /* O - New job record */
148 cupsdAddJob(int priority
, /* I - Job priority */
149 const char *dest
) /* I - Job destination */
151 cupsd_job_t
*job
; /* New job record */
154 if ((job
= calloc(sizeof(cupsd_job_t
), 1)) == NULL
)
157 job
->id
= NextJobId
++;
158 job
->priority
= priority
;
159 job
->back_pipes
[0] = -1;
160 job
->back_pipes
[1] = -1;
161 job
->print_pipes
[0] = -1;
162 job
->print_pipes
[1] = -1;
163 job
->side_pipes
[0] = -1;
164 job
->side_pipes
[1] = -1;
165 job
->status_pipes
[0] = -1;
166 job
->status_pipes
[1] = -1;
168 cupsdSetString(&job
->dest
, dest
);
171 * Add the new job to the "all jobs" and "active jobs" lists...
174 cupsArrayAdd(Jobs
, job
);
175 cupsArrayAdd(ActiveJobs
, job
);
182 * 'cupsdCancelJobs()' - Cancel all jobs for the given destination/user.
186 cupsdCancelJobs(const char *dest
, /* I - Destination to cancel */
187 const char *username
, /* I - Username or NULL */
188 int purge
) /* I - Purge jobs? */
190 cupsd_job_t
*job
; /* Current job */
193 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
195 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
197 if ((!job
->dest
|| !job
->username
) && !cupsdLoadJob(job
))
200 if ((!dest
|| !strcmp(job
->dest
, dest
)) &&
201 (!username
|| !strcmp(job
->username
, username
)))
204 * Cancel all jobs matching this destination/user...
208 cupsdSetJobState(job
, IPP_JOB_CANCELED
, CUPSD_JOB_PURGE
,
209 "Job purged by user.");
210 else if (job
->state_value
< IPP_JOB_CANCELED
)
211 cupsdSetJobState(job
, IPP_JOB_CANCELED
, CUPSD_JOB_DEFAULT
,
212 "Job canceled by user.");
221 * 'cupsdCheckJobs()' - Check the pending jobs and start any if the destination
228 cupsd_job_t
*job
; /* Current job in queue */
229 cupsd_printer_t
*printer
, /* Printer destination */
230 *pclass
; /* Printer class destination */
231 ipp_attribute_t
*attr
; /* Job attribute */
232 time_t curtime
; /* Current time */
235 curtime
= time(NULL
);
237 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
238 "cupsdCheckJobs: %d active jobs, sleeping=%d, reload=%d, "
239 "curtime=%ld", cupsArrayCount(ActiveJobs
), Sleeping
,
240 NeedReload
, (long)curtime
);
242 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
244 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
246 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
247 "cupsdCheckJobs: Job %d - dest=\"%s\", printer=%p, "
248 "state=%d, cancel_time=%ld, hold_until=%ld, kill_time=%ld, "
249 "pending_cost=%d, pending_timeout=%ld", job
->id
, job
->dest
,
250 job
->printer
, job
->state_value
, (long)job
->cancel_time
,
251 (long)job
->hold_until
, (long)job
->kill_time
,
252 job
->pending_cost
, (long)job
->pending_timeout
);
255 * Kill jobs if they are unresponsive...
258 if (job
->kill_time
&& job
->kill_time
<= curtime
)
261 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Stopping unresponsive job.");
263 stop_job(job
, CUPSD_JOB_FORCE
);
268 * Cancel stuck jobs...
271 if (job
->cancel_time
&& job
->cancel_time
<= curtime
)
273 int cancel_after
; /* job-cancel-after value */
275 attr
= ippFindAttribute(job
->attrs
, "job-cancel-after", IPP_TAG_INTEGER
);
276 cancel_after
= attr
? ippGetInteger(attr
, 0) : MaxJobTime
;
279 cupsdSetJobState(job
, IPP_JOB_CANCELED
, CUPSD_JOB_FORCE
, "Marking stuck job as completed after %d seconds.", cancel_after
);
281 cupsdSetJobState(job
, IPP_JOB_CANCELED
, CUPSD_JOB_DEFAULT
, "Canceling stuck job after %d seconds.", cancel_after
);
286 * Start held jobs if they are ready...
289 if (job
->state_value
== IPP_JOB_HELD
&&
291 job
->hold_until
< curtime
)
293 if (job
->pending_timeout
)
296 * This job is pending; check that we don't have an active Send-Document
297 * operation in progress on any of the client connections, then timeout
298 * the job so we can start printing...
301 cupsd_client_t
*con
; /* Current client connection */
303 for (con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
305 con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
307 con
->request
->request
.op
.operation_id
== IPP_SEND_DOCUMENT
)
313 if (cupsdTimeoutJob(job
))
317 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
318 "Job submission timed out.");
322 * Continue jobs that are waiting on the FilterLimit...
325 if (job
->pending_cost
> 0 &&
326 ((FilterLevel
+ job
->pending_cost
) < FilterLimit
|| FilterLevel
== 0))
327 cupsdContinueJob(job
);
330 * Start pending jobs if the destination is available...
333 if (job
->state_value
== IPP_JOB_PENDING
&& !NeedReload
&&
334 !Sleeping
&& !DoingShutdown
&& !job
->printer
)
336 printer
= cupsdFindDest(job
->dest
);
339 while (printer
&& (printer
->type
& CUPS_PRINTER_CLASS
))
342 * If the class is remote, just pass it to the remote server...
347 if (pclass
->state
== IPP_PRINTER_STOPPED
)
349 else if (pclass
->type
& CUPS_PRINTER_REMOTE
)
352 printer
= cupsdFindAvailablePrinter(printer
->name
);
355 if (!printer
&& !pclass
)
358 * Whoa, the printer and/or class for this destination went away;
362 cupsdSetJobState(job
, IPP_JOB_ABORTED
, CUPSD_JOB_PURGE
,
363 "Job aborted because the destination printer/class "
366 else if (printer
&& !printer
->holding_new_jobs
)
369 * See if the printer is available or remote and not printing a job;
370 * if so, start the job...
376 * Add/update a job-actual-printer-uri attribute for this job
377 * so that we know which printer actually printed the job...
380 if ((attr
= ippFindAttribute(job
->attrs
, "job-actual-printer-uri",
381 IPP_TAG_URI
)) != NULL
)
382 cupsdSetString(&attr
->values
[0].string
.text
, printer
->uri
);
384 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
,
385 "job-actual-printer-uri", NULL
, printer
->uri
);
388 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
391 if (!printer
->job
&& printer
->state
== IPP_PRINTER_IDLE
)
397 start_job(job
, printer
);
406 * 'cupsdCleanJobs()' - Clean out old jobs.
412 cupsd_job_t
*job
; /* Current job */
413 time_t curtime
; /* Current time */
416 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
417 "cupsdCleanJobs: MaxJobs=%d, JobHistory=%d, JobFiles=%d",
418 MaxJobs
, JobHistory
, JobFiles
);
420 if (MaxJobs
<= 0 && JobHistory
== INT_MAX
&& JobFiles
== INT_MAX
)
423 curtime
= time(NULL
);
424 JobHistoryUpdate
= 0;
426 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
428 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
430 if (job
->state_value
>= IPP_JOB_CANCELED
&& !job
->printer
)
433 * Expire old jobs (or job files)...
436 if ((MaxJobs
> 0 && cupsArrayCount(Jobs
) >= MaxJobs
) ||
437 (job
->history_time
&& job
->history_time
<= curtime
))
439 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Removing from history.");
440 cupsdDeleteJob(job
, CUPSD_JOB_PURGE
);
442 else if (job
->file_time
&& job
->file_time
<= curtime
)
444 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Removing document files.");
445 remove_job_files(job
);
447 if (job
->history_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
448 JobHistoryUpdate
= job
->history_time
;
452 if (job
->history_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
453 JobHistoryUpdate
= job
->history_time
;
455 if (job
->file_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
456 JobHistoryUpdate
= job
->file_time
;
461 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdCleanJobs: JobHistoryUpdate=%ld",
462 (long)JobHistoryUpdate
);
467 * 'cupsdContinueJob()' - Continue printing with the next file in a job.
471 cupsdContinueJob(cupsd_job_t
*job
) /* I - Job */
473 int i
; /* Looping var */
474 int slot
; /* Pipe slot */
475 cups_array_t
*filters
= NULL
,/* Filters for job */
476 *prefilters
; /* Filters with prefilters */
477 mime_filter_t
*filter
, /* Current filter */
478 *prefilter
, /* Prefilter */
479 port_monitor
; /* Port monitor filter */
480 char scheme
[255]; /* Device URI scheme */
481 ipp_attribute_t
*attr
; /* Current attribute */
482 const char *ptr
, /* Pointer into value */
483 *abort_message
; /* Abort message */
484 ipp_jstate_t abort_state
= IPP_JOB_STOPPED
;
485 /* New job state on abort */
486 struct stat backinfo
; /* Backend file information */
487 int backroot
; /* Run backend as root? */
488 int pid
; /* Process ID of new filter process */
489 int banner_page
; /* 1 if banner page, 0 otherwise */
490 int filterfds
[2][2] = { { -1, -1 }, { -1, -1 } };
491 /* Pipes used between filters */
492 int envc
; /* Number of environment variables */
493 struct stat fileinfo
; /* Job file information */
494 int argc
; /* Number of arguments */
495 char **argv
= NULL
, /* Filter command-line arguments */
496 filename
[1024], /* Job filename */
497 command
[1024], /* Full path to command */
498 jobid
[255], /* Job ID string */
500 /* Job title string */
501 copies
[255], /* # copies string */
502 *options
, /* Options string */
504 /* Environment variables */
505 charset
[255], /* CHARSET env variable */
506 class_name
[255],/* CLASS env variable */
507 classification
[1024],
508 /* CLASSIFICATION env variable */
510 /* CONTENT_TYPE env variable */
512 /* DEVICE_URI env variable */
513 final_content_type
[1024] = "",
514 /* FINAL_CONTENT_TYPE env variable */
515 lang
[255], /* LANG env variable */
518 /* APPLE_LANGUAGE env variable */
519 #endif /* __APPLE__ */
520 auth_info_required
[255],
521 /* AUTH_INFO_REQUIRED env variable */
522 ppd
[1024], /* PPD env variable */
524 /* PRINTER_INFO env variable */
525 printer_location
[255],
526 /* PRINTER_LOCATION env variable */
528 /* PRINTER env variable */
529 *printer_state_reasons
= NULL
,
530 /* PRINTER_STATE_REASONS env var */
532 /* RIP_MAX_CACHE env variable */
535 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
536 "cupsdContinueJob(job=%p(%d)): current_file=%d, num_files=%d",
537 job
, job
->id
, job
->current_file
, job
->num_files
);
540 * Figure out what filters are required to convert from
541 * the source to the destination type...
544 FilterLevel
-= job
->cost
;
547 job
->pending_cost
= 0;
549 memset(job
->filters
, 0, sizeof(job
->filters
));
551 if (job
->printer
->raw
)
554 * Remote jobs and raw queues go directly to the printer without
558 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Sending job to queue tagged as raw...");
563 * Local jobs get filtered...
566 mime_type_t
*dst
= job
->printer
->filetype
;
567 /* Destination file type */
569 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
570 job
->id
, job
->current_file
+ 1);
571 if (stat(filename
, &fileinfo
))
572 fileinfo
.st_size
= 0;
574 if (job
->retry_as_raster
)
577 * Need to figure out whether the printer supports image/pwg-raster or
578 * image/urf, and use the corresponding type...
581 char type
[MIME_MAX_TYPE
]; /* MIME media type for printer */
583 snprintf(type
, sizeof(type
), "%s/image/urf", job
->printer
->name
);
584 if ((dst
= mimeType(MimeDatabase
, "printer", type
)) == NULL
)
586 snprintf(type
, sizeof(type
), "%s/image/pwg-raster", job
->printer
->name
);
587 dst
= mimeType(MimeDatabase
, "printer", type
);
591 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Retrying job as \"%s\".", strchr(dst
->type
, '/') + 1);
593 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Unable to retry job using a supported raster format.");
596 filters
= mimeFilter2(MimeDatabase
, job
->filetypes
[job
->current_file
], (size_t)fileinfo
.st_size
, dst
, &(job
->cost
));
600 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
601 "Unable to convert file %d to printable format.",
604 abort_message
= "Aborting job because it cannot be printed.";
605 abort_state
= IPP_JOB_ABORTED
;
607 ippSetString(job
->attrs
, &job
->reasons
, 0, "document-unprintable-error");
612 * Figure out the final content type...
615 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "%d filters for job:",
616 cupsArrayCount(filters
));
617 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
619 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
620 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "%s (%s/%s to %s/%s, cost %d)",
622 filter
->src
? filter
->src
->super
: "???",
623 filter
->src
? filter
->src
->type
: "???",
624 filter
->dst
? filter
->dst
->super
: "???",
625 filter
->dst
? filter
->dst
->type
: "???",
628 if (!job
->printer
->remote
)
630 for (filter
= (mime_filter_t
*)cupsArrayLast(filters
);
631 filter
&& filter
->dst
;
632 filter
= (mime_filter_t
*)cupsArrayPrev(filters
))
633 if (strcmp(filter
->dst
->super
, "printer") ||
634 strcmp(filter
->dst
->type
, job
->printer
->name
))
637 if (filter
&& filter
->dst
)
639 if ((ptr
= strchr(filter
->dst
->type
, '/')) != NULL
)
640 snprintf(final_content_type
, sizeof(final_content_type
),
641 "FINAL_CONTENT_TYPE=%s", ptr
+ 1);
643 snprintf(final_content_type
, sizeof(final_content_type
),
644 "FINAL_CONTENT_TYPE=%s/%s", filter
->dst
->super
,
648 snprintf(final_content_type
, sizeof(final_content_type
),
649 "FINAL_CONTENT_TYPE=printer/%s", job
->printer
->name
);
653 * Remove NULL ("-") filters...
656 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
658 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
659 if (!strcmp(filter
->filter
, "-"))
660 cupsArrayRemove(filters
, filter
);
662 if (cupsArrayCount(filters
) == 0)
664 cupsArrayDelete(filters
);
669 * If this printer has any pre-filters, insert the required pre-filter
670 * in the filters array...
673 if (job
->printer
->prefiltertype
&& filters
)
675 prefilters
= cupsArrayNew(NULL
, NULL
);
677 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
679 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
681 if ((prefilter
= mimeFilterLookup(MimeDatabase
, filter
->src
,
682 job
->printer
->prefiltertype
)))
684 cupsArrayAdd(prefilters
, prefilter
);
685 job
->cost
+= prefilter
->cost
;
688 cupsArrayAdd(prefilters
, filter
);
691 cupsArrayDelete(filters
);
692 filters
= prefilters
;
697 * Set a minimum cost of 100 for all jobs so that FilterLimit
698 * works with raw queues and other low-cost paths.
705 * See if the filter cost is too high...
708 if ((FilterLevel
+ job
->cost
) > FilterLimit
&& FilterLevel
> 0 &&
712 * Don't print this job quite yet...
715 cupsArrayDelete(filters
);
717 cupsdLogJob(job
, CUPSD_LOG_INFO
,
718 "Holding because filter limit has been reached.");
719 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
720 "cupsdContinueJob: file=%d, cost=%d, level=%d, limit=%d",
721 job
->current_file
, job
->cost
, FilterLevel
,
724 job
->pending_cost
= job
->cost
;
729 FilterLevel
+= job
->cost
;
732 * Add decompression/raw filter as needed...
735 if (job
->compressions
[job
->current_file
] &&
736 (!job
->printer
->remote
|| job
->num_files
== 1))
739 * Add gziptoany filter to the front of the list...
743 filters
= cupsArrayNew(NULL
, NULL
);
745 if (!cupsArrayInsert(filters
, &gziptoany_filter
))
747 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
748 "Unable to add decompression filter - %s", strerror(errno
));
750 cupsArrayDelete(filters
);
752 abort_message
= "Stopping job because the scheduler ran out of memory.";
759 * Add port monitor, if any...
762 if (job
->printer
->port_monitor
)
765 * Add port monitor to the end of the list...
769 filters
= cupsArrayNew(NULL
, NULL
);
771 port_monitor
.src
= NULL
;
772 port_monitor
.dst
= NULL
;
773 port_monitor
.cost
= 0;
775 snprintf(port_monitor
.filter
, sizeof(port_monitor
.filter
),
776 "%s/monitor/%s", ServerBin
, job
->printer
->port_monitor
);
778 if (!cupsArrayAdd(filters
, &port_monitor
))
780 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
781 "Unable to add port monitor - %s", strerror(errno
));
783 abort_message
= "Stopping job because the scheduler ran out of memory.";
790 * Make sure we don't go over the "MAX_FILTERS" limit...
793 if (cupsArrayCount(filters
) > MAX_FILTERS
)
795 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
796 "Too many filters (%d > %d), unable to print.",
797 cupsArrayCount(filters
), MAX_FILTERS
);
799 abort_message
= "Aborting job because it needs too many filters to print.";
800 abort_state
= IPP_JOB_ABORTED
;
802 ippSetString(job
->attrs
, &job
->reasons
, 0, "document-unprintable-error");
808 * Determine if we are printing a banner page or not...
811 if (job
->job_sheets
== NULL
)
813 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "No job-sheets attribute.");
814 if ((job
->job_sheets
=
815 ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_ZERO
)) != NULL
)
816 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
817 "... but someone added one without setting job_sheets.");
819 else if (job
->job_sheets
->num_values
== 1)
820 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "job-sheets=%s",
821 job
->job_sheets
->values
[0].string
.text
);
823 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "job-sheets=%s,%s",
824 job
->job_sheets
->values
[0].string
.text
,
825 job
->job_sheets
->values
[1].string
.text
);
827 if (job
->printer
->type
& CUPS_PRINTER_REMOTE
)
829 else if (job
->job_sheets
== NULL
)
831 else if (_cups_strcasecmp(job
->job_sheets
->values
[0].string
.text
, "none") != 0 &&
832 job
->current_file
== 0)
834 else if (job
->job_sheets
->num_values
> 1 &&
835 _cups_strcasecmp(job
->job_sheets
->values
[1].string
.text
, "none") != 0 &&
836 job
->current_file
== (job
->num_files
- 1))
841 if ((options
= get_options(job
, banner_page
, copies
, sizeof(copies
), title
,
842 sizeof(title
))) == NULL
)
844 abort_message
= "Stopping job because the scheduler ran out of memory.";
850 * Build the command-line arguments for the filters. Each filter
851 * has 6 or 7 arguments:
859 * argv[6] = filename (optional; normally stdin)
861 * This allows legacy printer drivers that use the old System V
862 * printing interface to be used by CUPS.
864 * For remote jobs, we send all of the files in the argument list.
867 if (job
->printer
->remote
)
868 argc
= 6 + job
->num_files
;
872 if ((argv
= calloc((size_t)argc
+ 1, sizeof(char *))) == NULL
)
874 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Unable to allocate argument array - %s",
877 abort_message
= "Stopping job because the scheduler ran out of memory.";
882 sprintf(jobid
, "%d", job
->id
);
884 argv
[0] = job
->printer
->name
;
886 argv
[2] = job
->username
;
891 if (job
->printer
->remote
&& job
->num_files
> 1)
893 for (i
= 0; i
< job
->num_files
; i
++)
895 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
897 argv
[6 + i
] = strdup(filename
);
902 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
903 job
->id
, job
->current_file
+ 1);
904 argv
[6] = strdup(filename
);
907 for (i
= 0; argv
[i
]; i
++)
908 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "argv[%d]=\"%s\"", i
, argv
[i
]);
911 * Create environment variable strings for the filters...
914 attr
= ippFindAttribute(job
->attrs
, "attributes-natural-language",
918 strlcpy(apple_language
, "APPLE_LANGUAGE=", sizeof(apple_language
));
919 _cupsAppleLanguage(attr
->values
[0].string
.text
,
920 apple_language
+ 15, sizeof(apple_language
) - 15);
921 #endif /* __APPLE__ */
923 switch (strlen(attr
->values
[0].string
.text
))
927 * This is an unknown or badly formatted language code; use
928 * the POSIX locale...
931 strlcpy(lang
, "LANG=C", sizeof(lang
));
936 * Just the language code (ll)...
939 snprintf(lang
, sizeof(lang
), "LANG=%s.UTF-8",
940 attr
->values
[0].string
.text
);
945 * Language and country code (ll-cc)...
948 snprintf(lang
, sizeof(lang
), "LANG=%c%c_%c%c.UTF-8",
949 attr
->values
[0].string
.text
[0],
950 attr
->values
[0].string
.text
[1],
951 toupper(attr
->values
[0].string
.text
[3] & 255),
952 toupper(attr
->values
[0].string
.text
[4] & 255));
956 if ((attr
= ippFindAttribute(job
->attrs
, "document-format",
957 IPP_TAG_MIMETYPE
)) != NULL
&&
958 (ptr
= strstr(attr
->values
[0].string
.text
, "charset=")) != NULL
)
959 snprintf(charset
, sizeof(charset
), "CHARSET=%s", ptr
+ 8);
961 strlcpy(charset
, "CHARSET=utf-8", sizeof(charset
));
963 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s/%s",
964 job
->filetypes
[job
->current_file
]->super
,
965 job
->filetypes
[job
->current_file
]->type
);
966 snprintf(device_uri
, sizeof(device_uri
), "DEVICE_URI=%s",
967 job
->printer
->device_uri
);
968 snprintf(ppd
, sizeof(ppd
), "PPD=%s/ppd/%s.ppd", ServerRoot
,
970 snprintf(printer_info
, sizeof(printer_name
), "PRINTER_INFO=%s",
971 job
->printer
->info
? job
->printer
->info
: "");
972 snprintf(printer_location
, sizeof(printer_name
), "PRINTER_LOCATION=%s",
973 job
->printer
->location
? job
->printer
->location
: "");
974 snprintf(printer_name
, sizeof(printer_name
), "PRINTER=%s", job
->printer
->name
);
975 if (job
->printer
->num_reasons
> 0)
977 char *psrptr
; /* Pointer into PRINTER_STATE_REASONS */
978 size_t psrlen
; /* Size of PRINTER_STATE_REASONS */
980 for (psrlen
= 22, i
= 0; i
< job
->printer
->num_reasons
; i
++)
981 psrlen
+= strlen(job
->printer
->reasons
[i
]) + 1;
983 if ((printer_state_reasons
= malloc(psrlen
)) != NULL
)
986 * All of these strcpy's are safe because we allocated the psr string...
989 strlcpy(printer_state_reasons
, "PRINTER_STATE_REASONS=", psrlen
);
990 for (psrptr
= printer_state_reasons
+ 22, i
= 0;
991 i
< job
->printer
->num_reasons
;
996 strlcpy(psrptr
, job
->printer
->reasons
[i
], psrlen
- (size_t)(psrptr
- printer_state_reasons
));
997 psrptr
+= strlen(psrptr
);
1001 snprintf(rip_max_cache
, sizeof(rip_max_cache
), "RIP_MAX_CACHE=%s", RIPCache
);
1003 if (job
->printer
->num_auth_info_required
== 1)
1004 snprintf(auth_info_required
, sizeof(auth_info_required
),
1005 "AUTH_INFO_REQUIRED=%s",
1006 job
->printer
->auth_info_required
[0]);
1007 else if (job
->printer
->num_auth_info_required
== 2)
1008 snprintf(auth_info_required
, sizeof(auth_info_required
),
1009 "AUTH_INFO_REQUIRED=%s,%s",
1010 job
->printer
->auth_info_required
[0],
1011 job
->printer
->auth_info_required
[1]);
1012 else if (job
->printer
->num_auth_info_required
== 3)
1013 snprintf(auth_info_required
, sizeof(auth_info_required
),
1014 "AUTH_INFO_REQUIRED=%s,%s,%s",
1015 job
->printer
->auth_info_required
[0],
1016 job
->printer
->auth_info_required
[1],
1017 job
->printer
->auth_info_required
[2]);
1018 else if (job
->printer
->num_auth_info_required
== 4)
1019 snprintf(auth_info_required
, sizeof(auth_info_required
),
1020 "AUTH_INFO_REQUIRED=%s,%s,%s,%s",
1021 job
->printer
->auth_info_required
[0],
1022 job
->printer
->auth_info_required
[1],
1023 job
->printer
->auth_info_required
[2],
1024 job
->printer
->auth_info_required
[3]);
1026 strlcpy(auth_info_required
, "AUTH_INFO_REQUIRED=none",
1027 sizeof(auth_info_required
));
1029 envc
= cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
1031 envp
[envc
++] = charset
;
1032 envp
[envc
++] = lang
;
1034 envp
[envc
++] = apple_language
;
1035 #endif /* __APPLE__ */
1036 envp
[envc
++] = ppd
;
1037 envp
[envc
++] = rip_max_cache
;
1038 envp
[envc
++] = content_type
;
1039 envp
[envc
++] = device_uri
;
1040 envp
[envc
++] = printer_info
;
1041 envp
[envc
++] = printer_location
;
1042 envp
[envc
++] = printer_name
;
1043 envp
[envc
++] = printer_state_reasons
? printer_state_reasons
:
1044 "PRINTER_STATE_REASONS=none";
1045 envp
[envc
++] = banner_page
? "CUPS_FILETYPE=job-sheet" :
1046 "CUPS_FILETYPE=document";
1048 if (final_content_type
[0])
1049 envp
[envc
++] = final_content_type
;
1051 if (Classification
&& !banner_page
)
1053 if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
1054 IPP_TAG_NAME
)) == NULL
)
1055 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
1057 else if (attr
->num_values
> 1 &&
1058 strcmp(attr
->values
[1].string
.text
, "none") != 0)
1059 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
1060 attr
->values
[1].string
.text
);
1062 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
1063 attr
->values
[0].string
.text
);
1065 envp
[envc
++] = classification
;
1068 if (job
->dtype
& CUPS_PRINTER_CLASS
)
1070 snprintf(class_name
, sizeof(class_name
), "CLASS=%s", job
->dest
);
1071 envp
[envc
++] = class_name
;
1074 envp
[envc
++] = auth_info_required
;
1077 i
< (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0]));
1079 if (job
->auth_env
[i
])
1080 envp
[envc
++] = job
->auth_env
[i
];
1085 envp
[envc
++] = job
->auth_uid
;
1089 for (i
= 0; i
< envc
; i
++)
1090 if (!strncmp(envp
[i
], "AUTH_", 5))
1091 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"AUTH_%c****\"", i
,
1093 else if (strncmp(envp
[i
], "DEVICE_URI=", 11))
1094 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"%s\"", i
, envp
[i
]);
1096 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"DEVICE_URI=%s\"", i
,
1097 job
->printer
->sanitized_device_uri
);
1099 if (job
->printer
->remote
)
1100 job
->current_file
= job
->num_files
;
1102 job
->current_file
++;
1105 * Now create processes for all of the filters...
1108 for (i
= 0, slot
= 0, filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
1110 i
++, filter
= (mime_filter_t
*)cupsArrayNext(filters
))
1112 if (filter
->filter
[0] != '/')
1113 snprintf(command
, sizeof(command
), "%s/filter/%s", ServerBin
,
1116 strlcpy(command
, filter
->filter
, sizeof(command
));
1118 if (i
< (cupsArrayCount(filters
) - 1))
1120 if (cupsdOpenPipe(filterfds
[slot
]))
1122 abort_message
= "Stopping job because the scheduler could not create "
1123 "the filter pipes.";
1130 if (job
->current_file
== 1 ||
1131 (job
->printer
->pc
&& job
->printer
->pc
->single_file
))
1133 if (strncmp(job
->printer
->device_uri
, "file:", 5) != 0)
1135 if (cupsdOpenPipe(job
->print_pipes
))
1137 abort_message
= "Stopping job because the scheduler could not "
1138 "create the backend pipes.";
1145 job
->print_pipes
[0] = -1;
1146 if (!strcmp(job
->printer
->device_uri
, "file:/dev/null") ||
1147 !strcmp(job
->printer
->device_uri
, "file:///dev/null"))
1148 job
->print_pipes
[1] = -1;
1151 if (!strncmp(job
->printer
->device_uri
, "file:/dev/", 10))
1152 job
->print_pipes
[1] = open(job
->printer
->device_uri
+ 5,
1154 else if (!strncmp(job
->printer
->device_uri
, "file:///dev/", 12))
1155 job
->print_pipes
[1] = open(job
->printer
->device_uri
+ 7,
1157 else if (!strncmp(job
->printer
->device_uri
, "file:///", 8))
1158 job
->print_pipes
[1] = open(job
->printer
->device_uri
+ 7,
1159 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
1161 job
->print_pipes
[1] = open(job
->printer
->device_uri
+ 5,
1162 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
1164 if (job
->print_pipes
[1] < 0)
1166 abort_message
= "Stopping job because the scheduler could not "
1167 "open the output file.";
1172 fcntl(job
->print_pipes
[1], F_SETFD
,
1173 fcntl(job
->print_pipes
[1], F_GETFD
) | FD_CLOEXEC
);
1178 filterfds
[slot
][0] = job
->print_pipes
[0];
1179 filterfds
[slot
][1] = job
->print_pipes
[1];
1182 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
1183 filterfds
[slot
][1], job
->status_pipes
[1],
1184 job
->back_pipes
[0], job
->side_pipes
[0], 0,
1185 job
->profile
, job
, job
->filters
+ i
);
1187 cupsdClosePipe(filterfds
[!slot
]);
1191 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Unable to start filter \"%s\" - %s.",
1192 filter
->filter
, strerror(errno
));
1194 abort_message
= "Stopping job because the scheduler could not execute a "
1200 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Started filter %s (PID %d)", command
,
1212 cupsArrayDelete(filters
);
1216 * Finally, pipe the final output into a backend process if needed...
1219 if (strncmp(job
->printer
->device_uri
, "file:", 5) != 0)
1221 if (job
->current_file
== 1 || job
->printer
->remote
||
1222 (job
->printer
->pc
&& job
->printer
->pc
->single_file
))
1224 sscanf(job
->printer
->device_uri
, "%254[^:]", scheme
);
1225 snprintf(command
, sizeof(command
), "%s/backend/%s", ServerBin
, scheme
);
1228 * See if the backend needs to run as root...
1233 else if (stat(command
, &backinfo
))
1236 backroot
= !(backinfo
.st_mode
& (S_IWGRP
| S_IRWXO
));
1238 argv
[0] = job
->printer
->sanitized_device_uri
;
1240 filterfds
[slot
][0] = -1;
1241 filterfds
[slot
][1] = -1;
1243 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
1244 filterfds
[slot
][1], job
->status_pipes
[1],
1245 job
->back_pipes
[1], job
->side_pipes
[1],
1246 backroot
, job
->bprofile
, job
, &(job
->backend
));
1250 abort_message
= "Stopping job because the sheduler could not execute "
1257 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Started backend %s (PID %d)",
1262 if (job
->current_file
== job
->num_files
||
1263 (job
->printer
->pc
&& job
->printer
->pc
->single_file
))
1264 cupsdClosePipe(job
->print_pipes
);
1266 if (job
->current_file
== job
->num_files
)
1268 cupsdClosePipe(job
->back_pipes
);
1269 cupsdClosePipe(job
->side_pipes
);
1271 close(job
->status_pipes
[1]);
1272 job
->status_pipes
[1] = -1;
1277 filterfds
[slot
][0] = -1;
1278 filterfds
[slot
][1] = -1;
1280 if (job
->current_file
== job
->num_files
||
1281 (job
->printer
->pc
&& job
->printer
->pc
->single_file
))
1282 cupsdClosePipe(job
->print_pipes
);
1284 if (job
->current_file
== job
->num_files
)
1286 close(job
->status_pipes
[1]);
1287 job
->status_pipes
[1] = -1;
1291 cupsdClosePipe(filterfds
[slot
]);
1293 for (i
= 6; i
< argc
; i
++)
1299 if (printer_state_reasons
)
1300 free(printer_state_reasons
);
1302 cupsdAddSelect(job
->status_buffer
->fd
, (cupsd_selfunc_t
)update_job
, NULL
,
1305 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
, "Job #%d started.",
1312 * If we get here, we need to abort the current job and close out all
1313 * files and pipes...
1318 FilterLevel
-= job
->cost
;
1321 for (slot
= 0; slot
< 2; slot
++)
1322 cupsdClosePipe(filterfds
[slot
]);
1324 cupsArrayDelete(filters
);
1328 for (i
= 6; i
< argc
; i
++)
1333 if (printer_state_reasons
)
1334 free(printer_state_reasons
);
1336 cupsdClosePipe(job
->print_pipes
);
1337 cupsdClosePipe(job
->back_pipes
);
1338 cupsdClosePipe(job
->side_pipes
);
1340 cupsdRemoveSelect(job
->status_pipes
[0]);
1341 cupsdClosePipe(job
->status_pipes
);
1342 cupsdStatBufDelete(job
->status_buffer
);
1343 job
->status_buffer
= NULL
;
1346 * Update the printer and job state.
1349 cupsdSetJobState(job
, abort_state
, CUPSD_JOB_DEFAULT
, "%s", abort_message
);
1350 cupsdSetPrinterState(job
->printer
, IPP_PRINTER_IDLE
, 0);
1351 update_job_attrs(job
, 0);
1354 free_job_history(job
);
1356 cupsArrayRemove(PrintingJobs
, job
);
1359 * Clear the printer <-> job association...
1362 job
->printer
->job
= NULL
;
1363 job
->printer
= NULL
;
1368 * 'cupsdDeleteJob()' - Free all memory used by a job.
1372 cupsdDeleteJob(cupsd_job_t
*job
, /* I - Job */
1373 cupsd_jobaction_t action
)/* I - Action */
1375 int i
; /* Looping var */
1379 finalize_job(job
, 1);
1381 if (action
== CUPSD_JOB_PURGE
)
1382 remove_job_history(job
);
1384 cupsdClearString(&job
->username
);
1385 cupsdClearString(&job
->dest
);
1387 i
< (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0]));
1389 cupsdClearString(job
->auth_env
+ i
);
1390 cupsdClearString(&job
->auth_uid
);
1392 if (action
== CUPSD_JOB_PURGE
)
1393 remove_job_files(job
);
1394 else if (job
->num_files
> 0)
1396 free(job
->compressions
);
1397 free(job
->filetypes
);
1403 free_job_history(job
);
1407 cupsArrayRemove(Jobs
, job
);
1408 cupsArrayRemove(ActiveJobs
, job
);
1409 cupsArrayRemove(PrintingJobs
, job
);
1416 * 'cupsdFreeAllJobs()' - Free all jobs from memory.
1420 cupsdFreeAllJobs(void)
1422 cupsd_job_t
*job
; /* Current job */
1430 cupsdStopAllJobs(CUPSD_JOB_FORCE
, 0);
1433 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1435 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1436 cupsdDeleteJob(job
, CUPSD_JOB_DEFAULT
);
1438 cupsdReleaseSignals();
1443 * 'cupsdFindJob()' - Find the specified job.
1446 cupsd_job_t
* /* O - Job data */
1447 cupsdFindJob(int id
) /* I - Job ID */
1449 cupsd_job_t key
; /* Search key */
1454 return ((cupsd_job_t
*)cupsArrayFind(Jobs
, &key
));
1459 * 'cupsdGetPrinterJobCount()' - Get the number of pending, processing,
1460 * or held jobs in a printer or class.
1463 int /* O - Job count */
1464 cupsdGetPrinterJobCount(
1465 const char *dest
) /* I - Printer or class name */
1467 int count
; /* Job count */
1468 cupsd_job_t
*job
; /* Current job */
1471 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
1473 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
1474 if (job
->dest
&& !_cups_strcasecmp(job
->dest
, dest
))
1482 * 'cupsdGetUserJobCount()' - Get the number of pending, processing,
1483 * or held jobs for a user.
1486 int /* O - Job count */
1487 cupsdGetUserJobCount(
1488 const char *username
) /* I - Username */
1490 int count
; /* Job count */
1491 cupsd_job_t
*job
; /* Current job */
1494 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
1496 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
1497 if (!_cups_strcasecmp(job
->username
, username
))
1505 * 'cupsdLoadAllJobs()' - Load all jobs from disk.
1509 cupsdLoadAllJobs(void)
1511 char filename
[1024]; /* Full filename of job.cache file */
1512 struct stat fileinfo
, /* Information on job.cache file */
1513 dirinfo
; /* Information on RequestRoot dir */
1518 * Create the job arrays as needed...
1522 Jobs
= cupsArrayNew(compare_jobs
, NULL
);
1525 ActiveJobs
= cupsArrayNew(compare_active_jobs
, NULL
);
1528 PrintingJobs
= cupsArrayNew(compare_jobs
, NULL
);
1531 * See whether the job.cache file is older than the RequestRoot directory...
1534 snprintf(filename
, sizeof(filename
), "%s/job.cache", CacheDir
);
1536 if (stat(filename
, &fileinfo
))
1538 fileinfo
.st_mtime
= 0;
1540 if (errno
!= ENOENT
)
1541 cupsdLogMessage(CUPSD_LOG_ERROR
,
1542 "Unable to get file information for \"%s\" - %s",
1543 filename
, strerror(errno
));
1546 if (stat(RequestRoot
, &dirinfo
))
1548 dirinfo
.st_mtime
= 0;
1550 if (errno
!= ENOENT
)
1551 cupsdLogMessage(CUPSD_LOG_ERROR
,
1552 "Unable to get directory information for \"%s\" - %s",
1553 RequestRoot
, strerror(errno
));
1557 * Load the most recent source for job data...
1560 if (dirinfo
.st_mtime
> fileinfo
.st_mtime
)
1562 load_request_root();
1564 load_next_job_id(filename
);
1567 load_job_cache(filename
);
1570 * Clean out old jobs as needed...
1573 if (MaxJobs
> 0 && cupsArrayCount(Jobs
) >= MaxJobs
)
1579 * 'cupsdLoadJob()' - Load a single job.
1582 int /* O - 1 on success, 0 on failure */
1583 cupsdLoadJob(cupsd_job_t
*job
) /* I - Job */
1585 int i
; /* Looping var */
1586 char jobfile
[1024]; /* Job filename */
1587 cups_file_t
*fp
; /* Job file */
1588 int fileid
; /* Current file ID */
1589 ipp_attribute_t
*attr
; /* Job attribute */
1590 const char *dest
; /* Destination name */
1591 cupsd_printer_t
*destptr
; /* Pointer to destination */
1592 mime_type_t
**filetypes
; /* New filetypes array */
1593 int *compressions
; /* New compressions array */
1598 if (job
->state_value
> IPP_JOB_STOPPED
)
1599 job
->access_time
= time(NULL
);
1604 if ((job
->attrs
= ippNew()) == NULL
)
1606 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Ran out of memory for job attributes.");
1611 * Load job attributes...
1614 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Loading attributes...");
1616 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, job
->id
);
1617 if ((fp
= cupsdOpenConfFile(jobfile
)) == NULL
)
1620 if (ippReadIO(fp
, (ipp_iocb_t
)cupsFileRead
, 1, NULL
, job
->attrs
) != IPP_DATA
)
1622 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1623 "Unable to read job control file \"%s\".", jobfile
);
1631 * Copy attribute data to the job object...
1634 if (!ippFindAttribute(job
->attrs
, "time-at-creation", IPP_TAG_INTEGER
))
1636 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1637 "Missing or bad time-at-creation attribute in control file.");
1641 if ((job
->state
= ippFindAttribute(job
->attrs
, "job-state",
1642 IPP_TAG_ENUM
)) == NULL
)
1644 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1645 "Missing or bad job-state attribute in control file.");
1649 job
->state_value
= (ipp_jstate_t
)job
->state
->values
[0].integer
;
1651 job
->history_time
= 0;
1653 if (job
->state_value
>= IPP_JOB_CANCELED
&&
1654 (attr
= ippFindAttribute(job
->attrs
, "time-at-completed",
1655 IPP_TAG_INTEGER
)) != NULL
)
1657 if (JobHistory
< INT_MAX
)
1658 job
->history_time
= attr
->values
[0].integer
+ JobHistory
;
1660 job
->history_time
= INT_MAX
;
1662 if (job
->history_time
< time(NULL
))
1663 goto error
; /* Expired, remove from history */
1665 if (job
->history_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
1666 JobHistoryUpdate
= job
->history_time
;
1668 if (JobFiles
< INT_MAX
)
1669 job
->file_time
= attr
->values
[0].integer
+ JobFiles
;
1671 job
->file_time
= INT_MAX
;
1673 if (job
->file_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
1674 JobHistoryUpdate
= job
->file_time
;
1676 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdLoadJob: JobHistoryUpdate=%ld",
1677 (long)JobHistoryUpdate
);
1682 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1683 IPP_TAG_URI
)) == NULL
)
1685 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1686 "No job-printer-uri attribute in control file.");
1690 if ((dest
= cupsdValidateDest(attr
->values
[0].string
.text
, &(job
->dtype
),
1693 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1694 "Unable to queue job for destination \"%s\".",
1695 attr
->values
[0].string
.text
);
1699 cupsdSetString(&job
->dest
, dest
);
1701 else if ((destptr
= cupsdFindDest(job
->dest
)) == NULL
)
1703 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1704 "Unable to queue job for destination \"%s\".",
1709 if ((job
->reasons
= ippFindAttribute(job
->attrs
, "job-state-reasons",
1710 IPP_TAG_KEYWORD
)) == NULL
)
1712 const char *reason
; /* job-state-reason keyword */
1714 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
1715 "Adding missing job-state-reasons attribute to control file.");
1717 switch (job
->state_value
)
1720 case IPP_JOB_PENDING
:
1721 if (destptr
->state
== IPP_PRINTER_STOPPED
)
1722 reason
= "printer-stopped";
1728 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1729 IPP_TAG_ZERO
)) != NULL
&&
1730 (attr
->value_tag
== IPP_TAG_NAME
||
1731 attr
->value_tag
== IPP_TAG_NAMELANG
||
1732 attr
->value_tag
== IPP_TAG_KEYWORD
) &&
1733 strcmp(attr
->values
[0].string
.text
, "no-hold"))
1734 reason
= "job-hold-until-specified";
1736 reason
= "job-incoming";
1739 case IPP_JOB_PROCESSING
:
1740 reason
= "job-printing";
1743 case IPP_JOB_STOPPED
:
1744 reason
= "job-stopped";
1747 case IPP_JOB_CANCELED
:
1748 reason
= "job-canceled-by-user";
1751 case IPP_JOB_ABORTED
:
1752 reason
= "aborted-by-system";
1755 case IPP_JOB_COMPLETED
:
1756 reason
= "job-completed-successfully";
1760 job
->reasons
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1761 "job-state-reasons", NULL
, reason
);
1763 else if (job
->state_value
== IPP_JOB_PENDING
)
1765 if (destptr
->state
== IPP_PRINTER_STOPPED
)
1766 ippSetString(job
->attrs
, &job
->reasons
, 0, "printer-stopped");
1768 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
1771 job
->sheets
= ippFindAttribute(job
->attrs
, "job-media-sheets-completed",
1773 job
->job_sheets
= ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_NAME
);
1777 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
1778 IPP_TAG_INTEGER
)) == NULL
)
1780 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1781 "Missing or bad job-priority attribute in control file.");
1785 job
->priority
= attr
->values
[0].integer
;
1790 if ((attr
= ippFindAttribute(job
->attrs
, "job-originating-user-name",
1791 IPP_TAG_NAME
)) == NULL
)
1793 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1794 "Missing or bad job-originating-user-name "
1795 "attribute in control file.");
1799 cupsdSetString(&job
->username
, attr
->values
[0].string
.text
);
1803 * Set the job hold-until time and state...
1806 if (job
->state_value
== IPP_JOB_HELD
)
1808 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1809 IPP_TAG_KEYWORD
)) == NULL
)
1810 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1813 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
, CUPSD_JOB_DEFAULT
);
1816 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1817 job
->state_value
= IPP_JOB_PENDING
;
1820 else if (job
->state_value
== IPP_JOB_PROCESSING
)
1822 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1823 job
->state_value
= IPP_JOB_PENDING
;
1826 if (!job
->num_files
)
1829 * Find all the d##### files...
1832 for (fileid
= 1; fileid
< 10000; fileid
++)
1834 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
1837 if (access(jobfile
, 0))
1840 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
1841 "Auto-typing document file \"%s\"...", jobfile
);
1843 if (fileid
> job
->num_files
)
1845 if (job
->num_files
== 0)
1847 compressions
= (int *)calloc((size_t)fileid
, sizeof(int));
1848 filetypes
= (mime_type_t
**)calloc((size_t)fileid
, sizeof(mime_type_t
*));
1852 compressions
= (int *)realloc(job
->compressions
, sizeof(int) * (size_t)fileid
);
1853 filetypes
= (mime_type_t
**)realloc(job
->filetypes
, sizeof(mime_type_t
*) * (size_t)fileid
);
1857 job
->compressions
= compressions
;
1860 job
->filetypes
= filetypes
;
1862 if (!compressions
|| !filetypes
)
1864 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1865 "Ran out of memory for job file types.");
1867 ippDelete(job
->attrs
);
1870 if (job
->compressions
)
1872 free(job
->compressions
);
1873 job
->compressions
= NULL
;
1878 free(job
->filetypes
);
1879 job
->filetypes
= NULL
;
1886 job
->num_files
= fileid
;
1889 job
->filetypes
[fileid
- 1] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
1890 job
->compressions
+ fileid
- 1);
1892 if (!job
->filetypes
[fileid
- 1])
1893 job
->filetypes
[fileid
- 1] = mimeType(MimeDatabase
, "application",
1899 * Load authentication information as needed...
1902 if (job
->state_value
< IPP_JOB_STOPPED
)
1904 snprintf(jobfile
, sizeof(jobfile
), "%s/a%05d", RequestRoot
, job
->id
);
1907 i
< (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0]));
1909 cupsdClearString(job
->auth_env
+ i
);
1910 cupsdClearString(&job
->auth_uid
);
1912 if ((fp
= cupsFileOpen(jobfile
, "r")) != NULL
)
1914 int bytes
, /* Size of auth data */
1915 linenum
= 1; /* Current line number */
1916 char line
[65536], /* Line from file */
1917 *value
, /* Value from line */
1918 data
[65536]; /* Decoded data */
1921 if (cupsFileGets(fp
, line
, sizeof(line
)) &&
1922 !strcmp(line
, "CUPSD-AUTH-V3"))
1925 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
1931 if (strcmp(line
, "negotiate") && strcmp(line
, "uid"))
1933 bytes
= sizeof(data
);
1934 httpDecode64_2(data
, &bytes
, value
);
1938 * Assign environment variables...
1941 if (!strcmp(line
, "uid"))
1943 cupsdSetStringf(&job
->auth_uid
, "AUTH_UID=%s", value
);
1946 else if (i
>= (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0])))
1949 if (!strcmp(line
, "username"))
1950 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_USERNAME=%s", data
);
1951 else if (!strcmp(line
, "domain"))
1952 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_DOMAIN=%s", data
);
1953 else if (!strcmp(line
, "password"))
1954 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_PASSWORD=%s", data
);
1955 else if (!strcmp(line
, "negotiate"))
1956 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_NEGOTIATE=%s", value
);
1968 job
->access_time
= time(NULL
);
1972 * If we get here then something bad happened...
1977 ippDelete(job
->attrs
);
1980 remove_job_history(job
);
1981 remove_job_files(job
);
1988 * 'cupsdMoveJob()' - Move the specified job to a different destination.
1992 cupsdMoveJob(cupsd_job_t
*job
, /* I - Job */
1993 cupsd_printer_t
*p
) /* I - Destination printer or class */
1995 ipp_attribute_t
*attr
; /* job-printer-uri attribute */
1996 const char *olddest
; /* Old destination */
1997 cupsd_printer_t
*oldp
; /* Old pointer */
2001 * Don't move completed jobs...
2004 if (job
->state_value
> IPP_JOB_STOPPED
)
2008 * Get the old destination...
2011 olddest
= job
->dest
;
2014 oldp
= job
->printer
;
2016 oldp
= cupsdFindDest(olddest
);
2019 * Change the destination information...
2022 if (job
->state_value
> IPP_JOB_HELD
)
2023 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
2024 "Stopping job prior to move.");
2026 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, oldp
, job
,
2027 "Job #%d moved from %s to %s.", job
->id
, olddest
,
2030 cupsdSetString(&job
->dest
, p
->name
);
2031 job
->dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_REMOTE
);
2033 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
2034 IPP_TAG_URI
)) != NULL
)
2035 cupsdSetString(&(attr
->values
[0].string
.text
), p
->uri
);
2037 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, p
, job
,
2038 "Job #%d moved from %s to %s.", job
->id
, olddest
,
2042 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2047 * 'cupsdReleaseJob()' - Release the specified job.
2051 cupsdReleaseJob(cupsd_job_t
*job
) /* I - Job */
2053 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdReleaseJob(job=%p(%d))", job
,
2056 if (job
->state_value
== IPP_JOB_HELD
)
2059 * Add trailing banner as needed...
2062 if (job
->pending_timeout
)
2063 cupsdTimeoutJob(job
);
2065 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
2066 "Job released by user.");
2072 * 'cupsdRestartJob()' - Restart the specified job.
2076 cupsdRestartJob(cupsd_job_t
*job
) /* I - Job */
2078 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdRestartJob(job=%p(%d))", job
,
2081 if (job
->state_value
== IPP_JOB_STOPPED
|| job
->num_files
)
2082 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
2083 "Job restarted by user.");
2088 * 'cupsdSaveAllJobs()' - Save a summary of all jobs to disk.
2092 cupsdSaveAllJobs(void)
2094 int i
; /* Looping var */
2095 cups_file_t
*fp
; /* job.cache file */
2096 char filename
[1024], /* job.cache filename */
2097 temp
[1024]; /* Temporary string */
2098 cupsd_job_t
*job
; /* Current job */
2099 time_t curtime
; /* Current time */
2100 struct tm
*curdate
; /* Current date */
2103 snprintf(filename
, sizeof(filename
), "%s/job.cache", CacheDir
);
2104 if ((fp
= cupsdCreateConfFile(filename
, ConfigFilePerm
)) == NULL
)
2107 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving job.cache...");
2110 * Write a small header to the file...
2113 curtime
= time(NULL
);
2114 curdate
= localtime(&curtime
);
2115 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
2117 cupsFilePuts(fp
, "# Job cache file for " CUPS_SVERSION
"\n");
2118 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
2119 cupsFilePrintf(fp
, "NextJobId %d\n", NextJobId
);
2122 * Write each job known to the system...
2125 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
2127 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
2129 cupsFilePrintf(fp
, "<Job %d>\n", job
->id
);
2130 cupsFilePrintf(fp
, "State %d\n", job
->state_value
);
2131 cupsFilePrintf(fp
, "Priority %d\n", job
->priority
);
2132 cupsFilePrintf(fp
, "HoldUntil %d\n", (int)job
->hold_until
);
2133 cupsFilePrintf(fp
, "Username %s\n", job
->username
);
2134 cupsFilePrintf(fp
, "Destination %s\n", job
->dest
);
2135 cupsFilePrintf(fp
, "DestType %d\n", job
->dtype
);
2136 cupsFilePrintf(fp
, "NumFiles %d\n", job
->num_files
);
2137 for (i
= 0; i
< job
->num_files
; i
++)
2138 cupsFilePrintf(fp
, "File %d %s/%s %d\n", i
+ 1, job
->filetypes
[i
]->super
,
2139 job
->filetypes
[i
]->type
, job
->compressions
[i
]);
2140 cupsFilePuts(fp
, "</Job>\n");
2143 cupsdCloseCreatedConfFile(fp
, filename
);
2148 * 'cupsdSaveJob()' - Save a job to disk.
2152 cupsdSaveJob(cupsd_job_t
*job
) /* I - Job */
2154 char filename
[1024]; /* Job control filename */
2155 cups_file_t
*fp
; /* Job file */
2158 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSaveJob(job=%p(%d)): job->attrs=%p",
2159 job
, job
->id
, job
->attrs
);
2161 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
, job
->id
);
2163 if ((fp
= cupsdCreateConfFile(filename
, ConfigFilePerm
& 0600)) == NULL
)
2166 fchown(cupsFileNumber(fp
), RunUser
, Group
);
2168 job
->attrs
->state
= IPP_IDLE
;
2170 if (ippWriteIO(fp
, (ipp_iocb_t
)cupsFileWrite
, 1, NULL
,
2171 job
->attrs
) != IPP_DATA
)
2173 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Unable to write job control file.");
2178 if (!cupsdCloseCreatedConfFile(fp
, filename
))
2181 * Remove backup file and mark this job as clean...
2184 strlcat(filename
, ".O", sizeof(filename
));
2193 * 'cupsdSetJobHoldUntil()' - Set the hold time for a job.
2197 cupsdSetJobHoldUntil(cupsd_job_t
*job
, /* I - Job */
2198 const char *when
, /* I - When to resume */
2199 int update
)/* I - Update job-hold-until attr? */
2201 time_t curtime
; /* Current time */
2202 struct tm
*curdate
; /* Current date */
2203 int hour
; /* Hold hour */
2204 int minute
; /* Hold minute */
2205 int second
= 0; /* Hold second */
2208 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2209 "cupsdSetJobHoldUntil(job=%p(%d), when=\"%s\", update=%d)",
2210 job
, job
->id
, when
, update
);
2215 * Update the job-hold-until attribute...
2218 ipp_attribute_t
*attr
; /* job-hold-until attribute */
2220 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2221 IPP_TAG_KEYWORD
)) == NULL
)
2222 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2225 cupsdSetString(&(attr
->values
[0].string
.text
), when
);
2227 attr
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
2228 "job-hold-until", NULL
, when
);
2232 if (isdigit(when
[0] & 255))
2233 attr
->value_tag
= IPP_TAG_NAME
;
2235 attr
->value_tag
= IPP_TAG_KEYWORD
;
2238 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2243 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-hold-until-specified");
2246 * Update the hold time...
2249 job
->cancel_time
= 0;
2251 if (!strcmp(when
, "indefinite") || !strcmp(when
, "auth-info-required"))
2254 * Hold indefinitely...
2257 job
->hold_until
= 0;
2259 if (MaxHoldTime
> 0)
2260 job
->cancel_time
= time(NULL
) + MaxHoldTime
;
2262 else if (!strcmp(when
, "day-time"))
2265 * Hold to 6am the next morning unless local time is < 6pm.
2268 curtime
= time(NULL
);
2269 curdate
= localtime(&curtime
);
2271 if (curdate
->tm_hour
< 18)
2272 job
->hold_until
= curtime
;
2274 job
->hold_until
= curtime
+
2275 ((29 - curdate
->tm_hour
) * 60 + 59 -
2276 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2278 else if (!strcmp(when
, "evening") || !strcmp(when
, "night"))
2281 * Hold to 6pm unless local time is > 6pm or < 6am.
2284 curtime
= time(NULL
);
2285 curdate
= localtime(&curtime
);
2287 if (curdate
->tm_hour
< 6 || curdate
->tm_hour
>= 18)
2288 job
->hold_until
= curtime
;
2290 job
->hold_until
= curtime
+
2291 ((17 - curdate
->tm_hour
) * 60 + 59 -
2292 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2294 else if (!strcmp(when
, "second-shift"))
2297 * Hold to 4pm unless local time is > 4pm.
2300 curtime
= time(NULL
);
2301 curdate
= localtime(&curtime
);
2303 if (curdate
->tm_hour
>= 16)
2304 job
->hold_until
= curtime
;
2306 job
->hold_until
= curtime
+
2307 ((15 - curdate
->tm_hour
) * 60 + 59 -
2308 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2310 else if (!strcmp(when
, "third-shift"))
2313 * Hold to 12am unless local time is < 8am.
2316 curtime
= time(NULL
);
2317 curdate
= localtime(&curtime
);
2319 if (curdate
->tm_hour
< 8)
2320 job
->hold_until
= curtime
;
2322 job
->hold_until
= curtime
+
2323 ((23 - curdate
->tm_hour
) * 60 + 59 -
2324 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2326 else if (!strcmp(when
, "weekend"))
2329 * Hold to weekend unless we are in the weekend.
2332 curtime
= time(NULL
);
2333 curdate
= localtime(&curtime
);
2335 if (curdate
->tm_wday
== 0 || curdate
->tm_wday
== 6)
2336 job
->hold_until
= curtime
;
2338 job
->hold_until
= curtime
+
2339 (((5 - curdate
->tm_wday
) * 24 +
2340 (17 - curdate
->tm_hour
)) * 60 + 59 -
2341 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2343 else if (sscanf(when
, "%d:%d:%d", &hour
, &minute
, &second
) >= 2)
2346 * Hold to specified GMT time (HH:MM or HH:MM:SS)...
2349 curtime
= time(NULL
);
2350 curdate
= gmtime(&curtime
);
2352 job
->hold_until
= curtime
+
2353 ((hour
- curdate
->tm_hour
) * 60 + minute
-
2354 curdate
->tm_min
) * 60 + second
- curdate
->tm_sec
;
2357 * Hold until next day as needed...
2360 if (job
->hold_until
< curtime
)
2361 job
->hold_until
+= 24 * 60 * 60;
2364 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetJobHoldUntil: hold_until=%d",
2365 (int)job
->hold_until
);
2370 * 'cupsdSetJobPriority()' - Set the priority of a job, moving it up/down in
2371 * the list as needed.
2375 cupsdSetJobPriority(
2376 cupsd_job_t
*job
, /* I - Job ID */
2377 int priority
) /* I - New priority (0 to 100) */
2379 ipp_attribute_t
*attr
; /* Job attribute */
2383 * Don't change completed jobs...
2386 if (job
->state_value
>= IPP_JOB_PROCESSING
)
2390 * Set the new priority and re-add the job into the active list...
2393 cupsArrayRemove(ActiveJobs
, job
);
2395 job
->priority
= priority
;
2397 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
2398 IPP_TAG_INTEGER
)) != NULL
)
2399 attr
->values
[0].integer
= priority
;
2401 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-priority",
2404 cupsArrayAdd(ActiveJobs
, job
);
2407 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2412 * 'cupsdSetJobState()' - Set the state of the specified print job.
2417 cupsd_job_t
*job
, /* I - Job to cancel */
2418 ipp_jstate_t newstate
, /* I - New job state */
2419 cupsd_jobaction_t action
, /* I - Action to take */
2420 const char *message
, /* I - Message to log */
2421 ...) /* I - Additional arguments as needed */
2423 int i
; /* Looping var */
2424 ipp_jstate_t oldstate
; /* Old state */
2425 char filename
[1024]; /* Job filename */
2426 ipp_attribute_t
*attr
; /* Job attribute */
2429 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2430 "cupsdSetJobState(job=%p(%d), state=%d, newstate=%d, "
2431 "action=%d, message=\"%s\")", job
, job
->id
, job
->state_value
,
2432 newstate
, action
, message
? message
: "(null)");
2436 * Make sure we have the job attributes...
2439 if (!cupsdLoadJob(job
))
2443 * Don't do anything if the state is unchanged and we aren't purging the
2447 oldstate
= job
->state_value
;
2448 if (newstate
== oldstate
&& action
!= CUPSD_JOB_PURGE
)
2452 * Stop any processes that are working on the current job...
2455 if (oldstate
== IPP_JOB_PROCESSING
)
2456 stop_job(job
, action
);
2459 * Set the new job state...
2462 job
->state_value
= newstate
;
2465 job
->state
->values
[0].integer
= newstate
;
2469 case IPP_JOB_PENDING
:
2471 * Update job-hold-until as needed...
2474 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2475 IPP_TAG_KEYWORD
)) == NULL
)
2476 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2480 attr
->value_tag
= IPP_TAG_KEYWORD
;
2481 cupsdSetString(&(attr
->values
[0].string
.text
), "no-hold");
2487 case IPP_JOB_ABORTED
:
2488 case IPP_JOB_CANCELED
:
2489 case IPP_JOB_COMPLETED
:
2490 set_time(job
, "time-at-completed");
2491 ippSetString(job
->attrs
, &job
->reasons
, 0, "processing-to-stop-point");
2496 * Log message as needed...
2501 char buffer
[2048]; /* Message buffer */
2502 va_list ap
; /* Pointer to additional arguments */
2504 va_start(ap
, message
);
2505 vsnprintf(buffer
, sizeof(buffer
), message
, ap
);
2508 if (newstate
> IPP_JOB_STOPPED
)
2509 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
, "%s", buffer
);
2511 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
, "%s", buffer
);
2513 if (newstate
== IPP_JOB_STOPPED
|| newstate
== IPP_JOB_ABORTED
)
2514 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "%s", buffer
);
2516 cupsdLogJob(job
, CUPSD_LOG_INFO
, "%s", buffer
);
2520 * Handle post-state-change actions...
2525 case IPP_JOB_PROCESSING
:
2527 * Add the job to the "printing" list...
2530 if (!cupsArrayFind(PrintingJobs
, job
))
2531 cupsArrayAdd(PrintingJobs
, job
);
2534 * Set the processing time...
2537 set_time(job
, "time-at-processing");
2539 case IPP_JOB_PENDING
:
2541 case IPP_JOB_STOPPED
:
2543 * Make sure the job is in the active list...
2546 if (!cupsArrayFind(ActiveJobs
, job
))
2547 cupsArrayAdd(ActiveJobs
, job
);
2550 * Save the job state to disk...
2554 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2557 case IPP_JOB_ABORTED
:
2558 case IPP_JOB_CANCELED
:
2559 case IPP_JOB_COMPLETED
:
2560 if (newstate
== IPP_JOB_CANCELED
)
2563 * Remove the job from the active list if there are no processes still
2567 for (i
= 0; job
->filters
[i
] < 0; i
++);
2569 if (!job
->filters
[i
] && job
->backend
<= 0)
2570 cupsArrayRemove(ActiveJobs
, job
);
2575 * Otherwise just remove the job from the active list immediately...
2578 cupsArrayRemove(ActiveJobs
, job
);
2582 * Expire job subscriptions since the job is now "completed"...
2585 cupsdExpireSubscriptions(NULL
, job
);
2589 * If we are going to sleep and the PrintingJobs count is now 0, allow the
2590 * sleep to happen immediately...
2593 if (Sleeping
&& cupsArrayCount(PrintingJobs
) == 0)
2595 #endif /* __APPLE__ */
2598 * Remove any authentication data...
2601 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
2602 if (cupsdRemoveFile(filename
) && errno
!= ENOENT
)
2603 cupsdLogMessage(CUPSD_LOG_ERROR
,
2604 "Unable to remove authentication cache: %s",
2608 i
< (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0]));
2610 cupsdClearString(job
->auth_env
+ i
);
2612 cupsdClearString(&job
->auth_uid
);
2615 * Remove the print file for good if we aren't preserving jobs or
2619 if (!JobHistory
|| !JobFiles
|| action
== CUPSD_JOB_PURGE
)
2620 remove_job_files(job
);
2622 if (JobHistory
&& action
!= CUPSD_JOB_PURGE
)
2625 * Save job state info...
2629 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2631 else if (!job
->printer
)
2634 * Delete the job immediately if not actively printing...
2637 cupsdDeleteJob(job
, CUPSD_JOB_PURGE
);
2644 * Finalize the job immediately if we forced things...
2647 if (action
>= CUPSD_JOB_FORCE
&& job
&& job
->printer
)
2648 finalize_job(job
, 0);
2651 * Update the server "busy" state...
2654 cupsdSetBusyState();
2659 * 'cupsdStopAllJobs()' - Stop all print jobs.
2664 cupsd_jobaction_t action
, /* I - Action */
2665 int kill_delay
) /* I - Number of seconds before we kill */
2667 cupsd_job_t
*job
; /* Current job */
2670 DEBUG_puts("cupsdStopAllJobs()");
2672 for (job
= (cupsd_job_t
*)cupsArrayFirst(PrintingJobs
);
2674 job
= (cupsd_job_t
*)cupsArrayNext(PrintingJobs
))
2678 cupsdSetJobState(job
, IPP_JOB_COMPLETED
, CUPSD_JOB_FORCE
, NULL
);
2683 job
->kill_time
= time(NULL
) + kill_delay
;
2685 cupsdSetJobState(job
, IPP_JOB_PENDING
, action
, NULL
);
2692 * 'cupsdUnloadCompletedJobs()' - Flush completed job history from memory.
2696 cupsdUnloadCompletedJobs(void)
2698 cupsd_job_t
*job
; /* Current job */
2699 time_t expire
; /* Expiration time */
2702 expire
= time(NULL
) - 60;
2704 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
2706 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
2707 if (job
->attrs
&& job
->state_value
>= IPP_JOB_STOPPED
&& !job
->printer
&&
2708 job
->access_time
< expire
)
2719 * 'cupsdUpdateJobs()' - Update the history/file files for all jobs.
2723 cupsdUpdateJobs(void)
2725 cupsd_job_t
*job
; /* Current job */
2726 time_t curtime
; /* Current time */
2727 ipp_attribute_t
*attr
; /* time-at-completed attribute */
2730 curtime
= time(NULL
);
2731 JobHistoryUpdate
= 0;
2733 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
2735 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
2737 if (job
->state_value
>= IPP_JOB_CANCELED
&&
2738 (attr
= ippFindAttribute(job
->attrs
, "time-at-completed",
2739 IPP_TAG_INTEGER
)) != NULL
)
2742 * Update history/file expiration times...
2745 if (JobHistory
< INT_MAX
)
2746 job
->history_time
= attr
->values
[0].integer
+ JobHistory
;
2748 job
->history_time
= INT_MAX
;
2750 if (job
->history_time
< curtime
)
2752 cupsdDeleteJob(job
, CUPSD_JOB_PURGE
);
2756 if (job
->history_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
2757 JobHistoryUpdate
= job
->history_time
;
2759 if (JobFiles
< INT_MAX
)
2760 job
->file_time
= attr
->values
[0].integer
+ JobFiles
;
2762 job
->file_time
= INT_MAX
;
2764 if (job
->file_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
2765 JobHistoryUpdate
= job
->file_time
;
2769 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdUpdateAllJobs: JobHistoryUpdate=%ld",
2770 (long)JobHistoryUpdate
);
2775 * 'compare_active_jobs()' - Compare the job IDs and priorities of two jobs.
2778 static int /* O - Difference */
2779 compare_active_jobs(void *first
, /* I - First job */
2780 void *second
, /* I - Second job */
2781 void *data
) /* I - App data (not used) */
2783 int diff
; /* Difference */
2788 if ((diff
= ((cupsd_job_t
*)second
)->priority
-
2789 ((cupsd_job_t
*)first
)->priority
) != 0)
2792 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
2797 * 'compare_jobs()' - Compare the job IDs of two jobs.
2800 static int /* O - Difference */
2801 compare_jobs(void *first
, /* I - First job */
2802 void *second
, /* I - Second job */
2803 void *data
) /* I - App data (not used) */
2807 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
2812 * 'dump_job_history()' - Dump any debug messages for a job.
2816 dump_job_history(cupsd_job_t
*job
) /* I - Job */
2818 int i
, /* Looping var */
2819 oldsize
; /* Current MaxLogSize */
2820 struct tm
*date
; /* Date/time value */
2821 cupsd_joblog_t
*message
; /* Current message */
2822 char temp
[2048], /* Log message */
2823 *ptr
, /* Pointer into log message */
2824 start
[256], /* Start time */
2825 end
[256]; /* End time */
2826 cupsd_printer_t
*printer
; /* Printer for job */
2830 * See if we have anything to dump...
2837 * Disable log rotation temporarily...
2840 oldsize
= MaxLogSize
;
2844 * Copy the debug messages to the log...
2847 message
= (cupsd_joblog_t
*)cupsArrayFirst(job
->history
);
2848 date
= localtime(&(message
->time
));
2849 strftime(start
, sizeof(start
), "%X", date
);
2851 message
= (cupsd_joblog_t
*)cupsArrayLast(job
->history
);
2852 date
= localtime(&(message
->time
));
2853 strftime(end
, sizeof(end
), "%X", date
);
2855 snprintf(temp
, sizeof(temp
),
2856 "[Job %d] The following messages were recorded from %s to %s",
2857 job
->id
, start
, end
);
2858 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, temp
);
2860 for (message
= (cupsd_joblog_t
*)cupsArrayFirst(job
->history
);
2862 message
= (cupsd_joblog_t
*)cupsArrayNext(job
->history
))
2863 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, message
->message
);
2865 snprintf(temp
, sizeof(temp
), "[Job %d] End of messages", job
->id
);
2866 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, temp
);
2869 * Log the printer state values...
2872 if ((printer
= job
->printer
) == NULL
)
2873 printer
= cupsdFindDest(job
->dest
);
2877 snprintf(temp
, sizeof(temp
), "[Job %d] printer-state=%d(%s)", job
->id
,
2879 printer
->state
== IPP_PRINTER_IDLE
? "idle" :
2880 printer
->state
== IPP_PRINTER_PROCESSING
? "processing" :
2882 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, temp
);
2884 snprintf(temp
, sizeof(temp
), "[Job %d] printer-state-message=\"%s\"",
2885 job
->id
, printer
->state_message
);
2886 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, temp
);
2888 snprintf(temp
, sizeof(temp
), "[Job %d] printer-state-reasons=", job
->id
);
2889 ptr
= temp
+ strlen(temp
);
2890 if (printer
->num_reasons
== 0)
2891 strlcpy(ptr
, "none", sizeof(temp
) - (size_t)(ptr
- temp
));
2895 i
< printer
->num_reasons
&& ptr
< (temp
+ sizeof(temp
) - 2);
2901 strlcpy(ptr
, printer
->reasons
[i
], sizeof(temp
) - (size_t)(ptr
- temp
));
2905 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, temp
);
2909 * Restore log file rotation...
2912 MaxLogSize
= oldsize
;
2915 * Free all messages...
2918 free_job_history(job
);
2923 * 'free_job_history()' - Free any log history.
2927 free_job_history(cupsd_job_t
*job
) /* I - Job */
2929 char *message
; /* Current message */
2935 for (message
= (char *)cupsArrayFirst(job
->history
);
2937 message
= (char *)cupsArrayNext(job
->history
))
2940 cupsArrayDelete(job
->history
);
2941 job
->history
= NULL
;
2946 * 'finalize_job()' - Cleanup after job filter processes and support data.
2950 finalize_job(cupsd_job_t
*job
, /* I - Job */
2951 int set_job_state
) /* I - 1 = set the job state */
2953 ipp_pstate_t printer_state
; /* New printer state value */
2954 ipp_jstate_t job_state
; /* New job state value */
2955 const char *message
; /* Message for job state */
2956 char buffer
[1024]; /* Buffer for formatted messages */
2959 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "finalize_job(job=%p(%d))", job
, job
->id
);
2962 * Clear the "connecting-to-device" and "cups-waiting-for-job-completed"
2963 * reasons, which are only valid when a printer is processing, along with any
2964 * remote printing job state...
2967 cupsdSetPrinterReasons(job
->printer
, "-connecting-to-device,"
2968 "cups-waiting-for-job-completed,"
2969 "cups-remote-pending,"
2970 "cups-remote-pending-held,"
2971 "cups-remote-processing,"
2972 "cups-remote-stopped,"
2973 "cups-remote-canceled,"
2974 "cups-remote-aborted,"
2975 "cups-remote-completed");
2978 * Similarly, clear the "offline-report" reason for non-USB devices since we
2979 * rarely have current information for network devices...
2982 if (strncmp(job
->printer
->device_uri
, "usb:", 4) &&
2983 strncmp(job
->printer
->device_uri
, "ippusb:", 7))
2984 cupsdSetPrinterReasons(job
->printer
, "-offline-report");
2987 * Free the security profile...
2990 cupsdDestroyProfile(job
->profile
);
2991 job
->profile
= NULL
;
2992 cupsdDestroyProfile(job
->bprofile
);
2993 job
->bprofile
= NULL
;
2996 * Clear the unresponsive job watchdog timers...
2999 job
->cancel_time
= 0;
3003 * Close pipes and status buffer...
3006 cupsdClosePipe(job
->print_pipes
);
3007 cupsdClosePipe(job
->back_pipes
);
3008 cupsdClosePipe(job
->side_pipes
);
3010 cupsdRemoveSelect(job
->status_pipes
[0]);
3011 cupsdClosePipe(job
->status_pipes
);
3012 cupsdStatBufDelete(job
->status_buffer
);
3013 job
->status_buffer
= NULL
;
3016 * Process the exit status...
3019 if (job
->printer
->state
== IPP_PRINTER_PROCESSING
)
3020 printer_state
= IPP_PRINTER_IDLE
;
3022 printer_state
= job
->printer
->state
;
3024 switch (job_state
= job
->state_value
)
3026 case IPP_JOB_PENDING
:
3027 message
= "Job paused.";
3031 message
= "Job held.";
3035 case IPP_JOB_PROCESSING
:
3036 case IPP_JOB_COMPLETED
:
3037 job_state
= IPP_JOB_COMPLETED
;
3038 message
= "Job completed.";
3041 ippSetString(job
->attrs
, &job
->reasons
, 0,
3042 "job-completed-successfully");
3045 case IPP_JOB_STOPPED
:
3046 message
= "Job stopped.";
3048 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-stopped");
3051 case IPP_JOB_CANCELED
:
3052 message
= "Job canceled.";
3054 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-canceled-by-user");
3057 case IPP_JOB_ABORTED
:
3058 message
= "Job aborted.";
3062 if (job
->status
< 0)
3065 * Backend had errors...
3068 int exit_code
; /* Exit code from backend */
3071 * Convert the status to an exit code. Due to the way the W* macros are
3072 * implemented on MacOS X (bug?), we have to store the exit status in a
3073 * variable first and then convert...
3076 exit_code
= -job
->status
;
3077 if (WIFEXITED(exit_code
))
3078 exit_code
= WEXITSTATUS(exit_code
);
3081 ippSetString(job
->attrs
, &job
->reasons
, 0, "cups-backend-crashed");
3082 exit_code
= job
->status
;
3085 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Backend returned status %d (%s)",
3087 exit_code
== CUPS_BACKEND_FAILED
? "failed" :
3088 exit_code
== CUPS_BACKEND_AUTH_REQUIRED
?
3089 "authentication required" :
3090 exit_code
== CUPS_BACKEND_HOLD
? "hold job" :
3091 exit_code
== CUPS_BACKEND_STOP
? "stop printer" :
3092 exit_code
== CUPS_BACKEND_CANCEL
? "cancel job" :
3093 exit_code
== CUPS_BACKEND_RETRY
? "retry job later" :
3094 exit_code
== CUPS_BACKEND_RETRY_CURRENT
? "retry job immediately" :
3095 exit_code
< 0 ? "crashed" : "unknown");
3098 * Do what needs to be done...
3104 case CUPS_BACKEND_FAILED
:
3106 * Backend failure, use the error-policy to determine how to
3110 if (job
->dtype
& CUPS_PRINTER_CLASS
)
3113 * Queued on a class - mark the job as pending and we'll retry on
3114 * another printer...
3117 if (job_state
== IPP_JOB_COMPLETED
)
3119 job_state
= IPP_JOB_PENDING
;
3120 message
= "Retrying job on another printer.";
3122 ippSetString(job
->attrs
, &job
->reasons
, 0,
3123 "resources-are-not-ready");
3126 else if (!strcmp(job
->printer
->error_policy
, "retry-current-job"))
3129 * The error policy is "retry-current-job" - mark the job as pending
3130 * and we'll retry on the same printer...
3133 if (job_state
== IPP_JOB_COMPLETED
)
3135 job_state
= IPP_JOB_PENDING
;
3136 message
= "Retrying job on same printer.";
3138 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
3141 else if ((job
->printer
->type
& CUPS_PRINTER_FAX
) ||
3142 !strcmp(job
->printer
->error_policy
, "retry-job"))
3144 if (job_state
== IPP_JOB_COMPLETED
)
3147 * The job was queued on a fax or the error policy is "retry-job" -
3148 * hold the job if the number of retries is less than the
3149 * JobRetryLimit, otherwise abort the job.
3154 if (job
->tries
> JobRetryLimit
&& JobRetryLimit
> 0)
3160 snprintf(buffer
, sizeof(buffer
),
3161 "Job aborted after %d unsuccessful attempts.",
3163 job_state
= IPP_JOB_ABORTED
;
3166 ippSetString(job
->attrs
, &job
->reasons
, 0, "aborted-by-system");
3171 * Try again in N seconds...
3174 snprintf(buffer
, sizeof(buffer
),
3175 "Job held for %d seconds since it could not be sent.",
3178 job
->hold_until
= time(NULL
) + JobRetryInterval
;
3179 job_state
= IPP_JOB_HELD
;
3182 ippSetString(job
->attrs
, &job
->reasons
, 0,
3183 "resources-are-not-ready");
3187 else if (!strcmp(job
->printer
->error_policy
, "abort-job") &&
3188 job_state
== IPP_JOB_COMPLETED
)
3190 job_state
= IPP_JOB_ABORTED
;
3191 message
= "Job aborted due to backend errors; please consult "
3192 "the error_log file for details.";
3194 ippSetString(job
->attrs
, &job
->reasons
, 0, "aborted-by-system");
3196 else if (job
->state_value
== IPP_JOB_PROCESSING
)
3198 job_state
= IPP_JOB_PENDING
;
3199 printer_state
= IPP_PRINTER_STOPPED
;
3200 message
= "Printer stopped due to backend errors; please "
3201 "consult the error_log file for details.";
3203 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
3207 case CUPS_BACKEND_CANCEL
:
3212 if (job_state
== IPP_JOB_COMPLETED
)
3214 job_state
= IPP_JOB_CANCELED
;
3215 message
= "Job canceled at printer.";
3217 ippSetString(job
->attrs
, &job
->reasons
, 0, "canceled-at-device");
3221 case CUPS_BACKEND_HOLD
:
3222 if (job_state
== IPP_JOB_COMPLETED
)
3228 const char *reason
= ippGetString(job
->reasons
, 0, NULL
);
3230 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "job-state-reasons=\"%s\"",
3233 if (!reason
|| strncmp(reason
, "account-", 8))
3235 cupsdSetJobHoldUntil(job
, "indefinite", 1);
3237 ippSetString(job
->attrs
, &job
->reasons
, 0,
3238 "job-hold-until-specified");
3239 message
= "Job held indefinitely due to backend errors; please "
3240 "consult the error_log file for details.";
3242 else if (!strcmp(reason
, "account-info-needed"))
3244 cupsdSetJobHoldUntil(job
, "indefinite", 0);
3246 message
= "Job held indefinitely - account information is "
3249 else if (!strcmp(reason
, "account-closed"))
3251 cupsdSetJobHoldUntil(job
, "indefinite", 0);
3253 message
= "Job held indefinitely - account has been closed.";
3255 else if (!strcmp(reason
, "account-limit-reached"))
3257 cupsdSetJobHoldUntil(job
, "indefinite", 0);
3259 message
= "Job held indefinitely - account limit has been "
3264 cupsdSetJobHoldUntil(job
, "indefinite", 0);
3266 message
= "Job held indefinitely - account authorization failed.";
3269 job_state
= IPP_JOB_HELD
;
3273 case CUPS_BACKEND_STOP
:
3275 * Stop the printer...
3278 printer_state
= IPP_PRINTER_STOPPED
;
3279 message
= "Printer stopped due to backend errors; please "
3280 "consult the error_log file for details.";
3282 if (job_state
== IPP_JOB_COMPLETED
)
3284 job_state
= IPP_JOB_PENDING
;
3286 ippSetString(job
->attrs
, &job
->reasons
, 0,
3287 "resources-are-not-ready");
3291 case CUPS_BACKEND_AUTH_REQUIRED
:
3293 * Hold the job for authentication...
3296 if (job_state
== IPP_JOB_COMPLETED
)
3298 cupsdSetJobHoldUntil(job
, "auth-info-required", 1);
3300 job_state
= IPP_JOB_HELD
;
3301 message
= "Job held for authentication.";
3303 if (strncmp(job
->reasons
->values
[0].string
.text
, "account-", 8))
3304 ippSetString(job
->attrs
, &job
->reasons
, 0,
3305 "cups-held-for-authentication");
3309 case CUPS_BACKEND_RETRY
:
3310 if (job_state
== IPP_JOB_COMPLETED
)
3313 * Hold the job if the number of retries is less than the
3314 * JobRetryLimit, otherwise abort the job.
3319 if (job
->tries
> JobRetryLimit
&& JobRetryLimit
> 0)
3325 snprintf(buffer
, sizeof(buffer
),
3326 "Job aborted after %d unsuccessful attempts.",
3328 job_state
= IPP_JOB_ABORTED
;
3331 ippSetString(job
->attrs
, &job
->reasons
, 0, "aborted-by-system");
3336 * Try again in N seconds...
3339 snprintf(buffer
, sizeof(buffer
),
3340 "Job held for %d seconds since it could not be sent.",
3343 job
->hold_until
= time(NULL
) + JobRetryInterval
;
3344 job_state
= IPP_JOB_HELD
;
3347 ippSetString(job
->attrs
, &job
->reasons
, 0,
3348 "resources-are-not-ready");
3353 case CUPS_BACKEND_RETRY_CURRENT
:
3355 * Mark the job as pending and retry on the same printer...
3358 if (job_state
== IPP_JOB_COMPLETED
)
3360 job_state
= IPP_JOB_PENDING
;
3361 message
= "Retrying job on same printer.";
3363 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
3368 else if (job
->status
> 0)
3371 * Filter had errors; stop job...
3374 if (job_state
== IPP_JOB_COMPLETED
)
3376 job_state
= IPP_JOB_STOPPED
;
3377 message
= "Job stopped due to filter errors; please consult the "
3378 "error_log file for details.";
3380 if (WIFSIGNALED(job
->status
))
3381 ippSetString(job
->attrs
, &job
->reasons
, 0, "cups-filter-crashed");
3383 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-completed-with-errors");
3388 * Update the printer and job state.
3391 if (set_job_state
&& job_state
!= job
->state_value
)
3392 cupsdSetJobState(job
, job_state
, CUPSD_JOB_DEFAULT
, "%s", message
);
3394 cupsdSetPrinterState(job
->printer
, printer_state
,
3395 printer_state
== IPP_PRINTER_STOPPED
);
3396 update_job_attrs(job
, 0);
3401 (job
->state_value
== IPP_JOB_ABORTED
||
3402 job
->state_value
== IPP_JOB_STOPPED
))
3403 dump_job_history(job
);
3405 free_job_history(job
);
3408 cupsArrayRemove(PrintingJobs
, job
);
3411 * Apply any PPD updates...
3414 if (job
->num_keywords
)
3416 if (cupsdUpdatePrinterPPD(job
->printer
, job
->num_keywords
, job
->keywords
))
3417 cupsdSetPrinterAttrs(job
->printer
);
3419 cupsFreeOptions(job
->num_keywords
, job
->keywords
);
3421 job
->num_keywords
= 0;
3422 job
->keywords
= NULL
;
3426 * Clear the printer <-> job association...
3429 job
->printer
->job
= NULL
;
3430 job
->printer
= NULL
;
3433 * Try printing another job...
3436 if (printer_state
!= IPP_PRINTER_STOPPED
)
3442 * 'get_options()' - Get a string containing the job options.
3445 static char * /* O - Options string */
3446 get_options(cupsd_job_t
*job
, /* I - Job */
3447 int banner_page
, /* I - Printing a banner page? */
3448 char *copies
, /* I - Copies buffer */
3449 size_t copies_size
, /* I - Size of copies buffer */
3450 char *title
, /* I - Title buffer */
3451 size_t title_size
) /* I - Size of title buffer */
3453 int i
; /* Looping var */
3454 size_t newlength
; /* New option buffer length */
3455 char *optptr
, /* Pointer to options */
3456 *valptr
; /* Pointer in value string */
3457 ipp_attribute_t
*attr
; /* Current attribute */
3458 _ppd_cache_t
*pc
; /* PPD cache and mapping data */
3459 int num_pwgppds
; /* Number of PWG->PPD options */
3460 cups_option_t
*pwgppds
, /* PWG->PPD options */
3461 *pwgppd
, /* Current PWG->PPD option */
3462 *preset
; /* Current preset option */
3463 int print_color_mode
,
3464 /* Output mode (if any) */
3465 print_quality
; /* Print quality (if any) */
3466 const char *ppd
; /* PPD option choice */
3467 int exact
; /* Did we get an exact match? */
3468 static char *options
= NULL
;/* Full list of options */
3469 static size_t optlength
= 0; /* Length of option buffer */
3473 * Building the options string is harder than it needs to be, but for the
3474 * moment we need to pass strings for command-line args and not IPP attribute
3477 * First build an options array for any PWG->PPD mapped option/choice pairs.
3480 pc
= job
->printer
->pc
;
3485 !ippFindAttribute(job
->attrs
,
3486 "com.apple.print.DocumentTicket.PMSpoolFormat",
3488 !ippFindAttribute(job
->attrs
, "APPrinterPreset", IPP_TAG_ZERO
) &&
3489 (ippFindAttribute(job
->attrs
, "print-color-mode", IPP_TAG_ZERO
) ||
3490 ippFindAttribute(job
->attrs
, "print-quality", IPP_TAG_ZERO
)))
3493 * Map print-color-mode and print-quality to a preset...
3496 if ((attr
= ippFindAttribute(job
->attrs
, "print-color-mode",
3497 IPP_TAG_KEYWORD
)) != NULL
&&
3498 !strcmp(attr
->values
[0].string
.text
, "monochrome"))
3499 print_color_mode
= _PWG_PRINT_COLOR_MODE_MONOCHROME
;
3501 print_color_mode
= _PWG_PRINT_COLOR_MODE_COLOR
;
3503 if ((attr
= ippFindAttribute(job
->attrs
, "print-quality",
3504 IPP_TAG_ENUM
)) != NULL
&&
3505 attr
->values
[0].integer
>= IPP_QUALITY_DRAFT
&&
3506 attr
->values
[0].integer
<= IPP_QUALITY_HIGH
)
3507 print_quality
= attr
->values
[0].integer
- IPP_QUALITY_DRAFT
;
3509 print_quality
= _PWG_PRINT_QUALITY_NORMAL
;
3511 if (pc
->num_presets
[print_color_mode
][print_quality
] == 0)
3514 * Try to find a preset that works so that we maximize the chances of us
3515 * getting a good print using IPP attributes.
3518 if (pc
->num_presets
[print_color_mode
][_PWG_PRINT_QUALITY_NORMAL
] > 0)
3519 print_quality
= _PWG_PRINT_QUALITY_NORMAL
;
3520 else if (pc
->num_presets
[_PWG_PRINT_COLOR_MODE_COLOR
][print_quality
] > 0)
3521 print_color_mode
= _PWG_PRINT_COLOR_MODE_COLOR
;
3524 print_quality
= _PWG_PRINT_QUALITY_NORMAL
;
3525 print_color_mode
= _PWG_PRINT_COLOR_MODE_COLOR
;
3529 if (pc
->num_presets
[print_color_mode
][print_quality
] > 0)
3532 * Copy the preset options as long as the corresponding names are not
3533 * already defined in the IPP request...
3536 for (i
= pc
->num_presets
[print_color_mode
][print_quality
],
3537 preset
= pc
->presets
[print_color_mode
][print_quality
];
3541 if (!ippFindAttribute(job
->attrs
, preset
->name
, IPP_TAG_ZERO
))
3542 num_pwgppds
= cupsAddOption(preset
->name
, preset
->value
, num_pwgppds
,
3550 if (!ippFindAttribute(job
->attrs
, "InputSlot", IPP_TAG_ZERO
) &&
3551 !ippFindAttribute(job
->attrs
, "HPPaperSource", IPP_TAG_ZERO
))
3553 if ((ppd
= _ppdCacheGetInputSlot(pc
, job
->attrs
, NULL
)) != NULL
)
3554 num_pwgppds
= cupsAddOption(pc
->source_option
, ppd
, num_pwgppds
,
3557 if (!ippFindAttribute(job
->attrs
, "MediaType", IPP_TAG_ZERO
) &&
3558 (ppd
= _ppdCacheGetMediaType(pc
, job
->attrs
, NULL
)) != NULL
)
3559 num_pwgppds
= cupsAddOption("MediaType", ppd
, num_pwgppds
, &pwgppds
);
3561 if (!ippFindAttribute(job
->attrs
, "PageRegion", IPP_TAG_ZERO
) &&
3562 !ippFindAttribute(job
->attrs
, "PageSize", IPP_TAG_ZERO
) &&
3563 (ppd
= _ppdCacheGetPageSize(pc
, job
->attrs
, NULL
, &exact
)) != NULL
)
3565 num_pwgppds
= cupsAddOption("PageSize", ppd
, num_pwgppds
, &pwgppds
);
3567 if (!ippFindAttribute(job
->attrs
, "media", IPP_TAG_ZERO
))
3568 num_pwgppds
= cupsAddOption("media", ppd
, num_pwgppds
, &pwgppds
);
3571 if (!ippFindAttribute(job
->attrs
, "OutputBin", IPP_TAG_ZERO
) &&
3572 (attr
= ippFindAttribute(job
->attrs
, "output-bin",
3573 IPP_TAG_ZERO
)) != NULL
&&
3574 (attr
->value_tag
== IPP_TAG_KEYWORD
||
3575 attr
->value_tag
== IPP_TAG_NAME
) &&
3576 (ppd
= _ppdCacheGetOutputBin(pc
, attr
->values
[0].string
.text
)) != NULL
)
3579 * Map output-bin to OutputBin option...
3582 num_pwgppds
= cupsAddOption("OutputBin", ppd
, num_pwgppds
, &pwgppds
);
3585 if (pc
->sides_option
&&
3586 !ippFindAttribute(job
->attrs
, pc
->sides_option
, IPP_TAG_ZERO
) &&
3587 (attr
= ippFindAttribute(job
->attrs
, "sides", IPP_TAG_KEYWORD
)) != NULL
)
3590 * Map sides to duplex option...
3593 if (!strcmp(attr
->values
[0].string
.text
, "one-sided"))
3594 num_pwgppds
= cupsAddOption(pc
->sides_option
, pc
->sides_1sided
,
3595 num_pwgppds
, &pwgppds
);
3596 else if (!strcmp(attr
->values
[0].string
.text
, "two-sided-long-edge"))
3597 num_pwgppds
= cupsAddOption(pc
->sides_option
, pc
->sides_2sided_long
,
3598 num_pwgppds
, &pwgppds
);
3599 else if (!strcmp(attr
->values
[0].string
.text
, "two-sided-short-edge"))
3600 num_pwgppds
= cupsAddOption(pc
->sides_option
, pc
->sides_2sided_short
,
3601 num_pwgppds
, &pwgppds
);
3605 * Map finishings values...
3608 num_pwgppds
= _ppdCacheGetFinishingOptions(pc
, job
->attrs
,
3609 IPP_FINISHINGS_NONE
, num_pwgppds
,
3614 * Figure out how much room we need...
3617 newlength
= ipp_length(job
->attrs
);
3619 for (i
= num_pwgppds
, pwgppd
= pwgppds
; i
> 0; i
--, pwgppd
++)
3620 newlength
+= 1 + strlen(pwgppd
->name
) + 1 + strlen(pwgppd
->value
);
3623 * Then allocate/reallocate the option buffer as needed...
3626 if (newlength
== 0) /* This can never happen, but Clang */
3627 newlength
= 1; /* thinks it can... */
3629 if (newlength
> optlength
|| !options
)
3632 optptr
= malloc(newlength
);
3634 optptr
= realloc(options
, newlength
);
3638 cupsdLogJob(job
, CUPSD_LOG_CRIT
,
3639 "Unable to allocate " CUPS_LLFMT
" bytes for option buffer.",
3640 CUPS_LLCAST newlength
);
3645 optlength
= newlength
;
3649 * Now loop through the attributes and convert them to the textual
3650 * representation used by the filters...
3656 snprintf(title
, title_size
, "%s-%d", job
->printer
->name
, job
->id
);
3657 strlcpy(copies
, "1", copies_size
);
3659 for (attr
= job
->attrs
->attrs
; attr
!= NULL
; attr
= attr
->next
)
3661 if (!strcmp(attr
->name
, "copies") &&
3662 attr
->value_tag
== IPP_TAG_INTEGER
)
3665 * Don't use the # copies attribute if we are printing the job sheets...
3669 snprintf(copies
, copies_size
, "%d", attr
->values
[0].integer
);
3671 else if (!strcmp(attr
->name
, "job-name") &&
3672 (attr
->value_tag
== IPP_TAG_NAME
||
3673 attr
->value_tag
== IPP_TAG_NAMELANG
))
3674 strlcpy(title
, attr
->values
[0].string
.text
, title_size
);
3675 else if (attr
->group_tag
== IPP_TAG_JOB
)
3678 * Filter out other unwanted attributes...
3681 if (attr
->value_tag
== IPP_TAG_NOVALUE
||
3682 attr
->value_tag
== IPP_TAG_MIMETYPE
||
3683 attr
->value_tag
== IPP_TAG_NAMELANG
||
3684 attr
->value_tag
== IPP_TAG_TEXTLANG
||
3685 (attr
->value_tag
== IPP_TAG_URI
&& strcmp(attr
->name
, "job-uuid") &&
3686 strcmp(attr
->name
, "job-authorization-uri")) ||
3687 attr
->value_tag
== IPP_TAG_URISCHEME
||
3688 attr
->value_tag
== IPP_TAG_BEGIN_COLLECTION
) /* Not yet supported */
3691 if (!strcmp(attr
->name
, "job-hold-until") ||
3692 !strcmp(attr
->name
, "job-id") ||
3693 !strcmp(attr
->name
, "job-k-octets") ||
3694 !strcmp(attr
->name
, "job-media-sheets") ||
3695 !strcmp(attr
->name
, "job-media-sheets-completed") ||
3696 !strcmp(attr
->name
, "job-state") ||
3697 !strcmp(attr
->name
, "job-state-reasons"))
3700 if (!strncmp(attr
->name
, "job-", 4) &&
3701 strcmp(attr
->name
, "job-account-id") &&
3702 strcmp(attr
->name
, "job-accounting-user-id") &&
3703 strcmp(attr
->name
, "job-authorization-uri") &&
3704 strcmp(attr
->name
, "job-billing") &&
3705 strcmp(attr
->name
, "job-impressions") &&
3706 strcmp(attr
->name
, "job-originating-host-name") &&
3707 strcmp(attr
->name
, "job-password") &&
3708 strcmp(attr
->name
, "job-password-encryption") &&
3709 strcmp(attr
->name
, "job-uuid") &&
3710 !(job
->printer
->type
& CUPS_PRINTER_REMOTE
))
3713 if ((!strcmp(attr
->name
, "job-impressions") ||
3714 !strcmp(attr
->name
, "page-label") ||
3715 !strcmp(attr
->name
, "page-border") ||
3716 !strncmp(attr
->name
, "number-up", 9) ||
3717 !strcmp(attr
->name
, "page-ranges") ||
3718 !strcmp(attr
->name
, "page-set") ||
3719 !_cups_strcasecmp(attr
->name
, "AP_FIRSTPAGE_InputSlot") ||
3720 !_cups_strcasecmp(attr
->name
, "AP_FIRSTPAGE_ManualFeed") ||
3721 !_cups_strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
3722 "PMTotalSidesImaged..n.") ||
3723 !_cups_strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
3724 "PMTotalBeginPages..n.")) &&
3729 * Otherwise add them to the list...
3732 if (optptr
> options
)
3733 strlcat(optptr
, " ", optlength
- (size_t)(optptr
- options
));
3735 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
3737 strlcat(optptr
, attr
->name
, optlength
- (size_t)(optptr
- options
));
3738 strlcat(optptr
, "=", optlength
- (size_t)(optptr
- options
));
3741 for (i
= 0; i
< attr
->num_values
; i
++)
3744 strlcat(optptr
, ",", optlength
- (size_t)(optptr
- options
));
3746 optptr
+= strlen(optptr
);
3748 switch (attr
->value_tag
)
3750 case IPP_TAG_INTEGER
:
3752 snprintf(optptr
, optlength
- (size_t)(optptr
- options
),
3753 "%d", attr
->values
[i
].integer
);
3756 case IPP_TAG_BOOLEAN
:
3757 if (!attr
->values
[i
].boolean
)
3758 strlcat(optptr
, "no", optlength
- (size_t)(optptr
- options
));
3760 strlcat(optptr
, attr
->name
, optlength
- (size_t)(optptr
- options
));
3763 case IPP_TAG_RANGE
:
3764 if (attr
->values
[i
].range
.lower
== attr
->values
[i
].range
.upper
)
3765 snprintf(optptr
, optlength
- (size_t)(optptr
- options
) - 1,
3766 "%d", attr
->values
[i
].range
.lower
);
3768 snprintf(optptr
, optlength
- (size_t)(optptr
- options
) - 1,
3769 "%d-%d", attr
->values
[i
].range
.lower
,
3770 attr
->values
[i
].range
.upper
);
3773 case IPP_TAG_RESOLUTION
:
3774 snprintf(optptr
, optlength
- (size_t)(optptr
- options
) - 1,
3775 "%dx%d%s", attr
->values
[i
].resolution
.xres
,
3776 attr
->values
[i
].resolution
.yres
,
3777 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
3781 case IPP_TAG_STRING
:
3784 case IPP_TAG_KEYWORD
:
3785 case IPP_TAG_CHARSET
:
3786 case IPP_TAG_LANGUAGE
:
3788 for (valptr
= attr
->values
[i
].string
.text
; *valptr
;)
3790 if (strchr(" \t\n\\\'\"", *valptr
))
3792 *optptr
++ = *valptr
++;
3799 break; /* anti-compiler-warning-code */
3803 optptr
+= strlen(optptr
);
3808 * Finally loop through the PWG->PPD mapped options and add them...
3811 for (i
= num_pwgppds
, pwgppd
= pwgppds
; i
> 0; i
--, pwgppd
++)
3814 strlcpy(optptr
, pwgppd
->name
, optlength
- (size_t)(optptr
- options
));
3815 optptr
+= strlen(optptr
);
3817 strlcpy(optptr
, pwgppd
->value
, optlength
- (size_t)(optptr
- options
));
3818 optptr
+= strlen(optptr
);
3821 cupsFreeOptions(num_pwgppds
, pwgppds
);
3824 * Return the options string...
3832 * 'ipp_length()' - Compute the size of the buffer needed to hold
3833 * the textual IPP attributes.
3836 static size_t /* O - Size of attribute buffer */
3837 ipp_length(ipp_t
*ipp
) /* I - IPP request */
3839 size_t bytes
; /* Number of bytes */
3840 int i
; /* Looping var */
3841 ipp_attribute_t
*attr
; /* Current attribute */
3845 * Loop through all attributes...
3850 for (attr
= ipp
->attrs
; attr
!= NULL
; attr
= attr
->next
)
3853 * Skip attributes that won't be sent to filters...
3856 if (attr
->value_tag
== IPP_TAG_NOVALUE
||
3857 attr
->value_tag
== IPP_TAG_MIMETYPE
||
3858 attr
->value_tag
== IPP_TAG_NAMELANG
||
3859 attr
->value_tag
== IPP_TAG_TEXTLANG
||
3860 attr
->value_tag
== IPP_TAG_URI
||
3861 attr
->value_tag
== IPP_TAG_URISCHEME
)
3865 * Add space for a leading space and commas between each value.
3866 * For the first attribute, the leading space isn't used, so the
3867 * extra byte can be used as the nul terminator...
3870 bytes
++; /* " " separator */
3871 bytes
+= (size_t)attr
->num_values
; /* "," separators */
3874 * Boolean attributes appear as "foo,nofoo,foo,nofoo", while
3875 * other attributes appear as "foo=value1,value2,...,valueN".
3878 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
3879 bytes
+= strlen(attr
->name
);
3881 bytes
+= (size_t)attr
->num_values
* strlen(attr
->name
);
3884 * Now add the size required for each value in the attribute...
3887 switch (attr
->value_tag
)
3889 case IPP_TAG_INTEGER
:
3892 * Minimum value of a signed integer is -2147483647, or 11 digits.
3895 bytes
+= (size_t)attr
->num_values
* 11;
3898 case IPP_TAG_BOOLEAN
:
3900 * Add two bytes for each false ("no") value...
3903 for (i
= 0; i
< attr
->num_values
; i
++)
3904 if (!attr
->values
[i
].boolean
)
3908 case IPP_TAG_RANGE
:
3910 * A range is two signed integers separated by a hyphen, or
3911 * 23 characters max.
3914 bytes
+= (size_t)attr
->num_values
* 23;
3917 case IPP_TAG_RESOLUTION
:
3919 * A resolution is two signed integers separated by an "x" and
3920 * suffixed by the units, or 26 characters max.
3923 bytes
+= (size_t)attr
->num_values
* 26;
3926 case IPP_TAG_STRING
:
3929 case IPP_TAG_KEYWORD
:
3930 case IPP_TAG_CHARSET
:
3931 case IPP_TAG_LANGUAGE
:
3934 * Strings can contain characters that need quoting. We need
3935 * at least 2 * len + 2 characters to cover the quotes and
3936 * any backslashes in the string.
3939 for (i
= 0; i
< attr
->num_values
; i
++)
3940 bytes
+= 2 * strlen(attr
->values
[i
].string
.text
) + 2;
3944 break; /* anti-compiler-warning-code */
3953 * 'load_job_cache()' - Load jobs from the job.cache file.
3957 load_job_cache(const char *filename
) /* I - job.cache filename */
3959 cups_file_t
*fp
; /* job.cache file */
3960 char line
[1024], /* Line buffer */
3961 *value
; /* Value on line */
3962 int linenum
; /* Line number in file */
3963 cupsd_job_t
*job
; /* Current job */
3964 int jobid
; /* Job ID */
3965 char jobfile
[1024]; /* Job filename */
3969 * Open the job.cache file...
3972 if ((fp
= cupsdOpenConfFile(filename
)) == NULL
)
3974 load_request_root();
3979 * Read entries from the job cache file and create jobs as needed.
3982 cupsdLogMessage(CUPSD_LOG_INFO
, "Loading job cache file \"%s\"...",
3988 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
3990 if (!_cups_strcasecmp(line
, "NextJobId"))
3993 NextJobId
= atoi(value
);
3995 else if (!_cups_strcasecmp(line
, "<Job"))
3999 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing </Job> directive on line %d.",
4006 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing job ID on line %d.", linenum
);
4010 jobid
= atoi(value
);
4014 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad job ID %d on line %d.", jobid
,
4019 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, jobid
);
4020 if (access(jobfile
, 0))
4022 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d.N", RequestRoot
, jobid
);
4023 if (access(jobfile
, 0))
4025 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Job %d] Files have gone away.",
4031 job
= calloc(1, sizeof(cupsd_job_t
));
4034 cupsdLogMessage(CUPSD_LOG_EMERG
,
4035 "[Job %d] Unable to allocate memory for job.", jobid
);
4040 job
->back_pipes
[0] = -1;
4041 job
->back_pipes
[1] = -1;
4042 job
->print_pipes
[0] = -1;
4043 job
->print_pipes
[1] = -1;
4044 job
->side_pipes
[0] = -1;
4045 job
->side_pipes
[1] = -1;
4046 job
->status_pipes
[0] = -1;
4047 job
->status_pipes
[1] = -1;
4049 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Loading from cache...");
4053 cupsdLogMessage(CUPSD_LOG_ERROR
,
4054 "Missing <Job #> directive on line %d.", linenum
);
4057 else if (!_cups_strcasecmp(line
, "</Job>"))
4059 cupsArrayAdd(Jobs
, job
);
4061 if (job
->state_value
<= IPP_JOB_STOPPED
&& cupsdLoadJob(job
))
4062 cupsArrayAdd(ActiveJobs
, job
);
4068 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing value on line %d.", linenum
);
4071 else if (!_cups_strcasecmp(line
, "State"))
4073 job
->state_value
= (ipp_jstate_t
)atoi(value
);
4075 if (job
->state_value
< IPP_JOB_PENDING
)
4076 job
->state_value
= IPP_JOB_PENDING
;
4077 else if (job
->state_value
> IPP_JOB_COMPLETED
)
4078 job
->state_value
= IPP_JOB_COMPLETED
;
4080 else if (!_cups_strcasecmp(line
, "HoldUntil"))
4082 job
->hold_until
= atoi(value
);
4084 else if (!_cups_strcasecmp(line
, "Priority"))
4086 job
->priority
= atoi(value
);
4088 else if (!_cups_strcasecmp(line
, "Username"))
4090 cupsdSetString(&job
->username
, value
);
4092 else if (!_cups_strcasecmp(line
, "Destination"))
4094 cupsdSetString(&job
->dest
, value
);
4096 else if (!_cups_strcasecmp(line
, "DestType"))
4098 job
->dtype
= (cups_ptype_t
)atoi(value
);
4100 else if (!_cups_strcasecmp(line
, "NumFiles"))
4102 job
->num_files
= atoi(value
);
4104 if (job
->num_files
< 0)
4106 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad NumFiles value %d on line %d.",
4107 job
->num_files
, linenum
);
4112 if (job
->num_files
> 0)
4114 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-001", RequestRoot
,
4116 if (access(jobfile
, 0))
4118 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Data files have gone away.");
4123 job
->filetypes
= calloc((size_t)job
->num_files
, sizeof(mime_type_t
*));
4124 job
->compressions
= calloc((size_t)job
->num_files
, sizeof(int));
4126 if (!job
->filetypes
|| !job
->compressions
)
4128 cupsdLogJob(job
, CUPSD_LOG_EMERG
,
4129 "Unable to allocate memory for %d files.",
4135 else if (!_cups_strcasecmp(line
, "File"))
4137 int number
, /* File number */
4138 compression
; /* Compression value */
4139 char super
[MIME_MAX_SUPER
], /* MIME super type */
4140 type
[MIME_MAX_TYPE
]; /* MIME type */
4143 if (sscanf(value
, "%d%*[ \t]%15[^/]/%255s%d", &number
, super
, type
,
4146 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File on line %d.", linenum
);
4150 if (number
< 1 || number
> job
->num_files
)
4152 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File number %d on line %d.",
4159 job
->compressions
[number
] = compression
;
4160 job
->filetypes
[number
] = mimeType(MimeDatabase
, super
, type
);
4162 if (!job
->filetypes
[number
])
4165 * If the original MIME type is unknown, auto-type it!
4168 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
4169 "Unknown MIME type %s/%s for file %d.",
4170 super
, type
, number
+ 1);
4172 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
4173 job
->id
, number
+ 1);
4174 job
->filetypes
[number
] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
4175 job
->compressions
+ number
);
4178 * If that didn't work, assume it is raw...
4181 if (!job
->filetypes
[number
])
4182 job
->filetypes
[number
] = mimeType(MimeDatabase
, "application",
4187 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unknown %s directive on line %d.",
4193 cupsdLogMessage(CUPSD_LOG_ERROR
,
4194 "Missing </Job> directive on line %d.", linenum
);
4195 cupsdDeleteJob(job
, CUPSD_JOB_PURGE
);
4203 * 'load_next_job_id()' - Load the NextJobId value from the job.cache file.
4207 load_next_job_id(const char *filename
) /* I - job.cache filename */
4209 cups_file_t
*fp
; /* job.cache file */
4210 char line
[1024], /* Line buffer */
4211 *value
; /* Value on line */
4212 int linenum
; /* Line number in file */
4213 int next_job_id
; /* NextJobId value from line */
4217 * Read the NextJobId directive from the job.cache file and use
4218 * the value (if any).
4221 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
4223 if (errno
!= ENOENT
)
4224 cupsdLogMessage(CUPSD_LOG_ERROR
,
4225 "Unable to open job cache file \"%s\": %s",
4226 filename
, strerror(errno
));
4231 cupsdLogMessage(CUPSD_LOG_INFO
,
4232 "Loading NextJobId from job cache file \"%s\"...", filename
);
4236 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
4238 if (!_cups_strcasecmp(line
, "NextJobId"))
4242 next_job_id
= atoi(value
);
4244 if (next_job_id
> NextJobId
)
4245 NextJobId
= next_job_id
;
4256 * 'load_request_root()' - Load jobs from the RequestRoot directory.
4260 load_request_root(void)
4262 cups_dir_t
*dir
; /* Directory */
4263 cups_dentry_t
*dent
; /* Directory entry */
4264 cupsd_job_t
*job
; /* New job */
4268 * Open the requests directory...
4271 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Scanning %s for jobs...", RequestRoot
);
4273 if ((dir
= cupsDirOpen(RequestRoot
)) == NULL
)
4275 cupsdLogMessage(CUPSD_LOG_ERROR
,
4276 "Unable to open spool directory \"%s\": %s",
4277 RequestRoot
, strerror(errno
));
4282 * Read all the c##### files...
4285 while ((dent
= cupsDirRead(dir
)) != NULL
)
4286 if (strlen(dent
->filename
) >= 6 && dent
->filename
[0] == 'c')
4289 * Allocate memory for the job...
4292 if ((job
= calloc(sizeof(cupsd_job_t
), 1)) == NULL
)
4294 cupsdLogMessage(CUPSD_LOG_ERROR
, "Ran out of memory for jobs.");
4300 * Assign the job ID...
4303 job
->id
= atoi(dent
->filename
+ 1);
4304 job
->back_pipes
[0] = -1;
4305 job
->back_pipes
[1] = -1;
4306 job
->print_pipes
[0] = -1;
4307 job
->print_pipes
[1] = -1;
4308 job
->side_pipes
[0] = -1;
4309 job
->side_pipes
[1] = -1;
4310 job
->status_pipes
[0] = -1;
4311 job
->status_pipes
[1] = -1;
4313 if (job
->id
>= NextJobId
)
4314 NextJobId
= job
->id
+ 1;
4320 if (cupsdLoadJob(job
))
4323 * Insert the job into the array, sorting by job priority and ID...
4326 cupsArrayAdd(Jobs
, job
);
4328 if (job
->state_value
<= IPP_JOB_STOPPED
)
4329 cupsArrayAdd(ActiveJobs
, job
);
4342 * 'remove_job_files()' - Remove the document files for a job.
4346 remove_job_files(cupsd_job_t
*job
) /* I - Job */
4348 int i
; /* Looping var */
4349 char filename
[1024]; /* Document filename */
4352 if (job
->num_files
<= 0)
4355 for (i
= 1; i
<= job
->num_files
; i
++)
4357 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
4359 cupsdUnlinkOrRemoveFile(filename
);
4362 free(job
->filetypes
);
4363 free(job
->compressions
);
4367 job
->filetypes
= NULL
;
4368 job
->compressions
= NULL
;
4370 LastEvent
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
4375 * 'remove_job_history()' - Remove the control file for a job.
4379 remove_job_history(cupsd_job_t
*job
) /* I - Job */
4381 char filename
[1024]; /* Control filename */
4385 * Remove the job info file...
4388 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
,
4390 cupsdUnlinkOrRemoveFile(filename
);
4392 LastEvent
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
4397 * 'set_time()' - Set one of the "time-at-xyz" attributes.
4401 set_time(cupsd_job_t
*job
, /* I - Job to update */
4402 const char *name
) /* I - Name of attribute */
4404 ipp_attribute_t
*attr
; /* Time attribute */
4405 time_t curtime
; /* Current time */
4408 curtime
= time(NULL
);
4410 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "%s=%ld", name
, (long)curtime
);
4412 if ((attr
= ippFindAttribute(job
->attrs
, name
, IPP_TAG_ZERO
)) != NULL
)
4414 attr
->value_tag
= IPP_TAG_INTEGER
;
4415 attr
->values
[0].integer
= curtime
;
4418 if (!strcmp(name
, "time-at-completed"))
4420 if (JobHistory
< INT_MAX
&& attr
)
4421 job
->history_time
= attr
->values
[0].integer
+ JobHistory
;
4423 job
->history_time
= INT_MAX
;
4425 if (job
->history_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
4426 JobHistoryUpdate
= job
->history_time
;
4428 if (JobFiles
< INT_MAX
&& attr
)
4429 job
->file_time
= attr
->values
[0].integer
+ JobFiles
;
4431 job
->file_time
= INT_MAX
;
4433 if (job
->file_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
4434 JobHistoryUpdate
= job
->file_time
;
4436 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_time: JobHistoryUpdate=%ld",
4437 (long)JobHistoryUpdate
);
4443 * 'start_job()' - Start a print job.
4447 start_job(cupsd_job_t
*job
, /* I - Job ID */
4448 cupsd_printer_t
*printer
) /* I - Printer to print job */
4450 const char *filename
; /* Support filename */
4451 ipp_attribute_t
*cancel_after
= ippFindAttribute(job
->attrs
,
4454 /* job-cancel-after attribute */
4457 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "start_job(job=%p(%d), printer=%p(%s))",
4458 job
, job
->id
, printer
, printer
->name
);
4461 * Make sure we have some files around before we try to print...
4464 if (job
->num_files
== 0)
4466 ippSetString(job
->attrs
, &job
->reasons
, 0, "aborted-by-system");
4467 cupsdSetJobState(job
, IPP_JOB_ABORTED
, CUPSD_JOB_DEFAULT
,
4468 "Aborting job because it has no files.");
4473 * Update the printer and job state to "processing"...
4476 if (!cupsdLoadJob(job
))
4479 if (!job
->printer_message
)
4480 job
->printer_message
= ippFindAttribute(job
->attrs
,
4481 "job-printer-state-message",
4483 if (job
->printer_message
)
4484 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
), "");
4486 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-printing");
4487 cupsdSetJobState(job
, IPP_JOB_PROCESSING
, CUPSD_JOB_DEFAULT
, NULL
);
4488 cupsdSetPrinterState(printer
, IPP_PRINTER_PROCESSING
, 0);
4489 cupsdSetPrinterReasons(printer
, "-cups-remote-pending,"
4490 "cups-remote-pending-held,"
4491 "cups-remote-processing,"
4492 "cups-remote-stopped,"
4493 "cups-remote-canceled,"
4494 "cups-remote-aborted,"
4495 "cups-remote-completed");
4498 job
->current_file
= 0;
4500 job
->history_time
= 0;
4502 job
->printer
= printer
;
4506 job
->cancel_time
= time(NULL
) + ippGetInteger(cancel_after
, 0);
4507 else if (MaxJobTime
> 0)
4508 job
->cancel_time
= time(NULL
) + MaxJobTime
;
4510 job
->cancel_time
= 0;
4513 * Check for support files...
4516 cupsdSetPrinterReasons(job
->printer
, "-cups-missing-filter-warning,"
4517 "cups-insecure-filter-warning");
4521 for (filename
= (const char *)cupsArrayFirst(printer
->pc
->support_files
);
4523 filename
= (const char *)cupsArrayNext(printer
->pc
->support_files
))
4525 if (_cupsFileCheck(filename
, _CUPS_FILE_CHECK_FILE
, !RunUser
,
4526 cupsdLogFCMessage
, printer
))
4532 * Setup the last exit status and security profiles...
4536 job
->profile
= cupsdCreateProfile(job
->id
, 0);
4537 job
->bprofile
= cupsdCreateProfile(job
->id
, 1);
4540 * Create the status pipes and buffer...
4543 if (cupsdOpenPipe(job
->status_pipes
))
4545 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
4546 "Unable to create job status pipes - %s.", strerror(errno
));
4548 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
4549 "Job stopped because the scheduler could not create the "
4550 "job status pipes.");
4552 cupsdDestroyProfile(job
->profile
);
4553 job
->profile
= NULL
;
4554 cupsdDestroyProfile(job
->bprofile
);
4555 job
->bprofile
= NULL
;
4559 job
->status_buffer
= cupsdStatBufNew(job
->status_pipes
[0], NULL
);
4560 job
->status_level
= CUPSD_LOG_INFO
;
4563 * Create the backchannel pipes and make them non-blocking...
4566 if (cupsdOpenPipe(job
->back_pipes
))
4568 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
4569 "Unable to create back-channel pipes - %s.", strerror(errno
));
4571 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
4572 "Job stopped because the scheduler could not create the "
4573 "back-channel pipes.");
4575 cupsdClosePipe(job
->status_pipes
);
4576 cupsdStatBufDelete(job
->status_buffer
);
4577 job
->status_buffer
= NULL
;
4579 cupsdDestroyProfile(job
->profile
);
4580 job
->profile
= NULL
;
4581 cupsdDestroyProfile(job
->bprofile
);
4582 job
->bprofile
= NULL
;
4586 fcntl(job
->back_pipes
[0], F_SETFL
,
4587 fcntl(job
->back_pipes
[0], F_GETFL
) | O_NONBLOCK
);
4588 fcntl(job
->back_pipes
[1], F_SETFL
,
4589 fcntl(job
->back_pipes
[1], F_GETFL
) | O_NONBLOCK
);
4592 * Create the side-channel pipes and make them non-blocking...
4595 if (socketpair(AF_LOCAL
, SOCK_STREAM
, 0, job
->side_pipes
))
4597 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
4598 "Unable to create side-channel pipes - %s.", strerror(errno
));
4600 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
4601 "Job stopped because the scheduler could not create the "
4602 "side-channel pipes.");
4604 cupsdClosePipe(job
->back_pipes
);
4606 cupsdClosePipe(job
->status_pipes
);
4607 cupsdStatBufDelete(job
->status_buffer
);
4608 job
->status_buffer
= NULL
;
4610 cupsdDestroyProfile(job
->profile
);
4611 job
->profile
= NULL
;
4612 cupsdDestroyProfile(job
->bprofile
);
4613 job
->bprofile
= NULL
;
4617 fcntl(job
->side_pipes
[0], F_SETFL
,
4618 fcntl(job
->side_pipes
[0], F_GETFL
) | O_NONBLOCK
);
4619 fcntl(job
->side_pipes
[1], F_SETFL
,
4620 fcntl(job
->side_pipes
[1], F_GETFL
) | O_NONBLOCK
);
4622 fcntl(job
->side_pipes
[0], F_SETFD
,
4623 fcntl(job
->side_pipes
[0], F_GETFD
) | FD_CLOEXEC
);
4624 fcntl(job
->side_pipes
[1], F_SETFD
,
4625 fcntl(job
->side_pipes
[1], F_GETFD
) | FD_CLOEXEC
);
4628 * Now start the first file in the job...
4631 cupsdContinueJob(job
);
4636 * 'stop_job()' - Stop a print job.
4640 stop_job(cupsd_job_t
*job
, /* I - Job */
4641 cupsd_jobaction_t action
) /* I - Action */
4643 int i
; /* Looping var */
4646 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "stop_job(job=%p(%d), action=%d)", job
,
4649 FilterLevel
-= job
->cost
;
4652 if (action
== CUPSD_JOB_DEFAULT
&& !job
->kill_time
)
4653 job
->kill_time
= time(NULL
) + JobKillDelay
;
4654 else if (action
>= CUPSD_JOB_FORCE
)
4657 for (i
= 0; job
->filters
[i
]; i
++)
4658 if (job
->filters
[i
] > 0)
4660 cupsdEndProcess(job
->filters
[i
], action
>= CUPSD_JOB_FORCE
);
4662 if (action
>= CUPSD_JOB_FORCE
)
4663 job
->filters
[i
] = -job
->filters
[i
];
4666 if (job
->backend
> 0)
4668 cupsdEndProcess(job
->backend
, action
>= CUPSD_JOB_FORCE
);
4670 if (action
>= CUPSD_JOB_FORCE
)
4671 job
->backend
= -job
->backend
;
4674 if (action
>= CUPSD_JOB_FORCE
)
4677 * Clear job status...
4686 * 'unload_job()' - Unload a job from memory.
4690 unload_job(cupsd_job_t
*job
) /* I - Job */
4695 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Unloading...");
4697 ippDelete(job
->attrs
);
4701 job
->reasons
= NULL
;
4703 job
->job_sheets
= NULL
;
4704 job
->printer_message
= NULL
;
4705 job
->printer_reasons
= NULL
;
4710 * 'update_job()' - Read a status update from a job's filters.
4714 update_job(cupsd_job_t
*job
) /* I - Job to check */
4716 int i
; /* Looping var */
4717 int copies
; /* Number of copies printed */
4718 char message
[CUPSD_SB_BUFFER_SIZE
],
4720 *ptr
; /* Pointer update... */
4721 int loglevel
, /* Log level for message */
4722 event
= 0; /* Events? */
4723 static const char * const levels
[] = /* Log levels */
4739 * Get the printer associated with this job; if the printer is stopped for
4740 * any reason then job->printer will be reset to NULL, so make sure we have
4741 * a valid pointer...
4744 while ((ptr
= cupsdStatBufUpdate(job
->status_buffer
, &loglevel
,
4745 message
, sizeof(message
))) != NULL
)
4748 * Process page and printer state messages as needed...
4751 if (loglevel
== CUPSD_LOG_PAGE
)
4754 * Page message; send the message to the page_log file and update the
4755 * job sheet count...
4758 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "PAGE: %s", message
);
4762 if (!_cups_strncasecmp(message
, "total ", 6))
4765 * Got a total count of pages from a backend or filter...
4768 copies
= atoi(message
+ 6);
4769 copies
-= job
->sheets
->values
[0].integer
; /* Just track the delta */
4771 else if (!sscanf(message
, "%*d%d", &copies
))
4774 job
->sheets
->values
[0].integer
+= copies
;
4776 if (job
->printer
->page_limit
)
4777 cupsdUpdateQuota(job
->printer
, job
->username
, copies
, 0);
4780 cupsdLogPage(job
, message
);
4783 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
4784 "Printed %d page(s).", job
->sheets
->values
[0].integer
);
4786 else if (loglevel
== CUPSD_LOG_JOBSTATE
)
4789 * Support "keyword" to set job-state-reasons to the specified keyword.
4790 * This is sufficient for the current paid printing stuff.
4793 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "JOBSTATE: %s", message
);
4795 if (!strcmp(message
, "cups-retry-as-raster"))
4796 job
->retry_as_raster
= 1;
4798 ippSetString(job
->attrs
, &job
->reasons
, 0, message
);
4800 else if (loglevel
== CUPSD_LOG_STATE
)
4802 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "STATE: %s", message
);
4804 if (!strcmp(message
, "paused"))
4806 cupsdStopPrinter(job
->printer
, 1);
4809 else if (message
[0] && cupsdSetPrinterReasons(job
->printer
, message
))
4811 event
|= CUPSD_EVENT_PRINTER_STATE
;
4816 * Reset cancel time after connecting to the device...
4819 for (i
= 0; i
< job
->printer
->num_reasons
; i
++)
4820 if (!strcmp(job
->printer
->reasons
[i
], "connecting-to-device"))
4823 if (i
>= job
->printer
->num_reasons
)
4825 ipp_attribute_t
*cancel_after
= ippFindAttribute(job
->attrs
,
4828 /* job-cancel-after attribute */
4831 job
->cancel_time
= time(NULL
) + ippGetInteger(cancel_after
, 0);
4833 job
->cancel_time
= time(NULL
) + MaxJobTime
;
4838 update_job_attrs(job
, 0);
4840 else if (loglevel
== CUPSD_LOG_ATTR
)
4843 * Set attribute(s)...
4846 int num_attrs
; /* Number of attributes */
4847 cups_option_t
*attrs
; /* Attributes */
4848 const char *attr
; /* Attribute */
4850 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "ATTR: %s", message
);
4852 num_attrs
= cupsParseOptions(message
, 0, &attrs
);
4854 if ((attr
= cupsGetOption("auth-info-default", num_attrs
,
4857 job
->printer
->num_options
= cupsAddOption("auth-info", attr
,
4858 job
->printer
->num_options
,
4859 &(job
->printer
->options
));
4860 cupsdSetPrinterAttrs(job
->printer
);
4862 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4865 if ((attr
= cupsGetOption("auth-info-required", num_attrs
,
4868 cupsdSetAuthInfoRequired(job
->printer
, attr
, NULL
);
4869 cupsdSetPrinterAttrs(job
->printer
);
4871 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4874 if ((attr
= cupsGetOption("job-media-progress", num_attrs
,
4877 int progress
= atoi(attr
);
4880 if (progress
>= 0 && progress
<= 100)
4882 job
->progress
= progress
;
4885 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
4886 "Printing page %d, %d%%",
4887 job
->sheets
->values
[0].integer
, job
->progress
);
4891 if ((attr
= cupsGetOption("printer-alert", num_attrs
, attrs
)) != NULL
)
4893 cupsdSetString(&job
->printer
->alert
, attr
);
4894 event
|= CUPSD_EVENT_PRINTER_STATE
;
4897 if ((attr
= cupsGetOption("printer-alert-description", num_attrs
,
4900 cupsdSetString(&job
->printer
->alert_description
, attr
);
4901 event
|= CUPSD_EVENT_PRINTER_STATE
;
4904 if ((attr
= cupsGetOption("marker-colors", num_attrs
, attrs
)) != NULL
)
4906 cupsdSetPrinterAttr(job
->printer
, "marker-colors", (char *)attr
);
4907 job
->printer
->marker_time
= time(NULL
);
4908 event
|= CUPSD_EVENT_PRINTER_STATE
;
4909 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4912 if ((attr
= cupsGetOption("marker-levels", num_attrs
, attrs
)) != NULL
)
4914 cupsdSetPrinterAttr(job
->printer
, "marker-levels", (char *)attr
);
4915 job
->printer
->marker_time
= time(NULL
);
4916 event
|= CUPSD_EVENT_PRINTER_STATE
;
4917 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4920 if ((attr
= cupsGetOption("marker-low-levels", num_attrs
, attrs
)) != NULL
)
4922 cupsdSetPrinterAttr(job
->printer
, "marker-low-levels", (char *)attr
);
4923 job
->printer
->marker_time
= time(NULL
);
4924 event
|= CUPSD_EVENT_PRINTER_STATE
;
4925 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4928 if ((attr
= cupsGetOption("marker-high-levels", num_attrs
, attrs
)) != NULL
)
4930 cupsdSetPrinterAttr(job
->printer
, "marker-high-levels", (char *)attr
);
4931 job
->printer
->marker_time
= time(NULL
);
4932 event
|= CUPSD_EVENT_PRINTER_STATE
;
4933 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4936 if ((attr
= cupsGetOption("marker-message", num_attrs
, attrs
)) != NULL
)
4938 cupsdSetPrinterAttr(job
->printer
, "marker-message", (char *)attr
);
4939 job
->printer
->marker_time
= time(NULL
);
4940 event
|= CUPSD_EVENT_PRINTER_STATE
;
4941 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4944 if ((attr
= cupsGetOption("marker-names", num_attrs
, attrs
)) != NULL
)
4946 cupsdSetPrinterAttr(job
->printer
, "marker-names", (char *)attr
);
4947 job
->printer
->marker_time
= time(NULL
);
4948 event
|= CUPSD_EVENT_PRINTER_STATE
;
4949 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4952 if ((attr
= cupsGetOption("marker-types", num_attrs
, attrs
)) != NULL
)
4954 cupsdSetPrinterAttr(job
->printer
, "marker-types", (char *)attr
);
4955 job
->printer
->marker_time
= time(NULL
);
4956 event
|= CUPSD_EVENT_PRINTER_STATE
;
4957 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4960 cupsFreeOptions(num_attrs
, attrs
);
4962 else if (loglevel
== CUPSD_LOG_PPD
)
4965 * Set attribute(s)...
4968 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "PPD: %s", message
);
4970 job
->num_keywords
= cupsParseOptions(message
, job
->num_keywords
,
4976 * Strip legacy message prefix...
4979 if (!strncmp(message
, "recoverable:", 12))
4982 while (isspace(*ptr
& 255))
4985 else if (!strncmp(message
, "recovered:", 10))
4988 while (isspace(*ptr
& 255))
4995 cupsdLogJob(job
, loglevel
, "%s", ptr
);
4997 if (loglevel
< CUPSD_LOG_DEBUG
&&
4998 strcmp(job
->printer
->state_message
, ptr
))
5000 strlcpy(job
->printer
->state_message
, ptr
,
5001 sizeof(job
->printer
->state_message
));
5003 event
|= CUPSD_EVENT_PRINTER_STATE
| CUPSD_EVENT_JOB_PROGRESS
;
5005 if (loglevel
<= job
->status_level
&& job
->status_level
> CUPSD_LOG_ERROR
)
5008 * Some messages show in the job-printer-state-message attribute...
5011 if (loglevel
!= CUPSD_LOG_NOTICE
)
5012 job
->status_level
= loglevel
;
5014 update_job_attrs(job
, 1);
5016 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
5017 "Set job-printer-state-message to \"%s\", "
5019 job
->printer_message
->values
[0].string
.text
,
5020 levels
[job
->status_level
]);
5025 if (!strchr(job
->status_buffer
->buffer
, '\n'))
5029 if (event
& CUPSD_EVENT_JOB_PROGRESS
)
5030 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
5031 "%s", job
->printer
->state_message
);
5032 if (event
& CUPSD_EVENT_PRINTER_STATE
)
5033 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, job
->printer
, NULL
,
5034 (job
->printer
->type
& CUPS_PRINTER_CLASS
) ?
5035 "Class \"%s\" state changed." :
5036 "Printer \"%s\" state changed.",
5037 job
->printer
->name
);
5040 if (ptr
== NULL
&& !job
->status_buffer
->bufused
)
5043 * See if all of the filters and the backend have returned their
5047 for (i
= 0; job
->filters
[i
] < 0; i
++);
5049 if (job
->filters
[i
])
5052 * EOF but we haven't collected the exit status of all filters...
5055 cupsdCheckProcess();
5059 if (job
->current_file
>= job
->num_files
&& job
->backend
> 0)
5062 * EOF but we haven't collected the exit status of the backend...
5065 cupsdCheckProcess();
5070 * Handle the end of job stuff...
5073 finalize_job(job
, 1);
5076 * Check for new jobs...
5085 * 'update_job_attrs()' - Update the job-printer-* attributes.
5089 update_job_attrs(cupsd_job_t
*job
, /* I - Job to update */
5090 int do_message
)/* I - 1 = copy job-printer-state message */
5092 int i
; /* Looping var */
5093 int num_reasons
; /* Actual number of reasons */
5094 const char * const *reasons
; /* Reasons */
5095 static const char *none
= "none"; /* "none" reason */
5099 * Get/create the job-printer-state-* attributes...
5102 if (!job
->printer_message
)
5104 if ((job
->printer_message
= ippFindAttribute(job
->attrs
,
5105 "job-printer-state-message",
5106 IPP_TAG_TEXT
)) == NULL
)
5107 job
->printer_message
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_TEXT
,
5108 "job-printer-state-message",
5112 if (!job
->printer_reasons
)
5113 job
->printer_reasons
= ippFindAttribute(job
->attrs
,
5114 "job-printer-state-reasons",
5118 * Copy or clear the printer-state-message value as needed...
5121 if (job
->state_value
!= IPP_JOB_PROCESSING
&&
5122 job
->status_level
== CUPSD_LOG_INFO
)
5124 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
), "");
5127 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
5129 else if (job
->printer
->state_message
[0] && do_message
)
5131 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
),
5132 job
->printer
->state_message
);
5135 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
5139 * ... and the printer-state-reasons value...
5142 if (job
->printer
->num_reasons
== 0)
5149 num_reasons
= job
->printer
->num_reasons
;
5150 reasons
= (const char * const *)job
->printer
->reasons
;
5153 if (!job
->printer_reasons
|| job
->printer_reasons
->num_values
!= num_reasons
)
5156 * Replace/create a job-printer-state-reasons attribute...
5159 ippDeleteAttribute(job
->attrs
, job
->printer_reasons
);
5161 job
->printer_reasons
= ippAddStrings(job
->attrs
,
5162 IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
5163 "job-printer-state-reasons",
5164 num_reasons
, NULL
, NULL
);
5169 * Don't bother clearing the reason strings if they are the same...
5172 for (i
= 0; i
< num_reasons
; i
++)
5173 if (strcmp(job
->printer_reasons
->values
[i
].string
.text
, reasons
[i
]))
5176 if (i
>= num_reasons
)
5180 * Not the same, so free the current strings...
5183 for (i
= 0; i
< num_reasons
; i
++)
5184 _cupsStrFree(job
->printer_reasons
->values
[i
].string
.text
);
5188 * Copy the reasons...
5191 for (i
= 0; i
< num_reasons
; i
++)
5192 job
->printer_reasons
->values
[i
].string
.text
= _cupsStrAlloc(reasons
[i
]);
5195 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);