2 * "$Id: job.c 7902 2008-09-03 14:20:17Z mike $"
4 * Job management routines for the CUPS scheduler.
6 * Copyright 2007-2012 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
17 * cupsdAddJob() - Add a new job to the job queue.
18 * cupsdCancelJobs() - Cancel all jobs for the given
20 * cupsdCheckJobs() - Check the pending jobs and start any if the
21 * destination is available.
22 * cupsdCleanJobs() - Clean out old jobs.
23 * cupsdContinueJob() - Continue printing with the next file in a job.
24 * cupsdDeleteJob() - Free all memory used by a job.
25 * cupsdFreeAllJobs() - Free all jobs from memory.
26 * cupsdFindJob() - Find the specified job.
27 * cupsdGetPrinterJobCount() - Get the number of pending, processing, or held
28 * jobs in a printer or class.
29 * cupsdGetUserJobCount() - Get the number of pending, processing, or held
31 * cupsdLoadAllJobs() - Load all jobs from disk.
32 * cupsdLoadJob() - Load a single job.
33 * cupsdMoveJob() - Move the specified job to a different
35 * cupsdReleaseJob() - Release the specified job.
36 * cupsdRestartJob() - Restart the specified job.
37 * cupsdSaveAllJobs() - Save a summary of all jobs to disk.
38 * cupsdSaveJob() - Save a job to disk.
39 * cupsdSetJobHoldUntil() - Set the hold time for a job.
40 * cupsdSetJobPriority() - Set the priority of a job, moving it up/down
41 * in the list as needed.
42 * cupsdSetJobState() - Set the state of the specified print job.
43 * cupsdStopAllJobs() - Stop all print jobs.
44 * cupsdUnloadCompletedJobs() - Flush completed job history from memory.
45 * compare_active_jobs() - Compare the job IDs and priorities of two
47 * compare_jobs() - Compare the job IDs of two jobs.
48 * dump_job_history() - Dump any debug messages for a job.
49 * free_job_history() - Free any log history.
50 * finalize_job() - Cleanup after job filter processes and support
52 * get_options() - Get a string containing the job options.
53 * ipp_length() - Compute the size of the buffer needed to hold
54 * the textual IPP attributes.
55 * load_job_cache() - Load jobs from the job.cache file.
56 * load_next_job_id() - Load the NextJobId value from the job.cache
58 * load_request_root() - Load jobs from the RequestRoot directory.
59 * set_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>
76 # include <IOKit/pwr_mgt/IOPMLib.h>
77 # ifdef HAVE_IOKIT_PWR_MGT_IOPMLIBPRIVATE_H
78 # include <IOKit/pwr_mgt/IOPMLibPrivate.h>
79 # endif /* HAVE_IOKIT_PWR_MGT_IOPMLIBPRIVATE_H */
80 #endif /* __APPLE__ */
84 * Design Notes for Job Management
85 * -------------------------------
89 * pending Do nothing/check jobs
90 * pending-held Send SIGTERM to filters and backend
91 * processing Do nothing/start job
92 * stopped Send SIGKILL to filters and backend
93 * canceled Send SIGTERM to filters and backend
97 * Finalize clears the printer <-> job association, deletes the status
98 * buffer, closes all of the pipes, etc. and doesn't get run until all of
99 * the print processes are finished.
101 * UNLOADING OF JOBS (cupsdUnloadCompletedJobs)
103 * We unload the job attributes when they are not needed to reduce overall
104 * memory consumption. We don't unload jobs where job->state_value <
105 * IPP_JOB_STOPPED, job->printer != NULL, or job->access_time is recent.
107 * STARTING OF JOBS (start_job)
109 * When a job is started, a status buffer, several pipes, a security
110 * profile, and a backend process are created for the life of that job.
111 * These are shared for every file in a job. For remote print jobs, the
112 * IPP backend is provided with every file in the job and no filters are
115 * The job->printer member tracks which printer is printing a job, which
116 * can be different than the destination in job->dest for classes. The
117 * printer object also has a job pointer to track which job is being
120 * PRINTING OF JOB FILES (cupsdContinueJob)
122 * Each file in a job is filtered by 0 or more programs. After getting the
123 * list of filters needed and the total cost, the job is either passed or
124 * put back to the processing state until the current FilterLevel comes down
125 * enough to allow printing.
127 * If we can print, we build a string for the print options and run each of
128 * the filters, piping the output from one into the next.
130 * JOB STATUS UPDATES (update_job)
132 * The update_job function gets called whenever there are pending messages
133 * on the status pipe. These generally are updates to the marker-*,
134 * printer-state-message, or printer-state-reasons attributes. On EOF,
135 * finalize_job is called to clean up.
137 * FINALIZING JOBS (finalize_job)
139 * When all filters and the backend are done, we set the job state to
140 * completed (no errors), aborted (filter errors or abort-job policy),
141 * pending-held (auth required or retry-job policy), or pending
142 * (retry-current-job or stop-printer policies) as appropriate.
144 * Then we close the pipes and free the status buffers and profiles.
146 * JOB FILE COMPLETION (process_children in main.c)
148 * For multiple-file jobs, process_children (in main.c) sees that all
149 * filters have exited and calls in to print the next file if there are
150 * more files in the job, otherwise it waits for the backend to exit and
151 * update_job to do the cleanup.
159 static mime_filter_t gziptoany_filter
=
161 NULL
, /* Source type */
162 NULL
, /* Destination type */
164 "gziptoany" /* Filter program to run */
172 static int compare_active_jobs(void *first
, void *second
, void *data
);
173 static int compare_jobs(void *first
, void *second
, void *data
);
174 static void dump_job_history(cupsd_job_t
*job
);
175 static void finalize_job(cupsd_job_t
*job
, int set_job_state
);
176 static void free_job_history(cupsd_job_t
*job
);
177 static char *get_options(cupsd_job_t
*job
, int banner_page
, char *copies
,
178 size_t copies_size
, char *title
,
180 static size_t ipp_length(ipp_t
*ipp
);
181 static void load_job_cache(const char *filename
);
182 static void load_next_job_id(const char *filename
);
183 static void load_request_root(void);
184 static void set_time(cupsd_job_t
*job
, const char *name
);
185 static void start_job(cupsd_job_t
*job
, cupsd_printer_t
*printer
);
186 static void stop_job(cupsd_job_t
*job
, cupsd_jobaction_t action
);
187 static void unload_job(cupsd_job_t
*job
);
188 static void update_job(cupsd_job_t
*job
);
189 static void update_job_attrs(cupsd_job_t
*job
, int do_message
);
193 * 'cupsdAddJob()' - Add a new job to the job queue.
196 cupsd_job_t
* /* O - New job record */
197 cupsdAddJob(int priority
, /* I - Job priority */
198 const char *dest
) /* I - Job destination */
200 cupsd_job_t
*job
; /* New job record */
203 if ((job
= calloc(sizeof(cupsd_job_t
), 1)) == NULL
)
206 job
->id
= NextJobId
++;
207 job
->priority
= priority
;
208 job
->back_pipes
[0] = -1;
209 job
->back_pipes
[1] = -1;
210 job
->print_pipes
[0] = -1;
211 job
->print_pipes
[1] = -1;
212 job
->side_pipes
[0] = -1;
213 job
->side_pipes
[1] = -1;
214 job
->status_pipes
[0] = -1;
215 job
->status_pipes
[1] = -1;
217 cupsdSetString(&job
->dest
, dest
);
220 * Add the new job to the "all jobs" and "active jobs" lists...
223 cupsArrayAdd(Jobs
, job
);
224 cupsArrayAdd(ActiveJobs
, job
);
231 * 'cupsdCancelJobs()' - Cancel all jobs for the given destination/user.
235 cupsdCancelJobs(const char *dest
, /* I - Destination to cancel */
236 const char *username
, /* I - Username or NULL */
237 int purge
) /* I - Purge jobs? */
239 cupsd_job_t
*job
; /* Current job */
242 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
244 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
246 if ((!job
->dest
|| !job
->username
) && !cupsdLoadJob(job
))
249 if ((!dest
|| !strcmp(job
->dest
, dest
)) &&
250 (!username
|| !strcmp(job
->username
, username
)))
253 * Cancel all jobs matching this destination/user...
257 cupsdSetJobState(job
, IPP_JOB_CANCELED
, CUPSD_JOB_PURGE
,
258 "Job purged by user.");
259 else if (job
->state_value
< IPP_JOB_CANCELED
)
260 cupsdSetJobState(job
, IPP_JOB_CANCELED
, CUPSD_JOB_DEFAULT
,
261 "Job canceled by user.");
270 * 'cupsdCheckJobs()' - Check the pending jobs and start any if the destination
277 cupsd_job_t
*job
; /* Current job in queue */
278 cupsd_printer_t
*printer
, /* Printer destination */
279 *pclass
; /* Printer class destination */
280 ipp_attribute_t
*attr
; /* Job attribute */
281 time_t curtime
; /* Current time */
284 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
285 "cupsdCheckJobs: %d active jobs, sleeping=%d, reload=%d",
286 cupsArrayCount(ActiveJobs
), Sleeping
, NeedReload
);
288 curtime
= time(NULL
);
290 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
292 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
295 * Kill jobs if they are unresponsive...
298 if (job
->kill_time
&& job
->kill_time
<= curtime
)
300 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Job %d] Stopping unresponsive job.",
303 stop_job(job
, CUPSD_JOB_FORCE
);
308 * Cancel stuck jobs...
311 if (job
->cancel_time
&& job
->cancel_time
<= curtime
)
313 cupsdSetJobState(job
, IPP_JOB_CANCELED
, CUPSD_JOB_DEFAULT
,
314 "Canceling stuck job after %d seconds.", MaxJobTime
);
319 * Start held jobs if they are ready...
322 if (job
->state_value
== IPP_JOB_HELD
&&
324 job
->hold_until
< curtime
)
326 if (job
->pending_timeout
)
329 * This job is pending; check that we don't have an active Send-Document
330 * operation in progress on any of the client connections, then timeout
331 * the job so we can start printing...
334 cupsd_client_t
*con
; /* Current client connection */
337 for (con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
339 con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
341 con
->request
->request
.op
.operation_id
== IPP_SEND_DOCUMENT
)
347 if (cupsdTimeoutJob(job
))
351 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
352 "Job submission timed out.");
356 * Continue jobs that are waiting on the FilterLimit...
359 if (job
->pending_cost
> 0 &&
360 ((FilterLevel
+ job
->pending_cost
) < FilterLimit
|| FilterLevel
== 0))
361 cupsdContinueJob(job
);
364 * Start pending jobs if the destination is available...
367 if (job
->state_value
== IPP_JOB_PENDING
&& !NeedReload
&&
368 #ifndef kIOPMAssertionTypeDenySystemSleep
370 #endif /* !kIOPMAssertionTypeDenySystemSleep */
371 !DoingShutdown
&& !job
->printer
)
373 printer
= cupsdFindDest(job
->dest
);
376 while (printer
&& (printer
->type
& CUPS_PRINTER_CLASS
))
379 * If the class is remote, just pass it to the remote server...
384 if (pclass
->state
== IPP_PRINTER_STOPPED
)
386 else if (pclass
->type
& CUPS_PRINTER_REMOTE
)
389 printer
= cupsdFindAvailablePrinter(printer
->name
);
392 if (!printer
&& !pclass
)
395 * Whoa, the printer and/or class for this destination went away;
399 cupsdSetJobState(job
, IPP_JOB_ABORTED
, CUPSD_JOB_PURGE
,
400 "Job aborted because the destination printer/class "
403 else if (printer
&& !printer
->holding_new_jobs
)
406 * See if the printer is available or remote and not printing a job;
407 * if so, start the job...
413 * Add/update a job-actual-printer-uri attribute for this job
414 * so that we know which printer actually printed the job...
417 if ((attr
= ippFindAttribute(job
->attrs
, "job-actual-printer-uri",
418 IPP_TAG_URI
)) != NULL
)
419 cupsdSetString(&attr
->values
[0].string
.text
, printer
->uri
);
421 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
,
422 "job-actual-printer-uri", NULL
, printer
->uri
);
425 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
428 if (printer
->state
== IPP_PRINTER_IDLE
)
434 start_job(job
, printer
);
443 * 'cupsdCleanJobs()' - Clean out old jobs.
449 cupsd_job_t
*job
; /* Current job */
452 if (MaxJobs
<= 0 && JobHistory
)
455 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
456 job
&& (cupsArrayCount(Jobs
) >= MaxJobs
|| !JobHistory
);
457 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
458 if (job
->state_value
>= IPP_JOB_CANCELED
&& !job
->printer
)
459 cupsdDeleteJob(job
, CUPSD_JOB_PURGE
);
464 * 'cupsdContinueJob()' - Continue printing with the next file in a job.
468 cupsdContinueJob(cupsd_job_t
*job
) /* I - Job */
470 int i
; /* Looping var */
471 int slot
; /* Pipe slot */
472 cups_array_t
*filters
= NULL
,/* Filters for job */
473 *prefilters
; /* Filters with prefilters */
474 mime_filter_t
*filter
, /* Current filter */
475 *prefilter
, /* Prefilter */
476 port_monitor
; /* Port monitor filter */
477 char scheme
[255]; /* Device URI scheme */
478 ipp_attribute_t
*attr
; /* Current attribute */
479 const char *ptr
, /* Pointer into value */
480 *abort_message
; /* Abort message */
481 ipp_jstate_t abort_state
= IPP_JOB_STOPPED
;
482 /* New job state on abort */
483 struct stat backinfo
; /* Backend file information */
484 int backroot
; /* Run backend as root? */
485 int pid
; /* Process ID of new filter process */
486 int banner_page
; /* 1 if banner page, 0 otherwise */
487 int filterfds
[2][2] = { { -1, -1 }, { -1, -1 } };
488 /* Pipes used between filters */
489 int envc
; /* Number of environment variables */
490 struct stat fileinfo
; /* Job file information */
491 char **argv
= NULL
, /* Filter command-line arguments */
492 filename
[1024], /* Job filename */
493 command
[1024], /* Full path to command */
494 jobid
[255], /* Job ID string */
496 /* Job title string */
497 copies
[255], /* # copies string */
498 *options
, /* Options string */
500 /* Environment variables */
501 charset
[255], /* CHARSET env variable */
502 class_name
[255],/* CLASS env variable */
503 classification
[1024],
504 /* CLASSIFICATION env variable */
506 /* CONTENT_TYPE env variable */
508 /* DEVICE_URI env variable */
509 final_content_type
[1024],
510 /* FINAL_CONTENT_TYPE env variable */
511 lang
[255], /* LANG env variable */
514 /* APPLE_LANGUAGE env variable */
515 #endif /* __APPLE__ */
516 auth_info_required
[255],
517 /* AUTH_INFO_REQUIRED env variable */
518 ppd
[1024], /* PPD env variable */
520 /* PRINTER_INFO env variable */
521 printer_location
[255],
522 /* PRINTER_LOCATION env variable */
524 /* PRINTER env variable */
525 *printer_state_reasons
= NULL
,
526 /* PRINTER_STATE_REASONS env var */
528 /* RIP_MAX_CACHE env variable */
531 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
532 "cupsdContinueJob(job=%p(%d)): current_file=%d, num_files=%d",
533 job
, job
->id
, job
->current_file
, job
->num_files
);
536 * Figure out what filters are required to convert from
537 * the source to the destination type...
540 FilterLevel
-= job
->cost
;
543 job
->pending_cost
= 0;
545 memset(job
->filters
, 0, sizeof(job
->filters
));
548 if (job
->printer
->raw
)
551 * Remote jobs and raw queues go directly to the printer without
555 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Sending job to queue tagged as raw...");
560 * Local jobs get filtered...
563 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
564 job
->id
, job
->current_file
+ 1);
565 if (stat(filename
, &fileinfo
))
566 fileinfo
.st_size
= 0;
568 filters
= mimeFilter2(MimeDatabase
, job
->filetypes
[job
->current_file
],
569 fileinfo
.st_size
, job
->printer
->filetype
,
574 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
575 "Unable to convert file %d to printable format.",
578 abort_message
= "Aborting job because it cannot be printed.";
579 abort_state
= IPP_JOB_ABORTED
;
581 ippSetString(job
->attrs
, &job
->reasons
, 0, "document-unprintable-error");
586 * Remove NULL ("-") filters...
589 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
591 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
592 if (!strcmp(filter
->filter
, "-"))
593 cupsArrayRemove(filters
, filter
);
595 if (cupsArrayCount(filters
) == 0)
597 cupsArrayDelete(filters
);
602 * If this printer has any pre-filters, insert the required pre-filter
603 * in the filters array...
606 if (job
->printer
->prefiltertype
&& filters
)
608 prefilters
= cupsArrayNew(NULL
, NULL
);
610 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
612 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
614 if ((prefilter
= mimeFilterLookup(MimeDatabase
, filter
->src
,
615 job
->printer
->prefiltertype
)))
617 cupsArrayAdd(prefilters
, prefilter
);
618 job
->cost
+= prefilter
->cost
;
621 cupsArrayAdd(prefilters
, filter
);
624 cupsArrayDelete(filters
);
625 filters
= prefilters
;
630 * Set a minimum cost of 100 for all jobs so that FilterLimit
631 * works with raw queues and other low-cost paths.
638 * See if the filter cost is too high...
641 if ((FilterLevel
+ job
->cost
) > FilterLimit
&& FilterLevel
> 0 &&
645 * Don't print this job quite yet...
648 cupsArrayDelete(filters
);
650 cupsdLogJob(job
, CUPSD_LOG_INFO
,
651 "Holding because filter limit has been reached.");
652 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
653 "cupsdContinueJob: file=%d, cost=%d, level=%d, limit=%d",
654 job
->current_file
, job
->cost
, FilterLevel
,
657 job
->pending_cost
= job
->cost
;
662 FilterLevel
+= job
->cost
;
665 * Add decompression/raw filter as needed...
668 if ((!job
->printer
->raw
&& job
->compressions
[job
->current_file
]) ||
669 (!filters
&& !job
->printer
->remote
&&
670 (job
->num_files
> 1 || !strncmp(job
->printer
->device_uri
, "file:", 5))))
673 * Add gziptoany filter to the front of the list...
677 filters
= cupsArrayNew(NULL
, NULL
);
679 if (!cupsArrayInsert(filters
, &gziptoany_filter
))
681 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
682 "Unable to add decompression filter - %s", strerror(errno
));
684 cupsArrayDelete(filters
);
686 abort_message
= "Stopping job because the scheduler ran out of memory.";
693 * Add port monitor, if any...
696 if (job
->printer
->port_monitor
)
699 * Add port monitor to the end of the list...
703 filters
= cupsArrayNew(NULL
, NULL
);
705 port_monitor
.src
= NULL
;
706 port_monitor
.dst
= NULL
;
707 port_monitor
.cost
= 0;
709 snprintf(port_monitor
.filter
, sizeof(port_monitor
.filter
),
710 "%s/monitor/%s", ServerBin
, job
->printer
->port_monitor
);
712 if (!cupsArrayAdd(filters
, &port_monitor
))
714 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
715 "Unable to add port monitor - %s", strerror(errno
));
717 abort_message
= "Stopping job because the scheduler ran out of memory.";
724 * Make sure we don't go over the "MAX_FILTERS" limit...
727 if (cupsArrayCount(filters
) > MAX_FILTERS
)
729 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
730 "Too many filters (%d > %d), unable to print.",
731 cupsArrayCount(filters
), MAX_FILTERS
);
733 abort_message
= "Aborting job because it needs too many filters to print.";
734 abort_state
= IPP_JOB_ABORTED
;
736 ippSetString(job
->attrs
, &job
->reasons
, 0, "document-unprintable-error");
742 * Determine if we are printing a banner page or not...
745 if (job
->job_sheets
== NULL
)
747 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "No job-sheets attribute.");
748 if ((job
->job_sheets
=
749 ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_ZERO
)) != NULL
)
750 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
751 "... but someone added one without setting job_sheets.");
753 else if (job
->job_sheets
->num_values
== 1)
754 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "job-sheets=%s",
755 job
->job_sheets
->values
[0].string
.text
);
757 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "job-sheets=%s,%s",
758 job
->job_sheets
->values
[0].string
.text
,
759 job
->job_sheets
->values
[1].string
.text
);
761 if (job
->printer
->type
& CUPS_PRINTER_REMOTE
)
763 else if (job
->job_sheets
== NULL
)
765 else if (_cups_strcasecmp(job
->job_sheets
->values
[0].string
.text
, "none") != 0 &&
766 job
->current_file
== 0)
768 else if (job
->job_sheets
->num_values
> 1 &&
769 _cups_strcasecmp(job
->job_sheets
->values
[1].string
.text
, "none") != 0 &&
770 job
->current_file
== (job
->num_files
- 1))
775 if ((options
= get_options(job
, banner_page
, copies
, sizeof(copies
), title
,
776 sizeof(title
))) == NULL
)
778 abort_message
= "Stopping job because the scheduler ran out of memory.";
784 * Build the command-line arguments for the filters. Each filter
785 * has 6 or 7 arguments:
793 * argv[6] = filename (optional; normally stdin)
795 * This allows legacy printer drivers that use the old System V
796 * printing interface to be used by CUPS.
798 * For remote jobs, we send all of the files in the argument list.
801 if (job
->printer
->remote
)
802 argv
= calloc(7 + job
->num_files
, sizeof(char *));
804 argv
= calloc(8, sizeof(char *));
808 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Unable to allocate argument array - %s",
811 abort_message
= "Stopping job because the scheduler ran out of memory.";
816 sprintf(jobid
, "%d", job
->id
);
818 argv
[0] = job
->printer
->name
;
820 argv
[2] = job
->username
;
825 if (job
->printer
->remote
&& job
->num_files
> 1)
827 for (i
= 0; i
< job
->num_files
; i
++)
829 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
831 argv
[6 + i
] = strdup(filename
);
836 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
837 job
->id
, job
->current_file
+ 1);
841 for (i
= 0; argv
[i
]; i
++)
842 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "argv[%d]=\"%s\"", i
, argv
[i
]);
845 * Create environment variable strings for the filters...
848 attr
= ippFindAttribute(job
->attrs
, "attributes-natural-language",
852 strcpy(apple_language
, "APPLE_LANGUAGE=");
853 _cupsAppleLanguage(attr
->values
[0].string
.text
,
854 apple_language
+ 15, sizeof(apple_language
) - 15);
855 #endif /* __APPLE__ */
857 switch (strlen(attr
->values
[0].string
.text
))
861 * This is an unknown or badly formatted language code; use
862 * the POSIX locale...
865 strcpy(lang
, "LANG=C");
870 * Just the language code (ll)...
873 snprintf(lang
, sizeof(lang
), "LANG=%s.UTF-8",
874 attr
->values
[0].string
.text
);
879 * Language and country code (ll-cc)...
882 snprintf(lang
, sizeof(lang
), "LANG=%c%c_%c%c.UTF-8",
883 attr
->values
[0].string
.text
[0],
884 attr
->values
[0].string
.text
[1],
885 toupper(attr
->values
[0].string
.text
[3] & 255),
886 toupper(attr
->values
[0].string
.text
[4] & 255));
890 if ((attr
= ippFindAttribute(job
->attrs
, "document-format",
891 IPP_TAG_MIMETYPE
)) != NULL
&&
892 (ptr
= strstr(attr
->values
[0].string
.text
, "charset=")) != NULL
)
893 snprintf(charset
, sizeof(charset
), "CHARSET=%s", ptr
+ 8);
895 strlcpy(charset
, "CHARSET=utf-8", sizeof(charset
));
897 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s/%s",
898 job
->filetypes
[job
->current_file
]->super
,
899 job
->filetypes
[job
->current_file
]->type
);
900 snprintf(device_uri
, sizeof(device_uri
), "DEVICE_URI=%s",
901 job
->printer
->device_uri
);
902 snprintf(ppd
, sizeof(ppd
), "PPD=%s/ppd/%s.ppd", ServerRoot
,
904 snprintf(printer_info
, sizeof(printer_name
), "PRINTER_INFO=%s",
905 job
->printer
->info
? job
->printer
->info
: "");
906 snprintf(printer_location
, sizeof(printer_name
), "PRINTER_LOCATION=%s",
907 job
->printer
->location
? job
->printer
->location
: "");
908 snprintf(printer_name
, sizeof(printer_name
), "PRINTER=%s", job
->printer
->name
);
909 if (job
->printer
->num_reasons
> 0)
911 char *psrptr
; /* Pointer into PRINTER_STATE_REASONS */
912 size_t psrlen
; /* Size of PRINTER_STATE_REASONS */
914 for (psrlen
= 22, i
= 0; i
< job
->printer
->num_reasons
; i
++)
915 psrlen
+= strlen(job
->printer
->reasons
[i
]) + 1;
917 if ((printer_state_reasons
= malloc(psrlen
)) != NULL
)
920 * All of these strcpy's are safe because we allocated the psr string...
923 strcpy(printer_state_reasons
, "PRINTER_STATE_REASONS=");
924 for (psrptr
= printer_state_reasons
+ 22, i
= 0;
925 i
< job
->printer
->num_reasons
;
930 strcpy(psrptr
, job
->printer
->reasons
[i
]);
931 psrptr
+= strlen(psrptr
);
935 snprintf(rip_max_cache
, sizeof(rip_max_cache
), "RIP_MAX_CACHE=%s", RIPCache
);
937 if (job
->printer
->num_auth_info_required
== 1)
938 snprintf(auth_info_required
, sizeof(auth_info_required
),
939 "AUTH_INFO_REQUIRED=%s",
940 job
->printer
->auth_info_required
[0]);
941 else if (job
->printer
->num_auth_info_required
== 2)
942 snprintf(auth_info_required
, sizeof(auth_info_required
),
943 "AUTH_INFO_REQUIRED=%s,%s",
944 job
->printer
->auth_info_required
[0],
945 job
->printer
->auth_info_required
[1]);
946 else if (job
->printer
->num_auth_info_required
== 3)
947 snprintf(auth_info_required
, sizeof(auth_info_required
),
948 "AUTH_INFO_REQUIRED=%s,%s,%s",
949 job
->printer
->auth_info_required
[0],
950 job
->printer
->auth_info_required
[1],
951 job
->printer
->auth_info_required
[2]);
952 else if (job
->printer
->num_auth_info_required
== 4)
953 snprintf(auth_info_required
, sizeof(auth_info_required
),
954 "AUTH_INFO_REQUIRED=%s,%s,%s,%s",
955 job
->printer
->auth_info_required
[0],
956 job
->printer
->auth_info_required
[1],
957 job
->printer
->auth_info_required
[2],
958 job
->printer
->auth_info_required
[3]);
960 strlcpy(auth_info_required
, "AUTH_INFO_REQUIRED=none",
961 sizeof(auth_info_required
));
963 envc
= cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
965 envp
[envc
++] = charset
;
966 envp
[envc
++] = lang
;
968 envp
[envc
++] = apple_language
;
969 #endif /* __APPLE__ */
971 envp
[envc
++] = rip_max_cache
;
972 envp
[envc
++] = content_type
;
973 envp
[envc
++] = device_uri
;
974 envp
[envc
++] = printer_info
;
975 envp
[envc
++] = printer_location
;
976 envp
[envc
++] = printer_name
;
977 envp
[envc
++] = printer_state_reasons
? printer_state_reasons
:
978 "PRINTER_STATE_REASONS=none";
979 envp
[envc
++] = banner_page
? "CUPS_FILETYPE=job-sheet" :
980 "CUPS_FILETYPE=document";
982 if (!job
->printer
->remote
&& !job
->printer
->raw
)
984 filter
= (mime_filter_t
*)cupsArrayLast(filters
);
986 if (job
->printer
->port_monitor
)
987 filter
= (mime_filter_t
*)cupsArrayPrev(filters
);
989 if (filter
&& filter
->dst
)
991 if ((ptr
= strchr(filter
->dst
->type
, '/')) != NULL
)
992 snprintf(final_content_type
, sizeof(final_content_type
),
993 "FINAL_CONTENT_TYPE=%s", ptr
+ 1);
995 snprintf(final_content_type
, sizeof(final_content_type
),
996 "FINAL_CONTENT_TYPE=%s/%s", filter
->dst
->super
,
998 envp
[envc
++] = final_content_type
;
1002 if (Classification
&& !banner_page
)
1004 if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
1005 IPP_TAG_NAME
)) == NULL
)
1006 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
1008 else if (attr
->num_values
> 1 &&
1009 strcmp(attr
->values
[1].string
.text
, "none") != 0)
1010 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
1011 attr
->values
[1].string
.text
);
1013 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
1014 attr
->values
[0].string
.text
);
1016 envp
[envc
++] = classification
;
1019 if (job
->dtype
& CUPS_PRINTER_CLASS
)
1021 snprintf(class_name
, sizeof(class_name
), "CLASS=%s", job
->dest
);
1022 envp
[envc
++] = class_name
;
1025 envp
[envc
++] = auth_info_required
;
1028 i
< (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0]));
1030 if (job
->auth_env
[i
])
1031 envp
[envc
++] = job
->auth_env
[i
];
1036 envp
[envc
++] = job
->auth_uid
;
1040 for (i
= 0; i
< envc
; i
++)
1041 if (!strncmp(envp
[i
], "AUTH_", 5))
1042 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"AUTH_%c****\"", i
,
1044 else if (strncmp(envp
[i
], "DEVICE_URI=", 11))
1045 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"%s\"", i
, envp
[i
]);
1047 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"DEVICE_URI=%s\"", i
,
1048 job
->printer
->sanitized_device_uri
);
1050 if (job
->printer
->remote
)
1051 job
->current_file
= job
->num_files
;
1053 job
->current_file
++;
1056 * Now create processes for all of the filters...
1059 cupsdSetPrinterReasons(job
->printer
, "-cups-missing-filter-warning,"
1060 "cups-insecure-filter-warning");
1062 for (i
= 0, slot
= 0, filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
1064 i
++, filter
= (mime_filter_t
*)cupsArrayNext(filters
))
1066 if (filter
->filter
[0] != '/')
1067 snprintf(command
, sizeof(command
), "%s/filter/%s", ServerBin
,
1070 strlcpy(command
, filter
->filter
, sizeof(command
));
1072 if (i
< (cupsArrayCount(filters
) - 1))
1074 if (cupsdOpenPipe(filterfds
[slot
]))
1076 abort_message
= "Stopping job because the scheduler could not create "
1077 "the filter pipes.";
1084 if (job
->current_file
== 1 ||
1085 (job
->printer
->pc
&& job
->printer
->pc
->single_file
))
1087 if (strncmp(job
->printer
->device_uri
, "file:", 5) != 0)
1089 if (cupsdOpenPipe(job
->print_pipes
))
1091 abort_message
= "Stopping job because the scheduler could not "
1092 "create the backend pipes.";
1099 job
->print_pipes
[0] = -1;
1100 if (!strcmp(job
->printer
->device_uri
, "file:/dev/null") ||
1101 !strcmp(job
->printer
->device_uri
, "file:///dev/null"))
1102 job
->print_pipes
[1] = -1;
1105 if (!strncmp(job
->printer
->device_uri
, "file:/dev/", 10))
1106 job
->print_pipes
[1] = open(job
->printer
->device_uri
+ 5,
1108 else if (!strncmp(job
->printer
->device_uri
, "file:///dev/", 12))
1109 job
->print_pipes
[1] = open(job
->printer
->device_uri
+ 7,
1111 else if (!strncmp(job
->printer
->device_uri
, "file:///", 8))
1112 job
->print_pipes
[1] = open(job
->printer
->device_uri
+ 7,
1113 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
1115 job
->print_pipes
[1] = open(job
->printer
->device_uri
+ 5,
1116 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
1118 if (job
->print_pipes
[1] < 0)
1120 abort_message
= "Stopping job because the scheduler could not "
1121 "open the output file.";
1126 fcntl(job
->print_pipes
[1], F_SETFD
,
1127 fcntl(job
->print_pipes
[1], F_GETFD
) | FD_CLOEXEC
);
1132 filterfds
[slot
][0] = job
->print_pipes
[0];
1133 filterfds
[slot
][1] = job
->print_pipes
[1];
1136 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
1137 filterfds
[slot
][1], job
->status_pipes
[1],
1138 job
->back_pipes
[0], job
->side_pipes
[0], 0,
1139 job
->profile
, job
, job
->filters
+ i
);
1141 cupsdClosePipe(filterfds
[!slot
]);
1145 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Unable to start filter \"%s\" - %s.",
1146 filter
->filter
, strerror(errno
));
1148 abort_message
= "Stopping job because the scheduler could not execute a "
1154 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Started filter %s (PID %d)", command
,
1161 cupsArrayDelete(filters
);
1165 * Finally, pipe the final output into a backend process if needed...
1168 if (strncmp(job
->printer
->device_uri
, "file:", 5) != 0)
1170 if (job
->current_file
== 1 || job
->printer
->remote
||
1171 (job
->printer
->pc
&& job
->printer
->pc
->single_file
))
1173 sscanf(job
->printer
->device_uri
, "%254[^:]", scheme
);
1174 snprintf(command
, sizeof(command
), "%s/backend/%s", ServerBin
, scheme
);
1177 * See if the backend needs to run as root...
1182 else if (stat(command
, &backinfo
))
1185 backroot
= !(backinfo
.st_mode
& (S_IRWXG
| S_IRWXO
));
1187 argv
[0] = job
->printer
->sanitized_device_uri
;
1189 filterfds
[slot
][0] = -1;
1190 filterfds
[slot
][1] = -1;
1192 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
1193 filterfds
[slot
][1], job
->status_pipes
[1],
1194 job
->back_pipes
[1], job
->side_pipes
[1],
1195 backroot
, job
->profile
, job
, &(job
->backend
));
1199 abort_message
= "Stopping job because the sheduler could not execute "
1206 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Started backend %s (PID %d)",
1211 if (job
->current_file
== job
->num_files
||
1212 (job
->printer
->pc
&& job
->printer
->pc
->single_file
))
1213 cupsdClosePipe(job
->print_pipes
);
1215 if (job
->current_file
== job
->num_files
)
1217 cupsdClosePipe(job
->back_pipes
);
1218 cupsdClosePipe(job
->side_pipes
);
1220 close(job
->status_pipes
[1]);
1221 job
->status_pipes
[1] = -1;
1226 filterfds
[slot
][0] = -1;
1227 filterfds
[slot
][1] = -1;
1229 if (job
->current_file
== job
->num_files
||
1230 (job
->printer
->pc
&& job
->printer
->pc
->single_file
))
1231 cupsdClosePipe(job
->print_pipes
);
1233 if (job
->current_file
== job
->num_files
)
1235 close(job
->status_pipes
[1]);
1236 job
->status_pipes
[1] = -1;
1240 cupsdClosePipe(filterfds
[slot
]);
1242 if (job
->printer
->remote
&& job
->num_files
> 1)
1244 for (i
= 0; i
< job
->num_files
; i
++)
1249 if (printer_state_reasons
)
1250 free(printer_state_reasons
);
1252 cupsdAddSelect(job
->status_buffer
->fd
, (cupsd_selfunc_t
)update_job
, NULL
,
1255 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
, "Job #%d started.",
1262 * If we get here, we need to abort the current job and close out all
1263 * files and pipes...
1268 FilterLevel
-= job
->cost
;
1271 for (slot
= 0; slot
< 2; slot
++)
1272 cupsdClosePipe(filterfds
[slot
]);
1274 cupsArrayDelete(filters
);
1278 if (job
->printer
->remote
&& job
->num_files
> 1)
1280 for (i
= 0; i
< job
->num_files
; i
++)
1287 if (printer_state_reasons
)
1288 free(printer_state_reasons
);
1290 cupsdClosePipe(job
->print_pipes
);
1291 cupsdClosePipe(job
->back_pipes
);
1292 cupsdClosePipe(job
->side_pipes
);
1294 cupsdRemoveSelect(job
->status_pipes
[0]);
1295 cupsdClosePipe(job
->status_pipes
);
1296 cupsdStatBufDelete(job
->status_buffer
);
1297 job
->status_buffer
= NULL
;
1300 * Update the printer and job state.
1303 cupsdSetJobState(job
, abort_state
, CUPSD_JOB_DEFAULT
, "%s", abort_message
);
1304 cupsdSetPrinterState(job
->printer
, IPP_PRINTER_IDLE
, 0);
1305 update_job_attrs(job
, 0);
1308 free_job_history(job
);
1310 cupsArrayRemove(PrintingJobs
, job
);
1313 * Clear the printer <-> job association...
1316 job
->printer
->job
= NULL
;
1317 job
->printer
= NULL
;
1322 * 'cupsdDeleteJob()' - Free all memory used by a job.
1326 cupsdDeleteJob(cupsd_job_t
*job
, /* I - Job */
1327 cupsd_jobaction_t action
)/* I - Action */
1329 int i
; /* Looping var */
1330 char filename
[1024]; /* Job filename */
1334 finalize_job(job
, 1);
1336 if (action
== CUPSD_JOB_PURGE
)
1339 * Remove the job info file...
1342 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
,
1345 cupsdRemoveFile(filename
);
1350 cupsdClearString(&job
->username
);
1351 cupsdClearString(&job
->dest
);
1353 i
< (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0]));
1355 cupsdClearString(job
->auth_env
+ i
);
1356 cupsdClearString(&job
->auth_uid
);
1358 if (job
->num_files
> 0)
1360 free(job
->compressions
);
1361 free(job
->filetypes
);
1363 if (action
== CUPSD_JOB_PURGE
)
1365 while (job
->num_files
> 0)
1367 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
1368 job
->id
, job
->num_files
);
1370 cupsdRemoveFile(filename
);
1382 free_job_history(job
);
1386 cupsArrayRemove(Jobs
, job
);
1387 cupsArrayRemove(ActiveJobs
, job
);
1388 cupsArrayRemove(PrintingJobs
, job
);
1395 * 'cupsdFreeAllJobs()' - Free all jobs from memory.
1399 cupsdFreeAllJobs(void)
1401 cupsd_job_t
*job
; /* Current job */
1409 cupsdStopAllJobs(CUPSD_JOB_FORCE
, 0);
1412 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1414 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1415 cupsdDeleteJob(job
, CUPSD_JOB_DEFAULT
);
1417 cupsdReleaseSignals();
1422 * 'cupsdFindJob()' - Find the specified job.
1425 cupsd_job_t
* /* O - Job data */
1426 cupsdFindJob(int id
) /* I - Job ID */
1428 cupsd_job_t key
; /* Search key */
1433 return ((cupsd_job_t
*)cupsArrayFind(Jobs
, &key
));
1438 * 'cupsdGetPrinterJobCount()' - Get the number of pending, processing,
1439 * or held jobs in a printer or class.
1442 int /* O - Job count */
1443 cupsdGetPrinterJobCount(
1444 const char *dest
) /* I - Printer or class name */
1446 int count
; /* Job count */
1447 cupsd_job_t
*job
; /* Current job */
1450 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
1452 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
1453 if (job
->dest
&& !_cups_strcasecmp(job
->dest
, dest
))
1461 * 'cupsdGetUserJobCount()' - Get the number of pending, processing,
1462 * or held jobs for a user.
1465 int /* O - Job count */
1466 cupsdGetUserJobCount(
1467 const char *username
) /* I - Username */
1469 int count
; /* Job count */
1470 cupsd_job_t
*job
; /* Current job */
1473 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
1475 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
1476 if (!_cups_strcasecmp(job
->username
, username
))
1484 * 'cupsdLoadAllJobs()' - Load all jobs from disk.
1488 cupsdLoadAllJobs(void)
1490 char filename
[1024]; /* Full filename of job.cache file */
1491 struct stat fileinfo
, /* Information on job.cache file */
1492 dirinfo
; /* Information on RequestRoot dir */
1497 * Create the job arrays as needed...
1501 Jobs
= cupsArrayNew(compare_jobs
, NULL
);
1504 ActiveJobs
= cupsArrayNew(compare_active_jobs
, NULL
);
1507 PrintingJobs
= cupsArrayNew(compare_jobs
, NULL
);
1510 * See whether the job.cache file is older than the RequestRoot directory...
1513 snprintf(filename
, sizeof(filename
), "%s/job.cache", CacheDir
);
1515 if (stat(filename
, &fileinfo
))
1517 fileinfo
.st_mtime
= 0;
1519 if (errno
!= ENOENT
)
1520 cupsdLogMessage(CUPSD_LOG_ERROR
,
1521 "Unable to get file information for \"%s\" - %s",
1522 filename
, strerror(errno
));
1525 if (stat(RequestRoot
, &dirinfo
))
1527 dirinfo
.st_mtime
= 0;
1529 if (errno
!= ENOENT
)
1530 cupsdLogMessage(CUPSD_LOG_ERROR
,
1531 "Unable to get directory information for \"%s\" - %s",
1532 RequestRoot
, strerror(errno
));
1536 * Load the most recent source for job data...
1539 if (dirinfo
.st_mtime
> fileinfo
.st_mtime
)
1541 load_request_root();
1543 load_next_job_id(filename
);
1546 load_job_cache(filename
);
1549 * Clean out old jobs as needed...
1552 if (MaxJobs
> 0 && cupsArrayCount(Jobs
) >= MaxJobs
)
1558 * 'cupsdLoadJob()' - Load a single job.
1561 int /* O - 1 on success, 0 on failure */
1562 cupsdLoadJob(cupsd_job_t
*job
) /* I - Job */
1564 int i
; /* Looping var */
1565 char jobfile
[1024]; /* Job filename */
1566 cups_file_t
*fp
; /* Job file */
1567 int fileid
; /* Current file ID */
1568 ipp_attribute_t
*attr
; /* Job attribute */
1569 const char *dest
; /* Destination name */
1570 cupsd_printer_t
*destptr
; /* Pointer to destination */
1571 mime_type_t
**filetypes
; /* New filetypes array */
1572 int *compressions
; /* New compressions array */
1577 if (job
->state_value
> IPP_JOB_STOPPED
)
1578 job
->access_time
= time(NULL
);
1583 if ((job
->attrs
= ippNew()) == NULL
)
1585 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Ran out of memory for job attributes.");
1590 * Load job attributes...
1593 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Loading attributes...", job
->id
);
1595 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, job
->id
);
1596 if ((fp
= cupsFileOpen(jobfile
, "r")) == NULL
)
1598 char newfile
[1024]; /* New job filename */
1600 snprintf(newfile
, sizeof(newfile
), "%s/c%05d.N", RequestRoot
, job
->id
);
1601 if ((fp
= cupsFileOpen(newfile
, "r")) == NULL
)
1603 cupsdLogMessage(CUPSD_LOG_ERROR
,
1604 "[Job %d] Unable to open job control file \"%s\": %s",
1605 job
->id
, jobfile
, strerror(errno
));
1610 rename(newfile
, jobfile
);
1613 if (ippReadIO(fp
, (ipp_iocb_t
)cupsFileRead
, 1, NULL
, job
->attrs
) != IPP_DATA
)
1615 cupsdLogMessage(CUPSD_LOG_ERROR
,
1616 "[Job %d] Unable to read job control file \"%s\".", job
->id
,
1625 * Copy attribute data to the job object...
1628 if (!ippFindAttribute(job
->attrs
, "time-at-creation", IPP_TAG_INTEGER
))
1630 cupsdLogMessage(CUPSD_LOG_ERROR
,
1631 "[Job %d] Missing or bad time-at-creation attribute in "
1632 "control file.", job
->id
);
1636 if ((job
->state
= ippFindAttribute(job
->attrs
, "job-state",
1637 IPP_TAG_ENUM
)) == NULL
)
1639 cupsdLogMessage(CUPSD_LOG_ERROR
,
1640 "[Job %d] Missing or bad job-state attribute in control "
1645 job
->state_value
= (ipp_jstate_t
)job
->state
->values
[0].integer
;
1649 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1650 IPP_TAG_URI
)) == NULL
)
1652 cupsdLogMessage(CUPSD_LOG_ERROR
,
1653 "[Job %d] No job-printer-uri attribute in control file.",
1658 if ((dest
= cupsdValidateDest(attr
->values
[0].string
.text
, &(job
->dtype
),
1661 cupsdLogMessage(CUPSD_LOG_ERROR
,
1662 "[Job %d] Unable to queue job for destination \"%s\".",
1663 job
->id
, attr
->values
[0].string
.text
);
1667 cupsdSetString(&job
->dest
, dest
);
1669 else if ((destptr
= cupsdFindDest(job
->dest
)) == NULL
)
1671 cupsdLogMessage(CUPSD_LOG_ERROR
,
1672 "[Job %d] Unable to queue job for destination \"%s\".",
1673 job
->id
, job
->dest
);
1677 if ((job
->reasons
= ippFindAttribute(job
->attrs
, "job-state-reasons",
1678 IPP_TAG_KEYWORD
)) == NULL
)
1680 const char *reason
; /* job-state-reason keyword */
1682 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1683 "[Job %d] Adding missing job-state-reasons attribute to "
1684 " control file.", job
->id
);
1686 switch (job
->state_value
)
1688 case IPP_JOB_PENDING
:
1689 if (destptr
->state
== IPP_PRINTER_STOPPED
)
1690 reason
= "printer-stopped";
1696 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1697 IPP_TAG_ZERO
)) != NULL
&&
1698 (attr
->value_tag
== IPP_TAG_NAME
||
1699 attr
->value_tag
== IPP_TAG_NAMELANG
||
1700 attr
->value_tag
== IPP_TAG_KEYWORD
) &&
1701 strcmp(attr
->values
[0].string
.text
, "no-hold"))
1702 reason
= "job-hold-until-specified";
1704 reason
= "job-incoming";
1707 case IPP_JOB_PROCESSING
:
1708 reason
= "job-printing";
1711 case IPP_JOB_STOPPED
:
1712 reason
= "job-stopped";
1715 case IPP_JOB_CANCELED
:
1716 reason
= "job-canceled-by-user";
1719 case IPP_JOB_ABORTED
:
1720 reason
= "aborted-by-system";
1723 case IPP_JOB_COMPLETED
:
1724 reason
= "job-completed-successfully";
1728 job
->reasons
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1729 "job-state-reasons", NULL
, reason
);
1731 else if (job
->state_value
== IPP_JOB_PENDING
)
1733 if (destptr
->state
== IPP_PRINTER_STOPPED
)
1734 ippSetString(job
->attrs
, &job
->reasons
, 0, "printer-stopped");
1736 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
1739 job
->sheets
= ippFindAttribute(job
->attrs
, "job-media-sheets-completed",
1741 job
->job_sheets
= ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_NAME
);
1745 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
1746 IPP_TAG_INTEGER
)) == NULL
)
1748 cupsdLogMessage(CUPSD_LOG_ERROR
,
1749 "[Job %d] Missing or bad job-priority attribute in "
1750 "control file.", job
->id
);
1754 job
->priority
= attr
->values
[0].integer
;
1759 if ((attr
= ippFindAttribute(job
->attrs
, "job-originating-user-name",
1760 IPP_TAG_NAME
)) == NULL
)
1762 cupsdLogMessage(CUPSD_LOG_ERROR
,
1763 "[Job %d] Missing or bad job-originating-user-name "
1764 "attribute in control file.", job
->id
);
1768 cupsdSetString(&job
->username
, attr
->values
[0].string
.text
);
1772 * Set the job hold-until time and state...
1775 if (job
->state_value
== IPP_JOB_HELD
)
1777 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1778 IPP_TAG_KEYWORD
)) == NULL
)
1779 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1782 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
, CUPSD_JOB_DEFAULT
);
1785 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1786 job
->state_value
= IPP_JOB_PENDING
;
1789 else if (job
->state_value
== IPP_JOB_PROCESSING
)
1791 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1792 job
->state_value
= IPP_JOB_PENDING
;
1795 if (!job
->num_files
)
1798 * Find all the d##### files...
1801 for (fileid
= 1; fileid
< 10000; fileid
++)
1803 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
1806 if (access(jobfile
, 0))
1809 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1810 "[Job %d] Auto-typing document file \"%s\"...", job
->id
,
1813 if (fileid
> job
->num_files
)
1815 if (job
->num_files
== 0)
1817 compressions
= (int *)calloc(fileid
, sizeof(int));
1818 filetypes
= (mime_type_t
**)calloc(fileid
, sizeof(mime_type_t
*));
1822 compressions
= (int *)realloc(job
->compressions
,
1823 sizeof(int) * fileid
);
1824 filetypes
= (mime_type_t
**)realloc(job
->filetypes
,
1825 sizeof(mime_type_t
*) *
1829 if (!compressions
|| !filetypes
)
1831 cupsdLogMessage(CUPSD_LOG_ERROR
,
1832 "[Job %d] Ran out of memory for job file types.",
1835 ippDelete(job
->attrs
);
1844 if (job
->compressions
)
1846 free(job
->compressions
);
1847 job
->compressions
= NULL
;
1852 free(job
->filetypes
);
1853 job
->filetypes
= NULL
;
1860 job
->compressions
= compressions
;
1861 job
->filetypes
= filetypes
;
1862 job
->num_files
= fileid
;
1865 job
->filetypes
[fileid
- 1] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
1866 job
->compressions
+ fileid
- 1);
1868 if (!job
->filetypes
[fileid
- 1])
1869 job
->filetypes
[fileid
- 1] = mimeType(MimeDatabase
, "application",
1875 * Load authentication information as needed...
1878 if (job
->state_value
< IPP_JOB_STOPPED
)
1880 snprintf(jobfile
, sizeof(jobfile
), "%s/a%05d", RequestRoot
, job
->id
);
1883 i
< (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0]));
1885 cupsdClearString(job
->auth_env
+ i
);
1886 cupsdClearString(&job
->auth_uid
);
1888 if ((fp
= cupsFileOpen(jobfile
, "r")) != NULL
)
1890 int bytes
, /* Size of auth data */
1891 linenum
= 1; /* Current line number */
1892 char line
[65536], /* Line from file */
1893 *value
, /* Value from line */
1894 data
[65536]; /* Decoded data */
1897 if (cupsFileGets(fp
, line
, sizeof(line
)) &&
1898 !strcmp(line
, "CUPSD-AUTH-V2"))
1901 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
1907 bytes
= sizeof(data
);
1908 httpDecode64_2(data
, &bytes
, value
);
1911 * Assign environment variables...
1914 if (!strcmp(line
, "uid"))
1916 cupsdSetStringf(&job
->auth_uid
, "AUTH_UID=%s", value
);
1919 else if (i
>= (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0])))
1922 if (!strcmp(line
, "username"))
1923 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_USERNAME=%s", data
);
1924 else if (!strcmp(line
, "domain"))
1925 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_DOMAIN=%s", data
);
1926 else if (!strcmp(line
, "password"))
1927 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_PASSWORD=%s", data
);
1928 else if (!strcmp(line
, "negotiate"))
1929 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_NEGOTIATE=%s", line
);
1941 job
->access_time
= time(NULL
);
1945 * If we get here then something bad happened...
1950 ippDelete(job
->attrs
);
1953 if (job
->compressions
)
1955 free(job
->compressions
);
1956 job
->compressions
= NULL
;
1961 free(job
->filetypes
);
1962 job
->filetypes
= NULL
;
1968 cupsdRemoveFile(jobfile
);
1977 * 'cupsdMoveJob()' - Move the specified job to a different destination.
1981 cupsdMoveJob(cupsd_job_t
*job
, /* I - Job */
1982 cupsd_printer_t
*p
) /* I - Destination printer or class */
1984 ipp_attribute_t
*attr
; /* job-printer-uri attribute */
1985 const char *olddest
; /* Old destination */
1986 cupsd_printer_t
*oldp
; /* Old pointer */
1990 * Don't move completed jobs...
1993 if (job
->state_value
> IPP_JOB_STOPPED
)
1997 * Get the old destination...
2000 olddest
= job
->dest
;
2003 oldp
= job
->printer
;
2005 oldp
= cupsdFindDest(olddest
);
2008 * Change the destination information...
2011 if (job
->state_value
> IPP_JOB_HELD
)
2012 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
2013 "Stopping job prior to move.");
2015 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, oldp
, job
,
2016 "Job #%d moved from %s to %s.", job
->id
, olddest
,
2019 cupsdSetString(&job
->dest
, p
->name
);
2020 job
->dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_REMOTE
);
2022 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
2023 IPP_TAG_URI
)) != NULL
)
2024 cupsdSetString(&(attr
->values
[0].string
.text
), p
->uri
);
2026 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, p
, job
,
2027 "Job #%d moved from %s to %s.", job
->id
, olddest
,
2031 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2036 * 'cupsdReleaseJob()' - Release the specified job.
2040 cupsdReleaseJob(cupsd_job_t
*job
) /* I - Job */
2042 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdReleaseJob(job=%p(%d))", job
,
2045 if (job
->state_value
== IPP_JOB_HELD
)
2048 * Add trailing banner as needed...
2051 if (job
->pending_timeout
)
2052 cupsdTimeoutJob(job
);
2054 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
2055 "Job released by user.");
2061 * 'cupsdRestartJob()' - Restart the specified job.
2065 cupsdRestartJob(cupsd_job_t
*job
) /* I - Job */
2067 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdRestartJob(job=%p(%d))", job
,
2070 if (job
->state_value
== IPP_JOB_STOPPED
|| job
->num_files
)
2071 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
2072 "Job restarted by user.");
2077 * 'cupsdSaveAllJobs()' - Save a summary of all jobs to disk.
2081 cupsdSaveAllJobs(void)
2083 int i
; /* Looping var */
2084 cups_file_t
*fp
; /* job.cache file */
2085 char filename
[1024], /* job.cache filename */
2086 temp
[1024]; /* Temporary string */
2087 cupsd_job_t
*job
; /* Current job */
2088 time_t curtime
; /* Current time */
2089 struct tm
*curdate
; /* Current date */
2092 snprintf(filename
, sizeof(filename
), "%s/job.cache", CacheDir
);
2093 if ((fp
= cupsdCreateConfFile(filename
, ConfigFilePerm
)) == NULL
)
2096 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving job.cache...");
2099 * Write a small header to the file...
2102 curtime
= time(NULL
);
2103 curdate
= localtime(&curtime
);
2104 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
2106 cupsFilePuts(fp
, "# Job cache file for " CUPS_SVERSION
"\n");
2107 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
2108 cupsFilePrintf(fp
, "NextJobId %d\n", NextJobId
);
2111 * Write each job known to the system...
2114 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
2116 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
2118 cupsFilePrintf(fp
, "<Job %d>\n", job
->id
);
2119 cupsFilePrintf(fp
, "State %d\n", job
->state_value
);
2120 cupsFilePrintf(fp
, "Priority %d\n", job
->priority
);
2121 cupsFilePrintf(fp
, "HoldUntil %d\n", (int)job
->hold_until
);
2122 cupsFilePrintf(fp
, "Username %s\n", job
->username
);
2123 cupsFilePrintf(fp
, "Destination %s\n", job
->dest
);
2124 cupsFilePrintf(fp
, "DestType %d\n", job
->dtype
);
2125 cupsFilePrintf(fp
, "NumFiles %d\n", job
->num_files
);
2126 for (i
= 0; i
< job
->num_files
; i
++)
2127 cupsFilePrintf(fp
, "File %d %s/%s %d\n", i
+ 1, job
->filetypes
[i
]->super
,
2128 job
->filetypes
[i
]->type
, job
->compressions
[i
]);
2129 cupsFilePuts(fp
, "</Job>\n");
2132 cupsdCloseCreatedConfFile(fp
, filename
);
2137 * 'cupsdSaveJob()' - Save a job to disk.
2141 cupsdSaveJob(cupsd_job_t
*job
) /* I - Job */
2143 char filename
[1024], /* Job control filename */
2144 newfile
[1024]; /* New job control filename */
2145 cups_file_t
*fp
; /* Job file */
2148 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSaveJob(job=%p(%d)): job->attrs=%p",
2149 job
, job
->id
, job
->attrs
);
2151 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
, job
->id
);
2152 snprintf(newfile
, sizeof(newfile
), "%s/c%05d.N", RequestRoot
, job
->id
);
2154 if ((fp
= cupsFileOpen(newfile
, "w")) == NULL
)
2156 cupsdLogMessage(CUPSD_LOG_ERROR
,
2157 "[Job %d] Unable to create job control file \"%s\": %s",
2158 job
->id
, newfile
, strerror(errno
));
2162 fchmod(cupsFileNumber(fp
), 0600);
2163 fchown(cupsFileNumber(fp
), RunUser
, Group
);
2165 job
->attrs
->state
= IPP_IDLE
;
2167 if (ippWriteIO(fp
, (ipp_iocb_t
)cupsFileWrite
, 1, NULL
,
2168 job
->attrs
) != IPP_DATA
)
2170 cupsdLogMessage(CUPSD_LOG_ERROR
,
2171 "[Job %d] Unable to write job control file.", job
->id
);
2177 if (cupsFileClose(fp
))
2178 cupsdLogMessage(CUPSD_LOG_ERROR
,
2179 "[Job %d] Unable to close job control file: %s",
2180 job
->id
, strerror(errno
));
2184 if (rename(newfile
, filename
))
2185 cupsdLogMessage(CUPSD_LOG_ERROR
,
2186 "[Job %d] Unable to finalize job control file: %s",
2187 job
->id
, strerror(errno
));
2195 * 'cupsdSetJobHoldUntil()' - Set the hold time for a job.
2199 cupsdSetJobHoldUntil(cupsd_job_t
*job
, /* I - Job */
2200 const char *when
, /* I - When to resume */
2201 int update
)/* I - Update job-hold-until attr? */
2203 time_t curtime
; /* Current time */
2204 struct tm
*curdate
; /* Current date */
2205 int hour
; /* Hold hour */
2206 int minute
; /* Hold minute */
2207 int second
= 0; /* Hold second */
2210 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2211 "cupsdSetJobHoldUntil(job=%p(%d), when=\"%s\", update=%d)",
2212 job
, job
->id
, when
, update
);
2217 * Update the job-hold-until attribute...
2220 ipp_attribute_t
*attr
; /* job-hold-until attribute */
2222 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2223 IPP_TAG_KEYWORD
)) == NULL
)
2224 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2227 cupsdSetString(&(attr
->values
[0].string
.text
), when
);
2229 attr
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
2230 "job-hold-until", NULL
, when
);
2234 if (isdigit(when
[0] & 255))
2235 attr
->value_tag
= IPP_TAG_NAME
;
2237 attr
->value_tag
= IPP_TAG_KEYWORD
;
2240 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2245 * Update the hold time...
2248 if (!strcmp(when
, "indefinite") || !strcmp(when
, "auth-info-required"))
2251 * Hold indefinitely...
2254 job
->hold_until
= 0;
2256 else if (!strcmp(when
, "day-time"))
2259 * Hold to 6am the next morning unless local time is < 6pm.
2262 curtime
= time(NULL
);
2263 curdate
= localtime(&curtime
);
2265 if (curdate
->tm_hour
< 18)
2266 job
->hold_until
= curtime
;
2268 job
->hold_until
= curtime
+
2269 ((29 - curdate
->tm_hour
) * 60 + 59 -
2270 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2272 else if (!strcmp(when
, "evening") || !strcmp(when
, "night"))
2275 * Hold to 6pm unless local time is > 6pm or < 6am.
2278 curtime
= time(NULL
);
2279 curdate
= localtime(&curtime
);
2281 if (curdate
->tm_hour
< 6 || curdate
->tm_hour
>= 18)
2282 job
->hold_until
= curtime
;
2284 job
->hold_until
= curtime
+
2285 ((17 - curdate
->tm_hour
) * 60 + 59 -
2286 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2288 else if (!strcmp(when
, "second-shift"))
2291 * Hold to 4pm unless local time is > 4pm.
2294 curtime
= time(NULL
);
2295 curdate
= localtime(&curtime
);
2297 if (curdate
->tm_hour
>= 16)
2298 job
->hold_until
= curtime
;
2300 job
->hold_until
= curtime
+
2301 ((15 - curdate
->tm_hour
) * 60 + 59 -
2302 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2304 else if (!strcmp(when
, "third-shift"))
2307 * Hold to 12am unless local time is < 8am.
2310 curtime
= time(NULL
);
2311 curdate
= localtime(&curtime
);
2313 if (curdate
->tm_hour
< 8)
2314 job
->hold_until
= curtime
;
2316 job
->hold_until
= curtime
+
2317 ((23 - curdate
->tm_hour
) * 60 + 59 -
2318 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2320 else if (!strcmp(when
, "weekend"))
2323 * Hold to weekend unless we are in the weekend.
2326 curtime
= time(NULL
);
2327 curdate
= localtime(&curtime
);
2329 if (curdate
->tm_wday
== 0 || curdate
->tm_wday
== 6)
2330 job
->hold_until
= curtime
;
2332 job
->hold_until
= curtime
+
2333 (((5 - curdate
->tm_wday
) * 24 +
2334 (17 - curdate
->tm_hour
)) * 60 + 59 -
2335 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2337 else if (sscanf(when
, "%d:%d:%d", &hour
, &minute
, &second
) >= 2)
2340 * Hold to specified GMT time (HH:MM or HH:MM:SS)...
2343 curtime
= time(NULL
);
2344 curdate
= gmtime(&curtime
);
2346 job
->hold_until
= curtime
+
2347 ((hour
- curdate
->tm_hour
) * 60 + minute
-
2348 curdate
->tm_min
) * 60 + second
- curdate
->tm_sec
;
2351 * Hold until next day as needed...
2354 if (job
->hold_until
< curtime
)
2355 job
->hold_until
+= 24 * 60 * 60;
2358 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetJobHoldUntil: hold_until=%d",
2359 (int)job
->hold_until
);
2364 * 'cupsdSetJobPriority()' - Set the priority of a job, moving it up/down in
2365 * the list as needed.
2369 cupsdSetJobPriority(
2370 cupsd_job_t
*job
, /* I - Job ID */
2371 int priority
) /* I - New priority (0 to 100) */
2373 ipp_attribute_t
*attr
; /* Job attribute */
2377 * Don't change completed jobs...
2380 if (job
->state_value
>= IPP_JOB_PROCESSING
)
2384 * Set the new priority and re-add the job into the active list...
2387 cupsArrayRemove(ActiveJobs
, job
);
2389 job
->priority
= priority
;
2391 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
2392 IPP_TAG_INTEGER
)) != NULL
)
2393 attr
->values
[0].integer
= priority
;
2395 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-priority",
2398 cupsArrayAdd(ActiveJobs
, job
);
2401 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2406 * 'cupsdSetJobState()' - Set the state of the specified print job.
2411 cupsd_job_t
*job
, /* I - Job to cancel */
2412 ipp_jstate_t newstate
, /* I - New job state */
2413 cupsd_jobaction_t action
, /* I - Action to take */
2414 const char *message
, /* I - Message to log */
2415 ...) /* I - Additional arguments as needed */
2417 int i
; /* Looping var */
2418 ipp_jstate_t oldstate
; /* Old state */
2419 char filename
[1024]; /* Job filename */
2420 ipp_attribute_t
*attr
; /* Job attribute */
2423 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2424 "cupsdSetJobState(job=%p(%d), state=%d, newstate=%d, "
2425 "action=%d, message=\"%s\")", job
, job
->id
, job
->state_value
,
2426 newstate
, action
, message
? message
: "(null)");
2430 * Make sure we have the job attributes...
2433 if (!cupsdLoadJob(job
))
2437 * Don't do anything if the state is unchanged and we aren't purging the
2441 oldstate
= job
->state_value
;
2442 if (newstate
== oldstate
&& action
!= CUPSD_JOB_PURGE
)
2446 * Stop any processes that are working on the current job...
2449 if (oldstate
== IPP_JOB_PROCESSING
)
2450 stop_job(job
, action
);
2453 * Set the new job state...
2456 job
->state
->values
[0].integer
= newstate
;
2457 job
->state_value
= newstate
;
2461 case IPP_JOB_PENDING
:
2463 * Update job-hold-until as needed...
2466 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2467 IPP_TAG_KEYWORD
)) == NULL
)
2468 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2472 attr
->value_tag
= IPP_TAG_KEYWORD
;
2473 cupsdSetString(&(attr
->values
[0].string
.text
), "no-hold");
2479 case IPP_JOB_ABORTED
:
2480 case IPP_JOB_CANCELED
:
2481 case IPP_JOB_COMPLETED
:
2482 set_time(job
, "time-at-completed");
2483 ippSetString(job
->attrs
, &job
->reasons
, 0, "processing-to-stop-point");
2488 * Log message as needed...
2493 char buffer
[2048]; /* Message buffer */
2494 va_list ap
; /* Pointer to additional arguments */
2496 va_start(ap
, message
);
2497 vsnprintf(buffer
, sizeof(buffer
), message
, ap
);
2500 if (newstate
> IPP_JOB_STOPPED
)
2501 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
, "%s", buffer
);
2503 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
, "%s", buffer
);
2505 if (newstate
== IPP_JOB_STOPPED
|| newstate
== IPP_JOB_ABORTED
)
2506 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "%s", buffer
);
2508 cupsdLogJob(job
, CUPSD_LOG_INFO
, "%s", buffer
);
2512 * Handle post-state-change actions...
2517 case IPP_JOB_PROCESSING
:
2519 * Add the job to the "printing" list...
2522 if (!cupsArrayFind(PrintingJobs
, job
))
2523 cupsArrayAdd(PrintingJobs
, job
);
2526 * Set the processing time...
2529 set_time(job
, "time-at-processing");
2531 case IPP_JOB_PENDING
:
2533 case IPP_JOB_STOPPED
:
2535 * Make sure the job is in the active list...
2538 if (!cupsArrayFind(ActiveJobs
, job
))
2539 cupsArrayAdd(ActiveJobs
, job
);
2542 * Save the job state to disk...
2546 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2549 case IPP_JOB_ABORTED
:
2550 case IPP_JOB_CANCELED
:
2551 case IPP_JOB_COMPLETED
:
2552 if (newstate
== IPP_JOB_CANCELED
)
2555 * Remove the job from the active list if there are no processes still
2559 for (i
= 0; job
->filters
[i
] < 0; i
++);
2561 if (!job
->filters
[i
] && job
->backend
<= 0)
2562 cupsArrayRemove(ActiveJobs
, job
);
2567 * Otherwise just remove the job from the active list immediately...
2570 cupsArrayRemove(ActiveJobs
, job
);
2574 * Expire job subscriptions since the job is now "completed"...
2577 cupsdExpireSubscriptions(NULL
, job
);
2581 * If we are going to sleep and the PrintingJobs count is now 0, allow the
2582 * sleep to happen immediately...
2585 if (Sleeping
&& cupsArrayCount(PrintingJobs
) == 0)
2587 #endif /* __APPLE__ */
2590 * Remove any authentication data...
2593 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
2594 if (cupsdRemoveFile(filename
) && errno
!= ENOENT
)
2595 cupsdLogMessage(CUPSD_LOG_ERROR
,
2596 "Unable to remove authentication cache: %s",
2600 i
< (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0]));
2602 cupsdClearString(job
->auth_env
+ i
);
2604 cupsdClearString(&job
->auth_uid
);
2607 * Remove the print file for good if we aren't preserving jobs or
2611 if (!JobHistory
|| !JobFiles
|| action
== CUPSD_JOB_PURGE
)
2613 for (i
= 1; i
<= job
->num_files
; i
++)
2615 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
2618 cupsdRemoveFile(filename
);
2623 if (job
->num_files
> 0)
2625 free(job
->filetypes
);
2626 free(job
->compressions
);
2629 job
->filetypes
= NULL
;
2630 job
->compressions
= NULL
;
2634 if (JobHistory
&& action
!= CUPSD_JOB_PURGE
)
2637 * Save job state info...
2641 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2643 else if (!job
->printer
)
2646 * Delete the job immediately if not actively printing...
2649 cupsdDeleteJob(job
, CUPSD_JOB_PURGE
);
2656 * Finalize the job immediately if we forced things...
2659 if (action
>= CUPSD_JOB_FORCE
&& job
&& job
->printer
)
2660 finalize_job(job
, 0);
2663 * Update the server "busy" state...
2666 cupsdSetBusyState();
2671 * 'cupsdStopAllJobs()' - Stop all print jobs.
2676 cupsd_jobaction_t action
, /* I - Action */
2677 int kill_delay
) /* I - Number of seconds before we kill */
2679 cupsd_job_t
*job
; /* Current job */
2682 DEBUG_puts("cupsdStopAllJobs()");
2684 for (job
= (cupsd_job_t
*)cupsArrayFirst(PrintingJobs
);
2686 job
= (cupsd_job_t
*)cupsArrayNext(PrintingJobs
))
2689 job
->kill_time
= time(NULL
) + kill_delay
;
2691 cupsdSetJobState(job
, IPP_JOB_PENDING
, action
, NULL
);
2697 * 'cupsdUnloadCompletedJobs()' - Flush completed job history from memory.
2701 cupsdUnloadCompletedJobs(void)
2703 cupsd_job_t
*job
; /* Current job */
2704 time_t expire
; /* Expiration time */
2707 expire
= time(NULL
) - 60;
2709 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
2711 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
2712 if (job
->attrs
&& job
->state_value
>= IPP_JOB_STOPPED
&& !job
->printer
&&
2713 job
->access_time
< expire
)
2724 * 'compare_active_jobs()' - Compare the job IDs and priorities of two jobs.
2727 static int /* O - Difference */
2728 compare_active_jobs(void *first
, /* I - First job */
2729 void *second
, /* I - Second job */
2730 void *data
) /* I - App data (not used) */
2732 int diff
; /* Difference */
2737 if ((diff
= ((cupsd_job_t
*)second
)->priority
-
2738 ((cupsd_job_t
*)first
)->priority
) != 0)
2741 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
2746 * 'compare_jobs()' - Compare the job IDs of two jobs.
2749 static int /* O - Difference */
2750 compare_jobs(void *first
, /* I - First job */
2751 void *second
, /* I - Second job */
2752 void *data
) /* I - App data (not used) */
2756 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
2761 * 'dump_job_history()' - Dump any debug messages for a job.
2765 dump_job_history(cupsd_job_t
*job
) /* I - Job */
2767 int i
, /* Looping var */
2768 oldsize
; /* Current MaxLogSize */
2769 struct tm
*date
; /* Date/time value */
2770 cupsd_joblog_t
*message
; /* Current message */
2771 char temp
[2048], /* Log message */
2772 *ptr
, /* Pointer into log message */
2773 start
[256], /* Start time */
2774 end
[256]; /* End time */
2775 cupsd_printer_t
*printer
; /* Printer for job */
2779 * See if we have anything to dump...
2786 * Disable log rotation temporarily...
2789 oldsize
= MaxLogSize
;
2793 * Copy the debug messages to the log...
2796 message
= (cupsd_joblog_t
*)cupsArrayFirst(job
->history
);
2797 date
= localtime(&(message
->time
));
2798 strftime(start
, sizeof(start
), "%X", date
);
2800 message
= (cupsd_joblog_t
*)cupsArrayLast(job
->history
);
2801 date
= localtime(&(message
->time
));
2802 strftime(end
, sizeof(end
), "%X", date
);
2804 snprintf(temp
, sizeof(temp
),
2805 "[Job %d] The following messages were recorded from %s to %s",
2806 job
->id
, start
, end
);
2807 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, temp
);
2809 for (message
= (cupsd_joblog_t
*)cupsArrayFirst(job
->history
);
2811 message
= (cupsd_joblog_t
*)cupsArrayNext(job
->history
))
2812 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, message
->message
);
2814 snprintf(temp
, sizeof(temp
), "[Job %d] End of messages", job
->id
);
2815 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, temp
);
2818 * Log the printer state values...
2821 if ((printer
= job
->printer
) == NULL
)
2822 printer
= cupsdFindDest(job
->dest
);
2826 snprintf(temp
, sizeof(temp
), "[Job %d] printer-state=%d(%s)", job
->id
,
2828 printer
->state
== IPP_PRINTER_IDLE
? "idle" :
2829 printer
->state
== IPP_PRINTER_PROCESSING
? "processing" :
2831 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, temp
);
2833 snprintf(temp
, sizeof(temp
), "[Job %d] printer-state-message=\"%s\"",
2834 job
->id
, printer
->state_message
);
2835 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, temp
);
2837 snprintf(temp
, sizeof(temp
), "[Job %d] printer-state-reasons=", job
->id
);
2838 ptr
= temp
+ strlen(temp
);
2839 if (printer
->num_reasons
== 0)
2840 strlcpy(ptr
, "none", sizeof(temp
) - (ptr
- temp
));
2844 i
< printer
->num_reasons
&& ptr
< (temp
+ sizeof(temp
) - 2);
2850 strlcpy(ptr
, printer
->reasons
[i
], sizeof(temp
) - (ptr
- temp
));
2854 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, temp
);
2858 * Restore log file rotation...
2861 MaxLogSize
= oldsize
;
2864 * Free all messages...
2867 free_job_history(job
);
2872 * 'free_job_history()' - Free any log history.
2876 free_job_history(cupsd_job_t
*job
) /* I - Job */
2878 char *message
; /* Current message */
2884 for (message
= (char *)cupsArrayFirst(job
->history
);
2886 message
= (char *)cupsArrayNext(job
->history
))
2889 cupsArrayDelete(job
->history
);
2890 job
->history
= NULL
;
2895 * 'finalize_job()' - Cleanup after job filter processes and support data.
2899 finalize_job(cupsd_job_t
*job
, /* I - Job */
2900 int set_job_state
) /* I - 1 = set the job state */
2902 ipp_pstate_t printer_state
; /* New printer state value */
2903 ipp_jstate_t job_state
; /* New job state value */
2904 const char *message
; /* Message for job state */
2905 char buffer
[1024]; /* Buffer for formatted messages */
2908 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "finalize_job(job=%p(%d))", job
, job
->id
);
2911 * Clear the "connecting-to-device" reason, which is only valid when a printer
2912 * is processing, along with any remote printing job state...
2915 cupsdSetPrinterReasons(job
->printer
, "-connecting-to-device,"
2916 "cups-remote-pending,"
2917 "cups-remote-pending-held,"
2918 "cups-remote-processing,"
2919 "cups-remote-stopped,"
2920 "cups-remote-canceled,"
2921 "cups-remote-aborted,"
2922 "cups-remote-completed");
2925 * Similarly, clear the "offline-report" reason for non-USB devices since we
2926 * rarely have current information for network devices...
2929 if (strncmp(job
->printer
->device_uri
, "usb:", 4))
2930 cupsdSetPrinterReasons(job
->printer
, "-offline-report");
2933 * Free the security profile...
2936 cupsdDestroyProfile(job
->profile
);
2937 job
->profile
= NULL
;
2940 * Clear the unresponsive job watchdog timer...
2946 * Close pipes and status buffer...
2949 cupsdClosePipe(job
->print_pipes
);
2950 cupsdClosePipe(job
->back_pipes
);
2951 cupsdClosePipe(job
->side_pipes
);
2953 cupsdRemoveSelect(job
->status_pipes
[0]);
2954 cupsdClosePipe(job
->status_pipes
);
2955 cupsdStatBufDelete(job
->status_buffer
);
2956 job
->status_buffer
= NULL
;
2959 * Process the exit status...
2962 if (job
->printer
->state
== IPP_PRINTER_PROCESSING
)
2963 printer_state
= IPP_PRINTER_IDLE
;
2965 printer_state
= job
->printer
->state
;
2967 switch (job_state
= job
->state_value
)
2969 case IPP_JOB_PENDING
:
2970 message
= "Job paused.";
2974 message
= "Job held.";
2978 case IPP_JOB_PROCESSING
:
2979 case IPP_JOB_COMPLETED
:
2980 job_state
= IPP_JOB_COMPLETED
;
2981 message
= "Job completed.";
2983 ippSetString(job
->attrs
, &job
->reasons
, 0,
2984 "job-completed-successfully");
2987 case IPP_JOB_STOPPED
:
2988 message
= "Job stopped.";
2990 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-stopped");
2993 case IPP_JOB_CANCELED
:
2994 message
= "Job canceled.";
2996 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-canceled-by-user");
2999 case IPP_JOB_ABORTED
:
3000 message
= "Job aborted.";
3004 if (job
->status
< 0)
3007 * Backend had errors...
3010 int exit_code
; /* Exit code from backend */
3013 * Convert the status to an exit code. Due to the way the W* macros are
3014 * implemented on MacOS X (bug?), we have to store the exit status in a
3015 * variable first and then convert...
3018 exit_code
= -job
->status
;
3019 if (WIFEXITED(exit_code
))
3020 exit_code
= WEXITSTATUS(exit_code
);
3023 ippSetString(job
->attrs
, &job
->reasons
, 0, "cups-backend-crashed");
3024 exit_code
= job
->status
;
3027 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Backend returned status %d (%s)",
3029 exit_code
== CUPS_BACKEND_FAILED
? "failed" :
3030 exit_code
== CUPS_BACKEND_AUTH_REQUIRED
?
3031 "authentication required" :
3032 exit_code
== CUPS_BACKEND_HOLD
? "hold job" :
3033 exit_code
== CUPS_BACKEND_STOP
? "stop printer" :
3034 exit_code
== CUPS_BACKEND_CANCEL
? "cancel job" :
3035 exit_code
< 0 ? "crashed" : "unknown");
3038 * Do what needs to be done...
3044 case CUPS_BACKEND_FAILED
:
3046 * Backend failure, use the error-policy to determine how to
3050 if (job
->dtype
& CUPS_PRINTER_CLASS
)
3053 * Queued on a class - mark the job as pending and we'll retry on
3054 * another printer...
3057 if (job_state
== IPP_JOB_COMPLETED
)
3059 job_state
= IPP_JOB_PENDING
;
3060 message
= "Retrying job on another printer.";
3062 ippSetString(job
->attrs
, &job
->reasons
, 0,
3063 "resources-are-not-ready");
3066 else if (!strcmp(job
->printer
->error_policy
, "retry-current-job"))
3069 * The error policy is "retry-current-job" - mark the job as pending
3070 * and we'll retry on the same printer...
3073 if (job_state
== IPP_JOB_COMPLETED
)
3075 job_state
= IPP_JOB_PENDING
;
3076 message
= "Retrying job on same printer.";
3078 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
3081 else if ((job
->printer
->type
& CUPS_PRINTER_FAX
) ||
3082 !strcmp(job
->printer
->error_policy
, "retry-job"))
3084 if (job_state
== IPP_JOB_COMPLETED
)
3087 * The job was queued on a fax or the error policy is "retry-job" -
3088 * hold the job if the number of retries is less than the
3089 * JobRetryLimit, otherwise abort the job.
3094 if (job
->tries
> JobRetryLimit
&& JobRetryLimit
> 0)
3100 snprintf(buffer
, sizeof(buffer
),
3101 "Job aborted after %d unsuccessful attempts.",
3103 job_state
= IPP_JOB_ABORTED
;
3106 ippSetString(job
->attrs
, &job
->reasons
, 0, "aborted-by-system");
3111 * Try again in N seconds...
3114 snprintf(buffer
, sizeof(buffer
),
3115 "Job held for %d seconds since it could not be sent.",
3118 job
->hold_until
= time(NULL
) + JobRetryInterval
;
3119 job_state
= IPP_JOB_HELD
;
3122 ippSetString(job
->attrs
, &job
->reasons
, 0,
3123 "resources-are-not-ready");
3127 else if (!strcmp(job
->printer
->error_policy
, "abort-job") &&
3128 job_state
== IPP_JOB_COMPLETED
)
3130 job_state
= IPP_JOB_ABORTED
;
3131 message
= "Job aborted due to backend errors; please consult "
3132 "the error_log file for details.";
3134 ippSetString(job
->attrs
, &job
->reasons
, 0, "aborted-by-system");
3136 else if (job
->state_value
== IPP_JOB_PROCESSING
)
3138 job_state
= IPP_JOB_PENDING
;
3139 printer_state
= IPP_PRINTER_STOPPED
;
3140 message
= "Printer stopped due to backend errors; please "
3141 "consult the error_log file for details.";
3143 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
3147 case CUPS_BACKEND_CANCEL
:
3152 if (job_state
== IPP_JOB_COMPLETED
)
3154 job_state
= IPP_JOB_ABORTED
;
3155 message
= "Job aborted due to backend errors; please consult "
3156 "the error_log file for details.";
3158 ippSetString(job
->attrs
, &job
->reasons
, 0, "aborted-by-system");
3162 case CUPS_BACKEND_HOLD
:
3163 if (job_state
== IPP_JOB_COMPLETED
)
3169 cupsdSetJobHoldUntil(job
, "indefinite", 1);
3170 ippSetString(job
->attrs
, &job
->reasons
, 0,
3171 "job-hold-until-specified");
3173 job_state
= IPP_JOB_HELD
;
3174 message
= "Job held indefinitely due to backend errors; please "
3175 "consult the error_log file for details.";
3179 case CUPS_BACKEND_STOP
:
3181 * Stop the printer...
3184 printer_state
= IPP_PRINTER_STOPPED
;
3185 message
= "Printer stopped due to backend errors; please "
3186 "consult the error_log file for details.";
3188 if (job_state
== IPP_JOB_COMPLETED
)
3190 job_state
= IPP_JOB_PENDING
;
3192 ippSetString(job
->attrs
, &job
->reasons
, 0,
3193 "resources-are-not-ready");
3197 case CUPS_BACKEND_AUTH_REQUIRED
:
3199 * Hold the job for authentication...
3202 if (job_state
== IPP_JOB_COMPLETED
)
3204 cupsdSetJobHoldUntil(job
, "auth-info-required", 1);
3206 job_state
= IPP_JOB_HELD
;
3207 message
= "Job held for authentication.";
3209 ippSetString(job
->attrs
, &job
->reasons
, 0,
3210 "cups-held-for-authentication");
3214 case CUPS_BACKEND_RETRY
:
3215 if (job_state
== IPP_JOB_COMPLETED
)
3218 * Hold the job if the number of retries is less than the
3219 * JobRetryLimit, otherwise abort the job.
3224 if (job
->tries
> JobRetryLimit
&& JobRetryLimit
> 0)
3230 snprintf(buffer
, sizeof(buffer
),
3231 "Job aborted after %d unsuccessful attempts.",
3233 job_state
= IPP_JOB_ABORTED
;
3236 ippSetString(job
->attrs
, &job
->reasons
, 0, "aborted-by-system");
3241 * Try again in N seconds...
3244 snprintf(buffer
, sizeof(buffer
),
3245 "Job held for %d seconds since it could not be sent.",
3248 job
->hold_until
= time(NULL
) + JobRetryInterval
;
3249 job_state
= IPP_JOB_HELD
;
3252 ippSetString(job
->attrs
, &job
->reasons
, 0,
3253 "resources-are-not-ready");
3258 case CUPS_BACKEND_RETRY_CURRENT
:
3260 * Mark the job as pending and retry on the same printer...
3263 if (job_state
== IPP_JOB_COMPLETED
)
3265 job_state
= IPP_JOB_PENDING
;
3266 message
= "Retrying job on same printer.";
3268 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
3273 else if (job
->status
> 0)
3276 * Filter had errors; stop job...
3279 if (job_state
== IPP_JOB_COMPLETED
)
3281 job_state
= IPP_JOB_STOPPED
;
3282 message
= "Job stopped due to filter errors; please consult the "
3283 "error_log file for details.";
3285 if (WIFSIGNALED(job
->status
))
3286 ippSetString(job
->attrs
, &job
->reasons
, 0, "cups-filter-crashed");
3288 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-completed-with-errors");
3293 * Update the printer and job state.
3296 if (set_job_state
&& job_state
!= job
->state_value
)
3297 cupsdSetJobState(job
, job_state
, CUPSD_JOB_DEFAULT
, "%s", message
);
3299 cupsdSetPrinterState(job
->printer
, printer_state
,
3300 printer_state
== IPP_PRINTER_STOPPED
);
3301 update_job_attrs(job
, 0);
3306 (job
->state_value
== IPP_JOB_ABORTED
||
3307 job
->state_value
== IPP_JOB_STOPPED
))
3308 dump_job_history(job
);
3310 free_job_history(job
);
3313 cupsArrayRemove(PrintingJobs
, job
);
3316 * Clear the printer <-> job association...
3319 job
->printer
->job
= NULL
;
3320 job
->printer
= NULL
;
3323 * Try printing another job...
3326 if (printer_state
!= IPP_PRINTER_STOPPED
)
3332 * 'get_options()' - Get a string containing the job options.
3335 static char * /* O - Options string */
3336 get_options(cupsd_job_t
*job
, /* I - Job */
3337 int banner_page
, /* I - Printing a banner page? */
3338 char *copies
, /* I - Copies buffer */
3339 size_t copies_size
, /* I - Size of copies buffer */
3340 char *title
, /* I - Title buffer */
3341 size_t title_size
) /* I - Size of title buffer */
3343 int i
; /* Looping var */
3344 size_t newlength
; /* New option buffer length */
3345 char *optptr
, /* Pointer to options */
3346 *valptr
; /* Pointer in value string */
3347 ipp_attribute_t
*attr
; /* Current attribute */
3348 _ppd_cache_t
*pc
; /* PPD cache and mapping data */
3349 int num_pwgppds
; /* Number of PWG->PPD options */
3350 cups_option_t
*pwgppds
, /* PWG->PPD options */
3351 *pwgppd
, /* Current PWG->PPD option */
3352 *preset
; /* Current preset option */
3353 int print_color_mode
,
3354 /* Output mode (if any) */
3355 print_quality
; /* Print quality (if any) */
3356 const char *ppd
; /* PPD option choice */
3357 int exact
; /* Did we get an exact match? */
3358 static char *options
= NULL
;/* Full list of options */
3359 static size_t optlength
= 0; /* Length of option buffer */
3363 * Building the options string is harder than it needs to be, but for the
3364 * moment we need to pass strings for command-line args and not IPP attribute
3367 * First build an options array for any PWG->PPD mapped option/choice pairs.
3370 pc
= job
->printer
->pc
;
3375 !ippFindAttribute(job
->attrs
,
3376 "com.apple.print.DocumentTicket.PMSpoolFormat",
3378 !ippFindAttribute(job
->attrs
, "APPrinterPreset", IPP_TAG_ZERO
) &&
3379 (ippFindAttribute(job
->attrs
, "output-mode", IPP_TAG_ZERO
) ||
3380 ippFindAttribute(job
->attrs
, "print-color-mode", IPP_TAG_ZERO
) ||
3381 ippFindAttribute(job
->attrs
, "print-quality", IPP_TAG_ZERO
)))
3384 * Map output-mode and print-quality to a preset...
3387 if ((attr
= ippFindAttribute(job
->attrs
, "print-color-mode",
3388 IPP_TAG_KEYWORD
)) == NULL
)
3389 attr
= ippFindAttribute(job
->attrs
, "output-mode", IPP_TAG_KEYWORD
);
3391 if (attr
&& !strcmp(attr
->values
[0].string
.text
, "monochrome"))
3392 print_color_mode
= _PWG_PRINT_COLOR_MODE_MONOCHROME
;
3394 print_color_mode
= _PWG_PRINT_COLOR_MODE_COLOR
;
3396 if ((attr
= ippFindAttribute(job
->attrs
, "print-quality",
3397 IPP_TAG_ENUM
)) != NULL
&&
3398 attr
->values
[0].integer
>= IPP_QUALITY_DRAFT
&&
3399 attr
->values
[0].integer
<= IPP_QUALITY_HIGH
)
3400 print_quality
= attr
->values
[0].integer
- IPP_QUALITY_DRAFT
;
3402 print_quality
= _PWG_PRINT_QUALITY_NORMAL
;
3404 if (pc
->num_presets
[print_color_mode
][print_quality
] == 0)
3407 * Try to find a preset that works so that we maximize the chances of us
3408 * getting a good print using IPP attributes.
3411 if (pc
->num_presets
[print_color_mode
][_PWG_PRINT_QUALITY_NORMAL
] > 0)
3412 print_quality
= _PWG_PRINT_QUALITY_NORMAL
;
3413 else if (pc
->num_presets
[_PWG_PRINT_COLOR_MODE_COLOR
][print_quality
] > 0)
3414 print_color_mode
= _PWG_PRINT_COLOR_MODE_COLOR
;
3417 print_quality
= _PWG_PRINT_QUALITY_NORMAL
;
3418 print_color_mode
= _PWG_PRINT_COLOR_MODE_COLOR
;
3422 if (pc
->num_presets
[print_color_mode
][print_quality
] > 0)
3425 * Copy the preset options as long as the corresponding names are not
3426 * already defined in the IPP request...
3429 for (i
= pc
->num_presets
[print_color_mode
][print_quality
],
3430 preset
= pc
->presets
[print_color_mode
][print_quality
];
3434 if (!ippFindAttribute(job
->attrs
, preset
->name
, IPP_TAG_ZERO
))
3435 num_pwgppds
= cupsAddOption(preset
->name
, preset
->value
, num_pwgppds
,
3443 if (!ippFindAttribute(job
->attrs
, "InputSlot", IPP_TAG_ZERO
) &&
3444 !ippFindAttribute(job
->attrs
, "HPPaperSource", IPP_TAG_ZERO
))
3446 if ((ppd
= _ppdCacheGetInputSlot(pc
, job
->attrs
, NULL
)) != NULL
)
3447 num_pwgppds
= cupsAddOption(pc
->source_option
, ppd
, num_pwgppds
,
3450 if (!ippFindAttribute(job
->attrs
, "MediaType", IPP_TAG_ZERO
) &&
3451 (ppd
= _ppdCacheGetMediaType(pc
, job
->attrs
, NULL
)) != NULL
)
3452 num_pwgppds
= cupsAddOption("MediaType", ppd
, num_pwgppds
, &pwgppds
);
3454 if (!ippFindAttribute(job
->attrs
, "PageRegion", IPP_TAG_ZERO
) &&
3455 !ippFindAttribute(job
->attrs
, "PageSize", IPP_TAG_ZERO
) &&
3456 (ppd
= _ppdCacheGetPageSize(pc
, job
->attrs
, NULL
, &exact
)) != NULL
)
3458 num_pwgppds
= cupsAddOption("PageSize", ppd
, num_pwgppds
, &pwgppds
);
3460 if (!ippFindAttribute(job
->attrs
, "media", IPP_TAG_ZERO
))
3461 num_pwgppds
= cupsAddOption("media", ppd
, num_pwgppds
, &pwgppds
);
3464 if (!ippFindAttribute(job
->attrs
, "OutputBin", IPP_TAG_ZERO
) &&
3465 (attr
= ippFindAttribute(job
->attrs
, "output-bin",
3466 IPP_TAG_ZERO
)) != NULL
&&
3467 (attr
->value_tag
== IPP_TAG_KEYWORD
||
3468 attr
->value_tag
== IPP_TAG_NAME
) &&
3469 (ppd
= _ppdCacheGetOutputBin(pc
, attr
->values
[0].string
.text
)) != NULL
)
3472 * Map output-bin to OutputBin option...
3475 num_pwgppds
= cupsAddOption("OutputBin", ppd
, num_pwgppds
, &pwgppds
);
3478 if (pc
->sides_option
&&
3479 !ippFindAttribute(job
->attrs
, pc
->sides_option
, IPP_TAG_ZERO
) &&
3480 (attr
= ippFindAttribute(job
->attrs
, "sides", IPP_TAG_KEYWORD
)) != NULL
)
3483 * Map sides to duplex option...
3486 if (!strcmp(attr
->values
[0].string
.text
, "one-sided"))
3487 num_pwgppds
= cupsAddOption(pc
->sides_option
, pc
->sides_1sided
,
3488 num_pwgppds
, &pwgppds
);
3489 else if (!strcmp(attr
->values
[0].string
.text
, "two-sided-long-edge"))
3490 num_pwgppds
= cupsAddOption(pc
->sides_option
, pc
->sides_2sided_long
,
3491 num_pwgppds
, &pwgppds
);
3492 else if (!strcmp(attr
->values
[0].string
.text
, "two-sided-short-edge"))
3493 num_pwgppds
= cupsAddOption(pc
->sides_option
, pc
->sides_2sided_short
,
3494 num_pwgppds
, &pwgppds
);
3498 * Map finishings values...
3501 num_pwgppds
= _ppdCacheGetFinishingOptions(pc
, job
->attrs
,
3502 IPP_FINISHINGS_NONE
, num_pwgppds
,
3507 * Figure out how much room we need...
3510 newlength
= ipp_length(job
->attrs
);
3512 for (i
= num_pwgppds
, pwgppd
= pwgppds
; i
> 0; i
--, pwgppd
++)
3513 newlength
+= 1 + strlen(pwgppd
->name
) + 1 + strlen(pwgppd
->value
);
3516 * Then allocate/reallocate the option buffer as needed...
3519 if (newlength
== 0) /* This can never happen, but Clang */
3520 newlength
= 1; /* thinks it can... */
3522 if (newlength
> optlength
|| !options
)
3525 optptr
= malloc(newlength
);
3527 optptr
= realloc(options
, newlength
);
3531 cupsdLogJob(job
, CUPSD_LOG_CRIT
,
3532 "Unable to allocate " CUPS_LLFMT
" bytes for option buffer.",
3533 CUPS_LLCAST newlength
);
3538 optlength
= newlength
;
3542 * Now loop through the attributes and convert them to the textual
3543 * representation used by the filters...
3549 snprintf(title
, title_size
, "%s-%d", job
->printer
->name
, job
->id
);
3550 strlcpy(copies
, "1", copies_size
);
3552 for (attr
= job
->attrs
->attrs
; attr
!= NULL
; attr
= attr
->next
)
3554 if (!strcmp(attr
->name
, "copies") &&
3555 attr
->value_tag
== IPP_TAG_INTEGER
)
3558 * Don't use the # copies attribute if we are printing the job sheets...
3562 snprintf(copies
, copies_size
, "%d", attr
->values
[0].integer
);
3564 else if (!strcmp(attr
->name
, "job-name") &&
3565 (attr
->value_tag
== IPP_TAG_NAME
||
3566 attr
->value_tag
== IPP_TAG_NAMELANG
))
3567 strlcpy(title
, attr
->values
[0].string
.text
, title_size
);
3568 else if (attr
->group_tag
== IPP_TAG_JOB
)
3571 * Filter out other unwanted attributes...
3574 if (attr
->value_tag
== IPP_TAG_NOVALUE
||
3575 attr
->value_tag
== IPP_TAG_MIMETYPE
||
3576 attr
->value_tag
== IPP_TAG_NAMELANG
||
3577 attr
->value_tag
== IPP_TAG_TEXTLANG
||
3578 (attr
->value_tag
== IPP_TAG_URI
&& strcmp(attr
->name
, "job-uuid")) ||
3579 attr
->value_tag
== IPP_TAG_URISCHEME
||
3580 attr
->value_tag
== IPP_TAG_BEGIN_COLLECTION
) /* Not yet supported */
3583 if (!strcmp(attr
->name
, "job-hold-until"))
3586 if (!strncmp(attr
->name
, "job-", 4) &&
3587 strcmp(attr
->name
, "job-billing") &&
3588 strcmp(attr
->name
, "job-impressions") &&
3589 strcmp(attr
->name
, "job-originating-host-name") &&
3590 strcmp(attr
->name
, "job-uuid") &&
3591 !(job
->printer
->type
& CUPS_PRINTER_REMOTE
))
3594 if ((!strcmp(attr
->name
, "job-impressions") ||
3595 !strcmp(attr
->name
, "page-label") ||
3596 !strcmp(attr
->name
, "page-border") ||
3597 !strncmp(attr
->name
, "number-up", 9) ||
3598 !strcmp(attr
->name
, "page-ranges") ||
3599 !strcmp(attr
->name
, "page-set") ||
3600 !_cups_strcasecmp(attr
->name
, "AP_FIRSTPAGE_InputSlot") ||
3601 !_cups_strcasecmp(attr
->name
, "AP_FIRSTPAGE_ManualFeed") ||
3602 !_cups_strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
3603 "PMTotalSidesImaged..n.") ||
3604 !_cups_strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
3605 "PMTotalBeginPages..n.")) &&
3610 * Otherwise add them to the list...
3613 if (optptr
> options
)
3614 strlcat(optptr
, " ", optlength
- (optptr
- options
));
3616 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
3618 strlcat(optptr
, attr
->name
, optlength
- (optptr
- options
));
3619 strlcat(optptr
, "=", optlength
- (optptr
- options
));
3622 for (i
= 0; i
< attr
->num_values
; i
++)
3625 strlcat(optptr
, ",", optlength
- (optptr
- options
));
3627 optptr
+= strlen(optptr
);
3629 switch (attr
->value_tag
)
3631 case IPP_TAG_INTEGER
:
3633 snprintf(optptr
, optlength
- (optptr
- options
),
3634 "%d", attr
->values
[i
].integer
);
3637 case IPP_TAG_BOOLEAN
:
3638 if (!attr
->values
[i
].boolean
)
3639 strlcat(optptr
, "no", optlength
- (optptr
- options
));
3641 strlcat(optptr
, attr
->name
,
3642 optlength
- (optptr
- options
));
3645 case IPP_TAG_RANGE
:
3646 if (attr
->values
[i
].range
.lower
== attr
->values
[i
].range
.upper
)
3647 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
3648 "%d", attr
->values
[i
].range
.lower
);
3650 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
3651 "%d-%d", attr
->values
[i
].range
.lower
,
3652 attr
->values
[i
].range
.upper
);
3655 case IPP_TAG_RESOLUTION
:
3656 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
3657 "%dx%d%s", attr
->values
[i
].resolution
.xres
,
3658 attr
->values
[i
].resolution
.yres
,
3659 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
3663 case IPP_TAG_STRING
:
3666 case IPP_TAG_KEYWORD
:
3667 case IPP_TAG_CHARSET
:
3668 case IPP_TAG_LANGUAGE
:
3670 for (valptr
= attr
->values
[i
].string
.text
; *valptr
;)
3672 if (strchr(" \t\n\\\'\"", *valptr
))
3674 *optptr
++ = *valptr
++;
3681 break; /* anti-compiler-warning-code */
3685 optptr
+= strlen(optptr
);
3690 * Finally loop through the PWG->PPD mapped options and add them...
3693 for (i
= num_pwgppds
, pwgppd
= pwgppds
; i
> 0; i
--, pwgppd
++)
3696 strcpy(optptr
, pwgppd
->name
);
3697 optptr
+= strlen(optptr
);
3699 strcpy(optptr
, pwgppd
->value
);
3700 optptr
+= strlen(optptr
);
3703 cupsFreeOptions(num_pwgppds
, pwgppds
);
3706 * Return the options string...
3714 * 'ipp_length()' - Compute the size of the buffer needed to hold
3715 * the textual IPP attributes.
3718 static size_t /* O - Size of attribute buffer */
3719 ipp_length(ipp_t
*ipp
) /* I - IPP request */
3721 size_t bytes
; /* Number of bytes */
3722 int i
; /* Looping var */
3723 ipp_attribute_t
*attr
; /* Current attribute */
3727 * Loop through all attributes...
3732 for (attr
= ipp
->attrs
; attr
!= NULL
; attr
= attr
->next
)
3735 * Skip attributes that won't be sent to filters...
3738 if (attr
->value_tag
== IPP_TAG_NOVALUE
||
3739 attr
->value_tag
== IPP_TAG_MIMETYPE
||
3740 attr
->value_tag
== IPP_TAG_NAMELANG
||
3741 attr
->value_tag
== IPP_TAG_TEXTLANG
||
3742 attr
->value_tag
== IPP_TAG_URI
||
3743 attr
->value_tag
== IPP_TAG_URISCHEME
)
3747 * Add space for a leading space and commas between each value.
3748 * For the first attribute, the leading space isn't used, so the
3749 * extra byte can be used as the nul terminator...
3752 bytes
++; /* " " separator */
3753 bytes
+= attr
->num_values
; /* "," separators */
3756 * Boolean attributes appear as "foo,nofoo,foo,nofoo", while
3757 * other attributes appear as "foo=value1,value2,...,valueN".
3760 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
3761 bytes
+= strlen(attr
->name
);
3763 bytes
+= attr
->num_values
* strlen(attr
->name
);
3766 * Now add the size required for each value in the attribute...
3769 switch (attr
->value_tag
)
3771 case IPP_TAG_INTEGER
:
3774 * Minimum value of a signed integer is -2147483647, or 11 digits.
3777 bytes
+= attr
->num_values
* 11;
3780 case IPP_TAG_BOOLEAN
:
3782 * Add two bytes for each false ("no") value...
3785 for (i
= 0; i
< attr
->num_values
; i
++)
3786 if (!attr
->values
[i
].boolean
)
3790 case IPP_TAG_RANGE
:
3792 * A range is two signed integers separated by a hyphen, or
3793 * 23 characters max.
3796 bytes
+= attr
->num_values
* 23;
3799 case IPP_TAG_RESOLUTION
:
3801 * A resolution is two signed integers separated by an "x" and
3802 * suffixed by the units, or 26 characters max.
3805 bytes
+= attr
->num_values
* 26;
3808 case IPP_TAG_STRING
:
3811 case IPP_TAG_KEYWORD
:
3812 case IPP_TAG_CHARSET
:
3813 case IPP_TAG_LANGUAGE
:
3816 * Strings can contain characters that need quoting. We need
3817 * at least 2 * len + 2 characters to cover the quotes and
3818 * any backslashes in the string.
3821 for (i
= 0; i
< attr
->num_values
; i
++)
3822 bytes
+= 2 * strlen(attr
->values
[i
].string
.text
) + 2;
3826 break; /* anti-compiler-warning-code */
3835 * 'load_job_cache()' - Load jobs from the job.cache file.
3839 load_job_cache(const char *filename
) /* I - job.cache filename */
3841 cups_file_t
*fp
; /* job.cache file */
3842 char line
[1024], /* Line buffer */
3843 *value
; /* Value on line */
3844 int linenum
; /* Line number in file */
3845 cupsd_job_t
*job
; /* Current job */
3846 int jobid
; /* Job ID */
3847 char jobfile
[1024]; /* Job filename */
3851 * Open the job.cache file...
3854 if ((fp
= cupsdOpenConfFile(filename
)) == NULL
)
3856 load_request_root();
3861 * Read entries from the job cache file and create jobs as needed.
3864 cupsdLogMessage(CUPSD_LOG_INFO
, "Loading job cache file \"%s\"...",
3870 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
3872 if (!_cups_strcasecmp(line
, "NextJobId"))
3875 NextJobId
= atoi(value
);
3877 else if (!_cups_strcasecmp(line
, "<Job"))
3881 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing </Job> directive on line %d.",
3888 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing job ID on line %d.", linenum
);
3892 jobid
= atoi(value
);
3896 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad job ID %d on line %d.", jobid
,
3901 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, jobid
);
3902 if (access(jobfile
, 0))
3904 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d.N", RequestRoot
, jobid
);
3905 if (access(jobfile
, 0))
3907 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Job %d] Files have gone away.",
3913 job
= calloc(1, sizeof(cupsd_job_t
));
3916 cupsdLogMessage(CUPSD_LOG_EMERG
,
3917 "[Job %d] Unable to allocate memory for job.", jobid
);
3922 job
->back_pipes
[0] = -1;
3923 job
->back_pipes
[1] = -1;
3924 job
->print_pipes
[0] = -1;
3925 job
->print_pipes
[1] = -1;
3926 job
->side_pipes
[0] = -1;
3927 job
->side_pipes
[1] = -1;
3928 job
->status_pipes
[0] = -1;
3929 job
->status_pipes
[1] = -1;
3931 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Loading from cache...",
3936 cupsdLogMessage(CUPSD_LOG_ERROR
,
3937 "Missing <Job #> directive on line %d.", linenum
);
3940 else if (!_cups_strcasecmp(line
, "</Job>"))
3942 cupsArrayAdd(Jobs
, job
);
3944 if (job
->state_value
<= IPP_JOB_STOPPED
&& cupsdLoadJob(job
))
3945 cupsArrayAdd(ActiveJobs
, job
);
3951 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing value on line %d.", linenum
);
3954 else if (!_cups_strcasecmp(line
, "State"))
3956 job
->state_value
= (ipp_jstate_t
)atoi(value
);
3958 if (job
->state_value
< IPP_JOB_PENDING
)
3959 job
->state_value
= IPP_JOB_PENDING
;
3960 else if (job
->state_value
> IPP_JOB_COMPLETED
)
3961 job
->state_value
= IPP_JOB_COMPLETED
;
3963 else if (!_cups_strcasecmp(line
, "HoldUntil"))
3965 job
->hold_until
= atoi(value
);
3967 else if (!_cups_strcasecmp(line
, "Priority"))
3969 job
->priority
= atoi(value
);
3971 else if (!_cups_strcasecmp(line
, "Username"))
3973 cupsdSetString(&job
->username
, value
);
3975 else if (!_cups_strcasecmp(line
, "Destination"))
3977 cupsdSetString(&job
->dest
, value
);
3979 else if (!_cups_strcasecmp(line
, "DestType"))
3981 job
->dtype
= (cups_ptype_t
)atoi(value
);
3983 else if (!_cups_strcasecmp(line
, "NumFiles"))
3985 job
->num_files
= atoi(value
);
3987 if (job
->num_files
< 0)
3989 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad NumFiles value %d on line %d.",
3990 job
->num_files
, linenum
);
3995 if (job
->num_files
> 0)
3997 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-001", RequestRoot
,
3999 if (access(jobfile
, 0))
4001 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Data files have gone away.",
4007 job
->filetypes
= calloc(job
->num_files
, sizeof(mime_type_t
*));
4008 job
->compressions
= calloc(job
->num_files
, sizeof(int));
4010 if (!job
->filetypes
|| !job
->compressions
)
4012 cupsdLogMessage(CUPSD_LOG_EMERG
,
4013 "[Job %d] Unable to allocate memory for %d files.",
4014 job
->id
, job
->num_files
);
4019 else if (!_cups_strcasecmp(line
, "File"))
4021 int number
, /* File number */
4022 compression
; /* Compression value */
4023 char super
[MIME_MAX_SUPER
], /* MIME super type */
4024 type
[MIME_MAX_TYPE
]; /* MIME type */
4027 if (sscanf(value
, "%d%*[ \t]%15[^/]/%255s%d", &number
, super
, type
,
4030 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File on line %d.", linenum
);
4034 if (number
< 1 || number
> job
->num_files
)
4036 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File number %d on line %d.",
4043 job
->compressions
[number
] = compression
;
4044 job
->filetypes
[number
] = mimeType(MimeDatabase
, super
, type
);
4046 if (!job
->filetypes
[number
])
4049 * If the original MIME type is unknown, auto-type it!
4052 cupsdLogMessage(CUPSD_LOG_ERROR
,
4053 "[Job %d] Unknown MIME type %s/%s for file %d.",
4054 job
->id
, super
, type
, number
+ 1);
4056 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
4057 job
->id
, number
+ 1);
4058 job
->filetypes
[number
] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
4059 job
->compressions
+ number
);
4062 * If that didn't work, assume it is raw...
4065 if (!job
->filetypes
[number
])
4066 job
->filetypes
[number
] = mimeType(MimeDatabase
, "application",
4071 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unknown %s directive on line %d.",
4080 * 'load_next_job_id()' - Load the NextJobId value from the job.cache file.
4084 load_next_job_id(const char *filename
) /* I - job.cache filename */
4086 cups_file_t
*fp
; /* job.cache file */
4087 char line
[1024], /* Line buffer */
4088 *value
; /* Value on line */
4089 int linenum
; /* Line number in file */
4090 int next_job_id
; /* NextJobId value from line */
4094 * Read the NextJobId directive from the job.cache file and use
4095 * the value (if any).
4098 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
4100 if (errno
!= ENOENT
)
4101 cupsdLogMessage(CUPSD_LOG_ERROR
,
4102 "Unable to open job cache file \"%s\": %s",
4103 filename
, strerror(errno
));
4108 cupsdLogMessage(CUPSD_LOG_INFO
,
4109 "Loading NextJobId from job cache file \"%s\"...", filename
);
4113 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
4115 if (!_cups_strcasecmp(line
, "NextJobId"))
4119 next_job_id
= atoi(value
);
4121 if (next_job_id
> NextJobId
)
4122 NextJobId
= next_job_id
;
4133 * 'load_request_root()' - Load jobs from the RequestRoot directory.
4137 load_request_root(void)
4139 cups_dir_t
*dir
; /* Directory */
4140 cups_dentry_t
*dent
; /* Directory entry */
4141 cupsd_job_t
*job
; /* New job */
4145 * Open the requests directory...
4148 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Scanning %s for jobs...", RequestRoot
);
4150 if ((dir
= cupsDirOpen(RequestRoot
)) == NULL
)
4152 cupsdLogMessage(CUPSD_LOG_ERROR
,
4153 "Unable to open spool directory \"%s\": %s",
4154 RequestRoot
, strerror(errno
));
4159 * Read all the c##### files...
4162 while ((dent
= cupsDirRead(dir
)) != NULL
)
4163 if (strlen(dent
->filename
) >= 6 && dent
->filename
[0] == 'c')
4166 * Allocate memory for the job...
4169 if ((job
= calloc(sizeof(cupsd_job_t
), 1)) == NULL
)
4171 cupsdLogMessage(CUPSD_LOG_ERROR
, "Ran out of memory for jobs.");
4177 * Assign the job ID...
4180 job
->id
= atoi(dent
->filename
+ 1);
4181 job
->back_pipes
[0] = -1;
4182 job
->back_pipes
[1] = -1;
4183 job
->print_pipes
[0] = -1;
4184 job
->print_pipes
[1] = -1;
4185 job
->side_pipes
[0] = -1;
4186 job
->side_pipes
[1] = -1;
4187 job
->status_pipes
[0] = -1;
4188 job
->status_pipes
[1] = -1;
4190 if (job
->id
>= NextJobId
)
4191 NextJobId
= job
->id
+ 1;
4197 if (cupsdLoadJob(job
))
4200 * Insert the job into the array, sorting by job priority and ID...
4203 cupsArrayAdd(Jobs
, job
);
4205 if (job
->state_value
<= IPP_JOB_STOPPED
)
4206 cupsArrayAdd(ActiveJobs
, job
);
4217 * 'set_time()' - Set one of the "time-at-xyz" attributes.
4221 set_time(cupsd_job_t
*job
, /* I - Job to update */
4222 const char *name
) /* I - Name of attribute */
4224 ipp_attribute_t
*attr
; /* Time attribute */
4227 if ((attr
= ippFindAttribute(job
->attrs
, name
, IPP_TAG_ZERO
)) != NULL
)
4229 attr
->value_tag
= IPP_TAG_INTEGER
;
4230 attr
->values
[0].integer
= time(NULL
);
4236 * 'start_job()' - Start a print job.
4240 start_job(cupsd_job_t
*job
, /* I - Job ID */
4241 cupsd_printer_t
*printer
) /* I - Printer to print job */
4243 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "start_job(job=%p(%d), printer=%p(%s))",
4244 job
, job
->id
, printer
, printer
->name
);
4247 * Make sure we have some files around before we try to print...
4250 if (job
->num_files
== 0)
4252 ippSetString(job
->attrs
, &job
->reasons
, 0, "aborted-by-system");
4253 cupsdSetJobState(job
, IPP_JOB_ABORTED
, CUPSD_JOB_DEFAULT
,
4254 "Aborting job because it has no files.");
4259 * Update the printer and job state to "processing"...
4262 if (!cupsdLoadJob(job
))
4265 if (job
->printer_message
)
4266 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
), "");
4268 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-printing");
4269 cupsdSetJobState(job
, IPP_JOB_PROCESSING
, CUPSD_JOB_DEFAULT
, NULL
);
4270 cupsdSetPrinterState(printer
, IPP_PRINTER_PROCESSING
, 0);
4271 cupsdSetPrinterReasons(printer
, "-cups-remote-pending,"
4272 "cups-remote-pending-held,"
4273 "cups-remote-processing,"
4274 "cups-remote-stopped,"
4275 "cups-remote-canceled,"
4276 "cups-remote-aborted,"
4277 "cups-remote-completed");
4280 job
->current_file
= 0;
4282 job
->printer
= printer
;
4286 job
->cancel_time
= time(NULL
) + MaxJobTime
;
4288 job
->cancel_time
= 0;
4291 * Setup the last exit status and security profiles...
4295 job
->profile
= cupsdCreateProfile(job
->id
);
4298 * Create the status pipes and buffer...
4301 if (cupsdOpenPipe(job
->status_pipes
))
4303 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
4304 "Unable to create job status pipes - %s.", strerror(errno
));
4306 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
4307 "Job stopped because the scheduler could not create the "
4308 "job status pipes.");
4310 cupsdDestroyProfile(job
->profile
);
4311 job
->profile
= NULL
;
4315 job
->status_buffer
= cupsdStatBufNew(job
->status_pipes
[0], NULL
);
4316 job
->status_level
= CUPSD_LOG_INFO
;
4319 * Create the backchannel pipes and make them non-blocking...
4322 if (cupsdOpenPipe(job
->back_pipes
))
4324 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
4325 "Unable to create back-channel pipes - %s.", strerror(errno
));
4327 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
4328 "Job stopped because the scheduler could not create the "
4329 "back-channel pipes.");
4331 cupsdClosePipe(job
->status_pipes
);
4332 cupsdStatBufDelete(job
->status_buffer
);
4333 job
->status_buffer
= NULL
;
4335 cupsdDestroyProfile(job
->profile
);
4336 job
->profile
= NULL
;
4340 fcntl(job
->back_pipes
[0], F_SETFL
,
4341 fcntl(job
->back_pipes
[0], F_GETFL
) | O_NONBLOCK
);
4342 fcntl(job
->back_pipes
[1], F_SETFL
,
4343 fcntl(job
->back_pipes
[1], F_GETFL
) | O_NONBLOCK
);
4346 * Create the side-channel pipes and make them non-blocking...
4349 if (socketpair(AF_LOCAL
, SOCK_STREAM
, 0, job
->side_pipes
))
4351 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
4352 "Unable to create side-channel pipes - %s.", strerror(errno
));
4354 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
4355 "Job stopped because the scheduler could not create the "
4356 "side-channel pipes.");
4358 cupsdClosePipe(job
->back_pipes
);
4360 cupsdClosePipe(job
->status_pipes
);
4361 cupsdStatBufDelete(job
->status_buffer
);
4362 job
->status_buffer
= NULL
;
4364 cupsdDestroyProfile(job
->profile
);
4365 job
->profile
= NULL
;
4369 fcntl(job
->side_pipes
[0], F_SETFL
,
4370 fcntl(job
->side_pipes
[0], F_GETFL
) | O_NONBLOCK
);
4371 fcntl(job
->side_pipes
[1], F_SETFL
,
4372 fcntl(job
->side_pipes
[1], F_GETFL
) | O_NONBLOCK
);
4374 fcntl(job
->side_pipes
[0], F_SETFD
,
4375 fcntl(job
->side_pipes
[0], F_GETFD
) | FD_CLOEXEC
);
4376 fcntl(job
->side_pipes
[1], F_SETFD
,
4377 fcntl(job
->side_pipes
[1], F_GETFD
) | FD_CLOEXEC
);
4380 * Now start the first file in the job...
4383 cupsdContinueJob(job
);
4388 * 'stop_job()' - Stop a print job.
4392 stop_job(cupsd_job_t
*job
, /* I - Job */
4393 cupsd_jobaction_t action
) /* I - Action */
4395 int i
; /* Looping var */
4398 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "stop_job(job=%p(%d), action=%d)", job
,
4401 FilterLevel
-= job
->cost
;
4404 if (action
== CUPSD_JOB_DEFAULT
&& !job
->kill_time
)
4405 job
->kill_time
= time(NULL
) + JobKillDelay
;
4406 else if (action
>= CUPSD_JOB_FORCE
)
4409 for (i
= 0; job
->filters
[i
]; i
++)
4410 if (job
->filters
[i
] > 0)
4412 cupsdEndProcess(job
->filters
[i
], action
>= CUPSD_JOB_FORCE
);
4414 if (action
>= CUPSD_JOB_FORCE
)
4415 job
->filters
[i
] = -job
->filters
[i
];
4418 if (job
->backend
> 0)
4420 cupsdEndProcess(job
->backend
, action
>= CUPSD_JOB_FORCE
);
4422 if (action
>= CUPSD_JOB_FORCE
)
4423 job
->backend
= -job
->backend
;
4426 if (action
>= CUPSD_JOB_FORCE
)
4429 * Clear job status...
4438 * 'unload_job()' - Unload a job from memory.
4442 unload_job(cupsd_job_t
*job
) /* I - Job */
4447 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Unloading...", job
->id
);
4449 ippDelete(job
->attrs
);
4453 job
->reasons
= NULL
;
4455 job
->job_sheets
= NULL
;
4456 job
->printer_message
= NULL
;
4457 job
->printer_reasons
= NULL
;
4462 * 'update_job()' - Read a status update from a job's filters.
4466 update_job(cupsd_job_t
*job
) /* I - Job to check */
4468 int i
; /* Looping var */
4469 int copies
; /* Number of copies printed */
4470 char message
[CUPSD_SB_BUFFER_SIZE
],
4472 *ptr
; /* Pointer update... */
4473 int loglevel
, /* Log level for message */
4474 event
= 0; /* Events? */
4475 static const char * const levels
[] = /* Log levels */
4491 * Get the printer associated with this job; if the printer is stopped for
4492 * any reason then job->printer will be reset to NULL, so make sure we have
4493 * a valid pointer...
4496 while ((ptr
= cupsdStatBufUpdate(job
->status_buffer
, &loglevel
,
4497 message
, sizeof(message
))) != NULL
)
4500 * Process page and printer state messages as needed...
4503 if (loglevel
== CUPSD_LOG_PAGE
)
4506 * Page message; send the message to the page_log file and update the
4507 * job sheet count...
4510 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "PAGE: %s", message
);
4514 if (!_cups_strncasecmp(message
, "total ", 6))
4517 * Got a total count of pages from a backend or filter...
4520 copies
= atoi(message
+ 6);
4521 copies
-= job
->sheets
->values
[0].integer
; /* Just track the delta */
4523 else if (!sscanf(message
, "%*d%d", &copies
))
4526 job
->sheets
->values
[0].integer
+= copies
;
4528 if (job
->printer
->page_limit
)
4529 cupsdUpdateQuota(job
->printer
, job
->username
, copies
, 0);
4532 cupsdLogPage(job
, message
);
4535 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
4536 "Printed %d page(s).", job
->sheets
->values
[0].integer
);
4538 else if (loglevel
== CUPSD_LOG_STATE
)
4540 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "STATE: %s", message
);
4542 if (!strcmp(message
, "paused"))
4544 cupsdStopPrinter(job
->printer
, 1);
4547 else if (cupsdSetPrinterReasons(job
->printer
, message
))
4549 event
|= CUPSD_EVENT_PRINTER_STATE
;
4551 if (MaxJobTime
> 0 && strstr(message
, "connecting-to-device") != NULL
)
4554 * Reset cancel time after connecting to the device...
4557 for (i
= 0; i
< job
->printer
->num_reasons
; i
++)
4558 if (!strcmp(job
->printer
->reasons
[i
], "connecting-to-device"))
4561 if (i
>= job
->printer
->num_reasons
)
4562 job
->cancel_time
= time(NULL
) + MaxJobTime
;
4566 update_job_attrs(job
, 0);
4568 else if (loglevel
== CUPSD_LOG_ATTR
)
4571 * Set attribute(s)...
4574 int num_attrs
; /* Number of attributes */
4575 cups_option_t
*attrs
; /* Attributes */
4576 const char *attr
; /* Attribute */
4579 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "ATTR: %s", message
);
4581 num_attrs
= cupsParseOptions(message
, 0, &attrs
);
4583 if ((attr
= cupsGetOption("auth-info-required", num_attrs
,
4586 cupsdSetAuthInfoRequired(job
->printer
, attr
, NULL
);
4587 cupsdSetPrinterAttrs(job
->printer
);
4589 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4592 if ((attr
= cupsGetOption("job-media-progress", num_attrs
,
4595 int progress
= atoi(attr
);
4598 if (progress
>= 0 && progress
<= 100)
4600 job
->progress
= progress
;
4603 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
4604 "Printing page %d, %d%%",
4605 job
->sheets
->values
[0].integer
, job
->progress
);
4609 if ((attr
= cupsGetOption("printer-alert", num_attrs
, attrs
)) != NULL
)
4611 cupsdSetString(&job
->printer
->alert
, attr
);
4612 event
|= CUPSD_EVENT_PRINTER_STATE
;
4615 if ((attr
= cupsGetOption("printer-alert-description", num_attrs
,
4618 cupsdSetString(&job
->printer
->alert_description
, attr
);
4619 event
|= CUPSD_EVENT_PRINTER_STATE
;
4622 if ((attr
= cupsGetOption("marker-colors", num_attrs
, attrs
)) != NULL
)
4624 cupsdSetPrinterAttr(job
->printer
, "marker-colors", (char *)attr
);
4625 job
->printer
->marker_time
= time(NULL
);
4626 event
|= CUPSD_EVENT_PRINTER_STATE
;
4627 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4630 if ((attr
= cupsGetOption("marker-levels", num_attrs
, attrs
)) != NULL
)
4632 cupsdSetPrinterAttr(job
->printer
, "marker-levels", (char *)attr
);
4633 job
->printer
->marker_time
= time(NULL
);
4634 event
|= CUPSD_EVENT_PRINTER_STATE
;
4635 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4638 if ((attr
= cupsGetOption("marker-low-levels", num_attrs
, attrs
)) != NULL
)
4640 cupsdSetPrinterAttr(job
->printer
, "marker-low-levels", (char *)attr
);
4641 job
->printer
->marker_time
= time(NULL
);
4642 event
|= CUPSD_EVENT_PRINTER_STATE
;
4643 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4646 if ((attr
= cupsGetOption("marker-high-levels", num_attrs
, attrs
)) != NULL
)
4648 cupsdSetPrinterAttr(job
->printer
, "marker-high-levels", (char *)attr
);
4649 job
->printer
->marker_time
= time(NULL
);
4650 event
|= CUPSD_EVENT_PRINTER_STATE
;
4651 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4654 if ((attr
= cupsGetOption("marker-message", num_attrs
, attrs
)) != NULL
)
4656 cupsdSetPrinterAttr(job
->printer
, "marker-message", (char *)attr
);
4657 job
->printer
->marker_time
= time(NULL
);
4658 event
|= CUPSD_EVENT_PRINTER_STATE
;
4659 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4662 if ((attr
= cupsGetOption("marker-names", num_attrs
, attrs
)) != NULL
)
4664 cupsdSetPrinterAttr(job
->printer
, "marker-names", (char *)attr
);
4665 job
->printer
->marker_time
= time(NULL
);
4666 event
|= CUPSD_EVENT_PRINTER_STATE
;
4667 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4670 if ((attr
= cupsGetOption("marker-types", num_attrs
, attrs
)) != NULL
)
4672 cupsdSetPrinterAttr(job
->printer
, "marker-types", (char *)attr
);
4673 job
->printer
->marker_time
= time(NULL
);
4674 event
|= CUPSD_EVENT_PRINTER_STATE
;
4675 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4678 cupsFreeOptions(num_attrs
, attrs
);
4680 else if (loglevel
== CUPSD_LOG_PPD
)
4683 * Set attribute(s)...
4686 int num_keywords
; /* Number of keywords */
4687 cups_option_t
*keywords
; /* Keywords */
4690 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "PPD: %s", message
);
4692 num_keywords
= cupsParseOptions(message
, 0, &keywords
);
4694 if (cupsdUpdatePrinterPPD(job
->printer
, num_keywords
, keywords
))
4695 cupsdSetPrinterAttrs(job
->printer
);
4697 cupsFreeOptions(num_keywords
, keywords
);
4702 * Strip legacy message prefix...
4705 if (!strncmp(message
, "recoverable:", 12))
4708 while (isspace(*ptr
& 255))
4711 else if (!strncmp(message
, "recovered:", 10))
4714 while (isspace(*ptr
& 255))
4721 cupsdLogJob(job
, loglevel
, "%s", ptr
);
4723 if (loglevel
< CUPSD_LOG_DEBUG
&&
4724 strcmp(job
->printer
->state_message
, ptr
))
4726 strlcpy(job
->printer
->state_message
, ptr
,
4727 sizeof(job
->printer
->state_message
));
4729 event
|= CUPSD_EVENT_PRINTER_STATE
| CUPSD_EVENT_JOB_PROGRESS
;
4731 if (loglevel
<= job
->status_level
&& job
->status_level
> CUPSD_LOG_ERROR
)
4734 * Some messages show in the job-printer-state-message attribute...
4737 if (loglevel
!= CUPSD_LOG_NOTICE
)
4738 job
->status_level
= loglevel
;
4740 update_job_attrs(job
, 1);
4742 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
4743 "Set job-printer-state-message to \"%s\", "
4745 job
->printer_message
->values
[0].string
.text
,
4746 levels
[job
->status_level
]);
4751 if (!strchr(job
->status_buffer
->buffer
, '\n'))
4755 if (event
& CUPSD_EVENT_JOB_PROGRESS
)
4756 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
4757 "%s", job
->printer
->state_message
);
4758 if (event
& CUPSD_EVENT_PRINTER_STATE
)
4759 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, job
->printer
, NULL
,
4760 (job
->printer
->type
& CUPS_PRINTER_CLASS
) ?
4761 "Class \"%s\" state changed." :
4762 "Printer \"%s\" state changed.",
4763 job
->printer
->name
);
4766 if (ptr
== NULL
&& !job
->status_buffer
->bufused
)
4769 * See if all of the filters and the backend have returned their
4773 for (i
= 0; job
->filters
[i
] < 0; i
++);
4775 if (job
->filters
[i
])
4778 * EOF but we haven't collected the exit status of all filters...
4781 cupsdCheckProcess();
4785 if (job
->current_file
>= job
->num_files
&& job
->backend
> 0)
4788 * EOF but we haven't collected the exit status of the backend...
4791 cupsdCheckProcess();
4796 * Handle the end of job stuff...
4799 finalize_job(job
, 1);
4802 * Check for new jobs...
4811 * 'update_job_attrs()' - Update the job-printer-* attributes.
4815 update_job_attrs(cupsd_job_t
*job
, /* I - Job to update */
4816 int do_message
)/* I - 1 = copy job-printer-state message */
4818 int i
; /* Looping var */
4819 int num_reasons
; /* Actual number of reasons */
4820 const char * const *reasons
; /* Reasons */
4821 static const char *none
= "none"; /* "none" reason */
4825 * Get/create the job-printer-state-* attributes...
4828 if (!job
->printer_message
)
4830 if ((job
->printer_message
= ippFindAttribute(job
->attrs
,
4831 "job-printer-state-message",
4832 IPP_TAG_TEXT
)) == NULL
)
4833 job
->printer_message
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_TEXT
,
4834 "job-printer-state-message",
4838 if (!job
->printer_reasons
)
4839 job
->printer_reasons
= ippFindAttribute(job
->attrs
,
4840 "job-printer-state-reasons",
4844 * Copy or clear the printer-state-message value as needed...
4847 if (job
->state_value
!= IPP_JOB_PROCESSING
&&
4848 job
->status_level
== CUPSD_LOG_INFO
)
4850 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
), "");
4853 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
4855 else if (job
->printer
->state_message
[0] && do_message
)
4857 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
),
4858 job
->printer
->state_message
);
4861 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
4865 * ... and the printer-state-reasons value...
4868 if (job
->printer
->num_reasons
== 0)
4875 num_reasons
= job
->printer
->num_reasons
;
4876 reasons
= (const char * const *)job
->printer
->reasons
;
4879 if (!job
->printer_reasons
|| job
->printer_reasons
->num_values
!= num_reasons
)
4882 * Replace/create a job-printer-state-reasons attribute...
4885 ippDeleteAttribute(job
->attrs
, job
->printer_reasons
);
4887 job
->printer_reasons
= ippAddStrings(job
->attrs
,
4888 IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
4889 "job-printer-state-reasons",
4890 num_reasons
, NULL
, NULL
);
4895 * Don't bother clearing the reason strings if they are the same...
4898 for (i
= 0; i
< num_reasons
; i
++)
4899 if (strcmp(job
->printer_reasons
->values
[i
].string
.text
, reasons
[i
]))
4902 if (i
>= num_reasons
)
4906 * Not the same, so free the current strings...
4909 for (i
= 0; i
< num_reasons
; i
++)
4910 _cupsStrFree(job
->printer_reasons
->values
[i
].string
.text
);
4914 * Copy the reasons...
4917 for (i
= 0; i
< num_reasons
; i
++)
4918 job
->printer_reasons
->values
[i
].string
.text
= _cupsStrAlloc(reasons
[i
]);
4921 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
4926 * End of "$Id: job.c 7902 2008-09-03 14:20:17Z mike $".