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", line
);
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_ABORTED
;
3198 message
= "Job aborted due to backend errors; please consult "
3199 "the error_log file for details.";
3201 ippSetString(job
->attrs
, &job
->reasons
, 0, "aborted-by-system");
3205 case CUPS_BACKEND_HOLD
:
3206 if (job_state
== IPP_JOB_COMPLETED
)
3212 cupsdSetJobHoldUntil(job
, "indefinite", 1);
3213 ippSetString(job
->attrs
, &job
->reasons
, 0,
3214 "job-hold-until-specified");
3216 job_state
= IPP_JOB_HELD
;
3217 message
= "Job held indefinitely due to backend errors; please "
3218 "consult the error_log file for details.";
3222 case CUPS_BACKEND_STOP
:
3224 * Stop the printer...
3227 printer_state
= IPP_PRINTER_STOPPED
;
3228 message
= "Printer stopped due to backend errors; please "
3229 "consult the error_log file for details.";
3231 if (job_state
== IPP_JOB_COMPLETED
)
3233 job_state
= IPP_JOB_PENDING
;
3235 ippSetString(job
->attrs
, &job
->reasons
, 0,
3236 "resources-are-not-ready");
3240 case CUPS_BACKEND_AUTH_REQUIRED
:
3242 * Hold the job for authentication...
3245 if (job_state
== IPP_JOB_COMPLETED
)
3247 cupsdSetJobHoldUntil(job
, "auth-info-required", 1);
3249 job_state
= IPP_JOB_HELD
;
3250 message
= "Job held for authentication.";
3252 ippSetString(job
->attrs
, &job
->reasons
, 0,
3253 "cups-held-for-authentication");
3257 case CUPS_BACKEND_RETRY
:
3258 if (job_state
== IPP_JOB_COMPLETED
)
3261 * Hold the job if the number of retries is less than the
3262 * JobRetryLimit, otherwise abort the job.
3267 if (job
->tries
> JobRetryLimit
&& JobRetryLimit
> 0)
3273 snprintf(buffer
, sizeof(buffer
),
3274 "Job aborted after %d unsuccessful attempts.",
3276 job_state
= IPP_JOB_ABORTED
;
3279 ippSetString(job
->attrs
, &job
->reasons
, 0, "aborted-by-system");
3284 * Try again in N seconds...
3287 snprintf(buffer
, sizeof(buffer
),
3288 "Job held for %d seconds since it could not be sent.",
3291 job
->hold_until
= time(NULL
) + JobRetryInterval
;
3292 job_state
= IPP_JOB_HELD
;
3295 ippSetString(job
->attrs
, &job
->reasons
, 0,
3296 "resources-are-not-ready");
3301 case CUPS_BACKEND_RETRY_CURRENT
:
3303 * Mark the job as pending and retry on the same printer...
3306 if (job_state
== IPP_JOB_COMPLETED
)
3308 job_state
= IPP_JOB_PENDING
;
3309 message
= "Retrying job on same printer.";
3311 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
3316 else if (job
->status
> 0)
3319 * Filter had errors; stop job...
3322 if (job_state
== IPP_JOB_COMPLETED
)
3324 job_state
= IPP_JOB_STOPPED
;
3325 message
= "Job stopped due to filter errors; please consult the "
3326 "error_log file for details.";
3328 if (WIFSIGNALED(job
->status
))
3329 ippSetString(job
->attrs
, &job
->reasons
, 0, "cups-filter-crashed");
3331 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-completed-with-errors");
3336 * Update the printer and job state.
3339 if (set_job_state
&& job_state
!= job
->state_value
)
3340 cupsdSetJobState(job
, job_state
, CUPSD_JOB_DEFAULT
, "%s", message
);
3342 cupsdSetPrinterState(job
->printer
, printer_state
,
3343 printer_state
== IPP_PRINTER_STOPPED
);
3344 update_job_attrs(job
, 0);
3349 (job
->state_value
== IPP_JOB_ABORTED
||
3350 job
->state_value
== IPP_JOB_STOPPED
))
3351 dump_job_history(job
);
3353 free_job_history(job
);
3356 cupsArrayRemove(PrintingJobs
, job
);
3359 * Clear the printer <-> job association...
3362 job
->printer
->job
= NULL
;
3363 job
->printer
= NULL
;
3366 * Try printing another job...
3369 if (printer_state
!= IPP_PRINTER_STOPPED
)
3375 * 'get_options()' - Get a string containing the job options.
3378 static char * /* O - Options string */
3379 get_options(cupsd_job_t
*job
, /* I - Job */
3380 int banner_page
, /* I - Printing a banner page? */
3381 char *copies
, /* I - Copies buffer */
3382 size_t copies_size
, /* I - Size of copies buffer */
3383 char *title
, /* I - Title buffer */
3384 size_t title_size
) /* I - Size of title buffer */
3386 int i
; /* Looping var */
3387 size_t newlength
; /* New option buffer length */
3388 char *optptr
, /* Pointer to options */
3389 *valptr
; /* Pointer in value string */
3390 ipp_attribute_t
*attr
; /* Current attribute */
3391 _ppd_cache_t
*pc
; /* PPD cache and mapping data */
3392 int num_pwgppds
; /* Number of PWG->PPD options */
3393 cups_option_t
*pwgppds
, /* PWG->PPD options */
3394 *pwgppd
, /* Current PWG->PPD option */
3395 *preset
; /* Current preset option */
3396 int print_color_mode
,
3397 /* Output mode (if any) */
3398 print_quality
; /* Print quality (if any) */
3399 const char *ppd
; /* PPD option choice */
3400 int exact
; /* Did we get an exact match? */
3401 static char *options
= NULL
;/* Full list of options */
3402 static size_t optlength
= 0; /* Length of option buffer */
3406 * Building the options string is harder than it needs to be, but for the
3407 * moment we need to pass strings for command-line args and not IPP attribute
3410 * First build an options array for any PWG->PPD mapped option/choice pairs.
3413 pc
= job
->printer
->pc
;
3418 !ippFindAttribute(job
->attrs
,
3419 "com.apple.print.DocumentTicket.PMSpoolFormat",
3421 !ippFindAttribute(job
->attrs
, "APPrinterPreset", IPP_TAG_ZERO
) &&
3422 (ippFindAttribute(job
->attrs
, "output-mode", IPP_TAG_ZERO
) ||
3423 ippFindAttribute(job
->attrs
, "print-color-mode", IPP_TAG_ZERO
) ||
3424 ippFindAttribute(job
->attrs
, "print-quality", IPP_TAG_ZERO
)))
3427 * Map output-mode and print-quality to a preset...
3430 if ((attr
= ippFindAttribute(job
->attrs
, "print-color-mode",
3431 IPP_TAG_KEYWORD
)) == NULL
)
3432 attr
= ippFindAttribute(job
->attrs
, "output-mode", IPP_TAG_KEYWORD
);
3434 if (attr
&& !strcmp(attr
->values
[0].string
.text
, "monochrome"))
3435 print_color_mode
= _PWG_PRINT_COLOR_MODE_MONOCHROME
;
3437 print_color_mode
= _PWG_PRINT_COLOR_MODE_COLOR
;
3439 if ((attr
= ippFindAttribute(job
->attrs
, "print-quality",
3440 IPP_TAG_ENUM
)) != NULL
&&
3441 attr
->values
[0].integer
>= IPP_QUALITY_DRAFT
&&
3442 attr
->values
[0].integer
<= IPP_QUALITY_HIGH
)
3443 print_quality
= attr
->values
[0].integer
- IPP_QUALITY_DRAFT
;
3445 print_quality
= _PWG_PRINT_QUALITY_NORMAL
;
3447 if (pc
->num_presets
[print_color_mode
][print_quality
] == 0)
3450 * Try to find a preset that works so that we maximize the chances of us
3451 * getting a good print using IPP attributes.
3454 if (pc
->num_presets
[print_color_mode
][_PWG_PRINT_QUALITY_NORMAL
] > 0)
3455 print_quality
= _PWG_PRINT_QUALITY_NORMAL
;
3456 else if (pc
->num_presets
[_PWG_PRINT_COLOR_MODE_COLOR
][print_quality
] > 0)
3457 print_color_mode
= _PWG_PRINT_COLOR_MODE_COLOR
;
3460 print_quality
= _PWG_PRINT_QUALITY_NORMAL
;
3461 print_color_mode
= _PWG_PRINT_COLOR_MODE_COLOR
;
3465 if (pc
->num_presets
[print_color_mode
][print_quality
] > 0)
3468 * Copy the preset options as long as the corresponding names are not
3469 * already defined in the IPP request...
3472 for (i
= pc
->num_presets
[print_color_mode
][print_quality
],
3473 preset
= pc
->presets
[print_color_mode
][print_quality
];
3477 if (!ippFindAttribute(job
->attrs
, preset
->name
, IPP_TAG_ZERO
))
3478 num_pwgppds
= cupsAddOption(preset
->name
, preset
->value
, num_pwgppds
,
3486 if (!ippFindAttribute(job
->attrs
, "InputSlot", IPP_TAG_ZERO
) &&
3487 !ippFindAttribute(job
->attrs
, "HPPaperSource", IPP_TAG_ZERO
))
3489 if ((ppd
= _ppdCacheGetInputSlot(pc
, job
->attrs
, NULL
)) != NULL
)
3490 num_pwgppds
= cupsAddOption(pc
->source_option
, ppd
, num_pwgppds
,
3493 if (!ippFindAttribute(job
->attrs
, "MediaType", IPP_TAG_ZERO
) &&
3494 (ppd
= _ppdCacheGetMediaType(pc
, job
->attrs
, NULL
)) != NULL
)
3495 num_pwgppds
= cupsAddOption("MediaType", ppd
, num_pwgppds
, &pwgppds
);
3497 if (!ippFindAttribute(job
->attrs
, "PageRegion", IPP_TAG_ZERO
) &&
3498 !ippFindAttribute(job
->attrs
, "PageSize", IPP_TAG_ZERO
) &&
3499 (ppd
= _ppdCacheGetPageSize(pc
, job
->attrs
, NULL
, &exact
)) != NULL
)
3501 num_pwgppds
= cupsAddOption("PageSize", ppd
, num_pwgppds
, &pwgppds
);
3503 if (!ippFindAttribute(job
->attrs
, "media", IPP_TAG_ZERO
))
3504 num_pwgppds
= cupsAddOption("media", ppd
, num_pwgppds
, &pwgppds
);
3507 if (!ippFindAttribute(job
->attrs
, "OutputBin", IPP_TAG_ZERO
) &&
3508 (attr
= ippFindAttribute(job
->attrs
, "output-bin",
3509 IPP_TAG_ZERO
)) != NULL
&&
3510 (attr
->value_tag
== IPP_TAG_KEYWORD
||
3511 attr
->value_tag
== IPP_TAG_NAME
) &&
3512 (ppd
= _ppdCacheGetOutputBin(pc
, attr
->values
[0].string
.text
)) != NULL
)
3515 * Map output-bin to OutputBin option...
3518 num_pwgppds
= cupsAddOption("OutputBin", ppd
, num_pwgppds
, &pwgppds
);
3521 if (pc
->sides_option
&&
3522 !ippFindAttribute(job
->attrs
, pc
->sides_option
, IPP_TAG_ZERO
) &&
3523 (attr
= ippFindAttribute(job
->attrs
, "sides", IPP_TAG_KEYWORD
)) != NULL
)
3526 * Map sides to duplex option...
3529 if (!strcmp(attr
->values
[0].string
.text
, "one-sided"))
3530 num_pwgppds
= cupsAddOption(pc
->sides_option
, pc
->sides_1sided
,
3531 num_pwgppds
, &pwgppds
);
3532 else if (!strcmp(attr
->values
[0].string
.text
, "two-sided-long-edge"))
3533 num_pwgppds
= cupsAddOption(pc
->sides_option
, pc
->sides_2sided_long
,
3534 num_pwgppds
, &pwgppds
);
3535 else if (!strcmp(attr
->values
[0].string
.text
, "two-sided-short-edge"))
3536 num_pwgppds
= cupsAddOption(pc
->sides_option
, pc
->sides_2sided_short
,
3537 num_pwgppds
, &pwgppds
);
3541 * Map finishings values...
3544 num_pwgppds
= _ppdCacheGetFinishingOptions(pc
, job
->attrs
,
3545 IPP_FINISHINGS_NONE
, num_pwgppds
,
3550 * Figure out how much room we need...
3553 newlength
= ipp_length(job
->attrs
);
3555 for (i
= num_pwgppds
, pwgppd
= pwgppds
; i
> 0; i
--, pwgppd
++)
3556 newlength
+= 1 + strlen(pwgppd
->name
) + 1 + strlen(pwgppd
->value
);
3559 * Then allocate/reallocate the option buffer as needed...
3562 if (newlength
== 0) /* This can never happen, but Clang */
3563 newlength
= 1; /* thinks it can... */
3565 if (newlength
> optlength
|| !options
)
3568 optptr
= malloc(newlength
);
3570 optptr
= realloc(options
, newlength
);
3574 cupsdLogJob(job
, CUPSD_LOG_CRIT
,
3575 "Unable to allocate " CUPS_LLFMT
" bytes for option buffer.",
3576 CUPS_LLCAST newlength
);
3581 optlength
= newlength
;
3585 * Now loop through the attributes and convert them to the textual
3586 * representation used by the filters...
3592 snprintf(title
, title_size
, "%s-%d", job
->printer
->name
, job
->id
);
3593 strlcpy(copies
, "1", copies_size
);
3595 for (attr
= job
->attrs
->attrs
; attr
!= NULL
; attr
= attr
->next
)
3597 if (!strcmp(attr
->name
, "copies") &&
3598 attr
->value_tag
== IPP_TAG_INTEGER
)
3601 * Don't use the # copies attribute if we are printing the job sheets...
3605 snprintf(copies
, copies_size
, "%d", attr
->values
[0].integer
);
3607 else if (!strcmp(attr
->name
, "job-name") &&
3608 (attr
->value_tag
== IPP_TAG_NAME
||
3609 attr
->value_tag
== IPP_TAG_NAMELANG
))
3610 strlcpy(title
, attr
->values
[0].string
.text
, title_size
);
3611 else if (attr
->group_tag
== IPP_TAG_JOB
)
3614 * Filter out other unwanted attributes...
3617 if (attr
->value_tag
== IPP_TAG_NOVALUE
||
3618 attr
->value_tag
== IPP_TAG_MIMETYPE
||
3619 attr
->value_tag
== IPP_TAG_NAMELANG
||
3620 attr
->value_tag
== IPP_TAG_TEXTLANG
||
3621 (attr
->value_tag
== IPP_TAG_URI
&& strcmp(attr
->name
, "job-uuid")) ||
3622 attr
->value_tag
== IPP_TAG_URISCHEME
||
3623 attr
->value_tag
== IPP_TAG_BEGIN_COLLECTION
) /* Not yet supported */
3626 if (!strcmp(attr
->name
, "job-hold-until"))
3629 if (!strncmp(attr
->name
, "job-", 4) &&
3630 strcmp(attr
->name
, "job-billing") &&
3631 strcmp(attr
->name
, "job-impressions") &&
3632 strcmp(attr
->name
, "job-originating-host-name") &&
3633 strcmp(attr
->name
, "job-uuid") &&
3634 !(job
->printer
->type
& CUPS_PRINTER_REMOTE
))
3637 if ((!strcmp(attr
->name
, "job-impressions") ||
3638 !strcmp(attr
->name
, "page-label") ||
3639 !strcmp(attr
->name
, "page-border") ||
3640 !strncmp(attr
->name
, "number-up", 9) ||
3641 !strcmp(attr
->name
, "page-ranges") ||
3642 !strcmp(attr
->name
, "page-set") ||
3643 !_cups_strcasecmp(attr
->name
, "AP_FIRSTPAGE_InputSlot") ||
3644 !_cups_strcasecmp(attr
->name
, "AP_FIRSTPAGE_ManualFeed") ||
3645 !_cups_strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
3646 "PMTotalSidesImaged..n.") ||
3647 !_cups_strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
3648 "PMTotalBeginPages..n.")) &&
3653 * Otherwise add them to the list...
3656 if (optptr
> options
)
3657 strlcat(optptr
, " ", optlength
- (optptr
- options
));
3659 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
3661 strlcat(optptr
, attr
->name
, optlength
- (optptr
- options
));
3662 strlcat(optptr
, "=", optlength
- (optptr
- options
));
3665 for (i
= 0; i
< attr
->num_values
; i
++)
3668 strlcat(optptr
, ",", optlength
- (optptr
- options
));
3670 optptr
+= strlen(optptr
);
3672 switch (attr
->value_tag
)
3674 case IPP_TAG_INTEGER
:
3676 snprintf(optptr
, optlength
- (optptr
- options
),
3677 "%d", attr
->values
[i
].integer
);
3680 case IPP_TAG_BOOLEAN
:
3681 if (!attr
->values
[i
].boolean
)
3682 strlcat(optptr
, "no", optlength
- (optptr
- options
));
3684 strlcat(optptr
, attr
->name
,
3685 optlength
- (optptr
- options
));
3688 case IPP_TAG_RANGE
:
3689 if (attr
->values
[i
].range
.lower
== attr
->values
[i
].range
.upper
)
3690 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
3691 "%d", attr
->values
[i
].range
.lower
);
3693 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
3694 "%d-%d", attr
->values
[i
].range
.lower
,
3695 attr
->values
[i
].range
.upper
);
3698 case IPP_TAG_RESOLUTION
:
3699 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
3700 "%dx%d%s", attr
->values
[i
].resolution
.xres
,
3701 attr
->values
[i
].resolution
.yres
,
3702 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
3706 case IPP_TAG_STRING
:
3709 case IPP_TAG_KEYWORD
:
3710 case IPP_TAG_CHARSET
:
3711 case IPP_TAG_LANGUAGE
:
3713 for (valptr
= attr
->values
[i
].string
.text
; *valptr
;)
3715 if (strchr(" \t\n\\\'\"", *valptr
))
3717 *optptr
++ = *valptr
++;
3724 break; /* anti-compiler-warning-code */
3728 optptr
+= strlen(optptr
);
3733 * Finally loop through the PWG->PPD mapped options and add them...
3736 for (i
= num_pwgppds
, pwgppd
= pwgppds
; i
> 0; i
--, pwgppd
++)
3739 strcpy(optptr
, pwgppd
->name
);
3740 optptr
+= strlen(optptr
);
3742 strcpy(optptr
, pwgppd
->value
);
3743 optptr
+= strlen(optptr
);
3746 cupsFreeOptions(num_pwgppds
, pwgppds
);
3749 * Return the options string...
3757 * 'ipp_length()' - Compute the size of the buffer needed to hold
3758 * the textual IPP attributes.
3761 static size_t /* O - Size of attribute buffer */
3762 ipp_length(ipp_t
*ipp
) /* I - IPP request */
3764 size_t bytes
; /* Number of bytes */
3765 int i
; /* Looping var */
3766 ipp_attribute_t
*attr
; /* Current attribute */
3770 * Loop through all attributes...
3775 for (attr
= ipp
->attrs
; attr
!= NULL
; attr
= attr
->next
)
3778 * Skip attributes that won't be sent to filters...
3781 if (attr
->value_tag
== IPP_TAG_NOVALUE
||
3782 attr
->value_tag
== IPP_TAG_MIMETYPE
||
3783 attr
->value_tag
== IPP_TAG_NAMELANG
||
3784 attr
->value_tag
== IPP_TAG_TEXTLANG
||
3785 attr
->value_tag
== IPP_TAG_URI
||
3786 attr
->value_tag
== IPP_TAG_URISCHEME
)
3790 * Add space for a leading space and commas between each value.
3791 * For the first attribute, the leading space isn't used, so the
3792 * extra byte can be used as the nul terminator...
3795 bytes
++; /* " " separator */
3796 bytes
+= attr
->num_values
; /* "," separators */
3799 * Boolean attributes appear as "foo,nofoo,foo,nofoo", while
3800 * other attributes appear as "foo=value1,value2,...,valueN".
3803 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
3804 bytes
+= strlen(attr
->name
);
3806 bytes
+= attr
->num_values
* strlen(attr
->name
);
3809 * Now add the size required for each value in the attribute...
3812 switch (attr
->value_tag
)
3814 case IPP_TAG_INTEGER
:
3817 * Minimum value of a signed integer is -2147483647, or 11 digits.
3820 bytes
+= attr
->num_values
* 11;
3823 case IPP_TAG_BOOLEAN
:
3825 * Add two bytes for each false ("no") value...
3828 for (i
= 0; i
< attr
->num_values
; i
++)
3829 if (!attr
->values
[i
].boolean
)
3833 case IPP_TAG_RANGE
:
3835 * A range is two signed integers separated by a hyphen, or
3836 * 23 characters max.
3839 bytes
+= attr
->num_values
* 23;
3842 case IPP_TAG_RESOLUTION
:
3844 * A resolution is two signed integers separated by an "x" and
3845 * suffixed by the units, or 26 characters max.
3848 bytes
+= attr
->num_values
* 26;
3851 case IPP_TAG_STRING
:
3854 case IPP_TAG_KEYWORD
:
3855 case IPP_TAG_CHARSET
:
3856 case IPP_TAG_LANGUAGE
:
3859 * Strings can contain characters that need quoting. We need
3860 * at least 2 * len + 2 characters to cover the quotes and
3861 * any backslashes in the string.
3864 for (i
= 0; i
< attr
->num_values
; i
++)
3865 bytes
+= 2 * strlen(attr
->values
[i
].string
.text
) + 2;
3869 break; /* anti-compiler-warning-code */
3878 * 'load_job_cache()' - Load jobs from the job.cache file.
3882 load_job_cache(const char *filename
) /* I - job.cache filename */
3884 cups_file_t
*fp
; /* job.cache file */
3885 char line
[1024], /* Line buffer */
3886 *value
; /* Value on line */
3887 int linenum
; /* Line number in file */
3888 cupsd_job_t
*job
; /* Current job */
3889 int jobid
; /* Job ID */
3890 char jobfile
[1024]; /* Job filename */
3894 * Open the job.cache file...
3897 if ((fp
= cupsdOpenConfFile(filename
)) == NULL
)
3899 load_request_root();
3904 * Read entries from the job cache file and create jobs as needed.
3907 cupsdLogMessage(CUPSD_LOG_INFO
, "Loading job cache file \"%s\"...",
3913 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
3915 if (!_cups_strcasecmp(line
, "NextJobId"))
3918 NextJobId
= atoi(value
);
3920 else if (!_cups_strcasecmp(line
, "<Job"))
3924 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing </Job> directive on line %d.",
3931 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing job ID on line %d.", linenum
);
3935 jobid
= atoi(value
);
3939 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad job ID %d on line %d.", jobid
,
3944 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, jobid
);
3945 if (access(jobfile
, 0))
3947 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d.N", RequestRoot
, jobid
);
3948 if (access(jobfile
, 0))
3950 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Job %d] Files have gone away.",
3956 job
= calloc(1, sizeof(cupsd_job_t
));
3959 cupsdLogMessage(CUPSD_LOG_EMERG
,
3960 "[Job %d] Unable to allocate memory for job.", jobid
);
3965 job
->back_pipes
[0] = -1;
3966 job
->back_pipes
[1] = -1;
3967 job
->print_pipes
[0] = -1;
3968 job
->print_pipes
[1] = -1;
3969 job
->side_pipes
[0] = -1;
3970 job
->side_pipes
[1] = -1;
3971 job
->status_pipes
[0] = -1;
3972 job
->status_pipes
[1] = -1;
3974 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Loading from cache...",
3979 cupsdLogMessage(CUPSD_LOG_ERROR
,
3980 "Missing <Job #> directive on line %d.", linenum
);
3983 else if (!_cups_strcasecmp(line
, "</Job>"))
3985 cupsArrayAdd(Jobs
, job
);
3987 if (job
->state_value
<= IPP_JOB_STOPPED
&& cupsdLoadJob(job
))
3988 cupsArrayAdd(ActiveJobs
, job
);
3994 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing value on line %d.", linenum
);
3997 else if (!_cups_strcasecmp(line
, "State"))
3999 job
->state_value
= (ipp_jstate_t
)atoi(value
);
4001 if (job
->state_value
< IPP_JOB_PENDING
)
4002 job
->state_value
= IPP_JOB_PENDING
;
4003 else if (job
->state_value
> IPP_JOB_COMPLETED
)
4004 job
->state_value
= IPP_JOB_COMPLETED
;
4006 else if (!_cups_strcasecmp(line
, "HoldUntil"))
4008 job
->hold_until
= atoi(value
);
4010 else if (!_cups_strcasecmp(line
, "Priority"))
4012 job
->priority
= atoi(value
);
4014 else if (!_cups_strcasecmp(line
, "Username"))
4016 cupsdSetString(&job
->username
, value
);
4018 else if (!_cups_strcasecmp(line
, "Destination"))
4020 cupsdSetString(&job
->dest
, value
);
4022 else if (!_cups_strcasecmp(line
, "DestType"))
4024 job
->dtype
= (cups_ptype_t
)atoi(value
);
4026 else if (!_cups_strcasecmp(line
, "NumFiles"))
4028 job
->num_files
= atoi(value
);
4030 if (job
->num_files
< 0)
4032 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad NumFiles value %d on line %d.",
4033 job
->num_files
, linenum
);
4038 if (job
->num_files
> 0)
4040 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-001", RequestRoot
,
4042 if (access(jobfile
, 0))
4044 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Data files have gone away.",
4050 job
->filetypes
= calloc(job
->num_files
, sizeof(mime_type_t
*));
4051 job
->compressions
= calloc(job
->num_files
, sizeof(int));
4053 if (!job
->filetypes
|| !job
->compressions
)
4055 cupsdLogMessage(CUPSD_LOG_EMERG
,
4056 "[Job %d] Unable to allocate memory for %d files.",
4057 job
->id
, job
->num_files
);
4062 else if (!_cups_strcasecmp(line
, "File"))
4064 int number
, /* File number */
4065 compression
; /* Compression value */
4066 char super
[MIME_MAX_SUPER
], /* MIME super type */
4067 type
[MIME_MAX_TYPE
]; /* MIME type */
4070 if (sscanf(value
, "%d%*[ \t]%15[^/]/%255s%d", &number
, super
, type
,
4073 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File on line %d.", linenum
);
4077 if (number
< 1 || number
> job
->num_files
)
4079 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File number %d on line %d.",
4086 job
->compressions
[number
] = compression
;
4087 job
->filetypes
[number
] = mimeType(MimeDatabase
, super
, type
);
4089 if (!job
->filetypes
[number
])
4092 * If the original MIME type is unknown, auto-type it!
4095 cupsdLogMessage(CUPSD_LOG_ERROR
,
4096 "[Job %d] Unknown MIME type %s/%s for file %d.",
4097 job
->id
, super
, type
, number
+ 1);
4099 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
4100 job
->id
, number
+ 1);
4101 job
->filetypes
[number
] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
4102 job
->compressions
+ number
);
4105 * If that didn't work, assume it is raw...
4108 if (!job
->filetypes
[number
])
4109 job
->filetypes
[number
] = mimeType(MimeDatabase
, "application",
4114 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unknown %s directive on line %d.",
4123 * 'load_next_job_id()' - Load the NextJobId value from the job.cache file.
4127 load_next_job_id(const char *filename
) /* I - job.cache filename */
4129 cups_file_t
*fp
; /* job.cache file */
4130 char line
[1024], /* Line buffer */
4131 *value
; /* Value on line */
4132 int linenum
; /* Line number in file */
4133 int next_job_id
; /* NextJobId value from line */
4137 * Read the NextJobId directive from the job.cache file and use
4138 * the value (if any).
4141 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
4143 if (errno
!= ENOENT
)
4144 cupsdLogMessage(CUPSD_LOG_ERROR
,
4145 "Unable to open job cache file \"%s\": %s",
4146 filename
, strerror(errno
));
4151 cupsdLogMessage(CUPSD_LOG_INFO
,
4152 "Loading NextJobId from job cache file \"%s\"...", filename
);
4156 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
4158 if (!_cups_strcasecmp(line
, "NextJobId"))
4162 next_job_id
= atoi(value
);
4164 if (next_job_id
> NextJobId
)
4165 NextJobId
= next_job_id
;
4176 * 'load_request_root()' - Load jobs from the RequestRoot directory.
4180 load_request_root(void)
4182 cups_dir_t
*dir
; /* Directory */
4183 cups_dentry_t
*dent
; /* Directory entry */
4184 cupsd_job_t
*job
; /* New job */
4188 * Open the requests directory...
4191 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Scanning %s for jobs...", RequestRoot
);
4193 if ((dir
= cupsDirOpen(RequestRoot
)) == NULL
)
4195 cupsdLogMessage(CUPSD_LOG_ERROR
,
4196 "Unable to open spool directory \"%s\": %s",
4197 RequestRoot
, strerror(errno
));
4202 * Read all the c##### files...
4205 while ((dent
= cupsDirRead(dir
)) != NULL
)
4206 if (strlen(dent
->filename
) >= 6 && dent
->filename
[0] == 'c')
4209 * Allocate memory for the job...
4212 if ((job
= calloc(sizeof(cupsd_job_t
), 1)) == NULL
)
4214 cupsdLogMessage(CUPSD_LOG_ERROR
, "Ran out of memory for jobs.");
4220 * Assign the job ID...
4223 job
->id
= atoi(dent
->filename
+ 1);
4224 job
->back_pipes
[0] = -1;
4225 job
->back_pipes
[1] = -1;
4226 job
->print_pipes
[0] = -1;
4227 job
->print_pipes
[1] = -1;
4228 job
->side_pipes
[0] = -1;
4229 job
->side_pipes
[1] = -1;
4230 job
->status_pipes
[0] = -1;
4231 job
->status_pipes
[1] = -1;
4233 if (job
->id
>= NextJobId
)
4234 NextJobId
= job
->id
+ 1;
4240 if (cupsdLoadJob(job
))
4243 * Insert the job into the array, sorting by job priority and ID...
4246 cupsArrayAdd(Jobs
, job
);
4248 if (job
->state_value
<= IPP_JOB_STOPPED
)
4249 cupsArrayAdd(ActiveJobs
, job
);
4260 * 'remove_job_files()' - Remove the document files for a job.
4264 remove_job_files(cupsd_job_t
*job
) /* I - Job */
4266 int i
; /* Looping var */
4267 char filename
[1024]; /* Document filename */
4270 if (job
->num_files
<= 0)
4273 for (i
= 1; i
<= job
->num_files
; i
++)
4275 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
4278 cupsdRemoveFile(filename
);
4283 free(job
->filetypes
);
4284 free(job
->compressions
);
4288 job
->filetypes
= NULL
;
4289 job
->compressions
= NULL
;
4291 LastEvent
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
4296 * 'remove_job_history()' - Remove the control file for a job.
4300 remove_job_history(cupsd_job_t
*job
) /* I - Job */
4302 char filename
[1024]; /* Control filename */
4306 * Remove the job info file...
4309 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
,
4312 cupsdRemoveFile(filename
);
4316 LastEvent
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
4321 * 'set_time()' - Set one of the "time-at-xyz" attributes.
4325 set_time(cupsd_job_t
*job
, /* I - Job to update */
4326 const char *name
) /* I - Name of attribute */
4328 ipp_attribute_t
*attr
; /* Time attribute */
4329 time_t curtime
; /* Current time */
4332 curtime
= time(NULL
);
4334 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "%s=%ld", name
, (long)curtime
);
4336 if ((attr
= ippFindAttribute(job
->attrs
, name
, IPP_TAG_ZERO
)) != NULL
)
4338 attr
->value_tag
= IPP_TAG_INTEGER
;
4339 attr
->values
[0].integer
= curtime
;
4342 if (!strcmp(name
, "time-at-completed"))
4344 if (JobHistory
< INT_MAX
)
4345 job
->history_time
= attr
->values
[0].integer
+ JobHistory
;
4347 job
->history_time
= INT_MAX
;
4349 if (job
->history_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
4350 JobHistoryUpdate
= job
->history_time
;
4352 if (JobFiles
< INT_MAX
)
4353 job
->file_time
= attr
->values
[0].integer
+ JobFiles
;
4355 job
->file_time
= INT_MAX
;
4357 if (job
->file_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
4358 JobHistoryUpdate
= job
->file_time
;
4360 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_time: JobHistoryUpdate=%ld",
4361 (long)JobHistoryUpdate
);
4367 * 'start_job()' - Start a print job.
4371 start_job(cupsd_job_t
*job
, /* I - Job ID */
4372 cupsd_printer_t
*printer
) /* I - Printer to print job */
4374 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "start_job(job=%p(%d), printer=%p(%s))",
4375 job
, job
->id
, printer
, printer
->name
);
4378 * Make sure we have some files around before we try to print...
4381 if (job
->num_files
== 0)
4383 ippSetString(job
->attrs
, &job
->reasons
, 0, "aborted-by-system");
4384 cupsdSetJobState(job
, IPP_JOB_ABORTED
, CUPSD_JOB_DEFAULT
,
4385 "Aborting job because it has no files.");
4390 * Update the printer and job state to "processing"...
4393 if (!cupsdLoadJob(job
))
4396 if (!job
->printer_message
)
4397 job
->printer_message
= ippFindAttribute(job
->attrs
,
4398 "job-printer-state-message",
4400 if (job
->printer_message
)
4401 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
), "");
4403 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-printing");
4404 cupsdSetJobState(job
, IPP_JOB_PROCESSING
, CUPSD_JOB_DEFAULT
, NULL
);
4405 cupsdSetPrinterState(printer
, IPP_PRINTER_PROCESSING
, 0);
4406 cupsdSetPrinterReasons(printer
, "-cups-remote-pending,"
4407 "cups-remote-pending-held,"
4408 "cups-remote-processing,"
4409 "cups-remote-stopped,"
4410 "cups-remote-canceled,"
4411 "cups-remote-aborted,"
4412 "cups-remote-completed");
4415 job
->current_file
= 0;
4417 job
->history_time
= 0;
4419 job
->printer
= printer
;
4423 job
->cancel_time
= time(NULL
) + MaxJobTime
;
4425 job
->cancel_time
= 0;
4428 * Setup the last exit status and security profiles...
4432 job
->profile
= cupsdCreateProfile(job
->id
);
4435 * Create the status pipes and buffer...
4438 if (cupsdOpenPipe(job
->status_pipes
))
4440 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
4441 "Unable to create job status pipes - %s.", strerror(errno
));
4443 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
4444 "Job stopped because the scheduler could not create the "
4445 "job status pipes.");
4447 cupsdDestroyProfile(job
->profile
);
4448 job
->profile
= NULL
;
4452 job
->status_buffer
= cupsdStatBufNew(job
->status_pipes
[0], NULL
);
4453 job
->status_level
= CUPSD_LOG_INFO
;
4456 * Create the backchannel pipes and make them non-blocking...
4459 if (cupsdOpenPipe(job
->back_pipes
))
4461 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
4462 "Unable to create back-channel pipes - %s.", strerror(errno
));
4464 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
4465 "Job stopped because the scheduler could not create the "
4466 "back-channel pipes.");
4468 cupsdClosePipe(job
->status_pipes
);
4469 cupsdStatBufDelete(job
->status_buffer
);
4470 job
->status_buffer
= NULL
;
4472 cupsdDestroyProfile(job
->profile
);
4473 job
->profile
= NULL
;
4477 fcntl(job
->back_pipes
[0], F_SETFL
,
4478 fcntl(job
->back_pipes
[0], F_GETFL
) | O_NONBLOCK
);
4479 fcntl(job
->back_pipes
[1], F_SETFL
,
4480 fcntl(job
->back_pipes
[1], F_GETFL
) | O_NONBLOCK
);
4483 * Create the side-channel pipes and make them non-blocking...
4486 if (socketpair(AF_LOCAL
, SOCK_STREAM
, 0, job
->side_pipes
))
4488 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
4489 "Unable to create side-channel pipes - %s.", strerror(errno
));
4491 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
4492 "Job stopped because the scheduler could not create the "
4493 "side-channel pipes.");
4495 cupsdClosePipe(job
->back_pipes
);
4497 cupsdClosePipe(job
->status_pipes
);
4498 cupsdStatBufDelete(job
->status_buffer
);
4499 job
->status_buffer
= NULL
;
4501 cupsdDestroyProfile(job
->profile
);
4502 job
->profile
= NULL
;
4506 fcntl(job
->side_pipes
[0], F_SETFL
,
4507 fcntl(job
->side_pipes
[0], F_GETFL
) | O_NONBLOCK
);
4508 fcntl(job
->side_pipes
[1], F_SETFL
,
4509 fcntl(job
->side_pipes
[1], F_GETFL
) | O_NONBLOCK
);
4511 fcntl(job
->side_pipes
[0], F_SETFD
,
4512 fcntl(job
->side_pipes
[0], F_GETFD
) | FD_CLOEXEC
);
4513 fcntl(job
->side_pipes
[1], F_SETFD
,
4514 fcntl(job
->side_pipes
[1], F_GETFD
) | FD_CLOEXEC
);
4517 * Now start the first file in the job...
4520 cupsdContinueJob(job
);
4525 * 'stop_job()' - Stop a print job.
4529 stop_job(cupsd_job_t
*job
, /* I - Job */
4530 cupsd_jobaction_t action
) /* I - Action */
4532 int i
; /* Looping var */
4535 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "stop_job(job=%p(%d), action=%d)", job
,
4538 FilterLevel
-= job
->cost
;
4541 if (action
== CUPSD_JOB_DEFAULT
&& !job
->kill_time
)
4542 job
->kill_time
= time(NULL
) + JobKillDelay
;
4543 else if (action
>= CUPSD_JOB_FORCE
)
4546 for (i
= 0; job
->filters
[i
]; i
++)
4547 if (job
->filters
[i
] > 0)
4549 cupsdEndProcess(job
->filters
[i
], action
>= CUPSD_JOB_FORCE
);
4551 if (action
>= CUPSD_JOB_FORCE
)
4552 job
->filters
[i
] = -job
->filters
[i
];
4555 if (job
->backend
> 0)
4557 cupsdEndProcess(job
->backend
, action
>= CUPSD_JOB_FORCE
);
4559 if (action
>= CUPSD_JOB_FORCE
)
4560 job
->backend
= -job
->backend
;
4563 if (action
>= CUPSD_JOB_FORCE
)
4566 * Clear job status...
4575 * 'unload_job()' - Unload a job from memory.
4579 unload_job(cupsd_job_t
*job
) /* I - Job */
4584 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Unloading...", job
->id
);
4586 ippDelete(job
->attrs
);
4590 job
->reasons
= NULL
;
4592 job
->job_sheets
= NULL
;
4593 job
->printer_message
= NULL
;
4594 job
->printer_reasons
= NULL
;
4599 * 'update_job()' - Read a status update from a job's filters.
4603 update_job(cupsd_job_t
*job
) /* I - Job to check */
4605 int i
; /* Looping var */
4606 int copies
; /* Number of copies printed */
4607 char message
[CUPSD_SB_BUFFER_SIZE
],
4609 *ptr
; /* Pointer update... */
4610 int loglevel
, /* Log level for message */
4611 event
= 0; /* Events? */
4612 static const char * const levels
[] = /* Log levels */
4628 * Get the printer associated with this job; if the printer is stopped for
4629 * any reason then job->printer will be reset to NULL, so make sure we have
4630 * a valid pointer...
4633 while ((ptr
= cupsdStatBufUpdate(job
->status_buffer
, &loglevel
,
4634 message
, sizeof(message
))) != NULL
)
4637 * Process page and printer state messages as needed...
4640 if (loglevel
== CUPSD_LOG_PAGE
)
4643 * Page message; send the message to the page_log file and update the
4644 * job sheet count...
4647 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "PAGE: %s", message
);
4651 if (!_cups_strncasecmp(message
, "total ", 6))
4654 * Got a total count of pages from a backend or filter...
4657 copies
= atoi(message
+ 6);
4658 copies
-= job
->sheets
->values
[0].integer
; /* Just track the delta */
4660 else if (!sscanf(message
, "%*d%d", &copies
))
4663 job
->sheets
->values
[0].integer
+= copies
;
4665 if (job
->printer
->page_limit
)
4666 cupsdUpdateQuota(job
->printer
, job
->username
, copies
, 0);
4669 cupsdLogPage(job
, message
);
4672 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
4673 "Printed %d page(s).", job
->sheets
->values
[0].integer
);
4675 else if (loglevel
== CUPSD_LOG_STATE
)
4677 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "STATE: %s", message
);
4679 if (!strcmp(message
, "paused"))
4681 cupsdStopPrinter(job
->printer
, 1);
4684 else if (cupsdSetPrinterReasons(job
->printer
, message
))
4686 event
|= CUPSD_EVENT_PRINTER_STATE
;
4688 if (MaxJobTime
> 0 && strstr(message
, "connecting-to-device") != NULL
)
4691 * Reset cancel time after connecting to the device...
4694 for (i
= 0; i
< job
->printer
->num_reasons
; i
++)
4695 if (!strcmp(job
->printer
->reasons
[i
], "connecting-to-device"))
4698 if (i
>= job
->printer
->num_reasons
)
4699 job
->cancel_time
= time(NULL
) + MaxJobTime
;
4703 update_job_attrs(job
, 0);
4705 else if (loglevel
== CUPSD_LOG_ATTR
)
4708 * Set attribute(s)...
4711 int num_attrs
; /* Number of attributes */
4712 cups_option_t
*attrs
; /* Attributes */
4713 const char *attr
; /* Attribute */
4716 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "ATTR: %s", message
);
4718 num_attrs
= cupsParseOptions(message
, 0, &attrs
);
4720 if ((attr
= cupsGetOption("auth-info-required", num_attrs
,
4723 cupsdSetAuthInfoRequired(job
->printer
, attr
, NULL
);
4724 cupsdSetPrinterAttrs(job
->printer
);
4726 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4729 if ((attr
= cupsGetOption("job-media-progress", num_attrs
,
4732 int progress
= atoi(attr
);
4735 if (progress
>= 0 && progress
<= 100)
4737 job
->progress
= progress
;
4740 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
4741 "Printing page %d, %d%%",
4742 job
->sheets
->values
[0].integer
, job
->progress
);
4746 if ((attr
= cupsGetOption("printer-alert", num_attrs
, attrs
)) != NULL
)
4748 cupsdSetString(&job
->printer
->alert
, attr
);
4749 event
|= CUPSD_EVENT_PRINTER_STATE
;
4752 if ((attr
= cupsGetOption("printer-alert-description", num_attrs
,
4755 cupsdSetString(&job
->printer
->alert_description
, attr
);
4756 event
|= CUPSD_EVENT_PRINTER_STATE
;
4759 if ((attr
= cupsGetOption("marker-colors", num_attrs
, attrs
)) != NULL
)
4761 cupsdSetPrinterAttr(job
->printer
, "marker-colors", (char *)attr
);
4762 job
->printer
->marker_time
= time(NULL
);
4763 event
|= CUPSD_EVENT_PRINTER_STATE
;
4764 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4767 if ((attr
= cupsGetOption("marker-levels", num_attrs
, attrs
)) != NULL
)
4769 cupsdSetPrinterAttr(job
->printer
, "marker-levels", (char *)attr
);
4770 job
->printer
->marker_time
= time(NULL
);
4771 event
|= CUPSD_EVENT_PRINTER_STATE
;
4772 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4775 if ((attr
= cupsGetOption("marker-low-levels", num_attrs
, attrs
)) != NULL
)
4777 cupsdSetPrinterAttr(job
->printer
, "marker-low-levels", (char *)attr
);
4778 job
->printer
->marker_time
= time(NULL
);
4779 event
|= CUPSD_EVENT_PRINTER_STATE
;
4780 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4783 if ((attr
= cupsGetOption("marker-high-levels", num_attrs
, attrs
)) != NULL
)
4785 cupsdSetPrinterAttr(job
->printer
, "marker-high-levels", (char *)attr
);
4786 job
->printer
->marker_time
= time(NULL
);
4787 event
|= CUPSD_EVENT_PRINTER_STATE
;
4788 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4791 if ((attr
= cupsGetOption("marker-message", num_attrs
, attrs
)) != NULL
)
4793 cupsdSetPrinterAttr(job
->printer
, "marker-message", (char *)attr
);
4794 job
->printer
->marker_time
= time(NULL
);
4795 event
|= CUPSD_EVENT_PRINTER_STATE
;
4796 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4799 if ((attr
= cupsGetOption("marker-names", num_attrs
, attrs
)) != NULL
)
4801 cupsdSetPrinterAttr(job
->printer
, "marker-names", (char *)attr
);
4802 job
->printer
->marker_time
= time(NULL
);
4803 event
|= CUPSD_EVENT_PRINTER_STATE
;
4804 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4807 if ((attr
= cupsGetOption("marker-types", num_attrs
, attrs
)) != NULL
)
4809 cupsdSetPrinterAttr(job
->printer
, "marker-types", (char *)attr
);
4810 job
->printer
->marker_time
= time(NULL
);
4811 event
|= CUPSD_EVENT_PRINTER_STATE
;
4812 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4815 cupsFreeOptions(num_attrs
, attrs
);
4817 else if (loglevel
== CUPSD_LOG_PPD
)
4820 * Set attribute(s)...
4823 int num_keywords
; /* Number of keywords */
4824 cups_option_t
*keywords
; /* Keywords */
4827 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "PPD: %s", message
);
4829 num_keywords
= cupsParseOptions(message
, 0, &keywords
);
4831 if (cupsdUpdatePrinterPPD(job
->printer
, num_keywords
, keywords
))
4832 cupsdSetPrinterAttrs(job
->printer
);
4834 cupsFreeOptions(num_keywords
, keywords
);
4839 * Strip legacy message prefix...
4842 if (!strncmp(message
, "recoverable:", 12))
4845 while (isspace(*ptr
& 255))
4848 else if (!strncmp(message
, "recovered:", 10))
4851 while (isspace(*ptr
& 255))
4858 cupsdLogJob(job
, loglevel
, "%s", ptr
);
4860 if (loglevel
< CUPSD_LOG_DEBUG
&&
4861 strcmp(job
->printer
->state_message
, ptr
))
4863 strlcpy(job
->printer
->state_message
, ptr
,
4864 sizeof(job
->printer
->state_message
));
4866 event
|= CUPSD_EVENT_PRINTER_STATE
| CUPSD_EVENT_JOB_PROGRESS
;
4868 if (loglevel
<= job
->status_level
&& job
->status_level
> CUPSD_LOG_ERROR
)
4871 * Some messages show in the job-printer-state-message attribute...
4874 if (loglevel
!= CUPSD_LOG_NOTICE
)
4875 job
->status_level
= loglevel
;
4877 update_job_attrs(job
, 1);
4879 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
4880 "Set job-printer-state-message to \"%s\", "
4882 job
->printer_message
->values
[0].string
.text
,
4883 levels
[job
->status_level
]);
4888 if (!strchr(job
->status_buffer
->buffer
, '\n'))
4892 if (event
& CUPSD_EVENT_JOB_PROGRESS
)
4893 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
4894 "%s", job
->printer
->state_message
);
4895 if (event
& CUPSD_EVENT_PRINTER_STATE
)
4896 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, job
->printer
, NULL
,
4897 (job
->printer
->type
& CUPS_PRINTER_CLASS
) ?
4898 "Class \"%s\" state changed." :
4899 "Printer \"%s\" state changed.",
4900 job
->printer
->name
);
4903 if (ptr
== NULL
&& !job
->status_buffer
->bufused
)
4906 * See if all of the filters and the backend have returned their
4910 for (i
= 0; job
->filters
[i
] < 0; i
++);
4912 if (job
->filters
[i
])
4915 * EOF but we haven't collected the exit status of all filters...
4918 cupsdCheckProcess();
4922 if (job
->current_file
>= job
->num_files
&& job
->backend
> 0)
4925 * EOF but we haven't collected the exit status of the backend...
4928 cupsdCheckProcess();
4933 * Handle the end of job stuff...
4936 finalize_job(job
, 1);
4939 * Check for new jobs...
4948 * 'update_job_attrs()' - Update the job-printer-* attributes.
4952 update_job_attrs(cupsd_job_t
*job
, /* I - Job to update */
4953 int do_message
)/* I - 1 = copy job-printer-state message */
4955 int i
; /* Looping var */
4956 int num_reasons
; /* Actual number of reasons */
4957 const char * const *reasons
; /* Reasons */
4958 static const char *none
= "none"; /* "none" reason */
4962 * Get/create the job-printer-state-* attributes...
4965 if (!job
->printer_message
)
4967 if ((job
->printer_message
= ippFindAttribute(job
->attrs
,
4968 "job-printer-state-message",
4969 IPP_TAG_TEXT
)) == NULL
)
4970 job
->printer_message
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_TEXT
,
4971 "job-printer-state-message",
4975 if (!job
->printer_reasons
)
4976 job
->printer_reasons
= ippFindAttribute(job
->attrs
,
4977 "job-printer-state-reasons",
4981 * Copy or clear the printer-state-message value as needed...
4984 if (job
->state_value
!= IPP_JOB_PROCESSING
&&
4985 job
->status_level
== CUPSD_LOG_INFO
)
4987 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
), "");
4990 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
4992 else if (job
->printer
->state_message
[0] && do_message
)
4994 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
),
4995 job
->printer
->state_message
);
4998 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
5002 * ... and the printer-state-reasons value...
5005 if (job
->printer
->num_reasons
== 0)
5012 num_reasons
= job
->printer
->num_reasons
;
5013 reasons
= (const char * const *)job
->printer
->reasons
;
5016 if (!job
->printer_reasons
|| job
->printer_reasons
->num_values
!= num_reasons
)
5019 * Replace/create a job-printer-state-reasons attribute...
5022 ippDeleteAttribute(job
->attrs
, job
->printer_reasons
);
5024 job
->printer_reasons
= ippAddStrings(job
->attrs
,
5025 IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
5026 "job-printer-state-reasons",
5027 num_reasons
, NULL
, NULL
);
5032 * Don't bother clearing the reason strings if they are the same...
5035 for (i
= 0; i
< num_reasons
; i
++)
5036 if (strcmp(job
->printer_reasons
->values
[i
].string
.text
, reasons
[i
]))
5039 if (i
>= num_reasons
)
5043 * Not the same, so free the current strings...
5046 for (i
= 0; i
< num_reasons
; i
++)
5047 _cupsStrFree(job
->printer_reasons
->values
[i
].string
.text
);
5051 * Copy the reasons...
5054 for (i
= 0; i
< num_reasons
; i
++)
5055 job
->printer_reasons
->values
[i
].string
.text
= _cupsStrAlloc(reasons
[i
]);
5058 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
5063 * End of "$Id: job.c 7902 2008-09-03 14:20:17Z mike $".