2 * "$Id: job.c 7902 2008-09-03 14:20:17Z mike $"
4 * Job management routines for the CUPS scheduler.
6 * Copyright 2007-2011 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 * Start held jobs if they are ready...
311 if (job
->state_value
== IPP_JOB_HELD
&&
313 job
->hold_until
< curtime
)
315 if (job
->pending_timeout
)
318 * This job is pending; check that we don't have an active Send-Document
319 * operation in progress on any of the client connections, then timeout
320 * the job so we can start printing...
323 cupsd_client_t
*con
; /* Current client connection */
326 for (con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
328 con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
330 con
->request
->request
.op
.operation_id
== IPP_SEND_DOCUMENT
)
336 if (cupsdTimeoutJob(job
))
340 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
341 "Job submission timed out.");
345 * Continue jobs that are waiting on the FilterLimit...
348 if (job
->pending_cost
> 0 &&
349 ((FilterLevel
+ job
->pending_cost
) < FilterLimit
|| FilterLevel
== 0))
350 cupsdContinueJob(job
);
353 * Start pending jobs if the destination is available...
356 if (job
->state_value
== IPP_JOB_PENDING
&& !NeedReload
&&
357 #ifndef kIOPMAssertionTypeDenySystemSleep
359 #endif /* !kIOPMAssertionTypeDenySystemSleep */
360 !DoingShutdown
&& !job
->printer
)
362 printer
= cupsdFindDest(job
->dest
);
366 (printer
->type
& (CUPS_PRINTER_IMPLICIT
| CUPS_PRINTER_CLASS
)))
369 * If the class is remote, just pass it to the remote server...
374 if (pclass
->state
== IPP_PRINTER_STOPPED
)
376 else if (pclass
->type
& CUPS_PRINTER_REMOTE
)
379 printer
= cupsdFindAvailablePrinter(printer
->name
);
382 if (!printer
&& !pclass
)
385 * Whoa, the printer and/or class for this destination went away;
389 cupsdSetJobState(job
, IPP_JOB_ABORTED
, CUPSD_JOB_PURGE
,
390 "Job aborted because the destination printer/class "
393 else if (printer
&& !printer
->holding_new_jobs
)
396 * See if the printer is available or remote and not printing a job;
397 * if so, start the job...
403 * Add/update a job-actual-printer-uri attribute for this job
404 * so that we know which printer actually printed the job...
407 if ((attr
= ippFindAttribute(job
->attrs
, "job-actual-printer-uri",
408 IPP_TAG_URI
)) != NULL
)
409 cupsdSetString(&attr
->values
[0].string
.text
, printer
->uri
);
411 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
,
412 "job-actual-printer-uri", NULL
, printer
->uri
);
415 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
418 if ((!(printer
->type
& CUPS_PRINTER_DISCOVERED
) && /* Printer is local */
419 printer
->state
== IPP_PRINTER_IDLE
) || /* and idle, OR */
420 ((printer
->type
& CUPS_PRINTER_DISCOVERED
) && /* Printer is remote */
421 !printer
->job
)) /* and not printing */
427 start_job(job
, printer
);
436 * 'cupsdCleanJobs()' - Clean out old jobs.
442 cupsd_job_t
*job
; /* Current job */
445 if (MaxJobs
<= 0 && JobHistory
)
448 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
449 job
&& (cupsArrayCount(Jobs
) >= MaxJobs
|| !JobHistory
);
450 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
451 if (job
->state_value
>= IPP_JOB_CANCELED
&& !job
->printer
)
452 cupsdDeleteJob(job
, CUPSD_JOB_PURGE
);
457 * 'cupsdContinueJob()' - Continue printing with the next file in a job.
461 cupsdContinueJob(cupsd_job_t
*job
) /* I - Job */
463 int i
; /* Looping var */
464 int slot
; /* Pipe slot */
465 cups_array_t
*filters
= NULL
,/* Filters for job */
466 *prefilters
; /* Filters with prefilters */
467 mime_filter_t
*filter
, /* Current filter */
468 *prefilter
, /* Prefilter */
469 port_monitor
; /* Port monitor filter */
470 char scheme
[255]; /* Device URI scheme */
471 ipp_attribute_t
*attr
; /* Current attribute */
472 const char *ptr
, /* Pointer into value */
473 *abort_message
; /* Abort message */
474 ipp_jstate_t abort_state
= IPP_JOB_STOPPED
;
475 /* New job state on abort */
476 struct stat backinfo
; /* Backend file information */
477 int backroot
; /* Run backend as root? */
478 int pid
; /* Process ID of new filter process */
479 int banner_page
; /* 1 if banner page, 0 otherwise */
480 int filterfds
[2][2] = { { -1, -1 }, { -1, -1 } };
481 /* Pipes used between filters */
482 int envc
; /* Number of environment variables */
483 char **argv
= NULL
, /* Filter command-line arguments */
484 filename
[1024], /* Job filename */
485 command
[1024], /* Full path to command */
486 jobid
[255], /* Job ID string */
488 /* Job title string */
489 copies
[255], /* # copies string */
490 *options
, /* Options string */
492 /* Environment variables */
493 charset
[255], /* CHARSET env variable */
494 class_name
[255],/* CLASS env variable */
495 classification
[1024],
496 /* CLASSIFICATION env variable */
498 /* CONTENT_TYPE env variable */
500 /* DEVICE_URI env variable */
501 final_content_type
[1024],
502 /* FINAL_CONTENT_TYPE env variable */
503 lang
[255], /* LANG env variable */
506 /* APPLE_LANGUAGE env variable */
507 #endif /* __APPLE__ */
508 auth_info_required
[255],
509 /* AUTH_INFO_REQUIRED env variable */
510 ppd
[1024], /* PPD env variable */
512 /* PRINTER_INFO env variable */
513 printer_location
[255],
514 /* PRINTER_LOCATION env variable */
516 /* PRINTER env variable */
517 *printer_state_reasons
= NULL
,
518 /* PRINTER_STATE_REASONS env var */
520 /* RIP_MAX_CACHE env variable */
523 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
524 "cupsdContinueJob(job=%p(%d)): current_file=%d, num_files=%d",
525 job
, job
->id
, job
->current_file
, job
->num_files
);
528 * Figure out what filters are required to convert from
529 * the source to the destination type...
532 FilterLevel
-= job
->cost
;
535 job
->pending_cost
= 0;
537 memset(job
->filters
, 0, sizeof(job
->filters
));
540 if (job
->printer
->raw
)
543 * Remote jobs and raw queues go directly to the printer without
547 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Sending job to queue tagged as raw...");
552 * Local jobs get filtered...
555 filters
= mimeFilter(MimeDatabase
, job
->filetypes
[job
->current_file
],
556 job
->printer
->filetype
, &(job
->cost
));
560 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
561 "Unable to convert file %d to printable format!",
564 abort_message
= "Aborting job because it cannot be printed.";
565 abort_state
= IPP_JOB_ABORTED
;
571 * Remove NULL ("-") filters...
574 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
576 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
577 if (!strcmp(filter
->filter
, "-"))
578 cupsArrayRemove(filters
, filter
);
580 if (cupsArrayCount(filters
) == 0)
582 cupsArrayDelete(filters
);
587 * If this printer has any pre-filters, insert the required pre-filter
588 * in the filters array...
591 if (job
->printer
->prefiltertype
&& filters
)
593 prefilters
= cupsArrayNew(NULL
, NULL
);
595 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
597 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
599 if ((prefilter
= mimeFilterLookup(MimeDatabase
, filter
->src
,
600 job
->printer
->prefiltertype
)))
602 cupsArrayAdd(prefilters
, prefilter
);
603 job
->cost
+= prefilter
->cost
;
606 cupsArrayAdd(prefilters
, filter
);
609 cupsArrayDelete(filters
);
610 filters
= prefilters
;
615 * Set a minimum cost of 100 for all jobs so that FilterLimit
616 * works with raw queues and other low-cost paths.
623 * See if the filter cost is too high...
626 if ((FilterLevel
+ job
->cost
) > FilterLimit
&& FilterLevel
> 0 &&
630 * Don't print this job quite yet...
633 cupsArrayDelete(filters
);
635 cupsdLogJob(job
, CUPSD_LOG_INFO
,
636 "Holding because filter limit has been reached.");
637 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
638 "cupsdContinueJob: file=%d, cost=%d, level=%d, limit=%d",
639 job
->current_file
, job
->cost
, FilterLevel
,
642 job
->pending_cost
= job
->cost
;
647 FilterLevel
+= job
->cost
;
650 * Add decompression/raw filter as needed...
653 if ((!job
->printer
->raw
&& job
->compressions
[job
->current_file
]) ||
654 (!filters
&& !job
->printer
->remote
&&
655 (job
->num_files
> 1 || !strncmp(job
->printer
->device_uri
, "file:", 5))))
658 * Add gziptoany filter to the front of the list...
662 filters
= cupsArrayNew(NULL
, NULL
);
664 if (!cupsArrayInsert(filters
, &gziptoany_filter
))
666 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
667 "Unable to add decompression filter - %s", strerror(errno
));
669 cupsArrayDelete(filters
);
671 abort_message
= "Stopping job because the scheduler ran out of memory.";
678 * Add port monitor, if any...
681 if (job
->printer
->port_monitor
)
684 * Add port monitor to the end of the list...
688 filters
= cupsArrayNew(NULL
, NULL
);
690 port_monitor
.src
= NULL
;
691 port_monitor
.dst
= NULL
;
692 port_monitor
.cost
= 0;
694 snprintf(port_monitor
.filter
, sizeof(port_monitor
.filter
),
695 "%s/monitor/%s", ServerBin
, job
->printer
->port_monitor
);
697 if (!cupsArrayAdd(filters
, &port_monitor
))
699 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
700 "Unable to add port monitor - %s", strerror(errno
));
702 abort_message
= "Stopping job because the scheduler ran out of memory.";
709 * Make sure we don't go over the "MAX_FILTERS" limit...
712 if (cupsArrayCount(filters
) > MAX_FILTERS
)
714 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
715 "Too many filters (%d > %d), unable to print!",
716 cupsArrayCount(filters
), MAX_FILTERS
);
718 abort_message
= "Aborting job because it needs too many filters to print.";
719 abort_state
= IPP_JOB_ABORTED
;
725 * Determine if we are printing a banner page or not...
728 if (job
->job_sheets
== NULL
)
730 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "No job-sheets attribute.");
731 if ((job
->job_sheets
=
732 ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_ZERO
)) != NULL
)
733 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
734 "... but someone added one without setting job_sheets!");
736 else if (job
->job_sheets
->num_values
== 1)
737 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "job-sheets=%s",
738 job
->job_sheets
->values
[0].string
.text
);
740 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "job-sheets=%s,%s",
741 job
->job_sheets
->values
[0].string
.text
,
742 job
->job_sheets
->values
[1].string
.text
);
744 if (job
->printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
))
746 else if (job
->job_sheets
== NULL
)
748 else if (_cups_strcasecmp(job
->job_sheets
->values
[0].string
.text
, "none") != 0 &&
749 job
->current_file
== 0)
751 else if (job
->job_sheets
->num_values
> 1 &&
752 _cups_strcasecmp(job
->job_sheets
->values
[1].string
.text
, "none") != 0 &&
753 job
->current_file
== (job
->num_files
- 1))
758 if ((options
= get_options(job
, banner_page
, copies
, sizeof(copies
), title
,
759 sizeof(title
))) == NULL
)
761 abort_message
= "Stopping job because the scheduler ran out of memory.";
767 * Build the command-line arguments for the filters. Each filter
768 * has 6 or 7 arguments:
776 * argv[6] = filename (optional; normally stdin)
778 * This allows legacy printer drivers that use the old System V
779 * printing interface to be used by CUPS.
781 * For remote jobs, we send all of the files in the argument list.
784 if (job
->printer
->remote
)
785 argv
= calloc(7 + job
->num_files
, sizeof(char *));
787 argv
= calloc(8, sizeof(char *));
791 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Unable to allocate argument array - %s",
794 abort_message
= "Stopping job because the scheduler ran out of memory.";
799 sprintf(jobid
, "%d", job
->id
);
801 argv
[0] = job
->printer
->name
;
803 argv
[2] = job
->username
;
808 if (job
->printer
->remote
&& job
->num_files
> 1)
810 for (i
= 0; i
< job
->num_files
; i
++)
812 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
814 argv
[6 + i
] = strdup(filename
);
819 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
820 job
->id
, job
->current_file
+ 1);
824 for (i
= 0; argv
[i
]; i
++)
825 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "argv[%d]=\"%s\"", i
, argv
[i
]);
828 * Create environment variable strings for the filters...
831 attr
= ippFindAttribute(job
->attrs
, "attributes-natural-language",
835 strcpy(apple_language
, "APPLE_LANGUAGE=");
836 _cupsAppleLanguage(attr
->values
[0].string
.text
,
837 apple_language
+ 15, sizeof(apple_language
) - 15);
838 #endif /* __APPLE__ */
840 switch (strlen(attr
->values
[0].string
.text
))
844 * This is an unknown or badly formatted language code; use
845 * the POSIX locale...
848 strcpy(lang
, "LANG=C");
853 * Just the language code (ll)...
856 snprintf(lang
, sizeof(lang
), "LANG=%s.UTF-8",
857 attr
->values
[0].string
.text
);
862 * Language and country code (ll-cc)...
865 snprintf(lang
, sizeof(lang
), "LANG=%c%c_%c%c.UTF-8",
866 attr
->values
[0].string
.text
[0],
867 attr
->values
[0].string
.text
[1],
868 toupper(attr
->values
[0].string
.text
[3] & 255),
869 toupper(attr
->values
[0].string
.text
[4] & 255));
873 if ((attr
= ippFindAttribute(job
->attrs
, "document-format",
874 IPP_TAG_MIMETYPE
)) != NULL
&&
875 (ptr
= strstr(attr
->values
[0].string
.text
, "charset=")) != NULL
)
876 snprintf(charset
, sizeof(charset
), "CHARSET=%s", ptr
+ 8);
878 strlcpy(charset
, "CHARSET=utf-8", sizeof(charset
));
880 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s/%s",
881 job
->filetypes
[job
->current_file
]->super
,
882 job
->filetypes
[job
->current_file
]->type
);
883 snprintf(device_uri
, sizeof(device_uri
), "DEVICE_URI=%s",
884 job
->printer
->device_uri
);
885 snprintf(ppd
, sizeof(ppd
), "PPD=%s/ppd/%s.ppd", ServerRoot
,
887 snprintf(printer_info
, sizeof(printer_name
), "PRINTER_INFO=%s",
888 job
->printer
->info
? job
->printer
->info
: "");
889 snprintf(printer_location
, sizeof(printer_name
), "PRINTER_LOCATION=%s",
890 job
->printer
->location
? job
->printer
->location
: "");
891 snprintf(printer_name
, sizeof(printer_name
), "PRINTER=%s", job
->printer
->name
);
892 if (job
->printer
->num_reasons
> 0)
894 char *psrptr
; /* Pointer into PRINTER_STATE_REASONS */
895 size_t psrlen
; /* Size of PRINTER_STATE_REASONS */
897 for (psrlen
= 22, i
= 0; i
< job
->printer
->num_reasons
; i
++)
898 psrlen
+= strlen(job
->printer
->reasons
[i
]) + 1;
900 if ((printer_state_reasons
= malloc(psrlen
)) != NULL
)
903 * All of these strcpy's are safe because we allocated the psr string...
906 strcpy(printer_state_reasons
, "PRINTER_STATE_REASONS=");
907 for (psrptr
= printer_state_reasons
+ 22, i
= 0;
908 i
< job
->printer
->num_reasons
;
913 strcpy(psrptr
, job
->printer
->reasons
[i
]);
914 psrptr
+= strlen(psrptr
);
918 snprintf(rip_max_cache
, sizeof(rip_max_cache
), "RIP_MAX_CACHE=%s", RIPCache
);
920 if (job
->printer
->num_auth_info_required
== 1)
921 snprintf(auth_info_required
, sizeof(auth_info_required
),
922 "AUTH_INFO_REQUIRED=%s",
923 job
->printer
->auth_info_required
[0]);
924 else if (job
->printer
->num_auth_info_required
== 2)
925 snprintf(auth_info_required
, sizeof(auth_info_required
),
926 "AUTH_INFO_REQUIRED=%s,%s",
927 job
->printer
->auth_info_required
[0],
928 job
->printer
->auth_info_required
[1]);
929 else if (job
->printer
->num_auth_info_required
== 3)
930 snprintf(auth_info_required
, sizeof(auth_info_required
),
931 "AUTH_INFO_REQUIRED=%s,%s,%s",
932 job
->printer
->auth_info_required
[0],
933 job
->printer
->auth_info_required
[1],
934 job
->printer
->auth_info_required
[2]);
935 else if (job
->printer
->num_auth_info_required
== 4)
936 snprintf(auth_info_required
, sizeof(auth_info_required
),
937 "AUTH_INFO_REQUIRED=%s,%s,%s,%s",
938 job
->printer
->auth_info_required
[0],
939 job
->printer
->auth_info_required
[1],
940 job
->printer
->auth_info_required
[2],
941 job
->printer
->auth_info_required
[3]);
943 strlcpy(auth_info_required
, "AUTH_INFO_REQUIRED=none",
944 sizeof(auth_info_required
));
946 envc
= cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
948 envp
[envc
++] = charset
;
949 envp
[envc
++] = lang
;
951 envp
[envc
++] = apple_language
;
952 #endif /* __APPLE__ */
954 envp
[envc
++] = rip_max_cache
;
955 envp
[envc
++] = content_type
;
956 envp
[envc
++] = device_uri
;
957 envp
[envc
++] = printer_info
;
958 envp
[envc
++] = printer_location
;
959 envp
[envc
++] = printer_name
;
960 envp
[envc
++] = printer_state_reasons
? printer_state_reasons
:
961 "PRINTER_STATE_REASONS=none";
962 envp
[envc
++] = banner_page
? "CUPS_FILETYPE=job-sheet" :
963 "CUPS_FILETYPE=document";
965 if (!job
->printer
->remote
&& !job
->printer
->raw
)
967 filter
= (mime_filter_t
*)cupsArrayLast(filters
);
969 if (job
->printer
->port_monitor
)
970 filter
= (mime_filter_t
*)cupsArrayPrev(filters
);
972 if (filter
&& filter
->dst
)
974 if ((ptr
= strchr(filter
->dst
->type
, '/')) != NULL
)
975 snprintf(final_content_type
, sizeof(final_content_type
),
976 "FINAL_CONTENT_TYPE=%s", ptr
+ 1);
978 snprintf(final_content_type
, sizeof(final_content_type
),
979 "FINAL_CONTENT_TYPE=%s/%s", filter
->dst
->super
,
981 envp
[envc
++] = final_content_type
;
985 if (Classification
&& !banner_page
)
987 if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
988 IPP_TAG_NAME
)) == NULL
)
989 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
991 else if (attr
->num_values
> 1 &&
992 strcmp(attr
->values
[1].string
.text
, "none") != 0)
993 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
994 attr
->values
[1].string
.text
);
996 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
997 attr
->values
[0].string
.text
);
999 envp
[envc
++] = classification
;
1002 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
1004 snprintf(class_name
, sizeof(class_name
), "CLASS=%s", job
->dest
);
1005 envp
[envc
++] = class_name
;
1008 envp
[envc
++] = auth_info_required
;
1011 i
< (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0]));
1013 if (job
->auth_env
[i
])
1014 envp
[envc
++] = job
->auth_env
[i
];
1019 envp
[envc
++] = job
->auth_uid
;
1023 for (i
= 0; i
< envc
; i
++)
1024 if (!strncmp(envp
[i
], "AUTH_", 5))
1025 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"AUTH_%c****\"", i
,
1027 else if (strncmp(envp
[i
], "DEVICE_URI=", 11))
1028 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"%s\"", i
, envp
[i
]);
1030 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"DEVICE_URI=%s\"", i
,
1031 job
->printer
->sanitized_device_uri
);
1033 if (job
->printer
->remote
)
1034 job
->current_file
= job
->num_files
;
1036 job
->current_file
++;
1039 * Now create processes for all of the filters...
1042 cupsdSetPrinterReasons(job
->printer
, "-cups-missing-filter-warning,"
1043 "cups-insecure-filter-warning");
1045 for (i
= 0, slot
= 0, filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
1047 i
++, filter
= (mime_filter_t
*)cupsArrayNext(filters
))
1049 if (filter
->filter
[0] != '/')
1050 snprintf(command
, sizeof(command
), "%s/filter/%s", ServerBin
,
1053 strlcpy(command
, filter
->filter
, sizeof(command
));
1055 if (i
< (cupsArrayCount(filters
) - 1))
1057 if (cupsdOpenPipe(filterfds
[slot
]))
1059 abort_message
= "Stopping job because the scheduler could not create "
1060 "the filter pipes.";
1067 if (job
->current_file
== 1 ||
1068 (job
->printer
->pc
&& job
->printer
->pc
->single_file
))
1070 if (strncmp(job
->printer
->device_uri
, "file:", 5) != 0)
1072 if (cupsdOpenPipe(job
->print_pipes
))
1074 abort_message
= "Stopping job because the scheduler could not "
1075 "create the backend pipes.";
1082 job
->print_pipes
[0] = -1;
1083 if (!strcmp(job
->printer
->device_uri
, "file:/dev/null") ||
1084 !strcmp(job
->printer
->device_uri
, "file:///dev/null"))
1085 job
->print_pipes
[1] = -1;
1088 if (!strncmp(job
->printer
->device_uri
, "file:/dev/", 10))
1089 job
->print_pipes
[1] = open(job
->printer
->device_uri
+ 5,
1091 else if (!strncmp(job
->printer
->device_uri
, "file:///dev/", 12))
1092 job
->print_pipes
[1] = open(job
->printer
->device_uri
+ 7,
1094 else if (!strncmp(job
->printer
->device_uri
, "file:///", 8))
1095 job
->print_pipes
[1] = open(job
->printer
->device_uri
+ 7,
1096 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
1098 job
->print_pipes
[1] = open(job
->printer
->device_uri
+ 5,
1099 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
1101 if (job
->print_pipes
[1] < 0)
1103 abort_message
= "Stopping job because the scheduler could not "
1104 "open the output file.";
1109 fcntl(job
->print_pipes
[1], F_SETFD
,
1110 fcntl(job
->print_pipes
[1], F_GETFD
) | FD_CLOEXEC
);
1115 filterfds
[slot
][0] = job
->print_pipes
[0];
1116 filterfds
[slot
][1] = job
->print_pipes
[1];
1119 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
1120 filterfds
[slot
][1], job
->status_pipes
[1],
1121 job
->back_pipes
[0], job
->side_pipes
[0], 0,
1122 job
->profile
, job
, job
->filters
+ i
);
1124 cupsdClosePipe(filterfds
[!slot
]);
1128 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Unable to start filter \"%s\" - %s.",
1129 filter
->filter
, strerror(errno
));
1131 abort_message
= "Stopping job because the scheduler could not execute a "
1137 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Started filter %s (PID %d)", command
,
1144 cupsArrayDelete(filters
);
1148 * Finally, pipe the final output into a backend process if needed...
1151 if (strncmp(job
->printer
->device_uri
, "file:", 5) != 0)
1153 if (job
->current_file
== 1 || job
->printer
->remote
||
1154 (job
->printer
->pc
&& job
->printer
->pc
->single_file
))
1156 sscanf(job
->printer
->device_uri
, "%254[^:]", scheme
);
1157 snprintf(command
, sizeof(command
), "%s/backend/%s", ServerBin
, scheme
);
1160 * See if the backend needs to run as root...
1165 else if (stat(command
, &backinfo
))
1168 backroot
= !(backinfo
.st_mode
& (S_IRWXG
| S_IRWXO
));
1170 argv
[0] = job
->printer
->sanitized_device_uri
;
1172 filterfds
[slot
][0] = -1;
1173 filterfds
[slot
][1] = -1;
1175 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
1176 filterfds
[slot
][1], job
->status_pipes
[1],
1177 job
->back_pipes
[1], job
->side_pipes
[1],
1178 backroot
, job
->profile
, job
, &(job
->backend
));
1182 abort_message
= "Stopping job because the sheduler could not execute "
1189 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Started backend %s (PID %d)",
1194 if (job
->current_file
== job
->num_files
||
1195 (job
->printer
->pc
&& job
->printer
->pc
->single_file
))
1196 cupsdClosePipe(job
->print_pipes
);
1198 if (job
->current_file
== job
->num_files
)
1200 cupsdClosePipe(job
->back_pipes
);
1201 cupsdClosePipe(job
->side_pipes
);
1203 close(job
->status_pipes
[1]);
1204 job
->status_pipes
[1] = -1;
1209 filterfds
[slot
][0] = -1;
1210 filterfds
[slot
][1] = -1;
1212 if (job
->current_file
== job
->num_files
||
1213 (job
->printer
->pc
&& job
->printer
->pc
->single_file
))
1214 cupsdClosePipe(job
->print_pipes
);
1216 if (job
->current_file
== job
->num_files
)
1218 close(job
->status_pipes
[1]);
1219 job
->status_pipes
[1] = -1;
1223 cupsdClosePipe(filterfds
[slot
]);
1225 if (job
->printer
->remote
&& job
->num_files
> 1)
1227 for (i
= 0; i
< job
->num_files
; i
++)
1232 if (printer_state_reasons
)
1233 free(printer_state_reasons
);
1235 cupsdAddSelect(job
->status_buffer
->fd
, (cupsd_selfunc_t
)update_job
, NULL
,
1238 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
, "Job #%d started.",
1245 * If we get here, we need to abort the current job and close out all
1246 * files and pipes...
1251 FilterLevel
-= job
->cost
;
1254 for (slot
= 0; slot
< 2; slot
++)
1255 cupsdClosePipe(filterfds
[slot
]);
1257 cupsArrayDelete(filters
);
1261 if (job
->printer
->remote
&& job
->num_files
> 1)
1263 for (i
= 0; i
< job
->num_files
; i
++)
1270 if (printer_state_reasons
)
1271 free(printer_state_reasons
);
1273 cupsdClosePipe(job
->print_pipes
);
1274 cupsdClosePipe(job
->back_pipes
);
1275 cupsdClosePipe(job
->side_pipes
);
1277 cupsdRemoveSelect(job
->status_pipes
[0]);
1278 cupsdClosePipe(job
->status_pipes
);
1279 cupsdStatBufDelete(job
->status_buffer
);
1280 job
->status_buffer
= NULL
;
1283 * Update the printer and job state.
1286 cupsdSetJobState(job
, abort_state
, CUPSD_JOB_DEFAULT
, "%s", abort_message
);
1287 cupsdSetPrinterState(job
->printer
, IPP_PRINTER_IDLE
, 0);
1288 update_job_attrs(job
, 0);
1291 free_job_history(job
);
1293 cupsArrayRemove(PrintingJobs
, job
);
1296 * Clear the printer <-> job association...
1299 job
->printer
->job
= NULL
;
1300 job
->printer
= NULL
;
1305 * 'cupsdDeleteJob()' - Free all memory used by a job.
1309 cupsdDeleteJob(cupsd_job_t
*job
, /* I - Job */
1310 cupsd_jobaction_t action
)/* I - Action */
1312 int i
; /* Looping var */
1313 char filename
[1024]; /* Job filename */
1317 finalize_job(job
, 1);
1319 if (action
== CUPSD_JOB_PURGE
)
1322 * Remove the job info file...
1325 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
,
1328 cupsdRemoveFile(filename
);
1333 cupsdClearString(&job
->username
);
1334 cupsdClearString(&job
->dest
);
1336 i
< (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0]));
1338 cupsdClearString(job
->auth_env
+ i
);
1339 cupsdClearString(&job
->auth_uid
);
1341 if (job
->num_files
> 0)
1343 free(job
->compressions
);
1344 free(job
->filetypes
);
1346 while (job
->num_files
> 0)
1348 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
1349 job
->id
, job
->num_files
);
1351 cupsdRemoveFile(filename
);
1360 free_job_history(job
);
1364 cupsArrayRemove(Jobs
, job
);
1365 cupsArrayRemove(ActiveJobs
, job
);
1366 cupsArrayRemove(PrintingJobs
, job
);
1373 * 'cupsdFreeAllJobs()' - Free all jobs from memory.
1377 cupsdFreeAllJobs(void)
1379 cupsd_job_t
*job
; /* Current job */
1387 cupsdStopAllJobs(CUPSD_JOB_FORCE
, 0);
1390 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1392 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1393 cupsdDeleteJob(job
, CUPSD_JOB_DEFAULT
);
1395 cupsdReleaseSignals();
1400 * 'cupsdFindJob()' - Find the specified job.
1403 cupsd_job_t
* /* O - Job data */
1404 cupsdFindJob(int id
) /* I - Job ID */
1406 cupsd_job_t key
; /* Search key */
1411 return ((cupsd_job_t
*)cupsArrayFind(Jobs
, &key
));
1416 * 'cupsdGetPrinterJobCount()' - Get the number of pending, processing,
1417 * or held jobs in a printer or class.
1420 int /* O - Job count */
1421 cupsdGetPrinterJobCount(
1422 const char *dest
) /* I - Printer or class name */
1424 int count
; /* Job count */
1425 cupsd_job_t
*job
; /* Current job */
1428 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
1430 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
1431 if (job
->dest
&& !_cups_strcasecmp(job
->dest
, dest
))
1439 * 'cupsdGetUserJobCount()' - Get the number of pending, processing,
1440 * or held jobs for a user.
1443 int /* O - Job count */
1444 cupsdGetUserJobCount(
1445 const char *username
) /* I - Username */
1447 int count
; /* Job count */
1448 cupsd_job_t
*job
; /* Current job */
1451 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
1453 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
1454 if (!_cups_strcasecmp(job
->username
, username
))
1462 * 'cupsdLoadAllJobs()' - Load all jobs from disk.
1466 cupsdLoadAllJobs(void)
1468 char filename
[1024]; /* Full filename of job.cache file */
1469 struct stat fileinfo
, /* Information on job.cache file */
1470 dirinfo
; /* Information on RequestRoot dir */
1475 * Create the job arrays as needed...
1479 Jobs
= cupsArrayNew(compare_jobs
, NULL
);
1482 ActiveJobs
= cupsArrayNew(compare_active_jobs
, NULL
);
1485 PrintingJobs
= cupsArrayNew(compare_jobs
, NULL
);
1488 * See whether the job.cache file is older than the RequestRoot directory...
1491 snprintf(filename
, sizeof(filename
), "%s/job.cache", CacheDir
);
1493 if (stat(filename
, &fileinfo
))
1495 fileinfo
.st_mtime
= 0;
1497 if (errno
!= ENOENT
)
1498 cupsdLogMessage(CUPSD_LOG_ERROR
,
1499 "Unable to get file information for \"%s\" - %s",
1500 filename
, strerror(errno
));
1503 if (stat(RequestRoot
, &dirinfo
))
1505 dirinfo
.st_mtime
= 0;
1507 if (errno
!= ENOENT
)
1508 cupsdLogMessage(CUPSD_LOG_ERROR
,
1509 "Unable to get directory information for \"%s\" - %s",
1510 RequestRoot
, strerror(errno
));
1514 * Load the most recent source for job data...
1517 if (dirinfo
.st_mtime
> fileinfo
.st_mtime
)
1519 load_request_root();
1521 load_next_job_id(filename
);
1524 load_job_cache(filename
);
1527 * Clean out old jobs as needed...
1530 if (MaxJobs
> 0 && cupsArrayCount(Jobs
) >= MaxJobs
)
1536 * 'cupsdLoadJob()' - Load a single job.
1539 int /* O - 1 on success, 0 on failure */
1540 cupsdLoadJob(cupsd_job_t
*job
) /* I - Job */
1542 int i
; /* Looping var */
1543 char jobfile
[1024]; /* Job filename */
1544 cups_file_t
*fp
; /* Job file */
1545 int fileid
; /* Current file ID */
1546 ipp_attribute_t
*attr
; /* Job attribute */
1547 const char *dest
; /* Destination name */
1548 cupsd_printer_t
*destptr
; /* Pointer to destination */
1549 mime_type_t
**filetypes
; /* New filetypes array */
1550 int *compressions
; /* New compressions array */
1555 if (job
->state_value
> IPP_JOB_STOPPED
)
1556 job
->access_time
= time(NULL
);
1561 if ((job
->attrs
= ippNew()) == NULL
)
1563 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Ran out of memory for job attributes!");
1568 * Load job attributes...
1571 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Loading attributes...", job
->id
);
1573 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, job
->id
);
1574 if ((fp
= cupsFileOpen(jobfile
, "r")) == NULL
)
1576 char newfile
[1024]; /* New job filename */
1578 snprintf(newfile
, sizeof(newfile
), "%s/c%05d.N", RequestRoot
, job
->id
);
1579 if ((fp
= cupsFileOpen(newfile
, "r")) == NULL
)
1581 cupsdLogMessage(CUPSD_LOG_ERROR
,
1582 "[Job %d] Unable to open job control file \"%s\": %s",
1583 job
->id
, jobfile
, strerror(errno
));
1588 rename(newfile
, jobfile
);
1591 if (ippReadIO(fp
, (ipp_iocb_t
)cupsFileRead
, 1, NULL
, job
->attrs
) != IPP_DATA
)
1593 cupsdLogMessage(CUPSD_LOG_ERROR
,
1594 "[Job %d] Unable to read job control file \"%s\".", job
->id
,
1603 * Copy attribute data to the job object...
1606 if (!ippFindAttribute(job
->attrs
, "time-at-creation", IPP_TAG_INTEGER
))
1608 cupsdLogMessage(CUPSD_LOG_ERROR
,
1609 "[Job %d] Missing or bad time-at-creation attribute in "
1610 "control file!", job
->id
);
1614 if ((job
->state
= ippFindAttribute(job
->attrs
, "job-state",
1615 IPP_TAG_ENUM
)) == NULL
)
1617 cupsdLogMessage(CUPSD_LOG_ERROR
,
1618 "[Job %d] Missing or bad job-state attribute in control "
1623 job
->state_value
= (ipp_jstate_t
)job
->state
->values
[0].integer
;
1627 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1628 IPP_TAG_URI
)) == NULL
)
1630 cupsdLogMessage(CUPSD_LOG_ERROR
,
1631 "[Job %d] No job-printer-uri attribute in control file!",
1636 if ((dest
= cupsdValidateDest(attr
->values
[0].string
.text
, &(job
->dtype
),
1639 cupsdLogMessage(CUPSD_LOG_ERROR
,
1640 "[Job %d] Unable to queue job for destination \"%s\"!",
1641 job
->id
, attr
->values
[0].string
.text
);
1645 cupsdSetString(&job
->dest
, dest
);
1647 else if ((destptr
= cupsdFindDest(job
->dest
)) == NULL
)
1649 cupsdLogMessage(CUPSD_LOG_ERROR
,
1650 "[Job %d] Unable to queue job for destination \"%s\"!",
1651 job
->id
, job
->dest
);
1655 job
->sheets
= ippFindAttribute(job
->attrs
, "job-media-sheets-completed",
1657 job
->job_sheets
= ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_NAME
);
1661 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
1662 IPP_TAG_INTEGER
)) == NULL
)
1664 cupsdLogMessage(CUPSD_LOG_ERROR
,
1665 "[Job %d] Missing or bad job-priority attribute in "
1666 "control file!", job
->id
);
1670 job
->priority
= attr
->values
[0].integer
;
1675 if ((attr
= ippFindAttribute(job
->attrs
, "job-originating-user-name",
1676 IPP_TAG_NAME
)) == NULL
)
1678 cupsdLogMessage(CUPSD_LOG_ERROR
,
1679 "[Job %d] Missing or bad job-originating-user-name "
1680 "attribute in control file!", job
->id
);
1684 cupsdSetString(&job
->username
, attr
->values
[0].string
.text
);
1688 * Set the job hold-until time and state...
1691 if (job
->state_value
== IPP_JOB_HELD
)
1693 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1694 IPP_TAG_KEYWORD
)) == NULL
)
1695 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1698 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
, CUPSD_JOB_DEFAULT
);
1701 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1702 job
->state_value
= IPP_JOB_PENDING
;
1705 else if (job
->state_value
== IPP_JOB_PROCESSING
)
1707 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1708 job
->state_value
= IPP_JOB_PENDING
;
1711 if (!job
->num_files
)
1714 * Find all the d##### files...
1717 for (fileid
= 1; fileid
< 10000; fileid
++)
1719 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
1722 if (access(jobfile
, 0))
1725 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1726 "[Job %d] Auto-typing document file \"%s\"...", job
->id
,
1729 if (fileid
> job
->num_files
)
1731 if (job
->num_files
== 0)
1733 compressions
= (int *)calloc(fileid
, sizeof(int));
1734 filetypes
= (mime_type_t
**)calloc(fileid
, sizeof(mime_type_t
*));
1738 compressions
= (int *)realloc(job
->compressions
,
1739 sizeof(int) * fileid
);
1740 filetypes
= (mime_type_t
**)realloc(job
->filetypes
,
1741 sizeof(mime_type_t
*) *
1745 if (!compressions
|| !filetypes
)
1747 cupsdLogMessage(CUPSD_LOG_ERROR
,
1748 "[Job %d] Ran out of memory for job file types!",
1751 ippDelete(job
->attrs
);
1760 if (job
->compressions
)
1762 free(job
->compressions
);
1763 job
->compressions
= NULL
;
1768 free(job
->filetypes
);
1769 job
->filetypes
= NULL
;
1776 job
->compressions
= compressions
;
1777 job
->filetypes
= filetypes
;
1778 job
->num_files
= fileid
;
1781 job
->filetypes
[fileid
- 1] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
1782 job
->compressions
+ fileid
- 1);
1784 if (!job
->filetypes
[fileid
- 1])
1785 job
->filetypes
[fileid
- 1] = mimeType(MimeDatabase
, "application",
1791 * Load authentication information as needed...
1794 if (job
->state_value
< IPP_JOB_STOPPED
)
1796 snprintf(jobfile
, sizeof(jobfile
), "%s/a%05d", RequestRoot
, job
->id
);
1799 i
< (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0]));
1801 cupsdClearString(job
->auth_env
+ i
);
1802 cupsdClearString(&job
->auth_uid
);
1804 if ((fp
= cupsFileOpen(jobfile
, "r")) != NULL
)
1806 int bytes
; /* Size of auth data */
1807 char line
[255], /* Line from file */
1808 data
[255]; /* Decoded data */
1812 i
< destptr
->num_auth_info_required
&&
1813 i
< (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0])) &&
1814 cupsFileGets(fp
, line
, sizeof(line
));
1817 bytes
= sizeof(data
);
1818 httpDecode64_2(data
, &bytes
, line
);
1820 if (!strcmp(destptr
->auth_info_required
[i
], "username"))
1821 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_USERNAME=%s", data
);
1822 else if (!strcmp(destptr
->auth_info_required
[i
], "domain"))
1823 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_DOMAIN=%s", data
);
1824 else if (!strcmp(destptr
->auth_info_required
[i
], "password"))
1825 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_PASSWORD=%s", data
);
1826 else if (!strcmp(destptr
->auth_info_required
[i
], "negotiate"))
1827 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_NEGOTIATE=%s", line
);
1830 if (cupsFileGets(fp
, line
, sizeof(line
)) && isdigit(line
[0] & 255))
1831 cupsdSetStringf(&job
->auth_uid
, "AUTH_UID=%s", line
);
1837 job
->access_time
= time(NULL
);
1841 * If we get here then something bad happened...
1846 ippDelete(job
->attrs
);
1849 if (job
->compressions
)
1851 free(job
->compressions
);
1852 job
->compressions
= NULL
;
1857 free(job
->filetypes
);
1858 job
->filetypes
= NULL
;
1864 cupsdRemoveFile(jobfile
);
1873 * 'cupsdMoveJob()' - Move the specified job to a different destination.
1877 cupsdMoveJob(cupsd_job_t
*job
, /* I - Job */
1878 cupsd_printer_t
*p
) /* I - Destination printer or class */
1880 ipp_attribute_t
*attr
; /* job-printer-uri attribute */
1881 const char *olddest
; /* Old destination */
1882 cupsd_printer_t
*oldp
; /* Old pointer */
1886 * Don't move completed jobs...
1889 if (job
->state_value
> IPP_JOB_STOPPED
)
1893 * Get the old destination...
1896 olddest
= job
->dest
;
1899 oldp
= job
->printer
;
1901 oldp
= cupsdFindDest(olddest
);
1904 * Change the destination information...
1907 if (job
->state_value
> IPP_JOB_HELD
)
1908 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
1909 "Stopping job prior to move.");
1911 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, oldp
, job
,
1912 "Job #%d moved from %s to %s.", job
->id
, olddest
,
1915 cupsdSetString(&job
->dest
, p
->name
);
1916 job
->dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_REMOTE
|
1917 CUPS_PRINTER_IMPLICIT
);
1919 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1920 IPP_TAG_URI
)) != NULL
)
1921 cupsdSetString(&(attr
->values
[0].string
.text
), p
->uri
);
1923 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, p
, job
,
1924 "Job #%d moved from %s to %s.", job
->id
, olddest
,
1928 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1933 * 'cupsdReleaseJob()' - Release the specified job.
1937 cupsdReleaseJob(cupsd_job_t
*job
) /* I - Job */
1939 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdReleaseJob(job=%p(%d))", job
,
1942 if (job
->state_value
== IPP_JOB_HELD
)
1945 * Add trailing banner as needed...
1948 if (job
->pending_timeout
)
1949 cupsdTimeoutJob(job
);
1951 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
1952 "Job released by user.");
1958 * 'cupsdRestartJob()' - Restart the specified job.
1962 cupsdRestartJob(cupsd_job_t
*job
) /* I - Job */
1964 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdRestartJob(job=%p(%d))", job
,
1967 if (job
->state_value
== IPP_JOB_STOPPED
|| job
->num_files
)
1968 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
1969 "Job restarted by user.");
1974 * 'cupsdSaveAllJobs()' - Save a summary of all jobs to disk.
1978 cupsdSaveAllJobs(void)
1980 int i
; /* Looping var */
1981 cups_file_t
*fp
; /* job.cache file */
1982 char filename
[1024], /* job.cache filename */
1983 temp
[1024]; /* Temporary string */
1984 cupsd_job_t
*job
; /* Current job */
1985 time_t curtime
; /* Current time */
1986 struct tm
*curdate
; /* Current date */
1989 snprintf(filename
, sizeof(filename
), "%s/job.cache", CacheDir
);
1990 if ((fp
= cupsdCreateConfFile(filename
, ConfigFilePerm
)) == NULL
)
1993 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving job.cache...");
1996 * Write a small header to the file...
1999 curtime
= time(NULL
);
2000 curdate
= localtime(&curtime
);
2001 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
2003 cupsFilePuts(fp
, "# Job cache file for " CUPS_SVERSION
"\n");
2004 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
2005 cupsFilePrintf(fp
, "NextJobId %d\n", NextJobId
);
2008 * Write each job known to the system...
2011 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
2013 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
2015 cupsFilePrintf(fp
, "<Job %d>\n", job
->id
);
2016 cupsFilePrintf(fp
, "State %d\n", job
->state_value
);
2017 cupsFilePrintf(fp
, "Priority %d\n", job
->priority
);
2018 cupsFilePrintf(fp
, "HoldUntil %d\n", (int)job
->hold_until
);
2019 cupsFilePrintf(fp
, "Username %s\n", job
->username
);
2020 cupsFilePrintf(fp
, "Destination %s\n", job
->dest
);
2021 cupsFilePrintf(fp
, "DestType %d\n", job
->dtype
);
2022 cupsFilePrintf(fp
, "NumFiles %d\n", job
->num_files
);
2023 for (i
= 0; i
< job
->num_files
; i
++)
2024 cupsFilePrintf(fp
, "File %d %s/%s %d\n", i
+ 1, job
->filetypes
[i
]->super
,
2025 job
->filetypes
[i
]->type
, job
->compressions
[i
]);
2026 cupsFilePuts(fp
, "</Job>\n");
2029 cupsdCloseCreatedConfFile(fp
, filename
);
2034 * 'cupsdSaveJob()' - Save a job to disk.
2038 cupsdSaveJob(cupsd_job_t
*job
) /* I - Job */
2040 char filename
[1024], /* Job control filename */
2041 newfile
[1024]; /* New job control filename */
2042 cups_file_t
*fp
; /* Job file */
2045 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSaveJob(job=%p(%d)): job->attrs=%p",
2046 job
, job
->id
, job
->attrs
);
2048 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
, job
->id
);
2049 snprintf(newfile
, sizeof(newfile
), "%s/c%05d.N", RequestRoot
, job
->id
);
2051 if ((fp
= cupsFileOpen(newfile
, "w")) == NULL
)
2053 cupsdLogMessage(CUPSD_LOG_ERROR
,
2054 "[Job %d] Unable to create job control file \"%s\": %s",
2055 job
->id
, newfile
, strerror(errno
));
2059 fchmod(cupsFileNumber(fp
), 0600);
2060 fchown(cupsFileNumber(fp
), RunUser
, Group
);
2062 job
->attrs
->state
= IPP_IDLE
;
2064 if (ippWriteIO(fp
, (ipp_iocb_t
)cupsFileWrite
, 1, NULL
,
2065 job
->attrs
) != IPP_DATA
)
2067 cupsdLogMessage(CUPSD_LOG_ERROR
,
2068 "[Job %d] Unable to write job control file.", job
->id
);
2074 if (cupsFileClose(fp
))
2075 cupsdLogMessage(CUPSD_LOG_ERROR
,
2076 "[Job %d] Unable to close job control file: %s",
2077 job
->id
, strerror(errno
));
2081 if (rename(newfile
, filename
))
2082 cupsdLogMessage(CUPSD_LOG_ERROR
,
2083 "[Job %d] Unable to finalize job control file: %s",
2084 job
->id
, strerror(errno
));
2092 * 'cupsdSetJobHoldUntil()' - Set the hold time for a job.
2096 cupsdSetJobHoldUntil(cupsd_job_t
*job
, /* I - Job */
2097 const char *when
, /* I - When to resume */
2098 int update
)/* I - Update job-hold-until attr? */
2100 time_t curtime
; /* Current time */
2101 struct tm
*curdate
; /* Current date */
2102 int hour
; /* Hold hour */
2103 int minute
; /* Hold minute */
2104 int second
= 0; /* Hold second */
2107 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2108 "cupsdSetJobHoldUntil(job=%p(%d), when=\"%s\", update=%d)",
2109 job
, job
->id
, when
, update
);
2114 * Update the job-hold-until attribute...
2117 ipp_attribute_t
*attr
; /* job-hold-until attribute */
2119 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2120 IPP_TAG_KEYWORD
)) == NULL
)
2121 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2124 cupsdSetString(&(attr
->values
[0].string
.text
), when
);
2126 attr
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
2127 "job-hold-until", NULL
, when
);
2131 if (isdigit(when
[0] & 255))
2132 attr
->value_tag
= IPP_TAG_NAME
;
2134 attr
->value_tag
= IPP_TAG_KEYWORD
;
2137 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2142 * Update the hold time...
2145 if (!strcmp(when
, "indefinite") || !strcmp(when
, "auth-info-required"))
2148 * Hold indefinitely...
2151 job
->hold_until
= 0;
2153 else if (!strcmp(when
, "day-time"))
2156 * Hold to 6am the next morning unless local time is < 6pm.
2159 curtime
= time(NULL
);
2160 curdate
= localtime(&curtime
);
2162 if (curdate
->tm_hour
< 18)
2163 job
->hold_until
= curtime
;
2165 job
->hold_until
= curtime
+
2166 ((29 - curdate
->tm_hour
) * 60 + 59 -
2167 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2169 else if (!strcmp(when
, "evening") || !strcmp(when
, "night"))
2172 * Hold to 6pm unless local time is > 6pm or < 6am.
2175 curtime
= time(NULL
);
2176 curdate
= localtime(&curtime
);
2178 if (curdate
->tm_hour
< 6 || curdate
->tm_hour
>= 18)
2179 job
->hold_until
= curtime
;
2181 job
->hold_until
= curtime
+
2182 ((17 - curdate
->tm_hour
) * 60 + 59 -
2183 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2185 else if (!strcmp(when
, "second-shift"))
2188 * Hold to 4pm unless local time is > 4pm.
2191 curtime
= time(NULL
);
2192 curdate
= localtime(&curtime
);
2194 if (curdate
->tm_hour
>= 16)
2195 job
->hold_until
= curtime
;
2197 job
->hold_until
= curtime
+
2198 ((15 - curdate
->tm_hour
) * 60 + 59 -
2199 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2201 else if (!strcmp(when
, "third-shift"))
2204 * Hold to 12am unless local time is < 8am.
2207 curtime
= time(NULL
);
2208 curdate
= localtime(&curtime
);
2210 if (curdate
->tm_hour
< 8)
2211 job
->hold_until
= curtime
;
2213 job
->hold_until
= curtime
+
2214 ((23 - curdate
->tm_hour
) * 60 + 59 -
2215 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2217 else if (!strcmp(when
, "weekend"))
2220 * Hold to weekend unless we are in the weekend.
2223 curtime
= time(NULL
);
2224 curdate
= localtime(&curtime
);
2226 if (curdate
->tm_wday
== 0 || curdate
->tm_wday
== 6)
2227 job
->hold_until
= curtime
;
2229 job
->hold_until
= curtime
+
2230 (((5 - curdate
->tm_wday
) * 24 +
2231 (17 - curdate
->tm_hour
)) * 60 + 59 -
2232 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2234 else if (sscanf(when
, "%d:%d:%d", &hour
, &minute
, &second
) >= 2)
2237 * Hold to specified GMT time (HH:MM or HH:MM:SS)...
2240 curtime
= time(NULL
);
2241 curdate
= gmtime(&curtime
);
2243 job
->hold_until
= curtime
+
2244 ((hour
- curdate
->tm_hour
) * 60 + minute
-
2245 curdate
->tm_min
) * 60 + second
- curdate
->tm_sec
;
2248 * Hold until next day as needed...
2251 if (job
->hold_until
< curtime
)
2252 job
->hold_until
+= 24 * 60 * 60;
2255 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetJobHoldUntil: hold_until=%d",
2256 (int)job
->hold_until
);
2261 * 'cupsdSetJobPriority()' - Set the priority of a job, moving it up/down in
2262 * the list as needed.
2266 cupsdSetJobPriority(
2267 cupsd_job_t
*job
, /* I - Job ID */
2268 int priority
) /* I - New priority (0 to 100) */
2270 ipp_attribute_t
*attr
; /* Job attribute */
2274 * Don't change completed jobs...
2277 if (job
->state_value
>= IPP_JOB_PROCESSING
)
2281 * Set the new priority and re-add the job into the active list...
2284 cupsArrayRemove(ActiveJobs
, job
);
2286 job
->priority
= priority
;
2288 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
2289 IPP_TAG_INTEGER
)) != NULL
)
2290 attr
->values
[0].integer
= priority
;
2292 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-priority",
2295 cupsArrayAdd(ActiveJobs
, job
);
2298 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2303 * 'cupsdSetJobState()' - Set the state of the specified print job.
2308 cupsd_job_t
*job
, /* I - Job to cancel */
2309 ipp_jstate_t newstate
, /* I - New job state */
2310 cupsd_jobaction_t action
, /* I - Action to take */
2311 const char *message
, /* I - Message to log */
2312 ...) /* I - Additional arguments as needed */
2314 int i
; /* Looping var */
2315 ipp_jstate_t oldstate
; /* Old state */
2316 char filename
[1024]; /* Job filename */
2317 ipp_attribute_t
*attr
; /* Job attribute */
2320 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2321 "cupsdSetJobState(job=%p(%d), state=%d, newstate=%d, "
2322 "action=%d, message=\"%s\")", job
, job
->id
, job
->state_value
,
2323 newstate
, action
, message
? message
: "(null)");
2327 * Make sure we have the job attributes...
2330 if (!cupsdLoadJob(job
))
2334 * Don't do anything if the state is unchanged and we aren't purging the
2338 oldstate
= job
->state_value
;
2339 if (newstate
== oldstate
&& action
!= CUPSD_JOB_PURGE
)
2343 * Stop any processes that are working on the current job...
2346 if (oldstate
== IPP_JOB_PROCESSING
)
2347 stop_job(job
, action
);
2350 * Set the new job state...
2353 job
->state
->values
[0].integer
= newstate
;
2354 job
->state_value
= newstate
;
2358 case IPP_JOB_PENDING
:
2360 * Update job-hold-until as needed...
2363 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2364 IPP_TAG_KEYWORD
)) == NULL
)
2365 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2369 attr
->value_tag
= IPP_TAG_KEYWORD
;
2370 cupsdSetString(&(attr
->values
[0].string
.text
), "no-hold");
2376 case IPP_JOB_ABORTED
:
2377 case IPP_JOB_CANCELED
:
2378 case IPP_JOB_COMPLETED
:
2379 set_time(job
, "time-at-completed");
2384 * Log message as needed...
2389 char buffer
[2048]; /* Message buffer */
2390 va_list ap
; /* Pointer to additional arguments */
2392 va_start(ap
, message
);
2393 vsnprintf(buffer
, sizeof(buffer
), message
, ap
);
2396 if (newstate
> IPP_JOB_STOPPED
)
2397 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
, "%s", buffer
);
2399 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
, "%s", buffer
);
2401 if (newstate
== IPP_JOB_STOPPED
|| newstate
== IPP_JOB_ABORTED
)
2402 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "%s", buffer
);
2404 cupsdLogJob(job
, CUPSD_LOG_INFO
, "%s", buffer
);
2408 * Handle post-state-change actions...
2413 case IPP_JOB_PROCESSING
:
2415 * Add the job to the "printing" list...
2418 if (!cupsArrayFind(PrintingJobs
, job
))
2419 cupsArrayAdd(PrintingJobs
, job
);
2422 * Set the processing time...
2425 set_time(job
, "time-at-processing");
2427 case IPP_JOB_PENDING
:
2429 case IPP_JOB_STOPPED
:
2431 * Make sure the job is in the active list...
2434 if (!cupsArrayFind(ActiveJobs
, job
))
2435 cupsArrayAdd(ActiveJobs
, job
);
2438 * Save the job state to disk...
2442 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2445 case IPP_JOB_ABORTED
:
2446 case IPP_JOB_CANCELED
:
2447 case IPP_JOB_COMPLETED
:
2448 if (newstate
== IPP_JOB_CANCELED
)
2451 * Remove the job from the active list if there are no processes still
2455 for (i
= 0; job
->filters
[i
] < 0; i
++);
2457 if (!job
->filters
[i
] && job
->backend
<= 0)
2458 cupsArrayRemove(ActiveJobs
, job
);
2463 * Otherwise just remove the job from the active list immediately...
2466 cupsArrayRemove(ActiveJobs
, job
);
2470 * Expire job subscriptions since the job is now "completed"...
2473 cupsdExpireSubscriptions(NULL
, job
);
2477 * If we are going to sleep and the PrintingJobs count is now 0, allow the
2478 * sleep to happen immediately...
2481 if (Sleeping
&& cupsArrayCount(PrintingJobs
) == 0)
2483 #endif /* __APPLE__ */
2486 * Remove any authentication data...
2489 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
2490 if (cupsdRemoveFile(filename
) && errno
!= ENOENT
)
2491 cupsdLogMessage(CUPSD_LOG_ERROR
,
2492 "Unable to remove authentication cache: %s",
2496 i
< (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0]));
2498 cupsdClearString(job
->auth_env
+ i
);
2500 cupsdClearString(&job
->auth_uid
);
2503 * Remove the print file for good if we aren't preserving jobs or
2507 if (!JobHistory
|| !JobFiles
|| action
== CUPSD_JOB_PURGE
)
2509 for (i
= 1; i
<= job
->num_files
; i
++)
2511 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
2514 cupsdRemoveFile(filename
);
2519 if (job
->num_files
> 0)
2521 free(job
->filetypes
);
2522 free(job
->compressions
);
2525 job
->filetypes
= NULL
;
2526 job
->compressions
= NULL
;
2530 if (JobHistory
&& action
!= CUPSD_JOB_PURGE
)
2533 * Save job state info...
2537 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2539 else if (!job
->printer
)
2542 * Delete the job immediately if not actively printing...
2545 cupsdDeleteJob(job
, CUPSD_JOB_PURGE
);
2552 * Finalize the job immediately if we forced things...
2555 if (action
>= CUPSD_JOB_FORCE
&& job
&& job
->printer
)
2556 finalize_job(job
, 0);
2559 * Update the server "busy" state...
2562 cupsdSetBusyState();
2567 * 'cupsdStopAllJobs()' - Stop all print jobs.
2572 cupsd_jobaction_t action
, /* I - Action */
2573 int kill_delay
) /* I - Number of seconds before we kill */
2575 cupsd_job_t
*job
; /* Current job */
2578 DEBUG_puts("cupsdStopAllJobs()");
2580 for (job
= (cupsd_job_t
*)cupsArrayFirst(PrintingJobs
);
2582 job
= (cupsd_job_t
*)cupsArrayNext(PrintingJobs
))
2585 job
->kill_time
= time(NULL
) + kill_delay
;
2587 cupsdSetJobState(job
, IPP_JOB_PENDING
, action
, NULL
);
2593 * 'cupsdUnloadCompletedJobs()' - Flush completed job history from memory.
2597 cupsdUnloadCompletedJobs(void)
2599 cupsd_job_t
*job
; /* Current job */
2600 time_t expire
; /* Expiration time */
2603 expire
= time(NULL
) - 60;
2605 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
2607 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
2608 if (job
->attrs
&& job
->state_value
>= IPP_JOB_STOPPED
&& !job
->printer
&&
2609 job
->access_time
< expire
)
2620 * 'compare_active_jobs()' - Compare the job IDs and priorities of two jobs.
2623 static int /* O - Difference */
2624 compare_active_jobs(void *first
, /* I - First job */
2625 void *second
, /* I - Second job */
2626 void *data
) /* I - App data (not used) */
2628 int diff
; /* Difference */
2633 if ((diff
= ((cupsd_job_t
*)second
)->priority
-
2634 ((cupsd_job_t
*)first
)->priority
) != 0)
2637 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
2642 * 'compare_jobs()' - Compare the job IDs of two jobs.
2645 static int /* O - Difference */
2646 compare_jobs(void *first
, /* I - First job */
2647 void *second
, /* I - Second job */
2648 void *data
) /* I - App data (not used) */
2652 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
2657 * 'dump_job_history()' - Dump any debug messages for a job.
2661 dump_job_history(cupsd_job_t
*job
) /* I - Job */
2663 int i
, /* Looping var */
2664 oldsize
; /* Current MaxLogSize */
2665 struct tm
*date
; /* Date/time value */
2666 cupsd_joblog_t
*message
; /* Current message */
2667 char temp
[2048], /* Log message */
2668 *ptr
, /* Pointer into log message */
2669 start
[256], /* Start time */
2670 end
[256]; /* End time */
2671 cupsd_printer_t
*printer
; /* Printer for job */
2675 * See if we have anything to dump...
2682 * Disable log rotation temporarily...
2685 oldsize
= MaxLogSize
;
2689 * Copy the debug messages to the log...
2692 message
= (cupsd_joblog_t
*)cupsArrayFirst(job
->history
);
2693 date
= localtime(&(message
->time
));
2694 strftime(start
, sizeof(start
), "%X", date
);
2696 message
= (cupsd_joblog_t
*)cupsArrayLast(job
->history
);
2697 date
= localtime(&(message
->time
));
2698 strftime(end
, sizeof(end
), "%X", date
);
2700 snprintf(temp
, sizeof(temp
),
2701 "[Job %d] The following messages were recorded from %s to %s",
2702 job
->id
, start
, end
);
2703 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, temp
);
2705 for (message
= (cupsd_joblog_t
*)cupsArrayFirst(job
->history
);
2707 message
= (cupsd_joblog_t
*)cupsArrayNext(job
->history
))
2708 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, message
->message
);
2710 snprintf(temp
, sizeof(temp
), "[Job %d] End of messages", job
->id
);
2711 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, temp
);
2714 * Log the printer state values...
2717 if ((printer
= job
->printer
) == NULL
)
2718 printer
= cupsdFindDest(job
->dest
);
2722 snprintf(temp
, sizeof(temp
), "[Job %d] printer-state=%d(%s)", job
->id
,
2724 printer
->state
== IPP_PRINTER_IDLE
? "idle" :
2725 printer
->state
== IPP_PRINTER_PROCESSING
? "processing" :
2727 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, temp
);
2729 snprintf(temp
, sizeof(temp
), "[Job %d] printer-state-message=\"%s\"",
2730 job
->id
, printer
->state_message
);
2731 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, temp
);
2733 snprintf(temp
, sizeof(temp
), "[Job %d] printer-state-reasons=", job
->id
);
2734 ptr
= temp
+ strlen(temp
);
2735 if (printer
->num_reasons
== 0)
2736 strlcpy(ptr
, "none", sizeof(temp
) - (ptr
- temp
));
2740 i
< printer
->num_reasons
&& ptr
< (temp
+ sizeof(temp
) - 2);
2746 strlcpy(ptr
, printer
->reasons
[i
], sizeof(temp
) - (ptr
- temp
));
2750 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, temp
);
2754 * Restore log file rotation...
2757 MaxLogSize
= oldsize
;
2760 * Free all messages...
2763 free_job_history(job
);
2768 * 'free_job_history()' - Free any log history.
2772 free_job_history(cupsd_job_t
*job
) /* I - Job */
2774 char *message
; /* Current message */
2780 for (message
= (char *)cupsArrayFirst(job
->history
);
2782 message
= (char *)cupsArrayNext(job
->history
))
2785 cupsArrayDelete(job
->history
);
2786 job
->history
= NULL
;
2791 * 'finalize_job()' - Cleanup after job filter processes and support data.
2795 finalize_job(cupsd_job_t
*job
, /* I - Job */
2796 int set_job_state
) /* I - 1 = set the job state */
2798 ipp_pstate_t printer_state
; /* New printer state value */
2799 ipp_jstate_t job_state
; /* New job state value */
2800 const char *message
; /* Message for job state */
2801 char buffer
[1024]; /* Buffer for formatted messages */
2804 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "finalize_job(job=%p(%d))", job
, job
->id
);
2807 * Clear the "connecting-to-device" reason, which is only valid when a printer
2808 * is processing, along with any remote printing job state...
2811 cupsdSetPrinterReasons(job
->printer
, "-connecting-to-device,"
2812 "cups-remote-pending,"
2813 "cups-remote-pending-held,"
2814 "cups-remote-processing,"
2815 "cups-remote-stopped,"
2816 "cups-remote-canceled,"
2817 "cups-remote-aborted,"
2818 "cups-remote-completed");
2821 * Similarly, clear the "offline-report" reason for non-USB devices since we
2822 * rarely have current information for network devices...
2825 if (strncmp(job
->printer
->device_uri
, "usb:", 4))
2826 cupsdSetPrinterReasons(job
->printer
, "-offline-report");
2829 * Free the security profile...
2832 cupsdDestroyProfile(job
->profile
);
2833 job
->profile
= NULL
;
2836 * Clear the unresponsive job watchdog timer...
2842 * Close pipes and status buffer...
2845 cupsdClosePipe(job
->print_pipes
);
2846 cupsdClosePipe(job
->back_pipes
);
2847 cupsdClosePipe(job
->side_pipes
);
2849 cupsdRemoveSelect(job
->status_pipes
[0]);
2850 cupsdClosePipe(job
->status_pipes
);
2851 cupsdStatBufDelete(job
->status_buffer
);
2852 job
->status_buffer
= NULL
;
2855 * Process the exit status...
2858 if (job
->printer
->state
== IPP_PRINTER_PROCESSING
)
2859 printer_state
= IPP_PRINTER_IDLE
;
2861 printer_state
= job
->printer
->state
;
2863 switch (job_state
= job
->state_value
)
2865 case IPP_JOB_PENDING
:
2866 message
= "Job paused.";
2870 message
= "Job held.";
2874 case IPP_JOB_PROCESSING
:
2875 case IPP_JOB_COMPLETED
:
2876 job_state
= IPP_JOB_COMPLETED
;
2877 message
= "Job completed.";
2880 case IPP_JOB_STOPPED
:
2881 message
= "Job stopped.";
2884 case IPP_JOB_CANCELED
:
2885 message
= "Job canceled.";
2888 case IPP_JOB_ABORTED
:
2889 message
= "Job aborted.";
2893 if (job
->status
< 0)
2896 * Backend had errors...
2899 int exit_code
; /* Exit code from backend */
2903 * Convert the status to an exit code. Due to the way the W* macros are
2904 * implemented on MacOS X (bug?), we have to store the exit status in a
2905 * variable first and then convert...
2908 exit_code
= -job
->status
;
2909 if (WIFEXITED(exit_code
))
2910 exit_code
= WEXITSTATUS(exit_code
);
2912 exit_code
= job
->status
;
2914 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Backend returned status %d (%s)",
2916 exit_code
== CUPS_BACKEND_FAILED
? "failed" :
2917 exit_code
== CUPS_BACKEND_AUTH_REQUIRED
?
2918 "authentication required" :
2919 exit_code
== CUPS_BACKEND_HOLD
? "hold job" :
2920 exit_code
== CUPS_BACKEND_STOP
? "stop printer" :
2921 exit_code
== CUPS_BACKEND_CANCEL
? "cancel job" :
2922 exit_code
< 0 ? "crashed" : "unknown");
2925 * Do what needs to be done...
2931 case CUPS_BACKEND_FAILED
:
2933 * Backend failure, use the error-policy to determine how to
2937 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
2940 * Queued on a class - mark the job as pending and we'll retry on
2941 * another printer...
2944 if (job_state
== IPP_JOB_COMPLETED
)
2946 job_state
= IPP_JOB_PENDING
;
2947 message
= "Retrying job on another printer.";
2950 else if (!strcmp(job
->printer
->error_policy
, "retry-current-job"))
2953 * The error policy is "retry-current-job" - mark the job as pending
2954 * and we'll retry on the same printer...
2957 if (job_state
== IPP_JOB_COMPLETED
)
2959 job_state
= IPP_JOB_PENDING
;
2960 message
= "Retrying job on same printer.";
2963 else if ((job
->printer
->type
& CUPS_PRINTER_FAX
) ||
2964 !strcmp(job
->printer
->error_policy
, "retry-job"))
2966 if (job_state
== IPP_JOB_COMPLETED
)
2969 * The job was queued on a fax or the error policy is "retry-job" -
2970 * hold the job if the number of retries is less than the
2971 * JobRetryLimit, otherwise abort the job.
2976 if (job
->tries
> JobRetryLimit
&& JobRetryLimit
> 0)
2982 snprintf(buffer
, sizeof(buffer
),
2983 "Job aborted after %d unsuccessful attempts.",
2985 job_state
= IPP_JOB_ABORTED
;
2991 * Try again in N seconds...
2994 snprintf(buffer
, sizeof(buffer
),
2995 "Job held for %d seconds since it could not be sent.",
2998 job
->hold_until
= time(NULL
) + JobRetryInterval
;
2999 job_state
= IPP_JOB_HELD
;
3004 else if (!strcmp(job
->printer
->error_policy
, "abort-job") &&
3005 job_state
== IPP_JOB_COMPLETED
)
3007 job_state
= IPP_JOB_ABORTED
;
3008 message
= "Job aborted due to backend errors; please consult "
3009 "the error_log file for details.";
3011 else if (job
->state_value
== IPP_JOB_PROCESSING
)
3013 job_state
= IPP_JOB_PENDING
;
3014 printer_state
= IPP_PRINTER_STOPPED
;
3015 message
= "Printer stopped due to backend errors; please "
3016 "consult the error_log file for details.";
3020 case CUPS_BACKEND_CANCEL
:
3025 if (job_state
== IPP_JOB_COMPLETED
)
3027 job_state
= IPP_JOB_ABORTED
;
3028 message
= "Job aborted due to backend errors; please consult "
3029 "the error_log file for details.";
3033 case CUPS_BACKEND_HOLD
:
3034 if (job_state
== IPP_JOB_COMPLETED
)
3040 cupsdSetJobHoldUntil(job
, "indefinite", 1);
3042 job_state
= IPP_JOB_HELD
;
3043 message
= "Job held indefinitely due to backend errors; please "
3044 "consult the error_log file for details.";
3048 case CUPS_BACKEND_STOP
:
3050 * Stop the printer...
3053 printer_state
= IPP_PRINTER_STOPPED
;
3054 message
= "Printer stopped due to backend errors; please "
3055 "consult the error_log file for details.";
3057 if (job_state
== IPP_JOB_COMPLETED
)
3058 job_state
= IPP_JOB_PENDING
;
3061 case CUPS_BACKEND_AUTH_REQUIRED
:
3063 * Hold the job for authentication...
3066 if (job_state
== IPP_JOB_COMPLETED
)
3068 cupsdSetJobHoldUntil(job
, "auth-info-required", 1);
3070 job_state
= IPP_JOB_HELD
;
3071 message
= "Job held for authentication.";
3075 case CUPS_BACKEND_RETRY
:
3076 if (job_state
== IPP_JOB_COMPLETED
)
3079 * Hold the job if the number of retries is less than the
3080 * JobRetryLimit, otherwise abort the job.
3085 if (job
->tries
> JobRetryLimit
&& JobRetryLimit
> 0)
3091 snprintf(buffer
, sizeof(buffer
),
3092 "Job aborted after %d unsuccessful attempts.",
3094 job_state
= IPP_JOB_ABORTED
;
3100 * Try again in N seconds...
3103 snprintf(buffer
, sizeof(buffer
),
3104 "Job held for %d seconds since it could not be sent.",
3107 job
->hold_until
= time(NULL
) + JobRetryInterval
;
3108 job_state
= IPP_JOB_HELD
;
3114 case CUPS_BACKEND_RETRY_CURRENT
:
3116 * Mark the job as pending and retry on the same printer...
3119 if (job_state
== IPP_JOB_COMPLETED
)
3121 job_state
= IPP_JOB_PENDING
;
3122 message
= "Retrying job on same printer.";
3127 else if (job
->status
> 0)
3130 * Filter had errors; stop job...
3133 if (job_state
== IPP_JOB_COMPLETED
)
3135 job_state
= IPP_JOB_STOPPED
;
3136 message
= "Job stopped due to filter errors; please consult the "
3137 "error_log file for details.";
3142 * Update the printer and job state.
3145 if (set_job_state
&& job_state
!= job
->state_value
)
3146 cupsdSetJobState(job
, job_state
, CUPSD_JOB_DEFAULT
, "%s", message
);
3148 cupsdSetPrinterState(job
->printer
, printer_state
,
3149 printer_state
== IPP_PRINTER_STOPPED
);
3150 update_job_attrs(job
, 0);
3155 dump_job_history(job
);
3157 free_job_history(job
);
3160 cupsArrayRemove(PrintingJobs
, job
);
3163 * Clear the printer <-> job association...
3166 job
->printer
->job
= NULL
;
3167 job
->printer
= NULL
;
3170 * Try printing another job...
3173 if (printer_state
!= IPP_PRINTER_STOPPED
)
3179 * 'get_options()' - Get a string containing the job options.
3182 static char * /* O - Options string */
3183 get_options(cupsd_job_t
*job
, /* I - Job */
3184 int banner_page
, /* I - Printing a banner page? */
3185 char *copies
, /* I - Copies buffer */
3186 size_t copies_size
, /* I - Size of copies buffer */
3187 char *title
, /* I - Title buffer */
3188 size_t title_size
) /* I - Size of title buffer */
3190 int i
; /* Looping var */
3191 size_t newlength
; /* New option buffer length */
3192 char *optptr
, /* Pointer to options */
3193 *valptr
; /* Pointer in value string */
3194 ipp_attribute_t
*attr
; /* Current attribute */
3195 _ppd_cache_t
*pc
; /* PPD cache and mapping data */
3196 int num_pwgppds
; /* Number of PWG->PPD options */
3197 cups_option_t
*pwgppds
, /* PWG->PPD options */
3198 *pwgppd
, /* Current PWG->PPD option */
3199 *preset
; /* Current preset option */
3200 int print_color_mode
,
3201 /* Output mode (if any) */
3202 print_quality
; /* Print quality (if any) */
3203 const char *ppd
; /* PPD option choice */
3204 int exact
; /* Did we get an exact match? */
3205 static char *options
= NULL
;/* Full list of options */
3206 static size_t optlength
= 0; /* Length of option buffer */
3210 * Building the options string is harder than it needs to be, but for the
3211 * moment we need to pass strings for command-line args and not IPP attribute
3214 * First build an options array for any PWG->PPD mapped option/choice pairs.
3217 pc
= job
->printer
->pc
;
3222 !ippFindAttribute(job
->attrs
,
3223 "com.apple.print.DocumentTicket.PMSpoolFormat",
3225 !ippFindAttribute(job
->attrs
, "APPrinterPreset", IPP_TAG_ZERO
) &&
3226 (ippFindAttribute(job
->attrs
, "output-mode", IPP_TAG_ZERO
) ||
3227 ippFindAttribute(job
->attrs
, "print-color-mode", IPP_TAG_ZERO
) ||
3228 ippFindAttribute(job
->attrs
, "print-quality", IPP_TAG_ZERO
)))
3231 * Map output-mode and print-quality to a preset...
3234 if ((attr
= ippFindAttribute(job
->attrs
, "print-color-mode",
3235 IPP_TAG_KEYWORD
)) == NULL
)
3236 attr
= ippFindAttribute(job
->attrs
, "output-mode", IPP_TAG_KEYWORD
);
3238 if (attr
&& !strcmp(attr
->values
[0].string
.text
, "monochrome"))
3239 print_color_mode
= _PWG_PRINT_COLOR_MODE_MONOCHROME
;
3241 print_color_mode
= _PWG_PRINT_COLOR_MODE_COLOR
;
3243 if ((attr
= ippFindAttribute(job
->attrs
, "print-quality",
3244 IPP_TAG_ENUM
)) != NULL
&&
3245 attr
->values
[0].integer
>= IPP_QUALITY_DRAFT
&&
3246 attr
->values
[0].integer
<= IPP_QUALITY_HIGH
)
3247 print_quality
= attr
->values
[0].integer
- IPP_QUALITY_DRAFT
;
3249 print_quality
= _PWG_PRINT_QUALITY_NORMAL
;
3251 if (pc
->num_presets
[print_color_mode
][print_quality
] == 0)
3254 * Try to find a preset that works so that we maximize the chances of us
3255 * getting a good print using IPP attributes.
3258 if (pc
->num_presets
[print_color_mode
][_PWG_PRINT_QUALITY_NORMAL
] > 0)
3259 print_quality
= _PWG_PRINT_QUALITY_NORMAL
;
3260 else if (pc
->num_presets
[_PWG_PRINT_COLOR_MODE_COLOR
][print_quality
] > 0)
3261 print_color_mode
= _PWG_PRINT_COLOR_MODE_COLOR
;
3264 print_quality
= _PWG_PRINT_QUALITY_NORMAL
;
3265 print_color_mode
= _PWG_PRINT_COLOR_MODE_COLOR
;
3269 if (pc
->num_presets
[print_color_mode
][print_quality
] > 0)
3272 * Copy the preset options as long as the corresponding names are not
3273 * already defined in the IPP request...
3276 for (i
= pc
->num_presets
[print_color_mode
][print_quality
],
3277 preset
= pc
->presets
[print_color_mode
][print_quality
];
3281 if (!ippFindAttribute(job
->attrs
, preset
->name
, IPP_TAG_ZERO
))
3282 num_pwgppds
= cupsAddOption(preset
->name
, preset
->value
, num_pwgppds
,
3290 if (!ippFindAttribute(job
->attrs
, "InputSlot", IPP_TAG_ZERO
) &&
3291 !ippFindAttribute(job
->attrs
, "HPPaperSource", IPP_TAG_ZERO
))
3293 if ((ppd
= _ppdCacheGetInputSlot(pc
, job
->attrs
, NULL
)) != NULL
)
3294 num_pwgppds
= cupsAddOption(pc
->source_option
, ppd
, num_pwgppds
,
3296 else if (!ippFindAttribute(job
->attrs
, "AP_D_InputSlot", IPP_TAG_ZERO
))
3297 num_pwgppds
= cupsAddOption("AP_D_InputSlot", "", num_pwgppds
,
3300 if (!ippFindAttribute(job
->attrs
, "MediaType", IPP_TAG_ZERO
) &&
3301 (ppd
= _ppdCacheGetMediaType(pc
, job
->attrs
, NULL
)) != NULL
)
3302 num_pwgppds
= cupsAddOption("MediaType", ppd
, num_pwgppds
, &pwgppds
);
3304 if (!ippFindAttribute(job
->attrs
, "PageRegion", IPP_TAG_ZERO
) &&
3305 !ippFindAttribute(job
->attrs
, "PageSize", IPP_TAG_ZERO
) &&
3306 (ppd
= _ppdCacheGetPageSize(pc
, job
->attrs
, NULL
, &exact
)) != NULL
)
3308 num_pwgppds
= cupsAddOption("PageSize", ppd
, num_pwgppds
, &pwgppds
);
3310 if (!ippFindAttribute(job
->attrs
, "media", IPP_TAG_ZERO
))
3311 num_pwgppds
= cupsAddOption("media", ppd
, num_pwgppds
, &pwgppds
);
3314 if (!ippFindAttribute(job
->attrs
, "OutputBin", IPP_TAG_ZERO
) &&
3315 (attr
= ippFindAttribute(job
->attrs
, "output-bin",
3316 IPP_TAG_ZERO
)) != NULL
&&
3317 (attr
->value_tag
== IPP_TAG_KEYWORD
||
3318 attr
->value_tag
== IPP_TAG_NAME
) &&
3319 (ppd
= _ppdCacheGetOutputBin(pc
, attr
->values
[0].string
.text
)) != NULL
)
3322 * Map output-bin to OutputBin option...
3325 num_pwgppds
= cupsAddOption("OutputBin", ppd
, num_pwgppds
, &pwgppds
);
3328 if (pc
->sides_option
&&
3329 !ippFindAttribute(job
->attrs
, pc
->sides_option
, IPP_TAG_ZERO
) &&
3330 (attr
= ippFindAttribute(job
->attrs
, "sides", IPP_TAG_KEYWORD
)) != NULL
)
3333 * Map sides to duplex option...
3336 if (!strcmp(attr
->values
[0].string
.text
, "one-sided"))
3337 num_pwgppds
= cupsAddOption(pc
->sides_option
, pc
->sides_1sided
,
3338 num_pwgppds
, &pwgppds
);
3339 else if (!strcmp(attr
->values
[0].string
.text
, "two-sided-long-edge"))
3340 num_pwgppds
= cupsAddOption(pc
->sides_option
, pc
->sides_2sided_long
,
3341 num_pwgppds
, &pwgppds
);
3342 else if (!strcmp(attr
->values
[0].string
.text
, "two-sided-short-edge"))
3343 num_pwgppds
= cupsAddOption(pc
->sides_option
, pc
->sides_2sided_short
,
3344 num_pwgppds
, &pwgppds
);
3349 * Figure out how much room we need...
3352 newlength
= ipp_length(job
->attrs
);
3354 for (i
= num_pwgppds
, pwgppd
= pwgppds
; i
> 0; i
--, pwgppd
++)
3355 newlength
+= 1 + strlen(pwgppd
->name
) + 1 + strlen(pwgppd
->value
);
3358 * Then allocate/reallocate the option buffer as needed...
3361 if (newlength
> optlength
|| !options
)
3364 optptr
= malloc(newlength
);
3366 optptr
= realloc(options
, newlength
);
3370 cupsdLogJob(job
, CUPSD_LOG_CRIT
,
3371 "Unable to allocate " CUPS_LLFMT
" bytes for option buffer!",
3372 CUPS_LLCAST newlength
);
3377 optlength
= newlength
;
3381 * Now loop through the attributes and convert them to the textual
3382 * representation used by the filters...
3388 snprintf(title
, title_size
, "%s-%d", job
->printer
->name
, job
->id
);
3389 strlcpy(copies
, "1", copies_size
);
3391 for (attr
= job
->attrs
->attrs
; attr
!= NULL
; attr
= attr
->next
)
3393 if (!strcmp(attr
->name
, "copies") &&
3394 attr
->value_tag
== IPP_TAG_INTEGER
)
3397 * Don't use the # copies attribute if we are printing the job sheets...
3401 snprintf(copies
, copies_size
, "%d", attr
->values
[0].integer
);
3403 else if (!strcmp(attr
->name
, "job-name") &&
3404 (attr
->value_tag
== IPP_TAG_NAME
||
3405 attr
->value_tag
== IPP_TAG_NAMELANG
))
3406 strlcpy(title
, attr
->values
[0].string
.text
, title_size
);
3407 else if (attr
->group_tag
== IPP_TAG_JOB
)
3410 * Filter out other unwanted attributes...
3413 if (attr
->value_tag
== IPP_TAG_NOVALUE
||
3414 attr
->value_tag
== IPP_TAG_MIMETYPE
||
3415 attr
->value_tag
== IPP_TAG_NAMELANG
||
3416 attr
->value_tag
== IPP_TAG_TEXTLANG
||
3417 (attr
->value_tag
== IPP_TAG_URI
&& strcmp(attr
->name
, "job-uuid")) ||
3418 attr
->value_tag
== IPP_TAG_URISCHEME
||
3419 attr
->value_tag
== IPP_TAG_BEGIN_COLLECTION
) /* Not yet supported */
3422 if (!strcmp(attr
->name
, "job-hold-until"))
3425 if (!strncmp(attr
->name
, "job-", 4) &&
3426 strcmp(attr
->name
, "job-billing") &&
3427 strcmp(attr
->name
, "job-impressions") &&
3428 strcmp(attr
->name
, "job-originating-host-name") &&
3429 strcmp(attr
->name
, "job-uuid") &&
3430 !(job
->printer
->type
& CUPS_PRINTER_REMOTE
))
3433 if ((!strcmp(attr
->name
, "job-impressions") ||
3434 !strcmp(attr
->name
, "page-label") ||
3435 !strcmp(attr
->name
, "page-border") ||
3436 !strncmp(attr
->name
, "number-up", 9) ||
3437 !strcmp(attr
->name
, "page-ranges") ||
3438 !strcmp(attr
->name
, "page-set") ||
3439 !_cups_strcasecmp(attr
->name
, "AP_FIRSTPAGE_InputSlot") ||
3440 !_cups_strcasecmp(attr
->name
, "AP_FIRSTPAGE_ManualFeed") ||
3441 !_cups_strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
3442 "PMTotalSidesImaged..n.") ||
3443 !_cups_strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
3444 "PMTotalBeginPages..n.")) &&
3449 * Otherwise add them to the list...
3452 if (optptr
> options
)
3453 strlcat(optptr
, " ", optlength
- (optptr
- options
));
3455 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
3457 strlcat(optptr
, attr
->name
, optlength
- (optptr
- options
));
3458 strlcat(optptr
, "=", optlength
- (optptr
- options
));
3461 for (i
= 0; i
< attr
->num_values
; i
++)
3464 strlcat(optptr
, ",", optlength
- (optptr
- options
));
3466 optptr
+= strlen(optptr
);
3468 switch (attr
->value_tag
)
3470 case IPP_TAG_INTEGER
:
3472 snprintf(optptr
, optlength
- (optptr
- options
),
3473 "%d", attr
->values
[i
].integer
);
3476 case IPP_TAG_BOOLEAN
:
3477 if (!attr
->values
[i
].boolean
)
3478 strlcat(optptr
, "no", optlength
- (optptr
- options
));
3480 strlcat(optptr
, attr
->name
,
3481 optlength
- (optptr
- options
));
3484 case IPP_TAG_RANGE
:
3485 if (attr
->values
[i
].range
.lower
== attr
->values
[i
].range
.upper
)
3486 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
3487 "%d", attr
->values
[i
].range
.lower
);
3489 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
3490 "%d-%d", attr
->values
[i
].range
.lower
,
3491 attr
->values
[i
].range
.upper
);
3494 case IPP_TAG_RESOLUTION
:
3495 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
3496 "%dx%d%s", attr
->values
[i
].resolution
.xres
,
3497 attr
->values
[i
].resolution
.yres
,
3498 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
3502 case IPP_TAG_STRING
:
3505 case IPP_TAG_KEYWORD
:
3506 case IPP_TAG_CHARSET
:
3507 case IPP_TAG_LANGUAGE
:
3509 for (valptr
= attr
->values
[i
].string
.text
; *valptr
;)
3511 if (strchr(" \t\n\\\'\"", *valptr
))
3513 *optptr
++ = *valptr
++;
3520 break; /* anti-compiler-warning-code */
3524 optptr
+= strlen(optptr
);
3529 * Finally loop through the PWG->PPD mapped options and add them...
3532 for (i
= num_pwgppds
, pwgppd
= pwgppds
; i
> 0; i
--, pwgppd
++)
3535 strcpy(optptr
, pwgppd
->name
);
3536 optptr
+= strlen(optptr
);
3538 strcpy(optptr
, pwgppd
->value
);
3539 optptr
+= strlen(optptr
);
3542 cupsFreeOptions(num_pwgppds
, pwgppds
);
3545 * Return the options string...
3553 * 'ipp_length()' - Compute the size of the buffer needed to hold
3554 * the textual IPP attributes.
3557 static size_t /* O - Size of attribute buffer */
3558 ipp_length(ipp_t
*ipp
) /* I - IPP request */
3560 size_t bytes
; /* Number of bytes */
3561 int i
; /* Looping var */
3562 ipp_attribute_t
*attr
; /* Current attribute */
3566 * Loop through all attributes...
3571 for (attr
= ipp
->attrs
; attr
!= NULL
; attr
= attr
->next
)
3574 * Skip attributes that won't be sent to filters...
3577 if (attr
->value_tag
== IPP_TAG_NOVALUE
||
3578 attr
->value_tag
== IPP_TAG_MIMETYPE
||
3579 attr
->value_tag
== IPP_TAG_NAMELANG
||
3580 attr
->value_tag
== IPP_TAG_TEXTLANG
||
3581 attr
->value_tag
== IPP_TAG_URI
||
3582 attr
->value_tag
== IPP_TAG_URISCHEME
)
3586 * Add space for a leading space and commas between each value.
3587 * For the first attribute, the leading space isn't used, so the
3588 * extra byte can be used as the nul terminator...
3591 bytes
++; /* " " separator */
3592 bytes
+= attr
->num_values
; /* "," separators */
3595 * Boolean attributes appear as "foo,nofoo,foo,nofoo", while
3596 * other attributes appear as "foo=value1,value2,...,valueN".
3599 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
3600 bytes
+= strlen(attr
->name
);
3602 bytes
+= attr
->num_values
* strlen(attr
->name
);
3605 * Now add the size required for each value in the attribute...
3608 switch (attr
->value_tag
)
3610 case IPP_TAG_INTEGER
:
3613 * Minimum value of a signed integer is -2147483647, or 11 digits.
3616 bytes
+= attr
->num_values
* 11;
3619 case IPP_TAG_BOOLEAN
:
3621 * Add two bytes for each false ("no") value...
3624 for (i
= 0; i
< attr
->num_values
; i
++)
3625 if (!attr
->values
[i
].boolean
)
3629 case IPP_TAG_RANGE
:
3631 * A range is two signed integers separated by a hyphen, or
3632 * 23 characters max.
3635 bytes
+= attr
->num_values
* 23;
3638 case IPP_TAG_RESOLUTION
:
3640 * A resolution is two signed integers separated by an "x" and
3641 * suffixed by the units, or 26 characters max.
3644 bytes
+= attr
->num_values
* 26;
3647 case IPP_TAG_STRING
:
3650 case IPP_TAG_KEYWORD
:
3651 case IPP_TAG_CHARSET
:
3652 case IPP_TAG_LANGUAGE
:
3655 * Strings can contain characters that need quoting. We need
3656 * at least 2 * len + 2 characters to cover the quotes and
3657 * any backslashes in the string.
3660 for (i
= 0; i
< attr
->num_values
; i
++)
3661 bytes
+= 2 * strlen(attr
->values
[i
].string
.text
) + 2;
3665 break; /* anti-compiler-warning-code */
3674 * 'load_job_cache()' - Load jobs from the job.cache file.
3678 load_job_cache(const char *filename
) /* I - job.cache filename */
3680 cups_file_t
*fp
; /* job.cache file */
3681 char line
[1024], /* Line buffer */
3682 *value
; /* Value on line */
3683 int linenum
; /* Line number in file */
3684 cupsd_job_t
*job
; /* Current job */
3685 int jobid
; /* Job ID */
3686 char jobfile
[1024]; /* Job filename */
3690 * Open the job.cache file...
3693 if ((fp
= cupsdOpenConfFile(filename
)) == NULL
)
3695 load_request_root();
3700 * Read entries from the job cache file and create jobs as needed.
3703 cupsdLogMessage(CUPSD_LOG_INFO
, "Loading job cache file \"%s\"...",
3709 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
3711 if (!_cups_strcasecmp(line
, "NextJobId"))
3714 NextJobId
= atoi(value
);
3716 else if (!_cups_strcasecmp(line
, "<Job"))
3720 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing </Job> directive on line %d!",
3727 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing job ID on line %d!", linenum
);
3731 jobid
= atoi(value
);
3735 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad job ID %d on line %d!", jobid
,
3740 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, jobid
);
3741 if (access(jobfile
, 0))
3743 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d.N", RequestRoot
, jobid
);
3744 if (access(jobfile
, 0))
3746 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Job %d] Files have gone away!",
3752 job
= calloc(1, sizeof(cupsd_job_t
));
3755 cupsdLogMessage(CUPSD_LOG_EMERG
,
3756 "[Job %d] Unable to allocate memory for job!", jobid
);
3761 job
->back_pipes
[0] = -1;
3762 job
->back_pipes
[1] = -1;
3763 job
->print_pipes
[0] = -1;
3764 job
->print_pipes
[1] = -1;
3765 job
->side_pipes
[0] = -1;
3766 job
->side_pipes
[1] = -1;
3767 job
->status_pipes
[0] = -1;
3768 job
->status_pipes
[1] = -1;
3770 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Loading from cache...",
3775 cupsdLogMessage(CUPSD_LOG_ERROR
,
3776 "Missing <Job #> directive on line %d!", linenum
);
3779 else if (!_cups_strcasecmp(line
, "</Job>"))
3781 cupsArrayAdd(Jobs
, job
);
3783 if (job
->state_value
<= IPP_JOB_STOPPED
&& cupsdLoadJob(job
))
3784 cupsArrayAdd(ActiveJobs
, job
);
3790 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing value on line %d!", linenum
);
3793 else if (!_cups_strcasecmp(line
, "State"))
3795 job
->state_value
= (ipp_jstate_t
)atoi(value
);
3797 if (job
->state_value
< IPP_JOB_PENDING
)
3798 job
->state_value
= IPP_JOB_PENDING
;
3799 else if (job
->state_value
> IPP_JOB_COMPLETED
)
3800 job
->state_value
= IPP_JOB_COMPLETED
;
3802 else if (!_cups_strcasecmp(line
, "HoldUntil"))
3804 job
->hold_until
= atoi(value
);
3806 else if (!_cups_strcasecmp(line
, "Priority"))
3808 job
->priority
= atoi(value
);
3810 else if (!_cups_strcasecmp(line
, "Username"))
3812 cupsdSetString(&job
->username
, value
);
3814 else if (!_cups_strcasecmp(line
, "Destination"))
3816 cupsdSetString(&job
->dest
, value
);
3818 else if (!_cups_strcasecmp(line
, "DestType"))
3820 job
->dtype
= (cups_ptype_t
)atoi(value
);
3822 else if (!_cups_strcasecmp(line
, "NumFiles"))
3824 job
->num_files
= atoi(value
);
3826 if (job
->num_files
< 0)
3828 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad NumFiles value %d on line %d!",
3829 job
->num_files
, linenum
);
3834 if (job
->num_files
> 0)
3836 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-001", RequestRoot
,
3838 if (access(jobfile
, 0))
3840 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Data files have gone away!",
3846 job
->filetypes
= calloc(job
->num_files
, sizeof(mime_type_t
*));
3847 job
->compressions
= calloc(job
->num_files
, sizeof(int));
3849 if (!job
->filetypes
|| !job
->compressions
)
3851 cupsdLogMessage(CUPSD_LOG_EMERG
,
3852 "[Job %d] Unable to allocate memory for %d files!",
3853 job
->id
, job
->num_files
);
3858 else if (!_cups_strcasecmp(line
, "File"))
3860 int number
, /* File number */
3861 compression
; /* Compression value */
3862 char super
[MIME_MAX_SUPER
], /* MIME super type */
3863 type
[MIME_MAX_TYPE
]; /* MIME type */
3866 if (sscanf(value
, "%d%*[ \t]%15[^/]/%255s%d", &number
, super
, type
,
3869 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File on line %d!", linenum
);
3873 if (number
< 1 || number
> job
->num_files
)
3875 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File number %d on line %d!",
3882 job
->compressions
[number
] = compression
;
3883 job
->filetypes
[number
] = mimeType(MimeDatabase
, super
, type
);
3885 if (!job
->filetypes
[number
])
3888 * If the original MIME type is unknown, auto-type it!
3891 cupsdLogMessage(CUPSD_LOG_ERROR
,
3892 "[Job %d] Unknown MIME type %s/%s for file %d!",
3893 job
->id
, super
, type
, number
+ 1);
3895 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
3896 job
->id
, number
+ 1);
3897 job
->filetypes
[number
] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
3898 job
->compressions
+ number
);
3901 * If that didn't work, assume it is raw...
3904 if (!job
->filetypes
[number
])
3905 job
->filetypes
[number
] = mimeType(MimeDatabase
, "application",
3910 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unknown %s directive on line %d!",
3919 * 'load_next_job_id()' - Load the NextJobId value from the job.cache file.
3923 load_next_job_id(const char *filename
) /* I - job.cache filename */
3925 cups_file_t
*fp
; /* job.cache file */
3926 char line
[1024], /* Line buffer */
3927 *value
; /* Value on line */
3928 int linenum
; /* Line number in file */
3929 int next_job_id
; /* NextJobId value from line */
3933 * Read the NextJobId directive from the job.cache file and use
3934 * the value (if any).
3937 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
3939 if (errno
!= ENOENT
)
3940 cupsdLogMessage(CUPSD_LOG_ERROR
,
3941 "Unable to open job cache file \"%s\": %s",
3942 filename
, strerror(errno
));
3947 cupsdLogMessage(CUPSD_LOG_INFO
,
3948 "Loading NextJobId from job cache file \"%s\"...", filename
);
3952 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
3954 if (!_cups_strcasecmp(line
, "NextJobId"))
3958 next_job_id
= atoi(value
);
3960 if (next_job_id
> NextJobId
)
3961 NextJobId
= next_job_id
;
3972 * 'load_request_root()' - Load jobs from the RequestRoot directory.
3976 load_request_root(void)
3978 cups_dir_t
*dir
; /* Directory */
3979 cups_dentry_t
*dent
; /* Directory entry */
3980 cupsd_job_t
*job
; /* New job */
3984 * Open the requests directory...
3987 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Scanning %s for jobs...", RequestRoot
);
3989 if ((dir
= cupsDirOpen(RequestRoot
)) == NULL
)
3991 cupsdLogMessage(CUPSD_LOG_ERROR
,
3992 "Unable to open spool directory \"%s\": %s",
3993 RequestRoot
, strerror(errno
));
3998 * Read all the c##### files...
4001 while ((dent
= cupsDirRead(dir
)) != NULL
)
4002 if (strlen(dent
->filename
) >= 6 && dent
->filename
[0] == 'c')
4005 * Allocate memory for the job...
4008 if ((job
= calloc(sizeof(cupsd_job_t
), 1)) == NULL
)
4010 cupsdLogMessage(CUPSD_LOG_ERROR
, "Ran out of memory for jobs!");
4016 * Assign the job ID...
4019 job
->id
= atoi(dent
->filename
+ 1);
4020 job
->back_pipes
[0] = -1;
4021 job
->back_pipes
[1] = -1;
4022 job
->print_pipes
[0] = -1;
4023 job
->print_pipes
[1] = -1;
4024 job
->side_pipes
[0] = -1;
4025 job
->side_pipes
[1] = -1;
4026 job
->status_pipes
[0] = -1;
4027 job
->status_pipes
[1] = -1;
4029 if (job
->id
>= NextJobId
)
4030 NextJobId
= job
->id
+ 1;
4036 if (cupsdLoadJob(job
))
4039 * Insert the job into the array, sorting by job priority and ID...
4042 cupsArrayAdd(Jobs
, job
);
4044 if (job
->state_value
<= IPP_JOB_STOPPED
)
4045 cupsArrayAdd(ActiveJobs
, job
);
4056 * 'set_time()' - Set one of the "time-at-xyz" attributes.
4060 set_time(cupsd_job_t
*job
, /* I - Job to update */
4061 const char *name
) /* I - Name of attribute */
4063 ipp_attribute_t
*attr
; /* Time attribute */
4066 if ((attr
= ippFindAttribute(job
->attrs
, name
, IPP_TAG_ZERO
)) != NULL
)
4068 attr
->value_tag
= IPP_TAG_INTEGER
;
4069 attr
->values
[0].integer
= time(NULL
);
4075 * 'start_job()' - Start a print job.
4079 start_job(cupsd_job_t
*job
, /* I - Job ID */
4080 cupsd_printer_t
*printer
) /* I - Printer to print job */
4082 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "start_job(job=%p(%d), printer=%p(%s))",
4083 job
, job
->id
, printer
, printer
->name
);
4086 * Make sure we have some files around before we try to print...
4089 if (job
->num_files
== 0)
4091 cupsdSetJobState(job
, IPP_JOB_ABORTED
, CUPSD_JOB_DEFAULT
,
4092 "Aborting job because it has no files.");
4097 * Update the printer and job state to "processing"...
4100 if (!cupsdLoadJob(job
))
4103 if (job
->printer_message
)
4104 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
), "");
4106 cupsdSetJobState(job
, IPP_JOB_PROCESSING
, CUPSD_JOB_DEFAULT
, NULL
);
4107 cupsdSetPrinterState(printer
, IPP_PRINTER_PROCESSING
, 0);
4108 cupsdSetPrinterReasons(printer
, "-cups-remote-pending,"
4109 "cups-remote-pending-held,"
4110 "cups-remote-processing,"
4111 "cups-remote-stopped,"
4112 "cups-remote-canceled,"
4113 "cups-remote-aborted,"
4114 "cups-remote-completed");
4117 job
->current_file
= 0;
4119 job
->printer
= printer
;
4123 * Setup the last exit status and security profiles...
4127 job
->profile
= cupsdCreateProfile(job
->id
);
4130 * Create the status pipes and buffer...
4133 if (cupsdOpenPipe(job
->status_pipes
))
4135 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
4136 "Unable to create job status pipes - %s.", strerror(errno
));
4138 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
4139 "Job stopped because the scheduler could not create the "
4140 "job status pipes.");
4142 cupsdDestroyProfile(job
->profile
);
4143 job
->profile
= NULL
;
4147 job
->status_buffer
= cupsdStatBufNew(job
->status_pipes
[0], NULL
);
4148 job
->status_level
= CUPSD_LOG_INFO
;
4151 * Create the backchannel pipes and make them non-blocking...
4154 if (cupsdOpenPipe(job
->back_pipes
))
4156 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
4157 "Unable to create back-channel pipes - %s.", strerror(errno
));
4159 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
4160 "Job stopped because the scheduler could not create the "
4161 "back-channel pipes.");
4163 cupsdClosePipe(job
->status_pipes
);
4164 cupsdStatBufDelete(job
->status_buffer
);
4165 job
->status_buffer
= NULL
;
4167 cupsdDestroyProfile(job
->profile
);
4168 job
->profile
= NULL
;
4172 fcntl(job
->back_pipes
[0], F_SETFL
,
4173 fcntl(job
->back_pipes
[0], F_GETFL
) | O_NONBLOCK
);
4174 fcntl(job
->back_pipes
[1], F_SETFL
,
4175 fcntl(job
->back_pipes
[1], F_GETFL
) | O_NONBLOCK
);
4178 * Create the side-channel pipes and make them non-blocking...
4181 if (socketpair(AF_LOCAL
, SOCK_STREAM
, 0, job
->side_pipes
))
4183 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
4184 "Unable to create side-channel pipes - %s.", strerror(errno
));
4186 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
4187 "Job stopped because the scheduler could not create the "
4188 "side-channel pipes.");
4190 cupsdClosePipe(job
->back_pipes
);
4192 cupsdClosePipe(job
->status_pipes
);
4193 cupsdStatBufDelete(job
->status_buffer
);
4194 job
->status_buffer
= NULL
;
4196 cupsdDestroyProfile(job
->profile
);
4197 job
->profile
= NULL
;
4201 fcntl(job
->side_pipes
[0], F_SETFL
,
4202 fcntl(job
->side_pipes
[0], F_GETFL
) | O_NONBLOCK
);
4203 fcntl(job
->side_pipes
[1], F_SETFL
,
4204 fcntl(job
->side_pipes
[1], F_GETFL
) | O_NONBLOCK
);
4206 fcntl(job
->side_pipes
[0], F_SETFD
,
4207 fcntl(job
->side_pipes
[0], F_GETFD
) | FD_CLOEXEC
);
4208 fcntl(job
->side_pipes
[1], F_SETFD
,
4209 fcntl(job
->side_pipes
[1], F_GETFD
) | FD_CLOEXEC
);
4212 * Now start the first file in the job...
4215 cupsdContinueJob(job
);
4220 * 'stop_job()' - Stop a print job.
4224 stop_job(cupsd_job_t
*job
, /* I - Job */
4225 cupsd_jobaction_t action
) /* I - Action */
4227 int i
; /* Looping var */
4230 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "stop_job(job=%p(%d), action=%d)", job
,
4233 FilterLevel
-= job
->cost
;
4236 if (action
== CUPSD_JOB_DEFAULT
&& !job
->kill_time
)
4237 job
->kill_time
= time(NULL
) + JobKillDelay
;
4238 else if (action
>= CUPSD_JOB_FORCE
)
4241 for (i
= 0; job
->filters
[i
]; i
++)
4242 if (job
->filters
[i
] > 0)
4244 cupsdEndProcess(job
->filters
[i
], action
>= CUPSD_JOB_FORCE
);
4246 if (action
>= CUPSD_JOB_FORCE
)
4247 job
->filters
[i
] = -job
->filters
[i
];
4250 if (job
->backend
> 0)
4252 cupsdEndProcess(job
->backend
, action
>= CUPSD_JOB_FORCE
);
4254 if (action
>= CUPSD_JOB_FORCE
)
4255 job
->backend
= -job
->backend
;
4258 if (action
>= CUPSD_JOB_FORCE
)
4261 * Clear job status...
4270 * 'unload_job()' - Unload a job from memory.
4274 unload_job(cupsd_job_t
*job
) /* I - Job */
4279 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Unloading...", job
->id
);
4281 ippDelete(job
->attrs
);
4286 job
->job_sheets
= NULL
;
4287 job
->printer_message
= NULL
;
4288 job
->printer_reasons
= NULL
;
4293 * 'update_job()' - Read a status update from a job's filters.
4297 update_job(cupsd_job_t
*job
) /* I - Job to check */
4299 int i
; /* Looping var */
4300 int copies
; /* Number of copies printed */
4301 char message
[CUPSD_SB_BUFFER_SIZE
],
4303 *ptr
; /* Pointer update... */
4304 int loglevel
, /* Log level for message */
4305 event
= 0; /* Events? */
4306 static const char * const levels
[] = /* Log levels */
4322 * Get the printer associated with this job; if the printer is stopped for
4323 * any reason then job->printer will be reset to NULL, so make sure we have
4324 * a valid pointer...
4327 while ((ptr
= cupsdStatBufUpdate(job
->status_buffer
, &loglevel
,
4328 message
, sizeof(message
))) != NULL
)
4331 * Process page and printer state messages as needed...
4334 if (loglevel
== CUPSD_LOG_PAGE
)
4337 * Page message; send the message to the page_log file and update the
4338 * job sheet count...
4341 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "PAGE: %s", message
);
4345 if (!_cups_strncasecmp(message
, "total ", 6))
4348 * Got a total count of pages from a backend or filter...
4351 copies
= atoi(message
+ 6);
4352 copies
-= job
->sheets
->values
[0].integer
; /* Just track the delta */
4354 else if (!sscanf(message
, "%*d%d", &copies
))
4357 job
->sheets
->values
[0].integer
+= copies
;
4359 if (job
->printer
->page_limit
)
4360 cupsdUpdateQuota(job
->printer
, job
->username
, copies
, 0);
4363 cupsdLogPage(job
, message
);
4366 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
4367 "Printed %d page(s).", job
->sheets
->values
[0].integer
);
4369 else if (loglevel
== CUPSD_LOG_STATE
)
4371 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "STATE: %s", message
);
4373 if (!strcmp(message
, "paused"))
4375 cupsdStopPrinter(job
->printer
, 1);
4378 else if (cupsdSetPrinterReasons(job
->printer
, message
))
4379 event
|= CUPSD_EVENT_PRINTER_STATE
;
4381 update_job_attrs(job
, 0);
4383 else if (loglevel
== CUPSD_LOG_ATTR
)
4386 * Set attribute(s)...
4389 int num_attrs
; /* Number of attributes */
4390 cups_option_t
*attrs
; /* Attributes */
4391 const char *attr
; /* Attribute */
4394 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "ATTR: %s", message
);
4396 num_attrs
= cupsParseOptions(message
, 0, &attrs
);
4398 if ((attr
= cupsGetOption("auth-info-required", num_attrs
,
4401 cupsdSetAuthInfoRequired(job
->printer
, attr
, NULL
);
4402 cupsdSetPrinterAttrs(job
->printer
);
4404 if (job
->printer
->type
& CUPS_PRINTER_DISCOVERED
)
4405 cupsdMarkDirty(CUPSD_DIRTY_REMOTE
);
4407 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4410 if ((attr
= cupsGetOption("job-media-progress", num_attrs
,
4413 int progress
= atoi(attr
);
4416 if (progress
>= 0 && progress
<= 100)
4418 job
->progress
= progress
;
4421 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
4422 "Printing page %d, %d%%",
4423 job
->sheets
->values
[0].integer
, job
->progress
);
4427 if ((attr
= cupsGetOption("printer-alert", num_attrs
, attrs
)) != NULL
)
4429 cupsdSetString(&job
->printer
->alert
, attr
);
4430 event
|= CUPSD_EVENT_PRINTER_STATE
;
4433 if ((attr
= cupsGetOption("printer-alert-description", num_attrs
,
4436 cupsdSetString(&job
->printer
->alert_description
, attr
);
4437 event
|= CUPSD_EVENT_PRINTER_STATE
;
4440 if ((attr
= cupsGetOption("marker-colors", num_attrs
, attrs
)) != NULL
)
4442 cupsdSetPrinterAttr(job
->printer
, "marker-colors", (char *)attr
);
4443 job
->printer
->marker_time
= time(NULL
);
4444 event
|= CUPSD_EVENT_PRINTER_STATE
;
4445 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4448 if ((attr
= cupsGetOption("marker-levels", num_attrs
, attrs
)) != NULL
)
4450 cupsdSetPrinterAttr(job
->printer
, "marker-levels", (char *)attr
);
4451 job
->printer
->marker_time
= time(NULL
);
4452 event
|= CUPSD_EVENT_PRINTER_STATE
;
4453 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4456 if ((attr
= cupsGetOption("marker-low-levels", num_attrs
, attrs
)) != NULL
)
4458 cupsdSetPrinterAttr(job
->printer
, "marker-low-levels", (char *)attr
);
4459 job
->printer
->marker_time
= time(NULL
);
4460 event
|= CUPSD_EVENT_PRINTER_STATE
;
4461 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4464 if ((attr
= cupsGetOption("marker-high-levels", num_attrs
, attrs
)) != NULL
)
4466 cupsdSetPrinterAttr(job
->printer
, "marker-high-levels", (char *)attr
);
4467 job
->printer
->marker_time
= time(NULL
);
4468 event
|= CUPSD_EVENT_PRINTER_STATE
;
4469 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4472 if ((attr
= cupsGetOption("marker-message", num_attrs
, attrs
)) != NULL
)
4474 cupsdSetPrinterAttr(job
->printer
, "marker-message", (char *)attr
);
4475 job
->printer
->marker_time
= time(NULL
);
4476 event
|= CUPSD_EVENT_PRINTER_STATE
;
4477 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4480 if ((attr
= cupsGetOption("marker-names", num_attrs
, attrs
)) != NULL
)
4482 cupsdSetPrinterAttr(job
->printer
, "marker-names", (char *)attr
);
4483 job
->printer
->marker_time
= time(NULL
);
4484 event
|= CUPSD_EVENT_PRINTER_STATE
;
4485 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4488 if ((attr
= cupsGetOption("marker-types", num_attrs
, attrs
)) != NULL
)
4490 cupsdSetPrinterAttr(job
->printer
, "marker-types", (char *)attr
);
4491 job
->printer
->marker_time
= time(NULL
);
4492 event
|= CUPSD_EVENT_PRINTER_STATE
;
4493 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4496 cupsFreeOptions(num_attrs
, attrs
);
4498 else if (loglevel
== CUPSD_LOG_PPD
)
4501 * Set attribute(s)...
4504 int num_keywords
; /* Number of keywords */
4505 cups_option_t
*keywords
; /* Keywords */
4508 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "PPD: %s", message
);
4510 num_keywords
= cupsParseOptions(message
, 0, &keywords
);
4512 if (cupsdUpdatePrinterPPD(job
->printer
, num_keywords
, keywords
))
4513 cupsdSetPrinterAttrs(job
->printer
);
4515 cupsFreeOptions(num_keywords
, keywords
);
4520 * Strip legacy message prefix...
4523 if (!strncmp(message
, "recoverable:", 12))
4526 while (isspace(*ptr
& 255))
4529 else if (!strncmp(message
, "recovered:", 10))
4532 while (isspace(*ptr
& 255))
4538 cupsdLogJob(job
, loglevel
, "%s", ptr
);
4540 if (loglevel
< CUPSD_LOG_DEBUG
&&
4541 strcmp(job
->printer
->state_message
, ptr
))
4543 strlcpy(job
->printer
->state_message
, ptr
,
4544 sizeof(job
->printer
->state_message
));
4546 event
|= CUPSD_EVENT_PRINTER_STATE
| CUPSD_EVENT_JOB_PROGRESS
;
4548 if (loglevel
<= job
->status_level
&& job
->status_level
> CUPSD_LOG_ERROR
)
4551 * Some messages show in the job-printer-state-message attribute...
4554 if (loglevel
!= CUPSD_LOG_NOTICE
)
4555 job
->status_level
= loglevel
;
4557 update_job_attrs(job
, 1);
4559 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
4560 "Set job-printer-state-message to \"%s\", "
4562 job
->printer_message
->values
[0].string
.text
,
4563 levels
[job
->status_level
]);
4568 if (!strchr(job
->status_buffer
->buffer
, '\n'))
4572 if (event
& CUPSD_EVENT_JOB_PROGRESS
)
4573 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
4574 "%s", job
->printer
->state_message
);
4575 if (event
& CUPSD_EVENT_PRINTER_STATE
)
4576 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, job
->printer
, NULL
,
4577 (job
->printer
->type
& CUPS_PRINTER_CLASS
) ?
4578 "Class \"%s\" state changed." :
4579 "Printer \"%s\" state changed.",
4580 job
->printer
->name
);
4583 if (ptr
== NULL
&& !job
->status_buffer
->bufused
)
4586 * See if all of the filters and the backend have returned their
4590 for (i
= 0; job
->filters
[i
] < 0; i
++);
4592 if (job
->filters
[i
])
4595 * EOF but we haven't collected the exit status of all filters...
4598 cupsdCheckProcess();
4602 if (job
->current_file
>= job
->num_files
&& job
->backend
> 0)
4605 * EOF but we haven't collected the exit status of the backend...
4608 cupsdCheckProcess();
4613 * Handle the end of job stuff...
4616 finalize_job(job
, 1);
4619 * Check for new jobs...
4628 * 'update_job_attrs()' - Update the job-printer-* attributes.
4632 update_job_attrs(cupsd_job_t
*job
, /* I - Job to update */
4633 int do_message
)/* I - 1 = copy job-printer-state message */
4635 int i
; /* Looping var */
4636 int num_reasons
; /* Actual number of reasons */
4637 const char * const *reasons
; /* Reasons */
4638 static const char *none
= "none"; /* "none" reason */
4642 * Get/create the job-printer-state-* attributes...
4645 if (!job
->printer_message
)
4647 if ((job
->printer_message
= ippFindAttribute(job
->attrs
,
4648 "job-printer-state-message",
4649 IPP_TAG_TEXT
)) == NULL
)
4650 job
->printer_message
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_TEXT
,
4651 "job-printer-state-message",
4655 if (!job
->printer_reasons
)
4656 job
->printer_reasons
= ippFindAttribute(job
->attrs
,
4657 "job-printer-state-reasons",
4661 * Copy or clear the printer-state-message value as needed...
4664 if (job
->state_value
!= IPP_JOB_PROCESSING
&&
4665 job
->status_level
== CUPSD_LOG_INFO
)
4666 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
), "");
4667 else if (job
->printer
->state_message
[0] && do_message
)
4668 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
),
4669 job
->printer
->state_message
);
4672 * ... and the printer-state-reasons value...
4675 if (job
->printer
->num_reasons
== 0)
4682 num_reasons
= job
->printer
->num_reasons
;
4683 reasons
= (const char * const *)job
->printer
->reasons
;
4686 if (!job
->printer_reasons
|| job
->printer_reasons
->num_values
!= num_reasons
)
4689 * Replace/create a job-printer-state-reasons attribute...
4692 ippDeleteAttribute(job
->attrs
, job
->printer_reasons
);
4694 job
->printer_reasons
= ippAddStrings(job
->attrs
,
4695 IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
4696 "job-printer-state-reasons",
4697 num_reasons
, NULL
, NULL
);
4702 * Don't bother clearing the reason strings if they are the same...
4705 for (i
= 0; i
< num_reasons
; i
++)
4706 if (strcmp(job
->printer_reasons
->values
[i
].string
.text
, reasons
[i
]))
4709 if (i
>= num_reasons
)
4713 * Not the same, so free the current strings...
4716 for (i
= 0; i
< num_reasons
; i
++)
4717 _cupsStrFree(job
->printer_reasons
->values
[i
].string
.text
);
4721 * Copy the reasons...
4724 for (i
= 0; i
< num_reasons
; i
++)
4725 job
->printer_reasons
->values
[i
].string
.text
= _cupsStrAlloc(reasons
[i
]);
4730 * End of "$Id: job.c 7902 2008-09-03 14:20:17Z mike $".