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 * finalize_job() - Cleanup after job filter processes and support
50 * get_options() - Get a string containing the job options.
51 * ipp_length() - Compute the size of the buffer needed to hold
52 * the textual IPP attributes.
53 * load_job_cache() - Load jobs from the job.cache file.
54 * load_next_job_id() - Load the NextJobId value from the job.cache
56 * load_request_root() - Load jobs from the RequestRoot directory.
57 * set_hold_until() - Set the hold time and update job-hold-until
59 * set_time() - Set one of the "time-at-xyz" attributes.
60 * start_job() - Start a print job.
61 * stop_job() - Stop a print job.
62 * unload_job() - Unload a job from memory.
63 * update_job() - Read a status update from a job's filters.
64 * update_job_attrs() - Update the job-printer-* attributes.
68 * Include necessary headers...
73 #include <cups/backend.h>
78 * Design Notes for Job Management
79 * -------------------------------
83 * pending Do nothing/check jobs
84 * pending-held Send SIGTERM to filters and backend
85 * processing Do nothing/start job
86 * stopped Send SIGKILL to filters and backend
87 * canceled Send SIGTERM to filters and backend
91 * Finalize clears the printer <-> job association, deletes the status
92 * buffer, closes all of the pipes, etc. and doesn't get run until all of
93 * the print processes are finished.
95 * UNLOADING OF JOBS (cupsdUnloadCompletedJobs)
97 * We unload the job attributes when they are not needed to reduce overall
98 * memory consumption. We don't unload jobs where job->state_value <
99 * IPP_JOB_STOPPED, job->printer != NULL, or job->access_time is recent.
101 * STARTING OF JOBS (start_job)
103 * When a job is started, a status buffer, several pipes, a security
104 * profile, and a backend process are created for the life of that job.
105 * These are shared for every file in a job. For remote print jobs, the
106 * IPP backend is provided with every file in the job and no filters are
109 * The job->printer member tracks which printer is printing a job, which
110 * can be different than the destination in job->dest for classes. The
111 * printer object also has a job pointer to track which job is being
114 * PRINTING OF JOB FILES (cupsdContinueJob)
116 * Each file in a job is filtered by 0 or more programs. After getting the
117 * list of filters needed and the total cost, the job is either passed or
118 * put back to the processing state until the current FilterLevel comes down
119 * enough to allow printing.
121 * If we can print, we build a string for the print options and run each of
122 * the filters, piping the output from one into the next.
124 * JOB STATUS UPDATES (update_job)
126 * The update_job function gets called whenever there are pending messages
127 * on the status pipe. These generally are updates to the marker-*,
128 * printer-state-message, or printer-state-reasons attributes. On EOF,
129 * finalize_job is called to clean up.
131 * FINALIZING JOBS (finalize_job)
133 * When all filters and the backend are done, we set the job state to
134 * completed (no errors), aborted (filter errors or abort-job policy),
135 * pending-held (auth required or retry-job policy), or pending
136 * (retry-current-job or stop-printer policies) as appropriate.
138 * Then we close the pipes and free the status buffers and profiles.
140 * JOB FILE COMPLETION (process_children in main.c)
142 * For multiple-file jobs, process_children (in main.c) sees that all
143 * filters have exited and calls in to print the next file if there are
144 * more files in the job, otherwise it waits for the backend to exit and
145 * update_job to do the cleanup.
153 static mime_filter_t gziptoany_filter
=
155 NULL
, /* Source type */
156 NULL
, /* Destination type */
158 "gziptoany" /* Filter program to run */
166 static int compare_active_jobs(void *first
, void *second
, void *data
);
167 static int compare_jobs(void *first
, void *second
, void *data
);
168 static void finalize_job(cupsd_job_t
*job
);
169 static char *get_options(cupsd_job_t
*job
, int banner_page
, char *copies
,
170 size_t copies_size
, char *title
,
172 static int ipp_length(ipp_t
*ipp
);
173 static void load_job_cache(const char *filename
);
174 static void load_next_job_id(const char *filename
);
175 static void load_request_root(void);
176 static void set_hold_until(cupsd_job_t
*job
, time_t holdtime
);
177 static void set_time(cupsd_job_t
*job
, const char *name
);
178 static void start_job(cupsd_job_t
*job
, cupsd_printer_t
*printer
);
179 static void stop_job(cupsd_job_t
*job
, cupsd_jobaction_t action
);
180 static void unload_job(cupsd_job_t
*job
);
181 static void update_job(cupsd_job_t
*job
);
182 static void update_job_attrs(cupsd_job_t
*job
, int do_message
);
186 * 'cupsdAddJob()' - Add a new job to the job queue.
189 cupsd_job_t
* /* O - New job record */
190 cupsdAddJob(int priority
, /* I - Job priority */
191 const char *dest
) /* I - Job destination */
193 cupsd_job_t
*job
; /* New job record */
196 if ((job
= calloc(sizeof(cupsd_job_t
), 1)) == NULL
)
199 job
->id
= NextJobId
++;
200 job
->priority
= priority
;
201 job
->back_pipes
[0] = -1;
202 job
->back_pipes
[1] = -1;
203 job
->print_pipes
[0] = -1;
204 job
->print_pipes
[1] = -1;
205 job
->side_pipes
[0] = -1;
206 job
->side_pipes
[1] = -1;
207 job
->status_pipes
[0] = -1;
208 job
->status_pipes
[1] = -1;
210 cupsdSetString(&job
->dest
, dest
);
213 * Add the new job to the "all jobs" and "active jobs" lists...
216 cupsArrayAdd(Jobs
, job
);
217 cupsArrayAdd(ActiveJobs
, job
);
224 * 'cupsdCancelJobs()' - Cancel all jobs for the given destination/user.
228 cupsdCancelJobs(const char *dest
, /* I - Destination to cancel */
229 const char *username
, /* I - Username or NULL */
230 int purge
) /* I - Purge jobs? */
232 cupsd_job_t
*job
; /* Current job */
235 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
237 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
239 if ((!job
->dest
|| !job
->username
) && !cupsdLoadJob(job
))
242 if ((!dest
|| !strcmp(job
->dest
, dest
)) &&
243 (!username
|| !strcmp(job
->username
, username
)))
246 * Cancel all jobs matching this destination/user...
250 cupsdSetJobState(job
, IPP_JOB_CANCELED
, CUPSD_JOB_PURGE
,
251 "Job purged by user.");
253 cupsdSetJobState(job
, IPP_JOB_CANCELED
, CUPSD_JOB_DEFAULT
,
254 "Job canceled by user.");
263 * 'cupsdCheckJobs()' - Check the pending jobs and start any if the destination
270 cupsd_job_t
*job
; /* Current job in queue */
271 cupsd_printer_t
*printer
, /* Printer destination */
272 *pclass
; /* Printer class destination */
273 ipp_attribute_t
*attr
; /* Job attribute */
274 time_t curtime
; /* Current time */
277 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
278 "cupsdCheckJobs: %d active jobs, sleeping=%d, reload=%d",
279 cupsArrayCount(ActiveJobs
), Sleeping
, NeedReload
);
281 curtime
= time(NULL
);
283 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
285 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
288 * Kill jobs if they are unresponsive...
291 if (job
->kill_time
&& job
->kill_time
<= curtime
)
293 stop_job(job
, CUPSD_JOB_FORCE
);
298 * Start held jobs if they are ready...
301 if (job
->state_value
== IPP_JOB_HELD
&&
303 job
->hold_until
< curtime
)
305 if (job
->pending_timeout
)
308 * This job is pending; check that we don't have an active Send-Document
309 * operation in progress on any of the client connections, then timeout
310 * the job so we can start printing...
313 cupsd_client_t
*con
; /* Current client connection */
316 for (con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
318 con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
320 con
->request
->request
.op
.operation_id
== IPP_SEND_DOCUMENT
)
326 if (cupsdTimeoutJob(job
))
330 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
331 "Job submission timed out.");
335 * Continue jobs that are waiting on the FilterLimit...
338 if (job
->pending_cost
> 0 &&
339 ((FilterLevel
+ job
->pending_cost
) < FilterLimit
|| FilterLevel
== 0))
340 cupsdContinueJob(job
);
343 * Start pending jobs if the destination is available...
346 if (job
->state_value
== IPP_JOB_PENDING
&& !NeedReload
&& !Sleeping
&&
349 printer
= cupsdFindDest(job
->dest
);
353 (printer
->type
& (CUPS_PRINTER_IMPLICIT
| CUPS_PRINTER_CLASS
)))
356 * If the class is remote, just pass it to the remote server...
361 if (pclass
->state
== IPP_PRINTER_STOPPED
)
363 else if (pclass
->type
& CUPS_PRINTER_REMOTE
)
366 printer
= cupsdFindAvailablePrinter(printer
->name
);
369 if (!printer
&& !pclass
)
372 * Whoa, the printer and/or class for this destination went away;
376 cupsdSetJobState(job
, IPP_JOB_ABORTED
, CUPSD_JOB_PURGE
,
377 "Job aborted because the destination printer/class "
380 else if (printer
&& !printer
->holding_new_jobs
)
383 * See if the printer is available or remote and not printing a job;
384 * if so, start the job...
390 * Add/update a job-actual-printer-uri attribute for this job
391 * so that we know which printer actually printed the job...
394 if ((attr
= ippFindAttribute(job
->attrs
, "job-actual-printer-uri",
395 IPP_TAG_URI
)) != NULL
)
396 cupsdSetString(&attr
->values
[0].string
.text
, printer
->uri
);
398 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
,
399 "job-actual-printer-uri", NULL
, printer
->uri
);
402 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
405 if ((!(printer
->type
& CUPS_PRINTER_DISCOVERED
) && /* Printer is local */
406 printer
->state
== IPP_PRINTER_IDLE
) || /* and idle, OR */
407 ((printer
->type
& CUPS_PRINTER_DISCOVERED
) && /* Printer is remote */
408 !printer
->job
)) /* and not printing */
414 start_job(job
, printer
);
423 * 'cupsdCleanJobs()' - Clean out old jobs.
429 cupsd_job_t
*job
; /* Current job */
435 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
436 job
&& cupsArrayCount(Jobs
) >= MaxJobs
;
437 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
438 if (job
->state_value
>= IPP_JOB_CANCELED
&& !job
->printer
)
439 cupsdDeleteJob(job
, CUPSD_JOB_PURGE
);
444 * 'cupsdContinueJob()' - Continue printing with the next file in a job.
448 cupsdContinueJob(cupsd_job_t
*job
) /* I - Job */
450 int i
; /* Looping var */
451 int slot
; /* Pipe slot */
452 cups_array_t
*filters
= NULL
,/* Filters for job */
453 *prefilters
; /* Filters with prefilters */
454 mime_filter_t
*filter
, /* Current filter */
455 *prefilter
, /* Prefilter */
456 port_monitor
; /* Port monitor filter */
457 char scheme
[255]; /* Device URI scheme */
458 ipp_attribute_t
*attr
; /* Current attribute */
459 const char *ptr
, /* Pointer into value */
460 *abort_message
; /* Abort message */
461 ipp_jstate_t abort_state
= IPP_JOB_STOPPED
;
462 /* New job state on abort */
463 struct stat backinfo
; /* Backend file information */
464 int backroot
; /* Run backend as root? */
465 int pid
; /* Process ID of new filter process */
466 int banner_page
; /* 1 if banner page, 0 otherwise */
467 int filterfds
[2][2] = { { -1, -1 }, { -1, -1 } };
468 /* Pipes used between filters */
469 int envc
; /* Number of environment variables */
470 char **argv
= NULL
, /* Filter command-line arguments */
471 filename
[1024], /* Job filename */
472 command
[1024], /* Full path to command */
473 jobid
[255], /* Job ID string */
475 /* Job title string */
476 copies
[255], /* # copies string */
477 *options
, /* Options string */
479 /* Environment variables */
480 charset
[255], /* CHARSET env variable */
481 class_name
[255],/* CLASS env variable */
482 classification
[1024],
483 /* CLASSIFICATION env variable */
485 /* CONTENT_TYPE env variable */
487 /* DEVICE_URI env variable */
488 final_content_type
[1024],
489 /* FINAL_CONTENT_TYPE env variable */
490 lang
[255], /* LANG env variable */
493 /* APPLE_LANGUAGE env variable */
494 #endif /* __APPLE__ */
495 ppd
[1024], /* PPD env variable */
497 /* PRINTER_INFO env variable */
498 printer_location
[255],
499 /* PRINTER_LOCATION env variable */
501 /* PRINTER env variable */
503 /* RIP_MAX_CACHE env variable */
506 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
507 "cupsdContinueJob(job=%p(%d)): current_file=%d, num_files=%d",
508 job
, job
->id
, job
->current_file
, job
->num_files
);
511 * Figure out what filters are required to convert from
512 * the source to the destination type...
515 FilterLevel
-= job
->cost
;
518 job
->pending_cost
= 0;
520 memset(job
->filters
, 0, sizeof(job
->filters
));
523 if (job
->printer
->raw
)
526 * Remote jobs and raw queues go directly to the printer without
530 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Sending job to queue tagged as raw...");
535 * Local jobs get filtered...
538 filters
= mimeFilter(MimeDatabase
, job
->filetypes
[job
->current_file
],
539 job
->printer
->filetype
, &(job
->cost
));
543 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
544 "Unable to convert file %d to printable format!",
547 abort_message
= "Aborting job because it cannot be printed.";
548 abort_state
= IPP_JOB_ABORTED
;
554 * Remove NULL ("-") filters...
557 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
559 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
560 if (!strcmp(filter
->filter
, "-"))
561 cupsArrayRemove(filters
, filter
);
563 if (cupsArrayCount(filters
) == 0)
565 cupsArrayDelete(filters
);
570 * If this printer has any pre-filters, insert the required pre-filter
571 * in the filters array...
574 if (job
->printer
->prefiltertype
&& filters
)
576 prefilters
= cupsArrayNew(NULL
, NULL
);
578 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
580 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
582 if ((prefilter
= mimeFilterLookup(MimeDatabase
, filter
->src
,
583 job
->printer
->prefiltertype
)))
585 cupsArrayAdd(prefilters
, prefilter
);
586 job
->cost
+= prefilter
->cost
;
589 cupsArrayAdd(prefilters
, filter
);
592 cupsArrayDelete(filters
);
593 filters
= prefilters
;
598 * Set a minimum cost of 100 for all jobs so that FilterLimit
599 * works with raw queues and other low-cost paths.
606 * See if the filter cost is too high...
609 if ((FilterLevel
+ job
->cost
) > FilterLimit
&& FilterLevel
> 0 &&
613 * Don't print this job quite yet...
616 cupsArrayDelete(filters
);
618 cupsdLogJob(job
, CUPSD_LOG_INFO
,
619 "Holding because filter limit has been reached.");
620 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
621 "cupsdContinueJob: file=%d, cost=%d, level=%d, limit=%d",
622 job
->current_file
, job
->cost
, FilterLevel
,
625 job
->pending_cost
= job
->cost
;
630 FilterLevel
+= job
->cost
;
633 * Add decompression/raw filter as needed...
636 if ((!job
->printer
->raw
&& job
->compressions
[job
->current_file
]) ||
637 (!filters
&& !job
->printer
->remote
&&
638 (job
->num_files
> 1 || !strncmp(job
->printer
->device_uri
, "file:", 5))))
641 * Add gziptoany filter to the front of the list...
645 filters
= cupsArrayNew(NULL
, NULL
);
647 if (!cupsArrayInsert(filters
, &gziptoany_filter
))
649 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
650 "Unable to add decompression filter - %s", strerror(errno
));
652 cupsArrayDelete(filters
);
654 abort_message
= "Stopping job because the scheduler ran out of memory.";
661 * Add port monitor, if any...
664 if (job
->printer
->port_monitor
)
667 * Add port monitor to the end of the list...
671 filters
= cupsArrayNew(NULL
, NULL
);
673 port_monitor
.src
= NULL
;
674 port_monitor
.dst
= NULL
;
675 port_monitor
.cost
= 0;
677 snprintf(port_monitor
.filter
, sizeof(port_monitor
.filter
),
678 "%s/monitor/%s", ServerBin
, job
->printer
->port_monitor
);
680 if (!cupsArrayAdd(filters
, &port_monitor
))
682 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
683 "Unable to add port monitor - %s", strerror(errno
));
685 abort_message
= "Stopping job because the scheduler ran out of memory.";
692 * Make sure we don't go over the "MAX_FILTERS" limit...
695 if (cupsArrayCount(filters
) > MAX_FILTERS
)
697 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
698 "Too many filters (%d > %d), unable to print!",
699 cupsArrayCount(filters
), MAX_FILTERS
);
701 abort_message
= "Aborting job because it needs too many filters to print.";
702 abort_state
= IPP_JOB_ABORTED
;
708 * Determine if we are printing a banner page or not...
711 if (job
->job_sheets
== NULL
)
713 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "No job-sheets attribute.");
714 if ((job
->job_sheets
=
715 ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_ZERO
)) != NULL
)
716 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
717 "... but someone added one without setting job_sheets!");
719 else if (job
->job_sheets
->num_values
== 1)
720 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "job-sheets=%s",
721 job
->job_sheets
->values
[0].string
.text
);
723 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "job-sheets=%s,%s",
724 job
->job_sheets
->values
[0].string
.text
,
725 job
->job_sheets
->values
[1].string
.text
);
727 if (job
->printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
))
729 else if (job
->job_sheets
== NULL
)
731 else if (strcasecmp(job
->job_sheets
->values
[0].string
.text
, "none") != 0 &&
732 job
->current_file
== 0)
734 else if (job
->job_sheets
->num_values
> 1 &&
735 strcasecmp(job
->job_sheets
->values
[1].string
.text
, "none") != 0 &&
736 job
->current_file
== (job
->num_files
- 1))
741 if ((options
= get_options(job
, banner_page
, copies
, sizeof(copies
), title
,
742 sizeof(title
))) == NULL
)
744 abort_message
= "Stopping job because the scheduler ran out of memory.";
750 * Build the command-line arguments for the filters. Each filter
751 * has 6 or 7 arguments:
759 * argv[6] = filename (optional; normally stdin)
761 * This allows legacy printer drivers that use the old System V
762 * printing interface to be used by CUPS.
764 * For remote jobs, we send all of the files in the argument list.
767 if (job
->printer
->remote
)
768 argv
= calloc(7 + job
->num_files
, sizeof(char *));
770 argv
= calloc(8, sizeof(char *));
774 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Unable to allocate argument array - %s",
777 abort_message
= "Stopping job because the scheduler ran out of memory.";
782 sprintf(jobid
, "%d", job
->id
);
784 argv
[0] = job
->printer
->name
;
786 argv
[2] = job
->username
;
791 if (job
->printer
->remote
&& job
->num_files
> 1)
793 for (i
= 0; i
< job
->num_files
; i
++)
795 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
797 argv
[6 + i
] = strdup(filename
);
802 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
803 job
->id
, job
->current_file
+ 1);
807 for (i
= 0; argv
[i
]; i
++)
808 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "argv[%d]=\"%s\"", i
, argv
[i
]);
811 * Create environment variable strings for the filters...
814 attr
= ippFindAttribute(job
->attrs
, "attributes-natural-language",
818 strcpy(apple_language
, "APPLE_LANGUAGE=");
819 _cupsAppleLanguage(attr
->values
[0].string
.text
,
820 apple_language
+ 15, sizeof(apple_language
) - 15);
821 #endif /* __APPLE__ */
823 switch (strlen(attr
->values
[0].string
.text
))
827 * This is an unknown or badly formatted language code; use
828 * the POSIX locale...
831 strcpy(lang
, "LANG=C");
836 * Just the language code (ll)...
839 snprintf(lang
, sizeof(lang
), "LANG=%s.UTF-8",
840 attr
->values
[0].string
.text
);
845 * Language and country code (ll-cc)...
848 snprintf(lang
, sizeof(lang
), "LANG=%c%c_%c%c.UTF-8",
849 attr
->values
[0].string
.text
[0],
850 attr
->values
[0].string
.text
[1],
851 toupper(attr
->values
[0].string
.text
[3] & 255),
852 toupper(attr
->values
[0].string
.text
[4] & 255));
856 if ((attr
= ippFindAttribute(job
->attrs
, "document-format",
857 IPP_TAG_MIMETYPE
)) != NULL
&&
858 (ptr
= strstr(attr
->values
[0].string
.text
, "charset=")) != NULL
)
859 snprintf(charset
, sizeof(charset
), "CHARSET=%s", ptr
+ 8);
861 strlcpy(charset
, "CHARSET=utf-8", sizeof(charset
));
863 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s/%s",
864 job
->filetypes
[job
->current_file
]->super
,
865 job
->filetypes
[job
->current_file
]->type
);
866 snprintf(device_uri
, sizeof(device_uri
), "DEVICE_URI=%s",
867 job
->printer
->device_uri
);
868 snprintf(ppd
, sizeof(ppd
), "PPD=%s/ppd/%s.ppd", ServerRoot
,
870 snprintf(printer_info
, sizeof(printer_name
), "PRINTER_INFO=%s",
871 job
->printer
->info
? job
->printer
->info
: "");
872 snprintf(printer_location
, sizeof(printer_name
), "PRINTER_LOCATION=%s",
873 job
->printer
->location
? job
->printer
->location
: "");
874 snprintf(printer_name
, sizeof(printer_name
), "PRINTER=%s", job
->printer
->name
);
875 snprintf(rip_max_cache
, sizeof(rip_max_cache
), "RIP_MAX_CACHE=%s", RIPCache
);
877 envc
= cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
879 envp
[envc
++] = charset
;
880 envp
[envc
++] = lang
;
882 envp
[envc
++] = apple_language
;
883 #endif /* __APPLE__ */
885 envp
[envc
++] = rip_max_cache
;
886 envp
[envc
++] = content_type
;
887 envp
[envc
++] = device_uri
;
888 envp
[envc
++] = printer_info
;
889 envp
[envc
++] = printer_location
;
890 envp
[envc
++] = printer_name
;
891 envp
[envc
++] = banner_page
? "CUPS_FILETYPE=job-sheet" :
892 "CUPS_FILETYPE=document";
894 if (!job
->printer
->remote
&& !job
->printer
->raw
)
896 filter
= (mime_filter_t
*)cupsArrayLast(filters
);
898 if (job
->printer
->port_monitor
)
899 filter
= (mime_filter_t
*)cupsArrayPrev(filters
);
901 if (filter
&& filter
->dst
)
903 snprintf(final_content_type
, sizeof(final_content_type
),
904 "FINAL_CONTENT_TYPE=%s/%s",
905 filter
->dst
->super
, filter
->dst
->type
);
906 envp
[envc
++] = final_content_type
;
910 if (Classification
&& !banner_page
)
912 if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
913 IPP_TAG_NAME
)) == NULL
)
914 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
916 else if (attr
->num_values
> 1 &&
917 strcmp(attr
->values
[1].string
.text
, "none") != 0)
918 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
919 attr
->values
[1].string
.text
);
921 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
922 attr
->values
[0].string
.text
);
924 envp
[envc
++] = classification
;
927 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
929 snprintf(class_name
, sizeof(class_name
), "CLASS=%s", job
->dest
);
930 envp
[envc
++] = class_name
;
933 if (job
->auth_username
)
934 envp
[envc
++] = job
->auth_username
;
935 if (job
->auth_domain
)
936 envp
[envc
++] = job
->auth_domain
;
937 if (job
->auth_password
)
938 envp
[envc
++] = job
->auth_password
;
942 envp
[envc
++] = job
->ccname
;
943 #endif /* HAVE_GSSAPI */
947 for (i
= 0; i
< envc
; i
++)
948 if (!strncmp(envp
[i
], "AUTH_", 5))
949 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"AUTH_%c****\"", i
,
951 else if (strncmp(envp
[i
], "DEVICE_URI=", 11))
952 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"%s\"", i
, envp
[i
]);
954 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"DEVICE_URI=%s\"", i
,
955 job
->printer
->sanitized_device_uri
);
957 if (job
->printer
->remote
)
958 job
->current_file
= job
->num_files
;
960 job
->current_file
++;
963 * Now create processes for all of the filters...
966 for (i
= 0, slot
= 0, filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
968 i
++, filter
= (mime_filter_t
*)cupsArrayNext(filters
))
970 if (filter
->filter
[0] != '/')
971 snprintf(command
, sizeof(command
), "%s/filter/%s", ServerBin
,
974 strlcpy(command
, filter
->filter
, sizeof(command
));
976 if (i
< (cupsArrayCount(filters
) - 1))
978 if (cupsdOpenPipe(filterfds
[slot
]))
980 abort_message
= "Stopping job because the scheduler could not create "
988 if (job
->current_file
== 1)
990 if (strncmp(job
->printer
->device_uri
, "file:", 5) != 0)
992 if (cupsdOpenPipe(job
->print_pipes
))
994 abort_message
= "Stopping job because the scheduler could not "
995 "create the backend pipes.";
1002 job
->print_pipes
[0] = -1;
1003 if (!strcmp(job
->printer
->device_uri
, "file:/dev/null") ||
1004 !strcmp(job
->printer
->device_uri
, "file:///dev/null"))
1005 job
->print_pipes
[1] = -1;
1008 if (!strncmp(job
->printer
->device_uri
, "file:/dev/", 10))
1009 job
->print_pipes
[1] = open(job
->printer
->device_uri
+ 5,
1011 else if (!strncmp(job
->printer
->device_uri
, "file:///dev/", 12))
1012 job
->print_pipes
[1] = open(job
->printer
->device_uri
+ 7,
1014 else if (!strncmp(job
->printer
->device_uri
, "file:///", 8))
1015 job
->print_pipes
[1] = open(job
->printer
->device_uri
+ 7,
1016 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
1018 job
->print_pipes
[1] = open(job
->printer
->device_uri
+ 5,
1019 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
1021 if (job
->print_pipes
[1] < 0)
1023 abort_message
= "Stopping job because the scheduler could not "
1024 "open the output file.";
1029 fcntl(job
->print_pipes
[1], F_SETFD
,
1030 fcntl(job
->print_pipes
[1], F_GETFD
) | FD_CLOEXEC
);
1035 filterfds
[slot
][0] = job
->print_pipes
[0];
1036 filterfds
[slot
][1] = job
->print_pipes
[1];
1039 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
1040 filterfds
[slot
][1], job
->status_pipes
[1],
1041 job
->back_pipes
[0], job
->side_pipes
[0], 0,
1042 job
->profile
, job
, job
->filters
+ i
);
1044 cupsdClosePipe(filterfds
[!slot
]);
1048 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Unable to start filter \"%s\" - %s.",
1049 filter
->filter
, strerror(errno
));
1051 abort_message
= "Stopping job because the scheduler could not execute a "
1057 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Started filter %s (PID %d)", command
,
1064 cupsArrayDelete(filters
);
1068 * Finally, pipe the final output into a backend process if needed...
1071 if (strncmp(job
->printer
->device_uri
, "file:", 5) != 0)
1073 if (job
->current_file
== 1 || job
->printer
->remote
)
1075 sscanf(job
->printer
->device_uri
, "%254[^:]", scheme
);
1076 snprintf(command
, sizeof(command
), "%s/backend/%s", ServerBin
, scheme
);
1079 * See if the backend needs to run as root...
1084 else if (stat(command
, &backinfo
))
1087 backroot
= !(backinfo
.st_mode
& (S_IRWXG
| S_IRWXO
));
1089 argv
[0] = job
->printer
->sanitized_device_uri
;
1091 filterfds
[slot
][0] = -1;
1092 filterfds
[slot
][1] = -1;
1094 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
1095 filterfds
[slot
][1], job
->status_pipes
[1],
1096 job
->back_pipes
[1], job
->side_pipes
[1],
1097 backroot
, job
->profile
, job
, &(job
->backend
));
1101 abort_message
= "Stopping job because the sheduler could not execute "
1108 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Started backend %s (PID %d)",
1113 if (job
->current_file
== job
->num_files
)
1115 cupsdClosePipe(job
->print_pipes
);
1116 cupsdClosePipe(job
->back_pipes
);
1117 cupsdClosePipe(job
->side_pipes
);
1119 close(job
->status_pipes
[1]);
1120 job
->status_pipes
[1] = -1;
1125 filterfds
[slot
][0] = -1;
1126 filterfds
[slot
][1] = -1;
1128 if (job
->current_file
== job
->num_files
)
1130 cupsdClosePipe(job
->print_pipes
);
1132 close(job
->status_pipes
[1]);
1133 job
->status_pipes
[1] = -1;
1137 cupsdClosePipe(filterfds
[slot
]);
1139 if (job
->printer
->remote
&& job
->num_files
> 1)
1141 for (i
= 0; i
< job
->num_files
; i
++)
1147 cupsdAddSelect(job
->status_buffer
->fd
, (cupsd_selfunc_t
)update_job
, NULL
,
1150 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
, "Job #%d started.",
1154 * If we get here than we are able to run the printer driver filters, so clear
1155 * the missing and insecure warnings...
1158 if (cupsdSetPrinterReasons(job
->printer
, "-cups-missing-filter-warning,"
1159 "cups-insecure-filter-warning"))
1160 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, job
->printer
, NULL
,
1161 "Printer drivers now functional.");
1167 * If we get here, we need to abort the current job and close out all
1168 * files and pipes...
1173 FilterLevel
-= job
->cost
;
1176 for (slot
= 0; slot
< 2; slot
++)
1177 cupsdClosePipe(filterfds
[slot
]);
1179 cupsArrayDelete(filters
);
1183 if (job
->printer
->remote
&& job
->num_files
> 1)
1185 for (i
= 0; i
< job
->num_files
; i
++)
1192 cupsdClosePipe(job
->print_pipes
);
1193 cupsdClosePipe(job
->back_pipes
);
1194 cupsdClosePipe(job
->side_pipes
);
1196 cupsdRemoveSelect(job
->status_pipes
[0]);
1197 cupsdClosePipe(job
->status_pipes
);
1198 cupsdStatBufDelete(job
->status_buffer
);
1199 job
->status_buffer
= NULL
;
1202 * Update the printer and job state.
1205 cupsdSetJobState(job
, abort_state
, CUPSD_JOB_DEFAULT
, "%s", abort_message
);
1206 cupsdSetPrinterState(job
->printer
, IPP_PRINTER_IDLE
, 0);
1207 update_job_attrs(job
, 0);
1209 cupsArrayRemove(PrintingJobs
, job
);
1212 * Clear the printer <-> job association...
1215 job
->printer
->job
= NULL
;
1216 job
->printer
= NULL
;
1221 * 'cupsdDeleteJob()' - Free all memory used by a job.
1225 cupsdDeleteJob(cupsd_job_t
*job
, /* I - Job */
1226 cupsd_jobaction_t action
)/* I - Action */
1231 if (action
== CUPSD_JOB_PURGE
)
1234 * Remove the job info file...
1237 char filename
[1024]; /* Job filename */
1239 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
,
1244 cupsdClearString(&job
->username
);
1245 cupsdClearString(&job
->dest
);
1246 cupsdClearString(&job
->auth_username
);
1247 cupsdClearString(&job
->auth_domain
);
1248 cupsdClearString(&job
->auth_password
);
1252 * Destroy the credential cache and clear the KRB5CCNAME env var string.
1257 krb5_cc_destroy(KerberosContext
, job
->ccache
);
1261 cupsdClearString(&job
->ccname
);
1262 #endif /* HAVE_GSSAPI */
1264 if (job
->num_files
> 0)
1266 free(job
->compressions
);
1267 free(job
->filetypes
);
1274 cupsArrayRemove(Jobs
, job
);
1275 cupsArrayRemove(ActiveJobs
, job
);
1276 cupsArrayRemove(PrintingJobs
, job
);
1283 * 'cupsdFreeAllJobs()' - Free all jobs from memory.
1287 cupsdFreeAllJobs(void)
1289 cupsd_job_t
*job
; /* Current job */
1297 cupsdStopAllJobs(CUPSD_JOB_FORCE
, 0);
1300 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1302 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1303 cupsdDeleteJob(job
, CUPSD_JOB_DEFAULT
);
1305 cupsdReleaseSignals();
1310 * 'cupsdFindJob()' - Find the specified job.
1313 cupsd_job_t
* /* O - Job data */
1314 cupsdFindJob(int id
) /* I - Job ID */
1316 cupsd_job_t key
; /* Search key */
1321 return ((cupsd_job_t
*)cupsArrayFind(Jobs
, &key
));
1326 * 'cupsdGetPrinterJobCount()' - Get the number of pending, processing,
1327 * or held jobs in a printer or class.
1330 int /* O - Job count */
1331 cupsdGetPrinterJobCount(
1332 const char *dest
) /* I - Printer or class name */
1334 int count
; /* Job count */
1335 cupsd_job_t
*job
; /* Current job */
1338 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
1340 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
1341 if (job
->dest
&& !strcasecmp(job
->dest
, dest
))
1349 * 'cupsdGetUserJobCount()' - Get the number of pending, processing,
1350 * or held jobs for a user.
1353 int /* O - Job count */
1354 cupsdGetUserJobCount(
1355 const char *username
) /* I - Username */
1357 int count
; /* Job count */
1358 cupsd_job_t
*job
; /* Current job */
1361 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
1363 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
1364 if (!strcasecmp(job
->username
, username
))
1372 * 'cupsdLoadAllJobs()' - Load all jobs from disk.
1376 cupsdLoadAllJobs(void)
1378 char filename
[1024]; /* Full filename of job.cache file */
1379 struct stat fileinfo
, /* Information on job.cache file */
1380 dirinfo
; /* Information on RequestRoot dir */
1385 * Create the job arrays as needed...
1389 Jobs
= cupsArrayNew(compare_jobs
, NULL
);
1392 ActiveJobs
= cupsArrayNew(compare_active_jobs
, NULL
);
1395 PrintingJobs
= cupsArrayNew(compare_jobs
, NULL
);
1398 * See whether the job.cache file is older than the RequestRoot directory...
1401 snprintf(filename
, sizeof(filename
), "%s/job.cache", CacheDir
);
1403 if (stat(filename
, &fileinfo
))
1405 fileinfo
.st_mtime
= 0;
1407 if (errno
!= ENOENT
)
1408 cupsdLogMessage(CUPSD_LOG_ERROR
,
1409 "Unable to get file information for \"%s\" - %s",
1410 filename
, strerror(errno
));
1413 if (stat(RequestRoot
, &dirinfo
))
1415 dirinfo
.st_mtime
= 0;
1417 if (errno
!= ENOENT
)
1418 cupsdLogMessage(CUPSD_LOG_ERROR
,
1419 "Unable to get directory information for \"%s\" - %s",
1420 RequestRoot
, strerror(errno
));
1424 * Load the most recent source for job data...
1427 if (dirinfo
.st_mtime
> fileinfo
.st_mtime
)
1429 load_request_root();
1431 load_next_job_id(filename
);
1434 load_job_cache(filename
);
1437 * Clean out old jobs as needed...
1440 if (MaxJobs
> 0 && cupsArrayCount(Jobs
) >= MaxJobs
)
1446 * 'cupsdLoadJob()' - Load a single job.
1449 int /* O - 1 on success, 0 on failure */
1450 cupsdLoadJob(cupsd_job_t
*job
) /* I - Job */
1452 char jobfile
[1024]; /* Job filename */
1453 cups_file_t
*fp
; /* Job file */
1454 int fileid
; /* Current file ID */
1455 ipp_attribute_t
*attr
; /* Job attribute */
1456 const char *dest
; /* Destination name */
1457 cupsd_printer_t
*destptr
; /* Pointer to destination */
1458 mime_type_t
**filetypes
; /* New filetypes array */
1459 int *compressions
; /* New compressions array */
1464 if (job
->state_value
> IPP_JOB_STOPPED
)
1465 job
->access_time
= time(NULL
);
1470 if ((job
->attrs
= ippNew()) == NULL
)
1472 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Ran out of memory for job attributes!");
1477 * Load job attributes...
1480 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Loading attributes...", job
->id
);
1482 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, job
->id
);
1483 if ((fp
= cupsFileOpen(jobfile
, "r")) == NULL
)
1485 cupsdLogMessage(CUPSD_LOG_ERROR
,
1486 "[Job %d] Unable to open job control file \"%s\" - %s!",
1487 job
->id
, jobfile
, strerror(errno
));
1491 if (ippReadIO(fp
, (ipp_iocb_t
)cupsFileRead
, 1, NULL
, job
->attrs
) != IPP_DATA
)
1493 cupsdLogMessage(CUPSD_LOG_ERROR
,
1494 "[Job %d] Unable to read job control file \"%s\"!", job
->id
,
1503 * Copy attribute data to the job object...
1506 if (!ippFindAttribute(job
->attrs
, "time-at-creation", IPP_TAG_INTEGER
))
1508 cupsdLogMessage(CUPSD_LOG_ERROR
,
1509 "[Job %d] Missing or bad time-at-creation attribute in "
1510 "control file!", job
->id
);
1514 if ((job
->state
= ippFindAttribute(job
->attrs
, "job-state",
1515 IPP_TAG_ENUM
)) == NULL
)
1517 cupsdLogMessage(CUPSD_LOG_ERROR
,
1518 "[Job %d] Missing or bad job-state attribute in control "
1523 job
->state_value
= (ipp_jstate_t
)job
->state
->values
[0].integer
;
1527 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1528 IPP_TAG_URI
)) == NULL
)
1530 cupsdLogMessage(CUPSD_LOG_ERROR
,
1531 "[Job %d] No job-printer-uri attribute in control file!",
1536 if ((dest
= cupsdValidateDest(attr
->values
[0].string
.text
, &(job
->dtype
),
1539 cupsdLogMessage(CUPSD_LOG_ERROR
,
1540 "[Job %d] Unable to queue job for destination \"%s\"!",
1541 job
->id
, attr
->values
[0].string
.text
);
1545 cupsdSetString(&job
->dest
, dest
);
1547 else if ((destptr
= cupsdFindDest(job
->dest
)) == NULL
)
1549 cupsdLogMessage(CUPSD_LOG_ERROR
,
1550 "[Job %d] Unable to queue job for destination \"%s\"!",
1551 job
->id
, job
->dest
);
1555 job
->sheets
= ippFindAttribute(job
->attrs
, "job-media-sheets-completed",
1557 job
->job_sheets
= ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_NAME
);
1561 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
1562 IPP_TAG_INTEGER
)) == NULL
)
1564 cupsdLogMessage(CUPSD_LOG_ERROR
,
1565 "[Job %d] Missing or bad job-priority attribute in "
1566 "control file!", job
->id
);
1570 job
->priority
= attr
->values
[0].integer
;
1575 if ((attr
= ippFindAttribute(job
->attrs
, "job-originating-user-name",
1576 IPP_TAG_NAME
)) == NULL
)
1578 cupsdLogMessage(CUPSD_LOG_ERROR
,
1579 "[Job %d] Missing or bad job-originating-user-name "
1580 "attribute in control file!", job
->id
);
1584 cupsdSetString(&job
->username
, attr
->values
[0].string
.text
);
1588 * Set the job hold-until time and state...
1591 if (job
->state_value
== IPP_JOB_HELD
)
1593 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1594 IPP_TAG_KEYWORD
)) == NULL
)
1595 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1598 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
, CUPSD_JOB_DEFAULT
);
1601 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1602 job
->state_value
= IPP_JOB_PENDING
;
1605 else if (job
->state_value
== IPP_JOB_PROCESSING
)
1607 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1608 job
->state_value
= IPP_JOB_PENDING
;
1611 if (!job
->num_files
)
1614 * Find all the d##### files...
1617 for (fileid
= 1; fileid
< 10000; fileid
++)
1619 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
1622 if (access(jobfile
, 0))
1625 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1626 "[Job %d] Auto-typing document file \"%s\"...", job
->id
,
1629 if (fileid
> job
->num_files
)
1631 if (job
->num_files
== 0)
1633 compressions
= (int *)calloc(fileid
, sizeof(int));
1634 filetypes
= (mime_type_t
**)calloc(fileid
, sizeof(mime_type_t
*));
1638 compressions
= (int *)realloc(job
->compressions
,
1639 sizeof(int) * fileid
);
1640 filetypes
= (mime_type_t
**)realloc(job
->filetypes
,
1641 sizeof(mime_type_t
*) *
1645 if (!compressions
|| !filetypes
)
1647 cupsdLogMessage(CUPSD_LOG_ERROR
,
1648 "[Job %d] Ran out of memory for job file types!",
1653 job
->compressions
= compressions
;
1654 job
->filetypes
= filetypes
;
1655 job
->num_files
= fileid
;
1658 job
->filetypes
[fileid
- 1] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
1659 job
->compressions
+ fileid
- 1);
1661 if (!job
->filetypes
[fileid
- 1])
1662 job
->filetypes
[fileid
- 1] = mimeType(MimeDatabase
, "application",
1668 * Load authentication information as needed...
1671 if (job
->state_value
< IPP_JOB_STOPPED
)
1673 snprintf(jobfile
, sizeof(jobfile
), "%s/a%05d", RequestRoot
, job
->id
);
1675 cupsdClearString(&job
->auth_username
);
1676 cupsdClearString(&job
->auth_domain
);
1677 cupsdClearString(&job
->auth_password
);
1679 if ((fp
= cupsFileOpen(jobfile
, "r")) != NULL
)
1681 int i
, /* Looping var */
1682 bytes
; /* Size of auth data */
1683 char line
[255], /* Line from file */
1684 data
[255]; /* Decoded data */
1688 i
< destptr
->num_auth_info_required
&&
1689 cupsFileGets(fp
, line
, sizeof(line
));
1692 bytes
= sizeof(data
);
1693 httpDecode64_2(data
, &bytes
, line
);
1695 if (!strcmp(destptr
->auth_info_required
[i
], "username"))
1696 cupsdSetStringf(&job
->auth_username
, "AUTH_USERNAME=%s", data
);
1697 else if (!strcmp(destptr
->auth_info_required
[i
], "domain"))
1698 cupsdSetStringf(&job
->auth_domain
, "AUTH_DOMAIN=%s", data
);
1699 else if (!strcmp(destptr
->auth_info_required
[i
], "password"))
1700 cupsdSetStringf(&job
->auth_password
, "AUTH_PASSWORD=%s", data
);
1707 job
->access_time
= time(NULL
);
1711 * If we get here then something bad happened...
1716 ippDelete(job
->attrs
);
1725 * 'cupsdMoveJob()' - Move the specified job to a different destination.
1729 cupsdMoveJob(cupsd_job_t
*job
, /* I - Job */
1730 cupsd_printer_t
*p
) /* I - Destination printer or class */
1732 ipp_attribute_t
*attr
; /* job-printer-uri attribute */
1733 const char *olddest
; /* Old destination */
1734 cupsd_printer_t
*oldp
; /* Old pointer */
1738 * Don't move completed jobs...
1741 if (job
->state_value
> IPP_JOB_STOPPED
)
1745 * Get the old destination...
1748 olddest
= job
->dest
;
1751 oldp
= job
->printer
;
1753 oldp
= cupsdFindDest(olddest
);
1756 * Change the destination information...
1759 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
1760 "Stopping job prior to move.");
1762 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, oldp
, job
,
1763 "Job #%d moved from %s to %s.", job
->id
, olddest
,
1766 cupsdSetString(&job
->dest
, p
->name
);
1767 job
->dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_REMOTE
|
1768 CUPS_PRINTER_IMPLICIT
);
1770 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1771 IPP_TAG_URI
)) != NULL
)
1772 cupsdSetString(&(attr
->values
[0].string
.text
), p
->uri
);
1774 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, p
, job
,
1775 "Job #%d moved from %s to %s.", job
->id
, olddest
,
1779 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1784 * 'cupsdReleaseJob()' - Release the specified job.
1788 cupsdReleaseJob(cupsd_job_t
*job
) /* I - Job */
1790 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdReleaseJob(job=%p(%d))", job
,
1793 if (job
->state_value
== IPP_JOB_HELD
)
1796 * Add trailing banner as needed...
1799 if (job
->pending_timeout
)
1800 cupsdTimeoutJob(job
);
1802 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
1803 "Job released by user.");
1809 * 'cupsdRestartJob()' - Restart the specified job.
1813 cupsdRestartJob(cupsd_job_t
*job
) /* I - Job */
1815 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdRestartJob(job=%p(%d))", job
,
1818 if (job
->state_value
== IPP_JOB_STOPPED
|| job
->num_files
)
1819 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
1820 "Job restarted by user.");
1825 * 'cupsdSaveAllJobs()' - Save a summary of all jobs to disk.
1829 cupsdSaveAllJobs(void)
1831 int i
; /* Looping var */
1832 cups_file_t
*fp
; /* Job cache file */
1833 char temp
[1024]; /* Temporary string */
1834 cupsd_job_t
*job
; /* Current job */
1835 time_t curtime
; /* Current time */
1836 struct tm
*curdate
; /* Current date */
1839 snprintf(temp
, sizeof(temp
), "%s/job.cache", CacheDir
);
1840 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
1842 cupsdLogMessage(CUPSD_LOG_ERROR
,
1843 "Unable to create job cache file \"%s\" - %s",
1844 temp
, strerror(errno
));
1848 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving job cache file \"%s\"...", temp
);
1851 * Restrict access to the file...
1854 fchown(cupsFileNumber(fp
), getuid(), Group
);
1855 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
1858 * Write a small header to the file...
1861 curtime
= time(NULL
);
1862 curdate
= localtime(&curtime
);
1863 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
1865 cupsFilePuts(fp
, "# Job cache file for " CUPS_SVERSION
"\n");
1866 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
1867 cupsFilePrintf(fp
, "NextJobId %d\n", NextJobId
);
1870 * Write each job known to the system...
1873 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1875 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1877 cupsFilePrintf(fp
, "<Job %d>\n", job
->id
);
1878 cupsFilePrintf(fp
, "State %d\n", job
->state_value
);
1879 cupsFilePrintf(fp
, "Priority %d\n", job
->priority
);
1880 cupsFilePrintf(fp
, "HoldUntil %d\n", (int)job
->hold_until
);
1881 cupsFilePrintf(fp
, "Username %s\n", job
->username
);
1882 cupsFilePrintf(fp
, "Destination %s\n", job
->dest
);
1883 cupsFilePrintf(fp
, "DestType %d\n", job
->dtype
);
1884 cupsFilePrintf(fp
, "NumFiles %d\n", job
->num_files
);
1885 for (i
= 0; i
< job
->num_files
; i
++)
1886 cupsFilePrintf(fp
, "File %d %s/%s %d\n", i
+ 1, job
->filetypes
[i
]->super
,
1887 job
->filetypes
[i
]->type
, job
->compressions
[i
]);
1888 cupsFilePuts(fp
, "</Job>\n");
1896 * 'cupsdSaveJob()' - Save a job to disk.
1900 cupsdSaveJob(cupsd_job_t
*job
) /* I - Job */
1902 char filename
[1024]; /* Job control filename */
1903 cups_file_t
*fp
; /* Job file */
1906 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSaveJob(job=%p(%d)): job->attrs=%p",
1907 job
, job
->id
, job
->attrs
);
1909 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
, job
->id
);
1911 if ((fp
= cupsFileOpen(filename
, "w")) == NULL
)
1913 cupsdLogMessage(CUPSD_LOG_ERROR
,
1914 "[Job %d] Unable to create job control file \"%s\" - %s.",
1915 job
->id
, filename
, strerror(errno
));
1919 fchmod(cupsFileNumber(fp
), 0600);
1920 fchown(cupsFileNumber(fp
), RunUser
, Group
);
1922 job
->attrs
->state
= IPP_IDLE
;
1924 if (ippWriteIO(fp
, (ipp_iocb_t
)cupsFileWrite
, 1, NULL
,
1925 job
->attrs
) != IPP_DATA
)
1926 cupsdLogMessage(CUPSD_LOG_ERROR
,
1927 "[Job %d] Unable to write job control file!", job
->id
);
1936 * 'cupsdSetJobHoldUntil()' - Set the hold time for a job.
1940 cupsdSetJobHoldUntil(cupsd_job_t
*job
, /* I - Job */
1941 const char *when
, /* I - When to resume */
1942 int update
)/* I - Update job-hold-until attr? */
1944 time_t curtime
; /* Current time */
1945 struct tm
*curdate
; /* Current date */
1946 int hour
; /* Hold hour */
1947 int minute
; /* Hold minute */
1948 int second
= 0; /* Hold second */
1951 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1952 "cupsdSetJobHoldUntil(job=%p(%d), when=\"%s\", update=%d)",
1953 job
, job
->id
, when
, update
);
1958 * Update the job-hold-until attribute...
1961 ipp_attribute_t
*attr
; /* job-hold-until attribute */
1963 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1964 IPP_TAG_KEYWORD
)) == NULL
)
1965 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1968 cupsdSetString(&(attr
->values
[0].string
.text
), when
);
1970 attr
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1971 "job-hold-until", NULL
, when
);
1975 if (isdigit(when
[0] & 255))
1976 attr
->value_tag
= IPP_TAG_NAME
;
1978 attr
->value_tag
= IPP_TAG_KEYWORD
;
1981 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1986 * Update the hold time...
1989 if (!strcmp(when
, "indefinite") || !strcmp(when
, "auth-info-required"))
1992 * Hold indefinitely...
1995 job
->hold_until
= 0;
1997 else if (!strcmp(when
, "day-time"))
2000 * Hold to 6am the next morning unless local time is < 6pm.
2003 curtime
= time(NULL
);
2004 curdate
= localtime(&curtime
);
2006 if (curdate
->tm_hour
< 18)
2007 job
->hold_until
= curtime
;
2009 job
->hold_until
= curtime
+
2010 ((29 - curdate
->tm_hour
) * 60 + 59 -
2011 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2013 else if (!strcmp(when
, "evening") || !strcmp(when
, "night"))
2016 * Hold to 6pm unless local time is > 6pm or < 6am.
2019 curtime
= time(NULL
);
2020 curdate
= localtime(&curtime
);
2022 if (curdate
->tm_hour
< 6 || curdate
->tm_hour
>= 18)
2023 job
->hold_until
= curtime
;
2025 job
->hold_until
= curtime
+
2026 ((17 - curdate
->tm_hour
) * 60 + 59 -
2027 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2029 else if (!strcmp(when
, "second-shift"))
2032 * Hold to 4pm unless local time is > 4pm.
2035 curtime
= time(NULL
);
2036 curdate
= localtime(&curtime
);
2038 if (curdate
->tm_hour
>= 16)
2039 job
->hold_until
= curtime
;
2041 job
->hold_until
= curtime
+
2042 ((15 - curdate
->tm_hour
) * 60 + 59 -
2043 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2045 else if (!strcmp(when
, "third-shift"))
2048 * Hold to 12am unless local time is < 8am.
2051 curtime
= time(NULL
);
2052 curdate
= localtime(&curtime
);
2054 if (curdate
->tm_hour
< 8)
2055 job
->hold_until
= curtime
;
2057 job
->hold_until
= curtime
+
2058 ((23 - curdate
->tm_hour
) * 60 + 59 -
2059 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2061 else if (!strcmp(when
, "weekend"))
2064 * Hold to weekend unless we are in the weekend.
2067 curtime
= time(NULL
);
2068 curdate
= localtime(&curtime
);
2070 if (curdate
->tm_wday
== 0 || curdate
->tm_wday
== 6)
2071 job
->hold_until
= curtime
;
2073 job
->hold_until
= curtime
+
2074 (((5 - curdate
->tm_wday
) * 24 +
2075 (17 - curdate
->tm_hour
)) * 60 + 59 -
2076 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2078 else if (sscanf(when
, "%d:%d:%d", &hour
, &minute
, &second
) >= 2)
2081 * Hold to specified GMT time (HH:MM or HH:MM:SS)...
2084 curtime
= time(NULL
);
2085 curdate
= gmtime(&curtime
);
2087 job
->hold_until
= curtime
+
2088 ((hour
- curdate
->tm_hour
) * 60 + minute
-
2089 curdate
->tm_min
) * 60 + second
- curdate
->tm_sec
;
2092 * Hold until next day as needed...
2095 if (job
->hold_until
< curtime
)
2096 job
->hold_until
+= 24 * 60 * 60;
2099 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetJobHoldUntil: hold_until=%d",
2100 (int)job
->hold_until
);
2105 * 'cupsdSetJobPriority()' - Set the priority of a job, moving it up/down in
2106 * the list as needed.
2110 cupsdSetJobPriority(
2111 cupsd_job_t
*job
, /* I - Job ID */
2112 int priority
) /* I - New priority (0 to 100) */
2114 ipp_attribute_t
*attr
; /* Job attribute */
2118 * Don't change completed jobs...
2121 if (job
->state_value
>= IPP_JOB_PROCESSING
)
2125 * Set the new priority and re-add the job into the active list...
2128 cupsArrayRemove(ActiveJobs
, job
);
2130 job
->priority
= priority
;
2132 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
2133 IPP_TAG_INTEGER
)) != NULL
)
2134 attr
->values
[0].integer
= priority
;
2136 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-priority",
2139 cupsArrayAdd(ActiveJobs
, job
);
2142 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2147 * 'cupsdSetJobState()' - Set the state of the specified print job.
2152 cupsd_job_t
*job
, /* I - Job to cancel */
2153 ipp_jstate_t newstate
, /* I - New job state */
2154 cupsd_jobaction_t action
, /* I - Action to take */
2155 const char *message
, /* I - Message to log */
2156 ...) /* I - Additional arguments as needed */
2158 int i
; /* Looping var */
2159 ipp_jstate_t oldstate
; /* Old state */
2160 char filename
[1024]; /* Job filename */
2161 ipp_attribute_t
*attr
; /* Job attribute */
2164 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2165 "cupsdSetJobState(job=%p(%d), state=%d, newstate=%d, "
2166 "action=%d, message=\"%s\")", job
, job
->id
, job
->state_value
,
2167 newstate
, action
, message
? message
: "(null)");
2171 * Make sure we have the job attributes...
2174 if (!cupsdLoadJob(job
))
2178 * Don't do anything if the state is unchanged...
2181 if (newstate
== (oldstate
= job
->state_value
))
2185 * Stop any processes that are working on the current job...
2188 if (oldstate
== IPP_JOB_PROCESSING
)
2189 stop_job(job
, action
!= CUPSD_JOB_DEFAULT
);
2192 * Set the new job state...
2195 job
->state
->values
[0].integer
= newstate
;
2196 job
->state_value
= newstate
;
2200 case IPP_JOB_PENDING
:
2202 * Update job-hold-until as needed...
2205 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2206 IPP_TAG_KEYWORD
)) == NULL
)
2207 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2211 attr
->value_tag
= IPP_TAG_KEYWORD
;
2212 cupsdSetString(&(attr
->values
[0].string
.text
), "no-hold");
2218 case IPP_JOB_ABORTED
:
2219 case IPP_JOB_CANCELED
:
2220 case IPP_JOB_COMPLETED
:
2221 set_time(job
, "time-at-completed");
2226 * Log message as needed...
2231 char buffer
[2048]; /* Message buffer */
2232 va_list ap
; /* Pointer to additional arguments */
2234 va_start(ap
, message
);
2235 vsnprintf(buffer
, sizeof(buffer
), message
, ap
);
2238 if (newstate
> IPP_JOB_STOPPED
)
2239 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
, "%s", buffer
);
2241 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
, "%s", buffer
);
2243 if (newstate
== IPP_JOB_STOPPED
|| newstate
== IPP_JOB_ABORTED
)
2244 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "%s", buffer
);
2246 cupsdLogJob(job
, CUPSD_LOG_INFO
, "%s", buffer
);
2250 * Handle post-state-change actions...
2255 case IPP_JOB_PROCESSING
:
2257 * Add the job to the "printing" list...
2260 if (!cupsArrayFind(PrintingJobs
, job
))
2261 cupsArrayAdd(PrintingJobs
, job
);
2264 * Set the processing time...
2267 set_time(job
, "time-at-processing");
2269 case IPP_JOB_PENDING
:
2271 case IPP_JOB_STOPPED
:
2273 * Make sure the job is in the active list...
2276 if (!cupsArrayFind(ActiveJobs
, job
))
2277 cupsArrayAdd(ActiveJobs
, job
);
2280 * Save the job state to disk...
2284 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2287 case IPP_JOB_ABORTED
:
2288 case IPP_JOB_CANCELED
:
2289 case IPP_JOB_COMPLETED
:
2291 * Expire job subscriptions since the job is now "completed"...
2294 cupsdExpireSubscriptions(NULL
, job
);
2297 * Remove the job from the active list...
2300 cupsArrayRemove(ActiveJobs
, job
);
2304 * If we are going to sleep and the PrintingJobs count is now 0, allow the
2305 * sleep to happen immediately...
2308 if (Sleeping
&& cupsArrayCount(PrintingJobs
) == 0)
2310 #endif /* __APPLE__ */
2313 * Remove any authentication data...
2316 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
2317 if (cupsdRemoveFile(filename
) && errno
!= ENOENT
)
2318 cupsdLogMessage(CUPSD_LOG_ERROR
,
2319 "Unable to remove authentication cache: %s",
2322 cupsdClearString(&job
->auth_username
);
2323 cupsdClearString(&job
->auth_domain
);
2324 cupsdClearString(&job
->auth_password
);
2328 * Destroy the credential cache and clear the KRB5CCNAME env var string.
2333 krb5_cc_destroy(KerberosContext
, job
->ccache
);
2337 cupsdClearString(&job
->ccname
);
2338 #endif /* HAVE_GSSAPI */
2341 * Remove the print file for good if we aren't preserving jobs or
2345 if (!JobHistory
|| !JobFiles
|| action
== CUPSD_JOB_PURGE
)
2347 for (i
= 1; i
<= job
->num_files
; i
++)
2349 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
2354 if (job
->num_files
> 0)
2356 free(job
->filetypes
);
2357 free(job
->compressions
);
2360 job
->filetypes
= NULL
;
2361 job
->compressions
= NULL
;
2365 if (JobHistory
&& action
!= CUPSD_JOB_PURGE
)
2368 * Save job state info...
2372 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2374 else if (!job
->printer
)
2375 cupsdDeleteJob(job
, CUPSD_JOB_PURGE
);
2380 * Finalize the job immediately if we forced things...
2383 if (action
== CUPSD_JOB_FORCE
)
2387 * Update the server "busy" state...
2390 cupsdSetBusyState();
2395 * 'cupsdStopAllJobs()' - Stop all print jobs.
2400 cupsd_jobaction_t action
, /* I - Action */
2401 int kill_delay
) /* I - Number of seconds before we kill */
2403 cupsd_job_t
*job
; /* Current job */
2406 DEBUG_puts("cupsdStopAllJobs()");
2408 for (job
= (cupsd_job_t
*)cupsArrayFirst(PrintingJobs
);
2410 job
= (cupsd_job_t
*)cupsArrayNext(PrintingJobs
))
2413 job
->kill_time
= time(NULL
) + kill_delay
;
2415 cupsdSetJobState(job
, IPP_JOB_PENDING
, action
, NULL
);
2421 * 'cupsdUnloadCompletedJobs()' - Flush completed job history from memory.
2425 cupsdUnloadCompletedJobs(void)
2427 cupsd_job_t
*job
; /* Current job */
2428 time_t expire
; /* Expiration time */
2431 expire
= time(NULL
) - 60;
2433 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
2435 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
2436 if (job
->attrs
&& job
->state_value
>= IPP_JOB_STOPPED
&& !job
->printer
&&
2437 job
->access_time
< expire
)
2448 * 'compare_active_jobs()' - Compare the job IDs and priorities of two jobs.
2451 static int /* O - Difference */
2452 compare_active_jobs(void *first
, /* I - First job */
2453 void *second
, /* I - Second job */
2454 void *data
) /* I - App data (not used) */
2456 int diff
; /* Difference */
2459 if ((diff
= ((cupsd_job_t
*)second
)->priority
-
2460 ((cupsd_job_t
*)first
)->priority
) != 0)
2463 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
2468 * 'compare_jobs()' - Compare the job IDs of two jobs.
2471 static int /* O - Difference */
2472 compare_jobs(void *first
, /* I - First job */
2473 void *second
, /* I - Second job */
2474 void *data
) /* I - App data (not used) */
2476 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
2481 * 'finalize_job()' - Cleanup after job filter processes and support data.
2485 finalize_job(cupsd_job_t
*job
) /* I - Job */
2487 ipp_pstate_t printer_state
; /* New printer state value */
2488 ipp_jstate_t job_state
; /* New job state value */
2489 const char *message
; /* Message for job state */
2490 char buffer
[1024]; /* Buffer for formatted messages */
2493 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "finalize_job(job=%p(%d))", job
, job
->id
);
2496 * Clear the "connecting-to-device" reason, which is only valid when a
2497 * printer is processing...
2500 cupsdSetPrinterReasons(job
->printer
, "-connecting-to-device");
2503 * Similarly, clear the "offline-report" reason for non-USB devices since we
2504 * rarely have current information for network devices...
2507 if (strncmp(job
->printer
->device_uri
, "usb:", 4))
2508 cupsdSetPrinterReasons(job
->printer
, "-offline-report");
2511 * Free the security profile...
2514 cupsdDestroyProfile(job
->profile
);
2515 job
->profile
= NULL
;
2518 * Close pipes and status buffer...
2521 cupsdClosePipe(job
->print_pipes
);
2522 cupsdClosePipe(job
->back_pipes
);
2523 cupsdClosePipe(job
->side_pipes
);
2525 cupsdRemoveSelect(job
->status_pipes
[0]);
2526 cupsdClosePipe(job
->status_pipes
);
2527 cupsdStatBufDelete(job
->status_buffer
);
2528 job
->status_buffer
= NULL
;
2531 * Process the exit status...
2534 if (job
->printer
->state
== IPP_PRINTER_PROCESSING
)
2535 printer_state
= IPP_PRINTER_IDLE
;
2537 printer_state
= job
->printer
->state
;
2539 switch (job_state
= job
->state_value
)
2541 case IPP_JOB_PENDING
:
2542 message
= "Job paused.";
2546 message
= "Job held.";
2550 case IPP_JOB_PROCESSING
:
2551 case IPP_JOB_COMPLETED
:
2552 job_state
= IPP_JOB_COMPLETED
;
2553 message
= "Job completed.";
2556 case IPP_JOB_STOPPED
:
2557 message
= "Job stopped.";
2560 case IPP_JOB_CANCELED
:
2561 message
= "Job canceled.";
2564 case IPP_JOB_ABORTED
:
2565 message
= "Job aborted.";
2569 if (job
->status
< 0)
2572 * Backend had errors...
2575 int exit_code
; /* Exit code from backend */
2579 * Convert the status to an exit code. Due to the way the W* macros are
2580 * implemented on MacOS X (bug?), we have to store the exit status in a
2581 * variable first and then convert...
2584 exit_code
= -job
->status
;
2585 if (WIFEXITED(exit_code
))
2586 exit_code
= WEXITSTATUS(exit_code
);
2588 exit_code
= job
->status
;
2590 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Backend returned status %d (%s)",
2592 exit_code
== CUPS_BACKEND_FAILED
? "failed" :
2593 exit_code
== CUPS_BACKEND_AUTH_REQUIRED
?
2594 "authentication required" :
2595 exit_code
== CUPS_BACKEND_HOLD
? "hold job" :
2596 exit_code
== CUPS_BACKEND_STOP
? "stop printer" :
2597 exit_code
== CUPS_BACKEND_CANCEL
? "cancel job" :
2598 exit_code
< 0 ? "crashed" : "unknown");
2601 * Do what needs to be done...
2607 case CUPS_BACKEND_FAILED
:
2609 * Backend failure, use the error-policy to determine how to
2613 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
2616 * Queued on a class - mark the job as pending and we'll retry on
2617 * another printer...
2620 if (job_state
== IPP_JOB_COMPLETED
)
2622 job_state
= IPP_JOB_PENDING
;
2623 message
= "Retrying job on another printer.";
2626 else if (!strcmp(job
->printer
->error_policy
, "retry-current-job"))
2629 * The error policy is "retry-current-job" - mark the job as pending
2630 * and we'll retry on the same printer...
2633 if (job_state
== IPP_JOB_COMPLETED
)
2635 job_state
= IPP_JOB_PENDING
;
2636 message
= "Retrying job on same printer.";
2639 else if ((job
->printer
->type
& CUPS_PRINTER_FAX
) ||
2640 !strcmp(job
->printer
->error_policy
, "retry-job"))
2642 if (job_state
== IPP_JOB_COMPLETED
)
2645 * The job was queued on a fax or the error policy is "retry-job" -
2646 * hold the job if the number of retries is less than the
2647 * JobRetryLimit, otherwise abort the job.
2652 if (job
->tries
>= JobRetryLimit
)
2658 snprintf(buffer
, sizeof(buffer
),
2659 "Job aborted after %d unsuccessful attempts.",
2661 job_state
= IPP_JOB_ABORTED
;
2667 * Try again in N seconds...
2670 set_hold_until(job
, time(NULL
) + JobRetryInterval
);
2672 snprintf(buffer
, sizeof(buffer
),
2673 "Job held for %d seconds since it could not be sent.",
2675 job_state
= IPP_JOB_HELD
;
2680 else if (!strcmp(job
->printer
->error_policy
, "abort-job") &&
2681 job_state
== IPP_JOB_COMPLETED
)
2683 job_state
= IPP_JOB_ABORTED
;
2684 message
= "Job aborted due to backend errors; please consult "
2685 "the error_log file for details.";
2687 else if (job
->state_value
== IPP_JOB_PROCESSING
)
2689 job_state
= IPP_JOB_PENDING
;
2690 printer_state
= IPP_PRINTER_STOPPED
;
2691 message
= "Printer stopped due to backend errors; please "
2692 "consult the error_log file for details.";
2696 case CUPS_BACKEND_CANCEL
:
2701 if (job_state
== IPP_JOB_COMPLETED
)
2703 job_state
= IPP_JOB_ABORTED
;
2704 message
= "Job aborted due to backend errors; please consult "
2705 "the error_log file for details.";
2709 case CUPS_BACKEND_HOLD
:
2710 if (job_state
== IPP_JOB_COMPLETED
)
2716 cupsdSetJobHoldUntil(job
, "indefinite", 1);
2718 job_state
= IPP_JOB_HELD
;
2719 message
= "Job held indefinitely due to backend errors; please "
2720 "consult the error_log file for details.";
2724 case CUPS_BACKEND_STOP
:
2726 * Stop the printer...
2729 printer_state
= IPP_PRINTER_STOPPED
;
2730 message
= "Printer stopped due to backend errors; please "
2731 "consult the error_log file for details.";
2733 if (job_state
== IPP_JOB_COMPLETED
)
2734 job_state
= IPP_JOB_PENDING
;
2737 case CUPS_BACKEND_AUTH_REQUIRED
:
2739 * Hold the job for authentication...
2742 if (job_state
== IPP_JOB_COMPLETED
)
2744 cupsdSetJobHoldUntil(job
, "auth-info-required", 1);
2746 job_state
= IPP_JOB_HELD
;
2747 message
= "Job held for authentication.";
2752 else if (job
->status
> 0)
2755 * Filter had errors; stop job...
2758 if (job_state
== IPP_JOB_COMPLETED
)
2760 job_state
= IPP_JOB_STOPPED
;
2761 message
= "Job stopped due to filter errors; please consult the "
2762 "error_log file for details.";
2767 * Update the printer and job state.
2770 cupsdSetJobState(job
, job_state
, CUPSD_JOB_DEFAULT
, "%s", message
);
2771 cupsdSetPrinterState(job
->printer
, printer_state
,
2772 printer_state
== IPP_PRINTER_STOPPED
);
2773 update_job_attrs(job
, 0);
2775 cupsArrayRemove(PrintingJobs
, job
);
2778 * Clear the printer <-> job association...
2781 job
->printer
->job
= NULL
;
2782 job
->printer
= NULL
;
2785 * Try printing another job...
2788 if (printer_state
!= IPP_PRINTER_STOPPED
)
2794 * 'get_options()' - Get a string containing the job options.
2797 static char * /* O - Options string */
2798 get_options(cupsd_job_t
*job
, /* I - Job */
2799 int banner_page
, /* I - Printing a banner page? */
2800 char *copies
, /* I - Copies buffer */
2801 size_t copies_size
, /* I - Size of copies buffer */
2802 char *title
, /* I - Title buffer */
2803 size_t title_size
) /* I - Size of title buffer */
2805 int i
; /* Looping var */
2806 char *optptr
, /* Pointer to options */
2807 *valptr
; /* Pointer in value string */
2808 ipp_attribute_t
*attr
; /* Current attribute */
2809 static char *options
= NULL
;/* Full list of options */
2810 static int optlength
= 0; /* Length of option buffer */
2814 * Building the options string is harder than it needs to be, but
2815 * for the moment we need to pass strings for command-line args and
2816 * not IPP attribute pointers... :)
2818 * First allocate/reallocate the option buffer as needed...
2821 i
= ipp_length(job
->attrs
);
2823 if (i
> optlength
|| !options
)
2828 optptr
= realloc(options
, i
);
2832 cupsdLogJob(job
, CUPSD_LOG_CRIT
,
2833 "Unable to allocate %d bytes for option buffer!", i
);
2842 * Now loop through the attributes and convert them to the textual
2843 * representation used by the filters...
2849 snprintf(title
, title_size
, "%s-%d", job
->printer
->name
, job
->id
);
2850 strlcpy(copies
, "1", copies_size
);
2852 for (attr
= job
->attrs
->attrs
; attr
!= NULL
; attr
= attr
->next
)
2854 if (!strcmp(attr
->name
, "copies") &&
2855 attr
->value_tag
== IPP_TAG_INTEGER
)
2858 * Don't use the # copies attribute if we are printing the job sheets...
2862 snprintf(copies
, copies_size
, "%d", attr
->values
[0].integer
);
2864 else if (!strcmp(attr
->name
, "job-name") &&
2865 (attr
->value_tag
== IPP_TAG_NAME
||
2866 attr
->value_tag
== IPP_TAG_NAMELANG
))
2867 strlcpy(title
, attr
->values
[0].string
.text
, title_size
);
2868 else if (attr
->group_tag
== IPP_TAG_JOB
)
2871 * Filter out other unwanted attributes...
2874 if (attr
->value_tag
== IPP_TAG_MIMETYPE
||
2875 attr
->value_tag
== IPP_TAG_NAMELANG
||
2876 attr
->value_tag
== IPP_TAG_TEXTLANG
||
2877 (attr
->value_tag
== IPP_TAG_URI
&& strcmp(attr
->name
, "job-uuid")) ||
2878 attr
->value_tag
== IPP_TAG_URISCHEME
||
2879 attr
->value_tag
== IPP_TAG_BEGIN_COLLECTION
) /* Not yet supported */
2882 if (!strncmp(attr
->name
, "time-", 5))
2885 if (!strncmp(attr
->name
, "job-", 4) &&
2886 strcmp(attr
->name
, "job-billing") &&
2887 strcmp(attr
->name
, "job-impressions") &&
2888 strcmp(attr
->name
, "job-originating-host-name") &&
2889 strcmp(attr
->name
, "job-uuid") &&
2890 !(job
->printer
->type
& CUPS_PRINTER_REMOTE
))
2893 if ((!strcmp(attr
->name
, "job-impressions") ||
2894 !strcmp(attr
->name
, "page-label") ||
2895 !strcmp(attr
->name
, "page-border") ||
2896 !strncmp(attr
->name
, "number-up", 9) ||
2897 !strcmp(attr
->name
, "page-ranges") ||
2898 !strcmp(attr
->name
, "page-set") ||
2899 !strcasecmp(attr
->name
, "AP_FIRSTPAGE_InputSlot") ||
2900 !strcasecmp(attr
->name
, "AP_FIRSTPAGE_ManualFeed") ||
2901 !strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
2902 "PMTotalSidesImaged..n.") ||
2903 !strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
2904 "PMTotalBeginPages..n.")) &&
2909 * Otherwise add them to the list...
2912 if (optptr
> options
)
2913 strlcat(optptr
, " ", optlength
- (optptr
- options
));
2915 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
2917 strlcat(optptr
, attr
->name
, optlength
- (optptr
- options
));
2918 strlcat(optptr
, "=", optlength
- (optptr
- options
));
2921 for (i
= 0; i
< attr
->num_values
; i
++)
2924 strlcat(optptr
, ",", optlength
- (optptr
- options
));
2926 optptr
+= strlen(optptr
);
2928 switch (attr
->value_tag
)
2930 case IPP_TAG_INTEGER
:
2932 snprintf(optptr
, optlength
- (optptr
- options
),
2933 "%d", attr
->values
[i
].integer
);
2936 case IPP_TAG_BOOLEAN
:
2937 if (!attr
->values
[i
].boolean
)
2938 strlcat(optptr
, "no", optlength
- (optptr
- options
));
2940 case IPP_TAG_NOVALUE
:
2941 strlcat(optptr
, attr
->name
,
2942 optlength
- (optptr
- options
));
2945 case IPP_TAG_RANGE
:
2946 if (attr
->values
[i
].range
.lower
== attr
->values
[i
].range
.upper
)
2947 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2948 "%d", attr
->values
[i
].range
.lower
);
2950 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2951 "%d-%d", attr
->values
[i
].range
.lower
,
2952 attr
->values
[i
].range
.upper
);
2955 case IPP_TAG_RESOLUTION
:
2956 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2957 "%dx%d%s", attr
->values
[i
].resolution
.xres
,
2958 attr
->values
[i
].resolution
.yres
,
2959 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
2963 case IPP_TAG_STRING
:
2966 case IPP_TAG_KEYWORD
:
2967 case IPP_TAG_CHARSET
:
2968 case IPP_TAG_LANGUAGE
:
2970 for (valptr
= attr
->values
[i
].string
.text
; *valptr
;)
2972 if (strchr(" \t\n\\\'\"", *valptr
))
2974 *optptr
++ = *valptr
++;
2981 break; /* anti-compiler-warning-code */
2985 optptr
+= strlen(optptr
);
2995 * 'ipp_length()' - Compute the size of the buffer needed to hold
2996 * the textual IPP attributes.
2999 static int /* O - Size of attribute buffer */
3000 ipp_length(ipp_t
*ipp
) /* I - IPP request */
3002 int bytes
; /* Number of bytes */
3003 int i
; /* Looping var */
3004 ipp_attribute_t
*attr
; /* Current attribute */
3008 * Loop through all attributes...
3013 for (attr
= ipp
->attrs
; attr
!= NULL
; attr
= attr
->next
)
3016 * Skip attributes that won't be sent to filters...
3019 if (attr
->value_tag
== IPP_TAG_MIMETYPE
||
3020 attr
->value_tag
== IPP_TAG_NAMELANG
||
3021 attr
->value_tag
== IPP_TAG_TEXTLANG
||
3022 attr
->value_tag
== IPP_TAG_URI
||
3023 attr
->value_tag
== IPP_TAG_URISCHEME
)
3026 if (strncmp(attr
->name
, "time-", 5) == 0)
3030 * Add space for a leading space and commas between each value.
3031 * For the first attribute, the leading space isn't used, so the
3032 * extra byte can be used as the nul terminator...
3035 bytes
++; /* " " separator */
3036 bytes
+= attr
->num_values
; /* "," separators */
3039 * Boolean attributes appear as "foo,nofoo,foo,nofoo", while
3040 * other attributes appear as "foo=value1,value2,...,valueN".
3043 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
3044 bytes
+= strlen(attr
->name
);
3046 bytes
+= attr
->num_values
* strlen(attr
->name
);
3049 * Now add the size required for each value in the attribute...
3052 switch (attr
->value_tag
)
3054 case IPP_TAG_INTEGER
:
3057 * Minimum value of a signed integer is -2147483647, or 11 digits.
3060 bytes
+= attr
->num_values
* 11;
3063 case IPP_TAG_BOOLEAN
:
3065 * Add two bytes for each false ("no") value...
3068 for (i
= 0; i
< attr
->num_values
; i
++)
3069 if (!attr
->values
[i
].boolean
)
3073 case IPP_TAG_RANGE
:
3075 * A range is two signed integers separated by a hyphen, or
3076 * 23 characters max.
3079 bytes
+= attr
->num_values
* 23;
3082 case IPP_TAG_RESOLUTION
:
3084 * A resolution is two signed integers separated by an "x" and
3085 * suffixed by the units, or 26 characters max.
3088 bytes
+= attr
->num_values
* 26;
3091 case IPP_TAG_STRING
:
3094 case IPP_TAG_KEYWORD
:
3095 case IPP_TAG_CHARSET
:
3096 case IPP_TAG_LANGUAGE
:
3099 * Strings can contain characters that need quoting. We need
3100 * at least 2 * len + 2 characters to cover the quotes and
3101 * any backslashes in the string.
3104 for (i
= 0; i
< attr
->num_values
; i
++)
3105 bytes
+= 2 * strlen(attr
->values
[i
].string
.text
) + 2;
3109 break; /* anti-compiler-warning-code */
3118 * 'load_job_cache()' - Load jobs from the job.cache file.
3122 load_job_cache(const char *filename
) /* I - job.cache filename */
3124 cups_file_t
*fp
; /* job.cache file */
3125 char line
[1024], /* Line buffer */
3126 *value
; /* Value on line */
3127 int linenum
; /* Line number in file */
3128 cupsd_job_t
*job
; /* Current job */
3129 int jobid
; /* Job ID */
3130 char jobfile
[1024]; /* Job filename */
3134 * Open the job.cache file...
3137 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
3139 if (errno
!= ENOENT
)
3140 cupsdLogMessage(CUPSD_LOG_ERROR
,
3141 "Unable to open job cache file \"%s\": %s",
3142 filename
, strerror(errno
));
3144 load_request_root();
3150 * Read entries from the job cache file and create jobs as needed.
3153 cupsdLogMessage(CUPSD_LOG_INFO
, "Loading job cache file \"%s\"...",
3159 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
3161 if (!strcasecmp(line
, "NextJobId"))
3164 NextJobId
= atoi(value
);
3166 else if (!strcasecmp(line
, "<Job"))
3170 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing </Job> directive on line %d!",
3177 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing job ID on line %d!", linenum
);
3181 jobid
= atoi(value
);
3185 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad job ID %d on line %d!", jobid
,
3190 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, jobid
);
3191 if (access(jobfile
, 0))
3193 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Job %d] Files have gone away!",
3198 job
= calloc(1, sizeof(cupsd_job_t
));
3201 cupsdLogMessage(CUPSD_LOG_EMERG
,
3202 "[Job %d] Unable to allocate memory for job!", jobid
);
3207 job
->back_pipes
[0] = -1;
3208 job
->back_pipes
[1] = -1;
3209 job
->print_pipes
[0] = -1;
3210 job
->print_pipes
[1] = -1;
3211 job
->side_pipes
[0] = -1;
3212 job
->side_pipes
[1] = -1;
3213 job
->status_pipes
[0] = -1;
3214 job
->status_pipes
[1] = -1;
3216 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Loading from cache...",
3221 cupsdLogMessage(CUPSD_LOG_ERROR
,
3222 "Missing <Job #> directive on line %d!", linenum
);
3225 else if (!strcasecmp(line
, "</Job>"))
3227 cupsArrayAdd(Jobs
, job
);
3229 if (job
->state_value
<= IPP_JOB_STOPPED
)
3231 cupsArrayAdd(ActiveJobs
, job
);
3239 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing value on line %d!", linenum
);
3242 else if (!strcasecmp(line
, "State"))
3244 job
->state_value
= (ipp_jstate_t
)atoi(value
);
3246 if (job
->state_value
< IPP_JOB_PENDING
)
3247 job
->state_value
= IPP_JOB_PENDING
;
3248 else if (job
->state_value
> IPP_JOB_COMPLETED
)
3249 job
->state_value
= IPP_JOB_COMPLETED
;
3251 else if (!strcasecmp(line
, "HoldUntil"))
3253 job
->hold_until
= atoi(value
);
3255 else if (!strcasecmp(line
, "Priority"))
3257 job
->priority
= atoi(value
);
3259 else if (!strcasecmp(line
, "Username"))
3261 cupsdSetString(&job
->username
, value
);
3263 else if (!strcasecmp(line
, "Destination"))
3265 cupsdSetString(&job
->dest
, value
);
3267 else if (!strcasecmp(line
, "DestType"))
3269 job
->dtype
= (cups_ptype_t
)atoi(value
);
3271 else if (!strcasecmp(line
, "NumFiles"))
3273 job
->num_files
= atoi(value
);
3275 if (job
->num_files
< 0)
3277 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad NumFiles value %d on line %d!",
3278 job
->num_files
, linenum
);
3283 if (job
->num_files
> 0)
3285 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-001", RequestRoot
,
3287 if (access(jobfile
, 0))
3289 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Data files have gone away!",
3295 job
->filetypes
= calloc(job
->num_files
, sizeof(mime_type_t
*));
3296 job
->compressions
= calloc(job
->num_files
, sizeof(int));
3298 if (!job
->filetypes
|| !job
->compressions
)
3300 cupsdLogMessage(CUPSD_LOG_EMERG
,
3301 "[Job %d] Unable to allocate memory for %d files!",
3302 job
->id
, job
->num_files
);
3307 else if (!strcasecmp(line
, "File"))
3309 int number
, /* File number */
3310 compression
; /* Compression value */
3311 char super
[MIME_MAX_SUPER
], /* MIME super type */
3312 type
[MIME_MAX_TYPE
]; /* MIME type */
3315 if (sscanf(value
, "%d%*[ \t]%15[^/]/%255s%d", &number
, super
, type
,
3318 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File on line %d!", linenum
);
3322 if (number
< 1 || number
> job
->num_files
)
3324 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File number %d on line %d!",
3331 job
->compressions
[number
] = compression
;
3332 job
->filetypes
[number
] = mimeType(MimeDatabase
, super
, type
);
3334 if (!job
->filetypes
[number
])
3337 * If the original MIME type is unknown, auto-type it!
3340 cupsdLogMessage(CUPSD_LOG_ERROR
,
3341 "[Job %d] Unknown MIME type %s/%s for file %d!",
3342 job
->id
, super
, type
, number
+ 1);
3344 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
3345 job
->id
, number
+ 1);
3346 job
->filetypes
[number
] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
3347 job
->compressions
+ number
);
3350 * If that didn't work, assume it is raw...
3353 if (!job
->filetypes
[number
])
3354 job
->filetypes
[number
] = mimeType(MimeDatabase
, "application",
3359 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unknown %s directive on line %d!",
3368 * 'load_next_job_id()' - Load the NextJobId value from the job.cache file.
3372 load_next_job_id(const char *filename
) /* I - job.cache filename */
3374 cups_file_t
*fp
; /* job.cache file */
3375 char line
[1024], /* Line buffer */
3376 *value
; /* Value on line */
3377 int linenum
; /* Line number in file */
3378 int next_job_id
; /* NextJobId value from line */
3382 * Read the NextJobId directive from the job.cache file and use
3383 * the value (if any).
3386 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
3388 if (errno
!= ENOENT
)
3389 cupsdLogMessage(CUPSD_LOG_ERROR
,
3390 "Unable to open job cache file \"%s\": %s",
3391 filename
, strerror(errno
));
3396 cupsdLogMessage(CUPSD_LOG_INFO
,
3397 "Loading NextJobId from job cache file \"%s\"...", filename
);
3401 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
3403 if (!strcasecmp(line
, "NextJobId"))
3407 next_job_id
= atoi(value
);
3409 if (next_job_id
> NextJobId
)
3410 NextJobId
= next_job_id
;
3421 * 'load_request_root()' - Load jobs from the RequestRoot directory.
3425 load_request_root(void)
3427 cups_dir_t
*dir
; /* Directory */
3428 cups_dentry_t
*dent
; /* Directory entry */
3429 cupsd_job_t
*job
; /* New job */
3433 * Open the requests directory...
3436 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Scanning %s for jobs...", RequestRoot
);
3438 if ((dir
= cupsDirOpen(RequestRoot
)) == NULL
)
3440 cupsdLogMessage(CUPSD_LOG_ERROR
,
3441 "Unable to open spool directory \"%s\": %s",
3442 RequestRoot
, strerror(errno
));
3447 * Read all the c##### files...
3450 while ((dent
= cupsDirRead(dir
)) != NULL
)
3451 if (strlen(dent
->filename
) >= 6 && dent
->filename
[0] == 'c')
3454 * Allocate memory for the job...
3457 if ((job
= calloc(sizeof(cupsd_job_t
), 1)) == NULL
)
3459 cupsdLogMessage(CUPSD_LOG_ERROR
, "Ran out of memory for jobs!");
3465 * Assign the job ID...
3468 job
->id
= atoi(dent
->filename
+ 1);
3469 job
->back_pipes
[0] = -1;
3470 job
->back_pipes
[1] = -1;
3471 job
->print_pipes
[0] = -1;
3472 job
->print_pipes
[1] = -1;
3473 job
->side_pipes
[0] = -1;
3474 job
->side_pipes
[1] = -1;
3475 job
->status_pipes
[0] = -1;
3476 job
->status_pipes
[1] = -1;
3478 if (job
->id
>= NextJobId
)
3479 NextJobId
= job
->id
+ 1;
3488 * Insert the job into the array, sorting by job priority and ID...
3491 cupsArrayAdd(Jobs
, job
);
3493 if (job
->state_value
<= IPP_JOB_STOPPED
)
3494 cupsArrayAdd(ActiveJobs
, job
);
3504 * 'set_hold_until()' - Set the hold time and update job-hold-until attribute.
3508 set_hold_until(cupsd_job_t
*job
, /* I - Job to update */
3509 time_t holdtime
) /* I - Hold until time */
3511 ipp_attribute_t
*attr
; /* job-hold-until attribute */
3512 struct tm
*holddate
; /* Hold date */
3513 char holdstr
[64]; /* Hold time */
3517 * Set the hold_until value and hold the job...
3520 cupsdLogMessage(CUPSD_LOG_DEBUG
, "set_hold_until: hold_until = %d",
3523 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
3524 job
->state_value
= IPP_JOB_HELD
;
3525 job
->hold_until
= holdtime
;
3528 * Update the job-hold-until attribute with a string representing GMT
3529 * time (HH:MM:SS)...
3532 holddate
= gmtime(&holdtime
);
3533 snprintf(holdstr
, sizeof(holdstr
), "%d:%d:%d", holddate
->tm_hour
,
3534 holddate
->tm_min
, holddate
->tm_sec
);
3536 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
3537 IPP_TAG_KEYWORD
)) == NULL
)
3538 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
3541 * Either add the attribute or update the value of the existing one
3545 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "job-hold-until",
3548 cupsdSetString(&attr
->values
[0].string
.text
, holdstr
);
3551 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
3556 * 'set_time()' - Set one of the "time-at-xyz" attributes.
3560 set_time(cupsd_job_t
*job
, /* I - Job to update */
3561 const char *name
) /* I - Name of attribute */
3563 ipp_attribute_t
*attr
; /* Time attribute */
3566 if ((attr
= ippFindAttribute(job
->attrs
, name
, IPP_TAG_ZERO
)) != NULL
)
3568 attr
->value_tag
= IPP_TAG_INTEGER
;
3569 attr
->values
[0].integer
= time(NULL
);
3575 * 'start_job()' - Start a print job.
3579 start_job(cupsd_job_t
*job
, /* I - Job ID */
3580 cupsd_printer_t
*printer
) /* I - Printer to print job */
3582 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "start_job(job=%p(%d), printer=%p(%s))",
3583 job
, job
->id
, printer
, printer
->name
);
3586 * Make sure we have some files around before we try to print...
3589 if (job
->num_files
== 0)
3591 cupsdSetJobState(job
, IPP_JOB_ABORTED
, CUPSD_JOB_DEFAULT
,
3592 "Aborting job because it has no files.");
3597 * Update the printer and job state to "processing"...
3600 if (!cupsdLoadJob(job
))
3603 cupsdSetJobState(job
, IPP_JOB_PROCESSING
, CUPSD_JOB_DEFAULT
, NULL
);
3604 cupsdSetPrinterState(printer
, IPP_PRINTER_PROCESSING
, 0);
3607 job
->current_file
= 0;
3609 job
->printer
= printer
;
3613 * Setup the last exit status and security profiles...
3617 job
->profile
= cupsdCreateProfile(job
->id
);
3620 * Create the status pipes and buffer...
3623 if (cupsdOpenPipe(job
->status_pipes
))
3625 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
3626 "Unable to create job status pipes - %s.", strerror(errno
));
3628 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
3629 "Job stopped because the scheduler could not create the "
3630 "job status pipes.");
3632 cupsdDestroyProfile(job
->profile
);
3633 job
->profile
= NULL
;
3637 job
->status_buffer
= cupsdStatBufNew(job
->status_pipes
[0], NULL
);
3638 job
->status_level
= CUPSD_LOG_INFO
;
3640 if (job
->printer_message
)
3641 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
), "");
3644 * Create the backchannel pipes and make them non-blocking...
3647 if (cupsdOpenPipe(job
->back_pipes
))
3649 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
3650 "Unable to create back-channel pipes - %s.", strerror(errno
));
3652 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
3653 "Job stopped because the scheduler could not create the "
3654 "back-channel pipes.");
3656 cupsdClosePipe(job
->status_pipes
);
3657 cupsdStatBufDelete(job
->status_buffer
);
3658 job
->status_buffer
= NULL
;
3660 cupsdDestroyProfile(job
->profile
);
3661 job
->profile
= NULL
;
3665 fcntl(job
->back_pipes
[0], F_SETFL
,
3666 fcntl(job
->back_pipes
[0], F_GETFL
) | O_NONBLOCK
);
3667 fcntl(job
->back_pipes
[1], F_SETFL
,
3668 fcntl(job
->back_pipes
[1], F_GETFL
) | O_NONBLOCK
);
3671 * Create the side-channel pipes and make them non-blocking...
3674 if (socketpair(AF_LOCAL
, SOCK_STREAM
, 0, job
->side_pipes
))
3676 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
3677 "Unable to create side-channel pipes - %s.", strerror(errno
));
3679 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
3680 "Job stopped because the scheduler could not create the "
3681 "side-channel pipes.");
3683 cupsdClosePipe(job
->back_pipes
);
3685 cupsdClosePipe(job
->status_pipes
);
3686 cupsdStatBufDelete(job
->status_buffer
);
3687 job
->status_buffer
= NULL
;
3689 cupsdDestroyProfile(job
->profile
);
3690 job
->profile
= NULL
;
3694 fcntl(job
->side_pipes
[0], F_SETFL
,
3695 fcntl(job
->side_pipes
[0], F_GETFL
) | O_NONBLOCK
);
3696 fcntl(job
->side_pipes
[1], F_SETFL
,
3697 fcntl(job
->side_pipes
[1], F_GETFL
) | O_NONBLOCK
);
3700 * Now start the first file in the job...
3703 cupsdContinueJob(job
);
3708 * 'stop_job()' - Stop a print job.
3712 stop_job(cupsd_job_t
*job
, /* I - Job */
3713 cupsd_jobaction_t action
) /* I - Action */
3715 int i
; /* Looping var */
3718 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "stop_job(job=%p(%d), action=%d)", job
,
3721 FilterLevel
-= job
->cost
;
3724 if (action
== CUPSD_JOB_DEFAULT
&& !job
->kill_time
)
3725 job
->kill_time
= time(NULL
) + JobKillDelay
;
3726 else if (action
== CUPSD_JOB_FORCE
)
3729 for (i
= 0; job
->filters
[i
]; i
++)
3730 if (job
->filters
[i
] > 0)
3731 cupsdEndProcess(job
->filters
[i
], action
== CUPSD_JOB_FORCE
);
3733 if (job
->backend
> 0)
3734 cupsdEndProcess(job
->backend
, action
== CUPSD_JOB_FORCE
);
3739 * 'unload_job()' - Unload a job from memory.
3743 unload_job(cupsd_job_t
*job
) /* I - Job */
3748 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Unloading...", job
->id
);
3750 ippDelete(job
->attrs
);
3755 job
->job_sheets
= NULL
;
3756 job
->printer_message
= NULL
;
3757 job
->printer_reasons
= NULL
;
3762 * 'update_job()' - Read a status update from a job's filters.
3766 update_job(cupsd_job_t
*job
) /* I - Job to check */
3768 int i
; /* Looping var */
3769 int copies
; /* Number of copies printed */
3770 char message
[1024], /* Message text */
3771 *ptr
; /* Pointer update... */
3772 int loglevel
, /* Log level for message */
3773 event
= 0; /* Events? */
3774 static const char * const levels
[] = /* Log levels */
3790 * Get the printer associated with this job; if the printer is stopped for
3791 * any reason then job->printer will be reset to NULL, so make sure we have
3792 * a valid pointer...
3795 while ((ptr
= cupsdStatBufUpdate(job
->status_buffer
, &loglevel
,
3796 message
, sizeof(message
))) != NULL
)
3799 * Process page and printer state messages as needed...
3802 if (loglevel
== CUPSD_LOG_PAGE
)
3805 * Page message; send the message to the page_log file and update the
3806 * job sheet count...
3809 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "PAGE: %s", message
);
3813 if (!strncasecmp(message
, "total ", 6))
3816 * Got a total count of pages from a backend or filter...
3819 copies
= atoi(message
+ 6);
3820 copies
-= job
->sheets
->values
[0].integer
; /* Just track the delta */
3822 else if (!sscanf(message
, "%*d%d", &copies
))
3825 job
->sheets
->values
[0].integer
+= copies
;
3827 if (job
->printer
->page_limit
)
3829 cupsd_quota_t
*q
= cupsdUpdateQuota(job
->printer
, job
->username
,
3833 if (AppleQuotas
&& q
->page_count
== -3)
3836 * Quota limit exceeded, cancel job in progress immediately...
3839 cupsdSetJobState(job
, IPP_JOB_CANCELED
, CUPSD_JOB_DEFAULT
,
3840 "Canceled job because pages exceed user %s "
3841 "quota limit on printer %s (%s).",
3842 job
->username
, job
->printer
->name
,
3843 job
->printer
->info
);
3848 #endif /* __APPLE__ */
3852 cupsdLogPage(job
, message
);
3855 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
3856 "Printed %d page(s).", job
->sheets
->values
[0].integer
);
3858 else if (loglevel
== CUPSD_LOG_STATE
)
3860 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "STATE: %s", message
);
3862 if (!strcmp(message
, "paused"))
3864 cupsdStopPrinter(job
->printer
, 1);
3869 cupsdSetPrinterReasons(job
->printer
, message
);
3870 cupsdAddPrinterHistory(job
->printer
);
3871 event
|= CUPSD_EVENT_PRINTER_STATE
;
3874 update_job_attrs(job
, 0);
3876 else if (loglevel
== CUPSD_LOG_ATTR
)
3879 * Set attribute(s)...
3882 int num_attrs
; /* Number of attributes */
3883 cups_option_t
*attrs
; /* Attributes */
3884 const char *attr
; /* Attribute */
3887 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "ATTR: %s", message
);
3889 num_attrs
= cupsParseOptions(message
, 0, &attrs
);
3891 if ((attr
= cupsGetOption("auth-info-required", num_attrs
,
3894 cupsdSetAuthInfoRequired(job
->printer
, attr
, NULL
);
3895 cupsdSetPrinterAttrs(job
->printer
);
3897 if (job
->printer
->type
& CUPS_PRINTER_DISCOVERED
)
3898 cupsdMarkDirty(CUPSD_DIRTY_REMOTE
);
3900 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3903 if ((attr
= cupsGetOption("job-media-progress", num_attrs
,
3906 int progress
= atoi(attr
);
3909 if (progress
>= 0 && progress
<= 100)
3911 job
->progress
= progress
;
3914 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
3915 "Printing page %d, %d%%",
3916 job
->sheets
->values
[0].integer
, job
->progress
);
3920 if ((attr
= cupsGetOption("printer-alert", num_attrs
, attrs
)) != NULL
)
3922 cupsdSetString(&job
->printer
->alert
, attr
);
3923 event
|= CUPSD_EVENT_PRINTER_STATE
;
3926 if ((attr
= cupsGetOption("printer-alert-description", num_attrs
,
3929 cupsdSetString(&job
->printer
->alert_description
, attr
);
3930 event
|= CUPSD_EVENT_PRINTER_STATE
;
3933 if ((attr
= cupsGetOption("marker-colors", num_attrs
, attrs
)) != NULL
)
3935 cupsdSetPrinterAttr(job
->printer
, "marker-colors", (char *)attr
);
3936 job
->printer
->marker_time
= time(NULL
);
3937 event
|= CUPSD_EVENT_PRINTER_STATE
;
3938 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3941 if ((attr
= cupsGetOption("marker-levels", num_attrs
, attrs
)) != NULL
)
3943 cupsdSetPrinterAttr(job
->printer
, "marker-levels", (char *)attr
);
3944 job
->printer
->marker_time
= time(NULL
);
3945 event
|= CUPSD_EVENT_PRINTER_STATE
;
3946 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3949 if ((attr
= cupsGetOption("marker-low-levels", num_attrs
, attrs
)) != NULL
)
3951 cupsdSetPrinterAttr(job
->printer
, "marker-low-levels", (char *)attr
);
3952 job
->printer
->marker_time
= time(NULL
);
3953 event
|= CUPSD_EVENT_PRINTER_STATE
;
3954 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3957 if ((attr
= cupsGetOption("marker-high-levels", num_attrs
, attrs
)) != NULL
)
3959 cupsdSetPrinterAttr(job
->printer
, "marker-high-levels", (char *)attr
);
3960 job
->printer
->marker_time
= time(NULL
);
3961 event
|= CUPSD_EVENT_PRINTER_STATE
;
3962 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3965 if ((attr
= cupsGetOption("marker-message", num_attrs
, attrs
)) != NULL
)
3967 cupsdSetPrinterAttr(job
->printer
, "marker-message", (char *)attr
);
3968 job
->printer
->marker_time
= time(NULL
);
3969 event
|= CUPSD_EVENT_PRINTER_STATE
;
3970 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3973 if ((attr
= cupsGetOption("marker-names", num_attrs
, attrs
)) != NULL
)
3975 cupsdSetPrinterAttr(job
->printer
, "marker-names", (char *)attr
);
3976 job
->printer
->marker_time
= time(NULL
);
3977 event
|= CUPSD_EVENT_PRINTER_STATE
;
3978 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3981 if ((attr
= cupsGetOption("marker-types", num_attrs
, attrs
)) != NULL
)
3983 cupsdSetPrinterAttr(job
->printer
, "marker-types", (char *)attr
);
3984 job
->printer
->marker_time
= time(NULL
);
3985 event
|= CUPSD_EVENT_PRINTER_STATE
;
3986 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3989 cupsFreeOptions(num_attrs
, attrs
);
3991 else if (loglevel
== CUPSD_LOG_PPD
)
3994 * Set attribute(s)...
3997 int num_keywords
; /* Number of keywords */
3998 cups_option_t
*keywords
; /* Keywords */
4001 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "PPD: %s", message
);
4003 num_keywords
= cupsParseOptions(message
, 0, &keywords
);
4005 if (cupsdUpdatePrinterPPD(job
->printer
, num_keywords
, keywords
))
4006 cupsdSetPrinterAttrs(job
->printer
);
4008 cupsFreeOptions(num_keywords
, keywords
);
4011 else if (!strncmp(message
, "recoverable:", 12))
4014 while (isspace(*ptr
& 255))
4019 cupsdSetPrinterReasons(job
->printer
,
4020 "+com.apple.print.recoverable-warning");
4021 cupsdSetString(&(job
->printer
->recoverable
), ptr
);
4022 cupsdAddPrinterHistory(job
->printer
);
4023 event
|= CUPSD_EVENT_PRINTER_STATE
;
4026 else if (!strncmp(message
, "recovered:", 10))
4028 cupsdSetPrinterReasons(job
->printer
,
4029 "-com.apple.print.recoverable-warning");
4032 while (isspace(*ptr
& 255))
4035 cupsdSetString(&(job
->printer
->recoverable
), ptr
);
4036 cupsdAddPrinterHistory(job
->printer
);
4037 event
|= CUPSD_EVENT_PRINTER_STATE
;
4039 #endif /* __APPLE__ */
4042 cupsdLogJob(job
, loglevel
, "%s", message
);
4044 if (loglevel
< CUPSD_LOG_DEBUG
)
4046 strlcpy(job
->printer
->state_message
, message
,
4047 sizeof(job
->printer
->state_message
));
4048 cupsdAddPrinterHistory(job
->printer
);
4050 event
|= CUPSD_EVENT_PRINTER_STATE
| CUPSD_EVENT_JOB_PROGRESS
;
4052 if (loglevel
< job
->status_level
)
4055 * Some messages show in the job-printer-state-message attribute...
4058 if (loglevel
!= CUPSD_LOG_NOTICE
)
4059 job
->status_level
= loglevel
;
4061 update_job_attrs(job
, 1);
4063 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
4064 "Set job-printer-state-message to \"%s\", "
4066 job
->printer_message
->values
[0].string
.text
,
4067 levels
[job
->status_level
]);
4072 if (!strchr(job
->status_buffer
->buffer
, '\n'))
4076 if (event
& CUPSD_EVENT_PRINTER_STATE
)
4077 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, job
->printer
, NULL
,
4078 (job
->printer
->type
& CUPS_PRINTER_CLASS
) ?
4079 "Class \"%s\" state changed." :
4080 "Printer \"%s\" state changed.",
4081 job
->printer
->name
);
4083 if (event
& CUPSD_EVENT_JOB_PROGRESS
)
4084 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
4085 "%s", job
->printer
->state_message
);
4087 if (ptr
== NULL
&& !job
->status_buffer
->bufused
)
4090 * See if all of the filters and the backend have returned their
4094 for (i
= 0; job
->filters
[i
] < 0; i
++);
4096 if (job
->filters
[i
])
4099 * EOF but we haven't collected the exit status of all filters...
4102 cupsdCheckProcess();
4106 if (job
->current_file
>= job
->num_files
&& job
->backend
> 0)
4109 * EOF but we haven't collected the exit status of the backend...
4112 cupsdCheckProcess();
4117 * Handle the end of job stuff...
4123 * Check for new jobs...
4132 * 'update_job_attrs()' - Update the job-printer-* attributes.
4136 update_job_attrs(cupsd_job_t
*job
, /* I - Job to update */
4137 int do_message
)/* I - 1 = copy job-printer-state message */
4139 int i
; /* Looping var */
4140 int num_reasons
; /* Actual number of reasons */
4141 const char * const *reasons
; /* Reasons */
4142 static const char *none
= "none"; /* "none" reason */
4146 * Get/create the job-printer-state-* attributes...
4149 if (!job
->printer_message
)
4151 if ((job
->printer_message
= ippFindAttribute(job
->attrs
,
4152 "job-printer-state-message",
4153 IPP_TAG_TEXT
)) == NULL
)
4154 job
->printer_message
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_TEXT
,
4155 "job-printer-state-message",
4159 if (!job
->printer_reasons
)
4160 job
->printer_reasons
= ippFindAttribute(job
->attrs
,
4161 "job-printer-state-reasons",
4165 * Copy or clear the printer-state-message value as needed...
4168 if (job
->state_value
!= IPP_JOB_PROCESSING
&&
4169 job
->status_level
== CUPSD_LOG_INFO
)
4170 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
), "");
4171 else if (job
->printer
->state_message
[0] && do_message
)
4172 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
),
4173 job
->printer
->state_message
);
4176 * ... and the printer-state-reasons value...
4179 if (job
->printer
->num_reasons
== 0)
4186 num_reasons
= job
->printer
->num_reasons
;
4187 reasons
= (const char * const *)job
->printer
->reasons
;
4190 if (!job
->printer_reasons
|| job
->printer_reasons
->num_values
!= num_reasons
)
4193 * Replace/create a job-printer-state-reasons attribute...
4196 ippDeleteAttribute(job
->attrs
, job
->printer_reasons
);
4198 job
->printer_reasons
= ippAddStrings(job
->attrs
,
4199 IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
4200 "job-printer-state-reasons",
4201 num_reasons
, NULL
, NULL
);
4206 * Don't bother clearing the reason strings if they are the same...
4209 for (i
= 0; i
< num_reasons
; i
++)
4210 if (strcmp(job
->printer_reasons
->values
[i
].string
.text
, reasons
[i
]))
4213 if (i
>= num_reasons
)
4217 * Not the same, so free the current strings...
4220 for (i
= 0; i
< num_reasons
; i
++)
4221 _cupsStrFree(job
->printer_reasons
->values
[i
].string
.text
);
4225 * Copy the reasons...
4228 for (i
= 0; i
< num_reasons
; i
++)
4229 job
->printer_reasons
->values
[i
].string
.text
= _cupsStrAlloc(reasons
[i
]);
4234 * End of "$Id: job.c 7902 2008-09-03 14:20:17Z mike $".