2 * "$Id: job.c 6735 2007-07-26 18:38:29Z 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
);
233 * Remove the print file for good if we aren't preserving jobs or
237 job
->current_file
= 0;
239 if (!JobHistory
|| !JobFiles
|| purge
|| (job
->dtype
& CUPS_PRINTER_REMOTE
))
241 for (i
= 1; i
<= job
->num_files
; i
++)
243 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
248 if (job
->num_files
> 0)
250 free(job
->filetypes
);
251 free(job
->compressions
);
254 job
->filetypes
= NULL
;
255 job
->compressions
= NULL
;
259 if (JobHistory
&& !purge
&& !(job
->dtype
& CUPS_PRINTER_REMOTE
))
262 * Save job state info...
270 * Remove the job info file...
273 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
,
278 * Remove the job from the "all jobs" list...
281 cupsArrayRemove(Jobs
, job
);
284 * Free all memory used...
293 * 'cupsdCancelJobs()' - Cancel all jobs for the given destination/user...
297 cupsdCancelJobs(const char *dest
, /* I - Destination to cancel */
298 const char *username
, /* I - Username or NULL */
299 int purge
) /* I - Purge jobs? */
301 cupsd_job_t
*job
; /* Current job */
304 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
306 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
307 if ((dest
== NULL
|| !strcmp(job
->dest
, dest
)) &&
308 (username
== NULL
|| !strcmp(job
->username
, username
)))
311 * Cancel all jobs matching this destination/user...
314 cupsdCancelJob(job
, purge
, IPP_JOB_CANCELED
);
322 * 'cupsdCheckJobs()' - Check the pending jobs and start any if the destination
329 cupsd_job_t
*job
; /* Current job in queue */
330 cupsd_printer_t
*printer
, /* Printer destination */
331 *pclass
; /* Printer class destination */
334 DEBUG_puts("cupsdCheckJobs()");
336 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
337 "cupsdCheckJobs: %d active jobs, sleeping=%d, reload=%d",
338 cupsArrayCount(ActiveJobs
), Sleeping
, NeedReload
);
340 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
342 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
345 * Start held jobs if they are ready...
348 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
349 "cupsdCheckJobs: Job %d: state_value=%d, loaded=%s",
350 job
->id
, job
->state_value
, job
->attrs
? "yes" : "no");
352 if (job
->state_value
== IPP_JOB_HELD
&&
354 job
->hold_until
< time(NULL
))
356 if (job
->pending_timeout
)
357 cupsdTimeoutJob(job
); /* Add trailing banner as needed */
359 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
360 job
->state_value
= IPP_JOB_PENDING
;
364 * Start pending jobs if the destination is available...
367 if (job
->state_value
== IPP_JOB_PENDING
&& !NeedReload
&& !Sleeping
)
369 printer
= cupsdFindDest(job
->dest
);
373 (printer
->type
& (CUPS_PRINTER_IMPLICIT
| CUPS_PRINTER_CLASS
)))
376 * If the class is remote, just pass it to the remote server...
381 if (pclass
->state
== IPP_PRINTER_STOPPED
)
383 else if (pclass
->type
& CUPS_PRINTER_REMOTE
)
386 printer
= cupsdFindAvailablePrinter(printer
->name
);
389 if (!printer
&& !pclass
)
392 * Whoa, the printer and/or class for this destination went away;
396 cupsdLogMessage(CUPSD_LOG_WARN
,
397 "[Job %d] Printer/class %s has gone away; canceling job!",
400 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
401 "Job canceled because the destination printer/class has "
404 cupsdCancelJob(job
, 1, IPP_JOB_ABORTED
);
409 * See if the printer is available or remote and not printing a job;
410 * if so, start the job...
416 * Add/update a job-actual-printer-uri attribute for this job
417 * so that we know which printer actually printed the job...
420 ipp_attribute_t
*attr
; /* job-actual-printer-uri attribute */
423 if ((attr
= ippFindAttribute(job
->attrs
, "job-actual-printer-uri",
424 IPP_TAG_URI
)) != NULL
)
425 cupsdSetString(&attr
->values
[0].string
.text
, printer
->uri
);
427 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
,
428 "job-actual-printer-uri", NULL
, printer
->uri
);
431 if ((!(printer
->type
& CUPS_PRINTER_DISCOVERED
) && /* Printer is local */
432 printer
->state
== IPP_PRINTER_IDLE
) || /* and idle */
433 ((printer
->type
& CUPS_PRINTER_DISCOVERED
) && /* Printer is remote */
434 !printer
->job
)) /* and not printing */
437 * Clear any message and reasons for the queue...
440 printer
->state_message
[0] = '\0';
442 cupsdSetPrinterReasons(printer
, "none");
448 start_job(job
, printer
);
457 * 'cupsdCleanJobs()' - Clean out old jobs.
463 cupsd_job_t
*job
; /* Current job */
469 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
470 job
&& cupsArrayCount(Jobs
) >= MaxJobs
;
471 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
472 if (job
->state_value
>= IPP_JOB_CANCELED
)
473 cupsdCancelJob(job
, 1, IPP_JOB_CANCELED
);
478 * 'cupsdFinishJob()' - Finish a job.
482 cupsdFinishJob(cupsd_job_t
*job
) /* I - Job */
484 cupsd_printer_t
*printer
; /* Current printer */
485 ipp_attribute_t
*attr
; /* job-hold-until attribute */
488 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] File %d is complete.",
489 job
->id
, job
->current_file
- 1);
491 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
492 "[Job %d] cupsdFinishJob: job->status is %d",
493 job
->id
, job
->status
);
495 if (job
->status_buffer
&&
496 (job
->status
< 0 || job
->current_file
>= job
->num_files
))
499 * Close the pipe and clear the input bit.
502 cupsdRemoveSelect(job
->status_buffer
->fd
);
504 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
505 "[Job %d] cupsdFinishJob: Closing status pipes [ %d %d ]...",
506 job
->id
, job
->status_pipes
[0], job
->status_pipes
[1]);
508 cupsdClosePipe(job
->status_pipes
);
509 cupsdStatBufDelete(job
->status_buffer
);
511 job
->status_buffer
= NULL
;
514 printer
= job
->printer
;
516 update_job_attrs(job
);
521 * Backend had errors; stop it...
524 int exit_code
; /* Exit code from backend */
528 * Convert the status to an exit code. Due to the way the W* macros are
529 * implemented on MacOS X (bug?), we have to store the exit status in a
530 * variable first and then convert...
533 exit_code
= -job
->status
;
534 if (WIFEXITED(exit_code
))
535 exit_code
= WEXITSTATUS(exit_code
);
537 exit_code
= job
->status
;
539 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Backend returned status %d (%s)",
541 exit_code
== CUPS_BACKEND_FAILED
? "failed" :
542 exit_code
== CUPS_BACKEND_AUTH_REQUIRED
?
543 "authentication required" :
544 exit_code
== CUPS_BACKEND_HOLD
? "hold job" :
545 exit_code
== CUPS_BACKEND_STOP
? "stop printer" :
546 exit_code
== CUPS_BACKEND_CANCEL
? "cancel job" :
547 exit_code
< 0 ? "crashed" : "unknown");
550 * Do what needs to be done...
556 case CUPS_BACKEND_FAILED
:
558 * Backend failure, use the error-policy to determine how to
562 cupsdStopJob(job
, 0);
564 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
567 * Mark the job as pending again - we'll retry on another
571 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
572 job
->state_value
= IPP_JOB_PENDING
;
578 * If the job was queued to a class, try requeuing it... For
579 * faxes and retry-job queues, hold the current job for 5 minutes.
582 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
584 else if ((printer
->type
& CUPS_PRINTER_FAX
) ||
585 !strcmp(printer
->error_policy
, "retry-job"))
588 * See how many times we've tried to send the job; if more than
589 * the limit, cancel the job.
594 if (job
->tries
>= JobRetryLimit
)
600 cupsdLogMessage(CUPSD_LOG_ERROR
,
601 "[Job %d] Canceling job since it could not be "
602 "sent after %d tries.",
603 job
->id
, JobRetryLimit
);
605 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
610 * Try again in N seconds...
613 set_hold_until(job
, time(NULL
) + JobRetryInterval
);
615 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
,
616 "Job held due to fax errors; please consult "
617 "the error_log file for details.");
618 cupsdSetPrinterState(printer
, IPP_PRINTER_IDLE
, 0);
621 else if (!strcmp(printer
->error_policy
, "abort-job"))
622 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
625 cupsdSetPrinterState(printer
, IPP_PRINTER_STOPPED
, 1);
627 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
628 "Job stopped due to backend errors; please consult "
629 "the error_log file for details.");
633 case CUPS_BACKEND_CANCEL
:
638 cupsdCancelJob(job
, 0, IPP_JOB_CANCELED
);
641 case CUPS_BACKEND_HOLD
:
646 cupsdStopJob(job
, 0);
648 cupsdSetJobHoldUntil(job
, "indefinite");
650 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
651 job
->state_value
= IPP_JOB_HELD
;
655 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
656 "Job held due to backend errors; please consult "
657 "the error_log file for details.");
660 case CUPS_BACKEND_STOP
:
662 * Stop the printer...
665 cupsdStopJob(job
, 0);
667 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
668 job
->state_value
= IPP_JOB_PENDING
;
671 cupsdSetPrinterState(printer
, IPP_PRINTER_STOPPED
, 1);
673 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
674 "Job stopped due to backend errors; please consult "
675 "the error_log file for details.");
678 case CUPS_BACKEND_AUTH_REQUIRED
:
679 cupsdStopJob(job
, 0);
681 cupsdSetJobHoldUntil(job
, "auth-info-required");
683 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
684 IPP_TAG_KEYWORD
)) == NULL
)
685 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
689 attr
->value_tag
= IPP_TAG_KEYWORD
;
690 cupsdSetString(&(attr
->values
[0].string
.text
),
691 "auth-info-required");
694 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
695 job
->state_value
= IPP_JOB_HELD
;
699 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
700 "Authentication is required for job %d.", job
->id
);
705 * Try printing another job...
710 else if (job
->status
> 0)
713 * Filter had errors; stop job...
716 cupsdLogMessage(CUPSD_LOG_ERROR
,
717 "[Job %d] Job stopped due to filter errors.", job
->id
);
718 cupsdStopJob(job
, 1);
720 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
721 "Job stopped due to filter errors; please consult the "
722 "error_log file for details.");
728 * Job printed successfully; cancel it...
731 if (job
->current_file
< job
->num_files
)
734 * Start the next file in the job...
737 FilterLevel
-= job
->cost
;
738 start_job(job
, printer
);
743 * Close out this job...
746 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Completed successfully.",
748 cupsdCancelJob(job
, 0, IPP_JOB_COMPLETED
);
756 * 'cupsdFreeAllJobs()' - Free all jobs from memory.
760 cupsdFreeAllJobs(void)
762 cupsd_job_t
*job
; /* Current job */
773 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
775 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
777 cupsArrayRemove(Jobs
, job
);
778 cupsArrayRemove(ActiveJobs
, job
);
783 cupsdReleaseSignals();
788 * 'cupsdFindJob()' - Find the specified job.
791 cupsd_job_t
* /* O - Job data */
792 cupsdFindJob(int id
) /* I - Job ID */
794 cupsd_job_t key
; /* Search key */
799 return ((cupsd_job_t
*)cupsArrayFind(Jobs
, &key
));
804 * 'cupsdGetPrinterJobCount()' - Get the number of pending, processing,
805 * or held jobs in a printer or class.
808 int /* O - Job count */
809 cupsdGetPrinterJobCount(
810 const char *dest
) /* I - Printer or class name */
812 int count
; /* Job count */
813 cupsd_job_t
*job
; /* Current job */
816 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
818 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
819 if (job
->dest
&& !strcasecmp(job
->dest
, dest
))
827 * 'cupsdGetUserJobCount()' - Get the number of pending, processing,
828 * or held jobs for a user.
831 int /* O - Job count */
832 cupsdGetUserJobCount(
833 const char *username
) /* I - Username */
835 int count
; /* Job count */
836 cupsd_job_t
*job
; /* Current job */
839 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
841 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
842 if (!strcasecmp(job
->username
, username
))
850 * 'cupsdHoldJob()' - Hold the specified job.
854 cupsdHoldJob(cupsd_job_t
*job
) /* I - Job data */
856 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdHoldJob: id = %d", job
->id
);
858 if (job
->state_value
== IPP_JOB_PROCESSING
)
859 cupsdStopJob(job
, 0);
863 DEBUG_puts("cupsdHoldJob: setting state to held...");
865 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
866 job
->state_value
= IPP_JOB_HELD
;
875 * 'cupsdLoadAllJobs()' - Load all jobs from disk.
879 cupsdLoadAllJobs(void)
881 char filename
[1024]; /* Full filename of job.cache file */
882 struct stat fileinfo
, /* Information on job.cache file */
883 dirinfo
; /* Information on RequestRoot dir */
888 * Create the job arrays as needed...
892 Jobs
= cupsArrayNew(compare_jobs
, NULL
);
895 ActiveJobs
= cupsArrayNew(compare_active_jobs
, NULL
);
898 * See whether the job.cache file is older than the RequestRoot directory...
901 snprintf(filename
, sizeof(filename
), "%s/job.cache", CacheDir
);
903 if (stat(filename
, &fileinfo
))
905 fileinfo
.st_mtime
= 0;
908 cupsdLogMessage(CUPSD_LOG_ERROR
,
909 "Unable to get file information for \"%s\" - %s",
910 filename
, strerror(errno
));
913 if (stat(RequestRoot
, &dirinfo
))
915 dirinfo
.st_mtime
= 0;
918 cupsdLogMessage(CUPSD_LOG_ERROR
,
919 "Unable to get directory information for \"%s\" - %s",
920 RequestRoot
, strerror(errno
));
924 * Load the most recent source for job data...
927 if (dirinfo
.st_mtime
> fileinfo
.st_mtime
)
931 load_next_job_id(filename
);
934 load_job_cache(filename
);
937 * Clean out old jobs as needed...
940 if (MaxJobs
> 0 && cupsArrayCount(Jobs
) >= MaxJobs
)
946 * 'cupsdLoadJob()' - Load a single job...
950 cupsdLoadJob(cupsd_job_t
*job
) /* I - Job */
952 char jobfile
[1024]; /* Job filename */
953 cups_file_t
*fp
; /* Job file */
954 int fileid
; /* Current file ID */
955 ipp_attribute_t
*attr
; /* Job attribute */
956 const char *dest
; /* Destination name */
957 cupsd_printer_t
*destptr
; /* Pointer to destination */
958 mime_type_t
**filetypes
; /* New filetypes array */
959 int *compressions
; /* New compressions array */
964 if (job
->state_value
> IPP_JOB_STOPPED
)
965 job
->access_time
= time(NULL
);
970 if ((job
->attrs
= ippNew()) == NULL
)
972 cupsdLogMessage(CUPSD_LOG_ERROR
,
973 "[Job %d] Ran out of memory for job attributes!", job
->id
);
978 * Load job attributes...
981 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Loading attributes...", job
->id
);
983 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, job
->id
);
984 if ((fp
= cupsFileOpen(jobfile
, "r")) == NULL
)
986 cupsdLogMessage(CUPSD_LOG_ERROR
,
987 "[Job %d] Unable to open job control file \"%s\" - %s!",
988 job
->id
, jobfile
, strerror(errno
));
989 ippDelete(job
->attrs
);
994 if (ippReadIO(fp
, (ipp_iocb_t
)cupsFileRead
, 1, NULL
, job
->attrs
) != IPP_DATA
)
996 cupsdLogMessage(CUPSD_LOG_ERROR
,
997 "[Job %d] Unable to read job control file \"%s\"!",
1000 ippDelete(job
->attrs
);
1009 * Copy attribute data to the job object...
1012 if ((job
->state
= ippFindAttribute(job
->attrs
, "job-state",
1013 IPP_TAG_ENUM
)) == NULL
)
1015 cupsdLogMessage(CUPSD_LOG_ERROR
,
1016 "[Job %d] Missing or bad job-state attribute in "
1019 ippDelete(job
->attrs
);
1025 job
->state_value
= (ipp_jstate_t
)job
->state
->values
[0].integer
;
1029 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1030 IPP_TAG_URI
)) == NULL
)
1032 cupsdLogMessage(CUPSD_LOG_ERROR
,
1033 "[Job %d] No job-printer-uri attribute in control file!",
1035 ippDelete(job
->attrs
);
1041 if ((dest
= cupsdValidateDest(attr
->values
[0].string
.text
, &(job
->dtype
),
1044 cupsdLogMessage(CUPSD_LOG_ERROR
,
1045 "[Job %d] Unable to queue job for destination \"%s\"!",
1046 job
->id
, attr
->values
[0].string
.text
);
1047 ippDelete(job
->attrs
);
1053 cupsdSetString(&job
->dest
, dest
);
1055 else if ((destptr
= cupsdFindDest(job
->dest
)) == NULL
)
1057 cupsdLogMessage(CUPSD_LOG_ERROR
,
1058 "[Job %d] Unable to queue job for destination \"%s\"!",
1059 job
->id
, job
->dest
);
1060 ippDelete(job
->attrs
);
1066 job
->sheets
= ippFindAttribute(job
->attrs
, "job-media-sheets-completed",
1068 job
->job_sheets
= ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_NAME
);
1072 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
1073 IPP_TAG_INTEGER
)) == NULL
)
1075 cupsdLogMessage(CUPSD_LOG_ERROR
,
1076 "[Job %d] Missing or bad job-priority attribute in "
1077 "control file!", job
->id
);
1078 ippDelete(job
->attrs
);
1084 job
->priority
= attr
->values
[0].integer
;
1089 if ((attr
= ippFindAttribute(job
->attrs
, "job-originating-user-name",
1090 IPP_TAG_NAME
)) == NULL
)
1092 cupsdLogMessage(CUPSD_LOG_ERROR
,
1093 "[Job %d] Missing or bad job-originating-user-name "
1094 "attribute in control file!", job
->id
);
1095 ippDelete(job
->attrs
);
1101 cupsdSetString(&job
->username
, attr
->values
[0].string
.text
);
1105 * Set the job hold-until time and state...
1108 if (job
->state_value
== IPP_JOB_HELD
)
1110 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1111 IPP_TAG_KEYWORD
)) == NULL
)
1112 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1115 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
1118 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1119 job
->state_value
= IPP_JOB_PENDING
;
1122 else if (job
->state_value
== IPP_JOB_PROCESSING
)
1124 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1125 job
->state_value
= IPP_JOB_PENDING
;
1128 if (!job
->num_files
)
1131 * Find all the d##### files...
1134 for (fileid
= 1; fileid
< 10000; fileid
++)
1136 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
1139 if (access(jobfile
, 0))
1142 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1143 "[Job %d] Auto-typing document file \"%s\"...",
1146 if (fileid
> job
->num_files
)
1148 if (job
->num_files
== 0)
1150 compressions
= (int *)calloc(fileid
, sizeof(int));
1151 filetypes
= (mime_type_t
**)calloc(fileid
, sizeof(mime_type_t
*));
1155 compressions
= (int *)realloc(job
->compressions
,
1156 sizeof(int) * fileid
);
1157 filetypes
= (mime_type_t
**)realloc(job
->filetypes
,
1158 sizeof(mime_type_t
*) *
1162 if (!compressions
|| !filetypes
)
1164 cupsdLogMessage(CUPSD_LOG_ERROR
,
1165 "[Job %d] Ran out of memory for job file types!",
1170 job
->compressions
= compressions
;
1171 job
->filetypes
= filetypes
;
1172 job
->num_files
= fileid
;
1175 job
->filetypes
[fileid
- 1] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
1176 job
->compressions
+ fileid
- 1);
1178 if (!job
->filetypes
[fileid
- 1])
1179 job
->filetypes
[fileid
- 1] = mimeType(MimeDatabase
, "application",
1185 * Load authentication information as needed...
1188 if (job
->state_value
< IPP_JOB_STOPPED
)
1190 snprintf(jobfile
, sizeof(jobfile
), "%s/a%05d", RequestRoot
, job
->id
);
1192 cupsdClearString(&job
->auth_username
);
1193 cupsdClearString(&job
->auth_domain
);
1194 cupsdClearString(&job
->auth_password
);
1196 if ((fp
= cupsFileOpen(jobfile
, "r")) != NULL
)
1198 int i
, /* Looping var */
1199 bytes
; /* Size of auth data */
1200 char line
[255], /* Line from file */
1201 data
[255]; /* Decoded data */
1205 i
< destptr
->num_auth_info_required
&&
1206 cupsFileGets(fp
, line
, sizeof(line
));
1209 bytes
= sizeof(data
);
1210 httpDecode64_2(data
, &bytes
, line
);
1212 if (!strcmp(destptr
->auth_info_required
[i
], "username"))
1213 cupsdSetStringf(&job
->auth_username
, "AUTH_USERNAME=%s", data
);
1214 else if (!strcmp(destptr
->auth_info_required
[i
], "domain"))
1215 cupsdSetStringf(&job
->auth_domain
, "AUTH_DOMAIN=%s", data
);
1216 else if (!strcmp(destptr
->auth_info_required
[i
], "password"))
1217 cupsdSetStringf(&job
->auth_password
, "AUTH_PASSWORD=%s", data
);
1223 job
->access_time
= time(NULL
);
1228 * 'cupsdMoveJob()' - Move the specified job to a different destination.
1232 cupsdMoveJob(cupsd_job_t
*job
, /* I - Job */
1233 cupsd_printer_t
*p
) /* I - Destination printer or class */
1235 ipp_attribute_t
*attr
; /* job-printer-uri attribute */
1236 const char *olddest
; /* Old destination */
1237 cupsd_printer_t
*oldp
; /* Old pointer */
1241 * Don't move completed jobs...
1244 if (job
->state_value
> IPP_JOB_STOPPED
)
1248 * Get the old destination...
1251 olddest
= job
->dest
;
1254 oldp
= job
->printer
;
1256 oldp
= cupsdFindDest(olddest
);
1259 * Change the destination information...
1262 if (job
->state_value
== IPP_JOB_PROCESSING
)
1263 cupsdStopJob(job
, 0);
1267 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, oldp
, job
,
1268 "Job #%d moved from %s to %s.", job
->id
, olddest
,
1271 cupsdSetString(&job
->dest
, p
->name
);
1272 job
->dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_REMOTE
|
1273 CUPS_PRINTER_IMPLICIT
);
1275 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1276 IPP_TAG_URI
)) != NULL
)
1277 cupsdSetString(&(attr
->values
[0].string
.text
), p
->uri
);
1279 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, p
, job
,
1280 "Job #%d moved from %s to %s.", job
->id
, olddest
,
1288 * 'cupsdReleaseJob()' - Release the specified job.
1292 cupsdReleaseJob(cupsd_job_t
*job
) /* I - Job */
1294 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdReleaseJob: id = %d", job
->id
);
1296 if (job
->state_value
== IPP_JOB_HELD
)
1298 DEBUG_puts("cupsdReleaseJob: setting state to pending...");
1300 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1301 job
->state_value
= IPP_JOB_PENDING
;
1309 * 'cupsdRestartJob()' - Restart the specified job.
1313 cupsdRestartJob(cupsd_job_t
*job
) /* I - Job */
1315 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdRestartJob: id = %d", job
->id
);
1317 if (job
->state_value
== IPP_JOB_STOPPED
|| job
->num_files
)
1319 ipp_jstate_t old_state
; /* Old job state */
1324 old_state
= job
->state_value
;
1327 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1328 job
->state_value
= IPP_JOB_PENDING
;
1332 if (old_state
> IPP_JOB_STOPPED
)
1333 cupsArrayAdd(ActiveJobs
, job
);
1341 * 'cupsdSaveAllJobs()' - Save a summary of all jobs to disk.
1345 cupsdSaveAllJobs(void)
1347 int i
; /* Looping var */
1348 cups_file_t
*fp
; /* Job cache file */
1349 char temp
[1024]; /* Temporary string */
1350 cupsd_job_t
*job
; /* Current job */
1351 time_t curtime
; /* Current time */
1352 struct tm
*curdate
; /* Current date */
1355 snprintf(temp
, sizeof(temp
), "%s/job.cache", CacheDir
);
1356 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
1358 cupsdLogMessage(CUPSD_LOG_ERROR
,
1359 "Unable to create job cache file \"%s\" - %s",
1360 temp
, strerror(errno
));
1364 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving job cache file \"%s\"...", temp
);
1367 * Restrict access to the file...
1370 fchown(cupsFileNumber(fp
), getuid(), Group
);
1371 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
1374 * Write a small header to the file...
1377 curtime
= time(NULL
);
1378 curdate
= localtime(&curtime
);
1379 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
1381 cupsFilePuts(fp
, "# Job cache file for " CUPS_SVERSION
"\n");
1382 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
1383 cupsFilePrintf(fp
, "NextJobId %d\n", NextJobId
);
1386 * Write each job known to the system...
1389 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1391 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1393 cupsFilePrintf(fp
, "<Job %d>\n", job
->id
);
1394 cupsFilePrintf(fp
, "State %d\n", job
->state_value
);
1395 cupsFilePrintf(fp
, "Priority %d\n", job
->priority
);
1396 cupsFilePrintf(fp
, "Username %s\n", job
->username
);
1397 cupsFilePrintf(fp
, "Destination %s\n", job
->dest
);
1398 cupsFilePrintf(fp
, "DestType %d\n", job
->dtype
);
1399 cupsFilePrintf(fp
, "NumFiles %d\n", job
->num_files
);
1400 for (i
= 0; i
< job
->num_files
; i
++)
1401 cupsFilePrintf(fp
, "File %d %s/%s %d\n", i
+ 1, job
->filetypes
[i
]->super
,
1402 job
->filetypes
[i
]->type
, job
->compressions
[i
]);
1403 cupsFilePuts(fp
, "</Job>\n");
1411 * 'cupsdSaveJob()' - Save a job to disk.
1415 cupsdSaveJob(cupsd_job_t
*job
) /* I - Job */
1417 char filename
[1024]; /* Job control filename */
1418 cups_file_t
*fp
; /* Job file */
1421 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSaveJob(job=%p(%d)): job->attrs=%p",
1422 job
, job
->id
, job
->attrs
);
1424 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
, job
->id
);
1426 if ((fp
= cupsFileOpen(filename
, "w")) == NULL
)
1428 cupsdLogMessage(CUPSD_LOG_ERROR
,
1429 "[Job %d] Unable to create job control file \"%s\" - %s.",
1430 job
->id
, filename
, strerror(errno
));
1434 fchmod(cupsFileNumber(fp
), 0600);
1435 fchown(cupsFileNumber(fp
), RunUser
, Group
);
1437 job
->attrs
->state
= IPP_IDLE
;
1439 if (ippWriteIO(fp
, (ipp_iocb_t
)cupsFileWrite
, 1, NULL
,
1440 job
->attrs
) != IPP_DATA
)
1441 cupsdLogMessage(CUPSD_LOG_ERROR
,
1442 "[Job %d] Unable to write job control file!", job
->id
);
1449 * 'cupsdSetJobHoldUntil()' - Set the hold time for a job...
1453 cupsdSetJobHoldUntil(cupsd_job_t
*job
, /* I - Job */
1454 const char *when
) /* I - When to resume */
1456 time_t curtime
; /* Current time */
1457 struct tm
*curdate
; /* Current date */
1458 int hour
; /* Hold hour */
1459 int minute
; /* Hold minute */
1460 int second
; /* Hold second */
1463 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetJobHoldUntil(%d, \"%s\")",
1468 if (!strcmp(when
, "indefinite") || !strcmp(when
, "auth-info-required"))
1471 * Hold indefinitely...
1474 job
->hold_until
= 0;
1476 else if (!strcmp(when
, "day-time"))
1479 * Hold to 6am the next morning unless local time is < 6pm.
1482 curtime
= time(NULL
);
1483 curdate
= localtime(&curtime
);
1485 if (curdate
->tm_hour
< 18)
1486 job
->hold_until
= curtime
;
1488 job
->hold_until
= curtime
+
1489 ((29 - curdate
->tm_hour
) * 60 + 59 -
1490 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1492 else if (!strcmp(when
, "evening") || !strcmp(when
, "night"))
1495 * Hold to 6pm unless local time is > 6pm or < 6am.
1498 curtime
= time(NULL
);
1499 curdate
= localtime(&curtime
);
1501 if (curdate
->tm_hour
< 6 || curdate
->tm_hour
>= 18)
1502 job
->hold_until
= curtime
;
1504 job
->hold_until
= curtime
+
1505 ((17 - curdate
->tm_hour
) * 60 + 59 -
1506 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1508 else if (!strcmp(when
, "second-shift"))
1511 * Hold to 4pm unless local time is > 4pm.
1514 curtime
= time(NULL
);
1515 curdate
= localtime(&curtime
);
1517 if (curdate
->tm_hour
>= 16)
1518 job
->hold_until
= curtime
;
1520 job
->hold_until
= curtime
+
1521 ((15 - curdate
->tm_hour
) * 60 + 59 -
1522 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1524 else if (!strcmp(when
, "third-shift"))
1527 * Hold to 12am unless local time is < 8am.
1530 curtime
= time(NULL
);
1531 curdate
= localtime(&curtime
);
1533 if (curdate
->tm_hour
< 8)
1534 job
->hold_until
= curtime
;
1536 job
->hold_until
= curtime
+
1537 ((23 - curdate
->tm_hour
) * 60 + 59 -
1538 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1540 else if (!strcmp(when
, "weekend"))
1543 * Hold to weekend unless we are in the weekend.
1546 curtime
= time(NULL
);
1547 curdate
= localtime(&curtime
);
1549 if (curdate
->tm_wday
|| curdate
->tm_wday
== 6)
1550 job
->hold_until
= curtime
;
1552 job
->hold_until
= curtime
+
1553 (((5 - curdate
->tm_wday
) * 24 +
1554 (17 - curdate
->tm_hour
)) * 60 + 59 -
1555 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1557 else if (sscanf(when
, "%d:%d:%d", &hour
, &minute
, &second
) >= 2)
1560 * Hold to specified GMT time (HH:MM or HH:MM:SS)...
1563 curtime
= time(NULL
);
1564 curdate
= gmtime(&curtime
);
1566 job
->hold_until
= curtime
+
1567 ((hour
- curdate
->tm_hour
) * 60 + minute
-
1568 curdate
->tm_min
) * 60 + second
- curdate
->tm_sec
;
1571 * Hold until next day as needed...
1574 if (job
->hold_until
< curtime
)
1575 job
->hold_until
+= 24 * 60 * 60;
1578 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetJobHoldUntil: hold_until = %d",
1579 (int)job
->hold_until
);
1584 * 'cupsdSetJobPriority()' - Set the priority of a job, moving it up/down in
1585 * the list as needed.
1589 cupsdSetJobPriority(
1590 cupsd_job_t
*job
, /* I - Job ID */
1591 int priority
) /* I - New priority (0 to 100) */
1593 ipp_attribute_t
*attr
; /* Job attribute */
1597 * Don't change completed jobs...
1600 if (job
->state_value
>= IPP_JOB_PROCESSING
)
1604 * Set the new priority and re-add the job into the active list...
1607 cupsArrayRemove(ActiveJobs
, job
);
1609 job
->priority
= priority
;
1611 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
1612 IPP_TAG_INTEGER
)) != NULL
)
1613 attr
->values
[0].integer
= priority
;
1615 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-priority",
1618 cupsArrayAdd(ActiveJobs
, job
);
1625 * 'cupsdStopAllJobs()' - Stop all print jobs.
1629 cupsdStopAllJobs(int force
) /* I - 1 = Force all filters to stop */
1631 cupsd_job_t
*job
; /* Current job */
1634 DEBUG_puts("cupsdStopAllJobs()");
1636 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
1638 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
1639 if (job
->state_value
== IPP_JOB_PROCESSING
)
1641 cupsdStopJob(job
, force
);
1642 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1643 job
->state_value
= IPP_JOB_PENDING
;
1649 * 'cupsdStopJob()' - Stop a print job.
1653 cupsdStopJob(cupsd_job_t
*job
, /* I - Job */
1654 int force
) /* I - 1 = Force all filters to stop */
1656 int i
; /* Looping var */
1659 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1660 "[Job %d] cupsdStopJob: force = %d", job
->id
, force
);
1662 if (job
->state_value
!= IPP_JOB_PROCESSING
)
1665 FilterLevel
-= job
->cost
;
1667 if (job
->printer
->state
== IPP_PRINTER_PROCESSING
)
1668 cupsdSetPrinterState(job
->printer
, IPP_PRINTER_IDLE
, 0);
1670 job
->state
->values
[0].integer
= IPP_JOB_STOPPED
;
1671 job
->state_value
= IPP_JOB_STOPPED
;
1672 job
->printer
->job
= NULL
;
1673 job
->printer
= NULL
;
1675 job
->current_file
--;
1677 for (i
= 0; job
->filters
[i
]; i
++)
1678 if (job
->filters
[i
] > 0)
1680 cupsdEndProcess(job
->filters
[i
], force
);
1681 job
->filters
[i
] = 0;
1684 if (job
->backend
> 0)
1686 cupsdEndProcess(job
->backend
, force
);
1690 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] Closing print pipes [ %d %d ]...",
1691 job
->id
, job
->print_pipes
[0], job
->print_pipes
[1]);
1693 cupsdClosePipe(job
->print_pipes
);
1695 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] Closing back pipes [ %d %d ]...",
1696 job
->id
, job
->back_pipes
[0], job
->back_pipes
[1]);
1698 cupsdClosePipe(job
->back_pipes
);
1700 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] Closing side pipes [ %d %d ]...",
1701 job
->id
, job
->side_pipes
[0], job
->side_pipes
[1]);
1703 cupsdClosePipe(job
->side_pipes
);
1705 if (job
->status_buffer
)
1708 * Close the pipe and clear the input bit.
1711 cupsdRemoveSelect(job
->status_buffer
->fd
);
1713 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1714 "[Job %d] Closing status pipes [ %d %d ]...", job
->id
,
1715 job
->status_pipes
[0], job
->status_pipes
[1]);
1717 cupsdClosePipe(job
->status_pipes
);
1718 cupsdStatBufDelete(job
->status_buffer
);
1720 job
->status_buffer
= NULL
;
1726 * 'cupsdUnloadCompletedJobs()' - Flush completed job history from memory.
1730 cupsdUnloadCompletedJobs(void)
1732 cupsd_job_t
*job
; /* Current job */
1733 time_t expire
; /* Expiration time */
1736 expire
= time(NULL
) - 60;
1738 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1740 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1741 if (job
->attrs
&& job
->state_value
>= IPP_JOB_STOPPED
&&
1742 job
->access_time
< expire
)
1748 * 'compare_active_jobs()' - Compare the job IDs and priorities of two jobs.
1751 static int /* O - Difference */
1752 compare_active_jobs(void *first
, /* I - First job */
1753 void *second
, /* I - Second job */
1754 void *data
) /* I - App data (not used) */
1756 int diff
; /* Difference */
1759 if ((diff
= ((cupsd_job_t
*)second
)->priority
-
1760 ((cupsd_job_t
*)first
)->priority
) != 0)
1763 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
1768 * 'compare_jobs()' - Compare the job IDs of two jobs.
1771 static int /* O - Difference */
1772 compare_jobs(void *first
, /* I - First job */
1773 void *second
, /* I - Second job */
1774 void *data
) /* I - App data (not used) */
1776 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
1781 * 'free_job()' - Free all memory used by a job.
1785 free_job(cupsd_job_t
*job
) /* I - Job */
1787 cupsdClearString(&job
->username
);
1788 cupsdClearString(&job
->dest
);
1789 cupsdClearString(&job
->auth_username
);
1790 cupsdClearString(&job
->auth_domain
);
1791 cupsdClearString(&job
->auth_password
);
1796 * First erase the credential cache file, then clear the string referencing
1797 * it. We know the filename since the string will be of the form
1798 * "KRB5CCNAME=FILE:/foo/bar"...
1801 if (cupsdRemoveFile(job
->ccname
+ 16) && errno
!= ENOENT
)
1802 cupsdLogMessage(CUPSD_LOG_ERROR
,
1803 "[Job %d] Unable to remove Kerberos credential cache: %s",
1804 job
->id
, strerror(errno
));
1806 cupsdClearString(&job
->ccname
);
1808 #endif /* HAVE_GSSAPI */
1810 if (job
->num_files
> 0)
1812 free(job
->compressions
);
1813 free(job
->filetypes
);
1816 ippDelete(job
->attrs
);
1823 * 'ipp_length()' - Compute the size of the buffer needed to hold
1824 * the textual IPP attributes.
1827 static int /* O - Size of attribute buffer */
1828 ipp_length(ipp_t
*ipp
) /* I - IPP request */
1830 int bytes
; /* Number of bytes */
1831 int i
; /* Looping var */
1832 ipp_attribute_t
*attr
; /* Current attribute */
1836 * Loop through all attributes...
1841 for (attr
= ipp
->attrs
; attr
!= NULL
; attr
= attr
->next
)
1844 * Skip attributes that won't be sent to filters...
1847 if (attr
->value_tag
== IPP_TAG_MIMETYPE
||
1848 attr
->value_tag
== IPP_TAG_NAMELANG
||
1849 attr
->value_tag
== IPP_TAG_TEXTLANG
||
1850 attr
->value_tag
== IPP_TAG_URI
||
1851 attr
->value_tag
== IPP_TAG_URISCHEME
)
1854 if (strncmp(attr
->name
, "time-", 5) == 0)
1858 * Add space for a leading space and commas between each value.
1859 * For the first attribute, the leading space isn't used, so the
1860 * extra byte can be used as the nul terminator...
1863 bytes
++; /* " " separator */
1864 bytes
+= attr
->num_values
; /* "," separators */
1867 * Boolean attributes appear as "foo,nofoo,foo,nofoo", while
1868 * other attributes appear as "foo=value1,value2,...,valueN".
1871 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
1872 bytes
+= strlen(attr
->name
);
1874 bytes
+= attr
->num_values
* strlen(attr
->name
);
1877 * Now add the size required for each value in the attribute...
1880 switch (attr
->value_tag
)
1882 case IPP_TAG_INTEGER
:
1885 * Minimum value of a signed integer is -2147483647, or 11 digits.
1888 bytes
+= attr
->num_values
* 11;
1891 case IPP_TAG_BOOLEAN
:
1893 * Add two bytes for each false ("no") value...
1896 for (i
= 0; i
< attr
->num_values
; i
++)
1897 if (!attr
->values
[i
].boolean
)
1901 case IPP_TAG_RANGE
:
1903 * A range is two signed integers separated by a hyphen, or
1904 * 23 characters max.
1907 bytes
+= attr
->num_values
* 23;
1910 case IPP_TAG_RESOLUTION
:
1912 * A resolution is two signed integers separated by an "x" and
1913 * suffixed by the units, or 26 characters max.
1916 bytes
+= attr
->num_values
* 26;
1919 case IPP_TAG_STRING
:
1922 case IPP_TAG_KEYWORD
:
1923 case IPP_TAG_CHARSET
:
1924 case IPP_TAG_LANGUAGE
:
1927 * Strings can contain characters that need quoting. We need
1928 * at least 2 * len + 2 characters to cover the quotes and
1929 * any backslashes in the string.
1932 for (i
= 0; i
< attr
->num_values
; i
++)
1933 bytes
+= 2 * strlen(attr
->values
[i
].string
.text
) + 2;
1937 break; /* anti-compiler-warning-code */
1946 * 'load_job_cache()' - Load jobs from the job.cache file.
1950 load_job_cache(const char *filename
) /* I - job.cache filename */
1952 cups_file_t
*fp
; /* job.cache file */
1953 char line
[1024], /* Line buffer */
1954 *value
; /* Value on line */
1955 int linenum
; /* Line number in file */
1956 cupsd_job_t
*job
; /* Current job */
1957 int jobid
; /* Job ID */
1958 char jobfile
[1024]; /* Job filename */
1962 * Open the job.cache file...
1965 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
1967 if (errno
!= ENOENT
)
1968 cupsdLogMessage(CUPSD_LOG_ERROR
,
1969 "Unable to open job cache file \"%s\": %s",
1970 filename
, strerror(errno
));
1972 load_request_root();
1978 * Read entries from the job cache file and create jobs as needed.
1981 cupsdLogMessage(CUPSD_LOG_INFO
, "Loading job cache file \"%s\"...",
1987 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
1989 if (!strcasecmp(line
, "NextJobId"))
1992 NextJobId
= atoi(value
);
1994 else if (!strcasecmp(line
, "<Job"))
1998 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing </Job> directive on line %d!",
2005 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing job ID on line %d!", linenum
);
2009 jobid
= atoi(value
);
2013 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad job ID %d on line %d!", jobid
,
2018 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, jobid
);
2019 if (access(jobfile
, 0))
2021 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Job %d] Files have gone away!",
2026 job
= calloc(1, sizeof(cupsd_job_t
));
2029 cupsdLogMessage(CUPSD_LOG_EMERG
,
2030 "[Job %d] Unable to allocate memory for job!", jobid
);
2035 job
->back_pipes
[0] = -1;
2036 job
->back_pipes
[1] = -1;
2037 job
->print_pipes
[0] = -1;
2038 job
->print_pipes
[1] = -1;
2039 job
->side_pipes
[0] = -1;
2040 job
->side_pipes
[1] = -1;
2041 job
->status_pipes
[0] = -1;
2042 job
->status_pipes
[1] = -1;
2044 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Loading from cache...", job
->id
);
2048 cupsdLogMessage(CUPSD_LOG_ERROR
,
2049 "Missing <Job #> directive on line %d!", linenum
);
2052 else if (!strcasecmp(line
, "</Job>"))
2054 cupsArrayAdd(Jobs
, job
);
2056 if (job
->state_value
<= IPP_JOB_STOPPED
)
2058 cupsArrayAdd(ActiveJobs
, job
);
2066 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing value on line %d!", linenum
);
2069 else if (!strcasecmp(line
, "State"))
2071 job
->state_value
= (ipp_jstate_t
)atoi(value
);
2073 if (job
->state_value
< IPP_JOB_PENDING
)
2074 job
->state_value
= IPP_JOB_PENDING
;
2075 else if (job
->state_value
> IPP_JOB_COMPLETED
)
2076 job
->state_value
= IPP_JOB_COMPLETED
;
2078 else if (!strcasecmp(line
, "Priority"))
2080 job
->priority
= atoi(value
);
2082 else if (!strcasecmp(line
, "Username"))
2084 cupsdSetString(&job
->username
, value
);
2086 else if (!strcasecmp(line
, "Destination"))
2088 cupsdSetString(&job
->dest
, value
);
2090 else if (!strcasecmp(line
, "DestType"))
2092 job
->dtype
= (cups_ptype_t
)atoi(value
);
2094 else if (!strcasecmp(line
, "NumFiles"))
2096 job
->num_files
= atoi(value
);
2098 if (job
->num_files
< 0)
2100 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad NumFiles value %d on line %d!",
2101 job
->num_files
, linenum
);
2106 if (job
->num_files
> 0)
2108 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-001", RequestRoot
,
2110 if (access(jobfile
, 0))
2112 cupsdLogMessage(CUPSD_LOG_INFO
,
2113 "[Job %d] Data files have gone away!", job
->id
);
2118 job
->filetypes
= calloc(job
->num_files
, sizeof(mime_type_t
*));
2119 job
->compressions
= calloc(job
->num_files
, sizeof(int));
2121 if (!job
->filetypes
|| !job
->compressions
)
2123 cupsdLogMessage(CUPSD_LOG_EMERG
,
2124 "[Job %d] Unable to allocate memory for %d files!",
2125 job
->id
, job
->num_files
);
2130 else if (!strcasecmp(line
, "File"))
2132 int number
, /* File number */
2133 compression
; /* Compression value */
2134 char super
[MIME_MAX_SUPER
], /* MIME super type */
2135 type
[MIME_MAX_TYPE
]; /* MIME type */
2138 if (sscanf(value
, "%d%*[ \t]%15[^/]/%255s%d", &number
, super
, type
,
2141 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File on line %d!", linenum
);
2145 if (number
< 1 || number
> job
->num_files
)
2147 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File number %d on line %d!",
2154 job
->compressions
[number
] = compression
;
2155 job
->filetypes
[number
] = mimeType(MimeDatabase
, super
, type
);
2157 if (!job
->filetypes
[number
])
2160 * If the original MIME type is unknown, auto-type it!
2163 cupsdLogMessage(CUPSD_LOG_ERROR
,
2164 "[Job %d] Unknown MIME type %s/%s for file %d!",
2165 job
->id
, super
, type
, number
+ 1);
2167 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
2168 job
->id
, number
+ 1);
2169 job
->filetypes
[number
] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
2170 job
->compressions
+ number
);
2173 * If that didn't work, assume it is raw...
2176 if (!job
->filetypes
[number
])
2177 job
->filetypes
[number
] = mimeType(MimeDatabase
, "application",
2182 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unknown %s directive on line %d!",
2191 * 'load_next_job_id()' - Load the NextJobId value from the job.cache file.
2195 load_next_job_id(const char *filename
) /* I - job.cache filename */
2197 cups_file_t
*fp
; /* job.cache file */
2198 char line
[1024], /* Line buffer */
2199 *value
; /* Value on line */
2200 int linenum
; /* Line number in file */
2201 int next_job_id
; /* NextJobId value from line */
2205 * Read the NextJobId directive from the job.cache file and use
2206 * the value (if any).
2209 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
2211 if (errno
!= ENOENT
)
2212 cupsdLogMessage(CUPSD_LOG_ERROR
,
2213 "Unable to open job cache file \"%s\": %s",
2214 filename
, strerror(errno
));
2219 cupsdLogMessage(CUPSD_LOG_INFO
,
2220 "Loading NextJobId from job cache file \"%s\"...", filename
);
2224 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
2226 if (!strcasecmp(line
, "NextJobId"))
2230 next_job_id
= atoi(value
);
2232 if (next_job_id
> NextJobId
)
2233 NextJobId
= next_job_id
;
2244 * 'load_request_root()' - Load jobs from the RequestRoot directory.
2248 load_request_root(void)
2250 cups_dir_t
*dir
; /* Directory */
2251 cups_dentry_t
*dent
; /* Directory entry */
2252 cupsd_job_t
*job
; /* New job */
2256 * Open the requests directory...
2259 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Scanning %s for jobs...", RequestRoot
);
2261 if ((dir
= cupsDirOpen(RequestRoot
)) == NULL
)
2263 cupsdLogMessage(CUPSD_LOG_ERROR
,
2264 "Unable to open spool directory \"%s\": %s",
2265 RequestRoot
, strerror(errno
));
2270 * Read all the c##### files...
2273 while ((dent
= cupsDirRead(dir
)) != NULL
)
2274 if (strlen(dent
->filename
) >= 6 && dent
->filename
[0] == 'c')
2277 * Allocate memory for the job...
2280 if ((job
= calloc(sizeof(cupsd_job_t
), 1)) == NULL
)
2282 cupsdLogMessage(CUPSD_LOG_ERROR
, "Ran out of memory for jobs!");
2288 * Assign the job ID...
2291 job
->id
= atoi(dent
->filename
+ 1);
2292 job
->back_pipes
[0] = -1;
2293 job
->back_pipes
[1] = -1;
2294 job
->print_pipes
[0] = -1;
2295 job
->print_pipes
[1] = -1;
2296 job
->side_pipes
[0] = -1;
2297 job
->side_pipes
[1] = -1;
2298 job
->status_pipes
[0] = -1;
2299 job
->status_pipes
[1] = -1;
2301 if (job
->id
>= NextJobId
)
2302 NextJobId
= job
->id
+ 1;
2311 * Insert the job into the array, sorting by job priority and ID...
2314 cupsArrayAdd(Jobs
, job
);
2316 if (job
->state_value
<= IPP_JOB_STOPPED
)
2317 cupsArrayAdd(ActiveJobs
, job
);
2327 * 'set_time()' - Set one of the "time-at-xyz" attributes...
2331 set_time(cupsd_job_t
*job
, /* I - Job to update */
2332 const char *name
) /* I - Name of attribute */
2334 ipp_attribute_t
*attr
; /* Time attribute */
2337 if ((attr
= ippFindAttribute(job
->attrs
, name
, IPP_TAG_ZERO
)) != NULL
)
2339 attr
->value_tag
= IPP_TAG_INTEGER
;
2340 attr
->values
[0].integer
= time(NULL
);
2346 * 'set_hold_until()' - Set the hold time and update job-hold-until attribute...
2350 set_hold_until(cupsd_job_t
*job
, /* I - Job to update */
2351 time_t holdtime
) /* I - Hold until time */
2353 ipp_attribute_t
*attr
; /* job-hold-until attribute */
2354 struct tm
*holddate
; /* Hold date */
2355 char holdstr
[64]; /* Hold time */
2359 * Set the hold_until value and hold the job...
2362 cupsdLogMessage(CUPSD_LOG_DEBUG
, "set_hold_until: hold_until = %d",
2365 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
2366 job
->state_value
= IPP_JOB_HELD
;
2367 job
->hold_until
= holdtime
;
2370 * Update the job-hold-until attribute with a string representing GMT
2371 * time (HH:MM:SS)...
2374 holddate
= gmtime(&holdtime
);
2375 snprintf(holdstr
, sizeof(holdstr
), "%d:%d:%d", holddate
->tm_hour
,
2376 holddate
->tm_min
, holddate
->tm_sec
);
2378 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2379 IPP_TAG_KEYWORD
)) == NULL
)
2380 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2383 * Either add the attribute or update the value of the existing one
2387 attr
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
2388 "job-hold-until", NULL
, holdstr
);
2390 cupsdSetString(&attr
->values
[0].string
.text
, holdstr
);
2397 * 'start_job()' - Start a print job.
2401 start_job(cupsd_job_t
*job
, /* I - Job ID */
2402 cupsd_printer_t
*printer
) /* I - Printer to print job */
2404 int i
; /* Looping var */
2405 int slot
; /* Pipe slot */
2406 cups_array_t
*filters
, /* Filters for job */
2407 *prefilters
; /* Filters with prefilters */
2408 mime_filter_t
*filter
, /* Current filter */
2409 *prefilter
, /* Prefilter */
2410 port_monitor
; /* Port monitor filter */
2411 char method
[255], /* Method for output */
2412 *optptr
, /* Pointer to options */
2413 *valptr
; /* Pointer in value string */
2414 ipp_attribute_t
*attr
; /* Current attribute */
2415 struct stat backinfo
; /* Backend file information */
2416 int backroot
; /* Run backend as root? */
2417 int pid
; /* Process ID of new filter process */
2418 int banner_page
; /* 1 if banner page, 0 otherwise */
2419 int filterfds
[2][2];/* Pipes used between filters */
2420 int envc
; /* Number of environment variables */
2421 char **argv
, /* Filter command-line arguments */
2422 sani_uri
[1024], /* Sanitized DEVICE_URI env var */
2423 filename
[1024], /* Job filename */
2424 command
[1024], /* Full path to command */
2425 jobid
[255], /* Job ID string */
2426 title
[IPP_MAX_NAME
],
2427 /* Job title string */
2428 copies
[255], /* # copies string */
2429 *envp
[MAX_ENV
+ 15],
2430 /* Environment variables */
2431 charset
[255], /* CHARSET env variable */
2432 class_name
[255],/* CLASS env variable */
2433 classification
[1024],
2434 /* CLASSIFICATION env variable */
2436 /* CONTENT_TYPE env variable */
2438 /* DEVICE_URI env variable */
2439 final_content_type
[1024],
2440 /* FINAL_CONTENT_TYPE env variable */
2441 lang
[255], /* LANG env variable */
2442 ppd
[1024], /* PPD env variable */
2444 /* PRINTER env variable */
2446 /* RIP_MAX_CACHE env variable */
2447 static char *options
= NULL
;/* Full list of options */
2448 static int optlength
= 0; /* Length of option buffer */
2451 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] start_job: file = %d/%d",
2452 job
->id
, job
->current_file
, job
->num_files
);
2454 if (job
->num_files
== 0)
2456 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Job %d] No files, canceling job!",
2459 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2464 * Figure out what filters are required to convert from
2465 * the source to the destination type...
2474 * Remote jobs and raw queues go directly to the printer without
2478 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2479 "[Job %d] Sending job to queue tagged as raw...", job
->id
);
2486 * Local jobs get filtered...
2489 filters
= mimeFilter(MimeDatabase
, job
->filetypes
[job
->current_file
],
2490 printer
->filetype
, &(job
->cost
));
2494 cupsdLogMessage(CUPSD_LOG_ERROR
,
2495 "[Job %d] Unable to convert file %d to printable format!",
2496 job
->current_file
, job
->id
);
2497 cupsdLogMessage(CUPSD_LOG_INFO
,
2498 "Hint: Do you have ESP Ghostscript installed?");
2500 if (LogLevel
< CUPSD_LOG_DEBUG
)
2501 cupsdLogMessage(CUPSD_LOG_INFO
,
2502 "Hint: Try setting the LogLevel to \"debug\".");
2504 job
->current_file
++;
2506 if (job
->current_file
== job
->num_files
)
2507 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2513 * Remove NULL ("-") filters...
2516 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
2518 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
2519 if (!strcmp(filter
->filter
, "-"))
2520 cupsArrayRemove(filters
, filter
);
2522 if (cupsArrayCount(filters
) == 0)
2524 cupsArrayDelete(filters
);
2529 * If this printer has any pre-filters, insert the required pre-filter
2530 * in the filters array...
2533 if (printer
->prefiltertype
&& filters
)
2535 prefilters
= cupsArrayNew(NULL
, NULL
);
2537 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
2539 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
2541 if ((prefilter
= mimeFilterLookup(MimeDatabase
, filter
->src
,
2542 printer
->prefiltertype
)))
2544 cupsArrayAdd(prefilters
, prefilter
);
2545 job
->cost
+= prefilter
->cost
;
2548 cupsArrayAdd(prefilters
, filter
);
2551 cupsArrayDelete(filters
);
2552 filters
= prefilters
;
2557 * Set a minimum cost of 100 for all jobs so that FilterLimit
2558 * works with raw queues and other low-cost paths.
2561 if (job
->cost
< 100)
2565 * See if the filter cost is too high...
2568 if ((FilterLevel
+ job
->cost
) > FilterLimit
&& FilterLevel
> 0 &&
2572 * Don't print this job quite yet...
2575 cupsArrayDelete(filters
);
2577 cupsdLogMessage(CUPSD_LOG_INFO
,
2578 "[Job %d] Holding because filter limit has been reached.",
2580 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2581 "[Job %d] start_job: file=%d, cost=%d, level=%d, limit=%d",
2582 job
->id
, job
->current_file
, job
->cost
, FilterLevel
,
2587 FilterLevel
+= job
->cost
;
2590 * Add decompression/raw filter as needed...
2593 if ((!printer
->raw
&& job
->compressions
[job
->current_file
]) ||
2594 (!filters
&& !printer
->remote
&&
2595 (job
->num_files
> 1 || !strncmp(printer
->device_uri
, "file:", 5))))
2598 * Add gziptoany filter to the front of the list...
2602 filters
= cupsArrayNew(NULL
, NULL
);
2604 if (!cupsArrayInsert(filters
, &gziptoany_filter
))
2606 cupsdLogMessage(CUPSD_LOG_ERROR
,
2607 "[Job %d] Unable to add decompression filter - %s",
2608 job
->id
, strerror(errno
));
2610 cupsArrayDelete(filters
);
2612 job
->current_file
++;
2614 if (job
->current_file
== job
->num_files
)
2615 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2622 * Add port monitor, if any...
2625 if (printer
->port_monitor
)
2628 * Add port monitor to the end of the list...
2632 filters
= cupsArrayNew(NULL
, NULL
);
2634 if (!cupsArrayAdd(filters
, &port_monitor
))
2636 cupsdLogMessage(CUPSD_LOG_ERROR
,
2637 "[Job %d] Unable to add port monitor - %s",
2638 job
->id
, strerror(errno
));
2640 cupsArrayDelete(filters
);
2642 job
->current_file
++;
2644 if (job
->current_file
== job
->num_files
)
2645 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2650 snprintf(port_monitor
.filter
, sizeof(port_monitor
.filter
),
2651 "%s/monitor/%s", ServerBin
, printer
->port_monitor
);
2655 * Update the printer and job state to "processing"...
2658 job
->state
->values
[0].integer
= IPP_JOB_PROCESSING
;
2659 job
->state_value
= IPP_JOB_PROCESSING
;
2662 job
->printer
= printer
;
2665 cupsdSetPrinterState(printer
, IPP_PRINTER_PROCESSING
, 0);
2667 if (job
->current_file
== 0)
2670 * Set the processing time...
2673 set_time(job
, "time-at-processing");
2676 * Create the backchannel pipes and make them non-blocking...
2679 cupsdOpenPipe(job
->back_pipes
);
2681 fcntl(job
->back_pipes
[0], F_SETFL
,
2682 fcntl(job
->back_pipes
[0], F_GETFL
) | O_NONBLOCK
);
2684 fcntl(job
->back_pipes
[1], F_SETFL
,
2685 fcntl(job
->back_pipes
[1], F_GETFL
) | O_NONBLOCK
);
2688 * Create the side-channel pipes and make them non-blocking...
2691 socketpair(AF_LOCAL
, SOCK_STREAM
, 0, job
->side_pipes
);
2693 fcntl(job
->side_pipes
[0], F_SETFL
,
2694 fcntl(job
->side_pipes
[0], F_GETFL
) | O_NONBLOCK
);
2696 fcntl(job
->side_pipes
[1], F_SETFL
,
2697 fcntl(job
->side_pipes
[1], F_GETFL
) | O_NONBLOCK
);
2701 * Determine if we are printing a banner page or not...
2704 if (job
->job_sheets
== NULL
)
2706 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] No job-sheets attribute.",
2708 if ((job
->job_sheets
=
2709 ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_ZERO
)) != NULL
)
2710 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2711 "[Job %d] ... but someone added one without setting "
2712 "job_sheets!", job
->id
);
2714 else if (job
->job_sheets
->num_values
== 1)
2715 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] job-sheets=%s", job
->id
,
2716 job
->job_sheets
->values
[0].string
.text
);
2718 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] job-sheets=%s,%s", job
->id
,
2719 job
->job_sheets
->values
[0].string
.text
,
2720 job
->job_sheets
->values
[1].string
.text
);
2722 if (printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
))
2724 else if (job
->job_sheets
== NULL
)
2726 else if (strcasecmp(job
->job_sheets
->values
[0].string
.text
, "none") != 0 &&
2727 job
->current_file
== 0)
2729 else if (job
->job_sheets
->num_values
> 1 &&
2730 strcasecmp(job
->job_sheets
->values
[1].string
.text
, "none") != 0 &&
2731 job
->current_file
== (job
->num_files
- 1))
2736 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] banner_page = %d", job
->id
,
2740 * Building the options string is harder than it needs to be, but
2741 * for the moment we need to pass strings for command-line args and
2742 * not IPP attribute pointers... :)
2744 * First allocate/reallocate the option buffer as needed...
2747 i
= ipp_length(job
->attrs
);
2754 optptr
= realloc(options
, i
);
2758 cupsdLogMessage(CUPSD_LOG_CRIT
,
2759 "[Job %d] Unable to allocate %d bytes for option buffer!",
2762 cupsArrayDelete(filters
);
2764 FilterLevel
-= job
->cost
;
2766 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2775 * Now loop through the attributes and convert them to the textual
2776 * representation used by the filters...
2782 snprintf(title
, sizeof(title
), "%s-%d", printer
->name
, job
->id
);
2783 strcpy(copies
, "1");
2785 for (attr
= job
->attrs
->attrs
; attr
!= NULL
; attr
= attr
->next
)
2787 if (!strcmp(attr
->name
, "copies") &&
2788 attr
->value_tag
== IPP_TAG_INTEGER
)
2791 * Don't use the # copies attribute if we are printing the job sheets...
2795 sprintf(copies
, "%d", attr
->values
[0].integer
);
2797 else if (!strcmp(attr
->name
, "job-name") &&
2798 (attr
->value_tag
== IPP_TAG_NAME
||
2799 attr
->value_tag
== IPP_TAG_NAMELANG
))
2800 strlcpy(title
, attr
->values
[0].string
.text
, sizeof(title
));
2801 else if (attr
->group_tag
== IPP_TAG_JOB
)
2804 * Filter out other unwanted attributes...
2807 if (attr
->value_tag
== IPP_TAG_MIMETYPE
||
2808 attr
->value_tag
== IPP_TAG_NAMELANG
||
2809 attr
->value_tag
== IPP_TAG_TEXTLANG
||
2810 (attr
->value_tag
== IPP_TAG_URI
&& strcmp(attr
->name
, "job-uuid")) ||
2811 attr
->value_tag
== IPP_TAG_URISCHEME
||
2812 attr
->value_tag
== IPP_TAG_BEGIN_COLLECTION
) /* Not yet supported */
2815 if (!strncmp(attr
->name
, "time-", 5))
2818 if (!strncmp(attr
->name
, "job-", 4) && strcmp(attr
->name
, "job-uuid") &&
2819 !(printer
->type
& CUPS_PRINTER_REMOTE
))
2822 if (!strncmp(attr
->name
, "job-", 4) &&
2823 strcmp(attr
->name
, "job-uuid") &&
2824 strcmp(attr
->name
, "job-billing") &&
2825 strcmp(attr
->name
, "job-sheets") &&
2826 strcmp(attr
->name
, "job-hold-until") &&
2827 strcmp(attr
->name
, "job-priority"))
2830 if ((!strcmp(attr
->name
, "page-label") ||
2831 !strcmp(attr
->name
, "page-border") ||
2832 !strncmp(attr
->name
, "number-up", 9) ||
2833 !strcmp(attr
->name
, "page-ranges") ||
2834 !strcmp(attr
->name
, "page-set") ||
2835 !strcasecmp(attr
->name
, "AP_FIRSTPAGE_InputSlot") ||
2836 !strcasecmp(attr
->name
, "AP_FIRSTPAGE_ManualFeed")) &&
2841 * Otherwise add them to the list...
2844 if (optptr
> options
)
2845 strlcat(optptr
, " ", optlength
- (optptr
- options
));
2847 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
2849 strlcat(optptr
, attr
->name
, optlength
- (optptr
- options
));
2850 strlcat(optptr
, "=", optlength
- (optptr
- options
));
2853 for (i
= 0; i
< attr
->num_values
; i
++)
2856 strlcat(optptr
, ",", optlength
- (optptr
- options
));
2858 optptr
+= strlen(optptr
);
2860 switch (attr
->value_tag
)
2862 case IPP_TAG_INTEGER
:
2864 snprintf(optptr
, optlength
- (optptr
- options
),
2865 "%d", attr
->values
[i
].integer
);
2868 case IPP_TAG_BOOLEAN
:
2869 if (!attr
->values
[i
].boolean
)
2870 strlcat(optptr
, "no", optlength
- (optptr
- options
));
2872 case IPP_TAG_NOVALUE
:
2873 strlcat(optptr
, attr
->name
,
2874 optlength
- (optptr
- options
));
2877 case IPP_TAG_RANGE
:
2878 if (attr
->values
[i
].range
.lower
== attr
->values
[i
].range
.upper
)
2879 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2880 "%d", attr
->values
[i
].range
.lower
);
2882 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2883 "%d-%d", attr
->values
[i
].range
.lower
,
2884 attr
->values
[i
].range
.upper
);
2887 case IPP_TAG_RESOLUTION
:
2888 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2889 "%dx%d%s", attr
->values
[i
].resolution
.xres
,
2890 attr
->values
[i
].resolution
.yres
,
2891 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
2895 case IPP_TAG_STRING
:
2898 case IPP_TAG_KEYWORD
:
2899 case IPP_TAG_CHARSET
:
2900 case IPP_TAG_LANGUAGE
:
2902 for (valptr
= attr
->values
[i
].string
.text
; *valptr
;)
2904 if (strchr(" \t\n\\\'\"", *valptr
))
2906 *optptr
++ = *valptr
++;
2913 break; /* anti-compiler-warning-code */
2917 optptr
+= strlen(optptr
);
2922 * Build the command-line arguments for the filters. Each filter
2923 * has 6 or 7 arguments:
2927 * argv[2] = username
2929 * argv[4] = # copies
2931 * argv[6] = filename (optional; normally stdin)
2933 * This allows legacy printer drivers that use the old System V
2934 * printing interface to be used by CUPS.
2936 * For remote jobs, we send all of the files in the argument list.
2939 if (printer
->remote
&& job
->num_files
> 1)
2940 argv
= calloc(7 + job
->num_files
, sizeof(char *));
2942 argv
= calloc(8, sizeof(char *));
2944 sprintf(jobid
, "%d", job
->id
);
2946 argv
[0] = printer
->name
;
2948 argv
[2] = job
->username
;
2953 if (printer
->remote
&& job
->num_files
> 1)
2955 for (i
= 0; i
< job
->num_files
; i
++)
2957 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
2959 argv
[6 + i
] = strdup(filename
);
2964 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
2965 job
->id
, job
->current_file
+ 1);
2969 for (i
= 0; argv
[i
]; i
++)
2970 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2971 "[Job %d] argv[%d]=\"%s\"", job
->id
, i
, argv
[i
]);
2974 * Create environment variable strings for the filters...
2977 attr
= ippFindAttribute(job
->attrs
, "attributes-natural-language",
2980 switch (strlen(attr
->values
[0].string
.text
))
2984 * This is an unknown or badly formatted language code; use
2985 * the POSIX locale...
2988 strcpy(lang
, "LANG=C");
2993 * Just the language code (ll)...
2996 snprintf(lang
, sizeof(lang
), "LANG=%s",
2997 attr
->values
[0].string
.text
);
3002 * Language and country code (ll-cc)...
3005 snprintf(lang
, sizeof(lang
), "LANG=%c%c_%c%c",
3006 attr
->values
[0].string
.text
[0],
3007 attr
->values
[0].string
.text
[1],
3008 toupper(attr
->values
[0].string
.text
[3] & 255),
3009 toupper(attr
->values
[0].string
.text
[4] & 255));
3013 attr
= ippFindAttribute(job
->attrs
, "document-format",
3016 (optptr
= strstr(attr
->values
[0].string
.text
, "charset=")) != NULL
)
3017 snprintf(charset
, sizeof(charset
), "CHARSET=%s", optptr
+ 8);
3020 attr
= ippFindAttribute(job
->attrs
, "attributes-charset",
3022 snprintf(charset
, sizeof(charset
), "CHARSET=%s",
3023 attr
->values
[0].string
.text
);
3026 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s/%s",
3027 job
->filetypes
[job
->current_file
]->super
,
3028 job
->filetypes
[job
->current_file
]->type
);
3029 snprintf(device_uri
, sizeof(device_uri
), "DEVICE_URI=%s",
3030 printer
->device_uri
);
3031 cupsdSanitizeURI(printer
->device_uri
, sani_uri
, sizeof(sani_uri
));
3032 snprintf(ppd
, sizeof(ppd
), "PPD=%s/ppd/%s.ppd", ServerRoot
, printer
->name
);
3033 snprintf(printer_name
, sizeof(printer_name
), "PRINTER=%s", printer
->name
);
3034 snprintf(rip_max_cache
, sizeof(rip_max_cache
), "RIP_MAX_CACHE=%s", RIPCache
);
3036 envc
= cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
3038 envp
[envc
++] = charset
;
3039 envp
[envc
++] = lang
;
3040 envp
[envc
++] = ppd
;
3041 envp
[envc
++] = rip_max_cache
;
3042 envp
[envc
++] = content_type
;
3043 envp
[envc
++] = device_uri
;
3044 envp
[envc
++] = printer_name
;
3046 if (!printer
->remote
&& !printer
->raw
&&
3047 (filter
= (mime_filter_t
*)cupsArrayLast(filters
)) != NULL
&&
3050 snprintf(final_content_type
, sizeof(final_content_type
),
3051 "FINAL_CONTENT_TYPE=%s/%s",
3052 filter
->dst
->super
, filter
->dst
->type
);
3053 envp
[envc
++] = final_content_type
;
3056 if (Classification
&& !banner_page
)
3058 if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
3059 IPP_TAG_NAME
)) == NULL
)
3060 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3062 else if (attr
->num_values
> 1 &&
3063 strcmp(attr
->values
[1].string
.text
, "none") != 0)
3064 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3065 attr
->values
[1].string
.text
);
3067 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3068 attr
->values
[0].string
.text
);
3070 envp
[envc
++] = classification
;
3073 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
3075 snprintf(class_name
, sizeof(class_name
), "CLASS=%s", job
->dest
);
3076 envp
[envc
++] = class_name
;
3079 if (job
->auth_username
)
3080 envp
[envc
++] = job
->auth_username
;
3081 if (job
->auth_domain
)
3082 envp
[envc
++] = job
->auth_domain
;
3083 if (job
->auth_password
)
3084 envp
[envc
++] = job
->auth_password
;
3088 envp
[envc
++] = job
->ccname
;
3089 #endif /* HAVE_GSSAPI */
3093 for (i
= 0; i
< envc
; i
++)
3094 if (!strncmp(envp
[i
], "AUTH_", 5))
3095 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] envp[%d]=\"AUTH_%c****\"",
3096 job
->id
, i
, envp
[i
][5]);
3097 else if (strncmp(envp
[i
], "DEVICE_URI=", 11))
3098 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] envp[%d]=\"%s\"",
3099 job
->id
, i
, envp
[i
]);
3101 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] envp[%d]=\"DEVICE_URI=%s\"",
3102 job
->id
, i
, sani_uri
);
3104 if (printer
->remote
)
3105 job
->current_file
= job
->num_files
;
3107 job
->current_file
++;
3110 * Now create processes for all of the filters...
3113 filterfds
[0][0] = -1;
3114 filterfds
[0][1] = -1;
3115 filterfds
[1][0] = -1;
3116 filterfds
[1][1] = -1;
3118 if (!job
->status_buffer
)
3120 if (cupsdOpenPipe(job
->status_pipes
))
3122 cupsdLogMessage(CUPSD_LOG_ERROR
,
3123 "[Job %d] Unable to create job status pipes - %s.",
3124 job
->id
, strerror(errno
));
3125 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3126 "Unable to create status pipes - %s.", strerror(errno
));
3128 cupsdAddPrinterHistory(printer
);
3130 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3131 "Job canceled because the server could not create the job "
3137 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3138 "[Job %d] start_job: status_pipes = [ %d %d ]",
3139 job
->id
, job
->status_pipes
[0], job
->status_pipes
[1]);
3141 job
->status_buffer
= cupsdStatBufNew(job
->status_pipes
[0], "[Job %d]",
3143 job
->status_level
= CUPSD_LOG_INFO
;
3147 memset(job
->filters
, 0, sizeof(job
->filters
));
3149 for (i
= 0, slot
= 0, filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
3151 i
++, filter
= (mime_filter_t
*)cupsArrayNext(filters
))
3153 if (filter
->filter
[0] != '/')
3154 snprintf(command
, sizeof(command
), "%s/filter/%s", ServerBin
,
3157 strlcpy(command
, filter
->filter
, sizeof(command
));
3159 if (i
< (cupsArrayCount(filters
) - 1))
3161 if (cupsdOpenPipe(filterfds
[slot
]))
3163 cupsdLogMessage(CUPSD_LOG_ERROR
,
3164 "[Job %d] Unable to create job filter pipes - %s.",
3165 job
->id
, strerror(errno
));
3166 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3167 "Unable to create filter pipes - %s.", strerror(errno
));
3168 cupsdAddPrinterHistory(printer
);
3170 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3171 "Job canceled because the server could not create the "
3179 if (job
->current_file
== 1)
3181 if (strncmp(printer
->device_uri
, "file:", 5) != 0)
3183 if (cupsdOpenPipe(job
->print_pipes
))
3185 cupsdLogMessage(CUPSD_LOG_ERROR
,
3186 "[Job %d] Unable to create job backend pipes - %s.",
3187 job
->id
, strerror(errno
));
3188 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3189 "Unable to create backend pipes - %s.", strerror(errno
));
3190 cupsdAddPrinterHistory(printer
);
3192 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3193 "Job canceled because the server could not create "
3194 "the backend pipes.");
3201 job
->print_pipes
[0] = -1;
3202 if (!strcmp(printer
->device_uri
, "file:/dev/null") ||
3203 !strcmp(printer
->device_uri
, "file:///dev/null"))
3204 job
->print_pipes
[1] = -1;
3207 if (!strncmp(printer
->device_uri
, "file:/dev/", 10))
3208 job
->print_pipes
[1] = open(printer
->device_uri
+ 5,
3210 else if (!strncmp(printer
->device_uri
, "file:///dev/", 12))
3211 job
->print_pipes
[1] = open(printer
->device_uri
+ 7,
3213 else if (!strncmp(printer
->device_uri
, "file:///", 8))
3214 job
->print_pipes
[1] = open(printer
->device_uri
+ 7,
3215 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
3217 job
->print_pipes
[1] = open(printer
->device_uri
+ 5,
3218 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
3220 if (job
->print_pipes
[1] < 0)
3222 cupsdLogMessage(CUPSD_LOG_ERROR
,
3223 "[Job %d] Unable to open output file \"%s\" - %s.",
3224 job
->id
, printer
->device_uri
, strerror(errno
));
3225 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3226 "Unable to open output file \"%s\" - %s.",
3227 printer
->device_uri
, strerror(errno
));
3229 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3230 "Job canceled because the server could not open the "
3236 fcntl(job
->print_pipes
[1], F_SETFD
,
3237 fcntl(job
->print_pipes
[1], F_GETFD
) | FD_CLOEXEC
);
3241 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3242 "[Job %d] start_job: print_pipes = [ %d %d ]",
3243 job
->id
, job
->print_pipes
[0], job
->print_pipes
[1]);
3246 filterfds
[slot
][0] = job
->print_pipes
[0];
3247 filterfds
[slot
][1] = job
->print_pipes
[1];
3250 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] start_job: filter=\"%s\"",
3252 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3253 "[Job %d] start_job: filterfds[%d]=[ %d %d ]",
3254 job
->id
, slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3256 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
3257 filterfds
[slot
][1], job
->status_pipes
[1],
3258 job
->back_pipes
[0], job
->side_pipes
[0], 0,
3261 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3262 "[Job %d] start_job: Closing filter pipes for slot %d "
3264 job
->id
, !slot
, filterfds
[!slot
][0], filterfds
[!slot
][1]);
3266 cupsdClosePipe(filterfds
[!slot
]);
3270 cupsdLogMessage(CUPSD_LOG_ERROR
,
3271 "[Job %d] Unable to start filter \"%s\" - %s.",
3272 job
->id
, filter
->filter
, strerror(errno
));
3273 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3274 "Unable to start filter \"%s\" - %s.",
3275 filter
->filter
, strerror(errno
));
3277 cupsdAddPrinterHistory(printer
);
3279 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3280 "Job canceled because the server could not execute a "
3286 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Started filter %s (PID %d)",
3287 job
->id
, command
, pid
);
3293 cupsArrayDelete(filters
);
3296 * Finally, pipe the final output into a backend process if needed...
3299 if (strncmp(printer
->device_uri
, "file:", 5) != 0)
3301 if (job
->current_file
== 1)
3303 sscanf(printer
->device_uri
, "%254[^:]", method
);
3304 snprintf(command
, sizeof(command
), "%s/backend/%s", ServerBin
, method
);
3307 * See if the backend needs to run as root...
3312 else if (stat(command
, &backinfo
))
3315 backroot
= !(backinfo
.st_mode
& (S_IRWXG
| S_IRWXO
));
3319 filterfds
[slot
][0] = -1;
3320 filterfds
[slot
][1] = -1;
3322 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] start_job: backend=\"%s\"",
3324 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3325 "[Job %d] start_job: filterfds[%d] = [ %d %d ]", job
->id
,
3326 slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3328 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
3329 filterfds
[slot
][1], job
->status_pipes
[1],
3330 job
->back_pipes
[1], job
->side_pipes
[1],
3331 backroot
, &(job
->backend
));
3335 cupsdLogMessage(CUPSD_LOG_ERROR
,
3336 "[Job %d] Unable to start backend \"%s\" - %s.",
3337 job
->id
, method
, strerror(errno
));
3338 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3339 "Unable to start backend \"%s\" - %s.", method
,
3342 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3343 "Job canceled because the server could not execute "
3350 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Started backend %s (PID %d)",
3351 job
->id
, command
, pid
);
3355 if (job
->current_file
== job
->num_files
)
3357 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3358 "[Job %d] start_job: Closing print pipes [ %d %d ]...",
3359 job
->id
, job
->print_pipes
[0], job
->print_pipes
[1]);
3361 cupsdClosePipe(job
->print_pipes
);
3363 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3364 "[Job %d] start_job: Closing back pipes [ %d %d ]...",
3365 job
->id
, job
->back_pipes
[0], job
->back_pipes
[1]);
3367 cupsdClosePipe(job
->back_pipes
);
3369 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3370 "[Job %d] start_job: Closing side pipes [ %d %d ]...",
3371 job
->id
, job
->side_pipes
[0], job
->side_pipes
[1]);
3373 cupsdClosePipe(job
->side_pipes
);
3375 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3376 "[Job %d] start_job: Closing status output pipe %d...",
3377 job
->id
, job
->status_pipes
[1]);
3379 close(job
->status_pipes
[1]);
3380 job
->status_pipes
[1] = -1;
3385 filterfds
[slot
][0] = -1;
3386 filterfds
[slot
][1] = -1;
3388 if (job
->current_file
== job
->num_files
)
3390 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3391 "[Job %d] start_job: Closing print pipes [ %d %d ]...",
3392 job
->id
, job
->print_pipes
[0], job
->print_pipes
[1]);
3394 cupsdClosePipe(job
->print_pipes
);
3396 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3397 "[Job %d] start_job: Closing status output pipe %d...",
3398 job
->id
, job
->status_pipes
[1]);
3400 close(job
->status_pipes
[1]);
3401 job
->status_pipes
[1] = -1;
3405 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3406 "[Job %d] start_job: Closing filter pipes for slot %d "
3408 job
->id
, slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3409 cupsdClosePipe(filterfds
[slot
]);
3411 if (printer
->remote
&& job
->num_files
> 1)
3413 for (i
= 0; i
< job
->num_files
; i
++)
3419 cupsdAddSelect(job
->status_buffer
->fd
, (cupsd_selfunc_t
)update_job
, NULL
,
3422 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
, "Job #%d started.",
3429 * If we get here, we need to abort the current job and close out all
3430 * files and pipes...
3435 for (slot
= 0; slot
< 2; slot
++)
3437 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3438 "[Job %d] start_job: Closing filter pipes for slot %d "
3440 job
->id
, slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3441 cupsdClosePipe(filterfds
[slot
]);
3444 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3445 "[Job %d] start_job: Closing status pipes [ %d %d ]...",
3446 job
->id
, job
->status_pipes
[0], job
->status_pipes
[1]);
3447 cupsdClosePipe(job
->status_pipes
);
3448 cupsdStatBufDelete(job
->status_buffer
);
3450 job
->status_buffer
= NULL
;
3452 cupsArrayDelete(filters
);
3454 if (printer
->remote
&& job
->num_files
> 1)
3456 for (i
= 0; i
< job
->num_files
; i
++)
3462 cupsdStopJob(job
, 0);
3467 * 'unload_job()' - Unload a job from memory.
3471 unload_job(cupsd_job_t
*job
) /* I - Job */
3476 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Unloading...", job
->id
);
3478 ippDelete(job
->attrs
);
3483 job
->job_sheets
= NULL
;
3484 job
->printer_message
= NULL
;
3485 job
->printer_reasons
= NULL
;
3490 * 'update_job()' - Read a status update from a job's filters.
3494 update_job(cupsd_job_t
*job
) /* I - Job to check */
3496 int i
; /* Looping var */
3497 int copies
; /* Number of copies printed */
3498 char message
[1024], /* Message text */
3499 *ptr
; /* Pointer update... */
3500 int loglevel
, /* Log level for message */
3501 event
= 0; /* Events? */
3504 while ((ptr
= cupsdStatBufUpdate(job
->status_buffer
, &loglevel
,
3505 message
, sizeof(message
))) != NULL
)
3508 * Process page and printer state messages as needed...
3511 if (loglevel
== CUPSD_LOG_PAGE
)
3514 * Page message; send the message to the page_log file and update the
3515 * job sheet count...
3518 if (job
->sheets
!= NULL
)
3520 if (!strncasecmp(message
, "total ", 6))
3523 * Got a total count of pages from a backend or filter...
3526 copies
= atoi(message
+ 6);
3527 copies
-= job
->sheets
->values
[0].integer
; /* Just track the delta */
3529 else if (!sscanf(message
, "%*d%d", &copies
))
3532 job
->sheets
->values
[0].integer
+= copies
;
3534 if (job
->printer
->page_limit
)
3536 cupsd_quota_t
*q
= cupsdUpdateQuota(job
->printer
, job
->username
,
3540 if (AppleQuotas
&& q
->page_count
== -3)
3543 * Quota limit exceeded, cancel job in progress immediately...
3546 cupsdLogMessage(CUPSD_LOG_INFO
,
3547 "[Job %d] Canceled because pages exceed user %s "
3548 "quota limit on printer %s (%s).",
3549 job
->id
, job
->username
, job
->printer
->name
,
3550 job
->printer
->info
);
3552 cupsdCancelJob(job
, 1, IPP_JOB_CANCELED
);
3557 #endif /* __APPLE__ */
3561 cupsdLogPage(job
, message
);
3563 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
3564 "Printed %d page(s).", job
->sheets
->values
[0].integer
);
3566 else if (loglevel
== CUPSD_LOG_STATE
)
3568 if (!strcmp(message
, "paused"))
3569 cupsdStopPrinter(job
->printer
, 1);
3572 cupsdSetPrinterReasons(job
->printer
, message
);
3573 cupsdAddPrinterHistory(job
->printer
);
3574 event
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
3577 update_job_attrs(job
);
3579 else if (loglevel
== CUPSD_LOG_ATTR
)
3582 * Set attribute(s)...
3585 int num_attrs
; /* Number of attributes */
3586 cups_option_t
*attrs
; /* Attributes */
3587 const char *attr
; /* Attribute */
3590 num_attrs
= cupsParseOptions(message
, 0, &attrs
);
3592 if ((attr
= cupsGetOption("auth-info-required", num_attrs
,
3594 cupsdSetAuthInfoRequired(job
->printer
, attr
, NULL
);
3596 if ((attr
= cupsGetOption("printer-alert", num_attrs
, attrs
)) != NULL
)
3598 cupsdSetString(&job
->printer
->alert
, attr
);
3599 event
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
3602 if ((attr
= cupsGetOption("printer-alert-description", num_attrs
,
3605 cupsdSetString(&job
->printer
->alert_description
, attr
);
3606 event
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
3609 cupsFreeOptions(num_attrs
, attrs
);
3612 else if (!strncmp(message
, "recoverable:", 12))
3614 cupsdSetPrinterReasons(job
->printer
,
3615 "+com.apple.print.recoverable-warning");
3618 while (isspace(*ptr
& 255))
3621 cupsdSetString(&job
->printer
->recoverable
, ptr
);
3622 cupsdAddPrinterHistory(job
->printer
);
3623 event
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
3625 else if (!strncmp(message
, "recovered:", 10))
3627 cupsdSetPrinterReasons(job
->printer
,
3628 "-com.apple.print.recoverable-warning");
3631 while (isspace(*ptr
& 255))
3634 cupsdSetString(&job
->printer
->recoverable
, ptr
);
3635 cupsdAddPrinterHistory(job
->printer
);
3636 event
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
3638 #endif /* __APPLE__ */
3639 else if (loglevel
<= job
->status_level
)
3642 * Some message to show in the printer-state-message attribute...
3645 if (loglevel
!= CUPSD_LOG_NOTICE
)
3646 job
->status_level
= loglevel
;
3648 strlcpy(job
->printer
->state_message
, message
,
3649 sizeof(job
->printer
->state_message
));
3650 cupsdAddPrinterHistory(job
->printer
);
3651 event
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
3653 update_job_attrs(job
);
3656 if (!strchr(job
->status_buffer
->buffer
, '\n'))
3660 if ((event
& CUPSD_EVENT_PRINTER_STATE_CHANGED
))
3661 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE_CHANGED
, job
->printer
, NULL
,
3662 (job
->printer
->type
& CUPS_PRINTER_CLASS
) ?
3663 "Class \"%s\" state changed." :
3664 "Printer \"%s\" state changed.",
3665 job
->printer
->name
);
3667 if (ptr
== NULL
&& !job
->status_buffer
->bufused
)
3670 * See if all of the filters and the backend have returned their
3674 for (i
= 0; job
->filters
[i
] < 0; i
++);
3676 if (job
->filters
[i
])
3679 if (job
->current_file
>= job
->num_files
&& job
->backend
> 0)
3683 * Handle the end of job stuff...
3686 cupsdFinishJob(job
);
3692 * 'update_job_attrs()' - Update the job-printer-* attributes.
3696 update_job_attrs(cupsd_job_t
*job
) /* I - Job to update */
3698 int i
; /* Looping var */
3699 int num_reasons
; /* Actual number of reasons */
3700 const char * const *reasons
; /* Reasons */
3701 static const char *none
= "none", /* "none" */
3707 * Get/create the job-printer-state-* attributes...
3710 if (!job
->printer_message
)
3712 if ((job
->printer_message
= ippFindAttribute(job
->attrs
,
3713 "job-printer-state-message",
3714 IPP_TAG_TEXT
)) == NULL
)
3715 job
->printer_message
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_TEXT
,
3716 "job-printer-state-message",
3720 if (!job
->printer_reasons
)
3721 job
->printer_reasons
= ippFindAttribute(job
->attrs
,
3722 "job-printer-state-reasons",
3726 * If the job isn't printing, return now...
3733 * Otherwise copy the printer-state-message value...
3736 if (job
->printer
->state_message
[0])
3737 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
),
3738 job
->printer
->state_message
);
3741 * ... and the printer-state-reasons value...
3744 if (job
->printer
->num_reasons
== 0)
3747 reasons
= job
->printer
->state
== IPP_PRINTER_STOPPED
? &paused
: &none
;
3751 num_reasons
= job
->printer
->num_reasons
;
3752 reasons
= (const char * const *)job
->printer
->reasons
;
3755 if (!job
->printer_reasons
|| job
->printer_reasons
->num_values
!= num_reasons
)
3757 ippDeleteAttribute(job
->attrs
, job
->printer_reasons
);
3759 job
->printer_reasons
= ippAddStrings(job
->attrs
,
3760 IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
3761 "job-printer-state-reasons",
3762 num_reasons
, NULL
, NULL
);
3765 for (i
= 0; i
< num_reasons
; i
++)
3766 cupsdSetString(&(job
->printer_reasons
->values
[i
].string
.text
), reasons
[i
]);
3771 * End of "$Id: job.c 6735 2007-07-26 18:38:29Z mike $".