2 * "$Id: job.c 7005 2007-10-01 23:45:48Z 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
22 * the destination is available.
23 * cupsdCleanJobs() - Clean out old jobs.
24 * cupsdFinishJob() - Finish a job.
25 * cupsdFreeAllJobs() - Free all jobs from memory.
26 * cupsdFindJob() - Find the specified job.
27 * cupsdGetPrinterJobCount() - Get the number of pending, processing,
28 * cupsdGetUserJobCount() - Get the number of pending, processing,
29 * cupsdHoldJob() - Hold the specified job.
30 * cupsdLoadAllJobs() - Load all jobs from disk.
31 * cupsdLoadJob() - Load a single job...
32 * cupsdMoveJob() - Move the specified job to a different
34 * cupsdReleaseJob() - Release the specified job.
35 * cupsdRestartJob() - Restart the specified job.
36 * cupsdSaveAllJobs() - Save a summary of all jobs to disk.
37 * cupsdSaveJob() - Save a job to disk.
38 * cupsdSetJobHoldUntil() - Set the hold time for a job...
39 * cupsdSetJobPriority() - Set the priority of a job, moving it
40 * up/down in the list as needed.
41 * cupsdStopAllJobs() - Stop all print jobs.
42 * cupsdStopJob() - Stop a print job.
43 * cupsdUnloadCompletedJobs() - Flush completed job history from memory.
44 * compare_active_jobs() - Compare the job IDs and priorities of two
46 * compare_jobs() - Compare the job IDs of two jobs.
47 * free_job() - Free all memory used by a job.
48 * ipp_length() - Compute the size of the buffer needed to
49 * hold the textual IPP attributes.
50 * load_job_cache() - Load jobs from the job.cache file.
51 * load_next_job_id() - Load the NextJobId value from the
53 * load_request_root() - Load jobs from the RequestRoot directory.
54 * set_time() - Set one of the "time-at-xyz" attributes...
55 * set_hold_until() - Set the hold time and update job-hold-until
57 * start_job() - Start a print job.
58 * unload_job() - Unload a job from memory.
59 * update_job() - Read a status update from a jobs filters.
60 * update_job_attrs() - Update the job-printer-* attributes.
64 * Include necessary headers...
69 #include <cups/backend.h>
77 static mime_filter_t gziptoany_filter
=
79 NULL
, /* Source type */
80 NULL
, /* Destination type */
82 "gziptoany" /* Filter program to run */
90 static int compare_active_jobs(void *first
, void *second
, void *data
);
91 static int compare_jobs(void *first
, void *second
, void *data
);
92 static void free_job(cupsd_job_t
*job
);
93 static int ipp_length(ipp_t
*ipp
);
94 static void load_job_cache(const char *filename
);
95 static void load_next_job_id(const char *filename
);
96 static void load_request_root(void);
97 static void set_time(cupsd_job_t
*job
, const char *name
);
98 static void set_hold_until(cupsd_job_t
*job
, time_t holdtime
);
99 static void start_job(cupsd_job_t
*job
, cupsd_printer_t
*printer
);
100 static void unload_job(cupsd_job_t
*job
);
101 static void update_job(cupsd_job_t
*job
);
102 static void update_job_attrs(cupsd_job_t
*job
);
106 * 'cupsdAddJob()' - Add a new job to the job queue...
109 cupsd_job_t
* /* O - New job record */
110 cupsdAddJob(int priority
, /* I - Job priority */
111 const char *dest
) /* I - Job destination */
113 cupsd_job_t
*job
; /* New job record */
116 if ((job
= calloc(sizeof(cupsd_job_t
), 1)) == NULL
)
119 job
->id
= NextJobId
++;
120 job
->priority
= priority
;
121 job
->back_pipes
[0] = -1;
122 job
->back_pipes
[1] = -1;
123 job
->print_pipes
[0] = -1;
124 job
->print_pipes
[1] = -1;
125 job
->side_pipes
[0] = -1;
126 job
->side_pipes
[1] = -1;
127 job
->status_pipes
[0] = -1;
128 job
->status_pipes
[1] = -1;
130 cupsdSetString(&job
->dest
, dest
);
133 * Add the new job to the "all jobs" and "active jobs" lists...
136 cupsArrayAdd(Jobs
, job
);
137 cupsArrayAdd(ActiveJobs
, job
);
144 * 'cupsdCancelJob()' - Cancel the specified print job.
148 cupsdCancelJob(cupsd_job_t
*job
, /* I - Job to cancel */
149 int purge
, /* I - Purge jobs? */
150 ipp_jstate_t newstate
) /* I - New job state */
152 int i
; /* Looping var */
153 char filename
[1024]; /* Job filename */
154 cupsd_printer_t
*printer
; /* Printer used by job */
157 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdCancelJob: id = %d", job
->id
);
160 * Stop any processes that are working on the current job...
163 printer
= job
->printer
;
165 if (job
->state_value
== IPP_JOB_PROCESSING
)
166 cupsdStopJob(job
, 0);
171 job
->state
->values
[0].integer
= newstate
;
173 job
->state_value
= newstate
;
175 set_time(job
, "time-at-completed");
178 * Send any pending notifications and then expire them...
186 case IPP_JOB_CANCELED
:
187 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, printer
, job
,
188 purge
? "Job purged." : "Job canceled.");
191 case IPP_JOB_ABORTED
:
192 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, printer
, job
,
193 "Job aborted; please consult the error_log file "
197 case IPP_JOB_COMPLETED
:
199 * Clear the printer's printer-state-message and move on...
202 printer
->state_message
[0] = '\0';
204 cupsdSetPrinterState(printer
, IPP_PRINTER_IDLE
, 0);
206 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, printer
, job
,
211 cupsdExpireSubscriptions(NULL
, job
);
214 * Remove the job from the active list...
217 cupsArrayRemove(ActiveJobs
, job
);
220 * Remove any authentication data...
223 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
224 if (cupsdRemoveFile(filename
) && errno
!= ENOENT
)
225 cupsdLogMessage(CUPSD_LOG_ERROR
,
226 "Unable to remove authentication cache: %s",
229 cupsdClearString(&job
->auth_username
);
230 cupsdClearString(&job
->auth_domain
);
231 cupsdClearString(&job
->auth_password
);
235 * Destroy the credential cache and clear the KRB5CCNAME env var string.
240 krb5_cc_destroy(KerberosContext
, job
->ccache
);
244 cupsdClearString(&job
->ccname
);
245 #endif /* HAVE_GSSAPI */
248 * Remove the print file for good if we aren't preserving jobs or
252 job
->current_file
= 0;
254 if (!JobHistory
|| !JobFiles
|| purge
)
256 for (i
= 1; i
<= job
->num_files
; i
++)
258 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
263 if (job
->num_files
> 0)
265 free(job
->filetypes
);
266 free(job
->compressions
);
269 job
->filetypes
= NULL
;
270 job
->compressions
= NULL
;
274 if (JobHistory
&& !purge
)
277 * Save job state info...
285 * Remove the job info file...
288 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
,
293 * Remove the job from the "all jobs" list...
296 cupsArrayRemove(Jobs
, job
);
299 * Free all memory used...
308 * 'cupsdCancelJobs()' - Cancel all jobs for the given destination/user...
312 cupsdCancelJobs(const char *dest
, /* I - Destination to cancel */
313 const char *username
, /* I - Username or NULL */
314 int purge
) /* I - Purge jobs? */
316 cupsd_job_t
*job
; /* Current job */
319 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
321 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
323 if (!job
->dest
|| !job
->username
)
326 if (!job
->dest
|| !job
->username
)
329 if ((dest
== NULL
|| !strcmp(job
->dest
, dest
)) &&
330 (username
== NULL
|| !strcmp(job
->username
, username
)))
333 * Cancel all jobs matching this destination/user...
336 cupsdCancelJob(job
, purge
, IPP_JOB_CANCELED
);
345 * 'cupsdCheckJobs()' - Check the pending jobs and start any if the destination
352 cupsd_job_t
*job
; /* Current job in queue */
353 cupsd_printer_t
*printer
, /* Printer destination */
354 *pclass
; /* Printer class destination */
355 ipp_attribute_t
*attr
; /* Job attribute */
358 DEBUG_puts("cupsdCheckJobs()");
360 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
361 "cupsdCheckJobs: %d active jobs, sleeping=%d, reload=%d",
362 cupsArrayCount(ActiveJobs
), Sleeping
, NeedReload
);
364 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
366 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
369 * Start held jobs if they are ready...
372 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
373 "cupsdCheckJobs: Job %d: state_value=%d, loaded=%s",
374 job
->id
, job
->state_value
, job
->attrs
? "yes" : "no");
376 if (job
->state_value
== IPP_JOB_HELD
&&
378 job
->hold_until
< time(NULL
))
380 if (job
->pending_timeout
)
382 /* Add trailing banner as needed */
383 if (cupsdTimeoutJob(job
))
387 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
388 job
->state_value
= IPP_JOB_PENDING
;
390 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
391 IPP_TAG_KEYWORD
)) == NULL
)
392 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
396 attr
->value_tag
= IPP_TAG_KEYWORD
;
397 cupsdSetString(&(attr
->values
[0].string
.text
), "no-hold");
403 * Start pending jobs if the destination is available...
406 if (job
->state_value
== IPP_JOB_PENDING
&& !NeedReload
&& !Sleeping
)
408 printer
= cupsdFindDest(job
->dest
);
412 (printer
->type
& (CUPS_PRINTER_IMPLICIT
| CUPS_PRINTER_CLASS
)))
415 * If the class is remote, just pass it to the remote server...
420 if (pclass
->state
== IPP_PRINTER_STOPPED
)
422 else if (pclass
->type
& CUPS_PRINTER_REMOTE
)
425 printer
= cupsdFindAvailablePrinter(printer
->name
);
428 if (!printer
&& !pclass
)
431 * Whoa, the printer and/or class for this destination went away;
435 cupsdLogMessage(CUPSD_LOG_WARN
,
436 "[Job %d] Printer/class %s has gone away; canceling job!",
439 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
440 "Job canceled because the destination printer/class has "
443 cupsdCancelJob(job
, 1, IPP_JOB_ABORTED
);
448 * See if the printer is available or remote and not printing a job;
449 * if so, start the job...
455 * Add/update a job-actual-printer-uri attribute for this job
456 * so that we know which printer actually printed the job...
459 if ((attr
= ippFindAttribute(job
->attrs
, "job-actual-printer-uri",
460 IPP_TAG_URI
)) != NULL
)
461 cupsdSetString(&attr
->values
[0].string
.text
, printer
->uri
);
463 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
,
464 "job-actual-printer-uri", NULL
, printer
->uri
);
467 if ((!(printer
->type
& CUPS_PRINTER_DISCOVERED
) && /* Printer is local */
468 printer
->state
== IPP_PRINTER_IDLE
) || /* and idle */
469 ((printer
->type
& CUPS_PRINTER_DISCOVERED
) && /* Printer is remote */
470 !printer
->job
)) /* and not printing */
473 * Clear any message and reasons for the queue...
476 printer
->state_message
[0] = '\0';
478 cupsdSetPrinterReasons(printer
, "none");
484 start_job(job
, printer
);
493 * 'cupsdCleanJobs()' - Clean out old jobs.
499 cupsd_job_t
*job
; /* Current job */
505 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
506 job
&& cupsArrayCount(Jobs
) >= MaxJobs
;
507 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
508 if (job
->state_value
>= IPP_JOB_CANCELED
)
509 cupsdCancelJob(job
, 1, IPP_JOB_CANCELED
);
514 * 'cupsdFinishJob()' - Finish a job.
518 cupsdFinishJob(cupsd_job_t
*job
) /* I - Job */
520 cupsd_printer_t
*printer
; /* Current printer */
521 ipp_attribute_t
*attr
; /* job-hold-until attribute */
524 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] File %d is complete.",
525 job
->id
, job
->current_file
- 1);
527 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
528 "[Job %d] cupsdFinishJob: job->status is %d",
529 job
->id
, job
->status
);
531 if (job
->status_buffer
&&
532 (job
->status
< 0 || job
->current_file
>= job
->num_files
))
535 * Close the pipe and clear the input bit.
538 cupsdRemoveSelect(job
->status_buffer
->fd
);
540 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
541 "[Job %d] cupsdFinishJob: Closing status pipes [ %d %d ]...",
542 job
->id
, job
->status_pipes
[0], job
->status_pipes
[1]);
544 cupsdClosePipe(job
->status_pipes
);
545 cupsdStatBufDelete(job
->status_buffer
);
547 job
->status_buffer
= NULL
;
550 printer
= job
->printer
;
552 update_job_attrs(job
);
557 * Backend had errors; stop it...
560 int exit_code
; /* Exit code from backend */
564 * Convert the status to an exit code. Due to the way the W* macros are
565 * implemented on MacOS X (bug?), we have to store the exit status in a
566 * variable first and then convert...
569 exit_code
= -job
->status
;
570 if (WIFEXITED(exit_code
))
571 exit_code
= WEXITSTATUS(exit_code
);
573 exit_code
= job
->status
;
575 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Backend returned status %d (%s)",
577 exit_code
== CUPS_BACKEND_FAILED
? "failed" :
578 exit_code
== CUPS_BACKEND_AUTH_REQUIRED
?
579 "authentication required" :
580 exit_code
== CUPS_BACKEND_HOLD
? "hold job" :
581 exit_code
== CUPS_BACKEND_STOP
? "stop printer" :
582 exit_code
== CUPS_BACKEND_CANCEL
? "cancel job" :
583 exit_code
< 0 ? "crashed" : "unknown");
586 * Do what needs to be done...
592 case CUPS_BACKEND_FAILED
:
594 * Backend failure, use the error-policy to determine how to
598 cupsdStopJob(job
, 0);
600 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
603 * Mark the job as pending again - we'll retry on another
607 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
608 job
->state_value
= IPP_JOB_PENDING
;
614 * If the job was queued to a class, try requeuing it... For
615 * faxes and retry-job queues, hold the current job for 5 minutes.
618 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
620 else if ((printer
->type
& CUPS_PRINTER_FAX
) ||
621 !strcmp(printer
->error_policy
, "retry-job"))
624 * See how many times we've tried to send the job; if more than
625 * the limit, cancel the job.
630 if (job
->tries
>= JobRetryLimit
)
636 cupsdLogMessage(CUPSD_LOG_ERROR
,
637 "[Job %d] Canceling job since it could not be "
638 "sent after %d tries.",
639 job
->id
, JobRetryLimit
);
641 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
646 * Try again in N seconds...
649 set_hold_until(job
, time(NULL
) + JobRetryInterval
);
651 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
,
652 "Job held due to fax errors; please consult "
653 "the error_log file for details.");
654 cupsdSetPrinterState(printer
, IPP_PRINTER_IDLE
, 0);
657 else if (!strcmp(printer
->error_policy
, "abort-job"))
658 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
661 cupsdSetPrinterState(printer
, IPP_PRINTER_STOPPED
, 1);
663 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
664 "Job stopped due to backend errors; please consult "
665 "the error_log file for details.");
669 case CUPS_BACKEND_CANCEL
:
674 cupsdCancelJob(job
, 0, IPP_JOB_CANCELED
);
677 case CUPS_BACKEND_HOLD
:
682 cupsdStopJob(job
, 0);
684 cupsdSetJobHoldUntil(job
, "indefinite");
686 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
687 job
->state_value
= IPP_JOB_HELD
;
691 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
692 "Job held due to backend errors; please consult "
693 "the error_log file for details.");
696 case CUPS_BACKEND_STOP
:
698 * Stop the printer...
701 cupsdStopJob(job
, 0);
703 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
704 job
->state_value
= IPP_JOB_PENDING
;
707 cupsdSetPrinterState(printer
, IPP_PRINTER_STOPPED
, 1);
709 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
710 "Job stopped due to backend errors; please consult "
711 "the error_log file for details.");
714 case CUPS_BACKEND_AUTH_REQUIRED
:
715 cupsdStopJob(job
, 0);
717 cupsdSetJobHoldUntil(job
, "auth-info-required");
719 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
720 IPP_TAG_KEYWORD
)) == NULL
)
721 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
725 attr
->value_tag
= IPP_TAG_KEYWORD
;
726 cupsdSetString(&(attr
->values
[0].string
.text
),
727 "auth-info-required");
730 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
731 job
->state_value
= IPP_JOB_HELD
;
735 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
736 "Authentication is required for job %d.", job
->id
);
741 * Try printing another job...
746 else if (job
->status
> 0)
749 * Filter had errors; stop job...
752 cupsdLogMessage(CUPSD_LOG_ERROR
,
753 "[Job %d] Job stopped due to filter errors.", job
->id
);
754 cupsdStopJob(job
, 1);
756 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
757 "Job stopped due to filter errors; please consult the "
758 "error_log file for details.");
764 * Job printed successfully; cancel it...
767 if (job
->current_file
< job
->num_files
)
770 * Start the next file in the job...
773 FilterLevel
-= job
->cost
;
774 start_job(job
, printer
);
779 * Close out this job...
782 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Completed successfully.",
784 cupsdCancelJob(job
, 0, IPP_JOB_COMPLETED
);
792 * 'cupsdFreeAllJobs()' - Free all jobs from memory.
796 cupsdFreeAllJobs(void)
798 cupsd_job_t
*job
; /* Current job */
809 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
811 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
813 cupsArrayRemove(Jobs
, job
);
814 cupsArrayRemove(ActiveJobs
, job
);
819 cupsdReleaseSignals();
824 * 'cupsdFindJob()' - Find the specified job.
827 cupsd_job_t
* /* O - Job data */
828 cupsdFindJob(int id
) /* I - Job ID */
830 cupsd_job_t key
; /* Search key */
835 return ((cupsd_job_t
*)cupsArrayFind(Jobs
, &key
));
840 * 'cupsdGetPrinterJobCount()' - Get the number of pending, processing,
841 * or held jobs in a printer or class.
844 int /* O - Job count */
845 cupsdGetPrinterJobCount(
846 const char *dest
) /* I - Printer or class name */
848 int count
; /* Job count */
849 cupsd_job_t
*job
; /* Current job */
852 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
854 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
855 if (job
->dest
&& !strcasecmp(job
->dest
, dest
))
863 * 'cupsdGetUserJobCount()' - Get the number of pending, processing,
864 * or held jobs for a user.
867 int /* O - Job count */
868 cupsdGetUserJobCount(
869 const char *username
) /* I - Username */
871 int count
; /* Job count */
872 cupsd_job_t
*job
; /* Current job */
875 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
877 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
878 if (!strcasecmp(job
->username
, username
))
886 * 'cupsdHoldJob()' - Hold the specified job.
890 cupsdHoldJob(cupsd_job_t
*job
) /* I - Job data */
892 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdHoldJob: id = %d", job
->id
);
894 if (job
->state_value
== IPP_JOB_PROCESSING
)
895 cupsdStopJob(job
, 0);
899 DEBUG_puts("cupsdHoldJob: setting state to held...");
901 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
902 job
->state_value
= IPP_JOB_HELD
;
903 job
->current_file
= 0;
912 * 'cupsdLoadAllJobs()' - Load all jobs from disk.
916 cupsdLoadAllJobs(void)
918 char filename
[1024]; /* Full filename of job.cache file */
919 struct stat fileinfo
, /* Information on job.cache file */
920 dirinfo
; /* Information on RequestRoot dir */
925 * Create the job arrays as needed...
929 Jobs
= cupsArrayNew(compare_jobs
, NULL
);
932 ActiveJobs
= cupsArrayNew(compare_active_jobs
, NULL
);
935 * See whether the job.cache file is older than the RequestRoot directory...
938 snprintf(filename
, sizeof(filename
), "%s/job.cache", CacheDir
);
940 if (stat(filename
, &fileinfo
))
942 fileinfo
.st_mtime
= 0;
945 cupsdLogMessage(CUPSD_LOG_ERROR
,
946 "Unable to get file information for \"%s\" - %s",
947 filename
, strerror(errno
));
950 if (stat(RequestRoot
, &dirinfo
))
952 dirinfo
.st_mtime
= 0;
955 cupsdLogMessage(CUPSD_LOG_ERROR
,
956 "Unable to get directory information for \"%s\" - %s",
957 RequestRoot
, strerror(errno
));
961 * Load the most recent source for job data...
964 if (dirinfo
.st_mtime
> fileinfo
.st_mtime
)
968 load_next_job_id(filename
);
971 load_job_cache(filename
);
974 * Clean out old jobs as needed...
977 if (MaxJobs
> 0 && cupsArrayCount(Jobs
) >= MaxJobs
)
983 * 'cupsdLoadJob()' - Load a single job...
987 cupsdLoadJob(cupsd_job_t
*job
) /* I - Job */
989 char jobfile
[1024]; /* Job filename */
990 cups_file_t
*fp
; /* Job file */
991 int fileid
; /* Current file ID */
992 ipp_attribute_t
*attr
; /* Job attribute */
993 const char *dest
; /* Destination name */
994 cupsd_printer_t
*destptr
; /* Pointer to destination */
995 mime_type_t
**filetypes
; /* New filetypes array */
996 int *compressions
; /* New compressions array */
1001 if (job
->state_value
> IPP_JOB_STOPPED
)
1002 job
->access_time
= time(NULL
);
1007 if ((job
->attrs
= ippNew()) == NULL
)
1009 cupsdLogMessage(CUPSD_LOG_ERROR
,
1010 "[Job %d] Ran out of memory for job attributes!", job
->id
);
1015 * Load job attributes...
1018 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Loading attributes...", job
->id
);
1020 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, job
->id
);
1021 if ((fp
= cupsFileOpen(jobfile
, "r")) == NULL
)
1023 cupsdLogMessage(CUPSD_LOG_ERROR
,
1024 "[Job %d] Unable to open job control file \"%s\" - %s!",
1025 job
->id
, jobfile
, strerror(errno
));
1026 ippDelete(job
->attrs
);
1031 if (ippReadIO(fp
, (ipp_iocb_t
)cupsFileRead
, 1, NULL
, job
->attrs
) != IPP_DATA
)
1033 cupsdLogMessage(CUPSD_LOG_ERROR
,
1034 "[Job %d] Unable to read job control file \"%s\"!",
1037 ippDelete(job
->attrs
);
1046 * Copy attribute data to the job object...
1049 if ((job
->state
= ippFindAttribute(job
->attrs
, "job-state",
1050 IPP_TAG_ENUM
)) == NULL
)
1052 cupsdLogMessage(CUPSD_LOG_ERROR
,
1053 "[Job %d] Missing or bad job-state attribute in "
1056 ippDelete(job
->attrs
);
1062 job
->state_value
= (ipp_jstate_t
)job
->state
->values
[0].integer
;
1066 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1067 IPP_TAG_URI
)) == NULL
)
1069 cupsdLogMessage(CUPSD_LOG_ERROR
,
1070 "[Job %d] No job-printer-uri attribute in control file!",
1072 ippDelete(job
->attrs
);
1078 if ((dest
= cupsdValidateDest(attr
->values
[0].string
.text
, &(job
->dtype
),
1081 cupsdLogMessage(CUPSD_LOG_ERROR
,
1082 "[Job %d] Unable to queue job for destination \"%s\"!",
1083 job
->id
, attr
->values
[0].string
.text
);
1084 ippDelete(job
->attrs
);
1090 cupsdSetString(&job
->dest
, dest
);
1092 else if ((destptr
= cupsdFindDest(job
->dest
)) == NULL
)
1094 cupsdLogMessage(CUPSD_LOG_ERROR
,
1095 "[Job %d] Unable to queue job for destination \"%s\"!",
1096 job
->id
, job
->dest
);
1097 ippDelete(job
->attrs
);
1103 job
->sheets
= ippFindAttribute(job
->attrs
, "job-media-sheets-completed",
1105 job
->job_sheets
= ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_NAME
);
1109 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
1110 IPP_TAG_INTEGER
)) == NULL
)
1112 cupsdLogMessage(CUPSD_LOG_ERROR
,
1113 "[Job %d] Missing or bad job-priority attribute in "
1114 "control file!", job
->id
);
1115 ippDelete(job
->attrs
);
1121 job
->priority
= attr
->values
[0].integer
;
1126 if ((attr
= ippFindAttribute(job
->attrs
, "job-originating-user-name",
1127 IPP_TAG_NAME
)) == NULL
)
1129 cupsdLogMessage(CUPSD_LOG_ERROR
,
1130 "[Job %d] Missing or bad job-originating-user-name "
1131 "attribute in control file!", job
->id
);
1132 ippDelete(job
->attrs
);
1138 cupsdSetString(&job
->username
, attr
->values
[0].string
.text
);
1142 * Set the job hold-until time and state...
1145 if (job
->state_value
== IPP_JOB_HELD
)
1147 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1148 IPP_TAG_KEYWORD
)) == NULL
)
1149 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1152 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
1155 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1156 job
->state_value
= IPP_JOB_PENDING
;
1159 else if (job
->state_value
== IPP_JOB_PROCESSING
)
1161 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1162 job
->state_value
= IPP_JOB_PENDING
;
1165 if (!job
->num_files
)
1168 * Find all the d##### files...
1171 for (fileid
= 1; fileid
< 10000; fileid
++)
1173 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
1176 if (access(jobfile
, 0))
1179 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1180 "[Job %d] Auto-typing document file \"%s\"...",
1183 if (fileid
> job
->num_files
)
1185 if (job
->num_files
== 0)
1187 compressions
= (int *)calloc(fileid
, sizeof(int));
1188 filetypes
= (mime_type_t
**)calloc(fileid
, sizeof(mime_type_t
*));
1192 compressions
= (int *)realloc(job
->compressions
,
1193 sizeof(int) * fileid
);
1194 filetypes
= (mime_type_t
**)realloc(job
->filetypes
,
1195 sizeof(mime_type_t
*) *
1199 if (!compressions
|| !filetypes
)
1201 cupsdLogMessage(CUPSD_LOG_ERROR
,
1202 "[Job %d] Ran out of memory for job file types!",
1207 job
->compressions
= compressions
;
1208 job
->filetypes
= filetypes
;
1209 job
->num_files
= fileid
;
1212 job
->filetypes
[fileid
- 1] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
1213 job
->compressions
+ fileid
- 1);
1215 if (!job
->filetypes
[fileid
- 1])
1216 job
->filetypes
[fileid
- 1] = mimeType(MimeDatabase
, "application",
1222 * Load authentication information as needed...
1225 if (job
->state_value
< IPP_JOB_STOPPED
)
1227 snprintf(jobfile
, sizeof(jobfile
), "%s/a%05d", RequestRoot
, job
->id
);
1229 cupsdClearString(&job
->auth_username
);
1230 cupsdClearString(&job
->auth_domain
);
1231 cupsdClearString(&job
->auth_password
);
1233 if ((fp
= cupsFileOpen(jobfile
, "r")) != NULL
)
1235 int i
, /* Looping var */
1236 bytes
; /* Size of auth data */
1237 char line
[255], /* Line from file */
1238 data
[255]; /* Decoded data */
1242 i
< destptr
->num_auth_info_required
&&
1243 cupsFileGets(fp
, line
, sizeof(line
));
1246 bytes
= sizeof(data
);
1247 httpDecode64_2(data
, &bytes
, line
);
1249 if (!strcmp(destptr
->auth_info_required
[i
], "username"))
1250 cupsdSetStringf(&job
->auth_username
, "AUTH_USERNAME=%s", data
);
1251 else if (!strcmp(destptr
->auth_info_required
[i
], "domain"))
1252 cupsdSetStringf(&job
->auth_domain
, "AUTH_DOMAIN=%s", data
);
1253 else if (!strcmp(destptr
->auth_info_required
[i
], "password"))
1254 cupsdSetStringf(&job
->auth_password
, "AUTH_PASSWORD=%s", data
);
1260 job
->access_time
= time(NULL
);
1265 * 'cupsdMoveJob()' - Move the specified job to a different destination.
1269 cupsdMoveJob(cupsd_job_t
*job
, /* I - Job */
1270 cupsd_printer_t
*p
) /* I - Destination printer or class */
1272 ipp_attribute_t
*attr
; /* job-printer-uri attribute */
1273 const char *olddest
; /* Old destination */
1274 cupsd_printer_t
*oldp
; /* Old pointer */
1278 * Don't move completed jobs...
1281 if (job
->state_value
> IPP_JOB_STOPPED
)
1285 * Get the old destination...
1288 olddest
= job
->dest
;
1291 oldp
= job
->printer
;
1293 oldp
= cupsdFindDest(olddest
);
1296 * Change the destination information...
1299 if (job
->state_value
== IPP_JOB_PROCESSING
)
1300 cupsdStopJob(job
, 0);
1304 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, oldp
, job
,
1305 "Job #%d moved from %s to %s.", job
->id
, olddest
,
1308 cupsdSetString(&job
->dest
, p
->name
);
1309 job
->dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_REMOTE
|
1310 CUPS_PRINTER_IMPLICIT
);
1312 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1313 IPP_TAG_URI
)) != NULL
)
1314 cupsdSetString(&(attr
->values
[0].string
.text
), p
->uri
);
1316 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, p
, job
,
1317 "Job #%d moved from %s to %s.", job
->id
, olddest
,
1325 * 'cupsdReleaseJob()' - Release the specified job.
1329 cupsdReleaseJob(cupsd_job_t
*job
) /* I - Job */
1331 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdReleaseJob: id = %d", job
->id
);
1333 if (job
->state_value
== IPP_JOB_HELD
)
1335 DEBUG_puts("cupsdReleaseJob: setting state to pending...");
1337 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1338 job
->state_value
= IPP_JOB_PENDING
;
1346 * 'cupsdRestartJob()' - Restart the specified job.
1350 cupsdRestartJob(cupsd_job_t
*job
) /* I - Job */
1352 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdRestartJob: id = %d", job
->id
);
1354 if (job
->state_value
== IPP_JOB_STOPPED
|| job
->num_files
)
1356 ipp_jstate_t old_state
; /* Old job state */
1361 old_state
= job
->state_value
;
1364 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1365 job
->state_value
= IPP_JOB_PENDING
;
1369 if (old_state
> IPP_JOB_STOPPED
)
1370 cupsArrayAdd(ActiveJobs
, job
);
1378 * 'cupsdSaveAllJobs()' - Save a summary of all jobs to disk.
1382 cupsdSaveAllJobs(void)
1384 int i
; /* Looping var */
1385 cups_file_t
*fp
; /* Job cache file */
1386 char temp
[1024]; /* Temporary string */
1387 cupsd_job_t
*job
; /* Current job */
1388 time_t curtime
; /* Current time */
1389 struct tm
*curdate
; /* Current date */
1392 snprintf(temp
, sizeof(temp
), "%s/job.cache", CacheDir
);
1393 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
1395 cupsdLogMessage(CUPSD_LOG_ERROR
,
1396 "Unable to create job cache file \"%s\" - %s",
1397 temp
, strerror(errno
));
1401 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving job cache file \"%s\"...", temp
);
1404 * Restrict access to the file...
1407 fchown(cupsFileNumber(fp
), getuid(), Group
);
1408 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
1411 * Write a small header to the file...
1414 curtime
= time(NULL
);
1415 curdate
= localtime(&curtime
);
1416 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
1418 cupsFilePuts(fp
, "# Job cache file for " CUPS_SVERSION
"\n");
1419 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
1420 cupsFilePrintf(fp
, "NextJobId %d\n", NextJobId
);
1423 * Write each job known to the system...
1426 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1428 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1430 cupsFilePrintf(fp
, "<Job %d>\n", job
->id
);
1431 cupsFilePrintf(fp
, "State %d\n", job
->state_value
);
1432 cupsFilePrintf(fp
, "Priority %d\n", job
->priority
);
1433 cupsFilePrintf(fp
, "Username %s\n", job
->username
);
1434 cupsFilePrintf(fp
, "Destination %s\n", job
->dest
);
1435 cupsFilePrintf(fp
, "DestType %d\n", job
->dtype
);
1436 cupsFilePrintf(fp
, "NumFiles %d\n", job
->num_files
);
1437 for (i
= 0; i
< job
->num_files
; i
++)
1438 cupsFilePrintf(fp
, "File %d %s/%s %d\n", i
+ 1, job
->filetypes
[i
]->super
,
1439 job
->filetypes
[i
]->type
, job
->compressions
[i
]);
1440 cupsFilePuts(fp
, "</Job>\n");
1448 * 'cupsdSaveJob()' - Save a job to disk.
1452 cupsdSaveJob(cupsd_job_t
*job
) /* I - Job */
1454 char filename
[1024]; /* Job control filename */
1455 cups_file_t
*fp
; /* Job file */
1458 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSaveJob(job=%p(%d)): job->attrs=%p",
1459 job
, job
->id
, job
->attrs
);
1461 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
, job
->id
);
1463 if ((fp
= cupsFileOpen(filename
, "w")) == NULL
)
1465 cupsdLogMessage(CUPSD_LOG_ERROR
,
1466 "[Job %d] Unable to create job control file \"%s\" - %s.",
1467 job
->id
, filename
, strerror(errno
));
1471 fchmod(cupsFileNumber(fp
), 0600);
1472 fchown(cupsFileNumber(fp
), RunUser
, Group
);
1474 job
->attrs
->state
= IPP_IDLE
;
1476 if (ippWriteIO(fp
, (ipp_iocb_t
)cupsFileWrite
, 1, NULL
,
1477 job
->attrs
) != IPP_DATA
)
1478 cupsdLogMessage(CUPSD_LOG_ERROR
,
1479 "[Job %d] Unable to write job control file!", job
->id
);
1486 * 'cupsdSetJobHoldUntil()' - Set the hold time for a job...
1490 cupsdSetJobHoldUntil(cupsd_job_t
*job
, /* I - Job */
1491 const char *when
) /* I - When to resume */
1493 time_t curtime
; /* Current time */
1494 struct tm
*curdate
; /* Current date */
1495 int hour
; /* Hold hour */
1496 int minute
; /* Hold minute */
1497 int second
; /* Hold second */
1500 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetJobHoldUntil(%d, \"%s\")",
1505 if (!strcmp(when
, "indefinite") || !strcmp(when
, "auth-info-required"))
1508 * Hold indefinitely...
1511 job
->hold_until
= 0;
1513 else if (!strcmp(when
, "day-time"))
1516 * Hold to 6am the next morning unless local time is < 6pm.
1519 curtime
= time(NULL
);
1520 curdate
= localtime(&curtime
);
1522 if (curdate
->tm_hour
< 18)
1523 job
->hold_until
= curtime
;
1525 job
->hold_until
= curtime
+
1526 ((29 - curdate
->tm_hour
) * 60 + 59 -
1527 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1529 else if (!strcmp(when
, "evening") || !strcmp(when
, "night"))
1532 * Hold to 6pm unless local time is > 6pm or < 6am.
1535 curtime
= time(NULL
);
1536 curdate
= localtime(&curtime
);
1538 if (curdate
->tm_hour
< 6 || curdate
->tm_hour
>= 18)
1539 job
->hold_until
= curtime
;
1541 job
->hold_until
= curtime
+
1542 ((17 - curdate
->tm_hour
) * 60 + 59 -
1543 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1545 else if (!strcmp(when
, "second-shift"))
1548 * Hold to 4pm unless local time is > 4pm.
1551 curtime
= time(NULL
);
1552 curdate
= localtime(&curtime
);
1554 if (curdate
->tm_hour
>= 16)
1555 job
->hold_until
= curtime
;
1557 job
->hold_until
= curtime
+
1558 ((15 - curdate
->tm_hour
) * 60 + 59 -
1559 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1561 else if (!strcmp(when
, "third-shift"))
1564 * Hold to 12am unless local time is < 8am.
1567 curtime
= time(NULL
);
1568 curdate
= localtime(&curtime
);
1570 if (curdate
->tm_hour
< 8)
1571 job
->hold_until
= curtime
;
1573 job
->hold_until
= curtime
+
1574 ((23 - curdate
->tm_hour
) * 60 + 59 -
1575 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1577 else if (!strcmp(when
, "weekend"))
1580 * Hold to weekend unless we are in the weekend.
1583 curtime
= time(NULL
);
1584 curdate
= localtime(&curtime
);
1586 if (curdate
->tm_wday
|| curdate
->tm_wday
== 6)
1587 job
->hold_until
= curtime
;
1589 job
->hold_until
= curtime
+
1590 (((5 - curdate
->tm_wday
) * 24 +
1591 (17 - curdate
->tm_hour
)) * 60 + 59 -
1592 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1594 else if (sscanf(when
, "%d:%d:%d", &hour
, &minute
, &second
) >= 2)
1597 * Hold to specified GMT time (HH:MM or HH:MM:SS)...
1600 curtime
= time(NULL
);
1601 curdate
= gmtime(&curtime
);
1603 job
->hold_until
= curtime
+
1604 ((hour
- curdate
->tm_hour
) * 60 + minute
-
1605 curdate
->tm_min
) * 60 + second
- curdate
->tm_sec
;
1608 * Hold until next day as needed...
1611 if (job
->hold_until
< curtime
)
1612 job
->hold_until
+= 24 * 60 * 60;
1615 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetJobHoldUntil: hold_until = %d",
1616 (int)job
->hold_until
);
1621 * 'cupsdSetJobPriority()' - Set the priority of a job, moving it up/down in
1622 * the list as needed.
1626 cupsdSetJobPriority(
1627 cupsd_job_t
*job
, /* I - Job ID */
1628 int priority
) /* I - New priority (0 to 100) */
1630 ipp_attribute_t
*attr
; /* Job attribute */
1634 * Don't change completed jobs...
1637 if (job
->state_value
>= IPP_JOB_PROCESSING
)
1641 * Set the new priority and re-add the job into the active list...
1644 cupsArrayRemove(ActiveJobs
, job
);
1646 job
->priority
= priority
;
1648 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
1649 IPP_TAG_INTEGER
)) != NULL
)
1650 attr
->values
[0].integer
= priority
;
1652 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-priority",
1655 cupsArrayAdd(ActiveJobs
, job
);
1662 * 'cupsdStopAllJobs()' - Stop all print jobs.
1666 cupsdStopAllJobs(int force
) /* I - 1 = Force all filters to stop */
1668 cupsd_job_t
*job
; /* Current job */
1671 DEBUG_puts("cupsdStopAllJobs()");
1673 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
1675 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
1676 if (job
->state_value
== IPP_JOB_PROCESSING
)
1678 cupsdStopJob(job
, force
);
1679 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1680 job
->state_value
= IPP_JOB_PENDING
;
1686 * 'cupsdStopJob()' - Stop a print job.
1690 cupsdStopJob(cupsd_job_t
*job
, /* I - Job */
1691 int force
) /* I - 1 = Force all filters to stop */
1693 int i
; /* Looping var */
1696 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1697 "[Job %d] cupsdStopJob: force = %d", job
->id
, force
);
1699 if (job
->state_value
!= IPP_JOB_PROCESSING
)
1702 FilterLevel
-= job
->cost
;
1704 if (job
->printer
->state
== IPP_PRINTER_PROCESSING
)
1705 cupsdSetPrinterState(job
->printer
, IPP_PRINTER_IDLE
, 0);
1707 job
->state
->values
[0].integer
= IPP_JOB_STOPPED
;
1708 job
->state_value
= IPP_JOB_STOPPED
;
1709 job
->printer
->job
= NULL
;
1710 job
->printer
= NULL
;
1712 job
->current_file
--;
1714 for (i
= 0; job
->filters
[i
]; i
++)
1715 if (job
->filters
[i
] > 0)
1717 cupsdEndProcess(job
->filters
[i
], force
);
1718 job
->filters
[i
] = 0;
1721 if (job
->backend
> 0)
1723 cupsdEndProcess(job
->backend
, force
);
1727 cupsdDestroyProfile(job
->profile
);
1728 job
->profile
= NULL
;
1730 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] Closing print pipes [ %d %d ]...",
1731 job
->id
, job
->print_pipes
[0], job
->print_pipes
[1]);
1733 cupsdClosePipe(job
->print_pipes
);
1735 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] Closing back pipes [ %d %d ]...",
1736 job
->id
, job
->back_pipes
[0], job
->back_pipes
[1]);
1738 cupsdClosePipe(job
->back_pipes
);
1740 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] Closing side pipes [ %d %d ]...",
1741 job
->id
, job
->side_pipes
[0], job
->side_pipes
[1]);
1743 cupsdClosePipe(job
->side_pipes
);
1745 if (job
->status_buffer
)
1748 * Close the pipe and clear the input bit.
1751 cupsdRemoveSelect(job
->status_buffer
->fd
);
1753 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1754 "[Job %d] Closing status pipes [ %d %d ]...", job
->id
,
1755 job
->status_pipes
[0], job
->status_pipes
[1]);
1757 cupsdClosePipe(job
->status_pipes
);
1758 cupsdStatBufDelete(job
->status_buffer
);
1760 job
->status_buffer
= NULL
;
1766 * 'cupsdUnloadCompletedJobs()' - Flush completed job history from memory.
1770 cupsdUnloadCompletedJobs(void)
1772 cupsd_job_t
*job
; /* Current job */
1773 time_t expire
; /* Expiration time */
1776 expire
= time(NULL
) - 60;
1778 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1780 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1781 if (job
->attrs
&& job
->state_value
>= IPP_JOB_STOPPED
&&
1782 job
->access_time
< expire
)
1788 * 'compare_active_jobs()' - Compare the job IDs and priorities of two jobs.
1791 static int /* O - Difference */
1792 compare_active_jobs(void *first
, /* I - First job */
1793 void *second
, /* I - Second job */
1794 void *data
) /* I - App data (not used) */
1796 int diff
; /* Difference */
1799 if ((diff
= ((cupsd_job_t
*)second
)->priority
-
1800 ((cupsd_job_t
*)first
)->priority
) != 0)
1803 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
1808 * 'compare_jobs()' - Compare the job IDs of two jobs.
1811 static int /* O - Difference */
1812 compare_jobs(void *first
, /* I - First job */
1813 void *second
, /* I - Second job */
1814 void *data
) /* I - App data (not used) */
1816 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
1821 * 'free_job()' - Free all memory used by a job.
1825 free_job(cupsd_job_t
*job
) /* I - Job */
1827 cupsdClearString(&job
->username
);
1828 cupsdClearString(&job
->dest
);
1829 cupsdClearString(&job
->auth_username
);
1830 cupsdClearString(&job
->auth_domain
);
1831 cupsdClearString(&job
->auth_password
);
1835 * Destroy the credential cache and clear the KRB5CCNAME env var string.
1840 krb5_cc_destroy(KerberosContext
, job
->ccache
);
1844 cupsdClearString(&job
->ccname
);
1845 #endif /* HAVE_GSSAPI */
1847 if (job
->num_files
> 0)
1849 free(job
->compressions
);
1850 free(job
->filetypes
);
1853 ippDelete(job
->attrs
);
1860 * 'ipp_length()' - Compute the size of the buffer needed to hold
1861 * the textual IPP attributes.
1864 static int /* O - Size of attribute buffer */
1865 ipp_length(ipp_t
*ipp
) /* I - IPP request */
1867 int bytes
; /* Number of bytes */
1868 int i
; /* Looping var */
1869 ipp_attribute_t
*attr
; /* Current attribute */
1873 * Loop through all attributes...
1878 for (attr
= ipp
->attrs
; attr
!= NULL
; attr
= attr
->next
)
1881 * Skip attributes that won't be sent to filters...
1884 if (attr
->value_tag
== IPP_TAG_MIMETYPE
||
1885 attr
->value_tag
== IPP_TAG_NAMELANG
||
1886 attr
->value_tag
== IPP_TAG_TEXTLANG
||
1887 attr
->value_tag
== IPP_TAG_URI
||
1888 attr
->value_tag
== IPP_TAG_URISCHEME
)
1891 if (strncmp(attr
->name
, "time-", 5) == 0)
1895 * Add space for a leading space and commas between each value.
1896 * For the first attribute, the leading space isn't used, so the
1897 * extra byte can be used as the nul terminator...
1900 bytes
++; /* " " separator */
1901 bytes
+= attr
->num_values
; /* "," separators */
1904 * Boolean attributes appear as "foo,nofoo,foo,nofoo", while
1905 * other attributes appear as "foo=value1,value2,...,valueN".
1908 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
1909 bytes
+= strlen(attr
->name
);
1911 bytes
+= attr
->num_values
* strlen(attr
->name
);
1914 * Now add the size required for each value in the attribute...
1917 switch (attr
->value_tag
)
1919 case IPP_TAG_INTEGER
:
1922 * Minimum value of a signed integer is -2147483647, or 11 digits.
1925 bytes
+= attr
->num_values
* 11;
1928 case IPP_TAG_BOOLEAN
:
1930 * Add two bytes for each false ("no") value...
1933 for (i
= 0; i
< attr
->num_values
; i
++)
1934 if (!attr
->values
[i
].boolean
)
1938 case IPP_TAG_RANGE
:
1940 * A range is two signed integers separated by a hyphen, or
1941 * 23 characters max.
1944 bytes
+= attr
->num_values
* 23;
1947 case IPP_TAG_RESOLUTION
:
1949 * A resolution is two signed integers separated by an "x" and
1950 * suffixed by the units, or 26 characters max.
1953 bytes
+= attr
->num_values
* 26;
1956 case IPP_TAG_STRING
:
1959 case IPP_TAG_KEYWORD
:
1960 case IPP_TAG_CHARSET
:
1961 case IPP_TAG_LANGUAGE
:
1964 * Strings can contain characters that need quoting. We need
1965 * at least 2 * len + 2 characters to cover the quotes and
1966 * any backslashes in the string.
1969 for (i
= 0; i
< attr
->num_values
; i
++)
1970 bytes
+= 2 * strlen(attr
->values
[i
].string
.text
) + 2;
1974 break; /* anti-compiler-warning-code */
1983 * 'load_job_cache()' - Load jobs from the job.cache file.
1987 load_job_cache(const char *filename
) /* I - job.cache filename */
1989 cups_file_t
*fp
; /* job.cache file */
1990 char line
[1024], /* Line buffer */
1991 *value
; /* Value on line */
1992 int linenum
; /* Line number in file */
1993 cupsd_job_t
*job
; /* Current job */
1994 int jobid
; /* Job ID */
1995 char jobfile
[1024]; /* Job filename */
1999 * Open the job.cache file...
2002 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
2004 if (errno
!= ENOENT
)
2005 cupsdLogMessage(CUPSD_LOG_ERROR
,
2006 "Unable to open job cache file \"%s\": %s",
2007 filename
, strerror(errno
));
2009 load_request_root();
2015 * Read entries from the job cache file and create jobs as needed.
2018 cupsdLogMessage(CUPSD_LOG_INFO
, "Loading job cache file \"%s\"...",
2024 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
2026 if (!strcasecmp(line
, "NextJobId"))
2029 NextJobId
= atoi(value
);
2031 else if (!strcasecmp(line
, "<Job"))
2035 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing </Job> directive on line %d!",
2042 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing job ID on line %d!", linenum
);
2046 jobid
= atoi(value
);
2050 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad job ID %d on line %d!", jobid
,
2055 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, jobid
);
2056 if (access(jobfile
, 0))
2058 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Job %d] Files have gone away!",
2063 job
= calloc(1, sizeof(cupsd_job_t
));
2066 cupsdLogMessage(CUPSD_LOG_EMERG
,
2067 "[Job %d] Unable to allocate memory for job!", jobid
);
2072 job
->back_pipes
[0] = -1;
2073 job
->back_pipes
[1] = -1;
2074 job
->print_pipes
[0] = -1;
2075 job
->print_pipes
[1] = -1;
2076 job
->side_pipes
[0] = -1;
2077 job
->side_pipes
[1] = -1;
2078 job
->status_pipes
[0] = -1;
2079 job
->status_pipes
[1] = -1;
2081 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Loading from cache...", job
->id
);
2085 cupsdLogMessage(CUPSD_LOG_ERROR
,
2086 "Missing <Job #> directive on line %d!", linenum
);
2089 else if (!strcasecmp(line
, "</Job>"))
2091 cupsArrayAdd(Jobs
, job
);
2093 if (job
->state_value
<= IPP_JOB_STOPPED
)
2095 cupsArrayAdd(ActiveJobs
, job
);
2103 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing value on line %d!", linenum
);
2106 else if (!strcasecmp(line
, "State"))
2108 job
->state_value
= (ipp_jstate_t
)atoi(value
);
2110 if (job
->state_value
< IPP_JOB_PENDING
)
2111 job
->state_value
= IPP_JOB_PENDING
;
2112 else if (job
->state_value
> IPP_JOB_COMPLETED
)
2113 job
->state_value
= IPP_JOB_COMPLETED
;
2115 else if (!strcasecmp(line
, "Priority"))
2117 job
->priority
= atoi(value
);
2119 else if (!strcasecmp(line
, "Username"))
2121 cupsdSetString(&job
->username
, value
);
2123 else if (!strcasecmp(line
, "Destination"))
2125 cupsdSetString(&job
->dest
, value
);
2127 else if (!strcasecmp(line
, "DestType"))
2129 job
->dtype
= (cups_ptype_t
)atoi(value
);
2131 else if (!strcasecmp(line
, "NumFiles"))
2133 job
->num_files
= atoi(value
);
2135 if (job
->num_files
< 0)
2137 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad NumFiles value %d on line %d!",
2138 job
->num_files
, linenum
);
2143 if (job
->num_files
> 0)
2145 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-001", RequestRoot
,
2147 if (access(jobfile
, 0))
2149 cupsdLogMessage(CUPSD_LOG_INFO
,
2150 "[Job %d] Data files have gone away!", job
->id
);
2155 job
->filetypes
= calloc(job
->num_files
, sizeof(mime_type_t
*));
2156 job
->compressions
= calloc(job
->num_files
, sizeof(int));
2158 if (!job
->filetypes
|| !job
->compressions
)
2160 cupsdLogMessage(CUPSD_LOG_EMERG
,
2161 "[Job %d] Unable to allocate memory for %d files!",
2162 job
->id
, job
->num_files
);
2167 else if (!strcasecmp(line
, "File"))
2169 int number
, /* File number */
2170 compression
; /* Compression value */
2171 char super
[MIME_MAX_SUPER
], /* MIME super type */
2172 type
[MIME_MAX_TYPE
]; /* MIME type */
2175 if (sscanf(value
, "%d%*[ \t]%15[^/]/%255s%d", &number
, super
, type
,
2178 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File on line %d!", linenum
);
2182 if (number
< 1 || number
> job
->num_files
)
2184 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File number %d on line %d!",
2191 job
->compressions
[number
] = compression
;
2192 job
->filetypes
[number
] = mimeType(MimeDatabase
, super
, type
);
2194 if (!job
->filetypes
[number
])
2197 * If the original MIME type is unknown, auto-type it!
2200 cupsdLogMessage(CUPSD_LOG_ERROR
,
2201 "[Job %d] Unknown MIME type %s/%s for file %d!",
2202 job
->id
, super
, type
, number
+ 1);
2204 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
2205 job
->id
, number
+ 1);
2206 job
->filetypes
[number
] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
2207 job
->compressions
+ number
);
2210 * If that didn't work, assume it is raw...
2213 if (!job
->filetypes
[number
])
2214 job
->filetypes
[number
] = mimeType(MimeDatabase
, "application",
2219 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unknown %s directive on line %d!",
2228 * 'load_next_job_id()' - Load the NextJobId value from the job.cache file.
2232 load_next_job_id(const char *filename
) /* I - job.cache filename */
2234 cups_file_t
*fp
; /* job.cache file */
2235 char line
[1024], /* Line buffer */
2236 *value
; /* Value on line */
2237 int linenum
; /* Line number in file */
2238 int next_job_id
; /* NextJobId value from line */
2242 * Read the NextJobId directive from the job.cache file and use
2243 * the value (if any).
2246 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
2248 if (errno
!= ENOENT
)
2249 cupsdLogMessage(CUPSD_LOG_ERROR
,
2250 "Unable to open job cache file \"%s\": %s",
2251 filename
, strerror(errno
));
2256 cupsdLogMessage(CUPSD_LOG_INFO
,
2257 "Loading NextJobId from job cache file \"%s\"...", filename
);
2261 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
2263 if (!strcasecmp(line
, "NextJobId"))
2267 next_job_id
= atoi(value
);
2269 if (next_job_id
> NextJobId
)
2270 NextJobId
= next_job_id
;
2281 * 'load_request_root()' - Load jobs from the RequestRoot directory.
2285 load_request_root(void)
2287 cups_dir_t
*dir
; /* Directory */
2288 cups_dentry_t
*dent
; /* Directory entry */
2289 cupsd_job_t
*job
; /* New job */
2293 * Open the requests directory...
2296 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Scanning %s for jobs...", RequestRoot
);
2298 if ((dir
= cupsDirOpen(RequestRoot
)) == NULL
)
2300 cupsdLogMessage(CUPSD_LOG_ERROR
,
2301 "Unable to open spool directory \"%s\": %s",
2302 RequestRoot
, strerror(errno
));
2307 * Read all the c##### files...
2310 while ((dent
= cupsDirRead(dir
)) != NULL
)
2311 if (strlen(dent
->filename
) >= 6 && dent
->filename
[0] == 'c')
2314 * Allocate memory for the job...
2317 if ((job
= calloc(sizeof(cupsd_job_t
), 1)) == NULL
)
2319 cupsdLogMessage(CUPSD_LOG_ERROR
, "Ran out of memory for jobs!");
2325 * Assign the job ID...
2328 job
->id
= atoi(dent
->filename
+ 1);
2329 job
->back_pipes
[0] = -1;
2330 job
->back_pipes
[1] = -1;
2331 job
->print_pipes
[0] = -1;
2332 job
->print_pipes
[1] = -1;
2333 job
->side_pipes
[0] = -1;
2334 job
->side_pipes
[1] = -1;
2335 job
->status_pipes
[0] = -1;
2336 job
->status_pipes
[1] = -1;
2338 if (job
->id
>= NextJobId
)
2339 NextJobId
= job
->id
+ 1;
2348 * Insert the job into the array, sorting by job priority and ID...
2351 cupsArrayAdd(Jobs
, job
);
2353 if (job
->state_value
<= IPP_JOB_STOPPED
)
2354 cupsArrayAdd(ActiveJobs
, job
);
2364 * 'set_time()' - Set one of the "time-at-xyz" attributes...
2368 set_time(cupsd_job_t
*job
, /* I - Job to update */
2369 const char *name
) /* I - Name of attribute */
2371 ipp_attribute_t
*attr
; /* Time attribute */
2374 if ((attr
= ippFindAttribute(job
->attrs
, name
, IPP_TAG_ZERO
)) != NULL
)
2376 attr
->value_tag
= IPP_TAG_INTEGER
;
2377 attr
->values
[0].integer
= time(NULL
);
2383 * 'set_hold_until()' - Set the hold time and update job-hold-until attribute...
2387 set_hold_until(cupsd_job_t
*job
, /* I - Job to update */
2388 time_t holdtime
) /* I - Hold until time */
2390 ipp_attribute_t
*attr
; /* job-hold-until attribute */
2391 struct tm
*holddate
; /* Hold date */
2392 char holdstr
[64]; /* Hold time */
2396 * Set the hold_until value and hold the job...
2399 cupsdLogMessage(CUPSD_LOG_DEBUG
, "set_hold_until: hold_until = %d",
2402 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
2403 job
->state_value
= IPP_JOB_HELD
;
2404 job
->hold_until
= holdtime
;
2407 * Update the job-hold-until attribute with a string representing GMT
2408 * time (HH:MM:SS)...
2411 holddate
= gmtime(&holdtime
);
2412 snprintf(holdstr
, sizeof(holdstr
), "%d:%d:%d", holddate
->tm_hour
,
2413 holddate
->tm_min
, holddate
->tm_sec
);
2415 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2416 IPP_TAG_KEYWORD
)) == NULL
)
2417 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2420 * Either add the attribute or update the value of the existing one
2424 attr
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
2425 "job-hold-until", NULL
, holdstr
);
2427 cupsdSetString(&attr
->values
[0].string
.text
, holdstr
);
2434 * 'start_job()' - Start a print job.
2438 start_job(cupsd_job_t
*job
, /* I - Job ID */
2439 cupsd_printer_t
*printer
) /* I - Printer to print job */
2441 int i
; /* Looping var */
2442 int slot
; /* Pipe slot */
2443 cups_array_t
*filters
, /* Filters for job */
2444 *prefilters
; /* Filters with prefilters */
2445 mime_filter_t
*filter
, /* Current filter */
2446 *prefilter
, /* Prefilter */
2447 port_monitor
; /* Port monitor filter */
2448 char method
[255], /* Method for output */
2449 *optptr
, /* Pointer to options */
2450 *valptr
; /* Pointer in value string */
2451 ipp_attribute_t
*attr
; /* Current attribute */
2452 struct stat backinfo
; /* Backend file information */
2453 int backroot
; /* Run backend as root? */
2454 int pid
; /* Process ID of new filter process */
2455 int banner_page
; /* 1 if banner page, 0 otherwise */
2456 int filterfds
[2][2];/* Pipes used between filters */
2457 int envc
; /* Number of environment variables */
2458 char **argv
, /* Filter command-line arguments */
2459 sani_uri
[1024], /* Sanitized DEVICE_URI env var */
2460 filename
[1024], /* Job filename */
2461 command
[1024], /* Full path to command */
2462 jobid
[255], /* Job ID string */
2463 title
[IPP_MAX_NAME
],
2464 /* Job title string */
2465 copies
[255], /* # copies string */
2466 *envp
[MAX_ENV
+ 16],
2467 /* Environment variables */
2468 charset
[255], /* CHARSET env variable */
2469 class_name
[255],/* CLASS env variable */
2470 classification
[1024],
2471 /* CLASSIFICATION env variable */
2473 /* CONTENT_TYPE env variable */
2475 /* DEVICE_URI env variable */
2476 final_content_type
[1024],
2477 /* FINAL_CONTENT_TYPE env variable */
2478 lang
[255], /* LANG env variable */
2480 apple_language
[255],
2481 /* APPLE_LANGUAGE env variable */
2482 #endif /* __APPLE__ */
2483 ppd
[1024], /* PPD env variable */
2485 /* PRINTER env variable */
2487 /* RIP_MAX_CACHE env variable */
2488 static char *options
= NULL
;/* Full list of options */
2489 static int optlength
= 0; /* Length of option buffer */
2492 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] start_job: file = %d/%d",
2493 job
->id
, job
->current_file
, job
->num_files
);
2495 if (job
->num_files
== 0)
2497 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Job %d] No files, canceling job!",
2500 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2505 * Figure out what filters are required to convert from
2506 * the source to the destination type...
2515 * Remote jobs and raw queues go directly to the printer without
2519 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2520 "[Job %d] Sending job to queue tagged as raw...", job
->id
);
2527 * Local jobs get filtered...
2530 filters
= mimeFilter(MimeDatabase
, job
->filetypes
[job
->current_file
],
2531 printer
->filetype
, &(job
->cost
));
2535 cupsdLogMessage(CUPSD_LOG_ERROR
,
2536 "[Job %d] Unable to convert file %d to printable format!",
2537 job
->current_file
, job
->id
);
2538 cupsdLogMessage(CUPSD_LOG_INFO
,
2539 "Hint: Do you have Ghostscript installed?");
2541 if (LogLevel
< CUPSD_LOG_DEBUG
)
2542 cupsdLogMessage(CUPSD_LOG_INFO
,
2543 "Hint: Try setting the LogLevel to \"debug\".");
2545 job
->current_file
++;
2547 if (job
->current_file
== job
->num_files
)
2548 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2554 * Remove NULL ("-") filters...
2557 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
2559 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
2560 if (!strcmp(filter
->filter
, "-"))
2561 cupsArrayRemove(filters
, filter
);
2563 if (cupsArrayCount(filters
) == 0)
2565 cupsArrayDelete(filters
);
2570 * If this printer has any pre-filters, insert the required pre-filter
2571 * in the filters array...
2574 if (printer
->prefiltertype
&& filters
)
2576 prefilters
= cupsArrayNew(NULL
, NULL
);
2578 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
2580 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
2582 if ((prefilter
= mimeFilterLookup(MimeDatabase
, filter
->src
,
2583 printer
->prefiltertype
)))
2585 cupsArrayAdd(prefilters
, prefilter
);
2586 job
->cost
+= prefilter
->cost
;
2589 cupsArrayAdd(prefilters
, filter
);
2592 cupsArrayDelete(filters
);
2593 filters
= prefilters
;
2598 * Set a minimum cost of 100 for all jobs so that FilterLimit
2599 * works with raw queues and other low-cost paths.
2602 if (job
->cost
< 100)
2606 * See if the filter cost is too high...
2609 if ((FilterLevel
+ job
->cost
) > FilterLimit
&& FilterLevel
> 0 &&
2613 * Don't print this job quite yet...
2616 cupsArrayDelete(filters
);
2618 cupsdLogMessage(CUPSD_LOG_INFO
,
2619 "[Job %d] Holding because filter limit has been reached.",
2621 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2622 "[Job %d] start_job: file=%d, cost=%d, level=%d, limit=%d",
2623 job
->id
, job
->current_file
, job
->cost
, FilterLevel
,
2628 FilterLevel
+= job
->cost
;
2631 * Add decompression/raw filter as needed...
2634 if ((!printer
->raw
&& job
->compressions
[job
->current_file
]) ||
2635 (!filters
&& !printer
->remote
&&
2636 (job
->num_files
> 1 || !strncmp(printer
->device_uri
, "file:", 5))))
2639 * Add gziptoany filter to the front of the list...
2643 filters
= cupsArrayNew(NULL
, NULL
);
2645 if (!cupsArrayInsert(filters
, &gziptoany_filter
))
2647 cupsdLogMessage(CUPSD_LOG_ERROR
,
2648 "[Job %d] Unable to add decompression filter - %s",
2649 job
->id
, strerror(errno
));
2651 cupsArrayDelete(filters
);
2653 job
->current_file
++;
2655 if (job
->current_file
== job
->num_files
)
2656 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2663 * Add port monitor, if any...
2666 if (printer
->port_monitor
)
2669 * Add port monitor to the end of the list...
2673 filters
= cupsArrayNew(NULL
, NULL
);
2675 if (!cupsArrayAdd(filters
, &port_monitor
))
2677 cupsdLogMessage(CUPSD_LOG_ERROR
,
2678 "[Job %d] Unable to add port monitor - %s",
2679 job
->id
, strerror(errno
));
2681 cupsArrayDelete(filters
);
2683 job
->current_file
++;
2685 if (job
->current_file
== job
->num_files
)
2686 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2691 snprintf(port_monitor
.filter
, sizeof(port_monitor
.filter
),
2692 "%s/monitor/%s", ServerBin
, printer
->port_monitor
);
2696 * Update the printer and job state to "processing"...
2699 job
->state
->values
[0].integer
= IPP_JOB_PROCESSING
;
2700 job
->state_value
= IPP_JOB_PROCESSING
;
2703 job
->printer
= printer
;
2706 cupsdSetPrinterState(printer
, IPP_PRINTER_PROCESSING
, 0);
2708 if (job
->current_file
== 0)
2711 * Set the processing time...
2714 set_time(job
, "time-at-processing");
2717 * Create the backchannel pipes and make them non-blocking...
2720 cupsdOpenPipe(job
->back_pipes
);
2722 fcntl(job
->back_pipes
[0], F_SETFL
,
2723 fcntl(job
->back_pipes
[0], F_GETFL
) | O_NONBLOCK
);
2725 fcntl(job
->back_pipes
[1], F_SETFL
,
2726 fcntl(job
->back_pipes
[1], F_GETFL
) | O_NONBLOCK
);
2729 * Create the side-channel pipes and make them non-blocking...
2732 socketpair(AF_LOCAL
, SOCK_STREAM
, 0, job
->side_pipes
);
2734 fcntl(job
->side_pipes
[0], F_SETFL
,
2735 fcntl(job
->side_pipes
[0], F_GETFL
) | O_NONBLOCK
);
2737 fcntl(job
->side_pipes
[1], F_SETFL
,
2738 fcntl(job
->side_pipes
[1], F_GETFL
) | O_NONBLOCK
);
2742 * Determine if we are printing a banner page or not...
2745 if (job
->job_sheets
== NULL
)
2747 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] No job-sheets attribute.",
2749 if ((job
->job_sheets
=
2750 ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_ZERO
)) != NULL
)
2751 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2752 "[Job %d] ... but someone added one without setting "
2753 "job_sheets!", job
->id
);
2755 else if (job
->job_sheets
->num_values
== 1)
2756 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] job-sheets=%s", job
->id
,
2757 job
->job_sheets
->values
[0].string
.text
);
2759 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] job-sheets=%s,%s", job
->id
,
2760 job
->job_sheets
->values
[0].string
.text
,
2761 job
->job_sheets
->values
[1].string
.text
);
2763 if (printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
))
2765 else if (job
->job_sheets
== NULL
)
2767 else if (strcasecmp(job
->job_sheets
->values
[0].string
.text
, "none") != 0 &&
2768 job
->current_file
== 0)
2770 else if (job
->job_sheets
->num_values
> 1 &&
2771 strcasecmp(job
->job_sheets
->values
[1].string
.text
, "none") != 0 &&
2772 job
->current_file
== (job
->num_files
- 1))
2777 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] banner_page = %d", job
->id
,
2781 * Building the options string is harder than it needs to be, but
2782 * for the moment we need to pass strings for command-line args and
2783 * not IPP attribute pointers... :)
2785 * First allocate/reallocate the option buffer as needed...
2788 i
= ipp_length(job
->attrs
);
2795 optptr
= realloc(options
, i
);
2799 cupsdLogMessage(CUPSD_LOG_CRIT
,
2800 "[Job %d] Unable to allocate %d bytes for option buffer!",
2803 cupsArrayDelete(filters
);
2805 FilterLevel
-= job
->cost
;
2807 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2816 * Now loop through the attributes and convert them to the textual
2817 * representation used by the filters...
2823 snprintf(title
, sizeof(title
), "%s-%d", printer
->name
, job
->id
);
2824 strcpy(copies
, "1");
2826 for (attr
= job
->attrs
->attrs
; attr
!= NULL
; attr
= attr
->next
)
2828 if (!strcmp(attr
->name
, "copies") &&
2829 attr
->value_tag
== IPP_TAG_INTEGER
)
2832 * Don't use the # copies attribute if we are printing the job sheets...
2836 sprintf(copies
, "%d", attr
->values
[0].integer
);
2838 else if (!strcmp(attr
->name
, "job-name") &&
2839 (attr
->value_tag
== IPP_TAG_NAME
||
2840 attr
->value_tag
== IPP_TAG_NAMELANG
))
2841 strlcpy(title
, attr
->values
[0].string
.text
, sizeof(title
));
2842 else if (attr
->group_tag
== IPP_TAG_JOB
)
2845 * Filter out other unwanted attributes...
2848 if (attr
->value_tag
== IPP_TAG_MIMETYPE
||
2849 attr
->value_tag
== IPP_TAG_NAMELANG
||
2850 attr
->value_tag
== IPP_TAG_TEXTLANG
||
2851 (attr
->value_tag
== IPP_TAG_URI
&& strcmp(attr
->name
, "job-uuid")) ||
2852 attr
->value_tag
== IPP_TAG_URISCHEME
||
2853 attr
->value_tag
== IPP_TAG_BEGIN_COLLECTION
) /* Not yet supported */
2856 if (!strncmp(attr
->name
, "time-", 5))
2859 if (!strncmp(attr
->name
, "job-", 4) && strcmp(attr
->name
, "job-uuid") &&
2860 !(printer
->type
& CUPS_PRINTER_REMOTE
))
2863 if (!strncmp(attr
->name
, "job-", 4) &&
2864 strcmp(attr
->name
, "job-uuid") &&
2865 strcmp(attr
->name
, "job-billing") &&
2866 strcmp(attr
->name
, "job-sheets") &&
2867 strcmp(attr
->name
, "job-hold-until") &&
2868 strcmp(attr
->name
, "job-priority"))
2871 if ((!strcmp(attr
->name
, "page-label") ||
2872 !strcmp(attr
->name
, "page-border") ||
2873 !strncmp(attr
->name
, "number-up", 9) ||
2874 !strcmp(attr
->name
, "page-ranges") ||
2875 !strcmp(attr
->name
, "page-set") ||
2876 !strcasecmp(attr
->name
, "AP_FIRSTPAGE_InputSlot") ||
2877 !strcasecmp(attr
->name
, "AP_FIRSTPAGE_ManualFeed") ||
2878 !strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
2879 "PMTotalSidesImaged..n.") ||
2880 !strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
2881 "PMTotalBeginPages..n.")) &&
2886 * Otherwise add them to the list...
2889 if (optptr
> options
)
2890 strlcat(optptr
, " ", optlength
- (optptr
- options
));
2892 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
2894 strlcat(optptr
, attr
->name
, optlength
- (optptr
- options
));
2895 strlcat(optptr
, "=", optlength
- (optptr
- options
));
2898 for (i
= 0; i
< attr
->num_values
; i
++)
2901 strlcat(optptr
, ",", optlength
- (optptr
- options
));
2903 optptr
+= strlen(optptr
);
2905 switch (attr
->value_tag
)
2907 case IPP_TAG_INTEGER
:
2909 snprintf(optptr
, optlength
- (optptr
- options
),
2910 "%d", attr
->values
[i
].integer
);
2913 case IPP_TAG_BOOLEAN
:
2914 if (!attr
->values
[i
].boolean
)
2915 strlcat(optptr
, "no", optlength
- (optptr
- options
));
2917 case IPP_TAG_NOVALUE
:
2918 strlcat(optptr
, attr
->name
,
2919 optlength
- (optptr
- options
));
2922 case IPP_TAG_RANGE
:
2923 if (attr
->values
[i
].range
.lower
== attr
->values
[i
].range
.upper
)
2924 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2925 "%d", attr
->values
[i
].range
.lower
);
2927 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2928 "%d-%d", attr
->values
[i
].range
.lower
,
2929 attr
->values
[i
].range
.upper
);
2932 case IPP_TAG_RESOLUTION
:
2933 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2934 "%dx%d%s", attr
->values
[i
].resolution
.xres
,
2935 attr
->values
[i
].resolution
.yres
,
2936 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
2940 case IPP_TAG_STRING
:
2943 case IPP_TAG_KEYWORD
:
2944 case IPP_TAG_CHARSET
:
2945 case IPP_TAG_LANGUAGE
:
2947 for (valptr
= attr
->values
[i
].string
.text
; *valptr
;)
2949 if (strchr(" \t\n\\\'\"", *valptr
))
2951 *optptr
++ = *valptr
++;
2958 break; /* anti-compiler-warning-code */
2962 optptr
+= strlen(optptr
);
2967 * Build the command-line arguments for the filters. Each filter
2968 * has 6 or 7 arguments:
2972 * argv[2] = username
2974 * argv[4] = # copies
2976 * argv[6] = filename (optional; normally stdin)
2978 * This allows legacy printer drivers that use the old System V
2979 * printing interface to be used by CUPS.
2981 * For remote jobs, we send all of the files in the argument list.
2984 if (printer
->remote
&& job
->num_files
> 1)
2985 argv
= calloc(7 + job
->num_files
, sizeof(char *));
2987 argv
= calloc(8, sizeof(char *));
2991 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to allocate argument array!");
2992 cupsArrayDelete(filters
);
2994 FilterLevel
-= job
->cost
;
2996 cupsdStopPrinter(printer
, 0);
3000 sprintf(jobid
, "%d", job
->id
);
3002 argv
[0] = printer
->name
;
3004 argv
[2] = job
->username
;
3009 if (printer
->remote
&& job
->num_files
> 1)
3011 for (i
= 0; i
< job
->num_files
; i
++)
3013 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
3015 argv
[6 + i
] = strdup(filename
);
3020 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
3021 job
->id
, job
->current_file
+ 1);
3025 for (i
= 0; argv
[i
]; i
++)
3026 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3027 "[Job %d] argv[%d]=\"%s\"", job
->id
, i
, argv
[i
]);
3030 * Create environment variable strings for the filters...
3033 attr
= ippFindAttribute(job
->attrs
, "attributes-natural-language",
3037 strcpy(apple_language
, "APPLE_LANGUAGE=");
3038 _cupsAppleLanguage(attr
->values
[0].string
.text
,
3039 apple_language
+ 15, sizeof(apple_language
) - 15);
3040 #endif /* __APPLE__ */
3042 switch (strlen(attr
->values
[0].string
.text
))
3046 * This is an unknown or badly formatted language code; use
3047 * the POSIX locale...
3050 strcpy(lang
, "LANG=C");
3055 * Just the language code (ll)...
3058 snprintf(lang
, sizeof(lang
), "LANG=%s.UTF8",
3059 attr
->values
[0].string
.text
);
3064 * Language and country code (ll-cc)...
3067 snprintf(lang
, sizeof(lang
), "LANG=%c%c_%c%c.UTF8",
3068 attr
->values
[0].string
.text
[0],
3069 attr
->values
[0].string
.text
[1],
3070 toupper(attr
->values
[0].string
.text
[3] & 255),
3071 toupper(attr
->values
[0].string
.text
[4] & 255));
3075 attr
= ippFindAttribute(job
->attrs
, "document-format",
3078 (optptr
= strstr(attr
->values
[0].string
.text
, "charset=")) != NULL
)
3079 snprintf(charset
, sizeof(charset
), "CHARSET=%s", optptr
+ 8);
3082 attr
= ippFindAttribute(job
->attrs
, "attributes-charset",
3084 snprintf(charset
, sizeof(charset
), "CHARSET=%s",
3085 attr
->values
[0].string
.text
);
3088 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s/%s",
3089 job
->filetypes
[job
->current_file
]->super
,
3090 job
->filetypes
[job
->current_file
]->type
);
3091 snprintf(device_uri
, sizeof(device_uri
), "DEVICE_URI=%s",
3092 printer
->device_uri
);
3093 cupsdSanitizeURI(printer
->device_uri
, sani_uri
, sizeof(sani_uri
));
3094 snprintf(ppd
, sizeof(ppd
), "PPD=%s/ppd/%s.ppd", ServerRoot
, printer
->name
);
3095 snprintf(printer_name
, sizeof(printer_name
), "PRINTER=%s", printer
->name
);
3096 snprintf(rip_max_cache
, sizeof(rip_max_cache
), "RIP_MAX_CACHE=%s", RIPCache
);
3098 envc
= cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
3100 envp
[envc
++] = charset
;
3101 envp
[envc
++] = lang
;
3103 envp
[envc
++] = apple_language
;
3104 #endif /* __APPLE__ */
3105 envp
[envc
++] = ppd
;
3106 envp
[envc
++] = rip_max_cache
;
3107 envp
[envc
++] = content_type
;
3108 envp
[envc
++] = device_uri
;
3109 envp
[envc
++] = printer_name
;
3111 if (!printer
->remote
&& !printer
->raw
&&
3112 (filter
= (mime_filter_t
*)cupsArrayLast(filters
)) != NULL
&&
3115 snprintf(final_content_type
, sizeof(final_content_type
),
3116 "FINAL_CONTENT_TYPE=%s/%s",
3117 filter
->dst
->super
, filter
->dst
->type
);
3118 envp
[envc
++] = final_content_type
;
3121 if (Classification
&& !banner_page
)
3123 if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
3124 IPP_TAG_NAME
)) == NULL
)
3125 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3127 else if (attr
->num_values
> 1 &&
3128 strcmp(attr
->values
[1].string
.text
, "none") != 0)
3129 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3130 attr
->values
[1].string
.text
);
3132 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3133 attr
->values
[0].string
.text
);
3135 envp
[envc
++] = classification
;
3138 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
3140 snprintf(class_name
, sizeof(class_name
), "CLASS=%s", job
->dest
);
3141 envp
[envc
++] = class_name
;
3144 if (job
->auth_username
)
3145 envp
[envc
++] = job
->auth_username
;
3146 if (job
->auth_domain
)
3147 envp
[envc
++] = job
->auth_domain
;
3148 if (job
->auth_password
)
3149 envp
[envc
++] = job
->auth_password
;
3153 envp
[envc
++] = job
->ccname
;
3154 #endif /* HAVE_GSSAPI */
3158 for (i
= 0; i
< envc
; i
++)
3159 if (!strncmp(envp
[i
], "CUPSD_AUTH_", 5))
3160 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] envp[%d]=\"CUPSD_AUTH_%c****\"",
3161 job
->id
, i
, envp
[i
][5]);
3162 else if (strncmp(envp
[i
], "DEVICE_URI=", 11))
3163 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] envp[%d]=\"%s\"",
3164 job
->id
, i
, envp
[i
]);
3166 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] envp[%d]=\"DEVICE_URI=%s\"",
3167 job
->id
, i
, sani_uri
);
3169 if (printer
->remote
)
3170 job
->current_file
= job
->num_files
;
3172 job
->current_file
++;
3175 * Now create processes for all of the filters...
3178 filterfds
[0][0] = -1;
3179 filterfds
[0][1] = -1;
3180 filterfds
[1][0] = -1;
3181 filterfds
[1][1] = -1;
3183 if (!job
->status_buffer
)
3185 if (cupsdOpenPipe(job
->status_pipes
))
3187 cupsdLogMessage(CUPSD_LOG_ERROR
,
3188 "[Job %d] Unable to create job status pipes - %s.",
3189 job
->id
, strerror(errno
));
3190 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3191 "Unable to create status pipes - %s.", strerror(errno
));
3193 cupsdAddPrinterHistory(printer
);
3195 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3196 "Job canceled because the server could not create the job "
3202 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3203 "[Job %d] start_job: status_pipes = [ %d %d ]",
3204 job
->id
, job
->status_pipes
[0], job
->status_pipes
[1]);
3206 job
->status_buffer
= cupsdStatBufNew(job
->status_pipes
[0], "[Job %d]",
3208 job
->status_level
= CUPSD_LOG_INFO
;
3212 memset(job
->filters
, 0, sizeof(job
->filters
));
3215 job
->profile
= cupsdCreateProfile(job
->id
);
3217 for (i
= 0, slot
= 0, filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
3219 i
++, filter
= (mime_filter_t
*)cupsArrayNext(filters
))
3221 if (filter
->filter
[0] != '/')
3222 snprintf(command
, sizeof(command
), "%s/filter/%s", ServerBin
,
3225 strlcpy(command
, filter
->filter
, sizeof(command
));
3227 if (i
< (cupsArrayCount(filters
) - 1))
3229 if (cupsdOpenPipe(filterfds
[slot
]))
3231 cupsdLogMessage(CUPSD_LOG_ERROR
,
3232 "[Job %d] Unable to create job filter pipes - %s.",
3233 job
->id
, strerror(errno
));
3234 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3235 "Unable to create filter pipes - %s.", strerror(errno
));
3236 cupsdAddPrinterHistory(printer
);
3238 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3239 "Job canceled because the server could not create the "
3247 if (job
->current_file
== 1)
3249 if (strncmp(printer
->device_uri
, "file:", 5) != 0)
3251 if (cupsdOpenPipe(job
->print_pipes
))
3253 cupsdLogMessage(CUPSD_LOG_ERROR
,
3254 "[Job %d] Unable to create job backend pipes - %s.",
3255 job
->id
, strerror(errno
));
3256 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3257 "Unable to create backend pipes - %s.", strerror(errno
));
3258 cupsdAddPrinterHistory(printer
);
3260 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3261 "Job canceled because the server could not create "
3262 "the backend pipes.");
3269 job
->print_pipes
[0] = -1;
3270 if (!strcmp(printer
->device_uri
, "file:/dev/null") ||
3271 !strcmp(printer
->device_uri
, "file:///dev/null"))
3272 job
->print_pipes
[1] = -1;
3275 if (!strncmp(printer
->device_uri
, "file:/dev/", 10))
3276 job
->print_pipes
[1] = open(printer
->device_uri
+ 5,
3278 else if (!strncmp(printer
->device_uri
, "file:///dev/", 12))
3279 job
->print_pipes
[1] = open(printer
->device_uri
+ 7,
3281 else if (!strncmp(printer
->device_uri
, "file:///", 8))
3282 job
->print_pipes
[1] = open(printer
->device_uri
+ 7,
3283 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
3285 job
->print_pipes
[1] = open(printer
->device_uri
+ 5,
3286 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
3288 if (job
->print_pipes
[1] < 0)
3290 cupsdLogMessage(CUPSD_LOG_ERROR
,
3291 "[Job %d] Unable to open output file \"%s\" - %s.",
3292 job
->id
, printer
->device_uri
, strerror(errno
));
3293 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3294 "Unable to open output file \"%s\" - %s.",
3295 printer
->device_uri
, strerror(errno
));
3297 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3298 "Job canceled because the server could not open the "
3304 fcntl(job
->print_pipes
[1], F_SETFD
,
3305 fcntl(job
->print_pipes
[1], F_GETFD
) | FD_CLOEXEC
);
3309 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3310 "[Job %d] start_job: print_pipes = [ %d %d ]",
3311 job
->id
, job
->print_pipes
[0], job
->print_pipes
[1]);
3314 filterfds
[slot
][0] = job
->print_pipes
[0];
3315 filterfds
[slot
][1] = job
->print_pipes
[1];
3318 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] start_job: filter=\"%s\"",
3320 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3321 "[Job %d] start_job: filterfds[%d]=[ %d %d ]",
3322 job
->id
, slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3324 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
3325 filterfds
[slot
][1], job
->status_pipes
[1],
3326 job
->back_pipes
[0], job
->side_pipes
[0], 0,
3327 job
->profile
, job
->filters
+ i
);
3329 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3330 "[Job %d] start_job: Closing filter pipes for slot %d "
3332 job
->id
, !slot
, filterfds
[!slot
][0], filterfds
[!slot
][1]);
3334 cupsdClosePipe(filterfds
[!slot
]);
3338 cupsdLogMessage(CUPSD_LOG_ERROR
,
3339 "[Job %d] Unable to start filter \"%s\" - %s.",
3340 job
->id
, filter
->filter
, strerror(errno
));
3341 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3342 "Unable to start filter \"%s\" - %s.",
3343 filter
->filter
, strerror(errno
));
3345 cupsdAddPrinterHistory(printer
);
3347 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3348 "Job canceled because the server could not execute a "
3354 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Started filter %s (PID %d)",
3355 job
->id
, command
, pid
);
3361 cupsArrayDelete(filters
);
3364 * Finally, pipe the final output into a backend process if needed...
3367 if (strncmp(printer
->device_uri
, "file:", 5) != 0)
3369 if (job
->current_file
== 1 || printer
->remote
)
3371 sscanf(printer
->device_uri
, "%254[^:]", method
);
3372 snprintf(command
, sizeof(command
), "%s/backend/%s", ServerBin
, method
);
3375 * See if the backend needs to run as root...
3380 else if (stat(command
, &backinfo
))
3383 backroot
= !(backinfo
.st_mode
& (S_IRWXG
| S_IRWXO
));
3387 filterfds
[slot
][0] = -1;
3388 filterfds
[slot
][1] = -1;
3390 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] start_job: backend=\"%s\"",
3392 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3393 "[Job %d] start_job: filterfds[%d] = [ %d %d ]", job
->id
,
3394 slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3396 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
3397 filterfds
[slot
][1], job
->status_pipes
[1],
3398 job
->back_pipes
[1], job
->side_pipes
[1],
3399 backroot
, job
->profile
, &(job
->backend
));
3403 cupsdLogMessage(CUPSD_LOG_ERROR
,
3404 "[Job %d] Unable to start backend \"%s\" - %s.",
3405 job
->id
, method
, strerror(errno
));
3406 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3407 "Unable to start backend \"%s\" - %s.", method
,
3410 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3411 "Job canceled because the server could not execute "
3418 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Started backend %s (PID %d)",
3419 job
->id
, command
, pid
);
3423 if (job
->current_file
== job
->num_files
)
3425 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3426 "[Job %d] start_job: Closing print pipes [ %d %d ]...",
3427 job
->id
, job
->print_pipes
[0], job
->print_pipes
[1]);
3429 cupsdClosePipe(job
->print_pipes
);
3431 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3432 "[Job %d] start_job: Closing back pipes [ %d %d ]...",
3433 job
->id
, job
->back_pipes
[0], job
->back_pipes
[1]);
3435 cupsdClosePipe(job
->back_pipes
);
3437 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3438 "[Job %d] start_job: Closing side pipes [ %d %d ]...",
3439 job
->id
, job
->side_pipes
[0], job
->side_pipes
[1]);
3441 cupsdClosePipe(job
->side_pipes
);
3443 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3444 "[Job %d] start_job: Closing status output pipe %d...",
3445 job
->id
, job
->status_pipes
[1]);
3447 close(job
->status_pipes
[1]);
3448 job
->status_pipes
[1] = -1;
3453 filterfds
[slot
][0] = -1;
3454 filterfds
[slot
][1] = -1;
3456 if (job
->current_file
== job
->num_files
)
3458 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3459 "[Job %d] start_job: Closing print pipes [ %d %d ]...",
3460 job
->id
, job
->print_pipes
[0], job
->print_pipes
[1]);
3462 cupsdClosePipe(job
->print_pipes
);
3464 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3465 "[Job %d] start_job: Closing status output pipe %d...",
3466 job
->id
, job
->status_pipes
[1]);
3468 close(job
->status_pipes
[1]);
3469 job
->status_pipes
[1] = -1;
3473 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3474 "[Job %d] start_job: Closing filter pipes for slot %d "
3476 job
->id
, slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3477 cupsdClosePipe(filterfds
[slot
]);
3479 if (printer
->remote
&& job
->num_files
> 1)
3481 for (i
= 0; i
< job
->num_files
; i
++)
3487 cupsdAddSelect(job
->status_buffer
->fd
, (cupsd_selfunc_t
)update_job
, NULL
,
3490 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
, "Job #%d started.",
3497 * If we get here, we need to abort the current job and close out all
3498 * files and pipes...
3503 for (slot
= 0; slot
< 2; slot
++)
3505 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3506 "[Job %d] start_job: Closing filter pipes for slot %d "
3508 job
->id
, slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3509 cupsdClosePipe(filterfds
[slot
]);
3512 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3513 "[Job %d] start_job: Closing status pipes [ %d %d ]...",
3514 job
->id
, job
->status_pipes
[0], job
->status_pipes
[1]);
3515 cupsdClosePipe(job
->status_pipes
);
3516 cupsdStatBufDelete(job
->status_buffer
);
3518 job
->status_buffer
= NULL
;
3520 cupsArrayDelete(filters
);
3522 if (printer
->remote
&& job
->num_files
> 1)
3524 for (i
= 0; i
< job
->num_files
; i
++)
3530 cupsdStopJob(job
, 0);
3535 * 'unload_job()' - Unload a job from memory.
3539 unload_job(cupsd_job_t
*job
) /* I - Job */
3544 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Unloading...", job
->id
);
3546 ippDelete(job
->attrs
);
3551 job
->job_sheets
= NULL
;
3552 job
->printer_message
= NULL
;
3553 job
->printer_reasons
= NULL
;
3558 * 'update_job()' - Read a status update from a job's filters.
3562 update_job(cupsd_job_t
*job
) /* I - Job to check */
3564 int i
; /* Looping var */
3565 int copies
; /* Number of copies printed */
3566 char message
[1024], /* Message text */
3567 *ptr
; /* Pointer update... */
3568 int loglevel
, /* Log level for message */
3569 event
= 0; /* Events? */
3572 while ((ptr
= cupsdStatBufUpdate(job
->status_buffer
, &loglevel
,
3573 message
, sizeof(message
))) != NULL
)
3576 * Process page and printer state messages as needed...
3579 if (loglevel
== CUPSD_LOG_PAGE
)
3582 * Page message; send the message to the page_log file and update the
3583 * job sheet count...
3588 if (!strncasecmp(message
, "total ", 6))
3591 * Got a total count of pages from a backend or filter...
3594 copies
= atoi(message
+ 6);
3595 copies
-= job
->sheets
->values
[0].integer
; /* Just track the delta */
3597 else if (!sscanf(message
, "%*d%d", &copies
))
3600 job
->sheets
->values
[0].integer
+= copies
;
3602 if (job
->printer
->page_limit
)
3604 cupsd_quota_t
*q
= cupsdUpdateQuota(job
->printer
, job
->username
,
3608 if (AppleQuotas
&& q
->page_count
== -3)
3611 * Quota limit exceeded, cancel job in progress immediately...
3614 cupsdLogMessage(CUPSD_LOG_INFO
,
3615 "[Job %d] Canceled because pages exceed user %s "
3616 "quota limit on printer %s (%s).",
3617 job
->id
, job
->username
, job
->printer
->name
,
3618 job
->printer
->info
);
3620 cupsdCancelJob(job
, 1, IPP_JOB_CANCELED
);
3625 #endif /* __APPLE__ */
3629 cupsdLogPage(job
, message
);
3632 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
3633 "Printed %d page(s).", job
->sheets
->values
[0].integer
);
3635 else if (loglevel
== CUPSD_LOG_STATE
)
3637 if (!strcmp(message
, "paused"))
3639 cupsdStopPrinter(job
->printer
, 1);
3644 cupsdSetPrinterReasons(job
->printer
, message
);
3645 cupsdAddPrinterHistory(job
->printer
);
3646 event
|= CUPSD_EVENT_PRINTER_STATE
;
3649 update_job_attrs(job
);
3651 else if (loglevel
== CUPSD_LOG_ATTR
)
3654 * Set attribute(s)...
3657 int num_attrs
; /* Number of attributes */
3658 cups_option_t
*attrs
; /* Attributes */
3659 const char *attr
; /* Attribute */
3662 num_attrs
= cupsParseOptions(message
, 0, &attrs
);
3664 if ((attr
= cupsGetOption("auth-info-required", num_attrs
,
3667 cupsdSetAuthInfoRequired(job
->printer
, attr
, NULL
);
3668 cupsdSetPrinterAttrs(job
->printer
);
3669 cupsdSaveAllPrinters();
3672 if ((attr
= cupsGetOption("printer-alert", num_attrs
, attrs
)) != NULL
)
3674 cupsdSetString(&job
->printer
->alert
, attr
);
3675 event
|= CUPSD_EVENT_PRINTER_STATE
;
3678 if ((attr
= cupsGetOption("printer-alert-description", num_attrs
,
3681 cupsdSetString(&job
->printer
->alert_description
, attr
);
3682 event
|= CUPSD_EVENT_PRINTER_STATE
;
3685 if ((attr
= cupsGetOption("marker-colors", num_attrs
, attrs
)) != NULL
)
3687 cupsdSetPrinterAttr(job
->printer
, "marker-colors", (char *)attr
);
3688 job
->printer
->marker_time
= time(NULL
);
3689 event
|= CUPSD_EVENT_PRINTER_STATE
;
3692 if ((attr
= cupsGetOption("marker-levels", num_attrs
, attrs
)) != NULL
)
3694 cupsdSetPrinterAttr(job
->printer
, "marker-levels", (char *)attr
);
3695 job
->printer
->marker_time
= time(NULL
);
3696 event
|= CUPSD_EVENT_PRINTER_STATE
;
3699 if ((attr
= cupsGetOption("marker-names", num_attrs
, attrs
)) != NULL
)
3701 cupsdSetPrinterAttr(job
->printer
, "marker-names", (char *)attr
);
3702 job
->printer
->marker_time
= time(NULL
);
3703 event
|= CUPSD_EVENT_PRINTER_STATE
;
3706 if ((attr
= cupsGetOption("marker-types", num_attrs
, attrs
)) != NULL
)
3708 cupsdSetPrinterAttr(job
->printer
, "marker-types", (char *)attr
);
3709 job
->printer
->marker_time
= time(NULL
);
3710 event
|= CUPSD_EVENT_PRINTER_STATE
;
3713 cupsFreeOptions(num_attrs
, attrs
);
3716 else if (!strncmp(message
, "recoverable:", 12))
3718 cupsdSetPrinterReasons(job
->printer
,
3719 "+com.apple.print.recoverable-warning");
3722 while (isspace(*ptr
& 255))
3725 cupsdSetString(&job
->printer
->recoverable
, ptr
);
3726 cupsdAddPrinterHistory(job
->printer
);
3727 event
|= CUPSD_EVENT_PRINTER_STATE
;
3729 else if (!strncmp(message
, "recovered:", 10))
3731 cupsdSetPrinterReasons(job
->printer
,
3732 "-com.apple.print.recoverable-warning");
3735 while (isspace(*ptr
& 255))
3738 cupsdSetString(&job
->printer
->recoverable
, ptr
);
3739 cupsdAddPrinterHistory(job
->printer
);
3740 event
|= CUPSD_EVENT_PRINTER_STATE
;
3742 #endif /* __APPLE__ */
3743 else if (loglevel
<= job
->status_level
)
3746 * Some message to show in the printer-state-message attribute...
3749 if (loglevel
!= CUPSD_LOG_NOTICE
)
3750 job
->status_level
= loglevel
;
3752 strlcpy(job
->printer
->state_message
, message
,
3753 sizeof(job
->printer
->state_message
));
3754 cupsdAddPrinterHistory(job
->printer
);
3755 event
|= CUPSD_EVENT_PRINTER_STATE
;
3757 update_job_attrs(job
);
3760 if (!strchr(job
->status_buffer
->buffer
, '\n'))
3764 if (event
& CUPSD_EVENT_PRINTER_STATE
)
3765 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, job
->printer
, NULL
,
3766 (job
->printer
->type
& CUPS_PRINTER_CLASS
) ?
3767 "Class \"%s\" state changed." :
3768 "Printer \"%s\" state changed.",
3769 job
->printer
->name
);
3771 if (ptr
== NULL
&& !job
->status_buffer
->bufused
)
3774 * See if all of the filters and the backend have returned their
3778 for (i
= 0; job
->filters
[i
] < 0; i
++);
3780 if (job
->filters
[i
])
3783 if (job
->current_file
>= job
->num_files
&& job
->backend
> 0)
3787 * Handle the end of job stuff...
3790 cupsdFinishJob(job
);
3796 * 'update_job_attrs()' - Update the job-printer-* attributes.
3800 update_job_attrs(cupsd_job_t
*job
) /* I - Job to update */
3802 int i
; /* Looping var */
3803 int num_reasons
; /* Actual number of reasons */
3804 const char * const *reasons
; /* Reasons */
3805 static const char *none
= "none", /* "none" */
3811 * Get/create the job-printer-state-* attributes...
3814 if (!job
->printer_message
)
3816 if ((job
->printer_message
= ippFindAttribute(job
->attrs
,
3817 "job-printer-state-message",
3818 IPP_TAG_TEXT
)) == NULL
)
3819 job
->printer_message
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_TEXT
,
3820 "job-printer-state-message",
3824 if (!job
->printer_reasons
)
3825 job
->printer_reasons
= ippFindAttribute(job
->attrs
,
3826 "job-printer-state-reasons",
3830 * If the job isn't printing, return now...
3837 * Otherwise copy the printer-state-message value...
3840 if (job
->printer
->state_message
[0])
3841 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
),
3842 job
->printer
->state_message
);
3845 * ... and the printer-state-reasons value...
3848 if (job
->printer
->num_reasons
== 0)
3851 reasons
= job
->printer
->state
== IPP_PRINTER_STOPPED
? &paused
: &none
;
3855 num_reasons
= job
->printer
->num_reasons
;
3856 reasons
= (const char * const *)job
->printer
->reasons
;
3859 if (!job
->printer_reasons
|| job
->printer_reasons
->num_values
!= num_reasons
)
3861 ippDeleteAttribute(job
->attrs
, job
->printer_reasons
);
3863 job
->printer_reasons
= ippAddStrings(job
->attrs
,
3864 IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
3865 "job-printer-state-reasons",
3866 num_reasons
, NULL
, NULL
);
3869 for (i
= 0; i
< num_reasons
; i
++)
3870 cupsdSetString(&(job
->printer_reasons
->values
[i
].string
.text
), reasons
[i
]);
3875 * End of "$Id: job.c 7005 2007-10-01 23:45:48Z mike $".