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 */
357 DEBUG_puts("cupsdCheckJobs()");
359 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
360 "cupsdCheckJobs: %d active jobs, sleeping=%d, reload=%d",
361 cupsArrayCount(ActiveJobs
), Sleeping
, NeedReload
);
363 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
365 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
368 * Start held jobs if they are ready...
371 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
372 "cupsdCheckJobs: Job %d: state_value=%d, loaded=%s",
373 job
->id
, job
->state_value
, job
->attrs
? "yes" : "no");
375 if (job
->state_value
== IPP_JOB_HELD
&&
377 job
->hold_until
< time(NULL
))
379 if (job
->pending_timeout
)
381 /* Add trailing banner as needed */
382 if (cupsdTimeoutJob(job
))
386 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
387 job
->state_value
= IPP_JOB_PENDING
;
391 * Start pending jobs if the destination is available...
394 if (job
->state_value
== IPP_JOB_PENDING
&& !NeedReload
&& !Sleeping
)
396 printer
= cupsdFindDest(job
->dest
);
400 (printer
->type
& (CUPS_PRINTER_IMPLICIT
| CUPS_PRINTER_CLASS
)))
403 * If the class is remote, just pass it to the remote server...
408 if (pclass
->state
== IPP_PRINTER_STOPPED
)
410 else if (pclass
->type
& CUPS_PRINTER_REMOTE
)
413 printer
= cupsdFindAvailablePrinter(printer
->name
);
416 if (!printer
&& !pclass
)
419 * Whoa, the printer and/or class for this destination went away;
423 cupsdLogMessage(CUPSD_LOG_WARN
,
424 "[Job %d] Printer/class %s has gone away; canceling job!",
427 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
428 "Job canceled because the destination printer/class has "
431 cupsdCancelJob(job
, 1, IPP_JOB_ABORTED
);
436 * See if the printer is available or remote and not printing a job;
437 * if so, start the job...
443 * Add/update a job-actual-printer-uri attribute for this job
444 * so that we know which printer actually printed the job...
447 ipp_attribute_t
*attr
; /* job-actual-printer-uri attribute */
450 if ((attr
= ippFindAttribute(job
->attrs
, "job-actual-printer-uri",
451 IPP_TAG_URI
)) != NULL
)
452 cupsdSetString(&attr
->values
[0].string
.text
, printer
->uri
);
454 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
,
455 "job-actual-printer-uri", NULL
, printer
->uri
);
458 if ((!(printer
->type
& CUPS_PRINTER_DISCOVERED
) && /* Printer is local */
459 printer
->state
== IPP_PRINTER_IDLE
) || /* and idle */
460 ((printer
->type
& CUPS_PRINTER_DISCOVERED
) && /* Printer is remote */
461 !printer
->job
)) /* and not printing */
464 * Clear any message and reasons for the queue...
467 printer
->state_message
[0] = '\0';
469 cupsdSetPrinterReasons(printer
, "none");
475 start_job(job
, printer
);
484 * 'cupsdCleanJobs()' - Clean out old jobs.
490 cupsd_job_t
*job
; /* Current job */
496 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
497 job
&& cupsArrayCount(Jobs
) >= MaxJobs
;
498 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
499 if (job
->state_value
>= IPP_JOB_CANCELED
)
500 cupsdCancelJob(job
, 1, IPP_JOB_CANCELED
);
505 * 'cupsdFinishJob()' - Finish a job.
509 cupsdFinishJob(cupsd_job_t
*job
) /* I - Job */
511 cupsd_printer_t
*printer
; /* Current printer */
512 ipp_attribute_t
*attr
; /* job-hold-until attribute */
515 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] File %d is complete.",
516 job
->id
, job
->current_file
- 1);
518 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
519 "[Job %d] cupsdFinishJob: job->status is %d",
520 job
->id
, job
->status
);
522 if (job
->status_buffer
&&
523 (job
->status
< 0 || job
->current_file
>= job
->num_files
))
526 * Close the pipe and clear the input bit.
529 cupsdRemoveSelect(job
->status_buffer
->fd
);
531 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
532 "[Job %d] cupsdFinishJob: Closing status pipes [ %d %d ]...",
533 job
->id
, job
->status_pipes
[0], job
->status_pipes
[1]);
535 cupsdClosePipe(job
->status_pipes
);
536 cupsdStatBufDelete(job
->status_buffer
);
538 job
->status_buffer
= NULL
;
541 printer
= job
->printer
;
543 update_job_attrs(job
);
548 * Backend had errors; stop it...
551 int exit_code
; /* Exit code from backend */
555 * Convert the status to an exit code. Due to the way the W* macros are
556 * implemented on MacOS X (bug?), we have to store the exit status in a
557 * variable first and then convert...
560 exit_code
= -job
->status
;
561 if (WIFEXITED(exit_code
))
562 exit_code
= WEXITSTATUS(exit_code
);
564 exit_code
= job
->status
;
566 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Backend returned status %d (%s)",
568 exit_code
== CUPS_BACKEND_FAILED
? "failed" :
569 exit_code
== CUPS_BACKEND_AUTH_REQUIRED
?
570 "authentication required" :
571 exit_code
== CUPS_BACKEND_HOLD
? "hold job" :
572 exit_code
== CUPS_BACKEND_STOP
? "stop printer" :
573 exit_code
== CUPS_BACKEND_CANCEL
? "cancel job" :
574 exit_code
< 0 ? "crashed" : "unknown");
577 * Do what needs to be done...
583 case CUPS_BACKEND_FAILED
:
585 * Backend failure, use the error-policy to determine how to
589 cupsdStopJob(job
, 0);
591 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
594 * Mark the job as pending again - we'll retry on another
598 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
599 job
->state_value
= IPP_JOB_PENDING
;
605 * If the job was queued to a class, try requeuing it... For
606 * faxes and retry-job queues, hold the current job for 5 minutes.
609 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
611 else if ((printer
->type
& CUPS_PRINTER_FAX
) ||
612 !strcmp(printer
->error_policy
, "retry-job"))
615 * See how many times we've tried to send the job; if more than
616 * the limit, cancel the job.
621 if (job
->tries
>= JobRetryLimit
)
627 cupsdLogMessage(CUPSD_LOG_ERROR
,
628 "[Job %d] Canceling job since it could not be "
629 "sent after %d tries.",
630 job
->id
, JobRetryLimit
);
632 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
637 * Try again in N seconds...
640 set_hold_until(job
, time(NULL
) + JobRetryInterval
);
642 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
,
643 "Job held due to fax errors; please consult "
644 "the error_log file for details.");
645 cupsdSetPrinterState(printer
, IPP_PRINTER_IDLE
, 0);
648 else if (!strcmp(printer
->error_policy
, "abort-job"))
649 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
652 cupsdSetPrinterState(printer
, IPP_PRINTER_STOPPED
, 1);
654 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
655 "Job stopped due to backend errors; please consult "
656 "the error_log file for details.");
660 case CUPS_BACKEND_CANCEL
:
665 cupsdCancelJob(job
, 0, IPP_JOB_CANCELED
);
668 case CUPS_BACKEND_HOLD
:
673 cupsdStopJob(job
, 0);
675 cupsdSetJobHoldUntil(job
, "indefinite");
677 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
678 job
->state_value
= IPP_JOB_HELD
;
682 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
683 "Job held due to backend errors; please consult "
684 "the error_log file for details.");
687 case CUPS_BACKEND_STOP
:
689 * Stop the printer...
692 cupsdStopJob(job
, 0);
694 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
695 job
->state_value
= IPP_JOB_PENDING
;
698 cupsdSetPrinterState(printer
, IPP_PRINTER_STOPPED
, 1);
700 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
701 "Job stopped due to backend errors; please consult "
702 "the error_log file for details.");
705 case CUPS_BACKEND_AUTH_REQUIRED
:
706 cupsdStopJob(job
, 0);
708 cupsdSetJobHoldUntil(job
, "auth-info-required");
710 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
711 IPP_TAG_KEYWORD
)) == NULL
)
712 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
716 attr
->value_tag
= IPP_TAG_KEYWORD
;
717 cupsdSetString(&(attr
->values
[0].string
.text
),
718 "auth-info-required");
721 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
722 job
->state_value
= IPP_JOB_HELD
;
726 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
727 "Authentication is required for job %d.", job
->id
);
732 * Try printing another job...
737 else if (job
->status
> 0)
740 * Filter had errors; stop job...
743 cupsdLogMessage(CUPSD_LOG_ERROR
,
744 "[Job %d] Job stopped due to filter errors.", job
->id
);
745 cupsdStopJob(job
, 1);
747 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
748 "Job stopped due to filter errors; please consult the "
749 "error_log file for details.");
755 * Job printed successfully; cancel it...
758 if (job
->current_file
< job
->num_files
)
761 * Start the next file in the job...
764 FilterLevel
-= job
->cost
;
765 start_job(job
, printer
);
770 * Close out this job...
773 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Completed successfully.",
775 cupsdCancelJob(job
, 0, IPP_JOB_COMPLETED
);
783 * 'cupsdFreeAllJobs()' - Free all jobs from memory.
787 cupsdFreeAllJobs(void)
789 cupsd_job_t
*job
; /* Current job */
800 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
802 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
804 cupsArrayRemove(Jobs
, job
);
805 cupsArrayRemove(ActiveJobs
, job
);
810 cupsdReleaseSignals();
815 * 'cupsdFindJob()' - Find the specified job.
818 cupsd_job_t
* /* O - Job data */
819 cupsdFindJob(int id
) /* I - Job ID */
821 cupsd_job_t key
; /* Search key */
826 return ((cupsd_job_t
*)cupsArrayFind(Jobs
, &key
));
831 * 'cupsdGetPrinterJobCount()' - Get the number of pending, processing,
832 * or held jobs in a printer or class.
835 int /* O - Job count */
836 cupsdGetPrinterJobCount(
837 const char *dest
) /* I - Printer or class name */
839 int count
; /* Job count */
840 cupsd_job_t
*job
; /* Current job */
843 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
845 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
846 if (job
->dest
&& !strcasecmp(job
->dest
, dest
))
854 * 'cupsdGetUserJobCount()' - Get the number of pending, processing,
855 * or held jobs for a user.
858 int /* O - Job count */
859 cupsdGetUserJobCount(
860 const char *username
) /* I - Username */
862 int count
; /* Job count */
863 cupsd_job_t
*job
; /* Current job */
866 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
868 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
869 if (!strcasecmp(job
->username
, username
))
877 * 'cupsdHoldJob()' - Hold the specified job.
881 cupsdHoldJob(cupsd_job_t
*job
) /* I - Job data */
883 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdHoldJob: id = %d", job
->id
);
885 if (job
->state_value
== IPP_JOB_PROCESSING
)
886 cupsdStopJob(job
, 0);
890 DEBUG_puts("cupsdHoldJob: setting state to held...");
892 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
893 job
->state_value
= IPP_JOB_HELD
;
894 job
->current_file
= 0;
903 * 'cupsdLoadAllJobs()' - Load all jobs from disk.
907 cupsdLoadAllJobs(void)
909 char filename
[1024]; /* Full filename of job.cache file */
910 struct stat fileinfo
, /* Information on job.cache file */
911 dirinfo
; /* Information on RequestRoot dir */
916 * Create the job arrays as needed...
920 Jobs
= cupsArrayNew(compare_jobs
, NULL
);
923 ActiveJobs
= cupsArrayNew(compare_active_jobs
, NULL
);
926 * See whether the job.cache file is older than the RequestRoot directory...
929 snprintf(filename
, sizeof(filename
), "%s/job.cache", CacheDir
);
931 if (stat(filename
, &fileinfo
))
933 fileinfo
.st_mtime
= 0;
936 cupsdLogMessage(CUPSD_LOG_ERROR
,
937 "Unable to get file information for \"%s\" - %s",
938 filename
, strerror(errno
));
941 if (stat(RequestRoot
, &dirinfo
))
943 dirinfo
.st_mtime
= 0;
946 cupsdLogMessage(CUPSD_LOG_ERROR
,
947 "Unable to get directory information for \"%s\" - %s",
948 RequestRoot
, strerror(errno
));
952 * Load the most recent source for job data...
955 if (dirinfo
.st_mtime
> fileinfo
.st_mtime
)
959 load_next_job_id(filename
);
962 load_job_cache(filename
);
965 * Clean out old jobs as needed...
968 if (MaxJobs
> 0 && cupsArrayCount(Jobs
) >= MaxJobs
)
974 * 'cupsdLoadJob()' - Load a single job...
978 cupsdLoadJob(cupsd_job_t
*job
) /* I - Job */
980 char jobfile
[1024]; /* Job filename */
981 cups_file_t
*fp
; /* Job file */
982 int fileid
; /* Current file ID */
983 ipp_attribute_t
*attr
; /* Job attribute */
984 const char *dest
; /* Destination name */
985 cupsd_printer_t
*destptr
; /* Pointer to destination */
986 mime_type_t
**filetypes
; /* New filetypes array */
987 int *compressions
; /* New compressions array */
992 if (job
->state_value
> IPP_JOB_STOPPED
)
993 job
->access_time
= time(NULL
);
998 if ((job
->attrs
= ippNew()) == NULL
)
1000 cupsdLogMessage(CUPSD_LOG_ERROR
,
1001 "[Job %d] Ran out of memory for job attributes!", job
->id
);
1006 * Load job attributes...
1009 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Loading attributes...", job
->id
);
1011 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, job
->id
);
1012 if ((fp
= cupsFileOpen(jobfile
, "r")) == NULL
)
1014 cupsdLogMessage(CUPSD_LOG_ERROR
,
1015 "[Job %d] Unable to open job control file \"%s\" - %s!",
1016 job
->id
, jobfile
, strerror(errno
));
1017 ippDelete(job
->attrs
);
1022 if (ippReadIO(fp
, (ipp_iocb_t
)cupsFileRead
, 1, NULL
, job
->attrs
) != IPP_DATA
)
1024 cupsdLogMessage(CUPSD_LOG_ERROR
,
1025 "[Job %d] Unable to read job control file \"%s\"!",
1028 ippDelete(job
->attrs
);
1037 * Copy attribute data to the job object...
1040 if ((job
->state
= ippFindAttribute(job
->attrs
, "job-state",
1041 IPP_TAG_ENUM
)) == NULL
)
1043 cupsdLogMessage(CUPSD_LOG_ERROR
,
1044 "[Job %d] Missing or bad job-state attribute in "
1047 ippDelete(job
->attrs
);
1053 job
->state_value
= (ipp_jstate_t
)job
->state
->values
[0].integer
;
1057 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1058 IPP_TAG_URI
)) == NULL
)
1060 cupsdLogMessage(CUPSD_LOG_ERROR
,
1061 "[Job %d] No job-printer-uri attribute in control file!",
1063 ippDelete(job
->attrs
);
1069 if ((dest
= cupsdValidateDest(attr
->values
[0].string
.text
, &(job
->dtype
),
1072 cupsdLogMessage(CUPSD_LOG_ERROR
,
1073 "[Job %d] Unable to queue job for destination \"%s\"!",
1074 job
->id
, attr
->values
[0].string
.text
);
1075 ippDelete(job
->attrs
);
1081 cupsdSetString(&job
->dest
, dest
);
1083 else if ((destptr
= cupsdFindDest(job
->dest
)) == NULL
)
1085 cupsdLogMessage(CUPSD_LOG_ERROR
,
1086 "[Job %d] Unable to queue job for destination \"%s\"!",
1087 job
->id
, job
->dest
);
1088 ippDelete(job
->attrs
);
1094 job
->sheets
= ippFindAttribute(job
->attrs
, "job-media-sheets-completed",
1096 job
->job_sheets
= ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_NAME
);
1100 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
1101 IPP_TAG_INTEGER
)) == NULL
)
1103 cupsdLogMessage(CUPSD_LOG_ERROR
,
1104 "[Job %d] Missing or bad job-priority attribute in "
1105 "control file!", job
->id
);
1106 ippDelete(job
->attrs
);
1112 job
->priority
= attr
->values
[0].integer
;
1117 if ((attr
= ippFindAttribute(job
->attrs
, "job-originating-user-name",
1118 IPP_TAG_NAME
)) == NULL
)
1120 cupsdLogMessage(CUPSD_LOG_ERROR
,
1121 "[Job %d] Missing or bad job-originating-user-name "
1122 "attribute in control file!", job
->id
);
1123 ippDelete(job
->attrs
);
1129 cupsdSetString(&job
->username
, attr
->values
[0].string
.text
);
1133 * Set the job hold-until time and state...
1136 if (job
->state_value
== IPP_JOB_HELD
)
1138 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1139 IPP_TAG_KEYWORD
)) == NULL
)
1140 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1143 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
1146 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1147 job
->state_value
= IPP_JOB_PENDING
;
1150 else if (job
->state_value
== IPP_JOB_PROCESSING
)
1152 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1153 job
->state_value
= IPP_JOB_PENDING
;
1156 if (!job
->num_files
)
1159 * Find all the d##### files...
1162 for (fileid
= 1; fileid
< 10000; fileid
++)
1164 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
1167 if (access(jobfile
, 0))
1170 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1171 "[Job %d] Auto-typing document file \"%s\"...",
1174 if (fileid
> job
->num_files
)
1176 if (job
->num_files
== 0)
1178 compressions
= (int *)calloc(fileid
, sizeof(int));
1179 filetypes
= (mime_type_t
**)calloc(fileid
, sizeof(mime_type_t
*));
1183 compressions
= (int *)realloc(job
->compressions
,
1184 sizeof(int) * fileid
);
1185 filetypes
= (mime_type_t
**)realloc(job
->filetypes
,
1186 sizeof(mime_type_t
*) *
1190 if (!compressions
|| !filetypes
)
1192 cupsdLogMessage(CUPSD_LOG_ERROR
,
1193 "[Job %d] Ran out of memory for job file types!",
1198 job
->compressions
= compressions
;
1199 job
->filetypes
= filetypes
;
1200 job
->num_files
= fileid
;
1203 job
->filetypes
[fileid
- 1] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
1204 job
->compressions
+ fileid
- 1);
1206 if (!job
->filetypes
[fileid
- 1])
1207 job
->filetypes
[fileid
- 1] = mimeType(MimeDatabase
, "application",
1213 * Load authentication information as needed...
1216 if (job
->state_value
< IPP_JOB_STOPPED
)
1218 snprintf(jobfile
, sizeof(jobfile
), "%s/a%05d", RequestRoot
, job
->id
);
1220 cupsdClearString(&job
->auth_username
);
1221 cupsdClearString(&job
->auth_domain
);
1222 cupsdClearString(&job
->auth_password
);
1224 if ((fp
= cupsFileOpen(jobfile
, "r")) != NULL
)
1226 int i
, /* Looping var */
1227 bytes
; /* Size of auth data */
1228 char line
[255], /* Line from file */
1229 data
[255]; /* Decoded data */
1233 i
< destptr
->num_auth_info_required
&&
1234 cupsFileGets(fp
, line
, sizeof(line
));
1237 bytes
= sizeof(data
);
1238 httpDecode64_2(data
, &bytes
, line
);
1240 if (!strcmp(destptr
->auth_info_required
[i
], "username"))
1241 cupsdSetStringf(&job
->auth_username
, "AUTH_USERNAME=%s", data
);
1242 else if (!strcmp(destptr
->auth_info_required
[i
], "domain"))
1243 cupsdSetStringf(&job
->auth_domain
, "AUTH_DOMAIN=%s", data
);
1244 else if (!strcmp(destptr
->auth_info_required
[i
], "password"))
1245 cupsdSetStringf(&job
->auth_password
, "AUTH_PASSWORD=%s", data
);
1251 job
->access_time
= time(NULL
);
1256 * 'cupsdMoveJob()' - Move the specified job to a different destination.
1260 cupsdMoveJob(cupsd_job_t
*job
, /* I - Job */
1261 cupsd_printer_t
*p
) /* I - Destination printer or class */
1263 ipp_attribute_t
*attr
; /* job-printer-uri attribute */
1264 const char *olddest
; /* Old destination */
1265 cupsd_printer_t
*oldp
; /* Old pointer */
1269 * Don't move completed jobs...
1272 if (job
->state_value
> IPP_JOB_STOPPED
)
1276 * Get the old destination...
1279 olddest
= job
->dest
;
1282 oldp
= job
->printer
;
1284 oldp
= cupsdFindDest(olddest
);
1287 * Change the destination information...
1290 if (job
->state_value
== IPP_JOB_PROCESSING
)
1291 cupsdStopJob(job
, 0);
1295 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, oldp
, job
,
1296 "Job #%d moved from %s to %s.", job
->id
, olddest
,
1299 cupsdSetString(&job
->dest
, p
->name
);
1300 job
->dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_REMOTE
|
1301 CUPS_PRINTER_IMPLICIT
);
1303 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1304 IPP_TAG_URI
)) != NULL
)
1305 cupsdSetString(&(attr
->values
[0].string
.text
), p
->uri
);
1307 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, p
, job
,
1308 "Job #%d moved from %s to %s.", job
->id
, olddest
,
1316 * 'cupsdReleaseJob()' - Release the specified job.
1320 cupsdReleaseJob(cupsd_job_t
*job
) /* I - Job */
1322 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdReleaseJob: id = %d", job
->id
);
1324 if (job
->state_value
== IPP_JOB_HELD
)
1326 DEBUG_puts("cupsdReleaseJob: setting state to pending...");
1328 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1329 job
->state_value
= IPP_JOB_PENDING
;
1337 * 'cupsdRestartJob()' - Restart the specified job.
1341 cupsdRestartJob(cupsd_job_t
*job
) /* I - Job */
1343 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdRestartJob: id = %d", job
->id
);
1345 if (job
->state_value
== IPP_JOB_STOPPED
|| job
->num_files
)
1347 ipp_jstate_t old_state
; /* Old job state */
1352 old_state
= job
->state_value
;
1355 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1356 job
->state_value
= IPP_JOB_PENDING
;
1360 if (old_state
> IPP_JOB_STOPPED
)
1361 cupsArrayAdd(ActiveJobs
, job
);
1369 * 'cupsdSaveAllJobs()' - Save a summary of all jobs to disk.
1373 cupsdSaveAllJobs(void)
1375 int i
; /* Looping var */
1376 cups_file_t
*fp
; /* Job cache file */
1377 char temp
[1024]; /* Temporary string */
1378 cupsd_job_t
*job
; /* Current job */
1379 time_t curtime
; /* Current time */
1380 struct tm
*curdate
; /* Current date */
1383 snprintf(temp
, sizeof(temp
), "%s/job.cache", CacheDir
);
1384 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
1386 cupsdLogMessage(CUPSD_LOG_ERROR
,
1387 "Unable to create job cache file \"%s\" - %s",
1388 temp
, strerror(errno
));
1392 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving job cache file \"%s\"...", temp
);
1395 * Restrict access to the file...
1398 fchown(cupsFileNumber(fp
), getuid(), Group
);
1399 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
1402 * Write a small header to the file...
1405 curtime
= time(NULL
);
1406 curdate
= localtime(&curtime
);
1407 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
1409 cupsFilePuts(fp
, "# Job cache file for " CUPS_SVERSION
"\n");
1410 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
1411 cupsFilePrintf(fp
, "NextJobId %d\n", NextJobId
);
1414 * Write each job known to the system...
1417 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1419 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1421 cupsFilePrintf(fp
, "<Job %d>\n", job
->id
);
1422 cupsFilePrintf(fp
, "State %d\n", job
->state_value
);
1423 cupsFilePrintf(fp
, "Priority %d\n", job
->priority
);
1424 cupsFilePrintf(fp
, "Username %s\n", job
->username
);
1425 cupsFilePrintf(fp
, "Destination %s\n", job
->dest
);
1426 cupsFilePrintf(fp
, "DestType %d\n", job
->dtype
);
1427 cupsFilePrintf(fp
, "NumFiles %d\n", job
->num_files
);
1428 for (i
= 0; i
< job
->num_files
; i
++)
1429 cupsFilePrintf(fp
, "File %d %s/%s %d\n", i
+ 1, job
->filetypes
[i
]->super
,
1430 job
->filetypes
[i
]->type
, job
->compressions
[i
]);
1431 cupsFilePuts(fp
, "</Job>\n");
1439 * 'cupsdSaveJob()' - Save a job to disk.
1443 cupsdSaveJob(cupsd_job_t
*job
) /* I - Job */
1445 char filename
[1024]; /* Job control filename */
1446 cups_file_t
*fp
; /* Job file */
1449 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSaveJob(job=%p(%d)): job->attrs=%p",
1450 job
, job
->id
, job
->attrs
);
1452 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
, job
->id
);
1454 if ((fp
= cupsFileOpen(filename
, "w")) == NULL
)
1456 cupsdLogMessage(CUPSD_LOG_ERROR
,
1457 "[Job %d] Unable to create job control file \"%s\" - %s.",
1458 job
->id
, filename
, strerror(errno
));
1462 fchmod(cupsFileNumber(fp
), 0600);
1463 fchown(cupsFileNumber(fp
), RunUser
, Group
);
1465 job
->attrs
->state
= IPP_IDLE
;
1467 if (ippWriteIO(fp
, (ipp_iocb_t
)cupsFileWrite
, 1, NULL
,
1468 job
->attrs
) != IPP_DATA
)
1469 cupsdLogMessage(CUPSD_LOG_ERROR
,
1470 "[Job %d] Unable to write job control file!", job
->id
);
1477 * 'cupsdSetJobHoldUntil()' - Set the hold time for a job...
1481 cupsdSetJobHoldUntil(cupsd_job_t
*job
, /* I - Job */
1482 const char *when
) /* I - When to resume */
1484 time_t curtime
; /* Current time */
1485 struct tm
*curdate
; /* Current date */
1486 int hour
; /* Hold hour */
1487 int minute
; /* Hold minute */
1488 int second
; /* Hold second */
1491 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetJobHoldUntil(%d, \"%s\")",
1496 if (!strcmp(when
, "indefinite") || !strcmp(when
, "auth-info-required"))
1499 * Hold indefinitely...
1502 job
->hold_until
= 0;
1504 else if (!strcmp(when
, "day-time"))
1507 * Hold to 6am the next morning unless local time is < 6pm.
1510 curtime
= time(NULL
);
1511 curdate
= localtime(&curtime
);
1513 if (curdate
->tm_hour
< 18)
1514 job
->hold_until
= curtime
;
1516 job
->hold_until
= curtime
+
1517 ((29 - curdate
->tm_hour
) * 60 + 59 -
1518 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1520 else if (!strcmp(when
, "evening") || !strcmp(when
, "night"))
1523 * Hold to 6pm unless local time is > 6pm or < 6am.
1526 curtime
= time(NULL
);
1527 curdate
= localtime(&curtime
);
1529 if (curdate
->tm_hour
< 6 || curdate
->tm_hour
>= 18)
1530 job
->hold_until
= curtime
;
1532 job
->hold_until
= curtime
+
1533 ((17 - curdate
->tm_hour
) * 60 + 59 -
1534 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1536 else if (!strcmp(when
, "second-shift"))
1539 * Hold to 4pm unless local time is > 4pm.
1542 curtime
= time(NULL
);
1543 curdate
= localtime(&curtime
);
1545 if (curdate
->tm_hour
>= 16)
1546 job
->hold_until
= curtime
;
1548 job
->hold_until
= curtime
+
1549 ((15 - curdate
->tm_hour
) * 60 + 59 -
1550 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1552 else if (!strcmp(when
, "third-shift"))
1555 * Hold to 12am unless local time is < 8am.
1558 curtime
= time(NULL
);
1559 curdate
= localtime(&curtime
);
1561 if (curdate
->tm_hour
< 8)
1562 job
->hold_until
= curtime
;
1564 job
->hold_until
= curtime
+
1565 ((23 - curdate
->tm_hour
) * 60 + 59 -
1566 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1568 else if (!strcmp(when
, "weekend"))
1571 * Hold to weekend unless we are in the weekend.
1574 curtime
= time(NULL
);
1575 curdate
= localtime(&curtime
);
1577 if (curdate
->tm_wday
|| curdate
->tm_wday
== 6)
1578 job
->hold_until
= curtime
;
1580 job
->hold_until
= curtime
+
1581 (((5 - curdate
->tm_wday
) * 24 +
1582 (17 - curdate
->tm_hour
)) * 60 + 59 -
1583 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1585 else if (sscanf(when
, "%d:%d:%d", &hour
, &minute
, &second
) >= 2)
1588 * Hold to specified GMT time (HH:MM or HH:MM:SS)...
1591 curtime
= time(NULL
);
1592 curdate
= gmtime(&curtime
);
1594 job
->hold_until
= curtime
+
1595 ((hour
- curdate
->tm_hour
) * 60 + minute
-
1596 curdate
->tm_min
) * 60 + second
- curdate
->tm_sec
;
1599 * Hold until next day as needed...
1602 if (job
->hold_until
< curtime
)
1603 job
->hold_until
+= 24 * 60 * 60;
1606 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetJobHoldUntil: hold_until = %d",
1607 (int)job
->hold_until
);
1612 * 'cupsdSetJobPriority()' - Set the priority of a job, moving it up/down in
1613 * the list as needed.
1617 cupsdSetJobPriority(
1618 cupsd_job_t
*job
, /* I - Job ID */
1619 int priority
) /* I - New priority (0 to 100) */
1621 ipp_attribute_t
*attr
; /* Job attribute */
1625 * Don't change completed jobs...
1628 if (job
->state_value
>= IPP_JOB_PROCESSING
)
1632 * Set the new priority and re-add the job into the active list...
1635 cupsArrayRemove(ActiveJobs
, job
);
1637 job
->priority
= priority
;
1639 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
1640 IPP_TAG_INTEGER
)) != NULL
)
1641 attr
->values
[0].integer
= priority
;
1643 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-priority",
1646 cupsArrayAdd(ActiveJobs
, job
);
1653 * 'cupsdStopAllJobs()' - Stop all print jobs.
1657 cupsdStopAllJobs(int force
) /* I - 1 = Force all filters to stop */
1659 cupsd_job_t
*job
; /* Current job */
1662 DEBUG_puts("cupsdStopAllJobs()");
1664 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
1666 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
1667 if (job
->state_value
== IPP_JOB_PROCESSING
)
1669 cupsdStopJob(job
, force
);
1670 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1671 job
->state_value
= IPP_JOB_PENDING
;
1677 * 'cupsdStopJob()' - Stop a print job.
1681 cupsdStopJob(cupsd_job_t
*job
, /* I - Job */
1682 int force
) /* I - 1 = Force all filters to stop */
1684 int i
; /* Looping var */
1687 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1688 "[Job %d] cupsdStopJob: force = %d", job
->id
, force
);
1690 if (job
->state_value
!= IPP_JOB_PROCESSING
)
1693 FilterLevel
-= job
->cost
;
1695 if (job
->printer
->state
== IPP_PRINTER_PROCESSING
)
1696 cupsdSetPrinterState(job
->printer
, IPP_PRINTER_IDLE
, 0);
1698 job
->state
->values
[0].integer
= IPP_JOB_STOPPED
;
1699 job
->state_value
= IPP_JOB_STOPPED
;
1700 job
->printer
->job
= NULL
;
1701 job
->printer
= NULL
;
1703 job
->current_file
--;
1705 for (i
= 0; job
->filters
[i
]; i
++)
1706 if (job
->filters
[i
] > 0)
1708 cupsdEndProcess(job
->filters
[i
], force
);
1709 job
->filters
[i
] = 0;
1712 if (job
->backend
> 0)
1714 cupsdEndProcess(job
->backend
, force
);
1718 cupsdDestroyProfile(job
->profile
);
1719 job
->profile
= NULL
;
1721 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] Closing print pipes [ %d %d ]...",
1722 job
->id
, job
->print_pipes
[0], job
->print_pipes
[1]);
1724 cupsdClosePipe(job
->print_pipes
);
1726 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] Closing back pipes [ %d %d ]...",
1727 job
->id
, job
->back_pipes
[0], job
->back_pipes
[1]);
1729 cupsdClosePipe(job
->back_pipes
);
1731 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] Closing side pipes [ %d %d ]...",
1732 job
->id
, job
->side_pipes
[0], job
->side_pipes
[1]);
1734 cupsdClosePipe(job
->side_pipes
);
1736 if (job
->status_buffer
)
1739 * Close the pipe and clear the input bit.
1742 cupsdRemoveSelect(job
->status_buffer
->fd
);
1744 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1745 "[Job %d] Closing status pipes [ %d %d ]...", job
->id
,
1746 job
->status_pipes
[0], job
->status_pipes
[1]);
1748 cupsdClosePipe(job
->status_pipes
);
1749 cupsdStatBufDelete(job
->status_buffer
);
1751 job
->status_buffer
= NULL
;
1757 * 'cupsdUnloadCompletedJobs()' - Flush completed job history from memory.
1761 cupsdUnloadCompletedJobs(void)
1763 cupsd_job_t
*job
; /* Current job */
1764 time_t expire
; /* Expiration time */
1767 expire
= time(NULL
) - 60;
1769 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1771 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1772 if (job
->attrs
&& job
->state_value
>= IPP_JOB_STOPPED
&&
1773 job
->access_time
< expire
)
1779 * 'compare_active_jobs()' - Compare the job IDs and priorities of two jobs.
1782 static int /* O - Difference */
1783 compare_active_jobs(void *first
, /* I - First job */
1784 void *second
, /* I - Second job */
1785 void *data
) /* I - App data (not used) */
1787 int diff
; /* Difference */
1790 if ((diff
= ((cupsd_job_t
*)second
)->priority
-
1791 ((cupsd_job_t
*)first
)->priority
) != 0)
1794 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
1799 * 'compare_jobs()' - Compare the job IDs of two jobs.
1802 static int /* O - Difference */
1803 compare_jobs(void *first
, /* I - First job */
1804 void *second
, /* I - Second job */
1805 void *data
) /* I - App data (not used) */
1807 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
1812 * 'free_job()' - Free all memory used by a job.
1816 free_job(cupsd_job_t
*job
) /* I - Job */
1818 cupsdClearString(&job
->username
);
1819 cupsdClearString(&job
->dest
);
1820 cupsdClearString(&job
->auth_username
);
1821 cupsdClearString(&job
->auth_domain
);
1822 cupsdClearString(&job
->auth_password
);
1826 * Destroy the credential cache and clear the KRB5CCNAME env var string.
1831 krb5_cc_destroy(KerberosContext
, job
->ccache
);
1835 cupsdClearString(&job
->ccname
);
1836 #endif /* HAVE_GSSAPI */
1838 if (job
->num_files
> 0)
1840 free(job
->compressions
);
1841 free(job
->filetypes
);
1844 ippDelete(job
->attrs
);
1851 * 'ipp_length()' - Compute the size of the buffer needed to hold
1852 * the textual IPP attributes.
1855 static int /* O - Size of attribute buffer */
1856 ipp_length(ipp_t
*ipp
) /* I - IPP request */
1858 int bytes
; /* Number of bytes */
1859 int i
; /* Looping var */
1860 ipp_attribute_t
*attr
; /* Current attribute */
1864 * Loop through all attributes...
1869 for (attr
= ipp
->attrs
; attr
!= NULL
; attr
= attr
->next
)
1872 * Skip attributes that won't be sent to filters...
1875 if (attr
->value_tag
== IPP_TAG_MIMETYPE
||
1876 attr
->value_tag
== IPP_TAG_NAMELANG
||
1877 attr
->value_tag
== IPP_TAG_TEXTLANG
||
1878 attr
->value_tag
== IPP_TAG_URI
||
1879 attr
->value_tag
== IPP_TAG_URISCHEME
)
1882 if (strncmp(attr
->name
, "time-", 5) == 0)
1886 * Add space for a leading space and commas between each value.
1887 * For the first attribute, the leading space isn't used, so the
1888 * extra byte can be used as the nul terminator...
1891 bytes
++; /* " " separator */
1892 bytes
+= attr
->num_values
; /* "," separators */
1895 * Boolean attributes appear as "foo,nofoo,foo,nofoo", while
1896 * other attributes appear as "foo=value1,value2,...,valueN".
1899 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
1900 bytes
+= strlen(attr
->name
);
1902 bytes
+= attr
->num_values
* strlen(attr
->name
);
1905 * Now add the size required for each value in the attribute...
1908 switch (attr
->value_tag
)
1910 case IPP_TAG_INTEGER
:
1913 * Minimum value of a signed integer is -2147483647, or 11 digits.
1916 bytes
+= attr
->num_values
* 11;
1919 case IPP_TAG_BOOLEAN
:
1921 * Add two bytes for each false ("no") value...
1924 for (i
= 0; i
< attr
->num_values
; i
++)
1925 if (!attr
->values
[i
].boolean
)
1929 case IPP_TAG_RANGE
:
1931 * A range is two signed integers separated by a hyphen, or
1932 * 23 characters max.
1935 bytes
+= attr
->num_values
* 23;
1938 case IPP_TAG_RESOLUTION
:
1940 * A resolution is two signed integers separated by an "x" and
1941 * suffixed by the units, or 26 characters max.
1944 bytes
+= attr
->num_values
* 26;
1947 case IPP_TAG_STRING
:
1950 case IPP_TAG_KEYWORD
:
1951 case IPP_TAG_CHARSET
:
1952 case IPP_TAG_LANGUAGE
:
1955 * Strings can contain characters that need quoting. We need
1956 * at least 2 * len + 2 characters to cover the quotes and
1957 * any backslashes in the string.
1960 for (i
= 0; i
< attr
->num_values
; i
++)
1961 bytes
+= 2 * strlen(attr
->values
[i
].string
.text
) + 2;
1965 break; /* anti-compiler-warning-code */
1974 * 'load_job_cache()' - Load jobs from the job.cache file.
1978 load_job_cache(const char *filename
) /* I - job.cache filename */
1980 cups_file_t
*fp
; /* job.cache file */
1981 char line
[1024], /* Line buffer */
1982 *value
; /* Value on line */
1983 int linenum
; /* Line number in file */
1984 cupsd_job_t
*job
; /* Current job */
1985 int jobid
; /* Job ID */
1986 char jobfile
[1024]; /* Job filename */
1990 * Open the job.cache file...
1993 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
1995 if (errno
!= ENOENT
)
1996 cupsdLogMessage(CUPSD_LOG_ERROR
,
1997 "Unable to open job cache file \"%s\": %s",
1998 filename
, strerror(errno
));
2000 load_request_root();
2006 * Read entries from the job cache file and create jobs as needed.
2009 cupsdLogMessage(CUPSD_LOG_INFO
, "Loading job cache file \"%s\"...",
2015 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
2017 if (!strcasecmp(line
, "NextJobId"))
2020 NextJobId
= atoi(value
);
2022 else if (!strcasecmp(line
, "<Job"))
2026 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing </Job> directive on line %d!",
2033 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing job ID on line %d!", linenum
);
2037 jobid
= atoi(value
);
2041 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad job ID %d on line %d!", jobid
,
2046 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, jobid
);
2047 if (access(jobfile
, 0))
2049 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Job %d] Files have gone away!",
2054 job
= calloc(1, sizeof(cupsd_job_t
));
2057 cupsdLogMessage(CUPSD_LOG_EMERG
,
2058 "[Job %d] Unable to allocate memory for job!", jobid
);
2063 job
->back_pipes
[0] = -1;
2064 job
->back_pipes
[1] = -1;
2065 job
->print_pipes
[0] = -1;
2066 job
->print_pipes
[1] = -1;
2067 job
->side_pipes
[0] = -1;
2068 job
->side_pipes
[1] = -1;
2069 job
->status_pipes
[0] = -1;
2070 job
->status_pipes
[1] = -1;
2072 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Loading from cache...", job
->id
);
2076 cupsdLogMessage(CUPSD_LOG_ERROR
,
2077 "Missing <Job #> directive on line %d!", linenum
);
2080 else if (!strcasecmp(line
, "</Job>"))
2082 cupsArrayAdd(Jobs
, job
);
2084 if (job
->state_value
<= IPP_JOB_STOPPED
)
2086 cupsArrayAdd(ActiveJobs
, job
);
2094 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing value on line %d!", linenum
);
2097 else if (!strcasecmp(line
, "State"))
2099 job
->state_value
= (ipp_jstate_t
)atoi(value
);
2101 if (job
->state_value
< IPP_JOB_PENDING
)
2102 job
->state_value
= IPP_JOB_PENDING
;
2103 else if (job
->state_value
> IPP_JOB_COMPLETED
)
2104 job
->state_value
= IPP_JOB_COMPLETED
;
2106 else if (!strcasecmp(line
, "Priority"))
2108 job
->priority
= atoi(value
);
2110 else if (!strcasecmp(line
, "Username"))
2112 cupsdSetString(&job
->username
, value
);
2114 else if (!strcasecmp(line
, "Destination"))
2116 cupsdSetString(&job
->dest
, value
);
2118 else if (!strcasecmp(line
, "DestType"))
2120 job
->dtype
= (cups_ptype_t
)atoi(value
);
2122 else if (!strcasecmp(line
, "NumFiles"))
2124 job
->num_files
= atoi(value
);
2126 if (job
->num_files
< 0)
2128 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad NumFiles value %d on line %d!",
2129 job
->num_files
, linenum
);
2134 if (job
->num_files
> 0)
2136 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-001", RequestRoot
,
2138 if (access(jobfile
, 0))
2140 cupsdLogMessage(CUPSD_LOG_INFO
,
2141 "[Job %d] Data files have gone away!", job
->id
);
2146 job
->filetypes
= calloc(job
->num_files
, sizeof(mime_type_t
*));
2147 job
->compressions
= calloc(job
->num_files
, sizeof(int));
2149 if (!job
->filetypes
|| !job
->compressions
)
2151 cupsdLogMessage(CUPSD_LOG_EMERG
,
2152 "[Job %d] Unable to allocate memory for %d files!",
2153 job
->id
, job
->num_files
);
2158 else if (!strcasecmp(line
, "File"))
2160 int number
, /* File number */
2161 compression
; /* Compression value */
2162 char super
[MIME_MAX_SUPER
], /* MIME super type */
2163 type
[MIME_MAX_TYPE
]; /* MIME type */
2166 if (sscanf(value
, "%d%*[ \t]%15[^/]/%255s%d", &number
, super
, type
,
2169 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File on line %d!", linenum
);
2173 if (number
< 1 || number
> job
->num_files
)
2175 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File number %d on line %d!",
2182 job
->compressions
[number
] = compression
;
2183 job
->filetypes
[number
] = mimeType(MimeDatabase
, super
, type
);
2185 if (!job
->filetypes
[number
])
2188 * If the original MIME type is unknown, auto-type it!
2191 cupsdLogMessage(CUPSD_LOG_ERROR
,
2192 "[Job %d] Unknown MIME type %s/%s for file %d!",
2193 job
->id
, super
, type
, number
+ 1);
2195 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
2196 job
->id
, number
+ 1);
2197 job
->filetypes
[number
] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
2198 job
->compressions
+ number
);
2201 * If that didn't work, assume it is raw...
2204 if (!job
->filetypes
[number
])
2205 job
->filetypes
[number
] = mimeType(MimeDatabase
, "application",
2210 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unknown %s directive on line %d!",
2219 * 'load_next_job_id()' - Load the NextJobId value from the job.cache file.
2223 load_next_job_id(const char *filename
) /* I - job.cache filename */
2225 cups_file_t
*fp
; /* job.cache file */
2226 char line
[1024], /* Line buffer */
2227 *value
; /* Value on line */
2228 int linenum
; /* Line number in file */
2229 int next_job_id
; /* NextJobId value from line */
2233 * Read the NextJobId directive from the job.cache file and use
2234 * the value (if any).
2237 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
2239 if (errno
!= ENOENT
)
2240 cupsdLogMessage(CUPSD_LOG_ERROR
,
2241 "Unable to open job cache file \"%s\": %s",
2242 filename
, strerror(errno
));
2247 cupsdLogMessage(CUPSD_LOG_INFO
,
2248 "Loading NextJobId from job cache file \"%s\"...", filename
);
2252 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
2254 if (!strcasecmp(line
, "NextJobId"))
2258 next_job_id
= atoi(value
);
2260 if (next_job_id
> NextJobId
)
2261 NextJobId
= next_job_id
;
2272 * 'load_request_root()' - Load jobs from the RequestRoot directory.
2276 load_request_root(void)
2278 cups_dir_t
*dir
; /* Directory */
2279 cups_dentry_t
*dent
; /* Directory entry */
2280 cupsd_job_t
*job
; /* New job */
2284 * Open the requests directory...
2287 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Scanning %s for jobs...", RequestRoot
);
2289 if ((dir
= cupsDirOpen(RequestRoot
)) == NULL
)
2291 cupsdLogMessage(CUPSD_LOG_ERROR
,
2292 "Unable to open spool directory \"%s\": %s",
2293 RequestRoot
, strerror(errno
));
2298 * Read all the c##### files...
2301 while ((dent
= cupsDirRead(dir
)) != NULL
)
2302 if (strlen(dent
->filename
) >= 6 && dent
->filename
[0] == 'c')
2305 * Allocate memory for the job...
2308 if ((job
= calloc(sizeof(cupsd_job_t
), 1)) == NULL
)
2310 cupsdLogMessage(CUPSD_LOG_ERROR
, "Ran out of memory for jobs!");
2316 * Assign the job ID...
2319 job
->id
= atoi(dent
->filename
+ 1);
2320 job
->back_pipes
[0] = -1;
2321 job
->back_pipes
[1] = -1;
2322 job
->print_pipes
[0] = -1;
2323 job
->print_pipes
[1] = -1;
2324 job
->side_pipes
[0] = -1;
2325 job
->side_pipes
[1] = -1;
2326 job
->status_pipes
[0] = -1;
2327 job
->status_pipes
[1] = -1;
2329 if (job
->id
>= NextJobId
)
2330 NextJobId
= job
->id
+ 1;
2339 * Insert the job into the array, sorting by job priority and ID...
2342 cupsArrayAdd(Jobs
, job
);
2344 if (job
->state_value
<= IPP_JOB_STOPPED
)
2345 cupsArrayAdd(ActiveJobs
, job
);
2355 * 'set_time()' - Set one of the "time-at-xyz" attributes...
2359 set_time(cupsd_job_t
*job
, /* I - Job to update */
2360 const char *name
) /* I - Name of attribute */
2362 ipp_attribute_t
*attr
; /* Time attribute */
2365 if ((attr
= ippFindAttribute(job
->attrs
, name
, IPP_TAG_ZERO
)) != NULL
)
2367 attr
->value_tag
= IPP_TAG_INTEGER
;
2368 attr
->values
[0].integer
= time(NULL
);
2374 * 'set_hold_until()' - Set the hold time and update job-hold-until attribute...
2378 set_hold_until(cupsd_job_t
*job
, /* I - Job to update */
2379 time_t holdtime
) /* I - Hold until time */
2381 ipp_attribute_t
*attr
; /* job-hold-until attribute */
2382 struct tm
*holddate
; /* Hold date */
2383 char holdstr
[64]; /* Hold time */
2387 * Set the hold_until value and hold the job...
2390 cupsdLogMessage(CUPSD_LOG_DEBUG
, "set_hold_until: hold_until = %d",
2393 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
2394 job
->state_value
= IPP_JOB_HELD
;
2395 job
->hold_until
= holdtime
;
2398 * Update the job-hold-until attribute with a string representing GMT
2399 * time (HH:MM:SS)...
2402 holddate
= gmtime(&holdtime
);
2403 snprintf(holdstr
, sizeof(holdstr
), "%d:%d:%d", holddate
->tm_hour
,
2404 holddate
->tm_min
, holddate
->tm_sec
);
2406 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2407 IPP_TAG_KEYWORD
)) == NULL
)
2408 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2411 * Either add the attribute or update the value of the existing one
2415 attr
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
2416 "job-hold-until", NULL
, holdstr
);
2418 cupsdSetString(&attr
->values
[0].string
.text
, holdstr
);
2425 * 'start_job()' - Start a print job.
2429 start_job(cupsd_job_t
*job
, /* I - Job ID */
2430 cupsd_printer_t
*printer
) /* I - Printer to print job */
2432 int i
; /* Looping var */
2433 int slot
; /* Pipe slot */
2434 cups_array_t
*filters
, /* Filters for job */
2435 *prefilters
; /* Filters with prefilters */
2436 mime_filter_t
*filter
, /* Current filter */
2437 *prefilter
, /* Prefilter */
2438 port_monitor
; /* Port monitor filter */
2439 char method
[255], /* Method for output */
2440 *optptr
, /* Pointer to options */
2441 *valptr
; /* Pointer in value string */
2442 ipp_attribute_t
*attr
; /* Current attribute */
2443 struct stat backinfo
; /* Backend file information */
2444 int backroot
; /* Run backend as root? */
2445 int pid
; /* Process ID of new filter process */
2446 int banner_page
; /* 1 if banner page, 0 otherwise */
2447 int filterfds
[2][2];/* Pipes used between filters */
2448 int envc
; /* Number of environment variables */
2449 char **argv
, /* Filter command-line arguments */
2450 sani_uri
[1024], /* Sanitized DEVICE_URI env var */
2451 filename
[1024], /* Job filename */
2452 command
[1024], /* Full path to command */
2453 jobid
[255], /* Job ID string */
2454 title
[IPP_MAX_NAME
],
2455 /* Job title string */
2456 copies
[255], /* # copies string */
2457 *envp
[MAX_ENV
+ 16],
2458 /* Environment variables */
2459 charset
[255], /* CHARSET env variable */
2460 class_name
[255],/* CLASS env variable */
2461 classification
[1024],
2462 /* CLASSIFICATION env variable */
2464 /* CONTENT_TYPE env variable */
2466 /* DEVICE_URI env variable */
2467 final_content_type
[1024],
2468 /* FINAL_CONTENT_TYPE env variable */
2469 lang
[255], /* LANG env variable */
2471 apple_language
[255],
2472 /* APPLE_LANGUAGE env variable */
2473 #endif /* __APPLE__ */
2474 ppd
[1024], /* PPD env variable */
2476 /* PRINTER env variable */
2478 /* RIP_MAX_CACHE env variable */
2479 static char *options
= NULL
;/* Full list of options */
2480 static int optlength
= 0; /* Length of option buffer */
2483 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] start_job: file = %d/%d",
2484 job
->id
, job
->current_file
, job
->num_files
);
2486 if (job
->num_files
== 0)
2488 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Job %d] No files, canceling job!",
2491 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2496 * Figure out what filters are required to convert from
2497 * the source to the destination type...
2506 * Remote jobs and raw queues go directly to the printer without
2510 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2511 "[Job %d] Sending job to queue tagged as raw...", job
->id
);
2518 * Local jobs get filtered...
2521 filters
= mimeFilter(MimeDatabase
, job
->filetypes
[job
->current_file
],
2522 printer
->filetype
, &(job
->cost
));
2526 cupsdLogMessage(CUPSD_LOG_ERROR
,
2527 "[Job %d] Unable to convert file %d to printable format!",
2528 job
->current_file
, job
->id
);
2529 cupsdLogMessage(CUPSD_LOG_INFO
,
2530 "Hint: Do you have Ghostscript installed?");
2532 if (LogLevel
< CUPSD_LOG_DEBUG
)
2533 cupsdLogMessage(CUPSD_LOG_INFO
,
2534 "Hint: Try setting the LogLevel to \"debug\".");
2536 job
->current_file
++;
2538 if (job
->current_file
== job
->num_files
)
2539 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2545 * Remove NULL ("-") filters...
2548 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
2550 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
2551 if (!strcmp(filter
->filter
, "-"))
2552 cupsArrayRemove(filters
, filter
);
2554 if (cupsArrayCount(filters
) == 0)
2556 cupsArrayDelete(filters
);
2561 * If this printer has any pre-filters, insert the required pre-filter
2562 * in the filters array...
2565 if (printer
->prefiltertype
&& filters
)
2567 prefilters
= cupsArrayNew(NULL
, NULL
);
2569 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
2571 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
2573 if ((prefilter
= mimeFilterLookup(MimeDatabase
, filter
->src
,
2574 printer
->prefiltertype
)))
2576 cupsArrayAdd(prefilters
, prefilter
);
2577 job
->cost
+= prefilter
->cost
;
2580 cupsArrayAdd(prefilters
, filter
);
2583 cupsArrayDelete(filters
);
2584 filters
= prefilters
;
2589 * Set a minimum cost of 100 for all jobs so that FilterLimit
2590 * works with raw queues and other low-cost paths.
2593 if (job
->cost
< 100)
2597 * See if the filter cost is too high...
2600 if ((FilterLevel
+ job
->cost
) > FilterLimit
&& FilterLevel
> 0 &&
2604 * Don't print this job quite yet...
2607 cupsArrayDelete(filters
);
2609 cupsdLogMessage(CUPSD_LOG_INFO
,
2610 "[Job %d] Holding because filter limit has been reached.",
2612 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2613 "[Job %d] start_job: file=%d, cost=%d, level=%d, limit=%d",
2614 job
->id
, job
->current_file
, job
->cost
, FilterLevel
,
2619 FilterLevel
+= job
->cost
;
2622 * Add decompression/raw filter as needed...
2625 if ((!printer
->raw
&& job
->compressions
[job
->current_file
]) ||
2626 (!filters
&& !printer
->remote
&&
2627 (job
->num_files
> 1 || !strncmp(printer
->device_uri
, "file:", 5))))
2630 * Add gziptoany filter to the front of the list...
2634 filters
= cupsArrayNew(NULL
, NULL
);
2636 if (!cupsArrayInsert(filters
, &gziptoany_filter
))
2638 cupsdLogMessage(CUPSD_LOG_ERROR
,
2639 "[Job %d] Unable to add decompression filter - %s",
2640 job
->id
, strerror(errno
));
2642 cupsArrayDelete(filters
);
2644 job
->current_file
++;
2646 if (job
->current_file
== job
->num_files
)
2647 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2654 * Add port monitor, if any...
2657 if (printer
->port_monitor
)
2660 * Add port monitor to the end of the list...
2664 filters
= cupsArrayNew(NULL
, NULL
);
2666 if (!cupsArrayAdd(filters
, &port_monitor
))
2668 cupsdLogMessage(CUPSD_LOG_ERROR
,
2669 "[Job %d] Unable to add port monitor - %s",
2670 job
->id
, strerror(errno
));
2672 cupsArrayDelete(filters
);
2674 job
->current_file
++;
2676 if (job
->current_file
== job
->num_files
)
2677 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2682 snprintf(port_monitor
.filter
, sizeof(port_monitor
.filter
),
2683 "%s/monitor/%s", ServerBin
, printer
->port_monitor
);
2687 * Update the printer and job state to "processing"...
2690 job
->state
->values
[0].integer
= IPP_JOB_PROCESSING
;
2691 job
->state_value
= IPP_JOB_PROCESSING
;
2694 job
->printer
= printer
;
2697 cupsdSetPrinterState(printer
, IPP_PRINTER_PROCESSING
, 0);
2699 if (job
->current_file
== 0)
2702 * Set the processing time...
2705 set_time(job
, "time-at-processing");
2708 * Create the backchannel pipes and make them non-blocking...
2711 cupsdOpenPipe(job
->back_pipes
);
2713 fcntl(job
->back_pipes
[0], F_SETFL
,
2714 fcntl(job
->back_pipes
[0], F_GETFL
) | O_NONBLOCK
);
2716 fcntl(job
->back_pipes
[1], F_SETFL
,
2717 fcntl(job
->back_pipes
[1], F_GETFL
) | O_NONBLOCK
);
2720 * Create the side-channel pipes and make them non-blocking...
2723 socketpair(AF_LOCAL
, SOCK_STREAM
, 0, job
->side_pipes
);
2725 fcntl(job
->side_pipes
[0], F_SETFL
,
2726 fcntl(job
->side_pipes
[0], F_GETFL
) | O_NONBLOCK
);
2728 fcntl(job
->side_pipes
[1], F_SETFL
,
2729 fcntl(job
->side_pipes
[1], F_GETFL
) | O_NONBLOCK
);
2733 * Determine if we are printing a banner page or not...
2736 if (job
->job_sheets
== NULL
)
2738 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] No job-sheets attribute.",
2740 if ((job
->job_sheets
=
2741 ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_ZERO
)) != NULL
)
2742 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2743 "[Job %d] ... but someone added one without setting "
2744 "job_sheets!", job
->id
);
2746 else if (job
->job_sheets
->num_values
== 1)
2747 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] job-sheets=%s", job
->id
,
2748 job
->job_sheets
->values
[0].string
.text
);
2750 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] job-sheets=%s,%s", job
->id
,
2751 job
->job_sheets
->values
[0].string
.text
,
2752 job
->job_sheets
->values
[1].string
.text
);
2754 if (printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
))
2756 else if (job
->job_sheets
== NULL
)
2758 else if (strcasecmp(job
->job_sheets
->values
[0].string
.text
, "none") != 0 &&
2759 job
->current_file
== 0)
2761 else if (job
->job_sheets
->num_values
> 1 &&
2762 strcasecmp(job
->job_sheets
->values
[1].string
.text
, "none") != 0 &&
2763 job
->current_file
== (job
->num_files
- 1))
2768 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] banner_page = %d", job
->id
,
2772 * Building the options string is harder than it needs to be, but
2773 * for the moment we need to pass strings for command-line args and
2774 * not IPP attribute pointers... :)
2776 * First allocate/reallocate the option buffer as needed...
2779 i
= ipp_length(job
->attrs
);
2786 optptr
= realloc(options
, i
);
2790 cupsdLogMessage(CUPSD_LOG_CRIT
,
2791 "[Job %d] Unable to allocate %d bytes for option buffer!",
2794 cupsArrayDelete(filters
);
2796 FilterLevel
-= job
->cost
;
2798 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2807 * Now loop through the attributes and convert them to the textual
2808 * representation used by the filters...
2814 snprintf(title
, sizeof(title
), "%s-%d", printer
->name
, job
->id
);
2815 strcpy(copies
, "1");
2817 for (attr
= job
->attrs
->attrs
; attr
!= NULL
; attr
= attr
->next
)
2819 if (!strcmp(attr
->name
, "copies") &&
2820 attr
->value_tag
== IPP_TAG_INTEGER
)
2823 * Don't use the # copies attribute if we are printing the job sheets...
2827 sprintf(copies
, "%d", attr
->values
[0].integer
);
2829 else if (!strcmp(attr
->name
, "job-name") &&
2830 (attr
->value_tag
== IPP_TAG_NAME
||
2831 attr
->value_tag
== IPP_TAG_NAMELANG
))
2832 strlcpy(title
, attr
->values
[0].string
.text
, sizeof(title
));
2833 else if (attr
->group_tag
== IPP_TAG_JOB
)
2836 * Filter out other unwanted attributes...
2839 if (attr
->value_tag
== IPP_TAG_MIMETYPE
||
2840 attr
->value_tag
== IPP_TAG_NAMELANG
||
2841 attr
->value_tag
== IPP_TAG_TEXTLANG
||
2842 (attr
->value_tag
== IPP_TAG_URI
&& strcmp(attr
->name
, "job-uuid")) ||
2843 attr
->value_tag
== IPP_TAG_URISCHEME
||
2844 attr
->value_tag
== IPP_TAG_BEGIN_COLLECTION
) /* Not yet supported */
2847 if (!strncmp(attr
->name
, "time-", 5))
2850 if (!strncmp(attr
->name
, "job-", 4) && strcmp(attr
->name
, "job-uuid") &&
2851 !(printer
->type
& CUPS_PRINTER_REMOTE
))
2854 if (!strncmp(attr
->name
, "job-", 4) &&
2855 strcmp(attr
->name
, "job-uuid") &&
2856 strcmp(attr
->name
, "job-billing") &&
2857 strcmp(attr
->name
, "job-sheets") &&
2858 strcmp(attr
->name
, "job-hold-until") &&
2859 strcmp(attr
->name
, "job-priority"))
2862 if ((!strcmp(attr
->name
, "page-label") ||
2863 !strcmp(attr
->name
, "page-border") ||
2864 !strncmp(attr
->name
, "number-up", 9) ||
2865 !strcmp(attr
->name
, "page-ranges") ||
2866 !strcmp(attr
->name
, "page-set") ||
2867 !strcasecmp(attr
->name
, "AP_FIRSTPAGE_InputSlot") ||
2868 !strcasecmp(attr
->name
, "AP_FIRSTPAGE_ManualFeed") ||
2869 !strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
2870 "PMTotalSidesImaged..n.") ||
2871 !strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
2872 "PMTotalBeginPages..n.")) &&
2877 * Otherwise add them to the list...
2880 if (optptr
> options
)
2881 strlcat(optptr
, " ", optlength
- (optptr
- options
));
2883 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
2885 strlcat(optptr
, attr
->name
, optlength
- (optptr
- options
));
2886 strlcat(optptr
, "=", optlength
- (optptr
- options
));
2889 for (i
= 0; i
< attr
->num_values
; i
++)
2892 strlcat(optptr
, ",", optlength
- (optptr
- options
));
2894 optptr
+= strlen(optptr
);
2896 switch (attr
->value_tag
)
2898 case IPP_TAG_INTEGER
:
2900 snprintf(optptr
, optlength
- (optptr
- options
),
2901 "%d", attr
->values
[i
].integer
);
2904 case IPP_TAG_BOOLEAN
:
2905 if (!attr
->values
[i
].boolean
)
2906 strlcat(optptr
, "no", optlength
- (optptr
- options
));
2908 case IPP_TAG_NOVALUE
:
2909 strlcat(optptr
, attr
->name
,
2910 optlength
- (optptr
- options
));
2913 case IPP_TAG_RANGE
:
2914 if (attr
->values
[i
].range
.lower
== attr
->values
[i
].range
.upper
)
2915 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2916 "%d", attr
->values
[i
].range
.lower
);
2918 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2919 "%d-%d", attr
->values
[i
].range
.lower
,
2920 attr
->values
[i
].range
.upper
);
2923 case IPP_TAG_RESOLUTION
:
2924 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2925 "%dx%d%s", attr
->values
[i
].resolution
.xres
,
2926 attr
->values
[i
].resolution
.yres
,
2927 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
2931 case IPP_TAG_STRING
:
2934 case IPP_TAG_KEYWORD
:
2935 case IPP_TAG_CHARSET
:
2936 case IPP_TAG_LANGUAGE
:
2938 for (valptr
= attr
->values
[i
].string
.text
; *valptr
;)
2940 if (strchr(" \t\n\\\'\"", *valptr
))
2942 *optptr
++ = *valptr
++;
2949 break; /* anti-compiler-warning-code */
2953 optptr
+= strlen(optptr
);
2958 * Build the command-line arguments for the filters. Each filter
2959 * has 6 or 7 arguments:
2963 * argv[2] = username
2965 * argv[4] = # copies
2967 * argv[6] = filename (optional; normally stdin)
2969 * This allows legacy printer drivers that use the old System V
2970 * printing interface to be used by CUPS.
2972 * For remote jobs, we send all of the files in the argument list.
2975 if (printer
->remote
&& job
->num_files
> 1)
2976 argv
= calloc(7 + job
->num_files
, sizeof(char *));
2978 argv
= calloc(8, sizeof(char *));
2982 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to allocate argument array!");
2983 cupsArrayDelete(filters
);
2985 FilterLevel
-= job
->cost
;
2987 cupsdStopPrinter(printer
, 0);
2991 sprintf(jobid
, "%d", job
->id
);
2993 argv
[0] = printer
->name
;
2995 argv
[2] = job
->username
;
3000 if (printer
->remote
&& job
->num_files
> 1)
3002 for (i
= 0; i
< job
->num_files
; i
++)
3004 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
3006 argv
[6 + i
] = strdup(filename
);
3011 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
3012 job
->id
, job
->current_file
+ 1);
3016 for (i
= 0; argv
[i
]; i
++)
3017 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3018 "[Job %d] argv[%d]=\"%s\"", job
->id
, i
, argv
[i
]);
3021 * Create environment variable strings for the filters...
3024 attr
= ippFindAttribute(job
->attrs
, "attributes-natural-language",
3028 strcpy(apple_language
, "APPLE_LANGUAGE=");
3029 _cupsAppleLanguage(attr
->values
[0].string
.text
,
3030 apple_language
+ 15, sizeof(apple_language
) - 15);
3031 #endif /* __APPLE__ */
3033 switch (strlen(attr
->values
[0].string
.text
))
3037 * This is an unknown or badly formatted language code; use
3038 * the POSIX locale...
3041 strcpy(lang
, "LANG=C");
3046 * Just the language code (ll)...
3049 snprintf(lang
, sizeof(lang
), "LANG=%s.UTF8",
3050 attr
->values
[0].string
.text
);
3055 * Language and country code (ll-cc)...
3058 snprintf(lang
, sizeof(lang
), "LANG=%c%c_%c%c.UTF8",
3059 attr
->values
[0].string
.text
[0],
3060 attr
->values
[0].string
.text
[1],
3061 toupper(attr
->values
[0].string
.text
[3] & 255),
3062 toupper(attr
->values
[0].string
.text
[4] & 255));
3066 attr
= ippFindAttribute(job
->attrs
, "document-format",
3069 (optptr
= strstr(attr
->values
[0].string
.text
, "charset=")) != NULL
)
3070 snprintf(charset
, sizeof(charset
), "CHARSET=%s", optptr
+ 8);
3073 attr
= ippFindAttribute(job
->attrs
, "attributes-charset",
3075 snprintf(charset
, sizeof(charset
), "CHARSET=%s",
3076 attr
->values
[0].string
.text
);
3079 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s/%s",
3080 job
->filetypes
[job
->current_file
]->super
,
3081 job
->filetypes
[job
->current_file
]->type
);
3082 snprintf(device_uri
, sizeof(device_uri
), "DEVICE_URI=%s",
3083 printer
->device_uri
);
3084 cupsdSanitizeURI(printer
->device_uri
, sani_uri
, sizeof(sani_uri
));
3085 snprintf(ppd
, sizeof(ppd
), "PPD=%s/ppd/%s.ppd", ServerRoot
, printer
->name
);
3086 snprintf(printer_name
, sizeof(printer_name
), "PRINTER=%s", printer
->name
);
3087 snprintf(rip_max_cache
, sizeof(rip_max_cache
), "RIP_MAX_CACHE=%s", RIPCache
);
3089 envc
= cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
3091 envp
[envc
++] = charset
;
3092 envp
[envc
++] = lang
;
3094 envp
[envc
++] = apple_language
;
3095 #endif /* __APPLE__ */
3096 envp
[envc
++] = ppd
;
3097 envp
[envc
++] = rip_max_cache
;
3098 envp
[envc
++] = content_type
;
3099 envp
[envc
++] = device_uri
;
3100 envp
[envc
++] = printer_name
;
3102 if (!printer
->remote
&& !printer
->raw
&&
3103 (filter
= (mime_filter_t
*)cupsArrayLast(filters
)) != NULL
&&
3106 snprintf(final_content_type
, sizeof(final_content_type
),
3107 "FINAL_CONTENT_TYPE=%s/%s",
3108 filter
->dst
->super
, filter
->dst
->type
);
3109 envp
[envc
++] = final_content_type
;
3112 if (Classification
&& !banner_page
)
3114 if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
3115 IPP_TAG_NAME
)) == NULL
)
3116 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3118 else if (attr
->num_values
> 1 &&
3119 strcmp(attr
->values
[1].string
.text
, "none") != 0)
3120 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3121 attr
->values
[1].string
.text
);
3123 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3124 attr
->values
[0].string
.text
);
3126 envp
[envc
++] = classification
;
3129 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
3131 snprintf(class_name
, sizeof(class_name
), "CLASS=%s", job
->dest
);
3132 envp
[envc
++] = class_name
;
3135 if (job
->auth_username
)
3136 envp
[envc
++] = job
->auth_username
;
3137 if (job
->auth_domain
)
3138 envp
[envc
++] = job
->auth_domain
;
3139 if (job
->auth_password
)
3140 envp
[envc
++] = job
->auth_password
;
3144 envp
[envc
++] = job
->ccname
;
3145 #endif /* HAVE_GSSAPI */
3149 for (i
= 0; i
< envc
; i
++)
3150 if (!strncmp(envp
[i
], "CUPSD_AUTH_", 5))
3151 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] envp[%d]=\"CUPSD_AUTH_%c****\"",
3152 job
->id
, i
, envp
[i
][5]);
3153 else if (strncmp(envp
[i
], "DEVICE_URI=", 11))
3154 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] envp[%d]=\"%s\"",
3155 job
->id
, i
, envp
[i
]);
3157 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] envp[%d]=\"DEVICE_URI=%s\"",
3158 job
->id
, i
, sani_uri
);
3160 if (printer
->remote
)
3161 job
->current_file
= job
->num_files
;
3163 job
->current_file
++;
3166 * Now create processes for all of the filters...
3169 filterfds
[0][0] = -1;
3170 filterfds
[0][1] = -1;
3171 filterfds
[1][0] = -1;
3172 filterfds
[1][1] = -1;
3174 if (!job
->status_buffer
)
3176 if (cupsdOpenPipe(job
->status_pipes
))
3178 cupsdLogMessage(CUPSD_LOG_ERROR
,
3179 "[Job %d] Unable to create job status pipes - %s.",
3180 job
->id
, strerror(errno
));
3181 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3182 "Unable to create status pipes - %s.", strerror(errno
));
3184 cupsdAddPrinterHistory(printer
);
3186 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3187 "Job canceled because the server could not create the job "
3193 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3194 "[Job %d] start_job: status_pipes = [ %d %d ]",
3195 job
->id
, job
->status_pipes
[0], job
->status_pipes
[1]);
3197 job
->status_buffer
= cupsdStatBufNew(job
->status_pipes
[0], "[Job %d]",
3199 job
->status_level
= CUPSD_LOG_INFO
;
3203 memset(job
->filters
, 0, sizeof(job
->filters
));
3206 job
->profile
= cupsdCreateProfile(job
->id
);
3208 for (i
= 0, slot
= 0, filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
3210 i
++, filter
= (mime_filter_t
*)cupsArrayNext(filters
))
3212 if (filter
->filter
[0] != '/')
3213 snprintf(command
, sizeof(command
), "%s/filter/%s", ServerBin
,
3216 strlcpy(command
, filter
->filter
, sizeof(command
));
3218 if (i
< (cupsArrayCount(filters
) - 1))
3220 if (cupsdOpenPipe(filterfds
[slot
]))
3222 cupsdLogMessage(CUPSD_LOG_ERROR
,
3223 "[Job %d] Unable to create job filter pipes - %s.",
3224 job
->id
, strerror(errno
));
3225 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3226 "Unable to create filter pipes - %s.", strerror(errno
));
3227 cupsdAddPrinterHistory(printer
);
3229 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3230 "Job canceled because the server could not create the "
3238 if (job
->current_file
== 1)
3240 if (strncmp(printer
->device_uri
, "file:", 5) != 0)
3242 if (cupsdOpenPipe(job
->print_pipes
))
3244 cupsdLogMessage(CUPSD_LOG_ERROR
,
3245 "[Job %d] Unable to create job backend pipes - %s.",
3246 job
->id
, strerror(errno
));
3247 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3248 "Unable to create backend pipes - %s.", strerror(errno
));
3249 cupsdAddPrinterHistory(printer
);
3251 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3252 "Job canceled because the server could not create "
3253 "the backend pipes.");
3260 job
->print_pipes
[0] = -1;
3261 if (!strcmp(printer
->device_uri
, "file:/dev/null") ||
3262 !strcmp(printer
->device_uri
, "file:///dev/null"))
3263 job
->print_pipes
[1] = -1;
3266 if (!strncmp(printer
->device_uri
, "file:/dev/", 10))
3267 job
->print_pipes
[1] = open(printer
->device_uri
+ 5,
3269 else if (!strncmp(printer
->device_uri
, "file:///dev/", 12))
3270 job
->print_pipes
[1] = open(printer
->device_uri
+ 7,
3272 else if (!strncmp(printer
->device_uri
, "file:///", 8))
3273 job
->print_pipes
[1] = open(printer
->device_uri
+ 7,
3274 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
3276 job
->print_pipes
[1] = open(printer
->device_uri
+ 5,
3277 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
3279 if (job
->print_pipes
[1] < 0)
3281 cupsdLogMessage(CUPSD_LOG_ERROR
,
3282 "[Job %d] Unable to open output file \"%s\" - %s.",
3283 job
->id
, printer
->device_uri
, strerror(errno
));
3284 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3285 "Unable to open output file \"%s\" - %s.",
3286 printer
->device_uri
, strerror(errno
));
3288 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3289 "Job canceled because the server could not open the "
3295 fcntl(job
->print_pipes
[1], F_SETFD
,
3296 fcntl(job
->print_pipes
[1], F_GETFD
) | FD_CLOEXEC
);
3300 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3301 "[Job %d] start_job: print_pipes = [ %d %d ]",
3302 job
->id
, job
->print_pipes
[0], job
->print_pipes
[1]);
3305 filterfds
[slot
][0] = job
->print_pipes
[0];
3306 filterfds
[slot
][1] = job
->print_pipes
[1];
3309 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] start_job: filter=\"%s\"",
3311 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3312 "[Job %d] start_job: filterfds[%d]=[ %d %d ]",
3313 job
->id
, slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3315 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
3316 filterfds
[slot
][1], job
->status_pipes
[1],
3317 job
->back_pipes
[0], job
->side_pipes
[0], 0,
3318 job
->profile
, job
->filters
+ i
);
3320 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3321 "[Job %d] start_job: Closing filter pipes for slot %d "
3323 job
->id
, !slot
, filterfds
[!slot
][0], filterfds
[!slot
][1]);
3325 cupsdClosePipe(filterfds
[!slot
]);
3329 cupsdLogMessage(CUPSD_LOG_ERROR
,
3330 "[Job %d] Unable to start filter \"%s\" - %s.",
3331 job
->id
, filter
->filter
, strerror(errno
));
3332 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3333 "Unable to start filter \"%s\" - %s.",
3334 filter
->filter
, strerror(errno
));
3336 cupsdAddPrinterHistory(printer
);
3338 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3339 "Job canceled because the server could not execute a "
3345 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Started filter %s (PID %d)",
3346 job
->id
, command
, pid
);
3352 cupsArrayDelete(filters
);
3355 * Finally, pipe the final output into a backend process if needed...
3358 if (strncmp(printer
->device_uri
, "file:", 5) != 0)
3360 if (job
->current_file
== 1 || printer
->remote
)
3362 sscanf(printer
->device_uri
, "%254[^:]", method
);
3363 snprintf(command
, sizeof(command
), "%s/backend/%s", ServerBin
, method
);
3366 * See if the backend needs to run as root...
3371 else if (stat(command
, &backinfo
))
3374 backroot
= !(backinfo
.st_mode
& (S_IRWXG
| S_IRWXO
));
3378 filterfds
[slot
][0] = -1;
3379 filterfds
[slot
][1] = -1;
3381 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] start_job: backend=\"%s\"",
3383 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3384 "[Job %d] start_job: filterfds[%d] = [ %d %d ]", job
->id
,
3385 slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3387 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
3388 filterfds
[slot
][1], job
->status_pipes
[1],
3389 job
->back_pipes
[1], job
->side_pipes
[1],
3390 backroot
, job
->profile
, &(job
->backend
));
3394 cupsdLogMessage(CUPSD_LOG_ERROR
,
3395 "[Job %d] Unable to start backend \"%s\" - %s.",
3396 job
->id
, method
, strerror(errno
));
3397 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3398 "Unable to start backend \"%s\" - %s.", method
,
3401 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3402 "Job canceled because the server could not execute "
3409 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Started backend %s (PID %d)",
3410 job
->id
, command
, pid
);
3414 if (job
->current_file
== job
->num_files
)
3416 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3417 "[Job %d] start_job: Closing print pipes [ %d %d ]...",
3418 job
->id
, job
->print_pipes
[0], job
->print_pipes
[1]);
3420 cupsdClosePipe(job
->print_pipes
);
3422 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3423 "[Job %d] start_job: Closing back pipes [ %d %d ]...",
3424 job
->id
, job
->back_pipes
[0], job
->back_pipes
[1]);
3426 cupsdClosePipe(job
->back_pipes
);
3428 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3429 "[Job %d] start_job: Closing side pipes [ %d %d ]...",
3430 job
->id
, job
->side_pipes
[0], job
->side_pipes
[1]);
3432 cupsdClosePipe(job
->side_pipes
);
3434 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3435 "[Job %d] start_job: Closing status output pipe %d...",
3436 job
->id
, job
->status_pipes
[1]);
3438 close(job
->status_pipes
[1]);
3439 job
->status_pipes
[1] = -1;
3444 filterfds
[slot
][0] = -1;
3445 filterfds
[slot
][1] = -1;
3447 if (job
->current_file
== job
->num_files
)
3449 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3450 "[Job %d] start_job: Closing print pipes [ %d %d ]...",
3451 job
->id
, job
->print_pipes
[0], job
->print_pipes
[1]);
3453 cupsdClosePipe(job
->print_pipes
);
3455 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3456 "[Job %d] start_job: Closing status output pipe %d...",
3457 job
->id
, job
->status_pipes
[1]);
3459 close(job
->status_pipes
[1]);
3460 job
->status_pipes
[1] = -1;
3464 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3465 "[Job %d] start_job: Closing filter pipes for slot %d "
3467 job
->id
, slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3468 cupsdClosePipe(filterfds
[slot
]);
3470 if (printer
->remote
&& job
->num_files
> 1)
3472 for (i
= 0; i
< job
->num_files
; i
++)
3478 cupsdAddSelect(job
->status_buffer
->fd
, (cupsd_selfunc_t
)update_job
, NULL
,
3481 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
, "Job #%d started.",
3488 * If we get here, we need to abort the current job and close out all
3489 * files and pipes...
3494 for (slot
= 0; slot
< 2; slot
++)
3496 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3497 "[Job %d] start_job: Closing filter pipes for slot %d "
3499 job
->id
, slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3500 cupsdClosePipe(filterfds
[slot
]);
3503 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3504 "[Job %d] start_job: Closing status pipes [ %d %d ]...",
3505 job
->id
, job
->status_pipes
[0], job
->status_pipes
[1]);
3506 cupsdClosePipe(job
->status_pipes
);
3507 cupsdStatBufDelete(job
->status_buffer
);
3509 job
->status_buffer
= NULL
;
3511 cupsArrayDelete(filters
);
3513 if (printer
->remote
&& job
->num_files
> 1)
3515 for (i
= 0; i
< job
->num_files
; i
++)
3521 cupsdStopJob(job
, 0);
3526 * 'unload_job()' - Unload a job from memory.
3530 unload_job(cupsd_job_t
*job
) /* I - Job */
3535 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Unloading...", job
->id
);
3537 ippDelete(job
->attrs
);
3542 job
->job_sheets
= NULL
;
3543 job
->printer_message
= NULL
;
3544 job
->printer_reasons
= NULL
;
3549 * 'update_job()' - Read a status update from a job's filters.
3553 update_job(cupsd_job_t
*job
) /* I - Job to check */
3555 int i
; /* Looping var */
3556 int copies
; /* Number of copies printed */
3557 char message
[1024], /* Message text */
3558 *ptr
; /* Pointer update... */
3559 int loglevel
, /* Log level for message */
3560 event
= 0; /* Events? */
3563 while ((ptr
= cupsdStatBufUpdate(job
->status_buffer
, &loglevel
,
3564 message
, sizeof(message
))) != NULL
)
3567 * Process page and printer state messages as needed...
3570 if (loglevel
== CUPSD_LOG_PAGE
)
3573 * Page message; send the message to the page_log file and update the
3574 * job sheet count...
3579 if (!strncasecmp(message
, "total ", 6))
3582 * Got a total count of pages from a backend or filter...
3585 copies
= atoi(message
+ 6);
3586 copies
-= job
->sheets
->values
[0].integer
; /* Just track the delta */
3588 else if (!sscanf(message
, "%*d%d", &copies
))
3591 job
->sheets
->values
[0].integer
+= copies
;
3593 if (job
->printer
->page_limit
)
3595 cupsd_quota_t
*q
= cupsdUpdateQuota(job
->printer
, job
->username
,
3599 if (AppleQuotas
&& q
->page_count
== -3)
3602 * Quota limit exceeded, cancel job in progress immediately...
3605 cupsdLogMessage(CUPSD_LOG_INFO
,
3606 "[Job %d] Canceled because pages exceed user %s "
3607 "quota limit on printer %s (%s).",
3608 job
->id
, job
->username
, job
->printer
->name
,
3609 job
->printer
->info
);
3611 cupsdCancelJob(job
, 1, IPP_JOB_CANCELED
);
3616 #endif /* __APPLE__ */
3620 cupsdLogPage(job
, message
);
3623 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
3624 "Printed %d page(s).", job
->sheets
->values
[0].integer
);
3626 else if (loglevel
== CUPSD_LOG_STATE
)
3628 if (!strcmp(message
, "paused"))
3630 cupsdStopPrinter(job
->printer
, 1);
3635 cupsdSetPrinterReasons(job
->printer
, message
);
3636 cupsdAddPrinterHistory(job
->printer
);
3637 event
|= CUPSD_EVENT_PRINTER_STATE
;
3640 update_job_attrs(job
);
3642 else if (loglevel
== CUPSD_LOG_ATTR
)
3645 * Set attribute(s)...
3648 int num_attrs
; /* Number of attributes */
3649 cups_option_t
*attrs
; /* Attributes */
3650 const char *attr
; /* Attribute */
3653 num_attrs
= cupsParseOptions(message
, 0, &attrs
);
3655 if ((attr
= cupsGetOption("auth-info-required", num_attrs
,
3658 cupsdSetAuthInfoRequired(job
->printer
, attr
, NULL
);
3659 cupsdSetPrinterAttrs(job
->printer
);
3660 cupsdSaveAllPrinters();
3663 if ((attr
= cupsGetOption("printer-alert", num_attrs
, attrs
)) != NULL
)
3665 cupsdSetString(&job
->printer
->alert
, attr
);
3666 event
|= CUPSD_EVENT_PRINTER_STATE
;
3669 if ((attr
= cupsGetOption("printer-alert-description", num_attrs
,
3672 cupsdSetString(&job
->printer
->alert_description
, attr
);
3673 event
|= CUPSD_EVENT_PRINTER_STATE
;
3676 cupsFreeOptions(num_attrs
, attrs
);
3679 else if (!strncmp(message
, "recoverable:", 12))
3681 cupsdSetPrinterReasons(job
->printer
,
3682 "+com.apple.print.recoverable-warning");
3685 while (isspace(*ptr
& 255))
3688 cupsdSetString(&job
->printer
->recoverable
, ptr
);
3689 cupsdAddPrinterHistory(job
->printer
);
3690 event
|= CUPSD_EVENT_PRINTER_STATE
;
3692 else if (!strncmp(message
, "recovered:", 10))
3694 cupsdSetPrinterReasons(job
->printer
,
3695 "-com.apple.print.recoverable-warning");
3698 while (isspace(*ptr
& 255))
3701 cupsdSetString(&job
->printer
->recoverable
, ptr
);
3702 cupsdAddPrinterHistory(job
->printer
);
3703 event
|= CUPSD_EVENT_PRINTER_STATE
;
3705 #endif /* __APPLE__ */
3706 else if (loglevel
<= job
->status_level
)
3709 * Some message to show in the printer-state-message attribute...
3712 if (loglevel
!= CUPSD_LOG_NOTICE
)
3713 job
->status_level
= loglevel
;
3715 strlcpy(job
->printer
->state_message
, message
,
3716 sizeof(job
->printer
->state_message
));
3717 cupsdAddPrinterHistory(job
->printer
);
3718 event
|= CUPSD_EVENT_PRINTER_STATE
;
3720 update_job_attrs(job
);
3723 if (!strchr(job
->status_buffer
->buffer
, '\n'))
3727 if (event
& CUPSD_EVENT_PRINTER_STATE
)
3728 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, job
->printer
, NULL
,
3729 (job
->printer
->type
& CUPS_PRINTER_CLASS
) ?
3730 "Class \"%s\" state changed." :
3731 "Printer \"%s\" state changed.",
3732 job
->printer
->name
);
3734 if (ptr
== NULL
&& !job
->status_buffer
->bufused
)
3737 * See if all of the filters and the backend have returned their
3741 for (i
= 0; job
->filters
[i
] < 0; i
++);
3743 if (job
->filters
[i
])
3746 if (job
->current_file
>= job
->num_files
&& job
->backend
> 0)
3750 * Handle the end of job stuff...
3753 cupsdFinishJob(job
);
3759 * 'update_job_attrs()' - Update the job-printer-* attributes.
3763 update_job_attrs(cupsd_job_t
*job
) /* I - Job to update */
3765 int i
; /* Looping var */
3766 int num_reasons
; /* Actual number of reasons */
3767 const char * const *reasons
; /* Reasons */
3768 static const char *none
= "none", /* "none" */
3774 * Get/create the job-printer-state-* attributes...
3777 if (!job
->printer_message
)
3779 if ((job
->printer_message
= ippFindAttribute(job
->attrs
,
3780 "job-printer-state-message",
3781 IPP_TAG_TEXT
)) == NULL
)
3782 job
->printer_message
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_TEXT
,
3783 "job-printer-state-message",
3787 if (!job
->printer_reasons
)
3788 job
->printer_reasons
= ippFindAttribute(job
->attrs
,
3789 "job-printer-state-reasons",
3793 * If the job isn't printing, return now...
3800 * Otherwise copy the printer-state-message value...
3803 if (job
->printer
->state_message
[0])
3804 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
),
3805 job
->printer
->state_message
);
3808 * ... and the printer-state-reasons value...
3811 if (job
->printer
->num_reasons
== 0)
3814 reasons
= job
->printer
->state
== IPP_PRINTER_STOPPED
? &paused
: &none
;
3818 num_reasons
= job
->printer
->num_reasons
;
3819 reasons
= (const char * const *)job
->printer
->reasons
;
3822 if (!job
->printer_reasons
|| job
->printer_reasons
->num_values
!= num_reasons
)
3824 ippDeleteAttribute(job
->attrs
, job
->printer_reasons
);
3826 job
->printer_reasons
= ippAddStrings(job
->attrs
,
3827 IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
3828 "job-printer-state-reasons",
3829 num_reasons
, NULL
, NULL
);
3832 for (i
= 0; i
< num_reasons
; i
++)
3833 cupsdSetString(&(job
->printer_reasons
->values
[i
].string
.text
), reasons
[i
]);
3838 * End of "$Id: job.c 7005 2007-10-01 23:45:48Z mike $".