2 * "$Id: job.c 6689 2007-07-18 23:52:15Z 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
))
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 cupsdStopJob(job
, 1);
718 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
719 "Job stopped due to filter errors; please consult the "
720 "error_log file for details.");
726 * Job printed successfully; cancel it...
729 if (job
->current_file
< job
->num_files
)
732 * Start the next file in the job...
735 FilterLevel
-= job
->cost
;
736 start_job(job
, printer
);
741 * Close out this job...
744 cupsdCancelJob(job
, 0, IPP_JOB_COMPLETED
);
752 * 'cupsdFreeAllJobs()' - Free all jobs from memory.
756 cupsdFreeAllJobs(void)
758 cupsd_job_t
*job
; /* Current job */
769 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
771 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
773 cupsArrayRemove(Jobs
, job
);
774 cupsArrayRemove(ActiveJobs
, job
);
779 cupsdReleaseSignals();
784 * 'cupsdFindJob()' - Find the specified job.
787 cupsd_job_t
* /* O - Job data */
788 cupsdFindJob(int id
) /* I - Job ID */
790 cupsd_job_t key
; /* Search key */
795 return ((cupsd_job_t
*)cupsArrayFind(Jobs
, &key
));
800 * 'cupsdGetPrinterJobCount()' - Get the number of pending, processing,
801 * or held jobs in a printer or class.
804 int /* O - Job count */
805 cupsdGetPrinterJobCount(
806 const char *dest
) /* I - Printer or class name */
808 int count
; /* Job count */
809 cupsd_job_t
*job
; /* Current job */
812 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
814 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
815 if (job
->dest
&& !strcasecmp(job
->dest
, dest
))
823 * 'cupsdGetUserJobCount()' - Get the number of pending, processing,
824 * or held jobs for a user.
827 int /* O - Job count */
828 cupsdGetUserJobCount(
829 const char *username
) /* I - Username */
831 int count
; /* Job count */
832 cupsd_job_t
*job
; /* Current job */
835 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
837 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
838 if (!strcasecmp(job
->username
, username
))
846 * 'cupsdHoldJob()' - Hold the specified job.
850 cupsdHoldJob(cupsd_job_t
*job
) /* I - Job data */
852 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdHoldJob: id = %d", job
->id
);
854 if (job
->state_value
== IPP_JOB_PROCESSING
)
855 cupsdStopJob(job
, 0);
859 DEBUG_puts("cupsdHoldJob: setting state to held...");
861 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
862 job
->state_value
= IPP_JOB_HELD
;
871 * 'cupsdLoadAllJobs()' - Load all jobs from disk.
875 cupsdLoadAllJobs(void)
877 char filename
[1024]; /* Full filename of job.cache file */
878 struct stat fileinfo
, /* Information on job.cache file */
879 dirinfo
; /* Information on RequestRoot dir */
884 * Create the job arrays as needed...
888 Jobs
= cupsArrayNew(compare_jobs
, NULL
);
891 ActiveJobs
= cupsArrayNew(compare_active_jobs
, NULL
);
894 * See whether the job.cache file is older than the RequestRoot directory...
897 snprintf(filename
, sizeof(filename
), "%s/job.cache", CacheDir
);
899 if (stat(filename
, &fileinfo
))
901 fileinfo
.st_mtime
= 0;
904 cupsdLogMessage(CUPSD_LOG_ERROR
,
905 "Unable to get file information for \"%s\" - %s",
906 filename
, strerror(errno
));
909 if (stat(RequestRoot
, &dirinfo
))
911 dirinfo
.st_mtime
= 0;
914 cupsdLogMessage(CUPSD_LOG_ERROR
,
915 "Unable to get directory information for \"%s\" - %s",
916 RequestRoot
, strerror(errno
));
920 * Load the most recent source for job data...
923 if (dirinfo
.st_mtime
> fileinfo
.st_mtime
)
927 load_next_job_id(filename
);
930 load_job_cache(filename
);
933 * Clean out old jobs as needed...
936 if (MaxJobs
> 0 && cupsArrayCount(Jobs
) >= MaxJobs
)
942 * 'cupsdLoadJob()' - Load a single job...
946 cupsdLoadJob(cupsd_job_t
*job
) /* I - Job */
948 char jobfile
[1024]; /* Job filename */
949 cups_file_t
*fp
; /* Job file */
950 int fileid
; /* Current file ID */
951 ipp_attribute_t
*attr
; /* Job attribute */
952 const char *dest
; /* Destination name */
953 cupsd_printer_t
*destptr
; /* Pointer to destination */
954 mime_type_t
**filetypes
; /* New filetypes array */
955 int *compressions
; /* New compressions array */
960 if (job
->state_value
> IPP_JOB_STOPPED
)
961 job
->access_time
= time(NULL
);
966 if ((job
->attrs
= ippNew()) == NULL
)
968 cupsdLogMessage(CUPSD_LOG_ERROR
,
969 "[Job %d] Ran out of memory for job attributes!", job
->id
);
974 * Load job attributes...
977 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Loading attributes...", job
->id
);
979 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, job
->id
);
980 if ((fp
= cupsFileOpen(jobfile
, "r")) == NULL
)
982 cupsdLogMessage(CUPSD_LOG_ERROR
,
983 "[Job %d] Unable to open job control file \"%s\" - %s!",
984 job
->id
, jobfile
, strerror(errno
));
985 ippDelete(job
->attrs
);
990 if (ippReadIO(fp
, (ipp_iocb_t
)cupsFileRead
, 1, NULL
, job
->attrs
) != IPP_DATA
)
992 cupsdLogMessage(CUPSD_LOG_ERROR
,
993 "[Job %d] Unable to read job control file \"%s\"!",
996 ippDelete(job
->attrs
);
1005 * Copy attribute data to the job object...
1008 if ((job
->state
= ippFindAttribute(job
->attrs
, "job-state",
1009 IPP_TAG_ENUM
)) == NULL
)
1011 cupsdLogMessage(CUPSD_LOG_ERROR
,
1012 "[Job %d] Missing or bad job-state attribute in "
1015 ippDelete(job
->attrs
);
1021 job
->state_value
= (ipp_jstate_t
)job
->state
->values
[0].integer
;
1025 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1026 IPP_TAG_URI
)) == NULL
)
1028 cupsdLogMessage(CUPSD_LOG_ERROR
,
1029 "[Job %d] No job-printer-uri attribute in control file!",
1031 ippDelete(job
->attrs
);
1037 if ((dest
= cupsdValidateDest(attr
->values
[0].string
.text
, &(job
->dtype
),
1040 cupsdLogMessage(CUPSD_LOG_ERROR
,
1041 "[Job %d] Unable to queue job for destination \"%s\"!",
1042 job
->id
, attr
->values
[0].string
.text
);
1043 ippDelete(job
->attrs
);
1049 cupsdSetString(&job
->dest
, dest
);
1051 else if ((destptr
= cupsdFindDest(job
->dest
)) == NULL
)
1053 cupsdLogMessage(CUPSD_LOG_ERROR
,
1054 "[Job %d] Unable to queue job for destination \"%s\"!",
1055 job
->id
, job
->dest
);
1056 ippDelete(job
->attrs
);
1062 job
->sheets
= ippFindAttribute(job
->attrs
, "job-media-sheets-completed",
1064 job
->job_sheets
= ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_NAME
);
1068 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
1069 IPP_TAG_INTEGER
)) == NULL
)
1071 cupsdLogMessage(CUPSD_LOG_ERROR
,
1072 "[Job %d] Missing or bad job-priority attribute in "
1073 "control file!", job
->id
);
1074 ippDelete(job
->attrs
);
1080 job
->priority
= attr
->values
[0].integer
;
1085 if ((attr
= ippFindAttribute(job
->attrs
, "job-originating-user-name",
1086 IPP_TAG_NAME
)) == NULL
)
1088 cupsdLogMessage(CUPSD_LOG_ERROR
,
1089 "[Job %d] Missing or bad job-originating-user-name "
1090 "attribute in control file!", job
->id
);
1091 ippDelete(job
->attrs
);
1097 cupsdSetString(&job
->username
, attr
->values
[0].string
.text
);
1101 * Set the job hold-until time and state...
1104 if (job
->state_value
== IPP_JOB_HELD
)
1106 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1107 IPP_TAG_KEYWORD
)) == NULL
)
1108 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1111 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
1114 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1115 job
->state_value
= IPP_JOB_PENDING
;
1118 else if (job
->state_value
== IPP_JOB_PROCESSING
)
1120 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1121 job
->state_value
= IPP_JOB_PENDING
;
1124 if (!job
->num_files
)
1127 * Find all the d##### files...
1130 for (fileid
= 1; fileid
< 10000; fileid
++)
1132 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
1135 if (access(jobfile
, 0))
1138 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1139 "[Job %d] Auto-typing document file \"%s\"...",
1142 if (fileid
> job
->num_files
)
1144 if (job
->num_files
== 0)
1146 compressions
= (int *)calloc(fileid
, sizeof(int));
1147 filetypes
= (mime_type_t
**)calloc(fileid
, sizeof(mime_type_t
*));
1151 compressions
= (int *)realloc(job
->compressions
,
1152 sizeof(int) * fileid
);
1153 filetypes
= (mime_type_t
**)realloc(job
->filetypes
,
1154 sizeof(mime_type_t
*) *
1158 if (!compressions
|| !filetypes
)
1160 cupsdLogMessage(CUPSD_LOG_ERROR
,
1161 "[Job %d] Ran out of memory for job file types!",
1166 job
->compressions
= compressions
;
1167 job
->filetypes
= filetypes
;
1168 job
->num_files
= fileid
;
1171 job
->filetypes
[fileid
- 1] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
1172 job
->compressions
+ fileid
- 1);
1174 if (!job
->filetypes
[fileid
- 1])
1175 job
->filetypes
[fileid
- 1] = mimeType(MimeDatabase
, "application",
1181 * Load authentication information as needed...
1184 if (job
->state_value
< IPP_JOB_STOPPED
)
1186 snprintf(jobfile
, sizeof(jobfile
), "%s/a%05d", RequestRoot
, job
->id
);
1188 cupsdClearString(&job
->auth_username
);
1189 cupsdClearString(&job
->auth_domain
);
1190 cupsdClearString(&job
->auth_password
);
1192 if ((fp
= cupsFileOpen(jobfile
, "r")) != NULL
)
1194 int i
, /* Looping var */
1195 bytes
; /* Size of auth data */
1196 char line
[255], /* Line from file */
1197 data
[255]; /* Decoded data */
1201 i
< destptr
->num_auth_info_required
&&
1202 cupsFileGets(fp
, line
, sizeof(line
));
1205 bytes
= sizeof(data
);
1206 httpDecode64_2(data
, &bytes
, line
);
1208 if (!strcmp(destptr
->auth_info_required
[i
], "username"))
1209 cupsdSetStringf(&job
->auth_username
, "AUTH_USERNAME=%s", data
);
1210 else if (!strcmp(destptr
->auth_info_required
[i
], "domain"))
1211 cupsdSetStringf(&job
->auth_domain
, "AUTH_DOMAIN=%s", data
);
1212 else if (!strcmp(destptr
->auth_info_required
[i
], "password"))
1213 cupsdSetStringf(&job
->auth_password
, "AUTH_PASSWORD=%s", data
);
1219 job
->access_time
= time(NULL
);
1224 * 'cupsdMoveJob()' - Move the specified job to a different destination.
1228 cupsdMoveJob(cupsd_job_t
*job
, /* I - Job */
1229 cupsd_printer_t
*p
) /* I - Destination printer or class */
1231 ipp_attribute_t
*attr
; /* job-printer-uri attribute */
1232 const char *olddest
; /* Old destination */
1233 cupsd_printer_t
*oldp
; /* Old pointer */
1237 * Don't move completed jobs...
1240 if (job
->state_value
> IPP_JOB_STOPPED
)
1244 * Get the old destination...
1247 olddest
= job
->dest
;
1250 oldp
= job
->printer
;
1252 oldp
= cupsdFindDest(olddest
);
1255 * Change the destination information...
1260 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, oldp
, job
,
1261 "Job #%d moved from %s to %s.", job
->id
, olddest
,
1264 cupsdSetString(&job
->dest
, p
->name
);
1265 job
->dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_REMOTE
|
1266 CUPS_PRINTER_IMPLICIT
);
1268 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1269 IPP_TAG_URI
)) != NULL
)
1270 cupsdSetString(&(attr
->values
[0].string
.text
), p
->uri
);
1272 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, p
, job
,
1273 "Job #%d moved from %s to %s.", job
->id
, olddest
,
1281 * 'cupsdReleaseJob()' - Release the specified job.
1285 cupsdReleaseJob(cupsd_job_t
*job
) /* I - Job */
1287 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdReleaseJob: id = %d", job
->id
);
1289 if (job
->state_value
== IPP_JOB_HELD
)
1291 DEBUG_puts("cupsdReleaseJob: setting state to pending...");
1293 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1294 job
->state_value
= IPP_JOB_PENDING
;
1302 * 'cupsdRestartJob()' - Restart the specified job.
1306 cupsdRestartJob(cupsd_job_t
*job
) /* I - Job */
1308 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdRestartJob: id = %d", job
->id
);
1310 if (job
->state_value
== IPP_JOB_STOPPED
|| job
->num_files
)
1312 ipp_jstate_t old_state
; /* Old job state */
1317 old_state
= job
->state_value
;
1320 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1321 job
->state_value
= IPP_JOB_PENDING
;
1325 if (old_state
> IPP_JOB_STOPPED
)
1326 cupsArrayAdd(ActiveJobs
, job
);
1334 * 'cupsdSaveAllJobs()' - Save a summary of all jobs to disk.
1338 cupsdSaveAllJobs(void)
1340 int i
; /* Looping var */
1341 cups_file_t
*fp
; /* Job cache file */
1342 char temp
[1024]; /* Temporary string */
1343 cupsd_job_t
*job
; /* Current job */
1344 time_t curtime
; /* Current time */
1345 struct tm
*curdate
; /* Current date */
1348 snprintf(temp
, sizeof(temp
), "%s/job.cache", CacheDir
);
1349 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
1351 cupsdLogMessage(CUPSD_LOG_ERROR
,
1352 "Unable to create job cache file \"%s\" - %s",
1353 temp
, strerror(errno
));
1357 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving job cache file \"%s\"...", temp
);
1360 * Restrict access to the file...
1363 fchown(cupsFileNumber(fp
), getuid(), Group
);
1364 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
1367 * Write a small header to the file...
1370 curtime
= time(NULL
);
1371 curdate
= localtime(&curtime
);
1372 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
1374 cupsFilePuts(fp
, "# Job cache file for " CUPS_SVERSION
"\n");
1375 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
1376 cupsFilePrintf(fp
, "NextJobId %d\n", NextJobId
);
1379 * Write each job known to the system...
1382 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1384 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1386 cupsFilePrintf(fp
, "<Job %d>\n", job
->id
);
1387 cupsFilePrintf(fp
, "State %d\n", job
->state_value
);
1388 cupsFilePrintf(fp
, "Priority %d\n", job
->priority
);
1389 cupsFilePrintf(fp
, "Username %s\n", job
->username
);
1390 cupsFilePrintf(fp
, "Destination %s\n", job
->dest
);
1391 cupsFilePrintf(fp
, "DestType %d\n", job
->dtype
);
1392 cupsFilePrintf(fp
, "NumFiles %d\n", job
->num_files
);
1393 for (i
= 0; i
< job
->num_files
; i
++)
1394 cupsFilePrintf(fp
, "File %d %s/%s %d\n", i
+ 1, job
->filetypes
[i
]->super
,
1395 job
->filetypes
[i
]->type
, job
->compressions
[i
]);
1396 cupsFilePuts(fp
, "</Job>\n");
1404 * 'cupsdSaveJob()' - Save a job to disk.
1408 cupsdSaveJob(cupsd_job_t
*job
) /* I - Job */
1410 char filename
[1024]; /* Job control filename */
1411 cups_file_t
*fp
; /* Job file */
1414 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSaveJob(job=%p(%d)): job->attrs=%p",
1415 job
, job
->id
, job
->attrs
);
1417 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
, job
->id
);
1419 if ((fp
= cupsFileOpen(filename
, "w")) == NULL
)
1421 cupsdLogMessage(CUPSD_LOG_ERROR
,
1422 "[Job %d] Unable to create job control file \"%s\" - %s.",
1423 job
->id
, filename
, strerror(errno
));
1427 fchmod(cupsFileNumber(fp
), 0600);
1428 fchown(cupsFileNumber(fp
), RunUser
, Group
);
1430 job
->attrs
->state
= IPP_IDLE
;
1432 if (ippWriteIO(fp
, (ipp_iocb_t
)cupsFileWrite
, 1, NULL
,
1433 job
->attrs
) != IPP_DATA
)
1434 cupsdLogMessage(CUPSD_LOG_ERROR
,
1435 "[Job %d] Unable to write job control file!", job
->id
);
1442 * 'cupsdSetJobHoldUntil()' - Set the hold time for a job...
1446 cupsdSetJobHoldUntil(cupsd_job_t
*job
, /* I - Job */
1447 const char *when
) /* I - When to resume */
1449 time_t curtime
; /* Current time */
1450 struct tm
*curdate
; /* Current date */
1451 int hour
; /* Hold hour */
1452 int minute
; /* Hold minute */
1453 int second
; /* Hold second */
1456 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetJobHoldUntil(%d, \"%s\")",
1461 if (!strcmp(when
, "indefinite") || !strcmp(when
, "auth-info-required"))
1464 * Hold indefinitely...
1467 job
->hold_until
= 0;
1469 else if (!strcmp(when
, "day-time"))
1472 * Hold to 6am the next morning unless local time is < 6pm.
1475 curtime
= time(NULL
);
1476 curdate
= localtime(&curtime
);
1478 if (curdate
->tm_hour
< 18)
1479 job
->hold_until
= curtime
;
1481 job
->hold_until
= curtime
+
1482 ((29 - curdate
->tm_hour
) * 60 + 59 -
1483 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1485 else if (!strcmp(when
, "evening") || !strcmp(when
, "night"))
1488 * Hold to 6pm unless local time is > 6pm or < 6am.
1491 curtime
= time(NULL
);
1492 curdate
= localtime(&curtime
);
1494 if (curdate
->tm_hour
< 6 || curdate
->tm_hour
>= 18)
1495 job
->hold_until
= curtime
;
1497 job
->hold_until
= curtime
+
1498 ((17 - curdate
->tm_hour
) * 60 + 59 -
1499 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1501 else if (!strcmp(when
, "second-shift"))
1504 * Hold to 4pm unless local time is > 4pm.
1507 curtime
= time(NULL
);
1508 curdate
= localtime(&curtime
);
1510 if (curdate
->tm_hour
>= 16)
1511 job
->hold_until
= curtime
;
1513 job
->hold_until
= curtime
+
1514 ((15 - curdate
->tm_hour
) * 60 + 59 -
1515 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1517 else if (!strcmp(when
, "third-shift"))
1520 * Hold to 12am unless local time is < 8am.
1523 curtime
= time(NULL
);
1524 curdate
= localtime(&curtime
);
1526 if (curdate
->tm_hour
< 8)
1527 job
->hold_until
= curtime
;
1529 job
->hold_until
= curtime
+
1530 ((23 - curdate
->tm_hour
) * 60 + 59 -
1531 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1533 else if (!strcmp(when
, "weekend"))
1536 * Hold to weekend unless we are in the weekend.
1539 curtime
= time(NULL
);
1540 curdate
= localtime(&curtime
);
1542 if (curdate
->tm_wday
|| curdate
->tm_wday
== 6)
1543 job
->hold_until
= curtime
;
1545 job
->hold_until
= curtime
+
1546 (((5 - curdate
->tm_wday
) * 24 +
1547 (17 - curdate
->tm_hour
)) * 60 + 59 -
1548 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1550 else if (sscanf(when
, "%d:%d:%d", &hour
, &minute
, &second
) >= 2)
1553 * Hold to specified GMT time (HH:MM or HH:MM:SS)...
1556 curtime
= time(NULL
);
1557 curdate
= gmtime(&curtime
);
1559 job
->hold_until
= curtime
+
1560 ((hour
- curdate
->tm_hour
) * 60 + minute
-
1561 curdate
->tm_min
) * 60 + second
- curdate
->tm_sec
;
1564 * Hold until next day as needed...
1567 if (job
->hold_until
< curtime
)
1568 job
->hold_until
+= 24 * 60 * 60;
1571 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetJobHoldUntil: hold_until = %d",
1572 (int)job
->hold_until
);
1577 * 'cupsdSetJobPriority()' - Set the priority of a job, moving it up/down in
1578 * the list as needed.
1582 cupsdSetJobPriority(
1583 cupsd_job_t
*job
, /* I - Job ID */
1584 int priority
) /* I - New priority (0 to 100) */
1586 ipp_attribute_t
*attr
; /* Job attribute */
1590 * Don't change completed jobs...
1593 if (job
->state_value
>= IPP_JOB_PROCESSING
)
1597 * Set the new priority and re-add the job into the active list...
1600 cupsArrayRemove(ActiveJobs
, job
);
1602 job
->priority
= priority
;
1604 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
1605 IPP_TAG_INTEGER
)) != NULL
)
1606 attr
->values
[0].integer
= priority
;
1608 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-priority",
1611 cupsArrayAdd(ActiveJobs
, job
);
1618 * 'cupsdStopAllJobs()' - Stop all print jobs.
1622 cupsdStopAllJobs(int force
) /* I - 1 = Force all filters to stop */
1624 cupsd_job_t
*job
; /* Current job */
1627 DEBUG_puts("cupsdStopAllJobs()");
1629 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
1631 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
1632 if (job
->state_value
== IPP_JOB_PROCESSING
)
1634 cupsdStopJob(job
, force
);
1635 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1636 job
->state_value
= IPP_JOB_PENDING
;
1642 * 'cupsdStopJob()' - Stop a print job.
1646 cupsdStopJob(cupsd_job_t
*job
, /* I - Job */
1647 int force
) /* I - 1 = Force all filters to stop */
1649 int i
; /* Looping var */
1652 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1653 "[Job %d] cupsdStopJob: force = %d", job
->id
, force
);
1655 if (job
->state_value
!= IPP_JOB_PROCESSING
)
1658 FilterLevel
-= job
->cost
;
1660 if (job
->printer
->state
== IPP_PRINTER_PROCESSING
)
1661 cupsdSetPrinterState(job
->printer
, IPP_PRINTER_IDLE
, 0);
1663 job
->state
->values
[0].integer
= IPP_JOB_STOPPED
;
1664 job
->state_value
= IPP_JOB_STOPPED
;
1665 job
->printer
->job
= NULL
;
1666 job
->printer
= NULL
;
1668 job
->current_file
--;
1670 for (i
= 0; job
->filters
[i
]; i
++)
1671 if (job
->filters
[i
] > 0)
1673 cupsdEndProcess(job
->filters
[i
], force
);
1674 job
->filters
[i
] = 0;
1677 if (job
->backend
> 0)
1679 cupsdEndProcess(job
->backend
, force
);
1683 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] Closing print pipes [ %d %d ]...",
1684 job
->id
, job
->print_pipes
[0], job
->print_pipes
[1]);
1686 cupsdClosePipe(job
->print_pipes
);
1688 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] Closing back pipes [ %d %d ]...",
1689 job
->id
, job
->back_pipes
[0], job
->back_pipes
[1]);
1691 cupsdClosePipe(job
->back_pipes
);
1693 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] Closing side pipes [ %d %d ]...",
1694 job
->id
, job
->side_pipes
[0], job
->side_pipes
[1]);
1696 cupsdClosePipe(job
->side_pipes
);
1698 if (job
->status_buffer
)
1701 * Close the pipe and clear the input bit.
1704 cupsdRemoveSelect(job
->status_buffer
->fd
);
1706 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1707 "[Job %d] Closing status pipes [ %d %d ]...", job
->id
,
1708 job
->status_pipes
[0], job
->status_pipes
[1]);
1710 cupsdClosePipe(job
->status_pipes
);
1711 cupsdStatBufDelete(job
->status_buffer
);
1713 job
->status_buffer
= NULL
;
1719 * 'cupsdUnloadCompletedJobs()' - Flush completed job history from memory.
1723 cupsdUnloadCompletedJobs(void)
1725 cupsd_job_t
*job
; /* Current job */
1726 time_t expire
; /* Expiration time */
1729 expire
= time(NULL
) - 60;
1731 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1733 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1734 if (job
->attrs
&& job
->state_value
>= IPP_JOB_STOPPED
&&
1735 job
->access_time
< expire
)
1741 * 'compare_active_jobs()' - Compare the job IDs and priorities of two jobs.
1744 static int /* O - Difference */
1745 compare_active_jobs(void *first
, /* I - First job */
1746 void *second
, /* I - Second job */
1747 void *data
) /* I - App data (not used) */
1749 int diff
; /* Difference */
1752 if ((diff
= ((cupsd_job_t
*)second
)->priority
-
1753 ((cupsd_job_t
*)first
)->priority
) != 0)
1756 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
1761 * 'compare_jobs()' - Compare the job IDs of two jobs.
1764 static int /* O - Difference */
1765 compare_jobs(void *first
, /* I - First job */
1766 void *second
, /* I - Second job */
1767 void *data
) /* I - App data (not used) */
1769 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
1774 * 'free_job()' - Free all memory used by a job.
1778 free_job(cupsd_job_t
*job
) /* I - Job */
1780 cupsdClearString(&job
->username
);
1781 cupsdClearString(&job
->dest
);
1782 cupsdClearString(&job
->auth_username
);
1783 cupsdClearString(&job
->auth_domain
);
1784 cupsdClearString(&job
->auth_password
);
1789 * First erase the credential cache file, then clear the string referencing
1790 * it. We know the filename since the string will be of the form
1791 * "KRB5CCNAME=FILE:/foo/bar"...
1794 if (cupsdRemoveFile(job
->ccname
+ 16))
1795 cupsdLogMessage(CUPSD_LOG_ERROR
,
1796 "Unable to remove Kerberos credential cache: %s",
1799 cupsdClearString(&job
->ccname
);
1801 #endif /* HAVE_GSSAPI */
1803 if (job
->num_files
> 0)
1805 free(job
->compressions
);
1806 free(job
->filetypes
);
1809 ippDelete(job
->attrs
);
1816 * 'ipp_length()' - Compute the size of the buffer needed to hold
1817 * the textual IPP attributes.
1820 static int /* O - Size of attribute buffer */
1821 ipp_length(ipp_t
*ipp
) /* I - IPP request */
1823 int bytes
; /* Number of bytes */
1824 int i
; /* Looping var */
1825 ipp_attribute_t
*attr
; /* Current attribute */
1829 * Loop through all attributes...
1834 for (attr
= ipp
->attrs
; attr
!= NULL
; attr
= attr
->next
)
1837 * Skip attributes that won't be sent to filters...
1840 if (attr
->value_tag
== IPP_TAG_MIMETYPE
||
1841 attr
->value_tag
== IPP_TAG_NAMELANG
||
1842 attr
->value_tag
== IPP_TAG_TEXTLANG
||
1843 attr
->value_tag
== IPP_TAG_URI
||
1844 attr
->value_tag
== IPP_TAG_URISCHEME
)
1847 if (strncmp(attr
->name
, "time-", 5) == 0)
1851 * Add space for a leading space and commas between each value.
1852 * For the first attribute, the leading space isn't used, so the
1853 * extra byte can be used as the nul terminator...
1856 bytes
++; /* " " separator */
1857 bytes
+= attr
->num_values
; /* "," separators */
1860 * Boolean attributes appear as "foo,nofoo,foo,nofoo", while
1861 * other attributes appear as "foo=value1,value2,...,valueN".
1864 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
1865 bytes
+= strlen(attr
->name
);
1867 bytes
+= attr
->num_values
* strlen(attr
->name
);
1870 * Now add the size required for each value in the attribute...
1873 switch (attr
->value_tag
)
1875 case IPP_TAG_INTEGER
:
1878 * Minimum value of a signed integer is -2147483647, or 11 digits.
1881 bytes
+= attr
->num_values
* 11;
1884 case IPP_TAG_BOOLEAN
:
1886 * Add two bytes for each false ("no") value...
1889 for (i
= 0; i
< attr
->num_values
; i
++)
1890 if (!attr
->values
[i
].boolean
)
1894 case IPP_TAG_RANGE
:
1896 * A range is two signed integers separated by a hyphen, or
1897 * 23 characters max.
1900 bytes
+= attr
->num_values
* 23;
1903 case IPP_TAG_RESOLUTION
:
1905 * A resolution is two signed integers separated by an "x" and
1906 * suffixed by the units, or 26 characters max.
1909 bytes
+= attr
->num_values
* 26;
1912 case IPP_TAG_STRING
:
1915 case IPP_TAG_KEYWORD
:
1916 case IPP_TAG_CHARSET
:
1917 case IPP_TAG_LANGUAGE
:
1920 * Strings can contain characters that need quoting. We need
1921 * at least 2 * len + 2 characters to cover the quotes and
1922 * any backslashes in the string.
1925 for (i
= 0; i
< attr
->num_values
; i
++)
1926 bytes
+= 2 * strlen(attr
->values
[i
].string
.text
) + 2;
1930 break; /* anti-compiler-warning-code */
1939 * 'load_job_cache()' - Load jobs from the job.cache file.
1943 load_job_cache(const char *filename
) /* I - job.cache filename */
1945 cups_file_t
*fp
; /* job.cache file */
1946 char line
[1024], /* Line buffer */
1947 *value
; /* Value on line */
1948 int linenum
; /* Line number in file */
1949 cupsd_job_t
*job
; /* Current job */
1950 int jobid
; /* Job ID */
1951 char jobfile
[1024]; /* Job filename */
1955 * Open the job.cache file...
1958 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
1960 if (errno
!= ENOENT
)
1961 cupsdLogMessage(CUPSD_LOG_ERROR
,
1962 "Unable to open job cache file \"%s\": %s",
1963 filename
, strerror(errno
));
1965 load_request_root();
1971 * Read entries from the job cache file and create jobs as needed.
1974 cupsdLogMessage(CUPSD_LOG_INFO
, "Loading job cache file \"%s\"...",
1980 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
1982 if (!strcasecmp(line
, "NextJobId"))
1985 NextJobId
= atoi(value
);
1987 else if (!strcasecmp(line
, "<Job"))
1991 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing </Job> directive on line %d!",
1998 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing job ID on line %d!", linenum
);
2002 jobid
= atoi(value
);
2006 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad job ID %d on line %d!", jobid
,
2011 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, jobid
);
2012 if (access(jobfile
, 0))
2014 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Job %d] Files have gone away!",
2019 job
= calloc(1, sizeof(cupsd_job_t
));
2022 cupsdLogMessage(CUPSD_LOG_EMERG
,
2023 "[Job %d] Unable to allocate memory for job!", jobid
);
2028 job
->back_pipes
[0] = -1;
2029 job
->back_pipes
[1] = -1;
2030 job
->print_pipes
[0] = -1;
2031 job
->print_pipes
[1] = -1;
2032 job
->side_pipes
[0] = -1;
2033 job
->side_pipes
[1] = -1;
2034 job
->status_pipes
[0] = -1;
2035 job
->status_pipes
[1] = -1;
2037 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Loading from cache...", job
->id
);
2041 cupsdLogMessage(CUPSD_LOG_ERROR
,
2042 "Missing <Job #> directive on line %d!", linenum
);
2045 else if (!strcasecmp(line
, "</Job>"))
2047 cupsArrayAdd(Jobs
, job
);
2049 if (job
->state_value
<= IPP_JOB_STOPPED
)
2051 cupsArrayAdd(ActiveJobs
, job
);
2059 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing value on line %d!", linenum
);
2062 else if (!strcasecmp(line
, "State"))
2064 job
->state_value
= (ipp_jstate_t
)atoi(value
);
2066 if (job
->state_value
< IPP_JOB_PENDING
)
2067 job
->state_value
= IPP_JOB_PENDING
;
2068 else if (job
->state_value
> IPP_JOB_COMPLETED
)
2069 job
->state_value
= IPP_JOB_COMPLETED
;
2071 else if (!strcasecmp(line
, "Priority"))
2073 job
->priority
= atoi(value
);
2075 else if (!strcasecmp(line
, "Username"))
2077 cupsdSetString(&job
->username
, value
);
2079 else if (!strcasecmp(line
, "Destination"))
2081 cupsdSetString(&job
->dest
, value
);
2083 else if (!strcasecmp(line
, "DestType"))
2085 job
->dtype
= (cups_ptype_t
)atoi(value
);
2087 else if (!strcasecmp(line
, "NumFiles"))
2089 job
->num_files
= atoi(value
);
2091 if (job
->num_files
< 0)
2093 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad NumFiles value %d on line %d!",
2094 job
->num_files
, linenum
);
2099 if (job
->num_files
> 0)
2101 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-001", RequestRoot
,
2103 if (access(jobfile
, 0))
2105 cupsdLogMessage(CUPSD_LOG_INFO
,
2106 "[Job %d] Data files have gone away!", job
->id
);
2111 job
->filetypes
= calloc(job
->num_files
, sizeof(mime_type_t
*));
2112 job
->compressions
= calloc(job
->num_files
, sizeof(int));
2114 if (!job
->filetypes
|| !job
->compressions
)
2116 cupsdLogMessage(CUPSD_LOG_EMERG
,
2117 "[Job %d] Unable to allocate memory for %d files!",
2118 job
->id
, job
->num_files
);
2123 else if (!strcasecmp(line
, "File"))
2125 int number
, /* File number */
2126 compression
; /* Compression value */
2127 char super
[MIME_MAX_SUPER
], /* MIME super type */
2128 type
[MIME_MAX_TYPE
]; /* MIME type */
2131 if (sscanf(value
, "%d%*[ \t]%15[^/]/%255s%d", &number
, super
, type
,
2134 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File on line %d!", linenum
);
2138 if (number
< 1 || number
> job
->num_files
)
2140 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File number %d on line %d!",
2147 job
->compressions
[number
] = compression
;
2148 job
->filetypes
[number
] = mimeType(MimeDatabase
, super
, type
);
2150 if (!job
->filetypes
[number
])
2153 * If the original MIME type is unknown, auto-type it!
2156 cupsdLogMessage(CUPSD_LOG_ERROR
,
2157 "[Job %d] Unknown MIME type %s/%s for file %d!",
2158 job
->id
, super
, type
, number
+ 1);
2160 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
2161 job
->id
, number
+ 1);
2162 job
->filetypes
[number
] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
2163 job
->compressions
+ number
);
2166 * If that didn't work, assume it is raw...
2169 if (!job
->filetypes
[number
])
2170 job
->filetypes
[number
] = mimeType(MimeDatabase
, "application",
2175 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unknown %s directive on line %d!",
2184 * 'load_next_job_id()' - Load the NextJobId value from the job.cache file.
2188 load_next_job_id(const char *filename
) /* I - job.cache filename */
2190 cups_file_t
*fp
; /* job.cache file */
2191 char line
[1024], /* Line buffer */
2192 *value
; /* Value on line */
2193 int linenum
; /* Line number in file */
2194 int next_job_id
; /* NextJobId value from line */
2198 * Read the NextJobId directive from the job.cache file and use
2199 * the value (if any).
2202 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
2204 if (errno
!= ENOENT
)
2205 cupsdLogMessage(CUPSD_LOG_ERROR
,
2206 "Unable to open job cache file \"%s\": %s",
2207 filename
, strerror(errno
));
2212 cupsdLogMessage(CUPSD_LOG_INFO
,
2213 "Loading NextJobId from job cache file \"%s\"...", filename
);
2217 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
2219 if (!strcasecmp(line
, "NextJobId"))
2223 next_job_id
= atoi(value
);
2225 if (next_job_id
> NextJobId
)
2226 NextJobId
= next_job_id
;
2237 * 'load_request_root()' - Load jobs from the RequestRoot directory.
2241 load_request_root(void)
2243 cups_dir_t
*dir
; /* Directory */
2244 cups_dentry_t
*dent
; /* Directory entry */
2245 cupsd_job_t
*job
; /* New job */
2249 * Open the requests directory...
2252 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Scanning %s for jobs...", RequestRoot
);
2254 if ((dir
= cupsDirOpen(RequestRoot
)) == NULL
)
2256 cupsdLogMessage(CUPSD_LOG_ERROR
,
2257 "Unable to open spool directory \"%s\": %s",
2258 RequestRoot
, strerror(errno
));
2263 * Read all the c##### files...
2266 while ((dent
= cupsDirRead(dir
)) != NULL
)
2267 if (strlen(dent
->filename
) >= 6 && dent
->filename
[0] == 'c')
2270 * Allocate memory for the job...
2273 if ((job
= calloc(sizeof(cupsd_job_t
), 1)) == NULL
)
2275 cupsdLogMessage(CUPSD_LOG_ERROR
, "Ran out of memory for jobs!");
2281 * Assign the job ID...
2284 job
->id
= atoi(dent
->filename
+ 1);
2285 job
->back_pipes
[0] = -1;
2286 job
->back_pipes
[1] = -1;
2287 job
->print_pipes
[0] = -1;
2288 job
->print_pipes
[1] = -1;
2289 job
->side_pipes
[0] = -1;
2290 job
->side_pipes
[1] = -1;
2291 job
->status_pipes
[0] = -1;
2292 job
->status_pipes
[1] = -1;
2294 if (job
->id
>= NextJobId
)
2295 NextJobId
= job
->id
+ 1;
2304 * Insert the job into the array, sorting by job priority and ID...
2307 cupsArrayAdd(Jobs
, job
);
2309 if (job
->state_value
<= IPP_JOB_STOPPED
)
2310 cupsArrayAdd(ActiveJobs
, job
);
2320 * 'set_time()' - Set one of the "time-at-xyz" attributes...
2324 set_time(cupsd_job_t
*job
, /* I - Job to update */
2325 const char *name
) /* I - Name of attribute */
2327 ipp_attribute_t
*attr
; /* Time attribute */
2330 if ((attr
= ippFindAttribute(job
->attrs
, name
, IPP_TAG_ZERO
)) != NULL
)
2332 attr
->value_tag
= IPP_TAG_INTEGER
;
2333 attr
->values
[0].integer
= time(NULL
);
2339 * 'set_hold_until()' - Set the hold time and update job-hold-until attribute...
2343 set_hold_until(cupsd_job_t
*job
, /* I - Job to update */
2344 time_t holdtime
) /* I - Hold until time */
2346 ipp_attribute_t
*attr
; /* job-hold-until attribute */
2347 struct tm
*holddate
; /* Hold date */
2348 char holdstr
[64]; /* Hold time */
2352 * Set the hold_until value and hold the job...
2355 cupsdLogMessage(CUPSD_LOG_DEBUG
, "set_hold_until: hold_until = %d",
2358 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
2359 job
->state_value
= IPP_JOB_HELD
;
2360 job
->hold_until
= holdtime
;
2363 * Update the job-hold-until attribute with a string representing GMT
2364 * time (HH:MM:SS)...
2367 holddate
= gmtime(&holdtime
);
2368 snprintf(holdstr
, sizeof(holdstr
), "%d:%d:%d", holddate
->tm_hour
,
2369 holddate
->tm_min
, holddate
->tm_sec
);
2371 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2372 IPP_TAG_KEYWORD
)) == NULL
)
2373 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2376 * Either add the attribute or update the value of the existing one
2380 attr
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
2381 "job-hold-until", NULL
, holdstr
);
2383 cupsdSetString(&attr
->values
[0].string
.text
, holdstr
);
2390 * 'start_job()' - Start a print job.
2394 start_job(cupsd_job_t
*job
, /* I - Job ID */
2395 cupsd_printer_t
*printer
) /* I - Printer to print job */
2397 int i
; /* Looping var */
2398 int slot
; /* Pipe slot */
2399 cups_array_t
*filters
, /* Filters for job */
2400 *prefilters
; /* Filters with prefilters */
2401 mime_filter_t
*filter
, /* Current filter */
2402 *prefilter
, /* Prefilter */
2403 port_monitor
; /* Port monitor filter */
2404 char method
[255], /* Method for output */
2405 *optptr
, /* Pointer to options */
2406 *valptr
; /* Pointer in value string */
2407 ipp_attribute_t
*attr
; /* Current attribute */
2408 struct stat backinfo
; /* Backend file information */
2409 int backroot
; /* Run backend as root? */
2410 int pid
; /* Process ID of new filter process */
2411 int banner_page
; /* 1 if banner page, 0 otherwise */
2412 int filterfds
[2][2];/* Pipes used between filters */
2413 int envc
; /* Number of environment variables */
2414 char **argv
, /* Filter command-line arguments */
2415 sani_uri
[1024], /* Sanitized DEVICE_URI env var */
2416 filename
[1024], /* Job filename */
2417 command
[1024], /* Full path to command */
2418 jobid
[255], /* Job ID string */
2419 title
[IPP_MAX_NAME
],
2420 /* Job title string */
2421 copies
[255], /* # copies string */
2422 *envp
[MAX_ENV
+ 15],
2423 /* Environment variables */
2424 charset
[255], /* CHARSET env variable */
2425 class_name
[255],/* CLASS env variable */
2426 classification
[1024],
2427 /* CLASSIFICATION env variable */
2429 /* CONTENT_TYPE env variable */
2431 /* DEVICE_URI env variable */
2432 final_content_type
[1024],
2433 /* FINAL_CONTENT_TYPE env variable */
2434 lang
[255], /* LANG env variable */
2435 ppd
[1024], /* PPD env variable */
2437 /* PRINTER env variable */
2439 /* RIP_MAX_CACHE env variable */
2440 static char *options
= NULL
;/* Full list of options */
2441 static int optlength
= 0; /* Length of option buffer */
2444 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] start_job: file = %d/%d",
2445 job
->id
, job
->current_file
, job
->num_files
);
2447 if (job
->num_files
== 0)
2449 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Job %d] No files, canceling job!",
2452 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2457 * Figure out what filters are required to convert from
2458 * the source to the destination type...
2467 * Remote jobs and raw queues go directly to the printer without
2471 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2472 "[Job %d] Sending job to queue tagged as raw...", job
->id
);
2479 * Local jobs get filtered...
2482 filters
= mimeFilter(MimeDatabase
, job
->filetypes
[job
->current_file
],
2483 printer
->filetype
, &(job
->cost
));
2487 cupsdLogMessage(CUPSD_LOG_ERROR
,
2488 "[Job %d] Unable to convert file %d to printable format!",
2489 job
->current_file
, job
->id
);
2490 cupsdLogMessage(CUPSD_LOG_INFO
,
2491 "Hint: Do you have ESP Ghostscript installed?");
2493 if (LogLevel
< CUPSD_LOG_DEBUG
)
2494 cupsdLogMessage(CUPSD_LOG_INFO
,
2495 "Hint: Try setting the LogLevel to \"debug\".");
2497 job
->current_file
++;
2499 if (job
->current_file
== job
->num_files
)
2500 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2506 * Remove NULL ("-") filters...
2509 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
2511 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
2512 if (!strcmp(filter
->filter
, "-"))
2513 cupsArrayRemove(filters
, filter
);
2515 if (cupsArrayCount(filters
) == 0)
2517 cupsArrayDelete(filters
);
2522 * If this printer has any pre-filters, insert the required pre-filter
2523 * in the filters array...
2526 if (printer
->prefiltertype
&& filters
)
2528 prefilters
= cupsArrayNew(NULL
, NULL
);
2530 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
2532 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
2534 if ((prefilter
= mimeFilterLookup(MimeDatabase
, filter
->src
,
2535 printer
->prefiltertype
)))
2537 cupsArrayAdd(prefilters
, prefilter
);
2538 job
->cost
+= prefilter
->cost
;
2541 cupsArrayAdd(prefilters
, filter
);
2544 cupsArrayDelete(filters
);
2545 filters
= prefilters
;
2550 * Set a minimum cost of 100 for all jobs so that FilterLimit
2551 * works with raw queues and other low-cost paths.
2554 if (job
->cost
< 100)
2558 * See if the filter cost is too high...
2561 if ((FilterLevel
+ job
->cost
) > FilterLimit
&& FilterLevel
> 0 &&
2565 * Don't print this job quite yet...
2568 cupsArrayDelete(filters
);
2570 cupsdLogMessage(CUPSD_LOG_INFO
,
2571 "[Job %d] Holding because filter limit has been reached.",
2573 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2574 "[Job %d] start_job: file=%d, cost=%d, level=%d, limit=%d",
2575 job
->id
, job
->current_file
, job
->cost
, FilterLevel
,
2580 FilterLevel
+= job
->cost
;
2583 * Add decompression/raw filter as needed...
2586 if ((!printer
->raw
&& job
->compressions
[job
->current_file
]) ||
2587 (!filters
&& !printer
->remote
&&
2588 (job
->num_files
> 1 || !strncmp(printer
->device_uri
, "file:", 5))))
2591 * Add gziptoany filter to the front of the list...
2595 filters
= cupsArrayNew(NULL
, NULL
);
2597 if (!cupsArrayInsert(filters
, &gziptoany_filter
))
2599 cupsdLogMessage(CUPSD_LOG_ERROR
,
2600 "[Job %d] Unable to add decompression filter - %s",
2601 job
->id
, strerror(errno
));
2603 cupsArrayDelete(filters
);
2605 job
->current_file
++;
2607 if (job
->current_file
== job
->num_files
)
2608 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2615 * Add port monitor, if any...
2618 if (printer
->port_monitor
)
2621 * Add port monitor to the end of the list...
2625 filters
= cupsArrayNew(NULL
, NULL
);
2627 if (!cupsArrayAdd(filters
, &port_monitor
))
2629 cupsdLogMessage(CUPSD_LOG_ERROR
,
2630 "[Job %d] Unable to add port monitor - %s",
2631 job
->id
, strerror(errno
));
2633 cupsArrayDelete(filters
);
2635 job
->current_file
++;
2637 if (job
->current_file
== job
->num_files
)
2638 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2643 snprintf(port_monitor
.filter
, sizeof(port_monitor
.filter
),
2644 "%s/monitor/%s", ServerBin
, printer
->port_monitor
);
2648 * Update the printer and job state to "processing"...
2651 job
->state
->values
[0].integer
= IPP_JOB_PROCESSING
;
2652 job
->state_value
= IPP_JOB_PROCESSING
;
2655 job
->printer
= printer
;
2658 cupsdSetPrinterState(printer
, IPP_PRINTER_PROCESSING
, 0);
2660 if (job
->current_file
== 0)
2663 * Set the processing time...
2666 set_time(job
, "time-at-processing");
2669 * Create the backchannel pipes and make them non-blocking...
2672 cupsdOpenPipe(job
->back_pipes
);
2674 fcntl(job
->back_pipes
[0], F_SETFL
,
2675 fcntl(job
->back_pipes
[0], F_GETFL
) | O_NONBLOCK
);
2677 fcntl(job
->back_pipes
[1], F_SETFL
,
2678 fcntl(job
->back_pipes
[1], F_GETFL
) | O_NONBLOCK
);
2681 * Create the side-channel pipes and make them non-blocking...
2684 socketpair(AF_LOCAL
, SOCK_STREAM
, 0, job
->side_pipes
);
2686 fcntl(job
->side_pipes
[0], F_SETFL
,
2687 fcntl(job
->side_pipes
[0], F_GETFL
) | O_NONBLOCK
);
2689 fcntl(job
->side_pipes
[1], F_SETFL
,
2690 fcntl(job
->side_pipes
[1], F_GETFL
) | O_NONBLOCK
);
2694 * Determine if we are printing a banner page or not...
2697 if (job
->job_sheets
== NULL
)
2699 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] No job-sheets attribute.",
2701 if ((job
->job_sheets
=
2702 ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_ZERO
)) != NULL
)
2703 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2704 "[Job %d] ... but someone added one without setting "
2705 "job_sheets!", job
->id
);
2707 else if (job
->job_sheets
->num_values
== 1)
2708 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] job-sheets=%s", job
->id
,
2709 job
->job_sheets
->values
[0].string
.text
);
2711 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] job-sheets=%s,%s", job
->id
,
2712 job
->job_sheets
->values
[0].string
.text
,
2713 job
->job_sheets
->values
[1].string
.text
);
2715 if (printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
))
2717 else if (job
->job_sheets
== NULL
)
2719 else if (strcasecmp(job
->job_sheets
->values
[0].string
.text
, "none") != 0 &&
2720 job
->current_file
== 0)
2722 else if (job
->job_sheets
->num_values
> 1 &&
2723 strcasecmp(job
->job_sheets
->values
[1].string
.text
, "none") != 0 &&
2724 job
->current_file
== (job
->num_files
- 1))
2729 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] banner_page = %d", job
->id
,
2733 * Building the options string is harder than it needs to be, but
2734 * for the moment we need to pass strings for command-line args and
2735 * not IPP attribute pointers... :)
2737 * First allocate/reallocate the option buffer as needed...
2740 i
= ipp_length(job
->attrs
);
2747 optptr
= realloc(options
, i
);
2751 cupsdLogMessage(CUPSD_LOG_CRIT
,
2752 "[Job %d] Unable to allocate %d bytes for option buffer!",
2755 cupsArrayDelete(filters
);
2757 FilterLevel
-= job
->cost
;
2759 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2768 * Now loop through the attributes and convert them to the textual
2769 * representation used by the filters...
2775 snprintf(title
, sizeof(title
), "%s-%d", printer
->name
, job
->id
);
2776 strcpy(copies
, "1");
2778 for (attr
= job
->attrs
->attrs
; attr
!= NULL
; attr
= attr
->next
)
2780 if (!strcmp(attr
->name
, "copies") &&
2781 attr
->value_tag
== IPP_TAG_INTEGER
)
2784 * Don't use the # copies attribute if we are printing the job sheets...
2788 sprintf(copies
, "%d", attr
->values
[0].integer
);
2790 else if (!strcmp(attr
->name
, "job-name") &&
2791 (attr
->value_tag
== IPP_TAG_NAME
||
2792 attr
->value_tag
== IPP_TAG_NAMELANG
))
2793 strlcpy(title
, attr
->values
[0].string
.text
, sizeof(title
));
2794 else if (attr
->group_tag
== IPP_TAG_JOB
)
2797 * Filter out other unwanted attributes...
2800 if (attr
->value_tag
== IPP_TAG_MIMETYPE
||
2801 attr
->value_tag
== IPP_TAG_NAMELANG
||
2802 attr
->value_tag
== IPP_TAG_TEXTLANG
||
2803 (attr
->value_tag
== IPP_TAG_URI
&& strcmp(attr
->name
, "job-uuid")) ||
2804 attr
->value_tag
== IPP_TAG_URISCHEME
||
2805 attr
->value_tag
== IPP_TAG_BEGIN_COLLECTION
) /* Not yet supported */
2808 if (!strncmp(attr
->name
, "time-", 5))
2811 if (!strncmp(attr
->name
, "job-", 4) && strcmp(attr
->name
, "job-uuid") &&
2812 !(printer
->type
& CUPS_PRINTER_REMOTE
))
2815 if (!strncmp(attr
->name
, "job-", 4) &&
2816 strcmp(attr
->name
, "job-uuid") &&
2817 strcmp(attr
->name
, "job-billing") &&
2818 strcmp(attr
->name
, "job-sheets") &&
2819 strcmp(attr
->name
, "job-hold-until") &&
2820 strcmp(attr
->name
, "job-priority"))
2823 if ((!strcmp(attr
->name
, "page-label") ||
2824 !strcmp(attr
->name
, "page-border") ||
2825 !strncmp(attr
->name
, "number-up", 9) ||
2826 !strcmp(attr
->name
, "page-ranges") ||
2827 !strcmp(attr
->name
, "page-set") ||
2828 !strcasecmp(attr
->name
, "AP_FIRSTPAGE_InputSlot") ||
2829 !strcasecmp(attr
->name
, "AP_FIRSTPAGE_ManualFeed")) &&
2834 * Otherwise add them to the list...
2837 if (optptr
> options
)
2838 strlcat(optptr
, " ", optlength
- (optptr
- options
));
2840 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
2842 strlcat(optptr
, attr
->name
, optlength
- (optptr
- options
));
2843 strlcat(optptr
, "=", optlength
- (optptr
- options
));
2846 for (i
= 0; i
< attr
->num_values
; i
++)
2849 strlcat(optptr
, ",", optlength
- (optptr
- options
));
2851 optptr
+= strlen(optptr
);
2853 switch (attr
->value_tag
)
2855 case IPP_TAG_INTEGER
:
2857 snprintf(optptr
, optlength
- (optptr
- options
),
2858 "%d", attr
->values
[i
].integer
);
2861 case IPP_TAG_BOOLEAN
:
2862 if (!attr
->values
[i
].boolean
)
2863 strlcat(optptr
, "no", optlength
- (optptr
- options
));
2865 case IPP_TAG_NOVALUE
:
2866 strlcat(optptr
, attr
->name
,
2867 optlength
- (optptr
- options
));
2870 case IPP_TAG_RANGE
:
2871 if (attr
->values
[i
].range
.lower
== attr
->values
[i
].range
.upper
)
2872 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2873 "%d", attr
->values
[i
].range
.lower
);
2875 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2876 "%d-%d", attr
->values
[i
].range
.lower
,
2877 attr
->values
[i
].range
.upper
);
2880 case IPP_TAG_RESOLUTION
:
2881 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2882 "%dx%d%s", attr
->values
[i
].resolution
.xres
,
2883 attr
->values
[i
].resolution
.yres
,
2884 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
2888 case IPP_TAG_STRING
:
2891 case IPP_TAG_KEYWORD
:
2892 case IPP_TAG_CHARSET
:
2893 case IPP_TAG_LANGUAGE
:
2895 for (valptr
= attr
->values
[i
].string
.text
; *valptr
;)
2897 if (strchr(" \t\n\\\'\"", *valptr
))
2899 *optptr
++ = *valptr
++;
2906 break; /* anti-compiler-warning-code */
2910 optptr
+= strlen(optptr
);
2915 * Build the command-line arguments for the filters. Each filter
2916 * has 6 or 7 arguments:
2920 * argv[2] = username
2922 * argv[4] = # copies
2924 * argv[6] = filename (optional; normally stdin)
2926 * This allows legacy printer drivers that use the old System V
2927 * printing interface to be used by CUPS.
2929 * For remote jobs, we send all of the files in the argument list.
2932 if (printer
->remote
&& job
->num_files
> 1)
2933 argv
= calloc(7 + job
->num_files
, sizeof(char *));
2935 argv
= calloc(8, sizeof(char *));
2937 sprintf(jobid
, "%d", job
->id
);
2939 argv
[0] = printer
->name
;
2941 argv
[2] = job
->username
;
2946 if (printer
->remote
&& job
->num_files
> 1)
2948 for (i
= 0; i
< job
->num_files
; i
++)
2950 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
2952 argv
[6 + i
] = strdup(filename
);
2957 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
2958 job
->id
, job
->current_file
+ 1);
2962 for (i
= 0; argv
[i
]; i
++)
2963 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2964 "[Job %d] argv[%d]=\"%s\"", job
->id
, i
, argv
[i
]);
2967 * Create environment variable strings for the filters...
2970 attr
= ippFindAttribute(job
->attrs
, "attributes-natural-language",
2973 switch (strlen(attr
->values
[0].string
.text
))
2977 * This is an unknown or badly formatted language code; use
2978 * the POSIX locale...
2981 strcpy(lang
, "LANG=C");
2986 * Just the language code (ll)...
2989 snprintf(lang
, sizeof(lang
), "LANG=%s",
2990 attr
->values
[0].string
.text
);
2995 * Language and country code (ll-cc)...
2998 snprintf(lang
, sizeof(lang
), "LANG=%c%c_%c%c",
2999 attr
->values
[0].string
.text
[0],
3000 attr
->values
[0].string
.text
[1],
3001 toupper(attr
->values
[0].string
.text
[3] & 255),
3002 toupper(attr
->values
[0].string
.text
[4] & 255));
3006 attr
= ippFindAttribute(job
->attrs
, "document-format",
3009 (optptr
= strstr(attr
->values
[0].string
.text
, "charset=")) != NULL
)
3010 snprintf(charset
, sizeof(charset
), "CHARSET=%s", optptr
+ 8);
3013 attr
= ippFindAttribute(job
->attrs
, "attributes-charset",
3015 snprintf(charset
, sizeof(charset
), "CHARSET=%s",
3016 attr
->values
[0].string
.text
);
3019 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s/%s",
3020 job
->filetypes
[job
->current_file
]->super
,
3021 job
->filetypes
[job
->current_file
]->type
);
3022 snprintf(device_uri
, sizeof(device_uri
), "DEVICE_URI=%s",
3023 printer
->device_uri
);
3024 cupsdSanitizeURI(printer
->device_uri
, sani_uri
, sizeof(sani_uri
));
3025 snprintf(ppd
, sizeof(ppd
), "PPD=%s/ppd/%s.ppd", ServerRoot
, printer
->name
);
3026 snprintf(printer_name
, sizeof(printer_name
), "PRINTER=%s", printer
->name
);
3027 snprintf(rip_max_cache
, sizeof(rip_max_cache
), "RIP_MAX_CACHE=%s", RIPCache
);
3029 envc
= cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
3031 envp
[envc
++] = charset
;
3032 envp
[envc
++] = lang
;
3033 envp
[envc
++] = ppd
;
3034 envp
[envc
++] = rip_max_cache
;
3035 envp
[envc
++] = content_type
;
3036 envp
[envc
++] = device_uri
;
3037 envp
[envc
++] = printer_name
;
3039 if (!printer
->remote
&& !printer
->raw
&&
3040 (filter
= (mime_filter_t
*)cupsArrayLast(filters
)) != NULL
&&
3043 snprintf(final_content_type
, sizeof(final_content_type
),
3044 "FINAL_CONTENT_TYPE=%s/%s",
3045 filter
->dst
->super
, filter
->dst
->type
);
3046 envp
[envc
++] = final_content_type
;
3049 if (Classification
&& !banner_page
)
3051 if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
3052 IPP_TAG_NAME
)) == NULL
)
3053 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3055 else if (attr
->num_values
> 1 &&
3056 strcmp(attr
->values
[1].string
.text
, "none") != 0)
3057 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3058 attr
->values
[1].string
.text
);
3060 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3061 attr
->values
[0].string
.text
);
3063 envp
[envc
++] = classification
;
3066 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
3068 snprintf(class_name
, sizeof(class_name
), "CLASS=%s", job
->dest
);
3069 envp
[envc
++] = class_name
;
3072 if (job
->auth_username
)
3073 envp
[envc
++] = job
->auth_username
;
3074 if (job
->auth_domain
)
3075 envp
[envc
++] = job
->auth_domain
;
3076 if (job
->auth_password
)
3077 envp
[envc
++] = job
->auth_password
;
3081 envp
[envc
++] = job
->ccname
;
3082 #endif /* HAVE_GSSAPI */
3086 for (i
= 0; i
< envc
; i
++)
3087 if (!strncmp(envp
[i
], "AUTH_", 5))
3088 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] envp[%d]=\"AUTH_%c****\"",
3089 job
->id
, i
, envp
[i
][5]);
3090 else if (strncmp(envp
[i
], "DEVICE_URI=", 11))
3091 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] envp[%d]=\"%s\"",
3092 job
->id
, i
, envp
[i
]);
3094 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] envp[%d]=\"DEVICE_URI=%s\"",
3095 job
->id
, i
, sani_uri
);
3097 if (printer
->remote
)
3098 job
->current_file
= job
->num_files
;
3100 job
->current_file
++;
3103 * Now create processes for all of the filters...
3106 filterfds
[0][0] = -1;
3107 filterfds
[0][1] = -1;
3108 filterfds
[1][0] = -1;
3109 filterfds
[1][1] = -1;
3111 if (!job
->status_buffer
)
3113 if (cupsdOpenPipe(job
->status_pipes
))
3115 cupsdLogMessage(CUPSD_LOG_ERROR
,
3116 "[Job %d] Unable to create job status pipes - %s.",
3117 job
->id
, strerror(errno
));
3118 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3119 "Unable to create status pipes - %s.", strerror(errno
));
3121 cupsdAddPrinterHistory(printer
);
3123 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3124 "Job canceled because the server could not create the job "
3130 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3131 "[Job %d] start_job: status_pipes = [ %d %d ]",
3132 job
->id
, job
->status_pipes
[0], job
->status_pipes
[1]);
3134 job
->status_buffer
= cupsdStatBufNew(job
->status_pipes
[0], "[Job %d]",
3136 job
->status_level
= CUPSD_LOG_INFO
;
3140 memset(job
->filters
, 0, sizeof(job
->filters
));
3142 for (i
= 0, slot
= 0, filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
3144 i
++, filter
= (mime_filter_t
*)cupsArrayNext(filters
))
3146 if (filter
->filter
[0] != '/')
3147 snprintf(command
, sizeof(command
), "%s/filter/%s", ServerBin
,
3150 strlcpy(command
, filter
->filter
, sizeof(command
));
3152 if (i
< (cupsArrayCount(filters
) - 1))
3154 if (cupsdOpenPipe(filterfds
[slot
]))
3156 cupsdLogMessage(CUPSD_LOG_ERROR
,
3157 "[Job %d] Unable to create job filter pipes - %s.",
3158 job
->id
, strerror(errno
));
3159 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3160 "Unable to create filter pipes - %s.", strerror(errno
));
3161 cupsdAddPrinterHistory(printer
);
3163 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3164 "Job canceled because the server could not create the "
3172 if (job
->current_file
== 1)
3174 if (strncmp(printer
->device_uri
, "file:", 5) != 0)
3176 if (cupsdOpenPipe(job
->print_pipes
))
3178 cupsdLogMessage(CUPSD_LOG_ERROR
,
3179 "[Job %d] Unable to create job backend pipes - %s.",
3180 job
->id
, strerror(errno
));
3181 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3182 "Unable to create backend pipes - %s.", strerror(errno
));
3183 cupsdAddPrinterHistory(printer
);
3185 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3186 "Job canceled because the server could not create "
3187 "the backend pipes.");
3194 job
->print_pipes
[0] = -1;
3195 if (!strcmp(printer
->device_uri
, "file:/dev/null") ||
3196 !strcmp(printer
->device_uri
, "file:///dev/null"))
3197 job
->print_pipes
[1] = -1;
3200 if (!strncmp(printer
->device_uri
, "file:/dev/", 10))
3201 job
->print_pipes
[1] = open(printer
->device_uri
+ 5,
3203 else if (!strncmp(printer
->device_uri
, "file:///dev/", 12))
3204 job
->print_pipes
[1] = open(printer
->device_uri
+ 7,
3206 else if (!strncmp(printer
->device_uri
, "file:///", 8))
3207 job
->print_pipes
[1] = open(printer
->device_uri
+ 7,
3208 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
3210 job
->print_pipes
[1] = open(printer
->device_uri
+ 5,
3211 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
3213 if (job
->print_pipes
[1] < 0)
3215 cupsdLogMessage(CUPSD_LOG_ERROR
,
3216 "[Job %d] Unable to open output file \"%s\" - %s.",
3217 job
->id
, printer
->device_uri
, strerror(errno
));
3218 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3219 "Unable to open output file \"%s\" - %s.",
3220 printer
->device_uri
, strerror(errno
));
3222 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3223 "Job canceled because the server could not open the "
3229 fcntl(job
->print_pipes
[1], F_SETFD
,
3230 fcntl(job
->print_pipes
[1], F_GETFD
) | FD_CLOEXEC
);
3234 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3235 "[Job %d] start_job: print_pipes = [ %d %d ]",
3236 job
->id
, job
->print_pipes
[0], job
->print_pipes
[1]);
3239 filterfds
[slot
][0] = job
->print_pipes
[0];
3240 filterfds
[slot
][1] = job
->print_pipes
[1];
3243 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] start_job: filter=\"%s\"",
3245 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3246 "[Job %d] start_job: filterfds[%d]=[ %d %d ]",
3247 job
->id
, slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3249 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
3250 filterfds
[slot
][1], job
->status_pipes
[1],
3251 job
->back_pipes
[0], job
->side_pipes
[0], 0,
3254 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3255 "[Job %d] start_job: Closing filter pipes for slot %d "
3257 job
->id
, !slot
, filterfds
[!slot
][0], filterfds
[!slot
][1]);
3259 cupsdClosePipe(filterfds
[!slot
]);
3263 cupsdLogMessage(CUPSD_LOG_ERROR
,
3264 "[Job %d] Unable to start filter \"%s\" - %s.",
3265 job
->id
, filter
->filter
, strerror(errno
));
3266 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3267 "Unable to start filter \"%s\" - %s.",
3268 filter
->filter
, strerror(errno
));
3270 cupsdAddPrinterHistory(printer
);
3272 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3273 "Job canceled because the server could not execute a "
3279 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Started filter %s (PID %d)",
3280 job
->id
, command
, pid
);
3286 cupsArrayDelete(filters
);
3289 * Finally, pipe the final output into a backend process if needed...
3292 if (strncmp(printer
->device_uri
, "file:", 5) != 0)
3294 if (job
->current_file
== 1)
3296 sscanf(printer
->device_uri
, "%254[^:]", method
);
3297 snprintf(command
, sizeof(command
), "%s/backend/%s", ServerBin
, method
);
3300 * See if the backend needs to run as root...
3305 else if (stat(command
, &backinfo
))
3308 backroot
= !(backinfo
.st_mode
& (S_IRWXG
| S_IRWXO
));
3312 filterfds
[slot
][0] = -1;
3313 filterfds
[slot
][1] = -1;
3315 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] start_job: backend=\"%s\"",
3317 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3318 "[Job %d] start_job: filterfds[%d] = [ %d %d ]", job
->id
,
3319 slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3321 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
3322 filterfds
[slot
][1], job
->status_pipes
[1],
3323 job
->back_pipes
[1], job
->side_pipes
[1],
3324 backroot
, &(job
->backend
));
3328 cupsdLogMessage(CUPSD_LOG_ERROR
,
3329 "[Job %d] Unable to start backend \"%s\" - %s.",
3330 job
->id
, method
, strerror(errno
));
3331 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3332 "Unable to start backend \"%s\" - %s.", method
,
3335 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3336 "Job canceled because the server could not execute "
3343 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Started backend %s (PID %d)",
3344 job
->id
, command
, pid
);
3348 if (job
->current_file
== job
->num_files
)
3350 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3351 "[Job %d] start_job: Closing print pipes [ %d %d ]...",
3352 job
->id
, job
->print_pipes
[0], job
->print_pipes
[1]);
3354 cupsdClosePipe(job
->print_pipes
);
3356 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3357 "[Job %d] start_job: Closing back pipes [ %d %d ]...",
3358 job
->id
, job
->back_pipes
[0], job
->back_pipes
[1]);
3360 cupsdClosePipe(job
->back_pipes
);
3362 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3363 "[Job %d] start_job: Closing side pipes [ %d %d ]...",
3364 job
->id
, job
->side_pipes
[0], job
->side_pipes
[1]);
3366 cupsdClosePipe(job
->side_pipes
);
3368 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3369 "[Job %d] start_job: Closing status output pipe %d...",
3370 job
->id
, job
->status_pipes
[1]);
3372 close(job
->status_pipes
[1]);
3373 job
->status_pipes
[1] = -1;
3378 filterfds
[slot
][0] = -1;
3379 filterfds
[slot
][1] = -1;
3381 if (job
->current_file
== job
->num_files
)
3383 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3384 "[Job %d] start_job: Closing print pipes [ %d %d ]...",
3385 job
->id
, job
->print_pipes
[0], job
->print_pipes
[1]);
3387 cupsdClosePipe(job
->print_pipes
);
3389 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3390 "[Job %d] start_job: Closing status output pipe %d...",
3391 job
->id
, job
->status_pipes
[1]);
3393 close(job
->status_pipes
[1]);
3394 job
->status_pipes
[1] = -1;
3398 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3399 "[Job %d] start_job: Closing filter pipes for slot %d "
3401 job
->id
, slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3402 cupsdClosePipe(filterfds
[slot
]);
3404 if (printer
->remote
&& job
->num_files
> 1)
3406 for (i
= 0; i
< job
->num_files
; i
++)
3412 cupsdAddSelect(job
->status_buffer
->fd
, (cupsd_selfunc_t
)update_job
, NULL
,
3415 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
, "Job #%d started.",
3422 * If we get here, we need to abort the current job and close out all
3423 * files and pipes...
3428 for (slot
= 0; slot
< 2; slot
++)
3430 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3431 "[Job %d] start_job: Closing filter pipes for slot %d "
3433 job
->id
, slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3434 cupsdClosePipe(filterfds
[slot
]);
3437 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3438 "[Job %d] start_job: Closing status pipes [ %d %d ]...",
3439 job
->id
, job
->status_pipes
[0], job
->status_pipes
[1]);
3440 cupsdClosePipe(job
->status_pipes
);
3441 cupsdStatBufDelete(job
->status_buffer
);
3443 job
->status_buffer
= NULL
;
3445 cupsArrayDelete(filters
);
3447 if (printer
->remote
&& job
->num_files
> 1)
3449 for (i
= 0; i
< job
->num_files
; i
++)
3455 cupsdStopJob(job
, 0);
3460 * 'unload_job()' - Unload a job from memory.
3464 unload_job(cupsd_job_t
*job
) /* I - Job */
3469 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Unloading...", job
->id
);
3471 ippDelete(job
->attrs
);
3476 job
->job_sheets
= NULL
;
3477 job
->printer_message
= NULL
;
3478 job
->printer_reasons
= NULL
;
3483 * 'update_job()' - Read a status update from a job's filters.
3487 update_job(cupsd_job_t
*job
) /* I - Job to check */
3489 int i
; /* Looping var */
3490 int copies
; /* Number of copies printed */
3491 char message
[1024], /* Message text */
3492 *ptr
; /* Pointer update... */
3493 int loglevel
, /* Log level for message */
3494 event
= 0; /* Events? */
3497 while ((ptr
= cupsdStatBufUpdate(job
->status_buffer
, &loglevel
,
3498 message
, sizeof(message
))) != NULL
)
3501 * Process page and printer state messages as needed...
3504 if (loglevel
== CUPSD_LOG_PAGE
)
3507 * Page message; send the message to the page_log file and update the
3508 * job sheet count...
3511 if (job
->sheets
!= NULL
)
3513 if (!strncasecmp(message
, "total ", 6))
3516 * Got a total count of pages from a backend or filter...
3519 copies
= atoi(message
+ 6);
3520 copies
-= job
->sheets
->values
[0].integer
; /* Just track the delta */
3522 else if (!sscanf(message
, "%*d%d", &copies
))
3525 job
->sheets
->values
[0].integer
+= copies
;
3527 if (job
->printer
->page_limit
)
3529 cupsd_quota_t
*q
= cupsdUpdateQuota(job
->printer
, job
->username
,
3533 if (AppleQuotas
&& q
->page_count
== -3)
3536 * Quota limit exceeded, cancel job in progress immediately...
3539 cupsdLogMessage(CUPSD_LOG_INFO
,
3540 "[Job %d] Canceled because pages exceed user %s "
3541 "quota limit on printer %s (%s).",
3542 job
->id
, job
->username
, job
->printer
->name
,
3543 job
->printer
->info
);
3545 cupsdCancelJob(job
, 1, IPP_JOB_CANCELED
);
3550 #endif /* __APPLE__ */
3554 cupsdLogPage(job
, message
);
3556 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
3557 "Printed %d page(s).", job
->sheets
->values
[0].integer
);
3559 else if (loglevel
== CUPSD_LOG_STATE
)
3561 if (!strcmp(message
, "paused"))
3562 cupsdStopPrinter(job
->printer
, 1);
3565 cupsdSetPrinterReasons(job
->printer
, message
);
3566 cupsdAddPrinterHistory(job
->printer
);
3567 event
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
3570 update_job_attrs(job
);
3572 else if (loglevel
== CUPSD_LOG_ATTR
)
3575 * Set attribute(s)...
3578 int num_attrs
; /* Number of attributes */
3579 cups_option_t
*attrs
; /* Attributes */
3580 const char *attr
; /* Attribute */
3583 num_attrs
= cupsParseOptions(message
, 0, &attrs
);
3585 if ((attr
= cupsGetOption("auth-info-required", num_attrs
,
3587 cupsdSetAuthInfoRequired(job
->printer
, attr
, NULL
);
3589 if ((attr
= cupsGetOption("printer-alert", num_attrs
, attrs
)) != NULL
)
3591 cupsdSetString(&job
->printer
->alert
, attr
);
3592 event
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
3595 if ((attr
= cupsGetOption("printer-alert-description", num_attrs
,
3598 cupsdSetString(&job
->printer
->alert_description
, attr
);
3599 event
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
3602 cupsFreeOptions(num_attrs
, attrs
);
3605 else if (!strncmp(message
, "recoverable:", 12))
3607 cupsdSetPrinterReasons(job
->printer
,
3608 "+com.apple.print.recoverable-warning");
3611 while (isspace(*ptr
& 255))
3614 cupsdSetString(&job
->printer
->recoverable
, ptr
);
3615 cupsdAddPrinterHistory(job
->printer
);
3616 event
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
3618 else if (!strncmp(message
, "recovered:", 10))
3620 cupsdSetPrinterReasons(job
->printer
,
3621 "-com.apple.print.recoverable-warning");
3624 while (isspace(*ptr
& 255))
3627 cupsdSetString(&job
->printer
->recoverable
, ptr
);
3628 cupsdAddPrinterHistory(job
->printer
);
3629 event
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
3631 #endif /* __APPLE__ */
3632 else if (loglevel
<= job
->status_level
)
3635 * Some message to show in the printer-state-message attribute...
3638 if (loglevel
!= CUPSD_LOG_NOTICE
)
3639 job
->status_level
= loglevel
;
3641 strlcpy(job
->printer
->state_message
, message
,
3642 sizeof(job
->printer
->state_message
));
3643 cupsdAddPrinterHistory(job
->printer
);
3644 event
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
3646 update_job_attrs(job
);
3649 if (!strchr(job
->status_buffer
->buffer
, '\n'))
3653 if ((event
& CUPSD_EVENT_PRINTER_STATE_CHANGED
))
3654 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE_CHANGED
, job
->printer
, NULL
,
3655 (job
->printer
->type
& CUPS_PRINTER_CLASS
) ?
3656 "Class \"%s\" state changed." :
3657 "Printer \"%s\" state changed.",
3658 job
->printer
->name
);
3660 if (ptr
== NULL
&& !job
->status_buffer
->bufused
)
3663 * See if all of the filters and the backend have returned their
3667 for (i
= 0; job
->filters
[i
] < 0; i
++);
3669 if (job
->filters
[i
])
3672 if (job
->current_file
>= job
->num_files
&& job
->backend
> 0)
3676 * Handle the end of job stuff...
3679 cupsdFinishJob(job
);
3685 * 'update_job_attrs()' - Update the job-printer-* attributes.
3689 update_job_attrs(cupsd_job_t
*job
) /* I - Job to update */
3691 int i
; /* Looping var */
3692 int num_reasons
; /* Actual number of reasons */
3693 const char * const *reasons
; /* Reasons */
3694 static const char *none
= "none", /* "none" */
3700 * Get/create the job-printer-state-* attributes...
3703 if (!job
->printer_message
)
3705 if ((job
->printer_message
= ippFindAttribute(job
->attrs
,
3706 "job-printer-state-message",
3707 IPP_TAG_TEXT
)) == NULL
)
3708 job
->printer_message
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_TEXT
,
3709 "job-printer-state-message",
3713 if (!job
->printer_reasons
)
3714 job
->printer_reasons
= ippFindAttribute(job
->attrs
,
3715 "job-printer-state-reasons",
3719 * If the job isn't printing, return now...
3726 * Otherwise copy the printer-state-message value...
3729 if (job
->printer
->state_message
[0])
3730 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
),
3731 job
->printer
->state_message
);
3734 * ... and the printer-state-reasons value...
3737 if (job
->printer
->num_reasons
== 0)
3740 reasons
= job
->printer
->state
== IPP_PRINTER_STOPPED
? &paused
: &none
;
3744 num_reasons
= job
->printer
->num_reasons
;
3745 reasons
= (const char * const *)job
->printer
->reasons
;
3748 if (!job
->printer_reasons
|| job
->printer_reasons
->num_values
!= num_reasons
)
3750 ippDeleteAttribute(job
->attrs
, job
->printer_reasons
);
3752 job
->printer_reasons
= ippAddStrings(job
->attrs
,
3753 IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
3754 "job-printer-state-reasons",
3755 num_reasons
, NULL
, NULL
);
3758 for (i
= 0; i
< num_reasons
; i
++)
3759 cupsdSetString(&(job
->printer_reasons
->values
[i
].string
.text
), reasons
[i
]);
3764 * End of "$Id: job.c 6689 2007-07-18 23:52:15Z mike $".