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
);
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 * Destroy the credential cache and clear the KRB5CCNAME env var string.
1799 krb5_cc_destroy(KerberosContext
, job
->ccache
);
1801 cupsdClearString(&job
->ccname
);
1803 #endif /* HAVE_GSSAPI */
1805 if (job
->num_files
> 0)
1807 free(job
->compressions
);
1808 free(job
->filetypes
);
1811 ippDelete(job
->attrs
);
1818 * 'ipp_length()' - Compute the size of the buffer needed to hold
1819 * the textual IPP attributes.
1822 static int /* O - Size of attribute buffer */
1823 ipp_length(ipp_t
*ipp
) /* I - IPP request */
1825 int bytes
; /* Number of bytes */
1826 int i
; /* Looping var */
1827 ipp_attribute_t
*attr
; /* Current attribute */
1831 * Loop through all attributes...
1836 for (attr
= ipp
->attrs
; attr
!= NULL
; attr
= attr
->next
)
1839 * Skip attributes that won't be sent to filters...
1842 if (attr
->value_tag
== IPP_TAG_MIMETYPE
||
1843 attr
->value_tag
== IPP_TAG_NAMELANG
||
1844 attr
->value_tag
== IPP_TAG_TEXTLANG
||
1845 attr
->value_tag
== IPP_TAG_URI
||
1846 attr
->value_tag
== IPP_TAG_URISCHEME
)
1849 if (strncmp(attr
->name
, "time-", 5) == 0)
1853 * Add space for a leading space and commas between each value.
1854 * For the first attribute, the leading space isn't used, so the
1855 * extra byte can be used as the nul terminator...
1858 bytes
++; /* " " separator */
1859 bytes
+= attr
->num_values
; /* "," separators */
1862 * Boolean attributes appear as "foo,nofoo,foo,nofoo", while
1863 * other attributes appear as "foo=value1,value2,...,valueN".
1866 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
1867 bytes
+= strlen(attr
->name
);
1869 bytes
+= attr
->num_values
* strlen(attr
->name
);
1872 * Now add the size required for each value in the attribute...
1875 switch (attr
->value_tag
)
1877 case IPP_TAG_INTEGER
:
1880 * Minimum value of a signed integer is -2147483647, or 11 digits.
1883 bytes
+= attr
->num_values
* 11;
1886 case IPP_TAG_BOOLEAN
:
1888 * Add two bytes for each false ("no") value...
1891 for (i
= 0; i
< attr
->num_values
; i
++)
1892 if (!attr
->values
[i
].boolean
)
1896 case IPP_TAG_RANGE
:
1898 * A range is two signed integers separated by a hyphen, or
1899 * 23 characters max.
1902 bytes
+= attr
->num_values
* 23;
1905 case IPP_TAG_RESOLUTION
:
1907 * A resolution is two signed integers separated by an "x" and
1908 * suffixed by the units, or 26 characters max.
1911 bytes
+= attr
->num_values
* 26;
1914 case IPP_TAG_STRING
:
1917 case IPP_TAG_KEYWORD
:
1918 case IPP_TAG_CHARSET
:
1919 case IPP_TAG_LANGUAGE
:
1922 * Strings can contain characters that need quoting. We need
1923 * at least 2 * len + 2 characters to cover the quotes and
1924 * any backslashes in the string.
1927 for (i
= 0; i
< attr
->num_values
; i
++)
1928 bytes
+= 2 * strlen(attr
->values
[i
].string
.text
) + 2;
1932 break; /* anti-compiler-warning-code */
1941 * 'load_job_cache()' - Load jobs from the job.cache file.
1945 load_job_cache(const char *filename
) /* I - job.cache filename */
1947 cups_file_t
*fp
; /* job.cache file */
1948 char line
[1024], /* Line buffer */
1949 *value
; /* Value on line */
1950 int linenum
; /* Line number in file */
1951 cupsd_job_t
*job
; /* Current job */
1952 int jobid
; /* Job ID */
1953 char jobfile
[1024]; /* Job filename */
1957 * Open the job.cache file...
1960 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
1962 if (errno
!= ENOENT
)
1963 cupsdLogMessage(CUPSD_LOG_ERROR
,
1964 "Unable to open job cache file \"%s\": %s",
1965 filename
, strerror(errno
));
1967 load_request_root();
1973 * Read entries from the job cache file and create jobs as needed.
1976 cupsdLogMessage(CUPSD_LOG_INFO
, "Loading job cache file \"%s\"...",
1982 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
1984 if (!strcasecmp(line
, "NextJobId"))
1987 NextJobId
= atoi(value
);
1989 else if (!strcasecmp(line
, "<Job"))
1993 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing </Job> directive on line %d!",
2000 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing job ID on line %d!", linenum
);
2004 jobid
= atoi(value
);
2008 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad job ID %d on line %d!", jobid
,
2013 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, jobid
);
2014 if (access(jobfile
, 0))
2016 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Job %d] Files have gone away!",
2021 job
= calloc(1, sizeof(cupsd_job_t
));
2024 cupsdLogMessage(CUPSD_LOG_EMERG
,
2025 "[Job %d] Unable to allocate memory for job!", jobid
);
2030 job
->back_pipes
[0] = -1;
2031 job
->back_pipes
[1] = -1;
2032 job
->print_pipes
[0] = -1;
2033 job
->print_pipes
[1] = -1;
2034 job
->side_pipes
[0] = -1;
2035 job
->side_pipes
[1] = -1;
2036 job
->status_pipes
[0] = -1;
2037 job
->status_pipes
[1] = -1;
2039 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Loading from cache...", job
->id
);
2043 cupsdLogMessage(CUPSD_LOG_ERROR
,
2044 "Missing <Job #> directive on line %d!", linenum
);
2047 else if (!strcasecmp(line
, "</Job>"))
2049 cupsArrayAdd(Jobs
, job
);
2051 if (job
->state_value
<= IPP_JOB_STOPPED
)
2053 cupsArrayAdd(ActiveJobs
, job
);
2061 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing value on line %d!", linenum
);
2064 else if (!strcasecmp(line
, "State"))
2066 job
->state_value
= (ipp_jstate_t
)atoi(value
);
2068 if (job
->state_value
< IPP_JOB_PENDING
)
2069 job
->state_value
= IPP_JOB_PENDING
;
2070 else if (job
->state_value
> IPP_JOB_COMPLETED
)
2071 job
->state_value
= IPP_JOB_COMPLETED
;
2073 else if (!strcasecmp(line
, "Priority"))
2075 job
->priority
= atoi(value
);
2077 else if (!strcasecmp(line
, "Username"))
2079 cupsdSetString(&job
->username
, value
);
2081 else if (!strcasecmp(line
, "Destination"))
2083 cupsdSetString(&job
->dest
, value
);
2085 else if (!strcasecmp(line
, "DestType"))
2087 job
->dtype
= (cups_ptype_t
)atoi(value
);
2089 else if (!strcasecmp(line
, "NumFiles"))
2091 job
->num_files
= atoi(value
);
2093 if (job
->num_files
< 0)
2095 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad NumFiles value %d on line %d!",
2096 job
->num_files
, linenum
);
2101 if (job
->num_files
> 0)
2103 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-001", RequestRoot
,
2105 if (access(jobfile
, 0))
2107 cupsdLogMessage(CUPSD_LOG_INFO
,
2108 "[Job %d] Data files have gone away!", job
->id
);
2113 job
->filetypes
= calloc(job
->num_files
, sizeof(mime_type_t
*));
2114 job
->compressions
= calloc(job
->num_files
, sizeof(int));
2116 if (!job
->filetypes
|| !job
->compressions
)
2118 cupsdLogMessage(CUPSD_LOG_EMERG
,
2119 "[Job %d] Unable to allocate memory for %d files!",
2120 job
->id
, job
->num_files
);
2125 else if (!strcasecmp(line
, "File"))
2127 int number
, /* File number */
2128 compression
; /* Compression value */
2129 char super
[MIME_MAX_SUPER
], /* MIME super type */
2130 type
[MIME_MAX_TYPE
]; /* MIME type */
2133 if (sscanf(value
, "%d%*[ \t]%15[^/]/%255s%d", &number
, super
, type
,
2136 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File on line %d!", linenum
);
2140 if (number
< 1 || number
> job
->num_files
)
2142 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File number %d on line %d!",
2149 job
->compressions
[number
] = compression
;
2150 job
->filetypes
[number
] = mimeType(MimeDatabase
, super
, type
);
2152 if (!job
->filetypes
[number
])
2155 * If the original MIME type is unknown, auto-type it!
2158 cupsdLogMessage(CUPSD_LOG_ERROR
,
2159 "[Job %d] Unknown MIME type %s/%s for file %d!",
2160 job
->id
, super
, type
, number
+ 1);
2162 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
2163 job
->id
, number
+ 1);
2164 job
->filetypes
[number
] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
2165 job
->compressions
+ number
);
2168 * If that didn't work, assume it is raw...
2171 if (!job
->filetypes
[number
])
2172 job
->filetypes
[number
] = mimeType(MimeDatabase
, "application",
2177 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unknown %s directive on line %d!",
2186 * 'load_next_job_id()' - Load the NextJobId value from the job.cache file.
2190 load_next_job_id(const char *filename
) /* I - job.cache filename */
2192 cups_file_t
*fp
; /* job.cache file */
2193 char line
[1024], /* Line buffer */
2194 *value
; /* Value on line */
2195 int linenum
; /* Line number in file */
2196 int next_job_id
; /* NextJobId value from line */
2200 * Read the NextJobId directive from the job.cache file and use
2201 * the value (if any).
2204 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
2206 if (errno
!= ENOENT
)
2207 cupsdLogMessage(CUPSD_LOG_ERROR
,
2208 "Unable to open job cache file \"%s\": %s",
2209 filename
, strerror(errno
));
2214 cupsdLogMessage(CUPSD_LOG_INFO
,
2215 "Loading NextJobId from job cache file \"%s\"...", filename
);
2219 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
2221 if (!strcasecmp(line
, "NextJobId"))
2225 next_job_id
= atoi(value
);
2227 if (next_job_id
> NextJobId
)
2228 NextJobId
= next_job_id
;
2239 * 'load_request_root()' - Load jobs from the RequestRoot directory.
2243 load_request_root(void)
2245 cups_dir_t
*dir
; /* Directory */
2246 cups_dentry_t
*dent
; /* Directory entry */
2247 cupsd_job_t
*job
; /* New job */
2251 * Open the requests directory...
2254 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Scanning %s for jobs...", RequestRoot
);
2256 if ((dir
= cupsDirOpen(RequestRoot
)) == NULL
)
2258 cupsdLogMessage(CUPSD_LOG_ERROR
,
2259 "Unable to open spool directory \"%s\": %s",
2260 RequestRoot
, strerror(errno
));
2265 * Read all the c##### files...
2268 while ((dent
= cupsDirRead(dir
)) != NULL
)
2269 if (strlen(dent
->filename
) >= 6 && dent
->filename
[0] == 'c')
2272 * Allocate memory for the job...
2275 if ((job
= calloc(sizeof(cupsd_job_t
), 1)) == NULL
)
2277 cupsdLogMessage(CUPSD_LOG_ERROR
, "Ran out of memory for jobs!");
2283 * Assign the job ID...
2286 job
->id
= atoi(dent
->filename
+ 1);
2287 job
->back_pipes
[0] = -1;
2288 job
->back_pipes
[1] = -1;
2289 job
->print_pipes
[0] = -1;
2290 job
->print_pipes
[1] = -1;
2291 job
->side_pipes
[0] = -1;
2292 job
->side_pipes
[1] = -1;
2293 job
->status_pipes
[0] = -1;
2294 job
->status_pipes
[1] = -1;
2296 if (job
->id
>= NextJobId
)
2297 NextJobId
= job
->id
+ 1;
2306 * Insert the job into the array, sorting by job priority and ID...
2309 cupsArrayAdd(Jobs
, job
);
2311 if (job
->state_value
<= IPP_JOB_STOPPED
)
2312 cupsArrayAdd(ActiveJobs
, job
);
2322 * 'set_time()' - Set one of the "time-at-xyz" attributes...
2326 set_time(cupsd_job_t
*job
, /* I - Job to update */
2327 const char *name
) /* I - Name of attribute */
2329 ipp_attribute_t
*attr
; /* Time attribute */
2332 if ((attr
= ippFindAttribute(job
->attrs
, name
, IPP_TAG_ZERO
)) != NULL
)
2334 attr
->value_tag
= IPP_TAG_INTEGER
;
2335 attr
->values
[0].integer
= time(NULL
);
2341 * 'set_hold_until()' - Set the hold time and update job-hold-until attribute...
2345 set_hold_until(cupsd_job_t
*job
, /* I - Job to update */
2346 time_t holdtime
) /* I - Hold until time */
2348 ipp_attribute_t
*attr
; /* job-hold-until attribute */
2349 struct tm
*holddate
; /* Hold date */
2350 char holdstr
[64]; /* Hold time */
2354 * Set the hold_until value and hold the job...
2357 cupsdLogMessage(CUPSD_LOG_DEBUG
, "set_hold_until: hold_until = %d",
2360 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
2361 job
->state_value
= IPP_JOB_HELD
;
2362 job
->hold_until
= holdtime
;
2365 * Update the job-hold-until attribute with a string representing GMT
2366 * time (HH:MM:SS)...
2369 holddate
= gmtime(&holdtime
);
2370 snprintf(holdstr
, sizeof(holdstr
), "%d:%d:%d", holddate
->tm_hour
,
2371 holddate
->tm_min
, holddate
->tm_sec
);
2373 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2374 IPP_TAG_KEYWORD
)) == NULL
)
2375 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2378 * Either add the attribute or update the value of the existing one
2382 attr
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
2383 "job-hold-until", NULL
, holdstr
);
2385 cupsdSetString(&attr
->values
[0].string
.text
, holdstr
);
2392 * 'start_job()' - Start a print job.
2396 start_job(cupsd_job_t
*job
, /* I - Job ID */
2397 cupsd_printer_t
*printer
) /* I - Printer to print job */
2399 int i
; /* Looping var */
2400 int slot
; /* Pipe slot */
2401 cups_array_t
*filters
, /* Filters for job */
2402 *prefilters
; /* Filters with prefilters */
2403 mime_filter_t
*filter
, /* Current filter */
2404 *prefilter
, /* Prefilter */
2405 port_monitor
; /* Port monitor filter */
2406 char method
[255], /* Method for output */
2407 *optptr
, /* Pointer to options */
2408 *valptr
; /* Pointer in value string */
2409 ipp_attribute_t
*attr
; /* Current attribute */
2410 struct stat backinfo
; /* Backend file information */
2411 int backroot
; /* Run backend as root? */
2412 int pid
; /* Process ID of new filter process */
2413 int banner_page
; /* 1 if banner page, 0 otherwise */
2414 int filterfds
[2][2];/* Pipes used between filters */
2415 int envc
; /* Number of environment variables */
2416 char **argv
, /* Filter command-line arguments */
2417 sani_uri
[1024], /* Sanitized DEVICE_URI env var */
2418 filename
[1024], /* Job filename */
2419 command
[1024], /* Full path to command */
2420 jobid
[255], /* Job ID string */
2421 title
[IPP_MAX_NAME
],
2422 /* Job title string */
2423 copies
[255], /* # copies string */
2424 *envp
[MAX_ENV
+ 15],
2425 /* Environment variables */
2426 charset
[255], /* CHARSET env variable */
2427 class_name
[255],/* CLASS env variable */
2428 classification
[1024],
2429 /* CLASSIFICATION env variable */
2431 /* CONTENT_TYPE env variable */
2433 /* DEVICE_URI env variable */
2434 final_content_type
[1024],
2435 /* FINAL_CONTENT_TYPE env variable */
2436 lang
[255], /* LANG env variable */
2437 ppd
[1024], /* PPD env variable */
2439 /* PRINTER env variable */
2441 /* RIP_MAX_CACHE env variable */
2442 static char *options
= NULL
;/* Full list of options */
2443 static int optlength
= 0; /* Length of option buffer */
2446 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] start_job: file = %d/%d",
2447 job
->id
, job
->current_file
, job
->num_files
);
2449 if (job
->num_files
== 0)
2451 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Job %d] No files, canceling job!",
2454 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2459 * Figure out what filters are required to convert from
2460 * the source to the destination type...
2469 * Remote jobs and raw queues go directly to the printer without
2473 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2474 "[Job %d] Sending job to queue tagged as raw...", job
->id
);
2481 * Local jobs get filtered...
2484 filters
= mimeFilter(MimeDatabase
, job
->filetypes
[job
->current_file
],
2485 printer
->filetype
, &(job
->cost
));
2489 cupsdLogMessage(CUPSD_LOG_ERROR
,
2490 "[Job %d] Unable to convert file %d to printable format!",
2491 job
->current_file
, job
->id
);
2492 cupsdLogMessage(CUPSD_LOG_INFO
,
2493 "Hint: Do you have ESP Ghostscript installed?");
2495 if (LogLevel
< CUPSD_LOG_DEBUG
)
2496 cupsdLogMessage(CUPSD_LOG_INFO
,
2497 "Hint: Try setting the LogLevel to \"debug\".");
2499 job
->current_file
++;
2501 if (job
->current_file
== job
->num_files
)
2502 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2508 * Remove NULL ("-") filters...
2511 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
2513 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
2514 if (!strcmp(filter
->filter
, "-"))
2515 cupsArrayRemove(filters
, filter
);
2517 if (cupsArrayCount(filters
) == 0)
2519 cupsArrayDelete(filters
);
2524 * If this printer has any pre-filters, insert the required pre-filter
2525 * in the filters array...
2528 if (printer
->prefiltertype
&& filters
)
2530 prefilters
= cupsArrayNew(NULL
, NULL
);
2532 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
2534 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
2536 if ((prefilter
= mimeFilterLookup(MimeDatabase
, filter
->src
,
2537 printer
->prefiltertype
)))
2539 cupsArrayAdd(prefilters
, prefilter
);
2540 job
->cost
+= prefilter
->cost
;
2543 cupsArrayAdd(prefilters
, filter
);
2546 cupsArrayDelete(filters
);
2547 filters
= prefilters
;
2552 * Set a minimum cost of 100 for all jobs so that FilterLimit
2553 * works with raw queues and other low-cost paths.
2556 if (job
->cost
< 100)
2560 * See if the filter cost is too high...
2563 if ((FilterLevel
+ job
->cost
) > FilterLimit
&& FilterLevel
> 0 &&
2567 * Don't print this job quite yet...
2570 cupsArrayDelete(filters
);
2572 cupsdLogMessage(CUPSD_LOG_INFO
,
2573 "[Job %d] Holding because filter limit has been reached.",
2575 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2576 "[Job %d] start_job: file=%d, cost=%d, level=%d, limit=%d",
2577 job
->id
, job
->current_file
, job
->cost
, FilterLevel
,
2582 FilterLevel
+= job
->cost
;
2585 * Add decompression/raw filter as needed...
2588 if ((!printer
->raw
&& job
->compressions
[job
->current_file
]) ||
2589 (!filters
&& !printer
->remote
&&
2590 (job
->num_files
> 1 || !strncmp(printer
->device_uri
, "file:", 5))))
2593 * Add gziptoany filter to the front of the list...
2597 filters
= cupsArrayNew(NULL
, NULL
);
2599 if (!cupsArrayInsert(filters
, &gziptoany_filter
))
2601 cupsdLogMessage(CUPSD_LOG_ERROR
,
2602 "[Job %d] Unable to add decompression filter - %s",
2603 job
->id
, strerror(errno
));
2605 cupsArrayDelete(filters
);
2607 job
->current_file
++;
2609 if (job
->current_file
== job
->num_files
)
2610 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2617 * Add port monitor, if any...
2620 if (printer
->port_monitor
)
2623 * Add port monitor to the end of the list...
2627 filters
= cupsArrayNew(NULL
, NULL
);
2629 if (!cupsArrayAdd(filters
, &port_monitor
))
2631 cupsdLogMessage(CUPSD_LOG_ERROR
,
2632 "[Job %d] Unable to add port monitor - %s",
2633 job
->id
, strerror(errno
));
2635 cupsArrayDelete(filters
);
2637 job
->current_file
++;
2639 if (job
->current_file
== job
->num_files
)
2640 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2645 snprintf(port_monitor
.filter
, sizeof(port_monitor
.filter
),
2646 "%s/monitor/%s", ServerBin
, printer
->port_monitor
);
2650 * Update the printer and job state to "processing"...
2653 job
->state
->values
[0].integer
= IPP_JOB_PROCESSING
;
2654 job
->state_value
= IPP_JOB_PROCESSING
;
2657 job
->printer
= printer
;
2660 cupsdSetPrinterState(printer
, IPP_PRINTER_PROCESSING
, 0);
2662 if (job
->current_file
== 0)
2665 * Set the processing time...
2668 set_time(job
, "time-at-processing");
2671 * Create the backchannel pipes and make them non-blocking...
2674 cupsdOpenPipe(job
->back_pipes
);
2676 fcntl(job
->back_pipes
[0], F_SETFL
,
2677 fcntl(job
->back_pipes
[0], F_GETFL
) | O_NONBLOCK
);
2679 fcntl(job
->back_pipes
[1], F_SETFL
,
2680 fcntl(job
->back_pipes
[1], F_GETFL
) | O_NONBLOCK
);
2683 * Create the side-channel pipes and make them non-blocking...
2686 socketpair(AF_LOCAL
, SOCK_STREAM
, 0, job
->side_pipes
);
2688 fcntl(job
->side_pipes
[0], F_SETFL
,
2689 fcntl(job
->side_pipes
[0], F_GETFL
) | O_NONBLOCK
);
2691 fcntl(job
->side_pipes
[1], F_SETFL
,
2692 fcntl(job
->side_pipes
[1], F_GETFL
) | O_NONBLOCK
);
2696 * Determine if we are printing a banner page or not...
2699 if (job
->job_sheets
== NULL
)
2701 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] No job-sheets attribute.",
2703 if ((job
->job_sheets
=
2704 ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_ZERO
)) != NULL
)
2705 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2706 "[Job %d] ... but someone added one without setting "
2707 "job_sheets!", job
->id
);
2709 else if (job
->job_sheets
->num_values
== 1)
2710 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] job-sheets=%s", job
->id
,
2711 job
->job_sheets
->values
[0].string
.text
);
2713 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] job-sheets=%s,%s", job
->id
,
2714 job
->job_sheets
->values
[0].string
.text
,
2715 job
->job_sheets
->values
[1].string
.text
);
2717 if (printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
))
2719 else if (job
->job_sheets
== NULL
)
2721 else if (strcasecmp(job
->job_sheets
->values
[0].string
.text
, "none") != 0 &&
2722 job
->current_file
== 0)
2724 else if (job
->job_sheets
->num_values
> 1 &&
2725 strcasecmp(job
->job_sheets
->values
[1].string
.text
, "none") != 0 &&
2726 job
->current_file
== (job
->num_files
- 1))
2731 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] banner_page = %d", job
->id
,
2735 * Building the options string is harder than it needs to be, but
2736 * for the moment we need to pass strings for command-line args and
2737 * not IPP attribute pointers... :)
2739 * First allocate/reallocate the option buffer as needed...
2742 i
= ipp_length(job
->attrs
);
2749 optptr
= realloc(options
, i
);
2753 cupsdLogMessage(CUPSD_LOG_CRIT
,
2754 "[Job %d] Unable to allocate %d bytes for option buffer!",
2757 cupsArrayDelete(filters
);
2759 FilterLevel
-= job
->cost
;
2761 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2770 * Now loop through the attributes and convert them to the textual
2771 * representation used by the filters...
2777 snprintf(title
, sizeof(title
), "%s-%d", printer
->name
, job
->id
);
2778 strcpy(copies
, "1");
2780 for (attr
= job
->attrs
->attrs
; attr
!= NULL
; attr
= attr
->next
)
2782 if (!strcmp(attr
->name
, "copies") &&
2783 attr
->value_tag
== IPP_TAG_INTEGER
)
2786 * Don't use the # copies attribute if we are printing the job sheets...
2790 sprintf(copies
, "%d", attr
->values
[0].integer
);
2792 else if (!strcmp(attr
->name
, "job-name") &&
2793 (attr
->value_tag
== IPP_TAG_NAME
||
2794 attr
->value_tag
== IPP_TAG_NAMELANG
))
2795 strlcpy(title
, attr
->values
[0].string
.text
, sizeof(title
));
2796 else if (attr
->group_tag
== IPP_TAG_JOB
)
2799 * Filter out other unwanted attributes...
2802 if (attr
->value_tag
== IPP_TAG_MIMETYPE
||
2803 attr
->value_tag
== IPP_TAG_NAMELANG
||
2804 attr
->value_tag
== IPP_TAG_TEXTLANG
||
2805 (attr
->value_tag
== IPP_TAG_URI
&& strcmp(attr
->name
, "job-uuid")) ||
2806 attr
->value_tag
== IPP_TAG_URISCHEME
||
2807 attr
->value_tag
== IPP_TAG_BEGIN_COLLECTION
) /* Not yet supported */
2810 if (!strncmp(attr
->name
, "time-", 5))
2813 if (!strncmp(attr
->name
, "job-", 4) && strcmp(attr
->name
, "job-uuid") &&
2814 !(printer
->type
& CUPS_PRINTER_REMOTE
))
2817 if (!strncmp(attr
->name
, "job-", 4) &&
2818 strcmp(attr
->name
, "job-uuid") &&
2819 strcmp(attr
->name
, "job-billing") &&
2820 strcmp(attr
->name
, "job-sheets") &&
2821 strcmp(attr
->name
, "job-hold-until") &&
2822 strcmp(attr
->name
, "job-priority"))
2825 if ((!strcmp(attr
->name
, "page-label") ||
2826 !strcmp(attr
->name
, "page-border") ||
2827 !strncmp(attr
->name
, "number-up", 9) ||
2828 !strcmp(attr
->name
, "page-ranges") ||
2829 !strcmp(attr
->name
, "page-set") ||
2830 !strcasecmp(attr
->name
, "AP_FIRSTPAGE_InputSlot") ||
2831 !strcasecmp(attr
->name
, "AP_FIRSTPAGE_ManualFeed")) &&
2836 * Otherwise add them to the list...
2839 if (optptr
> options
)
2840 strlcat(optptr
, " ", optlength
- (optptr
- options
));
2842 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
2844 strlcat(optptr
, attr
->name
, optlength
- (optptr
- options
));
2845 strlcat(optptr
, "=", optlength
- (optptr
- options
));
2848 for (i
= 0; i
< attr
->num_values
; i
++)
2851 strlcat(optptr
, ",", optlength
- (optptr
- options
));
2853 optptr
+= strlen(optptr
);
2855 switch (attr
->value_tag
)
2857 case IPP_TAG_INTEGER
:
2859 snprintf(optptr
, optlength
- (optptr
- options
),
2860 "%d", attr
->values
[i
].integer
);
2863 case IPP_TAG_BOOLEAN
:
2864 if (!attr
->values
[i
].boolean
)
2865 strlcat(optptr
, "no", optlength
- (optptr
- options
));
2867 case IPP_TAG_NOVALUE
:
2868 strlcat(optptr
, attr
->name
,
2869 optlength
- (optptr
- options
));
2872 case IPP_TAG_RANGE
:
2873 if (attr
->values
[i
].range
.lower
== attr
->values
[i
].range
.upper
)
2874 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2875 "%d", attr
->values
[i
].range
.lower
);
2877 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2878 "%d-%d", attr
->values
[i
].range
.lower
,
2879 attr
->values
[i
].range
.upper
);
2882 case IPP_TAG_RESOLUTION
:
2883 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2884 "%dx%d%s", attr
->values
[i
].resolution
.xres
,
2885 attr
->values
[i
].resolution
.yres
,
2886 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
2890 case IPP_TAG_STRING
:
2893 case IPP_TAG_KEYWORD
:
2894 case IPP_TAG_CHARSET
:
2895 case IPP_TAG_LANGUAGE
:
2897 for (valptr
= attr
->values
[i
].string
.text
; *valptr
;)
2899 if (strchr(" \t\n\\\'\"", *valptr
))
2901 *optptr
++ = *valptr
++;
2908 break; /* anti-compiler-warning-code */
2912 optptr
+= strlen(optptr
);
2917 * Build the command-line arguments for the filters. Each filter
2918 * has 6 or 7 arguments:
2922 * argv[2] = username
2924 * argv[4] = # copies
2926 * argv[6] = filename (optional; normally stdin)
2928 * This allows legacy printer drivers that use the old System V
2929 * printing interface to be used by CUPS.
2931 * For remote jobs, we send all of the files in the argument list.
2934 if (printer
->remote
&& job
->num_files
> 1)
2935 argv
= calloc(7 + job
->num_files
, sizeof(char *));
2937 argv
= calloc(8, sizeof(char *));
2939 sprintf(jobid
, "%d", job
->id
);
2941 argv
[0] = printer
->name
;
2943 argv
[2] = job
->username
;
2948 if (printer
->remote
&& job
->num_files
> 1)
2950 for (i
= 0; i
< job
->num_files
; i
++)
2952 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
2954 argv
[6 + i
] = strdup(filename
);
2959 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
2960 job
->id
, job
->current_file
+ 1);
2964 for (i
= 0; argv
[i
]; i
++)
2965 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2966 "[Job %d] argv[%d]=\"%s\"", job
->id
, i
, argv
[i
]);
2969 * Create environment variable strings for the filters...
2972 attr
= ippFindAttribute(job
->attrs
, "attributes-natural-language",
2975 switch (strlen(attr
->values
[0].string
.text
))
2979 * This is an unknown or badly formatted language code; use
2980 * the POSIX locale...
2983 strcpy(lang
, "LANG=C");
2988 * Just the language code (ll)...
2991 snprintf(lang
, sizeof(lang
), "LANG=%s",
2992 attr
->values
[0].string
.text
);
2997 * Language and country code (ll-cc)...
3000 snprintf(lang
, sizeof(lang
), "LANG=%c%c_%c%c",
3001 attr
->values
[0].string
.text
[0],
3002 attr
->values
[0].string
.text
[1],
3003 toupper(attr
->values
[0].string
.text
[3] & 255),
3004 toupper(attr
->values
[0].string
.text
[4] & 255));
3008 attr
= ippFindAttribute(job
->attrs
, "document-format",
3011 (optptr
= strstr(attr
->values
[0].string
.text
, "charset=")) != NULL
)
3012 snprintf(charset
, sizeof(charset
), "CHARSET=%s", optptr
+ 8);
3015 attr
= ippFindAttribute(job
->attrs
, "attributes-charset",
3017 snprintf(charset
, sizeof(charset
), "CHARSET=%s",
3018 attr
->values
[0].string
.text
);
3021 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s/%s",
3022 job
->filetypes
[job
->current_file
]->super
,
3023 job
->filetypes
[job
->current_file
]->type
);
3024 snprintf(device_uri
, sizeof(device_uri
), "DEVICE_URI=%s",
3025 printer
->device_uri
);
3026 cupsdSanitizeURI(printer
->device_uri
, sani_uri
, sizeof(sani_uri
));
3027 snprintf(ppd
, sizeof(ppd
), "PPD=%s/ppd/%s.ppd", ServerRoot
, printer
->name
);
3028 snprintf(printer_name
, sizeof(printer_name
), "PRINTER=%s", printer
->name
);
3029 snprintf(rip_max_cache
, sizeof(rip_max_cache
), "RIP_MAX_CACHE=%s", RIPCache
);
3031 envc
= cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
3033 envp
[envc
++] = charset
;
3034 envp
[envc
++] = lang
;
3035 envp
[envc
++] = ppd
;
3036 envp
[envc
++] = rip_max_cache
;
3037 envp
[envc
++] = content_type
;
3038 envp
[envc
++] = device_uri
;
3039 envp
[envc
++] = printer_name
;
3041 if (!printer
->remote
&& !printer
->raw
&&
3042 (filter
= (mime_filter_t
*)cupsArrayLast(filters
)) != NULL
&&
3045 snprintf(final_content_type
, sizeof(final_content_type
),
3046 "FINAL_CONTENT_TYPE=%s/%s",
3047 filter
->dst
->super
, filter
->dst
->type
);
3048 envp
[envc
++] = final_content_type
;
3051 if (Classification
&& !banner_page
)
3053 if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
3054 IPP_TAG_NAME
)) == NULL
)
3055 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3057 else if (attr
->num_values
> 1 &&
3058 strcmp(attr
->values
[1].string
.text
, "none") != 0)
3059 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3060 attr
->values
[1].string
.text
);
3062 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3063 attr
->values
[0].string
.text
);
3065 envp
[envc
++] = classification
;
3068 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
3070 snprintf(class_name
, sizeof(class_name
), "CLASS=%s", job
->dest
);
3071 envp
[envc
++] = class_name
;
3074 if (job
->auth_username
)
3075 envp
[envc
++] = job
->auth_username
;
3076 if (job
->auth_domain
)
3077 envp
[envc
++] = job
->auth_domain
;
3078 if (job
->auth_password
)
3079 envp
[envc
++] = job
->auth_password
;
3083 envp
[envc
++] = job
->ccname
;
3084 #endif /* HAVE_GSSAPI */
3088 for (i
= 0; i
< envc
; i
++)
3089 if (!strncmp(envp
[i
], "AUTH_", 5))
3090 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] envp[%d]=\"AUTH_%c****\"",
3091 job
->id
, i
, envp
[i
][5]);
3092 else if (strncmp(envp
[i
], "DEVICE_URI=", 11))
3093 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] envp[%d]=\"%s\"",
3094 job
->id
, i
, envp
[i
]);
3096 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] envp[%d]=\"DEVICE_URI=%s\"",
3097 job
->id
, i
, sani_uri
);
3099 if (printer
->remote
)
3100 job
->current_file
= job
->num_files
;
3102 job
->current_file
++;
3105 * Now create processes for all of the filters...
3108 filterfds
[0][0] = -1;
3109 filterfds
[0][1] = -1;
3110 filterfds
[1][0] = -1;
3111 filterfds
[1][1] = -1;
3113 if (!job
->status_buffer
)
3115 if (cupsdOpenPipe(job
->status_pipes
))
3117 cupsdLogMessage(CUPSD_LOG_ERROR
,
3118 "[Job %d] Unable to create job status pipes - %s.",
3119 job
->id
, strerror(errno
));
3120 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3121 "Unable to create status pipes - %s.", strerror(errno
));
3123 cupsdAddPrinterHistory(printer
);
3125 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3126 "Job canceled because the server could not create the job "
3132 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3133 "[Job %d] start_job: status_pipes = [ %d %d ]",
3134 job
->id
, job
->status_pipes
[0], job
->status_pipes
[1]);
3136 job
->status_buffer
= cupsdStatBufNew(job
->status_pipes
[0], "[Job %d]",
3138 job
->status_level
= CUPSD_LOG_INFO
;
3142 memset(job
->filters
, 0, sizeof(job
->filters
));
3144 for (i
= 0, slot
= 0, filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
3146 i
++, filter
= (mime_filter_t
*)cupsArrayNext(filters
))
3148 if (filter
->filter
[0] != '/')
3149 snprintf(command
, sizeof(command
), "%s/filter/%s", ServerBin
,
3152 strlcpy(command
, filter
->filter
, sizeof(command
));
3154 if (i
< (cupsArrayCount(filters
) - 1))
3156 if (cupsdOpenPipe(filterfds
[slot
]))
3158 cupsdLogMessage(CUPSD_LOG_ERROR
,
3159 "[Job %d] Unable to create job filter pipes - %s.",
3160 job
->id
, strerror(errno
));
3161 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3162 "Unable to create filter pipes - %s.", strerror(errno
));
3163 cupsdAddPrinterHistory(printer
);
3165 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3166 "Job canceled because the server could not create the "
3174 if (job
->current_file
== 1)
3176 if (strncmp(printer
->device_uri
, "file:", 5) != 0)
3178 if (cupsdOpenPipe(job
->print_pipes
))
3180 cupsdLogMessage(CUPSD_LOG_ERROR
,
3181 "[Job %d] Unable to create job backend pipes - %s.",
3182 job
->id
, strerror(errno
));
3183 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3184 "Unable to create backend pipes - %s.", strerror(errno
));
3185 cupsdAddPrinterHistory(printer
);
3187 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3188 "Job canceled because the server could not create "
3189 "the backend pipes.");
3196 job
->print_pipes
[0] = -1;
3197 if (!strcmp(printer
->device_uri
, "file:/dev/null") ||
3198 !strcmp(printer
->device_uri
, "file:///dev/null"))
3199 job
->print_pipes
[1] = -1;
3202 if (!strncmp(printer
->device_uri
, "file:/dev/", 10))
3203 job
->print_pipes
[1] = open(printer
->device_uri
+ 5,
3205 else if (!strncmp(printer
->device_uri
, "file:///dev/", 12))
3206 job
->print_pipes
[1] = open(printer
->device_uri
+ 7,
3208 else if (!strncmp(printer
->device_uri
, "file:///", 8))
3209 job
->print_pipes
[1] = open(printer
->device_uri
+ 7,
3210 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
3212 job
->print_pipes
[1] = open(printer
->device_uri
+ 5,
3213 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
3215 if (job
->print_pipes
[1] < 0)
3217 cupsdLogMessage(CUPSD_LOG_ERROR
,
3218 "[Job %d] Unable to open output file \"%s\" - %s.",
3219 job
->id
, printer
->device_uri
, strerror(errno
));
3220 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3221 "Unable to open output file \"%s\" - %s.",
3222 printer
->device_uri
, strerror(errno
));
3224 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3225 "Job canceled because the server could not open the "
3231 fcntl(job
->print_pipes
[1], F_SETFD
,
3232 fcntl(job
->print_pipes
[1], F_GETFD
) | FD_CLOEXEC
);
3236 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3237 "[Job %d] start_job: print_pipes = [ %d %d ]",
3238 job
->id
, job
->print_pipes
[0], job
->print_pipes
[1]);
3241 filterfds
[slot
][0] = job
->print_pipes
[0];
3242 filterfds
[slot
][1] = job
->print_pipes
[1];
3245 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] start_job: filter=\"%s\"",
3247 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3248 "[Job %d] start_job: filterfds[%d]=[ %d %d ]",
3249 job
->id
, slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3251 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
3252 filterfds
[slot
][1], job
->status_pipes
[1],
3253 job
->back_pipes
[0], job
->side_pipes
[0], 0,
3256 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3257 "[Job %d] start_job: Closing filter pipes for slot %d "
3259 job
->id
, !slot
, filterfds
[!slot
][0], filterfds
[!slot
][1]);
3261 cupsdClosePipe(filterfds
[!slot
]);
3265 cupsdLogMessage(CUPSD_LOG_ERROR
,
3266 "[Job %d] Unable to start filter \"%s\" - %s.",
3267 job
->id
, filter
->filter
, strerror(errno
));
3268 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3269 "Unable to start filter \"%s\" - %s.",
3270 filter
->filter
, strerror(errno
));
3272 cupsdAddPrinterHistory(printer
);
3274 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3275 "Job canceled because the server could not execute a "
3281 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Started filter %s (PID %d)",
3282 job
->id
, command
, pid
);
3288 cupsArrayDelete(filters
);
3291 * Finally, pipe the final output into a backend process if needed...
3294 if (strncmp(printer
->device_uri
, "file:", 5) != 0)
3296 if (job
->current_file
== 1)
3298 sscanf(printer
->device_uri
, "%254[^:]", method
);
3299 snprintf(command
, sizeof(command
), "%s/backend/%s", ServerBin
, method
);
3302 * See if the backend needs to run as root...
3307 else if (stat(command
, &backinfo
))
3310 backroot
= !(backinfo
.st_mode
& (S_IRWXG
| S_IRWXO
));
3314 filterfds
[slot
][0] = -1;
3315 filterfds
[slot
][1] = -1;
3317 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] start_job: backend=\"%s\"",
3319 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3320 "[Job %d] start_job: filterfds[%d] = [ %d %d ]", job
->id
,
3321 slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3323 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
3324 filterfds
[slot
][1], job
->status_pipes
[1],
3325 job
->back_pipes
[1], job
->side_pipes
[1],
3326 backroot
, &(job
->backend
));
3330 cupsdLogMessage(CUPSD_LOG_ERROR
,
3331 "[Job %d] Unable to start backend \"%s\" - %s.",
3332 job
->id
, method
, strerror(errno
));
3333 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3334 "Unable to start backend \"%s\" - %s.", method
,
3337 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3338 "Job canceled because the server could not execute "
3345 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Started backend %s (PID %d)",
3346 job
->id
, command
, pid
);
3350 if (job
->current_file
== job
->num_files
)
3352 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3353 "[Job %d] start_job: Closing print pipes [ %d %d ]...",
3354 job
->id
, job
->print_pipes
[0], job
->print_pipes
[1]);
3356 cupsdClosePipe(job
->print_pipes
);
3358 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3359 "[Job %d] start_job: Closing back pipes [ %d %d ]...",
3360 job
->id
, job
->back_pipes
[0], job
->back_pipes
[1]);
3362 cupsdClosePipe(job
->back_pipes
);
3364 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3365 "[Job %d] start_job: Closing side pipes [ %d %d ]...",
3366 job
->id
, job
->side_pipes
[0], job
->side_pipes
[1]);
3368 cupsdClosePipe(job
->side_pipes
);
3370 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3371 "[Job %d] start_job: Closing status output pipe %d...",
3372 job
->id
, job
->status_pipes
[1]);
3374 close(job
->status_pipes
[1]);
3375 job
->status_pipes
[1] = -1;
3380 filterfds
[slot
][0] = -1;
3381 filterfds
[slot
][1] = -1;
3383 if (job
->current_file
== job
->num_files
)
3385 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3386 "[Job %d] start_job: Closing print pipes [ %d %d ]...",
3387 job
->id
, job
->print_pipes
[0], job
->print_pipes
[1]);
3389 cupsdClosePipe(job
->print_pipes
);
3391 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3392 "[Job %d] start_job: Closing status output pipe %d...",
3393 job
->id
, job
->status_pipes
[1]);
3395 close(job
->status_pipes
[1]);
3396 job
->status_pipes
[1] = -1;
3400 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3401 "[Job %d] start_job: Closing filter pipes for slot %d "
3403 job
->id
, slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3404 cupsdClosePipe(filterfds
[slot
]);
3406 if (printer
->remote
&& job
->num_files
> 1)
3408 for (i
= 0; i
< job
->num_files
; i
++)
3414 cupsdAddSelect(job
->status_buffer
->fd
, (cupsd_selfunc_t
)update_job
, NULL
,
3417 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
, "Job #%d started.",
3424 * If we get here, we need to abort the current job and close out all
3425 * files and pipes...
3430 for (slot
= 0; slot
< 2; slot
++)
3432 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3433 "[Job %d] start_job: Closing filter pipes for slot %d "
3435 job
->id
, slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3436 cupsdClosePipe(filterfds
[slot
]);
3439 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3440 "[Job %d] start_job: Closing status pipes [ %d %d ]...",
3441 job
->id
, job
->status_pipes
[0], job
->status_pipes
[1]);
3442 cupsdClosePipe(job
->status_pipes
);
3443 cupsdStatBufDelete(job
->status_buffer
);
3445 job
->status_buffer
= NULL
;
3447 cupsArrayDelete(filters
);
3449 if (printer
->remote
&& job
->num_files
> 1)
3451 for (i
= 0; i
< job
->num_files
; i
++)
3457 cupsdStopJob(job
, 0);
3462 * 'unload_job()' - Unload a job from memory.
3466 unload_job(cupsd_job_t
*job
) /* I - Job */
3471 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Unloading...", job
->id
);
3473 ippDelete(job
->attrs
);
3478 job
->job_sheets
= NULL
;
3479 job
->printer_message
= NULL
;
3480 job
->printer_reasons
= NULL
;
3485 * 'update_job()' - Read a status update from a job's filters.
3489 update_job(cupsd_job_t
*job
) /* I - Job to check */
3491 int i
; /* Looping var */
3492 int copies
; /* Number of copies printed */
3493 char message
[1024], /* Message text */
3494 *ptr
; /* Pointer update... */
3495 int loglevel
, /* Log level for message */
3496 event
= 0; /* Events? */
3499 while ((ptr
= cupsdStatBufUpdate(job
->status_buffer
, &loglevel
,
3500 message
, sizeof(message
))) != NULL
)
3503 * Process page and printer state messages as needed...
3506 if (loglevel
== CUPSD_LOG_PAGE
)
3509 * Page message; send the message to the page_log file and update the
3510 * job sheet count...
3513 if (job
->sheets
!= NULL
)
3515 if (!strncasecmp(message
, "total ", 6))
3518 * Got a total count of pages from a backend or filter...
3521 copies
= atoi(message
+ 6);
3522 copies
-= job
->sheets
->values
[0].integer
; /* Just track the delta */
3524 else if (!sscanf(message
, "%*d%d", &copies
))
3527 job
->sheets
->values
[0].integer
+= copies
;
3529 if (job
->printer
->page_limit
)
3531 cupsd_quota_t
*q
= cupsdUpdateQuota(job
->printer
, job
->username
,
3535 if (AppleQuotas
&& q
->page_count
== -3)
3538 * Quota limit exceeded, cancel job in progress immediately...
3541 cupsdLogMessage(CUPSD_LOG_INFO
,
3542 "[Job %d] Canceled because pages exceed user %s "
3543 "quota limit on printer %s (%s).",
3544 job
->id
, job
->username
, job
->printer
->name
,
3545 job
->printer
->info
);
3547 cupsdCancelJob(job
, 1, IPP_JOB_CANCELED
);
3552 #endif /* __APPLE__ */
3556 cupsdLogPage(job
, message
);
3558 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
3559 "Printed %d page(s).", job
->sheets
->values
[0].integer
);
3561 else if (loglevel
== CUPSD_LOG_STATE
)
3563 if (!strcmp(message
, "paused"))
3565 cupsdStopPrinter(job
->printer
, 1);
3570 cupsdSetPrinterReasons(job
->printer
, message
);
3571 cupsdAddPrinterHistory(job
->printer
);
3572 event
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
3575 update_job_attrs(job
);
3577 else if (loglevel
== CUPSD_LOG_ATTR
)
3580 * Set attribute(s)...
3583 int num_attrs
; /* Number of attributes */
3584 cups_option_t
*attrs
; /* Attributes */
3585 const char *attr
; /* Attribute */
3588 num_attrs
= cupsParseOptions(message
, 0, &attrs
);
3590 if ((attr
= cupsGetOption("auth-info-required", num_attrs
,
3592 cupsdSetAuthInfoRequired(job
->printer
, attr
, NULL
);
3594 if ((attr
= cupsGetOption("printer-alert", num_attrs
, attrs
)) != NULL
)
3596 cupsdSetString(&job
->printer
->alert
, attr
);
3597 event
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
3600 if ((attr
= cupsGetOption("printer-alert-description", num_attrs
,
3603 cupsdSetString(&job
->printer
->alert_description
, attr
);
3604 event
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
3607 cupsFreeOptions(num_attrs
, attrs
);
3610 else if (!strncmp(message
, "recoverable:", 12))
3612 cupsdSetPrinterReasons(job
->printer
,
3613 "+com.apple.print.recoverable-warning");
3616 while (isspace(*ptr
& 255))
3619 cupsdSetString(&job
->printer
->recoverable
, ptr
);
3620 cupsdAddPrinterHistory(job
->printer
);
3621 event
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
3623 else if (!strncmp(message
, "recovered:", 10))
3625 cupsdSetPrinterReasons(job
->printer
,
3626 "-com.apple.print.recoverable-warning");
3629 while (isspace(*ptr
& 255))
3632 cupsdSetString(&job
->printer
->recoverable
, ptr
);
3633 cupsdAddPrinterHistory(job
->printer
);
3634 event
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
3636 #endif /* __APPLE__ */
3637 else if (loglevel
<= job
->status_level
)
3640 * Some message to show in the printer-state-message attribute...
3643 if (loglevel
!= CUPSD_LOG_NOTICE
)
3644 job
->status_level
= loglevel
;
3646 strlcpy(job
->printer
->state_message
, message
,
3647 sizeof(job
->printer
->state_message
));
3648 cupsdAddPrinterHistory(job
->printer
);
3649 event
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
3651 update_job_attrs(job
);
3654 if (!strchr(job
->status_buffer
->buffer
, '\n'))
3658 if ((event
& CUPSD_EVENT_PRINTER_STATE_CHANGED
))
3659 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE_CHANGED
, job
->printer
, NULL
,
3660 (job
->printer
->type
& CUPS_PRINTER_CLASS
) ?
3661 "Class \"%s\" state changed." :
3662 "Printer \"%s\" state changed.",
3663 job
->printer
->name
);
3665 if (ptr
== NULL
&& !job
->status_buffer
->bufused
)
3668 * See if all of the filters and the backend have returned their
3672 for (i
= 0; job
->filters
[i
] < 0; i
++);
3674 if (job
->filters
[i
])
3677 if (job
->current_file
>= job
->num_files
&& job
->backend
> 0)
3681 * Handle the end of job stuff...
3684 cupsdFinishJob(job
);
3690 * 'update_job_attrs()' - Update the job-printer-* attributes.
3694 update_job_attrs(cupsd_job_t
*job
) /* I - Job to update */
3696 int i
; /* Looping var */
3697 int num_reasons
; /* Actual number of reasons */
3698 const char * const *reasons
; /* Reasons */
3699 static const char *none
= "none", /* "none" */
3705 * Get/create the job-printer-state-* attributes...
3708 if (!job
->printer_message
)
3710 if ((job
->printer_message
= ippFindAttribute(job
->attrs
,
3711 "job-printer-state-message",
3712 IPP_TAG_TEXT
)) == NULL
)
3713 job
->printer_message
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_TEXT
,
3714 "job-printer-state-message",
3718 if (!job
->printer_reasons
)
3719 job
->printer_reasons
= ippFindAttribute(job
->attrs
,
3720 "job-printer-state-reasons",
3724 * If the job isn't printing, return now...
3731 * Otherwise copy the printer-state-message value...
3734 if (job
->printer
->state_message
[0])
3735 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
),
3736 job
->printer
->state_message
);
3739 * ... and the printer-state-reasons value...
3742 if (job
->printer
->num_reasons
== 0)
3745 reasons
= job
->printer
->state
== IPP_PRINTER_STOPPED
? &paused
: &none
;
3749 num_reasons
= job
->printer
->num_reasons
;
3750 reasons
= (const char * const *)job
->printer
->reasons
;
3753 if (!job
->printer_reasons
|| job
->printer_reasons
->num_values
!= num_reasons
)
3755 ippDeleteAttribute(job
->attrs
, job
->printer_reasons
);
3757 job
->printer_reasons
= ippAddStrings(job
->attrs
,
3758 IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
3759 "job-printer-state-reasons",
3760 num_reasons
, NULL
, NULL
);
3763 for (i
= 0; i
< num_reasons
; i
++)
3764 cupsdSetString(&(job
->printer_reasons
->values
[i
].string
.text
), reasons
[i
]);
3769 * End of "$Id: job.c 6755 2007-08-01 19:02:47Z mike $".