4 * Job management routines for the CUPS scheduler.
6 * Copyright 2007-2014 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 * Include necessary headers...
22 #include <cups/backend.h>
25 # include <IOKit/pwr_mgt/IOPMLib.h>
26 # ifdef HAVE_IOKIT_PWR_MGT_IOPMLIBPRIVATE_H
27 # include <IOKit/pwr_mgt/IOPMLibPrivate.h>
28 # endif /* HAVE_IOKIT_PWR_MGT_IOPMLIBPRIVATE_H */
29 #endif /* __APPLE__ */
33 * Design Notes for Job Management
34 * -------------------------------
38 * pending Do nothing/check jobs
39 * pending-held Send SIGTERM to filters and backend
40 * processing Do nothing/start job
41 * stopped Send SIGKILL to filters and backend
42 * canceled Send SIGTERM to filters and backend
46 * Finalize clears the printer <-> job association, deletes the status
47 * buffer, closes all of the pipes, etc. and doesn't get run until all of
48 * the print processes are finished.
50 * UNLOADING OF JOBS (cupsdUnloadCompletedJobs)
52 * We unload the job attributes when they are not needed to reduce overall
53 * memory consumption. We don't unload jobs where job->state_value <
54 * IPP_JOB_STOPPED, job->printer != NULL, or job->access_time is recent.
56 * STARTING OF JOBS (start_job)
58 * When a job is started, a status buffer, several pipes, a security
59 * profile, and a backend process are created for the life of that job.
60 * These are shared for every file in a job. For remote print jobs, the
61 * IPP backend is provided with every file in the job and no filters are
64 * The job->printer member tracks which printer is printing a job, which
65 * can be different than the destination in job->dest for classes. The
66 * printer object also has a job pointer to track which job is being
69 * PRINTING OF JOB FILES (cupsdContinueJob)
71 * Each file in a job is filtered by 0 or more programs. After getting the
72 * list of filters needed and the total cost, the job is either passed or
73 * put back to the processing state until the current FilterLevel comes down
74 * enough to allow printing.
76 * If we can print, we build a string for the print options and run each of
77 * the filters, piping the output from one into the next.
79 * JOB STATUS UPDATES (update_job)
81 * The update_job function gets called whenever there are pending messages
82 * on the status pipe. These generally are updates to the marker-*,
83 * printer-state-message, or printer-state-reasons attributes. On EOF,
84 * finalize_job is called to clean up.
86 * FINALIZING JOBS (finalize_job)
88 * When all filters and the backend are done, we set the job state to
89 * completed (no errors), aborted (filter errors or abort-job policy),
90 * pending-held (auth required or retry-job policy), or pending
91 * (retry-current-job or stop-printer policies) as appropriate.
93 * Then we close the pipes and free the status buffers and profiles.
95 * JOB FILE COMPLETION (process_children in main.c)
97 * For multiple-file jobs, process_children (in main.c) sees that all
98 * filters have exited and calls in to print the next file if there are
99 * more files in the job, otherwise it waits for the backend to exit and
100 * update_job to do the cleanup.
108 static mime_filter_t gziptoany_filter
=
110 NULL
, /* Source type */
111 NULL
, /* Destination type */
113 "gziptoany" /* Filter program to run */
121 static int compare_active_jobs(void *first
, void *second
, void *data
);
122 static int compare_jobs(void *first
, void *second
, void *data
);
123 static void dump_job_history(cupsd_job_t
*job
);
124 static void finalize_job(cupsd_job_t
*job
, int set_job_state
);
125 static void free_job_history(cupsd_job_t
*job
);
126 static char *get_options(cupsd_job_t
*job
, int banner_page
, char *copies
,
127 size_t copies_size
, char *title
,
129 static size_t ipp_length(ipp_t
*ipp
);
130 static void load_job_cache(const char *filename
);
131 static void load_next_job_id(const char *filename
);
132 static void load_request_root(void);
133 static void remove_job_files(cupsd_job_t
*job
);
134 static void remove_job_history(cupsd_job_t
*job
);
135 static void set_time(cupsd_job_t
*job
, const char *name
);
136 static void start_job(cupsd_job_t
*job
, cupsd_printer_t
*printer
);
137 static void stop_job(cupsd_job_t
*job
, cupsd_jobaction_t action
);
138 static void unload_job(cupsd_job_t
*job
);
139 static void update_job(cupsd_job_t
*job
);
140 static void update_job_attrs(cupsd_job_t
*job
, int do_message
);
144 * 'cupsdAddJob()' - Add a new job to the job queue.
147 cupsd_job_t
* /* O - New job record */
148 cupsdAddJob(int priority
, /* I - Job priority */
149 const char *dest
) /* I - Job destination */
151 cupsd_job_t
*job
; /* New job record */
154 if ((job
= calloc(sizeof(cupsd_job_t
), 1)) == NULL
)
157 job
->id
= NextJobId
++;
158 job
->priority
= priority
;
159 job
->back_pipes
[0] = -1;
160 job
->back_pipes
[1] = -1;
161 job
->print_pipes
[0] = -1;
162 job
->print_pipes
[1] = -1;
163 job
->side_pipes
[0] = -1;
164 job
->side_pipes
[1] = -1;
165 job
->status_pipes
[0] = -1;
166 job
->status_pipes
[1] = -1;
168 cupsdSetString(&job
->dest
, dest
);
171 * Add the new job to the "all jobs" and "active jobs" lists...
174 cupsArrayAdd(Jobs
, job
);
175 cupsArrayAdd(ActiveJobs
, job
);
182 * 'cupsdCancelJobs()' - Cancel all jobs for the given destination/user.
186 cupsdCancelJobs(const char *dest
, /* I - Destination to cancel */
187 const char *username
, /* I - Username or NULL */
188 int purge
) /* I - Purge jobs? */
190 cupsd_job_t
*job
; /* Current job */
193 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
195 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
197 if ((!job
->dest
|| !job
->username
) && !cupsdLoadJob(job
))
200 if ((!dest
|| !strcmp(job
->dest
, dest
)) &&
201 (!username
|| !strcmp(job
->username
, username
)))
204 * Cancel all jobs matching this destination/user...
208 cupsdSetJobState(job
, IPP_JOB_CANCELED
, CUPSD_JOB_PURGE
,
209 "Job purged by user.");
210 else if (job
->state_value
< IPP_JOB_CANCELED
)
211 cupsdSetJobState(job
, IPP_JOB_CANCELED
, CUPSD_JOB_DEFAULT
,
212 "Job canceled by user.");
221 * 'cupsdCheckJobs()' - Check the pending jobs and start any if the destination
228 cupsd_job_t
*job
; /* Current job in queue */
229 cupsd_printer_t
*printer
, /* Printer destination */
230 *pclass
; /* Printer class destination */
231 ipp_attribute_t
*attr
; /* Job attribute */
232 time_t curtime
; /* Current time */
235 curtime
= time(NULL
);
237 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
238 "cupsdCheckJobs: %d active jobs, sleeping=%d, reload=%d, "
239 "curtime=%ld", cupsArrayCount(ActiveJobs
), Sleeping
,
240 NeedReload
, (long)curtime
);
242 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
244 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
246 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
247 "cupsdCheckJobs: Job %d - dest=\"%s\", printer=%p, "
248 "state=%d, cancel_time=%ld, hold_until=%ld, kill_time=%ld, "
249 "pending_cost=%d, pending_timeout=%ld", job
->id
, job
->dest
,
250 job
->printer
, job
->state_value
, (long)job
->cancel_time
,
251 (long)job
->hold_until
, (long)job
->kill_time
,
252 job
->pending_cost
, (long)job
->pending_timeout
);
255 * Kill jobs if they are unresponsive...
258 if (job
->kill_time
&& job
->kill_time
<= curtime
)
261 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Stopping unresponsive job.");
263 stop_job(job
, CUPSD_JOB_FORCE
);
268 * Cancel stuck jobs...
271 if (job
->cancel_time
&& job
->cancel_time
<= curtime
)
273 int cancel_after
; /* job-cancel-after value */
275 attr
= ippFindAttribute(job
->attrs
, "job-cancel-after", IPP_TAG_INTEGER
);
276 cancel_after
= attr
? ippGetInteger(attr
, 0) : MaxJobTime
;
279 cupsdSetJobState(job
, IPP_JOB_CANCELED
, CUPSD_JOB_FORCE
, "Marking stuck job as completed after %d seconds.", cancel_after
);
281 cupsdSetJobState(job
, IPP_JOB_CANCELED
, CUPSD_JOB_DEFAULT
, "Canceling stuck job after %d seconds.", cancel_after
);
286 * Start held jobs if they are ready...
289 if (job
->state_value
== IPP_JOB_HELD
&&
291 job
->hold_until
< curtime
)
293 if (job
->pending_timeout
)
296 * This job is pending; check that we don't have an active Send-Document
297 * operation in progress on any of the client connections, then timeout
298 * the job so we can start printing...
301 cupsd_client_t
*con
; /* Current client connection */
303 for (con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
305 con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
307 con
->request
->request
.op
.operation_id
== IPP_SEND_DOCUMENT
)
313 if (cupsdTimeoutJob(job
))
317 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
318 "Job submission timed out.");
322 * Continue jobs that are waiting on the FilterLimit...
325 if (job
->pending_cost
> 0 &&
326 ((FilterLevel
+ job
->pending_cost
) < FilterLimit
|| FilterLevel
== 0))
327 cupsdContinueJob(job
);
330 * Start pending jobs if the destination is available...
333 if (job
->state_value
== IPP_JOB_PENDING
&& !NeedReload
&&
334 !Sleeping
&& !DoingShutdown
&& !job
->printer
)
336 printer
= cupsdFindDest(job
->dest
);
339 while (printer
&& (printer
->type
& CUPS_PRINTER_CLASS
))
342 * If the class is remote, just pass it to the remote server...
347 if (pclass
->state
== IPP_PRINTER_STOPPED
)
349 else if (pclass
->type
& CUPS_PRINTER_REMOTE
)
352 printer
= cupsdFindAvailablePrinter(printer
->name
);
355 if (!printer
&& !pclass
)
358 * Whoa, the printer and/or class for this destination went away;
362 cupsdSetJobState(job
, IPP_JOB_ABORTED
, CUPSD_JOB_PURGE
,
363 "Job aborted because the destination printer/class "
366 else if (printer
&& !printer
->holding_new_jobs
)
369 * See if the printer is available or remote and not printing a job;
370 * if so, start the job...
376 * Add/update a job-actual-printer-uri attribute for this job
377 * so that we know which printer actually printed the job...
380 if ((attr
= ippFindAttribute(job
->attrs
, "job-actual-printer-uri",
381 IPP_TAG_URI
)) != NULL
)
382 cupsdSetString(&attr
->values
[0].string
.text
, printer
->uri
);
384 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
,
385 "job-actual-printer-uri", NULL
, printer
->uri
);
388 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
391 if (!printer
->job
&& printer
->state
== IPP_PRINTER_IDLE
)
397 start_job(job
, printer
);
406 * 'cupsdCleanJobs()' - Clean out old jobs.
412 cupsd_job_t
*job
; /* Current job */
413 time_t curtime
; /* Current time */
416 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
417 "cupsdCleanJobs: MaxJobs=%d, JobHistory=%d, JobFiles=%d",
418 MaxJobs
, JobHistory
, JobFiles
);
420 if (MaxJobs
<= 0 && JobHistory
== INT_MAX
&& JobFiles
== INT_MAX
)
423 curtime
= time(NULL
);
424 JobHistoryUpdate
= 0;
426 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
428 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
430 if (job
->state_value
>= IPP_JOB_CANCELED
&& !job
->printer
)
433 * Expire old jobs (or job files)...
436 if ((MaxJobs
> 0 && cupsArrayCount(Jobs
) >= MaxJobs
) ||
437 (job
->history_time
&& job
->history_time
<= curtime
))
439 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Removing from history.");
440 cupsdDeleteJob(job
, CUPSD_JOB_PURGE
);
442 else if (job
->file_time
&& job
->file_time
<= curtime
)
444 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Removing document files.");
445 remove_job_files(job
);
447 if (job
->history_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
448 JobHistoryUpdate
= job
->history_time
;
452 if (job
->history_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
453 JobHistoryUpdate
= job
->history_time
;
455 if (job
->file_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
456 JobHistoryUpdate
= job
->file_time
;
461 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdCleanJobs: JobHistoryUpdate=%ld",
462 (long)JobHistoryUpdate
);
467 * 'cupsdContinueJob()' - Continue printing with the next file in a job.
471 cupsdContinueJob(cupsd_job_t
*job
) /* I - Job */
473 int i
; /* Looping var */
474 int slot
; /* Pipe slot */
475 cups_array_t
*filters
= NULL
,/* Filters for job */
476 *prefilters
; /* Filters with prefilters */
477 mime_filter_t
*filter
, /* Current filter */
478 *prefilter
, /* Prefilter */
479 port_monitor
; /* Port monitor filter */
480 char scheme
[255]; /* Device URI scheme */
481 ipp_attribute_t
*attr
; /* Current attribute */
482 const char *ptr
, /* Pointer into value */
483 *abort_message
; /* Abort message */
484 ipp_jstate_t abort_state
= IPP_JOB_STOPPED
;
485 /* New job state on abort */
486 struct stat backinfo
; /* Backend file information */
487 int backroot
; /* Run backend as root? */
488 int pid
; /* Process ID of new filter process */
489 int banner_page
; /* 1 if banner page, 0 otherwise */
490 int filterfds
[2][2] = { { -1, -1 }, { -1, -1 } };
491 /* Pipes used between filters */
492 int envc
; /* Number of environment variables */
493 struct stat fileinfo
; /* Job file information */
494 int argc
; /* Number of arguments */
495 char **argv
= NULL
, /* Filter command-line arguments */
496 filename
[1024], /* Job filename */
497 command
[1024], /* Full path to command */
498 jobid
[255], /* Job ID string */
500 /* Job title string */
501 copies
[255], /* # copies string */
502 *options
, /* Options string */
504 /* Environment variables */
505 charset
[255], /* CHARSET env variable */
506 class_name
[255],/* CLASS env variable */
507 classification
[1024],
508 /* CLASSIFICATION env variable */
510 /* CONTENT_TYPE env variable */
512 /* DEVICE_URI env variable */
513 final_content_type
[1024] = "",
514 /* FINAL_CONTENT_TYPE env variable */
515 lang
[255], /* LANG env variable */
518 /* APPLE_LANGUAGE env variable */
519 #endif /* __APPLE__ */
520 auth_info_required
[255],
521 /* AUTH_INFO_REQUIRED env variable */
522 ppd
[1024], /* PPD env variable */
524 /* PRINTER_INFO env variable */
525 printer_location
[255],
526 /* PRINTER_LOCATION env variable */
528 /* PRINTER env variable */
529 *printer_state_reasons
= NULL
,
530 /* PRINTER_STATE_REASONS env var */
532 /* RIP_MAX_CACHE env variable */
535 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
536 "cupsdContinueJob(job=%p(%d)): current_file=%d, num_files=%d",
537 job
, job
->id
, job
->current_file
, job
->num_files
);
540 * Figure out what filters are required to convert from
541 * the source to the destination type...
544 FilterLevel
-= job
->cost
;
547 job
->pending_cost
= 0;
549 memset(job
->filters
, 0, sizeof(job
->filters
));
551 if (job
->printer
->raw
)
554 * Remote jobs and raw queues go directly to the printer without
558 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Sending job to queue tagged as raw...");
563 * Local jobs get filtered...
566 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
567 job
->id
, job
->current_file
+ 1);
568 if (stat(filename
, &fileinfo
))
569 fileinfo
.st_size
= 0;
571 filters
= mimeFilter2(MimeDatabase
, job
->filetypes
[job
->current_file
], (size_t)fileinfo
.st_size
, job
->printer
->filetype
, &(job
->cost
));
575 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
576 "Unable to convert file %d to printable format.",
579 abort_message
= "Aborting job because it cannot be printed.";
580 abort_state
= IPP_JOB_ABORTED
;
582 ippSetString(job
->attrs
, &job
->reasons
, 0, "document-unprintable-error");
587 * Figure out the final content type...
590 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "%d filters for job:",
591 cupsArrayCount(filters
));
592 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
594 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
595 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "%s (%s/%s to %s/%s, cost %d)",
597 filter
->src
? filter
->src
->super
: "???",
598 filter
->src
? filter
->src
->type
: "???",
599 filter
->dst
? filter
->dst
->super
: "???",
600 filter
->dst
? filter
->dst
->type
: "???",
603 if (!job
->printer
->remote
)
605 for (filter
= (mime_filter_t
*)cupsArrayLast(filters
);
606 filter
&& filter
->dst
;
607 filter
= (mime_filter_t
*)cupsArrayPrev(filters
))
608 if (strcmp(filter
->dst
->super
, "printer") ||
609 strcmp(filter
->dst
->type
, job
->printer
->name
))
612 if (filter
&& filter
->dst
)
614 if ((ptr
= strchr(filter
->dst
->type
, '/')) != NULL
)
615 snprintf(final_content_type
, sizeof(final_content_type
),
616 "FINAL_CONTENT_TYPE=%s", ptr
+ 1);
618 snprintf(final_content_type
, sizeof(final_content_type
),
619 "FINAL_CONTENT_TYPE=%s/%s", filter
->dst
->super
,
623 snprintf(final_content_type
, sizeof(final_content_type
),
624 "FINAL_CONTENT_TYPE=printer/%s", job
->printer
->name
);
628 * Remove NULL ("-") filters...
631 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
633 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
634 if (!strcmp(filter
->filter
, "-"))
635 cupsArrayRemove(filters
, filter
);
637 if (cupsArrayCount(filters
) == 0)
639 cupsArrayDelete(filters
);
644 * If this printer has any pre-filters, insert the required pre-filter
645 * in the filters array...
648 if (job
->printer
->prefiltertype
&& filters
)
650 prefilters
= cupsArrayNew(NULL
, NULL
);
652 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
654 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
656 if ((prefilter
= mimeFilterLookup(MimeDatabase
, filter
->src
,
657 job
->printer
->prefiltertype
)))
659 cupsArrayAdd(prefilters
, prefilter
);
660 job
->cost
+= prefilter
->cost
;
663 cupsArrayAdd(prefilters
, filter
);
666 cupsArrayDelete(filters
);
667 filters
= prefilters
;
672 * Set a minimum cost of 100 for all jobs so that FilterLimit
673 * works with raw queues and other low-cost paths.
680 * See if the filter cost is too high...
683 if ((FilterLevel
+ job
->cost
) > FilterLimit
&& FilterLevel
> 0 &&
687 * Don't print this job quite yet...
690 cupsArrayDelete(filters
);
692 cupsdLogJob(job
, CUPSD_LOG_INFO
,
693 "Holding because filter limit has been reached.");
694 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
695 "cupsdContinueJob: file=%d, cost=%d, level=%d, limit=%d",
696 job
->current_file
, job
->cost
, FilterLevel
,
699 job
->pending_cost
= job
->cost
;
704 FilterLevel
+= job
->cost
;
707 * Add decompression/raw filter as needed...
710 if (job
->compressions
[job
->current_file
] &&
711 (!job
->printer
->remote
|| job
->num_files
== 1))
714 * Add gziptoany filter to the front of the list...
718 filters
= cupsArrayNew(NULL
, NULL
);
720 if (!cupsArrayInsert(filters
, &gziptoany_filter
))
722 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
723 "Unable to add decompression filter - %s", strerror(errno
));
725 cupsArrayDelete(filters
);
727 abort_message
= "Stopping job because the scheduler ran out of memory.";
734 * Add port monitor, if any...
737 if (job
->printer
->port_monitor
)
740 * Add port monitor to the end of the list...
744 filters
= cupsArrayNew(NULL
, NULL
);
746 port_monitor
.src
= NULL
;
747 port_monitor
.dst
= NULL
;
748 port_monitor
.cost
= 0;
750 snprintf(port_monitor
.filter
, sizeof(port_monitor
.filter
),
751 "%s/monitor/%s", ServerBin
, job
->printer
->port_monitor
);
753 if (!cupsArrayAdd(filters
, &port_monitor
))
755 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
756 "Unable to add port monitor - %s", strerror(errno
));
758 abort_message
= "Stopping job because the scheduler ran out of memory.";
765 * Make sure we don't go over the "MAX_FILTERS" limit...
768 if (cupsArrayCount(filters
) > MAX_FILTERS
)
770 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
771 "Too many filters (%d > %d), unable to print.",
772 cupsArrayCount(filters
), MAX_FILTERS
);
774 abort_message
= "Aborting job because it needs too many filters to print.";
775 abort_state
= IPP_JOB_ABORTED
;
777 ippSetString(job
->attrs
, &job
->reasons
, 0, "document-unprintable-error");
783 * Determine if we are printing a banner page or not...
786 if (job
->job_sheets
== NULL
)
788 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "No job-sheets attribute.");
789 if ((job
->job_sheets
=
790 ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_ZERO
)) != NULL
)
791 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
792 "... but someone added one without setting job_sheets.");
794 else if (job
->job_sheets
->num_values
== 1)
795 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "job-sheets=%s",
796 job
->job_sheets
->values
[0].string
.text
);
798 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "job-sheets=%s,%s",
799 job
->job_sheets
->values
[0].string
.text
,
800 job
->job_sheets
->values
[1].string
.text
);
802 if (job
->printer
->type
& CUPS_PRINTER_REMOTE
)
804 else if (job
->job_sheets
== NULL
)
806 else if (_cups_strcasecmp(job
->job_sheets
->values
[0].string
.text
, "none") != 0 &&
807 job
->current_file
== 0)
809 else if (job
->job_sheets
->num_values
> 1 &&
810 _cups_strcasecmp(job
->job_sheets
->values
[1].string
.text
, "none") != 0 &&
811 job
->current_file
== (job
->num_files
- 1))
816 if ((options
= get_options(job
, banner_page
, copies
, sizeof(copies
), title
,
817 sizeof(title
))) == NULL
)
819 abort_message
= "Stopping job because the scheduler ran out of memory.";
825 * Build the command-line arguments for the filters. Each filter
826 * has 6 or 7 arguments:
834 * argv[6] = filename (optional; normally stdin)
836 * This allows legacy printer drivers that use the old System V
837 * printing interface to be used by CUPS.
839 * For remote jobs, we send all of the files in the argument list.
842 if (job
->printer
->remote
)
843 argc
= 6 + job
->num_files
;
847 if ((argv
= calloc((size_t)argc
+ 1, sizeof(char *))) == NULL
)
849 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Unable to allocate argument array - %s",
852 abort_message
= "Stopping job because the scheduler ran out of memory.";
857 sprintf(jobid
, "%d", job
->id
);
859 argv
[0] = job
->printer
->name
;
861 argv
[2] = job
->username
;
866 if (job
->printer
->remote
&& job
->num_files
> 1)
868 for (i
= 0; i
< job
->num_files
; i
++)
870 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
872 argv
[6 + i
] = strdup(filename
);
877 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
878 job
->id
, job
->current_file
+ 1);
879 argv
[6] = strdup(filename
);
882 for (i
= 0; argv
[i
]; i
++)
883 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "argv[%d]=\"%s\"", i
, argv
[i
]);
886 * Create environment variable strings for the filters...
889 attr
= ippFindAttribute(job
->attrs
, "attributes-natural-language",
893 strlcpy(apple_language
, "APPLE_LANGUAGE=", sizeof(apple_language
));
894 _cupsAppleLanguage(attr
->values
[0].string
.text
,
895 apple_language
+ 15, sizeof(apple_language
) - 15);
896 #endif /* __APPLE__ */
898 switch (strlen(attr
->values
[0].string
.text
))
902 * This is an unknown or badly formatted language code; use
903 * the POSIX locale...
906 strlcpy(lang
, "LANG=C", sizeof(lang
));
911 * Just the language code (ll)...
914 snprintf(lang
, sizeof(lang
), "LANG=%s.UTF-8",
915 attr
->values
[0].string
.text
);
920 * Language and country code (ll-cc)...
923 snprintf(lang
, sizeof(lang
), "LANG=%c%c_%c%c.UTF-8",
924 attr
->values
[0].string
.text
[0],
925 attr
->values
[0].string
.text
[1],
926 toupper(attr
->values
[0].string
.text
[3] & 255),
927 toupper(attr
->values
[0].string
.text
[4] & 255));
931 if ((attr
= ippFindAttribute(job
->attrs
, "document-format",
932 IPP_TAG_MIMETYPE
)) != NULL
&&
933 (ptr
= strstr(attr
->values
[0].string
.text
, "charset=")) != NULL
)
934 snprintf(charset
, sizeof(charset
), "CHARSET=%s", ptr
+ 8);
936 strlcpy(charset
, "CHARSET=utf-8", sizeof(charset
));
938 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s/%s",
939 job
->filetypes
[job
->current_file
]->super
,
940 job
->filetypes
[job
->current_file
]->type
);
941 snprintf(device_uri
, sizeof(device_uri
), "DEVICE_URI=%s",
942 job
->printer
->device_uri
);
943 snprintf(ppd
, sizeof(ppd
), "PPD=%s/ppd/%s.ppd", ServerRoot
,
945 snprintf(printer_info
, sizeof(printer_name
), "PRINTER_INFO=%s",
946 job
->printer
->info
? job
->printer
->info
: "");
947 snprintf(printer_location
, sizeof(printer_name
), "PRINTER_LOCATION=%s",
948 job
->printer
->location
? job
->printer
->location
: "");
949 snprintf(printer_name
, sizeof(printer_name
), "PRINTER=%s", job
->printer
->name
);
950 if (job
->printer
->num_reasons
> 0)
952 char *psrptr
; /* Pointer into PRINTER_STATE_REASONS */
953 size_t psrlen
; /* Size of PRINTER_STATE_REASONS */
955 for (psrlen
= 22, i
= 0; i
< job
->printer
->num_reasons
; i
++)
956 psrlen
+= strlen(job
->printer
->reasons
[i
]) + 1;
958 if ((printer_state_reasons
= malloc(psrlen
)) != NULL
)
961 * All of these strcpy's are safe because we allocated the psr string...
964 strlcpy(printer_state_reasons
, "PRINTER_STATE_REASONS=", psrlen
);
965 for (psrptr
= printer_state_reasons
+ 22, i
= 0;
966 i
< job
->printer
->num_reasons
;
971 strlcpy(psrptr
, job
->printer
->reasons
[i
], psrlen
- (size_t)(psrptr
- printer_state_reasons
));
972 psrptr
+= strlen(psrptr
);
976 snprintf(rip_max_cache
, sizeof(rip_max_cache
), "RIP_MAX_CACHE=%s", RIPCache
);
978 if (job
->printer
->num_auth_info_required
== 1)
979 snprintf(auth_info_required
, sizeof(auth_info_required
),
980 "AUTH_INFO_REQUIRED=%s",
981 job
->printer
->auth_info_required
[0]);
982 else if (job
->printer
->num_auth_info_required
== 2)
983 snprintf(auth_info_required
, sizeof(auth_info_required
),
984 "AUTH_INFO_REQUIRED=%s,%s",
985 job
->printer
->auth_info_required
[0],
986 job
->printer
->auth_info_required
[1]);
987 else if (job
->printer
->num_auth_info_required
== 3)
988 snprintf(auth_info_required
, sizeof(auth_info_required
),
989 "AUTH_INFO_REQUIRED=%s,%s,%s",
990 job
->printer
->auth_info_required
[0],
991 job
->printer
->auth_info_required
[1],
992 job
->printer
->auth_info_required
[2]);
993 else if (job
->printer
->num_auth_info_required
== 4)
994 snprintf(auth_info_required
, sizeof(auth_info_required
),
995 "AUTH_INFO_REQUIRED=%s,%s,%s,%s",
996 job
->printer
->auth_info_required
[0],
997 job
->printer
->auth_info_required
[1],
998 job
->printer
->auth_info_required
[2],
999 job
->printer
->auth_info_required
[3]);
1001 strlcpy(auth_info_required
, "AUTH_INFO_REQUIRED=none",
1002 sizeof(auth_info_required
));
1004 envc
= cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
1006 envp
[envc
++] = charset
;
1007 envp
[envc
++] = lang
;
1009 envp
[envc
++] = apple_language
;
1010 #endif /* __APPLE__ */
1011 envp
[envc
++] = ppd
;
1012 envp
[envc
++] = rip_max_cache
;
1013 envp
[envc
++] = content_type
;
1014 envp
[envc
++] = device_uri
;
1015 envp
[envc
++] = printer_info
;
1016 envp
[envc
++] = printer_location
;
1017 envp
[envc
++] = printer_name
;
1018 envp
[envc
++] = printer_state_reasons
? printer_state_reasons
:
1019 "PRINTER_STATE_REASONS=none";
1020 envp
[envc
++] = banner_page
? "CUPS_FILETYPE=job-sheet" :
1021 "CUPS_FILETYPE=document";
1023 if (final_content_type
[0])
1024 envp
[envc
++] = final_content_type
;
1026 if (Classification
&& !banner_page
)
1028 if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
1029 IPP_TAG_NAME
)) == NULL
)
1030 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
1032 else if (attr
->num_values
> 1 &&
1033 strcmp(attr
->values
[1].string
.text
, "none") != 0)
1034 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
1035 attr
->values
[1].string
.text
);
1037 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
1038 attr
->values
[0].string
.text
);
1040 envp
[envc
++] = classification
;
1043 if (job
->dtype
& CUPS_PRINTER_CLASS
)
1045 snprintf(class_name
, sizeof(class_name
), "CLASS=%s", job
->dest
);
1046 envp
[envc
++] = class_name
;
1049 envp
[envc
++] = auth_info_required
;
1052 i
< (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0]));
1054 if (job
->auth_env
[i
])
1055 envp
[envc
++] = job
->auth_env
[i
];
1060 envp
[envc
++] = job
->auth_uid
;
1064 for (i
= 0; i
< envc
; i
++)
1065 if (!strncmp(envp
[i
], "AUTH_", 5))
1066 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"AUTH_%c****\"", i
,
1068 else if (strncmp(envp
[i
], "DEVICE_URI=", 11))
1069 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"%s\"", i
, envp
[i
]);
1071 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"DEVICE_URI=%s\"", i
,
1072 job
->printer
->sanitized_device_uri
);
1074 if (job
->printer
->remote
)
1075 job
->current_file
= job
->num_files
;
1077 job
->current_file
++;
1080 * Now create processes for all of the filters...
1083 for (i
= 0, slot
= 0, filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
1085 i
++, filter
= (mime_filter_t
*)cupsArrayNext(filters
))
1087 if (filter
->filter
[0] != '/')
1088 snprintf(command
, sizeof(command
), "%s/filter/%s", ServerBin
,
1091 strlcpy(command
, filter
->filter
, sizeof(command
));
1093 if (i
< (cupsArrayCount(filters
) - 1))
1095 if (cupsdOpenPipe(filterfds
[slot
]))
1097 abort_message
= "Stopping job because the scheduler could not create "
1098 "the filter pipes.";
1105 if (job
->current_file
== 1 ||
1106 (job
->printer
->pc
&& job
->printer
->pc
->single_file
))
1108 if (strncmp(job
->printer
->device_uri
, "file:", 5) != 0)
1110 if (cupsdOpenPipe(job
->print_pipes
))
1112 abort_message
= "Stopping job because the scheduler could not "
1113 "create the backend pipes.";
1120 job
->print_pipes
[0] = -1;
1121 if (!strcmp(job
->printer
->device_uri
, "file:/dev/null") ||
1122 !strcmp(job
->printer
->device_uri
, "file:///dev/null"))
1123 job
->print_pipes
[1] = -1;
1126 if (!strncmp(job
->printer
->device_uri
, "file:/dev/", 10))
1127 job
->print_pipes
[1] = open(job
->printer
->device_uri
+ 5,
1129 else if (!strncmp(job
->printer
->device_uri
, "file:///dev/", 12))
1130 job
->print_pipes
[1] = open(job
->printer
->device_uri
+ 7,
1132 else if (!strncmp(job
->printer
->device_uri
, "file:///", 8))
1133 job
->print_pipes
[1] = open(job
->printer
->device_uri
+ 7,
1134 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
1136 job
->print_pipes
[1] = open(job
->printer
->device_uri
+ 5,
1137 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
1139 if (job
->print_pipes
[1] < 0)
1141 abort_message
= "Stopping job because the scheduler could not "
1142 "open the output file.";
1147 fcntl(job
->print_pipes
[1], F_SETFD
,
1148 fcntl(job
->print_pipes
[1], F_GETFD
) | FD_CLOEXEC
);
1153 filterfds
[slot
][0] = job
->print_pipes
[0];
1154 filterfds
[slot
][1] = job
->print_pipes
[1];
1157 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
1158 filterfds
[slot
][1], job
->status_pipes
[1],
1159 job
->back_pipes
[0], job
->side_pipes
[0], 0,
1160 job
->profile
, job
, job
->filters
+ i
);
1162 cupsdClosePipe(filterfds
[!slot
]);
1166 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Unable to start filter \"%s\" - %s.",
1167 filter
->filter
, strerror(errno
));
1169 abort_message
= "Stopping job because the scheduler could not execute a "
1175 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Started filter %s (PID %d)", command
,
1187 cupsArrayDelete(filters
);
1191 * Finally, pipe the final output into a backend process if needed...
1194 if (strncmp(job
->printer
->device_uri
, "file:", 5) != 0)
1196 if (job
->current_file
== 1 || job
->printer
->remote
||
1197 (job
->printer
->pc
&& job
->printer
->pc
->single_file
))
1199 sscanf(job
->printer
->device_uri
, "%254[^:]", scheme
);
1200 snprintf(command
, sizeof(command
), "%s/backend/%s", ServerBin
, scheme
);
1203 * See if the backend needs to run as root...
1208 else if (stat(command
, &backinfo
))
1211 backroot
= !(backinfo
.st_mode
& (S_IRWXG
| S_IRWXO
));
1213 argv
[0] = job
->printer
->sanitized_device_uri
;
1215 filterfds
[slot
][0] = -1;
1216 filterfds
[slot
][1] = -1;
1218 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
1219 filterfds
[slot
][1], job
->status_pipes
[1],
1220 job
->back_pipes
[1], job
->side_pipes
[1],
1221 backroot
, job
->bprofile
, job
, &(job
->backend
));
1225 abort_message
= "Stopping job because the sheduler could not execute "
1232 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Started backend %s (PID %d)",
1237 if (job
->current_file
== job
->num_files
||
1238 (job
->printer
->pc
&& job
->printer
->pc
->single_file
))
1239 cupsdClosePipe(job
->print_pipes
);
1241 if (job
->current_file
== job
->num_files
)
1243 cupsdClosePipe(job
->back_pipes
);
1244 cupsdClosePipe(job
->side_pipes
);
1246 close(job
->status_pipes
[1]);
1247 job
->status_pipes
[1] = -1;
1252 filterfds
[slot
][0] = -1;
1253 filterfds
[slot
][1] = -1;
1255 if (job
->current_file
== job
->num_files
||
1256 (job
->printer
->pc
&& job
->printer
->pc
->single_file
))
1257 cupsdClosePipe(job
->print_pipes
);
1259 if (job
->current_file
== job
->num_files
)
1261 close(job
->status_pipes
[1]);
1262 job
->status_pipes
[1] = -1;
1266 cupsdClosePipe(filterfds
[slot
]);
1268 for (i
= 6; i
< argc
; i
++)
1274 if (printer_state_reasons
)
1275 free(printer_state_reasons
);
1277 cupsdAddSelect(job
->status_buffer
->fd
, (cupsd_selfunc_t
)update_job
, NULL
,
1280 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
, "Job #%d started.",
1287 * If we get here, we need to abort the current job and close out all
1288 * files and pipes...
1293 FilterLevel
-= job
->cost
;
1296 for (slot
= 0; slot
< 2; slot
++)
1297 cupsdClosePipe(filterfds
[slot
]);
1299 cupsArrayDelete(filters
);
1303 for (i
= 6; i
< argc
; i
++)
1308 if (printer_state_reasons
)
1309 free(printer_state_reasons
);
1311 cupsdClosePipe(job
->print_pipes
);
1312 cupsdClosePipe(job
->back_pipes
);
1313 cupsdClosePipe(job
->side_pipes
);
1315 cupsdRemoveSelect(job
->status_pipes
[0]);
1316 cupsdClosePipe(job
->status_pipes
);
1317 cupsdStatBufDelete(job
->status_buffer
);
1318 job
->status_buffer
= NULL
;
1321 * Update the printer and job state.
1324 cupsdSetJobState(job
, abort_state
, CUPSD_JOB_DEFAULT
, "%s", abort_message
);
1325 cupsdSetPrinterState(job
->printer
, IPP_PRINTER_IDLE
, 0);
1326 update_job_attrs(job
, 0);
1329 free_job_history(job
);
1331 cupsArrayRemove(PrintingJobs
, job
);
1334 * Clear the printer <-> job association...
1337 job
->printer
->job
= NULL
;
1338 job
->printer
= NULL
;
1343 * 'cupsdDeleteJob()' - Free all memory used by a job.
1347 cupsdDeleteJob(cupsd_job_t
*job
, /* I - Job */
1348 cupsd_jobaction_t action
)/* I - Action */
1350 int i
; /* Looping var */
1354 finalize_job(job
, 1);
1356 if (action
== CUPSD_JOB_PURGE
)
1357 remove_job_history(job
);
1359 cupsdClearString(&job
->username
);
1360 cupsdClearString(&job
->dest
);
1362 i
< (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0]));
1364 cupsdClearString(job
->auth_env
+ i
);
1365 cupsdClearString(&job
->auth_uid
);
1367 if (action
== CUPSD_JOB_PURGE
)
1368 remove_job_files(job
);
1369 else if (job
->num_files
> 0)
1371 free(job
->compressions
);
1372 free(job
->filetypes
);
1378 free_job_history(job
);
1382 cupsArrayRemove(Jobs
, job
);
1383 cupsArrayRemove(ActiveJobs
, job
);
1384 cupsArrayRemove(PrintingJobs
, job
);
1391 * 'cupsdFreeAllJobs()' - Free all jobs from memory.
1395 cupsdFreeAllJobs(void)
1397 cupsd_job_t
*job
; /* Current job */
1405 cupsdStopAllJobs(CUPSD_JOB_FORCE
, 0);
1408 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1410 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1411 cupsdDeleteJob(job
, CUPSD_JOB_DEFAULT
);
1413 cupsdReleaseSignals();
1418 * 'cupsdFindJob()' - Find the specified job.
1421 cupsd_job_t
* /* O - Job data */
1422 cupsdFindJob(int id
) /* I - Job ID */
1424 cupsd_job_t key
; /* Search key */
1429 return ((cupsd_job_t
*)cupsArrayFind(Jobs
, &key
));
1434 * 'cupsdGetPrinterJobCount()' - Get the number of pending, processing,
1435 * or held jobs in a printer or class.
1438 int /* O - Job count */
1439 cupsdGetPrinterJobCount(
1440 const char *dest
) /* I - Printer or class name */
1442 int count
; /* Job count */
1443 cupsd_job_t
*job
; /* Current job */
1446 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
1448 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
1449 if (job
->dest
&& !_cups_strcasecmp(job
->dest
, dest
))
1457 * 'cupsdGetUserJobCount()' - Get the number of pending, processing,
1458 * or held jobs for a user.
1461 int /* O - Job count */
1462 cupsdGetUserJobCount(
1463 const char *username
) /* I - Username */
1465 int count
; /* Job count */
1466 cupsd_job_t
*job
; /* Current job */
1469 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
1471 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
1472 if (!_cups_strcasecmp(job
->username
, username
))
1480 * 'cupsdLoadAllJobs()' - Load all jobs from disk.
1484 cupsdLoadAllJobs(void)
1486 char filename
[1024]; /* Full filename of job.cache file */
1487 struct stat fileinfo
, /* Information on job.cache file */
1488 dirinfo
; /* Information on RequestRoot dir */
1493 * Create the job arrays as needed...
1497 Jobs
= cupsArrayNew(compare_jobs
, NULL
);
1500 ActiveJobs
= cupsArrayNew(compare_active_jobs
, NULL
);
1503 PrintingJobs
= cupsArrayNew(compare_jobs
, NULL
);
1506 * See whether the job.cache file is older than the RequestRoot directory...
1509 snprintf(filename
, sizeof(filename
), "%s/job.cache", CacheDir
);
1511 if (stat(filename
, &fileinfo
))
1513 fileinfo
.st_mtime
= 0;
1515 if (errno
!= ENOENT
)
1516 cupsdLogMessage(CUPSD_LOG_ERROR
,
1517 "Unable to get file information for \"%s\" - %s",
1518 filename
, strerror(errno
));
1521 if (stat(RequestRoot
, &dirinfo
))
1523 dirinfo
.st_mtime
= 0;
1525 if (errno
!= ENOENT
)
1526 cupsdLogMessage(CUPSD_LOG_ERROR
,
1527 "Unable to get directory information for \"%s\" - %s",
1528 RequestRoot
, strerror(errno
));
1532 * Load the most recent source for job data...
1535 if (dirinfo
.st_mtime
> fileinfo
.st_mtime
)
1537 load_request_root();
1539 load_next_job_id(filename
);
1542 load_job_cache(filename
);
1545 * Clean out old jobs as needed...
1548 if (MaxJobs
> 0 && cupsArrayCount(Jobs
) >= MaxJobs
)
1554 * 'cupsdLoadJob()' - Load a single job.
1557 int /* O - 1 on success, 0 on failure */
1558 cupsdLoadJob(cupsd_job_t
*job
) /* I - Job */
1560 int i
; /* Looping var */
1561 char jobfile
[1024]; /* Job filename */
1562 cups_file_t
*fp
; /* Job file */
1563 int fileid
; /* Current file ID */
1564 ipp_attribute_t
*attr
; /* Job attribute */
1565 const char *dest
; /* Destination name */
1566 cupsd_printer_t
*destptr
; /* Pointer to destination */
1567 mime_type_t
**filetypes
; /* New filetypes array */
1568 int *compressions
; /* New compressions array */
1573 if (job
->state_value
> IPP_JOB_STOPPED
)
1574 job
->access_time
= time(NULL
);
1579 if ((job
->attrs
= ippNew()) == NULL
)
1581 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Ran out of memory for job attributes.");
1586 * Load job attributes...
1589 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Loading attributes...");
1591 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, job
->id
);
1592 if ((fp
= cupsdOpenConfFile(jobfile
)) == NULL
)
1595 if (ippReadIO(fp
, (ipp_iocb_t
)cupsFileRead
, 1, NULL
, job
->attrs
) != IPP_DATA
)
1597 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1598 "Unable to read job control file \"%s\".", jobfile
);
1606 * Copy attribute data to the job object...
1609 if (!ippFindAttribute(job
->attrs
, "time-at-creation", IPP_TAG_INTEGER
))
1611 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1612 "Missing or bad time-at-creation attribute in control file.");
1616 if ((job
->state
= ippFindAttribute(job
->attrs
, "job-state",
1617 IPP_TAG_ENUM
)) == NULL
)
1619 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1620 "Missing or bad job-state attribute in control file.");
1624 job
->state_value
= (ipp_jstate_t
)job
->state
->values
[0].integer
;
1626 job
->history_time
= 0;
1628 if (job
->state_value
>= IPP_JOB_CANCELED
&&
1629 (attr
= ippFindAttribute(job
->attrs
, "time-at-completed",
1630 IPP_TAG_INTEGER
)) != NULL
)
1632 if (JobHistory
< INT_MAX
)
1633 job
->history_time
= attr
->values
[0].integer
+ JobHistory
;
1635 job
->history_time
= INT_MAX
;
1637 if (job
->history_time
< time(NULL
))
1638 goto error
; /* Expired, remove from history */
1640 if (job
->history_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
1641 JobHistoryUpdate
= job
->history_time
;
1643 if (JobFiles
< INT_MAX
)
1644 job
->file_time
= attr
->values
[0].integer
+ JobFiles
;
1646 job
->file_time
= INT_MAX
;
1648 if (job
->file_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
1649 JobHistoryUpdate
= job
->file_time
;
1651 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdLoadJob: JobHistoryUpdate=%ld",
1652 (long)JobHistoryUpdate
);
1657 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1658 IPP_TAG_URI
)) == NULL
)
1660 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1661 "No job-printer-uri attribute in control file.");
1665 if ((dest
= cupsdValidateDest(attr
->values
[0].string
.text
, &(job
->dtype
),
1668 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1669 "Unable to queue job for destination \"%s\".",
1670 attr
->values
[0].string
.text
);
1674 cupsdSetString(&job
->dest
, dest
);
1676 else if ((destptr
= cupsdFindDest(job
->dest
)) == NULL
)
1678 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1679 "Unable to queue job for destination \"%s\".",
1684 if ((job
->reasons
= ippFindAttribute(job
->attrs
, "job-state-reasons",
1685 IPP_TAG_KEYWORD
)) == NULL
)
1687 const char *reason
; /* job-state-reason keyword */
1689 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
1690 "Adding missing job-state-reasons attribute to control file.");
1692 switch (job
->state_value
)
1695 case IPP_JOB_PENDING
:
1696 if (destptr
->state
== IPP_PRINTER_STOPPED
)
1697 reason
= "printer-stopped";
1703 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1704 IPP_TAG_ZERO
)) != NULL
&&
1705 (attr
->value_tag
== IPP_TAG_NAME
||
1706 attr
->value_tag
== IPP_TAG_NAMELANG
||
1707 attr
->value_tag
== IPP_TAG_KEYWORD
) &&
1708 strcmp(attr
->values
[0].string
.text
, "no-hold"))
1709 reason
= "job-hold-until-specified";
1711 reason
= "job-incoming";
1714 case IPP_JOB_PROCESSING
:
1715 reason
= "job-printing";
1718 case IPP_JOB_STOPPED
:
1719 reason
= "job-stopped";
1722 case IPP_JOB_CANCELED
:
1723 reason
= "job-canceled-by-user";
1726 case IPP_JOB_ABORTED
:
1727 reason
= "aborted-by-system";
1730 case IPP_JOB_COMPLETED
:
1731 reason
= "job-completed-successfully";
1735 job
->reasons
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1736 "job-state-reasons", NULL
, reason
);
1738 else if (job
->state_value
== IPP_JOB_PENDING
)
1740 if (destptr
->state
== IPP_PRINTER_STOPPED
)
1741 ippSetString(job
->attrs
, &job
->reasons
, 0, "printer-stopped");
1743 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
1746 job
->sheets
= ippFindAttribute(job
->attrs
, "job-media-sheets-completed",
1748 job
->job_sheets
= ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_NAME
);
1752 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
1753 IPP_TAG_INTEGER
)) == NULL
)
1755 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1756 "Missing or bad job-priority attribute in control file.");
1760 job
->priority
= attr
->values
[0].integer
;
1765 if ((attr
= ippFindAttribute(job
->attrs
, "job-originating-user-name",
1766 IPP_TAG_NAME
)) == NULL
)
1768 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1769 "Missing or bad job-originating-user-name "
1770 "attribute in control file.");
1774 cupsdSetString(&job
->username
, attr
->values
[0].string
.text
);
1778 * Set the job hold-until time and state...
1781 if (job
->state_value
== IPP_JOB_HELD
)
1783 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1784 IPP_TAG_KEYWORD
)) == NULL
)
1785 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1788 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
, CUPSD_JOB_DEFAULT
);
1791 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1792 job
->state_value
= IPP_JOB_PENDING
;
1795 else if (job
->state_value
== IPP_JOB_PROCESSING
)
1797 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1798 job
->state_value
= IPP_JOB_PENDING
;
1801 if (!job
->num_files
)
1804 * Find all the d##### files...
1807 for (fileid
= 1; fileid
< 10000; fileid
++)
1809 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
1812 if (access(jobfile
, 0))
1815 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
1816 "Auto-typing document file \"%s\"...", jobfile
);
1818 if (fileid
> job
->num_files
)
1820 if (job
->num_files
== 0)
1822 compressions
= (int *)calloc((size_t)fileid
, sizeof(int));
1823 filetypes
= (mime_type_t
**)calloc((size_t)fileid
, sizeof(mime_type_t
*));
1827 compressions
= (int *)realloc(job
->compressions
, sizeof(int) * (size_t)fileid
);
1828 filetypes
= (mime_type_t
**)realloc(job
->filetypes
, sizeof(mime_type_t
*) * (size_t)fileid
);
1832 job
->compressions
= compressions
;
1835 job
->filetypes
= filetypes
;
1837 if (!compressions
|| !filetypes
)
1839 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1840 "Ran out of memory for job file types.");
1842 ippDelete(job
->attrs
);
1845 if (job
->compressions
)
1847 free(job
->compressions
);
1848 job
->compressions
= NULL
;
1853 free(job
->filetypes
);
1854 job
->filetypes
= NULL
;
1861 job
->num_files
= fileid
;
1864 job
->filetypes
[fileid
- 1] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
1865 job
->compressions
+ fileid
- 1);
1867 if (!job
->filetypes
[fileid
- 1])
1868 job
->filetypes
[fileid
- 1] = mimeType(MimeDatabase
, "application",
1874 * Load authentication information as needed...
1877 if (job
->state_value
< IPP_JOB_STOPPED
)
1879 snprintf(jobfile
, sizeof(jobfile
), "%s/a%05d", RequestRoot
, job
->id
);
1882 i
< (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0]));
1884 cupsdClearString(job
->auth_env
+ i
);
1885 cupsdClearString(&job
->auth_uid
);
1887 if ((fp
= cupsFileOpen(jobfile
, "r")) != NULL
)
1889 int bytes
, /* Size of auth data */
1890 linenum
= 1; /* Current line number */
1891 char line
[65536], /* Line from file */
1892 *value
, /* Value from line */
1893 data
[65536]; /* Decoded data */
1896 if (cupsFileGets(fp
, line
, sizeof(line
)) &&
1897 !strcmp(line
, "CUPSD-AUTH-V3"))
1900 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
1906 if (strcmp(line
, "negotiate") && strcmp(line
, "uid"))
1908 bytes
= sizeof(data
);
1909 httpDecode64_2(data
, &bytes
, value
);
1913 * Assign environment variables...
1916 if (!strcmp(line
, "uid"))
1918 cupsdSetStringf(&job
->auth_uid
, "AUTH_UID=%s", value
);
1921 else if (i
>= (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0])))
1924 if (!strcmp(line
, "username"))
1925 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_USERNAME=%s", data
);
1926 else if (!strcmp(line
, "domain"))
1927 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_DOMAIN=%s", data
);
1928 else if (!strcmp(line
, "password"))
1929 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_PASSWORD=%s", data
);
1930 else if (!strcmp(line
, "negotiate"))
1931 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_NEGOTIATE=%s", value
);
1943 job
->access_time
= time(NULL
);
1947 * If we get here then something bad happened...
1952 ippDelete(job
->attrs
);
1955 remove_job_history(job
);
1956 remove_job_files(job
);
1963 * 'cupsdMoveJob()' - Move the specified job to a different destination.
1967 cupsdMoveJob(cupsd_job_t
*job
, /* I - Job */
1968 cupsd_printer_t
*p
) /* I - Destination printer or class */
1970 ipp_attribute_t
*attr
; /* job-printer-uri attribute */
1971 const char *olddest
; /* Old destination */
1972 cupsd_printer_t
*oldp
; /* Old pointer */
1976 * Don't move completed jobs...
1979 if (job
->state_value
> IPP_JOB_STOPPED
)
1983 * Get the old destination...
1986 olddest
= job
->dest
;
1989 oldp
= job
->printer
;
1991 oldp
= cupsdFindDest(olddest
);
1994 * Change the destination information...
1997 if (job
->state_value
> IPP_JOB_HELD
)
1998 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
1999 "Stopping job prior to move.");
2001 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, oldp
, job
,
2002 "Job #%d moved from %s to %s.", job
->id
, olddest
,
2005 cupsdSetString(&job
->dest
, p
->name
);
2006 job
->dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_REMOTE
);
2008 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
2009 IPP_TAG_URI
)) != NULL
)
2010 cupsdSetString(&(attr
->values
[0].string
.text
), p
->uri
);
2012 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, p
, job
,
2013 "Job #%d moved from %s to %s.", job
->id
, olddest
,
2017 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2022 * 'cupsdReleaseJob()' - Release the specified job.
2026 cupsdReleaseJob(cupsd_job_t
*job
) /* I - Job */
2028 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdReleaseJob(job=%p(%d))", job
,
2031 if (job
->state_value
== IPP_JOB_HELD
)
2034 * Add trailing banner as needed...
2037 if (job
->pending_timeout
)
2038 cupsdTimeoutJob(job
);
2040 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
2041 "Job released by user.");
2047 * 'cupsdRestartJob()' - Restart the specified job.
2051 cupsdRestartJob(cupsd_job_t
*job
) /* I - Job */
2053 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdRestartJob(job=%p(%d))", job
,
2056 if (job
->state_value
== IPP_JOB_STOPPED
|| job
->num_files
)
2057 cupsdSetJobState(job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
2058 "Job restarted by user.");
2063 * 'cupsdSaveAllJobs()' - Save a summary of all jobs to disk.
2067 cupsdSaveAllJobs(void)
2069 int i
; /* Looping var */
2070 cups_file_t
*fp
; /* job.cache file */
2071 char filename
[1024], /* job.cache filename */
2072 temp
[1024]; /* Temporary string */
2073 cupsd_job_t
*job
; /* Current job */
2074 time_t curtime
; /* Current time */
2075 struct tm
*curdate
; /* Current date */
2078 snprintf(filename
, sizeof(filename
), "%s/job.cache", CacheDir
);
2079 if ((fp
= cupsdCreateConfFile(filename
, ConfigFilePerm
)) == NULL
)
2082 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving job.cache...");
2085 * Write a small header to the file...
2088 curtime
= time(NULL
);
2089 curdate
= localtime(&curtime
);
2090 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
2092 cupsFilePuts(fp
, "# Job cache file for " CUPS_SVERSION
"\n");
2093 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
2094 cupsFilePrintf(fp
, "NextJobId %d\n", NextJobId
);
2097 * Write each job known to the system...
2100 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
2102 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
2104 cupsFilePrintf(fp
, "<Job %d>\n", job
->id
);
2105 cupsFilePrintf(fp
, "State %d\n", job
->state_value
);
2106 cupsFilePrintf(fp
, "Priority %d\n", job
->priority
);
2107 cupsFilePrintf(fp
, "HoldUntil %d\n", (int)job
->hold_until
);
2108 cupsFilePrintf(fp
, "Username %s\n", job
->username
);
2109 cupsFilePrintf(fp
, "Destination %s\n", job
->dest
);
2110 cupsFilePrintf(fp
, "DestType %d\n", job
->dtype
);
2111 cupsFilePrintf(fp
, "NumFiles %d\n", job
->num_files
);
2112 for (i
= 0; i
< job
->num_files
; i
++)
2113 cupsFilePrintf(fp
, "File %d %s/%s %d\n", i
+ 1, job
->filetypes
[i
]->super
,
2114 job
->filetypes
[i
]->type
, job
->compressions
[i
]);
2115 cupsFilePuts(fp
, "</Job>\n");
2118 cupsdCloseCreatedConfFile(fp
, filename
);
2123 * 'cupsdSaveJob()' - Save a job to disk.
2127 cupsdSaveJob(cupsd_job_t
*job
) /* I - Job */
2129 char filename
[1024]; /* Job control filename */
2130 cups_file_t
*fp
; /* Job file */
2133 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSaveJob(job=%p(%d)): job->attrs=%p",
2134 job
, job
->id
, job
->attrs
);
2136 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
, job
->id
);
2138 if ((fp
= cupsdCreateConfFile(filename
, ConfigFilePerm
& 0600)) == NULL
)
2141 fchown(cupsFileNumber(fp
), RunUser
, Group
);
2143 job
->attrs
->state
= IPP_IDLE
;
2145 if (ippWriteIO(fp
, (ipp_iocb_t
)cupsFileWrite
, 1, NULL
,
2146 job
->attrs
) != IPP_DATA
)
2148 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Unable to write job control file.");
2153 if (!cupsdCloseCreatedConfFile(fp
, filename
))
2156 * Remove backup file and mark this job as clean...
2159 strlcat(filename
, ".O", sizeof(filename
));
2168 * 'cupsdSetJobHoldUntil()' - Set the hold time for a job.
2172 cupsdSetJobHoldUntil(cupsd_job_t
*job
, /* I - Job */
2173 const char *when
, /* I - When to resume */
2174 int update
)/* I - Update job-hold-until attr? */
2176 time_t curtime
; /* Current time */
2177 struct tm
*curdate
; /* Current date */
2178 int hour
; /* Hold hour */
2179 int minute
; /* Hold minute */
2180 int second
= 0; /* Hold second */
2183 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2184 "cupsdSetJobHoldUntil(job=%p(%d), when=\"%s\", update=%d)",
2185 job
, job
->id
, when
, update
);
2190 * Update the job-hold-until attribute...
2193 ipp_attribute_t
*attr
; /* job-hold-until attribute */
2195 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2196 IPP_TAG_KEYWORD
)) == NULL
)
2197 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2200 cupsdSetString(&(attr
->values
[0].string
.text
), when
);
2202 attr
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
2203 "job-hold-until", NULL
, when
);
2207 if (isdigit(when
[0] & 255))
2208 attr
->value_tag
= IPP_TAG_NAME
;
2210 attr
->value_tag
= IPP_TAG_KEYWORD
;
2213 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2216 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-hold-until-specified");
2220 * Update the hold time...
2223 job
->cancel_time
= 0;
2225 if (!strcmp(when
, "indefinite") || !strcmp(when
, "auth-info-required"))
2228 * Hold indefinitely...
2231 job
->hold_until
= 0;
2233 if (MaxHoldTime
> 0)
2234 job
->cancel_time
= time(NULL
) + MaxHoldTime
;
2236 else if (!strcmp(when
, "day-time"))
2239 * Hold to 6am the next morning unless local time is < 6pm.
2242 curtime
= time(NULL
);
2243 curdate
= localtime(&curtime
);
2245 if (curdate
->tm_hour
< 18)
2246 job
->hold_until
= curtime
;
2248 job
->hold_until
= curtime
+
2249 ((29 - curdate
->tm_hour
) * 60 + 59 -
2250 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2252 else if (!strcmp(when
, "evening") || !strcmp(when
, "night"))
2255 * Hold to 6pm unless local time is > 6pm or < 6am.
2258 curtime
= time(NULL
);
2259 curdate
= localtime(&curtime
);
2261 if (curdate
->tm_hour
< 6 || curdate
->tm_hour
>= 18)
2262 job
->hold_until
= curtime
;
2264 job
->hold_until
= curtime
+
2265 ((17 - curdate
->tm_hour
) * 60 + 59 -
2266 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2268 else if (!strcmp(when
, "second-shift"))
2271 * Hold to 4pm unless local time is > 4pm.
2274 curtime
= time(NULL
);
2275 curdate
= localtime(&curtime
);
2277 if (curdate
->tm_hour
>= 16)
2278 job
->hold_until
= curtime
;
2280 job
->hold_until
= curtime
+
2281 ((15 - curdate
->tm_hour
) * 60 + 59 -
2282 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2284 else if (!strcmp(when
, "third-shift"))
2287 * Hold to 12am unless local time is < 8am.
2290 curtime
= time(NULL
);
2291 curdate
= localtime(&curtime
);
2293 if (curdate
->tm_hour
< 8)
2294 job
->hold_until
= curtime
;
2296 job
->hold_until
= curtime
+
2297 ((23 - curdate
->tm_hour
) * 60 + 59 -
2298 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2300 else if (!strcmp(when
, "weekend"))
2303 * Hold to weekend unless we are in the weekend.
2306 curtime
= time(NULL
);
2307 curdate
= localtime(&curtime
);
2309 if (curdate
->tm_wday
== 0 || curdate
->tm_wday
== 6)
2310 job
->hold_until
= curtime
;
2312 job
->hold_until
= curtime
+
2313 (((5 - curdate
->tm_wday
) * 24 +
2314 (17 - curdate
->tm_hour
)) * 60 + 59 -
2315 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
2317 else if (sscanf(when
, "%d:%d:%d", &hour
, &minute
, &second
) >= 2)
2320 * Hold to specified GMT time (HH:MM or HH:MM:SS)...
2323 curtime
= time(NULL
);
2324 curdate
= gmtime(&curtime
);
2326 job
->hold_until
= curtime
+
2327 ((hour
- curdate
->tm_hour
) * 60 + minute
-
2328 curdate
->tm_min
) * 60 + second
- curdate
->tm_sec
;
2331 * Hold until next day as needed...
2334 if (job
->hold_until
< curtime
)
2335 job
->hold_until
+= 24 * 60 * 60;
2338 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetJobHoldUntil: hold_until=%d",
2339 (int)job
->hold_until
);
2344 * 'cupsdSetJobPriority()' - Set the priority of a job, moving it up/down in
2345 * the list as needed.
2349 cupsdSetJobPriority(
2350 cupsd_job_t
*job
, /* I - Job ID */
2351 int priority
) /* I - New priority (0 to 100) */
2353 ipp_attribute_t
*attr
; /* Job attribute */
2357 * Don't change completed jobs...
2360 if (job
->state_value
>= IPP_JOB_PROCESSING
)
2364 * Set the new priority and re-add the job into the active list...
2367 cupsArrayRemove(ActiveJobs
, job
);
2369 job
->priority
= priority
;
2371 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
2372 IPP_TAG_INTEGER
)) != NULL
)
2373 attr
->values
[0].integer
= priority
;
2375 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-priority",
2378 cupsArrayAdd(ActiveJobs
, job
);
2381 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2386 * 'cupsdSetJobState()' - Set the state of the specified print job.
2391 cupsd_job_t
*job
, /* I - Job to cancel */
2392 ipp_jstate_t newstate
, /* I - New job state */
2393 cupsd_jobaction_t action
, /* I - Action to take */
2394 const char *message
, /* I - Message to log */
2395 ...) /* I - Additional arguments as needed */
2397 int i
; /* Looping var */
2398 ipp_jstate_t oldstate
; /* Old state */
2399 char filename
[1024]; /* Job filename */
2400 ipp_attribute_t
*attr
; /* Job attribute */
2403 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2404 "cupsdSetJobState(job=%p(%d), state=%d, newstate=%d, "
2405 "action=%d, message=\"%s\")", job
, job
->id
, job
->state_value
,
2406 newstate
, action
, message
? message
: "(null)");
2410 * Make sure we have the job attributes...
2413 if (!cupsdLoadJob(job
))
2417 * Don't do anything if the state is unchanged and we aren't purging the
2421 oldstate
= job
->state_value
;
2422 if (newstate
== oldstate
&& action
!= CUPSD_JOB_PURGE
)
2426 * Stop any processes that are working on the current job...
2429 if (oldstate
== IPP_JOB_PROCESSING
)
2430 stop_job(job
, action
);
2433 * Set the new job state...
2436 job
->state_value
= newstate
;
2439 job
->state
->values
[0].integer
= newstate
;
2443 case IPP_JOB_PENDING
:
2445 * Update job-hold-until as needed...
2448 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2449 IPP_TAG_KEYWORD
)) == NULL
)
2450 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2454 attr
->value_tag
= IPP_TAG_KEYWORD
;
2455 cupsdSetString(&(attr
->values
[0].string
.text
), "no-hold");
2461 case IPP_JOB_ABORTED
:
2462 case IPP_JOB_CANCELED
:
2463 case IPP_JOB_COMPLETED
:
2464 set_time(job
, "time-at-completed");
2465 ippSetString(job
->attrs
, &job
->reasons
, 0, "processing-to-stop-point");
2470 * Log message as needed...
2475 char buffer
[2048]; /* Message buffer */
2476 va_list ap
; /* Pointer to additional arguments */
2478 va_start(ap
, message
);
2479 vsnprintf(buffer
, sizeof(buffer
), message
, ap
);
2482 if (newstate
> IPP_JOB_STOPPED
)
2483 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
, "%s", buffer
);
2485 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
, "%s", buffer
);
2487 if (newstate
== IPP_JOB_STOPPED
|| newstate
== IPP_JOB_ABORTED
)
2488 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "%s", buffer
);
2490 cupsdLogJob(job
, CUPSD_LOG_INFO
, "%s", buffer
);
2494 * Handle post-state-change actions...
2499 case IPP_JOB_PROCESSING
:
2501 * Add the job to the "printing" list...
2504 if (!cupsArrayFind(PrintingJobs
, job
))
2505 cupsArrayAdd(PrintingJobs
, job
);
2508 * Set the processing time...
2511 set_time(job
, "time-at-processing");
2513 case IPP_JOB_PENDING
:
2515 case IPP_JOB_STOPPED
:
2517 * Make sure the job is in the active list...
2520 if (!cupsArrayFind(ActiveJobs
, job
))
2521 cupsArrayAdd(ActiveJobs
, job
);
2524 * Save the job state to disk...
2528 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2531 case IPP_JOB_ABORTED
:
2532 case IPP_JOB_CANCELED
:
2533 case IPP_JOB_COMPLETED
:
2534 if (newstate
== IPP_JOB_CANCELED
)
2537 * Remove the job from the active list if there are no processes still
2541 for (i
= 0; job
->filters
[i
] < 0; i
++);
2543 if (!job
->filters
[i
] && job
->backend
<= 0)
2544 cupsArrayRemove(ActiveJobs
, job
);
2549 * Otherwise just remove the job from the active list immediately...
2552 cupsArrayRemove(ActiveJobs
, job
);
2556 * Expire job subscriptions since the job is now "completed"...
2559 cupsdExpireSubscriptions(NULL
, job
);
2563 * If we are going to sleep and the PrintingJobs count is now 0, allow the
2564 * sleep to happen immediately...
2567 if (Sleeping
&& cupsArrayCount(PrintingJobs
) == 0)
2569 #endif /* __APPLE__ */
2572 * Remove any authentication data...
2575 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
2576 if (cupsdRemoveFile(filename
) && errno
!= ENOENT
)
2577 cupsdLogMessage(CUPSD_LOG_ERROR
,
2578 "Unable to remove authentication cache: %s",
2582 i
< (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0]));
2584 cupsdClearString(job
->auth_env
+ i
);
2586 cupsdClearString(&job
->auth_uid
);
2589 * Remove the print file for good if we aren't preserving jobs or
2593 if (!JobHistory
|| !JobFiles
|| action
== CUPSD_JOB_PURGE
)
2594 remove_job_files(job
);
2596 if (JobHistory
&& action
!= CUPSD_JOB_PURGE
)
2599 * Save job state info...
2603 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2605 else if (!job
->printer
)
2608 * Delete the job immediately if not actively printing...
2611 cupsdDeleteJob(job
, CUPSD_JOB_PURGE
);
2618 * Finalize the job immediately if we forced things...
2621 if (action
>= CUPSD_JOB_FORCE
&& job
&& job
->printer
)
2622 finalize_job(job
, 0);
2625 * Update the server "busy" state...
2628 cupsdSetBusyState();
2633 * 'cupsdStopAllJobs()' - Stop all print jobs.
2638 cupsd_jobaction_t action
, /* I - Action */
2639 int kill_delay
) /* I - Number of seconds before we kill */
2641 cupsd_job_t
*job
; /* Current job */
2644 DEBUG_puts("cupsdStopAllJobs()");
2646 for (job
= (cupsd_job_t
*)cupsArrayFirst(PrintingJobs
);
2648 job
= (cupsd_job_t
*)cupsArrayNext(PrintingJobs
))
2652 cupsdSetJobState(job
, IPP_JOB_COMPLETED
, CUPSD_JOB_FORCE
, NULL
);
2657 job
->kill_time
= time(NULL
) + kill_delay
;
2659 cupsdSetJobState(job
, IPP_JOB_PENDING
, action
, NULL
);
2666 * 'cupsdUnloadCompletedJobs()' - Flush completed job history from memory.
2670 cupsdUnloadCompletedJobs(void)
2672 cupsd_job_t
*job
; /* Current job */
2673 time_t expire
; /* Expiration time */
2676 expire
= time(NULL
) - 60;
2678 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
2680 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
2681 if (job
->attrs
&& job
->state_value
>= IPP_JOB_STOPPED
&& !job
->printer
&&
2682 job
->access_time
< expire
)
2693 * 'cupsdUpdateJobs()' - Update the history/file files for all jobs.
2697 cupsdUpdateJobs(void)
2699 cupsd_job_t
*job
; /* Current job */
2700 time_t curtime
; /* Current time */
2701 ipp_attribute_t
*attr
; /* time-at-completed attribute */
2704 curtime
= time(NULL
);
2705 JobHistoryUpdate
= 0;
2707 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
2709 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
2711 if (job
->state_value
>= IPP_JOB_CANCELED
&&
2712 (attr
= ippFindAttribute(job
->attrs
, "time-at-completed",
2713 IPP_TAG_INTEGER
)) != NULL
)
2716 * Update history/file expiration times...
2719 if (JobHistory
< INT_MAX
)
2720 job
->history_time
= attr
->values
[0].integer
+ JobHistory
;
2722 job
->history_time
= INT_MAX
;
2724 if (job
->history_time
< curtime
)
2726 cupsdDeleteJob(job
, CUPSD_JOB_PURGE
);
2730 if (job
->history_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
2731 JobHistoryUpdate
= job
->history_time
;
2733 if (JobFiles
< INT_MAX
)
2734 job
->file_time
= attr
->values
[0].integer
+ JobFiles
;
2736 job
->file_time
= INT_MAX
;
2738 if (job
->file_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
2739 JobHistoryUpdate
= job
->file_time
;
2743 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdUpdateAllJobs: JobHistoryUpdate=%ld",
2744 (long)JobHistoryUpdate
);
2749 * 'compare_active_jobs()' - Compare the job IDs and priorities of two jobs.
2752 static int /* O - Difference */
2753 compare_active_jobs(void *first
, /* I - First job */
2754 void *second
, /* I - Second job */
2755 void *data
) /* I - App data (not used) */
2757 int diff
; /* Difference */
2762 if ((diff
= ((cupsd_job_t
*)second
)->priority
-
2763 ((cupsd_job_t
*)first
)->priority
) != 0)
2766 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
2771 * 'compare_jobs()' - Compare the job IDs of two jobs.
2774 static int /* O - Difference */
2775 compare_jobs(void *first
, /* I - First job */
2776 void *second
, /* I - Second job */
2777 void *data
) /* I - App data (not used) */
2781 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
2786 * 'dump_job_history()' - Dump any debug messages for a job.
2790 dump_job_history(cupsd_job_t
*job
) /* I - Job */
2792 int i
, /* Looping var */
2793 oldsize
; /* Current MaxLogSize */
2794 struct tm
*date
; /* Date/time value */
2795 cupsd_joblog_t
*message
; /* Current message */
2796 char temp
[2048], /* Log message */
2797 *ptr
, /* Pointer into log message */
2798 start
[256], /* Start time */
2799 end
[256]; /* End time */
2800 cupsd_printer_t
*printer
; /* Printer for job */
2804 * See if we have anything to dump...
2811 * Disable log rotation temporarily...
2814 oldsize
= MaxLogSize
;
2818 * Copy the debug messages to the log...
2821 message
= (cupsd_joblog_t
*)cupsArrayFirst(job
->history
);
2822 date
= localtime(&(message
->time
));
2823 strftime(start
, sizeof(start
), "%X", date
);
2825 message
= (cupsd_joblog_t
*)cupsArrayLast(job
->history
);
2826 date
= localtime(&(message
->time
));
2827 strftime(end
, sizeof(end
), "%X", date
);
2829 snprintf(temp
, sizeof(temp
),
2830 "[Job %d] The following messages were recorded from %s to %s",
2831 job
->id
, start
, end
);
2832 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, temp
);
2834 for (message
= (cupsd_joblog_t
*)cupsArrayFirst(job
->history
);
2836 message
= (cupsd_joblog_t
*)cupsArrayNext(job
->history
))
2837 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, message
->message
);
2839 snprintf(temp
, sizeof(temp
), "[Job %d] End of messages", job
->id
);
2840 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, temp
);
2843 * Log the printer state values...
2846 if ((printer
= job
->printer
) == NULL
)
2847 printer
= cupsdFindDest(job
->dest
);
2851 snprintf(temp
, sizeof(temp
), "[Job %d] printer-state=%d(%s)", job
->id
,
2853 printer
->state
== IPP_PRINTER_IDLE
? "idle" :
2854 printer
->state
== IPP_PRINTER_PROCESSING
? "processing" :
2856 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, temp
);
2858 snprintf(temp
, sizeof(temp
), "[Job %d] printer-state-message=\"%s\"",
2859 job
->id
, printer
->state_message
);
2860 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, temp
);
2862 snprintf(temp
, sizeof(temp
), "[Job %d] printer-state-reasons=", job
->id
);
2863 ptr
= temp
+ strlen(temp
);
2864 if (printer
->num_reasons
== 0)
2865 strlcpy(ptr
, "none", sizeof(temp
) - (size_t)(ptr
- temp
));
2869 i
< printer
->num_reasons
&& ptr
< (temp
+ sizeof(temp
) - 2);
2875 strlcpy(ptr
, printer
->reasons
[i
], sizeof(temp
) - (size_t)(ptr
- temp
));
2879 cupsdWriteErrorLog(CUPSD_LOG_DEBUG
, temp
);
2883 * Restore log file rotation...
2886 MaxLogSize
= oldsize
;
2889 * Free all messages...
2892 free_job_history(job
);
2897 * 'free_job_history()' - Free any log history.
2901 free_job_history(cupsd_job_t
*job
) /* I - Job */
2903 char *message
; /* Current message */
2909 for (message
= (char *)cupsArrayFirst(job
->history
);
2911 message
= (char *)cupsArrayNext(job
->history
))
2914 cupsArrayDelete(job
->history
);
2915 job
->history
= NULL
;
2920 * 'finalize_job()' - Cleanup after job filter processes and support data.
2924 finalize_job(cupsd_job_t
*job
, /* I - Job */
2925 int set_job_state
) /* I - 1 = set the job state */
2927 ipp_pstate_t printer_state
; /* New printer state value */
2928 ipp_jstate_t job_state
; /* New job state value */
2929 const char *message
; /* Message for job state */
2930 char buffer
[1024]; /* Buffer for formatted messages */
2933 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "finalize_job(job=%p(%d))", job
, job
->id
);
2936 * Clear the "connecting-to-device" and "cups-waiting-for-job-completed"
2937 * reasons, which are only valid when a printer is processing, along with any
2938 * remote printing job state...
2941 cupsdSetPrinterReasons(job
->printer
, "-connecting-to-device,"
2942 "cups-waiting-for-job-completed,"
2943 "cups-remote-pending,"
2944 "cups-remote-pending-held,"
2945 "cups-remote-processing,"
2946 "cups-remote-stopped,"
2947 "cups-remote-canceled,"
2948 "cups-remote-aborted,"
2949 "cups-remote-completed");
2952 * Similarly, clear the "offline-report" reason for non-USB devices since we
2953 * rarely have current information for network devices...
2956 if (strncmp(job
->printer
->device_uri
, "usb:", 4) &&
2957 strncmp(job
->printer
->device_uri
, "ippusb:", 7))
2958 cupsdSetPrinterReasons(job
->printer
, "-offline-report");
2961 * Free the security profile...
2964 cupsdDestroyProfile(job
->profile
);
2965 job
->profile
= NULL
;
2966 cupsdDestroyProfile(job
->bprofile
);
2967 job
->bprofile
= NULL
;
2970 * Clear the unresponsive job watchdog timers...
2973 job
->cancel_time
= 0;
2977 * Close pipes and status buffer...
2980 cupsdClosePipe(job
->print_pipes
);
2981 cupsdClosePipe(job
->back_pipes
);
2982 cupsdClosePipe(job
->side_pipes
);
2984 cupsdRemoveSelect(job
->status_pipes
[0]);
2985 cupsdClosePipe(job
->status_pipes
);
2986 cupsdStatBufDelete(job
->status_buffer
);
2987 job
->status_buffer
= NULL
;
2990 * Process the exit status...
2993 if (job
->printer
->state
== IPP_PRINTER_PROCESSING
)
2994 printer_state
= IPP_PRINTER_IDLE
;
2996 printer_state
= job
->printer
->state
;
2998 switch (job_state
= job
->state_value
)
3000 case IPP_JOB_PENDING
:
3001 message
= "Job paused.";
3005 message
= "Job held.";
3009 case IPP_JOB_PROCESSING
:
3010 case IPP_JOB_COMPLETED
:
3011 job_state
= IPP_JOB_COMPLETED
;
3012 message
= "Job completed.";
3015 ippSetString(job
->attrs
, &job
->reasons
, 0,
3016 "job-completed-successfully");
3019 case IPP_JOB_STOPPED
:
3020 message
= "Job stopped.";
3022 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-stopped");
3025 case IPP_JOB_CANCELED
:
3026 message
= "Job canceled.";
3028 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-canceled-by-user");
3031 case IPP_JOB_ABORTED
:
3032 message
= "Job aborted.";
3036 if (job
->status
< 0)
3039 * Backend had errors...
3042 int exit_code
; /* Exit code from backend */
3045 * Convert the status to an exit code. Due to the way the W* macros are
3046 * implemented on MacOS X (bug?), we have to store the exit status in a
3047 * variable first and then convert...
3050 exit_code
= -job
->status
;
3051 if (WIFEXITED(exit_code
))
3052 exit_code
= WEXITSTATUS(exit_code
);
3055 ippSetString(job
->attrs
, &job
->reasons
, 0, "cups-backend-crashed");
3056 exit_code
= job
->status
;
3059 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Backend returned status %d (%s)",
3061 exit_code
== CUPS_BACKEND_FAILED
? "failed" :
3062 exit_code
== CUPS_BACKEND_AUTH_REQUIRED
?
3063 "authentication required" :
3064 exit_code
== CUPS_BACKEND_HOLD
? "hold job" :
3065 exit_code
== CUPS_BACKEND_STOP
? "stop printer" :
3066 exit_code
== CUPS_BACKEND_CANCEL
? "cancel job" :
3067 exit_code
== CUPS_BACKEND_RETRY
? "retry job later" :
3068 exit_code
== CUPS_BACKEND_RETRY_CURRENT
? "retry job immediately" :
3069 exit_code
< 0 ? "crashed" : "unknown");
3072 * Do what needs to be done...
3078 case CUPS_BACKEND_FAILED
:
3080 * Backend failure, use the error-policy to determine how to
3084 if (job
->dtype
& CUPS_PRINTER_CLASS
)
3087 * Queued on a class - mark the job as pending and we'll retry on
3088 * another printer...
3091 if (job_state
== IPP_JOB_COMPLETED
)
3093 job_state
= IPP_JOB_PENDING
;
3094 message
= "Retrying job on another printer.";
3096 ippSetString(job
->attrs
, &job
->reasons
, 0,
3097 "resources-are-not-ready");
3100 else if (!strcmp(job
->printer
->error_policy
, "retry-current-job"))
3103 * The error policy is "retry-current-job" - mark the job as pending
3104 * and we'll retry on the same printer...
3107 if (job_state
== IPP_JOB_COMPLETED
)
3109 job_state
= IPP_JOB_PENDING
;
3110 message
= "Retrying job on same printer.";
3112 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
3115 else if ((job
->printer
->type
& CUPS_PRINTER_FAX
) ||
3116 !strcmp(job
->printer
->error_policy
, "retry-job"))
3118 if (job_state
== IPP_JOB_COMPLETED
)
3121 * The job was queued on a fax or the error policy is "retry-job" -
3122 * hold the job if the number of retries is less than the
3123 * JobRetryLimit, otherwise abort the job.
3128 if (job
->tries
> JobRetryLimit
&& JobRetryLimit
> 0)
3134 snprintf(buffer
, sizeof(buffer
),
3135 "Job aborted after %d unsuccessful attempts.",
3137 job_state
= IPP_JOB_ABORTED
;
3140 ippSetString(job
->attrs
, &job
->reasons
, 0, "aborted-by-system");
3145 * Try again in N seconds...
3148 snprintf(buffer
, sizeof(buffer
),
3149 "Job held for %d seconds since it could not be sent.",
3152 job
->hold_until
= time(NULL
) + JobRetryInterval
;
3153 job_state
= IPP_JOB_HELD
;
3156 ippSetString(job
->attrs
, &job
->reasons
, 0,
3157 "resources-are-not-ready");
3161 else if (!strcmp(job
->printer
->error_policy
, "abort-job") &&
3162 job_state
== IPP_JOB_COMPLETED
)
3164 job_state
= IPP_JOB_ABORTED
;
3165 message
= "Job aborted due to backend errors; please consult "
3166 "the error_log file for details.";
3168 ippSetString(job
->attrs
, &job
->reasons
, 0, "aborted-by-system");
3170 else if (job
->state_value
== IPP_JOB_PROCESSING
)
3172 job_state
= IPP_JOB_PENDING
;
3173 printer_state
= IPP_PRINTER_STOPPED
;
3174 message
= "Printer stopped due to backend errors; please "
3175 "consult the error_log file for details.";
3177 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
3181 case CUPS_BACKEND_CANCEL
:
3186 if (job_state
== IPP_JOB_COMPLETED
)
3188 job_state
= IPP_JOB_CANCELED
;
3189 message
= "Job canceled at printer.";
3191 ippSetString(job
->attrs
, &job
->reasons
, 0, "canceled-at-device");
3195 case CUPS_BACKEND_HOLD
:
3196 if (job_state
== IPP_JOB_COMPLETED
)
3202 const char *reason
= ippGetString(job
->reasons
, 0, NULL
);
3204 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "job-state-reasons=\"%s\"",
3207 if (!reason
|| strncmp(reason
, "account-", 8))
3209 cupsdSetJobHoldUntil(job
, "indefinite", 1);
3211 ippSetString(job
->attrs
, &job
->reasons
, 0,
3212 "job-hold-until-specified");
3213 message
= "Job held indefinitely due to backend errors; please "
3214 "consult the error_log file for details.";
3216 else if (!strcmp(reason
, "account-info-needed"))
3218 cupsdSetJobHoldUntil(job
, "indefinite", 0);
3220 message
= "Job held indefinitely - account information is "
3223 else if (!strcmp(reason
, "account-closed"))
3225 cupsdSetJobHoldUntil(job
, "indefinite", 0);
3227 message
= "Job held indefinitely - account has been closed.";
3229 else if (!strcmp(reason
, "account-limit-reached"))
3231 cupsdSetJobHoldUntil(job
, "indefinite", 0);
3233 message
= "Job held indefinitely - account limit has been "
3238 cupsdSetJobHoldUntil(job
, "indefinite", 0);
3240 message
= "Job held indefinitely - account authorization failed.";
3243 job_state
= IPP_JOB_HELD
;
3247 case CUPS_BACKEND_STOP
:
3249 * Stop the printer...
3252 printer_state
= IPP_PRINTER_STOPPED
;
3253 message
= "Printer stopped due to backend errors; please "
3254 "consult the error_log file for details.";
3256 if (job_state
== IPP_JOB_COMPLETED
)
3258 job_state
= IPP_JOB_PENDING
;
3260 ippSetString(job
->attrs
, &job
->reasons
, 0,
3261 "resources-are-not-ready");
3265 case CUPS_BACKEND_AUTH_REQUIRED
:
3267 * Hold the job for authentication...
3270 if (job_state
== IPP_JOB_COMPLETED
)
3272 cupsdSetJobHoldUntil(job
, "auth-info-required", 1);
3274 job_state
= IPP_JOB_HELD
;
3275 message
= "Job held for authentication.";
3277 if (strncmp(job
->reasons
->values
[0].string
.text
, "account-", 8))
3278 ippSetString(job
->attrs
, &job
->reasons
, 0,
3279 "cups-held-for-authentication");
3283 case CUPS_BACKEND_RETRY
:
3284 if (job_state
== IPP_JOB_COMPLETED
)
3287 * Hold the job if the number of retries is less than the
3288 * JobRetryLimit, otherwise abort the job.
3293 if (job
->tries
> JobRetryLimit
&& JobRetryLimit
> 0)
3299 snprintf(buffer
, sizeof(buffer
),
3300 "Job aborted after %d unsuccessful attempts.",
3302 job_state
= IPP_JOB_ABORTED
;
3305 ippSetString(job
->attrs
, &job
->reasons
, 0, "aborted-by-system");
3310 * Try again in N seconds...
3313 snprintf(buffer
, sizeof(buffer
),
3314 "Job held for %d seconds since it could not be sent.",
3317 job
->hold_until
= time(NULL
) + JobRetryInterval
;
3318 job_state
= IPP_JOB_HELD
;
3321 ippSetString(job
->attrs
, &job
->reasons
, 0,
3322 "resources-are-not-ready");
3327 case CUPS_BACKEND_RETRY_CURRENT
:
3329 * Mark the job as pending and retry on the same printer...
3332 if (job_state
== IPP_JOB_COMPLETED
)
3334 job_state
= IPP_JOB_PENDING
;
3335 message
= "Retrying job on same printer.";
3337 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
3342 else if (job
->status
> 0)
3345 * Filter had errors; stop job...
3348 if (job_state
== IPP_JOB_COMPLETED
)
3350 job_state
= IPP_JOB_STOPPED
;
3351 message
= "Job stopped due to filter errors; please consult the "
3352 "error_log file for details.";
3354 if (WIFSIGNALED(job
->status
))
3355 ippSetString(job
->attrs
, &job
->reasons
, 0, "cups-filter-crashed");
3357 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-completed-with-errors");
3362 * Update the printer and job state.
3365 if (set_job_state
&& job_state
!= job
->state_value
)
3366 cupsdSetJobState(job
, job_state
, CUPSD_JOB_DEFAULT
, "%s", message
);
3368 cupsdSetPrinterState(job
->printer
, printer_state
,
3369 printer_state
== IPP_PRINTER_STOPPED
);
3370 update_job_attrs(job
, 0);
3375 (job
->state_value
== IPP_JOB_ABORTED
||
3376 job
->state_value
== IPP_JOB_STOPPED
))
3377 dump_job_history(job
);
3379 free_job_history(job
);
3382 cupsArrayRemove(PrintingJobs
, job
);
3385 * Apply any PPD updates...
3388 if (job
->num_keywords
)
3390 if (cupsdUpdatePrinterPPD(job
->printer
, job
->num_keywords
, job
->keywords
))
3391 cupsdSetPrinterAttrs(job
->printer
);
3393 cupsFreeOptions(job
->num_keywords
, job
->keywords
);
3395 job
->num_keywords
= 0;
3396 job
->keywords
= NULL
;
3400 * Clear the printer <-> job association...
3403 job
->printer
->job
= NULL
;
3404 job
->printer
= NULL
;
3407 * Try printing another job...
3410 if (printer_state
!= IPP_PRINTER_STOPPED
)
3416 * 'get_options()' - Get a string containing the job options.
3419 static char * /* O - Options string */
3420 get_options(cupsd_job_t
*job
, /* I - Job */
3421 int banner_page
, /* I - Printing a banner page? */
3422 char *copies
, /* I - Copies buffer */
3423 size_t copies_size
, /* I - Size of copies buffer */
3424 char *title
, /* I - Title buffer */
3425 size_t title_size
) /* I - Size of title buffer */
3427 int i
; /* Looping var */
3428 size_t newlength
; /* New option buffer length */
3429 char *optptr
, /* Pointer to options */
3430 *valptr
; /* Pointer in value string */
3431 ipp_attribute_t
*attr
; /* Current attribute */
3432 _ppd_cache_t
*pc
; /* PPD cache and mapping data */
3433 int num_pwgppds
; /* Number of PWG->PPD options */
3434 cups_option_t
*pwgppds
, /* PWG->PPD options */
3435 *pwgppd
, /* Current PWG->PPD option */
3436 *preset
; /* Current preset option */
3437 int print_color_mode
,
3438 /* Output mode (if any) */
3439 print_quality
; /* Print quality (if any) */
3440 const char *ppd
; /* PPD option choice */
3441 int exact
; /* Did we get an exact match? */
3442 static char *options
= NULL
;/* Full list of options */
3443 static size_t optlength
= 0; /* Length of option buffer */
3447 * Building the options string is harder than it needs to be, but for the
3448 * moment we need to pass strings for command-line args and not IPP attribute
3451 * First build an options array for any PWG->PPD mapped option/choice pairs.
3454 pc
= job
->printer
->pc
;
3459 !ippFindAttribute(job
->attrs
,
3460 "com.apple.print.DocumentTicket.PMSpoolFormat",
3462 !ippFindAttribute(job
->attrs
, "APPrinterPreset", IPP_TAG_ZERO
) &&
3463 (ippFindAttribute(job
->attrs
, "print-color-mode", IPP_TAG_ZERO
) ||
3464 ippFindAttribute(job
->attrs
, "print-quality", IPP_TAG_ZERO
)))
3467 * Map print-color-mode and print-quality to a preset...
3470 if ((attr
= ippFindAttribute(job
->attrs
, "print-color-mode",
3471 IPP_TAG_KEYWORD
)) != NULL
&&
3472 !strcmp(attr
->values
[0].string
.text
, "monochrome"))
3473 print_color_mode
= _PWG_PRINT_COLOR_MODE_MONOCHROME
;
3475 print_color_mode
= _PWG_PRINT_COLOR_MODE_COLOR
;
3477 if ((attr
= ippFindAttribute(job
->attrs
, "print-quality",
3478 IPP_TAG_ENUM
)) != NULL
&&
3479 attr
->values
[0].integer
>= IPP_QUALITY_DRAFT
&&
3480 attr
->values
[0].integer
<= IPP_QUALITY_HIGH
)
3481 print_quality
= attr
->values
[0].integer
- IPP_QUALITY_DRAFT
;
3483 print_quality
= _PWG_PRINT_QUALITY_NORMAL
;
3485 if (pc
->num_presets
[print_color_mode
][print_quality
] == 0)
3488 * Try to find a preset that works so that we maximize the chances of us
3489 * getting a good print using IPP attributes.
3492 if (pc
->num_presets
[print_color_mode
][_PWG_PRINT_QUALITY_NORMAL
] > 0)
3493 print_quality
= _PWG_PRINT_QUALITY_NORMAL
;
3494 else if (pc
->num_presets
[_PWG_PRINT_COLOR_MODE_COLOR
][print_quality
] > 0)
3495 print_color_mode
= _PWG_PRINT_COLOR_MODE_COLOR
;
3498 print_quality
= _PWG_PRINT_QUALITY_NORMAL
;
3499 print_color_mode
= _PWG_PRINT_COLOR_MODE_COLOR
;
3503 if (pc
->num_presets
[print_color_mode
][print_quality
] > 0)
3506 * Copy the preset options as long as the corresponding names are not
3507 * already defined in the IPP request...
3510 for (i
= pc
->num_presets
[print_color_mode
][print_quality
],
3511 preset
= pc
->presets
[print_color_mode
][print_quality
];
3515 if (!ippFindAttribute(job
->attrs
, preset
->name
, IPP_TAG_ZERO
))
3516 num_pwgppds
= cupsAddOption(preset
->name
, preset
->value
, num_pwgppds
,
3524 if (!ippFindAttribute(job
->attrs
, "InputSlot", IPP_TAG_ZERO
) &&
3525 !ippFindAttribute(job
->attrs
, "HPPaperSource", IPP_TAG_ZERO
))
3527 if ((ppd
= _ppdCacheGetInputSlot(pc
, job
->attrs
, NULL
)) != NULL
)
3528 num_pwgppds
= cupsAddOption(pc
->source_option
, ppd
, num_pwgppds
,
3531 if (!ippFindAttribute(job
->attrs
, "MediaType", IPP_TAG_ZERO
) &&
3532 (ppd
= _ppdCacheGetMediaType(pc
, job
->attrs
, NULL
)) != NULL
)
3533 num_pwgppds
= cupsAddOption("MediaType", ppd
, num_pwgppds
, &pwgppds
);
3535 if (!ippFindAttribute(job
->attrs
, "PageRegion", IPP_TAG_ZERO
) &&
3536 !ippFindAttribute(job
->attrs
, "PageSize", IPP_TAG_ZERO
) &&
3537 (ppd
= _ppdCacheGetPageSize(pc
, job
->attrs
, NULL
, &exact
)) != NULL
)
3539 num_pwgppds
= cupsAddOption("PageSize", ppd
, num_pwgppds
, &pwgppds
);
3541 if (!ippFindAttribute(job
->attrs
, "media", IPP_TAG_ZERO
))
3542 num_pwgppds
= cupsAddOption("media", ppd
, num_pwgppds
, &pwgppds
);
3545 if (!ippFindAttribute(job
->attrs
, "OutputBin", IPP_TAG_ZERO
) &&
3546 (attr
= ippFindAttribute(job
->attrs
, "output-bin",
3547 IPP_TAG_ZERO
)) != NULL
&&
3548 (attr
->value_tag
== IPP_TAG_KEYWORD
||
3549 attr
->value_tag
== IPP_TAG_NAME
) &&
3550 (ppd
= _ppdCacheGetOutputBin(pc
, attr
->values
[0].string
.text
)) != NULL
)
3553 * Map output-bin to OutputBin option...
3556 num_pwgppds
= cupsAddOption("OutputBin", ppd
, num_pwgppds
, &pwgppds
);
3559 if (pc
->sides_option
&&
3560 !ippFindAttribute(job
->attrs
, pc
->sides_option
, IPP_TAG_ZERO
) &&
3561 (attr
= ippFindAttribute(job
->attrs
, "sides", IPP_TAG_KEYWORD
)) != NULL
)
3564 * Map sides to duplex option...
3567 if (!strcmp(attr
->values
[0].string
.text
, "one-sided"))
3568 num_pwgppds
= cupsAddOption(pc
->sides_option
, pc
->sides_1sided
,
3569 num_pwgppds
, &pwgppds
);
3570 else if (!strcmp(attr
->values
[0].string
.text
, "two-sided-long-edge"))
3571 num_pwgppds
= cupsAddOption(pc
->sides_option
, pc
->sides_2sided_long
,
3572 num_pwgppds
, &pwgppds
);
3573 else if (!strcmp(attr
->values
[0].string
.text
, "two-sided-short-edge"))
3574 num_pwgppds
= cupsAddOption(pc
->sides_option
, pc
->sides_2sided_short
,
3575 num_pwgppds
, &pwgppds
);
3579 * Map finishings values...
3582 num_pwgppds
= _ppdCacheGetFinishingOptions(pc
, job
->attrs
,
3583 IPP_FINISHINGS_NONE
, num_pwgppds
,
3588 * Figure out how much room we need...
3591 newlength
= ipp_length(job
->attrs
);
3593 for (i
= num_pwgppds
, pwgppd
= pwgppds
; i
> 0; i
--, pwgppd
++)
3594 newlength
+= 1 + strlen(pwgppd
->name
) + 1 + strlen(pwgppd
->value
);
3597 * Then allocate/reallocate the option buffer as needed...
3600 if (newlength
== 0) /* This can never happen, but Clang */
3601 newlength
= 1; /* thinks it can... */
3603 if (newlength
> optlength
|| !options
)
3606 optptr
= malloc(newlength
);
3608 optptr
= realloc(options
, newlength
);
3612 cupsdLogJob(job
, CUPSD_LOG_CRIT
,
3613 "Unable to allocate " CUPS_LLFMT
" bytes for option buffer.",
3614 CUPS_LLCAST newlength
);
3619 optlength
= newlength
;
3623 * Now loop through the attributes and convert them to the textual
3624 * representation used by the filters...
3630 snprintf(title
, title_size
, "%s-%d", job
->printer
->name
, job
->id
);
3631 strlcpy(copies
, "1", copies_size
);
3633 for (attr
= job
->attrs
->attrs
; attr
!= NULL
; attr
= attr
->next
)
3635 if (!strcmp(attr
->name
, "copies") &&
3636 attr
->value_tag
== IPP_TAG_INTEGER
)
3639 * Don't use the # copies attribute if we are printing the job sheets...
3643 snprintf(copies
, copies_size
, "%d", attr
->values
[0].integer
);
3645 else if (!strcmp(attr
->name
, "job-name") &&
3646 (attr
->value_tag
== IPP_TAG_NAME
||
3647 attr
->value_tag
== IPP_TAG_NAMELANG
))
3648 strlcpy(title
, attr
->values
[0].string
.text
, title_size
);
3649 else if (attr
->group_tag
== IPP_TAG_JOB
)
3652 * Filter out other unwanted attributes...
3655 if (attr
->value_tag
== IPP_TAG_NOVALUE
||
3656 attr
->value_tag
== IPP_TAG_MIMETYPE
||
3657 attr
->value_tag
== IPP_TAG_NAMELANG
||
3658 attr
->value_tag
== IPP_TAG_TEXTLANG
||
3659 (attr
->value_tag
== IPP_TAG_URI
&& strcmp(attr
->name
, "job-uuid") &&
3660 strcmp(attr
->name
, "job-authorization-uri")) ||
3661 attr
->value_tag
== IPP_TAG_URISCHEME
||
3662 attr
->value_tag
== IPP_TAG_BEGIN_COLLECTION
) /* Not yet supported */
3665 if (!strcmp(attr
->name
, "job-hold-until") ||
3666 !strcmp(attr
->name
, "job-id") ||
3667 !strcmp(attr
->name
, "job-k-octets") ||
3668 !strcmp(attr
->name
, "job-media-sheets") ||
3669 !strcmp(attr
->name
, "job-media-sheets-completed") ||
3670 !strcmp(attr
->name
, "job-state") ||
3671 !strcmp(attr
->name
, "job-state-reasons"))
3674 if (!strncmp(attr
->name
, "job-", 4) &&
3675 strcmp(attr
->name
, "job-account-id") &&
3676 strcmp(attr
->name
, "job-accounting-user-id") &&
3677 strcmp(attr
->name
, "job-authorization-uri") &&
3678 strcmp(attr
->name
, "job-billing") &&
3679 strcmp(attr
->name
, "job-impressions") &&
3680 strcmp(attr
->name
, "job-originating-host-name") &&
3681 strcmp(attr
->name
, "job-password") &&
3682 strcmp(attr
->name
, "job-password-encryption") &&
3683 strcmp(attr
->name
, "job-uuid") &&
3684 !(job
->printer
->type
& CUPS_PRINTER_REMOTE
))
3687 if ((!strcmp(attr
->name
, "job-impressions") ||
3688 !strcmp(attr
->name
, "page-label") ||
3689 !strcmp(attr
->name
, "page-border") ||
3690 !strncmp(attr
->name
, "number-up", 9) ||
3691 !strcmp(attr
->name
, "page-ranges") ||
3692 !strcmp(attr
->name
, "page-set") ||
3693 !_cups_strcasecmp(attr
->name
, "AP_FIRSTPAGE_InputSlot") ||
3694 !_cups_strcasecmp(attr
->name
, "AP_FIRSTPAGE_ManualFeed") ||
3695 !_cups_strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
3696 "PMTotalSidesImaged..n.") ||
3697 !_cups_strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
3698 "PMTotalBeginPages..n.")) &&
3703 * Otherwise add them to the list...
3706 if (optptr
> options
)
3707 strlcat(optptr
, " ", optlength
- (size_t)(optptr
- options
));
3709 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
3711 strlcat(optptr
, attr
->name
, optlength
- (size_t)(optptr
- options
));
3712 strlcat(optptr
, "=", optlength
- (size_t)(optptr
- options
));
3715 for (i
= 0; i
< attr
->num_values
; i
++)
3718 strlcat(optptr
, ",", optlength
- (size_t)(optptr
- options
));
3720 optptr
+= strlen(optptr
);
3722 switch (attr
->value_tag
)
3724 case IPP_TAG_INTEGER
:
3726 snprintf(optptr
, optlength
- (size_t)(optptr
- options
),
3727 "%d", attr
->values
[i
].integer
);
3730 case IPP_TAG_BOOLEAN
:
3731 if (!attr
->values
[i
].boolean
)
3732 strlcat(optptr
, "no", optlength
- (size_t)(optptr
- options
));
3734 strlcat(optptr
, attr
->name
, optlength
- (size_t)(optptr
- options
));
3737 case IPP_TAG_RANGE
:
3738 if (attr
->values
[i
].range
.lower
== attr
->values
[i
].range
.upper
)
3739 snprintf(optptr
, optlength
- (size_t)(optptr
- options
) - 1,
3740 "%d", attr
->values
[i
].range
.lower
);
3742 snprintf(optptr
, optlength
- (size_t)(optptr
- options
) - 1,
3743 "%d-%d", attr
->values
[i
].range
.lower
,
3744 attr
->values
[i
].range
.upper
);
3747 case IPP_TAG_RESOLUTION
:
3748 snprintf(optptr
, optlength
- (size_t)(optptr
- options
) - 1,
3749 "%dx%d%s", attr
->values
[i
].resolution
.xres
,
3750 attr
->values
[i
].resolution
.yres
,
3751 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
3755 case IPP_TAG_STRING
:
3758 case IPP_TAG_KEYWORD
:
3759 case IPP_TAG_CHARSET
:
3760 case IPP_TAG_LANGUAGE
:
3762 for (valptr
= attr
->values
[i
].string
.text
; *valptr
;)
3764 if (strchr(" \t\n\\\'\"", *valptr
))
3766 *optptr
++ = *valptr
++;
3773 break; /* anti-compiler-warning-code */
3777 optptr
+= strlen(optptr
);
3782 * Finally loop through the PWG->PPD mapped options and add them...
3785 for (i
= num_pwgppds
, pwgppd
= pwgppds
; i
> 0; i
--, pwgppd
++)
3788 strlcpy(optptr
, pwgppd
->name
, optlength
- (size_t)(optptr
- options
));
3789 optptr
+= strlen(optptr
);
3791 strlcpy(optptr
, pwgppd
->value
, optlength
- (size_t)(optptr
- options
));
3792 optptr
+= strlen(optptr
);
3795 cupsFreeOptions(num_pwgppds
, pwgppds
);
3798 * Return the options string...
3806 * 'ipp_length()' - Compute the size of the buffer needed to hold
3807 * the textual IPP attributes.
3810 static size_t /* O - Size of attribute buffer */
3811 ipp_length(ipp_t
*ipp
) /* I - IPP request */
3813 size_t bytes
; /* Number of bytes */
3814 int i
; /* Looping var */
3815 ipp_attribute_t
*attr
; /* Current attribute */
3819 * Loop through all attributes...
3824 for (attr
= ipp
->attrs
; attr
!= NULL
; attr
= attr
->next
)
3827 * Skip attributes that won't be sent to filters...
3830 if (attr
->value_tag
== IPP_TAG_NOVALUE
||
3831 attr
->value_tag
== IPP_TAG_MIMETYPE
||
3832 attr
->value_tag
== IPP_TAG_NAMELANG
||
3833 attr
->value_tag
== IPP_TAG_TEXTLANG
||
3834 attr
->value_tag
== IPP_TAG_URI
||
3835 attr
->value_tag
== IPP_TAG_URISCHEME
)
3839 * Add space for a leading space and commas between each value.
3840 * For the first attribute, the leading space isn't used, so the
3841 * extra byte can be used as the nul terminator...
3844 bytes
++; /* " " separator */
3845 bytes
+= (size_t)attr
->num_values
; /* "," separators */
3848 * Boolean attributes appear as "foo,nofoo,foo,nofoo", while
3849 * other attributes appear as "foo=value1,value2,...,valueN".
3852 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
3853 bytes
+= strlen(attr
->name
);
3855 bytes
+= (size_t)attr
->num_values
* strlen(attr
->name
);
3858 * Now add the size required for each value in the attribute...
3861 switch (attr
->value_tag
)
3863 case IPP_TAG_INTEGER
:
3866 * Minimum value of a signed integer is -2147483647, or 11 digits.
3869 bytes
+= (size_t)attr
->num_values
* 11;
3872 case IPP_TAG_BOOLEAN
:
3874 * Add two bytes for each false ("no") value...
3877 for (i
= 0; i
< attr
->num_values
; i
++)
3878 if (!attr
->values
[i
].boolean
)
3882 case IPP_TAG_RANGE
:
3884 * A range is two signed integers separated by a hyphen, or
3885 * 23 characters max.
3888 bytes
+= (size_t)attr
->num_values
* 23;
3891 case IPP_TAG_RESOLUTION
:
3893 * A resolution is two signed integers separated by an "x" and
3894 * suffixed by the units, or 26 characters max.
3897 bytes
+= (size_t)attr
->num_values
* 26;
3900 case IPP_TAG_STRING
:
3903 case IPP_TAG_KEYWORD
:
3904 case IPP_TAG_CHARSET
:
3905 case IPP_TAG_LANGUAGE
:
3908 * Strings can contain characters that need quoting. We need
3909 * at least 2 * len + 2 characters to cover the quotes and
3910 * any backslashes in the string.
3913 for (i
= 0; i
< attr
->num_values
; i
++)
3914 bytes
+= 2 * strlen(attr
->values
[i
].string
.text
) + 2;
3918 break; /* anti-compiler-warning-code */
3927 * 'load_job_cache()' - Load jobs from the job.cache file.
3931 load_job_cache(const char *filename
) /* I - job.cache filename */
3933 cups_file_t
*fp
; /* job.cache file */
3934 char line
[1024], /* Line buffer */
3935 *value
; /* Value on line */
3936 int linenum
; /* Line number in file */
3937 cupsd_job_t
*job
; /* Current job */
3938 int jobid
; /* Job ID */
3939 char jobfile
[1024]; /* Job filename */
3943 * Open the job.cache file...
3946 if ((fp
= cupsdOpenConfFile(filename
)) == NULL
)
3948 load_request_root();
3953 * Read entries from the job cache file and create jobs as needed.
3956 cupsdLogMessage(CUPSD_LOG_INFO
, "Loading job cache file \"%s\"...",
3962 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
3964 if (!_cups_strcasecmp(line
, "NextJobId"))
3967 NextJobId
= atoi(value
);
3969 else if (!_cups_strcasecmp(line
, "<Job"))
3973 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing </Job> directive on line %d.",
3980 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing job ID on line %d.", linenum
);
3984 jobid
= atoi(value
);
3988 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad job ID %d on line %d.", jobid
,
3993 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, jobid
);
3994 if (access(jobfile
, 0))
3996 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d.N", RequestRoot
, jobid
);
3997 if (access(jobfile
, 0))
3999 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Job %d] Files have gone away.",
4005 job
= calloc(1, sizeof(cupsd_job_t
));
4008 cupsdLogMessage(CUPSD_LOG_EMERG
,
4009 "[Job %d] Unable to allocate memory for job.", jobid
);
4014 job
->back_pipes
[0] = -1;
4015 job
->back_pipes
[1] = -1;
4016 job
->print_pipes
[0] = -1;
4017 job
->print_pipes
[1] = -1;
4018 job
->side_pipes
[0] = -1;
4019 job
->side_pipes
[1] = -1;
4020 job
->status_pipes
[0] = -1;
4021 job
->status_pipes
[1] = -1;
4023 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Loading from cache...");
4027 cupsdLogMessage(CUPSD_LOG_ERROR
,
4028 "Missing <Job #> directive on line %d.", linenum
);
4031 else if (!_cups_strcasecmp(line
, "</Job>"))
4033 cupsArrayAdd(Jobs
, job
);
4035 if (job
->state_value
<= IPP_JOB_STOPPED
&& cupsdLoadJob(job
))
4036 cupsArrayAdd(ActiveJobs
, job
);
4042 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing value on line %d.", linenum
);
4045 else if (!_cups_strcasecmp(line
, "State"))
4047 job
->state_value
= (ipp_jstate_t
)atoi(value
);
4049 if (job
->state_value
< IPP_JOB_PENDING
)
4050 job
->state_value
= IPP_JOB_PENDING
;
4051 else if (job
->state_value
> IPP_JOB_COMPLETED
)
4052 job
->state_value
= IPP_JOB_COMPLETED
;
4054 else if (!_cups_strcasecmp(line
, "HoldUntil"))
4056 job
->hold_until
= atoi(value
);
4058 else if (!_cups_strcasecmp(line
, "Priority"))
4060 job
->priority
= atoi(value
);
4062 else if (!_cups_strcasecmp(line
, "Username"))
4064 cupsdSetString(&job
->username
, value
);
4066 else if (!_cups_strcasecmp(line
, "Destination"))
4068 cupsdSetString(&job
->dest
, value
);
4070 else if (!_cups_strcasecmp(line
, "DestType"))
4072 job
->dtype
= (cups_ptype_t
)atoi(value
);
4074 else if (!_cups_strcasecmp(line
, "NumFiles"))
4076 job
->num_files
= atoi(value
);
4078 if (job
->num_files
< 0)
4080 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad NumFiles value %d on line %d.",
4081 job
->num_files
, linenum
);
4086 if (job
->num_files
> 0)
4088 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-001", RequestRoot
,
4090 if (access(jobfile
, 0))
4092 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Data files have gone away.");
4097 job
->filetypes
= calloc((size_t)job
->num_files
, sizeof(mime_type_t
*));
4098 job
->compressions
= calloc((size_t)job
->num_files
, sizeof(int));
4100 if (!job
->filetypes
|| !job
->compressions
)
4102 cupsdLogJob(job
, CUPSD_LOG_EMERG
,
4103 "Unable to allocate memory for %d files.",
4109 else if (!_cups_strcasecmp(line
, "File"))
4111 int number
, /* File number */
4112 compression
; /* Compression value */
4113 char super
[MIME_MAX_SUPER
], /* MIME super type */
4114 type
[MIME_MAX_TYPE
]; /* MIME type */
4117 if (sscanf(value
, "%d%*[ \t]%15[^/]/%255s%d", &number
, super
, type
,
4120 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File on line %d.", linenum
);
4124 if (number
< 1 || number
> job
->num_files
)
4126 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File number %d on line %d.",
4133 job
->compressions
[number
] = compression
;
4134 job
->filetypes
[number
] = mimeType(MimeDatabase
, super
, type
);
4136 if (!job
->filetypes
[number
])
4139 * If the original MIME type is unknown, auto-type it!
4142 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
4143 "Unknown MIME type %s/%s for file %d.",
4144 super
, type
, number
+ 1);
4146 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
4147 job
->id
, number
+ 1);
4148 job
->filetypes
[number
] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
4149 job
->compressions
+ number
);
4152 * If that didn't work, assume it is raw...
4155 if (!job
->filetypes
[number
])
4156 job
->filetypes
[number
] = mimeType(MimeDatabase
, "application",
4161 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unknown %s directive on line %d.",
4167 cupsdLogMessage(CUPSD_LOG_ERROR
,
4168 "Missing </Job> directive on line %d.", linenum
);
4169 cupsdDeleteJob(job
, CUPSD_JOB_PURGE
);
4177 * 'load_next_job_id()' - Load the NextJobId value from the job.cache file.
4181 load_next_job_id(const char *filename
) /* I - job.cache filename */
4183 cups_file_t
*fp
; /* job.cache file */
4184 char line
[1024], /* Line buffer */
4185 *value
; /* Value on line */
4186 int linenum
; /* Line number in file */
4187 int next_job_id
; /* NextJobId value from line */
4191 * Read the NextJobId directive from the job.cache file and use
4192 * the value (if any).
4195 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
4197 if (errno
!= ENOENT
)
4198 cupsdLogMessage(CUPSD_LOG_ERROR
,
4199 "Unable to open job cache file \"%s\": %s",
4200 filename
, strerror(errno
));
4205 cupsdLogMessage(CUPSD_LOG_INFO
,
4206 "Loading NextJobId from job cache file \"%s\"...", filename
);
4210 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
4212 if (!_cups_strcasecmp(line
, "NextJobId"))
4216 next_job_id
= atoi(value
);
4218 if (next_job_id
> NextJobId
)
4219 NextJobId
= next_job_id
;
4230 * 'load_request_root()' - Load jobs from the RequestRoot directory.
4234 load_request_root(void)
4236 cups_dir_t
*dir
; /* Directory */
4237 cups_dentry_t
*dent
; /* Directory entry */
4238 cupsd_job_t
*job
; /* New job */
4242 * Open the requests directory...
4245 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Scanning %s for jobs...", RequestRoot
);
4247 if ((dir
= cupsDirOpen(RequestRoot
)) == NULL
)
4249 cupsdLogMessage(CUPSD_LOG_ERROR
,
4250 "Unable to open spool directory \"%s\": %s",
4251 RequestRoot
, strerror(errno
));
4256 * Read all the c##### files...
4259 while ((dent
= cupsDirRead(dir
)) != NULL
)
4260 if (strlen(dent
->filename
) >= 6 && dent
->filename
[0] == 'c')
4263 * Allocate memory for the job...
4266 if ((job
= calloc(sizeof(cupsd_job_t
), 1)) == NULL
)
4268 cupsdLogMessage(CUPSD_LOG_ERROR
, "Ran out of memory for jobs.");
4274 * Assign the job ID...
4277 job
->id
= atoi(dent
->filename
+ 1);
4278 job
->back_pipes
[0] = -1;
4279 job
->back_pipes
[1] = -1;
4280 job
->print_pipes
[0] = -1;
4281 job
->print_pipes
[1] = -1;
4282 job
->side_pipes
[0] = -1;
4283 job
->side_pipes
[1] = -1;
4284 job
->status_pipes
[0] = -1;
4285 job
->status_pipes
[1] = -1;
4287 if (job
->id
>= NextJobId
)
4288 NextJobId
= job
->id
+ 1;
4294 if (cupsdLoadJob(job
))
4297 * Insert the job into the array, sorting by job priority and ID...
4300 cupsArrayAdd(Jobs
, job
);
4302 if (job
->state_value
<= IPP_JOB_STOPPED
)
4303 cupsArrayAdd(ActiveJobs
, job
);
4316 * 'remove_job_files()' - Remove the document files for a job.
4320 remove_job_files(cupsd_job_t
*job
) /* I - Job */
4322 int i
; /* Looping var */
4323 char filename
[1024]; /* Document filename */
4326 if (job
->num_files
<= 0)
4329 for (i
= 1; i
<= job
->num_files
; i
++)
4331 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
4333 cupsdUnlinkOrRemoveFile(filename
);
4336 free(job
->filetypes
);
4337 free(job
->compressions
);
4341 job
->filetypes
= NULL
;
4342 job
->compressions
= NULL
;
4344 LastEvent
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
4349 * 'remove_job_history()' - Remove the control file for a job.
4353 remove_job_history(cupsd_job_t
*job
) /* I - Job */
4355 char filename
[1024]; /* Control filename */
4359 * Remove the job info file...
4362 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
,
4364 cupsdUnlinkOrRemoveFile(filename
);
4366 LastEvent
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
4371 * 'set_time()' - Set one of the "time-at-xyz" attributes.
4375 set_time(cupsd_job_t
*job
, /* I - Job to update */
4376 const char *name
) /* I - Name of attribute */
4378 ipp_attribute_t
*attr
; /* Time attribute */
4379 time_t curtime
; /* Current time */
4382 curtime
= time(NULL
);
4384 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "%s=%ld", name
, (long)curtime
);
4386 if ((attr
= ippFindAttribute(job
->attrs
, name
, IPP_TAG_ZERO
)) != NULL
)
4388 attr
->value_tag
= IPP_TAG_INTEGER
;
4389 attr
->values
[0].integer
= curtime
;
4392 if (!strcmp(name
, "time-at-completed"))
4394 if (JobHistory
< INT_MAX
&& attr
)
4395 job
->history_time
= attr
->values
[0].integer
+ JobHistory
;
4397 job
->history_time
= INT_MAX
;
4399 if (job
->history_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
4400 JobHistoryUpdate
= job
->history_time
;
4402 if (JobFiles
< INT_MAX
&& attr
)
4403 job
->file_time
= attr
->values
[0].integer
+ JobFiles
;
4405 job
->file_time
= INT_MAX
;
4407 if (job
->file_time
< JobHistoryUpdate
|| !JobHistoryUpdate
)
4408 JobHistoryUpdate
= job
->file_time
;
4410 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_time: JobHistoryUpdate=%ld",
4411 (long)JobHistoryUpdate
);
4417 * 'start_job()' - Start a print job.
4421 start_job(cupsd_job_t
*job
, /* I - Job ID */
4422 cupsd_printer_t
*printer
) /* I - Printer to print job */
4424 const char *filename
; /* Support filename */
4425 ipp_attribute_t
*cancel_after
= ippFindAttribute(job
->attrs
,
4428 /* job-cancel-after attribute */
4431 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "start_job(job=%p(%d), printer=%p(%s))",
4432 job
, job
->id
, printer
, printer
->name
);
4435 * Make sure we have some files around before we try to print...
4438 if (job
->num_files
== 0)
4440 ippSetString(job
->attrs
, &job
->reasons
, 0, "aborted-by-system");
4441 cupsdSetJobState(job
, IPP_JOB_ABORTED
, CUPSD_JOB_DEFAULT
,
4442 "Aborting job because it has no files.");
4447 * Update the printer and job state to "processing"...
4450 if (!cupsdLoadJob(job
))
4453 if (!job
->printer_message
)
4454 job
->printer_message
= ippFindAttribute(job
->attrs
,
4455 "job-printer-state-message",
4457 if (job
->printer_message
)
4458 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
), "");
4460 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-printing");
4461 cupsdSetJobState(job
, IPP_JOB_PROCESSING
, CUPSD_JOB_DEFAULT
, NULL
);
4462 cupsdSetPrinterState(printer
, IPP_PRINTER_PROCESSING
, 0);
4463 cupsdSetPrinterReasons(printer
, "-cups-remote-pending,"
4464 "cups-remote-pending-held,"
4465 "cups-remote-processing,"
4466 "cups-remote-stopped,"
4467 "cups-remote-canceled,"
4468 "cups-remote-aborted,"
4469 "cups-remote-completed");
4472 job
->current_file
= 0;
4474 job
->history_time
= 0;
4476 job
->printer
= printer
;
4480 job
->cancel_time
= time(NULL
) + ippGetInteger(cancel_after
, 0);
4481 else if (MaxJobTime
> 0)
4482 job
->cancel_time
= time(NULL
) + MaxJobTime
;
4484 job
->cancel_time
= 0;
4487 * Check for support files...
4490 cupsdSetPrinterReasons(job
->printer
, "-cups-missing-filter-warning,"
4491 "cups-insecure-filter-warning");
4495 for (filename
= (const char *)cupsArrayFirst(printer
->pc
->support_files
);
4497 filename
= (const char *)cupsArrayNext(printer
->pc
->support_files
))
4499 if (_cupsFileCheck(filename
, _CUPS_FILE_CHECK_FILE
, !RunUser
,
4500 cupsdLogFCMessage
, printer
))
4506 * Setup the last exit status and security profiles...
4510 job
->profile
= cupsdCreateProfile(job
->id
, 0);
4511 job
->bprofile
= cupsdCreateProfile(job
->id
, 1);
4514 * Create the status pipes and buffer...
4517 if (cupsdOpenPipe(job
->status_pipes
))
4519 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
4520 "Unable to create job status pipes - %s.", strerror(errno
));
4522 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
4523 "Job stopped because the scheduler could not create the "
4524 "job status pipes.");
4526 cupsdDestroyProfile(job
->profile
);
4527 job
->profile
= NULL
;
4528 cupsdDestroyProfile(job
->bprofile
);
4529 job
->bprofile
= NULL
;
4533 job
->status_buffer
= cupsdStatBufNew(job
->status_pipes
[0], NULL
);
4534 job
->status_level
= CUPSD_LOG_INFO
;
4537 * Create the backchannel pipes and make them non-blocking...
4540 if (cupsdOpenPipe(job
->back_pipes
))
4542 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
4543 "Unable to create back-channel pipes - %s.", strerror(errno
));
4545 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
4546 "Job stopped because the scheduler could not create the "
4547 "back-channel pipes.");
4549 cupsdClosePipe(job
->status_pipes
);
4550 cupsdStatBufDelete(job
->status_buffer
);
4551 job
->status_buffer
= NULL
;
4553 cupsdDestroyProfile(job
->profile
);
4554 job
->profile
= NULL
;
4555 cupsdDestroyProfile(job
->bprofile
);
4556 job
->bprofile
= NULL
;
4560 fcntl(job
->back_pipes
[0], F_SETFL
,
4561 fcntl(job
->back_pipes
[0], F_GETFL
) | O_NONBLOCK
);
4562 fcntl(job
->back_pipes
[1], F_SETFL
,
4563 fcntl(job
->back_pipes
[1], F_GETFL
) | O_NONBLOCK
);
4566 * Create the side-channel pipes and make them non-blocking...
4569 if (socketpair(AF_LOCAL
, SOCK_STREAM
, 0, job
->side_pipes
))
4571 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
4572 "Unable to create side-channel pipes - %s.", strerror(errno
));
4574 cupsdSetJobState(job
, IPP_JOB_STOPPED
, CUPSD_JOB_DEFAULT
,
4575 "Job stopped because the scheduler could not create the "
4576 "side-channel pipes.");
4578 cupsdClosePipe(job
->back_pipes
);
4580 cupsdClosePipe(job
->status_pipes
);
4581 cupsdStatBufDelete(job
->status_buffer
);
4582 job
->status_buffer
= NULL
;
4584 cupsdDestroyProfile(job
->profile
);
4585 job
->profile
= NULL
;
4586 cupsdDestroyProfile(job
->bprofile
);
4587 job
->bprofile
= NULL
;
4591 fcntl(job
->side_pipes
[0], F_SETFL
,
4592 fcntl(job
->side_pipes
[0], F_GETFL
) | O_NONBLOCK
);
4593 fcntl(job
->side_pipes
[1], F_SETFL
,
4594 fcntl(job
->side_pipes
[1], F_GETFL
) | O_NONBLOCK
);
4596 fcntl(job
->side_pipes
[0], F_SETFD
,
4597 fcntl(job
->side_pipes
[0], F_GETFD
) | FD_CLOEXEC
);
4598 fcntl(job
->side_pipes
[1], F_SETFD
,
4599 fcntl(job
->side_pipes
[1], F_GETFD
) | FD_CLOEXEC
);
4602 * Now start the first file in the job...
4605 cupsdContinueJob(job
);
4610 * 'stop_job()' - Stop a print job.
4614 stop_job(cupsd_job_t
*job
, /* I - Job */
4615 cupsd_jobaction_t action
) /* I - Action */
4617 int i
; /* Looping var */
4620 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "stop_job(job=%p(%d), action=%d)", job
,
4623 FilterLevel
-= job
->cost
;
4626 if (action
== CUPSD_JOB_DEFAULT
&& !job
->kill_time
)
4627 job
->kill_time
= time(NULL
) + JobKillDelay
;
4628 else if (action
>= CUPSD_JOB_FORCE
)
4631 for (i
= 0; job
->filters
[i
]; i
++)
4632 if (job
->filters
[i
] > 0)
4634 cupsdEndProcess(job
->filters
[i
], action
>= CUPSD_JOB_FORCE
);
4636 if (action
>= CUPSD_JOB_FORCE
)
4637 job
->filters
[i
] = -job
->filters
[i
];
4640 if (job
->backend
> 0)
4642 cupsdEndProcess(job
->backend
, action
>= CUPSD_JOB_FORCE
);
4644 if (action
>= CUPSD_JOB_FORCE
)
4645 job
->backend
= -job
->backend
;
4648 if (action
>= CUPSD_JOB_FORCE
)
4651 * Clear job status...
4660 * 'unload_job()' - Unload a job from memory.
4664 unload_job(cupsd_job_t
*job
) /* I - Job */
4669 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Unloading...");
4671 ippDelete(job
->attrs
);
4675 job
->reasons
= NULL
;
4677 job
->job_sheets
= NULL
;
4678 job
->printer_message
= NULL
;
4679 job
->printer_reasons
= NULL
;
4684 * 'update_job()' - Read a status update from a job's filters.
4688 update_job(cupsd_job_t
*job
) /* I - Job to check */
4690 int i
; /* Looping var */
4691 int copies
; /* Number of copies printed */
4692 char message
[CUPSD_SB_BUFFER_SIZE
],
4694 *ptr
; /* Pointer update... */
4695 int loglevel
, /* Log level for message */
4696 event
= 0; /* Events? */
4697 static const char * const levels
[] = /* Log levels */
4713 * Get the printer associated with this job; if the printer is stopped for
4714 * any reason then job->printer will be reset to NULL, so make sure we have
4715 * a valid pointer...
4718 while ((ptr
= cupsdStatBufUpdate(job
->status_buffer
, &loglevel
,
4719 message
, sizeof(message
))) != NULL
)
4722 * Process page and printer state messages as needed...
4725 if (loglevel
== CUPSD_LOG_PAGE
)
4728 * Page message; send the message to the page_log file and update the
4729 * job sheet count...
4732 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "PAGE: %s", message
);
4736 if (!_cups_strncasecmp(message
, "total ", 6))
4739 * Got a total count of pages from a backend or filter...
4742 copies
= atoi(message
+ 6);
4743 copies
-= job
->sheets
->values
[0].integer
; /* Just track the delta */
4745 else if (!sscanf(message
, "%*d%d", &copies
))
4748 job
->sheets
->values
[0].integer
+= copies
;
4750 if (job
->printer
->page_limit
)
4751 cupsdUpdateQuota(job
->printer
, job
->username
, copies
, 0);
4754 cupsdLogPage(job
, message
);
4757 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
4758 "Printed %d page(s).", job
->sheets
->values
[0].integer
);
4760 else if (loglevel
== CUPSD_LOG_JOBSTATE
)
4763 * Support "keyword" to set job-state-reasons to the specified keyword.
4764 * This is sufficient for the current paid printing stuff.
4767 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "JOBSTATE: %s", message
);
4769 ippSetString(job
->attrs
, &job
->reasons
, 0, message
);
4771 else if (loglevel
== CUPSD_LOG_STATE
)
4773 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "STATE: %s", message
);
4775 if (!strcmp(message
, "paused"))
4777 cupsdStopPrinter(job
->printer
, 1);
4780 else if (message
[0] && cupsdSetPrinterReasons(job
->printer
, message
))
4782 event
|= CUPSD_EVENT_PRINTER_STATE
;
4787 * Reset cancel time after connecting to the device...
4790 for (i
= 0; i
< job
->printer
->num_reasons
; i
++)
4791 if (!strcmp(job
->printer
->reasons
[i
], "connecting-to-device"))
4794 if (i
>= job
->printer
->num_reasons
)
4796 ipp_attribute_t
*cancel_after
= ippFindAttribute(job
->attrs
,
4799 /* job-cancel-after attribute */
4802 job
->cancel_time
= time(NULL
) + ippGetInteger(cancel_after
, 0);
4804 job
->cancel_time
= time(NULL
) + MaxJobTime
;
4809 update_job_attrs(job
, 0);
4811 else if (loglevel
== CUPSD_LOG_ATTR
)
4814 * Set attribute(s)...
4817 int num_attrs
; /* Number of attributes */
4818 cups_option_t
*attrs
; /* Attributes */
4819 const char *attr
; /* Attribute */
4821 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "ATTR: %s", message
);
4823 num_attrs
= cupsParseOptions(message
, 0, &attrs
);
4825 if ((attr
= cupsGetOption("auth-info-default", num_attrs
,
4828 job
->printer
->num_options
= cupsAddOption("auth-info", attr
,
4829 job
->printer
->num_options
,
4830 &(job
->printer
->options
));
4831 cupsdSetPrinterAttrs(job
->printer
);
4833 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4836 if ((attr
= cupsGetOption("auth-info-required", num_attrs
,
4839 cupsdSetAuthInfoRequired(job
->printer
, attr
, NULL
);
4840 cupsdSetPrinterAttrs(job
->printer
);
4842 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4845 if ((attr
= cupsGetOption("job-media-progress", num_attrs
,
4848 int progress
= atoi(attr
);
4851 if (progress
>= 0 && progress
<= 100)
4853 job
->progress
= progress
;
4856 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
4857 "Printing page %d, %d%%",
4858 job
->sheets
->values
[0].integer
, job
->progress
);
4862 if ((attr
= cupsGetOption("printer-alert", num_attrs
, attrs
)) != NULL
)
4864 cupsdSetString(&job
->printer
->alert
, attr
);
4865 event
|= CUPSD_EVENT_PRINTER_STATE
;
4868 if ((attr
= cupsGetOption("printer-alert-description", num_attrs
,
4871 cupsdSetString(&job
->printer
->alert_description
, attr
);
4872 event
|= CUPSD_EVENT_PRINTER_STATE
;
4875 if ((attr
= cupsGetOption("marker-colors", num_attrs
, attrs
)) != NULL
)
4877 cupsdSetPrinterAttr(job
->printer
, "marker-colors", (char *)attr
);
4878 job
->printer
->marker_time
= time(NULL
);
4879 event
|= CUPSD_EVENT_PRINTER_STATE
;
4880 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4883 if ((attr
= cupsGetOption("marker-levels", num_attrs
, attrs
)) != NULL
)
4885 cupsdSetPrinterAttr(job
->printer
, "marker-levels", (char *)attr
);
4886 job
->printer
->marker_time
= time(NULL
);
4887 event
|= CUPSD_EVENT_PRINTER_STATE
;
4888 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4891 if ((attr
= cupsGetOption("marker-low-levels", num_attrs
, attrs
)) != NULL
)
4893 cupsdSetPrinterAttr(job
->printer
, "marker-low-levels", (char *)attr
);
4894 job
->printer
->marker_time
= time(NULL
);
4895 event
|= CUPSD_EVENT_PRINTER_STATE
;
4896 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4899 if ((attr
= cupsGetOption("marker-high-levels", num_attrs
, attrs
)) != NULL
)
4901 cupsdSetPrinterAttr(job
->printer
, "marker-high-levels", (char *)attr
);
4902 job
->printer
->marker_time
= time(NULL
);
4903 event
|= CUPSD_EVENT_PRINTER_STATE
;
4904 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4907 if ((attr
= cupsGetOption("marker-message", num_attrs
, attrs
)) != NULL
)
4909 cupsdSetPrinterAttr(job
->printer
, "marker-message", (char *)attr
);
4910 job
->printer
->marker_time
= time(NULL
);
4911 event
|= CUPSD_EVENT_PRINTER_STATE
;
4912 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4915 if ((attr
= cupsGetOption("marker-names", num_attrs
, attrs
)) != NULL
)
4917 cupsdSetPrinterAttr(job
->printer
, "marker-names", (char *)attr
);
4918 job
->printer
->marker_time
= time(NULL
);
4919 event
|= CUPSD_EVENT_PRINTER_STATE
;
4920 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4923 if ((attr
= cupsGetOption("marker-types", num_attrs
, attrs
)) != NULL
)
4925 cupsdSetPrinterAttr(job
->printer
, "marker-types", (char *)attr
);
4926 job
->printer
->marker_time
= time(NULL
);
4927 event
|= CUPSD_EVENT_PRINTER_STATE
;
4928 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
4931 cupsFreeOptions(num_attrs
, attrs
);
4933 else if (loglevel
== CUPSD_LOG_PPD
)
4936 * Set attribute(s)...
4939 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "PPD: %s", message
);
4941 job
->num_keywords
= cupsParseOptions(message
, job
->num_keywords
,
4947 * Strip legacy message prefix...
4950 if (!strncmp(message
, "recoverable:", 12))
4953 while (isspace(*ptr
& 255))
4956 else if (!strncmp(message
, "recovered:", 10))
4959 while (isspace(*ptr
& 255))
4966 cupsdLogJob(job
, loglevel
, "%s", ptr
);
4968 if (loglevel
< CUPSD_LOG_DEBUG
&&
4969 strcmp(job
->printer
->state_message
, ptr
))
4971 strlcpy(job
->printer
->state_message
, ptr
,
4972 sizeof(job
->printer
->state_message
));
4974 event
|= CUPSD_EVENT_PRINTER_STATE
| CUPSD_EVENT_JOB_PROGRESS
;
4976 if (loglevel
<= job
->status_level
&& job
->status_level
> CUPSD_LOG_ERROR
)
4979 * Some messages show in the job-printer-state-message attribute...
4982 if (loglevel
!= CUPSD_LOG_NOTICE
)
4983 job
->status_level
= loglevel
;
4985 update_job_attrs(job
, 1);
4987 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
4988 "Set job-printer-state-message to \"%s\", "
4990 job
->printer_message
->values
[0].string
.text
,
4991 levels
[job
->status_level
]);
4996 if (!strchr(job
->status_buffer
->buffer
, '\n'))
5000 if (event
& CUPSD_EVENT_JOB_PROGRESS
)
5001 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
5002 "%s", job
->printer
->state_message
);
5003 if (event
& CUPSD_EVENT_PRINTER_STATE
)
5004 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, job
->printer
, NULL
,
5005 (job
->printer
->type
& CUPS_PRINTER_CLASS
) ?
5006 "Class \"%s\" state changed." :
5007 "Printer \"%s\" state changed.",
5008 job
->printer
->name
);
5011 if (ptr
== NULL
&& !job
->status_buffer
->bufused
)
5014 * See if all of the filters and the backend have returned their
5018 for (i
= 0; job
->filters
[i
] < 0; i
++);
5020 if (job
->filters
[i
])
5023 * EOF but we haven't collected the exit status of all filters...
5026 cupsdCheckProcess();
5030 if (job
->current_file
>= job
->num_files
&& job
->backend
> 0)
5033 * EOF but we haven't collected the exit status of the backend...
5036 cupsdCheckProcess();
5041 * Handle the end of job stuff...
5044 finalize_job(job
, 1);
5047 * Check for new jobs...
5056 * 'update_job_attrs()' - Update the job-printer-* attributes.
5060 update_job_attrs(cupsd_job_t
*job
, /* I - Job to update */
5061 int do_message
)/* I - 1 = copy job-printer-state message */
5063 int i
; /* Looping var */
5064 int num_reasons
; /* Actual number of reasons */
5065 const char * const *reasons
; /* Reasons */
5066 static const char *none
= "none"; /* "none" reason */
5070 * Get/create the job-printer-state-* attributes...
5073 if (!job
->printer_message
)
5075 if ((job
->printer_message
= ippFindAttribute(job
->attrs
,
5076 "job-printer-state-message",
5077 IPP_TAG_TEXT
)) == NULL
)
5078 job
->printer_message
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_TEXT
,
5079 "job-printer-state-message",
5083 if (!job
->printer_reasons
)
5084 job
->printer_reasons
= ippFindAttribute(job
->attrs
,
5085 "job-printer-state-reasons",
5089 * Copy or clear the printer-state-message value as needed...
5092 if (job
->state_value
!= IPP_JOB_PROCESSING
&&
5093 job
->status_level
== CUPSD_LOG_INFO
)
5095 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
), "");
5098 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
5100 else if (job
->printer
->state_message
[0] && do_message
)
5102 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
),
5103 job
->printer
->state_message
);
5106 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
5110 * ... and the printer-state-reasons value...
5113 if (job
->printer
->num_reasons
== 0)
5120 num_reasons
= job
->printer
->num_reasons
;
5121 reasons
= (const char * const *)job
->printer
->reasons
;
5124 if (!job
->printer_reasons
|| job
->printer_reasons
->num_values
!= num_reasons
)
5127 * Replace/create a job-printer-state-reasons attribute...
5130 ippDeleteAttribute(job
->attrs
, job
->printer_reasons
);
5132 job
->printer_reasons
= ippAddStrings(job
->attrs
,
5133 IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
5134 "job-printer-state-reasons",
5135 num_reasons
, NULL
, NULL
);
5140 * Don't bother clearing the reason strings if they are the same...
5143 for (i
= 0; i
< num_reasons
; i
++)
5144 if (strcmp(job
->printer_reasons
->values
[i
].string
.text
, reasons
[i
]))
5147 if (i
>= num_reasons
)
5151 * Not the same, so free the current strings...
5154 for (i
= 0; i
< num_reasons
; i
++)
5155 _cupsStrFree(job
->printer_reasons
->values
[i
].string
.text
);
5159 * Copy the reasons...
5162 for (i
= 0; i
< num_reasons
; i
++)
5163 job
->printer_reasons
->values
[i
].string
.text
= _cupsStrAlloc(reasons
[i
]);
5166 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);