2 * "$Id: job.c 6671 2007-07-13 23:35:24Z 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
);
225 cupsdClearString(&job
->auth_username
);
226 cupsdClearString(&job
->auth_domain
);
227 cupsdClearString(&job
->auth_password
);
230 * Remove the print file for good if we aren't preserving jobs or
234 job
->current_file
= 0;
236 if (!JobHistory
|| !JobFiles
|| purge
|| (job
->dtype
& CUPS_PRINTER_REMOTE
))
238 for (i
= 1; i
<= job
->num_files
; i
++)
240 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
245 if (job
->num_files
> 0)
247 free(job
->filetypes
);
248 free(job
->compressions
);
251 job
->filetypes
= NULL
;
252 job
->compressions
= NULL
;
256 if (JobHistory
&& !purge
&& !(job
->dtype
& CUPS_PRINTER_REMOTE
))
259 * Save job state info...
267 * Remove the job info file...
270 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
,
275 * Remove the job from the "all jobs" list...
278 cupsArrayRemove(Jobs
, job
);
281 * Free all memory used...
290 * 'cupsdCancelJobs()' - Cancel all jobs for the given destination/user...
294 cupsdCancelJobs(const char *dest
, /* I - Destination to cancel */
295 const char *username
, /* I - Username or NULL */
296 int purge
) /* I - Purge jobs? */
298 cupsd_job_t
*job
; /* Current job */
301 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
303 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
304 if ((dest
== NULL
|| !strcmp(job
->dest
, dest
)) &&
305 (username
== NULL
|| !strcmp(job
->username
, username
)))
308 * Cancel all jobs matching this destination/user...
311 cupsdCancelJob(job
, purge
, IPP_JOB_CANCELED
);
319 * 'cupsdCheckJobs()' - Check the pending jobs and start any if the destination
326 cupsd_job_t
*job
; /* Current job in queue */
327 cupsd_printer_t
*printer
, /* Printer destination */
328 *pclass
; /* Printer class destination */
331 DEBUG_puts("cupsdCheckJobs()");
333 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
334 "cupsdCheckJobs: %d active jobs, sleeping=%d, reload=%d",
335 cupsArrayCount(ActiveJobs
), Sleeping
, NeedReload
);
337 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
339 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
342 * Start held jobs if they are ready...
345 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
346 "cupsdCheckJobs: Job %d: state_value=%d, loaded=%s",
347 job
->id
, job
->state_value
, job
->attrs
? "yes" : "no");
349 if (job
->state_value
== IPP_JOB_HELD
&&
351 job
->hold_until
< time(NULL
))
353 if (job
->pending_timeout
)
354 cupsdTimeoutJob(job
); /* Add trailing banner as needed */
356 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
357 job
->state_value
= IPP_JOB_PENDING
;
361 * Start pending jobs if the destination is available...
364 if (job
->state_value
== IPP_JOB_PENDING
&& !NeedReload
&& !Sleeping
)
366 printer
= cupsdFindDest(job
->dest
);
370 (printer
->type
& (CUPS_PRINTER_IMPLICIT
| CUPS_PRINTER_CLASS
)))
373 * If the class is remote, just pass it to the remote server...
378 if (pclass
->state
== IPP_PRINTER_STOPPED
)
380 else if (pclass
->type
& CUPS_PRINTER_REMOTE
)
383 printer
= cupsdFindAvailablePrinter(printer
->name
);
386 if (!printer
&& !pclass
)
389 * Whoa, the printer and/or class for this destination went away;
393 cupsdLogMessage(CUPSD_LOG_WARN
,
394 "[Job %d] Printer/class %s has gone away; canceling job!",
397 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
398 "Job canceled because the destination printer/class has "
401 cupsdCancelJob(job
, 1, IPP_JOB_ABORTED
);
406 * See if the printer is available or remote and not printing a job;
407 * if so, start the job...
413 * Add/update a job-actual-printer-uri attribute for this job
414 * so that we know which printer actually printed the job...
417 ipp_attribute_t
*attr
; /* job-actual-printer-uri attribute */
420 if ((attr
= ippFindAttribute(job
->attrs
, "job-actual-printer-uri",
421 IPP_TAG_URI
)) != NULL
)
422 cupsdSetString(&attr
->values
[0].string
.text
, printer
->uri
);
424 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
,
425 "job-actual-printer-uri", NULL
, printer
->uri
);
428 if ((!(printer
->type
& CUPS_PRINTER_DISCOVERED
) && /* Printer is local */
429 printer
->state
== IPP_PRINTER_IDLE
) || /* and idle */
430 ((printer
->type
& CUPS_PRINTER_DISCOVERED
) && /* Printer is remote */
431 !printer
->job
)) /* and not printing */
434 * Clear any message and reasons for the queue...
437 printer
->state_message
[0] = '\0';
439 cupsdSetPrinterReasons(printer
, "none");
445 start_job(job
, printer
);
454 * 'cupsdCleanJobs()' - Clean out old jobs.
460 cupsd_job_t
*job
; /* Current job */
466 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
467 job
&& cupsArrayCount(Jobs
) >= MaxJobs
;
468 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
469 if (job
->state_value
>= IPP_JOB_CANCELED
)
470 cupsdCancelJob(job
, 1, IPP_JOB_CANCELED
);
475 * 'cupsdFinishJob()' - Finish a job.
479 cupsdFinishJob(cupsd_job_t
*job
) /* I - Job */
481 cupsd_printer_t
*printer
; /* Current printer */
482 ipp_attribute_t
*attr
; /* job-hold-until attribute */
485 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] File %d is complete.",
486 job
->id
, job
->current_file
- 1);
488 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
489 "[Job %d] cupsdFinishJob: job->status is %d",
490 job
->id
, job
->status
);
492 if (job
->status_buffer
&&
493 (job
->status
< 0 || job
->current_file
>= job
->num_files
))
496 * Close the pipe and clear the input bit.
499 cupsdRemoveSelect(job
->status_buffer
->fd
);
501 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
502 "[Job %d] cupsdFinishJob: Closing status pipes [ %d %d ]...",
503 job
->id
, job
->status_pipes
[0], job
->status_pipes
[1]);
505 cupsdClosePipe(job
->status_pipes
);
506 cupsdStatBufDelete(job
->status_buffer
);
508 job
->status_buffer
= NULL
;
511 printer
= job
->printer
;
513 update_job_attrs(job
);
518 * Backend had errors; stop it...
521 int exit_code
; /* Exit code from backend */
525 * Convert the status to an exit code. Due to the way the W* macros are
526 * implemented on MacOS X (bug?), we have to store the exit status in a
527 * variable first and then convert...
530 exit_code
= -job
->status
;
531 if (WIFEXITED(exit_code
))
532 exit_code
= WEXITSTATUS(exit_code
);
534 exit_code
= job
->status
;
536 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Backend returned status %d (%s)",
538 exit_code
== CUPS_BACKEND_FAILED
? "failed" :
539 exit_code
== CUPS_BACKEND_AUTH_REQUIRED
?
540 "authentication required" :
541 exit_code
== CUPS_BACKEND_HOLD
? "hold job" :
542 exit_code
== CUPS_BACKEND_STOP
? "stop printer" :
543 exit_code
== CUPS_BACKEND_CANCEL
? "cancel job" :
544 exit_code
< 0 ? "crashed" : "unknown");
547 * Do what needs to be done...
553 case CUPS_BACKEND_FAILED
:
555 * Backend failure, use the error-policy to determine how to
559 cupsdStopJob(job
, 0);
561 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
564 * Mark the job as pending again - we'll retry on another
568 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
569 job
->state_value
= IPP_JOB_PENDING
;
575 * If the job was queued to a class, try requeuing it... For
576 * faxes and retry-job queues, hold the current job for 5 minutes.
579 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
581 else if ((printer
->type
& CUPS_PRINTER_FAX
) ||
582 !strcmp(printer
->error_policy
, "retry-job"))
585 * See how many times we've tried to send the job; if more than
586 * the limit, cancel the job.
591 if (job
->tries
>= JobRetryLimit
)
597 cupsdLogMessage(CUPSD_LOG_ERROR
,
598 "[Job %d] Canceling job since it could not be "
599 "sent after %d tries.",
600 job
->id
, JobRetryLimit
);
602 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
607 * Try again in N seconds...
610 set_hold_until(job
, time(NULL
) + JobRetryInterval
);
612 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
,
613 "Job held due to fax errors; please consult "
614 "the error_log file for details.");
615 cupsdSetPrinterState(printer
, IPP_PRINTER_IDLE
, 0);
618 else if (!strcmp(printer
->error_policy
, "abort-job"))
619 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
622 cupsdSetPrinterState(printer
, IPP_PRINTER_STOPPED
, 1);
624 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
625 "Job stopped due to backend errors; please consult "
626 "the error_log file for details.");
630 case CUPS_BACKEND_CANCEL
:
635 cupsdCancelJob(job
, 0, IPP_JOB_CANCELED
);
638 case CUPS_BACKEND_HOLD
:
643 cupsdStopJob(job
, 0);
645 cupsdSetJobHoldUntil(job
, "indefinite");
647 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
648 job
->state_value
= IPP_JOB_HELD
;
652 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
653 "Job held due to backend errors; please consult "
654 "the error_log file for details.");
657 case CUPS_BACKEND_STOP
:
659 * Stop the printer...
662 cupsdStopJob(job
, 0);
664 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
665 job
->state_value
= IPP_JOB_PENDING
;
668 cupsdSetPrinterState(printer
, IPP_PRINTER_STOPPED
, 1);
670 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
671 "Job stopped due to backend errors; please consult "
672 "the error_log file for details.");
675 case CUPS_BACKEND_AUTH_REQUIRED
:
676 cupsdStopJob(job
, 0);
678 cupsdSetJobHoldUntil(job
, "auth-info-required");
680 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
681 IPP_TAG_KEYWORD
)) == NULL
)
682 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
686 attr
->value_tag
= IPP_TAG_KEYWORD
;
687 cupsdSetString(&(attr
->values
[0].string
.text
),
688 "auth-info-required");
691 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
692 job
->state_value
= IPP_JOB_HELD
;
696 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
697 "Authentication is required for job %d.", job
->id
);
702 * Try printing another job...
707 else if (job
->status
> 0)
710 * Filter had errors; stop job...
713 cupsdStopJob(job
, 1);
715 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
716 "Job stopped due to filter errors; please consult the "
717 "error_log file for details.");
723 * Job printed successfully; cancel it...
726 if (job
->current_file
< job
->num_files
)
729 * Start the next file in the job...
732 FilterLevel
-= job
->cost
;
733 start_job(job
, printer
);
738 * Close out this job...
741 cupsdCancelJob(job
, 0, IPP_JOB_COMPLETED
);
749 * 'cupsdFreeAllJobs()' - Free all jobs from memory.
753 cupsdFreeAllJobs(void)
755 cupsd_job_t
*job
; /* Current job */
766 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
768 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
770 cupsArrayRemove(Jobs
, job
);
771 cupsArrayRemove(ActiveJobs
, job
);
776 cupsdReleaseSignals();
781 * 'cupsdFindJob()' - Find the specified job.
784 cupsd_job_t
* /* O - Job data */
785 cupsdFindJob(int id
) /* I - Job ID */
787 cupsd_job_t key
; /* Search key */
792 return ((cupsd_job_t
*)cupsArrayFind(Jobs
, &key
));
797 * 'cupsdGetPrinterJobCount()' - Get the number of pending, processing,
798 * or held jobs in a printer or class.
801 int /* O - Job count */
802 cupsdGetPrinterJobCount(
803 const char *dest
) /* I - Printer or class name */
805 int count
; /* Job count */
806 cupsd_job_t
*job
; /* Current job */
809 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
811 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
812 if (job
->dest
&& !strcasecmp(job
->dest
, dest
))
820 * 'cupsdGetUserJobCount()' - Get the number of pending, processing,
821 * or held jobs for a user.
824 int /* O - Job count */
825 cupsdGetUserJobCount(
826 const char *username
) /* I - Username */
828 int count
; /* Job count */
829 cupsd_job_t
*job
; /* Current job */
832 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
834 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
835 if (!strcasecmp(job
->username
, username
))
843 * 'cupsdHoldJob()' - Hold the specified job.
847 cupsdHoldJob(cupsd_job_t
*job
) /* I - Job data */
849 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdHoldJob: id = %d", job
->id
);
851 if (job
->state_value
== IPP_JOB_PROCESSING
)
852 cupsdStopJob(job
, 0);
856 DEBUG_puts("cupsdHoldJob: setting state to held...");
858 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
859 job
->state_value
= IPP_JOB_HELD
;
868 * 'cupsdLoadAllJobs()' - Load all jobs from disk.
872 cupsdLoadAllJobs(void)
874 char filename
[1024]; /* Full filename of job.cache file */
875 struct stat fileinfo
, /* Information on job.cache file */
876 dirinfo
; /* Information on RequestRoot dir */
881 * Create the job arrays as needed...
885 Jobs
= cupsArrayNew(compare_jobs
, NULL
);
888 ActiveJobs
= cupsArrayNew(compare_active_jobs
, NULL
);
891 * See whether the job.cache file is older than the RequestRoot directory...
894 snprintf(filename
, sizeof(filename
), "%s/job.cache", CacheDir
);
896 if (stat(filename
, &fileinfo
))
898 fileinfo
.st_mtime
= 0;
901 cupsdLogMessage(CUPSD_LOG_ERROR
,
902 "Unable to get file information for \"%s\" - %s",
903 filename
, strerror(errno
));
906 if (stat(RequestRoot
, &dirinfo
))
908 dirinfo
.st_mtime
= 0;
911 cupsdLogMessage(CUPSD_LOG_ERROR
,
912 "Unable to get directory information for \"%s\" - %s",
913 RequestRoot
, strerror(errno
));
917 * Load the most recent source for job data...
920 if (dirinfo
.st_mtime
> fileinfo
.st_mtime
)
924 load_next_job_id(filename
);
927 load_job_cache(filename
);
930 * Clean out old jobs as needed...
933 if (MaxJobs
> 0 && cupsArrayCount(Jobs
) >= MaxJobs
)
939 * 'cupsdLoadJob()' - Load a single job...
943 cupsdLoadJob(cupsd_job_t
*job
) /* I - Job */
945 char jobfile
[1024]; /* Job filename */
946 cups_file_t
*fp
; /* Job file */
947 int fileid
; /* Current file ID */
948 ipp_attribute_t
*attr
; /* Job attribute */
949 const char *dest
; /* Destination name */
950 cupsd_printer_t
*destptr
; /* Pointer to destination */
951 mime_type_t
**filetypes
; /* New filetypes array */
952 int *compressions
; /* New compressions array */
957 if (job
->state_value
> IPP_JOB_STOPPED
)
958 job
->access_time
= time(NULL
);
963 if ((job
->attrs
= ippNew()) == NULL
)
965 cupsdLogMessage(CUPSD_LOG_ERROR
,
966 "[Job %d] Ran out of memory for job attributes!", job
->id
);
971 * Load job attributes...
974 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Loading attributes...", job
->id
);
976 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, job
->id
);
977 if ((fp
= cupsFileOpen(jobfile
, "r")) == NULL
)
979 cupsdLogMessage(CUPSD_LOG_ERROR
,
980 "[Job %d] Unable to open job control file \"%s\" - %s!",
981 job
->id
, jobfile
, strerror(errno
));
982 ippDelete(job
->attrs
);
987 if (ippReadIO(fp
, (ipp_iocb_t
)cupsFileRead
, 1, NULL
, job
->attrs
) != IPP_DATA
)
989 cupsdLogMessage(CUPSD_LOG_ERROR
,
990 "[Job %d] Unable to read job control file \"%s\"!",
993 ippDelete(job
->attrs
);
1002 * Copy attribute data to the job object...
1005 if ((job
->state
= ippFindAttribute(job
->attrs
, "job-state",
1006 IPP_TAG_ENUM
)) == NULL
)
1008 cupsdLogMessage(CUPSD_LOG_ERROR
,
1009 "[Job %d] Missing or bad job-state attribute in "
1012 ippDelete(job
->attrs
);
1018 job
->state_value
= (ipp_jstate_t
)job
->state
->values
[0].integer
;
1022 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1023 IPP_TAG_URI
)) == NULL
)
1025 cupsdLogMessage(CUPSD_LOG_ERROR
,
1026 "[Job %d] No job-printer-uri attribute in control file!",
1028 ippDelete(job
->attrs
);
1034 if ((dest
= cupsdValidateDest(attr
->values
[0].string
.text
, &(job
->dtype
),
1037 cupsdLogMessage(CUPSD_LOG_ERROR
,
1038 "[Job %d] Unable to queue job for destination \"%s\"!",
1039 job
->id
, attr
->values
[0].string
.text
);
1040 ippDelete(job
->attrs
);
1046 cupsdSetString(&job
->dest
, dest
);
1048 else if ((destptr
= cupsdFindDest(job
->dest
)) == NULL
)
1050 cupsdLogMessage(CUPSD_LOG_ERROR
,
1051 "[Job %d] Unable to queue job for destination \"%s\"!",
1052 job
->id
, job
->dest
);
1053 ippDelete(job
->attrs
);
1059 job
->sheets
= ippFindAttribute(job
->attrs
, "job-media-sheets-completed",
1061 job
->job_sheets
= ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_NAME
);
1065 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
1066 IPP_TAG_INTEGER
)) == NULL
)
1068 cupsdLogMessage(CUPSD_LOG_ERROR
,
1069 "[Job %d] Missing or bad job-priority attribute in "
1070 "control file!", job
->id
);
1071 ippDelete(job
->attrs
);
1077 job
->priority
= attr
->values
[0].integer
;
1082 if ((attr
= ippFindAttribute(job
->attrs
, "job-originating-user-name",
1083 IPP_TAG_NAME
)) == NULL
)
1085 cupsdLogMessage(CUPSD_LOG_ERROR
,
1086 "[Job %d] Missing or bad job-originating-user-name "
1087 "attribute in control file!", job
->id
);
1088 ippDelete(job
->attrs
);
1094 cupsdSetString(&job
->username
, attr
->values
[0].string
.text
);
1098 * Set the job hold-until time and state...
1101 if (job
->state_value
== IPP_JOB_HELD
)
1103 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1104 IPP_TAG_KEYWORD
)) == NULL
)
1105 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1108 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
1111 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1112 job
->state_value
= IPP_JOB_PENDING
;
1115 else if (job
->state_value
== IPP_JOB_PROCESSING
)
1117 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1118 job
->state_value
= IPP_JOB_PENDING
;
1121 if (!job
->num_files
)
1124 * Find all the d##### files...
1127 for (fileid
= 1; fileid
< 10000; fileid
++)
1129 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
1132 if (access(jobfile
, 0))
1135 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1136 "[Job %d] Auto-typing document file \"%s\"...",
1139 if (fileid
> job
->num_files
)
1141 if (job
->num_files
== 0)
1143 compressions
= (int *)calloc(fileid
, sizeof(int));
1144 filetypes
= (mime_type_t
**)calloc(fileid
, sizeof(mime_type_t
*));
1148 compressions
= (int *)realloc(job
->compressions
,
1149 sizeof(int) * fileid
);
1150 filetypes
= (mime_type_t
**)realloc(job
->filetypes
,
1151 sizeof(mime_type_t
*) *
1155 if (!compressions
|| !filetypes
)
1157 cupsdLogMessage(CUPSD_LOG_ERROR
,
1158 "[Job %d] Ran out of memory for job file types!",
1163 job
->compressions
= compressions
;
1164 job
->filetypes
= filetypes
;
1165 job
->num_files
= fileid
;
1168 job
->filetypes
[fileid
- 1] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
1169 job
->compressions
+ fileid
- 1);
1171 if (!job
->filetypes
[fileid
- 1])
1172 job
->filetypes
[fileid
- 1] = mimeType(MimeDatabase
, "application",
1178 * Load authentication information as needed...
1181 if (job
->state_value
< IPP_JOB_STOPPED
)
1183 snprintf(jobfile
, sizeof(jobfile
), "%s/a%05d", RequestRoot
, job
->id
);
1185 cupsdClearString(&job
->auth_username
);
1186 cupsdClearString(&job
->auth_domain
);
1187 cupsdClearString(&job
->auth_password
);
1189 if ((fp
= cupsFileOpen(jobfile
, "r")) != NULL
)
1191 int i
, /* Looping var */
1192 bytes
; /* Size of auth data */
1193 char line
[255], /* Line from file */
1194 data
[255]; /* Decoded data */
1198 i
< destptr
->num_auth_info_required
&&
1199 cupsFileGets(fp
, line
, sizeof(line
));
1202 bytes
= sizeof(data
);
1203 httpDecode64_2(data
, &bytes
, line
);
1205 if (!strcmp(destptr
->auth_info_required
[i
], "username"))
1206 cupsdSetStringf(&job
->auth_username
, "AUTH_USERNAME=%s", data
);
1207 else if (!strcmp(destptr
->auth_info_required
[i
], "domain"))
1208 cupsdSetStringf(&job
->auth_domain
, "AUTH_DOMAIN=%s", data
);
1209 else if (!strcmp(destptr
->auth_info_required
[i
], "password"))
1210 cupsdSetStringf(&job
->auth_password
, "AUTH_PASSWORD=%s", data
);
1216 job
->access_time
= time(NULL
);
1221 * 'cupsdMoveJob()' - Move the specified job to a different destination.
1225 cupsdMoveJob(cupsd_job_t
*job
, /* I - Job */
1226 cupsd_printer_t
*p
) /* I - Destination printer or class */
1228 ipp_attribute_t
*attr
; /* job-printer-uri attribute */
1229 const char *olddest
; /* Old destination */
1230 cupsd_printer_t
*oldp
; /* Old pointer */
1234 * Don't move completed jobs...
1237 if (job
->state_value
> IPP_JOB_STOPPED
)
1241 * Get the old destination...
1244 olddest
= job
->dest
;
1247 oldp
= job
->printer
;
1249 oldp
= cupsdFindDest(olddest
);
1252 * Change the destination information...
1257 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, oldp
, job
,
1258 "Job #%d moved from %s to %s.", job
->id
, olddest
,
1261 cupsdSetString(&job
->dest
, p
->name
);
1262 job
->dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_REMOTE
|
1263 CUPS_PRINTER_IMPLICIT
);
1265 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1266 IPP_TAG_URI
)) != NULL
)
1267 cupsdSetString(&(attr
->values
[0].string
.text
), p
->uri
);
1269 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, p
, job
,
1270 "Job #%d moved from %s to %s.", job
->id
, olddest
,
1278 * 'cupsdReleaseJob()' - Release the specified job.
1282 cupsdReleaseJob(cupsd_job_t
*job
) /* I - Job */
1284 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdReleaseJob: id = %d", job
->id
);
1286 if (job
->state_value
== IPP_JOB_HELD
)
1288 DEBUG_puts("cupsdReleaseJob: setting state to pending...");
1290 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1291 job
->state_value
= IPP_JOB_PENDING
;
1299 * 'cupsdRestartJob()' - Restart the specified job.
1303 cupsdRestartJob(cupsd_job_t
*job
) /* I - Job */
1305 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdRestartJob: id = %d", job
->id
);
1307 if (job
->state_value
== IPP_JOB_STOPPED
|| job
->num_files
)
1309 ipp_jstate_t old_state
; /* Old job state */
1314 old_state
= job
->state_value
;
1317 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1318 job
->state_value
= IPP_JOB_PENDING
;
1322 if (old_state
> IPP_JOB_STOPPED
)
1323 cupsArrayAdd(ActiveJobs
, job
);
1331 * 'cupsdSaveAllJobs()' - Save a summary of all jobs to disk.
1335 cupsdSaveAllJobs(void)
1337 int i
; /* Looping var */
1338 cups_file_t
*fp
; /* Job cache file */
1339 char temp
[1024]; /* Temporary string */
1340 cupsd_job_t
*job
; /* Current job */
1341 time_t curtime
; /* Current time */
1342 struct tm
*curdate
; /* Current date */
1345 snprintf(temp
, sizeof(temp
), "%s/job.cache", CacheDir
);
1346 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
1348 cupsdLogMessage(CUPSD_LOG_ERROR
,
1349 "Unable to create job cache file \"%s\" - %s",
1350 temp
, strerror(errno
));
1354 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving job cache file \"%s\"...", temp
);
1357 * Restrict access to the file...
1360 fchown(cupsFileNumber(fp
), getuid(), Group
);
1361 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
1364 * Write a small header to the file...
1367 curtime
= time(NULL
);
1368 curdate
= localtime(&curtime
);
1369 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
1371 cupsFilePuts(fp
, "# Job cache file for " CUPS_SVERSION
"\n");
1372 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
1373 cupsFilePrintf(fp
, "NextJobId %d\n", NextJobId
);
1376 * Write each job known to the system...
1379 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1381 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1383 cupsFilePrintf(fp
, "<Job %d>\n", job
->id
);
1384 cupsFilePrintf(fp
, "State %d\n", job
->state_value
);
1385 cupsFilePrintf(fp
, "Priority %d\n", job
->priority
);
1386 cupsFilePrintf(fp
, "Username %s\n", job
->username
);
1387 cupsFilePrintf(fp
, "Destination %s\n", job
->dest
);
1388 cupsFilePrintf(fp
, "DestType %d\n", job
->dtype
);
1389 cupsFilePrintf(fp
, "NumFiles %d\n", job
->num_files
);
1390 for (i
= 0; i
< job
->num_files
; i
++)
1391 cupsFilePrintf(fp
, "File %d %s/%s %d\n", i
+ 1, job
->filetypes
[i
]->super
,
1392 job
->filetypes
[i
]->type
, job
->compressions
[i
]);
1393 cupsFilePuts(fp
, "</Job>\n");
1401 * 'cupsdSaveJob()' - Save a job to disk.
1405 cupsdSaveJob(cupsd_job_t
*job
) /* I - Job */
1407 char filename
[1024]; /* Job control filename */
1408 cups_file_t
*fp
; /* Job file */
1411 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSaveJob(job=%p(%d)): job->attrs=%p",
1412 job
, job
->id
, job
->attrs
);
1414 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
, job
->id
);
1416 if ((fp
= cupsFileOpen(filename
, "w")) == NULL
)
1418 cupsdLogMessage(CUPSD_LOG_ERROR
,
1419 "[Job %d] Unable to create job control file \"%s\" - %s.",
1420 job
->id
, filename
, strerror(errno
));
1424 fchmod(cupsFileNumber(fp
), 0600);
1425 fchown(cupsFileNumber(fp
), RunUser
, Group
);
1427 job
->attrs
->state
= IPP_IDLE
;
1429 if (ippWriteIO(fp
, (ipp_iocb_t
)cupsFileWrite
, 1, NULL
,
1430 job
->attrs
) != IPP_DATA
)
1431 cupsdLogMessage(CUPSD_LOG_ERROR
,
1432 "[Job %d] Unable to write job control file!", job
->id
);
1439 * 'cupsdSetJobHoldUntil()' - Set the hold time for a job...
1443 cupsdSetJobHoldUntil(cupsd_job_t
*job
, /* I - Job */
1444 const char *when
) /* I - When to resume */
1446 time_t curtime
; /* Current time */
1447 struct tm
*curdate
; /* Current date */
1448 int hour
; /* Hold hour */
1449 int minute
; /* Hold minute */
1450 int second
; /* Hold second */
1453 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetJobHoldUntil(%d, \"%s\")",
1458 if (!strcmp(when
, "indefinite") || !strcmp(when
, "auth-info-required"))
1461 * Hold indefinitely...
1464 job
->hold_until
= 0;
1466 else if (!strcmp(when
, "day-time"))
1469 * Hold to 6am the next morning unless local time is < 6pm.
1472 curtime
= time(NULL
);
1473 curdate
= localtime(&curtime
);
1475 if (curdate
->tm_hour
< 18)
1476 job
->hold_until
= curtime
;
1478 job
->hold_until
= curtime
+
1479 ((29 - curdate
->tm_hour
) * 60 + 59 -
1480 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1482 else if (!strcmp(when
, "evening") || !strcmp(when
, "night"))
1485 * Hold to 6pm unless local time is > 6pm or < 6am.
1488 curtime
= time(NULL
);
1489 curdate
= localtime(&curtime
);
1491 if (curdate
->tm_hour
< 6 || curdate
->tm_hour
>= 18)
1492 job
->hold_until
= curtime
;
1494 job
->hold_until
= curtime
+
1495 ((17 - curdate
->tm_hour
) * 60 + 59 -
1496 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1498 else if (!strcmp(when
, "second-shift"))
1501 * Hold to 4pm unless local time is > 4pm.
1504 curtime
= time(NULL
);
1505 curdate
= localtime(&curtime
);
1507 if (curdate
->tm_hour
>= 16)
1508 job
->hold_until
= curtime
;
1510 job
->hold_until
= curtime
+
1511 ((15 - curdate
->tm_hour
) * 60 + 59 -
1512 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1514 else if (!strcmp(when
, "third-shift"))
1517 * Hold to 12am unless local time is < 8am.
1520 curtime
= time(NULL
);
1521 curdate
= localtime(&curtime
);
1523 if (curdate
->tm_hour
< 8)
1524 job
->hold_until
= curtime
;
1526 job
->hold_until
= curtime
+
1527 ((23 - curdate
->tm_hour
) * 60 + 59 -
1528 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1530 else if (!strcmp(when
, "weekend"))
1533 * Hold to weekend unless we are in the weekend.
1536 curtime
= time(NULL
);
1537 curdate
= localtime(&curtime
);
1539 if (curdate
->tm_wday
|| curdate
->tm_wday
== 6)
1540 job
->hold_until
= curtime
;
1542 job
->hold_until
= curtime
+
1543 (((5 - curdate
->tm_wday
) * 24 +
1544 (17 - curdate
->tm_hour
)) * 60 + 59 -
1545 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1547 else if (sscanf(when
, "%d:%d:%d", &hour
, &minute
, &second
) >= 2)
1550 * Hold to specified GMT time (HH:MM or HH:MM:SS)...
1553 curtime
= time(NULL
);
1554 curdate
= gmtime(&curtime
);
1556 job
->hold_until
= curtime
+
1557 ((hour
- curdate
->tm_hour
) * 60 + minute
-
1558 curdate
->tm_min
) * 60 + second
- curdate
->tm_sec
;
1561 * Hold until next day as needed...
1564 if (job
->hold_until
< curtime
)
1565 job
->hold_until
+= 24 * 60 * 60;
1568 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetJobHoldUntil: hold_until = %d",
1569 (int)job
->hold_until
);
1574 * 'cupsdSetJobPriority()' - Set the priority of a job, moving it up/down in
1575 * the list as needed.
1579 cupsdSetJobPriority(
1580 cupsd_job_t
*job
, /* I - Job ID */
1581 int priority
) /* I - New priority (0 to 100) */
1583 ipp_attribute_t
*attr
; /* Job attribute */
1587 * Don't change completed jobs...
1590 if (job
->state_value
>= IPP_JOB_PROCESSING
)
1594 * Set the new priority and re-add the job into the active list...
1597 cupsArrayRemove(ActiveJobs
, job
);
1599 job
->priority
= priority
;
1601 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
1602 IPP_TAG_INTEGER
)) != NULL
)
1603 attr
->values
[0].integer
= priority
;
1605 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-priority",
1608 cupsArrayAdd(ActiveJobs
, job
);
1615 * 'cupsdStopAllJobs()' - Stop all print jobs.
1619 cupsdStopAllJobs(int force
) /* I - 1 = Force all filters to stop */
1621 cupsd_job_t
*job
; /* Current job */
1624 DEBUG_puts("cupsdStopAllJobs()");
1626 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
1628 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
1629 if (job
->state_value
== IPP_JOB_PROCESSING
)
1631 cupsdStopJob(job
, force
);
1632 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1633 job
->state_value
= IPP_JOB_PENDING
;
1639 * 'cupsdStopJob()' - Stop a print job.
1643 cupsdStopJob(cupsd_job_t
*job
, /* I - Job */
1644 int force
) /* I - 1 = Force all filters to stop */
1646 int i
; /* Looping var */
1649 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1650 "[Job %d] cupsdStopJob: force = %d", job
->id
, force
);
1652 if (job
->state_value
!= IPP_JOB_PROCESSING
)
1655 FilterLevel
-= job
->cost
;
1657 if (job
->printer
->state
== IPP_PRINTER_PROCESSING
)
1658 cupsdSetPrinterState(job
->printer
, IPP_PRINTER_IDLE
, 0);
1660 job
->state
->values
[0].integer
= IPP_JOB_STOPPED
;
1661 job
->state_value
= IPP_JOB_STOPPED
;
1662 job
->printer
->job
= NULL
;
1663 job
->printer
= NULL
;
1665 job
->current_file
--;
1667 for (i
= 0; job
->filters
[i
]; i
++)
1668 if (job
->filters
[i
] > 0)
1670 cupsdEndProcess(job
->filters
[i
], force
);
1671 job
->filters
[i
] = 0;
1674 if (job
->backend
> 0)
1676 cupsdEndProcess(job
->backend
, force
);
1680 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] Closing print pipes [ %d %d ]...",
1681 job
->id
, job
->print_pipes
[0], job
->print_pipes
[1]);
1683 cupsdClosePipe(job
->print_pipes
);
1685 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] Closing back pipes [ %d %d ]...",
1686 job
->id
, job
->back_pipes
[0], job
->back_pipes
[1]);
1688 cupsdClosePipe(job
->back_pipes
);
1690 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] Closing side pipes [ %d %d ]...",
1691 job
->id
, job
->side_pipes
[0], job
->side_pipes
[1]);
1693 cupsdClosePipe(job
->side_pipes
);
1695 if (job
->status_buffer
)
1698 * Close the pipe and clear the input bit.
1701 cupsdRemoveSelect(job
->status_buffer
->fd
);
1703 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1704 "[Job %d] Closing status pipes [ %d %d ]...", job
->id
,
1705 job
->status_pipes
[0], job
->status_pipes
[1]);
1707 cupsdClosePipe(job
->status_pipes
);
1708 cupsdStatBufDelete(job
->status_buffer
);
1710 job
->status_buffer
= NULL
;
1716 * 'cupsdUnloadCompletedJobs()' - Flush completed job history from memory.
1720 cupsdUnloadCompletedJobs(void)
1722 cupsd_job_t
*job
; /* Current job */
1723 time_t expire
; /* Expiration time */
1726 expire
= time(NULL
) - 60;
1728 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1730 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1731 if (job
->attrs
&& job
->state_value
>= IPP_JOB_STOPPED
&&
1732 job
->access_time
< expire
)
1738 * 'compare_active_jobs()' - Compare the job IDs and priorities of two jobs.
1741 static int /* O - Difference */
1742 compare_active_jobs(void *first
, /* I - First job */
1743 void *second
, /* I - Second job */
1744 void *data
) /* I - App data (not used) */
1746 int diff
; /* Difference */
1749 if ((diff
= ((cupsd_job_t
*)second
)->priority
-
1750 ((cupsd_job_t
*)first
)->priority
) != 0)
1753 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
1758 * 'compare_jobs()' - Compare the job IDs of two jobs.
1761 static int /* O - Difference */
1762 compare_jobs(void *first
, /* I - First job */
1763 void *second
, /* I - Second job */
1764 void *data
) /* I - App data (not used) */
1766 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
1771 * 'free_job()' - Free all memory used by a job.
1775 free_job(cupsd_job_t
*job
) /* I - Job */
1777 cupsdClearString(&job
->username
);
1778 cupsdClearString(&job
->dest
);
1779 cupsdClearString(&job
->auth_username
);
1780 cupsdClearString(&job
->auth_domain
);
1781 cupsdClearString(&job
->auth_password
);
1783 cupsdClearString(&job
->ccname
);
1784 #endif /* HAVE_GSSAPI */
1786 if (job
->num_files
> 0)
1788 free(job
->compressions
);
1789 free(job
->filetypes
);
1792 ippDelete(job
->attrs
);
1799 * 'ipp_length()' - Compute the size of the buffer needed to hold
1800 * the textual IPP attributes.
1803 static int /* O - Size of attribute buffer */
1804 ipp_length(ipp_t
*ipp
) /* I - IPP request */
1806 int bytes
; /* Number of bytes */
1807 int i
; /* Looping var */
1808 ipp_attribute_t
*attr
; /* Current attribute */
1812 * Loop through all attributes...
1817 for (attr
= ipp
->attrs
; attr
!= NULL
; attr
= attr
->next
)
1820 * Skip attributes that won't be sent to filters...
1823 if (attr
->value_tag
== IPP_TAG_MIMETYPE
||
1824 attr
->value_tag
== IPP_TAG_NAMELANG
||
1825 attr
->value_tag
== IPP_TAG_TEXTLANG
||
1826 attr
->value_tag
== IPP_TAG_URI
||
1827 attr
->value_tag
== IPP_TAG_URISCHEME
)
1830 if (strncmp(attr
->name
, "time-", 5) == 0)
1834 * Add space for a leading space and commas between each value.
1835 * For the first attribute, the leading space isn't used, so the
1836 * extra byte can be used as the nul terminator...
1839 bytes
++; /* " " separator */
1840 bytes
+= attr
->num_values
; /* "," separators */
1843 * Boolean attributes appear as "foo,nofoo,foo,nofoo", while
1844 * other attributes appear as "foo=value1,value2,...,valueN".
1847 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
1848 bytes
+= strlen(attr
->name
);
1850 bytes
+= attr
->num_values
* strlen(attr
->name
);
1853 * Now add the size required for each value in the attribute...
1856 switch (attr
->value_tag
)
1858 case IPP_TAG_INTEGER
:
1861 * Minimum value of a signed integer is -2147483647, or 11 digits.
1864 bytes
+= attr
->num_values
* 11;
1867 case IPP_TAG_BOOLEAN
:
1869 * Add two bytes for each false ("no") value...
1872 for (i
= 0; i
< attr
->num_values
; i
++)
1873 if (!attr
->values
[i
].boolean
)
1877 case IPP_TAG_RANGE
:
1879 * A range is two signed integers separated by a hyphen, or
1880 * 23 characters max.
1883 bytes
+= attr
->num_values
* 23;
1886 case IPP_TAG_RESOLUTION
:
1888 * A resolution is two signed integers separated by an "x" and
1889 * suffixed by the units, or 26 characters max.
1892 bytes
+= attr
->num_values
* 26;
1895 case IPP_TAG_STRING
:
1898 case IPP_TAG_KEYWORD
:
1899 case IPP_TAG_CHARSET
:
1900 case IPP_TAG_LANGUAGE
:
1903 * Strings can contain characters that need quoting. We need
1904 * at least 2 * len + 2 characters to cover the quotes and
1905 * any backslashes in the string.
1908 for (i
= 0; i
< attr
->num_values
; i
++)
1909 bytes
+= 2 * strlen(attr
->values
[i
].string
.text
) + 2;
1913 break; /* anti-compiler-warning-code */
1922 * 'load_job_cache()' - Load jobs from the job.cache file.
1926 load_job_cache(const char *filename
) /* I - job.cache filename */
1928 cups_file_t
*fp
; /* job.cache file */
1929 char line
[1024], /* Line buffer */
1930 *value
; /* Value on line */
1931 int linenum
; /* Line number in file */
1932 cupsd_job_t
*job
; /* Current job */
1933 int jobid
; /* Job ID */
1934 char jobfile
[1024]; /* Job filename */
1938 * Open the job.cache file...
1941 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
1943 if (errno
!= ENOENT
)
1944 cupsdLogMessage(CUPSD_LOG_ERROR
,
1945 "Unable to open job cache file \"%s\": %s",
1946 filename
, strerror(errno
));
1948 load_request_root();
1954 * Read entries from the job cache file and create jobs as needed.
1957 cupsdLogMessage(CUPSD_LOG_INFO
, "Loading job cache file \"%s\"...",
1963 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
1965 if (!strcasecmp(line
, "NextJobId"))
1968 NextJobId
= atoi(value
);
1970 else if (!strcasecmp(line
, "<Job"))
1974 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing </Job> directive on line %d!",
1981 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing job ID on line %d!", linenum
);
1985 jobid
= atoi(value
);
1989 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad job ID %d on line %d!", jobid
,
1994 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, jobid
);
1995 if (access(jobfile
, 0))
1997 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Job %d] Files have gone away!",
2002 job
= calloc(1, sizeof(cupsd_job_t
));
2005 cupsdLogMessage(CUPSD_LOG_EMERG
,
2006 "[Job %d] Unable to allocate memory for job!", jobid
);
2011 job
->back_pipes
[0] = -1;
2012 job
->back_pipes
[1] = -1;
2013 job
->print_pipes
[0] = -1;
2014 job
->print_pipes
[1] = -1;
2015 job
->side_pipes
[0] = -1;
2016 job
->side_pipes
[1] = -1;
2017 job
->status_pipes
[0] = -1;
2018 job
->status_pipes
[1] = -1;
2020 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Loading from cache...", job
->id
);
2024 cupsdLogMessage(CUPSD_LOG_ERROR
,
2025 "Missing <Job #> directive on line %d!", linenum
);
2028 else if (!strcasecmp(line
, "</Job>"))
2030 cupsArrayAdd(Jobs
, job
);
2032 if (job
->state_value
<= IPP_JOB_STOPPED
)
2034 cupsArrayAdd(ActiveJobs
, job
);
2042 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing value on line %d!", linenum
);
2045 else if (!strcasecmp(line
, "State"))
2047 job
->state_value
= (ipp_jstate_t
)atoi(value
);
2049 if (job
->state_value
< IPP_JOB_PENDING
)
2050 job
->state_value
= IPP_JOB_PENDING
;
2051 else if (job
->state_value
> IPP_JOB_COMPLETED
)
2052 job
->state_value
= IPP_JOB_COMPLETED
;
2054 else if (!strcasecmp(line
, "Priority"))
2056 job
->priority
= atoi(value
);
2058 else if (!strcasecmp(line
, "Username"))
2060 cupsdSetString(&job
->username
, value
);
2062 else if (!strcasecmp(line
, "Destination"))
2064 cupsdSetString(&job
->dest
, value
);
2066 else if (!strcasecmp(line
, "DestType"))
2068 job
->dtype
= (cups_ptype_t
)atoi(value
);
2070 else if (!strcasecmp(line
, "NumFiles"))
2072 job
->num_files
= atoi(value
);
2074 if (job
->num_files
< 0)
2076 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad NumFiles value %d on line %d!",
2077 job
->num_files
, linenum
);
2082 if (job
->num_files
> 0)
2084 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-001", RequestRoot
,
2086 if (access(jobfile
, 0))
2088 cupsdLogMessage(CUPSD_LOG_INFO
,
2089 "[Job %d] Data files have gone away!", job
->id
);
2094 job
->filetypes
= calloc(job
->num_files
, sizeof(mime_type_t
*));
2095 job
->compressions
= calloc(job
->num_files
, sizeof(int));
2097 if (!job
->filetypes
|| !job
->compressions
)
2099 cupsdLogMessage(CUPSD_LOG_EMERG
,
2100 "[Job %d] Unable to allocate memory for %d files!",
2101 job
->id
, job
->num_files
);
2106 else if (!strcasecmp(line
, "File"))
2108 int number
, /* File number */
2109 compression
; /* Compression value */
2110 char super
[MIME_MAX_SUPER
], /* MIME super type */
2111 type
[MIME_MAX_TYPE
]; /* MIME type */
2114 if (sscanf(value
, "%d%*[ \t]%15[^/]/%255s%d", &number
, super
, type
,
2117 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File on line %d!", linenum
);
2121 if (number
< 1 || number
> job
->num_files
)
2123 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File number %d on line %d!",
2130 job
->compressions
[number
] = compression
;
2131 job
->filetypes
[number
] = mimeType(MimeDatabase
, super
, type
);
2133 if (!job
->filetypes
[number
])
2136 * If the original MIME type is unknown, auto-type it!
2139 cupsdLogMessage(CUPSD_LOG_ERROR
,
2140 "[Job %d] Unknown MIME type %s/%s for file %d!",
2141 job
->id
, super
, type
, number
+ 1);
2143 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
2144 job
->id
, number
+ 1);
2145 job
->filetypes
[number
] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
2146 job
->compressions
+ number
);
2149 * If that didn't work, assume it is raw...
2152 if (!job
->filetypes
[number
])
2153 job
->filetypes
[number
] = mimeType(MimeDatabase
, "application",
2158 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unknown %s directive on line %d!",
2167 * 'load_next_job_id()' - Load the NextJobId value from the job.cache file.
2171 load_next_job_id(const char *filename
) /* I - job.cache filename */
2173 cups_file_t
*fp
; /* job.cache file */
2174 char line
[1024], /* Line buffer */
2175 *value
; /* Value on line */
2176 int linenum
; /* Line number in file */
2177 int next_job_id
; /* NextJobId value from line */
2181 * Read the NextJobId directive from the job.cache file and use
2182 * the value (if any).
2185 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
2187 if (errno
!= ENOENT
)
2188 cupsdLogMessage(CUPSD_LOG_ERROR
,
2189 "Unable to open job cache file \"%s\": %s",
2190 filename
, strerror(errno
));
2195 cupsdLogMessage(CUPSD_LOG_INFO
,
2196 "Loading NextJobId from job cache file \"%s\"...", filename
);
2200 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
2202 if (!strcasecmp(line
, "NextJobId"))
2206 next_job_id
= atoi(value
);
2208 if (next_job_id
> NextJobId
)
2209 NextJobId
= next_job_id
;
2220 * 'load_request_root()' - Load jobs from the RequestRoot directory.
2224 load_request_root(void)
2226 cups_dir_t
*dir
; /* Directory */
2227 cups_dentry_t
*dent
; /* Directory entry */
2228 cupsd_job_t
*job
; /* New job */
2232 * Open the requests directory...
2235 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Scanning %s for jobs...", RequestRoot
);
2237 if ((dir
= cupsDirOpen(RequestRoot
)) == NULL
)
2239 cupsdLogMessage(CUPSD_LOG_ERROR
,
2240 "Unable to open spool directory \"%s\": %s",
2241 RequestRoot
, strerror(errno
));
2246 * Read all the c##### files...
2249 while ((dent
= cupsDirRead(dir
)) != NULL
)
2250 if (strlen(dent
->filename
) >= 6 && dent
->filename
[0] == 'c')
2253 * Allocate memory for the job...
2256 if ((job
= calloc(sizeof(cupsd_job_t
), 1)) == NULL
)
2258 cupsdLogMessage(CUPSD_LOG_ERROR
, "Ran out of memory for jobs!");
2264 * Assign the job ID...
2267 job
->id
= atoi(dent
->filename
+ 1);
2268 job
->back_pipes
[0] = -1;
2269 job
->back_pipes
[1] = -1;
2270 job
->print_pipes
[0] = -1;
2271 job
->print_pipes
[1] = -1;
2272 job
->side_pipes
[0] = -1;
2273 job
->side_pipes
[1] = -1;
2274 job
->status_pipes
[0] = -1;
2275 job
->status_pipes
[1] = -1;
2277 if (job
->id
>= NextJobId
)
2278 NextJobId
= job
->id
+ 1;
2287 * Insert the job into the array, sorting by job priority and ID...
2290 cupsArrayAdd(Jobs
, job
);
2292 if (job
->state_value
<= IPP_JOB_STOPPED
)
2293 cupsArrayAdd(ActiveJobs
, job
);
2303 * 'set_time()' - Set one of the "time-at-xyz" attributes...
2307 set_time(cupsd_job_t
*job
, /* I - Job to update */
2308 const char *name
) /* I - Name of attribute */
2310 ipp_attribute_t
*attr
; /* Time attribute */
2313 if ((attr
= ippFindAttribute(job
->attrs
, name
, IPP_TAG_ZERO
)) != NULL
)
2315 attr
->value_tag
= IPP_TAG_INTEGER
;
2316 attr
->values
[0].integer
= time(NULL
);
2322 * 'set_hold_until()' - Set the hold time and update job-hold-until attribute...
2326 set_hold_until(cupsd_job_t
*job
, /* I - Job to update */
2327 time_t holdtime
) /* I - Hold until time */
2329 ipp_attribute_t
*attr
; /* job-hold-until attribute */
2330 struct tm
*holddate
; /* Hold date */
2331 char holdstr
[64]; /* Hold time */
2335 * Set the hold_until value and hold the job...
2338 cupsdLogMessage(CUPSD_LOG_DEBUG
, "set_hold_until: hold_until = %d",
2341 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
2342 job
->state_value
= IPP_JOB_HELD
;
2343 job
->hold_until
= holdtime
;
2346 * Update the job-hold-until attribute with a string representing GMT
2347 * time (HH:MM:SS)...
2350 holddate
= gmtime(&holdtime
);
2351 snprintf(holdstr
, sizeof(holdstr
), "%d:%d:%d", holddate
->tm_hour
,
2352 holddate
->tm_min
, holddate
->tm_sec
);
2354 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2355 IPP_TAG_KEYWORD
)) == NULL
)
2356 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2359 * Either add the attribute or update the value of the existing one
2363 attr
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
2364 "job-hold-until", NULL
, holdstr
);
2366 cupsdSetString(&attr
->values
[0].string
.text
, holdstr
);
2373 * 'start_job()' - Start a print job.
2377 start_job(cupsd_job_t
*job
, /* I - Job ID */
2378 cupsd_printer_t
*printer
) /* I - Printer to print job */
2380 int i
; /* Looping var */
2381 int slot
; /* Pipe slot */
2382 cups_array_t
*filters
, /* Filters for job */
2383 *prefilters
; /* Filters with prefilters */
2384 mime_filter_t
*filter
, /* Current filter */
2385 *prefilter
, /* Prefilter */
2386 port_monitor
; /* Port monitor filter */
2387 char method
[255], /* Method for output */
2388 *optptr
, /* Pointer to options */
2389 *valptr
; /* Pointer in value string */
2390 ipp_attribute_t
*attr
; /* Current attribute */
2391 struct stat backinfo
; /* Backend file information */
2392 int backroot
; /* Run backend as root? */
2393 int pid
; /* Process ID of new filter process */
2394 int banner_page
; /* 1 if banner page, 0 otherwise */
2395 int filterfds
[2][2];/* Pipes used between filters */
2396 int envc
; /* Number of environment variables */
2397 char **argv
, /* Filter command-line arguments */
2398 sani_uri
[1024], /* Sanitized DEVICE_URI env var */
2399 filename
[1024], /* Job filename */
2400 command
[1024], /* Full path to command */
2401 jobid
[255], /* Job ID string */
2402 title
[IPP_MAX_NAME
],
2403 /* Job title string */
2404 copies
[255], /* # copies string */
2405 *envp
[MAX_ENV
+ 15],
2406 /* Environment variables */
2407 charset
[255], /* CHARSET env variable */
2408 class_name
[255],/* CLASS env variable */
2409 classification
[1024],
2410 /* CLASSIFICATION env variable */
2412 /* CONTENT_TYPE env variable */
2414 /* DEVICE_URI env variable */
2415 final_content_type
[1024],
2416 /* FINAL_CONTENT_TYPE env variable */
2417 lang
[255], /* LANG env variable */
2418 ppd
[1024], /* PPD env variable */
2420 /* PRINTER env variable */
2422 /* RIP_MAX_CACHE env variable */
2423 static char *options
= NULL
;/* Full list of options */
2424 static int optlength
= 0; /* Length of option buffer */
2427 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] start_job: file = %d/%d",
2428 job
->id
, job
->current_file
, job
->num_files
);
2430 if (job
->num_files
== 0)
2432 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Job %d] No files, canceling job!",
2435 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2440 * Figure out what filters are required to convert from
2441 * the source to the destination type...
2450 * Remote jobs and raw queues go directly to the printer without
2454 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2455 "[Job %d] Sending job to queue tagged as raw...", job
->id
);
2462 * Local jobs get filtered...
2465 filters
= mimeFilter(MimeDatabase
, job
->filetypes
[job
->current_file
],
2466 printer
->filetype
, &(job
->cost
));
2470 cupsdLogMessage(CUPSD_LOG_ERROR
,
2471 "[Job %d] Unable to convert file %d to printable format!",
2472 job
->current_file
, job
->id
);
2473 cupsdLogMessage(CUPSD_LOG_INFO
,
2474 "Hint: Do you have ESP Ghostscript installed?");
2476 if (LogLevel
< CUPSD_LOG_DEBUG
)
2477 cupsdLogMessage(CUPSD_LOG_INFO
,
2478 "Hint: Try setting the LogLevel to \"debug\".");
2480 job
->current_file
++;
2482 if (job
->current_file
== job
->num_files
)
2483 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2489 * Remove NULL ("-") filters...
2492 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
2494 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
2495 if (!strcmp(filter
->filter
, "-"))
2496 cupsArrayRemove(filters
, filter
);
2498 if (cupsArrayCount(filters
) == 0)
2500 cupsArrayDelete(filters
);
2505 * If this printer has any pre-filters, insert the required pre-filter
2506 * in the filters array...
2509 if (printer
->prefiltertype
&& filters
)
2511 prefilters
= cupsArrayNew(NULL
, NULL
);
2513 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
2515 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
2517 if ((prefilter
= mimeFilterLookup(MimeDatabase
, filter
->src
,
2518 printer
->prefiltertype
)))
2520 cupsArrayAdd(prefilters
, prefilter
);
2521 job
->cost
+= prefilter
->cost
;
2524 cupsArrayAdd(prefilters
, filter
);
2527 cupsArrayDelete(filters
);
2528 filters
= prefilters
;
2533 * Set a minimum cost of 100 for all jobs so that FilterLimit
2534 * works with raw queues and other low-cost paths.
2537 if (job
->cost
< 100)
2541 * See if the filter cost is too high...
2544 if ((FilterLevel
+ job
->cost
) > FilterLimit
&& FilterLevel
> 0 &&
2548 * Don't print this job quite yet...
2551 cupsArrayDelete(filters
);
2553 cupsdLogMessage(CUPSD_LOG_INFO
,
2554 "[Job %d] Holding because filter limit has been reached.",
2556 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2557 "[Job %d] start_job: file=%d, cost=%d, level=%d, limit=%d",
2558 job
->id
, job
->current_file
, job
->cost
, FilterLevel
,
2563 FilterLevel
+= job
->cost
;
2566 * Add decompression/raw filter as needed...
2569 if ((!printer
->raw
&& job
->compressions
[job
->current_file
]) ||
2570 (!filters
&& !printer
->remote
&&
2571 (job
->num_files
> 1 || !strncmp(printer
->device_uri
, "file:", 5))))
2574 * Add gziptoany filter to the front of the list...
2578 filters
= cupsArrayNew(NULL
, NULL
);
2580 if (!cupsArrayInsert(filters
, &gziptoany_filter
))
2582 cupsdLogMessage(CUPSD_LOG_ERROR
,
2583 "[Job %d] Unable to add decompression filter - %s",
2584 job
->id
, strerror(errno
));
2586 cupsArrayDelete(filters
);
2588 job
->current_file
++;
2590 if (job
->current_file
== job
->num_files
)
2591 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2598 * Add port monitor, if any...
2601 if (printer
->port_monitor
)
2604 * Add port monitor to the end of the list...
2608 filters
= cupsArrayNew(NULL
, NULL
);
2610 if (!cupsArrayAdd(filters
, &port_monitor
))
2612 cupsdLogMessage(CUPSD_LOG_ERROR
,
2613 "[Job %d] Unable to add port monitor - %s",
2614 job
->id
, strerror(errno
));
2616 cupsArrayDelete(filters
);
2618 job
->current_file
++;
2620 if (job
->current_file
== job
->num_files
)
2621 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2626 snprintf(port_monitor
.filter
, sizeof(port_monitor
.filter
),
2627 "%s/monitor/%s", ServerBin
, printer
->port_monitor
);
2631 * Update the printer and job state to "processing"...
2634 job
->state
->values
[0].integer
= IPP_JOB_PROCESSING
;
2635 job
->state_value
= IPP_JOB_PROCESSING
;
2638 job
->printer
= printer
;
2641 cupsdSetPrinterState(printer
, IPP_PRINTER_PROCESSING
, 0);
2643 if (job
->current_file
== 0)
2646 * Set the processing time...
2649 set_time(job
, "time-at-processing");
2652 * Create the backchannel pipes and make them non-blocking...
2655 cupsdOpenPipe(job
->back_pipes
);
2657 fcntl(job
->back_pipes
[0], F_SETFL
,
2658 fcntl(job
->back_pipes
[0], F_GETFL
) | O_NONBLOCK
);
2660 fcntl(job
->back_pipes
[1], F_SETFL
,
2661 fcntl(job
->back_pipes
[1], F_GETFL
) | O_NONBLOCK
);
2664 * Create the side-channel pipes and make them non-blocking...
2667 socketpair(AF_LOCAL
, SOCK_STREAM
, 0, job
->side_pipes
);
2669 fcntl(job
->side_pipes
[0], F_SETFL
,
2670 fcntl(job
->side_pipes
[0], F_GETFL
) | O_NONBLOCK
);
2672 fcntl(job
->side_pipes
[1], F_SETFL
,
2673 fcntl(job
->side_pipes
[1], F_GETFL
) | O_NONBLOCK
);
2677 * Determine if we are printing a banner page or not...
2680 if (job
->job_sheets
== NULL
)
2682 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] No job-sheets attribute.",
2684 if ((job
->job_sheets
=
2685 ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_ZERO
)) != NULL
)
2686 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2687 "[Job %d] ... but someone added one without setting "
2688 "job_sheets!", job
->id
);
2690 else if (job
->job_sheets
->num_values
== 1)
2691 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] job-sheets=%s", job
->id
,
2692 job
->job_sheets
->values
[0].string
.text
);
2694 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] job-sheets=%s,%s", job
->id
,
2695 job
->job_sheets
->values
[0].string
.text
,
2696 job
->job_sheets
->values
[1].string
.text
);
2698 if (printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
))
2700 else if (job
->job_sheets
== NULL
)
2702 else if (strcasecmp(job
->job_sheets
->values
[0].string
.text
, "none") != 0 &&
2703 job
->current_file
== 0)
2705 else if (job
->job_sheets
->num_values
> 1 &&
2706 strcasecmp(job
->job_sheets
->values
[1].string
.text
, "none") != 0 &&
2707 job
->current_file
== (job
->num_files
- 1))
2712 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] banner_page = %d", job
->id
,
2716 * Building the options string is harder than it needs to be, but
2717 * for the moment we need to pass strings for command-line args and
2718 * not IPP attribute pointers... :)
2720 * First allocate/reallocate the option buffer as needed...
2723 i
= ipp_length(job
->attrs
);
2730 optptr
= realloc(options
, i
);
2734 cupsdLogMessage(CUPSD_LOG_CRIT
,
2735 "[Job %d] Unable to allocate %d bytes for option buffer!",
2738 cupsArrayDelete(filters
);
2740 FilterLevel
-= job
->cost
;
2742 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2751 * Now loop through the attributes and convert them to the textual
2752 * representation used by the filters...
2758 snprintf(title
, sizeof(title
), "%s-%d", printer
->name
, job
->id
);
2759 strcpy(copies
, "1");
2761 for (attr
= job
->attrs
->attrs
; attr
!= NULL
; attr
= attr
->next
)
2763 if (!strcmp(attr
->name
, "copies") &&
2764 attr
->value_tag
== IPP_TAG_INTEGER
)
2767 * Don't use the # copies attribute if we are printing the job sheets...
2771 sprintf(copies
, "%d", attr
->values
[0].integer
);
2773 else if (!strcmp(attr
->name
, "job-name") &&
2774 (attr
->value_tag
== IPP_TAG_NAME
||
2775 attr
->value_tag
== IPP_TAG_NAMELANG
))
2776 strlcpy(title
, attr
->values
[0].string
.text
, sizeof(title
));
2777 else if (attr
->group_tag
== IPP_TAG_JOB
)
2780 * Filter out other unwanted attributes...
2783 if (attr
->value_tag
== IPP_TAG_MIMETYPE
||
2784 attr
->value_tag
== IPP_TAG_NAMELANG
||
2785 attr
->value_tag
== IPP_TAG_TEXTLANG
||
2786 (attr
->value_tag
== IPP_TAG_URI
&& strcmp(attr
->name
, "job-uuid")) ||
2787 attr
->value_tag
== IPP_TAG_URISCHEME
||
2788 attr
->value_tag
== IPP_TAG_BEGIN_COLLECTION
) /* Not yet supported */
2791 if (!strncmp(attr
->name
, "time-", 5))
2794 if (!strncmp(attr
->name
, "job-", 4) && strcmp(attr
->name
, "job-uuid") &&
2795 !(printer
->type
& CUPS_PRINTER_REMOTE
))
2798 if (!strncmp(attr
->name
, "job-", 4) &&
2799 strcmp(attr
->name
, "job-uuid") &&
2800 strcmp(attr
->name
, "job-billing") &&
2801 strcmp(attr
->name
, "job-sheets") &&
2802 strcmp(attr
->name
, "job-hold-until") &&
2803 strcmp(attr
->name
, "job-priority"))
2806 if ((!strcmp(attr
->name
, "page-label") ||
2807 !strcmp(attr
->name
, "page-border") ||
2808 !strncmp(attr
->name
, "number-up", 9) ||
2809 !strcmp(attr
->name
, "page-ranges") ||
2810 !strcmp(attr
->name
, "page-set") ||
2811 !strcasecmp(attr
->name
, "AP_FIRSTPAGE_InputSlot") ||
2812 !strcasecmp(attr
->name
, "AP_FIRSTPAGE_ManualFeed")) &&
2817 * Otherwise add them to the list...
2820 if (optptr
> options
)
2821 strlcat(optptr
, " ", optlength
- (optptr
- options
));
2823 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
2825 strlcat(optptr
, attr
->name
, optlength
- (optptr
- options
));
2826 strlcat(optptr
, "=", optlength
- (optptr
- options
));
2829 for (i
= 0; i
< attr
->num_values
; i
++)
2832 strlcat(optptr
, ",", optlength
- (optptr
- options
));
2834 optptr
+= strlen(optptr
);
2836 switch (attr
->value_tag
)
2838 case IPP_TAG_INTEGER
:
2840 snprintf(optptr
, optlength
- (optptr
- options
),
2841 "%d", attr
->values
[i
].integer
);
2844 case IPP_TAG_BOOLEAN
:
2845 if (!attr
->values
[i
].boolean
)
2846 strlcat(optptr
, "no", optlength
- (optptr
- options
));
2848 case IPP_TAG_NOVALUE
:
2849 strlcat(optptr
, attr
->name
,
2850 optlength
- (optptr
- options
));
2853 case IPP_TAG_RANGE
:
2854 if (attr
->values
[i
].range
.lower
== attr
->values
[i
].range
.upper
)
2855 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2856 "%d", attr
->values
[i
].range
.lower
);
2858 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2859 "%d-%d", attr
->values
[i
].range
.lower
,
2860 attr
->values
[i
].range
.upper
);
2863 case IPP_TAG_RESOLUTION
:
2864 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2865 "%dx%d%s", attr
->values
[i
].resolution
.xres
,
2866 attr
->values
[i
].resolution
.yres
,
2867 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
2871 case IPP_TAG_STRING
:
2874 case IPP_TAG_KEYWORD
:
2875 case IPP_TAG_CHARSET
:
2876 case IPP_TAG_LANGUAGE
:
2878 for (valptr
= attr
->values
[i
].string
.text
; *valptr
;)
2880 if (strchr(" \t\n\\\'\"", *valptr
))
2882 *optptr
++ = *valptr
++;
2889 break; /* anti-compiler-warning-code */
2893 optptr
+= strlen(optptr
);
2898 * Build the command-line arguments for the filters. Each filter
2899 * has 6 or 7 arguments:
2903 * argv[2] = username
2905 * argv[4] = # copies
2907 * argv[6] = filename (optional; normally stdin)
2909 * This allows legacy printer drivers that use the old System V
2910 * printing interface to be used by CUPS.
2912 * For remote jobs, we send all of the files in the argument list.
2915 if (printer
->remote
&& job
->num_files
> 1)
2916 argv
= calloc(7 + job
->num_files
, sizeof(char *));
2918 argv
= calloc(8, sizeof(char *));
2920 sprintf(jobid
, "%d", job
->id
);
2922 argv
[0] = printer
->name
;
2924 argv
[2] = job
->username
;
2929 if (printer
->remote
&& job
->num_files
> 1)
2931 for (i
= 0; i
< job
->num_files
; i
++)
2933 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
2935 argv
[6 + i
] = strdup(filename
);
2940 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
2941 job
->id
, job
->current_file
+ 1);
2945 for (i
= 0; argv
[i
]; i
++)
2946 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2947 "[Job %d] argv[%d]=\"%s\"", job
->id
, i
, argv
[i
]);
2950 * Create environment variable strings for the filters...
2953 attr
= ippFindAttribute(job
->attrs
, "attributes-natural-language",
2956 switch (strlen(attr
->values
[0].string
.text
))
2960 * This is an unknown or badly formatted language code; use
2961 * the POSIX locale...
2964 strcpy(lang
, "LANG=C");
2969 * Just the language code (ll)...
2972 snprintf(lang
, sizeof(lang
), "LANG=%s",
2973 attr
->values
[0].string
.text
);
2978 * Language and country code (ll-cc)...
2981 snprintf(lang
, sizeof(lang
), "LANG=%c%c_%c%c",
2982 attr
->values
[0].string
.text
[0],
2983 attr
->values
[0].string
.text
[1],
2984 toupper(attr
->values
[0].string
.text
[3] & 255),
2985 toupper(attr
->values
[0].string
.text
[4] & 255));
2989 attr
= ippFindAttribute(job
->attrs
, "document-format",
2992 (optptr
= strstr(attr
->values
[0].string
.text
, "charset=")) != NULL
)
2993 snprintf(charset
, sizeof(charset
), "CHARSET=%s", optptr
+ 8);
2996 attr
= ippFindAttribute(job
->attrs
, "attributes-charset",
2998 snprintf(charset
, sizeof(charset
), "CHARSET=%s",
2999 attr
->values
[0].string
.text
);
3002 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s/%s",
3003 job
->filetypes
[job
->current_file
]->super
,
3004 job
->filetypes
[job
->current_file
]->type
);
3005 snprintf(device_uri
, sizeof(device_uri
), "DEVICE_URI=%s",
3006 printer
->device_uri
);
3007 cupsdSanitizeURI(printer
->device_uri
, sani_uri
, sizeof(sani_uri
));
3008 snprintf(ppd
, sizeof(ppd
), "PPD=%s/ppd/%s.ppd", ServerRoot
, printer
->name
);
3009 snprintf(printer_name
, sizeof(printer_name
), "PRINTER=%s", printer
->name
);
3010 snprintf(rip_max_cache
, sizeof(rip_max_cache
), "RIP_MAX_CACHE=%s", RIPCache
);
3012 envc
= cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
3014 envp
[envc
++] = charset
;
3015 envp
[envc
++] = lang
;
3016 envp
[envc
++] = ppd
;
3017 envp
[envc
++] = rip_max_cache
;
3018 envp
[envc
++] = content_type
;
3019 envp
[envc
++] = device_uri
;
3020 envp
[envc
++] = printer_name
;
3022 if (!printer
->remote
&& !printer
->raw
&&
3023 (filter
= (mime_filter_t
*)cupsArrayLast(filters
)) != NULL
&&
3026 snprintf(final_content_type
, sizeof(final_content_type
),
3027 "FINAL_CONTENT_TYPE=%s/%s",
3028 filter
->dst
->super
, filter
->dst
->type
);
3029 envp
[envc
++] = final_content_type
;
3032 if (Classification
&& !banner_page
)
3034 if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
3035 IPP_TAG_NAME
)) == NULL
)
3036 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3038 else if (attr
->num_values
> 1 &&
3039 strcmp(attr
->values
[1].string
.text
, "none") != 0)
3040 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3041 attr
->values
[1].string
.text
);
3043 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3044 attr
->values
[0].string
.text
);
3046 envp
[envc
++] = classification
;
3049 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
3051 snprintf(class_name
, sizeof(class_name
), "CLASS=%s", job
->dest
);
3052 envp
[envc
++] = class_name
;
3055 if (job
->auth_username
)
3056 envp
[envc
++] = job
->auth_username
;
3057 if (job
->auth_domain
)
3058 envp
[envc
++] = job
->auth_domain
;
3059 if (job
->auth_password
)
3060 envp
[envc
++] = job
->auth_password
;
3064 envp
[envc
++] = job
->ccname
;
3065 #endif /* HAVE_GSSAPI */
3069 for (i
= 0; i
< envc
; i
++)
3070 if (!strncmp(envp
[i
], "AUTH_", 5))
3071 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] envp[%d]=\"AUTH_%c****\"",
3072 job
->id
, i
, envp
[i
][5]);
3073 else if (strncmp(envp
[i
], "DEVICE_URI=", 11))
3074 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] envp[%d]=\"%s\"",
3075 job
->id
, i
, envp
[i
]);
3077 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] envp[%d]=\"DEVICE_URI=%s\"",
3078 job
->id
, i
, sani_uri
);
3080 if (printer
->remote
)
3081 job
->current_file
= job
->num_files
;
3083 job
->current_file
++;
3086 * Now create processes for all of the filters...
3089 filterfds
[0][0] = -1;
3090 filterfds
[0][1] = -1;
3091 filterfds
[1][0] = -1;
3092 filterfds
[1][1] = -1;
3094 if (!job
->status_buffer
)
3096 if (cupsdOpenPipe(job
->status_pipes
))
3098 cupsdLogMessage(CUPSD_LOG_ERROR
,
3099 "[Job %d] Unable to create job status pipes - %s.",
3100 job
->id
, strerror(errno
));
3101 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3102 "Unable to create status pipes - %s.", strerror(errno
));
3104 cupsdAddPrinterHistory(printer
);
3106 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3107 "Job canceled because the server could not create the job "
3113 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3114 "[Job %d] start_job: status_pipes = [ %d %d ]",
3115 job
->id
, job
->status_pipes
[0], job
->status_pipes
[1]);
3117 job
->status_buffer
= cupsdStatBufNew(job
->status_pipes
[0], "[Job %d]",
3119 job
->status_level
= CUPSD_LOG_INFO
;
3123 memset(job
->filters
, 0, sizeof(job
->filters
));
3125 for (i
= 0, slot
= 0, filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
3127 i
++, filter
= (mime_filter_t
*)cupsArrayNext(filters
))
3129 if (filter
->filter
[0] != '/')
3130 snprintf(command
, sizeof(command
), "%s/filter/%s", ServerBin
,
3133 strlcpy(command
, filter
->filter
, sizeof(command
));
3135 if (i
< (cupsArrayCount(filters
) - 1))
3137 if (cupsdOpenPipe(filterfds
[slot
]))
3139 cupsdLogMessage(CUPSD_LOG_ERROR
,
3140 "[Job %d] Unable to create job filter pipes - %s.",
3141 job
->id
, strerror(errno
));
3142 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3143 "Unable to create filter pipes - %s.", strerror(errno
));
3144 cupsdAddPrinterHistory(printer
);
3146 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3147 "Job canceled because the server could not create the "
3155 if (job
->current_file
== 1)
3157 if (strncmp(printer
->device_uri
, "file:", 5) != 0)
3159 if (cupsdOpenPipe(job
->print_pipes
))
3161 cupsdLogMessage(CUPSD_LOG_ERROR
,
3162 "[Job %d] Unable to create job backend pipes - %s.",
3163 job
->id
, strerror(errno
));
3164 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3165 "Unable to create backend pipes - %s.", strerror(errno
));
3166 cupsdAddPrinterHistory(printer
);
3168 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3169 "Job canceled because the server could not create "
3170 "the backend pipes.");
3177 job
->print_pipes
[0] = -1;
3178 if (!strcmp(printer
->device_uri
, "file:/dev/null") ||
3179 !strcmp(printer
->device_uri
, "file:///dev/null"))
3180 job
->print_pipes
[1] = -1;
3183 if (!strncmp(printer
->device_uri
, "file:/dev/", 10))
3184 job
->print_pipes
[1] = open(printer
->device_uri
+ 5,
3186 else if (!strncmp(printer
->device_uri
, "file:///dev/", 12))
3187 job
->print_pipes
[1] = open(printer
->device_uri
+ 7,
3189 else if (!strncmp(printer
->device_uri
, "file:///", 8))
3190 job
->print_pipes
[1] = open(printer
->device_uri
+ 7,
3191 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
3193 job
->print_pipes
[1] = open(printer
->device_uri
+ 5,
3194 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
3196 if (job
->print_pipes
[1] < 0)
3198 cupsdLogMessage(CUPSD_LOG_ERROR
,
3199 "[Job %d] Unable to open output file \"%s\" - %s.",
3200 job
->id
, printer
->device_uri
, strerror(errno
));
3201 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3202 "Unable to open output file \"%s\" - %s.",
3203 printer
->device_uri
, strerror(errno
));
3205 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3206 "Job canceled because the server could not open the "
3212 fcntl(job
->print_pipes
[1], F_SETFD
,
3213 fcntl(job
->print_pipes
[1], F_GETFD
) | FD_CLOEXEC
);
3217 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3218 "[Job %d] start_job: print_pipes = [ %d %d ]",
3219 job
->id
, job
->print_pipes
[0], job
->print_pipes
[1]);
3222 filterfds
[slot
][0] = job
->print_pipes
[0];
3223 filterfds
[slot
][1] = job
->print_pipes
[1];
3226 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] start_job: filter=\"%s\"",
3228 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3229 "[Job %d] start_job: filterfds[%d]=[ %d %d ]",
3230 job
->id
, slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3232 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
3233 filterfds
[slot
][1], job
->status_pipes
[1],
3234 job
->back_pipes
[0], job
->side_pipes
[0], 0,
3237 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3238 "[Job %d] start_job: Closing filter pipes for slot %d "
3240 job
->id
, !slot
, filterfds
[!slot
][0], filterfds
[!slot
][1]);
3242 cupsdClosePipe(filterfds
[!slot
]);
3246 cupsdLogMessage(CUPSD_LOG_ERROR
,
3247 "[Job %d] Unable to start filter \"%s\" - %s.",
3248 job
->id
, filter
->filter
, strerror(errno
));
3249 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3250 "Unable to start filter \"%s\" - %s.",
3251 filter
->filter
, strerror(errno
));
3253 cupsdAddPrinterHistory(printer
);
3255 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3256 "Job canceled because the server could not execute a "
3262 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Started filter %s (PID %d)",
3263 job
->id
, command
, pid
);
3269 cupsArrayDelete(filters
);
3272 * Finally, pipe the final output into a backend process if needed...
3275 if (strncmp(printer
->device_uri
, "file:", 5) != 0)
3277 if (job
->current_file
== 1)
3279 sscanf(printer
->device_uri
, "%254[^:]", method
);
3280 snprintf(command
, sizeof(command
), "%s/backend/%s", ServerBin
, method
);
3283 * See if the backend needs to run as root...
3288 else if (stat(command
, &backinfo
))
3291 backroot
= !(backinfo
.st_mode
& (S_IRWXG
| S_IRWXO
));
3295 filterfds
[slot
][0] = -1;
3296 filterfds
[slot
][1] = -1;
3298 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] start_job: backend=\"%s\"",
3300 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3301 "[Job %d] start_job: filterfds[%d] = [ %d %d ]", job
->id
,
3302 slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3304 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
3305 filterfds
[slot
][1], job
->status_pipes
[1],
3306 job
->back_pipes
[1], job
->side_pipes
[1],
3307 backroot
, &(job
->backend
));
3311 cupsdLogMessage(CUPSD_LOG_ERROR
,
3312 "[Job %d] Unable to start backend \"%s\" - %s.",
3313 job
->id
, method
, strerror(errno
));
3314 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3315 "Unable to start backend \"%s\" - %s.", method
,
3318 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3319 "Job canceled because the server could not execute "
3326 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Started backend %s (PID %d)",
3327 job
->id
, command
, pid
);
3331 if (job
->current_file
== job
->num_files
)
3333 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3334 "[Job %d] start_job: Closing print pipes [ %d %d ]...",
3335 job
->id
, job
->print_pipes
[0], job
->print_pipes
[1]);
3337 cupsdClosePipe(job
->print_pipes
);
3339 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3340 "[Job %d] start_job: Closing back pipes [ %d %d ]...",
3341 job
->id
, job
->back_pipes
[0], job
->back_pipes
[1]);
3343 cupsdClosePipe(job
->back_pipes
);
3345 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3346 "[Job %d] start_job: Closing side pipes [ %d %d ]...",
3347 job
->id
, job
->side_pipes
[0], job
->side_pipes
[1]);
3349 cupsdClosePipe(job
->side_pipes
);
3351 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3352 "[Job %d] start_job: Closing status output pipe %d...",
3353 job
->id
, job
->status_pipes
[1]);
3355 close(job
->status_pipes
[1]);
3356 job
->status_pipes
[1] = -1;
3361 filterfds
[slot
][0] = -1;
3362 filterfds
[slot
][1] = -1;
3364 if (job
->current_file
== job
->num_files
)
3366 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3367 "[Job %d] start_job: Closing print pipes [ %d %d ]...",
3368 job
->id
, job
->print_pipes
[0], job
->print_pipes
[1]);
3370 cupsdClosePipe(job
->print_pipes
);
3372 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3373 "[Job %d] start_job: Closing status output pipe %d...",
3374 job
->id
, job
->status_pipes
[1]);
3376 close(job
->status_pipes
[1]);
3377 job
->status_pipes
[1] = -1;
3381 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3382 "[Job %d] start_job: Closing filter pipes for slot %d "
3384 job
->id
, slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3385 cupsdClosePipe(filterfds
[slot
]);
3387 if (printer
->remote
&& job
->num_files
> 1)
3389 for (i
= 0; i
< job
->num_files
; i
++)
3395 cupsdAddSelect(job
->status_buffer
->fd
, (cupsd_selfunc_t
)update_job
, NULL
,
3398 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
, "Job #%d started.",
3405 * If we get here, we need to abort the current job and close out all
3406 * files and pipes...
3411 for (slot
= 0; slot
< 2; slot
++)
3413 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3414 "[Job %d] start_job: Closing filter pipes for slot %d "
3416 job
->id
, slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3417 cupsdClosePipe(filterfds
[slot
]);
3420 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3421 "[Job %d] start_job: Closing status pipes [ %d %d ]...",
3422 job
->id
, job
->status_pipes
[0], job
->status_pipes
[1]);
3423 cupsdClosePipe(job
->status_pipes
);
3424 cupsdStatBufDelete(job
->status_buffer
);
3426 job
->status_buffer
= NULL
;
3428 cupsArrayDelete(filters
);
3430 if (printer
->remote
&& job
->num_files
> 1)
3432 for (i
= 0; i
< job
->num_files
; i
++)
3438 cupsdStopJob(job
, 0);
3443 * 'unload_job()' - Unload a job from memory.
3447 unload_job(cupsd_job_t
*job
) /* I - Job */
3452 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Unloading...", job
->id
);
3454 ippDelete(job
->attrs
);
3459 job
->job_sheets
= NULL
;
3464 * 'update_job()' - Read a status update from a job's filters.
3468 update_job(cupsd_job_t
*job
) /* I - Job to check */
3470 int i
; /* Looping var */
3471 int copies
; /* Number of copies printed */
3472 char message
[1024], /* Message text */
3473 *ptr
; /* Pointer update... */
3474 int loglevel
, /* Log level for message */
3475 event
= 0; /* Events? */
3478 while ((ptr
= cupsdStatBufUpdate(job
->status_buffer
, &loglevel
,
3479 message
, sizeof(message
))) != NULL
)
3482 * Process page and printer state messages as needed...
3485 if (loglevel
== CUPSD_LOG_PAGE
)
3488 * Page message; send the message to the page_log file and update the
3489 * job sheet count...
3492 if (job
->sheets
!= NULL
)
3494 if (!strncasecmp(message
, "total ", 6))
3497 * Got a total count of pages from a backend or filter...
3500 copies
= atoi(message
+ 6);
3501 copies
-= job
->sheets
->values
[0].integer
; /* Just track the delta */
3503 else if (!sscanf(message
, "%*d%d", &copies
))
3506 job
->sheets
->values
[0].integer
+= copies
;
3508 if (job
->printer
->page_limit
)
3510 cupsd_quota_t
*q
= cupsdUpdateQuota(job
->printer
, job
->username
,
3514 if (AppleQuotas
&& q
->page_count
== -3)
3517 * Quota limit exceeded, cancel job in progress immediately...
3520 cupsdLogMessage(CUPSD_LOG_INFO
,
3521 "[Job %d] Canceled because pages exceed user %s "
3522 "quota limit on printer %s (%s).",
3523 job
->id
, job
->username
, job
->printer
->name
,
3524 job
->printer
->info
);
3526 cupsdCancelJob(job
, 1, IPP_JOB_CANCELED
);
3531 #endif /* __APPLE__ */
3535 cupsdLogPage(job
, message
);
3537 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
3538 "Printed %d page(s).", job
->sheets
->values
[0].integer
);
3540 else if (loglevel
== CUPSD_LOG_STATE
)
3542 if (!strcmp(message
, "paused"))
3543 cupsdStopPrinter(job
->printer
, 1);
3546 cupsdSetPrinterReasons(job
->printer
, message
);
3547 cupsdAddPrinterHistory(job
->printer
);
3548 event
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
3551 update_job_attrs(job
);
3553 else if (loglevel
== CUPSD_LOG_ATTR
)
3556 * Set attribute(s)...
3559 int num_attrs
; /* Number of attributes */
3560 cups_option_t
*attrs
; /* Attributes */
3561 const char *attr
; /* Attribute */
3564 num_attrs
= cupsParseOptions(message
, 0, &attrs
);
3566 if ((attr
= cupsGetOption("auth-info-required", num_attrs
,
3568 cupsdSetAuthInfoRequired(job
->printer
, attr
, NULL
);
3570 if ((attr
= cupsGetOption("printer-alert", num_attrs
, attrs
)) != NULL
)
3572 cupsdSetString(&job
->printer
->alert
, attr
);
3573 event
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
3576 if ((attr
= cupsGetOption("printer-alert-description", num_attrs
,
3579 cupsdSetString(&job
->printer
->alert_description
, attr
);
3580 event
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
3583 cupsFreeOptions(num_attrs
, attrs
);
3586 else if (!strncmp(message
, "recoverable:", 12))
3588 cupsdSetPrinterReasons(job
->printer
,
3589 "+com.apple.print.recoverable-warning");
3592 while (isspace(*ptr
& 255))
3595 cupsdSetString(&job
->printer
->recoverable
, ptr
);
3596 cupsdAddPrinterHistory(job
->printer
);
3597 event
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
3599 else if (!strncmp(message
, "recovered:", 10))
3601 cupsdSetPrinterReasons(job
->printer
,
3602 "-com.apple.print.recoverable-warning");
3605 while (isspace(*ptr
& 255))
3608 cupsdSetString(&job
->printer
->recoverable
, ptr
);
3609 cupsdAddPrinterHistory(job
->printer
);
3610 event
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
3612 #endif /* __APPLE__ */
3613 else if (loglevel
<= job
->status_level
)
3616 * Some message to show in the printer-state-message attribute...
3619 job
->status_level
= loglevel
;
3621 strlcpy(job
->printer
->state_message
, message
,
3622 sizeof(job
->printer
->state_message
));
3623 cupsdAddPrinterHistory(job
->printer
);
3624 event
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
3626 update_job_attrs(job
);
3629 if (!strchr(job
->status_buffer
->buffer
, '\n'))
3633 if ((event
& CUPSD_EVENT_PRINTER_STATE_CHANGED
))
3634 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE_CHANGED
, job
->printer
, NULL
,
3635 (job
->printer
->type
& CUPS_PRINTER_CLASS
) ?
3636 "Class \"%s\" state changed." :
3637 "Printer \"%s\" state changed.",
3638 job
->printer
->name
);
3640 if (ptr
== NULL
&& !job
->status_buffer
->bufused
)
3643 * See if all of the filters and the backend have returned their
3647 for (i
= 0; job
->filters
[i
] < 0; i
++);
3649 if (job
->filters
[i
])
3652 if (job
->current_file
>= job
->num_files
&& job
->backend
> 0)
3656 * Handle the end of job stuff...
3659 cupsdFinishJob(job
);
3665 * 'update_job_attrs()' - Update the job-printer-* attributes.
3669 update_job_attrs(cupsd_job_t
*job
) /* I - Job to update */
3671 int i
; /* Looping var */
3672 int num_reasons
; /* Actual number of reasons */
3673 const char * const *reasons
; /* Reasons */
3674 static const char *none
= "none", /* "none" */
3680 * Get/create the job-printer-state-* attributes...
3683 if (!job
->printer_message
)
3685 if ((job
->printer_message
= ippFindAttribute(job
->attrs
,
3686 "job-printer-state-message",
3687 IPP_TAG_TEXT
)) == NULL
)
3688 job
->printer_message
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_TEXT
,
3689 "job-printer-state-message",
3693 if (!job
->printer_reasons
)
3694 job
->printer_reasons
= ippFindAttribute(job
->attrs
,
3695 "job-printer-state-reasons",
3699 * If the job isn't printing, return now...
3706 * Otherwise copy the printer-state-message value...
3709 if (job
->printer
->state_message
[0])
3710 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
),
3711 job
->printer
->state_message
);
3714 * ... and the printer-state-reasons value...
3717 if (job
->printer
->num_reasons
== 0)
3720 reasons
= job
->printer
->state
== IPP_PRINTER_STOPPED
? &paused
: &none
;
3724 num_reasons
= job
->printer
->num_reasons
;
3725 reasons
= (const char * const *)job
->printer
->reasons
;
3728 if (!job
->printer_reasons
|| job
->printer_reasons
->num_values
!= num_reasons
)
3730 ippDeleteAttribute(job
->attrs
, job
->printer_reasons
);
3732 job
->printer_reasons
= ippAddStrings(job
->attrs
,
3733 IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
3734 "job-printer-state-reasons",
3735 num_reasons
, NULL
, NULL
);
3738 for (i
= 0; i
< num_reasons
; i
++)
3739 cupsdSetString(&(job
->printer_reasons
->values
[i
].string
.text
), reasons
[i
]);
3744 * End of "$Id: job.c 6671 2007-07-13 23:35:24Z mike $".