2 * "$Id: job.c 7902 2008-09-03 14:20:17Z mike $"
4 * Job management routines for the CUPS scheduler.
6 * Copyright 2007-2012 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 * cupsdAddJob() - Add a new job to the job queue.
18 * cupsdCancelJobs() - Cancel all jobs for the given
20 * cupsdCheckJobs() - Check the pending jobs and start any if the
21 * destination is available.
22 * cupsdCleanJobs() - Clean out old jobs.
23 * cupsdContinueJob() - Continue printing with the next file in a
25 * cupsdDeleteJob() - Free all memory used by a job.
26 * cupsdFreeAllJobs() - Free all jobs from memory.
27 * cupsdFindJob() - Find the specified job.
28 * cupsdGetPrinterJobCount() - Get the number of pending, processing, or
29 * held jobs in a printer or class.
30 * cupsdGetUserJobCount() - Get the number of pending, processing, or
31 * held jobs for a user.
32 * cupsdLoadAllJobs() - Load all jobs from disk.
33 * cupsdLoadJob() - Load a single job.
34 * cupsdMoveJob() - Move the specified job to a different
36 * cupsdReleaseJob() - Release the specified job.
37 * cupsdRestartJob() - Restart the specified job.
38 * cupsdSaveAllJobs() - Save a summary of all jobs to disk.
39 * cupsdSaveJob() - Save a job to disk.
40 * cupsdSetJobHoldUntil() - Set the hold time for a job.
41 * cupsdSetJobPriority() - Set the priority of a job, moving it up/down
42 * in the list as needed.
43 * cupsdSetJobState() - Set the state of the specified print job.
44 * cupsdStopAllJobs() - Stop all print jobs.
45 * cupsdUnloadCompletedJobs() - Flush completed job history from memory.
46 * cupsdUpdateJobs() - Update the history/file files for all jobs.
47 * compare_active_jobs() - Compare the job IDs and priorities of two
49 * compare_jobs() - Compare the job IDs of two jobs.
50 * dump_job_history() - Dump any debug messages for a job.
51 * free_job_history() - Free any log history.
52 * finalize_job() - Cleanup after job filter processes and
54 * get_options() - Get a string containing the job options.
55 * ipp_length() - Compute the size of the buffer needed to hold
56 * the textual IPP attributes.
57 * load_job_cache() - Load jobs from the job.cache file.
58 * load_next_job_id() - Load the NextJobId value from the job.cache
60 * load_request_root() - Load jobs from the RequestRoot directory.
61 * remove_job_files() - Remove the document files for a job.
62 * remove_job_history() - Remove the control file for a job.
63 * set_time() - Set one of the "time-at-xyz" attributes.
64 * start_job() - Start a print job.
65 * stop_job() - Stop a print job.
66 * unload_job() - Unload a job from memory.
67 * update_job() - Read a status update from a job's filters.
68 * update_job_attrs() - Update the job-printer-* attributes.
72 * Include necessary headers...
77 #include <cups/backend.h>
80 # include <IOKit/pwr_mgt/IOPMLib.h>
81 # ifdef HAVE_IOKIT_PWR_MGT_IOPMLIBPRIVATE_H
82 # include <IOKit/pwr_mgt/IOPMLibPrivate.h>
83 # endif /* HAVE_IOKIT_PWR_MGT_IOPMLIBPRIVATE_H */
84 #endif /* __APPLE__ */
88 * Design Notes for Job Management
89 * -------------------------------
93 * pending Do nothing/check jobs
94 * pending-held Send SIGTERM to filters and backend
95 * processing Do nothing/start job
96 * stopped Send SIGKILL to filters and backend
97 * canceled Send SIGTERM to filters and backend
101 * Finalize clears the printer <-> job association, deletes the status
102 * buffer, closes all of the pipes, etc. and doesn't get run until all of
103 * the print processes are finished.
105 * UNLOADING OF JOBS (cupsdUnloadCompletedJobs)
107 * We unload the job attributes when they are not needed to reduce overall
108 * memory consumption. We don't unload jobs where job->state_value <
109 * IPP_JOB_STOPPED, job->printer != NULL, or job->access_time is recent.
111 * STARTING OF JOBS (start_job)
113 * When a job is started, a status buffer, several pipes, a security
114 * profile, and a backend process are created for the life of that job.
115 * These are shared for every file in a job. For remote print jobs, the
116 * IPP backend is provided with every file in the job and no filters are
119 * The job->printer member tracks which printer is printing a job, which
120 * can be different than the destination in job->dest for classes. The
121 * printer object also has a job pointer to track which job is being
124 * PRINTING OF JOB FILES (cupsdContinueJob)
126 * Each file in a job is filtered by 0 or more programs. After getting the
127 * list of filters needed and the total cost, the job is either passed or
128 * put back to the processing state until the current FilterLevel comes down
129 * enough to allow printing.
131 * If we can print, we build a string for the print options and run each of
132 * the filters, piping the output from one into the next.
134 * JOB STATUS UPDATES (update_job)
136 * The update_job function gets called whenever there are pending messages
137 * on the status pipe. These generally are updates to the marker-*,
138 * printer-state-message, or printer-state-reasons attributes. On EOF,
139 * finalize_job is called to clean up.
141 * FINALIZING JOBS (finalize_job)
143 * When all filters and the backend are done, we set the job state to
144 * completed (no errors), aborted (filter errors or abort-job policy),
145 * pending-held (auth required or retry-job policy), or pending
146 * (retry-current-job or stop-printer policies) as appropriate.
148 * Then we close the pipes and free the status buffers and profiles.
150 * JOB FILE COMPLETION (process_children in main.c)
152 * For multiple-file jobs, process_children (in main.c) sees that all
153 * filters have exited and calls in to print the next file if there are
154 * more files in the job, otherwise it waits for the backend to exit and
155 * update_job to do the cleanup.
163 static mime_filter_t gziptoany_filter
=
165 NULL
, /* Source type */
166 NULL
, /* Destination type */
168 "gziptoany" /* Filter program to run */
176 static int compare_active_jobs(void *first
, void *second
, void *data
);
177 static int compare_jobs(void *first
, void *second
, void *data
);
178 static void dump_job_history(cupsd_job_t
*job
);
179 static void finalize_job(cupsd_job_t
*job
, int set_job_state
);
180 static void free_job_history(cupsd_job_t
*job
);
181 static char *get_options(cupsd_job_t
*job
, int banner_page
, char *copies
,
182 size_t copies_size
, char *title
,
184 static size_t ipp_length(ipp_t
*ipp
);
185 static void load_job_cache(const char *filename
);
186 static void load_next_job_id(const char *filename
);
187 static void load_request_root(void);
188 static void remove_job_files(cupsd_job_t
*job
);
189 static void remove_job_history(cupsd_job_t
*job
);
190 static void set_time(cupsd_job_t
*job
, const char *name
);
191 static void start_job(cupsd_job_t
*job
, cupsd_printer_t
*printer
);
192 static void stop_job(cupsd_job_t
*job
, cupsd_jobaction_t action
);
193 static void unload_job(cupsd_job_t
*job
);
194 static void update_job(cupsd_job_t
*job
);
195 static void update_job_attrs(cupsd_job_t
*job
, int do_message
);
199 * 'cupsdAddJob()' - Add a new job to the job queue.
202 cupsd_job_t
* /* O - New job record */
203 cupsdAddJob(int priority
, /* I - Job priority */
204 const char *dest
) /* I - Job destination */
206 cupsd_job_t
*job
; /* New job record */
209 if ((job
= calloc(sizeof(cupsd_job_t
), 1)) == NULL
)
212 job
->id
= NextJobId
++;
213 job
->priority
= priority
;
214 job
->back_pipes
[0] = -1;
215 job
->back_pipes
[1] = -1;
216 job
->print_pipes
[0] = -1;
217 job
->print_pipes
[1] = -1;
218 job
->side_pipes
[0] = -1;
219 job
->side_pipes
[1] = -1;
220 job
->status_pipes
[0] = -1;
221 job
->status_pipes
[1] = -1;
223 cupsdSetString(&job
->dest
, dest
);
226 * Add the new job to the "all jobs" and "active jobs" lists...
229 cupsArrayAdd(Jobs
, job
);
230 cupsArrayAdd(ActiveJobs
, job
);
237 * 'cupsdCancelJobs()' - Cancel all jobs for the given destination/user.
241 cupsdCancelJobs(const char *dest
, /* I - Destination to cancel */
242 const char *username
, /* I - Username or NULL */
243 int purge
) /* I - Purge jobs? */
245 cupsd_job_t
*job
; /* Current job */
248 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
250 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
252 if ((!job
->dest
|| !job
->username
) && !cupsdLoadJob(job
))
255 if ((!dest
|| !strcmp(job
->dest
, dest
)) &&
256 (!username
|| !strcmp(job
->username
, username
)))
259 * Cancel all jobs matching this destination/user...
263 cupsdSetJobState(job
, IPP_JOB_CANCELED
, CUPSD_JOB_PURGE
,
264 "Job purged by user.");
265 else if (job
->state_value
< IPP_JOB_CANCELED
)
266 cupsdSetJobState(job
, IPP_JOB_CANCELED
, CUPSD_JOB_DEFAULT
,
267 "Job canceled by user.");
276 * 'cupsdCheckJobs()' - Check the pending jobs and start any if the destination
283 cupsd_job_t
*job
; /* Current job in queue */
284 cupsd_printer_t
*printer
, /* Printer destination */
285 *pclass
; /* Printer class destination */
286 ipp_attribute_t
*attr
; /* Job attribute */
287 time_t curtime
; /* Current time */
290 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
291 "cupsdCheckJobs: %d active jobs, sleeping=%d, reload=%d",
292 cupsArrayCount(ActiveJobs
), Sleeping
, NeedReload
);
294 curtime
= time(NULL
);
296 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
298 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
301 * Kill jobs if they are unresponsive...
304 if (job
->kill_time
&& job
->kill_time
<= curtime
)
306 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Job %d] Stopping unresponsive job.",
309 stop_job(job
, CUPSD_JOB_FORCE
);
314 * Cancel stuck jobs...
317 if (job
->cancel_time
&& job
->cancel_time
<= curtime
)
319 cupsdSetJobState(job
, IPP_JOB_CANCELED
, CUPSD_JOB_DEFAULT
,
320 "Canceling stuck job after %d seconds.", MaxJobTime
);
325 * Start held jobs if they are ready...
328 if (job
->state_value
== IPP_JOB_HELD
&&
330 job
->hold_until
< curtime
)
332 if (job
->pending_timeout
)
335 * This job is pending; check that we don't have an active Send-Document
336 * operation in progress on any of the client connections, then timeout
337 * the job so we can start printing...
340 cupsd_client_t
*con
; /* Current client connection */
343 for (con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
345 con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
347 con
->request
->request
.op
.operation_id
== IPP_SEND_DOCUMENT
)
353 if (cupsdTimeoutJob(job
))
357 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
358 "Job submission timed out.");
362 * Continue jobs that are waiting on the FilterLimit...
365 if (job
->pending_cost
> 0 &&
366 ((FilterLevel
+ job
->pending_cost
) < FilterLimit
|| FilterLevel
== 0))
367 cupsdContinueJob(job
);
370 * Start pending jobs if the destination is available...
373 if (job
->state_value
== IPP_JOB_PENDING
&& !NeedReload
&&
374 #ifndef kIOPMAssertionTypeDenySystemSleep
376 #endif /* !kIOPMAssertionTypeDenySystemSleep */
377 !DoingShutdown
&& !job
->printer
)
379 printer
= cupsdFindDest(job
->dest
);
382 while (printer
&& (printer
->type
& CUPS_PRINTER_CLASS
))
385 * If the class is remote, just pass it to the remote server...
390 if (pclass
->state
== IPP_PRINTER_STOPPED
)
392 else if (pclass
->type
& CUPS_PRINTER_REMOTE
)
395 printer
= cupsdFindAvailablePrinter(printer
->name
);
398 if (!printer
&& !pclass
)
401 * Whoa, the printer and/or class for this destination went away;
405 cupsdSetJobState(job
, IPP_JOB_ABORTED
, CUPSD_JOB_PURGE
,
406 "Job aborted because the destination printer/class "
409 else if (printer
&& !printer
->holding_new_jobs
)
412 * See if the printer is available or remote and not printing a job;
413 * if so, start the job...
419 * Add/update a job-actual-printer-uri attribute for this job
420 * so that we know which printer actually printed the job...
423 if ((attr
= ippFindAttribute(job
->attrs
, "job-actual-printer-uri",
424 IPP_TAG_URI
)) != NULL
)
425 cupsdSetString(&attr
->values
[0].string
.text
, printer
->uri
);
427 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
,
428 "job-actual-printer-uri", NULL
, printer
->uri
);
431 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
434 if (printer
->state
== IPP_PRINTER_IDLE
)
440 start_job(job
, printer
);
449 * 'cupsdCleanJobs()' - Clean out old jobs.
455 cupsd_job_t
*job
; /* Current job */
456 time_t curtime
; /* Current time */
459 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
460 "cupsdCleanJobs: MaxJobs=%d, JobHistory=%d, JobFiles=%d",
461 MaxJobs
, JobHistory
, JobFiles
);
463 if (MaxJobs
<= 0 && JobHistory
== INT_MAX
&& JobFiles
== INT_MAX
)
466 curtime
= time(NULL
);
467 JobHistoryUpdate
= 0;
469 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
471 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
473 if (job
->state_value
>= IPP_JOB_CANCELED
&& !job
->printer
)
476 * Expire old jobs (or job files)...
479 if ((MaxJobs
> 0 && cupsArrayCount(Jobs
) >= MaxJobs
) ||
480 (job
->history_time
&& job
->history_time
<= curtime
))
482 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Removing from history.");
483 cupsdDeleteJob(job
, CUPSD_JOB_PURGE
);
485 else if (job
->file_time
&& job
->file_time
<= curtime
)
487 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Removing document files.");
488 remove_job_files(job
);
490 if (job
->history_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
491 JobHistoryUpdate
= job
->history_time
;
495 if (job
->history_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
496 JobHistoryUpdate
= job
->history_time
;
498 if (job
->file_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
499 JobHistoryUpdate
= job
->file_time
;
504 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdCleanJobs: JobHistoryUpdate=%ld",
505 (long)JobHistoryUpdate
);
510 * 'cupsdContinueJob()' - Continue printing with the next file in a job.
514 cupsdContinueJob(cupsd_job_t
*job
) /* I - Job */
516 int i
; /* Looping var */
517 int slot
; /* Pipe slot */
518 cups_array_t
*filters
= NULL
,/* Filters for job */
519 *prefilters
; /* Filters with prefilters */
520 mime_filter_t
*filter
, /* Current filter */
521 *prefilter
, /* Prefilter */
522 port_monitor
; /* Port monitor filter */
523 char scheme
[255]; /* Device URI scheme */
524 ipp_attribute_t
*attr
; /* Current attribute */
525 const char *ptr
, /* Pointer into value */
526 *abort_message
; /* Abort message */
527 ipp_jstate_t abort_state
= IPP_JOB_STOPPED
;
528 /* New job state on abort */
529 struct stat backinfo
; /* Backend file information */
530 int backroot
; /* Run backend as root? */
531 int pid
; /* Process ID of new filter process */
532 int banner_page
; /* 1 if banner page, 0 otherwise */
533 int filterfds
[2][2] = { { -1, -1 }, { -1, -1 } };
534 /* Pipes used between filters */
535 int envc
; /* Number of environment variables */
536 struct stat fileinfo
; /* Job file information */
537 char **argv
= NULL
, /* Filter command-line arguments */
538 filename
[1024], /* Job filename */
539 command
[1024], /* Full path to command */
540 jobid
[255], /* Job ID string */
542 /* Job title string */
543 copies
[255], /* # copies string */
544 *options
, /* Options string */
546 /* Environment variables */
547 charset
[255], /* CHARSET env variable */
548 class_name
[255],/* CLASS env variable */
549 classification
[1024],
550 /* CLASSIFICATION env variable */
552 /* CONTENT_TYPE env variable */
554 /* DEVICE_URI env variable */
555 final_content_type
[1024],
556 /* FINAL_CONTENT_TYPE env variable */
557 lang
[255], /* LANG env variable */
560 /* APPLE_LANGUAGE env variable */
561 #endif /* __APPLE__ */
562 auth_info_required
[255],
563 /* AUTH_INFO_REQUIRED env variable */
564 ppd
[1024], /* PPD env variable */
566 /* PRINTER_INFO env variable */
567 printer_location
[255],
568 /* PRINTER_LOCATION env variable */
570 /* PRINTER env variable */
571 *printer_state_reasons
= NULL
,
572 /* PRINTER_STATE_REASONS env var */
574 /* RIP_MAX_CACHE env variable */
577 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
578 "cupsdContinueJob(job=%p(%d)): current_file=%d, num_files=%d",
579 job
, job
->id
, job
->current_file
, job
->num_files
);
582 * Figure out what filters are required to convert from
583 * the source to the destination type...
586 FilterLevel
-= job
->cost
;
589 job
->pending_cost
= 0;
591 memset(job
->filters
, 0, sizeof(job
->filters
));
594 if (job
->printer
->raw
)
597 * Remote jobs and raw queues go directly to the printer without
601 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Sending job to queue tagged as raw...");
606 * Local jobs get filtered...
609 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
610 job
->id
, job
->current_file
+ 1);
611 if (stat(filename
, &fileinfo
))
612 fileinfo
.st_size
= 0;
614 filters
= mimeFilter2(MimeDatabase
, job
->filetypes
[job
->current_file
],
615 fileinfo
.st_size
, job
->printer
->filetype
,
620 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
621 "Unable to convert file %d to printable format.",
624 abort_message
= "Aborting job because it cannot be printed.";
625 abort_state
= IPP_JOB_ABORTED
;
627 ippSetString(job
->attrs
, &job
->reasons
, 0, "document-unprintable-error");
632 * Remove NULL ("-") filters...
635 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
637 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
638 if (!strcmp(filter
->filter
, "-"))
639 cupsArrayRemove(filters
, filter
);
641 if (cupsArrayCount(filters
) == 0)
643 cupsArrayDelete(filters
);
648 * If this printer has any pre-filters, insert the required pre-filter
649 * in the filters array...
652 if (job
->printer
->prefiltertype
&& filters
)
654 prefilters
= cupsArrayNew(NULL
, NULL
);
656 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
658 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
660 if ((prefilter
= mimeFilterLookup(MimeDatabase
, filter
->src
,
661 job
->printer
->prefiltertype
)))
663 cupsArrayAdd(prefilters
, prefilter
);
664 job
->cost
+= prefilter
->cost
;
667 cupsArrayAdd(prefilters
, filter
);
670 cupsArrayDelete(filters
);
671 filters
= prefilters
;
676 * Set a minimum cost of 100 for all jobs so that FilterLimit
677 * works with raw queues and other low-cost paths.
684 * See if the filter cost is too high...
687 if ((FilterLevel
+ job
->cost
) > FilterLimit
&& FilterLevel
> 0 &&
691 * Don't print this job quite yet...
694 cupsArrayDelete(filters
);
696 cupsdLogJob(job
, CUPSD_LOG_INFO
,
697 "Holding because filter limit has been reached.");
698 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
699 "cupsdContinueJob: file=%d, cost=%d, level=%d, limit=%d",
700 job
->current_file
, job
->cost
, FilterLevel
,
703 job
->pending_cost
= job
->cost
;
708 FilterLevel
+= job
->cost
;
711 * Add decompression/raw filter as needed...
714 if ((!job
->printer
->raw
&& job
->compressions
[job
->current_file
]) ||
715 (!filters
&& !job
->printer
->remote
&&
716 (job
->num_files
> 1 || !strncmp(job
->printer
->device_uri
, "file:", 5))))
719 * Add gziptoany filter to the front of the list...
723 filters
= cupsArrayNew(NULL
, NULL
);
725 if (!cupsArrayInsert(filters
, &gziptoany_filter
))
727 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
728 "Unable to add decompression filter - %s", strerror(errno
));
730 cupsArrayDelete(filters
);
732 abort_message
= "Stopping job because the scheduler ran out of memory.";
739 * Add port monitor, if any...
742 if (job
->printer
->port_monitor
)
745 * Add port monitor to the end of the list...
749 filters
= cupsArrayNew(NULL
, NULL
);
751 port_monitor
.src
= NULL
;
752 port_monitor
.dst
= NULL
;
753 port_monitor
.cost
= 0;
755 snprintf(port_monitor
.filter
, sizeof(port_monitor
.filter
),
756 "%s/monitor/%s", ServerBin
, job
->printer
->port_monitor
);
758 if (!cupsArrayAdd(filters
, &port_monitor
))
760 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
761 "Unable to add port monitor - %s", strerror(errno
));
763 abort_message
= "Stopping job because the scheduler ran out of memory.";
770 * Make sure we don't go over the "MAX_FILTERS" limit...
773 if (cupsArrayCount(filters
) > MAX_FILTERS
)
775 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
776 "Too many filters (%d > %d), unable to print.",
777 cupsArrayCount(filters
), MAX_FILTERS
);
779 abort_message
= "Aborting job because it needs too many filters to print.";
780 abort_state
= IPP_JOB_ABORTED
;
782 ippSetString(job
->attrs
, &job
->reasons
, 0, "document-unprintable-error");
788 * Determine if we are printing a banner page or not...
791 if (job
->job_sheets
== NULL
)
793 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "No job-sheets attribute.");
794 if ((job
->job_sheets
=
795 ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_ZERO
)) != NULL
)
796 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
797 "... but someone added one without setting job_sheets.");
799 else if (job
->job_sheets
->num_values
== 1)
800 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "job-sheets=%s",
801 job
->job_sheets
->values
[0].string
.text
);
803 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "job-sheets=%s,%s",
804 job
->job_sheets
->values
[0].string
.text
,
805 job
->job_sheets
->values
[1].string
.text
);
807 if (job
->printer
->type
& CUPS_PRINTER_REMOTE
)
809 else if (job
->job_sheets
== NULL
)
811 else if (_cups_strcasecmp(job
->job_sheets
->values
[0].string
.text
, "none") != 0 &&
812 job
->current_file
== 0)
814 else if (job
->job_sheets
->num_values
> 1 &&
815 _cups_strcasecmp(job
->job_sheets
->values
[1].string
.text
, "none") != 0 &&
816 job
->current_file
== (job
->num_files
- 1))
821 if ((options
= get_options(job
, banner_page
, copies
, sizeof(copies
), title
,
822 sizeof(title
))) == NULL
)
824 abort_message
= "Stopping job because the scheduler ran out of memory.";
830 * Build the command-line arguments for the filters. Each filter
831 * has 6 or 7 arguments:
839 * argv[6] = filename (optional; normally stdin)
841 * This allows legacy printer drivers that use the old System V
842 * printing interface to be used by CUPS.
844 * For remote jobs, we send all of the files in the argument list.
847 if (job
->printer
->remote
)
848 argv
= calloc(7 + job
->num_files
, sizeof(char *));
850 argv
= calloc(8, sizeof(char *));
854 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Unable to allocate argument array - %s",
857 abort_message
= "Stopping job because the scheduler ran out of memory.";
862 sprintf(jobid
, "%d", job
->id
);
864 argv
[0] = job
->printer
->name
;
866 argv
[2] = job
->username
;
871 if (job
->printer
->remote
&& job
->num_files
> 1)
873 for (i
= 0; i
< job
->num_files
; i
++)
875 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
877 argv
[6 + i
] = strdup(filename
);
882 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
883 job
->id
, job
->current_file
+ 1);
887 for (i
= 0; argv
[i
]; i
++)
888 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "argv[%d]=\"%s\"", i
, argv
[i
]);
891 * Create environment variable strings for the filters...
894 attr
= ippFindAttribute(job
->attrs
, "attributes-natural-language",
898 strcpy(apple_language
, "APPLE_LANGUAGE=");
899 _cupsAppleLanguage(attr
->values
[0].string
.text
,
900 apple_language
+ 15, sizeof(apple_language
) - 15);
901 #endif /* __APPLE__ */
903 switch (strlen(attr
->values
[0].string
.text
))
907 * This is an unknown or badly formatted language code; use
908 * the POSIX locale...
911 strcpy(lang
, "LANG=C");
916 * Just the language code (ll)...
919 snprintf(lang
, sizeof(lang
), "LANG=%s.UTF-8",
920 attr
->values
[0].string
.text
);
925 * Language and country code (ll-cc)...
928 snprintf(lang
, sizeof(lang
), "LANG=%c%c_%c%c.UTF-8",
929 attr
->values
[0].string
.text
[0],
930 attr
->values
[0].string
.text
[1],
931 toupper(attr
->values
[0].string
.text
[3] & 255),
932 toupper(attr
->values
[0].string
.text
[4] & 255));
936 if ((attr
= ippFindAttribute(job
->attrs
, "document-format",
937 IPP_TAG_MIMETYPE
)) != NULL
&&
938 (ptr
= strstr(attr
->values
[0].string
.text
, "charset=")) != NULL
)
939 snprintf(charset
, sizeof(charset
), "CHARSET=%s", ptr
+ 8);
941 strlcpy(charset
, "CHARSET=utf-8", sizeof(charset
));
943 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s/%s",
944 job
->filetypes
[job
->current_file
]->super
,
945 job
->filetypes
[job
->current_file
]->type
);
946 snprintf(device_uri
, sizeof(device_uri
), "DEVICE_URI=%s",
947 job
->printer
->device_uri
);
948 snprintf(ppd
, sizeof(ppd
), "PPD=%s/ppd/%s.ppd", ServerRoot
,
950 snprintf(printer_info
, sizeof(printer_name
), "PRINTER_INFO=%s",
951 job
->printer
->info
? job
->printer
->info
: "");
952 snprintf(printer_location
, sizeof(printer_name
), "PRINTER_LOCATION=%s",
953 job
->printer
->location
? job
->printer
->location
: "");
954 snprintf(printer_name
, sizeof(printer_name
), "PRINTER=%s", job
->printer
->name
);
955 if (job
->printer
->num_reasons
> 0)
957 char *psrptr
; /* Pointer into PRINTER_STATE_REASONS */
958 size_t psrlen
; /* Size of PRINTER_STATE_REASONS */
960 for (psrlen
= 22, i
= 0; i
< job
->printer
->num_reasons
; i
++)
961 psrlen
+= strlen(job
->printer
->reasons
[i
]) + 1;
963 if ((printer_state_reasons
= malloc(psrlen
)) != NULL
)
966 * All of these strcpy's are safe because we allocated the psr string...
969 strcpy(printer_state_reasons
, "PRINTER_STATE_REASONS=");
970 for (psrptr
= printer_state_reasons
+ 22, i
= 0;
971 i
< job
->printer
->num_reasons
;
976 strcpy(psrptr
, job
->printer
->reasons
[i
]);
977 psrptr
+= strlen(psrptr
);
981 snprintf(rip_max_cache
, sizeof(rip_max_cache
), "RIP_MAX_CACHE=%s", RIPCache
);
983 if (job
->printer
->num_auth_info_required
== 1)
984 snprintf(auth_info_required
, sizeof(auth_info_required
),
985 "AUTH_INFO_REQUIRED=%s",
986 job
->printer
->auth_info_required
[0]);
987 else if (job
->printer
->num_auth_info_required
== 2)
988 snprintf(auth_info_required
, sizeof(auth_info_required
),
989 "AUTH_INFO_REQUIRED=%s,%s",
990 job
->printer
->auth_info_required
[0],
991 job
->printer
->auth_info_required
[1]);
992 else if (job
->printer
->num_auth_info_required
== 3)
993 snprintf(auth_info_required
, sizeof(auth_info_required
),
994 "AUTH_INFO_REQUIRED=%s,%s,%s",
995 job
->printer
->auth_info_required
[0],
996 job
->printer
->auth_info_required
[1],
997 job
->printer
->auth_info_required
[2]);
998 else if (job
->printer
->num_auth_info_required
== 4)
999 snprintf(auth_info_required
, sizeof(auth_info_required
),
1000 "AUTH_INFO_REQUIRED=%s,%s,%s,%s",
1001 job
->printer
->auth_info_required
[0],
1002 job
->printer
->auth_info_required
[1],
1003 job
->printer
->auth_info_required
[2],
1004 job
->printer
->auth_info_required
[3]);
1006 strlcpy(auth_info_required
, "AUTH_INFO_REQUIRED=none",
1007 sizeof(auth_info_required
));
1009 envc
= cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
1011 envp
[envc
++] = charset
;
1012 envp
[envc
++] = lang
;
1014 envp
[envc
++] = apple_language
;
1015 #endif /* __APPLE__ */
1016 envp
[envc
++] = ppd
;
1017 envp
[envc
++] = rip_max_cache
;
1018 envp
[envc
++] = content_type
;
1019 envp
[envc
++] = device_uri
;
1020 envp
[envc
++] = printer_info
;
1021 envp
[envc
++] = printer_location
;
1022 envp
[envc
++] = printer_name
;
1023 envp
[envc
++] = printer_state_reasons
? printer_state_reasons
:
1024 "PRINTER_STATE_REASONS=none";
1025 envp
[envc
++] = banner_page
? "CUPS_FILETYPE=job-sheet" :
1026 "CUPS_FILETYPE=document";
1028 if (!job
->printer
->remote
&& !job
->printer
->raw
)
1030 filter
= (mime_filter_t
*)cupsArrayLast(filters
);
1032 if (job
->printer
->port_monitor
)
1033 filter
= (mime_filter_t
*)cupsArrayPrev(filters
);
1035 if (filter
&& filter
->dst
)
1037 if ((ptr
= strchr(filter
->dst
->type
, '/')) != NULL
)
1038 snprintf(final_content_type
, sizeof(final_content_type
),
1039 "FINAL_CONTENT_TYPE=%s", ptr
+ 1);
1041 snprintf(final_content_type
, sizeof(final_content_type
),
1042 "FINAL_CONTENT_TYPE=%s/%s", filter
->dst
->super
,
1044 envp
[envc
++] = final_content_type
;
1048 if (Classification
&& !banner_page
)
1050 if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
1051 IPP_TAG_NAME
)) == NULL
)
1052 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
1054 else if (attr
->num_values
> 1 &&
1055 strcmp(attr
->values
[1].string
.text
, "none") != 0)
1056 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
1057 attr
->values
[1].string
.text
);
1059 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
1060 attr
->values
[0].string
.text
);
1062 envp
[envc
++] = classification
;
1065 if (job
->dtype
& CUPS_PRINTER_CLASS
)
1067 snprintf(class_name
, sizeof(class_name
), "CLASS=%s", job
->dest
);
1068 envp
[envc
++] = class_name
;
1071 envp
[envc
++] = auth_info_required
;
1074 i
< (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0]));
1076 if (job
->auth_env
[i
])
1077 envp
[envc
++] = job
->auth_env
[i
];
1082 envp
[envc
++] = job
->auth_uid
;
1086 for (i
= 0; i
< envc
; i
++)
1087 if (!strncmp(envp
[i
], "AUTH_", 5))
1088 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"AUTH_%c****\"", i
,
1090 else if (strncmp(envp
[i
], "DEVICE_URI=", 11))
1091 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"%s\"", i
, envp
[i
]);
1093 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"DEVICE_URI=%s\"", i
,
1094 job
->printer
->sanitized_device_uri
);
1096 if (job
->printer
->remote
)
1097 job
->current_file
= job
->num_files
;
1099 job
->current_file
++;
1102 * Now create processes for all of the filters...
1105 cupsdSetPrinterReasons(job
->printer
, "-cups-missing-filter-warning,"
1106 "cups-insecure-filter-warning");
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
,
1207 cupsArrayDelete(filters
);
1211 * Finally, pipe the final output into a backend process if needed...
1214 if (strncmp(job
->printer
->device_uri
, "file:", 5) != 0)
1216 if (job
->current_file
== 1 || job
->printer
->remote
||
1217 (job
->printer
->pc
&& job
->printer
->pc
->single_file
))
1219 sscanf(job
->printer
->device_uri
, "%254[^:]", scheme
);
1220 snprintf(command
, sizeof(command
), "%s/backend/%s", ServerBin
, scheme
);
1223 * See if the backend needs to run as root...
1228 else if (stat(command
, &backinfo
))
1231 backroot
= !(backinfo
.st_mode
& (S_IRWXG
| S_IRWXO
));
1233 argv
[0] = job
->printer
->sanitized_device_uri
;
1235 filterfds
[slot
][0] = -1;
1236 filterfds
[slot
][1] = -1;
1238 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
1239 filterfds
[slot
][1], job
->status_pipes
[1],
1240 job
->back_pipes
[1], job
->side_pipes
[1],
1241 backroot
, job
->profile
, job
, &(job
->backend
));
1245 abort_message
= "Stopping job because the sheduler could not execute "
1252 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Started backend %s (PID %d)",
1257 if (job
->current_file
== job
->num_files
||
1258 (job
->printer
->pc
&& job
->printer
->pc
->single_file
))
1259 cupsdClosePipe(job
->print_pipes
);
1261 if (job
->current_file
== job
->num_files
)
1263 cupsdClosePipe(job
->back_pipes
);
1264 cupsdClosePipe(job
->side_pipes
);
1266 close(job
->status_pipes
[1]);
1267 job
->status_pipes
[1] = -1;
1272 filterfds
[slot
][0] = -1;
1273 filterfds
[slot
][1] = -1;
1275 if (job
->current_file
== job
->num_files
||
1276 (job
->printer
->pc
&& job
->printer
->pc
->single_file
))
1277 cupsdClosePipe(job
->print_pipes
);
1279 if (job
->current_file
== job
->num_files
)
1281 close(job
->status_pipes
[1]);
1282 job
->status_pipes
[1] = -1;
1286 cupsdClosePipe(filterfds
[slot
]);
1288 if (job
->printer
->remote
&& job
->num_files
> 1)
1290 for (i
= 0; i
< job
->num_files
; i
++)
1295 if (printer_state_reasons
)
1296 free(printer_state_reasons
);
1298 cupsdAddSelect(job
->status_buffer
->fd
, (cupsd_selfunc_t
)update_job
, NULL
,
1301 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
, "Job #%d started.",
1308 * If we get here, we need to abort the current job and close out all
1309 * files and pipes...
1314 FilterLevel
-= job
->cost
;
1317 for (slot
= 0; slot
< 2; slot
++)
1318 cupsdClosePipe(filterfds
[slot
]);
1320 cupsArrayDelete(filters
);
1324 if (job
->printer
->remote
&& job
->num_files
> 1)
1326 for (i
= 0; i
< job
->num_files
; 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 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Loading attributes...", job
->id
);
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 cupsdLogMessage(CUPSD_LOG_ERROR
,
1623 "[Job %d] Unable to read job control file \"%s\".", job
->id
,
1632 * Copy attribute data to the job object...
1635 if (!ippFindAttribute(job
->attrs
, "time-at-creation", IPP_TAG_INTEGER
))
1637 cupsdLogMessage(CUPSD_LOG_ERROR
,
1638 "[Job %d] Missing or bad time-at-creation attribute in "
1639 "control file.", job
->id
);
1643 if ((job
->state
= ippFindAttribute(job
->attrs
, "job-state",
1644 IPP_TAG_ENUM
)) == NULL
)
1646 cupsdLogMessage(CUPSD_LOG_ERROR
,
1647 "[Job %d] Missing or bad job-state attribute in control "
1652 job
->state_value
= (ipp_jstate_t
)job
->state
->values
[0].integer
;
1654 job
->history_time
= 0;
1656 if (job
->state_value
>= IPP_JOB_CANCELED
&&
1657 (attr
= ippFindAttribute(job
->attrs
, "time-at-completed",
1658 IPP_TAG_INTEGER
)) != NULL
)
1660 if (JobHistory
< INT_MAX
)
1661 job
->history_time
= attr
->values
[0].integer
+ JobHistory
;
1663 job
->history_time
= INT_MAX
;
1665 if (job
->history_time
< time(NULL
))
1666 goto error
; /* Expired, remove from history */
1668 if (job
->history_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
1669 JobHistoryUpdate
= job
->history_time
;
1671 if (JobFiles
< INT_MAX
)
1672 job
->file_time
= attr
->values
[0].integer
+ JobFiles
;
1674 job
->file_time
= INT_MAX
;
1676 if (job
->file_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
1677 JobHistoryUpdate
= job
->file_time
;
1679 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdLoadJob: JobHistoryUpdate=%ld",
1680 (long)JobHistoryUpdate
);
1685 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1686 IPP_TAG_URI
)) == NULL
)
1688 cupsdLogMessage(CUPSD_LOG_ERROR
,
1689 "[Job %d] No job-printer-uri attribute in control file.",
1694 if ((dest
= cupsdValidateDest(attr
->values
[0].string
.text
, &(job
->dtype
),
1697 cupsdLogMessage(CUPSD_LOG_ERROR
,
1698 "[Job %d] Unable to queue job for destination \"%s\".",
1699 job
->id
, attr
->values
[0].string
.text
);
1703 cupsdSetString(&job
->dest
, dest
);
1705 else if ((destptr
= cupsdFindDest(job
->dest
)) == NULL
)
1707 cupsdLogMessage(CUPSD_LOG_ERROR
,
1708 "[Job %d] Unable to queue job for destination \"%s\".",
1709 job
->id
, job
->dest
);
1713 if ((job
->reasons
= ippFindAttribute(job
->attrs
, "job-state-reasons",
1714 IPP_TAG_KEYWORD
)) == NULL
)
1716 const char *reason
; /* job-state-reason keyword */
1718 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1719 "[Job %d] Adding missing job-state-reasons attribute to "
1720 " control file.", job
->id
);
1722 switch (job
->state_value
)
1725 case IPP_JOB_PENDING
:
1726 if (destptr
->state
== IPP_PRINTER_STOPPED
)
1727 reason
= "printer-stopped";
1733 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1734 IPP_TAG_ZERO
)) != NULL
&&
1735 (attr
->value_tag
== IPP_TAG_NAME
||
1736 attr
->value_tag
== IPP_TAG_NAMELANG
||
1737 attr
->value_tag
== IPP_TAG_KEYWORD
) &&
1738 strcmp(attr
->values
[0].string
.text
, "no-hold"))
1739 reason
= "job-hold-until-specified";
1741 reason
= "job-incoming";
1744 case IPP_JOB_PROCESSING
:
1745 reason
= "job-printing";
1748 case IPP_JOB_STOPPED
:
1749 reason
= "job-stopped";
1752 case IPP_JOB_CANCELED
:
1753 reason
= "job-canceled-by-user";
1756 case IPP_JOB_ABORTED
:
1757 reason
= "aborted-by-system";
1760 case IPP_JOB_COMPLETED
:
1761 reason
= "job-completed-successfully";
1765 job
->reasons
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1766 "job-state-reasons", NULL
, reason
);
1768 else if (job
->state_value
== IPP_JOB_PENDING
)
1770 if (destptr
->state
== IPP_PRINTER_STOPPED
)
1771 ippSetString(job
->attrs
, &job
->reasons
, 0, "printer-stopped");
1773 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
1776 job
->sheets
= ippFindAttribute(job
->attrs
, "job-media-sheets-completed",
1778 job
->job_sheets
= ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_NAME
);
1782 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
1783 IPP_TAG_INTEGER
)) == NULL
)
1785 cupsdLogMessage(CUPSD_LOG_ERROR
,
1786 "[Job %d] Missing or bad job-priority attribute in "
1787 "control file.", job
->id
);
1791 job
->priority
= attr
->values
[0].integer
;
1796 if ((attr
= ippFindAttribute(job
->attrs
, "job-originating-user-name",
1797 IPP_TAG_NAME
)) == NULL
)
1799 cupsdLogMessage(CUPSD_LOG_ERROR
,
1800 "[Job %d] Missing or bad job-originating-user-name "
1801 "attribute in control file.", job
->id
);
1805 cupsdSetString(&job
->username
, attr
->values
[0].string
.text
);
1809 * Set the job hold-until time and state...
1812 if (job
->state_value
== IPP_JOB_HELD
)
1814 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1815 IPP_TAG_KEYWORD
)) == NULL
)
1816 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1819 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
, CUPSD_JOB_DEFAULT
);
1822 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1823 job
->state_value
= IPP_JOB_PENDING
;
1826 else if (job
->state_value
== IPP_JOB_PROCESSING
)
1828 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1829 job
->state_value
= IPP_JOB_PENDING
;
1832 if (!job
->num_files
)
1835 * Find all the d##### files...
1838 for (fileid
= 1; fileid
< 10000; fileid
++)
1840 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
1843 if (access(jobfile
, 0))
1846 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1847 "[Job %d] Auto-typing document file \"%s\"...", job
->id
,
1850 if (fileid
> job
->num_files
)
1852 if (job
->num_files
== 0)
1854 compressions
= (int *)calloc(fileid
, sizeof(int));
1855 filetypes
= (mime_type_t
**)calloc(fileid
, sizeof(mime_type_t
*));
1859 compressions
= (int *)realloc(job
->compressions
,
1860 sizeof(int) * fileid
);
1861 filetypes
= (mime_type_t
**)realloc(job
->filetypes
,
1862 sizeof(mime_type_t
*) *
1866 if (!compressions
|| !filetypes
)
1868 cupsdLogMessage(CUPSD_LOG_ERROR
,
1869 "[Job %d] Ran out of memory for job file types.",
1872 ippDelete(job
->attrs
);
1881 if (job
->compressions
)
1883 free(job
->compressions
);
1884 job
->compressions
= NULL
;
1889 free(job
->filetypes
);
1890 job
->filetypes
= NULL
;
1897 job
->compressions
= compressions
;
1898 job
->filetypes
= filetypes
;
1899 job
->num_files
= fileid
;
1902 job
->filetypes
[fileid
- 1] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
1903 job
->compressions
+ fileid
- 1);
1905 if (!job
->filetypes
[fileid
- 1])
1906 job
->filetypes
[fileid
- 1] = mimeType(MimeDatabase
, "application",
1912 * Load authentication information as needed...
1915 if (job
->state_value
< IPP_JOB_STOPPED
)
1917 snprintf(jobfile
, sizeof(jobfile
), "%s/a%05d", RequestRoot
, job
->id
);
1920 i
< (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0]));
1922 cupsdClearString(job
->auth_env
+ i
);
1923 cupsdClearString(&job
->auth_uid
);
1925 if ((fp
= cupsFileOpen(jobfile
, "r")) != NULL
)
1927 int bytes
, /* Size of auth data */
1928 linenum
= 1; /* Current line number */
1929 char line
[65536], /* Line from file */
1930 *value
, /* Value from line */
1931 data
[65536]; /* Decoded data */
1934 if (cupsFileGets(fp
, line
, sizeof(line
)) &&
1935 !strcmp(line
, "CUPSD-AUTH-V2"))
1938 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
1944 bytes
= sizeof(data
);
1945 httpDecode64_2(data
, &bytes
, value
);
1948 * Assign environment variables...
1951 if (!strcmp(line
, "uid"))
1953 cupsdSetStringf(&job
->auth_uid
, "AUTH_UID=%s", value
);
1956 else if (i
>= (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0])))
1959 if (!strcmp(line
, "username"))
1960 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_USERNAME=%s", data
);
1961 else if (!strcmp(line
, "domain"))
1962 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_DOMAIN=%s", data
);
1963 else if (!strcmp(line
, "password"))
1964 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_PASSWORD=%s", data
);
1965 else if (!strcmp(line
, "negotiate"))
1966 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_NEGOTIATE=%s", data
);
1978 job
->access_time
= time(NULL
);
1982 * If we get here then something bad happened...
1987 ippDelete(job
->attrs
);
1990 remove_job_history(job
);
1991 remove_job_files(job
);
1998 * 'cupsdMoveJob()' - Move the specified job to a different destination.
2002 cupsdMoveJob(cupsd_job_t
*job
, /* I - Job */
2003 cupsd_printer_t
*p
) /* I - Destination printer or class */
2005 ipp_attribute_t
*attr
; /* job-printer-uri attribute */
2006 const char *olddest
; /* Old destination */
2007 cupsd_printer_t
*oldp
; /* Old pointer */
2011 * Don't move completed jobs...
2014 if (job
->state_value
> IPP_JOB_STOPPED
)
2018 * Get the old destination...
2021 olddest
= job
->dest
;
2024 oldp
= job
->printer
;
2026 oldp
= cupsdFindDest(olddest
);
2029 * Change the destination information...
2032 if (job
->state_value
> IPP_JOB_HELD
)
2033 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
2034 "Stopping job prior to move.");
2036 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, oldp
, job
,
2037 "Job #%d moved from %s to %s.", job
->id
, olddest
,
2040 cupsdSetString(&job
->dest
, p
->name
);
2041 job
->dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_REMOTE
);
2043 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
2044 IPP_TAG_URI
)) != NULL
)
2045 cupsdSetString(&(attr
->values
[0].string
.text
), p
->uri
);
2047 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, p
, job
,
2048 "Job #%d moved from %s to %s.", job
->id
, olddest
,
2052 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2057 * 'cupsdReleaseJob()' - Release the specified job.
2061 cupsdReleaseJob(cupsd_job_t
*job
) /* I - Job */
2063 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdReleaseJob(job=%p(%d))", job
,
2066 if (job
->state_value
== IPP_JOB_HELD
)
2069 * Add trailing banner as needed...
2072 if (job
->pending_timeout
)
2073 cupsdTimeoutJob(job
);
2075 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
2076 "Job released by user.");
2082 * 'cupsdRestartJob()' - Restart the specified job.
2086 cupsdRestartJob(cupsd_job_t
*job
) /* I - Job */
2088 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdRestartJob(job=%p(%d))", job
,
2091 if (job
->state_value
== IPP_JOB_STOPPED
|| job
->num_files
)
2092 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
2093 "Job restarted by user.");
2098 * 'cupsdSaveAllJobs()' - Save a summary of all jobs to disk.
2102 cupsdSaveAllJobs(void)
2104 int i
; /* Looping var */
2105 cups_file_t
*fp
; /* job.cache file */
2106 char filename
[1024], /* job.cache filename */
2107 temp
[1024]; /* Temporary string */
2108 cupsd_job_t
*job
; /* Current job */
2109 time_t curtime
; /* Current time */
2110 struct tm
*curdate
; /* Current date */
2113 snprintf(filename
, sizeof(filename
), "%s/job.cache", CacheDir
);
2114 if ((fp
= cupsdCreateConfFile(filename
, ConfigFilePerm
)) == NULL
)
2117 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving job.cache...");
2120 * Write a small header to the file...
2123 curtime
= time(NULL
);
2124 curdate
= localtime(&curtime
);
2125 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
2127 cupsFilePuts(fp
, "# Job cache file for " CUPS_SVERSION
"\n");
2128 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
2129 cupsFilePrintf(fp
, "NextJobId %d\n", NextJobId
);
2132 * Write each job known to the system...
2135 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
2137 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
2139 cupsFilePrintf(fp
, "<Job %d>\n", job
->id
);
2140 cupsFilePrintf(fp
, "State %d\n", job
->state_value
);
2141 cupsFilePrintf(fp
, "Priority %d\n", job
->priority
);
2142 cupsFilePrintf(fp
, "HoldUntil %d\n", (int)job
->hold_until
);
2143 cupsFilePrintf(fp
, "Username %s\n", job
->username
);
2144 cupsFilePrintf(fp
, "Destination %s\n", job
->dest
);
2145 cupsFilePrintf(fp
, "DestType %d\n", job
->dtype
);
2146 cupsFilePrintf(fp
, "NumFiles %d\n", job
->num_files
);
2147 for (i
= 0; i
< job
->num_files
; i
++)
2148 cupsFilePrintf(fp
, "File %d %s/%s %d\n", i
+ 1, job
->filetypes
[i
]->super
,
2149 job
->filetypes
[i
]->type
, job
->compressions
[i
]);
2150 cupsFilePuts(fp
, "</Job>\n");
2153 cupsdCloseCreatedConfFile(fp
, filename
);
2158 * 'cupsdSaveJob()' - Save a job to disk.
2162 cupsdSaveJob(cupsd_job_t
*job
) /* I - Job */
2164 char filename
[1024]; /* Job control filename */
2165 cups_file_t
*fp
; /* Job file */
2168 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSaveJob(job=%p(%d)): job->attrs=%p",
2169 job
, job
->id
, job
->attrs
);
2171 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
, job
->id
);
2173 if ((fp
= cupsdCreateConfFile(filename
, ConfigFilePerm
& 0600)) == NULL
)
2176 fchown(cupsFileNumber(fp
), RunUser
, Group
);
2178 job
->attrs
->state
= IPP_IDLE
;
2180 if (ippWriteIO(fp
, (ipp_iocb_t
)cupsFileWrite
, 1, NULL
,
2181 job
->attrs
) != IPP_DATA
)
2183 cupsdLogMessage(CUPSD_LOG_ERROR
,
2184 "[Job %d] Unable to write job control file.", job
->id
);
2189 if (!cupsdCloseCreatedConfFile(fp
, filename
))
2195 * 'cupsdSetJobHoldUntil()' - Set the hold time for a job.
2199 cupsdSetJobHoldUntil(cupsd_job_t
*job
, /* I - Job */
2200 const char *when
, /* I - When to resume */
2201 int update
)/* I - Update job-hold-until attr? */
2203 time_t curtime
; /* Current time */
2204 struct tm
*curdate
; /* Current date */
2205 int hour
; /* Hold hour */
2206 int minute
; /* Hold minute */
2207 int second
= 0; /* Hold second */
2210 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2211 "cupsdSetJobHoldUntil(job=%p(%d), when=\"%s\", update=%d)",
2212 job
, job
->id
, when
, update
);
2217 * Update the job-hold-until attribute...
2220 ipp_attribute_t
*attr
; /* job-hold-until attribute */
2222 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2223 IPP_TAG_KEYWORD
)) == NULL
)
2224 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2227 cupsdSetString(&(attr
->values
[0].string
.text
), when
);
2229 attr
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
2230 "job-hold-until", NULL
, when
);
2234 if (isdigit(when
[0] & 255))
2235 attr
->value_tag
= IPP_TAG_NAME
;
2237 attr
->value_tag
= IPP_TAG_KEYWORD
;
2240 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2243 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-hold-until-specified");
2247 * Update the hold time...
2250 job
->cancel_time
= 0;
2252 if (!strcmp(when
, "indefinite") || !strcmp(when
, "auth-info-required"))
2255 * Hold indefinitely...
2258 job
->hold_until
= 0;
2260 if (MaxHoldTime
> 0)
2261 job
->cancel_time
= time(NULL
) + MaxHoldTime
;
2263 else if (!strcmp(when
, "day-time"))
2266 * Hold to 6am the next morning unless local time is < 6pm.
2269 curtime
= time(NULL
);
2270 curdate
= localtime(&curtime
);
2272 if (curdate
->tm_hour
< 18)
2273 job
->hold_until
= curtime
;
2275 job
->hold_until
= curtime
+
2276 ((29 - curdate
->tm_hour
) * 60 + 59 -
2277 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2279 else if (!strcmp(when
, "evening") || !strcmp(when
, "night"))
2282 * Hold to 6pm unless local time is > 6pm or < 6am.
2285 curtime
= time(NULL
);
2286 curdate
= localtime(&curtime
);
2288 if (curdate
->tm_hour
< 6 || curdate
->tm_hour
>= 18)
2289 job
->hold_until
= curtime
;
2291 job
->hold_until
= curtime
+
2292 ((17 - curdate
->tm_hour
) * 60 + 59 -
2293 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2295 else if (!strcmp(when
, "second-shift"))
2298 * Hold to 4pm unless local time is > 4pm.
2301 curtime
= time(NULL
);
2302 curdate
= localtime(&curtime
);
2304 if (curdate
->tm_hour
>= 16)
2305 job
->hold_until
= curtime
;
2307 job
->hold_until
= curtime
+
2308 ((15 - curdate
->tm_hour
) * 60 + 59 -
2309 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2311 else if (!strcmp(when
, "third-shift"))
2314 * Hold to 12am unless local time is < 8am.
2317 curtime
= time(NULL
);
2318 curdate
= localtime(&curtime
);
2320 if (curdate
->tm_hour
< 8)
2321 job
->hold_until
= curtime
;
2323 job
->hold_until
= curtime
+
2324 ((23 - curdate
->tm_hour
) * 60 + 59 -
2325 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2327 else if (!strcmp(when
, "weekend"))
2330 * Hold to weekend unless we are in the weekend.
2333 curtime
= time(NULL
);
2334 curdate
= localtime(&curtime
);
2336 if (curdate
->tm_wday
== 0 || curdate
->tm_wday
== 6)
2337 job
->hold_until
= curtime
;
2339 job
->hold_until
= curtime
+
2340 (((5 - curdate
->tm_wday
) * 24 +
2341 (17 - curdate
->tm_hour
)) * 60 + 59 -
2342 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2344 else if (sscanf(when
, "%d:%d:%d", &hour
, &minute
, &second
) >= 2)
2347 * Hold to specified GMT time (HH:MM or HH:MM:SS)...
2350 curtime
= time(NULL
);
2351 curdate
= gmtime(&curtime
);
2353 job
->hold_until
= curtime
+
2354 ((hour
- curdate
->tm_hour
) * 60 + minute
-
2355 curdate
->tm_min
) * 60 + second
- curdate
->tm_sec
;
2358 * Hold until next day as needed...
2361 if (job
->hold_until
< curtime
)
2362 job
->hold_until
+= 24 * 60 * 60;
2365 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetJobHoldUntil: hold_until=%d",
2366 (int)job
->hold_until
);
2371 * 'cupsdSetJobPriority()' - Set the priority of a job, moving it up/down in
2372 * the list as needed.
2376 cupsdSetJobPriority(
2377 cupsd_job_t
*job
, /* I - Job ID */
2378 int priority
) /* I - New priority (0 to 100) */
2380 ipp_attribute_t
*attr
; /* Job attribute */
2384 * Don't change completed jobs...
2387 if (job
->state_value
>= IPP_JOB_PROCESSING
)
2391 * Set the new priority and re-add the job into the active list...
2394 cupsArrayRemove(ActiveJobs
, job
);
2396 job
->priority
= priority
;
2398 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
2399 IPP_TAG_INTEGER
)) != NULL
)
2400 attr
->values
[0].integer
= priority
;
2402 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-priority",
2405 cupsArrayAdd(ActiveJobs
, job
);
2408 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2413 * 'cupsdSetJobState()' - Set the state of the specified print job.
2418 cupsd_job_t
*job
, /* I - Job to cancel */
2419 ipp_jstate_t newstate
, /* I - New job state */
2420 cupsd_jobaction_t action
, /* I - Action to take */
2421 const char *message
, /* I - Message to log */
2422 ...) /* I - Additional arguments as needed */
2424 int i
; /* Looping var */
2425 ipp_jstate_t oldstate
; /* Old state */
2426 char filename
[1024]; /* Job filename */
2427 ipp_attribute_t
*attr
; /* Job attribute */
2430 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2431 "cupsdSetJobState(job=%p(%d), state=%d, newstate=%d, "
2432 "action=%d, message=\"%s\")", job
, job
->id
, job
->state_value
,
2433 newstate
, action
, message
? message
: "(null)");
2437 * Make sure we have the job attributes...
2440 if (!cupsdLoadJob(job
))
2444 * Don't do anything if the state is unchanged and we aren't purging the
2448 oldstate
= job
->state_value
;
2449 if (newstate
== oldstate
&& action
!= CUPSD_JOB_PURGE
)
2453 * Stop any processes that are working on the current job...
2456 if (oldstate
== IPP_JOB_PROCESSING
)
2457 stop_job(job
, action
);
2460 * Set the new job state...
2463 job
->state
->values
[0].integer
= newstate
;
2464 job
->state_value
= newstate
;
2468 case IPP_JOB_PENDING
:
2470 * Update job-hold-until as needed...
2473 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2474 IPP_TAG_KEYWORD
)) == NULL
)
2475 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2479 attr
->value_tag
= IPP_TAG_KEYWORD
;
2480 cupsdSetString(&(attr
->values
[0].string
.text
), "no-hold");
2486 case IPP_JOB_ABORTED
:
2487 case IPP_JOB_CANCELED
:
2488 case IPP_JOB_COMPLETED
:
2489 set_time(job
, "time-at-completed");
2490 ippSetString(job
->attrs
, &job
->reasons
, 0, "processing-to-stop-point");
2495 * Log message as needed...
2500 char buffer
[2048]; /* Message buffer */
2501 va_list ap
; /* Pointer to additional arguments */
2503 va_start(ap
, message
);
2504 vsnprintf(buffer
, sizeof(buffer
), message
, ap
);
2507 if (newstate
> IPP_JOB_STOPPED
)
2508 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
, "%s", buffer
);
2510 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
, "%s", buffer
);
2512 if (newstate
== IPP_JOB_STOPPED
|| newstate
== IPP_JOB_ABORTED
)
2513 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "%s", buffer
);
2515 cupsdLogJob(job
, CUPSD_LOG_INFO
, "%s", buffer
);
2519 * Handle post-state-change actions...
2524 case IPP_JOB_PROCESSING
:
2526 * Add the job to the "printing" list...
2529 if (!cupsArrayFind(PrintingJobs
, job
))
2530 cupsArrayAdd(PrintingJobs
, job
);
2533 * Set the processing time...
2536 set_time(job
, "time-at-processing");
2538 case IPP_JOB_PENDING
:
2540 case IPP_JOB_STOPPED
:
2542 * Make sure the job is in the active list...
2545 if (!cupsArrayFind(ActiveJobs
, job
))
2546 cupsArrayAdd(ActiveJobs
, job
);
2549 * Save the job state to disk...
2553 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2556 case IPP_JOB_ABORTED
:
2557 case IPP_JOB_CANCELED
:
2558 case IPP_JOB_COMPLETED
:
2559 if (newstate
== IPP_JOB_CANCELED
)
2562 * Remove the job from the active list if there are no processes still
2566 for (i
= 0; job
->filters
[i
] < 0; i
++);
2568 if (!job
->filters
[i
] && job
->backend
<= 0)
2569 cupsArrayRemove(ActiveJobs
, job
);
2574 * Otherwise just remove the job from the active list immediately...
2577 cupsArrayRemove(ActiveJobs
, job
);
2581 * Expire job subscriptions since the job is now "completed"...
2584 cupsdExpireSubscriptions(NULL
, job
);
2588 * If we are going to sleep and the PrintingJobs count is now 0, allow the
2589 * sleep to happen immediately...
2592 if (Sleeping
&& cupsArrayCount(PrintingJobs
) == 0)
2594 #endif /* __APPLE__ */
2597 * Remove any authentication data...
2600 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
2601 if (cupsdRemoveFile(filename
) && errno
!= ENOENT
)
2602 cupsdLogMessage(CUPSD_LOG_ERROR
,
2603 "Unable to remove authentication cache: %s",
2607 i
< (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0]));
2609 cupsdClearString(job
->auth_env
+ i
);
2611 cupsdClearString(&job
->auth_uid
);
2614 * Remove the print file for good if we aren't preserving jobs or
2618 if (!JobHistory
|| !JobFiles
|| action
== CUPSD_JOB_PURGE
)
2619 remove_job_files(job
);
2621 if (JobHistory
&& action
!= CUPSD_JOB_PURGE
)
2624 * Save job state info...
2628 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2630 else if (!job
->printer
)
2633 * Delete the job immediately if not actively printing...
2636 cupsdDeleteJob(job
, CUPSD_JOB_PURGE
);
2643 * Finalize the job immediately if we forced things...
2646 if (action
>= CUPSD_JOB_FORCE
&& job
&& job
->printer
)
2647 finalize_job(job
, 0);
2650 * Update the server "busy" state...
2653 cupsdSetBusyState();
2658 * 'cupsdStopAllJobs()' - Stop all print jobs.
2663 cupsd_jobaction_t action
, /* I - Action */
2664 int kill_delay
) /* I - Number of seconds before we kill */
2666 cupsd_job_t
*job
; /* Current job */
2669 DEBUG_puts("cupsdStopAllJobs()");
2671 for (job
= (cupsd_job_t
*)cupsArrayFirst(PrintingJobs
);
2673 job
= (cupsd_job_t
*)cupsArrayNext(PrintingJobs
))
2676 job
->kill_time
= time(NULL
) + kill_delay
;
2678 cupsdSetJobState(job
, IPP_JOB_PENDING
, action
, NULL
);
2684 * 'cupsdUnloadCompletedJobs()' - Flush completed job history from memory.
2688 cupsdUnloadCompletedJobs(void)
2690 cupsd_job_t
*job
; /* Current job */
2691 time_t expire
; /* Expiration time */
2694 expire
= time(NULL
) - 60;
2696 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
2698 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
2699 if (job
->attrs
&& job
->state_value
>= IPP_JOB_STOPPED
&& !job
->printer
&&
2700 job
->access_time
< expire
)
2711 * 'cupsdUpdateJobs()' - Update the history/file files for all jobs.
2715 cupsdUpdateJobs(void)
2717 cupsd_job_t
*job
; /* Current job */
2718 time_t curtime
; /* Current time */
2719 ipp_attribute_t
*attr
; /* time-at-completed attribute */
2722 curtime
= time(NULL
);
2723 JobHistoryUpdate
= 0;
2725 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
2727 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
2729 if (job
->state_value
>= IPP_JOB_CANCELED
&&
2730 (attr
= ippFindAttribute(job
->attrs
, "time-at-completed",
2731 IPP_TAG_INTEGER
)) != NULL
)
2734 * Update history/file expiration times...
2737 if (JobHistory
< INT_MAX
)
2738 job
->history_time
= attr
->values
[0].integer
+ JobHistory
;
2740 job
->history_time
= INT_MAX
;
2742 if (job
->history_time
< curtime
)
2744 cupsdDeleteJob(job
, CUPSD_JOB_PURGE
);
2748 if (job
->history_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
2749 JobHistoryUpdate
= job
->history_time
;
2751 if (JobFiles
< INT_MAX
)
2752 job
->file_time
= attr
->values
[0].integer
+ JobFiles
;
2754 job
->file_time
= INT_MAX
;
2756 if (job
->file_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
2757 JobHistoryUpdate
= job
->file_time
;
2761 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdUpdateAllJobs: JobHistoryUpdate=%ld",
2762 (long)JobHistoryUpdate
);
2767 * 'compare_active_jobs()' - Compare the job IDs and priorities of two jobs.
2770 static int /* O - Difference */
2771 compare_active_jobs(void *first
, /* I - First job */
2772 void *second
, /* I - Second job */
2773 void *data
) /* I - App data (not used) */
2775 int diff
; /* Difference */
2780 if ((diff
= ((cupsd_job_t
*)second
)->priority
-
2781 ((cupsd_job_t
*)first
)->priority
) != 0)
2784 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
2789 * 'compare_jobs()' - Compare the job IDs of two jobs.
2792 static int /* O - Difference */
2793 compare_jobs(void *first
, /* I - First job */
2794 void *second
, /* I - Second job */
2795 void *data
) /* I - App data (not used) */
2799 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
2804 * 'dump_job_history()' - Dump any debug messages for a job.
2808 dump_job_history(cupsd_job_t
*job
) /* I - Job */
2810 int i
, /* Looping var */
2811 oldsize
; /* Current MaxLogSize */
2812 struct tm
*date
; /* Date/time value */
2813 cupsd_joblog_t
*message
; /* Current message */
2814 char temp
[2048], /* Log message */
2815 *ptr
, /* Pointer into log message */
2816 start
[256], /* Start time */
2817 end
[256]; /* End time */
2818 cupsd_printer_t
*printer
; /* Printer for job */
2822 * See if we have anything to dump...
2829 * Disable log rotation temporarily...
2832 oldsize
= MaxLogSize
;
2836 * Copy the debug messages to the log...
2839 message
= (cupsd_joblog_t
*)cupsArrayFirst(job
->history
);
2840 date
= localtime(&(message
->time
));
2841 strftime(start
, sizeof(start
), "%X", date
);
2843 message
= (cupsd_joblog_t
*)cupsArrayLast(job
->history
);
2844 date
= localtime(&(message
->time
));
2845 strftime(end
, sizeof(end
), "%X", date
);
2847 snprintf(temp
, sizeof(temp
),
2848 "[Job %d] The following messages were recorded from %s to %s",
2849 job
->id
, start
, end
);
2850 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, temp
);
2852 for (message
= (cupsd_joblog_t
*)cupsArrayFirst(job
->history
);
2854 message
= (cupsd_joblog_t
*)cupsArrayNext(job
->history
))
2855 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, message
->message
);
2857 snprintf(temp
, sizeof(temp
), "[Job %d] End of messages", job
->id
);
2858 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, temp
);
2861 * Log the printer state values...
2864 if ((printer
= job
->printer
) == NULL
)
2865 printer
= cupsdFindDest(job
->dest
);
2869 snprintf(temp
, sizeof(temp
), "[Job %d] printer-state=%d(%s)", job
->id
,
2871 printer
->state
== IPP_PRINTER_IDLE
? "idle" :
2872 printer
->state
== IPP_PRINTER_PROCESSING
? "processing" :
2874 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, temp
);
2876 snprintf(temp
, sizeof(temp
), "[Job %d] printer-state-message=\"%s\"",
2877 job
->id
, printer
->state_message
);
2878 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, temp
);
2880 snprintf(temp
, sizeof(temp
), "[Job %d] printer-state-reasons=", job
->id
);
2881 ptr
= temp
+ strlen(temp
);
2882 if (printer
->num_reasons
== 0)
2883 strlcpy(ptr
, "none", sizeof(temp
) - (ptr
- temp
));
2887 i
< printer
->num_reasons
&& ptr
< (temp
+ sizeof(temp
) - 2);
2893 strlcpy(ptr
, printer
->reasons
[i
], sizeof(temp
) - (ptr
- temp
));
2897 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, temp
);
2901 * Restore log file rotation...
2904 MaxLogSize
= oldsize
;
2907 * Free all messages...
2910 free_job_history(job
);
2915 * 'free_job_history()' - Free any log history.
2919 free_job_history(cupsd_job_t
*job
) /* I - Job */
2921 char *message
; /* Current message */
2927 for (message
= (char *)cupsArrayFirst(job
->history
);
2929 message
= (char *)cupsArrayNext(job
->history
))
2932 cupsArrayDelete(job
->history
);
2933 job
->history
= NULL
;
2938 * 'finalize_job()' - Cleanup after job filter processes and support data.
2942 finalize_job(cupsd_job_t
*job
, /* I - Job */
2943 int set_job_state
) /* I - 1 = set the job state */
2945 ipp_pstate_t printer_state
; /* New printer state value */
2946 ipp_jstate_t job_state
; /* New job state value */
2947 const char *message
; /* Message for job state */
2948 char buffer
[1024]; /* Buffer for formatted messages */
2951 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "finalize_job(job=%p(%d))", job
, job
->id
);
2954 * Clear the "connecting-to-device" reason, which is only valid when a printer
2955 * is processing, along with any remote printing job state...
2958 cupsdSetPrinterReasons(job
->printer
, "-connecting-to-device,"
2959 "cups-remote-pending,"
2960 "cups-remote-pending-held,"
2961 "cups-remote-processing,"
2962 "cups-remote-stopped,"
2963 "cups-remote-canceled,"
2964 "cups-remote-aborted,"
2965 "cups-remote-completed");
2968 * Similarly, clear the "offline-report" reason for non-USB devices since we
2969 * rarely have current information for network devices...
2972 if (strncmp(job
->printer
->device_uri
, "usb:", 4))
2973 cupsdSetPrinterReasons(job
->printer
, "-offline-report");
2976 * Free the security profile...
2979 cupsdDestroyProfile(job
->profile
);
2980 job
->profile
= NULL
;
2983 * Clear the unresponsive job watchdog timer...
2989 * Close pipes and status buffer...
2992 cupsdClosePipe(job
->print_pipes
);
2993 cupsdClosePipe(job
->back_pipes
);
2994 cupsdClosePipe(job
->side_pipes
);
2996 cupsdRemoveSelect(job
->status_pipes
[0]);
2997 cupsdClosePipe(job
->status_pipes
);
2998 cupsdStatBufDelete(job
->status_buffer
);
2999 job
->status_buffer
= NULL
;
3002 * Process the exit status...
3005 if (job
->printer
->state
== IPP_PRINTER_PROCESSING
)
3006 printer_state
= IPP_PRINTER_IDLE
;
3008 printer_state
= job
->printer
->state
;
3010 switch (job_state
= job
->state_value
)
3012 case IPP_JOB_PENDING
:
3013 message
= "Job paused.";
3017 message
= "Job held.";
3021 case IPP_JOB_PROCESSING
:
3022 case IPP_JOB_COMPLETED
:
3023 job_state
= IPP_JOB_COMPLETED
;
3024 message
= "Job completed.";
3026 ippSetString(job
->attrs
, &job
->reasons
, 0,
3027 "job-completed-successfully");
3030 case IPP_JOB_STOPPED
:
3031 message
= "Job stopped.";
3033 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-stopped");
3036 case IPP_JOB_CANCELED
:
3037 message
= "Job canceled.";
3039 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-canceled-by-user");
3042 case IPP_JOB_ABORTED
:
3043 message
= "Job aborted.";
3047 if (job
->status
< 0)
3050 * Backend had errors...
3053 int exit_code
; /* Exit code from backend */
3056 * Convert the status to an exit code. Due to the way the W* macros are
3057 * implemented on MacOS X (bug?), we have to store the exit status in a
3058 * variable first and then convert...
3061 exit_code
= -job
->status
;
3062 if (WIFEXITED(exit_code
))
3063 exit_code
= WEXITSTATUS(exit_code
);
3066 ippSetString(job
->attrs
, &job
->reasons
, 0, "cups-backend-crashed");
3067 exit_code
= job
->status
;
3070 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Backend returned status %d (%s)",
3072 exit_code
== CUPS_BACKEND_FAILED
? "failed" :
3073 exit_code
== CUPS_BACKEND_AUTH_REQUIRED
?
3074 "authentication required" :
3075 exit_code
== CUPS_BACKEND_HOLD
? "hold job" :
3076 exit_code
== CUPS_BACKEND_STOP
? "stop printer" :
3077 exit_code
== CUPS_BACKEND_CANCEL
? "cancel job" :
3078 exit_code
< 0 ? "crashed" : "unknown");
3081 * Do what needs to be done...
3087 case CUPS_BACKEND_FAILED
:
3089 * Backend failure, use the error-policy to determine how to
3093 if (job
->dtype
& CUPS_PRINTER_CLASS
)
3096 * Queued on a class - mark the job as pending and we'll retry on
3097 * another printer...
3100 if (job_state
== IPP_JOB_COMPLETED
)
3102 job_state
= IPP_JOB_PENDING
;
3103 message
= "Retrying job on another printer.";
3105 ippSetString(job
->attrs
, &job
->reasons
, 0,
3106 "resources-are-not-ready");
3109 else if (!strcmp(job
->printer
->error_policy
, "retry-current-job"))
3112 * The error policy is "retry-current-job" - mark the job as pending
3113 * and we'll retry on the same printer...
3116 if (job_state
== IPP_JOB_COMPLETED
)
3118 job_state
= IPP_JOB_PENDING
;
3119 message
= "Retrying job on same printer.";
3121 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
3124 else if ((job
->printer
->type
& CUPS_PRINTER_FAX
) ||
3125 !strcmp(job
->printer
->error_policy
, "retry-job"))
3127 if (job_state
== IPP_JOB_COMPLETED
)
3130 * The job was queued on a fax or the error policy is "retry-job" -
3131 * hold the job if the number of retries is less than the
3132 * JobRetryLimit, otherwise abort the job.
3137 if (job
->tries
> JobRetryLimit
&& JobRetryLimit
> 0)
3143 snprintf(buffer
, sizeof(buffer
),
3144 "Job aborted after %d unsuccessful attempts.",
3146 job_state
= IPP_JOB_ABORTED
;
3149 ippSetString(job
->attrs
, &job
->reasons
, 0, "aborted-by-system");
3154 * Try again in N seconds...
3157 snprintf(buffer
, sizeof(buffer
),
3158 "Job held for %d seconds since it could not be sent.",
3161 job
->hold_until
= time(NULL
) + JobRetryInterval
;
3162 job_state
= IPP_JOB_HELD
;
3165 ippSetString(job
->attrs
, &job
->reasons
, 0,
3166 "resources-are-not-ready");
3170 else if (!strcmp(job
->printer
->error_policy
, "abort-job") &&
3171 job_state
== IPP_JOB_COMPLETED
)
3173 job_state
= IPP_JOB_ABORTED
;
3174 message
= "Job aborted due to backend errors; please consult "
3175 "the error_log file for details.";
3177 ippSetString(job
->attrs
, &job
->reasons
, 0, "aborted-by-system");
3179 else if (job
->state_value
== IPP_JOB_PROCESSING
)
3181 job_state
= IPP_JOB_PENDING
;
3182 printer_state
= IPP_PRINTER_STOPPED
;
3183 message
= "Printer stopped due to backend errors; please "
3184 "consult the error_log file for details.";
3186 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
3190 case CUPS_BACKEND_CANCEL
:
3195 if (job_state
== IPP_JOB_COMPLETED
)
3197 job_state
= IPP_JOB_CANCELED
;
3198 message
= "Job canceled at printer.";
3200 ippSetString(job
->attrs
, &job
->reasons
, 0, "canceled-at-device");
3204 case CUPS_BACKEND_HOLD
:
3205 if (job_state
== IPP_JOB_COMPLETED
)
3211 cupsdSetJobHoldUntil(job
, "indefinite", 1);
3212 ippSetString(job
->attrs
, &job
->reasons
, 0,
3213 "job-hold-until-specified");
3215 job_state
= IPP_JOB_HELD
;
3216 message
= "Job held indefinitely due to backend errors; please "
3217 "consult the error_log file for details.";
3221 case CUPS_BACKEND_STOP
:
3223 * Stop the printer...
3226 printer_state
= IPP_PRINTER_STOPPED
;
3227 message
= "Printer stopped due to backend errors; please "
3228 "consult the error_log file for details.";
3230 if (job_state
== IPP_JOB_COMPLETED
)
3232 job_state
= IPP_JOB_PENDING
;
3234 ippSetString(job
->attrs
, &job
->reasons
, 0,
3235 "resources-are-not-ready");
3239 case CUPS_BACKEND_AUTH_REQUIRED
:
3241 * Hold the job for authentication...
3244 if (job_state
== IPP_JOB_COMPLETED
)
3246 cupsdSetJobHoldUntil(job
, "auth-info-required", 1);
3248 job_state
= IPP_JOB_HELD
;
3249 message
= "Job held for authentication.";
3251 ippSetString(job
->attrs
, &job
->reasons
, 0,
3252 "cups-held-for-authentication");
3256 case CUPS_BACKEND_RETRY
:
3257 if (job_state
== IPP_JOB_COMPLETED
)
3260 * Hold the job if the number of retries is less than the
3261 * JobRetryLimit, otherwise abort the job.
3266 if (job
->tries
> JobRetryLimit
&& JobRetryLimit
> 0)
3272 snprintf(buffer
, sizeof(buffer
),
3273 "Job aborted after %d unsuccessful attempts.",
3275 job_state
= IPP_JOB_ABORTED
;
3278 ippSetString(job
->attrs
, &job
->reasons
, 0, "aborted-by-system");
3283 * Try again in N seconds...
3286 snprintf(buffer
, sizeof(buffer
),
3287 "Job held for %d seconds since it could not be sent.",
3290 job
->hold_until
= time(NULL
) + JobRetryInterval
;
3291 job_state
= IPP_JOB_HELD
;
3294 ippSetString(job
->attrs
, &job
->reasons
, 0,
3295 "resources-are-not-ready");
3300 case CUPS_BACKEND_RETRY_CURRENT
:
3302 * Mark the job as pending and retry on the same printer...
3305 if (job_state
== IPP_JOB_COMPLETED
)
3307 job_state
= IPP_JOB_PENDING
;
3308 message
= "Retrying job on same printer.";
3310 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
3315 else if (job
->status
> 0)
3318 * Filter had errors; stop job...
3321 if (job_state
== IPP_JOB_COMPLETED
)
3323 job_state
= IPP_JOB_STOPPED
;
3324 message
= "Job stopped due to filter errors; please consult the "
3325 "error_log file for details.";
3327 if (WIFSIGNALED(job
->status
))
3328 ippSetString(job
->attrs
, &job
->reasons
, 0, "cups-filter-crashed");
3330 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-completed-with-errors");
3335 * Update the printer and job state.
3338 if (set_job_state
&& job_state
!= job
->state_value
)
3339 cupsdSetJobState(job
, job_state
, CUPSD_JOB_DEFAULT
, "%s", message
);
3341 cupsdSetPrinterState(job
->printer
, printer_state
,
3342 printer_state
== IPP_PRINTER_STOPPED
);
3343 update_job_attrs(job
, 0);
3348 (job
->state_value
== IPP_JOB_ABORTED
||
3349 job
->state_value
== IPP_JOB_STOPPED
))
3350 dump_job_history(job
);
3352 free_job_history(job
);
3355 cupsArrayRemove(PrintingJobs
, job
);
3358 * Clear the printer <-> job association...
3361 job
->printer
->job
= NULL
;
3362 job
->printer
= NULL
;
3365 * Try printing another job...
3368 if (printer_state
!= IPP_PRINTER_STOPPED
)
3374 * 'get_options()' - Get a string containing the job options.
3377 static char * /* O - Options string */
3378 get_options(cupsd_job_t
*job
, /* I - Job */
3379 int banner_page
, /* I - Printing a banner page? */
3380 char *copies
, /* I - Copies buffer */
3381 size_t copies_size
, /* I - Size of copies buffer */
3382 char *title
, /* I - Title buffer */
3383 size_t title_size
) /* I - Size of title buffer */
3385 int i
; /* Looping var */
3386 size_t newlength
; /* New option buffer length */
3387 char *optptr
, /* Pointer to options */
3388 *valptr
; /* Pointer in value string */
3389 ipp_attribute_t
*attr
; /* Current attribute */
3390 _ppd_cache_t
*pc
; /* PPD cache and mapping data */
3391 int num_pwgppds
; /* Number of PWG->PPD options */
3392 cups_option_t
*pwgppds
, /* PWG->PPD options */
3393 *pwgppd
, /* Current PWG->PPD option */
3394 *preset
; /* Current preset option */
3395 int print_color_mode
,
3396 /* Output mode (if any) */
3397 print_quality
; /* Print quality (if any) */
3398 const char *ppd
; /* PPD option choice */
3399 int exact
; /* Did we get an exact match? */
3400 static char *options
= NULL
;/* Full list of options */
3401 static size_t optlength
= 0; /* Length of option buffer */
3405 * Building the options string is harder than it needs to be, but for the
3406 * moment we need to pass strings for command-line args and not IPP attribute
3409 * First build an options array for any PWG->PPD mapped option/choice pairs.
3412 pc
= job
->printer
->pc
;
3417 !ippFindAttribute(job
->attrs
,
3418 "com.apple.print.DocumentTicket.PMSpoolFormat",
3420 !ippFindAttribute(job
->attrs
, "APPrinterPreset", IPP_TAG_ZERO
) &&
3421 (ippFindAttribute(job
->attrs
, "output-mode", IPP_TAG_ZERO
) ||
3422 ippFindAttribute(job
->attrs
, "print-color-mode", IPP_TAG_ZERO
) ||
3423 ippFindAttribute(job
->attrs
, "print-quality", IPP_TAG_ZERO
)))
3426 * Map output-mode and print-quality to a preset...
3429 if ((attr
= ippFindAttribute(job
->attrs
, "print-color-mode",
3430 IPP_TAG_KEYWORD
)) == NULL
)
3431 attr
= ippFindAttribute(job
->attrs
, "output-mode", IPP_TAG_KEYWORD
);
3433 if (attr
&& !strcmp(attr
->values
[0].string
.text
, "monochrome"))
3434 print_color_mode
= _PWG_PRINT_COLOR_MODE_MONOCHROME
;
3436 print_color_mode
= _PWG_PRINT_COLOR_MODE_COLOR
;
3438 if ((attr
= ippFindAttribute(job
->attrs
, "print-quality",
3439 IPP_TAG_ENUM
)) != NULL
&&
3440 attr
->values
[0].integer
>= IPP_QUALITY_DRAFT
&&
3441 attr
->values
[0].integer
<= IPP_QUALITY_HIGH
)
3442 print_quality
= attr
->values
[0].integer
- IPP_QUALITY_DRAFT
;
3444 print_quality
= _PWG_PRINT_QUALITY_NORMAL
;
3446 if (pc
->num_presets
[print_color_mode
][print_quality
] == 0)
3449 * Try to find a preset that works so that we maximize the chances of us
3450 * getting a good print using IPP attributes.
3453 if (pc
->num_presets
[print_color_mode
][_PWG_PRINT_QUALITY_NORMAL
] > 0)
3454 print_quality
= _PWG_PRINT_QUALITY_NORMAL
;
3455 else if (pc
->num_presets
[_PWG_PRINT_COLOR_MODE_COLOR
][print_quality
] > 0)
3456 print_color_mode
= _PWG_PRINT_COLOR_MODE_COLOR
;
3459 print_quality
= _PWG_PRINT_QUALITY_NORMAL
;
3460 print_color_mode
= _PWG_PRINT_COLOR_MODE_COLOR
;
3464 if (pc
->num_presets
[print_color_mode
][print_quality
] > 0)
3467 * Copy the preset options as long as the corresponding names are not
3468 * already defined in the IPP request...
3471 for (i
= pc
->num_presets
[print_color_mode
][print_quality
],
3472 preset
= pc
->presets
[print_color_mode
][print_quality
];
3476 if (!ippFindAttribute(job
->attrs
, preset
->name
, IPP_TAG_ZERO
))
3477 num_pwgppds
= cupsAddOption(preset
->name
, preset
->value
, num_pwgppds
,
3485 if (!ippFindAttribute(job
->attrs
, "InputSlot", IPP_TAG_ZERO
) &&
3486 !ippFindAttribute(job
->attrs
, "HPPaperSource", IPP_TAG_ZERO
))
3488 if ((ppd
= _ppdCacheGetInputSlot(pc
, job
->attrs
, NULL
)) != NULL
)
3489 num_pwgppds
= cupsAddOption(pc
->source_option
, ppd
, num_pwgppds
,
3492 if (!ippFindAttribute(job
->attrs
, "MediaType", IPP_TAG_ZERO
) &&
3493 (ppd
= _ppdCacheGetMediaType(pc
, job
->attrs
, NULL
)) != NULL
)
3494 num_pwgppds
= cupsAddOption("MediaType", ppd
, num_pwgppds
, &pwgppds
);
3496 if (!ippFindAttribute(job
->attrs
, "PageRegion", IPP_TAG_ZERO
) &&
3497 !ippFindAttribute(job
->attrs
, "PageSize", IPP_TAG_ZERO
) &&
3498 (ppd
= _ppdCacheGetPageSize(pc
, job
->attrs
, NULL
, &exact
)) != NULL
)
3500 num_pwgppds
= cupsAddOption("PageSize", ppd
, num_pwgppds
, &pwgppds
);
3502 if (!ippFindAttribute(job
->attrs
, "media", IPP_TAG_ZERO
))
3503 num_pwgppds
= cupsAddOption("media", ppd
, num_pwgppds
, &pwgppds
);
3506 if (!ippFindAttribute(job
->attrs
, "OutputBin", IPP_TAG_ZERO
) &&
3507 (attr
= ippFindAttribute(job
->attrs
, "output-bin",
3508 IPP_TAG_ZERO
)) != NULL
&&
3509 (attr
->value_tag
== IPP_TAG_KEYWORD
||
3510 attr
->value_tag
== IPP_TAG_NAME
) &&
3511 (ppd
= _ppdCacheGetOutputBin(pc
, attr
->values
[0].string
.text
)) != NULL
)
3514 * Map output-bin to OutputBin option...
3517 num_pwgppds
= cupsAddOption("OutputBin", ppd
, num_pwgppds
, &pwgppds
);
3520 if (pc
->sides_option
&&
3521 !ippFindAttribute(job
->attrs
, pc
->sides_option
, IPP_TAG_ZERO
) &&
3522 (attr
= ippFindAttribute(job
->attrs
, "sides", IPP_TAG_KEYWORD
)) != NULL
)
3525 * Map sides to duplex option...
3528 if (!strcmp(attr
->values
[0].string
.text
, "one-sided"))
3529 num_pwgppds
= cupsAddOption(pc
->sides_option
, pc
->sides_1sided
,
3530 num_pwgppds
, &pwgppds
);
3531 else if (!strcmp(attr
->values
[0].string
.text
, "two-sided-long-edge"))
3532 num_pwgppds
= cupsAddOption(pc
->sides_option
, pc
->sides_2sided_long
,
3533 num_pwgppds
, &pwgppds
);
3534 else if (!strcmp(attr
->values
[0].string
.text
, "two-sided-short-edge"))
3535 num_pwgppds
= cupsAddOption(pc
->sides_option
, pc
->sides_2sided_short
,
3536 num_pwgppds
, &pwgppds
);
3540 * Map finishings values...
3543 num_pwgppds
= _ppdCacheGetFinishingOptions(pc
, job
->attrs
,
3544 IPP_FINISHINGS_NONE
, num_pwgppds
,
3549 * Figure out how much room we need...
3552 newlength
= ipp_length(job
->attrs
);
3554 for (i
= num_pwgppds
, pwgppd
= pwgppds
; i
> 0; i
--, pwgppd
++)
3555 newlength
+= 1 + strlen(pwgppd
->name
) + 1 + strlen(pwgppd
->value
);
3558 * Then allocate/reallocate the option buffer as needed...
3561 if (newlength
== 0) /* This can never happen, but Clang */
3562 newlength
= 1; /* thinks it can... */
3564 if (newlength
> optlength
|| !options
)
3567 optptr
= malloc(newlength
);
3569 optptr
= realloc(options
, newlength
);
3573 cupsdLogJob(job
, CUPSD_LOG_CRIT
,
3574 "Unable to allocate " CUPS_LLFMT
" bytes for option buffer.",
3575 CUPS_LLCAST newlength
);
3580 optlength
= newlength
;
3584 * Now loop through the attributes and convert them to the textual
3585 * representation used by the filters...
3591 snprintf(title
, title_size
, "%s-%d", job
->printer
->name
, job
->id
);
3592 strlcpy(copies
, "1", copies_size
);
3594 for (attr
= job
->attrs
->attrs
; attr
!= NULL
; attr
= attr
->next
)
3596 if (!strcmp(attr
->name
, "copies") &&
3597 attr
->value_tag
== IPP_TAG_INTEGER
)
3600 * Don't use the # copies attribute if we are printing the job sheets...
3604 snprintf(copies
, copies_size
, "%d", attr
->values
[0].integer
);
3606 else if (!strcmp(attr
->name
, "job-name") &&
3607 (attr
->value_tag
== IPP_TAG_NAME
||
3608 attr
->value_tag
== IPP_TAG_NAMELANG
))
3609 strlcpy(title
, attr
->values
[0].string
.text
, title_size
);
3610 else if (attr
->group_tag
== IPP_TAG_JOB
)
3613 * Filter out other unwanted attributes...
3616 if (attr
->value_tag
== IPP_TAG_NOVALUE
||
3617 attr
->value_tag
== IPP_TAG_MIMETYPE
||
3618 attr
->value_tag
== IPP_TAG_NAMELANG
||
3619 attr
->value_tag
== IPP_TAG_TEXTLANG
||
3620 (attr
->value_tag
== IPP_TAG_URI
&& strcmp(attr
->name
, "job-uuid")) ||
3621 attr
->value_tag
== IPP_TAG_URISCHEME
||
3622 attr
->value_tag
== IPP_TAG_BEGIN_COLLECTION
) /* Not yet supported */
3625 if (!strcmp(attr
->name
, "job-hold-until"))
3628 if (!strncmp(attr
->name
, "job-", 4) &&
3629 strcmp(attr
->name
, "job-billing") &&
3630 strcmp(attr
->name
, "job-impressions") &&
3631 strcmp(attr
->name
, "job-originating-host-name") &&
3632 strcmp(attr
->name
, "job-uuid") &&
3633 !(job
->printer
->type
& CUPS_PRINTER_REMOTE
))
3636 if ((!strcmp(attr
->name
, "job-impressions") ||
3637 !strcmp(attr
->name
, "page-label") ||
3638 !strcmp(attr
->name
, "page-border") ||
3639 !strncmp(attr
->name
, "number-up", 9) ||
3640 !strcmp(attr
->name
, "page-ranges") ||
3641 !strcmp(attr
->name
, "page-set") ||
3642 !_cups_strcasecmp(attr
->name
, "AP_FIRSTPAGE_InputSlot") ||
3643 !_cups_strcasecmp(attr
->name
, "AP_FIRSTPAGE_ManualFeed") ||
3644 !_cups_strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
3645 "PMTotalSidesImaged..n.") ||
3646 !_cups_strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
3647 "PMTotalBeginPages..n.")) &&
3652 * Otherwise add them to the list...
3655 if (optptr
> options
)
3656 strlcat(optptr
, " ", optlength
- (optptr
- options
));
3658 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
3660 strlcat(optptr
, attr
->name
, optlength
- (optptr
- options
));
3661 strlcat(optptr
, "=", optlength
- (optptr
- options
));
3664 for (i
= 0; i
< attr
->num_values
; i
++)
3667 strlcat(optptr
, ",", optlength
- (optptr
- options
));
3669 optptr
+= strlen(optptr
);
3671 switch (attr
->value_tag
)
3673 case IPP_TAG_INTEGER
:
3675 snprintf(optptr
, optlength
- (optptr
- options
),
3676 "%d", attr
->values
[i
].integer
);
3679 case IPP_TAG_BOOLEAN
:
3680 if (!attr
->values
[i
].boolean
)
3681 strlcat(optptr
, "no", optlength
- (optptr
- options
));
3683 strlcat(optptr
, attr
->name
,
3684 optlength
- (optptr
- options
));
3687 case IPP_TAG_RANGE
:
3688 if (attr
->values
[i
].range
.lower
== attr
->values
[i
].range
.upper
)
3689 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
3690 "%d", attr
->values
[i
].range
.lower
);
3692 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
3693 "%d-%d", attr
->values
[i
].range
.lower
,
3694 attr
->values
[i
].range
.upper
);
3697 case IPP_TAG_RESOLUTION
:
3698 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
3699 "%dx%d%s", attr
->values
[i
].resolution
.xres
,
3700 attr
->values
[i
].resolution
.yres
,
3701 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
3705 case IPP_TAG_STRING
:
3708 case IPP_TAG_KEYWORD
:
3709 case IPP_TAG_CHARSET
:
3710 case IPP_TAG_LANGUAGE
:
3712 for (valptr
= attr
->values
[i
].string
.text
; *valptr
;)
3714 if (strchr(" \t\n\\\'\"", *valptr
))
3716 *optptr
++ = *valptr
++;
3723 break; /* anti-compiler-warning-code */
3727 optptr
+= strlen(optptr
);
3732 * Finally loop through the PWG->PPD mapped options and add them...
3735 for (i
= num_pwgppds
, pwgppd
= pwgppds
; i
> 0; i
--, pwgppd
++)
3738 strcpy(optptr
, pwgppd
->name
);
3739 optptr
+= strlen(optptr
);
3741 strcpy(optptr
, pwgppd
->value
);
3742 optptr
+= strlen(optptr
);
3745 cupsFreeOptions(num_pwgppds
, pwgppds
);
3748 * Return the options string...
3756 * 'ipp_length()' - Compute the size of the buffer needed to hold
3757 * the textual IPP attributes.
3760 static size_t /* O - Size of attribute buffer */
3761 ipp_length(ipp_t
*ipp
) /* I - IPP request */
3763 size_t bytes
; /* Number of bytes */
3764 int i
; /* Looping var */
3765 ipp_attribute_t
*attr
; /* Current attribute */
3769 * Loop through all attributes...
3774 for (attr
= ipp
->attrs
; attr
!= NULL
; attr
= attr
->next
)
3777 * Skip attributes that won't be sent to filters...
3780 if (attr
->value_tag
== IPP_TAG_NOVALUE
||
3781 attr
->value_tag
== IPP_TAG_MIMETYPE
||
3782 attr
->value_tag
== IPP_TAG_NAMELANG
||
3783 attr
->value_tag
== IPP_TAG_TEXTLANG
||
3784 attr
->value_tag
== IPP_TAG_URI
||
3785 attr
->value_tag
== IPP_TAG_URISCHEME
)
3789 * Add space for a leading space and commas between each value.
3790 * For the first attribute, the leading space isn't used, so the
3791 * extra byte can be used as the nul terminator...
3794 bytes
++; /* " " separator */
3795 bytes
+= attr
->num_values
; /* "," separators */
3798 * Boolean attributes appear as "foo,nofoo,foo,nofoo", while
3799 * other attributes appear as "foo=value1,value2,...,valueN".
3802 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
3803 bytes
+= strlen(attr
->name
);
3805 bytes
+= attr
->num_values
* strlen(attr
->name
);
3808 * Now add the size required for each value in the attribute...
3811 switch (attr
->value_tag
)
3813 case IPP_TAG_INTEGER
:
3816 * Minimum value of a signed integer is -2147483647, or 11 digits.
3819 bytes
+= attr
->num_values
* 11;
3822 case IPP_TAG_BOOLEAN
:
3824 * Add two bytes for each false ("no") value...
3827 for (i
= 0; i
< attr
->num_values
; i
++)
3828 if (!attr
->values
[i
].boolean
)
3832 case IPP_TAG_RANGE
:
3834 * A range is two signed integers separated by a hyphen, or
3835 * 23 characters max.
3838 bytes
+= attr
->num_values
* 23;
3841 case IPP_TAG_RESOLUTION
:
3843 * A resolution is two signed integers separated by an "x" and
3844 * suffixed by the units, or 26 characters max.
3847 bytes
+= attr
->num_values
* 26;
3850 case IPP_TAG_STRING
:
3853 case IPP_TAG_KEYWORD
:
3854 case IPP_TAG_CHARSET
:
3855 case IPP_TAG_LANGUAGE
:
3858 * Strings can contain characters that need quoting. We need
3859 * at least 2 * len + 2 characters to cover the quotes and
3860 * any backslashes in the string.
3863 for (i
= 0; i
< attr
->num_values
; i
++)
3864 bytes
+= 2 * strlen(attr
->values
[i
].string
.text
) + 2;
3868 break; /* anti-compiler-warning-code */
3877 * 'load_job_cache()' - Load jobs from the job.cache file.
3881 load_job_cache(const char *filename
) /* I - job.cache filename */
3883 cups_file_t
*fp
; /* job.cache file */
3884 char line
[1024], /* Line buffer */
3885 *value
; /* Value on line */
3886 int linenum
; /* Line number in file */
3887 cupsd_job_t
*job
; /* Current job */
3888 int jobid
; /* Job ID */
3889 char jobfile
[1024]; /* Job filename */
3893 * Open the job.cache file...
3896 if ((fp
= cupsdOpenConfFile(filename
)) == NULL
)
3898 load_request_root();
3903 * Read entries from the job cache file and create jobs as needed.
3906 cupsdLogMessage(CUPSD_LOG_INFO
, "Loading job cache file \"%s\"...",
3912 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
3914 if (!_cups_strcasecmp(line
, "NextJobId"))
3917 NextJobId
= atoi(value
);
3919 else if (!_cups_strcasecmp(line
, "<Job"))
3923 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing </Job> directive on line %d.",
3930 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing job ID on line %d.", linenum
);
3934 jobid
= atoi(value
);
3938 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad job ID %d on line %d.", jobid
,
3943 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, jobid
);
3944 if (access(jobfile
, 0))
3946 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d.N", RequestRoot
, jobid
);
3947 if (access(jobfile
, 0))
3949 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Job %d] Files have gone away.",
3955 job
= calloc(1, sizeof(cupsd_job_t
));
3958 cupsdLogMessage(CUPSD_LOG_EMERG
,
3959 "[Job %d] Unable to allocate memory for job.", jobid
);
3964 job
->back_pipes
[0] = -1;
3965 job
->back_pipes
[1] = -1;
3966 job
->print_pipes
[0] = -1;
3967 job
->print_pipes
[1] = -1;
3968 job
->side_pipes
[0] = -1;
3969 job
->side_pipes
[1] = -1;
3970 job
->status_pipes
[0] = -1;
3971 job
->status_pipes
[1] = -1;
3973 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Loading from cache...",
3978 cupsdLogMessage(CUPSD_LOG_ERROR
,
3979 "Missing <Job #> directive on line %d.", linenum
);
3982 else if (!_cups_strcasecmp(line
, "</Job>"))
3984 cupsArrayAdd(Jobs
, job
);
3986 if (job
->state_value
<= IPP_JOB_STOPPED
&& cupsdLoadJob(job
))
3987 cupsArrayAdd(ActiveJobs
, job
);
3993 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing value on line %d.", linenum
);
3996 else if (!_cups_strcasecmp(line
, "State"))
3998 job
->state_value
= (ipp_jstate_t
)atoi(value
);
4000 if (job
->state_value
< IPP_JOB_PENDING
)
4001 job
->state_value
= IPP_JOB_PENDING
;
4002 else if (job
->state_value
> IPP_JOB_COMPLETED
)
4003 job
->state_value
= IPP_JOB_COMPLETED
;
4005 else if (!_cups_strcasecmp(line
, "HoldUntil"))
4007 job
->hold_until
= atoi(value
);
4009 else if (!_cups_strcasecmp(line
, "Priority"))
4011 job
->priority
= atoi(value
);
4013 else if (!_cups_strcasecmp(line
, "Username"))
4015 cupsdSetString(&job
->username
, value
);
4017 else if (!_cups_strcasecmp(line
, "Destination"))
4019 cupsdSetString(&job
->dest
, value
);
4021 else if (!_cups_strcasecmp(line
, "DestType"))
4023 job
->dtype
= (cups_ptype_t
)atoi(value
);
4025 else if (!_cups_strcasecmp(line
, "NumFiles"))
4027 job
->num_files
= atoi(value
);
4029 if (job
->num_files
< 0)
4031 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad NumFiles value %d on line %d.",
4032 job
->num_files
, linenum
);
4037 if (job
->num_files
> 0)
4039 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-001", RequestRoot
,
4041 if (access(jobfile
, 0))
4043 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Data files have gone away.",
4049 job
->filetypes
= calloc(job
->num_files
, sizeof(mime_type_t
*));
4050 job
->compressions
= calloc(job
->num_files
, sizeof(int));
4052 if (!job
->filetypes
|| !job
->compressions
)
4054 cupsdLogMessage(CUPSD_LOG_EMERG
,
4055 "[Job %d] Unable to allocate memory for %d files.",
4056 job
->id
, job
->num_files
);
4061 else if (!_cups_strcasecmp(line
, "File"))
4063 int number
, /* File number */
4064 compression
; /* Compression value */
4065 char super
[MIME_MAX_SUPER
], /* MIME super type */
4066 type
[MIME_MAX_TYPE
]; /* MIME type */
4069 if (sscanf(value
, "%d%*[ \t]%15[^/]/%255s%d", &number
, super
, type
,
4072 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File on line %d.", linenum
);
4076 if (number
< 1 || number
> job
->num_files
)
4078 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File number %d on line %d.",
4085 job
->compressions
[number
] = compression
;
4086 job
->filetypes
[number
] = mimeType(MimeDatabase
, super
, type
);
4088 if (!job
->filetypes
[number
])
4091 * If the original MIME type is unknown, auto-type it!
4094 cupsdLogMessage(CUPSD_LOG_ERROR
,
4095 "[Job %d] Unknown MIME type %s/%s for file %d.",
4096 job
->id
, super
, type
, number
+ 1);
4098 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
4099 job
->id
, number
+ 1);
4100 job
->filetypes
[number
] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
4101 job
->compressions
+ number
);
4104 * If that didn't work, assume it is raw...
4107 if (!job
->filetypes
[number
])
4108 job
->filetypes
[number
] = mimeType(MimeDatabase
, "application",
4113 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unknown %s directive on line %d.",
4122 * 'load_next_job_id()' - Load the NextJobId value from the job.cache file.
4126 load_next_job_id(const char *filename
) /* I - job.cache filename */
4128 cups_file_t
*fp
; /* job.cache file */
4129 char line
[1024], /* Line buffer */
4130 *value
; /* Value on line */
4131 int linenum
; /* Line number in file */
4132 int next_job_id
; /* NextJobId value from line */
4136 * Read the NextJobId directive from the job.cache file and use
4137 * the value (if any).
4140 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
4142 if (errno
!= ENOENT
)
4143 cupsdLogMessage(CUPSD_LOG_ERROR
,
4144 "Unable to open job cache file \"%s\": %s",
4145 filename
, strerror(errno
));
4150 cupsdLogMessage(CUPSD_LOG_INFO
,
4151 "Loading NextJobId from job cache file \"%s\"...", filename
);
4155 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
4157 if (!_cups_strcasecmp(line
, "NextJobId"))
4161 next_job_id
= atoi(value
);
4163 if (next_job_id
> NextJobId
)
4164 NextJobId
= next_job_id
;
4175 * 'load_request_root()' - Load jobs from the RequestRoot directory.
4179 load_request_root(void)
4181 cups_dir_t
*dir
; /* Directory */
4182 cups_dentry_t
*dent
; /* Directory entry */
4183 cupsd_job_t
*job
; /* New job */
4187 * Open the requests directory...
4190 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Scanning %s for jobs...", RequestRoot
);
4192 if ((dir
= cupsDirOpen(RequestRoot
)) == NULL
)
4194 cupsdLogMessage(CUPSD_LOG_ERROR
,
4195 "Unable to open spool directory \"%s\": %s",
4196 RequestRoot
, strerror(errno
));
4201 * Read all the c##### files...
4204 while ((dent
= cupsDirRead(dir
)) != NULL
)
4205 if (strlen(dent
->filename
) >= 6 && dent
->filename
[0] == 'c')
4208 * Allocate memory for the job...
4211 if ((job
= calloc(sizeof(cupsd_job_t
), 1)) == NULL
)
4213 cupsdLogMessage(CUPSD_LOG_ERROR
, "Ran out of memory for jobs.");
4219 * Assign the job ID...
4222 job
->id
= atoi(dent
->filename
+ 1);
4223 job
->back_pipes
[0] = -1;
4224 job
->back_pipes
[1] = -1;
4225 job
->print_pipes
[0] = -1;
4226 job
->print_pipes
[1] = -1;
4227 job
->side_pipes
[0] = -1;
4228 job
->side_pipes
[1] = -1;
4229 job
->status_pipes
[0] = -1;
4230 job
->status_pipes
[1] = -1;
4232 if (job
->id
>= NextJobId
)
4233 NextJobId
= job
->id
+ 1;
4239 if (cupsdLoadJob(job
))
4242 * Insert the job into the array, sorting by job priority and ID...
4245 cupsArrayAdd(Jobs
, job
);
4247 if (job
->state_value
<= IPP_JOB_STOPPED
)
4248 cupsArrayAdd(ActiveJobs
, job
);
4259 * 'remove_job_files()' - Remove the document files for a job.
4263 remove_job_files(cupsd_job_t
*job
) /* I - Job */
4265 int i
; /* Looping var */
4266 char filename
[1024]; /* Document filename */
4269 if (job
->num_files
<= 0)
4272 for (i
= 1; i
<= job
->num_files
; i
++)
4274 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
4277 cupsdRemoveFile(filename
);
4282 free(job
->filetypes
);
4283 free(job
->compressions
);
4287 job
->filetypes
= NULL
;
4288 job
->compressions
= NULL
;
4290 LastEvent
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
4295 * 'remove_job_history()' - Remove the control file for a job.
4299 remove_job_history(cupsd_job_t
*job
) /* I - Job */
4301 char filename
[1024]; /* Control filename */
4305 * Remove the job info file...
4308 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
,
4311 cupsdRemoveFile(filename
);
4315 LastEvent
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
4320 * 'set_time()' - Set one of the "time-at-xyz" attributes.
4324 set_time(cupsd_job_t
*job
, /* I - Job to update */
4325 const char *name
) /* I - Name of attribute */
4327 ipp_attribute_t
*attr
; /* Time attribute */
4328 time_t curtime
; /* Current time */
4331 curtime
= time(NULL
);
4333 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "%s=%ld", name
, (long)curtime
);
4335 if ((attr
= ippFindAttribute(job
->attrs
, name
, IPP_TAG_ZERO
)) != NULL
)
4337 attr
->value_tag
= IPP_TAG_INTEGER
;
4338 attr
->values
[0].integer
= curtime
;
4341 if (!strcmp(name
, "time-at-completed"))
4343 if (JobHistory
< INT_MAX
)
4344 job
->history_time
= attr
->values
[0].integer
+ JobHistory
;
4346 job
->history_time
= INT_MAX
;
4348 if (job
->history_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
4349 JobHistoryUpdate
= job
->history_time
;
4351 if (JobFiles
< INT_MAX
)
4352 job
->file_time
= attr
->values
[0].integer
+ JobFiles
;
4354 job
->file_time
= INT_MAX
;
4356 if (job
->file_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
4357 JobHistoryUpdate
= job
->file_time
;
4359 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_time: JobHistoryUpdate=%ld",
4360 (long)JobHistoryUpdate
);
4366 * 'start_job()' - Start a print job.
4370 start_job(cupsd_job_t
*job
, /* I - Job ID */
4371 cupsd_printer_t
*printer
) /* I - Printer to print job */
4373 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "start_job(job=%p(%d), printer=%p(%s))",
4374 job
, job
->id
, printer
, printer
->name
);
4377 * Make sure we have some files around before we try to print...
4380 if (job
->num_files
== 0)
4382 ippSetString(job
->attrs
, &job
->reasons
, 0, "aborted-by-system");
4383 cupsdSetJobState(job
, IPP_JOB_ABORTED
, CUPSD_JOB_DEFAULT
,
4384 "Aborting job because it has no files.");
4389 * Update the printer and job state to "processing"...
4392 if (!cupsdLoadJob(job
))
4395 if (!job
->printer_message
)
4396 job
->printer_message
= ippFindAttribute(job
->attrs
,
4397 "job-printer-state-message",
4399 if (job
->printer_message
)
4400 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
), "");
4402 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-printing");
4403 cupsdSetJobState(job
, IPP_JOB_PROCESSING
, CUPSD_JOB_DEFAULT
, NULL
);
4404 cupsdSetPrinterState(printer
, IPP_PRINTER_PROCESSING
, 0);
4405 cupsdSetPrinterReasons(printer
, "-cups-remote-pending,"
4406 "cups-remote-pending-held,"
4407 "cups-remote-processing,"
4408 "cups-remote-stopped,"
4409 "cups-remote-canceled,"
4410 "cups-remote-aborted,"
4411 "cups-remote-completed");
4414 job
->current_file
= 0;
4416 job
->history_time
= 0;
4418 job
->printer
= printer
;
4422 job
->cancel_time
= time(NULL
) + MaxJobTime
;
4424 job
->cancel_time
= 0;
4427 * Setup the last exit status and security profiles...
4431 job
->profile
= cupsdCreateProfile(job
->id
);
4434 * Create the status pipes and buffer...
4437 if (cupsdOpenPipe(job
->status_pipes
))
4439 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
4440 "Unable to create job status pipes - %s.", strerror(errno
));
4442 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
4443 "Job stopped because the scheduler could not create the "
4444 "job status pipes.");
4446 cupsdDestroyProfile(job
->profile
);
4447 job
->profile
= NULL
;
4451 job
->status_buffer
= cupsdStatBufNew(job
->status_pipes
[0], NULL
);
4452 job
->status_level
= CUPSD_LOG_INFO
;
4455 * Create the backchannel pipes and make them non-blocking...
4458 if (cupsdOpenPipe(job
->back_pipes
))
4460 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
4461 "Unable to create back-channel pipes - %s.", strerror(errno
));
4463 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
4464 "Job stopped because the scheduler could not create the "
4465 "back-channel pipes.");
4467 cupsdClosePipe(job
->status_pipes
);
4468 cupsdStatBufDelete(job
->status_buffer
);
4469 job
->status_buffer
= NULL
;
4471 cupsdDestroyProfile(job
->profile
);
4472 job
->profile
= NULL
;
4476 fcntl(job
->back_pipes
[0], F_SETFL
,
4477 fcntl(job
->back_pipes
[0], F_GETFL
) | O_NONBLOCK
);
4478 fcntl(job
->back_pipes
[1], F_SETFL
,
4479 fcntl(job
->back_pipes
[1], F_GETFL
) | O_NONBLOCK
);
4482 * Create the side-channel pipes and make them non-blocking...
4485 if (socketpair(AF_LOCAL
, SOCK_STREAM
, 0, job
->side_pipes
))
4487 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
4488 "Unable to create side-channel pipes - %s.", strerror(errno
));
4490 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
4491 "Job stopped because the scheduler could not create the "
4492 "side-channel pipes.");
4494 cupsdClosePipe(job
->back_pipes
);
4496 cupsdClosePipe(job
->status_pipes
);
4497 cupsdStatBufDelete(job
->status_buffer
);
4498 job
->status_buffer
= NULL
;
4500 cupsdDestroyProfile(job
->profile
);
4501 job
->profile
= NULL
;
4505 fcntl(job
->side_pipes
[0], F_SETFL
,
4506 fcntl(job
->side_pipes
[0], F_GETFL
) | O_NONBLOCK
);
4507 fcntl(job
->side_pipes
[1], F_SETFL
,
4508 fcntl(job
->side_pipes
[1], F_GETFL
) | O_NONBLOCK
);
4510 fcntl(job
->side_pipes
[0], F_SETFD
,
4511 fcntl(job
->side_pipes
[0], F_GETFD
) | FD_CLOEXEC
);
4512 fcntl(job
->side_pipes
[1], F_SETFD
,
4513 fcntl(job
->side_pipes
[1], F_GETFD
) | FD_CLOEXEC
);
4516 * Now start the first file in the job...
4519 cupsdContinueJob(job
);
4524 * 'stop_job()' - Stop a print job.
4528 stop_job(cupsd_job_t
*job
, /* I - Job */
4529 cupsd_jobaction_t action
) /* I - Action */
4531 int i
; /* Looping var */
4534 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "stop_job(job=%p(%d), action=%d)", job
,
4537 FilterLevel
-= job
->cost
;
4540 if (action
== CUPSD_JOB_DEFAULT
&& !job
->kill_time
)
4541 job
->kill_time
= time(NULL
) + JobKillDelay
;
4542 else if (action
>= CUPSD_JOB_FORCE
)
4545 for (i
= 0; job
->filters
[i
]; i
++)
4546 if (job
->filters
[i
] > 0)
4548 cupsdEndProcess(job
->filters
[i
], action
>= CUPSD_JOB_FORCE
);
4550 if (action
>= CUPSD_JOB_FORCE
)
4551 job
->filters
[i
] = -job
->filters
[i
];
4554 if (job
->backend
> 0)
4556 cupsdEndProcess(job
->backend
, action
>= CUPSD_JOB_FORCE
);
4558 if (action
>= CUPSD_JOB_FORCE
)
4559 job
->backend
= -job
->backend
;
4562 if (action
>= CUPSD_JOB_FORCE
)
4565 * Clear job status...
4574 * 'unload_job()' - Unload a job from memory.
4578 unload_job(cupsd_job_t
*job
) /* I - Job */
4583 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Unloading...", job
->id
);
4585 ippDelete(job
->attrs
);
4589 job
->reasons
= NULL
;
4591 job
->job_sheets
= NULL
;
4592 job
->printer_message
= NULL
;
4593 job
->printer_reasons
= NULL
;
4598 * 'update_job()' - Read a status update from a job's filters.
4602 update_job(cupsd_job_t
*job
) /* I - Job to check */
4604 int i
; /* Looping var */
4605 int copies
; /* Number of copies printed */
4606 char message
[CUPSD_SB_BUFFER_SIZE
],
4608 *ptr
; /* Pointer update... */
4609 int loglevel
, /* Log level for message */
4610 event
= 0; /* Events? */
4611 static const char * const levels
[] = /* Log levels */
4627 * Get the printer associated with this job; if the printer is stopped for
4628 * any reason then job->printer will be reset to NULL, so make sure we have
4629 * a valid pointer...
4632 while ((ptr
= cupsdStatBufUpdate(job
->status_buffer
, &loglevel
,
4633 message
, sizeof(message
))) != NULL
)
4636 * Process page and printer state messages as needed...
4639 if (loglevel
== CUPSD_LOG_PAGE
)
4642 * Page message; send the message to the page_log file and update the
4643 * job sheet count...
4646 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "PAGE: %s", message
);
4650 if (!_cups_strncasecmp(message
, "total ", 6))
4653 * Got a total count of pages from a backend or filter...
4656 copies
= atoi(message
+ 6);
4657 copies
-= job
->sheets
->values
[0].integer
; /* Just track the delta */
4659 else if (!sscanf(message
, "%*d%d", &copies
))
4662 job
->sheets
->values
[0].integer
+= copies
;
4664 if (job
->printer
->page_limit
)
4665 cupsdUpdateQuota(job
->printer
, job
->username
, copies
, 0);
4668 cupsdLogPage(job
, message
);
4671 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
4672 "Printed %d page(s).", job
->sheets
->values
[0].integer
);
4674 else if (loglevel
== CUPSD_LOG_STATE
)
4676 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "STATE: %s", message
);
4678 if (!strcmp(message
, "paused"))
4680 cupsdStopPrinter(job
->printer
, 1);
4683 else if (cupsdSetPrinterReasons(job
->printer
, message
))
4685 event
|= CUPSD_EVENT_PRINTER_STATE
;
4687 if (MaxJobTime
> 0 && strstr(message
, "connecting-to-device") != NULL
)
4690 * Reset cancel time after connecting to the device...
4693 for (i
= 0; i
< job
->printer
->num_reasons
; i
++)
4694 if (!strcmp(job
->printer
->reasons
[i
], "connecting-to-device"))
4697 if (i
>= job
->printer
->num_reasons
)
4698 job
->cancel_time
= time(NULL
) + MaxJobTime
;
4702 update_job_attrs(job
, 0);
4704 else if (loglevel
== CUPSD_LOG_ATTR
)
4707 * Set attribute(s)...
4710 int num_attrs
; /* Number of attributes */
4711 cups_option_t
*attrs
; /* Attributes */
4712 const char *attr
; /* Attribute */
4715 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "ATTR: %s", message
);
4717 num_attrs
= cupsParseOptions(message
, 0, &attrs
);
4719 if ((attr
= cupsGetOption("auth-info-required", num_attrs
,
4722 cupsdSetAuthInfoRequired(job
->printer
, attr
, NULL
);
4723 cupsdSetPrinterAttrs(job
->printer
);
4725 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4728 if ((attr
= cupsGetOption("job-media-progress", num_attrs
,
4731 int progress
= atoi(attr
);
4734 if (progress
>= 0 && progress
<= 100)
4736 job
->progress
= progress
;
4739 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
4740 "Printing page %d, %d%%",
4741 job
->sheets
->values
[0].integer
, job
->progress
);
4745 if ((attr
= cupsGetOption("printer-alert", num_attrs
, attrs
)) != NULL
)
4747 cupsdSetString(&job
->printer
->alert
, attr
);
4748 event
|= CUPSD_EVENT_PRINTER_STATE
;
4751 if ((attr
= cupsGetOption("printer-alert-description", num_attrs
,
4754 cupsdSetString(&job
->printer
->alert_description
, attr
);
4755 event
|= CUPSD_EVENT_PRINTER_STATE
;
4758 if ((attr
= cupsGetOption("marker-colors", num_attrs
, attrs
)) != NULL
)
4760 cupsdSetPrinterAttr(job
->printer
, "marker-colors", (char *)attr
);
4761 job
->printer
->marker_time
= time(NULL
);
4762 event
|= CUPSD_EVENT_PRINTER_STATE
;
4763 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4766 if ((attr
= cupsGetOption("marker-levels", num_attrs
, attrs
)) != NULL
)
4768 cupsdSetPrinterAttr(job
->printer
, "marker-levels", (char *)attr
);
4769 job
->printer
->marker_time
= time(NULL
);
4770 event
|= CUPSD_EVENT_PRINTER_STATE
;
4771 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4774 if ((attr
= cupsGetOption("marker-low-levels", num_attrs
, attrs
)) != NULL
)
4776 cupsdSetPrinterAttr(job
->printer
, "marker-low-levels", (char *)attr
);
4777 job
->printer
->marker_time
= time(NULL
);
4778 event
|= CUPSD_EVENT_PRINTER_STATE
;
4779 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4782 if ((attr
= cupsGetOption("marker-high-levels", num_attrs
, attrs
)) != NULL
)
4784 cupsdSetPrinterAttr(job
->printer
, "marker-high-levels", (char *)attr
);
4785 job
->printer
->marker_time
= time(NULL
);
4786 event
|= CUPSD_EVENT_PRINTER_STATE
;
4787 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4790 if ((attr
= cupsGetOption("marker-message", num_attrs
, attrs
)) != NULL
)
4792 cupsdSetPrinterAttr(job
->printer
, "marker-message", (char *)attr
);
4793 job
->printer
->marker_time
= time(NULL
);
4794 event
|= CUPSD_EVENT_PRINTER_STATE
;
4795 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4798 if ((attr
= cupsGetOption("marker-names", num_attrs
, attrs
)) != NULL
)
4800 cupsdSetPrinterAttr(job
->printer
, "marker-names", (char *)attr
);
4801 job
->printer
->marker_time
= time(NULL
);
4802 event
|= CUPSD_EVENT_PRINTER_STATE
;
4803 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4806 if ((attr
= cupsGetOption("marker-types", num_attrs
, attrs
)) != NULL
)
4808 cupsdSetPrinterAttr(job
->printer
, "marker-types", (char *)attr
);
4809 job
->printer
->marker_time
= time(NULL
);
4810 event
|= CUPSD_EVENT_PRINTER_STATE
;
4811 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4814 cupsFreeOptions(num_attrs
, attrs
);
4816 else if (loglevel
== CUPSD_LOG_PPD
)
4819 * Set attribute(s)...
4822 int num_keywords
; /* Number of keywords */
4823 cups_option_t
*keywords
; /* Keywords */
4826 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "PPD: %s", message
);
4828 num_keywords
= cupsParseOptions(message
, 0, &keywords
);
4830 if (cupsdUpdatePrinterPPD(job
->printer
, num_keywords
, keywords
))
4831 cupsdSetPrinterAttrs(job
->printer
);
4833 cupsFreeOptions(num_keywords
, keywords
);
4838 * Strip legacy message prefix...
4841 if (!strncmp(message
, "recoverable:", 12))
4844 while (isspace(*ptr
& 255))
4847 else if (!strncmp(message
, "recovered:", 10))
4850 while (isspace(*ptr
& 255))
4857 cupsdLogJob(job
, loglevel
, "%s", ptr
);
4859 if (loglevel
< CUPSD_LOG_DEBUG
&&
4860 strcmp(job
->printer
->state_message
, ptr
))
4862 strlcpy(job
->printer
->state_message
, ptr
,
4863 sizeof(job
->printer
->state_message
));
4865 event
|= CUPSD_EVENT_PRINTER_STATE
| CUPSD_EVENT_JOB_PROGRESS
;
4867 if (loglevel
<= job
->status_level
&& job
->status_level
> CUPSD_LOG_ERROR
)
4870 * Some messages show in the job-printer-state-message attribute...
4873 if (loglevel
!= CUPSD_LOG_NOTICE
)
4874 job
->status_level
= loglevel
;
4876 update_job_attrs(job
, 1);
4878 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
4879 "Set job-printer-state-message to \"%s\", "
4881 job
->printer_message
->values
[0].string
.text
,
4882 levels
[job
->status_level
]);
4887 if (!strchr(job
->status_buffer
->buffer
, '\n'))
4891 if (event
& CUPSD_EVENT_JOB_PROGRESS
)
4892 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
4893 "%s", job
->printer
->state_message
);
4894 if (event
& CUPSD_EVENT_PRINTER_STATE
)
4895 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, job
->printer
, NULL
,
4896 (job
->printer
->type
& CUPS_PRINTER_CLASS
) ?
4897 "Class \"%s\" state changed." :
4898 "Printer \"%s\" state changed.",
4899 job
->printer
->name
);
4902 if (ptr
== NULL
&& !job
->status_buffer
->bufused
)
4905 * See if all of the filters and the backend have returned their
4909 for (i
= 0; job
->filters
[i
] < 0; i
++);
4911 if (job
->filters
[i
])
4914 * EOF but we haven't collected the exit status of all filters...
4917 cupsdCheckProcess();
4921 if (job
->current_file
>= job
->num_files
&& job
->backend
> 0)
4924 * EOF but we haven't collected the exit status of the backend...
4927 cupsdCheckProcess();
4932 * Handle the end of job stuff...
4935 finalize_job(job
, 1);
4938 * Check for new jobs...
4947 * 'update_job_attrs()' - Update the job-printer-* attributes.
4951 update_job_attrs(cupsd_job_t
*job
, /* I - Job to update */
4952 int do_message
)/* I - 1 = copy job-printer-state message */
4954 int i
; /* Looping var */
4955 int num_reasons
; /* Actual number of reasons */
4956 const char * const *reasons
; /* Reasons */
4957 static const char *none
= "none"; /* "none" reason */
4961 * Get/create the job-printer-state-* attributes...
4964 if (!job
->printer_message
)
4966 if ((job
->printer_message
= ippFindAttribute(job
->attrs
,
4967 "job-printer-state-message",
4968 IPP_TAG_TEXT
)) == NULL
)
4969 job
->printer_message
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_TEXT
,
4970 "job-printer-state-message",
4974 if (!job
->printer_reasons
)
4975 job
->printer_reasons
= ippFindAttribute(job
->attrs
,
4976 "job-printer-state-reasons",
4980 * Copy or clear the printer-state-message value as needed...
4983 if (job
->state_value
!= IPP_JOB_PROCESSING
&&
4984 job
->status_level
== CUPSD_LOG_INFO
)
4986 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
), "");
4989 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
4991 else if (job
->printer
->state_message
[0] && do_message
)
4993 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
),
4994 job
->printer
->state_message
);
4997 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
5001 * ... and the printer-state-reasons value...
5004 if (job
->printer
->num_reasons
== 0)
5011 num_reasons
= job
->printer
->num_reasons
;
5012 reasons
= (const char * const *)job
->printer
->reasons
;
5015 if (!job
->printer_reasons
|| job
->printer_reasons
->num_values
!= num_reasons
)
5018 * Replace/create a job-printer-state-reasons attribute...
5021 ippDeleteAttribute(job
->attrs
, job
->printer_reasons
);
5023 job
->printer_reasons
= ippAddStrings(job
->attrs
,
5024 IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
5025 "job-printer-state-reasons",
5026 num_reasons
, NULL
, NULL
);
5031 * Don't bother clearing the reason strings if they are the same...
5034 for (i
= 0; i
< num_reasons
; i
++)
5035 if (strcmp(job
->printer_reasons
->values
[i
].string
.text
, reasons
[i
]))
5038 if (i
>= num_reasons
)
5042 * Not the same, so free the current strings...
5045 for (i
= 0; i
< num_reasons
; i
++)
5046 _cupsStrFree(job
->printer_reasons
->values
[i
].string
.text
);
5050 * Copy the reasons...
5053 for (i
= 0; i
< num_reasons
; i
++)
5054 job
->printer_reasons
->values
[i
].string
.text
= _cupsStrAlloc(reasons
[i
]);
5057 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
5062 * End of "$Id: job.c 7902 2008-09-03 14:20:17Z mike $".