2 * "$Id: job.c 7902 2008-09-03 14:20:17Z mike $"
4 * Job management routines for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2008 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
17 * cupsdAddJob() - Add a new job to the job queue...
18 * cupsdCancelJob() - Cancel the specified print job.
19 * cupsdCancelJobs() - Cancel all jobs for the given
21 * cupsdCheckJobs() - Check the pending jobs and start any if the
22 * destination is available.
23 * cupsdCleanJobs() - Clean out old jobs.
24 * cupsdDeleteJob() - Free all memory used by a job.
25 * cupsdFinishJob() - Finish a job.
26 * cupsdFreeAllJobs() - Free all jobs from memory.
27 * cupsdFindJob() - Find the specified job.
28 * cupsdGetPrinterJobCount() - Get the number of pending, processing, or held
29 * jobs in a printer or class.
30 * cupsdGetUserJobCount() - Get the number of pending, processing, or held
32 * cupsdHoldJob() - Hold the specified job.
33 * cupsdLoadAllJobs() - Load all jobs from disk.
34 * cupsdLoadJob() - Load a single job...
35 * cupsdMoveJob() - Move the specified job to a different
37 * cupsdReleaseJob() - Release the specified job.
38 * cupsdRestartJob() - Restart the specified job.
39 * cupsdSaveAllJobs() - Save a summary of all jobs to disk.
40 * cupsdSaveJob() - Save a job to disk.
41 * cupsdSetJobHoldUntil() - Set the hold time for a job...
42 * cupsdSetJobPriority() - Set the priority of a job, moving it up/down
43 * in the list as needed.
44 * cupsdStopAllJobs() - Stop all print jobs.
45 * cupsdStopJob() - Stop a print job.
46 * cupsdUnloadCompletedJobs() - Flush completed job history from memory.
47 * compare_active_jobs() - Compare the job IDs and priorities of two
49 * compare_jobs() - Compare the job IDs of two jobs.
50 * ipp_length() - Compute the size of the buffer needed to hold
51 * the textual IPP attributes.
52 * load_job_cache() - Load jobs from the job.cache file.
53 * load_next_job_id() - Load the NextJobId value from the job.cache
55 * load_request_root() - Load jobs from the RequestRoot directory.
56 * set_time() - Set one of the "time-at-xyz" attributes...
57 * set_hold_until() - Set the hold time and update job-hold-until
59 * start_job() - Start a print job.
60 * unload_job() - Unload a job from memory.
61 * update_job() - Read a status update from a job's filters.
62 * update_job_attrs() - Update the job-printer-* attributes.
66 * Include necessary headers...
71 #include <cups/backend.h>
79 static mime_filter_t gziptoany_filter
=
81 NULL
, /* Source type */
82 NULL
, /* Destination type */
84 "gziptoany" /* Filter program to run */
92 static int compare_active_jobs(void *first
, void *second
, void *data
);
93 static int compare_jobs(void *first
, void *second
, void *data
);
94 static int ipp_length(ipp_t
*ipp
);
95 static void load_job_cache(const char *filename
);
96 static void load_next_job_id(const char *filename
);
97 static void load_request_root(void);
98 static void set_time(cupsd_job_t
*job
, const char *name
);
99 static void set_hold_until(cupsd_job_t
*job
, time_t holdtime
);
100 static void start_job(cupsd_job_t
*job
, cupsd_printer_t
*printer
);
101 static void unload_job(cupsd_job_t
*job
);
102 static void update_job(cupsd_job_t
*job
);
103 static void update_job_attrs(cupsd_job_t
*job
, int do_message
);
107 * 'cupsdAddJob()' - Add a new job to the job queue...
110 cupsd_job_t
* /* O - New job record */
111 cupsdAddJob(int priority
, /* I - Job priority */
112 const char *dest
) /* I - Job destination */
114 cupsd_job_t
*job
; /* New job record */
117 if ((job
= calloc(sizeof(cupsd_job_t
), 1)) == NULL
)
120 job
->id
= NextJobId
++;
121 job
->priority
= priority
;
122 job
->back_pipes
[0] = -1;
123 job
->back_pipes
[1] = -1;
124 job
->print_pipes
[0] = -1;
125 job
->print_pipes
[1] = -1;
126 job
->side_pipes
[0] = -1;
127 job
->side_pipes
[1] = -1;
128 job
->status_pipes
[0] = -1;
129 job
->status_pipes
[1] = -1;
131 cupsdSetString(&job
->dest
, dest
);
134 * Add the new job to the "all jobs" and "active jobs" lists...
137 cupsArrayAdd(Jobs
, job
);
138 cupsArrayAdd(ActiveJobs
, job
);
145 * 'cupsdCancelJob()' - Cancel the specified print job.
149 cupsdCancelJob(cupsd_job_t
*job
, /* I - Job to cancel */
150 int purge
, /* I - Purge jobs? */
151 ipp_jstate_t newstate
) /* I - New job state */
153 int i
; /* Looping var */
154 char filename
[1024]; /* Job filename */
155 cupsd_printer_t
*printer
; /* Printer used by job */
158 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdCancelJob: id = %d", job
->id
);
161 * Stop any processes that are working on the current job...
164 printer
= job
->printer
;
166 if (job
->state_value
== IPP_JOB_PROCESSING
)
167 cupsdStopJob(job
, 0);
172 job
->state
->values
[0].integer
= newstate
;
174 job
->state_value
= newstate
;
176 set_time(job
, "time-at-completed");
179 * Send any pending notifications and then expire them...
187 case IPP_JOB_CANCELED
:
188 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, printer
, job
,
189 purge
? "Job purged." : "Job canceled.");
192 case IPP_JOB_ABORTED
:
193 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, printer
, job
,
194 "Job aborted; please consult the error_log file "
198 case IPP_JOB_COMPLETED
:
200 * Clear the printer's printer-state-message and move on...
203 printer
->state_message
[0] = '\0';
205 cupsdSetPrinterState(printer
, IPP_PRINTER_IDLE
, 0);
207 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, printer
, job
,
212 cupsdExpireSubscriptions(NULL
, job
);
215 * Remove the job from the active and printing lists...
218 cupsArrayRemove(ActiveJobs
, job
);
219 cupsArrayRemove(PrintingJobs
, job
);
223 * If we are going to sleep and the PrintingJobs count is now 0, allow the
224 * sleep to happen immediately...
227 if (Sleeping
&& cupsArrayCount(PrintingJobs
) == 0)
229 #endif /* __APPLE__ */
232 * Remove any authentication data...
235 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
236 if (cupsdRemoveFile(filename
) && errno
!= ENOENT
)
237 cupsdLogMessage(CUPSD_LOG_ERROR
,
238 "Unable to remove authentication cache: %s",
241 cupsdClearString(&job
->auth_username
);
242 cupsdClearString(&job
->auth_domain
);
243 cupsdClearString(&job
->auth_password
);
247 * Destroy the credential cache and clear the KRB5CCNAME env var string.
252 krb5_cc_destroy(KerberosContext
, job
->ccache
);
256 cupsdClearString(&job
->ccname
);
257 #endif /* HAVE_GSSAPI */
260 * Remove the print file for good if we aren't preserving jobs or
264 job
->current_file
= 0;
266 if (!JobHistory
|| !JobFiles
|| purge
)
268 for (i
= 1; i
<= job
->num_files
; i
++)
270 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
275 if (job
->num_files
> 0)
277 free(job
->filetypes
);
278 free(job
->compressions
);
281 job
->filetypes
= NULL
;
282 job
->compressions
= NULL
;
286 if (JobHistory
&& !purge
)
289 * Save job state info...
293 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
298 * Remove the job info file...
301 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
,
306 * Remove the job from the "all jobs" list...
309 cupsArrayRemove(Jobs
, job
);
312 * Free all memory used...
323 * 'cupsdCancelJobs()' - Cancel all jobs for the given destination/user...
327 cupsdCancelJobs(const char *dest
, /* I - Destination to cancel */
328 const char *username
, /* I - Username or NULL */
329 int purge
) /* I - Purge jobs? */
331 cupsd_job_t
*job
; /* Current job */
334 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
336 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
338 if (!job
->dest
|| !job
->username
)
341 if (!job
->dest
|| !job
->username
)
344 if ((dest
== NULL
|| !strcmp(job
->dest
, dest
)) &&
345 (username
== NULL
|| !strcmp(job
->username
, username
)))
348 * Cancel all jobs matching this destination/user...
351 cupsdCancelJob(job
, purge
, IPP_JOB_CANCELED
);
360 * 'cupsdCheckJobs()' - Check the pending jobs and start any if the destination
367 cupsd_job_t
*job
; /* Current job in queue */
368 cupsd_printer_t
*printer
, /* Printer destination */
369 *pclass
; /* Printer class destination */
370 ipp_attribute_t
*attr
; /* Job attribute */
373 DEBUG_puts("cupsdCheckJobs()");
375 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
376 "cupsdCheckJobs: %d active jobs, sleeping=%d, reload=%d",
377 cupsArrayCount(ActiveJobs
), Sleeping
, NeedReload
);
379 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
381 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
384 * Start held jobs if they are ready...
387 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
388 "cupsdCheckJobs: Job %d: dest=%s, dtype=%x, "
389 "state_value=%d, loaded=%s", job
->id
, job
->dest
, job
->dtype
,
390 job
->state_value
, job
->attrs
? "yes" : "no");
392 if (job
->state_value
== IPP_JOB_HELD
&&
394 job
->hold_until
< time(NULL
))
396 if (job
->pending_timeout
)
398 /* Add trailing banner as needed */
399 if (cupsdTimeoutJob(job
))
403 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
404 job
->state_value
= IPP_JOB_PENDING
;
406 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
407 IPP_TAG_KEYWORD
)) == NULL
)
408 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
412 attr
->value_tag
= IPP_TAG_KEYWORD
;
413 cupsdSetString(&(attr
->values
[0].string
.text
), "no-hold");
415 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
420 * Start pending jobs if the destination is available...
423 if (job
->state_value
== IPP_JOB_PENDING
&& !NeedReload
&& !Sleeping
)
425 printer
= cupsdFindDest(job
->dest
);
429 (printer
->type
& (CUPS_PRINTER_IMPLICIT
| CUPS_PRINTER_CLASS
)))
432 * If the class is remote, just pass it to the remote server...
437 if (pclass
->state
== IPP_PRINTER_STOPPED
)
439 else if (pclass
->type
& CUPS_PRINTER_REMOTE
)
442 printer
= cupsdFindAvailablePrinter(printer
->name
);
445 if (!printer
&& !pclass
)
448 * Whoa, the printer and/or class for this destination went away;
452 cupsdLogMessage(CUPSD_LOG_WARN
,
453 "[Job %d] Printer/class %s has gone away; canceling "
454 "job!", job
->id
, job
->dest
);
456 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
457 "Job canceled because the destination printer/class has "
460 cupsdCancelJob(job
, 1, IPP_JOB_ABORTED
);
462 else if (printer
&& !printer
->holding_new_jobs
)
465 * See if the printer is available or remote and not printing a job;
466 * if so, start the job...
472 * Add/update a job-actual-printer-uri attribute for this job
473 * so that we know which printer actually printed the job...
476 if ((attr
= ippFindAttribute(job
->attrs
, "job-actual-printer-uri",
477 IPP_TAG_URI
)) != NULL
)
478 cupsdSetString(&attr
->values
[0].string
.text
, printer
->uri
);
480 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
,
481 "job-actual-printer-uri", NULL
, printer
->uri
);
484 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
487 if ((!(printer
->type
& CUPS_PRINTER_DISCOVERED
) && /* Printer is local */
488 printer
->state
== IPP_PRINTER_IDLE
) || /* and idle, OR */
489 ((printer
->type
& CUPS_PRINTER_DISCOVERED
) && /* Printer is remote */
490 !printer
->job
)) /* and not printing */
493 * Clear any message for the queue...
496 printer
->state_message
[0] = '\0';
502 start_job(job
, printer
);
511 * 'cupsdCleanJobs()' - Clean out old jobs.
517 cupsd_job_t
*job
; /* Current job */
523 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
524 job
&& cupsArrayCount(Jobs
) >= MaxJobs
;
525 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
526 if (job
->state_value
>= IPP_JOB_CANCELED
)
527 cupsdCancelJob(job
, 1, IPP_JOB_CANCELED
);
532 * 'cupsdDeleteJob()' - Free all memory used by a job.
536 cupsdDeleteJob(cupsd_job_t
*job
) /* I - Job */
538 cupsdClearString(&job
->username
);
539 cupsdClearString(&job
->dest
);
540 cupsdClearString(&job
->auth_username
);
541 cupsdClearString(&job
->auth_domain
);
542 cupsdClearString(&job
->auth_password
);
546 * Destroy the credential cache and clear the KRB5CCNAME env var string.
551 krb5_cc_destroy(KerberosContext
, job
->ccache
);
555 cupsdClearString(&job
->ccname
);
556 #endif /* HAVE_GSSAPI */
558 if (job
->num_files
> 0)
560 free(job
->compressions
);
561 free(job
->filetypes
);
564 ippDelete(job
->attrs
);
571 * 'cupsdFinishJob()' - Finish a job.
575 cupsdFinishJob(cupsd_job_t
*job
) /* I - Job */
577 cupsd_printer_t
*printer
; /* Current printer */
578 ipp_attribute_t
*attr
; /* job-hold-until attribute */
581 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "File %d is complete.",
582 job
->current_file
- 1);
584 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
585 "cupsdFinishJob: job->status is %d",
588 if (job
->status_buffer
&&
589 (job
->status
< 0 || job
->current_file
>= job
->num_files
))
592 * Close the pipe and clear the input bit.
595 cupsdRemoveSelect(job
->status_buffer
->fd
);
597 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
598 "cupsdFinishJob: Closing status pipes [ %d %d ]...",
599 job
->status_pipes
[0], job
->status_pipes
[1]);
601 cupsdClosePipe(job
->status_pipes
);
602 cupsdStatBufDelete(job
->status_buffer
);
604 job
->status_buffer
= NULL
;
607 printer
= job
->printer
;
609 update_job_attrs(job
, 0);
612 * Clear the "connecting-to-device" reason, which is only valid when a
613 * printer is processing...
616 cupsdSetPrinterReasons(printer
, "-connecting-to-device");
621 * Backend had errors; stop it...
624 int exit_code
; /* Exit code from backend */
628 * Convert the status to an exit code. Due to the way the W* macros are
629 * implemented on MacOS X (bug?), we have to store the exit status in a
630 * variable first and then convert...
633 exit_code
= -job
->status
;
634 if (WIFEXITED(exit_code
))
635 exit_code
= WEXITSTATUS(exit_code
);
637 exit_code
= job
->status
;
639 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Backend returned status %d (%s)",
641 exit_code
== CUPS_BACKEND_FAILED
? "failed" :
642 exit_code
== CUPS_BACKEND_AUTH_REQUIRED
?
643 "authentication required" :
644 exit_code
== CUPS_BACKEND_HOLD
? "hold job" :
645 exit_code
== CUPS_BACKEND_STOP
? "stop printer" :
646 exit_code
== CUPS_BACKEND_CANCEL
? "cancel job" :
647 exit_code
< 0 ? "crashed" : "unknown");
650 * Do what needs to be done...
656 case CUPS_BACKEND_FAILED
:
658 * Backend failure, use the error-policy to determine how to
662 cupsdStopJob(job
, 0);
664 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
667 * Mark the job as pending again - we'll retry on another
671 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
672 job
->state_value
= IPP_JOB_PENDING
;
676 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
679 * If the job was queued to a class or the error policy is
680 * "retry-current-job", try requeuing it... For faxes and retry-job
681 * queues, hold the current job for 5 minutes.
684 if ((job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
)) ||
685 !strcmp(printer
->error_policy
, "retry-current-job"))
687 else if ((printer
->type
& CUPS_PRINTER_FAX
) ||
688 !strcmp(printer
->error_policy
, "retry-job"))
691 * See how many times we've tried to send the job; if more than
692 * the limit, cancel the job.
697 if (job
->tries
>= JobRetryLimit
)
703 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
704 "Canceling job since it could not be "
705 "sent after %d tries.",
708 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
713 * Try again in N seconds...
716 set_hold_until(job
, time(NULL
) + JobRetryInterval
);
718 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
,
719 "Job held due to fax errors; please consult "
720 "the error_log file for details.");
721 cupsdSetPrinterState(printer
, IPP_PRINTER_IDLE
, 0);
724 else if (!strcmp(printer
->error_policy
, "abort-job"))
725 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
728 cupsdSetPrinterState(printer
, IPP_PRINTER_STOPPED
, 1);
730 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
731 "Job stopped due to backend errors; please consult "
732 "the error_log file for details.");
736 case CUPS_BACKEND_CANCEL
:
741 cupsdCancelJob(job
, 0, IPP_JOB_CANCELED
);
744 case CUPS_BACKEND_HOLD
:
749 cupsdStopJob(job
, 0);
751 cupsdSetJobHoldUntil(job
, "indefinite");
753 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
754 job
->state_value
= IPP_JOB_HELD
;
757 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
759 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
760 "Job held due to backend errors; please consult "
761 "the error_log file for details.");
764 case CUPS_BACKEND_STOP
:
766 * Stop the printer...
769 cupsdStopJob(job
, 0);
771 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
772 job
->state_value
= IPP_JOB_PENDING
;
775 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
777 cupsdSetPrinterState(printer
, IPP_PRINTER_STOPPED
, 1);
779 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
780 "Job stopped due to backend errors; please consult "
781 "the error_log file for details.");
784 case CUPS_BACKEND_AUTH_REQUIRED
:
785 cupsdStopJob(job
, 0);
787 cupsdSetJobHoldUntil(job
, "auth-info-required");
789 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
790 IPP_TAG_KEYWORD
)) == NULL
)
791 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
795 attr
->value_tag
= IPP_TAG_KEYWORD
;
796 cupsdSetString(&(attr
->values
[0].string
.text
),
797 "auth-info-required");
800 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
801 job
->state_value
= IPP_JOB_HELD
;
804 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
806 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
807 "Authentication is required for job %d.", job
->id
);
812 * Try printing another job...
817 else if (job
->status
> 0)
820 * Filter had errors; stop job...
823 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Job stopped due to filter errors.");
824 cupsdStopJob(job
, 1);
826 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
827 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
828 "Job stopped due to filter errors; please consult the "
829 "error_log file for details.");
835 * Job printed successfully; cancel it...
838 if (job
->current_file
< job
->num_files
)
841 * Start the next file in the job...
844 FilterLevel
-= job
->cost
;
845 start_job(job
, printer
);
850 * Close out this job...
853 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Completed successfully.");
854 cupsdCancelJob(job
, 0, IPP_JOB_COMPLETED
);
862 * 'cupsdFreeAllJobs()' - Free all jobs from memory.
866 cupsdFreeAllJobs(void)
868 cupsd_job_t
*job
; /* Current job */
879 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
881 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
883 cupsArrayRemove(Jobs
, job
);
884 cupsArrayRemove(ActiveJobs
, job
);
885 cupsArrayRemove(PrintingJobs
, job
);
890 cupsdReleaseSignals();
895 * 'cupsdFindJob()' - Find the specified job.
898 cupsd_job_t
* /* O - Job data */
899 cupsdFindJob(int id
) /* I - Job ID */
901 cupsd_job_t key
; /* Search key */
906 return ((cupsd_job_t
*)cupsArrayFind(Jobs
, &key
));
911 * 'cupsdGetPrinterJobCount()' - Get the number of pending, processing,
912 * or held jobs in a printer or class.
915 int /* O - Job count */
916 cupsdGetPrinterJobCount(
917 const char *dest
) /* I - Printer or class name */
919 int count
; /* Job count */
920 cupsd_job_t
*job
; /* Current job */
923 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
925 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
926 if (job
->dest
&& !strcasecmp(job
->dest
, dest
))
934 * 'cupsdGetUserJobCount()' - Get the number of pending, processing,
935 * or held jobs for a user.
938 int /* O - Job count */
939 cupsdGetUserJobCount(
940 const char *username
) /* I - Username */
942 int count
; /* Job count */
943 cupsd_job_t
*job
; /* Current job */
946 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
948 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
949 if (!strcasecmp(job
->username
, username
))
957 * 'cupsdHoldJob()' - Hold the specified job.
961 cupsdHoldJob(cupsd_job_t
*job
) /* I - Job data */
963 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdHoldJob: id = %d", job
->id
);
965 if (job
->state_value
== IPP_JOB_PROCESSING
)
966 cupsdStopJob(job
, 0);
970 DEBUG_puts("cupsdHoldJob: setting state to held...");
972 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
973 job
->state_value
= IPP_JOB_HELD
;
974 job
->current_file
= 0;
977 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
982 * 'cupsdLoadAllJobs()' - Load all jobs from disk.
986 cupsdLoadAllJobs(void)
988 char filename
[1024]; /* Full filename of job.cache file */
989 struct stat fileinfo
, /* Information on job.cache file */
990 dirinfo
; /* Information on RequestRoot dir */
995 * Create the job arrays as needed...
999 Jobs
= cupsArrayNew(compare_jobs
, NULL
);
1002 ActiveJobs
= cupsArrayNew(compare_active_jobs
, NULL
);
1005 PrintingJobs
= cupsArrayNew(compare_jobs
, NULL
);
1008 * See whether the job.cache file is older than the RequestRoot directory...
1011 snprintf(filename
, sizeof(filename
), "%s/job.cache", CacheDir
);
1013 if (stat(filename
, &fileinfo
))
1015 fileinfo
.st_mtime
= 0;
1017 if (errno
!= ENOENT
)
1018 cupsdLogMessage(CUPSD_LOG_ERROR
,
1019 "Unable to get file information for \"%s\" - %s",
1020 filename
, strerror(errno
));
1023 if (stat(RequestRoot
, &dirinfo
))
1025 dirinfo
.st_mtime
= 0;
1027 if (errno
!= ENOENT
)
1028 cupsdLogMessage(CUPSD_LOG_ERROR
,
1029 "Unable to get directory information for \"%s\" - %s",
1030 RequestRoot
, strerror(errno
));
1034 * Load the most recent source for job data...
1037 if (dirinfo
.st_mtime
> fileinfo
.st_mtime
)
1039 load_request_root();
1041 load_next_job_id(filename
);
1044 load_job_cache(filename
);
1047 * Clean out old jobs as needed...
1050 if (MaxJobs
> 0 && cupsArrayCount(Jobs
) >= MaxJobs
)
1056 * 'cupsdLoadJob()' - Load a single job...
1060 cupsdLoadJob(cupsd_job_t
*job
) /* I - Job */
1062 char jobfile
[1024]; /* Job filename */
1063 cups_file_t
*fp
; /* Job file */
1064 int fileid
; /* Current file ID */
1065 ipp_attribute_t
*attr
; /* Job attribute */
1066 const char *dest
; /* Destination name */
1067 cupsd_printer_t
*destptr
; /* Pointer to destination */
1068 mime_type_t
**filetypes
; /* New filetypes array */
1069 int *compressions
; /* New compressions array */
1074 if (job
->state_value
> IPP_JOB_STOPPED
)
1075 job
->access_time
= time(NULL
);
1080 if ((job
->attrs
= ippNew()) == NULL
)
1082 cupsdLogMessage(CUPSD_LOG_ERROR
,
1083 "[Job %d] Ran out of memory for job attributes!", job
->id
);
1088 * Load job attributes...
1091 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Loading attributes...", job
->id
);
1093 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, job
->id
);
1094 if ((fp
= cupsFileOpen(jobfile
, "r")) == NULL
)
1096 cupsdLogMessage(CUPSD_LOG_ERROR
,
1097 "[Job %d] Unable to open job control file \"%s\" - %s!",
1098 job
->id
, jobfile
, strerror(errno
));
1099 ippDelete(job
->attrs
);
1104 if (ippReadIO(fp
, (ipp_iocb_t
)cupsFileRead
, 1, NULL
, job
->attrs
) != IPP_DATA
)
1106 cupsdLogMessage(CUPSD_LOG_ERROR
,
1107 "[Job %d] Unable to read job control file \"%s\"!", job
->id
,
1110 ippDelete(job
->attrs
);
1119 * Copy attribute data to the job object...
1122 if (!ippFindAttribute(job
->attrs
, "time-at-creation", IPP_TAG_INTEGER
))
1124 cupsdLogMessage(CUPSD_LOG_ERROR
,
1125 "[Job %d] Missing or bad time-at-creation attribute in "
1126 "control file!", job
->id
);
1127 ippDelete(job
->attrs
);
1133 if ((job
->state
= ippFindAttribute(job
->attrs
, "job-state",
1134 IPP_TAG_ENUM
)) == NULL
)
1136 cupsdLogMessage(CUPSD_LOG_ERROR
,
1137 "[Job %d] Missing or bad job-state attribute in control "
1139 ippDelete(job
->attrs
);
1145 job
->state_value
= (ipp_jstate_t
)job
->state
->values
[0].integer
;
1149 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1150 IPP_TAG_URI
)) == NULL
)
1152 cupsdLogMessage(CUPSD_LOG_ERROR
,
1153 "[Job %d] No job-printer-uri attribute in control file!",
1155 ippDelete(job
->attrs
);
1161 if ((dest
= cupsdValidateDest(attr
->values
[0].string
.text
, &(job
->dtype
),
1164 cupsdLogMessage(CUPSD_LOG_ERROR
,
1165 "[Job %d] Unable to queue job for destination \"%s\"!",
1166 job
->id
, attr
->values
[0].string
.text
);
1167 ippDelete(job
->attrs
);
1173 cupsdSetString(&job
->dest
, dest
);
1175 else if ((destptr
= cupsdFindDest(job
->dest
)) == NULL
)
1177 cupsdLogMessage(CUPSD_LOG_ERROR
,
1178 "[Job %d] Unable to queue job for destination \"%s\"!",
1179 job
->id
, job
->dest
);
1180 ippDelete(job
->attrs
);
1186 job
->sheets
= ippFindAttribute(job
->attrs
, "job-media-sheets-completed",
1188 job
->job_sheets
= ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_NAME
);
1192 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
1193 IPP_TAG_INTEGER
)) == NULL
)
1195 cupsdLogMessage(CUPSD_LOG_ERROR
,
1196 "[Job %d] Missing or bad job-priority attribute in "
1197 "control file!", job
->id
);
1198 ippDelete(job
->attrs
);
1204 job
->priority
= attr
->values
[0].integer
;
1209 if ((attr
= ippFindAttribute(job
->attrs
, "job-originating-user-name",
1210 IPP_TAG_NAME
)) == NULL
)
1212 cupsdLogMessage(CUPSD_LOG_ERROR
,
1213 "[Job %d] Missing or bad job-originating-user-name "
1214 "attribute in control file!", job
->id
);
1215 ippDelete(job
->attrs
);
1221 cupsdSetString(&job
->username
, attr
->values
[0].string
.text
);
1225 * Set the job hold-until time and state...
1228 if (job
->state_value
== IPP_JOB_HELD
)
1230 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1231 IPP_TAG_KEYWORD
)) == NULL
)
1232 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1235 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
1238 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1239 job
->state_value
= IPP_JOB_PENDING
;
1242 else if (job
->state_value
== IPP_JOB_PROCESSING
)
1244 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1245 job
->state_value
= IPP_JOB_PENDING
;
1248 if (!job
->num_files
)
1251 * Find all the d##### files...
1254 for (fileid
= 1; fileid
< 10000; fileid
++)
1256 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
1259 if (access(jobfile
, 0))
1262 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1263 "[Job %d] Auto-typing document file \"%s\"...", job
->id
,
1266 if (fileid
> job
->num_files
)
1268 if (job
->num_files
== 0)
1270 compressions
= (int *)calloc(fileid
, sizeof(int));
1271 filetypes
= (mime_type_t
**)calloc(fileid
, sizeof(mime_type_t
*));
1275 compressions
= (int *)realloc(job
->compressions
,
1276 sizeof(int) * fileid
);
1277 filetypes
= (mime_type_t
**)realloc(job
->filetypes
,
1278 sizeof(mime_type_t
*) *
1282 if (!compressions
|| !filetypes
)
1284 cupsdLogMessage(CUPSD_LOG_ERROR
,
1285 "[Job %d] Ran out of memory for job file types!",
1290 job
->compressions
= compressions
;
1291 job
->filetypes
= filetypes
;
1292 job
->num_files
= fileid
;
1295 job
->filetypes
[fileid
- 1] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
1296 job
->compressions
+ fileid
- 1);
1298 if (!job
->filetypes
[fileid
- 1])
1299 job
->filetypes
[fileid
- 1] = mimeType(MimeDatabase
, "application",
1305 * Load authentication information as needed...
1308 if (job
->state_value
< IPP_JOB_STOPPED
)
1310 snprintf(jobfile
, sizeof(jobfile
), "%s/a%05d", RequestRoot
, job
->id
);
1312 cupsdClearString(&job
->auth_username
);
1313 cupsdClearString(&job
->auth_domain
);
1314 cupsdClearString(&job
->auth_password
);
1316 if ((fp
= cupsFileOpen(jobfile
, "r")) != NULL
)
1318 int i
, /* Looping var */
1319 bytes
; /* Size of auth data */
1320 char line
[255], /* Line from file */
1321 data
[255]; /* Decoded data */
1325 i
< destptr
->num_auth_info_required
&&
1326 cupsFileGets(fp
, line
, sizeof(line
));
1329 bytes
= sizeof(data
);
1330 httpDecode64_2(data
, &bytes
, line
);
1332 if (!strcmp(destptr
->auth_info_required
[i
], "username"))
1333 cupsdSetStringf(&job
->auth_username
, "AUTH_USERNAME=%s", data
);
1334 else if (!strcmp(destptr
->auth_info_required
[i
], "domain"))
1335 cupsdSetStringf(&job
->auth_domain
, "AUTH_DOMAIN=%s", data
);
1336 else if (!strcmp(destptr
->auth_info_required
[i
], "password"))
1337 cupsdSetStringf(&job
->auth_password
, "AUTH_PASSWORD=%s", data
);
1344 job
->access_time
= time(NULL
);
1349 * 'cupsdMoveJob()' - Move the specified job to a different destination.
1353 cupsdMoveJob(cupsd_job_t
*job
, /* I - Job */
1354 cupsd_printer_t
*p
) /* I - Destination printer or class */
1356 ipp_attribute_t
*attr
; /* job-printer-uri attribute */
1357 const char *olddest
; /* Old destination */
1358 cupsd_printer_t
*oldp
; /* Old pointer */
1362 * Don't move completed jobs...
1365 if (job
->state_value
> IPP_JOB_STOPPED
)
1369 * Get the old destination...
1372 olddest
= job
->dest
;
1375 oldp
= job
->printer
;
1377 oldp
= cupsdFindDest(olddest
);
1380 * Change the destination information...
1383 if (job
->state_value
== IPP_JOB_PROCESSING
)
1384 cupsdStopJob(job
, 0);
1388 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, oldp
, job
,
1389 "Job #%d moved from %s to %s.", job
->id
, olddest
,
1392 cupsdSetString(&job
->dest
, p
->name
);
1393 job
->dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_REMOTE
|
1394 CUPS_PRINTER_IMPLICIT
);
1396 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1397 IPP_TAG_URI
)) != NULL
)
1398 cupsdSetString(&(attr
->values
[0].string
.text
), p
->uri
);
1400 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, p
, job
,
1401 "Job #%d moved from %s to %s.", job
->id
, olddest
,
1405 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1410 * 'cupsdReleaseJob()' - Release the specified job.
1414 cupsdReleaseJob(cupsd_job_t
*job
) /* I - Job */
1416 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdReleaseJob: id = %d", job
->id
);
1418 if (job
->state_value
== IPP_JOB_HELD
)
1421 * Add trailing banner as needed...
1424 if (job
->pending_timeout
)
1425 cupsdTimeoutJob(job
);
1427 DEBUG_puts("cupsdReleaseJob: setting state to pending...");
1429 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1430 job
->state_value
= IPP_JOB_PENDING
;
1432 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1438 * 'cupsdRestartJob()' - Restart the specified job.
1442 cupsdRestartJob(cupsd_job_t
*job
) /* I - Job */
1444 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdRestartJob: id = %d", job
->id
);
1446 if (job
->state_value
== IPP_JOB_STOPPED
|| job
->num_files
)
1448 ipp_jstate_t old_state
; /* Old job state */
1453 old_state
= job
->state_value
;
1456 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1457 job
->state_value
= IPP_JOB_PENDING
;
1460 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1462 if (old_state
> IPP_JOB_STOPPED
)
1463 cupsArrayAdd(ActiveJobs
, job
);
1469 * 'cupsdSaveAllJobs()' - Save a summary of all jobs to disk.
1473 cupsdSaveAllJobs(void)
1475 int i
; /* Looping var */
1476 cups_file_t
*fp
; /* Job cache file */
1477 char temp
[1024]; /* Temporary string */
1478 cupsd_job_t
*job
; /* Current job */
1479 time_t curtime
; /* Current time */
1480 struct tm
*curdate
; /* Current date */
1483 snprintf(temp
, sizeof(temp
), "%s/job.cache", CacheDir
);
1484 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
1486 cupsdLogMessage(CUPSD_LOG_ERROR
,
1487 "Unable to create job cache file \"%s\" - %s",
1488 temp
, strerror(errno
));
1492 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving job cache file \"%s\"...", temp
);
1495 * Restrict access to the file...
1498 fchown(cupsFileNumber(fp
), getuid(), Group
);
1499 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
1502 * Write a small header to the file...
1505 curtime
= time(NULL
);
1506 curdate
= localtime(&curtime
);
1507 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
1509 cupsFilePuts(fp
, "# Job cache file for " CUPS_SVERSION
"\n");
1510 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
1511 cupsFilePrintf(fp
, "NextJobId %d\n", NextJobId
);
1514 * Write each job known to the system...
1517 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1519 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1521 cupsFilePrintf(fp
, "<Job %d>\n", job
->id
);
1522 cupsFilePrintf(fp
, "State %d\n", job
->state_value
);
1523 cupsFilePrintf(fp
, "Priority %d\n", job
->priority
);
1524 cupsFilePrintf(fp
, "Username %s\n", job
->username
);
1525 cupsFilePrintf(fp
, "Destination %s\n", job
->dest
);
1526 cupsFilePrintf(fp
, "DestType %d\n", job
->dtype
);
1527 cupsFilePrintf(fp
, "NumFiles %d\n", job
->num_files
);
1528 for (i
= 0; i
< job
->num_files
; i
++)
1529 cupsFilePrintf(fp
, "File %d %s/%s %d\n", i
+ 1, job
->filetypes
[i
]->super
,
1530 job
->filetypes
[i
]->type
, job
->compressions
[i
]);
1531 cupsFilePuts(fp
, "</Job>\n");
1539 * 'cupsdSaveJob()' - Save a job to disk.
1543 cupsdSaveJob(cupsd_job_t
*job
) /* I - Job */
1545 char filename
[1024]; /* Job control filename */
1546 cups_file_t
*fp
; /* Job file */
1549 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSaveJob(job=%p(%d)): job->attrs=%p",
1550 job
, job
->id
, job
->attrs
);
1552 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
, job
->id
);
1554 if ((fp
= cupsFileOpen(filename
, "w")) == NULL
)
1556 cupsdLogMessage(CUPSD_LOG_ERROR
,
1557 "[Job %d] Unable to create job control file \"%s\" - %s.",
1558 job
->id
, filename
, strerror(errno
));
1562 fchmod(cupsFileNumber(fp
), 0600);
1563 fchown(cupsFileNumber(fp
), RunUser
, Group
);
1565 job
->attrs
->state
= IPP_IDLE
;
1567 if (ippWriteIO(fp
, (ipp_iocb_t
)cupsFileWrite
, 1, NULL
,
1568 job
->attrs
) != IPP_DATA
)
1569 cupsdLogMessage(CUPSD_LOG_ERROR
,
1570 "[Job %d] Unable to write job control file!", job
->id
);
1579 * 'cupsdSetJobHoldUntil()' - Set the hold time for a job...
1583 cupsdSetJobHoldUntil(cupsd_job_t
*job
, /* I - Job */
1584 const char *when
) /* I - When to resume */
1586 time_t curtime
; /* Current time */
1587 struct tm
*curdate
; /* Current date */
1588 int hour
; /* Hold hour */
1589 int minute
; /* Hold minute */
1590 int second
; /* Hold second */
1593 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetJobHoldUntil(%d, \"%s\")",
1598 if (!strcmp(when
, "indefinite") || !strcmp(when
, "auth-info-required"))
1601 * Hold indefinitely...
1604 job
->hold_until
= 0;
1606 else if (!strcmp(when
, "day-time"))
1609 * Hold to 6am the next morning unless local time is < 6pm.
1612 curtime
= time(NULL
);
1613 curdate
= localtime(&curtime
);
1615 if (curdate
->tm_hour
< 18)
1616 job
->hold_until
= curtime
;
1618 job
->hold_until
= curtime
+
1619 ((29 - curdate
->tm_hour
) * 60 + 59 -
1620 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1622 else if (!strcmp(when
, "evening") || !strcmp(when
, "night"))
1625 * Hold to 6pm unless local time is > 6pm or < 6am.
1628 curtime
= time(NULL
);
1629 curdate
= localtime(&curtime
);
1631 if (curdate
->tm_hour
< 6 || curdate
->tm_hour
>= 18)
1632 job
->hold_until
= curtime
;
1634 job
->hold_until
= curtime
+
1635 ((17 - curdate
->tm_hour
) * 60 + 59 -
1636 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1638 else if (!strcmp(when
, "second-shift"))
1641 * Hold to 4pm unless local time is > 4pm.
1644 curtime
= time(NULL
);
1645 curdate
= localtime(&curtime
);
1647 if (curdate
->tm_hour
>= 16)
1648 job
->hold_until
= curtime
;
1650 job
->hold_until
= curtime
+
1651 ((15 - curdate
->tm_hour
) * 60 + 59 -
1652 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1654 else if (!strcmp(when
, "third-shift"))
1657 * Hold to 12am unless local time is < 8am.
1660 curtime
= time(NULL
);
1661 curdate
= localtime(&curtime
);
1663 if (curdate
->tm_hour
< 8)
1664 job
->hold_until
= curtime
;
1666 job
->hold_until
= curtime
+
1667 ((23 - curdate
->tm_hour
) * 60 + 59 -
1668 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1670 else if (!strcmp(when
, "weekend"))
1673 * Hold to weekend unless we are in the weekend.
1676 curtime
= time(NULL
);
1677 curdate
= localtime(&curtime
);
1679 if (curdate
->tm_wday
== 0 || curdate
->tm_wday
== 6)
1680 job
->hold_until
= curtime
;
1682 job
->hold_until
= curtime
+
1683 (((5 - curdate
->tm_wday
) * 24 +
1684 (17 - curdate
->tm_hour
)) * 60 + 59 -
1685 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1687 else if (sscanf(when
, "%d:%d:%d", &hour
, &minute
, &second
) >= 2)
1690 * Hold to specified GMT time (HH:MM or HH:MM:SS)...
1693 curtime
= time(NULL
);
1694 curdate
= gmtime(&curtime
);
1696 job
->hold_until
= curtime
+
1697 ((hour
- curdate
->tm_hour
) * 60 + minute
-
1698 curdate
->tm_min
) * 60 + second
- curdate
->tm_sec
;
1701 * Hold until next day as needed...
1704 if (job
->hold_until
< curtime
)
1705 job
->hold_until
+= 24 * 60 * 60;
1708 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetJobHoldUntil: hold_until = %d",
1709 (int)job
->hold_until
);
1714 * 'cupsdSetJobPriority()' - Set the priority of a job, moving it up/down in
1715 * the list as needed.
1719 cupsdSetJobPriority(
1720 cupsd_job_t
*job
, /* I - Job ID */
1721 int priority
) /* I - New priority (0 to 100) */
1723 ipp_attribute_t
*attr
; /* Job attribute */
1727 * Don't change completed jobs...
1730 if (job
->state_value
>= IPP_JOB_PROCESSING
)
1734 * Set the new priority and re-add the job into the active list...
1737 cupsArrayRemove(ActiveJobs
, job
);
1739 job
->priority
= priority
;
1741 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
1742 IPP_TAG_INTEGER
)) != NULL
)
1743 attr
->values
[0].integer
= priority
;
1745 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-priority",
1748 cupsArrayAdd(ActiveJobs
, job
);
1751 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1756 * 'cupsdStopAllJobs()' - Stop all print jobs.
1760 cupsdStopAllJobs(int force
) /* I - 1 = Force all filters to stop */
1762 cupsd_job_t
*job
; /* Current job */
1765 DEBUG_puts("cupsdStopAllJobs()");
1767 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
1769 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
1770 if (job
->state_value
== IPP_JOB_PROCESSING
)
1772 cupsdStopJob(job
, force
);
1773 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1774 job
->state_value
= IPP_JOB_PENDING
;
1780 * 'cupsdStopJob()' - Stop a print job.
1784 cupsdStopJob(cupsd_job_t
*job
, /* I - Job */
1785 int force
) /* I - 1 = Force all filters to stop */
1787 int i
; /* Looping var */
1790 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] cupsdStopJob: force = %d",
1793 if (job
->state_value
!= IPP_JOB_PROCESSING
)
1796 FilterLevel
-= job
->cost
;
1798 if (job
->printer
->state
== IPP_PRINTER_PROCESSING
)
1799 cupsdSetPrinterState(job
->printer
, IPP_PRINTER_IDLE
, 0);
1801 job
->state
->values
[0].integer
= IPP_JOB_STOPPED
;
1802 job
->state_value
= IPP_JOB_STOPPED
;
1803 job
->printer
->job
= NULL
;
1804 job
->printer
= NULL
;
1806 job
->current_file
--;
1808 for (i
= 0; job
->filters
[i
]; i
++)
1809 if (job
->filters
[i
] > 0)
1811 cupsdEndProcess(job
->filters
[i
], force
);
1813 job
->filters
[i
] = 0;
1816 if (job
->backend
> 0)
1818 cupsdEndProcess(job
->backend
, force
);
1826 cupsdDestroyProfile(job
->profile
);
1827 job
->profile
= NULL
;
1829 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] Closing print pipes [ %d %d ]...",
1830 job
->id
, job
->print_pipes
[0], job
->print_pipes
[1]);
1832 cupsdClosePipe(job
->print_pipes
);
1834 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] Closing back pipes [ %d %d ]...",
1835 job
->id
, job
->back_pipes
[0], job
->back_pipes
[1]);
1837 cupsdClosePipe(job
->back_pipes
);
1839 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] Closing side pipes [ %d %d ]...",
1840 job
->id
, job
->side_pipes
[0], job
->side_pipes
[1]);
1842 cupsdClosePipe(job
->side_pipes
);
1844 if (job
->status_buffer
)
1847 * Close the pipe and clear the input bit.
1850 cupsdRemoveSelect(job
->status_buffer
->fd
);
1852 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1853 "[Job %d] Closing status pipes [ %d %d ]...",
1854 job
->id
, job
->status_pipes
[0], job
->status_pipes
[1]);
1856 cupsdClosePipe(job
->status_pipes
);
1857 cupsdStatBufDelete(job
->status_buffer
);
1859 job
->status_buffer
= NULL
;
1865 * 'cupsdUnloadCompletedJobs()' - Flush completed job history from memory.
1869 cupsdUnloadCompletedJobs(void)
1871 cupsd_job_t
*job
; /* Current job */
1872 time_t expire
; /* Expiration time */
1875 expire
= time(NULL
) - 60;
1877 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1879 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1880 if (job
->attrs
&& job
->state_value
>= IPP_JOB_STOPPED
&&
1881 job
->access_time
< expire
)
1892 * 'compare_active_jobs()' - Compare the job IDs and priorities of two jobs.
1895 static int /* O - Difference */
1896 compare_active_jobs(void *first
, /* I - First job */
1897 void *second
, /* I - Second job */
1898 void *data
) /* I - App data (not used) */
1900 int diff
; /* Difference */
1903 if ((diff
= ((cupsd_job_t
*)second
)->priority
-
1904 ((cupsd_job_t
*)first
)->priority
) != 0)
1907 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
1912 * 'compare_jobs()' - Compare the job IDs of two jobs.
1915 static int /* O - Difference */
1916 compare_jobs(void *first
, /* I - First job */
1917 void *second
, /* I - Second job */
1918 void *data
) /* I - App data (not used) */
1920 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
1925 * 'ipp_length()' - Compute the size of the buffer needed to hold
1926 * the textual IPP attributes.
1929 static int /* O - Size of attribute buffer */
1930 ipp_length(ipp_t
*ipp
) /* I - IPP request */
1932 int bytes
; /* Number of bytes */
1933 int i
; /* Looping var */
1934 ipp_attribute_t
*attr
; /* Current attribute */
1938 * Loop through all attributes...
1943 for (attr
= ipp
->attrs
; attr
!= NULL
; attr
= attr
->next
)
1946 * Skip attributes that won't be sent to filters...
1949 if (attr
->value_tag
== IPP_TAG_MIMETYPE
||
1950 attr
->value_tag
== IPP_TAG_NAMELANG
||
1951 attr
->value_tag
== IPP_TAG_TEXTLANG
||
1952 attr
->value_tag
== IPP_TAG_URI
||
1953 attr
->value_tag
== IPP_TAG_URISCHEME
)
1956 if (strncmp(attr
->name
, "time-", 5) == 0)
1960 * Add space for a leading space and commas between each value.
1961 * For the first attribute, the leading space isn't used, so the
1962 * extra byte can be used as the nul terminator...
1965 bytes
++; /* " " separator */
1966 bytes
+= attr
->num_values
; /* "," separators */
1969 * Boolean attributes appear as "foo,nofoo,foo,nofoo", while
1970 * other attributes appear as "foo=value1,value2,...,valueN".
1973 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
1974 bytes
+= strlen(attr
->name
);
1976 bytes
+= attr
->num_values
* strlen(attr
->name
);
1979 * Now add the size required for each value in the attribute...
1982 switch (attr
->value_tag
)
1984 case IPP_TAG_INTEGER
:
1987 * Minimum value of a signed integer is -2147483647, or 11 digits.
1990 bytes
+= attr
->num_values
* 11;
1993 case IPP_TAG_BOOLEAN
:
1995 * Add two bytes for each false ("no") value...
1998 for (i
= 0; i
< attr
->num_values
; i
++)
1999 if (!attr
->values
[i
].boolean
)
2003 case IPP_TAG_RANGE
:
2005 * A range is two signed integers separated by a hyphen, or
2006 * 23 characters max.
2009 bytes
+= attr
->num_values
* 23;
2012 case IPP_TAG_RESOLUTION
:
2014 * A resolution is two signed integers separated by an "x" and
2015 * suffixed by the units, or 26 characters max.
2018 bytes
+= attr
->num_values
* 26;
2021 case IPP_TAG_STRING
:
2024 case IPP_TAG_KEYWORD
:
2025 case IPP_TAG_CHARSET
:
2026 case IPP_TAG_LANGUAGE
:
2029 * Strings can contain characters that need quoting. We need
2030 * at least 2 * len + 2 characters to cover the quotes and
2031 * any backslashes in the string.
2034 for (i
= 0; i
< attr
->num_values
; i
++)
2035 bytes
+= 2 * strlen(attr
->values
[i
].string
.text
) + 2;
2039 break; /* anti-compiler-warning-code */
2048 * 'load_job_cache()' - Load jobs from the job.cache file.
2052 load_job_cache(const char *filename
) /* I - job.cache filename */
2054 cups_file_t
*fp
; /* job.cache file */
2055 char line
[1024], /* Line buffer */
2056 *value
; /* Value on line */
2057 int linenum
; /* Line number in file */
2058 cupsd_job_t
*job
; /* Current job */
2059 int jobid
; /* Job ID */
2060 char jobfile
[1024]; /* Job filename */
2064 * Open the job.cache file...
2067 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
2069 if (errno
!= ENOENT
)
2070 cupsdLogMessage(CUPSD_LOG_ERROR
,
2071 "Unable to open job cache file \"%s\": %s",
2072 filename
, strerror(errno
));
2074 load_request_root();
2080 * Read entries from the job cache file and create jobs as needed.
2083 cupsdLogMessage(CUPSD_LOG_INFO
, "Loading job cache file \"%s\"...",
2089 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
2091 if (!strcasecmp(line
, "NextJobId"))
2094 NextJobId
= atoi(value
);
2096 else if (!strcasecmp(line
, "<Job"))
2100 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing </Job> directive on line %d!",
2107 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing job ID on line %d!", linenum
);
2111 jobid
= atoi(value
);
2115 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad job ID %d on line %d!", jobid
,
2120 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, jobid
);
2121 if (access(jobfile
, 0))
2123 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Job %d] Files have gone away!",
2128 job
= calloc(1, sizeof(cupsd_job_t
));
2131 cupsdLogMessage(CUPSD_LOG_EMERG
,
2132 "[Job %d] Unable to allocate memory for job!", jobid
);
2137 job
->back_pipes
[0] = -1;
2138 job
->back_pipes
[1] = -1;
2139 job
->print_pipes
[0] = -1;
2140 job
->print_pipes
[1] = -1;
2141 job
->side_pipes
[0] = -1;
2142 job
->side_pipes
[1] = -1;
2143 job
->status_pipes
[0] = -1;
2144 job
->status_pipes
[1] = -1;
2146 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Loading from cache...",
2151 cupsdLogMessage(CUPSD_LOG_ERROR
,
2152 "Missing <Job #> directive on line %d!", linenum
);
2155 else if (!strcasecmp(line
, "</Job>"))
2157 cupsArrayAdd(Jobs
, job
);
2159 if (job
->state_value
<= IPP_JOB_STOPPED
)
2161 cupsArrayAdd(ActiveJobs
, job
);
2169 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing value on line %d!", linenum
);
2172 else if (!strcasecmp(line
, "State"))
2174 job
->state_value
= (ipp_jstate_t
)atoi(value
);
2176 if (job
->state_value
< IPP_JOB_PENDING
)
2177 job
->state_value
= IPP_JOB_PENDING
;
2178 else if (job
->state_value
> IPP_JOB_COMPLETED
)
2179 job
->state_value
= IPP_JOB_COMPLETED
;
2181 else if (!strcasecmp(line
, "Priority"))
2183 job
->priority
= atoi(value
);
2185 else if (!strcasecmp(line
, "Username"))
2187 cupsdSetString(&job
->username
, value
);
2189 else if (!strcasecmp(line
, "Destination"))
2191 cupsdSetString(&job
->dest
, value
);
2193 else if (!strcasecmp(line
, "DestType"))
2195 job
->dtype
= (cups_ptype_t
)atoi(value
);
2197 else if (!strcasecmp(line
, "NumFiles"))
2199 job
->num_files
= atoi(value
);
2201 if (job
->num_files
< 0)
2203 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad NumFiles value %d on line %d!",
2204 job
->num_files
, linenum
);
2209 if (job
->num_files
> 0)
2211 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-001", RequestRoot
,
2213 if (access(jobfile
, 0))
2215 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Data files have gone away!",
2221 job
->filetypes
= calloc(job
->num_files
, sizeof(mime_type_t
*));
2222 job
->compressions
= calloc(job
->num_files
, sizeof(int));
2224 if (!job
->filetypes
|| !job
->compressions
)
2226 cupsdLogMessage(CUPSD_LOG_EMERG
,
2227 "[Job %d] Unable to allocate memory for %d files!",
2228 job
->id
, job
->num_files
);
2233 else if (!strcasecmp(line
, "File"))
2235 int number
, /* File number */
2236 compression
; /* Compression value */
2237 char super
[MIME_MAX_SUPER
], /* MIME super type */
2238 type
[MIME_MAX_TYPE
]; /* MIME type */
2241 if (sscanf(value
, "%d%*[ \t]%15[^/]/%255s%d", &number
, super
, type
,
2244 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File on line %d!", linenum
);
2248 if (number
< 1 || number
> job
->num_files
)
2250 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File number %d on line %d!",
2257 job
->compressions
[number
] = compression
;
2258 job
->filetypes
[number
] = mimeType(MimeDatabase
, super
, type
);
2260 if (!job
->filetypes
[number
])
2263 * If the original MIME type is unknown, auto-type it!
2266 cupsdLogMessage(CUPSD_LOG_ERROR
,
2267 "[Job %d] Unknown MIME type %s/%s for file %d!",
2268 job
->id
, super
, type
, number
+ 1);
2270 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
2271 job
->id
, number
+ 1);
2272 job
->filetypes
[number
] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
2273 job
->compressions
+ number
);
2276 * If that didn't work, assume it is raw...
2279 if (!job
->filetypes
[number
])
2280 job
->filetypes
[number
] = mimeType(MimeDatabase
, "application",
2285 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unknown %s directive on line %d!",
2294 * 'load_next_job_id()' - Load the NextJobId value from the job.cache file.
2298 load_next_job_id(const char *filename
) /* I - job.cache filename */
2300 cups_file_t
*fp
; /* job.cache file */
2301 char line
[1024], /* Line buffer */
2302 *value
; /* Value on line */
2303 int linenum
; /* Line number in file */
2304 int next_job_id
; /* NextJobId value from line */
2308 * Read the NextJobId directive from the job.cache file and use
2309 * the value (if any).
2312 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
2314 if (errno
!= ENOENT
)
2315 cupsdLogMessage(CUPSD_LOG_ERROR
,
2316 "Unable to open job cache file \"%s\": %s",
2317 filename
, strerror(errno
));
2322 cupsdLogMessage(CUPSD_LOG_INFO
,
2323 "Loading NextJobId from job cache file \"%s\"...", filename
);
2327 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
2329 if (!strcasecmp(line
, "NextJobId"))
2333 next_job_id
= atoi(value
);
2335 if (next_job_id
> NextJobId
)
2336 NextJobId
= next_job_id
;
2347 * 'load_request_root()' - Load jobs from the RequestRoot directory.
2351 load_request_root(void)
2353 cups_dir_t
*dir
; /* Directory */
2354 cups_dentry_t
*dent
; /* Directory entry */
2355 cupsd_job_t
*job
; /* New job */
2359 * Open the requests directory...
2362 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Scanning %s for jobs...", RequestRoot
);
2364 if ((dir
= cupsDirOpen(RequestRoot
)) == NULL
)
2366 cupsdLogMessage(CUPSD_LOG_ERROR
,
2367 "Unable to open spool directory \"%s\": %s",
2368 RequestRoot
, strerror(errno
));
2373 * Read all the c##### files...
2376 while ((dent
= cupsDirRead(dir
)) != NULL
)
2377 if (strlen(dent
->filename
) >= 6 && dent
->filename
[0] == 'c')
2380 * Allocate memory for the job...
2383 if ((job
= calloc(sizeof(cupsd_job_t
), 1)) == NULL
)
2385 cupsdLogMessage(CUPSD_LOG_ERROR
, "Ran out of memory for jobs!");
2391 * Assign the job ID...
2394 job
->id
= atoi(dent
->filename
+ 1);
2395 job
->back_pipes
[0] = -1;
2396 job
->back_pipes
[1] = -1;
2397 job
->print_pipes
[0] = -1;
2398 job
->print_pipes
[1] = -1;
2399 job
->side_pipes
[0] = -1;
2400 job
->side_pipes
[1] = -1;
2401 job
->status_pipes
[0] = -1;
2402 job
->status_pipes
[1] = -1;
2404 if (job
->id
>= NextJobId
)
2405 NextJobId
= job
->id
+ 1;
2414 * Insert the job into the array, sorting by job priority and ID...
2417 cupsArrayAdd(Jobs
, job
);
2419 if (job
->state_value
<= IPP_JOB_STOPPED
)
2420 cupsArrayAdd(ActiveJobs
, job
);
2430 * 'set_time()' - Set one of the "time-at-xyz" attributes...
2434 set_time(cupsd_job_t
*job
, /* I - Job to update */
2435 const char *name
) /* I - Name of attribute */
2437 ipp_attribute_t
*attr
; /* Time attribute */
2440 if ((attr
= ippFindAttribute(job
->attrs
, name
, IPP_TAG_ZERO
)) != NULL
)
2442 attr
->value_tag
= IPP_TAG_INTEGER
;
2443 attr
->values
[0].integer
= time(NULL
);
2449 * 'set_hold_until()' - Set the hold time and update job-hold-until attribute...
2453 set_hold_until(cupsd_job_t
*job
, /* I - Job to update */
2454 time_t holdtime
) /* I - Hold until time */
2456 ipp_attribute_t
*attr
; /* job-hold-until attribute */
2457 struct tm
*holddate
; /* Hold date */
2458 char holdstr
[64]; /* Hold time */
2462 * Set the hold_until value and hold the job...
2465 cupsdLogMessage(CUPSD_LOG_DEBUG
, "set_hold_until: hold_until = %d",
2468 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
2469 job
->state_value
= IPP_JOB_HELD
;
2470 job
->hold_until
= holdtime
;
2473 * Update the job-hold-until attribute with a string representing GMT
2474 * time (HH:MM:SS)...
2477 holddate
= gmtime(&holdtime
);
2478 snprintf(holdstr
, sizeof(holdstr
), "%d:%d:%d", holddate
->tm_hour
,
2479 holddate
->tm_min
, holddate
->tm_sec
);
2481 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2482 IPP_TAG_KEYWORD
)) == NULL
)
2483 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2486 * Either add the attribute or update the value of the existing one
2490 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "job-hold-until",
2493 cupsdSetString(&attr
->values
[0].string
.text
, holdstr
);
2496 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2501 * 'start_job()' - Start a print job.
2505 start_job(cupsd_job_t
*job
, /* I - Job ID */
2506 cupsd_printer_t
*printer
) /* I - Printer to print job */
2508 int i
; /* Looping var */
2509 int slot
; /* Pipe slot */
2510 cups_array_t
*filters
, /* Filters for job */
2511 *prefilters
; /* Filters with prefilters */
2512 mime_filter_t
*filter
, /* Current filter */
2513 *prefilter
, /* Prefilter */
2514 port_monitor
; /* Port monitor filter */
2515 char method
[255], /* Method for output */
2516 *optptr
, /* Pointer to options */
2517 *valptr
; /* Pointer in value string */
2518 ipp_attribute_t
*attr
; /* Current attribute */
2519 struct stat backinfo
; /* Backend file information */
2520 int backroot
; /* Run backend as root? */
2521 int pid
; /* Process ID of new filter process */
2522 int banner_page
; /* 1 if banner page, 0 otherwise */
2523 int filterfds
[2][2];/* Pipes used between filters */
2524 int envc
; /* Number of environment variables */
2525 char **argv
, /* Filter command-line arguments */
2526 filename
[1024], /* Job filename */
2527 command
[1024], /* Full path to command */
2528 jobid
[255], /* Job ID string */
2529 title
[IPP_MAX_NAME
],
2530 /* Job title string */
2531 copies
[255], /* # copies string */
2532 *envp
[MAX_ENV
+ 19],
2533 /* Environment variables */
2534 charset
[255], /* CHARSET env variable */
2535 class_name
[255],/* CLASS env variable */
2536 classification
[1024],
2537 /* CLASSIFICATION env variable */
2539 /* CONTENT_TYPE env variable */
2541 /* DEVICE_URI env variable */
2542 final_content_type
[1024],
2543 /* FINAL_CONTENT_TYPE env variable */
2544 lang
[255], /* LANG env variable */
2546 apple_language
[255],
2547 /* APPLE_LANGUAGE env variable */
2548 #endif /* __APPLE__ */
2549 ppd
[1024], /* PPD env variable */
2551 /* PRINTER_INFO env variable */
2552 printer_location
[255],
2553 /* PRINTER_LOCATION env variable */
2555 /* PRINTER env variable */
2557 /* RIP_MAX_CACHE env variable */
2558 static char *options
= NULL
;/* Full list of options */
2559 static int optlength
= 0; /* Length of option buffer */
2562 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "start_job: file = %d/%d",
2563 job
->current_file
, job
->num_files
);
2565 if (job
->num_files
== 0)
2567 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "No files, canceling job!");
2568 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2573 * Figure out what filters are required to convert from
2574 * the source to the destination type...
2583 * Remote jobs and raw queues go directly to the printer without
2587 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Sending job to queue tagged as raw...");
2594 * Local jobs get filtered...
2597 filters
= mimeFilter(MimeDatabase
, job
->filetypes
[job
->current_file
],
2598 printer
->filetype
, &(job
->cost
));
2602 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
2603 "Unable to convert file %d to printable format!",
2605 cupsdLogMessage(CUPSD_LOG_INFO
,
2606 "Hint: Do you have Ghostscript installed?");
2608 if (LogLevel
< CUPSD_LOG_DEBUG
)
2609 cupsdLogMessage(CUPSD_LOG_INFO
,
2610 "Hint: Try setting the LogLevel to \"debug\".");
2612 job
->current_file
++;
2614 if (job
->current_file
== job
->num_files
)
2615 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2621 * Remove NULL ("-") filters...
2624 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
2626 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
2627 if (!strcmp(filter
->filter
, "-"))
2628 cupsArrayRemove(filters
, filter
);
2630 if (cupsArrayCount(filters
) == 0)
2632 cupsArrayDelete(filters
);
2637 * If this printer has any pre-filters, insert the required pre-filter
2638 * in the filters array...
2641 if (printer
->prefiltertype
&& filters
)
2643 prefilters
= cupsArrayNew(NULL
, NULL
);
2645 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
2647 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
2649 if ((prefilter
= mimeFilterLookup(MimeDatabase
, filter
->src
,
2650 printer
->prefiltertype
)))
2652 cupsArrayAdd(prefilters
, prefilter
);
2653 job
->cost
+= prefilter
->cost
;
2656 cupsArrayAdd(prefilters
, filter
);
2659 cupsArrayDelete(filters
);
2660 filters
= prefilters
;
2665 * Set a minimum cost of 100 for all jobs so that FilterLimit
2666 * works with raw queues and other low-cost paths.
2669 if (job
->cost
< 100)
2673 * See if the filter cost is too high...
2676 if ((FilterLevel
+ job
->cost
) > FilterLimit
&& FilterLevel
> 0 &&
2680 * Don't print this job quite yet...
2683 cupsArrayDelete(filters
);
2685 cupsdLogJob(job
, CUPSD_LOG_INFO
,
2686 "Holding because filter limit has been reached.");
2687 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
2688 "start_job: file=%d, cost=%d, level=%d, limit=%d",
2689 job
->current_file
, job
->cost
, FilterLevel
,
2694 FilterLevel
+= job
->cost
;
2697 * Add decompression/raw filter as needed...
2700 if ((!printer
->raw
&& job
->compressions
[job
->current_file
]) ||
2701 (!filters
&& !printer
->remote
&&
2702 (job
->num_files
> 1 || !strncmp(printer
->device_uri
, "file:", 5))))
2705 * Add gziptoany filter to the front of the list...
2709 filters
= cupsArrayNew(NULL
, NULL
);
2711 if (!cupsArrayInsert(filters
, &gziptoany_filter
))
2713 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
2714 "Unable to add decompression filter - %s", strerror(errno
));
2716 cupsArrayDelete(filters
);
2718 job
->current_file
++;
2720 if (job
->current_file
== job
->num_files
)
2721 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2728 * Add port monitor, if any...
2731 if (printer
->port_monitor
)
2734 * Add port monitor to the end of the list...
2738 filters
= cupsArrayNew(NULL
, NULL
);
2740 port_monitor
.src
= NULL
;
2741 port_monitor
.dst
= NULL
;
2742 port_monitor
.cost
= 0;
2744 snprintf(port_monitor
.filter
, sizeof(port_monitor
.filter
),
2745 "%s/monitor/%s", ServerBin
, printer
->port_monitor
);
2747 if (!cupsArrayAdd(filters
, &port_monitor
))
2749 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
2750 "Unable to add port monitor - %s", strerror(errno
));
2752 cupsArrayDelete(filters
);
2754 job
->current_file
++;
2756 if (job
->current_file
== job
->num_files
)
2757 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2764 * Make sure we don't go over the "MAX_FILTERS" limit...
2767 if (cupsArrayCount(filters
) > MAX_FILTERS
)
2769 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
2770 "Too many filters (%d > %d), unable to print!",
2771 cupsArrayCount(filters
), MAX_FILTERS
);
2773 cupsArrayDelete(filters
);
2774 cupsdCancelJob(job
, 0, IPP_JOB_STOPPED
);
2780 * Update the printer and job state to "processing"...
2783 job
->state
->values
[0].integer
= IPP_JOB_PROCESSING
;
2784 job
->state_value
= IPP_JOB_PROCESSING
;
2787 job
->printer
= printer
;
2790 cupsdSetPrinterState(printer
, IPP_PRINTER_PROCESSING
, 0);
2792 if (job
->current_file
== 0)
2795 * Add to the printing list...
2798 cupsArrayAdd(PrintingJobs
, job
);
2799 cupsdSetBusyState();
2802 * Set the processing time...
2805 set_time(job
, "time-at-processing");
2808 * Create the backchannel pipes and make them non-blocking...
2811 cupsdOpenPipe(job
->back_pipes
);
2813 fcntl(job
->back_pipes
[0], F_SETFL
,
2814 fcntl(job
->back_pipes
[0], F_GETFL
) | O_NONBLOCK
);
2816 fcntl(job
->back_pipes
[1], F_SETFL
,
2817 fcntl(job
->back_pipes
[1], F_GETFL
) | O_NONBLOCK
);
2820 * Create the side-channel pipes and make them non-blocking...
2823 socketpair(AF_LOCAL
, SOCK_STREAM
, 0, job
->side_pipes
);
2825 fcntl(job
->side_pipes
[0], F_SETFL
,
2826 fcntl(job
->side_pipes
[0], F_GETFL
) | O_NONBLOCK
);
2828 fcntl(job
->side_pipes
[1], F_SETFL
,
2829 fcntl(job
->side_pipes
[1], F_GETFL
) | O_NONBLOCK
);
2833 * Determine if we are printing a banner page or not...
2836 if (job
->job_sheets
== NULL
)
2838 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "No job-sheets attribute.");
2839 if ((job
->job_sheets
=
2840 ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_ZERO
)) != NULL
)
2841 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
2842 "... but someone added one without setting job_sheets!");
2844 else if (job
->job_sheets
->num_values
== 1)
2845 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "job-sheets=%s",
2846 job
->job_sheets
->values
[0].string
.text
);
2848 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "job-sheets=%s,%s",
2849 job
->job_sheets
->values
[0].string
.text
,
2850 job
->job_sheets
->values
[1].string
.text
);
2852 if (printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
))
2854 else if (job
->job_sheets
== NULL
)
2856 else if (strcasecmp(job
->job_sheets
->values
[0].string
.text
, "none") != 0 &&
2857 job
->current_file
== 0)
2859 else if (job
->job_sheets
->num_values
> 1 &&
2860 strcasecmp(job
->job_sheets
->values
[1].string
.text
, "none") != 0 &&
2861 job
->current_file
== (job
->num_files
- 1))
2866 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "banner_page = %d", banner_page
);
2869 * Building the options string is harder than it needs to be, but
2870 * for the moment we need to pass strings for command-line args and
2871 * not IPP attribute pointers... :)
2873 * First allocate/reallocate the option buffer as needed...
2876 i
= ipp_length(job
->attrs
);
2878 if (i
> optlength
|| !options
)
2883 optptr
= realloc(options
, i
);
2887 cupsdLogJob(job
, CUPSD_LOG_CRIT
,
2888 "Unable to allocate %d bytes for option buffer!", i
);
2890 cupsArrayDelete(filters
);
2892 FilterLevel
-= job
->cost
;
2894 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2903 * Now loop through the attributes and convert them to the textual
2904 * representation used by the filters...
2910 snprintf(title
, sizeof(title
), "%s-%d", printer
->name
, job
->id
);
2911 strcpy(copies
, "1");
2913 for (attr
= job
->attrs
->attrs
; attr
!= NULL
; attr
= attr
->next
)
2915 if (!strcmp(attr
->name
, "copies") &&
2916 attr
->value_tag
== IPP_TAG_INTEGER
)
2919 * Don't use the # copies attribute if we are printing the job sheets...
2923 sprintf(copies
, "%d", attr
->values
[0].integer
);
2925 else if (!strcmp(attr
->name
, "job-name") &&
2926 (attr
->value_tag
== IPP_TAG_NAME
||
2927 attr
->value_tag
== IPP_TAG_NAMELANG
))
2928 strlcpy(title
, attr
->values
[0].string
.text
, sizeof(title
));
2929 else if (attr
->group_tag
== IPP_TAG_JOB
)
2932 * Filter out other unwanted attributes...
2935 if (attr
->value_tag
== IPP_TAG_MIMETYPE
||
2936 attr
->value_tag
== IPP_TAG_NAMELANG
||
2937 attr
->value_tag
== IPP_TAG_TEXTLANG
||
2938 (attr
->value_tag
== IPP_TAG_URI
&& strcmp(attr
->name
, "job-uuid")) ||
2939 attr
->value_tag
== IPP_TAG_URISCHEME
||
2940 attr
->value_tag
== IPP_TAG_BEGIN_COLLECTION
) /* Not yet supported */
2943 if (!strncmp(attr
->name
, "time-", 5))
2946 if (!strncmp(attr
->name
, "job-", 4) &&
2947 strcmp(attr
->name
, "job-billing") &&
2948 strcmp(attr
->name
, "job-impressions") &&
2949 strcmp(attr
->name
, "job-originating-host-name") &&
2950 strcmp(attr
->name
, "job-uuid") &&
2951 !(printer
->type
& CUPS_PRINTER_REMOTE
))
2954 if ((!strcmp(attr
->name
, "job-impressions") ||
2955 !strcmp(attr
->name
, "page-label") ||
2956 !strcmp(attr
->name
, "page-border") ||
2957 !strncmp(attr
->name
, "number-up", 9) ||
2958 !strcmp(attr
->name
, "page-ranges") ||
2959 !strcmp(attr
->name
, "page-set") ||
2960 !strcasecmp(attr
->name
, "AP_FIRSTPAGE_InputSlot") ||
2961 !strcasecmp(attr
->name
, "AP_FIRSTPAGE_ManualFeed") ||
2962 !strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
2963 "PMTotalSidesImaged..n.") ||
2964 !strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
2965 "PMTotalBeginPages..n.")) &&
2970 * Otherwise add them to the list...
2973 if (optptr
> options
)
2974 strlcat(optptr
, " ", optlength
- (optptr
- options
));
2976 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
2978 strlcat(optptr
, attr
->name
, optlength
- (optptr
- options
));
2979 strlcat(optptr
, "=", optlength
- (optptr
- options
));
2982 for (i
= 0; i
< attr
->num_values
; i
++)
2985 strlcat(optptr
, ",", optlength
- (optptr
- options
));
2987 optptr
+= strlen(optptr
);
2989 switch (attr
->value_tag
)
2991 case IPP_TAG_INTEGER
:
2993 snprintf(optptr
, optlength
- (optptr
- options
),
2994 "%d", attr
->values
[i
].integer
);
2997 case IPP_TAG_BOOLEAN
:
2998 if (!attr
->values
[i
].boolean
)
2999 strlcat(optptr
, "no", optlength
- (optptr
- options
));
3001 case IPP_TAG_NOVALUE
:
3002 strlcat(optptr
, attr
->name
,
3003 optlength
- (optptr
- options
));
3006 case IPP_TAG_RANGE
:
3007 if (attr
->values
[i
].range
.lower
== attr
->values
[i
].range
.upper
)
3008 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
3009 "%d", attr
->values
[i
].range
.lower
);
3011 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
3012 "%d-%d", attr
->values
[i
].range
.lower
,
3013 attr
->values
[i
].range
.upper
);
3016 case IPP_TAG_RESOLUTION
:
3017 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
3018 "%dx%d%s", attr
->values
[i
].resolution
.xres
,
3019 attr
->values
[i
].resolution
.yres
,
3020 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
3024 case IPP_TAG_STRING
:
3027 case IPP_TAG_KEYWORD
:
3028 case IPP_TAG_CHARSET
:
3029 case IPP_TAG_LANGUAGE
:
3031 for (valptr
= attr
->values
[i
].string
.text
; *valptr
;)
3033 if (strchr(" \t\n\\\'\"", *valptr
))
3035 *optptr
++ = *valptr
++;
3042 break; /* anti-compiler-warning-code */
3046 optptr
+= strlen(optptr
);
3051 * Build the command-line arguments for the filters. Each filter
3052 * has 6 or 7 arguments:
3056 * argv[2] = username
3058 * argv[4] = # copies
3060 * argv[6] = filename (optional; normally stdin)
3062 * This allows legacy printer drivers that use the old System V
3063 * printing interface to be used by CUPS.
3065 * For remote jobs, we send all of the files in the argument list.
3068 if (printer
->remote
&& job
->num_files
> 1)
3069 argv
= calloc(7 + job
->num_files
, sizeof(char *));
3071 argv
= calloc(8, sizeof(char *));
3075 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to allocate argument array!");
3076 cupsArrayDelete(filters
);
3078 FilterLevel
-= job
->cost
;
3080 cupsdStopPrinter(printer
, 0);
3084 sprintf(jobid
, "%d", job
->id
);
3086 argv
[0] = printer
->name
;
3088 argv
[2] = job
->username
;
3093 if (printer
->remote
&& job
->num_files
> 1)
3095 for (i
= 0; i
< job
->num_files
; i
++)
3097 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
3099 argv
[6 + i
] = strdup(filename
);
3104 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
3105 job
->id
, job
->current_file
+ 1);
3109 for (i
= 0; argv
[i
]; i
++)
3110 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
3111 "argv[%d]=\"%s\"", i
, argv
[i
]);
3114 * Create environment variable strings for the filters...
3117 attr
= ippFindAttribute(job
->attrs
, "attributes-natural-language",
3121 strcpy(apple_language
, "APPLE_LANGUAGE=");
3122 _cupsAppleLanguage(attr
->values
[0].string
.text
,
3123 apple_language
+ 15, sizeof(apple_language
) - 15);
3124 #endif /* __APPLE__ */
3126 switch (strlen(attr
->values
[0].string
.text
))
3130 * This is an unknown or badly formatted language code; use
3131 * the POSIX locale...
3134 strcpy(lang
, "LANG=C");
3139 * Just the language code (ll)...
3142 snprintf(lang
, sizeof(lang
), "LANG=%s.UTF8",
3143 attr
->values
[0].string
.text
);
3148 * Language and country code (ll-cc)...
3151 snprintf(lang
, sizeof(lang
), "LANG=%c%c_%c%c.UTF8",
3152 attr
->values
[0].string
.text
[0],
3153 attr
->values
[0].string
.text
[1],
3154 toupper(attr
->values
[0].string
.text
[3] & 255),
3155 toupper(attr
->values
[0].string
.text
[4] & 255));
3159 attr
= ippFindAttribute(job
->attrs
, "document-format",
3162 (optptr
= strstr(attr
->values
[0].string
.text
, "charset=")) != NULL
)
3163 snprintf(charset
, sizeof(charset
), "CHARSET=%s", optptr
+ 8);
3166 attr
= ippFindAttribute(job
->attrs
, "attributes-charset",
3168 snprintf(charset
, sizeof(charset
), "CHARSET=%s",
3169 attr
->values
[0].string
.text
);
3172 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s/%s",
3173 job
->filetypes
[job
->current_file
]->super
,
3174 job
->filetypes
[job
->current_file
]->type
);
3175 snprintf(device_uri
, sizeof(device_uri
), "DEVICE_URI=%s",
3176 printer
->device_uri
);
3177 snprintf(ppd
, sizeof(ppd
), "PPD=%s/ppd/%s.ppd", ServerRoot
, printer
->name
);
3178 snprintf(printer_info
, sizeof(printer_name
), "PRINTER_INFO=%s",
3179 printer
->info
? printer
->info
: "");
3180 snprintf(printer_location
, sizeof(printer_name
), "PRINTER_LOCATION=%s",
3181 printer
->location
? printer
->location
: "");
3182 snprintf(printer_name
, sizeof(printer_name
), "PRINTER=%s", printer
->name
);
3183 snprintf(rip_max_cache
, sizeof(rip_max_cache
), "RIP_MAX_CACHE=%s", RIPCache
);
3185 envc
= cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
3187 envp
[envc
++] = charset
;
3188 envp
[envc
++] = lang
;
3190 envp
[envc
++] = apple_language
;
3191 #endif /* __APPLE__ */
3192 envp
[envc
++] = ppd
;
3193 envp
[envc
++] = rip_max_cache
;
3194 envp
[envc
++] = content_type
;
3195 envp
[envc
++] = device_uri
;
3196 envp
[envc
++] = printer_info
;
3197 envp
[envc
++] = printer_location
;
3198 envp
[envc
++] = printer_name
;
3199 envp
[envc
++] = banner_page
? "CUPS_FILETYPE=job-sheet" :
3200 "CUPS_FILETYPE=document";
3202 if (!printer
->remote
&& !printer
->raw
)
3204 filter
= (mime_filter_t
*)cupsArrayLast(filters
);
3206 if (printer
->port_monitor
)
3207 filter
= (mime_filter_t
*)cupsArrayPrev(filters
);
3209 if (filter
&& filter
->dst
)
3211 snprintf(final_content_type
, sizeof(final_content_type
),
3212 "FINAL_CONTENT_TYPE=%s/%s",
3213 filter
->dst
->super
, filter
->dst
->type
);
3214 envp
[envc
++] = final_content_type
;
3218 if (Classification
&& !banner_page
)
3220 if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
3221 IPP_TAG_NAME
)) == NULL
)
3222 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3224 else if (attr
->num_values
> 1 &&
3225 strcmp(attr
->values
[1].string
.text
, "none") != 0)
3226 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3227 attr
->values
[1].string
.text
);
3229 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3230 attr
->values
[0].string
.text
);
3232 envp
[envc
++] = classification
;
3235 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
3237 snprintf(class_name
, sizeof(class_name
), "CLASS=%s", job
->dest
);
3238 envp
[envc
++] = class_name
;
3241 if (job
->auth_username
)
3242 envp
[envc
++] = job
->auth_username
;
3243 if (job
->auth_domain
)
3244 envp
[envc
++] = job
->auth_domain
;
3245 if (job
->auth_password
)
3246 envp
[envc
++] = job
->auth_password
;
3250 envp
[envc
++] = job
->ccname
;
3251 #endif /* HAVE_GSSAPI */
3255 for (i
= 0; i
< envc
; i
++)
3256 if (!strncmp(envp
[i
], "AUTH_", 5))
3257 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"AUTH_%c****\"", i
,
3259 else if (strncmp(envp
[i
], "DEVICE_URI=", 11))
3260 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"%s\"", i
, envp
[i
]);
3262 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"DEVICE_URI=%s\"", i
,
3263 printer
->sanitized_device_uri
);
3265 if (printer
->remote
)
3266 job
->current_file
= job
->num_files
;
3268 job
->current_file
++;
3271 * Now create processes for all of the filters...
3274 filterfds
[0][0] = -1;
3275 filterfds
[0][1] = -1;
3276 filterfds
[1][0] = -1;
3277 filterfds
[1][1] = -1;
3279 if (!job
->status_buffer
)
3281 if (cupsdOpenPipe(job
->status_pipes
))
3283 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
3284 "Unable to create job status pipes - %s.", strerror(errno
));
3285 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3286 "Unable to create status pipes - %s.", strerror(errno
));
3288 cupsdAddPrinterHistory(printer
);
3290 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3291 "Job canceled because the server could not create the job "
3297 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3298 "start_job: status_pipes = [ %d %d ]",
3299 job
->status_pipes
[0], job
->status_pipes
[1]);
3301 job
->status_buffer
= cupsdStatBufNew(job
->status_pipes
[0], NULL
);
3302 job
->status_level
= CUPSD_LOG_INFO
;
3306 memset(job
->filters
, 0, sizeof(job
->filters
));
3309 job
->profile
= cupsdCreateProfile(job
->id
);
3311 for (i
= 0, slot
= 0, filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
3313 i
++, filter
= (mime_filter_t
*)cupsArrayNext(filters
))
3315 if (filter
->filter
[0] != '/')
3316 snprintf(command
, sizeof(command
), "%s/filter/%s", ServerBin
,
3319 strlcpy(command
, filter
->filter
, sizeof(command
));
3321 if (i
< (cupsArrayCount(filters
) - 1))
3323 if (cupsdOpenPipe(filterfds
[slot
]))
3325 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
3326 "Unable to create job filter pipes - %s.", strerror(errno
));
3327 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3328 "Unable to create filter pipes - %s.", strerror(errno
));
3329 cupsdAddPrinterHistory(printer
);
3331 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3332 "Job canceled because the server could not create the "
3340 if (job
->current_file
== 1)
3342 if (strncmp(printer
->device_uri
, "file:", 5) != 0)
3344 if (cupsdOpenPipe(job
->print_pipes
))
3346 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
3347 "Unable to create job backend pipes - %s.",
3349 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3350 "Unable to create backend pipes - %s.", strerror(errno
));
3351 cupsdAddPrinterHistory(printer
);
3353 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3354 "Job canceled because the server could not create "
3355 "the backend pipes.");
3362 job
->print_pipes
[0] = -1;
3363 if (!strcmp(printer
->device_uri
, "file:/dev/null") ||
3364 !strcmp(printer
->device_uri
, "file:///dev/null"))
3365 job
->print_pipes
[1] = -1;
3368 if (!strncmp(printer
->device_uri
, "file:/dev/", 10))
3369 job
->print_pipes
[1] = open(printer
->device_uri
+ 5,
3371 else if (!strncmp(printer
->device_uri
, "file:///dev/", 12))
3372 job
->print_pipes
[1] = open(printer
->device_uri
+ 7,
3374 else if (!strncmp(printer
->device_uri
, "file:///", 8))
3375 job
->print_pipes
[1] = open(printer
->device_uri
+ 7,
3376 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
3378 job
->print_pipes
[1] = open(printer
->device_uri
+ 5,
3379 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
3381 if (job
->print_pipes
[1] < 0)
3383 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
3384 "Unable to open output file \"%s\" - %s.",
3385 printer
->device_uri
, strerror(errno
));
3386 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3387 "Unable to open output file \"%s\" - %s.",
3388 printer
->device_uri
, strerror(errno
));
3390 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3391 "Job canceled because the server could not open the "
3397 fcntl(job
->print_pipes
[1], F_SETFD
,
3398 fcntl(job
->print_pipes
[1], F_GETFD
) | FD_CLOEXEC
);
3402 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3403 "start_job: print_pipes = [ %d %d ]",
3404 job
->print_pipes
[0], job
->print_pipes
[1]);
3407 filterfds
[slot
][0] = job
->print_pipes
[0];
3408 filterfds
[slot
][1] = job
->print_pipes
[1];
3411 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "start_job: filter=\"%s\"", command
);
3412 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "start_job: filterfds[%d]=[ %d %d ]",
3413 slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3415 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
3416 filterfds
[slot
][1], job
->status_pipes
[1],
3417 job
->back_pipes
[0], job
->side_pipes
[0], 0,
3418 job
->profile
, job
->filters
+ i
);
3420 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3421 "start_job: Closing filter pipes for slot %d [ %d %d ]...",
3422 !slot
, filterfds
[!slot
][0], filterfds
[!slot
][1]);
3424 cupsdClosePipe(filterfds
[!slot
]);
3428 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Unable to start filter \"%s\" - %s.",
3429 filter
->filter
, strerror(errno
));
3430 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3431 "Unable to start filter \"%s\" - %s.",
3432 filter
->filter
, strerror(errno
));
3434 cupsdAddPrinterHistory(printer
);
3436 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3437 "Job canceled because the server could not execute a "
3443 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Started filter %s (PID %d)", command
,
3450 cupsArrayDelete(filters
);
3454 * Finally, pipe the final output into a backend process if needed...
3457 if (strncmp(printer
->device_uri
, "file:", 5) != 0)
3459 if (job
->current_file
== 1 || printer
->remote
)
3461 sscanf(printer
->device_uri
, "%254[^:]", method
);
3462 snprintf(command
, sizeof(command
), "%s/backend/%s", ServerBin
, method
);
3465 * See if the backend needs to run as root...
3470 else if (stat(command
, &backinfo
))
3473 backroot
= !(backinfo
.st_mode
& (S_IRWXG
| S_IRWXO
));
3475 argv
[0] = printer
->sanitized_device_uri
;
3477 filterfds
[slot
][0] = -1;
3478 filterfds
[slot
][1] = -1;
3480 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "start_job: backend=\"%s\"", command
);
3481 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "start_job: filterfds[%d] = [ %d %d ]",
3482 slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3484 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
3485 filterfds
[slot
][1], job
->status_pipes
[1],
3486 job
->back_pipes
[1], job
->side_pipes
[1],
3487 backroot
, job
->profile
, &(job
->backend
));
3491 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Unable to start backend \"%s\" - %s.",
3492 method
, strerror(errno
));
3493 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3494 "Unable to start backend \"%s\" - %s.", method
,
3497 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3498 "Job canceled because the server could not execute "
3505 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Started backend %s (PID %d)",
3510 if (job
->current_file
== job
->num_files
)
3512 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3513 "start_job: Closing print pipes [ %d %d ]...",
3514 job
->print_pipes
[0], job
->print_pipes
[1]);
3516 cupsdClosePipe(job
->print_pipes
);
3518 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3519 "start_job: Closing back pipes [ %d %d ]...",
3520 job
->back_pipes
[0], job
->back_pipes
[1]);
3522 cupsdClosePipe(job
->back_pipes
);
3524 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3525 "start_job: Closing side pipes [ %d %d ]...",
3526 job
->side_pipes
[0], job
->side_pipes
[1]);
3528 cupsdClosePipe(job
->side_pipes
);
3530 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3531 "start_job: Closing status output pipe %d...",
3532 job
->status_pipes
[1]);
3534 close(job
->status_pipes
[1]);
3535 job
->status_pipes
[1] = -1;
3540 filterfds
[slot
][0] = -1;
3541 filterfds
[slot
][1] = -1;
3543 if (job
->current_file
== job
->num_files
)
3545 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3546 "start_job: Closing print pipes [ %d %d ]...",
3547 job
->print_pipes
[0], job
->print_pipes
[1]);
3549 cupsdClosePipe(job
->print_pipes
);
3551 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3552 "start_job: Closing status output pipe %d...",
3553 job
->status_pipes
[1]);
3555 close(job
->status_pipes
[1]);
3556 job
->status_pipes
[1] = -1;
3560 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3561 "start_job: Closing filter pipes for slot %d [ %d %d ]...",
3562 slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3563 cupsdClosePipe(filterfds
[slot
]);
3565 if (printer
->remote
&& job
->num_files
> 1)
3567 for (i
= 0; i
< job
->num_files
; i
++)
3573 cupsdAddSelect(job
->status_buffer
->fd
, (cupsd_selfunc_t
)update_job
, NULL
,
3576 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
, "Job #%d started.",
3583 * If we get here, we need to abort the current job and close out all
3584 * files and pipes...
3589 for (slot
= 0; slot
< 2; slot
++)
3591 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3592 "start_job: Closing filter pipes for slot %d [ %d %d ]...",
3593 slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3594 cupsdClosePipe(filterfds
[slot
]);
3597 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3598 "start_job: Closing status pipes [ %d %d ]...",
3599 job
->status_pipes
[0], job
->status_pipes
[1]);
3600 cupsdClosePipe(job
->status_pipes
);
3601 cupsdStatBufDelete(job
->status_buffer
);
3603 job
->status_buffer
= NULL
;
3605 cupsArrayDelete(filters
);
3607 if (printer
->remote
&& job
->num_files
> 1)
3609 for (i
= 0; i
< job
->num_files
; i
++)
3615 cupsdStopJob(job
, 0);
3620 * 'unload_job()' - Unload a job from memory.
3624 unload_job(cupsd_job_t
*job
) /* I - Job */
3629 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Unloading...", job
->id
);
3631 ippDelete(job
->attrs
);
3636 job
->job_sheets
= NULL
;
3637 job
->printer_message
= NULL
;
3638 job
->printer_reasons
= NULL
;
3643 * 'update_job()' - Read a status update from a job's filters.
3647 update_job(cupsd_job_t
*job
) /* I - Job to check */
3649 int i
; /* Looping var */
3650 int copies
; /* Number of copies printed */
3651 char message
[1024], /* Message text */
3652 *ptr
; /* Pointer update... */
3653 int loglevel
, /* Log level for message */
3654 event
= 0; /* Events? */
3657 while ((ptr
= cupsdStatBufUpdate(job
->status_buffer
, &loglevel
,
3658 message
, sizeof(message
))) != NULL
)
3661 * Process page and printer state messages as needed...
3664 if (loglevel
== CUPSD_LOG_PAGE
)
3667 * Page message; send the message to the page_log file and update the
3668 * job sheet count...
3671 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "PAGE: %s", message
);
3675 if (!strncasecmp(message
, "total ", 6))
3678 * Got a total count of pages from a backend or filter...
3681 copies
= atoi(message
+ 6);
3682 copies
-= job
->sheets
->values
[0].integer
; /* Just track the delta */
3684 else if (!sscanf(message
, "%*d%d", &copies
))
3687 job
->sheets
->values
[0].integer
+= copies
;
3689 if (job
->printer
->page_limit
)
3691 cupsd_quota_t
*q
= cupsdUpdateQuota(job
->printer
, job
->username
,
3695 if (AppleQuotas
&& q
->page_count
== -3)
3698 * Quota limit exceeded, cancel job in progress immediately...
3701 cupsdLogJob(job
, CUPSD_LOG_INFO
,
3702 "Canceled because pages exceed user %s "
3703 "quota limit on printer %s (%s).",
3704 job
->username
, job
->printer
->name
,
3705 job
->printer
->info
);
3707 cupsdCancelJob(job
, 1, IPP_JOB_CANCELED
);
3712 #endif /* __APPLE__ */
3716 cupsdLogPage(job
, message
);
3719 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
3720 "Printed %d page(s).", job
->sheets
->values
[0].integer
);
3722 else if (loglevel
== CUPSD_LOG_STATE
)
3724 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "STATE: %s", message
);
3726 if (!strcmp(message
, "paused"))
3728 cupsdStopPrinter(job
->printer
, 1);
3733 cupsdSetPrinterReasons(job
->printer
, message
);
3734 cupsdAddPrinterHistory(job
->printer
);
3735 event
|= CUPSD_EVENT_PRINTER_STATE
;
3738 update_job_attrs(job
, 0);
3740 else if (loglevel
== CUPSD_LOG_ATTR
)
3743 * Set attribute(s)...
3746 int num_attrs
; /* Number of attributes */
3747 cups_option_t
*attrs
; /* Attributes */
3748 const char *attr
; /* Attribute */
3751 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "ATTR: %s", message
);
3753 num_attrs
= cupsParseOptions(message
, 0, &attrs
);
3755 if ((attr
= cupsGetOption("auth-info-required", num_attrs
,
3758 cupsdSetAuthInfoRequired(job
->printer
, attr
, NULL
);
3759 cupsdSetPrinterAttrs(job
->printer
);
3761 if (job
->printer
->type
& CUPS_PRINTER_DISCOVERED
)
3762 cupsdMarkDirty(CUPSD_DIRTY_REMOTE
);
3764 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3767 if ((attr
= cupsGetOption("job-media-progress", num_attrs
,
3770 int progress
= atoi(attr
);
3773 if (progress
>= 0 && progress
<= 100)
3775 job
->progress
= progress
;
3778 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
3779 "Printing page %d, %d%%",
3780 job
->sheets
->values
[0].integer
, job
->progress
);
3784 if ((attr
= cupsGetOption("printer-alert", num_attrs
, attrs
)) != NULL
)
3786 cupsdSetString(&job
->printer
->alert
, attr
);
3787 event
|= CUPSD_EVENT_PRINTER_STATE
;
3790 if ((attr
= cupsGetOption("printer-alert-description", num_attrs
,
3793 cupsdSetString(&job
->printer
->alert_description
, attr
);
3794 event
|= CUPSD_EVENT_PRINTER_STATE
;
3797 if ((attr
= cupsGetOption("marker-colors", num_attrs
, attrs
)) != NULL
)
3799 cupsdSetPrinterAttr(job
->printer
, "marker-colors", (char *)attr
);
3800 job
->printer
->marker_time
= time(NULL
);
3801 event
|= CUPSD_EVENT_PRINTER_STATE
;
3802 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3805 if ((attr
= cupsGetOption("marker-levels", num_attrs
, attrs
)) != NULL
)
3807 cupsdSetPrinterAttr(job
->printer
, "marker-levels", (char *)attr
);
3808 job
->printer
->marker_time
= time(NULL
);
3809 event
|= CUPSD_EVENT_PRINTER_STATE
;
3810 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3813 if ((attr
= cupsGetOption("marker-low-levels", num_attrs
, attrs
)) != NULL
)
3815 cupsdSetPrinterAttr(job
->printer
, "marker-low-levels", (char *)attr
);
3816 job
->printer
->marker_time
= time(NULL
);
3817 event
|= CUPSD_EVENT_PRINTER_STATE
;
3818 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3821 if ((attr
= cupsGetOption("marker-high-levels", num_attrs
, attrs
)) != NULL
)
3823 cupsdSetPrinterAttr(job
->printer
, "marker-high-levels", (char *)attr
);
3824 job
->printer
->marker_time
= time(NULL
);
3825 event
|= CUPSD_EVENT_PRINTER_STATE
;
3826 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3829 if ((attr
= cupsGetOption("marker-message", num_attrs
, attrs
)) != NULL
)
3831 cupsdSetPrinterAttr(job
->printer
, "marker-message", (char *)attr
);
3832 job
->printer
->marker_time
= time(NULL
);
3833 event
|= CUPSD_EVENT_PRINTER_STATE
;
3834 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3837 if ((attr
= cupsGetOption("marker-names", num_attrs
, attrs
)) != NULL
)
3839 cupsdSetPrinterAttr(job
->printer
, "marker-names", (char *)attr
);
3840 job
->printer
->marker_time
= time(NULL
);
3841 event
|= CUPSD_EVENT_PRINTER_STATE
;
3842 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3845 if ((attr
= cupsGetOption("marker-types", num_attrs
, attrs
)) != NULL
)
3847 cupsdSetPrinterAttr(job
->printer
, "marker-types", (char *)attr
);
3848 job
->printer
->marker_time
= time(NULL
);
3849 event
|= CUPSD_EVENT_PRINTER_STATE
;
3850 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3853 cupsFreeOptions(num_attrs
, attrs
);
3855 else if (loglevel
== CUPSD_LOG_PPD
)
3858 * Set attribute(s)...
3861 int num_keywords
; /* Number of keywords */
3862 cups_option_t
*keywords
; /* Keywords */
3865 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "PPD: %s", message
);
3867 num_keywords
= cupsParseOptions(message
, 0, &keywords
);
3869 if (cupsdUpdatePrinterPPD(job
->printer
, num_keywords
, keywords
))
3870 cupsdSetPrinterAttrs(job
->printer
);
3872 cupsFreeOptions(num_keywords
, keywords
);
3875 else if (!strncmp(message
, "recoverable:", 12))
3877 cupsdSetPrinterReasons(job
->printer
,
3878 "+com.apple.print.recoverable-warning");
3881 while (isspace(*ptr
& 255))
3884 cupsdSetString(&job
->printer
->recoverable
, ptr
);
3885 cupsdAddPrinterHistory(job
->printer
);
3886 event
|= CUPSD_EVENT_PRINTER_STATE
;
3888 else if (!strncmp(message
, "recovered:", 10))
3890 cupsdSetPrinterReasons(job
->printer
,
3891 "-com.apple.print.recoverable-warning");
3894 while (isspace(*ptr
& 255))
3897 cupsdSetString(&job
->printer
->recoverable
, ptr
);
3898 cupsdAddPrinterHistory(job
->printer
);
3899 event
|= CUPSD_EVENT_PRINTER_STATE
;
3901 #endif /* __APPLE__ */
3904 cupsdLogJob(job
, loglevel
, "%s", message
);
3906 if (loglevel
< CUPSD_LOG_DEBUG
)
3908 strlcpy(job
->printer
->state_message
, message
,
3909 sizeof(job
->printer
->state_message
));
3910 cupsdAddPrinterHistory(job
->printer
);
3912 event
|= CUPSD_EVENT_PRINTER_STATE
;
3914 if (loglevel
<= job
->status_level
)
3917 * Some messages show in the printer-state-message attribute...
3920 if (loglevel
!= CUPSD_LOG_NOTICE
)
3921 job
->status_level
= loglevel
;
3923 update_job_attrs(job
, 1);
3928 if (!strchr(job
->status_buffer
->buffer
, '\n'))
3932 if (event
& CUPSD_EVENT_PRINTER_STATE
)
3933 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, job
->printer
, NULL
,
3934 (job
->printer
->type
& CUPS_PRINTER_CLASS
) ?
3935 "Class \"%s\" state changed." :
3936 "Printer \"%s\" state changed.",
3937 job
->printer
->name
);
3939 if (ptr
== NULL
&& !job
->status_buffer
->bufused
)
3942 * See if all of the filters and the backend have returned their
3946 for (i
= 0; job
->filters
[i
] < 0; i
++);
3948 if (job
->filters
[i
])
3951 if (job
->current_file
>= job
->num_files
&& job
->backend
> 0)
3955 * Handle the end of job stuff...
3958 cupsdFinishJob(job
);
3964 * 'update_job_attrs()' - Update the job-printer-* attributes.
3968 update_job_attrs(cupsd_job_t
*job
, /* I - Job to update */
3969 int do_message
)/* I - 1 = update job-printer-state message */
3971 int i
; /* Looping var */
3972 int num_reasons
; /* Actual number of reasons */
3973 const char * const *reasons
; /* Reasons */
3974 static const char *none
= "none", /* "none" */
3980 * Get/create the job-printer-state-* attributes...
3983 if (!job
->printer_message
)
3985 if ((job
->printer_message
= ippFindAttribute(job
->attrs
,
3986 "job-printer-state-message",
3987 IPP_TAG_TEXT
)) == NULL
)
3988 job
->printer_message
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_TEXT
,
3989 "job-printer-state-message",
3993 if (!job
->printer_reasons
)
3994 job
->printer_reasons
= ippFindAttribute(job
->attrs
,
3995 "job-printer-state-reasons",
3999 * If the job isn't printing, return now...
4006 * Otherwise copy the printer-state-message value...
4009 if (job
->printer
->state_message
[0] &&
4010 (do_message
|| !job
->printer_message
->values
[0].string
.text
[0]))
4011 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
),
4012 job
->printer
->state_message
);
4015 * ... and the printer-state-reasons value...
4018 if (job
->printer
->num_reasons
== 0)
4021 reasons
= job
->printer
->state
== IPP_PRINTER_STOPPED
? &paused
: &none
;
4025 num_reasons
= job
->printer
->num_reasons
;
4026 reasons
= (const char * const *)job
->printer
->reasons
;
4029 if (!job
->printer_reasons
|| job
->printer_reasons
->num_values
!= num_reasons
)
4031 ippDeleteAttribute(job
->attrs
, job
->printer_reasons
);
4033 job
->printer_reasons
= ippAddStrings(job
->attrs
,
4034 IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
4035 "job-printer-state-reasons",
4036 num_reasons
, NULL
, NULL
);
4039 for (i
= 0; i
< num_reasons
; i
++)
4040 cupsdSetString(&(job
->printer_reasons
->values
[i
].string
.text
), reasons
[i
]);
4045 * End of "$Id: job.c 7902 2008-09-03 14:20:17Z mike $".