2 * "$Id: job.c 6755 2007-08-01 19:02:47Z mike $"
4 * Job management routines for the Common UNIX Printing System (CUPS).
6 * Copyright 2007 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 job
= calloc(sizeof(cupsd_job_t
), 1);
118 job
->id
= NextJobId
++;
119 job
->priority
= priority
;
120 job
->back_pipes
[0] = -1;
121 job
->back_pipes
[1] = -1;
122 job
->print_pipes
[0] = -1;
123 job
->print_pipes
[1] = -1;
124 job
->side_pipes
[0] = -1;
125 job
->side_pipes
[1] = -1;
126 job
->status_pipes
[0] = -1;
127 job
->status_pipes
[1] = -1;
129 cupsdSetString(&job
->dest
, dest
);
132 * Add the new job to the "all jobs" and "active jobs" lists...
135 cupsArrayAdd(Jobs
, job
);
136 cupsArrayAdd(ActiveJobs
, job
);
143 * 'cupsdCancelJob()' - Cancel the specified print job.
147 cupsdCancelJob(cupsd_job_t
*job
, /* I - Job to cancel */
148 int purge
, /* I - Purge jobs? */
149 ipp_jstate_t newstate
) /* I - New job state */
151 int i
; /* Looping var */
152 char filename
[1024]; /* Job filename */
153 cupsd_printer_t
*printer
; /* Printer used by job */
156 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdCancelJob: id = %d", job
->id
);
159 * Stop any processes that are working on the current job...
162 printer
= job
->printer
;
164 if (job
->state_value
== IPP_JOB_PROCESSING
)
165 cupsdStopJob(job
, 0);
170 job
->state
->values
[0].integer
= newstate
;
172 job
->state_value
= newstate
;
174 set_time(job
, "time-at-completed");
177 * Send any pending notifications and then expire them...
185 case IPP_JOB_CANCELED
:
186 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, printer
, job
,
187 purge
? "Job purged." : "Job canceled.");
190 case IPP_JOB_ABORTED
:
191 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, printer
, job
,
192 "Job aborted; please consult the error_log file "
196 case IPP_JOB_COMPLETED
:
198 * Clear the printer's printer-state-message and move on...
201 printer
->state_message
[0] = '\0';
203 cupsdSetPrinterState(printer
, IPP_PRINTER_IDLE
, 0);
205 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, printer
, job
,
210 cupsdExpireSubscriptions(NULL
, job
);
213 * Remove the job from the active list...
216 cupsArrayRemove(ActiveJobs
, job
);
219 * Remove any authentication data...
222 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
223 if (cupsdRemoveFile(filename
) && errno
!= ENOENT
)
224 cupsdLogMessage(CUPSD_LOG_ERROR
,
225 "Unable to remove authentication cache: %s",
228 cupsdClearString(&job
->auth_username
);
229 cupsdClearString(&job
->auth_domain
);
230 cupsdClearString(&job
->auth_password
);
236 * Destroy the credential cache and clear the KRB5CCNAME env var string.
239 krb5_cc_destroy(KerberosContext
, job
->ccache
);
241 cupsdClearString(&job
->ccname
);
243 #endif /* HAVE_GSSAPI */
246 * Remove the print file for good if we aren't preserving jobs or
250 job
->current_file
= 0;
252 if (!JobHistory
|| !JobFiles
|| purge
|| (job
->dtype
& CUPS_PRINTER_REMOTE
))
254 for (i
= 1; i
<= job
->num_files
; i
++)
256 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
261 if (job
->num_files
> 0)
263 free(job
->filetypes
);
264 free(job
->compressions
);
267 job
->filetypes
= NULL
;
268 job
->compressions
= NULL
;
272 if (JobHistory
&& !purge
&& !(job
->dtype
& CUPS_PRINTER_REMOTE
))
275 * Save job state info...
283 * Remove the job info file...
286 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
,
291 * Remove the job from the "all jobs" list...
294 cupsArrayRemove(Jobs
, job
);
297 * Free all memory used...
306 * 'cupsdCancelJobs()' - Cancel all jobs for the given destination/user...
310 cupsdCancelJobs(const char *dest
, /* I - Destination to cancel */
311 const char *username
, /* I - Username or NULL */
312 int purge
) /* I - Purge jobs? */
314 cupsd_job_t
*job
; /* Current job */
317 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
319 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
320 if ((dest
== NULL
|| !strcmp(job
->dest
, dest
)) &&
321 (username
== NULL
|| !strcmp(job
->username
, username
)))
324 * Cancel all jobs matching this destination/user...
327 cupsdCancelJob(job
, purge
, IPP_JOB_CANCELED
);
335 * 'cupsdCheckJobs()' - Check the pending jobs and start any if the destination
342 cupsd_job_t
*job
; /* Current job in queue */
343 cupsd_printer_t
*printer
, /* Printer destination */
344 *pclass
; /* Printer class destination */
347 DEBUG_puts("cupsdCheckJobs()");
349 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
350 "cupsdCheckJobs: %d active jobs, sleeping=%d, reload=%d",
351 cupsArrayCount(ActiveJobs
), Sleeping
, NeedReload
);
353 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
355 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
358 * Start held jobs if they are ready...
361 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
362 "cupsdCheckJobs: Job %d: state_value=%d, loaded=%s",
363 job
->id
, job
->state_value
, job
->attrs
? "yes" : "no");
365 if (job
->state_value
== IPP_JOB_HELD
&&
367 job
->hold_until
< time(NULL
))
369 if (job
->pending_timeout
)
370 cupsdTimeoutJob(job
); /* Add trailing banner as needed */
372 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
373 job
->state_value
= IPP_JOB_PENDING
;
377 * Start pending jobs if the destination is available...
380 if (job
->state_value
== IPP_JOB_PENDING
&& !NeedReload
&& !Sleeping
)
382 printer
= cupsdFindDest(job
->dest
);
386 (printer
->type
& (CUPS_PRINTER_IMPLICIT
| CUPS_PRINTER_CLASS
)))
389 * If the class is remote, just pass it to the remote server...
394 if (pclass
->state
== IPP_PRINTER_STOPPED
)
396 else if (pclass
->type
& CUPS_PRINTER_REMOTE
)
399 printer
= cupsdFindAvailablePrinter(printer
->name
);
402 if (!printer
&& !pclass
)
405 * Whoa, the printer and/or class for this destination went away;
409 cupsdLogMessage(CUPSD_LOG_WARN
,
410 "[Job %d] Printer/class %s has gone away; canceling job!",
413 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
414 "Job canceled because the destination printer/class has "
417 cupsdCancelJob(job
, 1, IPP_JOB_ABORTED
);
422 * See if the printer is available or remote and not printing a job;
423 * if so, start the job...
429 * Add/update a job-actual-printer-uri attribute for this job
430 * so that we know which printer actually printed the job...
433 ipp_attribute_t
*attr
; /* job-actual-printer-uri attribute */
436 if ((attr
= ippFindAttribute(job
->attrs
, "job-actual-printer-uri",
437 IPP_TAG_URI
)) != NULL
)
438 cupsdSetString(&attr
->values
[0].string
.text
, printer
->uri
);
440 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
,
441 "job-actual-printer-uri", NULL
, printer
->uri
);
444 if ((!(printer
->type
& CUPS_PRINTER_DISCOVERED
) && /* Printer is local */
445 printer
->state
== IPP_PRINTER_IDLE
) || /* and idle */
446 ((printer
->type
& CUPS_PRINTER_DISCOVERED
) && /* Printer is remote */
447 !printer
->job
)) /* and not printing */
450 * Clear any message and reasons for the queue...
453 printer
->state_message
[0] = '\0';
455 cupsdSetPrinterReasons(printer
, "none");
461 start_job(job
, printer
);
470 * 'cupsdCleanJobs()' - Clean out old jobs.
476 cupsd_job_t
*job
; /* Current job */
482 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
483 job
&& cupsArrayCount(Jobs
) >= MaxJobs
;
484 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
485 if (job
->state_value
>= IPP_JOB_CANCELED
)
486 cupsdCancelJob(job
, 1, IPP_JOB_CANCELED
);
491 * 'cupsdFinishJob()' - Finish a job.
495 cupsdFinishJob(cupsd_job_t
*job
) /* I - Job */
497 cupsd_printer_t
*printer
; /* Current printer */
498 ipp_attribute_t
*attr
; /* job-hold-until attribute */
501 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] File %d is complete.",
502 job
->id
, job
->current_file
- 1);
504 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
505 "[Job %d] cupsdFinishJob: job->status is %d",
506 job
->id
, job
->status
);
508 if (job
->status_buffer
&&
509 (job
->status
< 0 || job
->current_file
>= job
->num_files
))
512 * Close the pipe and clear the input bit.
515 cupsdRemoveSelect(job
->status_buffer
->fd
);
517 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
518 "[Job %d] cupsdFinishJob: Closing status pipes [ %d %d ]...",
519 job
->id
, job
->status_pipes
[0], job
->status_pipes
[1]);
521 cupsdClosePipe(job
->status_pipes
);
522 cupsdStatBufDelete(job
->status_buffer
);
524 job
->status_buffer
= NULL
;
527 printer
= job
->printer
;
529 update_job_attrs(job
);
534 * Backend had errors; stop it...
537 int exit_code
; /* Exit code from backend */
541 * Convert the status to an exit code. Due to the way the W* macros are
542 * implemented on MacOS X (bug?), we have to store the exit status in a
543 * variable first and then convert...
546 exit_code
= -job
->status
;
547 if (WIFEXITED(exit_code
))
548 exit_code
= WEXITSTATUS(exit_code
);
550 exit_code
= job
->status
;
552 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Backend returned status %d (%s)",
554 exit_code
== CUPS_BACKEND_FAILED
? "failed" :
555 exit_code
== CUPS_BACKEND_AUTH_REQUIRED
?
556 "authentication required" :
557 exit_code
== CUPS_BACKEND_HOLD
? "hold job" :
558 exit_code
== CUPS_BACKEND_STOP
? "stop printer" :
559 exit_code
== CUPS_BACKEND_CANCEL
? "cancel job" :
560 exit_code
< 0 ? "crashed" : "unknown");
563 * Do what needs to be done...
569 case CUPS_BACKEND_FAILED
:
571 * Backend failure, use the error-policy to determine how to
575 cupsdStopJob(job
, 0);
577 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
580 * Mark the job as pending again - we'll retry on another
584 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
585 job
->state_value
= IPP_JOB_PENDING
;
591 * If the job was queued to a class, try requeuing it... For
592 * faxes and retry-job queues, hold the current job for 5 minutes.
595 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
597 else if ((printer
->type
& CUPS_PRINTER_FAX
) ||
598 !strcmp(printer
->error_policy
, "retry-job"))
601 * See how many times we've tried to send the job; if more than
602 * the limit, cancel the job.
607 if (job
->tries
>= JobRetryLimit
)
613 cupsdLogMessage(CUPSD_LOG_ERROR
,
614 "[Job %d] Canceling job since it could not be "
615 "sent after %d tries.",
616 job
->id
, JobRetryLimit
);
618 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
623 * Try again in N seconds...
626 set_hold_until(job
, time(NULL
) + JobRetryInterval
);
628 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
,
629 "Job held due to fax errors; please consult "
630 "the error_log file for details.");
631 cupsdSetPrinterState(printer
, IPP_PRINTER_IDLE
, 0);
634 else if (!strcmp(printer
->error_policy
, "abort-job"))
635 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
638 cupsdSetPrinterState(printer
, IPP_PRINTER_STOPPED
, 1);
640 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
641 "Job stopped due to backend errors; please consult "
642 "the error_log file for details.");
646 case CUPS_BACKEND_CANCEL
:
651 cupsdCancelJob(job
, 0, IPP_JOB_CANCELED
);
654 case CUPS_BACKEND_HOLD
:
659 cupsdStopJob(job
, 0);
661 cupsdSetJobHoldUntil(job
, "indefinite");
663 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
664 job
->state_value
= IPP_JOB_HELD
;
668 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
669 "Job held due to backend errors; please consult "
670 "the error_log file for details.");
673 case CUPS_BACKEND_STOP
:
675 * Stop the printer...
678 cupsdStopJob(job
, 0);
680 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
681 job
->state_value
= IPP_JOB_PENDING
;
684 cupsdSetPrinterState(printer
, IPP_PRINTER_STOPPED
, 1);
686 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
687 "Job stopped due to backend errors; please consult "
688 "the error_log file for details.");
691 case CUPS_BACKEND_AUTH_REQUIRED
:
692 cupsdStopJob(job
, 0);
694 cupsdSetJobHoldUntil(job
, "auth-info-required");
696 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
697 IPP_TAG_KEYWORD
)) == NULL
)
698 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
702 attr
->value_tag
= IPP_TAG_KEYWORD
;
703 cupsdSetString(&(attr
->values
[0].string
.text
),
704 "auth-info-required");
707 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
708 job
->state_value
= IPP_JOB_HELD
;
712 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
713 "Authentication is required for job %d.", job
->id
);
718 * Try printing another job...
723 else if (job
->status
> 0)
726 * Filter had errors; stop job...
729 cupsdLogMessage(CUPSD_LOG_ERROR
,
730 "[Job %d] Job stopped due to filter errors.", job
->id
);
731 cupsdStopJob(job
, 1);
733 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
734 "Job stopped due to filter errors; please consult the "
735 "error_log file for details.");
741 * Job printed successfully; cancel it...
744 if (job
->current_file
< job
->num_files
)
747 * Start the next file in the job...
750 FilterLevel
-= job
->cost
;
751 start_job(job
, printer
);
756 * Close out this job...
759 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Completed successfully.",
761 cupsdCancelJob(job
, 0, IPP_JOB_COMPLETED
);
769 * 'cupsdFreeAllJobs()' - Free all jobs from memory.
773 cupsdFreeAllJobs(void)
775 cupsd_job_t
*job
; /* Current job */
786 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
788 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
790 cupsArrayRemove(Jobs
, job
);
791 cupsArrayRemove(ActiveJobs
, job
);
796 cupsdReleaseSignals();
801 * 'cupsdFindJob()' - Find the specified job.
804 cupsd_job_t
* /* O - Job data */
805 cupsdFindJob(int id
) /* I - Job ID */
807 cupsd_job_t key
; /* Search key */
812 return ((cupsd_job_t
*)cupsArrayFind(Jobs
, &key
));
817 * 'cupsdGetPrinterJobCount()' - Get the number of pending, processing,
818 * or held jobs in a printer or class.
821 int /* O - Job count */
822 cupsdGetPrinterJobCount(
823 const char *dest
) /* I - Printer or class name */
825 int count
; /* Job count */
826 cupsd_job_t
*job
; /* Current job */
829 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
831 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
832 if (job
->dest
&& !strcasecmp(job
->dest
, dest
))
840 * 'cupsdGetUserJobCount()' - Get the number of pending, processing,
841 * or held jobs for a user.
844 int /* O - Job count */
845 cupsdGetUserJobCount(
846 const char *username
) /* I - Username */
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 (!strcasecmp(job
->username
, username
))
863 * 'cupsdHoldJob()' - Hold the specified job.
867 cupsdHoldJob(cupsd_job_t
*job
) /* I - Job data */
869 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdHoldJob: id = %d", job
->id
);
871 if (job
->state_value
== IPP_JOB_PROCESSING
)
872 cupsdStopJob(job
, 0);
876 DEBUG_puts("cupsdHoldJob: setting state to held...");
878 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
879 job
->state_value
= IPP_JOB_HELD
;
888 * 'cupsdLoadAllJobs()' - Load all jobs from disk.
892 cupsdLoadAllJobs(void)
894 char filename
[1024]; /* Full filename of job.cache file */
895 struct stat fileinfo
, /* Information on job.cache file */
896 dirinfo
; /* Information on RequestRoot dir */
901 * Create the job arrays as needed...
905 Jobs
= cupsArrayNew(compare_jobs
, NULL
);
908 ActiveJobs
= cupsArrayNew(compare_active_jobs
, NULL
);
911 * See whether the job.cache file is older than the RequestRoot directory...
914 snprintf(filename
, sizeof(filename
), "%s/job.cache", CacheDir
);
916 if (stat(filename
, &fileinfo
))
918 fileinfo
.st_mtime
= 0;
921 cupsdLogMessage(CUPSD_LOG_ERROR
,
922 "Unable to get file information for \"%s\" - %s",
923 filename
, strerror(errno
));
926 if (stat(RequestRoot
, &dirinfo
))
928 dirinfo
.st_mtime
= 0;
931 cupsdLogMessage(CUPSD_LOG_ERROR
,
932 "Unable to get directory information for \"%s\" - %s",
933 RequestRoot
, strerror(errno
));
937 * Load the most recent source for job data...
940 if (dirinfo
.st_mtime
> fileinfo
.st_mtime
)
944 load_next_job_id(filename
);
947 load_job_cache(filename
);
950 * Clean out old jobs as needed...
953 if (MaxJobs
> 0 && cupsArrayCount(Jobs
) >= MaxJobs
)
959 * 'cupsdLoadJob()' - Load a single job...
963 cupsdLoadJob(cupsd_job_t
*job
) /* I - Job */
965 char jobfile
[1024]; /* Job filename */
966 cups_file_t
*fp
; /* Job file */
967 int fileid
; /* Current file ID */
968 ipp_attribute_t
*attr
; /* Job attribute */
969 const char *dest
; /* Destination name */
970 cupsd_printer_t
*destptr
; /* Pointer to destination */
971 mime_type_t
**filetypes
; /* New filetypes array */
972 int *compressions
; /* New compressions array */
977 if (job
->state_value
> IPP_JOB_STOPPED
)
978 job
->access_time
= time(NULL
);
983 if ((job
->attrs
= ippNew()) == NULL
)
985 cupsdLogMessage(CUPSD_LOG_ERROR
,
986 "[Job %d] Ran out of memory for job attributes!", job
->id
);
991 * Load job attributes...
994 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Loading attributes...", job
->id
);
996 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, job
->id
);
997 if ((fp
= cupsFileOpen(jobfile
, "r")) == NULL
)
999 cupsdLogMessage(CUPSD_LOG_ERROR
,
1000 "[Job %d] Unable to open job control file \"%s\" - %s!",
1001 job
->id
, jobfile
, strerror(errno
));
1002 ippDelete(job
->attrs
);
1007 if (ippReadIO(fp
, (ipp_iocb_t
)cupsFileRead
, 1, NULL
, job
->attrs
) != IPP_DATA
)
1009 cupsdLogMessage(CUPSD_LOG_ERROR
,
1010 "[Job %d] Unable to read job control file \"%s\"!",
1013 ippDelete(job
->attrs
);
1022 * Copy attribute data to the job object...
1025 if ((job
->state
= ippFindAttribute(job
->attrs
, "job-state",
1026 IPP_TAG_ENUM
)) == NULL
)
1028 cupsdLogMessage(CUPSD_LOG_ERROR
,
1029 "[Job %d] Missing or bad job-state attribute in "
1032 ippDelete(job
->attrs
);
1038 job
->state_value
= (ipp_jstate_t
)job
->state
->values
[0].integer
;
1042 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1043 IPP_TAG_URI
)) == NULL
)
1045 cupsdLogMessage(CUPSD_LOG_ERROR
,
1046 "[Job %d] No job-printer-uri attribute in control file!",
1048 ippDelete(job
->attrs
);
1054 if ((dest
= cupsdValidateDest(attr
->values
[0].string
.text
, &(job
->dtype
),
1057 cupsdLogMessage(CUPSD_LOG_ERROR
,
1058 "[Job %d] Unable to queue job for destination \"%s\"!",
1059 job
->id
, attr
->values
[0].string
.text
);
1060 ippDelete(job
->attrs
);
1066 cupsdSetString(&job
->dest
, dest
);
1068 else if ((destptr
= cupsdFindDest(job
->dest
)) == NULL
)
1070 cupsdLogMessage(CUPSD_LOG_ERROR
,
1071 "[Job %d] Unable to queue job for destination \"%s\"!",
1072 job
->id
, job
->dest
);
1073 ippDelete(job
->attrs
);
1079 job
->sheets
= ippFindAttribute(job
->attrs
, "job-media-sheets-completed",
1081 job
->job_sheets
= ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_NAME
);
1085 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
1086 IPP_TAG_INTEGER
)) == NULL
)
1088 cupsdLogMessage(CUPSD_LOG_ERROR
,
1089 "[Job %d] Missing or bad job-priority attribute in "
1090 "control file!", job
->id
);
1091 ippDelete(job
->attrs
);
1097 job
->priority
= attr
->values
[0].integer
;
1102 if ((attr
= ippFindAttribute(job
->attrs
, "job-originating-user-name",
1103 IPP_TAG_NAME
)) == NULL
)
1105 cupsdLogMessage(CUPSD_LOG_ERROR
,
1106 "[Job %d] Missing or bad job-originating-user-name "
1107 "attribute in control file!", job
->id
);
1108 ippDelete(job
->attrs
);
1114 cupsdSetString(&job
->username
, attr
->values
[0].string
.text
);
1118 * Set the job hold-until time and state...
1121 if (job
->state_value
== IPP_JOB_HELD
)
1123 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1124 IPP_TAG_KEYWORD
)) == NULL
)
1125 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1128 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
1131 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1132 job
->state_value
= IPP_JOB_PENDING
;
1135 else if (job
->state_value
== IPP_JOB_PROCESSING
)
1137 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1138 job
->state_value
= IPP_JOB_PENDING
;
1141 if (!job
->num_files
)
1144 * Find all the d##### files...
1147 for (fileid
= 1; fileid
< 10000; fileid
++)
1149 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
1152 if (access(jobfile
, 0))
1155 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1156 "[Job %d] Auto-typing document file \"%s\"...",
1159 if (fileid
> job
->num_files
)
1161 if (job
->num_files
== 0)
1163 compressions
= (int *)calloc(fileid
, sizeof(int));
1164 filetypes
= (mime_type_t
**)calloc(fileid
, sizeof(mime_type_t
*));
1168 compressions
= (int *)realloc(job
->compressions
,
1169 sizeof(int) * fileid
);
1170 filetypes
= (mime_type_t
**)realloc(job
->filetypes
,
1171 sizeof(mime_type_t
*) *
1175 if (!compressions
|| !filetypes
)
1177 cupsdLogMessage(CUPSD_LOG_ERROR
,
1178 "[Job %d] Ran out of memory for job file types!",
1183 job
->compressions
= compressions
;
1184 job
->filetypes
= filetypes
;
1185 job
->num_files
= fileid
;
1188 job
->filetypes
[fileid
- 1] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
1189 job
->compressions
+ fileid
- 1);
1191 if (!job
->filetypes
[fileid
- 1])
1192 job
->filetypes
[fileid
- 1] = mimeType(MimeDatabase
, "application",
1198 * Load authentication information as needed...
1201 if (job
->state_value
< IPP_JOB_STOPPED
)
1203 snprintf(jobfile
, sizeof(jobfile
), "%s/a%05d", RequestRoot
, job
->id
);
1205 cupsdClearString(&job
->auth_username
);
1206 cupsdClearString(&job
->auth_domain
);
1207 cupsdClearString(&job
->auth_password
);
1209 if ((fp
= cupsFileOpen(jobfile
, "r")) != NULL
)
1211 int i
, /* Looping var */
1212 bytes
; /* Size of auth data */
1213 char line
[255], /* Line from file */
1214 data
[255]; /* Decoded data */
1218 i
< destptr
->num_auth_info_required
&&
1219 cupsFileGets(fp
, line
, sizeof(line
));
1222 bytes
= sizeof(data
);
1223 httpDecode64_2(data
, &bytes
, line
);
1225 if (!strcmp(destptr
->auth_info_required
[i
], "username"))
1226 cupsdSetStringf(&job
->auth_username
, "AUTH_USERNAME=%s", data
);
1227 else if (!strcmp(destptr
->auth_info_required
[i
], "domain"))
1228 cupsdSetStringf(&job
->auth_domain
, "AUTH_DOMAIN=%s", data
);
1229 else if (!strcmp(destptr
->auth_info_required
[i
], "password"))
1230 cupsdSetStringf(&job
->auth_password
, "AUTH_PASSWORD=%s", data
);
1236 job
->access_time
= time(NULL
);
1241 * 'cupsdMoveJob()' - Move the specified job to a different destination.
1245 cupsdMoveJob(cupsd_job_t
*job
, /* I - Job */
1246 cupsd_printer_t
*p
) /* I - Destination printer or class */
1248 ipp_attribute_t
*attr
; /* job-printer-uri attribute */
1249 const char *olddest
; /* Old destination */
1250 cupsd_printer_t
*oldp
; /* Old pointer */
1254 * Don't move completed jobs...
1257 if (job
->state_value
> IPP_JOB_STOPPED
)
1261 * Get the old destination...
1264 olddest
= job
->dest
;
1267 oldp
= job
->printer
;
1269 oldp
= cupsdFindDest(olddest
);
1272 * Change the destination information...
1275 if (job
->state_value
== IPP_JOB_PROCESSING
)
1276 cupsdStopJob(job
, 0);
1280 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, oldp
, job
,
1281 "Job #%d moved from %s to %s.", job
->id
, olddest
,
1284 cupsdSetString(&job
->dest
, p
->name
);
1285 job
->dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_REMOTE
|
1286 CUPS_PRINTER_IMPLICIT
);
1288 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1289 IPP_TAG_URI
)) != NULL
)
1290 cupsdSetString(&(attr
->values
[0].string
.text
), p
->uri
);
1292 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, p
, job
,
1293 "Job #%d moved from %s to %s.", job
->id
, olddest
,
1301 * 'cupsdReleaseJob()' - Release the specified job.
1305 cupsdReleaseJob(cupsd_job_t
*job
) /* I - Job */
1307 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdReleaseJob: id = %d", job
->id
);
1309 if (job
->state_value
== IPP_JOB_HELD
)
1311 DEBUG_puts("cupsdReleaseJob: setting state to pending...");
1313 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1314 job
->state_value
= IPP_JOB_PENDING
;
1322 * 'cupsdRestartJob()' - Restart the specified job.
1326 cupsdRestartJob(cupsd_job_t
*job
) /* I - Job */
1328 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdRestartJob: id = %d", job
->id
);
1330 if (job
->state_value
== IPP_JOB_STOPPED
|| job
->num_files
)
1332 ipp_jstate_t old_state
; /* Old job state */
1337 old_state
= job
->state_value
;
1340 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1341 job
->state_value
= IPP_JOB_PENDING
;
1345 if (old_state
> IPP_JOB_STOPPED
)
1346 cupsArrayAdd(ActiveJobs
, job
);
1354 * 'cupsdSaveAllJobs()' - Save a summary of all jobs to disk.
1358 cupsdSaveAllJobs(void)
1360 int i
; /* Looping var */
1361 cups_file_t
*fp
; /* Job cache file */
1362 char temp
[1024]; /* Temporary string */
1363 cupsd_job_t
*job
; /* Current job */
1364 time_t curtime
; /* Current time */
1365 struct tm
*curdate
; /* Current date */
1368 snprintf(temp
, sizeof(temp
), "%s/job.cache", CacheDir
);
1369 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
1371 cupsdLogMessage(CUPSD_LOG_ERROR
,
1372 "Unable to create job cache file \"%s\" - %s",
1373 temp
, strerror(errno
));
1377 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving job cache file \"%s\"...", temp
);
1380 * Restrict access to the file...
1383 fchown(cupsFileNumber(fp
), getuid(), Group
);
1384 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
1387 * Write a small header to the file...
1390 curtime
= time(NULL
);
1391 curdate
= localtime(&curtime
);
1392 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
1394 cupsFilePuts(fp
, "# Job cache file for " CUPS_SVERSION
"\n");
1395 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
1396 cupsFilePrintf(fp
, "NextJobId %d\n", NextJobId
);
1399 * Write each job known to the system...
1402 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1404 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1406 cupsFilePrintf(fp
, "<Job %d>\n", job
->id
);
1407 cupsFilePrintf(fp
, "State %d\n", job
->state_value
);
1408 cupsFilePrintf(fp
, "Priority %d\n", job
->priority
);
1409 cupsFilePrintf(fp
, "Username %s\n", job
->username
);
1410 cupsFilePrintf(fp
, "Destination %s\n", job
->dest
);
1411 cupsFilePrintf(fp
, "DestType %d\n", job
->dtype
);
1412 cupsFilePrintf(fp
, "NumFiles %d\n", job
->num_files
);
1413 for (i
= 0; i
< job
->num_files
; i
++)
1414 cupsFilePrintf(fp
, "File %d %s/%s %d\n", i
+ 1, job
->filetypes
[i
]->super
,
1415 job
->filetypes
[i
]->type
, job
->compressions
[i
]);
1416 cupsFilePuts(fp
, "</Job>\n");
1424 * 'cupsdSaveJob()' - Save a job to disk.
1428 cupsdSaveJob(cupsd_job_t
*job
) /* I - Job */
1430 char filename
[1024]; /* Job control filename */
1431 cups_file_t
*fp
; /* Job file */
1434 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSaveJob(job=%p(%d)): job->attrs=%p",
1435 job
, job
->id
, job
->attrs
);
1437 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
, job
->id
);
1439 if ((fp
= cupsFileOpen(filename
, "w")) == NULL
)
1441 cupsdLogMessage(CUPSD_LOG_ERROR
,
1442 "[Job %d] Unable to create job control file \"%s\" - %s.",
1443 job
->id
, filename
, strerror(errno
));
1447 fchmod(cupsFileNumber(fp
), 0600);
1448 fchown(cupsFileNumber(fp
), RunUser
, Group
);
1450 job
->attrs
->state
= IPP_IDLE
;
1452 if (ippWriteIO(fp
, (ipp_iocb_t
)cupsFileWrite
, 1, NULL
,
1453 job
->attrs
) != IPP_DATA
)
1454 cupsdLogMessage(CUPSD_LOG_ERROR
,
1455 "[Job %d] Unable to write job control file!", job
->id
);
1462 * 'cupsdSetJobHoldUntil()' - Set the hold time for a job...
1466 cupsdSetJobHoldUntil(cupsd_job_t
*job
, /* I - Job */
1467 const char *when
) /* I - When to resume */
1469 time_t curtime
; /* Current time */
1470 struct tm
*curdate
; /* Current date */
1471 int hour
; /* Hold hour */
1472 int minute
; /* Hold minute */
1473 int second
; /* Hold second */
1476 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetJobHoldUntil(%d, \"%s\")",
1481 if (!strcmp(when
, "indefinite") || !strcmp(when
, "auth-info-required"))
1484 * Hold indefinitely...
1487 job
->hold_until
= 0;
1489 else if (!strcmp(when
, "day-time"))
1492 * Hold to 6am the next morning unless local time is < 6pm.
1495 curtime
= time(NULL
);
1496 curdate
= localtime(&curtime
);
1498 if (curdate
->tm_hour
< 18)
1499 job
->hold_until
= curtime
;
1501 job
->hold_until
= curtime
+
1502 ((29 - curdate
->tm_hour
) * 60 + 59 -
1503 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1505 else if (!strcmp(when
, "evening") || !strcmp(when
, "night"))
1508 * Hold to 6pm unless local time is > 6pm or < 6am.
1511 curtime
= time(NULL
);
1512 curdate
= localtime(&curtime
);
1514 if (curdate
->tm_hour
< 6 || curdate
->tm_hour
>= 18)
1515 job
->hold_until
= curtime
;
1517 job
->hold_until
= curtime
+
1518 ((17 - curdate
->tm_hour
) * 60 + 59 -
1519 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1521 else if (!strcmp(when
, "second-shift"))
1524 * Hold to 4pm unless local time is > 4pm.
1527 curtime
= time(NULL
);
1528 curdate
= localtime(&curtime
);
1530 if (curdate
->tm_hour
>= 16)
1531 job
->hold_until
= curtime
;
1533 job
->hold_until
= curtime
+
1534 ((15 - curdate
->tm_hour
) * 60 + 59 -
1535 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1537 else if (!strcmp(when
, "third-shift"))
1540 * Hold to 12am unless local time is < 8am.
1543 curtime
= time(NULL
);
1544 curdate
= localtime(&curtime
);
1546 if (curdate
->tm_hour
< 8)
1547 job
->hold_until
= curtime
;
1549 job
->hold_until
= curtime
+
1550 ((23 - curdate
->tm_hour
) * 60 + 59 -
1551 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1553 else if (!strcmp(when
, "weekend"))
1556 * Hold to weekend unless we are in the weekend.
1559 curtime
= time(NULL
);
1560 curdate
= localtime(&curtime
);
1562 if (curdate
->tm_wday
|| curdate
->tm_wday
== 6)
1563 job
->hold_until
= curtime
;
1565 job
->hold_until
= curtime
+
1566 (((5 - curdate
->tm_wday
) * 24 +
1567 (17 - curdate
->tm_hour
)) * 60 + 59 -
1568 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1570 else if (sscanf(when
, "%d:%d:%d", &hour
, &minute
, &second
) >= 2)
1573 * Hold to specified GMT time (HH:MM or HH:MM:SS)...
1576 curtime
= time(NULL
);
1577 curdate
= gmtime(&curtime
);
1579 job
->hold_until
= curtime
+
1580 ((hour
- curdate
->tm_hour
) * 60 + minute
-
1581 curdate
->tm_min
) * 60 + second
- curdate
->tm_sec
;
1584 * Hold until next day as needed...
1587 if (job
->hold_until
< curtime
)
1588 job
->hold_until
+= 24 * 60 * 60;
1591 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetJobHoldUntil: hold_until = %d",
1592 (int)job
->hold_until
);
1597 * 'cupsdSetJobPriority()' - Set the priority of a job, moving it up/down in
1598 * the list as needed.
1602 cupsdSetJobPriority(
1603 cupsd_job_t
*job
, /* I - Job ID */
1604 int priority
) /* I - New priority (0 to 100) */
1606 ipp_attribute_t
*attr
; /* Job attribute */
1610 * Don't change completed jobs...
1613 if (job
->state_value
>= IPP_JOB_PROCESSING
)
1617 * Set the new priority and re-add the job into the active list...
1620 cupsArrayRemove(ActiveJobs
, job
);
1622 job
->priority
= priority
;
1624 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
1625 IPP_TAG_INTEGER
)) != NULL
)
1626 attr
->values
[0].integer
= priority
;
1628 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-priority",
1631 cupsArrayAdd(ActiveJobs
, job
);
1638 * 'cupsdStopAllJobs()' - Stop all print jobs.
1642 cupsdStopAllJobs(int force
) /* I - 1 = Force all filters to stop */
1644 cupsd_job_t
*job
; /* Current job */
1647 DEBUG_puts("cupsdStopAllJobs()");
1649 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
1651 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
1652 if (job
->state_value
== IPP_JOB_PROCESSING
)
1654 cupsdStopJob(job
, force
);
1655 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1656 job
->state_value
= IPP_JOB_PENDING
;
1662 * 'cupsdStopJob()' - Stop a print job.
1666 cupsdStopJob(cupsd_job_t
*job
, /* I - Job */
1667 int force
) /* I - 1 = Force all filters to stop */
1669 int i
; /* Looping var */
1672 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1673 "[Job %d] cupsdStopJob: force = %d", job
->id
, force
);
1675 if (job
->state_value
!= IPP_JOB_PROCESSING
)
1678 FilterLevel
-= job
->cost
;
1680 if (job
->printer
->state
== IPP_PRINTER_PROCESSING
)
1681 cupsdSetPrinterState(job
->printer
, IPP_PRINTER_IDLE
, 0);
1683 job
->state
->values
[0].integer
= IPP_JOB_STOPPED
;
1684 job
->state_value
= IPP_JOB_STOPPED
;
1685 job
->printer
->job
= NULL
;
1686 job
->printer
= NULL
;
1688 job
->current_file
--;
1690 for (i
= 0; job
->filters
[i
]; i
++)
1691 if (job
->filters
[i
] > 0)
1693 cupsdEndProcess(job
->filters
[i
], force
);
1694 job
->filters
[i
] = 0;
1697 if (job
->backend
> 0)
1699 cupsdEndProcess(job
->backend
, force
);
1703 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] Closing print pipes [ %d %d ]...",
1704 job
->id
, job
->print_pipes
[0], job
->print_pipes
[1]);
1706 cupsdClosePipe(job
->print_pipes
);
1708 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] Closing back pipes [ %d %d ]...",
1709 job
->id
, job
->back_pipes
[0], job
->back_pipes
[1]);
1711 cupsdClosePipe(job
->back_pipes
);
1713 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] Closing side pipes [ %d %d ]...",
1714 job
->id
, job
->side_pipes
[0], job
->side_pipes
[1]);
1716 cupsdClosePipe(job
->side_pipes
);
1718 if (job
->status_buffer
)
1721 * Close the pipe and clear the input bit.
1724 cupsdRemoveSelect(job
->status_buffer
->fd
);
1726 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1727 "[Job %d] Closing status pipes [ %d %d ]...", job
->id
,
1728 job
->status_pipes
[0], job
->status_pipes
[1]);
1730 cupsdClosePipe(job
->status_pipes
);
1731 cupsdStatBufDelete(job
->status_buffer
);
1733 job
->status_buffer
= NULL
;
1739 * 'cupsdUnloadCompletedJobs()' - Flush completed job history from memory.
1743 cupsdUnloadCompletedJobs(void)
1745 cupsd_job_t
*job
; /* Current job */
1746 time_t expire
; /* Expiration time */
1749 expire
= time(NULL
) - 60;
1751 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1753 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1754 if (job
->attrs
&& job
->state_value
>= IPP_JOB_STOPPED
&&
1755 job
->access_time
< expire
)
1761 * 'compare_active_jobs()' - Compare the job IDs and priorities of two jobs.
1764 static int /* O - Difference */
1765 compare_active_jobs(void *first
, /* I - First job */
1766 void *second
, /* I - Second job */
1767 void *data
) /* I - App data (not used) */
1769 int diff
; /* Difference */
1772 if ((diff
= ((cupsd_job_t
*)second
)->priority
-
1773 ((cupsd_job_t
*)first
)->priority
) != 0)
1776 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
1781 * 'compare_jobs()' - Compare the job IDs of two jobs.
1784 static int /* O - Difference */
1785 compare_jobs(void *first
, /* I - First job */
1786 void *second
, /* I - Second job */
1787 void *data
) /* I - App data (not used) */
1789 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
1794 * 'free_job()' - Free all memory used by a job.
1798 free_job(cupsd_job_t
*job
) /* I - Job */
1800 cupsdClearString(&job
->username
);
1801 cupsdClearString(&job
->dest
);
1802 cupsdClearString(&job
->auth_username
);
1803 cupsdClearString(&job
->auth_domain
);
1804 cupsdClearString(&job
->auth_password
);
1809 * Destroy the credential cache and clear the KRB5CCNAME env var string.
1812 krb5_cc_destroy(KerberosContext
, job
->ccache
);
1814 cupsdClearString(&job
->ccname
);
1816 #endif /* HAVE_GSSAPI */
1818 if (job
->num_files
> 0)
1820 free(job
->compressions
);
1821 free(job
->filetypes
);
1824 ippDelete(job
->attrs
);
1831 * 'ipp_length()' - Compute the size of the buffer needed to hold
1832 * the textual IPP attributes.
1835 static int /* O - Size of attribute buffer */
1836 ipp_length(ipp_t
*ipp
) /* I - IPP request */
1838 int bytes
; /* Number of bytes */
1839 int i
; /* Looping var */
1840 ipp_attribute_t
*attr
; /* Current attribute */
1844 * Loop through all attributes...
1849 for (attr
= ipp
->attrs
; attr
!= NULL
; attr
= attr
->next
)
1852 * Skip attributes that won't be sent to filters...
1855 if (attr
->value_tag
== IPP_TAG_MIMETYPE
||
1856 attr
->value_tag
== IPP_TAG_NAMELANG
||
1857 attr
->value_tag
== IPP_TAG_TEXTLANG
||
1858 attr
->value_tag
== IPP_TAG_URI
||
1859 attr
->value_tag
== IPP_TAG_URISCHEME
)
1862 if (strncmp(attr
->name
, "time-", 5) == 0)
1866 * Add space for a leading space and commas between each value.
1867 * For the first attribute, the leading space isn't used, so the
1868 * extra byte can be used as the nul terminator...
1871 bytes
++; /* " " separator */
1872 bytes
+= attr
->num_values
; /* "," separators */
1875 * Boolean attributes appear as "foo,nofoo,foo,nofoo", while
1876 * other attributes appear as "foo=value1,value2,...,valueN".
1879 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
1880 bytes
+= strlen(attr
->name
);
1882 bytes
+= attr
->num_values
* strlen(attr
->name
);
1885 * Now add the size required for each value in the attribute...
1888 switch (attr
->value_tag
)
1890 case IPP_TAG_INTEGER
:
1893 * Minimum value of a signed integer is -2147483647, or 11 digits.
1896 bytes
+= attr
->num_values
* 11;
1899 case IPP_TAG_BOOLEAN
:
1901 * Add two bytes for each false ("no") value...
1904 for (i
= 0; i
< attr
->num_values
; i
++)
1905 if (!attr
->values
[i
].boolean
)
1909 case IPP_TAG_RANGE
:
1911 * A range is two signed integers separated by a hyphen, or
1912 * 23 characters max.
1915 bytes
+= attr
->num_values
* 23;
1918 case IPP_TAG_RESOLUTION
:
1920 * A resolution is two signed integers separated by an "x" and
1921 * suffixed by the units, or 26 characters max.
1924 bytes
+= attr
->num_values
* 26;
1927 case IPP_TAG_STRING
:
1930 case IPP_TAG_KEYWORD
:
1931 case IPP_TAG_CHARSET
:
1932 case IPP_TAG_LANGUAGE
:
1935 * Strings can contain characters that need quoting. We need
1936 * at least 2 * len + 2 characters to cover the quotes and
1937 * any backslashes in the string.
1940 for (i
= 0; i
< attr
->num_values
; i
++)
1941 bytes
+= 2 * strlen(attr
->values
[i
].string
.text
) + 2;
1945 break; /* anti-compiler-warning-code */
1954 * 'load_job_cache()' - Load jobs from the job.cache file.
1958 load_job_cache(const char *filename
) /* I - job.cache filename */
1960 cups_file_t
*fp
; /* job.cache file */
1961 char line
[1024], /* Line buffer */
1962 *value
; /* Value on line */
1963 int linenum
; /* Line number in file */
1964 cupsd_job_t
*job
; /* Current job */
1965 int jobid
; /* Job ID */
1966 char jobfile
[1024]; /* Job filename */
1970 * Open the job.cache file...
1973 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
1975 if (errno
!= ENOENT
)
1976 cupsdLogMessage(CUPSD_LOG_ERROR
,
1977 "Unable to open job cache file \"%s\": %s",
1978 filename
, strerror(errno
));
1980 load_request_root();
1986 * Read entries from the job cache file and create jobs as needed.
1989 cupsdLogMessage(CUPSD_LOG_INFO
, "Loading job cache file \"%s\"...",
1995 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
1997 if (!strcasecmp(line
, "NextJobId"))
2000 NextJobId
= atoi(value
);
2002 else if (!strcasecmp(line
, "<Job"))
2006 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing </Job> directive on line %d!",
2013 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing job ID on line %d!", linenum
);
2017 jobid
= atoi(value
);
2021 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad job ID %d on line %d!", jobid
,
2026 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, jobid
);
2027 if (access(jobfile
, 0))
2029 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Job %d] Files have gone away!",
2034 job
= calloc(1, sizeof(cupsd_job_t
));
2037 cupsdLogMessage(CUPSD_LOG_EMERG
,
2038 "[Job %d] Unable to allocate memory for job!", jobid
);
2043 job
->back_pipes
[0] = -1;
2044 job
->back_pipes
[1] = -1;
2045 job
->print_pipes
[0] = -1;
2046 job
->print_pipes
[1] = -1;
2047 job
->side_pipes
[0] = -1;
2048 job
->side_pipes
[1] = -1;
2049 job
->status_pipes
[0] = -1;
2050 job
->status_pipes
[1] = -1;
2052 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Loading from cache...", job
->id
);
2056 cupsdLogMessage(CUPSD_LOG_ERROR
,
2057 "Missing <Job #> directive on line %d!", linenum
);
2060 else if (!strcasecmp(line
, "</Job>"))
2062 cupsArrayAdd(Jobs
, job
);
2064 if (job
->state_value
<= IPP_JOB_STOPPED
)
2066 cupsArrayAdd(ActiveJobs
, job
);
2074 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing value on line %d!", linenum
);
2077 else if (!strcasecmp(line
, "State"))
2079 job
->state_value
= (ipp_jstate_t
)atoi(value
);
2081 if (job
->state_value
< IPP_JOB_PENDING
)
2082 job
->state_value
= IPP_JOB_PENDING
;
2083 else if (job
->state_value
> IPP_JOB_COMPLETED
)
2084 job
->state_value
= IPP_JOB_COMPLETED
;
2086 else if (!strcasecmp(line
, "Priority"))
2088 job
->priority
= atoi(value
);
2090 else if (!strcasecmp(line
, "Username"))
2092 cupsdSetString(&job
->username
, value
);
2094 else if (!strcasecmp(line
, "Destination"))
2096 cupsdSetString(&job
->dest
, value
);
2098 else if (!strcasecmp(line
, "DestType"))
2100 job
->dtype
= (cups_ptype_t
)atoi(value
);
2102 else if (!strcasecmp(line
, "NumFiles"))
2104 job
->num_files
= atoi(value
);
2106 if (job
->num_files
< 0)
2108 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad NumFiles value %d on line %d!",
2109 job
->num_files
, linenum
);
2114 if (job
->num_files
> 0)
2116 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-001", RequestRoot
,
2118 if (access(jobfile
, 0))
2120 cupsdLogMessage(CUPSD_LOG_INFO
,
2121 "[Job %d] Data files have gone away!", job
->id
);
2126 job
->filetypes
= calloc(job
->num_files
, sizeof(mime_type_t
*));
2127 job
->compressions
= calloc(job
->num_files
, sizeof(int));
2129 if (!job
->filetypes
|| !job
->compressions
)
2131 cupsdLogMessage(CUPSD_LOG_EMERG
,
2132 "[Job %d] Unable to allocate memory for %d files!",
2133 job
->id
, job
->num_files
);
2138 else if (!strcasecmp(line
, "File"))
2140 int number
, /* File number */
2141 compression
; /* Compression value */
2142 char super
[MIME_MAX_SUPER
], /* MIME super type */
2143 type
[MIME_MAX_TYPE
]; /* MIME type */
2146 if (sscanf(value
, "%d%*[ \t]%15[^/]/%255s%d", &number
, super
, type
,
2149 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File on line %d!", linenum
);
2153 if (number
< 1 || number
> job
->num_files
)
2155 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File number %d on line %d!",
2162 job
->compressions
[number
] = compression
;
2163 job
->filetypes
[number
] = mimeType(MimeDatabase
, super
, type
);
2165 if (!job
->filetypes
[number
])
2168 * If the original MIME type is unknown, auto-type it!
2171 cupsdLogMessage(CUPSD_LOG_ERROR
,
2172 "[Job %d] Unknown MIME type %s/%s for file %d!",
2173 job
->id
, super
, type
, number
+ 1);
2175 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
2176 job
->id
, number
+ 1);
2177 job
->filetypes
[number
] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
2178 job
->compressions
+ number
);
2181 * If that didn't work, assume it is raw...
2184 if (!job
->filetypes
[number
])
2185 job
->filetypes
[number
] = mimeType(MimeDatabase
, "application",
2190 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unknown %s directive on line %d!",
2199 * 'load_next_job_id()' - Load the NextJobId value from the job.cache file.
2203 load_next_job_id(const char *filename
) /* I - job.cache filename */
2205 cups_file_t
*fp
; /* job.cache file */
2206 char line
[1024], /* Line buffer */
2207 *value
; /* Value on line */
2208 int linenum
; /* Line number in file */
2209 int next_job_id
; /* NextJobId value from line */
2213 * Read the NextJobId directive from the job.cache file and use
2214 * the value (if any).
2217 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
2219 if (errno
!= ENOENT
)
2220 cupsdLogMessage(CUPSD_LOG_ERROR
,
2221 "Unable to open job cache file \"%s\": %s",
2222 filename
, strerror(errno
));
2227 cupsdLogMessage(CUPSD_LOG_INFO
,
2228 "Loading NextJobId from job cache file \"%s\"...", filename
);
2232 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
2234 if (!strcasecmp(line
, "NextJobId"))
2238 next_job_id
= atoi(value
);
2240 if (next_job_id
> NextJobId
)
2241 NextJobId
= next_job_id
;
2252 * 'load_request_root()' - Load jobs from the RequestRoot directory.
2256 load_request_root(void)
2258 cups_dir_t
*dir
; /* Directory */
2259 cups_dentry_t
*dent
; /* Directory entry */
2260 cupsd_job_t
*job
; /* New job */
2264 * Open the requests directory...
2267 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Scanning %s for jobs...", RequestRoot
);
2269 if ((dir
= cupsDirOpen(RequestRoot
)) == NULL
)
2271 cupsdLogMessage(CUPSD_LOG_ERROR
,
2272 "Unable to open spool directory \"%s\": %s",
2273 RequestRoot
, strerror(errno
));
2278 * Read all the c##### files...
2281 while ((dent
= cupsDirRead(dir
)) != NULL
)
2282 if (strlen(dent
->filename
) >= 6 && dent
->filename
[0] == 'c')
2285 * Allocate memory for the job...
2288 if ((job
= calloc(sizeof(cupsd_job_t
), 1)) == NULL
)
2290 cupsdLogMessage(CUPSD_LOG_ERROR
, "Ran out of memory for jobs!");
2296 * Assign the job ID...
2299 job
->id
= atoi(dent
->filename
+ 1);
2300 job
->back_pipes
[0] = -1;
2301 job
->back_pipes
[1] = -1;
2302 job
->print_pipes
[0] = -1;
2303 job
->print_pipes
[1] = -1;
2304 job
->side_pipes
[0] = -1;
2305 job
->side_pipes
[1] = -1;
2306 job
->status_pipes
[0] = -1;
2307 job
->status_pipes
[1] = -1;
2309 if (job
->id
>= NextJobId
)
2310 NextJobId
= job
->id
+ 1;
2319 * Insert the job into the array, sorting by job priority and ID...
2322 cupsArrayAdd(Jobs
, job
);
2324 if (job
->state_value
<= IPP_JOB_STOPPED
)
2325 cupsArrayAdd(ActiveJobs
, job
);
2335 * 'set_time()' - Set one of the "time-at-xyz" attributes...
2339 set_time(cupsd_job_t
*job
, /* I - Job to update */
2340 const char *name
) /* I - Name of attribute */
2342 ipp_attribute_t
*attr
; /* Time attribute */
2345 if ((attr
= ippFindAttribute(job
->attrs
, name
, IPP_TAG_ZERO
)) != NULL
)
2347 attr
->value_tag
= IPP_TAG_INTEGER
;
2348 attr
->values
[0].integer
= time(NULL
);
2354 * 'set_hold_until()' - Set the hold time and update job-hold-until attribute...
2358 set_hold_until(cupsd_job_t
*job
, /* I - Job to update */
2359 time_t holdtime
) /* I - Hold until time */
2361 ipp_attribute_t
*attr
; /* job-hold-until attribute */
2362 struct tm
*holddate
; /* Hold date */
2363 char holdstr
[64]; /* Hold time */
2367 * Set the hold_until value and hold the job...
2370 cupsdLogMessage(CUPSD_LOG_DEBUG
, "set_hold_until: hold_until = %d",
2373 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
2374 job
->state_value
= IPP_JOB_HELD
;
2375 job
->hold_until
= holdtime
;
2378 * Update the job-hold-until attribute with a string representing GMT
2379 * time (HH:MM:SS)...
2382 holddate
= gmtime(&holdtime
);
2383 snprintf(holdstr
, sizeof(holdstr
), "%d:%d:%d", holddate
->tm_hour
,
2384 holddate
->tm_min
, holddate
->tm_sec
);
2386 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2387 IPP_TAG_KEYWORD
)) == NULL
)
2388 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2391 * Either add the attribute or update the value of the existing one
2395 attr
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
2396 "job-hold-until", NULL
, holdstr
);
2398 cupsdSetString(&attr
->values
[0].string
.text
, holdstr
);
2405 * 'start_job()' - Start a print job.
2409 start_job(cupsd_job_t
*job
, /* I - Job ID */
2410 cupsd_printer_t
*printer
) /* I - Printer to print job */
2412 int i
; /* Looping var */
2413 int slot
; /* Pipe slot */
2414 cups_array_t
*filters
, /* Filters for job */
2415 *prefilters
; /* Filters with prefilters */
2416 mime_filter_t
*filter
, /* Current filter */
2417 *prefilter
, /* Prefilter */
2418 port_monitor
; /* Port monitor filter */
2419 char method
[255], /* Method for output */
2420 *optptr
, /* Pointer to options */
2421 *valptr
; /* Pointer in value string */
2422 ipp_attribute_t
*attr
; /* Current attribute */
2423 struct stat backinfo
; /* Backend file information */
2424 int backroot
; /* Run backend as root? */
2425 int pid
; /* Process ID of new filter process */
2426 int banner_page
; /* 1 if banner page, 0 otherwise */
2427 int filterfds
[2][2];/* Pipes used between filters */
2428 int envc
; /* Number of environment variables */
2429 char **argv
, /* Filter command-line arguments */
2430 sani_uri
[1024], /* Sanitized DEVICE_URI env var */
2431 filename
[1024], /* Job filename */
2432 command
[1024], /* Full path to command */
2433 jobid
[255], /* Job ID string */
2434 title
[IPP_MAX_NAME
],
2435 /* Job title string */
2436 copies
[255], /* # copies string */
2437 *envp
[MAX_ENV
+ 15],
2438 /* Environment variables */
2439 charset
[255], /* CHARSET env variable */
2440 class_name
[255],/* CLASS env variable */
2441 classification
[1024],
2442 /* CLASSIFICATION env variable */
2444 /* CONTENT_TYPE env variable */
2446 /* DEVICE_URI env variable */
2447 final_content_type
[1024],
2448 /* FINAL_CONTENT_TYPE env variable */
2449 lang
[255], /* LANG env variable */
2450 ppd
[1024], /* PPD env variable */
2452 /* PRINTER env variable */
2454 /* RIP_MAX_CACHE env variable */
2455 static char *options
= NULL
;/* Full list of options */
2456 static int optlength
= 0; /* Length of option buffer */
2459 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] start_job: file = %d/%d",
2460 job
->id
, job
->current_file
, job
->num_files
);
2462 if (job
->num_files
== 0)
2464 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Job %d] No files, canceling job!",
2467 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2472 * Figure out what filters are required to convert from
2473 * the source to the destination type...
2482 * Remote jobs and raw queues go directly to the printer without
2486 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2487 "[Job %d] Sending job to queue tagged as raw...", job
->id
);
2494 * Local jobs get filtered...
2497 filters
= mimeFilter(MimeDatabase
, job
->filetypes
[job
->current_file
],
2498 printer
->filetype
, &(job
->cost
));
2502 cupsdLogMessage(CUPSD_LOG_ERROR
,
2503 "[Job %d] Unable to convert file %d to printable format!",
2504 job
->current_file
, job
->id
);
2505 cupsdLogMessage(CUPSD_LOG_INFO
,
2506 "Hint: Do you have ESP Ghostscript installed?");
2508 if (LogLevel
< CUPSD_LOG_DEBUG
)
2509 cupsdLogMessage(CUPSD_LOG_INFO
,
2510 "Hint: Try setting the LogLevel to \"debug\".");
2512 job
->current_file
++;
2514 if (job
->current_file
== job
->num_files
)
2515 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2521 * Remove NULL ("-") filters...
2524 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
2526 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
2527 if (!strcmp(filter
->filter
, "-"))
2528 cupsArrayRemove(filters
, filter
);
2530 if (cupsArrayCount(filters
) == 0)
2532 cupsArrayDelete(filters
);
2537 * If this printer has any pre-filters, insert the required pre-filter
2538 * in the filters array...
2541 if (printer
->prefiltertype
&& filters
)
2543 prefilters
= cupsArrayNew(NULL
, NULL
);
2545 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
2547 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
2549 if ((prefilter
= mimeFilterLookup(MimeDatabase
, filter
->src
,
2550 printer
->prefiltertype
)))
2552 cupsArrayAdd(prefilters
, prefilter
);
2553 job
->cost
+= prefilter
->cost
;
2556 cupsArrayAdd(prefilters
, filter
);
2559 cupsArrayDelete(filters
);
2560 filters
= prefilters
;
2565 * Set a minimum cost of 100 for all jobs so that FilterLimit
2566 * works with raw queues and other low-cost paths.
2569 if (job
->cost
< 100)
2573 * See if the filter cost is too high...
2576 if ((FilterLevel
+ job
->cost
) > FilterLimit
&& FilterLevel
> 0 &&
2580 * Don't print this job quite yet...
2583 cupsArrayDelete(filters
);
2585 cupsdLogMessage(CUPSD_LOG_INFO
,
2586 "[Job %d] Holding because filter limit has been reached.",
2588 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2589 "[Job %d] start_job: file=%d, cost=%d, level=%d, limit=%d",
2590 job
->id
, job
->current_file
, job
->cost
, FilterLevel
,
2595 FilterLevel
+= job
->cost
;
2598 * Add decompression/raw filter as needed...
2601 if ((!printer
->raw
&& job
->compressions
[job
->current_file
]) ||
2602 (!filters
&& !printer
->remote
&&
2603 (job
->num_files
> 1 || !strncmp(printer
->device_uri
, "file:", 5))))
2606 * Add gziptoany filter to the front of the list...
2610 filters
= cupsArrayNew(NULL
, NULL
);
2612 if (!cupsArrayInsert(filters
, &gziptoany_filter
))
2614 cupsdLogMessage(CUPSD_LOG_ERROR
,
2615 "[Job %d] Unable to add decompression filter - %s",
2616 job
->id
, strerror(errno
));
2618 cupsArrayDelete(filters
);
2620 job
->current_file
++;
2622 if (job
->current_file
== job
->num_files
)
2623 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2630 * Add port monitor, if any...
2633 if (printer
->port_monitor
)
2636 * Add port monitor to the end of the list...
2640 filters
= cupsArrayNew(NULL
, NULL
);
2642 if (!cupsArrayAdd(filters
, &port_monitor
))
2644 cupsdLogMessage(CUPSD_LOG_ERROR
,
2645 "[Job %d] Unable to add port monitor - %s",
2646 job
->id
, strerror(errno
));
2648 cupsArrayDelete(filters
);
2650 job
->current_file
++;
2652 if (job
->current_file
== job
->num_files
)
2653 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2658 snprintf(port_monitor
.filter
, sizeof(port_monitor
.filter
),
2659 "%s/monitor/%s", ServerBin
, printer
->port_monitor
);
2663 * Update the printer and job state to "processing"...
2666 job
->state
->values
[0].integer
= IPP_JOB_PROCESSING
;
2667 job
->state_value
= IPP_JOB_PROCESSING
;
2670 job
->printer
= printer
;
2673 cupsdSetPrinterState(printer
, IPP_PRINTER_PROCESSING
, 0);
2675 if (job
->current_file
== 0)
2678 * Set the processing time...
2681 set_time(job
, "time-at-processing");
2684 * Create the backchannel pipes and make them non-blocking...
2687 cupsdOpenPipe(job
->back_pipes
);
2689 fcntl(job
->back_pipes
[0], F_SETFL
,
2690 fcntl(job
->back_pipes
[0], F_GETFL
) | O_NONBLOCK
);
2692 fcntl(job
->back_pipes
[1], F_SETFL
,
2693 fcntl(job
->back_pipes
[1], F_GETFL
) | O_NONBLOCK
);
2696 * Create the side-channel pipes and make them non-blocking...
2699 socketpair(AF_LOCAL
, SOCK_STREAM
, 0, job
->side_pipes
);
2701 fcntl(job
->side_pipes
[0], F_SETFL
,
2702 fcntl(job
->side_pipes
[0], F_GETFL
) | O_NONBLOCK
);
2704 fcntl(job
->side_pipes
[1], F_SETFL
,
2705 fcntl(job
->side_pipes
[1], F_GETFL
) | O_NONBLOCK
);
2709 * Determine if we are printing a banner page or not...
2712 if (job
->job_sheets
== NULL
)
2714 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] No job-sheets attribute.",
2716 if ((job
->job_sheets
=
2717 ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_ZERO
)) != NULL
)
2718 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2719 "[Job %d] ... but someone added one without setting "
2720 "job_sheets!", job
->id
);
2722 else if (job
->job_sheets
->num_values
== 1)
2723 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] job-sheets=%s", job
->id
,
2724 job
->job_sheets
->values
[0].string
.text
);
2726 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] job-sheets=%s,%s", job
->id
,
2727 job
->job_sheets
->values
[0].string
.text
,
2728 job
->job_sheets
->values
[1].string
.text
);
2730 if (printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
))
2732 else if (job
->job_sheets
== NULL
)
2734 else if (strcasecmp(job
->job_sheets
->values
[0].string
.text
, "none") != 0 &&
2735 job
->current_file
== 0)
2737 else if (job
->job_sheets
->num_values
> 1 &&
2738 strcasecmp(job
->job_sheets
->values
[1].string
.text
, "none") != 0 &&
2739 job
->current_file
== (job
->num_files
- 1))
2744 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] banner_page = %d", job
->id
,
2748 * Building the options string is harder than it needs to be, but
2749 * for the moment we need to pass strings for command-line args and
2750 * not IPP attribute pointers... :)
2752 * First allocate/reallocate the option buffer as needed...
2755 i
= ipp_length(job
->attrs
);
2762 optptr
= realloc(options
, i
);
2766 cupsdLogMessage(CUPSD_LOG_CRIT
,
2767 "[Job %d] Unable to allocate %d bytes for option buffer!",
2770 cupsArrayDelete(filters
);
2772 FilterLevel
-= job
->cost
;
2774 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2783 * Now loop through the attributes and convert them to the textual
2784 * representation used by the filters...
2790 snprintf(title
, sizeof(title
), "%s-%d", printer
->name
, job
->id
);
2791 strcpy(copies
, "1");
2793 for (attr
= job
->attrs
->attrs
; attr
!= NULL
; attr
= attr
->next
)
2795 if (!strcmp(attr
->name
, "copies") &&
2796 attr
->value_tag
== IPP_TAG_INTEGER
)
2799 * Don't use the # copies attribute if we are printing the job sheets...
2803 sprintf(copies
, "%d", attr
->values
[0].integer
);
2805 else if (!strcmp(attr
->name
, "job-name") &&
2806 (attr
->value_tag
== IPP_TAG_NAME
||
2807 attr
->value_tag
== IPP_TAG_NAMELANG
))
2808 strlcpy(title
, attr
->values
[0].string
.text
, sizeof(title
));
2809 else if (attr
->group_tag
== IPP_TAG_JOB
)
2812 * Filter out other unwanted attributes...
2815 if (attr
->value_tag
== IPP_TAG_MIMETYPE
||
2816 attr
->value_tag
== IPP_TAG_NAMELANG
||
2817 attr
->value_tag
== IPP_TAG_TEXTLANG
||
2818 (attr
->value_tag
== IPP_TAG_URI
&& strcmp(attr
->name
, "job-uuid")) ||
2819 attr
->value_tag
== IPP_TAG_URISCHEME
||
2820 attr
->value_tag
== IPP_TAG_BEGIN_COLLECTION
) /* Not yet supported */
2823 if (!strncmp(attr
->name
, "time-", 5))
2826 if (!strncmp(attr
->name
, "job-", 4) && strcmp(attr
->name
, "job-uuid") &&
2827 !(printer
->type
& CUPS_PRINTER_REMOTE
))
2830 if (!strncmp(attr
->name
, "job-", 4) &&
2831 strcmp(attr
->name
, "job-uuid") &&
2832 strcmp(attr
->name
, "job-billing") &&
2833 strcmp(attr
->name
, "job-sheets") &&
2834 strcmp(attr
->name
, "job-hold-until") &&
2835 strcmp(attr
->name
, "job-priority"))
2838 if ((!strcmp(attr
->name
, "page-label") ||
2839 !strcmp(attr
->name
, "page-border") ||
2840 !strncmp(attr
->name
, "number-up", 9) ||
2841 !strcmp(attr
->name
, "page-ranges") ||
2842 !strcmp(attr
->name
, "page-set") ||
2843 !strcasecmp(attr
->name
, "AP_FIRSTPAGE_InputSlot") ||
2844 !strcasecmp(attr
->name
, "AP_FIRSTPAGE_ManualFeed")) &&
2849 * Otherwise add them to the list...
2852 if (optptr
> options
)
2853 strlcat(optptr
, " ", optlength
- (optptr
- options
));
2855 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
2857 strlcat(optptr
, attr
->name
, optlength
- (optptr
- options
));
2858 strlcat(optptr
, "=", optlength
- (optptr
- options
));
2861 for (i
= 0; i
< attr
->num_values
; i
++)
2864 strlcat(optptr
, ",", optlength
- (optptr
- options
));
2866 optptr
+= strlen(optptr
);
2868 switch (attr
->value_tag
)
2870 case IPP_TAG_INTEGER
:
2872 snprintf(optptr
, optlength
- (optptr
- options
),
2873 "%d", attr
->values
[i
].integer
);
2876 case IPP_TAG_BOOLEAN
:
2877 if (!attr
->values
[i
].boolean
)
2878 strlcat(optptr
, "no", optlength
- (optptr
- options
));
2880 case IPP_TAG_NOVALUE
:
2881 strlcat(optptr
, attr
->name
,
2882 optlength
- (optptr
- options
));
2885 case IPP_TAG_RANGE
:
2886 if (attr
->values
[i
].range
.lower
== attr
->values
[i
].range
.upper
)
2887 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2888 "%d", attr
->values
[i
].range
.lower
);
2890 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2891 "%d-%d", attr
->values
[i
].range
.lower
,
2892 attr
->values
[i
].range
.upper
);
2895 case IPP_TAG_RESOLUTION
:
2896 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2897 "%dx%d%s", attr
->values
[i
].resolution
.xres
,
2898 attr
->values
[i
].resolution
.yres
,
2899 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
2903 case IPP_TAG_STRING
:
2906 case IPP_TAG_KEYWORD
:
2907 case IPP_TAG_CHARSET
:
2908 case IPP_TAG_LANGUAGE
:
2910 for (valptr
= attr
->values
[i
].string
.text
; *valptr
;)
2912 if (strchr(" \t\n\\\'\"", *valptr
))
2914 *optptr
++ = *valptr
++;
2921 break; /* anti-compiler-warning-code */
2925 optptr
+= strlen(optptr
);
2930 * Build the command-line arguments for the filters. Each filter
2931 * has 6 or 7 arguments:
2935 * argv[2] = username
2937 * argv[4] = # copies
2939 * argv[6] = filename (optional; normally stdin)
2941 * This allows legacy printer drivers that use the old System V
2942 * printing interface to be used by CUPS.
2944 * For remote jobs, we send all of the files in the argument list.
2947 if (printer
->remote
&& job
->num_files
> 1)
2948 argv
= calloc(7 + job
->num_files
, sizeof(char *));
2950 argv
= calloc(8, sizeof(char *));
2952 sprintf(jobid
, "%d", job
->id
);
2954 argv
[0] = printer
->name
;
2956 argv
[2] = job
->username
;
2961 if (printer
->remote
&& job
->num_files
> 1)
2963 for (i
= 0; i
< job
->num_files
; i
++)
2965 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
2967 argv
[6 + i
] = strdup(filename
);
2972 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
2973 job
->id
, job
->current_file
+ 1);
2977 for (i
= 0; argv
[i
]; i
++)
2978 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2979 "[Job %d] argv[%d]=\"%s\"", job
->id
, i
, argv
[i
]);
2982 * Create environment variable strings for the filters...
2985 attr
= ippFindAttribute(job
->attrs
, "attributes-natural-language",
2988 switch (strlen(attr
->values
[0].string
.text
))
2992 * This is an unknown or badly formatted language code; use
2993 * the POSIX locale...
2996 strcpy(lang
, "LANG=C");
3001 * Just the language code (ll)...
3004 snprintf(lang
, sizeof(lang
), "LANG=%s",
3005 attr
->values
[0].string
.text
);
3010 * Language and country code (ll-cc)...
3013 snprintf(lang
, sizeof(lang
), "LANG=%c%c_%c%c",
3014 attr
->values
[0].string
.text
[0],
3015 attr
->values
[0].string
.text
[1],
3016 toupper(attr
->values
[0].string
.text
[3] & 255),
3017 toupper(attr
->values
[0].string
.text
[4] & 255));
3021 attr
= ippFindAttribute(job
->attrs
, "document-format",
3024 (optptr
= strstr(attr
->values
[0].string
.text
, "charset=")) != NULL
)
3025 snprintf(charset
, sizeof(charset
), "CHARSET=%s", optptr
+ 8);
3028 attr
= ippFindAttribute(job
->attrs
, "attributes-charset",
3030 snprintf(charset
, sizeof(charset
), "CHARSET=%s",
3031 attr
->values
[0].string
.text
);
3034 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s/%s",
3035 job
->filetypes
[job
->current_file
]->super
,
3036 job
->filetypes
[job
->current_file
]->type
);
3037 snprintf(device_uri
, sizeof(device_uri
), "DEVICE_URI=%s",
3038 printer
->device_uri
);
3039 cupsdSanitizeURI(printer
->device_uri
, sani_uri
, sizeof(sani_uri
));
3040 snprintf(ppd
, sizeof(ppd
), "PPD=%s/ppd/%s.ppd", ServerRoot
, printer
->name
);
3041 snprintf(printer_name
, sizeof(printer_name
), "PRINTER=%s", printer
->name
);
3042 snprintf(rip_max_cache
, sizeof(rip_max_cache
), "RIP_MAX_CACHE=%s", RIPCache
);
3044 envc
= cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
3046 envp
[envc
++] = charset
;
3047 envp
[envc
++] = lang
;
3048 envp
[envc
++] = ppd
;
3049 envp
[envc
++] = rip_max_cache
;
3050 envp
[envc
++] = content_type
;
3051 envp
[envc
++] = device_uri
;
3052 envp
[envc
++] = printer_name
;
3054 if (!printer
->remote
&& !printer
->raw
&&
3055 (filter
= (mime_filter_t
*)cupsArrayLast(filters
)) != NULL
&&
3058 snprintf(final_content_type
, sizeof(final_content_type
),
3059 "FINAL_CONTENT_TYPE=%s/%s",
3060 filter
->dst
->super
, filter
->dst
->type
);
3061 envp
[envc
++] = final_content_type
;
3064 if (Classification
&& !banner_page
)
3066 if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
3067 IPP_TAG_NAME
)) == NULL
)
3068 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3070 else if (attr
->num_values
> 1 &&
3071 strcmp(attr
->values
[1].string
.text
, "none") != 0)
3072 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3073 attr
->values
[1].string
.text
);
3075 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3076 attr
->values
[0].string
.text
);
3078 envp
[envc
++] = classification
;
3081 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
3083 snprintf(class_name
, sizeof(class_name
), "CLASS=%s", job
->dest
);
3084 envp
[envc
++] = class_name
;
3087 if (job
->auth_username
)
3088 envp
[envc
++] = job
->auth_username
;
3089 if (job
->auth_domain
)
3090 envp
[envc
++] = job
->auth_domain
;
3091 if (job
->auth_password
)
3092 envp
[envc
++] = job
->auth_password
;
3096 envp
[envc
++] = job
->ccname
;
3097 #endif /* HAVE_GSSAPI */
3101 for (i
= 0; i
< envc
; i
++)
3102 if (!strncmp(envp
[i
], "AUTH_", 5))
3103 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] envp[%d]=\"AUTH_%c****\"",
3104 job
->id
, i
, envp
[i
][5]);
3105 else if (strncmp(envp
[i
], "DEVICE_URI=", 11))
3106 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] envp[%d]=\"%s\"",
3107 job
->id
, i
, envp
[i
]);
3109 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] envp[%d]=\"DEVICE_URI=%s\"",
3110 job
->id
, i
, sani_uri
);
3112 if (printer
->remote
)
3113 job
->current_file
= job
->num_files
;
3115 job
->current_file
++;
3118 * Now create processes for all of the filters...
3121 filterfds
[0][0] = -1;
3122 filterfds
[0][1] = -1;
3123 filterfds
[1][0] = -1;
3124 filterfds
[1][1] = -1;
3126 if (!job
->status_buffer
)
3128 if (cupsdOpenPipe(job
->status_pipes
))
3130 cupsdLogMessage(CUPSD_LOG_ERROR
,
3131 "[Job %d] Unable to create job status pipes - %s.",
3132 job
->id
, strerror(errno
));
3133 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3134 "Unable to create status pipes - %s.", strerror(errno
));
3136 cupsdAddPrinterHistory(printer
);
3138 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3139 "Job canceled because the server could not create the job "
3145 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3146 "[Job %d] start_job: status_pipes = [ %d %d ]",
3147 job
->id
, job
->status_pipes
[0], job
->status_pipes
[1]);
3149 job
->status_buffer
= cupsdStatBufNew(job
->status_pipes
[0], "[Job %d]",
3151 job
->status_level
= CUPSD_LOG_INFO
;
3155 memset(job
->filters
, 0, sizeof(job
->filters
));
3157 for (i
= 0, slot
= 0, filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
3159 i
++, filter
= (mime_filter_t
*)cupsArrayNext(filters
))
3161 if (filter
->filter
[0] != '/')
3162 snprintf(command
, sizeof(command
), "%s/filter/%s", ServerBin
,
3165 strlcpy(command
, filter
->filter
, sizeof(command
));
3167 if (i
< (cupsArrayCount(filters
) - 1))
3169 if (cupsdOpenPipe(filterfds
[slot
]))
3171 cupsdLogMessage(CUPSD_LOG_ERROR
,
3172 "[Job %d] Unable to create job filter pipes - %s.",
3173 job
->id
, strerror(errno
));
3174 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3175 "Unable to create filter pipes - %s.", strerror(errno
));
3176 cupsdAddPrinterHistory(printer
);
3178 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3179 "Job canceled because the server could not create the "
3187 if (job
->current_file
== 1)
3189 if (strncmp(printer
->device_uri
, "file:", 5) != 0)
3191 if (cupsdOpenPipe(job
->print_pipes
))
3193 cupsdLogMessage(CUPSD_LOG_ERROR
,
3194 "[Job %d] Unable to create job backend pipes - %s.",
3195 job
->id
, strerror(errno
));
3196 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3197 "Unable to create backend pipes - %s.", strerror(errno
));
3198 cupsdAddPrinterHistory(printer
);
3200 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3201 "Job canceled because the server could not create "
3202 "the backend pipes.");
3209 job
->print_pipes
[0] = -1;
3210 if (!strcmp(printer
->device_uri
, "file:/dev/null") ||
3211 !strcmp(printer
->device_uri
, "file:///dev/null"))
3212 job
->print_pipes
[1] = -1;
3215 if (!strncmp(printer
->device_uri
, "file:/dev/", 10))
3216 job
->print_pipes
[1] = open(printer
->device_uri
+ 5,
3218 else if (!strncmp(printer
->device_uri
, "file:///dev/", 12))
3219 job
->print_pipes
[1] = open(printer
->device_uri
+ 7,
3221 else if (!strncmp(printer
->device_uri
, "file:///", 8))
3222 job
->print_pipes
[1] = open(printer
->device_uri
+ 7,
3223 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
3225 job
->print_pipes
[1] = open(printer
->device_uri
+ 5,
3226 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
3228 if (job
->print_pipes
[1] < 0)
3230 cupsdLogMessage(CUPSD_LOG_ERROR
,
3231 "[Job %d] Unable to open output file \"%s\" - %s.",
3232 job
->id
, printer
->device_uri
, strerror(errno
));
3233 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3234 "Unable to open output file \"%s\" - %s.",
3235 printer
->device_uri
, strerror(errno
));
3237 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3238 "Job canceled because the server could not open the "
3244 fcntl(job
->print_pipes
[1], F_SETFD
,
3245 fcntl(job
->print_pipes
[1], F_GETFD
) | FD_CLOEXEC
);
3249 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3250 "[Job %d] start_job: print_pipes = [ %d %d ]",
3251 job
->id
, job
->print_pipes
[0], job
->print_pipes
[1]);
3254 filterfds
[slot
][0] = job
->print_pipes
[0];
3255 filterfds
[slot
][1] = job
->print_pipes
[1];
3258 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] start_job: filter=\"%s\"",
3260 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3261 "[Job %d] start_job: filterfds[%d]=[ %d %d ]",
3262 job
->id
, slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3264 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
3265 filterfds
[slot
][1], job
->status_pipes
[1],
3266 job
->back_pipes
[0], job
->side_pipes
[0], 0,
3269 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3270 "[Job %d] start_job: Closing filter pipes for slot %d "
3272 job
->id
, !slot
, filterfds
[!slot
][0], filterfds
[!slot
][1]);
3274 cupsdClosePipe(filterfds
[!slot
]);
3278 cupsdLogMessage(CUPSD_LOG_ERROR
,
3279 "[Job %d] Unable to start filter \"%s\" - %s.",
3280 job
->id
, filter
->filter
, strerror(errno
));
3281 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3282 "Unable to start filter \"%s\" - %s.",
3283 filter
->filter
, strerror(errno
));
3285 cupsdAddPrinterHistory(printer
);
3287 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3288 "Job canceled because the server could not execute a "
3294 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Started filter %s (PID %d)",
3295 job
->id
, command
, pid
);
3301 cupsArrayDelete(filters
);
3304 * Finally, pipe the final output into a backend process if needed...
3307 if (strncmp(printer
->device_uri
, "file:", 5) != 0)
3309 if (job
->current_file
== 1)
3311 sscanf(printer
->device_uri
, "%254[^:]", method
);
3312 snprintf(command
, sizeof(command
), "%s/backend/%s", ServerBin
, method
);
3315 * See if the backend needs to run as root...
3320 else if (stat(command
, &backinfo
))
3323 backroot
= !(backinfo
.st_mode
& (S_IRWXG
| S_IRWXO
));
3327 filterfds
[slot
][0] = -1;
3328 filterfds
[slot
][1] = -1;
3330 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] start_job: backend=\"%s\"",
3332 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3333 "[Job %d] start_job: filterfds[%d] = [ %d %d ]", job
->id
,
3334 slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3336 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
3337 filterfds
[slot
][1], job
->status_pipes
[1],
3338 job
->back_pipes
[1], job
->side_pipes
[1],
3339 backroot
, &(job
->backend
));
3343 cupsdLogMessage(CUPSD_LOG_ERROR
,
3344 "[Job %d] Unable to start backend \"%s\" - %s.",
3345 job
->id
, method
, strerror(errno
));
3346 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3347 "Unable to start backend \"%s\" - %s.", method
,
3350 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3351 "Job canceled because the server could not execute "
3358 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Started backend %s (PID %d)",
3359 job
->id
, command
, pid
);
3363 if (job
->current_file
== job
->num_files
)
3365 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3366 "[Job %d] start_job: Closing print pipes [ %d %d ]...",
3367 job
->id
, job
->print_pipes
[0], job
->print_pipes
[1]);
3369 cupsdClosePipe(job
->print_pipes
);
3371 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3372 "[Job %d] start_job: Closing back pipes [ %d %d ]...",
3373 job
->id
, job
->back_pipes
[0], job
->back_pipes
[1]);
3375 cupsdClosePipe(job
->back_pipes
);
3377 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3378 "[Job %d] start_job: Closing side pipes [ %d %d ]...",
3379 job
->id
, job
->side_pipes
[0], job
->side_pipes
[1]);
3381 cupsdClosePipe(job
->side_pipes
);
3383 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3384 "[Job %d] start_job: Closing status output pipe %d...",
3385 job
->id
, job
->status_pipes
[1]);
3387 close(job
->status_pipes
[1]);
3388 job
->status_pipes
[1] = -1;
3393 filterfds
[slot
][0] = -1;
3394 filterfds
[slot
][1] = -1;
3396 if (job
->current_file
== job
->num_files
)
3398 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3399 "[Job %d] start_job: Closing print pipes [ %d %d ]...",
3400 job
->id
, job
->print_pipes
[0], job
->print_pipes
[1]);
3402 cupsdClosePipe(job
->print_pipes
);
3404 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3405 "[Job %d] start_job: Closing status output pipe %d...",
3406 job
->id
, job
->status_pipes
[1]);
3408 close(job
->status_pipes
[1]);
3409 job
->status_pipes
[1] = -1;
3413 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3414 "[Job %d] start_job: Closing filter pipes for slot %d "
3416 job
->id
, slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3417 cupsdClosePipe(filterfds
[slot
]);
3419 if (printer
->remote
&& job
->num_files
> 1)
3421 for (i
= 0; i
< job
->num_files
; i
++)
3427 cupsdAddSelect(job
->status_buffer
->fd
, (cupsd_selfunc_t
)update_job
, NULL
,
3430 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
, "Job #%d started.",
3437 * If we get here, we need to abort the current job and close out all
3438 * files and pipes...
3443 for (slot
= 0; slot
< 2; slot
++)
3445 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3446 "[Job %d] start_job: Closing filter pipes for slot %d "
3448 job
->id
, slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3449 cupsdClosePipe(filterfds
[slot
]);
3452 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3453 "[Job %d] start_job: Closing status pipes [ %d %d ]...",
3454 job
->id
, job
->status_pipes
[0], job
->status_pipes
[1]);
3455 cupsdClosePipe(job
->status_pipes
);
3456 cupsdStatBufDelete(job
->status_buffer
);
3458 job
->status_buffer
= NULL
;
3460 cupsArrayDelete(filters
);
3462 if (printer
->remote
&& job
->num_files
> 1)
3464 for (i
= 0; i
< job
->num_files
; i
++)
3470 cupsdStopJob(job
, 0);
3475 * 'unload_job()' - Unload a job from memory.
3479 unload_job(cupsd_job_t
*job
) /* I - Job */
3484 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Unloading...", job
->id
);
3486 ippDelete(job
->attrs
);
3491 job
->job_sheets
= NULL
;
3492 job
->printer_message
= NULL
;
3493 job
->printer_reasons
= NULL
;
3498 * 'update_job()' - Read a status update from a job's filters.
3502 update_job(cupsd_job_t
*job
) /* I - Job to check */
3504 int i
; /* Looping var */
3505 int copies
; /* Number of copies printed */
3506 char message
[1024], /* Message text */
3507 *ptr
; /* Pointer update... */
3508 int loglevel
, /* Log level for message */
3509 event
= 0; /* Events? */
3512 while ((ptr
= cupsdStatBufUpdate(job
->status_buffer
, &loglevel
,
3513 message
, sizeof(message
))) != NULL
)
3516 * Process page and printer state messages as needed...
3519 if (loglevel
== CUPSD_LOG_PAGE
)
3522 * Page message; send the message to the page_log file and update the
3523 * job sheet count...
3526 if (job
->sheets
!= NULL
)
3528 if (!strncasecmp(message
, "total ", 6))
3531 * Got a total count of pages from a backend or filter...
3534 copies
= atoi(message
+ 6);
3535 copies
-= job
->sheets
->values
[0].integer
; /* Just track the delta */
3537 else if (!sscanf(message
, "%*d%d", &copies
))
3540 job
->sheets
->values
[0].integer
+= copies
;
3542 if (job
->printer
->page_limit
)
3544 cupsd_quota_t
*q
= cupsdUpdateQuota(job
->printer
, job
->username
,
3548 if (AppleQuotas
&& q
->page_count
== -3)
3551 * Quota limit exceeded, cancel job in progress immediately...
3554 cupsdLogMessage(CUPSD_LOG_INFO
,
3555 "[Job %d] Canceled because pages exceed user %s "
3556 "quota limit on printer %s (%s).",
3557 job
->id
, job
->username
, job
->printer
->name
,
3558 job
->printer
->info
);
3560 cupsdCancelJob(job
, 1, IPP_JOB_CANCELED
);
3565 #endif /* __APPLE__ */
3569 cupsdLogPage(job
, message
);
3571 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
3572 "Printed %d page(s).", job
->sheets
->values
[0].integer
);
3574 else if (loglevel
== CUPSD_LOG_STATE
)
3576 if (!strcmp(message
, "paused"))
3578 cupsdStopPrinter(job
->printer
, 1);
3583 cupsdSetPrinterReasons(job
->printer
, message
);
3584 cupsdAddPrinterHistory(job
->printer
);
3585 event
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
3588 update_job_attrs(job
);
3590 else if (loglevel
== CUPSD_LOG_ATTR
)
3593 * Set attribute(s)...
3596 int num_attrs
; /* Number of attributes */
3597 cups_option_t
*attrs
; /* Attributes */
3598 const char *attr
; /* Attribute */
3601 num_attrs
= cupsParseOptions(message
, 0, &attrs
);
3603 if ((attr
= cupsGetOption("auth-info-required", num_attrs
,
3606 cupsdSetAuthInfoRequired(job
->printer
, attr
, NULL
);
3607 cupsdSaveAllPrinters();
3610 if ((attr
= cupsGetOption("printer-alert", num_attrs
, attrs
)) != NULL
)
3612 cupsdSetString(&job
->printer
->alert
, attr
);
3613 event
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
3616 if ((attr
= cupsGetOption("printer-alert-description", num_attrs
,
3619 cupsdSetString(&job
->printer
->alert_description
, attr
);
3620 event
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
3623 cupsFreeOptions(num_attrs
, attrs
);
3626 else if (!strncmp(message
, "recoverable:", 12))
3628 cupsdSetPrinterReasons(job
->printer
,
3629 "+com.apple.print.recoverable-warning");
3632 while (isspace(*ptr
& 255))
3635 cupsdSetString(&job
->printer
->recoverable
, ptr
);
3636 cupsdAddPrinterHistory(job
->printer
);
3637 event
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
3639 else if (!strncmp(message
, "recovered:", 10))
3641 cupsdSetPrinterReasons(job
->printer
,
3642 "-com.apple.print.recoverable-warning");
3645 while (isspace(*ptr
& 255))
3648 cupsdSetString(&job
->printer
->recoverable
, ptr
);
3649 cupsdAddPrinterHistory(job
->printer
);
3650 event
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
3652 #endif /* __APPLE__ */
3653 else if (loglevel
<= job
->status_level
)
3656 * Some message to show in the printer-state-message attribute...
3659 if (loglevel
!= CUPSD_LOG_NOTICE
)
3660 job
->status_level
= loglevel
;
3662 strlcpy(job
->printer
->state_message
, message
,
3663 sizeof(job
->printer
->state_message
));
3664 cupsdAddPrinterHistory(job
->printer
);
3665 event
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
3667 update_job_attrs(job
);
3670 if (!strchr(job
->status_buffer
->buffer
, '\n'))
3674 if ((event
& CUPSD_EVENT_PRINTER_STATE_CHANGED
))
3675 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE_CHANGED
, job
->printer
, NULL
,
3676 (job
->printer
->type
& CUPS_PRINTER_CLASS
) ?
3677 "Class \"%s\" state changed." :
3678 "Printer \"%s\" state changed.",
3679 job
->printer
->name
);
3681 if (ptr
== NULL
&& !job
->status_buffer
->bufused
)
3684 * See if all of the filters and the backend have returned their
3688 for (i
= 0; job
->filters
[i
] < 0; i
++);
3690 if (job
->filters
[i
])
3693 if (job
->current_file
>= job
->num_files
&& job
->backend
> 0)
3697 * Handle the end of job stuff...
3700 cupsdFinishJob(job
);
3706 * 'update_job_attrs()' - Update the job-printer-* attributes.
3710 update_job_attrs(cupsd_job_t
*job
) /* I - Job to update */
3712 int i
; /* Looping var */
3713 int num_reasons
; /* Actual number of reasons */
3714 const char * const *reasons
; /* Reasons */
3715 static const char *none
= "none", /* "none" */
3721 * Get/create the job-printer-state-* attributes...
3724 if (!job
->printer_message
)
3726 if ((job
->printer_message
= ippFindAttribute(job
->attrs
,
3727 "job-printer-state-message",
3728 IPP_TAG_TEXT
)) == NULL
)
3729 job
->printer_message
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_TEXT
,
3730 "job-printer-state-message",
3734 if (!job
->printer_reasons
)
3735 job
->printer_reasons
= ippFindAttribute(job
->attrs
,
3736 "job-printer-state-reasons",
3740 * If the job isn't printing, return now...
3747 * Otherwise copy the printer-state-message value...
3750 if (job
->printer
->state_message
[0])
3751 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
),
3752 job
->printer
->state_message
);
3755 * ... and the printer-state-reasons value...
3758 if (job
->printer
->num_reasons
== 0)
3761 reasons
= job
->printer
->state
== IPP_PRINTER_STOPPED
? &paused
: &none
;
3765 num_reasons
= job
->printer
->num_reasons
;
3766 reasons
= (const char * const *)job
->printer
->reasons
;
3769 if (!job
->printer_reasons
|| job
->printer_reasons
->num_values
!= num_reasons
)
3771 ippDeleteAttribute(job
->attrs
, job
->printer_reasons
);
3773 job
->printer_reasons
= ippAddStrings(job
->attrs
,
3774 IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
3775 "job-printer-state-reasons",
3776 num_reasons
, NULL
, NULL
);
3779 for (i
= 0; i
< num_reasons
; i
++)
3780 cupsdSetString(&(job
->printer_reasons
->values
[i
].string
.text
), reasons
[i
]);
3785 * End of "$Id: job.c 6755 2007-08-01 19:02:47Z mike $".