2 * "$Id: job.c 7902 2008-09-03 14:20:17Z mike $"
4 * Job management routines for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2009 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 job.
24 * cupsdDeleteJob() - Free all memory used by a job.
25 * cupsdFreeAllJobs() - Free all jobs from memory.
26 * cupsdFindJob() - Find the specified job.
27 * cupsdGetPrinterJobCount() - Get the number of pending, processing, or held
28 * jobs in a printer or class.
29 * cupsdGetUserJobCount() - Get the number of pending, processing, or held
31 * cupsdLoadAllJobs() - Load all jobs from disk.
32 * cupsdLoadJob() - Load a single job.
33 * cupsdMoveJob() - Move the specified job to a different
35 * cupsdReleaseJob() - Release the specified job.
36 * cupsdRestartJob() - Restart the specified job.
37 * cupsdSaveAllJobs() - Save a summary of all jobs to disk.
38 * cupsdSaveJob() - Save a job to disk.
39 * cupsdSetJobHoldUntil() - Set the hold time for a job.
40 * cupsdSetJobPriority() - Set the priority of a job, moving it up/down
41 * in the list as needed.
42 * cupsdSetJobState() - Set the state of the specified print job.
43 * cupsdStopAllJobs() - Stop all print jobs.
44 * cupsdUnloadCompletedJobs() - Flush completed job history from memory.
45 * compare_active_jobs() - Compare the job IDs and priorities of two
47 * compare_jobs() - Compare the job IDs of two jobs.
48 * dump_job_history() - Dump any debug messages for a job.
49 * free_job_history() - Free any log history.
50 * finalize_job() - Cleanup after job filter processes and support
52 * get_options() - Get a string containing the job options.
53 * ipp_length() - Compute the size of the buffer needed to hold
54 * the textual IPP attributes.
55 * load_job_cache() - Load jobs from the job.cache file.
56 * load_next_job_id() - Load the NextJobId value from the job.cache
58 * load_request_root() - Load jobs from the RequestRoot directory.
59 * set_hold_until() - Set the hold time and update job-hold-until
61 * set_time() - Set one of the "time-at-xyz" attributes.
62 * start_job() - Start a print job.
63 * stop_job() - Stop a print job.
64 * unload_job() - Unload a job from memory.
65 * update_job() - Read a status update from a job's filters.
66 * update_job_attrs() - Update the job-printer-* attributes.
70 * Include necessary headers...
75 #include <cups/backend.h>
80 * Design Notes for Job Management
81 * -------------------------------
85 * pending Do nothing/check jobs
86 * pending-held Send SIGTERM to filters and backend
87 * processing Do nothing/start job
88 * stopped Send SIGKILL to filters and backend
89 * canceled Send SIGTERM to filters and backend
93 * Finalize clears the printer <-> job association, deletes the status
94 * buffer, closes all of the pipes, etc. and doesn't get run until all of
95 * the print processes are finished.
97 * UNLOADING OF JOBS (cupsdUnloadCompletedJobs)
99 * We unload the job attributes when they are not needed to reduce overall
100 * memory consumption. We don't unload jobs where job->state_value <
101 * IPP_JOB_STOPPED, job->printer != NULL, or job->access_time is recent.
103 * STARTING OF JOBS (start_job)
105 * When a job is started, a status buffer, several pipes, a security
106 * profile, and a backend process are created for the life of that job.
107 * These are shared for every file in a job. For remote print jobs, the
108 * IPP backend is provided with every file in the job and no filters are
111 * The job->printer member tracks which printer is printing a job, which
112 * can be different than the destination in job->dest for classes. The
113 * printer object also has a job pointer to track which job is being
116 * PRINTING OF JOB FILES (cupsdContinueJob)
118 * Each file in a job is filtered by 0 or more programs. After getting the
119 * list of filters needed and the total cost, the job is either passed or
120 * put back to the processing state until the current FilterLevel comes down
121 * enough to allow printing.
123 * If we can print, we build a string for the print options and run each of
124 * the filters, piping the output from one into the next.
126 * JOB STATUS UPDATES (update_job)
128 * The update_job function gets called whenever there are pending messages
129 * on the status pipe. These generally are updates to the marker-*,
130 * printer-state-message, or printer-state-reasons attributes. On EOF,
131 * finalize_job is called to clean up.
133 * FINALIZING JOBS (finalize_job)
135 * When all filters and the backend are done, we set the job state to
136 * completed (no errors), aborted (filter errors or abort-job policy),
137 * pending-held (auth required or retry-job policy), or pending
138 * (retry-current-job or stop-printer policies) as appropriate.
140 * Then we close the pipes and free the status buffers and profiles.
142 * JOB FILE COMPLETION (process_children in main.c)
144 * For multiple-file jobs, process_children (in main.c) sees that all
145 * filters have exited and calls in to print the next file if there are
146 * more files in the job, otherwise it waits for the backend to exit and
147 * update_job to do the cleanup.
155 static mime_filter_t gziptoany_filter
=
157 NULL
, /* Source type */
158 NULL
, /* Destination type */
160 "gziptoany" /* Filter program to run */
168 static int compare_active_jobs(void *first
, void *second
, void *data
);
169 static int compare_jobs(void *first
, void *second
, void *data
);
170 static void dump_job_history(cupsd_job_t
*job
);
171 static void finalize_job(cupsd_job_t
*job
, int set_job_state
);
172 static void free_job_history(cupsd_job_t
*job
);
173 static char *get_options(cupsd_job_t
*job
, int banner_page
, char *copies
,
174 size_t copies_size
, char *title
,
176 static int ipp_length(ipp_t
*ipp
);
177 static void load_job_cache(const char *filename
);
178 static void load_next_job_id(const char *filename
);
179 static void load_request_root(void);
180 static void set_hold_until(cupsd_job_t
*job
, time_t holdtime
);
181 static void set_time(cupsd_job_t
*job
, const char *name
);
182 static void start_job(cupsd_job_t
*job
, cupsd_printer_t
*printer
);
183 static void stop_job(cupsd_job_t
*job
, cupsd_jobaction_t action
);
184 static void unload_job(cupsd_job_t
*job
);
185 static void update_job(cupsd_job_t
*job
);
186 static void update_job_attrs(cupsd_job_t
*job
, int do_message
);
190 * 'cupsdAddJob()' - Add a new job to the job queue.
193 cupsd_job_t
* /* O - New job record */
194 cupsdAddJob(int priority
, /* I - Job priority */
195 const char *dest
) /* I - Job destination */
197 cupsd_job_t
*job
; /* New job record */
200 if ((job
= calloc(sizeof(cupsd_job_t
), 1)) == NULL
)
203 job
->id
= NextJobId
++;
204 job
->priority
= priority
;
205 job
->back_pipes
[0] = -1;
206 job
->back_pipes
[1] = -1;
207 job
->print_pipes
[0] = -1;
208 job
->print_pipes
[1] = -1;
209 job
->side_pipes
[0] = -1;
210 job
->side_pipes
[1] = -1;
211 job
->status_pipes
[0] = -1;
212 job
->status_pipes
[1] = -1;
214 cupsdSetString(&job
->dest
, dest
);
217 * Add the new job to the "all jobs" and "active jobs" lists...
220 cupsArrayAdd(Jobs
, job
);
221 cupsArrayAdd(ActiveJobs
, job
);
228 * 'cupsdCancelJobs()' - Cancel all jobs for the given destination/user.
232 cupsdCancelJobs(const char *dest
, /* I - Destination to cancel */
233 const char *username
, /* I - Username or NULL */
234 int purge
) /* I - Purge jobs? */
236 cupsd_job_t
*job
; /* Current job */
239 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
241 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
243 if ((!job
->dest
|| !job
->username
) && !cupsdLoadJob(job
))
246 if ((!dest
|| !strcmp(job
->dest
, dest
)) &&
247 (!username
|| !strcmp(job
->username
, username
)))
250 * Cancel all jobs matching this destination/user...
254 cupsdSetJobState(job
, IPP_JOB_CANCELED
, CUPSD_JOB_PURGE
,
255 "Job purged by user.");
256 else if (job
->state_value
< IPP_JOB_CANCELED
)
257 cupsdSetJobState(job
, IPP_JOB_CANCELED
, CUPSD_JOB_DEFAULT
,
258 "Job canceled by user.");
267 * 'cupsdCheckJobs()' - Check the pending jobs and start any if the destination
274 cupsd_job_t
*job
; /* Current job in queue */
275 cupsd_printer_t
*printer
, /* Printer destination */
276 *pclass
; /* Printer class destination */
277 ipp_attribute_t
*attr
; /* Job attribute */
278 time_t curtime
; /* Current time */
281 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
282 "cupsdCheckJobs: %d active jobs, sleeping=%d, reload=%d",
283 cupsArrayCount(ActiveJobs
), Sleeping
, NeedReload
);
285 curtime
= time(NULL
);
287 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
289 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
292 * Kill jobs if they are unresponsive...
295 if (job
->kill_time
&& job
->kill_time
<= curtime
)
297 stop_job(job
, CUPSD_JOB_FORCE
);
302 * Start held jobs if they are ready...
305 if (job
->state_value
== IPP_JOB_HELD
&&
307 job
->hold_until
< curtime
)
309 if (job
->pending_timeout
)
312 * This job is pending; check that we don't have an active Send-Document
313 * operation in progress on any of the client connections, then timeout
314 * the job so we can start printing...
317 cupsd_client_t
*con
; /* Current client connection */
320 for (con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
322 con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
324 con
->request
->request
.op
.operation_id
== IPP_SEND_DOCUMENT
)
330 if (cupsdTimeoutJob(job
))
334 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
335 "Job submission timed out.");
339 * Continue jobs that are waiting on the FilterLimit...
342 if (job
->pending_cost
> 0 &&
343 ((FilterLevel
+ job
->pending_cost
) < FilterLimit
|| FilterLevel
== 0))
344 cupsdContinueJob(job
);
347 * Start pending jobs if the destination is available...
350 if (job
->state_value
== IPP_JOB_PENDING
&& !NeedReload
&& !Sleeping
&&
353 printer
= cupsdFindDest(job
->dest
);
357 (printer
->type
& (CUPS_PRINTER_IMPLICIT
| CUPS_PRINTER_CLASS
)))
360 * If the class is remote, just pass it to the remote server...
365 if (pclass
->state
== IPP_PRINTER_STOPPED
)
367 else if (pclass
->type
& CUPS_PRINTER_REMOTE
)
370 printer
= cupsdFindAvailablePrinter(printer
->name
);
373 if (!printer
&& !pclass
)
376 * Whoa, the printer and/or class for this destination went away;
380 cupsdSetJobState(job
, IPP_JOB_ABORTED
, CUPSD_JOB_PURGE
,
381 "Job aborted because the destination printer/class "
384 else if (printer
&& !printer
->holding_new_jobs
)
387 * See if the printer is available or remote and not printing a job;
388 * if so, start the job...
394 * Add/update a job-actual-printer-uri attribute for this job
395 * so that we know which printer actually printed the job...
398 if ((attr
= ippFindAttribute(job
->attrs
, "job-actual-printer-uri",
399 IPP_TAG_URI
)) != NULL
)
400 cupsdSetString(&attr
->values
[0].string
.text
, printer
->uri
);
402 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
,
403 "job-actual-printer-uri", NULL
, printer
->uri
);
406 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
409 if ((!(printer
->type
& CUPS_PRINTER_DISCOVERED
) && /* Printer is local */
410 printer
->state
== IPP_PRINTER_IDLE
) || /* and idle, OR */
411 ((printer
->type
& CUPS_PRINTER_DISCOVERED
) && /* Printer is remote */
412 !printer
->job
)) /* and not printing */
418 start_job(job
, printer
);
427 * 'cupsdCleanJobs()' - Clean out old jobs.
433 cupsd_job_t
*job
; /* Current job */
436 if (MaxJobs
<= 0 && JobHistory
)
439 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
440 job
&& (cupsArrayCount(Jobs
) >= MaxJobs
|| !JobHistory
);
441 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
442 if (job
->state_value
>= IPP_JOB_CANCELED
&& !job
->printer
)
443 cupsdDeleteJob(job
, CUPSD_JOB_PURGE
);
448 * 'cupsdContinueJob()' - Continue printing with the next file in a job.
452 cupsdContinueJob(cupsd_job_t
*job
) /* I - Job */
454 int i
; /* Looping var */
455 int slot
; /* Pipe slot */
456 cups_array_t
*filters
= NULL
,/* Filters for job */
457 *prefilters
; /* Filters with prefilters */
458 mime_filter_t
*filter
, /* Current filter */
459 *prefilter
, /* Prefilter */
460 port_monitor
; /* Port monitor filter */
461 char scheme
[255]; /* Device URI scheme */
462 ipp_attribute_t
*attr
; /* Current attribute */
463 const char *ptr
, /* Pointer into value */
464 *abort_message
; /* Abort message */
465 ipp_jstate_t abort_state
= IPP_JOB_STOPPED
;
466 /* New job state on abort */
467 struct stat backinfo
; /* Backend file information */
468 int backroot
; /* Run backend as root? */
469 int pid
; /* Process ID of new filter process */
470 int banner_page
; /* 1 if banner page, 0 otherwise */
471 int filterfds
[2][2] = { { -1, -1 }, { -1, -1 } };
472 /* Pipes used between filters */
473 int envc
; /* Number of environment variables */
474 char **argv
= NULL
, /* Filter command-line arguments */
475 filename
[1024], /* Job filename */
476 command
[1024], /* Full path to command */
477 jobid
[255], /* Job ID string */
479 /* Job title string */
480 copies
[255], /* # copies string */
481 *options
, /* Options string */
483 /* Environment variables */
484 charset
[255], /* CHARSET env variable */
485 class_name
[255],/* CLASS env variable */
486 classification
[1024],
487 /* CLASSIFICATION env variable */
489 /* CONTENT_TYPE env variable */
491 /* DEVICE_URI env variable */
492 final_content_type
[1024],
493 /* FINAL_CONTENT_TYPE env variable */
494 lang
[255], /* LANG env variable */
497 /* APPLE_LANGUAGE env variable */
498 #endif /* __APPLE__ */
499 ppd
[1024], /* PPD env variable */
501 /* PRINTER_INFO env variable */
502 printer_location
[255],
503 /* PRINTER_LOCATION env variable */
505 /* PRINTER env variable */
507 /* RIP_MAX_CACHE env variable */
510 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
511 "cupsdContinueJob(job=%p(%d)): current_file=%d, num_files=%d",
512 job
, job
->id
, job
->current_file
, job
->num_files
);
515 * Figure out what filters are required to convert from
516 * the source to the destination type...
519 FilterLevel
-= job
->cost
;
522 job
->pending_cost
= 0;
524 memset(job
->filters
, 0, sizeof(job
->filters
));
527 if (job
->printer
->raw
)
530 * Remote jobs and raw queues go directly to the printer without
534 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Sending job to queue tagged as raw...");
539 * Local jobs get filtered...
542 filters
= mimeFilter(MimeDatabase
, job
->filetypes
[job
->current_file
],
543 job
->printer
->filetype
, &(job
->cost
));
547 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
548 "Unable to convert file %d to printable format!",
551 abort_message
= "Aborting job because it cannot be printed.";
552 abort_state
= IPP_JOB_ABORTED
;
558 * Remove NULL ("-") filters...
561 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
563 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
564 if (!strcmp(filter
->filter
, "-"))
565 cupsArrayRemove(filters
, filter
);
567 if (cupsArrayCount(filters
) == 0)
569 cupsArrayDelete(filters
);
574 * If this printer has any pre-filters, insert the required pre-filter
575 * in the filters array...
578 if (job
->printer
->prefiltertype
&& filters
)
580 prefilters
= cupsArrayNew(NULL
, NULL
);
582 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
584 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
586 if ((prefilter
= mimeFilterLookup(MimeDatabase
, filter
->src
,
587 job
->printer
->prefiltertype
)))
589 cupsArrayAdd(prefilters
, prefilter
);
590 job
->cost
+= prefilter
->cost
;
593 cupsArrayAdd(prefilters
, filter
);
596 cupsArrayDelete(filters
);
597 filters
= prefilters
;
602 * Set a minimum cost of 100 for all jobs so that FilterLimit
603 * works with raw queues and other low-cost paths.
610 * See if the filter cost is too high...
613 if ((FilterLevel
+ job
->cost
) > FilterLimit
&& FilterLevel
> 0 &&
617 * Don't print this job quite yet...
620 cupsArrayDelete(filters
);
622 cupsdLogJob(job
, CUPSD_LOG_INFO
,
623 "Holding because filter limit has been reached.");
624 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
625 "cupsdContinueJob: file=%d, cost=%d, level=%d, limit=%d",
626 job
->current_file
, job
->cost
, FilterLevel
,
629 job
->pending_cost
= job
->cost
;
634 FilterLevel
+= job
->cost
;
637 * Add decompression/raw filter as needed...
640 if ((!job
->printer
->raw
&& job
->compressions
[job
->current_file
]) ||
641 (!filters
&& !job
->printer
->remote
&&
642 (job
->num_files
> 1 || !strncmp(job
->printer
->device_uri
, "file:", 5))))
645 * Add gziptoany filter to the front of the list...
649 filters
= cupsArrayNew(NULL
, NULL
);
651 if (!cupsArrayInsert(filters
, &gziptoany_filter
))
653 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
654 "Unable to add decompression filter - %s", strerror(errno
));
656 cupsArrayDelete(filters
);
658 abort_message
= "Stopping job because the scheduler ran out of memory.";
665 * Add port monitor, if any...
668 if (job
->printer
->port_monitor
)
671 * Add port monitor to the end of the list...
675 filters
= cupsArrayNew(NULL
, NULL
);
677 port_monitor
.src
= NULL
;
678 port_monitor
.dst
= NULL
;
679 port_monitor
.cost
= 0;
681 snprintf(port_monitor
.filter
, sizeof(port_monitor
.filter
),
682 "%s/monitor/%s", ServerBin
, job
->printer
->port_monitor
);
684 if (!cupsArrayAdd(filters
, &port_monitor
))
686 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
687 "Unable to add port monitor - %s", strerror(errno
));
689 abort_message
= "Stopping job because the scheduler ran out of memory.";
696 * Make sure we don't go over the "MAX_FILTERS" limit...
699 if (cupsArrayCount(filters
) > MAX_FILTERS
)
701 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
702 "Too many filters (%d > %d), unable to print!",
703 cupsArrayCount(filters
), MAX_FILTERS
);
705 abort_message
= "Aborting job because it needs too many filters to print.";
706 abort_state
= IPP_JOB_ABORTED
;
712 * Determine if we are printing a banner page or not...
715 if (job
->job_sheets
== NULL
)
717 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "No job-sheets attribute.");
718 if ((job
->job_sheets
=
719 ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_ZERO
)) != NULL
)
720 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
721 "... but someone added one without setting job_sheets!");
723 else if (job
->job_sheets
->num_values
== 1)
724 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "job-sheets=%s",
725 job
->job_sheets
->values
[0].string
.text
);
727 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "job-sheets=%s,%s",
728 job
->job_sheets
->values
[0].string
.text
,
729 job
->job_sheets
->values
[1].string
.text
);
731 if (job
->printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
))
733 else if (job
->job_sheets
== NULL
)
735 else if (strcasecmp(job
->job_sheets
->values
[0].string
.text
, "none") != 0 &&
736 job
->current_file
== 0)
738 else if (job
->job_sheets
->num_values
> 1 &&
739 strcasecmp(job
->job_sheets
->values
[1].string
.text
, "none") != 0 &&
740 job
->current_file
== (job
->num_files
- 1))
745 if ((options
= get_options(job
, banner_page
, copies
, sizeof(copies
), title
,
746 sizeof(title
))) == NULL
)
748 abort_message
= "Stopping job because the scheduler ran out of memory.";
754 * Build the command-line arguments for the filters. Each filter
755 * has 6 or 7 arguments:
763 * argv[6] = filename (optional; normally stdin)
765 * This allows legacy printer drivers that use the old System V
766 * printing interface to be used by CUPS.
768 * For remote jobs, we send all of the files in the argument list.
771 if (job
->printer
->remote
)
772 argv
= calloc(7 + job
->num_files
, sizeof(char *));
774 argv
= calloc(8, sizeof(char *));
778 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Unable to allocate argument array - %s",
781 abort_message
= "Stopping job because the scheduler ran out of memory.";
786 sprintf(jobid
, "%d", job
->id
);
788 argv
[0] = job
->printer
->name
;
790 argv
[2] = job
->username
;
795 if (job
->printer
->remote
&& job
->num_files
> 1)
797 for (i
= 0; i
< job
->num_files
; i
++)
799 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
801 argv
[6 + i
] = strdup(filename
);
806 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
807 job
->id
, job
->current_file
+ 1);
811 for (i
= 0; argv
[i
]; i
++)
812 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "argv[%d]=\"%s\"", i
, argv
[i
]);
815 * Create environment variable strings for the filters...
818 attr
= ippFindAttribute(job
->attrs
, "attributes-natural-language",
822 strcpy(apple_language
, "APPLE_LANGUAGE=");
823 _cupsAppleLanguage(attr
->values
[0].string
.text
,
824 apple_language
+ 15, sizeof(apple_language
) - 15);
825 #endif /* __APPLE__ */
827 switch (strlen(attr
->values
[0].string
.text
))
831 * This is an unknown or badly formatted language code; use
832 * the POSIX locale...
835 strcpy(lang
, "LANG=C");
840 * Just the language code (ll)...
843 snprintf(lang
, sizeof(lang
), "LANG=%s.UTF-8",
844 attr
->values
[0].string
.text
);
849 * Language and country code (ll-cc)...
852 snprintf(lang
, sizeof(lang
), "LANG=%c%c_%c%c.UTF-8",
853 attr
->values
[0].string
.text
[0],
854 attr
->values
[0].string
.text
[1],
855 toupper(attr
->values
[0].string
.text
[3] & 255),
856 toupper(attr
->values
[0].string
.text
[4] & 255));
860 if ((attr
= ippFindAttribute(job
->attrs
, "document-format",
861 IPP_TAG_MIMETYPE
)) != NULL
&&
862 (ptr
= strstr(attr
->values
[0].string
.text
, "charset=")) != NULL
)
863 snprintf(charset
, sizeof(charset
), "CHARSET=%s", ptr
+ 8);
865 strlcpy(charset
, "CHARSET=utf-8", sizeof(charset
));
867 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s/%s",
868 job
->filetypes
[job
->current_file
]->super
,
869 job
->filetypes
[job
->current_file
]->type
);
870 snprintf(device_uri
, sizeof(device_uri
), "DEVICE_URI=%s",
871 job
->printer
->device_uri
);
872 snprintf(ppd
, sizeof(ppd
), "PPD=%s/ppd/%s.ppd", ServerRoot
,
874 snprintf(printer_info
, sizeof(printer_name
), "PRINTER_INFO=%s",
875 job
->printer
->info
? job
->printer
->info
: "");
876 snprintf(printer_location
, sizeof(printer_name
), "PRINTER_LOCATION=%s",
877 job
->printer
->location
? job
->printer
->location
: "");
878 snprintf(printer_name
, sizeof(printer_name
), "PRINTER=%s", job
->printer
->name
);
879 snprintf(rip_max_cache
, sizeof(rip_max_cache
), "RIP_MAX_CACHE=%s", RIPCache
);
881 envc
= cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
883 envp
[envc
++] = charset
;
884 envp
[envc
++] = lang
;
886 envp
[envc
++] = apple_language
;
887 #endif /* __APPLE__ */
889 envp
[envc
++] = rip_max_cache
;
890 envp
[envc
++] = content_type
;
891 envp
[envc
++] = device_uri
;
892 envp
[envc
++] = printer_info
;
893 envp
[envc
++] = printer_location
;
894 envp
[envc
++] = printer_name
;
895 envp
[envc
++] = banner_page
? "CUPS_FILETYPE=job-sheet" :
896 "CUPS_FILETYPE=document";
898 if (!job
->printer
->remote
&& !job
->printer
->raw
)
900 filter
= (mime_filter_t
*)cupsArrayLast(filters
);
902 if (job
->printer
->port_monitor
)
903 filter
= (mime_filter_t
*)cupsArrayPrev(filters
);
905 if (filter
&& filter
->dst
)
907 snprintf(final_content_type
, sizeof(final_content_type
),
908 "FINAL_CONTENT_TYPE=%s/%s",
909 filter
->dst
->super
, filter
->dst
->type
);
910 envp
[envc
++] = final_content_type
;
914 if (Classification
&& !banner_page
)
916 if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
917 IPP_TAG_NAME
)) == NULL
)
918 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
920 else if (attr
->num_values
> 1 &&
921 strcmp(attr
->values
[1].string
.text
, "none") != 0)
922 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
923 attr
->values
[1].string
.text
);
925 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
926 attr
->values
[0].string
.text
);
928 envp
[envc
++] = classification
;
931 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
933 snprintf(class_name
, sizeof(class_name
), "CLASS=%s", job
->dest
);
934 envp
[envc
++] = class_name
;
937 if (job
->auth_username
)
938 envp
[envc
++] = job
->auth_username
;
939 if (job
->auth_domain
)
940 envp
[envc
++] = job
->auth_domain
;
941 if (job
->auth_password
)
942 envp
[envc
++] = job
->auth_password
;
946 envp
[envc
++] = job
->ccname
;
947 #endif /* HAVE_GSSAPI */
951 for (i
= 0; i
< envc
; i
++)
952 if (!strncmp(envp
[i
], "AUTH_", 5))
953 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"AUTH_%c****\"", i
,
955 else if (strncmp(envp
[i
], "DEVICE_URI=", 11))
956 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"%s\"", i
, envp
[i
]);
958 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"DEVICE_URI=%s\"", i
,
959 job
->printer
->sanitized_device_uri
);
961 if (job
->printer
->remote
)
962 job
->current_file
= job
->num_files
;
964 job
->current_file
++;
967 * Now create processes for all of the filters...
970 for (i
= 0, slot
= 0, filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
972 i
++, filter
= (mime_filter_t
*)cupsArrayNext(filters
))
974 if (filter
->filter
[0] != '/')
975 snprintf(command
, sizeof(command
), "%s/filter/%s", ServerBin
,
978 strlcpy(command
, filter
->filter
, sizeof(command
));
980 if (i
< (cupsArrayCount(filters
) - 1))
982 if (cupsdOpenPipe(filterfds
[slot
]))
984 abort_message
= "Stopping job because the scheduler could not create "
992 if (job
->current_file
== 1)
994 if (strncmp(job
->printer
->device_uri
, "file:", 5) != 0)
996 if (cupsdOpenPipe(job
->print_pipes
))
998 abort_message
= "Stopping job because the scheduler could not "
999 "create the backend pipes.";
1006 job
->print_pipes
[0] = -1;
1007 if (!strcmp(job
->printer
->device_uri
, "file:/dev/null") ||
1008 !strcmp(job
->printer
->device_uri
, "file:///dev/null"))
1009 job
->print_pipes
[1] = -1;
1012 if (!strncmp(job
->printer
->device_uri
, "file:/dev/", 10))
1013 job
->print_pipes
[1] = open(job
->printer
->device_uri
+ 5,
1015 else if (!strncmp(job
->printer
->device_uri
, "file:///dev/", 12))
1016 job
->print_pipes
[1] = open(job
->printer
->device_uri
+ 7,
1018 else if (!strncmp(job
->printer
->device_uri
, "file:///", 8))
1019 job
->print_pipes
[1] = open(job
->printer
->device_uri
+ 7,
1020 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
1022 job
->print_pipes
[1] = open(job
->printer
->device_uri
+ 5,
1023 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
1025 if (job
->print_pipes
[1] < 0)
1027 abort_message
= "Stopping job because the scheduler could not "
1028 "open the output file.";
1033 fcntl(job
->print_pipes
[1], F_SETFD
,
1034 fcntl(job
->print_pipes
[1], F_GETFD
) | FD_CLOEXEC
);
1039 filterfds
[slot
][0] = job
->print_pipes
[0];
1040 filterfds
[slot
][1] = job
->print_pipes
[1];
1043 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
1044 filterfds
[slot
][1], job
->status_pipes
[1],
1045 job
->back_pipes
[0], job
->side_pipes
[0], 0,
1046 job
->profile
, job
, job
->filters
+ i
);
1048 cupsdClosePipe(filterfds
[!slot
]);
1052 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Unable to start filter \"%s\" - %s.",
1053 filter
->filter
, strerror(errno
));
1055 abort_message
= "Stopping job because the scheduler could not execute a "
1061 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Started filter %s (PID %d)", command
,
1068 cupsArrayDelete(filters
);
1072 * Finally, pipe the final output into a backend process if needed...
1075 if (strncmp(job
->printer
->device_uri
, "file:", 5) != 0)
1077 if (job
->current_file
== 1 || job
->printer
->remote
)
1079 sscanf(job
->printer
->device_uri
, "%254[^:]", scheme
);
1080 snprintf(command
, sizeof(command
), "%s/backend/%s", ServerBin
, scheme
);
1083 * See if the backend needs to run as root...
1088 else if (stat(command
, &backinfo
))
1091 backroot
= !(backinfo
.st_mode
& (S_IRWXG
| S_IRWXO
));
1093 argv
[0] = job
->printer
->sanitized_device_uri
;
1095 filterfds
[slot
][0] = -1;
1096 filterfds
[slot
][1] = -1;
1098 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
1099 filterfds
[slot
][1], job
->status_pipes
[1],
1100 job
->back_pipes
[1], job
->side_pipes
[1],
1101 backroot
, job
->profile
, job
, &(job
->backend
));
1105 abort_message
= "Stopping job because the sheduler could not execute "
1112 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Started backend %s (PID %d)",
1117 if (job
->current_file
== job
->num_files
)
1119 cupsdClosePipe(job
->print_pipes
);
1120 cupsdClosePipe(job
->back_pipes
);
1121 cupsdClosePipe(job
->side_pipes
);
1123 close(job
->status_pipes
[1]);
1124 job
->status_pipes
[1] = -1;
1129 filterfds
[slot
][0] = -1;
1130 filterfds
[slot
][1] = -1;
1132 if (job
->current_file
== job
->num_files
)
1134 cupsdClosePipe(job
->print_pipes
);
1136 close(job
->status_pipes
[1]);
1137 job
->status_pipes
[1] = -1;
1141 cupsdClosePipe(filterfds
[slot
]);
1143 if (job
->printer
->remote
&& job
->num_files
> 1)
1145 for (i
= 0; i
< job
->num_files
; i
++)
1151 cupsdAddSelect(job
->status_buffer
->fd
, (cupsd_selfunc_t
)update_job
, NULL
,
1154 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
, "Job #%d started.",
1158 * If we get here than we are able to run the printer driver filters, so clear
1159 * the missing and insecure warnings...
1162 if (cupsdSetPrinterReasons(job
->printer
, "-cups-missing-filter-warning,"
1163 "cups-insecure-filter-warning"))
1164 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, job
->printer
, NULL
,
1165 "Printer drivers now functional.");
1171 * If we get here, we need to abort the current job and close out all
1172 * files and pipes...
1177 FilterLevel
-= job
->cost
;
1180 for (slot
= 0; slot
< 2; slot
++)
1181 cupsdClosePipe(filterfds
[slot
]);
1183 cupsArrayDelete(filters
);
1187 if (job
->printer
->remote
&& job
->num_files
> 1)
1189 for (i
= 0; i
< job
->num_files
; i
++)
1196 cupsdClosePipe(job
->print_pipes
);
1197 cupsdClosePipe(job
->back_pipes
);
1198 cupsdClosePipe(job
->side_pipes
);
1200 cupsdRemoveSelect(job
->status_pipes
[0]);
1201 cupsdClosePipe(job
->status_pipes
);
1202 cupsdStatBufDelete(job
->status_buffer
);
1203 job
->status_buffer
= NULL
;
1206 * Update the printer and job state.
1209 cupsdSetJobState(job
, abort_state
, CUPSD_JOB_DEFAULT
, "%s", abort_message
);
1210 cupsdSetPrinterState(job
->printer
, IPP_PRINTER_IDLE
, 0);
1211 update_job_attrs(job
, 0);
1214 free_job_history(job
);
1216 cupsArrayRemove(PrintingJobs
, job
);
1219 * Clear the printer <-> job association...
1222 job
->printer
->job
= NULL
;
1223 job
->printer
= NULL
;
1228 * 'cupsdDeleteJob()' - Free all memory used by a job.
1232 cupsdDeleteJob(cupsd_job_t
*job
, /* I - Job */
1233 cupsd_jobaction_t action
)/* I - Action */
1236 finalize_job(job
, 1);
1238 if (action
== CUPSD_JOB_PURGE
)
1241 * Remove the job info file...
1244 char filename
[1024]; /* Job filename */
1246 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
,
1251 cupsdClearString(&job
->username
);
1252 cupsdClearString(&job
->dest
);
1253 cupsdClearString(&job
->auth_username
);
1254 cupsdClearString(&job
->auth_domain
);
1255 cupsdClearString(&job
->auth_password
);
1259 * Destroy the credential cache and clear the KRB5CCNAME env var string.
1264 krb5_cc_destroy(KerberosContext
, job
->ccache
);
1268 cupsdClearString(&job
->ccname
);
1269 #endif /* HAVE_GSSAPI */
1271 if (job
->num_files
> 0)
1273 free(job
->compressions
);
1274 free(job
->filetypes
);
1280 free_job_history(job
);
1284 cupsArrayRemove(Jobs
, job
);
1285 cupsArrayRemove(ActiveJobs
, job
);
1286 cupsArrayRemove(PrintingJobs
, job
);
1293 * 'cupsdFreeAllJobs()' - Free all jobs from memory.
1297 cupsdFreeAllJobs(void)
1299 cupsd_job_t
*job
; /* Current job */
1307 cupsdStopAllJobs(CUPSD_JOB_FORCE
, 0);
1310 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1312 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1313 cupsdDeleteJob(job
, CUPSD_JOB_DEFAULT
);
1315 cupsdReleaseSignals();
1320 * 'cupsdFindJob()' - Find the specified job.
1323 cupsd_job_t
* /* O - Job data */
1324 cupsdFindJob(int id
) /* I - Job ID */
1326 cupsd_job_t key
; /* Search key */
1331 return ((cupsd_job_t
*)cupsArrayFind(Jobs
, &key
));
1336 * 'cupsdGetPrinterJobCount()' - Get the number of pending, processing,
1337 * or held jobs in a printer or class.
1340 int /* O - Job count */
1341 cupsdGetPrinterJobCount(
1342 const char *dest
) /* I - Printer or class name */
1344 int count
; /* Job count */
1345 cupsd_job_t
*job
; /* Current job */
1348 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
1350 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
1351 if (job
->dest
&& !strcasecmp(job
->dest
, dest
))
1359 * 'cupsdGetUserJobCount()' - Get the number of pending, processing,
1360 * or held jobs for a user.
1363 int /* O - Job count */
1364 cupsdGetUserJobCount(
1365 const char *username
) /* I - Username */
1367 int count
; /* Job count */
1368 cupsd_job_t
*job
; /* Current job */
1371 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
1373 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
1374 if (!strcasecmp(job
->username
, username
))
1382 * 'cupsdLoadAllJobs()' - Load all jobs from disk.
1386 cupsdLoadAllJobs(void)
1388 char filename
[1024]; /* Full filename of job.cache file */
1389 struct stat fileinfo
, /* Information on job.cache file */
1390 dirinfo
; /* Information on RequestRoot dir */
1395 * Create the job arrays as needed...
1399 Jobs
= cupsArrayNew(compare_jobs
, NULL
);
1402 ActiveJobs
= cupsArrayNew(compare_active_jobs
, NULL
);
1405 PrintingJobs
= cupsArrayNew(compare_jobs
, NULL
);
1408 * See whether the job.cache file is older than the RequestRoot directory...
1411 snprintf(filename
, sizeof(filename
), "%s/job.cache", CacheDir
);
1413 if (stat(filename
, &fileinfo
))
1415 fileinfo
.st_mtime
= 0;
1417 if (errno
!= ENOENT
)
1418 cupsdLogMessage(CUPSD_LOG_ERROR
,
1419 "Unable to get file information for \"%s\" - %s",
1420 filename
, strerror(errno
));
1423 if (stat(RequestRoot
, &dirinfo
))
1425 dirinfo
.st_mtime
= 0;
1427 if (errno
!= ENOENT
)
1428 cupsdLogMessage(CUPSD_LOG_ERROR
,
1429 "Unable to get directory information for \"%s\" - %s",
1430 RequestRoot
, strerror(errno
));
1434 * Load the most recent source for job data...
1437 if (dirinfo
.st_mtime
> fileinfo
.st_mtime
)
1439 load_request_root();
1441 load_next_job_id(filename
);
1444 load_job_cache(filename
);
1447 * Clean out old jobs as needed...
1450 if (MaxJobs
> 0 && cupsArrayCount(Jobs
) >= MaxJobs
)
1456 * 'cupsdLoadJob()' - Load a single job.
1459 int /* O - 1 on success, 0 on failure */
1460 cupsdLoadJob(cupsd_job_t
*job
) /* I - Job */
1462 char jobfile
[1024]; /* Job filename */
1463 cups_file_t
*fp
; /* Job file */
1464 int fileid
; /* Current file ID */
1465 ipp_attribute_t
*attr
; /* Job attribute */
1466 const char *dest
; /* Destination name */
1467 cupsd_printer_t
*destptr
; /* Pointer to destination */
1468 mime_type_t
**filetypes
; /* New filetypes array */
1469 int *compressions
; /* New compressions array */
1474 if (job
->state_value
> IPP_JOB_STOPPED
)
1475 job
->access_time
= time(NULL
);
1480 if ((job
->attrs
= ippNew()) == NULL
)
1482 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Ran out of memory for job attributes!");
1487 * Load job attributes...
1490 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Loading attributes...", job
->id
);
1492 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, job
->id
);
1493 if ((fp
= cupsFileOpen(jobfile
, "r")) == NULL
)
1495 cupsdLogMessage(CUPSD_LOG_ERROR
,
1496 "[Job %d] Unable to open job control file \"%s\" - %s!",
1497 job
->id
, jobfile
, strerror(errno
));
1501 if (ippReadIO(fp
, (ipp_iocb_t
)cupsFileRead
, 1, NULL
, job
->attrs
) != IPP_DATA
)
1503 cupsdLogMessage(CUPSD_LOG_ERROR
,
1504 "[Job %d] Unable to read job control file \"%s\"!", job
->id
,
1513 * Copy attribute data to the job object...
1516 if (!ippFindAttribute(job
->attrs
, "time-at-creation", IPP_TAG_INTEGER
))
1518 cupsdLogMessage(CUPSD_LOG_ERROR
,
1519 "[Job %d] Missing or bad time-at-creation attribute in "
1520 "control file!", job
->id
);
1524 if ((job
->state
= ippFindAttribute(job
->attrs
, "job-state",
1525 IPP_TAG_ENUM
)) == NULL
)
1527 cupsdLogMessage(CUPSD_LOG_ERROR
,
1528 "[Job %d] Missing or bad job-state attribute in control "
1533 job
->state_value
= (ipp_jstate_t
)job
->state
->values
[0].integer
;
1537 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1538 IPP_TAG_URI
)) == NULL
)
1540 cupsdLogMessage(CUPSD_LOG_ERROR
,
1541 "[Job %d] No job-printer-uri attribute in control file!",
1546 if ((dest
= cupsdValidateDest(attr
->values
[0].string
.text
, &(job
->dtype
),
1549 cupsdLogMessage(CUPSD_LOG_ERROR
,
1550 "[Job %d] Unable to queue job for destination \"%s\"!",
1551 job
->id
, attr
->values
[0].string
.text
);
1555 cupsdSetString(&job
->dest
, dest
);
1557 else if ((destptr
= cupsdFindDest(job
->dest
)) == NULL
)
1559 cupsdLogMessage(CUPSD_LOG_ERROR
,
1560 "[Job %d] Unable to queue job for destination \"%s\"!",
1561 job
->id
, job
->dest
);
1565 job
->sheets
= ippFindAttribute(job
->attrs
, "job-media-sheets-completed",
1567 job
->job_sheets
= ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_NAME
);
1571 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
1572 IPP_TAG_INTEGER
)) == NULL
)
1574 cupsdLogMessage(CUPSD_LOG_ERROR
,
1575 "[Job %d] Missing or bad job-priority attribute in "
1576 "control file!", job
->id
);
1580 job
->priority
= attr
->values
[0].integer
;
1585 if ((attr
= ippFindAttribute(job
->attrs
, "job-originating-user-name",
1586 IPP_TAG_NAME
)) == NULL
)
1588 cupsdLogMessage(CUPSD_LOG_ERROR
,
1589 "[Job %d] Missing or bad job-originating-user-name "
1590 "attribute in control file!", job
->id
);
1594 cupsdSetString(&job
->username
, attr
->values
[0].string
.text
);
1598 * Set the job hold-until time and state...
1601 if (job
->state_value
== IPP_JOB_HELD
)
1603 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1604 IPP_TAG_KEYWORD
)) == NULL
)
1605 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1608 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
, CUPSD_JOB_DEFAULT
);
1611 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1612 job
->state_value
= IPP_JOB_PENDING
;
1615 else if (job
->state_value
== IPP_JOB_PROCESSING
)
1617 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1618 job
->state_value
= IPP_JOB_PENDING
;
1621 if (!job
->num_files
)
1624 * Find all the d##### files...
1627 for (fileid
= 1; fileid
< 10000; fileid
++)
1629 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
1632 if (access(jobfile
, 0))
1635 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1636 "[Job %d] Auto-typing document file \"%s\"...", job
->id
,
1639 if (fileid
> job
->num_files
)
1641 if (job
->num_files
== 0)
1643 compressions
= (int *)calloc(fileid
, sizeof(int));
1644 filetypes
= (mime_type_t
**)calloc(fileid
, sizeof(mime_type_t
*));
1648 compressions
= (int *)realloc(job
->compressions
,
1649 sizeof(int) * fileid
);
1650 filetypes
= (mime_type_t
**)realloc(job
->filetypes
,
1651 sizeof(mime_type_t
*) *
1655 if (!compressions
|| !filetypes
)
1657 cupsdLogMessage(CUPSD_LOG_ERROR
,
1658 "[Job %d] Ran out of memory for job file types!",
1661 ippDelete(job
->attrs
);
1670 if (job
->compressions
)
1672 free(job
->compressions
);
1673 job
->compressions
= NULL
;
1678 free(job
->filetypes
);
1679 job
->filetypes
= NULL
;
1686 job
->compressions
= compressions
;
1687 job
->filetypes
= filetypes
;
1688 job
->num_files
= fileid
;
1691 job
->filetypes
[fileid
- 1] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
1692 job
->compressions
+ fileid
- 1);
1694 if (!job
->filetypes
[fileid
- 1])
1695 job
->filetypes
[fileid
- 1] = mimeType(MimeDatabase
, "application",
1701 * Load authentication information as needed...
1704 if (job
->state_value
< IPP_JOB_STOPPED
)
1706 snprintf(jobfile
, sizeof(jobfile
), "%s/a%05d", RequestRoot
, job
->id
);
1708 cupsdClearString(&job
->auth_username
);
1709 cupsdClearString(&job
->auth_domain
);
1710 cupsdClearString(&job
->auth_password
);
1712 if ((fp
= cupsFileOpen(jobfile
, "r")) != NULL
)
1714 int i
, /* Looping var */
1715 bytes
; /* Size of auth data */
1716 char line
[255], /* Line from file */
1717 data
[255]; /* Decoded data */
1721 i
< destptr
->num_auth_info_required
&&
1722 cupsFileGets(fp
, line
, sizeof(line
));
1725 bytes
= sizeof(data
);
1726 httpDecode64_2(data
, &bytes
, line
);
1728 if (!strcmp(destptr
->auth_info_required
[i
], "username"))
1729 cupsdSetStringf(&job
->auth_username
, "AUTH_USERNAME=%s", data
);
1730 else if (!strcmp(destptr
->auth_info_required
[i
], "domain"))
1731 cupsdSetStringf(&job
->auth_domain
, "AUTH_DOMAIN=%s", data
);
1732 else if (!strcmp(destptr
->auth_info_required
[i
], "password"))
1733 cupsdSetStringf(&job
->auth_password
, "AUTH_PASSWORD=%s", data
);
1740 job
->access_time
= time(NULL
);
1744 * If we get here then something bad happened...
1749 ippDelete(job
->attrs
);
1752 if (job
->compressions
)
1754 free(job
->compressions
);
1755 job
->compressions
= NULL
;
1760 free(job
->filetypes
);
1761 job
->filetypes
= NULL
;
1773 * 'cupsdMoveJob()' - Move the specified job to a different destination.
1777 cupsdMoveJob(cupsd_job_t
*job
, /* I - Job */
1778 cupsd_printer_t
*p
) /* I - Destination printer or class */
1780 ipp_attribute_t
*attr
; /* job-printer-uri attribute */
1781 const char *olddest
; /* Old destination */
1782 cupsd_printer_t
*oldp
; /* Old pointer */
1786 * Don't move completed jobs...
1789 if (job
->state_value
> IPP_JOB_STOPPED
)
1793 * Get the old destination...
1796 olddest
= job
->dest
;
1799 oldp
= job
->printer
;
1801 oldp
= cupsdFindDest(olddest
);
1804 * Change the destination information...
1807 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
1808 "Stopping job prior to move.");
1810 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, oldp
, job
,
1811 "Job #%d moved from %s to %s.", job
->id
, olddest
,
1814 cupsdSetString(&job
->dest
, p
->name
);
1815 job
->dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_REMOTE
|
1816 CUPS_PRINTER_IMPLICIT
);
1818 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1819 IPP_TAG_URI
)) != NULL
)
1820 cupsdSetString(&(attr
->values
[0].string
.text
), p
->uri
);
1822 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, p
, job
,
1823 "Job #%d moved from %s to %s.", job
->id
, olddest
,
1827 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1832 * 'cupsdReleaseJob()' - Release the specified job.
1836 cupsdReleaseJob(cupsd_job_t
*job
) /* I - Job */
1838 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdReleaseJob(job=%p(%d))", job
,
1841 if (job
->state_value
== IPP_JOB_HELD
)
1844 * Add trailing banner as needed...
1847 if (job
->pending_timeout
)
1848 cupsdTimeoutJob(job
);
1850 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
1851 "Job released by user.");
1857 * 'cupsdRestartJob()' - Restart the specified job.
1861 cupsdRestartJob(cupsd_job_t
*job
) /* I - Job */
1863 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdRestartJob(job=%p(%d))", job
,
1866 if (job
->state_value
== IPP_JOB_STOPPED
|| job
->num_files
)
1867 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
1868 "Job restarted by user.");
1873 * 'cupsdSaveAllJobs()' - Save a summary of all jobs to disk.
1877 cupsdSaveAllJobs(void)
1879 int i
; /* Looping var */
1880 cups_file_t
*fp
; /* Job cache file */
1881 char temp
[1024]; /* Temporary string */
1882 cupsd_job_t
*job
; /* Current job */
1883 time_t curtime
; /* Current time */
1884 struct tm
*curdate
; /* Current date */
1887 snprintf(temp
, sizeof(temp
), "%s/job.cache", CacheDir
);
1888 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
1890 cupsdLogMessage(CUPSD_LOG_ERROR
,
1891 "Unable to create job cache file \"%s\" - %s",
1892 temp
, strerror(errno
));
1896 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving job cache file \"%s\"...", temp
);
1899 * Restrict access to the file...
1902 fchown(cupsFileNumber(fp
), getuid(), Group
);
1903 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
1906 * Write a small header to the file...
1909 curtime
= time(NULL
);
1910 curdate
= localtime(&curtime
);
1911 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
1913 cupsFilePuts(fp
, "# Job cache file for " CUPS_SVERSION
"\n");
1914 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
1915 cupsFilePrintf(fp
, "NextJobId %d\n", NextJobId
);
1918 * Write each job known to the system...
1921 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1923 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1925 cupsFilePrintf(fp
, "<Job %d>\n", job
->id
);
1926 cupsFilePrintf(fp
, "State %d\n", job
->state_value
);
1927 cupsFilePrintf(fp
, "Priority %d\n", job
->priority
);
1928 cupsFilePrintf(fp
, "HoldUntil %d\n", (int)job
->hold_until
);
1929 cupsFilePrintf(fp
, "Username %s\n", job
->username
);
1930 cupsFilePrintf(fp
, "Destination %s\n", job
->dest
);
1931 cupsFilePrintf(fp
, "DestType %d\n", job
->dtype
);
1932 cupsFilePrintf(fp
, "NumFiles %d\n", job
->num_files
);
1933 for (i
= 0; i
< job
->num_files
; i
++)
1934 cupsFilePrintf(fp
, "File %d %s/%s %d\n", i
+ 1, job
->filetypes
[i
]->super
,
1935 job
->filetypes
[i
]->type
, job
->compressions
[i
]);
1936 cupsFilePuts(fp
, "</Job>\n");
1944 * 'cupsdSaveJob()' - Save a job to disk.
1948 cupsdSaveJob(cupsd_job_t
*job
) /* I - Job */
1950 char filename
[1024]; /* Job control filename */
1951 cups_file_t
*fp
; /* Job file */
1954 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSaveJob(job=%p(%d)): job->attrs=%p",
1955 job
, job
->id
, job
->attrs
);
1957 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
, job
->id
);
1959 if ((fp
= cupsFileOpen(filename
, "w")) == NULL
)
1961 cupsdLogMessage(CUPSD_LOG_ERROR
,
1962 "[Job %d] Unable to create job control file \"%s\" - %s.",
1963 job
->id
, filename
, strerror(errno
));
1967 fchmod(cupsFileNumber(fp
), 0600);
1968 fchown(cupsFileNumber(fp
), RunUser
, Group
);
1970 job
->attrs
->state
= IPP_IDLE
;
1972 if (ippWriteIO(fp
, (ipp_iocb_t
)cupsFileWrite
, 1, NULL
,
1973 job
->attrs
) != IPP_DATA
)
1974 cupsdLogMessage(CUPSD_LOG_ERROR
,
1975 "[Job %d] Unable to write job control file!", job
->id
);
1984 * 'cupsdSetJobHoldUntil()' - Set the hold time for a job.
1988 cupsdSetJobHoldUntil(cupsd_job_t
*job
, /* I - Job */
1989 const char *when
, /* I - When to resume */
1990 int update
)/* I - Update job-hold-until attr? */
1992 time_t curtime
; /* Current time */
1993 struct tm
*curdate
; /* Current date */
1994 int hour
; /* Hold hour */
1995 int minute
; /* Hold minute */
1996 int second
= 0; /* Hold second */
1999 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2000 "cupsdSetJobHoldUntil(job=%p(%d), when=\"%s\", update=%d)",
2001 job
, job
->id
, when
, update
);
2006 * Update the job-hold-until attribute...
2009 ipp_attribute_t
*attr
; /* job-hold-until attribute */
2011 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2012 IPP_TAG_KEYWORD
)) == NULL
)
2013 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2016 cupsdSetString(&(attr
->values
[0].string
.text
), when
);
2018 attr
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
2019 "job-hold-until", NULL
, when
);
2023 if (isdigit(when
[0] & 255))
2024 attr
->value_tag
= IPP_TAG_NAME
;
2026 attr
->value_tag
= IPP_TAG_KEYWORD
;
2029 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2034 * Update the hold time...
2037 if (!strcmp(when
, "indefinite") || !strcmp(when
, "auth-info-required"))
2040 * Hold indefinitely...
2043 job
->hold_until
= 0;
2045 else if (!strcmp(when
, "day-time"))
2048 * Hold to 6am the next morning unless local time is < 6pm.
2051 curtime
= time(NULL
);
2052 curdate
= localtime(&curtime
);
2054 if (curdate
->tm_hour
< 18)
2055 job
->hold_until
= curtime
;
2057 job
->hold_until
= curtime
+
2058 ((29 - curdate
->tm_hour
) * 60 + 59 -
2059 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2061 else if (!strcmp(when
, "evening") || !strcmp(when
, "night"))
2064 * Hold to 6pm unless local time is > 6pm or < 6am.
2067 curtime
= time(NULL
);
2068 curdate
= localtime(&curtime
);
2070 if (curdate
->tm_hour
< 6 || curdate
->tm_hour
>= 18)
2071 job
->hold_until
= curtime
;
2073 job
->hold_until
= curtime
+
2074 ((17 - curdate
->tm_hour
) * 60 + 59 -
2075 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2077 else if (!strcmp(when
, "second-shift"))
2080 * Hold to 4pm unless local time is > 4pm.
2083 curtime
= time(NULL
);
2084 curdate
= localtime(&curtime
);
2086 if (curdate
->tm_hour
>= 16)
2087 job
->hold_until
= curtime
;
2089 job
->hold_until
= curtime
+
2090 ((15 - curdate
->tm_hour
) * 60 + 59 -
2091 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2093 else if (!strcmp(when
, "third-shift"))
2096 * Hold to 12am unless local time is < 8am.
2099 curtime
= time(NULL
);
2100 curdate
= localtime(&curtime
);
2102 if (curdate
->tm_hour
< 8)
2103 job
->hold_until
= curtime
;
2105 job
->hold_until
= curtime
+
2106 ((23 - curdate
->tm_hour
) * 60 + 59 -
2107 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2109 else if (!strcmp(when
, "weekend"))
2112 * Hold to weekend unless we are in the weekend.
2115 curtime
= time(NULL
);
2116 curdate
= localtime(&curtime
);
2118 if (curdate
->tm_wday
== 0 || curdate
->tm_wday
== 6)
2119 job
->hold_until
= curtime
;
2121 job
->hold_until
= curtime
+
2122 (((5 - curdate
->tm_wday
) * 24 +
2123 (17 - curdate
->tm_hour
)) * 60 + 59 -
2124 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2126 else if (sscanf(when
, "%d:%d:%d", &hour
, &minute
, &second
) >= 2)
2129 * Hold to specified GMT time (HH:MM or HH:MM:SS)...
2132 curtime
= time(NULL
);
2133 curdate
= gmtime(&curtime
);
2135 job
->hold_until
= curtime
+
2136 ((hour
- curdate
->tm_hour
) * 60 + minute
-
2137 curdate
->tm_min
) * 60 + second
- curdate
->tm_sec
;
2140 * Hold until next day as needed...
2143 if (job
->hold_until
< curtime
)
2144 job
->hold_until
+= 24 * 60 * 60;
2147 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetJobHoldUntil: hold_until=%d",
2148 (int)job
->hold_until
);
2153 * 'cupsdSetJobPriority()' - Set the priority of a job, moving it up/down in
2154 * the list as needed.
2158 cupsdSetJobPriority(
2159 cupsd_job_t
*job
, /* I - Job ID */
2160 int priority
) /* I - New priority (0 to 100) */
2162 ipp_attribute_t
*attr
; /* Job attribute */
2166 * Don't change completed jobs...
2169 if (job
->state_value
>= IPP_JOB_PROCESSING
)
2173 * Set the new priority and re-add the job into the active list...
2176 cupsArrayRemove(ActiveJobs
, job
);
2178 job
->priority
= priority
;
2180 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
2181 IPP_TAG_INTEGER
)) != NULL
)
2182 attr
->values
[0].integer
= priority
;
2184 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-priority",
2187 cupsArrayAdd(ActiveJobs
, job
);
2190 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2195 * 'cupsdSetJobState()' - Set the state of the specified print job.
2200 cupsd_job_t
*job
, /* I - Job to cancel */
2201 ipp_jstate_t newstate
, /* I - New job state */
2202 cupsd_jobaction_t action
, /* I - Action to take */
2203 const char *message
, /* I - Message to log */
2204 ...) /* I - Additional arguments as needed */
2206 int i
; /* Looping var */
2207 ipp_jstate_t oldstate
; /* Old state */
2208 char filename
[1024]; /* Job filename */
2209 ipp_attribute_t
*attr
; /* Job attribute */
2212 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2213 "cupsdSetJobState(job=%p(%d), state=%d, newstate=%d, "
2214 "action=%d, message=\"%s\")", job
, job
->id
, job
->state_value
,
2215 newstate
, action
, message
? message
: "(null)");
2219 * Make sure we have the job attributes...
2222 if (!cupsdLoadJob(job
))
2226 * Don't do anything if the state is unchanged...
2229 if (newstate
== (oldstate
= job
->state_value
))
2233 * Stop any processes that are working on the current job...
2236 if (oldstate
== IPP_JOB_PROCESSING
)
2237 stop_job(job
, action
);
2240 * Set the new job state...
2243 job
->state
->values
[0].integer
= newstate
;
2244 job
->state_value
= newstate
;
2248 case IPP_JOB_PENDING
:
2250 * Update job-hold-until as needed...
2253 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2254 IPP_TAG_KEYWORD
)) == NULL
)
2255 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2259 attr
->value_tag
= IPP_TAG_KEYWORD
;
2260 cupsdSetString(&(attr
->values
[0].string
.text
), "no-hold");
2266 case IPP_JOB_ABORTED
:
2267 case IPP_JOB_CANCELED
:
2268 case IPP_JOB_COMPLETED
:
2269 set_time(job
, "time-at-completed");
2274 * Log message as needed...
2279 char buffer
[2048]; /* Message buffer */
2280 va_list ap
; /* Pointer to additional arguments */
2282 va_start(ap
, message
);
2283 vsnprintf(buffer
, sizeof(buffer
), message
, ap
);
2286 if (newstate
> IPP_JOB_STOPPED
)
2287 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
, "%s", buffer
);
2289 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
, "%s", buffer
);
2291 if (newstate
== IPP_JOB_STOPPED
|| newstate
== IPP_JOB_ABORTED
)
2292 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "%s", buffer
);
2294 cupsdLogJob(job
, CUPSD_LOG_INFO
, "%s", buffer
);
2298 * Handle post-state-change actions...
2303 case IPP_JOB_PROCESSING
:
2305 * Add the job to the "printing" list...
2308 if (!cupsArrayFind(PrintingJobs
, job
))
2309 cupsArrayAdd(PrintingJobs
, job
);
2312 * Set the processing time...
2315 set_time(job
, "time-at-processing");
2317 case IPP_JOB_PENDING
:
2319 case IPP_JOB_STOPPED
:
2321 * Make sure the job is in the active list...
2324 if (!cupsArrayFind(ActiveJobs
, job
))
2325 cupsArrayAdd(ActiveJobs
, job
);
2328 * Save the job state to disk...
2332 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2335 case IPP_JOB_ABORTED
:
2336 case IPP_JOB_CANCELED
:
2337 case IPP_JOB_COMPLETED
:
2338 if (newstate
== IPP_JOB_CANCELED
)
2341 * Remove the job from the active list if there are no processes still
2345 for (i
= 0; job
->filters
[i
] < 0; i
++);
2347 if (!job
->filters
[i
] && job
->backend
<= 0)
2348 cupsArrayRemove(ActiveJobs
, job
);
2353 * Otherwise just remove the job from the active list immediately...
2356 cupsArrayRemove(ActiveJobs
, job
);
2360 * Expire job subscriptions since the job is now "completed"...
2363 cupsdExpireSubscriptions(NULL
, job
);
2367 * If we are going to sleep and the PrintingJobs count is now 0, allow the
2368 * sleep to happen immediately...
2371 if (Sleeping
&& cupsArrayCount(PrintingJobs
) == 0)
2373 #endif /* __APPLE__ */
2376 * Remove any authentication data...
2379 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
2380 if (cupsdRemoveFile(filename
) && errno
!= ENOENT
)
2381 cupsdLogMessage(CUPSD_LOG_ERROR
,
2382 "Unable to remove authentication cache: %s",
2385 cupsdClearString(&job
->auth_username
);
2386 cupsdClearString(&job
->auth_domain
);
2387 cupsdClearString(&job
->auth_password
);
2391 * Destroy the credential cache and clear the KRB5CCNAME env var string.
2396 krb5_cc_destroy(KerberosContext
, job
->ccache
);
2400 cupsdClearString(&job
->ccname
);
2401 #endif /* HAVE_GSSAPI */
2404 * Remove the print file for good if we aren't preserving jobs or
2408 if (!JobHistory
|| !JobFiles
|| action
== CUPSD_JOB_PURGE
)
2410 for (i
= 1; i
<= job
->num_files
; i
++)
2412 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
2417 if (job
->num_files
> 0)
2419 free(job
->filetypes
);
2420 free(job
->compressions
);
2423 job
->filetypes
= NULL
;
2424 job
->compressions
= NULL
;
2428 if (JobHistory
&& action
!= CUPSD_JOB_PURGE
)
2431 * Save job state info...
2435 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2441 * Finalize the job immediately if we forced things...
2444 if (action
>= CUPSD_JOB_FORCE
&& job
->printer
)
2445 finalize_job(job
, 0);
2448 * Update the server "busy" state...
2451 cupsdSetBusyState();
2456 * 'cupsdStopAllJobs()' - Stop all print jobs.
2461 cupsd_jobaction_t action
, /* I - Action */
2462 int kill_delay
) /* I - Number of seconds before we kill */
2464 cupsd_job_t
*job
; /* Current job */
2467 DEBUG_puts("cupsdStopAllJobs()");
2469 for (job
= (cupsd_job_t
*)cupsArrayFirst(PrintingJobs
);
2471 job
= (cupsd_job_t
*)cupsArrayNext(PrintingJobs
))
2474 job
->kill_time
= time(NULL
) + kill_delay
;
2476 cupsdSetJobState(job
, IPP_JOB_PENDING
, action
, NULL
);
2482 * 'cupsdUnloadCompletedJobs()' - Flush completed job history from memory.
2486 cupsdUnloadCompletedJobs(void)
2488 cupsd_job_t
*job
; /* Current job */
2489 time_t expire
; /* Expiration time */
2492 expire
= time(NULL
) - 60;
2494 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
2496 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
2497 if (job
->attrs
&& job
->state_value
>= IPP_JOB_STOPPED
&& !job
->printer
&&
2498 job
->access_time
< expire
)
2509 * 'compare_active_jobs()' - Compare the job IDs and priorities of two jobs.
2512 static int /* O - Difference */
2513 compare_active_jobs(void *first
, /* I - First job */
2514 void *second
, /* I - Second job */
2515 void *data
) /* I - App data (not used) */
2517 int diff
; /* Difference */
2520 if ((diff
= ((cupsd_job_t
*)second
)->priority
-
2521 ((cupsd_job_t
*)first
)->priority
) != 0)
2524 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
2529 * 'compare_jobs()' - Compare the job IDs of two jobs.
2532 static int /* O - Difference */
2533 compare_jobs(void *first
, /* I - First job */
2534 void *second
, /* I - Second job */
2535 void *data
) /* I - App data (not used) */
2537 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
2542 * 'dump_job_history()' - Dump any debug messages for a job.
2546 dump_job_history(cupsd_job_t
*job
) /* I - Job */
2548 int i
, /* Looping var */
2549 oldsize
; /* Current MaxLogSize */
2550 struct tm
*date
; /* Date/time value */
2551 cupsd_joblog_t
*message
; /* Current message */
2552 char temp
[2048], /* Log message */
2553 *ptr
, /* Pointer into log message */
2554 start
[256], /* Start time */
2555 end
[256]; /* End time */
2556 cupsd_printer_t
*printer
; /* Printer for job */
2560 * See if we have anything to dump...
2567 * Disable log rotation temporarily...
2570 oldsize
= MaxLogSize
;
2574 * Copy the debug messages to the log...
2577 message
= (cupsd_joblog_t
*)cupsArrayFirst(job
->history
);
2578 date
= localtime(&(message
->time
));
2579 strftime(start
, sizeof(start
), "%X", date
);
2581 message
= (cupsd_joblog_t
*)cupsArrayLast(job
->history
);
2582 date
= localtime(&(message
->time
));
2583 strftime(end
, sizeof(end
), "%X", date
);
2585 snprintf(temp
, sizeof(temp
),
2586 "[Job %d] The following messages were recorded from %s to %s",
2587 job
->id
, start
, end
);
2588 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, temp
);
2590 for (message
= (cupsd_joblog_t
*)cupsArrayFirst(job
->history
);
2592 message
= (cupsd_joblog_t
*)cupsArrayNext(job
->history
))
2593 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, message
->message
);
2595 snprintf(temp
, sizeof(temp
), "[Job %d] End of messages", job
->id
);
2596 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, temp
);
2599 * Log the printer state values...
2602 if ((printer
= job
->printer
) == NULL
)
2603 printer
= cupsdFindDest(job
->dest
);
2607 snprintf(temp
, sizeof(temp
), "[Job %d] printer-state=%d(%s)", job
->id
,
2609 printer
->state
== IPP_PRINTER_IDLE
? "idle" :
2610 printer
->state
== IPP_PRINTER_PROCESSING
? "processing" :
2612 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, temp
);
2614 snprintf(temp
, sizeof(temp
), "[Job %d] printer-state-message=\"%s\"",
2615 job
->id
, printer
->state_message
);
2616 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, temp
);
2618 snprintf(temp
, sizeof(temp
), "[Job %d] printer-state-reasons=", job
->id
);
2619 ptr
= temp
+ strlen(temp
);
2620 if (printer
->num_reasons
== 0)
2621 strlcpy(ptr
, "none", sizeof(temp
) - (ptr
- temp
));
2625 i
< printer
->num_reasons
&& ptr
< (temp
+ sizeof(temp
) - 2);
2631 strlcpy(ptr
, printer
->reasons
[i
], sizeof(temp
) - (ptr
- temp
));
2635 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, temp
);
2639 * Restore log file rotation...
2642 MaxLogSize
= oldsize
;
2645 * Free all messages...
2648 free_job_history(job
);
2653 * 'free_job_history()' - Free any log history.
2657 free_job_history(cupsd_job_t
*job
) /* I - Job */
2659 char *message
; /* Current message */
2665 for (message
= (char *)cupsArrayFirst(job
->history
);
2667 message
= (char *)cupsArrayNext(job
->history
))
2670 cupsArrayDelete(job
->history
);
2671 job
->history
= NULL
;
2676 * 'finalize_job()' - Cleanup after job filter processes and support data.
2680 finalize_job(cupsd_job_t
*job
, /* I - Job */
2681 int set_job_state
) /* I - 1 = set the job state */
2683 ipp_pstate_t printer_state
; /* New printer state value */
2684 ipp_jstate_t job_state
; /* New job state value */
2685 const char *message
; /* Message for job state */
2686 char buffer
[1024]; /* Buffer for formatted messages */
2689 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "finalize_job(job=%p(%d))", job
, job
->id
);
2692 * Clear the "connecting-to-device" reason, which is only valid when a printer
2696 cupsdSetPrinterReasons(job
->printer
, "-connecting-to-device");
2699 * Similarly, clear the "offline-report" reason for non-USB devices since we
2700 * rarely have current information for network devices...
2703 if (strncmp(job
->printer
->device_uri
, "usb:", 4))
2704 cupsdSetPrinterReasons(job
->printer
, "-offline-report");
2707 * Free the security profile...
2710 cupsdDestroyProfile(job
->profile
);
2711 job
->profile
= NULL
;
2714 * Close pipes and status buffer...
2717 cupsdClosePipe(job
->print_pipes
);
2718 cupsdClosePipe(job
->back_pipes
);
2719 cupsdClosePipe(job
->side_pipes
);
2721 cupsdRemoveSelect(job
->status_pipes
[0]);
2722 cupsdClosePipe(job
->status_pipes
);
2723 cupsdStatBufDelete(job
->status_buffer
);
2724 job
->status_buffer
= NULL
;
2727 * Process the exit status...
2730 if (job
->printer
->state
== IPP_PRINTER_PROCESSING
)
2731 printer_state
= IPP_PRINTER_IDLE
;
2733 printer_state
= job
->printer
->state
;
2735 switch (job_state
= job
->state_value
)
2737 case IPP_JOB_PENDING
:
2738 message
= "Job paused.";
2742 message
= "Job held.";
2746 case IPP_JOB_PROCESSING
:
2747 case IPP_JOB_COMPLETED
:
2748 job_state
= IPP_JOB_COMPLETED
;
2749 message
= "Job completed.";
2752 case IPP_JOB_STOPPED
:
2753 message
= "Job stopped.";
2756 case IPP_JOB_CANCELED
:
2757 message
= "Job canceled.";
2760 case IPP_JOB_ABORTED
:
2761 message
= "Job aborted.";
2765 if (job
->status
< 0)
2768 * Backend had errors...
2771 int exit_code
; /* Exit code from backend */
2775 * Convert the status to an exit code. Due to the way the W* macros are
2776 * implemented on MacOS X (bug?), we have to store the exit status in a
2777 * variable first and then convert...
2780 exit_code
= -job
->status
;
2781 if (WIFEXITED(exit_code
))
2782 exit_code
= WEXITSTATUS(exit_code
);
2784 exit_code
= job
->status
;
2786 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Backend returned status %d (%s)",
2788 exit_code
== CUPS_BACKEND_FAILED
? "failed" :
2789 exit_code
== CUPS_BACKEND_AUTH_REQUIRED
?
2790 "authentication required" :
2791 exit_code
== CUPS_BACKEND_HOLD
? "hold job" :
2792 exit_code
== CUPS_BACKEND_STOP
? "stop printer" :
2793 exit_code
== CUPS_BACKEND_CANCEL
? "cancel job" :
2794 exit_code
< 0 ? "crashed" : "unknown");
2797 * Do what needs to be done...
2803 case CUPS_BACKEND_FAILED
:
2805 * Backend failure, use the error-policy to determine how to
2809 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
2812 * Queued on a class - mark the job as pending and we'll retry on
2813 * another printer...
2816 if (job_state
== IPP_JOB_COMPLETED
)
2818 job_state
= IPP_JOB_PENDING
;
2819 message
= "Retrying job on another printer.";
2822 else if (!strcmp(job
->printer
->error_policy
, "retry-current-job"))
2825 * The error policy is "retry-current-job" - mark the job as pending
2826 * and we'll retry on the same printer...
2829 if (job_state
== IPP_JOB_COMPLETED
)
2831 job_state
= IPP_JOB_PENDING
;
2832 message
= "Retrying job on same printer.";
2835 else if ((job
->printer
->type
& CUPS_PRINTER_FAX
) ||
2836 !strcmp(job
->printer
->error_policy
, "retry-job"))
2838 if (job_state
== IPP_JOB_COMPLETED
)
2841 * The job was queued on a fax or the error policy is "retry-job" -
2842 * hold the job if the number of retries is less than the
2843 * JobRetryLimit, otherwise abort the job.
2848 if (job
->tries
>= JobRetryLimit
)
2854 snprintf(buffer
, sizeof(buffer
),
2855 "Job aborted after %d unsuccessful attempts.",
2857 job_state
= IPP_JOB_ABORTED
;
2863 * Try again in N seconds...
2866 set_hold_until(job
, time(NULL
) + JobRetryInterval
);
2868 snprintf(buffer
, sizeof(buffer
),
2869 "Job held for %d seconds since it could not be sent.",
2871 job_state
= IPP_JOB_HELD
;
2876 else if (!strcmp(job
->printer
->error_policy
, "abort-job") &&
2877 job_state
== IPP_JOB_COMPLETED
)
2879 job_state
= IPP_JOB_ABORTED
;
2880 message
= "Job aborted due to backend errors; please consult "
2881 "the error_log file for details.";
2883 else if (job
->state_value
== IPP_JOB_PROCESSING
)
2885 job_state
= IPP_JOB_PENDING
;
2886 printer_state
= IPP_PRINTER_STOPPED
;
2887 message
= "Printer stopped due to backend errors; please "
2888 "consult the error_log file for details.";
2892 case CUPS_BACKEND_CANCEL
:
2897 if (job_state
== IPP_JOB_COMPLETED
)
2899 job_state
= IPP_JOB_ABORTED
;
2900 message
= "Job aborted due to backend errors; please consult "
2901 "the error_log file for details.";
2905 case CUPS_BACKEND_HOLD
:
2906 if (job_state
== IPP_JOB_COMPLETED
)
2912 cupsdSetJobHoldUntil(job
, "indefinite", 1);
2914 job_state
= IPP_JOB_HELD
;
2915 message
= "Job held indefinitely due to backend errors; please "
2916 "consult the error_log file for details.";
2920 case CUPS_BACKEND_STOP
:
2922 * Stop the printer...
2925 printer_state
= IPP_PRINTER_STOPPED
;
2926 message
= "Printer stopped due to backend errors; please "
2927 "consult the error_log file for details.";
2929 if (job_state
== IPP_JOB_COMPLETED
)
2930 job_state
= IPP_JOB_PENDING
;
2933 case CUPS_BACKEND_AUTH_REQUIRED
:
2935 * Hold the job for authentication...
2938 if (job_state
== IPP_JOB_COMPLETED
)
2940 cupsdSetJobHoldUntil(job
, "auth-info-required", 1);
2942 job_state
= IPP_JOB_HELD
;
2943 message
= "Job held for authentication.";
2948 else if (job
->status
> 0)
2951 * Filter had errors; stop job...
2954 if (job_state
== IPP_JOB_COMPLETED
)
2956 job_state
= IPP_JOB_STOPPED
;
2957 message
= "Job stopped due to filter errors; please consult the "
2958 "error_log file for details.";
2963 * Update the printer and job state.
2966 if (job_state
!= job
->state_value
)
2967 cupsdSetJobState(job
, job_state
, CUPSD_JOB_DEFAULT
, "%s", message
);
2969 cupsdSetPrinterState(job
->printer
, printer_state
,
2970 printer_state
== IPP_PRINTER_STOPPED
);
2971 update_job_attrs(job
, 0);
2976 dump_job_history(job
);
2978 free_job_history(job
);
2981 cupsArrayRemove(PrintingJobs
, job
);
2984 * Clear the printer <-> job association...
2987 job
->printer
->job
= NULL
;
2988 job
->printer
= NULL
;
2991 * Try printing another job...
2994 if (printer_state
!= IPP_PRINTER_STOPPED
)
3000 * 'get_options()' - Get a string containing the job options.
3003 static char * /* O - Options string */
3004 get_options(cupsd_job_t
*job
, /* I - Job */
3005 int banner_page
, /* I - Printing a banner page? */
3006 char *copies
, /* I - Copies buffer */
3007 size_t copies_size
, /* I - Size of copies buffer */
3008 char *title
, /* I - Title buffer */
3009 size_t title_size
) /* I - Size of title buffer */
3011 int i
; /* Looping var */
3012 char *optptr
, /* Pointer to options */
3013 *valptr
; /* Pointer in value string */
3014 ipp_attribute_t
*attr
; /* Current attribute */
3015 static char *options
= NULL
;/* Full list of options */
3016 static int optlength
= 0; /* Length of option buffer */
3020 * Building the options string is harder than it needs to be, but
3021 * for the moment we need to pass strings for command-line args and
3022 * not IPP attribute pointers... :)
3024 * First allocate/reallocate the option buffer as needed...
3027 i
= ipp_length(job
->attrs
);
3029 if (i
> optlength
|| !options
)
3034 optptr
= realloc(options
, i
);
3038 cupsdLogJob(job
, CUPSD_LOG_CRIT
,
3039 "Unable to allocate %d bytes for option buffer!", i
);
3048 * Now loop through the attributes and convert them to the textual
3049 * representation used by the filters...
3055 snprintf(title
, title_size
, "%s-%d", job
->printer
->name
, job
->id
);
3056 strlcpy(copies
, "1", copies_size
);
3058 for (attr
= job
->attrs
->attrs
; attr
!= NULL
; attr
= attr
->next
)
3060 if (!strcmp(attr
->name
, "copies") &&
3061 attr
->value_tag
== IPP_TAG_INTEGER
)
3064 * Don't use the # copies attribute if we are printing the job sheets...
3068 snprintf(copies
, copies_size
, "%d", attr
->values
[0].integer
);
3070 else if (!strcmp(attr
->name
, "job-name") &&
3071 (attr
->value_tag
== IPP_TAG_NAME
||
3072 attr
->value_tag
== IPP_TAG_NAMELANG
))
3073 strlcpy(title
, attr
->values
[0].string
.text
, title_size
);
3074 else if (attr
->group_tag
== IPP_TAG_JOB
)
3077 * Filter out other unwanted attributes...
3080 if (attr
->value_tag
== IPP_TAG_MIMETYPE
||
3081 attr
->value_tag
== IPP_TAG_NAMELANG
||
3082 attr
->value_tag
== IPP_TAG_TEXTLANG
||
3083 (attr
->value_tag
== IPP_TAG_URI
&& strcmp(attr
->name
, "job-uuid")) ||
3084 attr
->value_tag
== IPP_TAG_URISCHEME
||
3085 attr
->value_tag
== IPP_TAG_BEGIN_COLLECTION
) /* Not yet supported */
3088 if (!strncmp(attr
->name
, "time-", 5) ||
3089 !strcmp(attr
->name
, "job-hold-until"))
3092 if (!strncmp(attr
->name
, "job-", 4) &&
3093 strcmp(attr
->name
, "job-billing") &&
3094 strcmp(attr
->name
, "job-impressions") &&
3095 strcmp(attr
->name
, "job-originating-host-name") &&
3096 strcmp(attr
->name
, "job-uuid") &&
3097 !(job
->printer
->type
& CUPS_PRINTER_REMOTE
))
3100 if ((!strcmp(attr
->name
, "job-impressions") ||
3101 !strcmp(attr
->name
, "page-label") ||
3102 !strcmp(attr
->name
, "page-border") ||
3103 !strncmp(attr
->name
, "number-up", 9) ||
3104 !strcmp(attr
->name
, "page-ranges") ||
3105 !strcmp(attr
->name
, "page-set") ||
3106 !strcasecmp(attr
->name
, "AP_FIRSTPAGE_InputSlot") ||
3107 !strcasecmp(attr
->name
, "AP_FIRSTPAGE_ManualFeed") ||
3108 !strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
3109 "PMTotalSidesImaged..n.") ||
3110 !strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
3111 "PMTotalBeginPages..n.")) &&
3116 * Otherwise add them to the list...
3119 if (optptr
> options
)
3120 strlcat(optptr
, " ", optlength
- (optptr
- options
));
3122 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
3124 strlcat(optptr
, attr
->name
, optlength
- (optptr
- options
));
3125 strlcat(optptr
, "=", optlength
- (optptr
- options
));
3128 for (i
= 0; i
< attr
->num_values
; i
++)
3131 strlcat(optptr
, ",", optlength
- (optptr
- options
));
3133 optptr
+= strlen(optptr
);
3135 switch (attr
->value_tag
)
3137 case IPP_TAG_INTEGER
:
3139 snprintf(optptr
, optlength
- (optptr
- options
),
3140 "%d", attr
->values
[i
].integer
);
3143 case IPP_TAG_BOOLEAN
:
3144 if (!attr
->values
[i
].boolean
)
3145 strlcat(optptr
, "no", optlength
- (optptr
- options
));
3147 case IPP_TAG_NOVALUE
:
3148 strlcat(optptr
, attr
->name
,
3149 optlength
- (optptr
- options
));
3152 case IPP_TAG_RANGE
:
3153 if (attr
->values
[i
].range
.lower
== attr
->values
[i
].range
.upper
)
3154 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
3155 "%d", attr
->values
[i
].range
.lower
);
3157 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
3158 "%d-%d", attr
->values
[i
].range
.lower
,
3159 attr
->values
[i
].range
.upper
);
3162 case IPP_TAG_RESOLUTION
:
3163 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
3164 "%dx%d%s", attr
->values
[i
].resolution
.xres
,
3165 attr
->values
[i
].resolution
.yres
,
3166 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
3170 case IPP_TAG_STRING
:
3173 case IPP_TAG_KEYWORD
:
3174 case IPP_TAG_CHARSET
:
3175 case IPP_TAG_LANGUAGE
:
3177 for (valptr
= attr
->values
[i
].string
.text
; *valptr
;)
3179 if (strchr(" \t\n\\\'\"", *valptr
))
3181 *optptr
++ = *valptr
++;
3188 break; /* anti-compiler-warning-code */
3192 optptr
+= strlen(optptr
);
3202 * 'ipp_length()' - Compute the size of the buffer needed to hold
3203 * the textual IPP attributes.
3206 static int /* O - Size of attribute buffer */
3207 ipp_length(ipp_t
*ipp
) /* I - IPP request */
3209 int bytes
; /* Number of bytes */
3210 int i
; /* Looping var */
3211 ipp_attribute_t
*attr
; /* Current attribute */
3215 * Loop through all attributes...
3220 for (attr
= ipp
->attrs
; attr
!= NULL
; attr
= attr
->next
)
3223 * Skip attributes that won't be sent to filters...
3226 if (attr
->value_tag
== IPP_TAG_MIMETYPE
||
3227 attr
->value_tag
== IPP_TAG_NAMELANG
||
3228 attr
->value_tag
== IPP_TAG_TEXTLANG
||
3229 attr
->value_tag
== IPP_TAG_URI
||
3230 attr
->value_tag
== IPP_TAG_URISCHEME
)
3233 if (strncmp(attr
->name
, "time-", 5) == 0)
3237 * Add space for a leading space and commas between each value.
3238 * For the first attribute, the leading space isn't used, so the
3239 * extra byte can be used as the nul terminator...
3242 bytes
++; /* " " separator */
3243 bytes
+= attr
->num_values
; /* "," separators */
3246 * Boolean attributes appear as "foo,nofoo,foo,nofoo", while
3247 * other attributes appear as "foo=value1,value2,...,valueN".
3250 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
3251 bytes
+= strlen(attr
->name
);
3253 bytes
+= attr
->num_values
* strlen(attr
->name
);
3256 * Now add the size required for each value in the attribute...
3259 switch (attr
->value_tag
)
3261 case IPP_TAG_INTEGER
:
3264 * Minimum value of a signed integer is -2147483647, or 11 digits.
3267 bytes
+= attr
->num_values
* 11;
3270 case IPP_TAG_BOOLEAN
:
3272 * Add two bytes for each false ("no") value...
3275 for (i
= 0; i
< attr
->num_values
; i
++)
3276 if (!attr
->values
[i
].boolean
)
3280 case IPP_TAG_RANGE
:
3282 * A range is two signed integers separated by a hyphen, or
3283 * 23 characters max.
3286 bytes
+= attr
->num_values
* 23;
3289 case IPP_TAG_RESOLUTION
:
3291 * A resolution is two signed integers separated by an "x" and
3292 * suffixed by the units, or 26 characters max.
3295 bytes
+= attr
->num_values
* 26;
3298 case IPP_TAG_STRING
:
3301 case IPP_TAG_KEYWORD
:
3302 case IPP_TAG_CHARSET
:
3303 case IPP_TAG_LANGUAGE
:
3306 * Strings can contain characters that need quoting. We need
3307 * at least 2 * len + 2 characters to cover the quotes and
3308 * any backslashes in the string.
3311 for (i
= 0; i
< attr
->num_values
; i
++)
3312 bytes
+= 2 * strlen(attr
->values
[i
].string
.text
) + 2;
3316 break; /* anti-compiler-warning-code */
3325 * 'load_job_cache()' - Load jobs from the job.cache file.
3329 load_job_cache(const char *filename
) /* I - job.cache filename */
3331 cups_file_t
*fp
; /* job.cache file */
3332 char line
[1024], /* Line buffer */
3333 *value
; /* Value on line */
3334 int linenum
; /* Line number in file */
3335 cupsd_job_t
*job
; /* Current job */
3336 int jobid
; /* Job ID */
3337 char jobfile
[1024]; /* Job filename */
3341 * Open the job.cache file...
3344 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
3346 if (errno
!= ENOENT
)
3347 cupsdLogMessage(CUPSD_LOG_ERROR
,
3348 "Unable to open job cache file \"%s\": %s",
3349 filename
, strerror(errno
));
3351 load_request_root();
3357 * Read entries from the job cache file and create jobs as needed.
3360 cupsdLogMessage(CUPSD_LOG_INFO
, "Loading job cache file \"%s\"...",
3366 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
3368 if (!strcasecmp(line
, "NextJobId"))
3371 NextJobId
= atoi(value
);
3373 else if (!strcasecmp(line
, "<Job"))
3377 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing </Job> directive on line %d!",
3384 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing job ID on line %d!", linenum
);
3388 jobid
= atoi(value
);
3392 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad job ID %d on line %d!", jobid
,
3397 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, jobid
);
3398 if (access(jobfile
, 0))
3400 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Job %d] Files have gone away!",
3405 job
= calloc(1, sizeof(cupsd_job_t
));
3408 cupsdLogMessage(CUPSD_LOG_EMERG
,
3409 "[Job %d] Unable to allocate memory for job!", jobid
);
3414 job
->back_pipes
[0] = -1;
3415 job
->back_pipes
[1] = -1;
3416 job
->print_pipes
[0] = -1;
3417 job
->print_pipes
[1] = -1;
3418 job
->side_pipes
[0] = -1;
3419 job
->side_pipes
[1] = -1;
3420 job
->status_pipes
[0] = -1;
3421 job
->status_pipes
[1] = -1;
3423 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Loading from cache...",
3428 cupsdLogMessage(CUPSD_LOG_ERROR
,
3429 "Missing <Job #> directive on line %d!", linenum
);
3432 else if (!strcasecmp(line
, "</Job>"))
3434 cupsArrayAdd(Jobs
, job
);
3436 if (job
->state_value
<= IPP_JOB_STOPPED
&& cupsdLoadJob(job
))
3437 cupsArrayAdd(ActiveJobs
, job
);
3443 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing value on line %d!", linenum
);
3446 else if (!strcasecmp(line
, "State"))
3448 job
->state_value
= (ipp_jstate_t
)atoi(value
);
3450 if (job
->state_value
< IPP_JOB_PENDING
)
3451 job
->state_value
= IPP_JOB_PENDING
;
3452 else if (job
->state_value
> IPP_JOB_COMPLETED
)
3453 job
->state_value
= IPP_JOB_COMPLETED
;
3455 else if (!strcasecmp(line
, "HoldUntil"))
3457 job
->hold_until
= atoi(value
);
3459 else if (!strcasecmp(line
, "Priority"))
3461 job
->priority
= atoi(value
);
3463 else if (!strcasecmp(line
, "Username"))
3465 cupsdSetString(&job
->username
, value
);
3467 else if (!strcasecmp(line
, "Destination"))
3469 cupsdSetString(&job
->dest
, value
);
3471 else if (!strcasecmp(line
, "DestType"))
3473 job
->dtype
= (cups_ptype_t
)atoi(value
);
3475 else if (!strcasecmp(line
, "NumFiles"))
3477 job
->num_files
= atoi(value
);
3479 if (job
->num_files
< 0)
3481 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad NumFiles value %d on line %d!",
3482 job
->num_files
, linenum
);
3487 if (job
->num_files
> 0)
3489 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-001", RequestRoot
,
3491 if (access(jobfile
, 0))
3493 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Data files have gone away!",
3499 job
->filetypes
= calloc(job
->num_files
, sizeof(mime_type_t
*));
3500 job
->compressions
= calloc(job
->num_files
, sizeof(int));
3502 if (!job
->filetypes
|| !job
->compressions
)
3504 cupsdLogMessage(CUPSD_LOG_EMERG
,
3505 "[Job %d] Unable to allocate memory for %d files!",
3506 job
->id
, job
->num_files
);
3511 else if (!strcasecmp(line
, "File"))
3513 int number
, /* File number */
3514 compression
; /* Compression value */
3515 char super
[MIME_MAX_SUPER
], /* MIME super type */
3516 type
[MIME_MAX_TYPE
]; /* MIME type */
3519 if (sscanf(value
, "%d%*[ \t]%15[^/]/%255s%d", &number
, super
, type
,
3522 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File on line %d!", linenum
);
3526 if (number
< 1 || number
> job
->num_files
)
3528 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File number %d on line %d!",
3535 job
->compressions
[number
] = compression
;
3536 job
->filetypes
[number
] = mimeType(MimeDatabase
, super
, type
);
3538 if (!job
->filetypes
[number
])
3541 * If the original MIME type is unknown, auto-type it!
3544 cupsdLogMessage(CUPSD_LOG_ERROR
,
3545 "[Job %d] Unknown MIME type %s/%s for file %d!",
3546 job
->id
, super
, type
, number
+ 1);
3548 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
3549 job
->id
, number
+ 1);
3550 job
->filetypes
[number
] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
3551 job
->compressions
+ number
);
3554 * If that didn't work, assume it is raw...
3557 if (!job
->filetypes
[number
])
3558 job
->filetypes
[number
] = mimeType(MimeDatabase
, "application",
3563 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unknown %s directive on line %d!",
3572 * 'load_next_job_id()' - Load the NextJobId value from the job.cache file.
3576 load_next_job_id(const char *filename
) /* I - job.cache filename */
3578 cups_file_t
*fp
; /* job.cache file */
3579 char line
[1024], /* Line buffer */
3580 *value
; /* Value on line */
3581 int linenum
; /* Line number in file */
3582 int next_job_id
; /* NextJobId value from line */
3586 * Read the NextJobId directive from the job.cache file and use
3587 * the value (if any).
3590 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
3592 if (errno
!= ENOENT
)
3593 cupsdLogMessage(CUPSD_LOG_ERROR
,
3594 "Unable to open job cache file \"%s\": %s",
3595 filename
, strerror(errno
));
3600 cupsdLogMessage(CUPSD_LOG_INFO
,
3601 "Loading NextJobId from job cache file \"%s\"...", filename
);
3605 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
3607 if (!strcasecmp(line
, "NextJobId"))
3611 next_job_id
= atoi(value
);
3613 if (next_job_id
> NextJobId
)
3614 NextJobId
= next_job_id
;
3625 * 'load_request_root()' - Load jobs from the RequestRoot directory.
3629 load_request_root(void)
3631 cups_dir_t
*dir
; /* Directory */
3632 cups_dentry_t
*dent
; /* Directory entry */
3633 cupsd_job_t
*job
; /* New job */
3637 * Open the requests directory...
3640 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Scanning %s for jobs...", RequestRoot
);
3642 if ((dir
= cupsDirOpen(RequestRoot
)) == NULL
)
3644 cupsdLogMessage(CUPSD_LOG_ERROR
,
3645 "Unable to open spool directory \"%s\": %s",
3646 RequestRoot
, strerror(errno
));
3651 * Read all the c##### files...
3654 while ((dent
= cupsDirRead(dir
)) != NULL
)
3655 if (strlen(dent
->filename
) >= 6 && dent
->filename
[0] == 'c')
3658 * Allocate memory for the job...
3661 if ((job
= calloc(sizeof(cupsd_job_t
), 1)) == NULL
)
3663 cupsdLogMessage(CUPSD_LOG_ERROR
, "Ran out of memory for jobs!");
3669 * Assign the job ID...
3672 job
->id
= atoi(dent
->filename
+ 1);
3673 job
->back_pipes
[0] = -1;
3674 job
->back_pipes
[1] = -1;
3675 job
->print_pipes
[0] = -1;
3676 job
->print_pipes
[1] = -1;
3677 job
->side_pipes
[0] = -1;
3678 job
->side_pipes
[1] = -1;
3679 job
->status_pipes
[0] = -1;
3680 job
->status_pipes
[1] = -1;
3682 if (job
->id
>= NextJobId
)
3683 NextJobId
= job
->id
+ 1;
3689 if (cupsdLoadJob(job
))
3692 * Insert the job into the array, sorting by job priority and ID...
3695 cupsArrayAdd(Jobs
, job
);
3697 if (job
->state_value
<= IPP_JOB_STOPPED
)
3698 cupsArrayAdd(ActiveJobs
, job
);
3709 * 'set_hold_until()' - Set the hold time and update job-hold-until attribute.
3713 set_hold_until(cupsd_job_t
*job
, /* I - Job to update */
3714 time_t holdtime
) /* I - Hold until time */
3716 ipp_attribute_t
*attr
; /* job-hold-until attribute */
3717 struct tm
*holddate
; /* Hold date */
3718 char holdstr
[64]; /* Hold time */
3722 * Set the hold_until value and hold the job...
3725 cupsdLogMessage(CUPSD_LOG_DEBUG
, "set_hold_until: hold_until = %d",
3728 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
3729 job
->state_value
= IPP_JOB_HELD
;
3730 job
->hold_until
= holdtime
;
3733 * Update the job-hold-until attribute with a string representing GMT
3734 * time (HH:MM:SS)...
3737 holddate
= gmtime(&holdtime
);
3738 snprintf(holdstr
, sizeof(holdstr
), "%d:%d:%d", holddate
->tm_hour
,
3739 holddate
->tm_min
, holddate
->tm_sec
);
3741 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
3742 IPP_TAG_KEYWORD
)) == NULL
)
3743 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
3746 * Either add the attribute or update the value of the existing one
3750 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "job-hold-until",
3753 cupsdSetString(&attr
->values
[0].string
.text
, holdstr
);
3756 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
3761 * 'set_time()' - Set one of the "time-at-xyz" attributes.
3765 set_time(cupsd_job_t
*job
, /* I - Job to update */
3766 const char *name
) /* I - Name of attribute */
3768 ipp_attribute_t
*attr
; /* Time attribute */
3771 if ((attr
= ippFindAttribute(job
->attrs
, name
, IPP_TAG_ZERO
)) != NULL
)
3773 attr
->value_tag
= IPP_TAG_INTEGER
;
3774 attr
->values
[0].integer
= time(NULL
);
3780 * 'start_job()' - Start a print job.
3784 start_job(cupsd_job_t
*job
, /* I - Job ID */
3785 cupsd_printer_t
*printer
) /* I - Printer to print job */
3787 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "start_job(job=%p(%d), printer=%p(%s))",
3788 job
, job
->id
, printer
, printer
->name
);
3791 * Make sure we have some files around before we try to print...
3794 if (job
->num_files
== 0)
3796 cupsdSetJobState(job
, IPP_JOB_ABORTED
, CUPSD_JOB_DEFAULT
,
3797 "Aborting job because it has no files.");
3802 * Update the printer and job state to "processing"...
3805 if (!cupsdLoadJob(job
))
3808 cupsdSetJobState(job
, IPP_JOB_PROCESSING
, CUPSD_JOB_DEFAULT
, NULL
);
3809 cupsdSetPrinterState(printer
, IPP_PRINTER_PROCESSING
, 0);
3812 job
->current_file
= 0;
3814 job
->printer
= printer
;
3818 * Setup the last exit status and security profiles...
3822 job
->profile
= cupsdCreateProfile(job
->id
);
3825 * Create the status pipes and buffer...
3828 if (cupsdOpenPipe(job
->status_pipes
))
3830 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
3831 "Unable to create job status pipes - %s.", strerror(errno
));
3833 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
3834 "Job stopped because the scheduler could not create the "
3835 "job status pipes.");
3837 cupsdDestroyProfile(job
->profile
);
3838 job
->profile
= NULL
;
3842 job
->status_buffer
= cupsdStatBufNew(job
->status_pipes
[0], NULL
);
3843 job
->status_level
= CUPSD_LOG_INFO
;
3845 if (job
->printer_message
)
3846 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
), "");
3849 * Create the backchannel pipes and make them non-blocking...
3852 if (cupsdOpenPipe(job
->back_pipes
))
3854 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
3855 "Unable to create back-channel pipes - %s.", strerror(errno
));
3857 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
3858 "Job stopped because the scheduler could not create the "
3859 "back-channel pipes.");
3861 cupsdClosePipe(job
->status_pipes
);
3862 cupsdStatBufDelete(job
->status_buffer
);
3863 job
->status_buffer
= NULL
;
3865 cupsdDestroyProfile(job
->profile
);
3866 job
->profile
= NULL
;
3870 fcntl(job
->back_pipes
[0], F_SETFL
,
3871 fcntl(job
->back_pipes
[0], F_GETFL
) | O_NONBLOCK
);
3872 fcntl(job
->back_pipes
[1], F_SETFL
,
3873 fcntl(job
->back_pipes
[1], F_GETFL
) | O_NONBLOCK
);
3876 * Create the side-channel pipes and make them non-blocking...
3879 if (socketpair(AF_LOCAL
, SOCK_STREAM
, 0, job
->side_pipes
))
3881 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
3882 "Unable to create side-channel pipes - %s.", strerror(errno
));
3884 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
3885 "Job stopped because the scheduler could not create the "
3886 "side-channel pipes.");
3888 cupsdClosePipe(job
->back_pipes
);
3890 cupsdClosePipe(job
->status_pipes
);
3891 cupsdStatBufDelete(job
->status_buffer
);
3892 job
->status_buffer
= NULL
;
3894 cupsdDestroyProfile(job
->profile
);
3895 job
->profile
= NULL
;
3899 fcntl(job
->side_pipes
[0], F_SETFL
,
3900 fcntl(job
->side_pipes
[0], F_GETFL
) | O_NONBLOCK
);
3901 fcntl(job
->side_pipes
[1], F_SETFL
,
3902 fcntl(job
->side_pipes
[1], F_GETFL
) | O_NONBLOCK
);
3904 fcntl(job
->side_pipes
[0], F_SETFD
,
3905 fcntl(job
->side_pipes
[0], F_GETFD
) | FD_CLOEXEC
);
3906 fcntl(job
->side_pipes
[1], F_SETFD
,
3907 fcntl(job
->side_pipes
[1], F_GETFD
) | FD_CLOEXEC
);
3910 * Now start the first file in the job...
3913 cupsdContinueJob(job
);
3918 * 'stop_job()' - Stop a print job.
3922 stop_job(cupsd_job_t
*job
, /* I - Job */
3923 cupsd_jobaction_t action
) /* I - Action */
3925 int i
; /* Looping var */
3928 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "stop_job(job=%p(%d), action=%d)", job
,
3931 FilterLevel
-= job
->cost
;
3934 if (action
== CUPSD_JOB_DEFAULT
&& !job
->kill_time
)
3935 job
->kill_time
= time(NULL
) + JobKillDelay
;
3936 else if (action
>= CUPSD_JOB_FORCE
)
3939 for (i
= 0; job
->filters
[i
]; i
++)
3940 if (job
->filters
[i
] > 0)
3942 cupsdEndProcess(job
->filters
[i
], action
>= CUPSD_JOB_FORCE
);
3944 if (action
>= CUPSD_JOB_FORCE
)
3945 job
->filters
[i
] = -job
->filters
[i
];
3948 if (job
->backend
> 0)
3950 cupsdEndProcess(job
->backend
, action
>= CUPSD_JOB_FORCE
);
3952 if (action
>= CUPSD_JOB_FORCE
)
3953 job
->backend
= -job
->backend
;
3956 if (action
>= CUPSD_JOB_FORCE
)
3959 * Clear job status...
3968 * 'unload_job()' - Unload a job from memory.
3972 unload_job(cupsd_job_t
*job
) /* I - Job */
3977 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Unloading...", job
->id
);
3979 ippDelete(job
->attrs
);
3984 job
->job_sheets
= NULL
;
3985 job
->printer_message
= NULL
;
3986 job
->printer_reasons
= NULL
;
3991 * 'update_job()' - Read a status update from a job's filters.
3995 update_job(cupsd_job_t
*job
) /* I - Job to check */
3997 int i
; /* Looping var */
3998 int copies
; /* Number of copies printed */
3999 char message
[CUPSD_SB_BUFFER_SIZE
],
4001 *ptr
; /* Pointer update... */
4002 int loglevel
, /* Log level for message */
4003 event
= 0; /* Events? */
4004 static const char * const levels
[] = /* Log levels */
4020 * Get the printer associated with this job; if the printer is stopped for
4021 * any reason then job->printer will be reset to NULL, so make sure we have
4022 * a valid pointer...
4025 while ((ptr
= cupsdStatBufUpdate(job
->status_buffer
, &loglevel
,
4026 message
, sizeof(message
))) != NULL
)
4029 * Process page and printer state messages as needed...
4032 if (loglevel
== CUPSD_LOG_PAGE
)
4035 * Page message; send the message to the page_log file and update the
4036 * job sheet count...
4039 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "PAGE: %s", message
);
4043 if (!strncasecmp(message
, "total ", 6))
4046 * Got a total count of pages from a backend or filter...
4049 copies
= atoi(message
+ 6);
4050 copies
-= job
->sheets
->values
[0].integer
; /* Just track the delta */
4052 else if (!sscanf(message
, "%*d%d", &copies
))
4055 job
->sheets
->values
[0].integer
+= copies
;
4057 if (job
->printer
->page_limit
)
4059 cupsd_quota_t
*q
= cupsdUpdateQuota(job
->printer
, job
->username
,
4063 if (AppleQuotas
&& q
->page_count
== -3)
4066 * Quota limit exceeded, cancel job in progress immediately...
4069 cupsdSetJobState(job
, IPP_JOB_CANCELED
, CUPSD_JOB_DEFAULT
,
4070 "Canceled job because pages exceed user %s "
4071 "quota limit on printer %s (%s).",
4072 job
->username
, job
->printer
->name
,
4073 job
->printer
->info
);
4078 #endif /* __APPLE__ */
4082 cupsdLogPage(job
, message
);
4085 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
4086 "Printed %d page(s).", job
->sheets
->values
[0].integer
);
4088 else if (loglevel
== CUPSD_LOG_STATE
)
4090 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "STATE: %s", message
);
4092 if (!strcmp(message
, "paused"))
4094 cupsdStopPrinter(job
->printer
, 1);
4097 else if (cupsdSetPrinterReasons(job
->printer
, message
))
4099 cupsdAddPrinterHistory(job
->printer
);
4100 event
|= CUPSD_EVENT_PRINTER_STATE
;
4103 update_job_attrs(job
, 0);
4105 else if (loglevel
== CUPSD_LOG_ATTR
)
4108 * Set attribute(s)...
4111 int num_attrs
; /* Number of attributes */
4112 cups_option_t
*attrs
; /* Attributes */
4113 const char *attr
; /* Attribute */
4116 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "ATTR: %s", message
);
4118 num_attrs
= cupsParseOptions(message
, 0, &attrs
);
4120 if ((attr
= cupsGetOption("auth-info-required", num_attrs
,
4123 cupsdSetAuthInfoRequired(job
->printer
, attr
, NULL
);
4124 cupsdSetPrinterAttrs(job
->printer
);
4126 if (job
->printer
->type
& CUPS_PRINTER_DISCOVERED
)
4127 cupsdMarkDirty(CUPSD_DIRTY_REMOTE
);
4129 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4132 if ((attr
= cupsGetOption("job-media-progress", num_attrs
,
4135 int progress
= atoi(attr
);
4138 if (progress
>= 0 && progress
<= 100)
4140 job
->progress
= progress
;
4143 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
4144 "Printing page %d, %d%%",
4145 job
->sheets
->values
[0].integer
, job
->progress
);
4149 if ((attr
= cupsGetOption("printer-alert", num_attrs
, attrs
)) != NULL
)
4151 cupsdSetString(&job
->printer
->alert
, attr
);
4152 event
|= CUPSD_EVENT_PRINTER_STATE
;
4155 if ((attr
= cupsGetOption("printer-alert-description", num_attrs
,
4158 cupsdSetString(&job
->printer
->alert_description
, attr
);
4159 event
|= CUPSD_EVENT_PRINTER_STATE
;
4162 if ((attr
= cupsGetOption("marker-colors", num_attrs
, attrs
)) != NULL
)
4164 cupsdSetPrinterAttr(job
->printer
, "marker-colors", (char *)attr
);
4165 job
->printer
->marker_time
= time(NULL
);
4166 event
|= CUPSD_EVENT_PRINTER_STATE
;
4167 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4170 if ((attr
= cupsGetOption("marker-levels", num_attrs
, attrs
)) != NULL
)
4172 cupsdSetPrinterAttr(job
->printer
, "marker-levels", (char *)attr
);
4173 job
->printer
->marker_time
= time(NULL
);
4174 event
|= CUPSD_EVENT_PRINTER_STATE
;
4175 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4178 if ((attr
= cupsGetOption("marker-low-levels", num_attrs
, attrs
)) != NULL
)
4180 cupsdSetPrinterAttr(job
->printer
, "marker-low-levels", (char *)attr
);
4181 job
->printer
->marker_time
= time(NULL
);
4182 event
|= CUPSD_EVENT_PRINTER_STATE
;
4183 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4186 if ((attr
= cupsGetOption("marker-high-levels", num_attrs
, attrs
)) != NULL
)
4188 cupsdSetPrinterAttr(job
->printer
, "marker-high-levels", (char *)attr
);
4189 job
->printer
->marker_time
= time(NULL
);
4190 event
|= CUPSD_EVENT_PRINTER_STATE
;
4191 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4194 if ((attr
= cupsGetOption("marker-message", num_attrs
, attrs
)) != NULL
)
4196 cupsdSetPrinterAttr(job
->printer
, "marker-message", (char *)attr
);
4197 job
->printer
->marker_time
= time(NULL
);
4198 event
|= CUPSD_EVENT_PRINTER_STATE
;
4199 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4202 if ((attr
= cupsGetOption("marker-names", num_attrs
, attrs
)) != NULL
)
4204 cupsdSetPrinterAttr(job
->printer
, "marker-names", (char *)attr
);
4205 job
->printer
->marker_time
= time(NULL
);
4206 event
|= CUPSD_EVENT_PRINTER_STATE
;
4207 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4210 if ((attr
= cupsGetOption("marker-types", num_attrs
, attrs
)) != NULL
)
4212 cupsdSetPrinterAttr(job
->printer
, "marker-types", (char *)attr
);
4213 job
->printer
->marker_time
= time(NULL
);
4214 event
|= CUPSD_EVENT_PRINTER_STATE
;
4215 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4218 cupsFreeOptions(num_attrs
, attrs
);
4220 else if (loglevel
== CUPSD_LOG_PPD
)
4223 * Set attribute(s)...
4226 int num_keywords
; /* Number of keywords */
4227 cups_option_t
*keywords
; /* Keywords */
4230 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "PPD: %s", message
);
4232 num_keywords
= cupsParseOptions(message
, 0, &keywords
);
4234 if (cupsdUpdatePrinterPPD(job
->printer
, num_keywords
, keywords
))
4235 cupsdSetPrinterAttrs(job
->printer
);
4237 cupsFreeOptions(num_keywords
, keywords
);
4242 * Strip legacy message prefix...
4245 if (!strncmp(message
, "recoverable:", 12))
4248 while (isspace(*ptr
& 255))
4251 else if (!strncmp(message
, "recovered:", 10))
4254 while (isspace(*ptr
& 255))
4260 cupsdLogJob(job
, loglevel
, "%s", ptr
);
4262 if (loglevel
< CUPSD_LOG_DEBUG
)
4264 strlcpy(job
->printer
->state_message
, ptr
,
4265 sizeof(job
->printer
->state_message
));
4266 cupsdAddPrinterHistory(job
->printer
);
4268 event
|= CUPSD_EVENT_PRINTER_STATE
| CUPSD_EVENT_JOB_PROGRESS
;
4270 if (loglevel
< job
->status_level
)
4273 * Some messages show in the job-printer-state-message attribute...
4276 if (loglevel
!= CUPSD_LOG_NOTICE
)
4277 job
->status_level
= loglevel
;
4279 update_job_attrs(job
, 1);
4281 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
4282 "Set job-printer-state-message to \"%s\", "
4284 job
->printer_message
->values
[0].string
.text
,
4285 levels
[job
->status_level
]);
4290 if (!strchr(job
->status_buffer
->buffer
, '\n'))
4294 if (event
& CUPSD_EVENT_JOB_PROGRESS
)
4295 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
4296 "%s", job
->printer
->state_message
);
4297 else if (event
& CUPSD_EVENT_PRINTER_STATE
)
4298 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, job
->printer
, NULL
,
4299 (job
->printer
->type
& CUPS_PRINTER_CLASS
) ?
4300 "Class \"%s\" state changed." :
4301 "Printer \"%s\" state changed.",
4302 job
->printer
->name
);
4305 if (ptr
== NULL
&& !job
->status_buffer
->bufused
)
4308 * See if all of the filters and the backend have returned their
4312 for (i
= 0; job
->filters
[i
] < 0; i
++);
4314 if (job
->filters
[i
])
4317 * EOF but we haven't collected the exit status of all filters...
4320 cupsdCheckProcess();
4324 if (job
->current_file
>= job
->num_files
&& job
->backend
> 0)
4327 * EOF but we haven't collected the exit status of the backend...
4330 cupsdCheckProcess();
4335 * Handle the end of job stuff...
4338 finalize_job(job
, 1);
4341 * Check for new jobs...
4350 * 'update_job_attrs()' - Update the job-printer-* attributes.
4354 update_job_attrs(cupsd_job_t
*job
, /* I - Job to update */
4355 int do_message
)/* I - 1 = copy job-printer-state message */
4357 int i
; /* Looping var */
4358 int num_reasons
; /* Actual number of reasons */
4359 const char * const *reasons
; /* Reasons */
4360 static const char *none
= "none"; /* "none" reason */
4364 * Get/create the job-printer-state-* attributes...
4367 if (!job
->printer_message
)
4369 if ((job
->printer_message
= ippFindAttribute(job
->attrs
,
4370 "job-printer-state-message",
4371 IPP_TAG_TEXT
)) == NULL
)
4372 job
->printer_message
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_TEXT
,
4373 "job-printer-state-message",
4377 if (!job
->printer_reasons
)
4378 job
->printer_reasons
= ippFindAttribute(job
->attrs
,
4379 "job-printer-state-reasons",
4383 * Copy or clear the printer-state-message value as needed...
4386 if (job
->state_value
!= IPP_JOB_PROCESSING
&&
4387 job
->status_level
== CUPSD_LOG_INFO
)
4388 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
), "");
4389 else if (job
->printer
->state_message
[0] && do_message
)
4390 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
),
4391 job
->printer
->state_message
);
4394 * ... and the printer-state-reasons value...
4397 if (job
->printer
->num_reasons
== 0)
4404 num_reasons
= job
->printer
->num_reasons
;
4405 reasons
= (const char * const *)job
->printer
->reasons
;
4408 if (!job
->printer_reasons
|| job
->printer_reasons
->num_values
!= num_reasons
)
4411 * Replace/create a job-printer-state-reasons attribute...
4414 ippDeleteAttribute(job
->attrs
, job
->printer_reasons
);
4416 job
->printer_reasons
= ippAddStrings(job
->attrs
,
4417 IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
4418 "job-printer-state-reasons",
4419 num_reasons
, NULL
, NULL
);
4424 * Don't bother clearing the reason strings if they are the same...
4427 for (i
= 0; i
< num_reasons
; i
++)
4428 if (strcmp(job
->printer_reasons
->values
[i
].string
.text
, reasons
[i
]))
4431 if (i
>= num_reasons
)
4435 * Not the same, so free the current strings...
4438 for (i
= 0; i
< num_reasons
; i
++)
4439 _cupsStrFree(job
->printer_reasons
->values
[i
].string
.text
);
4443 * Copy the reasons...
4446 for (i
= 0; i
< num_reasons
; i
++)
4447 job
->printer_reasons
->values
[i
].string
.text
= _cupsStrAlloc(reasons
[i
]);
4452 * End of "$Id: job.c 7902 2008-09-03 14:20:17Z mike $".