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 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
);
234 * Destroy the credential cache and clear the KRB5CCNAME env var string.
239 krb5_cc_destroy(KerberosContext
, job
->ccache
);
243 cupsdClearString(&job
->ccname
);
244 #endif /* HAVE_GSSAPI */
247 * Remove the print file for good if we aren't preserving jobs or
251 job
->current_file
= 0;
253 if (!JobHistory
|| !JobFiles
|| purge
|| (job
->dtype
& CUPS_PRINTER_REMOTE
))
255 for (i
= 1; i
<= job
->num_files
; i
++)
257 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
262 if (job
->num_files
> 0)
264 free(job
->filetypes
);
265 free(job
->compressions
);
268 job
->filetypes
= NULL
;
269 job
->compressions
= NULL
;
273 if (JobHistory
&& !purge
&& !(job
->dtype
& CUPS_PRINTER_REMOTE
))
276 * Save job state info...
284 * Remove the job info file...
287 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
,
292 * Remove the job from the "all jobs" list...
295 cupsArrayRemove(Jobs
, job
);
298 * Free all memory used...
307 * 'cupsdCancelJobs()' - Cancel all jobs for the given destination/user...
311 cupsdCancelJobs(const char *dest
, /* I - Destination to cancel */
312 const char *username
, /* I - Username or NULL */
313 int purge
) /* I - Purge jobs? */
315 cupsd_job_t
*job
; /* Current job */
318 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
320 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
321 if ((dest
== NULL
|| !strcmp(job
->dest
, dest
)) &&
322 (username
== NULL
|| !strcmp(job
->username
, username
)))
325 * Cancel all jobs matching this destination/user...
328 cupsdCancelJob(job
, purge
, IPP_JOB_CANCELED
);
336 * 'cupsdCheckJobs()' - Check the pending jobs and start any if the destination
343 cupsd_job_t
*job
; /* Current job in queue */
344 cupsd_printer_t
*printer
, /* Printer destination */
345 *pclass
; /* Printer class destination */
348 DEBUG_puts("cupsdCheckJobs()");
350 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
351 "cupsdCheckJobs: %d active jobs, sleeping=%d, reload=%d",
352 cupsArrayCount(ActiveJobs
), Sleeping
, NeedReload
);
354 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
356 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
359 * Start held jobs if they are ready...
362 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
363 "cupsdCheckJobs: Job %d: state_value=%d, loaded=%s",
364 job
->id
, job
->state_value
, job
->attrs
? "yes" : "no");
366 if (job
->state_value
== IPP_JOB_HELD
&&
368 job
->hold_until
< time(NULL
))
370 if (job
->pending_timeout
)
371 cupsdTimeoutJob(job
); /* Add trailing banner as needed */
373 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
374 job
->state_value
= IPP_JOB_PENDING
;
378 * Start pending jobs if the destination is available...
381 if (job
->state_value
== IPP_JOB_PENDING
&& !NeedReload
&& !Sleeping
)
383 printer
= cupsdFindDest(job
->dest
);
387 (printer
->type
& (CUPS_PRINTER_IMPLICIT
| CUPS_PRINTER_CLASS
)))
390 * If the class is remote, just pass it to the remote server...
395 if (pclass
->state
== IPP_PRINTER_STOPPED
)
397 else if (pclass
->type
& CUPS_PRINTER_REMOTE
)
400 printer
= cupsdFindAvailablePrinter(printer
->name
);
403 if (!printer
&& !pclass
)
406 * Whoa, the printer and/or class for this destination went away;
410 cupsdLogMessage(CUPSD_LOG_WARN
,
411 "[Job %d] Printer/class %s has gone away; canceling job!",
414 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
415 "Job canceled because the destination printer/class has "
418 cupsdCancelJob(job
, 1, IPP_JOB_ABORTED
);
423 * See if the printer is available or remote and not printing a job;
424 * if so, start the job...
430 * Add/update a job-actual-printer-uri attribute for this job
431 * so that we know which printer actually printed the job...
434 ipp_attribute_t
*attr
; /* job-actual-printer-uri attribute */
437 if ((attr
= ippFindAttribute(job
->attrs
, "job-actual-printer-uri",
438 IPP_TAG_URI
)) != NULL
)
439 cupsdSetString(&attr
->values
[0].string
.text
, printer
->uri
);
441 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
,
442 "job-actual-printer-uri", NULL
, printer
->uri
);
445 if ((!(printer
->type
& CUPS_PRINTER_DISCOVERED
) && /* Printer is local */
446 printer
->state
== IPP_PRINTER_IDLE
) || /* and idle */
447 ((printer
->type
& CUPS_PRINTER_DISCOVERED
) && /* Printer is remote */
448 !printer
->job
)) /* and not printing */
451 * Clear any message and reasons for the queue...
454 printer
->state_message
[0] = '\0';
456 cupsdSetPrinterReasons(printer
, "none");
462 start_job(job
, printer
);
471 * 'cupsdCleanJobs()' - Clean out old jobs.
477 cupsd_job_t
*job
; /* Current job */
483 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
484 job
&& cupsArrayCount(Jobs
) >= MaxJobs
;
485 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
486 if (job
->state_value
>= IPP_JOB_CANCELED
)
487 cupsdCancelJob(job
, 1, IPP_JOB_CANCELED
);
492 * 'cupsdFinishJob()' - Finish a job.
496 cupsdFinishJob(cupsd_job_t
*job
) /* I - Job */
498 cupsd_printer_t
*printer
; /* Current printer */
499 ipp_attribute_t
*attr
; /* job-hold-until attribute */
502 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] File %d is complete.",
503 job
->id
, job
->current_file
- 1);
505 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
506 "[Job %d] cupsdFinishJob: job->status is %d",
507 job
->id
, job
->status
);
509 if (job
->status_buffer
&&
510 (job
->status
< 0 || job
->current_file
>= job
->num_files
))
513 * Close the pipe and clear the input bit.
516 cupsdRemoveSelect(job
->status_buffer
->fd
);
518 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
519 "[Job %d] cupsdFinishJob: Closing status pipes [ %d %d ]...",
520 job
->id
, job
->status_pipes
[0], job
->status_pipes
[1]);
522 cupsdClosePipe(job
->status_pipes
);
523 cupsdStatBufDelete(job
->status_buffer
);
525 job
->status_buffer
= NULL
;
528 printer
= job
->printer
;
530 update_job_attrs(job
);
535 * Backend had errors; stop it...
538 int exit_code
; /* Exit code from backend */
542 * Convert the status to an exit code. Due to the way the W* macros are
543 * implemented on MacOS X (bug?), we have to store the exit status in a
544 * variable first and then convert...
547 exit_code
= -job
->status
;
548 if (WIFEXITED(exit_code
))
549 exit_code
= WEXITSTATUS(exit_code
);
551 exit_code
= job
->status
;
553 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Backend returned status %d (%s)",
555 exit_code
== CUPS_BACKEND_FAILED
? "failed" :
556 exit_code
== CUPS_BACKEND_AUTH_REQUIRED
?
557 "authentication required" :
558 exit_code
== CUPS_BACKEND_HOLD
? "hold job" :
559 exit_code
== CUPS_BACKEND_STOP
? "stop printer" :
560 exit_code
== CUPS_BACKEND_CANCEL
? "cancel job" :
561 exit_code
< 0 ? "crashed" : "unknown");
564 * Do what needs to be done...
570 case CUPS_BACKEND_FAILED
:
572 * Backend failure, use the error-policy to determine how to
576 cupsdStopJob(job
, 0);
578 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
581 * Mark the job as pending again - we'll retry on another
585 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
586 job
->state_value
= IPP_JOB_PENDING
;
592 * If the job was queued to a class, try requeuing it... For
593 * faxes and retry-job queues, hold the current job for 5 minutes.
596 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
598 else if ((printer
->type
& CUPS_PRINTER_FAX
) ||
599 !strcmp(printer
->error_policy
, "retry-job"))
602 * See how many times we've tried to send the job; if more than
603 * the limit, cancel the job.
608 if (job
->tries
>= JobRetryLimit
)
614 cupsdLogMessage(CUPSD_LOG_ERROR
,
615 "[Job %d] Canceling job since it could not be "
616 "sent after %d tries.",
617 job
->id
, JobRetryLimit
);
619 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
624 * Try again in N seconds...
627 set_hold_until(job
, time(NULL
) + JobRetryInterval
);
629 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
,
630 "Job held due to fax errors; please consult "
631 "the error_log file for details.");
632 cupsdSetPrinterState(printer
, IPP_PRINTER_IDLE
, 0);
635 else if (!strcmp(printer
->error_policy
, "abort-job"))
636 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
639 cupsdSetPrinterState(printer
, IPP_PRINTER_STOPPED
, 1);
641 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
642 "Job stopped due to backend errors; please consult "
643 "the error_log file for details.");
647 case CUPS_BACKEND_CANCEL
:
652 cupsdCancelJob(job
, 0, IPP_JOB_CANCELED
);
655 case CUPS_BACKEND_HOLD
:
660 cupsdStopJob(job
, 0);
662 cupsdSetJobHoldUntil(job
, "indefinite");
664 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
665 job
->state_value
= IPP_JOB_HELD
;
669 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
670 "Job held due to backend errors; please consult "
671 "the error_log file for details.");
674 case CUPS_BACKEND_STOP
:
676 * Stop the printer...
679 cupsdStopJob(job
, 0);
681 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
682 job
->state_value
= IPP_JOB_PENDING
;
685 cupsdSetPrinterState(printer
, IPP_PRINTER_STOPPED
, 1);
687 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
688 "Job stopped due to backend errors; please consult "
689 "the error_log file for details.");
692 case CUPS_BACKEND_AUTH_REQUIRED
:
693 cupsdStopJob(job
, 0);
695 cupsdSetJobHoldUntil(job
, "auth-info-required");
697 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
698 IPP_TAG_KEYWORD
)) == NULL
)
699 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
703 attr
->value_tag
= IPP_TAG_KEYWORD
;
704 cupsdSetString(&(attr
->values
[0].string
.text
),
705 "auth-info-required");
708 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
709 job
->state_value
= IPP_JOB_HELD
;
713 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
714 "Authentication is required for job %d.", job
->id
);
719 * Try printing another job...
724 else if (job
->status
> 0)
727 * Filter had errors; stop job...
730 cupsdLogMessage(CUPSD_LOG_ERROR
,
731 "[Job %d] Job stopped due to filter errors.", job
->id
);
732 cupsdStopJob(job
, 1);
734 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
735 "Job stopped due to filter errors; please consult the "
736 "error_log file for details.");
742 * Job printed successfully; cancel it...
745 if (job
->current_file
< job
->num_files
)
748 * Start the next file in the job...
751 FilterLevel
-= job
->cost
;
752 start_job(job
, printer
);
757 * Close out this job...
760 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Completed successfully.",
762 cupsdCancelJob(job
, 0, IPP_JOB_COMPLETED
);
770 * 'cupsdFreeAllJobs()' - Free all jobs from memory.
774 cupsdFreeAllJobs(void)
776 cupsd_job_t
*job
; /* Current job */
787 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
789 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
791 cupsArrayRemove(Jobs
, job
);
792 cupsArrayRemove(ActiveJobs
, job
);
797 cupsdReleaseSignals();
802 * 'cupsdFindJob()' - Find the specified job.
805 cupsd_job_t
* /* O - Job data */
806 cupsdFindJob(int id
) /* I - Job ID */
808 cupsd_job_t key
; /* Search key */
813 return ((cupsd_job_t
*)cupsArrayFind(Jobs
, &key
));
818 * 'cupsdGetPrinterJobCount()' - Get the number of pending, processing,
819 * or held jobs in a printer or class.
822 int /* O - Job count */
823 cupsdGetPrinterJobCount(
824 const char *dest
) /* I - Printer or class name */
826 int count
; /* Job count */
827 cupsd_job_t
*job
; /* Current job */
830 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
832 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
833 if (job
->dest
&& !strcasecmp(job
->dest
, dest
))
841 * 'cupsdGetUserJobCount()' - Get the number of pending, processing,
842 * or held jobs for a user.
845 int /* O - Job count */
846 cupsdGetUserJobCount(
847 const char *username
) /* I - Username */
849 int count
; /* Job count */
850 cupsd_job_t
*job
; /* Current job */
853 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
855 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
856 if (!strcasecmp(job
->username
, username
))
864 * 'cupsdHoldJob()' - Hold the specified job.
868 cupsdHoldJob(cupsd_job_t
*job
) /* I - Job data */
870 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdHoldJob: id = %d", job
->id
);
872 if (job
->state_value
== IPP_JOB_PROCESSING
)
873 cupsdStopJob(job
, 0);
877 DEBUG_puts("cupsdHoldJob: setting state to held...");
879 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
880 job
->state_value
= IPP_JOB_HELD
;
881 job
->current_file
= 0;
890 * 'cupsdLoadAllJobs()' - Load all jobs from disk.
894 cupsdLoadAllJobs(void)
896 char filename
[1024]; /* Full filename of job.cache file */
897 struct stat fileinfo
, /* Information on job.cache file */
898 dirinfo
; /* Information on RequestRoot dir */
903 * Create the job arrays as needed...
907 Jobs
= cupsArrayNew(compare_jobs
, NULL
);
910 ActiveJobs
= cupsArrayNew(compare_active_jobs
, NULL
);
913 * See whether the job.cache file is older than the RequestRoot directory...
916 snprintf(filename
, sizeof(filename
), "%s/job.cache", CacheDir
);
918 if (stat(filename
, &fileinfo
))
920 fileinfo
.st_mtime
= 0;
923 cupsdLogMessage(CUPSD_LOG_ERROR
,
924 "Unable to get file information for \"%s\" - %s",
925 filename
, strerror(errno
));
928 if (stat(RequestRoot
, &dirinfo
))
930 dirinfo
.st_mtime
= 0;
933 cupsdLogMessage(CUPSD_LOG_ERROR
,
934 "Unable to get directory information for \"%s\" - %s",
935 RequestRoot
, strerror(errno
));
939 * Load the most recent source for job data...
942 if (dirinfo
.st_mtime
> fileinfo
.st_mtime
)
946 load_next_job_id(filename
);
949 load_job_cache(filename
);
952 * Clean out old jobs as needed...
955 if (MaxJobs
> 0 && cupsArrayCount(Jobs
) >= MaxJobs
)
961 * 'cupsdLoadJob()' - Load a single job...
965 cupsdLoadJob(cupsd_job_t
*job
) /* I - Job */
967 char jobfile
[1024]; /* Job filename */
968 cups_file_t
*fp
; /* Job file */
969 int fileid
; /* Current file ID */
970 ipp_attribute_t
*attr
; /* Job attribute */
971 const char *dest
; /* Destination name */
972 cupsd_printer_t
*destptr
; /* Pointer to destination */
973 mime_type_t
**filetypes
; /* New filetypes array */
974 int *compressions
; /* New compressions array */
979 if (job
->state_value
> IPP_JOB_STOPPED
)
980 job
->access_time
= time(NULL
);
985 if ((job
->attrs
= ippNew()) == NULL
)
987 cupsdLogMessage(CUPSD_LOG_ERROR
,
988 "[Job %d] Ran out of memory for job attributes!", job
->id
);
993 * Load job attributes...
996 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Loading attributes...", job
->id
);
998 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, job
->id
);
999 if ((fp
= cupsFileOpen(jobfile
, "r")) == NULL
)
1001 cupsdLogMessage(CUPSD_LOG_ERROR
,
1002 "[Job %d] Unable to open job control file \"%s\" - %s!",
1003 job
->id
, jobfile
, strerror(errno
));
1004 ippDelete(job
->attrs
);
1009 if (ippReadIO(fp
, (ipp_iocb_t
)cupsFileRead
, 1, NULL
, job
->attrs
) != IPP_DATA
)
1011 cupsdLogMessage(CUPSD_LOG_ERROR
,
1012 "[Job %d] Unable to read job control file \"%s\"!",
1015 ippDelete(job
->attrs
);
1024 * Copy attribute data to the job object...
1027 if ((job
->state
= ippFindAttribute(job
->attrs
, "job-state",
1028 IPP_TAG_ENUM
)) == NULL
)
1030 cupsdLogMessage(CUPSD_LOG_ERROR
,
1031 "[Job %d] Missing or bad job-state attribute in "
1034 ippDelete(job
->attrs
);
1040 job
->state_value
= (ipp_jstate_t
)job
->state
->values
[0].integer
;
1044 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1045 IPP_TAG_URI
)) == NULL
)
1047 cupsdLogMessage(CUPSD_LOG_ERROR
,
1048 "[Job %d] No job-printer-uri attribute in control file!",
1050 ippDelete(job
->attrs
);
1056 if ((dest
= cupsdValidateDest(attr
->values
[0].string
.text
, &(job
->dtype
),
1059 cupsdLogMessage(CUPSD_LOG_ERROR
,
1060 "[Job %d] Unable to queue job for destination \"%s\"!",
1061 job
->id
, attr
->values
[0].string
.text
);
1062 ippDelete(job
->attrs
);
1068 cupsdSetString(&job
->dest
, dest
);
1070 else if ((destptr
= cupsdFindDest(job
->dest
)) == NULL
)
1072 cupsdLogMessage(CUPSD_LOG_ERROR
,
1073 "[Job %d] Unable to queue job for destination \"%s\"!",
1074 job
->id
, job
->dest
);
1075 ippDelete(job
->attrs
);
1081 job
->sheets
= ippFindAttribute(job
->attrs
, "job-media-sheets-completed",
1083 job
->job_sheets
= ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_NAME
);
1087 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
1088 IPP_TAG_INTEGER
)) == NULL
)
1090 cupsdLogMessage(CUPSD_LOG_ERROR
,
1091 "[Job %d] Missing or bad job-priority attribute in "
1092 "control file!", job
->id
);
1093 ippDelete(job
->attrs
);
1099 job
->priority
= attr
->values
[0].integer
;
1104 if ((attr
= ippFindAttribute(job
->attrs
, "job-originating-user-name",
1105 IPP_TAG_NAME
)) == NULL
)
1107 cupsdLogMessage(CUPSD_LOG_ERROR
,
1108 "[Job %d] Missing or bad job-originating-user-name "
1109 "attribute in control file!", job
->id
);
1110 ippDelete(job
->attrs
);
1116 cupsdSetString(&job
->username
, attr
->values
[0].string
.text
);
1120 * Set the job hold-until time and state...
1123 if (job
->state_value
== IPP_JOB_HELD
)
1125 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1126 IPP_TAG_KEYWORD
)) == NULL
)
1127 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1130 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
1133 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1134 job
->state_value
= IPP_JOB_PENDING
;
1137 else if (job
->state_value
== IPP_JOB_PROCESSING
)
1139 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1140 job
->state_value
= IPP_JOB_PENDING
;
1143 if (!job
->num_files
)
1146 * Find all the d##### files...
1149 for (fileid
= 1; fileid
< 10000; fileid
++)
1151 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
1154 if (access(jobfile
, 0))
1157 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1158 "[Job %d] Auto-typing document file \"%s\"...",
1161 if (fileid
> job
->num_files
)
1163 if (job
->num_files
== 0)
1165 compressions
= (int *)calloc(fileid
, sizeof(int));
1166 filetypes
= (mime_type_t
**)calloc(fileid
, sizeof(mime_type_t
*));
1170 compressions
= (int *)realloc(job
->compressions
,
1171 sizeof(int) * fileid
);
1172 filetypes
= (mime_type_t
**)realloc(job
->filetypes
,
1173 sizeof(mime_type_t
*) *
1177 if (!compressions
|| !filetypes
)
1179 cupsdLogMessage(CUPSD_LOG_ERROR
,
1180 "[Job %d] Ran out of memory for job file types!",
1185 job
->compressions
= compressions
;
1186 job
->filetypes
= filetypes
;
1187 job
->num_files
= fileid
;
1190 job
->filetypes
[fileid
- 1] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
1191 job
->compressions
+ fileid
- 1);
1193 if (!job
->filetypes
[fileid
- 1])
1194 job
->filetypes
[fileid
- 1] = mimeType(MimeDatabase
, "application",
1200 * Load authentication information as needed...
1203 if (job
->state_value
< IPP_JOB_STOPPED
)
1205 snprintf(jobfile
, sizeof(jobfile
), "%s/a%05d", RequestRoot
, job
->id
);
1207 cupsdClearString(&job
->auth_username
);
1208 cupsdClearString(&job
->auth_domain
);
1209 cupsdClearString(&job
->auth_password
);
1211 if ((fp
= cupsFileOpen(jobfile
, "r")) != NULL
)
1213 int i
, /* Looping var */
1214 bytes
; /* Size of auth data */
1215 char line
[255], /* Line from file */
1216 data
[255]; /* Decoded data */
1220 i
< destptr
->num_auth_info_required
&&
1221 cupsFileGets(fp
, line
, sizeof(line
));
1224 bytes
= sizeof(data
);
1225 httpDecode64_2(data
, &bytes
, line
);
1227 if (!strcmp(destptr
->auth_info_required
[i
], "username"))
1228 cupsdSetStringf(&job
->auth_username
, "AUTH_USERNAME=%s", data
);
1229 else if (!strcmp(destptr
->auth_info_required
[i
], "domain"))
1230 cupsdSetStringf(&job
->auth_domain
, "AUTH_DOMAIN=%s", data
);
1231 else if (!strcmp(destptr
->auth_info_required
[i
], "password"))
1232 cupsdSetStringf(&job
->auth_password
, "AUTH_PASSWORD=%s", data
);
1238 job
->access_time
= time(NULL
);
1243 * 'cupsdMoveJob()' - Move the specified job to a different destination.
1247 cupsdMoveJob(cupsd_job_t
*job
, /* I - Job */
1248 cupsd_printer_t
*p
) /* I - Destination printer or class */
1250 ipp_attribute_t
*attr
; /* job-printer-uri attribute */
1251 const char *olddest
; /* Old destination */
1252 cupsd_printer_t
*oldp
; /* Old pointer */
1256 * Don't move completed jobs...
1259 if (job
->state_value
> IPP_JOB_STOPPED
)
1263 * Get the old destination...
1266 olddest
= job
->dest
;
1269 oldp
= job
->printer
;
1271 oldp
= cupsdFindDest(olddest
);
1274 * Change the destination information...
1277 if (job
->state_value
== IPP_JOB_PROCESSING
)
1278 cupsdStopJob(job
, 0);
1282 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, oldp
, job
,
1283 "Job #%d moved from %s to %s.", job
->id
, olddest
,
1286 cupsdSetString(&job
->dest
, p
->name
);
1287 job
->dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_REMOTE
|
1288 CUPS_PRINTER_IMPLICIT
);
1290 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1291 IPP_TAG_URI
)) != NULL
)
1292 cupsdSetString(&(attr
->values
[0].string
.text
), p
->uri
);
1294 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, p
, job
,
1295 "Job #%d moved from %s to %s.", job
->id
, olddest
,
1303 * 'cupsdReleaseJob()' - Release the specified job.
1307 cupsdReleaseJob(cupsd_job_t
*job
) /* I - Job */
1309 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdReleaseJob: id = %d", job
->id
);
1311 if (job
->state_value
== IPP_JOB_HELD
)
1313 DEBUG_puts("cupsdReleaseJob: setting state to pending...");
1315 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1316 job
->state_value
= IPP_JOB_PENDING
;
1324 * 'cupsdRestartJob()' - Restart the specified job.
1328 cupsdRestartJob(cupsd_job_t
*job
) /* I - Job */
1330 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdRestartJob: id = %d", job
->id
);
1332 if (job
->state_value
== IPP_JOB_STOPPED
|| job
->num_files
)
1334 ipp_jstate_t old_state
; /* Old job state */
1339 old_state
= job
->state_value
;
1342 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1343 job
->state_value
= IPP_JOB_PENDING
;
1347 if (old_state
> IPP_JOB_STOPPED
)
1348 cupsArrayAdd(ActiveJobs
, job
);
1356 * 'cupsdSaveAllJobs()' - Save a summary of all jobs to disk.
1360 cupsdSaveAllJobs(void)
1362 int i
; /* Looping var */
1363 cups_file_t
*fp
; /* Job cache file */
1364 char temp
[1024]; /* Temporary string */
1365 cupsd_job_t
*job
; /* Current job */
1366 time_t curtime
; /* Current time */
1367 struct tm
*curdate
; /* Current date */
1370 snprintf(temp
, sizeof(temp
), "%s/job.cache", CacheDir
);
1371 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
1373 cupsdLogMessage(CUPSD_LOG_ERROR
,
1374 "Unable to create job cache file \"%s\" - %s",
1375 temp
, strerror(errno
));
1379 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving job cache file \"%s\"...", temp
);
1382 * Restrict access to the file...
1385 fchown(cupsFileNumber(fp
), getuid(), Group
);
1386 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
1389 * Write a small header to the file...
1392 curtime
= time(NULL
);
1393 curdate
= localtime(&curtime
);
1394 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
1396 cupsFilePuts(fp
, "# Job cache file for " CUPS_SVERSION
"\n");
1397 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
1398 cupsFilePrintf(fp
, "NextJobId %d\n", NextJobId
);
1401 * Write each job known to the system...
1404 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1406 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1408 cupsFilePrintf(fp
, "<Job %d>\n", job
->id
);
1409 cupsFilePrintf(fp
, "State %d\n", job
->state_value
);
1410 cupsFilePrintf(fp
, "Priority %d\n", job
->priority
);
1411 cupsFilePrintf(fp
, "Username %s\n", job
->username
);
1412 cupsFilePrintf(fp
, "Destination %s\n", job
->dest
);
1413 cupsFilePrintf(fp
, "DestType %d\n", job
->dtype
);
1414 cupsFilePrintf(fp
, "NumFiles %d\n", job
->num_files
);
1415 for (i
= 0; i
< job
->num_files
; i
++)
1416 cupsFilePrintf(fp
, "File %d %s/%s %d\n", i
+ 1, job
->filetypes
[i
]->super
,
1417 job
->filetypes
[i
]->type
, job
->compressions
[i
]);
1418 cupsFilePuts(fp
, "</Job>\n");
1426 * 'cupsdSaveJob()' - Save a job to disk.
1430 cupsdSaveJob(cupsd_job_t
*job
) /* I - Job */
1432 char filename
[1024]; /* Job control filename */
1433 cups_file_t
*fp
; /* Job file */
1436 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSaveJob(job=%p(%d)): job->attrs=%p",
1437 job
, job
->id
, job
->attrs
);
1439 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
, job
->id
);
1441 if ((fp
= cupsFileOpen(filename
, "w")) == NULL
)
1443 cupsdLogMessage(CUPSD_LOG_ERROR
,
1444 "[Job %d] Unable to create job control file \"%s\" - %s.",
1445 job
->id
, filename
, strerror(errno
));
1449 fchmod(cupsFileNumber(fp
), 0600);
1450 fchown(cupsFileNumber(fp
), RunUser
, Group
);
1452 job
->attrs
->state
= IPP_IDLE
;
1454 if (ippWriteIO(fp
, (ipp_iocb_t
)cupsFileWrite
, 1, NULL
,
1455 job
->attrs
) != IPP_DATA
)
1456 cupsdLogMessage(CUPSD_LOG_ERROR
,
1457 "[Job %d] Unable to write job control file!", job
->id
);
1464 * 'cupsdSetJobHoldUntil()' - Set the hold time for a job...
1468 cupsdSetJobHoldUntil(cupsd_job_t
*job
, /* I - Job */
1469 const char *when
) /* I - When to resume */
1471 time_t curtime
; /* Current time */
1472 struct tm
*curdate
; /* Current date */
1473 int hour
; /* Hold hour */
1474 int minute
; /* Hold minute */
1475 int second
; /* Hold second */
1478 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetJobHoldUntil(%d, \"%s\")",
1483 if (!strcmp(when
, "indefinite") || !strcmp(when
, "auth-info-required"))
1486 * Hold indefinitely...
1489 job
->hold_until
= 0;
1491 else if (!strcmp(when
, "day-time"))
1494 * Hold to 6am the next morning unless local time is < 6pm.
1497 curtime
= time(NULL
);
1498 curdate
= localtime(&curtime
);
1500 if (curdate
->tm_hour
< 18)
1501 job
->hold_until
= curtime
;
1503 job
->hold_until
= curtime
+
1504 ((29 - curdate
->tm_hour
) * 60 + 59 -
1505 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1507 else if (!strcmp(when
, "evening") || !strcmp(when
, "night"))
1510 * Hold to 6pm unless local time is > 6pm or < 6am.
1513 curtime
= time(NULL
);
1514 curdate
= localtime(&curtime
);
1516 if (curdate
->tm_hour
< 6 || curdate
->tm_hour
>= 18)
1517 job
->hold_until
= curtime
;
1519 job
->hold_until
= curtime
+
1520 ((17 - curdate
->tm_hour
) * 60 + 59 -
1521 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1523 else if (!strcmp(when
, "second-shift"))
1526 * Hold to 4pm unless local time is > 4pm.
1529 curtime
= time(NULL
);
1530 curdate
= localtime(&curtime
);
1532 if (curdate
->tm_hour
>= 16)
1533 job
->hold_until
= curtime
;
1535 job
->hold_until
= curtime
+
1536 ((15 - curdate
->tm_hour
) * 60 + 59 -
1537 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1539 else if (!strcmp(when
, "third-shift"))
1542 * Hold to 12am unless local time is < 8am.
1545 curtime
= time(NULL
);
1546 curdate
= localtime(&curtime
);
1548 if (curdate
->tm_hour
< 8)
1549 job
->hold_until
= curtime
;
1551 job
->hold_until
= curtime
+
1552 ((23 - curdate
->tm_hour
) * 60 + 59 -
1553 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1555 else if (!strcmp(when
, "weekend"))
1558 * Hold to weekend unless we are in the weekend.
1561 curtime
= time(NULL
);
1562 curdate
= localtime(&curtime
);
1564 if (curdate
->tm_wday
|| curdate
->tm_wday
== 6)
1565 job
->hold_until
= curtime
;
1567 job
->hold_until
= curtime
+
1568 (((5 - curdate
->tm_wday
) * 24 +
1569 (17 - curdate
->tm_hour
)) * 60 + 59 -
1570 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1572 else if (sscanf(when
, "%d:%d:%d", &hour
, &minute
, &second
) >= 2)
1575 * Hold to specified GMT time (HH:MM or HH:MM:SS)...
1578 curtime
= time(NULL
);
1579 curdate
= gmtime(&curtime
);
1581 job
->hold_until
= curtime
+
1582 ((hour
- curdate
->tm_hour
) * 60 + minute
-
1583 curdate
->tm_min
) * 60 + second
- curdate
->tm_sec
;
1586 * Hold until next day as needed...
1589 if (job
->hold_until
< curtime
)
1590 job
->hold_until
+= 24 * 60 * 60;
1593 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetJobHoldUntil: hold_until = %d",
1594 (int)job
->hold_until
);
1599 * 'cupsdSetJobPriority()' - Set the priority of a job, moving it up/down in
1600 * the list as needed.
1604 cupsdSetJobPriority(
1605 cupsd_job_t
*job
, /* I - Job ID */
1606 int priority
) /* I - New priority (0 to 100) */
1608 ipp_attribute_t
*attr
; /* Job attribute */
1612 * Don't change completed jobs...
1615 if (job
->state_value
>= IPP_JOB_PROCESSING
)
1619 * Set the new priority and re-add the job into the active list...
1622 cupsArrayRemove(ActiveJobs
, job
);
1624 job
->priority
= priority
;
1626 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
1627 IPP_TAG_INTEGER
)) != NULL
)
1628 attr
->values
[0].integer
= priority
;
1630 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-priority",
1633 cupsArrayAdd(ActiveJobs
, job
);
1640 * 'cupsdStopAllJobs()' - Stop all print jobs.
1644 cupsdStopAllJobs(int force
) /* I - 1 = Force all filters to stop */
1646 cupsd_job_t
*job
; /* Current job */
1649 DEBUG_puts("cupsdStopAllJobs()");
1651 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
1653 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
1654 if (job
->state_value
== IPP_JOB_PROCESSING
)
1656 cupsdStopJob(job
, force
);
1657 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1658 job
->state_value
= IPP_JOB_PENDING
;
1664 * 'cupsdStopJob()' - Stop a print job.
1668 cupsdStopJob(cupsd_job_t
*job
, /* I - Job */
1669 int force
) /* I - 1 = Force all filters to stop */
1671 int i
; /* Looping var */
1674 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1675 "[Job %d] cupsdStopJob: force = %d", job
->id
, force
);
1677 if (job
->state_value
!= IPP_JOB_PROCESSING
)
1680 FilterLevel
-= job
->cost
;
1682 if (job
->printer
->state
== IPP_PRINTER_PROCESSING
)
1683 cupsdSetPrinterState(job
->printer
, IPP_PRINTER_IDLE
, 0);
1685 job
->state
->values
[0].integer
= IPP_JOB_STOPPED
;
1686 job
->state_value
= IPP_JOB_STOPPED
;
1687 job
->printer
->job
= NULL
;
1688 job
->printer
= NULL
;
1690 job
->current_file
--;
1692 for (i
= 0; job
->filters
[i
]; i
++)
1693 if (job
->filters
[i
] > 0)
1695 cupsdEndProcess(job
->filters
[i
], force
);
1696 job
->filters
[i
] = 0;
1699 if (job
->backend
> 0)
1701 cupsdEndProcess(job
->backend
, force
);
1705 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] Closing print pipes [ %d %d ]...",
1706 job
->id
, job
->print_pipes
[0], job
->print_pipes
[1]);
1708 cupsdClosePipe(job
->print_pipes
);
1710 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] Closing back pipes [ %d %d ]...",
1711 job
->id
, job
->back_pipes
[0], job
->back_pipes
[1]);
1713 cupsdClosePipe(job
->back_pipes
);
1715 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] Closing side pipes [ %d %d ]...",
1716 job
->id
, job
->side_pipes
[0], job
->side_pipes
[1]);
1718 cupsdClosePipe(job
->side_pipes
);
1720 if (job
->status_buffer
)
1723 * Close the pipe and clear the input bit.
1726 cupsdRemoveSelect(job
->status_buffer
->fd
);
1728 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1729 "[Job %d] Closing status pipes [ %d %d ]...", job
->id
,
1730 job
->status_pipes
[0], job
->status_pipes
[1]);
1732 cupsdClosePipe(job
->status_pipes
);
1733 cupsdStatBufDelete(job
->status_buffer
);
1735 job
->status_buffer
= NULL
;
1741 * 'cupsdUnloadCompletedJobs()' - Flush completed job history from memory.
1745 cupsdUnloadCompletedJobs(void)
1747 cupsd_job_t
*job
; /* Current job */
1748 time_t expire
; /* Expiration time */
1751 expire
= time(NULL
) - 60;
1753 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1755 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1756 if (job
->attrs
&& job
->state_value
>= IPP_JOB_STOPPED
&&
1757 job
->access_time
< expire
)
1763 * 'compare_active_jobs()' - Compare the job IDs and priorities of two jobs.
1766 static int /* O - Difference */
1767 compare_active_jobs(void *first
, /* I - First job */
1768 void *second
, /* I - Second job */
1769 void *data
) /* I - App data (not used) */
1771 int diff
; /* Difference */
1774 if ((diff
= ((cupsd_job_t
*)second
)->priority
-
1775 ((cupsd_job_t
*)first
)->priority
) != 0)
1778 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
1783 * 'compare_jobs()' - Compare the job IDs of two jobs.
1786 static int /* O - Difference */
1787 compare_jobs(void *first
, /* I - First job */
1788 void *second
, /* I - Second job */
1789 void *data
) /* I - App data (not used) */
1791 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
1796 * 'free_job()' - Free all memory used by a job.
1800 free_job(cupsd_job_t
*job
) /* I - Job */
1802 cupsdClearString(&job
->username
);
1803 cupsdClearString(&job
->dest
);
1804 cupsdClearString(&job
->auth_username
);
1805 cupsdClearString(&job
->auth_domain
);
1806 cupsdClearString(&job
->auth_password
);
1809 * Destroy the credential cache and clear the KRB5CCNAME env var string.
1814 krb5_cc_destroy(KerberosContext
, job
->ccache
);
1818 cupsdClearString(&job
->ccname
);
1819 #endif /* HAVE_GSSAPI */
1821 if (job
->num_files
> 0)
1823 free(job
->compressions
);
1824 free(job
->filetypes
);
1827 ippDelete(job
->attrs
);
1834 * 'ipp_length()' - Compute the size of the buffer needed to hold
1835 * the textual IPP attributes.
1838 static int /* O - Size of attribute buffer */
1839 ipp_length(ipp_t
*ipp
) /* I - IPP request */
1841 int bytes
; /* Number of bytes */
1842 int i
; /* Looping var */
1843 ipp_attribute_t
*attr
; /* Current attribute */
1847 * Loop through all attributes...
1852 for (attr
= ipp
->attrs
; attr
!= NULL
; attr
= attr
->next
)
1855 * Skip attributes that won't be sent to filters...
1858 if (attr
->value_tag
== IPP_TAG_MIMETYPE
||
1859 attr
->value_tag
== IPP_TAG_NAMELANG
||
1860 attr
->value_tag
== IPP_TAG_TEXTLANG
||
1861 attr
->value_tag
== IPP_TAG_URI
||
1862 attr
->value_tag
== IPP_TAG_URISCHEME
)
1865 if (strncmp(attr
->name
, "time-", 5) == 0)
1869 * Add space for a leading space and commas between each value.
1870 * For the first attribute, the leading space isn't used, so the
1871 * extra byte can be used as the nul terminator...
1874 bytes
++; /* " " separator */
1875 bytes
+= attr
->num_values
; /* "," separators */
1878 * Boolean attributes appear as "foo,nofoo,foo,nofoo", while
1879 * other attributes appear as "foo=value1,value2,...,valueN".
1882 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
1883 bytes
+= strlen(attr
->name
);
1885 bytes
+= attr
->num_values
* strlen(attr
->name
);
1888 * Now add the size required for each value in the attribute...
1891 switch (attr
->value_tag
)
1893 case IPP_TAG_INTEGER
:
1896 * Minimum value of a signed integer is -2147483647, or 11 digits.
1899 bytes
+= attr
->num_values
* 11;
1902 case IPP_TAG_BOOLEAN
:
1904 * Add two bytes for each false ("no") value...
1907 for (i
= 0; i
< attr
->num_values
; i
++)
1908 if (!attr
->values
[i
].boolean
)
1912 case IPP_TAG_RANGE
:
1914 * A range is two signed integers separated by a hyphen, or
1915 * 23 characters max.
1918 bytes
+= attr
->num_values
* 23;
1921 case IPP_TAG_RESOLUTION
:
1923 * A resolution is two signed integers separated by an "x" and
1924 * suffixed by the units, or 26 characters max.
1927 bytes
+= attr
->num_values
* 26;
1930 case IPP_TAG_STRING
:
1933 case IPP_TAG_KEYWORD
:
1934 case IPP_TAG_CHARSET
:
1935 case IPP_TAG_LANGUAGE
:
1938 * Strings can contain characters that need quoting. We need
1939 * at least 2 * len + 2 characters to cover the quotes and
1940 * any backslashes in the string.
1943 for (i
= 0; i
< attr
->num_values
; i
++)
1944 bytes
+= 2 * strlen(attr
->values
[i
].string
.text
) + 2;
1948 break; /* anti-compiler-warning-code */
1957 * 'load_job_cache()' - Load jobs from the job.cache file.
1961 load_job_cache(const char *filename
) /* I - job.cache filename */
1963 cups_file_t
*fp
; /* job.cache file */
1964 char line
[1024], /* Line buffer */
1965 *value
; /* Value on line */
1966 int linenum
; /* Line number in file */
1967 cupsd_job_t
*job
; /* Current job */
1968 int jobid
; /* Job ID */
1969 char jobfile
[1024]; /* Job filename */
1973 * Open the job.cache file...
1976 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
1978 if (errno
!= ENOENT
)
1979 cupsdLogMessage(CUPSD_LOG_ERROR
,
1980 "Unable to open job cache file \"%s\": %s",
1981 filename
, strerror(errno
));
1983 load_request_root();
1989 * Read entries from the job cache file and create jobs as needed.
1992 cupsdLogMessage(CUPSD_LOG_INFO
, "Loading job cache file \"%s\"...",
1998 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
2000 if (!strcasecmp(line
, "NextJobId"))
2003 NextJobId
= atoi(value
);
2005 else if (!strcasecmp(line
, "<Job"))
2009 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing </Job> directive on line %d!",
2016 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing job ID on line %d!", linenum
);
2020 jobid
= atoi(value
);
2024 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad job ID %d on line %d!", jobid
,
2029 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, jobid
);
2030 if (access(jobfile
, 0))
2032 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Job %d] Files have gone away!",
2037 job
= calloc(1, sizeof(cupsd_job_t
));
2040 cupsdLogMessage(CUPSD_LOG_EMERG
,
2041 "[Job %d] Unable to allocate memory for job!", jobid
);
2046 job
->back_pipes
[0] = -1;
2047 job
->back_pipes
[1] = -1;
2048 job
->print_pipes
[0] = -1;
2049 job
->print_pipes
[1] = -1;
2050 job
->side_pipes
[0] = -1;
2051 job
->side_pipes
[1] = -1;
2052 job
->status_pipes
[0] = -1;
2053 job
->status_pipes
[1] = -1;
2055 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Loading from cache...", job
->id
);
2059 cupsdLogMessage(CUPSD_LOG_ERROR
,
2060 "Missing <Job #> directive on line %d!", linenum
);
2063 else if (!strcasecmp(line
, "</Job>"))
2065 cupsArrayAdd(Jobs
, job
);
2067 if (job
->state_value
<= IPP_JOB_STOPPED
)
2069 cupsArrayAdd(ActiveJobs
, job
);
2077 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing value on line %d!", linenum
);
2080 else if (!strcasecmp(line
, "State"))
2082 job
->state_value
= (ipp_jstate_t
)atoi(value
);
2084 if (job
->state_value
< IPP_JOB_PENDING
)
2085 job
->state_value
= IPP_JOB_PENDING
;
2086 else if (job
->state_value
> IPP_JOB_COMPLETED
)
2087 job
->state_value
= IPP_JOB_COMPLETED
;
2089 else if (!strcasecmp(line
, "Priority"))
2091 job
->priority
= atoi(value
);
2093 else if (!strcasecmp(line
, "Username"))
2095 cupsdSetString(&job
->username
, value
);
2097 else if (!strcasecmp(line
, "Destination"))
2099 cupsdSetString(&job
->dest
, value
);
2101 else if (!strcasecmp(line
, "DestType"))
2103 job
->dtype
= (cups_ptype_t
)atoi(value
);
2105 else if (!strcasecmp(line
, "NumFiles"))
2107 job
->num_files
= atoi(value
);
2109 if (job
->num_files
< 0)
2111 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad NumFiles value %d on line %d!",
2112 job
->num_files
, linenum
);
2117 if (job
->num_files
> 0)
2119 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-001", RequestRoot
,
2121 if (access(jobfile
, 0))
2123 cupsdLogMessage(CUPSD_LOG_INFO
,
2124 "[Job %d] Data files have gone away!", job
->id
);
2129 job
->filetypes
= calloc(job
->num_files
, sizeof(mime_type_t
*));
2130 job
->compressions
= calloc(job
->num_files
, sizeof(int));
2132 if (!job
->filetypes
|| !job
->compressions
)
2134 cupsdLogMessage(CUPSD_LOG_EMERG
,
2135 "[Job %d] Unable to allocate memory for %d files!",
2136 job
->id
, job
->num_files
);
2141 else if (!strcasecmp(line
, "File"))
2143 int number
, /* File number */
2144 compression
; /* Compression value */
2145 char super
[MIME_MAX_SUPER
], /* MIME super type */
2146 type
[MIME_MAX_TYPE
]; /* MIME type */
2149 if (sscanf(value
, "%d%*[ \t]%15[^/]/%255s%d", &number
, super
, type
,
2152 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File on line %d!", linenum
);
2156 if (number
< 1 || number
> job
->num_files
)
2158 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File number %d on line %d!",
2165 job
->compressions
[number
] = compression
;
2166 job
->filetypes
[number
] = mimeType(MimeDatabase
, super
, type
);
2168 if (!job
->filetypes
[number
])
2171 * If the original MIME type is unknown, auto-type it!
2174 cupsdLogMessage(CUPSD_LOG_ERROR
,
2175 "[Job %d] Unknown MIME type %s/%s for file %d!",
2176 job
->id
, super
, type
, number
+ 1);
2178 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
2179 job
->id
, number
+ 1);
2180 job
->filetypes
[number
] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
2181 job
->compressions
+ number
);
2184 * If that didn't work, assume it is raw...
2187 if (!job
->filetypes
[number
])
2188 job
->filetypes
[number
] = mimeType(MimeDatabase
, "application",
2193 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unknown %s directive on line %d!",
2202 * 'load_next_job_id()' - Load the NextJobId value from the job.cache file.
2206 load_next_job_id(const char *filename
) /* I - job.cache filename */
2208 cups_file_t
*fp
; /* job.cache file */
2209 char line
[1024], /* Line buffer */
2210 *value
; /* Value on line */
2211 int linenum
; /* Line number in file */
2212 int next_job_id
; /* NextJobId value from line */
2216 * Read the NextJobId directive from the job.cache file and use
2217 * the value (if any).
2220 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
2222 if (errno
!= ENOENT
)
2223 cupsdLogMessage(CUPSD_LOG_ERROR
,
2224 "Unable to open job cache file \"%s\": %s",
2225 filename
, strerror(errno
));
2230 cupsdLogMessage(CUPSD_LOG_INFO
,
2231 "Loading NextJobId from job cache file \"%s\"...", filename
);
2235 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
2237 if (!strcasecmp(line
, "NextJobId"))
2241 next_job_id
= atoi(value
);
2243 if (next_job_id
> NextJobId
)
2244 NextJobId
= next_job_id
;
2255 * 'load_request_root()' - Load jobs from the RequestRoot directory.
2259 load_request_root(void)
2261 cups_dir_t
*dir
; /* Directory */
2262 cups_dentry_t
*dent
; /* Directory entry */
2263 cupsd_job_t
*job
; /* New job */
2267 * Open the requests directory...
2270 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Scanning %s for jobs...", RequestRoot
);
2272 if ((dir
= cupsDirOpen(RequestRoot
)) == NULL
)
2274 cupsdLogMessage(CUPSD_LOG_ERROR
,
2275 "Unable to open spool directory \"%s\": %s",
2276 RequestRoot
, strerror(errno
));
2281 * Read all the c##### files...
2284 while ((dent
= cupsDirRead(dir
)) != NULL
)
2285 if (strlen(dent
->filename
) >= 6 && dent
->filename
[0] == 'c')
2288 * Allocate memory for the job...
2291 if ((job
= calloc(sizeof(cupsd_job_t
), 1)) == NULL
)
2293 cupsdLogMessage(CUPSD_LOG_ERROR
, "Ran out of memory for jobs!");
2299 * Assign the job ID...
2302 job
->id
= atoi(dent
->filename
+ 1);
2303 job
->back_pipes
[0] = -1;
2304 job
->back_pipes
[1] = -1;
2305 job
->print_pipes
[0] = -1;
2306 job
->print_pipes
[1] = -1;
2307 job
->side_pipes
[0] = -1;
2308 job
->side_pipes
[1] = -1;
2309 job
->status_pipes
[0] = -1;
2310 job
->status_pipes
[1] = -1;
2312 if (job
->id
>= NextJobId
)
2313 NextJobId
= job
->id
+ 1;
2322 * Insert the job into the array, sorting by job priority and ID...
2325 cupsArrayAdd(Jobs
, job
);
2327 if (job
->state_value
<= IPP_JOB_STOPPED
)
2328 cupsArrayAdd(ActiveJobs
, job
);
2338 * 'set_time()' - Set one of the "time-at-xyz" attributes...
2342 set_time(cupsd_job_t
*job
, /* I - Job to update */
2343 const char *name
) /* I - Name of attribute */
2345 ipp_attribute_t
*attr
; /* Time attribute */
2348 if ((attr
= ippFindAttribute(job
->attrs
, name
, IPP_TAG_ZERO
)) != NULL
)
2350 attr
->value_tag
= IPP_TAG_INTEGER
;
2351 attr
->values
[0].integer
= time(NULL
);
2357 * 'set_hold_until()' - Set the hold time and update job-hold-until attribute...
2361 set_hold_until(cupsd_job_t
*job
, /* I - Job to update */
2362 time_t holdtime
) /* I - Hold until time */
2364 ipp_attribute_t
*attr
; /* job-hold-until attribute */
2365 struct tm
*holddate
; /* Hold date */
2366 char holdstr
[64]; /* Hold time */
2370 * Set the hold_until value and hold the job...
2373 cupsdLogMessage(CUPSD_LOG_DEBUG
, "set_hold_until: hold_until = %d",
2376 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
2377 job
->state_value
= IPP_JOB_HELD
;
2378 job
->hold_until
= holdtime
;
2381 * Update the job-hold-until attribute with a string representing GMT
2382 * time (HH:MM:SS)...
2385 holddate
= gmtime(&holdtime
);
2386 snprintf(holdstr
, sizeof(holdstr
), "%d:%d:%d", holddate
->tm_hour
,
2387 holddate
->tm_min
, holddate
->tm_sec
);
2389 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2390 IPP_TAG_KEYWORD
)) == NULL
)
2391 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2394 * Either add the attribute or update the value of the existing one
2398 attr
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
2399 "job-hold-until", NULL
, holdstr
);
2401 cupsdSetString(&attr
->values
[0].string
.text
, holdstr
);
2408 * 'start_job()' - Start a print job.
2412 start_job(cupsd_job_t
*job
, /* I - Job ID */
2413 cupsd_printer_t
*printer
) /* I - Printer to print job */
2415 int i
; /* Looping var */
2416 int slot
; /* Pipe slot */
2417 cups_array_t
*filters
, /* Filters for job */
2418 *prefilters
; /* Filters with prefilters */
2419 mime_filter_t
*filter
, /* Current filter */
2420 *prefilter
, /* Prefilter */
2421 port_monitor
; /* Port monitor filter */
2422 char method
[255], /* Method for output */
2423 *optptr
, /* Pointer to options */
2424 *valptr
; /* Pointer in value string */
2425 ipp_attribute_t
*attr
; /* Current attribute */
2426 struct stat backinfo
; /* Backend file information */
2427 int backroot
; /* Run backend as root? */
2428 int pid
; /* Process ID of new filter process */
2429 int banner_page
; /* 1 if banner page, 0 otherwise */
2430 int filterfds
[2][2];/* Pipes used between filters */
2431 int envc
; /* Number of environment variables */
2432 char **argv
, /* Filter command-line arguments */
2433 sani_uri
[1024], /* Sanitized DEVICE_URI env var */
2434 filename
[1024], /* Job filename */
2435 command
[1024], /* Full path to command */
2436 jobid
[255], /* Job ID string */
2437 title
[IPP_MAX_NAME
],
2438 /* Job title string */
2439 copies
[255], /* # copies string */
2440 *envp
[MAX_ENV
+ 15],
2441 /* Environment variables */
2442 charset
[255], /* CHARSET env variable */
2443 class_name
[255],/* CLASS env variable */
2444 classification
[1024],
2445 /* CLASSIFICATION env variable */
2447 /* CONTENT_TYPE env variable */
2449 /* DEVICE_URI env variable */
2450 final_content_type
[1024],
2451 /* FINAL_CONTENT_TYPE env variable */
2452 lang
[255], /* LANG env variable */
2453 ppd
[1024], /* PPD env variable */
2455 /* PRINTER env variable */
2457 /* RIP_MAX_CACHE env variable */
2458 static char *options
= NULL
;/* Full list of options */
2459 static int optlength
= 0; /* Length of option buffer */
2462 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] start_job: file = %d/%d",
2463 job
->id
, job
->current_file
, job
->num_files
);
2465 if (job
->num_files
== 0)
2467 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Job %d] No files, canceling job!",
2470 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2475 * Figure out what filters are required to convert from
2476 * the source to the destination type...
2485 * Remote jobs and raw queues go directly to the printer without
2489 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2490 "[Job %d] Sending job to queue tagged as raw...", job
->id
);
2497 * Local jobs get filtered...
2500 filters
= mimeFilter(MimeDatabase
, job
->filetypes
[job
->current_file
],
2501 printer
->filetype
, &(job
->cost
));
2505 cupsdLogMessage(CUPSD_LOG_ERROR
,
2506 "[Job %d] Unable to convert file %d to printable format!",
2507 job
->current_file
, job
->id
);
2508 cupsdLogMessage(CUPSD_LOG_INFO
,
2509 "Hint: Do you have ESP Ghostscript installed?");
2511 if (LogLevel
< CUPSD_LOG_DEBUG
)
2512 cupsdLogMessage(CUPSD_LOG_INFO
,
2513 "Hint: Try setting the LogLevel to \"debug\".");
2515 job
->current_file
++;
2517 if (job
->current_file
== job
->num_files
)
2518 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2524 * Remove NULL ("-") filters...
2527 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
2529 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
2530 if (!strcmp(filter
->filter
, "-"))
2531 cupsArrayRemove(filters
, filter
);
2533 if (cupsArrayCount(filters
) == 0)
2535 cupsArrayDelete(filters
);
2540 * If this printer has any pre-filters, insert the required pre-filter
2541 * in the filters array...
2544 if (printer
->prefiltertype
&& filters
)
2546 prefilters
= cupsArrayNew(NULL
, NULL
);
2548 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
2550 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
2552 if ((prefilter
= mimeFilterLookup(MimeDatabase
, filter
->src
,
2553 printer
->prefiltertype
)))
2555 cupsArrayAdd(prefilters
, prefilter
);
2556 job
->cost
+= prefilter
->cost
;
2559 cupsArrayAdd(prefilters
, filter
);
2562 cupsArrayDelete(filters
);
2563 filters
= prefilters
;
2568 * Set a minimum cost of 100 for all jobs so that FilterLimit
2569 * works with raw queues and other low-cost paths.
2572 if (job
->cost
< 100)
2576 * See if the filter cost is too high...
2579 if ((FilterLevel
+ job
->cost
) > FilterLimit
&& FilterLevel
> 0 &&
2583 * Don't print this job quite yet...
2586 cupsArrayDelete(filters
);
2588 cupsdLogMessage(CUPSD_LOG_INFO
,
2589 "[Job %d] Holding because filter limit has been reached.",
2591 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2592 "[Job %d] start_job: file=%d, cost=%d, level=%d, limit=%d",
2593 job
->id
, job
->current_file
, job
->cost
, FilterLevel
,
2598 FilterLevel
+= job
->cost
;
2601 * Add decompression/raw filter as needed...
2604 if ((!printer
->raw
&& job
->compressions
[job
->current_file
]) ||
2605 (!filters
&& !printer
->remote
&&
2606 (job
->num_files
> 1 || !strncmp(printer
->device_uri
, "file:", 5))))
2609 * Add gziptoany filter to the front of the list...
2613 filters
= cupsArrayNew(NULL
, NULL
);
2615 if (!cupsArrayInsert(filters
, &gziptoany_filter
))
2617 cupsdLogMessage(CUPSD_LOG_ERROR
,
2618 "[Job %d] Unable to add decompression filter - %s",
2619 job
->id
, strerror(errno
));
2621 cupsArrayDelete(filters
);
2623 job
->current_file
++;
2625 if (job
->current_file
== job
->num_files
)
2626 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2633 * Add port monitor, if any...
2636 if (printer
->port_monitor
)
2639 * Add port monitor to the end of the list...
2643 filters
= cupsArrayNew(NULL
, NULL
);
2645 if (!cupsArrayAdd(filters
, &port_monitor
))
2647 cupsdLogMessage(CUPSD_LOG_ERROR
,
2648 "[Job %d] Unable to add port monitor - %s",
2649 job
->id
, strerror(errno
));
2651 cupsArrayDelete(filters
);
2653 job
->current_file
++;
2655 if (job
->current_file
== job
->num_files
)
2656 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2661 snprintf(port_monitor
.filter
, sizeof(port_monitor
.filter
),
2662 "%s/monitor/%s", ServerBin
, printer
->port_monitor
);
2666 * Update the printer and job state to "processing"...
2669 job
->state
->values
[0].integer
= IPP_JOB_PROCESSING
;
2670 job
->state_value
= IPP_JOB_PROCESSING
;
2673 job
->printer
= printer
;
2676 cupsdSetPrinterState(printer
, IPP_PRINTER_PROCESSING
, 0);
2678 if (job
->current_file
== 0)
2681 * Set the processing time...
2684 set_time(job
, "time-at-processing");
2687 * Create the backchannel pipes and make them non-blocking...
2690 cupsdOpenPipe(job
->back_pipes
);
2692 fcntl(job
->back_pipes
[0], F_SETFL
,
2693 fcntl(job
->back_pipes
[0], F_GETFL
) | O_NONBLOCK
);
2695 fcntl(job
->back_pipes
[1], F_SETFL
,
2696 fcntl(job
->back_pipes
[1], F_GETFL
) | O_NONBLOCK
);
2699 * Create the side-channel pipes and make them non-blocking...
2702 socketpair(AF_LOCAL
, SOCK_STREAM
, 0, job
->side_pipes
);
2704 fcntl(job
->side_pipes
[0], F_SETFL
,
2705 fcntl(job
->side_pipes
[0], F_GETFL
) | O_NONBLOCK
);
2707 fcntl(job
->side_pipes
[1], F_SETFL
,
2708 fcntl(job
->side_pipes
[1], F_GETFL
) | O_NONBLOCK
);
2712 * Determine if we are printing a banner page or not...
2715 if (job
->job_sheets
== NULL
)
2717 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] No job-sheets attribute.",
2719 if ((job
->job_sheets
=
2720 ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_ZERO
)) != NULL
)
2721 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2722 "[Job %d] ... but someone added one without setting "
2723 "job_sheets!", job
->id
);
2725 else if (job
->job_sheets
->num_values
== 1)
2726 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] job-sheets=%s", job
->id
,
2727 job
->job_sheets
->values
[0].string
.text
);
2729 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] job-sheets=%s,%s", job
->id
,
2730 job
->job_sheets
->values
[0].string
.text
,
2731 job
->job_sheets
->values
[1].string
.text
);
2733 if (printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
))
2735 else if (job
->job_sheets
== NULL
)
2737 else if (strcasecmp(job
->job_sheets
->values
[0].string
.text
, "none") != 0 &&
2738 job
->current_file
== 0)
2740 else if (job
->job_sheets
->num_values
> 1 &&
2741 strcasecmp(job
->job_sheets
->values
[1].string
.text
, "none") != 0 &&
2742 job
->current_file
== (job
->num_files
- 1))
2747 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] banner_page = %d", job
->id
,
2751 * Building the options string is harder than it needs to be, but
2752 * for the moment we need to pass strings for command-line args and
2753 * not IPP attribute pointers... :)
2755 * First allocate/reallocate the option buffer as needed...
2758 i
= ipp_length(job
->attrs
);
2765 optptr
= realloc(options
, i
);
2769 cupsdLogMessage(CUPSD_LOG_CRIT
,
2770 "[Job %d] Unable to allocate %d bytes for option buffer!",
2773 cupsArrayDelete(filters
);
2775 FilterLevel
-= job
->cost
;
2777 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2786 * Now loop through the attributes and convert them to the textual
2787 * representation used by the filters...
2793 snprintf(title
, sizeof(title
), "%s-%d", printer
->name
, job
->id
);
2794 strcpy(copies
, "1");
2796 for (attr
= job
->attrs
->attrs
; attr
!= NULL
; attr
= attr
->next
)
2798 if (!strcmp(attr
->name
, "copies") &&
2799 attr
->value_tag
== IPP_TAG_INTEGER
)
2802 * Don't use the # copies attribute if we are printing the job sheets...
2806 sprintf(copies
, "%d", attr
->values
[0].integer
);
2808 else if (!strcmp(attr
->name
, "job-name") &&
2809 (attr
->value_tag
== IPP_TAG_NAME
||
2810 attr
->value_tag
== IPP_TAG_NAMELANG
))
2811 strlcpy(title
, attr
->values
[0].string
.text
, sizeof(title
));
2812 else if (attr
->group_tag
== IPP_TAG_JOB
)
2815 * Filter out other unwanted attributes...
2818 if (attr
->value_tag
== IPP_TAG_MIMETYPE
||
2819 attr
->value_tag
== IPP_TAG_NAMELANG
||
2820 attr
->value_tag
== IPP_TAG_TEXTLANG
||
2821 (attr
->value_tag
== IPP_TAG_URI
&& strcmp(attr
->name
, "job-uuid")) ||
2822 attr
->value_tag
== IPP_TAG_URISCHEME
||
2823 attr
->value_tag
== IPP_TAG_BEGIN_COLLECTION
) /* Not yet supported */
2826 if (!strncmp(attr
->name
, "time-", 5))
2829 if (!strncmp(attr
->name
, "job-", 4) && strcmp(attr
->name
, "job-uuid") &&
2830 !(printer
->type
& CUPS_PRINTER_REMOTE
))
2833 if (!strncmp(attr
->name
, "job-", 4) &&
2834 strcmp(attr
->name
, "job-uuid") &&
2835 strcmp(attr
->name
, "job-billing") &&
2836 strcmp(attr
->name
, "job-sheets") &&
2837 strcmp(attr
->name
, "job-hold-until") &&
2838 strcmp(attr
->name
, "job-priority"))
2841 if ((!strcmp(attr
->name
, "page-label") ||
2842 !strcmp(attr
->name
, "page-border") ||
2843 !strncmp(attr
->name
, "number-up", 9) ||
2844 !strcmp(attr
->name
, "page-ranges") ||
2845 !strcmp(attr
->name
, "page-set") ||
2846 !strcasecmp(attr
->name
, "AP_FIRSTPAGE_InputSlot") ||
2847 !strcasecmp(attr
->name
, "AP_FIRSTPAGE_ManualFeed") ||
2848 !strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
2849 "PMTotalSidesImaged..n.") ||
2850 !strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
2851 "PMTotalBeginPages..n.")) &&
2856 * Otherwise add them to the list...
2859 if (optptr
> options
)
2860 strlcat(optptr
, " ", optlength
- (optptr
- options
));
2862 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
2864 strlcat(optptr
, attr
->name
, optlength
- (optptr
- options
));
2865 strlcat(optptr
, "=", optlength
- (optptr
- options
));
2868 for (i
= 0; i
< attr
->num_values
; i
++)
2871 strlcat(optptr
, ",", optlength
- (optptr
- options
));
2873 optptr
+= strlen(optptr
);
2875 switch (attr
->value_tag
)
2877 case IPP_TAG_INTEGER
:
2879 snprintf(optptr
, optlength
- (optptr
- options
),
2880 "%d", attr
->values
[i
].integer
);
2883 case IPP_TAG_BOOLEAN
:
2884 if (!attr
->values
[i
].boolean
)
2885 strlcat(optptr
, "no", optlength
- (optptr
- options
));
2887 case IPP_TAG_NOVALUE
:
2888 strlcat(optptr
, attr
->name
,
2889 optlength
- (optptr
- options
));
2892 case IPP_TAG_RANGE
:
2893 if (attr
->values
[i
].range
.lower
== attr
->values
[i
].range
.upper
)
2894 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2895 "%d", attr
->values
[i
].range
.lower
);
2897 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2898 "%d-%d", attr
->values
[i
].range
.lower
,
2899 attr
->values
[i
].range
.upper
);
2902 case IPP_TAG_RESOLUTION
:
2903 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2904 "%dx%d%s", attr
->values
[i
].resolution
.xres
,
2905 attr
->values
[i
].resolution
.yres
,
2906 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
2910 case IPP_TAG_STRING
:
2913 case IPP_TAG_KEYWORD
:
2914 case IPP_TAG_CHARSET
:
2915 case IPP_TAG_LANGUAGE
:
2917 for (valptr
= attr
->values
[i
].string
.text
; *valptr
;)
2919 if (strchr(" \t\n\\\'\"", *valptr
))
2921 *optptr
++ = *valptr
++;
2928 break; /* anti-compiler-warning-code */
2932 optptr
+= strlen(optptr
);
2937 * Build the command-line arguments for the filters. Each filter
2938 * has 6 or 7 arguments:
2942 * argv[2] = username
2944 * argv[4] = # copies
2946 * argv[6] = filename (optional; normally stdin)
2948 * This allows legacy printer drivers that use the old System V
2949 * printing interface to be used by CUPS.
2951 * For remote jobs, we send all of the files in the argument list.
2954 if (printer
->remote
&& job
->num_files
> 1)
2955 argv
= calloc(7 + job
->num_files
, sizeof(char *));
2957 argv
= calloc(8, sizeof(char *));
2959 sprintf(jobid
, "%d", job
->id
);
2961 argv
[0] = printer
->name
;
2963 argv
[2] = job
->username
;
2968 if (printer
->remote
&& job
->num_files
> 1)
2970 for (i
= 0; i
< job
->num_files
; i
++)
2972 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
2974 argv
[6 + i
] = strdup(filename
);
2979 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
2980 job
->id
, job
->current_file
+ 1);
2984 for (i
= 0; argv
[i
]; i
++)
2985 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2986 "[Job %d] argv[%d]=\"%s\"", job
->id
, i
, argv
[i
]);
2989 * Create environment variable strings for the filters...
2992 attr
= ippFindAttribute(job
->attrs
, "attributes-natural-language",
2995 switch (strlen(attr
->values
[0].string
.text
))
2999 * This is an unknown or badly formatted language code; use
3000 * the POSIX locale...
3003 strcpy(lang
, "LANG=C");
3008 * Just the language code (ll)...
3011 snprintf(lang
, sizeof(lang
), "LANG=%s.UTF8",
3012 attr
->values
[0].string
.text
);
3017 * Language and country code (ll-cc)...
3020 snprintf(lang
, sizeof(lang
), "LANG=%c%c_%c%c.UTF8",
3021 attr
->values
[0].string
.text
[0],
3022 attr
->values
[0].string
.text
[1],
3023 toupper(attr
->values
[0].string
.text
[3] & 255),
3024 toupper(attr
->values
[0].string
.text
[4] & 255));
3028 attr
= ippFindAttribute(job
->attrs
, "document-format",
3031 (optptr
= strstr(attr
->values
[0].string
.text
, "charset=")) != NULL
)
3032 snprintf(charset
, sizeof(charset
), "CHARSET=%s", optptr
+ 8);
3035 attr
= ippFindAttribute(job
->attrs
, "attributes-charset",
3037 snprintf(charset
, sizeof(charset
), "CHARSET=%s",
3038 attr
->values
[0].string
.text
);
3041 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s/%s",
3042 job
->filetypes
[job
->current_file
]->super
,
3043 job
->filetypes
[job
->current_file
]->type
);
3044 snprintf(device_uri
, sizeof(device_uri
), "DEVICE_URI=%s",
3045 printer
->device_uri
);
3046 cupsdSanitizeURI(printer
->device_uri
, sani_uri
, sizeof(sani_uri
));
3047 snprintf(ppd
, sizeof(ppd
), "PPD=%s/ppd/%s.ppd", ServerRoot
, printer
->name
);
3048 snprintf(printer_name
, sizeof(printer_name
), "PRINTER=%s", printer
->name
);
3049 snprintf(rip_max_cache
, sizeof(rip_max_cache
), "RIP_MAX_CACHE=%s", RIPCache
);
3051 envc
= cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
3053 envp
[envc
++] = charset
;
3054 envp
[envc
++] = lang
;
3055 envp
[envc
++] = ppd
;
3056 envp
[envc
++] = rip_max_cache
;
3057 envp
[envc
++] = content_type
;
3058 envp
[envc
++] = device_uri
;
3059 envp
[envc
++] = printer_name
;
3061 if (!printer
->remote
&& !printer
->raw
&&
3062 (filter
= (mime_filter_t
*)cupsArrayLast(filters
)) != NULL
&&
3065 snprintf(final_content_type
, sizeof(final_content_type
),
3066 "FINAL_CONTENT_TYPE=%s/%s",
3067 filter
->dst
->super
, filter
->dst
->type
);
3068 envp
[envc
++] = final_content_type
;
3071 if (Classification
&& !banner_page
)
3073 if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
3074 IPP_TAG_NAME
)) == NULL
)
3075 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3077 else if (attr
->num_values
> 1 &&
3078 strcmp(attr
->values
[1].string
.text
, "none") != 0)
3079 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3080 attr
->values
[1].string
.text
);
3082 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3083 attr
->values
[0].string
.text
);
3085 envp
[envc
++] = classification
;
3088 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
3090 snprintf(class_name
, sizeof(class_name
), "CLASS=%s", job
->dest
);
3091 envp
[envc
++] = class_name
;
3094 if (job
->auth_username
)
3095 envp
[envc
++] = job
->auth_username
;
3096 if (job
->auth_domain
)
3097 envp
[envc
++] = job
->auth_domain
;
3098 if (job
->auth_password
)
3099 envp
[envc
++] = job
->auth_password
;
3103 envp
[envc
++] = job
->ccname
;
3104 #endif /* HAVE_GSSAPI */
3108 for (i
= 0; i
< envc
; i
++)
3109 if (!strncmp(envp
[i
], "AUTH_", 5))
3110 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] envp[%d]=\"AUTH_%c****\"",
3111 job
->id
, i
, envp
[i
][5]);
3112 else if (strncmp(envp
[i
], "DEVICE_URI=", 11))
3113 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] envp[%d]=\"%s\"",
3114 job
->id
, i
, envp
[i
]);
3116 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] envp[%d]=\"DEVICE_URI=%s\"",
3117 job
->id
, i
, sani_uri
);
3119 if (printer
->remote
)
3120 job
->current_file
= job
->num_files
;
3122 job
->current_file
++;
3125 * Now create processes for all of the filters...
3128 filterfds
[0][0] = -1;
3129 filterfds
[0][1] = -1;
3130 filterfds
[1][0] = -1;
3131 filterfds
[1][1] = -1;
3133 if (!job
->status_buffer
)
3135 if (cupsdOpenPipe(job
->status_pipes
))
3137 cupsdLogMessage(CUPSD_LOG_ERROR
,
3138 "[Job %d] Unable to create job status pipes - %s.",
3139 job
->id
, strerror(errno
));
3140 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3141 "Unable to create status pipes - %s.", strerror(errno
));
3143 cupsdAddPrinterHistory(printer
);
3145 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3146 "Job canceled because the server could not create the job "
3152 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3153 "[Job %d] start_job: status_pipes = [ %d %d ]",
3154 job
->id
, job
->status_pipes
[0], job
->status_pipes
[1]);
3156 job
->status_buffer
= cupsdStatBufNew(job
->status_pipes
[0], "[Job %d]",
3158 job
->status_level
= CUPSD_LOG_INFO
;
3162 memset(job
->filters
, 0, sizeof(job
->filters
));
3164 for (i
= 0, slot
= 0, filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
3166 i
++, filter
= (mime_filter_t
*)cupsArrayNext(filters
))
3168 if (filter
->filter
[0] != '/')
3169 snprintf(command
, sizeof(command
), "%s/filter/%s", ServerBin
,
3172 strlcpy(command
, filter
->filter
, sizeof(command
));
3174 if (i
< (cupsArrayCount(filters
) - 1))
3176 if (cupsdOpenPipe(filterfds
[slot
]))
3178 cupsdLogMessage(CUPSD_LOG_ERROR
,
3179 "[Job %d] Unable to create job filter pipes - %s.",
3180 job
->id
, strerror(errno
));
3181 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3182 "Unable to create filter pipes - %s.", strerror(errno
));
3183 cupsdAddPrinterHistory(printer
);
3185 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3186 "Job canceled because the server could not create the "
3194 if (job
->current_file
== 1)
3196 if (strncmp(printer
->device_uri
, "file:", 5) != 0)
3198 if (cupsdOpenPipe(job
->print_pipes
))
3200 cupsdLogMessage(CUPSD_LOG_ERROR
,
3201 "[Job %d] Unable to create job backend pipes - %s.",
3202 job
->id
, strerror(errno
));
3203 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3204 "Unable to create backend pipes - %s.", strerror(errno
));
3205 cupsdAddPrinterHistory(printer
);
3207 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3208 "Job canceled because the server could not create "
3209 "the backend pipes.");
3216 job
->print_pipes
[0] = -1;
3217 if (!strcmp(printer
->device_uri
, "file:/dev/null") ||
3218 !strcmp(printer
->device_uri
, "file:///dev/null"))
3219 job
->print_pipes
[1] = -1;
3222 if (!strncmp(printer
->device_uri
, "file:/dev/", 10))
3223 job
->print_pipes
[1] = open(printer
->device_uri
+ 5,
3225 else if (!strncmp(printer
->device_uri
, "file:///dev/", 12))
3226 job
->print_pipes
[1] = open(printer
->device_uri
+ 7,
3228 else if (!strncmp(printer
->device_uri
, "file:///", 8))
3229 job
->print_pipes
[1] = open(printer
->device_uri
+ 7,
3230 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
3232 job
->print_pipes
[1] = open(printer
->device_uri
+ 5,
3233 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
3235 if (job
->print_pipes
[1] < 0)
3237 cupsdLogMessage(CUPSD_LOG_ERROR
,
3238 "[Job %d] Unable to open output file \"%s\" - %s.",
3239 job
->id
, printer
->device_uri
, strerror(errno
));
3240 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3241 "Unable to open output file \"%s\" - %s.",
3242 printer
->device_uri
, strerror(errno
));
3244 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3245 "Job canceled because the server could not open the "
3251 fcntl(job
->print_pipes
[1], F_SETFD
,
3252 fcntl(job
->print_pipes
[1], F_GETFD
) | FD_CLOEXEC
);
3256 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3257 "[Job %d] start_job: print_pipes = [ %d %d ]",
3258 job
->id
, job
->print_pipes
[0], job
->print_pipes
[1]);
3261 filterfds
[slot
][0] = job
->print_pipes
[0];
3262 filterfds
[slot
][1] = job
->print_pipes
[1];
3265 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] start_job: filter=\"%s\"",
3267 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3268 "[Job %d] start_job: filterfds[%d]=[ %d %d ]",
3269 job
->id
, slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3271 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
3272 filterfds
[slot
][1], job
->status_pipes
[1],
3273 job
->back_pipes
[0], job
->side_pipes
[0], 0,
3276 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3277 "[Job %d] start_job: Closing filter pipes for slot %d "
3279 job
->id
, !slot
, filterfds
[!slot
][0], filterfds
[!slot
][1]);
3281 cupsdClosePipe(filterfds
[!slot
]);
3285 cupsdLogMessage(CUPSD_LOG_ERROR
,
3286 "[Job %d] Unable to start filter \"%s\" - %s.",
3287 job
->id
, filter
->filter
, strerror(errno
));
3288 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3289 "Unable to start filter \"%s\" - %s.",
3290 filter
->filter
, strerror(errno
));
3292 cupsdAddPrinterHistory(printer
);
3294 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3295 "Job canceled because the server could not execute a "
3301 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Started filter %s (PID %d)",
3302 job
->id
, command
, pid
);
3308 cupsArrayDelete(filters
);
3311 * Finally, pipe the final output into a backend process if needed...
3314 if (strncmp(printer
->device_uri
, "file:", 5) != 0)
3316 if (job
->current_file
== 1)
3318 sscanf(printer
->device_uri
, "%254[^:]", method
);
3319 snprintf(command
, sizeof(command
), "%s/backend/%s", ServerBin
, method
);
3322 * See if the backend needs to run as root...
3327 else if (stat(command
, &backinfo
))
3330 backroot
= !(backinfo
.st_mode
& (S_IRWXG
| S_IRWXO
));
3334 filterfds
[slot
][0] = -1;
3335 filterfds
[slot
][1] = -1;
3337 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] start_job: backend=\"%s\"",
3339 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3340 "[Job %d] start_job: filterfds[%d] = [ %d %d ]", job
->id
,
3341 slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3343 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
3344 filterfds
[slot
][1], job
->status_pipes
[1],
3345 job
->back_pipes
[1], job
->side_pipes
[1],
3346 backroot
, &(job
->backend
));
3350 cupsdLogMessage(CUPSD_LOG_ERROR
,
3351 "[Job %d] Unable to start backend \"%s\" - %s.",
3352 job
->id
, method
, strerror(errno
));
3353 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3354 "Unable to start backend \"%s\" - %s.", method
,
3357 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3358 "Job canceled because the server could not execute "
3365 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Started backend %s (PID %d)",
3366 job
->id
, command
, pid
);
3370 if (job
->current_file
== job
->num_files
)
3372 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3373 "[Job %d] start_job: Closing print pipes [ %d %d ]...",
3374 job
->id
, job
->print_pipes
[0], job
->print_pipes
[1]);
3376 cupsdClosePipe(job
->print_pipes
);
3378 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3379 "[Job %d] start_job: Closing back pipes [ %d %d ]...",
3380 job
->id
, job
->back_pipes
[0], job
->back_pipes
[1]);
3382 cupsdClosePipe(job
->back_pipes
);
3384 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3385 "[Job %d] start_job: Closing side pipes [ %d %d ]...",
3386 job
->id
, job
->side_pipes
[0], job
->side_pipes
[1]);
3388 cupsdClosePipe(job
->side_pipes
);
3390 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3391 "[Job %d] start_job: Closing status output pipe %d...",
3392 job
->id
, job
->status_pipes
[1]);
3394 close(job
->status_pipes
[1]);
3395 job
->status_pipes
[1] = -1;
3400 filterfds
[slot
][0] = -1;
3401 filterfds
[slot
][1] = -1;
3403 if (job
->current_file
== job
->num_files
)
3405 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3406 "[Job %d] start_job: Closing print pipes [ %d %d ]...",
3407 job
->id
, job
->print_pipes
[0], job
->print_pipes
[1]);
3409 cupsdClosePipe(job
->print_pipes
);
3411 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3412 "[Job %d] start_job: Closing status output pipe %d...",
3413 job
->id
, job
->status_pipes
[1]);
3415 close(job
->status_pipes
[1]);
3416 job
->status_pipes
[1] = -1;
3420 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3421 "[Job %d] start_job: Closing filter pipes for slot %d "
3423 job
->id
, slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3424 cupsdClosePipe(filterfds
[slot
]);
3426 if (printer
->remote
&& job
->num_files
> 1)
3428 for (i
= 0; i
< job
->num_files
; i
++)
3434 cupsdAddSelect(job
->status_buffer
->fd
, (cupsd_selfunc_t
)update_job
, NULL
,
3437 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
, "Job #%d started.",
3444 * If we get here, we need to abort the current job and close out all
3445 * files and pipes...
3450 for (slot
= 0; slot
< 2; slot
++)
3452 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3453 "[Job %d] start_job: Closing filter pipes for slot %d "
3455 job
->id
, slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3456 cupsdClosePipe(filterfds
[slot
]);
3459 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3460 "[Job %d] start_job: Closing status pipes [ %d %d ]...",
3461 job
->id
, job
->status_pipes
[0], job
->status_pipes
[1]);
3462 cupsdClosePipe(job
->status_pipes
);
3463 cupsdStatBufDelete(job
->status_buffer
);
3465 job
->status_buffer
= NULL
;
3467 cupsArrayDelete(filters
);
3469 if (printer
->remote
&& job
->num_files
> 1)
3471 for (i
= 0; i
< job
->num_files
; i
++)
3477 cupsdStopJob(job
, 0);
3482 * 'unload_job()' - Unload a job from memory.
3486 unload_job(cupsd_job_t
*job
) /* I - Job */
3491 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Unloading...", job
->id
);
3493 ippDelete(job
->attrs
);
3498 job
->job_sheets
= NULL
;
3499 job
->printer_message
= NULL
;
3500 job
->printer_reasons
= NULL
;
3505 * 'update_job()' - Read a status update from a job's filters.
3509 update_job(cupsd_job_t
*job
) /* I - Job to check */
3511 int i
; /* Looping var */
3512 int copies
; /* Number of copies printed */
3513 char message
[1024], /* Message text */
3514 *ptr
; /* Pointer update... */
3515 int loglevel
, /* Log level for message */
3516 event
= 0; /* Events? */
3519 while ((ptr
= cupsdStatBufUpdate(job
->status_buffer
, &loglevel
,
3520 message
, sizeof(message
))) != NULL
)
3523 * Process page and printer state messages as needed...
3526 if (loglevel
== CUPSD_LOG_PAGE
)
3529 * Page message; send the message to the page_log file and update the
3530 * job sheet count...
3533 if (job
->sheets
!= NULL
)
3535 if (!strncasecmp(message
, "total ", 6))
3538 * Got a total count of pages from a backend or filter...
3541 copies
= atoi(message
+ 6);
3542 copies
-= job
->sheets
->values
[0].integer
; /* Just track the delta */
3544 else if (!sscanf(message
, "%*d%d", &copies
))
3547 job
->sheets
->values
[0].integer
+= copies
;
3549 if (job
->printer
->page_limit
)
3551 cupsd_quota_t
*q
= cupsdUpdateQuota(job
->printer
, job
->username
,
3555 if (AppleQuotas
&& q
->page_count
== -3)
3558 * Quota limit exceeded, cancel job in progress immediately...
3561 cupsdLogMessage(CUPSD_LOG_INFO
,
3562 "[Job %d] Canceled because pages exceed user %s "
3563 "quota limit on printer %s (%s).",
3564 job
->id
, job
->username
, job
->printer
->name
,
3565 job
->printer
->info
);
3567 cupsdCancelJob(job
, 1, IPP_JOB_CANCELED
);
3572 #endif /* __APPLE__ */
3576 cupsdLogPage(job
, message
);
3578 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
3579 "Printed %d page(s).", job
->sheets
->values
[0].integer
);
3581 else if (loglevel
== CUPSD_LOG_STATE
)
3583 if (!strcmp(message
, "paused"))
3585 cupsdStopPrinter(job
->printer
, 1);
3590 cupsdSetPrinterReasons(job
->printer
, message
);
3591 cupsdAddPrinterHistory(job
->printer
);
3592 event
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
3595 update_job_attrs(job
);
3597 else if (loglevel
== CUPSD_LOG_ATTR
)
3600 * Set attribute(s)...
3603 int num_attrs
; /* Number of attributes */
3604 cups_option_t
*attrs
; /* Attributes */
3605 const char *attr
; /* Attribute */
3608 num_attrs
= cupsParseOptions(message
, 0, &attrs
);
3610 if ((attr
= cupsGetOption("auth-info-required", num_attrs
,
3613 cupsdSetAuthInfoRequired(job
->printer
, attr
, NULL
);
3614 cupsdSetPrinterAttrs(job
->printer
);
3615 cupsdSaveAllPrinters();
3618 if ((attr
= cupsGetOption("printer-alert", num_attrs
, attrs
)) != NULL
)
3620 cupsdSetString(&job
->printer
->alert
, attr
);
3621 event
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
3624 if ((attr
= cupsGetOption("printer-alert-description", num_attrs
,
3627 cupsdSetString(&job
->printer
->alert_description
, attr
);
3628 event
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
3631 cupsFreeOptions(num_attrs
, attrs
);
3634 else if (!strncmp(message
, "recoverable:", 12))
3636 cupsdSetPrinterReasons(job
->printer
,
3637 "+com.apple.print.recoverable-warning");
3640 while (isspace(*ptr
& 255))
3643 cupsdSetString(&job
->printer
->recoverable
, ptr
);
3644 cupsdAddPrinterHistory(job
->printer
);
3645 event
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
3647 else if (!strncmp(message
, "recovered:", 10))
3649 cupsdSetPrinterReasons(job
->printer
,
3650 "-com.apple.print.recoverable-warning");
3653 while (isspace(*ptr
& 255))
3656 cupsdSetString(&job
->printer
->recoverable
, ptr
);
3657 cupsdAddPrinterHistory(job
->printer
);
3658 event
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
3660 #endif /* __APPLE__ */
3661 else if (loglevel
<= job
->status_level
)
3664 * Some message to show in the printer-state-message attribute...
3667 if (loglevel
!= CUPSD_LOG_NOTICE
)
3668 job
->status_level
= loglevel
;
3670 strlcpy(job
->printer
->state_message
, message
,
3671 sizeof(job
->printer
->state_message
));
3672 cupsdAddPrinterHistory(job
->printer
);
3673 event
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
3675 update_job_attrs(job
);
3678 if (!strchr(job
->status_buffer
->buffer
, '\n'))
3682 if ((event
& CUPSD_EVENT_PRINTER_STATE_CHANGED
))
3683 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE_CHANGED
, job
->printer
, NULL
,
3684 (job
->printer
->type
& CUPS_PRINTER_CLASS
) ?
3685 "Class \"%s\" state changed." :
3686 "Printer \"%s\" state changed.",
3687 job
->printer
->name
);
3689 if (ptr
== NULL
&& !job
->status_buffer
->bufused
)
3692 * See if all of the filters and the backend have returned their
3696 for (i
= 0; job
->filters
[i
] < 0; i
++);
3698 if (job
->filters
[i
])
3701 if (job
->current_file
>= job
->num_files
&& job
->backend
> 0)
3705 * Handle the end of job stuff...
3708 cupsdFinishJob(job
);
3714 * 'update_job_attrs()' - Update the job-printer-* attributes.
3718 update_job_attrs(cupsd_job_t
*job
) /* I - Job to update */
3720 int i
; /* Looping var */
3721 int num_reasons
; /* Actual number of reasons */
3722 const char * const *reasons
; /* Reasons */
3723 static const char *none
= "none", /* "none" */
3729 * Get/create the job-printer-state-* attributes...
3732 if (!job
->printer_message
)
3734 if ((job
->printer_message
= ippFindAttribute(job
->attrs
,
3735 "job-printer-state-message",
3736 IPP_TAG_TEXT
)) == NULL
)
3737 job
->printer_message
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_TEXT
,
3738 "job-printer-state-message",
3742 if (!job
->printer_reasons
)
3743 job
->printer_reasons
= ippFindAttribute(job
->attrs
,
3744 "job-printer-state-reasons",
3748 * If the job isn't printing, return now...
3755 * Otherwise copy the printer-state-message value...
3758 if (job
->printer
->state_message
[0])
3759 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
),
3760 job
->printer
->state_message
);
3763 * ... and the printer-state-reasons value...
3766 if (job
->printer
->num_reasons
== 0)
3769 reasons
= job
->printer
->state
== IPP_PRINTER_STOPPED
? &paused
: &none
;
3773 num_reasons
= job
->printer
->num_reasons
;
3774 reasons
= (const char * const *)job
->printer
->reasons
;
3777 if (!job
->printer_reasons
|| job
->printer_reasons
->num_values
!= num_reasons
)
3779 ippDeleteAttribute(job
->attrs
, job
->printer_reasons
);
3781 job
->printer_reasons
= ippAddStrings(job
->attrs
,
3782 IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
3783 "job-printer-state-reasons",
3784 num_reasons
, NULL
, NULL
);
3787 for (i
= 0; i
< num_reasons
; i
++)
3788 cupsdSetString(&(job
->printer_reasons
->values
[i
].string
.text
), reasons
[i
]);
3793 * End of "$Id: job.c 7005 2007-10-01 23:45:48Z mike $".