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
);
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
;
889 * 'cupsdLoadAllJobs()' - Load all jobs from disk.
893 cupsdLoadAllJobs(void)
895 char filename
[1024]; /* Full filename of job.cache file */
896 struct stat fileinfo
, /* Information on job.cache file */
897 dirinfo
; /* Information on RequestRoot dir */
902 * Create the job arrays as needed...
906 Jobs
= cupsArrayNew(compare_jobs
, NULL
);
909 ActiveJobs
= cupsArrayNew(compare_active_jobs
, NULL
);
912 * See whether the job.cache file is older than the RequestRoot directory...
915 snprintf(filename
, sizeof(filename
), "%s/job.cache", CacheDir
);
917 if (stat(filename
, &fileinfo
))
919 fileinfo
.st_mtime
= 0;
922 cupsdLogMessage(CUPSD_LOG_ERROR
,
923 "Unable to get file information for \"%s\" - %s",
924 filename
, strerror(errno
));
927 if (stat(RequestRoot
, &dirinfo
))
929 dirinfo
.st_mtime
= 0;
932 cupsdLogMessage(CUPSD_LOG_ERROR
,
933 "Unable to get directory information for \"%s\" - %s",
934 RequestRoot
, strerror(errno
));
938 * Load the most recent source for job data...
941 if (dirinfo
.st_mtime
> fileinfo
.st_mtime
)
945 load_next_job_id(filename
);
948 load_job_cache(filename
);
951 * Clean out old jobs as needed...
954 if (MaxJobs
> 0 && cupsArrayCount(Jobs
) >= MaxJobs
)
960 * 'cupsdLoadJob()' - Load a single job...
964 cupsdLoadJob(cupsd_job_t
*job
) /* I - Job */
966 char jobfile
[1024]; /* Job filename */
967 cups_file_t
*fp
; /* Job file */
968 int fileid
; /* Current file ID */
969 ipp_attribute_t
*attr
; /* Job attribute */
970 const char *dest
; /* Destination name */
971 cupsd_printer_t
*destptr
; /* Pointer to destination */
972 mime_type_t
**filetypes
; /* New filetypes array */
973 int *compressions
; /* New compressions array */
978 if (job
->state_value
> IPP_JOB_STOPPED
)
979 job
->access_time
= time(NULL
);
984 if ((job
->attrs
= ippNew()) == NULL
)
986 cupsdLogMessage(CUPSD_LOG_ERROR
,
987 "[Job %d] Ran out of memory for job attributes!", job
->id
);
992 * Load job attributes...
995 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Loading attributes...", job
->id
);
997 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, job
->id
);
998 if ((fp
= cupsFileOpen(jobfile
, "r")) == NULL
)
1000 cupsdLogMessage(CUPSD_LOG_ERROR
,
1001 "[Job %d] Unable to open job control file \"%s\" - %s!",
1002 job
->id
, jobfile
, strerror(errno
));
1003 ippDelete(job
->attrs
);
1008 if (ippReadIO(fp
, (ipp_iocb_t
)cupsFileRead
, 1, NULL
, job
->attrs
) != IPP_DATA
)
1010 cupsdLogMessage(CUPSD_LOG_ERROR
,
1011 "[Job %d] Unable to read job control file \"%s\"!",
1014 ippDelete(job
->attrs
);
1023 * Copy attribute data to the job object...
1026 if ((job
->state
= ippFindAttribute(job
->attrs
, "job-state",
1027 IPP_TAG_ENUM
)) == NULL
)
1029 cupsdLogMessage(CUPSD_LOG_ERROR
,
1030 "[Job %d] Missing or bad job-state attribute in "
1033 ippDelete(job
->attrs
);
1039 job
->state_value
= (ipp_jstate_t
)job
->state
->values
[0].integer
;
1043 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1044 IPP_TAG_URI
)) == NULL
)
1046 cupsdLogMessage(CUPSD_LOG_ERROR
,
1047 "[Job %d] No job-printer-uri attribute in control file!",
1049 ippDelete(job
->attrs
);
1055 if ((dest
= cupsdValidateDest(attr
->values
[0].string
.text
, &(job
->dtype
),
1058 cupsdLogMessage(CUPSD_LOG_ERROR
,
1059 "[Job %d] Unable to queue job for destination \"%s\"!",
1060 job
->id
, attr
->values
[0].string
.text
);
1061 ippDelete(job
->attrs
);
1067 cupsdSetString(&job
->dest
, dest
);
1069 else if ((destptr
= cupsdFindDest(job
->dest
)) == NULL
)
1071 cupsdLogMessage(CUPSD_LOG_ERROR
,
1072 "[Job %d] Unable to queue job for destination \"%s\"!",
1073 job
->id
, job
->dest
);
1074 ippDelete(job
->attrs
);
1080 job
->sheets
= ippFindAttribute(job
->attrs
, "job-media-sheets-completed",
1082 job
->job_sheets
= ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_NAME
);
1086 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
1087 IPP_TAG_INTEGER
)) == NULL
)
1089 cupsdLogMessage(CUPSD_LOG_ERROR
,
1090 "[Job %d] Missing or bad job-priority attribute in "
1091 "control file!", job
->id
);
1092 ippDelete(job
->attrs
);
1098 job
->priority
= attr
->values
[0].integer
;
1103 if ((attr
= ippFindAttribute(job
->attrs
, "job-originating-user-name",
1104 IPP_TAG_NAME
)) == NULL
)
1106 cupsdLogMessage(CUPSD_LOG_ERROR
,
1107 "[Job %d] Missing or bad job-originating-user-name "
1108 "attribute in control file!", job
->id
);
1109 ippDelete(job
->attrs
);
1115 cupsdSetString(&job
->username
, attr
->values
[0].string
.text
);
1119 * Set the job hold-until time and state...
1122 if (job
->state_value
== IPP_JOB_HELD
)
1124 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1125 IPP_TAG_KEYWORD
)) == NULL
)
1126 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1129 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
1132 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1133 job
->state_value
= IPP_JOB_PENDING
;
1136 else if (job
->state_value
== IPP_JOB_PROCESSING
)
1138 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1139 job
->state_value
= IPP_JOB_PENDING
;
1142 if (!job
->num_files
)
1145 * Find all the d##### files...
1148 for (fileid
= 1; fileid
< 10000; fileid
++)
1150 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
1153 if (access(jobfile
, 0))
1156 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1157 "[Job %d] Auto-typing document file \"%s\"...",
1160 if (fileid
> job
->num_files
)
1162 if (job
->num_files
== 0)
1164 compressions
= (int *)calloc(fileid
, sizeof(int));
1165 filetypes
= (mime_type_t
**)calloc(fileid
, sizeof(mime_type_t
*));
1169 compressions
= (int *)realloc(job
->compressions
,
1170 sizeof(int) * fileid
);
1171 filetypes
= (mime_type_t
**)realloc(job
->filetypes
,
1172 sizeof(mime_type_t
*) *
1176 if (!compressions
|| !filetypes
)
1178 cupsdLogMessage(CUPSD_LOG_ERROR
,
1179 "[Job %d] Ran out of memory for job file types!",
1184 job
->compressions
= compressions
;
1185 job
->filetypes
= filetypes
;
1186 job
->num_files
= fileid
;
1189 job
->filetypes
[fileid
- 1] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
1190 job
->compressions
+ fileid
- 1);
1192 if (!job
->filetypes
[fileid
- 1])
1193 job
->filetypes
[fileid
- 1] = mimeType(MimeDatabase
, "application",
1199 * Load authentication information as needed...
1202 if (job
->state_value
< IPP_JOB_STOPPED
)
1204 snprintf(jobfile
, sizeof(jobfile
), "%s/a%05d", RequestRoot
, job
->id
);
1206 cupsdClearString(&job
->auth_username
);
1207 cupsdClearString(&job
->auth_domain
);
1208 cupsdClearString(&job
->auth_password
);
1210 if ((fp
= cupsFileOpen(jobfile
, "r")) != NULL
)
1212 int i
, /* Looping var */
1213 bytes
; /* Size of auth data */
1214 char line
[255], /* Line from file */
1215 data
[255]; /* Decoded data */
1219 i
< destptr
->num_auth_info_required
&&
1220 cupsFileGets(fp
, line
, sizeof(line
));
1223 bytes
= sizeof(data
);
1224 httpDecode64_2(data
, &bytes
, line
);
1226 if (!strcmp(destptr
->auth_info_required
[i
], "username"))
1227 cupsdSetStringf(&job
->auth_username
, "AUTH_USERNAME=%s", data
);
1228 else if (!strcmp(destptr
->auth_info_required
[i
], "domain"))
1229 cupsdSetStringf(&job
->auth_domain
, "AUTH_DOMAIN=%s", data
);
1230 else if (!strcmp(destptr
->auth_info_required
[i
], "password"))
1231 cupsdSetStringf(&job
->auth_password
, "AUTH_PASSWORD=%s", data
);
1237 job
->access_time
= time(NULL
);
1242 * 'cupsdMoveJob()' - Move the specified job to a different destination.
1246 cupsdMoveJob(cupsd_job_t
*job
, /* I - Job */
1247 cupsd_printer_t
*p
) /* I - Destination printer or class */
1249 ipp_attribute_t
*attr
; /* job-printer-uri attribute */
1250 const char *olddest
; /* Old destination */
1251 cupsd_printer_t
*oldp
; /* Old pointer */
1255 * Don't move completed jobs...
1258 if (job
->state_value
> IPP_JOB_STOPPED
)
1262 * Get the old destination...
1265 olddest
= job
->dest
;
1268 oldp
= job
->printer
;
1270 oldp
= cupsdFindDest(olddest
);
1273 * Change the destination information...
1276 if (job
->state_value
== IPP_JOB_PROCESSING
)
1277 cupsdStopJob(job
, 0);
1281 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, oldp
, job
,
1282 "Job #%d moved from %s to %s.", job
->id
, olddest
,
1285 cupsdSetString(&job
->dest
, p
->name
);
1286 job
->dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_REMOTE
|
1287 CUPS_PRINTER_IMPLICIT
);
1289 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1290 IPP_TAG_URI
)) != NULL
)
1291 cupsdSetString(&(attr
->values
[0].string
.text
), p
->uri
);
1293 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, p
, job
,
1294 "Job #%d moved from %s to %s.", job
->id
, olddest
,
1302 * 'cupsdReleaseJob()' - Release the specified job.
1306 cupsdReleaseJob(cupsd_job_t
*job
) /* I - Job */
1308 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdReleaseJob: id = %d", job
->id
);
1310 if (job
->state_value
== IPP_JOB_HELD
)
1312 DEBUG_puts("cupsdReleaseJob: setting state to pending...");
1314 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1315 job
->state_value
= IPP_JOB_PENDING
;
1323 * 'cupsdRestartJob()' - Restart the specified job.
1327 cupsdRestartJob(cupsd_job_t
*job
) /* I - Job */
1329 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdRestartJob: id = %d", job
->id
);
1331 if (job
->state_value
== IPP_JOB_STOPPED
|| job
->num_files
)
1333 ipp_jstate_t old_state
; /* Old job state */
1338 old_state
= job
->state_value
;
1341 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1342 job
->state_value
= IPP_JOB_PENDING
;
1346 if (old_state
> IPP_JOB_STOPPED
)
1347 cupsArrayAdd(ActiveJobs
, job
);
1355 * 'cupsdSaveAllJobs()' - Save a summary of all jobs to disk.
1359 cupsdSaveAllJobs(void)
1361 int i
; /* Looping var */
1362 cups_file_t
*fp
; /* Job cache file */
1363 char temp
[1024]; /* Temporary string */
1364 cupsd_job_t
*job
; /* Current job */
1365 time_t curtime
; /* Current time */
1366 struct tm
*curdate
; /* Current date */
1369 snprintf(temp
, sizeof(temp
), "%s/job.cache", CacheDir
);
1370 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
1372 cupsdLogMessage(CUPSD_LOG_ERROR
,
1373 "Unable to create job cache file \"%s\" - %s",
1374 temp
, strerror(errno
));
1378 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving job cache file \"%s\"...", temp
);
1381 * Restrict access to the file...
1384 fchown(cupsFileNumber(fp
), getuid(), Group
);
1385 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
1388 * Write a small header to the file...
1391 curtime
= time(NULL
);
1392 curdate
= localtime(&curtime
);
1393 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
1395 cupsFilePuts(fp
, "# Job cache file for " CUPS_SVERSION
"\n");
1396 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
1397 cupsFilePrintf(fp
, "NextJobId %d\n", NextJobId
);
1400 * Write each job known to the system...
1403 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1405 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1407 cupsFilePrintf(fp
, "<Job %d>\n", job
->id
);
1408 cupsFilePrintf(fp
, "State %d\n", job
->state_value
);
1409 cupsFilePrintf(fp
, "Priority %d\n", job
->priority
);
1410 cupsFilePrintf(fp
, "Username %s\n", job
->username
);
1411 cupsFilePrintf(fp
, "Destination %s\n", job
->dest
);
1412 cupsFilePrintf(fp
, "DestType %d\n", job
->dtype
);
1413 cupsFilePrintf(fp
, "NumFiles %d\n", job
->num_files
);
1414 for (i
= 0; i
< job
->num_files
; i
++)
1415 cupsFilePrintf(fp
, "File %d %s/%s %d\n", i
+ 1, job
->filetypes
[i
]->super
,
1416 job
->filetypes
[i
]->type
, job
->compressions
[i
]);
1417 cupsFilePuts(fp
, "</Job>\n");
1425 * 'cupsdSaveJob()' - Save a job to disk.
1429 cupsdSaveJob(cupsd_job_t
*job
) /* I - Job */
1431 char filename
[1024]; /* Job control filename */
1432 cups_file_t
*fp
; /* Job file */
1435 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSaveJob(job=%p(%d)): job->attrs=%p",
1436 job
, job
->id
, job
->attrs
);
1438 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
, job
->id
);
1440 if ((fp
= cupsFileOpen(filename
, "w")) == NULL
)
1442 cupsdLogMessage(CUPSD_LOG_ERROR
,
1443 "[Job %d] Unable to create job control file \"%s\" - %s.",
1444 job
->id
, filename
, strerror(errno
));
1448 fchmod(cupsFileNumber(fp
), 0600);
1449 fchown(cupsFileNumber(fp
), RunUser
, Group
);
1451 job
->attrs
->state
= IPP_IDLE
;
1453 if (ippWriteIO(fp
, (ipp_iocb_t
)cupsFileWrite
, 1, NULL
,
1454 job
->attrs
) != IPP_DATA
)
1455 cupsdLogMessage(CUPSD_LOG_ERROR
,
1456 "[Job %d] Unable to write job control file!", job
->id
);
1463 * 'cupsdSetJobHoldUntil()' - Set the hold time for a job...
1467 cupsdSetJobHoldUntil(cupsd_job_t
*job
, /* I - Job */
1468 const char *when
) /* I - When to resume */
1470 time_t curtime
; /* Current time */
1471 struct tm
*curdate
; /* Current date */
1472 int hour
; /* Hold hour */
1473 int minute
; /* Hold minute */
1474 int second
; /* Hold second */
1477 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetJobHoldUntil(%d, \"%s\")",
1482 if (!strcmp(when
, "indefinite") || !strcmp(when
, "auth-info-required"))
1485 * Hold indefinitely...
1488 job
->hold_until
= 0;
1490 else if (!strcmp(when
, "day-time"))
1493 * Hold to 6am the next morning unless local time is < 6pm.
1496 curtime
= time(NULL
);
1497 curdate
= localtime(&curtime
);
1499 if (curdate
->tm_hour
< 18)
1500 job
->hold_until
= curtime
;
1502 job
->hold_until
= curtime
+
1503 ((29 - curdate
->tm_hour
) * 60 + 59 -
1504 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1506 else if (!strcmp(when
, "evening") || !strcmp(when
, "night"))
1509 * Hold to 6pm unless local time is > 6pm or < 6am.
1512 curtime
= time(NULL
);
1513 curdate
= localtime(&curtime
);
1515 if (curdate
->tm_hour
< 6 || curdate
->tm_hour
>= 18)
1516 job
->hold_until
= curtime
;
1518 job
->hold_until
= curtime
+
1519 ((17 - curdate
->tm_hour
) * 60 + 59 -
1520 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1522 else if (!strcmp(when
, "second-shift"))
1525 * Hold to 4pm unless local time is > 4pm.
1528 curtime
= time(NULL
);
1529 curdate
= localtime(&curtime
);
1531 if (curdate
->tm_hour
>= 16)
1532 job
->hold_until
= curtime
;
1534 job
->hold_until
= curtime
+
1535 ((15 - curdate
->tm_hour
) * 60 + 59 -
1536 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1538 else if (!strcmp(when
, "third-shift"))
1541 * Hold to 12am unless local time is < 8am.
1544 curtime
= time(NULL
);
1545 curdate
= localtime(&curtime
);
1547 if (curdate
->tm_hour
< 8)
1548 job
->hold_until
= curtime
;
1550 job
->hold_until
= curtime
+
1551 ((23 - curdate
->tm_hour
) * 60 + 59 -
1552 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1554 else if (!strcmp(when
, "weekend"))
1557 * Hold to weekend unless we are in the weekend.
1560 curtime
= time(NULL
);
1561 curdate
= localtime(&curtime
);
1563 if (curdate
->tm_wday
|| curdate
->tm_wday
== 6)
1564 job
->hold_until
= curtime
;
1566 job
->hold_until
= curtime
+
1567 (((5 - curdate
->tm_wday
) * 24 +
1568 (17 - curdate
->tm_hour
)) * 60 + 59 -
1569 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1571 else if (sscanf(when
, "%d:%d:%d", &hour
, &minute
, &second
) >= 2)
1574 * Hold to specified GMT time (HH:MM or HH:MM:SS)...
1577 curtime
= time(NULL
);
1578 curdate
= gmtime(&curtime
);
1580 job
->hold_until
= curtime
+
1581 ((hour
- curdate
->tm_hour
) * 60 + minute
-
1582 curdate
->tm_min
) * 60 + second
- curdate
->tm_sec
;
1585 * Hold until next day as needed...
1588 if (job
->hold_until
< curtime
)
1589 job
->hold_until
+= 24 * 60 * 60;
1592 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetJobHoldUntil: hold_until = %d",
1593 (int)job
->hold_until
);
1598 * 'cupsdSetJobPriority()' - Set the priority of a job, moving it up/down in
1599 * the list as needed.
1603 cupsdSetJobPriority(
1604 cupsd_job_t
*job
, /* I - Job ID */
1605 int priority
) /* I - New priority (0 to 100) */
1607 ipp_attribute_t
*attr
; /* Job attribute */
1611 * Don't change completed jobs...
1614 if (job
->state_value
>= IPP_JOB_PROCESSING
)
1618 * Set the new priority and re-add the job into the active list...
1621 cupsArrayRemove(ActiveJobs
, job
);
1623 job
->priority
= priority
;
1625 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
1626 IPP_TAG_INTEGER
)) != NULL
)
1627 attr
->values
[0].integer
= priority
;
1629 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-priority",
1632 cupsArrayAdd(ActiveJobs
, job
);
1639 * 'cupsdStopAllJobs()' - Stop all print jobs.
1643 cupsdStopAllJobs(int force
) /* I - 1 = Force all filters to stop */
1645 cupsd_job_t
*job
; /* Current job */
1648 DEBUG_puts("cupsdStopAllJobs()");
1650 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
1652 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
1653 if (job
->state_value
== IPP_JOB_PROCESSING
)
1655 cupsdStopJob(job
, force
);
1656 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1657 job
->state_value
= IPP_JOB_PENDING
;
1663 * 'cupsdStopJob()' - Stop a print job.
1667 cupsdStopJob(cupsd_job_t
*job
, /* I - Job */
1668 int force
) /* I - 1 = Force all filters to stop */
1670 int i
; /* Looping var */
1673 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1674 "[Job %d] cupsdStopJob: force = %d", job
->id
, force
);
1676 if (job
->state_value
!= IPP_JOB_PROCESSING
)
1679 FilterLevel
-= job
->cost
;
1681 if (job
->printer
->state
== IPP_PRINTER_PROCESSING
)
1682 cupsdSetPrinterState(job
->printer
, IPP_PRINTER_IDLE
, 0);
1684 job
->state
->values
[0].integer
= IPP_JOB_STOPPED
;
1685 job
->state_value
= IPP_JOB_STOPPED
;
1686 job
->printer
->job
= NULL
;
1687 job
->printer
= NULL
;
1689 job
->current_file
--;
1691 for (i
= 0; job
->filters
[i
]; i
++)
1692 if (job
->filters
[i
] > 0)
1694 cupsdEndProcess(job
->filters
[i
], force
);
1695 job
->filters
[i
] = 0;
1698 if (job
->backend
> 0)
1700 cupsdEndProcess(job
->backend
, force
);
1704 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] Closing print pipes [ %d %d ]...",
1705 job
->id
, job
->print_pipes
[0], job
->print_pipes
[1]);
1707 cupsdClosePipe(job
->print_pipes
);
1709 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] Closing back pipes [ %d %d ]...",
1710 job
->id
, job
->back_pipes
[0], job
->back_pipes
[1]);
1712 cupsdClosePipe(job
->back_pipes
);
1714 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] Closing side pipes [ %d %d ]...",
1715 job
->id
, job
->side_pipes
[0], job
->side_pipes
[1]);
1717 cupsdClosePipe(job
->side_pipes
);
1719 if (job
->status_buffer
)
1722 * Close the pipe and clear the input bit.
1725 cupsdRemoveSelect(job
->status_buffer
->fd
);
1727 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1728 "[Job %d] Closing status pipes [ %d %d ]...", job
->id
,
1729 job
->status_pipes
[0], job
->status_pipes
[1]);
1731 cupsdClosePipe(job
->status_pipes
);
1732 cupsdStatBufDelete(job
->status_buffer
);
1734 job
->status_buffer
= NULL
;
1740 * 'cupsdUnloadCompletedJobs()' - Flush completed job history from memory.
1744 cupsdUnloadCompletedJobs(void)
1746 cupsd_job_t
*job
; /* Current job */
1747 time_t expire
; /* Expiration time */
1750 expire
= time(NULL
) - 60;
1752 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1754 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1755 if (job
->attrs
&& job
->state_value
>= IPP_JOB_STOPPED
&&
1756 job
->access_time
< expire
)
1762 * 'compare_active_jobs()' - Compare the job IDs and priorities of two jobs.
1765 static int /* O - Difference */
1766 compare_active_jobs(void *first
, /* I - First job */
1767 void *second
, /* I - Second job */
1768 void *data
) /* I - App data (not used) */
1770 int diff
; /* Difference */
1773 if ((diff
= ((cupsd_job_t
*)second
)->priority
-
1774 ((cupsd_job_t
*)first
)->priority
) != 0)
1777 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
1782 * 'compare_jobs()' - Compare the job IDs of two jobs.
1785 static int /* O - Difference */
1786 compare_jobs(void *first
, /* I - First job */
1787 void *second
, /* I - Second job */
1788 void *data
) /* I - App data (not used) */
1790 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
1795 * 'free_job()' - Free all memory used by a job.
1799 free_job(cupsd_job_t
*job
) /* I - Job */
1801 cupsdClearString(&job
->username
);
1802 cupsdClearString(&job
->dest
);
1803 cupsdClearString(&job
->auth_username
);
1804 cupsdClearString(&job
->auth_domain
);
1805 cupsdClearString(&job
->auth_password
);
1808 * Destroy the credential cache and clear the KRB5CCNAME env var string.
1813 krb5_cc_destroy(KerberosContext
, job
->ccache
);
1817 cupsdClearString(&job
->ccname
);
1818 #endif /* HAVE_GSSAPI */
1820 if (job
->num_files
> 0)
1822 free(job
->compressions
);
1823 free(job
->filetypes
);
1826 ippDelete(job
->attrs
);
1833 * 'ipp_length()' - Compute the size of the buffer needed to hold
1834 * the textual IPP attributes.
1837 static int /* O - Size of attribute buffer */
1838 ipp_length(ipp_t
*ipp
) /* I - IPP request */
1840 int bytes
; /* Number of bytes */
1841 int i
; /* Looping var */
1842 ipp_attribute_t
*attr
; /* Current attribute */
1846 * Loop through all attributes...
1851 for (attr
= ipp
->attrs
; attr
!= NULL
; attr
= attr
->next
)
1854 * Skip attributes that won't be sent to filters...
1857 if (attr
->value_tag
== IPP_TAG_MIMETYPE
||
1858 attr
->value_tag
== IPP_TAG_NAMELANG
||
1859 attr
->value_tag
== IPP_TAG_TEXTLANG
||
1860 attr
->value_tag
== IPP_TAG_URI
||
1861 attr
->value_tag
== IPP_TAG_URISCHEME
)
1864 if (strncmp(attr
->name
, "time-", 5) == 0)
1868 * Add space for a leading space and commas between each value.
1869 * For the first attribute, the leading space isn't used, so the
1870 * extra byte can be used as the nul terminator...
1873 bytes
++; /* " " separator */
1874 bytes
+= attr
->num_values
; /* "," separators */
1877 * Boolean attributes appear as "foo,nofoo,foo,nofoo", while
1878 * other attributes appear as "foo=value1,value2,...,valueN".
1881 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
1882 bytes
+= strlen(attr
->name
);
1884 bytes
+= attr
->num_values
* strlen(attr
->name
);
1887 * Now add the size required for each value in the attribute...
1890 switch (attr
->value_tag
)
1892 case IPP_TAG_INTEGER
:
1895 * Minimum value of a signed integer is -2147483647, or 11 digits.
1898 bytes
+= attr
->num_values
* 11;
1901 case IPP_TAG_BOOLEAN
:
1903 * Add two bytes for each false ("no") value...
1906 for (i
= 0; i
< attr
->num_values
; i
++)
1907 if (!attr
->values
[i
].boolean
)
1911 case IPP_TAG_RANGE
:
1913 * A range is two signed integers separated by a hyphen, or
1914 * 23 characters max.
1917 bytes
+= attr
->num_values
* 23;
1920 case IPP_TAG_RESOLUTION
:
1922 * A resolution is two signed integers separated by an "x" and
1923 * suffixed by the units, or 26 characters max.
1926 bytes
+= attr
->num_values
* 26;
1929 case IPP_TAG_STRING
:
1932 case IPP_TAG_KEYWORD
:
1933 case IPP_TAG_CHARSET
:
1934 case IPP_TAG_LANGUAGE
:
1937 * Strings can contain characters that need quoting. We need
1938 * at least 2 * len + 2 characters to cover the quotes and
1939 * any backslashes in the string.
1942 for (i
= 0; i
< attr
->num_values
; i
++)
1943 bytes
+= 2 * strlen(attr
->values
[i
].string
.text
) + 2;
1947 break; /* anti-compiler-warning-code */
1956 * 'load_job_cache()' - Load jobs from the job.cache file.
1960 load_job_cache(const char *filename
) /* I - job.cache filename */
1962 cups_file_t
*fp
; /* job.cache file */
1963 char line
[1024], /* Line buffer */
1964 *value
; /* Value on line */
1965 int linenum
; /* Line number in file */
1966 cupsd_job_t
*job
; /* Current job */
1967 int jobid
; /* Job ID */
1968 char jobfile
[1024]; /* Job filename */
1972 * Open the job.cache file...
1975 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
1977 if (errno
!= ENOENT
)
1978 cupsdLogMessage(CUPSD_LOG_ERROR
,
1979 "Unable to open job cache file \"%s\": %s",
1980 filename
, strerror(errno
));
1982 load_request_root();
1988 * Read entries from the job cache file and create jobs as needed.
1991 cupsdLogMessage(CUPSD_LOG_INFO
, "Loading job cache file \"%s\"...",
1997 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
1999 if (!strcasecmp(line
, "NextJobId"))
2002 NextJobId
= atoi(value
);
2004 else if (!strcasecmp(line
, "<Job"))
2008 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing </Job> directive on line %d!",
2015 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing job ID on line %d!", linenum
);
2019 jobid
= atoi(value
);
2023 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad job ID %d on line %d!", jobid
,
2028 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, jobid
);
2029 if (access(jobfile
, 0))
2031 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Job %d] Files have gone away!",
2036 job
= calloc(1, sizeof(cupsd_job_t
));
2039 cupsdLogMessage(CUPSD_LOG_EMERG
,
2040 "[Job %d] Unable to allocate memory for job!", jobid
);
2045 job
->back_pipes
[0] = -1;
2046 job
->back_pipes
[1] = -1;
2047 job
->print_pipes
[0] = -1;
2048 job
->print_pipes
[1] = -1;
2049 job
->side_pipes
[0] = -1;
2050 job
->side_pipes
[1] = -1;
2051 job
->status_pipes
[0] = -1;
2052 job
->status_pipes
[1] = -1;
2054 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Loading from cache...", job
->id
);
2058 cupsdLogMessage(CUPSD_LOG_ERROR
,
2059 "Missing <Job #> directive on line %d!", linenum
);
2062 else if (!strcasecmp(line
, "</Job>"))
2064 cupsArrayAdd(Jobs
, job
);
2066 if (job
->state_value
<= IPP_JOB_STOPPED
)
2068 cupsArrayAdd(ActiveJobs
, job
);
2076 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing value on line %d!", linenum
);
2079 else if (!strcasecmp(line
, "State"))
2081 job
->state_value
= (ipp_jstate_t
)atoi(value
);
2083 if (job
->state_value
< IPP_JOB_PENDING
)
2084 job
->state_value
= IPP_JOB_PENDING
;
2085 else if (job
->state_value
> IPP_JOB_COMPLETED
)
2086 job
->state_value
= IPP_JOB_COMPLETED
;
2088 else if (!strcasecmp(line
, "Priority"))
2090 job
->priority
= atoi(value
);
2092 else if (!strcasecmp(line
, "Username"))
2094 cupsdSetString(&job
->username
, value
);
2096 else if (!strcasecmp(line
, "Destination"))
2098 cupsdSetString(&job
->dest
, value
);
2100 else if (!strcasecmp(line
, "DestType"))
2102 job
->dtype
= (cups_ptype_t
)atoi(value
);
2104 else if (!strcasecmp(line
, "NumFiles"))
2106 job
->num_files
= atoi(value
);
2108 if (job
->num_files
< 0)
2110 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad NumFiles value %d on line %d!",
2111 job
->num_files
, linenum
);
2116 if (job
->num_files
> 0)
2118 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-001", RequestRoot
,
2120 if (access(jobfile
, 0))
2122 cupsdLogMessage(CUPSD_LOG_INFO
,
2123 "[Job %d] Data files have gone away!", job
->id
);
2128 job
->filetypes
= calloc(job
->num_files
, sizeof(mime_type_t
*));
2129 job
->compressions
= calloc(job
->num_files
, sizeof(int));
2131 if (!job
->filetypes
|| !job
->compressions
)
2133 cupsdLogMessage(CUPSD_LOG_EMERG
,
2134 "[Job %d] Unable to allocate memory for %d files!",
2135 job
->id
, job
->num_files
);
2140 else if (!strcasecmp(line
, "File"))
2142 int number
, /* File number */
2143 compression
; /* Compression value */
2144 char super
[MIME_MAX_SUPER
], /* MIME super type */
2145 type
[MIME_MAX_TYPE
]; /* MIME type */
2148 if (sscanf(value
, "%d%*[ \t]%15[^/]/%255s%d", &number
, super
, type
,
2151 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File on line %d!", linenum
);
2155 if (number
< 1 || number
> job
->num_files
)
2157 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File number %d on line %d!",
2164 job
->compressions
[number
] = compression
;
2165 job
->filetypes
[number
] = mimeType(MimeDatabase
, super
, type
);
2167 if (!job
->filetypes
[number
])
2170 * If the original MIME type is unknown, auto-type it!
2173 cupsdLogMessage(CUPSD_LOG_ERROR
,
2174 "[Job %d] Unknown MIME type %s/%s for file %d!",
2175 job
->id
, super
, type
, number
+ 1);
2177 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
2178 job
->id
, number
+ 1);
2179 job
->filetypes
[number
] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
2180 job
->compressions
+ number
);
2183 * If that didn't work, assume it is raw...
2186 if (!job
->filetypes
[number
])
2187 job
->filetypes
[number
] = mimeType(MimeDatabase
, "application",
2192 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unknown %s directive on line %d!",
2201 * 'load_next_job_id()' - Load the NextJobId value from the job.cache file.
2205 load_next_job_id(const char *filename
) /* I - job.cache filename */
2207 cups_file_t
*fp
; /* job.cache file */
2208 char line
[1024], /* Line buffer */
2209 *value
; /* Value on line */
2210 int linenum
; /* Line number in file */
2211 int next_job_id
; /* NextJobId value from line */
2215 * Read the NextJobId directive from the job.cache file and use
2216 * the value (if any).
2219 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
2221 if (errno
!= ENOENT
)
2222 cupsdLogMessage(CUPSD_LOG_ERROR
,
2223 "Unable to open job cache file \"%s\": %s",
2224 filename
, strerror(errno
));
2229 cupsdLogMessage(CUPSD_LOG_INFO
,
2230 "Loading NextJobId from job cache file \"%s\"...", filename
);
2234 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
2236 if (!strcasecmp(line
, "NextJobId"))
2240 next_job_id
= atoi(value
);
2242 if (next_job_id
> NextJobId
)
2243 NextJobId
= next_job_id
;
2254 * 'load_request_root()' - Load jobs from the RequestRoot directory.
2258 load_request_root(void)
2260 cups_dir_t
*dir
; /* Directory */
2261 cups_dentry_t
*dent
; /* Directory entry */
2262 cupsd_job_t
*job
; /* New job */
2266 * Open the requests directory...
2269 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Scanning %s for jobs...", RequestRoot
);
2271 if ((dir
= cupsDirOpen(RequestRoot
)) == NULL
)
2273 cupsdLogMessage(CUPSD_LOG_ERROR
,
2274 "Unable to open spool directory \"%s\": %s",
2275 RequestRoot
, strerror(errno
));
2280 * Read all the c##### files...
2283 while ((dent
= cupsDirRead(dir
)) != NULL
)
2284 if (strlen(dent
->filename
) >= 6 && dent
->filename
[0] == 'c')
2287 * Allocate memory for the job...
2290 if ((job
= calloc(sizeof(cupsd_job_t
), 1)) == NULL
)
2292 cupsdLogMessage(CUPSD_LOG_ERROR
, "Ran out of memory for jobs!");
2298 * Assign the job ID...
2301 job
->id
= atoi(dent
->filename
+ 1);
2302 job
->back_pipes
[0] = -1;
2303 job
->back_pipes
[1] = -1;
2304 job
->print_pipes
[0] = -1;
2305 job
->print_pipes
[1] = -1;
2306 job
->side_pipes
[0] = -1;
2307 job
->side_pipes
[1] = -1;
2308 job
->status_pipes
[0] = -1;
2309 job
->status_pipes
[1] = -1;
2311 if (job
->id
>= NextJobId
)
2312 NextJobId
= job
->id
+ 1;
2321 * Insert the job into the array, sorting by job priority and ID...
2324 cupsArrayAdd(Jobs
, job
);
2326 if (job
->state_value
<= IPP_JOB_STOPPED
)
2327 cupsArrayAdd(ActiveJobs
, job
);
2337 * 'set_time()' - Set one of the "time-at-xyz" attributes...
2341 set_time(cupsd_job_t
*job
, /* I - Job to update */
2342 const char *name
) /* I - Name of attribute */
2344 ipp_attribute_t
*attr
; /* Time attribute */
2347 if ((attr
= ippFindAttribute(job
->attrs
, name
, IPP_TAG_ZERO
)) != NULL
)
2349 attr
->value_tag
= IPP_TAG_INTEGER
;
2350 attr
->values
[0].integer
= time(NULL
);
2356 * 'set_hold_until()' - Set the hold time and update job-hold-until attribute...
2360 set_hold_until(cupsd_job_t
*job
, /* I - Job to update */
2361 time_t holdtime
) /* I - Hold until time */
2363 ipp_attribute_t
*attr
; /* job-hold-until attribute */
2364 struct tm
*holddate
; /* Hold date */
2365 char holdstr
[64]; /* Hold time */
2369 * Set the hold_until value and hold the job...
2372 cupsdLogMessage(CUPSD_LOG_DEBUG
, "set_hold_until: hold_until = %d",
2375 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
2376 job
->state_value
= IPP_JOB_HELD
;
2377 job
->hold_until
= holdtime
;
2380 * Update the job-hold-until attribute with a string representing GMT
2381 * time (HH:MM:SS)...
2384 holddate
= gmtime(&holdtime
);
2385 snprintf(holdstr
, sizeof(holdstr
), "%d:%d:%d", holddate
->tm_hour
,
2386 holddate
->tm_min
, holddate
->tm_sec
);
2388 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2389 IPP_TAG_KEYWORD
)) == NULL
)
2390 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2393 * Either add the attribute or update the value of the existing one
2397 attr
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
2398 "job-hold-until", NULL
, holdstr
);
2400 cupsdSetString(&attr
->values
[0].string
.text
, holdstr
);
2407 * 'start_job()' - Start a print job.
2411 start_job(cupsd_job_t
*job
, /* I - Job ID */
2412 cupsd_printer_t
*printer
) /* I - Printer to print job */
2414 int i
; /* Looping var */
2415 int slot
; /* Pipe slot */
2416 cups_array_t
*filters
, /* Filters for job */
2417 *prefilters
; /* Filters with prefilters */
2418 mime_filter_t
*filter
, /* Current filter */
2419 *prefilter
, /* Prefilter */
2420 port_monitor
; /* Port monitor filter */
2421 char method
[255], /* Method for output */
2422 *optptr
, /* Pointer to options */
2423 *valptr
; /* Pointer in value string */
2424 ipp_attribute_t
*attr
; /* Current attribute */
2425 struct stat backinfo
; /* Backend file information */
2426 int backroot
; /* Run backend as root? */
2427 int pid
; /* Process ID of new filter process */
2428 int banner_page
; /* 1 if banner page, 0 otherwise */
2429 int filterfds
[2][2];/* Pipes used between filters */
2430 int envc
; /* Number of environment variables */
2431 char **argv
, /* Filter command-line arguments */
2432 sani_uri
[1024], /* Sanitized DEVICE_URI env var */
2433 filename
[1024], /* Job filename */
2434 command
[1024], /* Full path to command */
2435 jobid
[255], /* Job ID string */
2436 title
[IPP_MAX_NAME
],
2437 /* Job title string */
2438 copies
[255], /* # copies string */
2439 *envp
[MAX_ENV
+ 15],
2440 /* Environment variables */
2441 charset
[255], /* CHARSET env variable */
2442 class_name
[255],/* CLASS env variable */
2443 classification
[1024],
2444 /* CLASSIFICATION env variable */
2446 /* CONTENT_TYPE env variable */
2448 /* DEVICE_URI env variable */
2449 final_content_type
[1024],
2450 /* FINAL_CONTENT_TYPE env variable */
2451 lang
[255], /* LANG env variable */
2452 ppd
[1024], /* PPD env variable */
2454 /* PRINTER env variable */
2456 /* RIP_MAX_CACHE env variable */
2457 static char *options
= NULL
;/* Full list of options */
2458 static int optlength
= 0; /* Length of option buffer */
2461 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] start_job: file = %d/%d",
2462 job
->id
, job
->current_file
, job
->num_files
);
2464 if (job
->num_files
== 0)
2466 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Job %d] No files, canceling job!",
2469 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2474 * Figure out what filters are required to convert from
2475 * the source to the destination type...
2484 * Remote jobs and raw queues go directly to the printer without
2488 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2489 "[Job %d] Sending job to queue tagged as raw...", job
->id
);
2496 * Local jobs get filtered...
2499 filters
= mimeFilter(MimeDatabase
, job
->filetypes
[job
->current_file
],
2500 printer
->filetype
, &(job
->cost
));
2504 cupsdLogMessage(CUPSD_LOG_ERROR
,
2505 "[Job %d] Unable to convert file %d to printable format!",
2506 job
->current_file
, job
->id
);
2507 cupsdLogMessage(CUPSD_LOG_INFO
,
2508 "Hint: Do you have ESP Ghostscript installed?");
2510 if (LogLevel
< CUPSD_LOG_DEBUG
)
2511 cupsdLogMessage(CUPSD_LOG_INFO
,
2512 "Hint: Try setting the LogLevel to \"debug\".");
2514 job
->current_file
++;
2516 if (job
->current_file
== job
->num_files
)
2517 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2523 * Remove NULL ("-") filters...
2526 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
2528 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
2529 if (!strcmp(filter
->filter
, "-"))
2530 cupsArrayRemove(filters
, filter
);
2532 if (cupsArrayCount(filters
) == 0)
2534 cupsArrayDelete(filters
);
2539 * If this printer has any pre-filters, insert the required pre-filter
2540 * in the filters array...
2543 if (printer
->prefiltertype
&& filters
)
2545 prefilters
= cupsArrayNew(NULL
, NULL
);
2547 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
2549 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
2551 if ((prefilter
= mimeFilterLookup(MimeDatabase
, filter
->src
,
2552 printer
->prefiltertype
)))
2554 cupsArrayAdd(prefilters
, prefilter
);
2555 job
->cost
+= prefilter
->cost
;
2558 cupsArrayAdd(prefilters
, filter
);
2561 cupsArrayDelete(filters
);
2562 filters
= prefilters
;
2567 * Set a minimum cost of 100 for all jobs so that FilterLimit
2568 * works with raw queues and other low-cost paths.
2571 if (job
->cost
< 100)
2575 * See if the filter cost is too high...
2578 if ((FilterLevel
+ job
->cost
) > FilterLimit
&& FilterLevel
> 0 &&
2582 * Don't print this job quite yet...
2585 cupsArrayDelete(filters
);
2587 cupsdLogMessage(CUPSD_LOG_INFO
,
2588 "[Job %d] Holding because filter limit has been reached.",
2590 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2591 "[Job %d] start_job: file=%d, cost=%d, level=%d, limit=%d",
2592 job
->id
, job
->current_file
, job
->cost
, FilterLevel
,
2597 FilterLevel
+= job
->cost
;
2600 * Add decompression/raw filter as needed...
2603 if ((!printer
->raw
&& job
->compressions
[job
->current_file
]) ||
2604 (!filters
&& !printer
->remote
&&
2605 (job
->num_files
> 1 || !strncmp(printer
->device_uri
, "file:", 5))))
2608 * Add gziptoany filter to the front of the list...
2612 filters
= cupsArrayNew(NULL
, NULL
);
2614 if (!cupsArrayInsert(filters
, &gziptoany_filter
))
2616 cupsdLogMessage(CUPSD_LOG_ERROR
,
2617 "[Job %d] Unable to add decompression filter - %s",
2618 job
->id
, strerror(errno
));
2620 cupsArrayDelete(filters
);
2622 job
->current_file
++;
2624 if (job
->current_file
== job
->num_files
)
2625 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2632 * Add port monitor, if any...
2635 if (printer
->port_monitor
)
2638 * Add port monitor to the end of the list...
2642 filters
= cupsArrayNew(NULL
, NULL
);
2644 if (!cupsArrayAdd(filters
, &port_monitor
))
2646 cupsdLogMessage(CUPSD_LOG_ERROR
,
2647 "[Job %d] Unable to add port monitor - %s",
2648 job
->id
, strerror(errno
));
2650 cupsArrayDelete(filters
);
2652 job
->current_file
++;
2654 if (job
->current_file
== job
->num_files
)
2655 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2660 snprintf(port_monitor
.filter
, sizeof(port_monitor
.filter
),
2661 "%s/monitor/%s", ServerBin
, printer
->port_monitor
);
2665 * Update the printer and job state to "processing"...
2668 job
->state
->values
[0].integer
= IPP_JOB_PROCESSING
;
2669 job
->state_value
= IPP_JOB_PROCESSING
;
2672 job
->printer
= printer
;
2675 cupsdSetPrinterState(printer
, IPP_PRINTER_PROCESSING
, 0);
2677 if (job
->current_file
== 0)
2680 * Set the processing time...
2683 set_time(job
, "time-at-processing");
2686 * Create the backchannel pipes and make them non-blocking...
2689 cupsdOpenPipe(job
->back_pipes
);
2691 fcntl(job
->back_pipes
[0], F_SETFL
,
2692 fcntl(job
->back_pipes
[0], F_GETFL
) | O_NONBLOCK
);
2694 fcntl(job
->back_pipes
[1], F_SETFL
,
2695 fcntl(job
->back_pipes
[1], F_GETFL
) | O_NONBLOCK
);
2698 * Create the side-channel pipes and make them non-blocking...
2701 socketpair(AF_LOCAL
, SOCK_STREAM
, 0, job
->side_pipes
);
2703 fcntl(job
->side_pipes
[0], F_SETFL
,
2704 fcntl(job
->side_pipes
[0], F_GETFL
) | O_NONBLOCK
);
2706 fcntl(job
->side_pipes
[1], F_SETFL
,
2707 fcntl(job
->side_pipes
[1], F_GETFL
) | O_NONBLOCK
);
2711 * Determine if we are printing a banner page or not...
2714 if (job
->job_sheets
== NULL
)
2716 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] No job-sheets attribute.",
2718 if ((job
->job_sheets
=
2719 ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_ZERO
)) != NULL
)
2720 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2721 "[Job %d] ... but someone added one without setting "
2722 "job_sheets!", job
->id
);
2724 else if (job
->job_sheets
->num_values
== 1)
2725 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] job-sheets=%s", job
->id
,
2726 job
->job_sheets
->values
[0].string
.text
);
2728 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] job-sheets=%s,%s", job
->id
,
2729 job
->job_sheets
->values
[0].string
.text
,
2730 job
->job_sheets
->values
[1].string
.text
);
2732 if (printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
))
2734 else if (job
->job_sheets
== NULL
)
2736 else if (strcasecmp(job
->job_sheets
->values
[0].string
.text
, "none") != 0 &&
2737 job
->current_file
== 0)
2739 else if (job
->job_sheets
->num_values
> 1 &&
2740 strcasecmp(job
->job_sheets
->values
[1].string
.text
, "none") != 0 &&
2741 job
->current_file
== (job
->num_files
- 1))
2746 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] banner_page = %d", job
->id
,
2750 * Building the options string is harder than it needs to be, but
2751 * for the moment we need to pass strings for command-line args and
2752 * not IPP attribute pointers... :)
2754 * First allocate/reallocate the option buffer as needed...
2757 i
= ipp_length(job
->attrs
);
2764 optptr
= realloc(options
, i
);
2768 cupsdLogMessage(CUPSD_LOG_CRIT
,
2769 "[Job %d] Unable to allocate %d bytes for option buffer!",
2772 cupsArrayDelete(filters
);
2774 FilterLevel
-= job
->cost
;
2776 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2785 * Now loop through the attributes and convert them to the textual
2786 * representation used by the filters...
2792 snprintf(title
, sizeof(title
), "%s-%d", printer
->name
, job
->id
);
2793 strcpy(copies
, "1");
2795 for (attr
= job
->attrs
->attrs
; attr
!= NULL
; attr
= attr
->next
)
2797 if (!strcmp(attr
->name
, "copies") &&
2798 attr
->value_tag
== IPP_TAG_INTEGER
)
2801 * Don't use the # copies attribute if we are printing the job sheets...
2805 sprintf(copies
, "%d", attr
->values
[0].integer
);
2807 else if (!strcmp(attr
->name
, "job-name") &&
2808 (attr
->value_tag
== IPP_TAG_NAME
||
2809 attr
->value_tag
== IPP_TAG_NAMELANG
))
2810 strlcpy(title
, attr
->values
[0].string
.text
, sizeof(title
));
2811 else if (attr
->group_tag
== IPP_TAG_JOB
)
2814 * Filter out other unwanted attributes...
2817 if (attr
->value_tag
== IPP_TAG_MIMETYPE
||
2818 attr
->value_tag
== IPP_TAG_NAMELANG
||
2819 attr
->value_tag
== IPP_TAG_TEXTLANG
||
2820 (attr
->value_tag
== IPP_TAG_URI
&& strcmp(attr
->name
, "job-uuid")) ||
2821 attr
->value_tag
== IPP_TAG_URISCHEME
||
2822 attr
->value_tag
== IPP_TAG_BEGIN_COLLECTION
) /* Not yet supported */
2825 if (!strncmp(attr
->name
, "time-", 5))
2828 if (!strncmp(attr
->name
, "job-", 4) && strcmp(attr
->name
, "job-uuid") &&
2829 !(printer
->type
& CUPS_PRINTER_REMOTE
))
2832 if (!strncmp(attr
->name
, "job-", 4) &&
2833 strcmp(attr
->name
, "job-uuid") &&
2834 strcmp(attr
->name
, "job-billing") &&
2835 strcmp(attr
->name
, "job-sheets") &&
2836 strcmp(attr
->name
, "job-hold-until") &&
2837 strcmp(attr
->name
, "job-priority"))
2840 if ((!strcmp(attr
->name
, "page-label") ||
2841 !strcmp(attr
->name
, "page-border") ||
2842 !strncmp(attr
->name
, "number-up", 9) ||
2843 !strcmp(attr
->name
, "page-ranges") ||
2844 !strcmp(attr
->name
, "page-set") ||
2845 !strcasecmp(attr
->name
, "AP_FIRSTPAGE_InputSlot") ||
2846 !strcasecmp(attr
->name
, "AP_FIRSTPAGE_ManualFeed")) &&
2851 * Otherwise add them to the list...
2854 if (optptr
> options
)
2855 strlcat(optptr
, " ", optlength
- (optptr
- options
));
2857 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
2859 strlcat(optptr
, attr
->name
, optlength
- (optptr
- options
));
2860 strlcat(optptr
, "=", optlength
- (optptr
- options
));
2863 for (i
= 0; i
< attr
->num_values
; i
++)
2866 strlcat(optptr
, ",", optlength
- (optptr
- options
));
2868 optptr
+= strlen(optptr
);
2870 switch (attr
->value_tag
)
2872 case IPP_TAG_INTEGER
:
2874 snprintf(optptr
, optlength
- (optptr
- options
),
2875 "%d", attr
->values
[i
].integer
);
2878 case IPP_TAG_BOOLEAN
:
2879 if (!attr
->values
[i
].boolean
)
2880 strlcat(optptr
, "no", optlength
- (optptr
- options
));
2882 case IPP_TAG_NOVALUE
:
2883 strlcat(optptr
, attr
->name
,
2884 optlength
- (optptr
- options
));
2887 case IPP_TAG_RANGE
:
2888 if (attr
->values
[i
].range
.lower
== attr
->values
[i
].range
.upper
)
2889 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2890 "%d", attr
->values
[i
].range
.lower
);
2892 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2893 "%d-%d", attr
->values
[i
].range
.lower
,
2894 attr
->values
[i
].range
.upper
);
2897 case IPP_TAG_RESOLUTION
:
2898 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2899 "%dx%d%s", attr
->values
[i
].resolution
.xres
,
2900 attr
->values
[i
].resolution
.yres
,
2901 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
2905 case IPP_TAG_STRING
:
2908 case IPP_TAG_KEYWORD
:
2909 case IPP_TAG_CHARSET
:
2910 case IPP_TAG_LANGUAGE
:
2912 for (valptr
= attr
->values
[i
].string
.text
; *valptr
;)
2914 if (strchr(" \t\n\\\'\"", *valptr
))
2916 *optptr
++ = *valptr
++;
2923 break; /* anti-compiler-warning-code */
2927 optptr
+= strlen(optptr
);
2932 * Build the command-line arguments for the filters. Each filter
2933 * has 6 or 7 arguments:
2937 * argv[2] = username
2939 * argv[4] = # copies
2941 * argv[6] = filename (optional; normally stdin)
2943 * This allows legacy printer drivers that use the old System V
2944 * printing interface to be used by CUPS.
2946 * For remote jobs, we send all of the files in the argument list.
2949 if (printer
->remote
&& job
->num_files
> 1)
2950 argv
= calloc(7 + job
->num_files
, sizeof(char *));
2952 argv
= calloc(8, sizeof(char *));
2954 sprintf(jobid
, "%d", job
->id
);
2956 argv
[0] = printer
->name
;
2958 argv
[2] = job
->username
;
2963 if (printer
->remote
&& job
->num_files
> 1)
2965 for (i
= 0; i
< job
->num_files
; i
++)
2967 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
2969 argv
[6 + i
] = strdup(filename
);
2974 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
2975 job
->id
, job
->current_file
+ 1);
2979 for (i
= 0; argv
[i
]; i
++)
2980 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2981 "[Job %d] argv[%d]=\"%s\"", job
->id
, i
, argv
[i
]);
2984 * Create environment variable strings for the filters...
2987 attr
= ippFindAttribute(job
->attrs
, "attributes-natural-language",
2990 switch (strlen(attr
->values
[0].string
.text
))
2994 * This is an unknown or badly formatted language code; use
2995 * the POSIX locale...
2998 strcpy(lang
, "LANG=C");
3003 * Just the language code (ll)...
3006 snprintf(lang
, sizeof(lang
), "LANG=%s",
3007 attr
->values
[0].string
.text
);
3012 * Language and country code (ll-cc)...
3015 snprintf(lang
, sizeof(lang
), "LANG=%c%c_%c%c",
3016 attr
->values
[0].string
.text
[0],
3017 attr
->values
[0].string
.text
[1],
3018 toupper(attr
->values
[0].string
.text
[3] & 255),
3019 toupper(attr
->values
[0].string
.text
[4] & 255));
3023 attr
= ippFindAttribute(job
->attrs
, "document-format",
3026 (optptr
= strstr(attr
->values
[0].string
.text
, "charset=")) != NULL
)
3027 snprintf(charset
, sizeof(charset
), "CHARSET=%s", optptr
+ 8);
3030 attr
= ippFindAttribute(job
->attrs
, "attributes-charset",
3032 snprintf(charset
, sizeof(charset
), "CHARSET=%s",
3033 attr
->values
[0].string
.text
);
3036 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s/%s",
3037 job
->filetypes
[job
->current_file
]->super
,
3038 job
->filetypes
[job
->current_file
]->type
);
3039 snprintf(device_uri
, sizeof(device_uri
), "DEVICE_URI=%s",
3040 printer
->device_uri
);
3041 cupsdSanitizeURI(printer
->device_uri
, sani_uri
, sizeof(sani_uri
));
3042 snprintf(ppd
, sizeof(ppd
), "PPD=%s/ppd/%s.ppd", ServerRoot
, printer
->name
);
3043 snprintf(printer_name
, sizeof(printer_name
), "PRINTER=%s", printer
->name
);
3044 snprintf(rip_max_cache
, sizeof(rip_max_cache
), "RIP_MAX_CACHE=%s", RIPCache
);
3046 envc
= cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
3048 envp
[envc
++] = charset
;
3049 envp
[envc
++] = lang
;
3050 envp
[envc
++] = ppd
;
3051 envp
[envc
++] = rip_max_cache
;
3052 envp
[envc
++] = content_type
;
3053 envp
[envc
++] = device_uri
;
3054 envp
[envc
++] = printer_name
;
3056 if (!printer
->remote
&& !printer
->raw
&&
3057 (filter
= (mime_filter_t
*)cupsArrayLast(filters
)) != NULL
&&
3060 snprintf(final_content_type
, sizeof(final_content_type
),
3061 "FINAL_CONTENT_TYPE=%s/%s",
3062 filter
->dst
->super
, filter
->dst
->type
);
3063 envp
[envc
++] = final_content_type
;
3066 if (Classification
&& !banner_page
)
3068 if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
3069 IPP_TAG_NAME
)) == NULL
)
3070 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3072 else if (attr
->num_values
> 1 &&
3073 strcmp(attr
->values
[1].string
.text
, "none") != 0)
3074 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3075 attr
->values
[1].string
.text
);
3077 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3078 attr
->values
[0].string
.text
);
3080 envp
[envc
++] = classification
;
3083 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
3085 snprintf(class_name
, sizeof(class_name
), "CLASS=%s", job
->dest
);
3086 envp
[envc
++] = class_name
;
3089 if (job
->auth_username
)
3090 envp
[envc
++] = job
->auth_username
;
3091 if (job
->auth_domain
)
3092 envp
[envc
++] = job
->auth_domain
;
3093 if (job
->auth_password
)
3094 envp
[envc
++] = job
->auth_password
;
3098 envp
[envc
++] = job
->ccname
;
3099 #endif /* HAVE_GSSAPI */
3103 for (i
= 0; i
< envc
; i
++)
3104 if (!strncmp(envp
[i
], "AUTH_", 5))
3105 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] envp[%d]=\"AUTH_%c****\"",
3106 job
->id
, i
, envp
[i
][5]);
3107 else if (strncmp(envp
[i
], "DEVICE_URI=", 11))
3108 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] envp[%d]=\"%s\"",
3109 job
->id
, i
, envp
[i
]);
3111 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] envp[%d]=\"DEVICE_URI=%s\"",
3112 job
->id
, i
, sani_uri
);
3114 if (printer
->remote
)
3115 job
->current_file
= job
->num_files
;
3117 job
->current_file
++;
3120 * Now create processes for all of the filters...
3123 filterfds
[0][0] = -1;
3124 filterfds
[0][1] = -1;
3125 filterfds
[1][0] = -1;
3126 filterfds
[1][1] = -1;
3128 if (!job
->status_buffer
)
3130 if (cupsdOpenPipe(job
->status_pipes
))
3132 cupsdLogMessage(CUPSD_LOG_ERROR
,
3133 "[Job %d] Unable to create job status pipes - %s.",
3134 job
->id
, strerror(errno
));
3135 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3136 "Unable to create status pipes - %s.", strerror(errno
));
3138 cupsdAddPrinterHistory(printer
);
3140 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3141 "Job canceled because the server could not create the job "
3147 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3148 "[Job %d] start_job: status_pipes = [ %d %d ]",
3149 job
->id
, job
->status_pipes
[0], job
->status_pipes
[1]);
3151 job
->status_buffer
= cupsdStatBufNew(job
->status_pipes
[0], "[Job %d]",
3153 job
->status_level
= CUPSD_LOG_INFO
;
3157 memset(job
->filters
, 0, sizeof(job
->filters
));
3159 for (i
= 0, slot
= 0, filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
3161 i
++, filter
= (mime_filter_t
*)cupsArrayNext(filters
))
3163 if (filter
->filter
[0] != '/')
3164 snprintf(command
, sizeof(command
), "%s/filter/%s", ServerBin
,
3167 strlcpy(command
, filter
->filter
, sizeof(command
));
3169 if (i
< (cupsArrayCount(filters
) - 1))
3171 if (cupsdOpenPipe(filterfds
[slot
]))
3173 cupsdLogMessage(CUPSD_LOG_ERROR
,
3174 "[Job %d] Unable to create job filter pipes - %s.",
3175 job
->id
, strerror(errno
));
3176 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3177 "Unable to create filter pipes - %s.", strerror(errno
));
3178 cupsdAddPrinterHistory(printer
);
3180 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3181 "Job canceled because the server could not create the "
3189 if (job
->current_file
== 1)
3191 if (strncmp(printer
->device_uri
, "file:", 5) != 0)
3193 if (cupsdOpenPipe(job
->print_pipes
))
3195 cupsdLogMessage(CUPSD_LOG_ERROR
,
3196 "[Job %d] Unable to create job backend pipes - %s.",
3197 job
->id
, strerror(errno
));
3198 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3199 "Unable to create backend pipes - %s.", strerror(errno
));
3200 cupsdAddPrinterHistory(printer
);
3202 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3203 "Job canceled because the server could not create "
3204 "the backend pipes.");
3211 job
->print_pipes
[0] = -1;
3212 if (!strcmp(printer
->device_uri
, "file:/dev/null") ||
3213 !strcmp(printer
->device_uri
, "file:///dev/null"))
3214 job
->print_pipes
[1] = -1;
3217 if (!strncmp(printer
->device_uri
, "file:/dev/", 10))
3218 job
->print_pipes
[1] = open(printer
->device_uri
+ 5,
3220 else if (!strncmp(printer
->device_uri
, "file:///dev/", 12))
3221 job
->print_pipes
[1] = open(printer
->device_uri
+ 7,
3223 else if (!strncmp(printer
->device_uri
, "file:///", 8))
3224 job
->print_pipes
[1] = open(printer
->device_uri
+ 7,
3225 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
3227 job
->print_pipes
[1] = open(printer
->device_uri
+ 5,
3228 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
3230 if (job
->print_pipes
[1] < 0)
3232 cupsdLogMessage(CUPSD_LOG_ERROR
,
3233 "[Job %d] Unable to open output file \"%s\" - %s.",
3234 job
->id
, printer
->device_uri
, strerror(errno
));
3235 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3236 "Unable to open output file \"%s\" - %s.",
3237 printer
->device_uri
, strerror(errno
));
3239 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3240 "Job canceled because the server could not open the "
3246 fcntl(job
->print_pipes
[1], F_SETFD
,
3247 fcntl(job
->print_pipes
[1], F_GETFD
) | FD_CLOEXEC
);
3251 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3252 "[Job %d] start_job: print_pipes = [ %d %d ]",
3253 job
->id
, job
->print_pipes
[0], job
->print_pipes
[1]);
3256 filterfds
[slot
][0] = job
->print_pipes
[0];
3257 filterfds
[slot
][1] = job
->print_pipes
[1];
3260 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] start_job: filter=\"%s\"",
3262 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3263 "[Job %d] start_job: filterfds[%d]=[ %d %d ]",
3264 job
->id
, slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3266 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
3267 filterfds
[slot
][1], job
->status_pipes
[1],
3268 job
->back_pipes
[0], job
->side_pipes
[0], 0,
3271 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3272 "[Job %d] start_job: Closing filter pipes for slot %d "
3274 job
->id
, !slot
, filterfds
[!slot
][0], filterfds
[!slot
][1]);
3276 cupsdClosePipe(filterfds
[!slot
]);
3280 cupsdLogMessage(CUPSD_LOG_ERROR
,
3281 "[Job %d] Unable to start filter \"%s\" - %s.",
3282 job
->id
, filter
->filter
, strerror(errno
));
3283 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3284 "Unable to start filter \"%s\" - %s.",
3285 filter
->filter
, strerror(errno
));
3287 cupsdAddPrinterHistory(printer
);
3289 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3290 "Job canceled because the server could not execute a "
3296 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Started filter %s (PID %d)",
3297 job
->id
, command
, pid
);
3303 cupsArrayDelete(filters
);
3306 * Finally, pipe the final output into a backend process if needed...
3309 if (strncmp(printer
->device_uri
, "file:", 5) != 0)
3311 if (job
->current_file
== 1)
3313 sscanf(printer
->device_uri
, "%254[^:]", method
);
3314 snprintf(command
, sizeof(command
), "%s/backend/%s", ServerBin
, method
);
3317 * See if the backend needs to run as root...
3322 else if (stat(command
, &backinfo
))
3325 backroot
= !(backinfo
.st_mode
& (S_IRWXG
| S_IRWXO
));
3329 filterfds
[slot
][0] = -1;
3330 filterfds
[slot
][1] = -1;
3332 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] start_job: backend=\"%s\"",
3334 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3335 "[Job %d] start_job: filterfds[%d] = [ %d %d ]", job
->id
,
3336 slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3338 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
3339 filterfds
[slot
][1], job
->status_pipes
[1],
3340 job
->back_pipes
[1], job
->side_pipes
[1],
3341 backroot
, &(job
->backend
));
3345 cupsdLogMessage(CUPSD_LOG_ERROR
,
3346 "[Job %d] Unable to start backend \"%s\" - %s.",
3347 job
->id
, method
, strerror(errno
));
3348 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3349 "Unable to start backend \"%s\" - %s.", method
,
3352 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3353 "Job canceled because the server could not execute "
3360 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Started backend %s (PID %d)",
3361 job
->id
, command
, pid
);
3365 if (job
->current_file
== job
->num_files
)
3367 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3368 "[Job %d] start_job: Closing print pipes [ %d %d ]...",
3369 job
->id
, job
->print_pipes
[0], job
->print_pipes
[1]);
3371 cupsdClosePipe(job
->print_pipes
);
3373 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3374 "[Job %d] start_job: Closing back pipes [ %d %d ]...",
3375 job
->id
, job
->back_pipes
[0], job
->back_pipes
[1]);
3377 cupsdClosePipe(job
->back_pipes
);
3379 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3380 "[Job %d] start_job: Closing side pipes [ %d %d ]...",
3381 job
->id
, job
->side_pipes
[0], job
->side_pipes
[1]);
3383 cupsdClosePipe(job
->side_pipes
);
3385 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3386 "[Job %d] start_job: Closing status output pipe %d...",
3387 job
->id
, job
->status_pipes
[1]);
3389 close(job
->status_pipes
[1]);
3390 job
->status_pipes
[1] = -1;
3395 filterfds
[slot
][0] = -1;
3396 filterfds
[slot
][1] = -1;
3398 if (job
->current_file
== job
->num_files
)
3400 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3401 "[Job %d] start_job: Closing print pipes [ %d %d ]...",
3402 job
->id
, job
->print_pipes
[0], job
->print_pipes
[1]);
3404 cupsdClosePipe(job
->print_pipes
);
3406 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3407 "[Job %d] start_job: Closing status output pipe %d...",
3408 job
->id
, job
->status_pipes
[1]);
3410 close(job
->status_pipes
[1]);
3411 job
->status_pipes
[1] = -1;
3415 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3416 "[Job %d] start_job: Closing filter pipes for slot %d "
3418 job
->id
, slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3419 cupsdClosePipe(filterfds
[slot
]);
3421 if (printer
->remote
&& job
->num_files
> 1)
3423 for (i
= 0; i
< job
->num_files
; i
++)
3429 cupsdAddSelect(job
->status_buffer
->fd
, (cupsd_selfunc_t
)update_job
, NULL
,
3432 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
, "Job #%d started.",
3439 * If we get here, we need to abort the current job and close out all
3440 * files and pipes...
3445 for (slot
= 0; slot
< 2; slot
++)
3447 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3448 "[Job %d] start_job: Closing filter pipes for slot %d "
3450 job
->id
, slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3451 cupsdClosePipe(filterfds
[slot
]);
3454 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3455 "[Job %d] start_job: Closing status pipes [ %d %d ]...",
3456 job
->id
, job
->status_pipes
[0], job
->status_pipes
[1]);
3457 cupsdClosePipe(job
->status_pipes
);
3458 cupsdStatBufDelete(job
->status_buffer
);
3460 job
->status_buffer
= NULL
;
3462 cupsArrayDelete(filters
);
3464 if (printer
->remote
&& job
->num_files
> 1)
3466 for (i
= 0; i
< job
->num_files
; i
++)
3472 cupsdStopJob(job
, 0);
3477 * 'unload_job()' - Unload a job from memory.
3481 unload_job(cupsd_job_t
*job
) /* I - Job */
3486 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Unloading...", job
->id
);
3488 ippDelete(job
->attrs
);
3493 job
->job_sheets
= NULL
;
3494 job
->printer_message
= NULL
;
3495 job
->printer_reasons
= NULL
;
3500 * 'update_job()' - Read a status update from a job's filters.
3504 update_job(cupsd_job_t
*job
) /* I - Job to check */
3506 int i
; /* Looping var */
3507 int copies
; /* Number of copies printed */
3508 char message
[1024], /* Message text */
3509 *ptr
; /* Pointer update... */
3510 int loglevel
, /* Log level for message */
3511 event
= 0; /* Events? */
3514 while ((ptr
= cupsdStatBufUpdate(job
->status_buffer
, &loglevel
,
3515 message
, sizeof(message
))) != NULL
)
3518 * Process page and printer state messages as needed...
3521 if (loglevel
== CUPSD_LOG_PAGE
)
3524 * Page message; send the message to the page_log file and update the
3525 * job sheet count...
3528 if (job
->sheets
!= NULL
)
3530 if (!strncasecmp(message
, "total ", 6))
3533 * Got a total count of pages from a backend or filter...
3536 copies
= atoi(message
+ 6);
3537 copies
-= job
->sheets
->values
[0].integer
; /* Just track the delta */
3539 else if (!sscanf(message
, "%*d%d", &copies
))
3542 job
->sheets
->values
[0].integer
+= copies
;
3544 if (job
->printer
->page_limit
)
3546 cupsd_quota_t
*q
= cupsdUpdateQuota(job
->printer
, job
->username
,
3550 if (AppleQuotas
&& q
->page_count
== -3)
3553 * Quota limit exceeded, cancel job in progress immediately...
3556 cupsdLogMessage(CUPSD_LOG_INFO
,
3557 "[Job %d] Canceled because pages exceed user %s "
3558 "quota limit on printer %s (%s).",
3559 job
->id
, job
->username
, job
->printer
->name
,
3560 job
->printer
->info
);
3562 cupsdCancelJob(job
, 1, IPP_JOB_CANCELED
);
3567 #endif /* __APPLE__ */
3571 cupsdLogPage(job
, message
);
3573 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
3574 "Printed %d page(s).", job
->sheets
->values
[0].integer
);
3576 else if (loglevel
== CUPSD_LOG_STATE
)
3578 if (!strcmp(message
, "paused"))
3580 cupsdStopPrinter(job
->printer
, 1);
3585 cupsdSetPrinterReasons(job
->printer
, message
);
3586 cupsdAddPrinterHistory(job
->printer
);
3587 event
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
3590 update_job_attrs(job
);
3592 else if (loglevel
== CUPSD_LOG_ATTR
)
3595 * Set attribute(s)...
3598 int num_attrs
; /* Number of attributes */
3599 cups_option_t
*attrs
; /* Attributes */
3600 const char *attr
; /* Attribute */
3603 num_attrs
= cupsParseOptions(message
, 0, &attrs
);
3605 if ((attr
= cupsGetOption("auth-info-required", num_attrs
,
3608 cupsdSetAuthInfoRequired(job
->printer
, attr
, NULL
);
3609 cupsdSaveAllPrinters();
3612 if ((attr
= cupsGetOption("printer-alert", num_attrs
, attrs
)) != NULL
)
3614 cupsdSetString(&job
->printer
->alert
, attr
);
3615 event
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
3618 if ((attr
= cupsGetOption("printer-alert-description", num_attrs
,
3621 cupsdSetString(&job
->printer
->alert_description
, attr
);
3622 event
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
3625 cupsFreeOptions(num_attrs
, attrs
);
3628 else if (!strncmp(message
, "recoverable:", 12))
3630 cupsdSetPrinterReasons(job
->printer
,
3631 "+com.apple.print.recoverable-warning");
3634 while (isspace(*ptr
& 255))
3637 cupsdSetString(&job
->printer
->recoverable
, ptr
);
3638 cupsdAddPrinterHistory(job
->printer
);
3639 event
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
3641 else if (!strncmp(message
, "recovered:", 10))
3643 cupsdSetPrinterReasons(job
->printer
,
3644 "-com.apple.print.recoverable-warning");
3647 while (isspace(*ptr
& 255))
3650 cupsdSetString(&job
->printer
->recoverable
, ptr
);
3651 cupsdAddPrinterHistory(job
->printer
);
3652 event
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
3654 #endif /* __APPLE__ */
3655 else if (loglevel
<= job
->status_level
)
3658 * Some message to show in the printer-state-message attribute...
3661 if (loglevel
!= CUPSD_LOG_NOTICE
)
3662 job
->status_level
= loglevel
;
3664 strlcpy(job
->printer
->state_message
, message
,
3665 sizeof(job
->printer
->state_message
));
3666 cupsdAddPrinterHistory(job
->printer
);
3667 event
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
3669 update_job_attrs(job
);
3672 if (!strchr(job
->status_buffer
->buffer
, '\n'))
3676 if ((event
& CUPSD_EVENT_PRINTER_STATE_CHANGED
))
3677 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE_CHANGED
, job
->printer
, NULL
,
3678 (job
->printer
->type
& CUPS_PRINTER_CLASS
) ?
3679 "Class \"%s\" state changed." :
3680 "Printer \"%s\" state changed.",
3681 job
->printer
->name
);
3683 if (ptr
== NULL
&& !job
->status_buffer
->bufused
)
3686 * See if all of the filters and the backend have returned their
3690 for (i
= 0; job
->filters
[i
] < 0; i
++);
3692 if (job
->filters
[i
])
3695 if (job
->current_file
>= job
->num_files
&& job
->backend
> 0)
3699 * Handle the end of job stuff...
3702 cupsdFinishJob(job
);
3708 * 'update_job_attrs()' - Update the job-printer-* attributes.
3712 update_job_attrs(cupsd_job_t
*job
) /* I - Job to update */
3714 int i
; /* Looping var */
3715 int num_reasons
; /* Actual number of reasons */
3716 const char * const *reasons
; /* Reasons */
3717 static const char *none
= "none", /* "none" */
3723 * Get/create the job-printer-state-* attributes...
3726 if (!job
->printer_message
)
3728 if ((job
->printer_message
= ippFindAttribute(job
->attrs
,
3729 "job-printer-state-message",
3730 IPP_TAG_TEXT
)) == NULL
)
3731 job
->printer_message
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_TEXT
,
3732 "job-printer-state-message",
3736 if (!job
->printer_reasons
)
3737 job
->printer_reasons
= ippFindAttribute(job
->attrs
,
3738 "job-printer-state-reasons",
3742 * If the job isn't printing, return now...
3749 * Otherwise copy the printer-state-message value...
3752 if (job
->printer
->state_message
[0])
3753 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
),
3754 job
->printer
->state_message
);
3757 * ... and the printer-state-reasons value...
3760 if (job
->printer
->num_reasons
== 0)
3763 reasons
= job
->printer
->state
== IPP_PRINTER_STOPPED
? &paused
: &none
;
3767 num_reasons
= job
->printer
->num_reasons
;
3768 reasons
= (const char * const *)job
->printer
->reasons
;
3771 if (!job
->printer_reasons
|| job
->printer_reasons
->num_values
!= num_reasons
)
3773 ippDeleteAttribute(job
->attrs
, job
->printer_reasons
);
3775 job
->printer_reasons
= ippAddStrings(job
->attrs
,
3776 IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
3777 "job-printer-state-reasons",
3778 num_reasons
, NULL
, NULL
);
3781 for (i
= 0; i
< num_reasons
; i
++)
3782 cupsdSetString(&(job
->printer_reasons
->values
[i
].string
.text
), reasons
[i
]);
3787 * End of "$Id: job.c 6755 2007-08-01 19:02:47Z mike $".