2 * "$Id: job.c 7902 2008-09-03 14:20:17Z mike $"
4 * Job management routines for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2008 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 * cupsdDeleteJob() - Free all memory used by a job.
25 * cupsdFinishJob() - Finish a job.
26 * cupsdFreeAllJobs() - Free all jobs from memory.
27 * cupsdFindJob() - Find the specified job.
28 * cupsdGetPrinterJobCount() - Get the number of pending, processing,
29 * cupsdGetUserJobCount() - Get the number of pending, processing,
30 * cupsdHoldJob() - Hold the specified job.
31 * cupsdLoadAllJobs() - Load all jobs from disk.
32 * cupsdLoadJob() - Load a single job...
33 * cupsdMoveJob() - Move the specified job to a different
35 * cupsdReleaseJob() - Release the specified job.
36 * cupsdRestartJob() - Restart the specified job.
37 * cupsdSaveAllJobs() - Save a summary of all jobs to disk.
38 * cupsdSaveJob() - Save a job to disk.
39 * cupsdSetJobHoldUntil() - Set the hold time for a job...
40 * cupsdSetJobPriority() - Set the priority of a job, moving it
41 * up/down in the list as needed.
42 * cupsdStopAllJobs() - Stop all print jobs.
43 * cupsdStopJob() - Stop a print job.
44 * cupsdUnloadCompletedJobs() - Flush completed job history from memory.
45 * compare_active_jobs() - Compare the job IDs and priorities of two
47 * compare_jobs() - Compare the job IDs of two jobs.
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 int ipp_length(ipp_t
*ipp
);
93 static void load_job_cache(const char *filename
);
94 static void load_next_job_id(const char *filename
);
95 static void load_request_root(void);
96 static void set_time(cupsd_job_t
*job
, const char *name
);
97 static void set_hold_until(cupsd_job_t
*job
, time_t holdtime
);
98 static void start_job(cupsd_job_t
*job
, cupsd_printer_t
*printer
);
99 static void unload_job(cupsd_job_t
*job
);
100 static void update_job(cupsd_job_t
*job
);
101 static void update_job_attrs(cupsd_job_t
*job
, int do_message
);
105 * 'cupsdAddJob()' - Add a new job to the job queue...
108 cupsd_job_t
* /* O - New job record */
109 cupsdAddJob(int priority
, /* I - Job priority */
110 const char *dest
) /* I - Job destination */
112 cupsd_job_t
*job
; /* New job record */
115 if ((job
= calloc(sizeof(cupsd_job_t
), 1)) == NULL
)
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 and printing lists...
216 cupsArrayRemove(ActiveJobs
, job
);
217 cupsArrayRemove(PrintingJobs
, job
);
220 * Remove any authentication data...
223 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
224 if (cupsdRemoveFile(filename
) && errno
!= ENOENT
)
225 cupsdLogMessage(CUPSD_LOG_ERROR
,
226 "Unable to remove authentication cache: %s",
229 cupsdClearString(&job
->auth_username
);
230 cupsdClearString(&job
->auth_domain
);
231 cupsdClearString(&job
->auth_password
);
235 * Destroy the credential cache and clear the KRB5CCNAME env var string.
240 krb5_cc_destroy(KerberosContext
, job
->ccache
);
244 cupsdClearString(&job
->ccname
);
245 #endif /* HAVE_GSSAPI */
248 * Remove the print file for good if we aren't preserving jobs or
252 job
->current_file
= 0;
254 if (!JobHistory
|| !JobFiles
|| purge
)
256 for (i
= 1; i
<= job
->num_files
; i
++)
258 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
263 if (job
->num_files
> 0)
265 free(job
->filetypes
);
266 free(job
->compressions
);
269 job
->filetypes
= NULL
;
270 job
->compressions
= NULL
;
274 if (JobHistory
&& !purge
)
277 * Save job state info...
281 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
286 * Remove the job info file...
289 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
,
294 * Remove the job from the "all jobs" list...
297 cupsArrayRemove(Jobs
, job
);
300 * Free all memory used...
311 * 'cupsdCancelJobs()' - Cancel all jobs for the given destination/user...
315 cupsdCancelJobs(const char *dest
, /* I - Destination to cancel */
316 const char *username
, /* I - Username or NULL */
317 int purge
) /* I - Purge jobs? */
319 cupsd_job_t
*job
; /* Current job */
322 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
324 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
326 if (!job
->dest
|| !job
->username
)
329 if (!job
->dest
|| !job
->username
)
332 if ((dest
== NULL
|| !strcmp(job
->dest
, dest
)) &&
333 (username
== NULL
|| !strcmp(job
->username
, username
)))
336 * Cancel all jobs matching this destination/user...
339 cupsdCancelJob(job
, purge
, IPP_JOB_CANCELED
);
348 * 'cupsdCheckJobs()' - Check the pending jobs and start any if the destination
355 cupsd_job_t
*job
; /* Current job in queue */
356 cupsd_printer_t
*printer
, /* Printer destination */
357 *pclass
; /* Printer class destination */
358 ipp_attribute_t
*attr
; /* Job attribute */
361 DEBUG_puts("cupsdCheckJobs()");
363 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
364 "cupsdCheckJobs: %d active jobs, sleeping=%d, reload=%d",
365 cupsArrayCount(ActiveJobs
), Sleeping
, NeedReload
);
367 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
369 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
372 * Start held jobs if they are ready...
375 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
376 "cupsdCheckJobs: Job %d: dest=%s, dtype=%x, "
377 "state_value=%d, loaded=%s", job
->id
, job
->dest
, job
->dtype
,
378 job
->state_value
, job
->attrs
? "yes" : "no");
380 if (job
->state_value
== IPP_JOB_HELD
&&
382 job
->hold_until
< time(NULL
))
384 if (job
->pending_timeout
)
386 /* Add trailing banner as needed */
387 if (cupsdTimeoutJob(job
))
391 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
392 job
->state_value
= IPP_JOB_PENDING
;
394 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
395 IPP_TAG_KEYWORD
)) == NULL
)
396 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
400 attr
->value_tag
= IPP_TAG_KEYWORD
;
401 cupsdSetString(&(attr
->values
[0].string
.text
), "no-hold");
403 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
408 * Start pending jobs if the destination is available...
411 if (job
->state_value
== IPP_JOB_PENDING
&& !NeedReload
&& !Sleeping
)
413 printer
= cupsdFindDest(job
->dest
);
417 (printer
->type
& (CUPS_PRINTER_IMPLICIT
| CUPS_PRINTER_CLASS
)))
420 * If the class is remote, just pass it to the remote server...
425 if (pclass
->state
== IPP_PRINTER_STOPPED
)
427 else if (pclass
->type
& CUPS_PRINTER_REMOTE
)
430 printer
= cupsdFindAvailablePrinter(printer
->name
);
433 if (!printer
&& !pclass
)
436 * Whoa, the printer and/or class for this destination went away;
440 cupsdLogJob(job
, CUPSD_LOG_WARN
,
441 "Printer/class %s has gone away; canceling job!",
444 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
445 "Job canceled because the destination printer/class has "
448 cupsdCancelJob(job
, 1, IPP_JOB_ABORTED
);
453 * See if the printer is available or remote and not printing a job;
454 * if so, start the job...
460 * Add/update a job-actual-printer-uri attribute for this job
461 * so that we know which printer actually printed the job...
464 if ((attr
= ippFindAttribute(job
->attrs
, "job-actual-printer-uri",
465 IPP_TAG_URI
)) != NULL
)
466 cupsdSetString(&attr
->values
[0].string
.text
, printer
->uri
);
468 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
,
469 "job-actual-printer-uri", NULL
, printer
->uri
);
472 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
475 if ((!(printer
->type
& CUPS_PRINTER_DISCOVERED
) && /* Printer is local */
476 printer
->state
== IPP_PRINTER_IDLE
) || /* and idle */
477 ((printer
->type
& CUPS_PRINTER_DISCOVERED
) && /* Printer is remote */
478 !printer
->job
)) /* and not printing */
481 * Clear any message and reasons for the queue...
484 printer
->state_message
[0] = '\0';
486 cupsdSetPrinterReasons(printer
, "none");
492 start_job(job
, printer
);
501 * 'cupsdCleanJobs()' - Clean out old jobs.
507 cupsd_job_t
*job
; /* Current job */
513 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
514 job
&& cupsArrayCount(Jobs
) >= MaxJobs
;
515 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
516 if (job
->state_value
>= IPP_JOB_CANCELED
)
517 cupsdCancelJob(job
, 1, IPP_JOB_CANCELED
);
522 * 'cupsdDeleteJob()' - Free all memory used by a job.
526 cupsdDeleteJob(cupsd_job_t
*job
) /* I - Job */
528 cupsdClearString(&job
->username
);
529 cupsdClearString(&job
->dest
);
530 cupsdClearString(&job
->auth_username
);
531 cupsdClearString(&job
->auth_domain
);
532 cupsdClearString(&job
->auth_password
);
536 * Destroy the credential cache and clear the KRB5CCNAME env var string.
541 krb5_cc_destroy(KerberosContext
, job
->ccache
);
545 cupsdClearString(&job
->ccname
);
546 #endif /* HAVE_GSSAPI */
548 if (job
->num_files
> 0)
550 free(job
->compressions
);
551 free(job
->filetypes
);
554 ippDelete(job
->attrs
);
561 * 'cupsdFinishJob()' - Finish a job.
565 cupsdFinishJob(cupsd_job_t
*job
) /* I - Job */
567 cupsd_printer_t
*printer
; /* Current printer */
568 ipp_attribute_t
*attr
; /* job-hold-until attribute */
571 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "File %d is complete.",
572 job
->current_file
- 1);
574 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
575 "cupsdFinishJob: job->status is %d",
578 if (job
->status_buffer
&&
579 (job
->status
< 0 || job
->current_file
>= job
->num_files
))
582 * Close the pipe and clear the input bit.
585 cupsdRemoveSelect(job
->status_buffer
->fd
);
587 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
588 "cupsdFinishJob: Closing status pipes [ %d %d ]...",
589 job
->status_pipes
[0], job
->status_pipes
[1]);
591 cupsdClosePipe(job
->status_pipes
);
592 cupsdStatBufDelete(job
->status_buffer
);
594 job
->status_buffer
= NULL
;
597 printer
= job
->printer
;
599 update_job_attrs(job
, 0);
604 * Backend had errors; stop it...
607 int exit_code
; /* Exit code from backend */
611 * Convert the status to an exit code. Due to the way the W* macros are
612 * implemented on MacOS X (bug?), we have to store the exit status in a
613 * variable first and then convert...
616 exit_code
= -job
->status
;
617 if (WIFEXITED(exit_code
))
618 exit_code
= WEXITSTATUS(exit_code
);
620 exit_code
= job
->status
;
622 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Backend returned status %d (%s)",
624 exit_code
== CUPS_BACKEND_FAILED
? "failed" :
625 exit_code
== CUPS_BACKEND_AUTH_REQUIRED
?
626 "authentication required" :
627 exit_code
== CUPS_BACKEND_HOLD
? "hold job" :
628 exit_code
== CUPS_BACKEND_STOP
? "stop printer" :
629 exit_code
== CUPS_BACKEND_CANCEL
? "cancel job" :
630 exit_code
< 0 ? "crashed" : "unknown");
633 * Do what needs to be done...
639 case CUPS_BACKEND_FAILED
:
641 * Backend failure, use the error-policy to determine how to
645 cupsdStopJob(job
, 0);
647 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
650 * Mark the job as pending again - we'll retry on another
654 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
655 job
->state_value
= IPP_JOB_PENDING
;
659 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
662 * If the job was queued to a class or the error policy is
663 * "retry-current-job", try requeuing it... For faxes and retry-job
664 * queues, hold the current job for 5 minutes.
667 if ((job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
)) ||
668 !strcmp(printer
->error_policy
, "retry-current-job"))
670 else if ((printer
->type
& CUPS_PRINTER_FAX
) ||
671 !strcmp(printer
->error_policy
, "retry-job"))
674 * See how many times we've tried to send the job; if more than
675 * the limit, cancel the job.
680 if (job
->tries
>= JobRetryLimit
)
686 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
687 "Canceling job since it could not be "
688 "sent after %d tries.",
691 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
696 * Try again in N seconds...
699 set_hold_until(job
, time(NULL
) + JobRetryInterval
);
701 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
,
702 "Job held due to fax errors; please consult "
703 "the error_log file for details.");
704 cupsdSetPrinterState(printer
, IPP_PRINTER_IDLE
, 0);
707 else if (!strcmp(printer
->error_policy
, "abort-job"))
708 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
711 cupsdSetPrinterState(printer
, IPP_PRINTER_STOPPED
, 1);
713 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
714 "Job stopped due to backend errors; please consult "
715 "the error_log file for details.");
719 case CUPS_BACKEND_CANCEL
:
724 cupsdCancelJob(job
, 0, IPP_JOB_CANCELED
);
727 case CUPS_BACKEND_HOLD
:
732 cupsdStopJob(job
, 0);
734 cupsdSetJobHoldUntil(job
, "indefinite");
736 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
737 job
->state_value
= IPP_JOB_HELD
;
740 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
742 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
743 "Job held due to backend errors; please consult "
744 "the error_log file for details.");
747 case CUPS_BACKEND_STOP
:
749 * Stop the printer...
752 cupsdStopJob(job
, 0);
754 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
755 job
->state_value
= IPP_JOB_PENDING
;
758 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
760 cupsdSetPrinterState(printer
, IPP_PRINTER_STOPPED
, 1);
762 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
763 "Job stopped due to backend errors; please consult "
764 "the error_log file for details.");
767 case CUPS_BACKEND_AUTH_REQUIRED
:
768 cupsdStopJob(job
, 0);
770 cupsdSetJobHoldUntil(job
, "auth-info-required");
772 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
773 IPP_TAG_KEYWORD
)) == NULL
)
774 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
778 attr
->value_tag
= IPP_TAG_KEYWORD
;
779 cupsdSetString(&(attr
->values
[0].string
.text
),
780 "auth-info-required");
783 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
784 job
->state_value
= IPP_JOB_HELD
;
787 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
789 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
790 "Authentication is required for job %d.", job
->id
);
795 * Try printing another job...
800 else if (job
->status
> 0)
803 * Filter had errors; stop job...
806 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Job stopped due to filter errors.");
807 cupsdStopJob(job
, 1);
809 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
810 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
811 "Job stopped due to filter errors; please consult the "
812 "error_log file for details.");
818 * Job printed successfully; cancel it...
821 if (job
->current_file
< job
->num_files
)
824 * Start the next file in the job...
827 FilterLevel
-= job
->cost
;
828 start_job(job
, printer
);
833 * Close out this job...
836 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Completed successfully.");
837 cupsdCancelJob(job
, 0, IPP_JOB_COMPLETED
);
845 * 'cupsdFreeAllJobs()' - Free all jobs from memory.
849 cupsdFreeAllJobs(void)
851 cupsd_job_t
*job
; /* Current job */
862 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
864 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
866 cupsArrayRemove(Jobs
, job
);
867 cupsArrayRemove(ActiveJobs
, job
);
868 cupsArrayRemove(PrintingJobs
, job
);
873 cupsdReleaseSignals();
878 * 'cupsdFindJob()' - Find the specified job.
881 cupsd_job_t
* /* O - Job data */
882 cupsdFindJob(int id
) /* I - Job ID */
884 cupsd_job_t key
; /* Search key */
889 return ((cupsd_job_t
*)cupsArrayFind(Jobs
, &key
));
894 * 'cupsdGetPrinterJobCount()' - Get the number of pending, processing,
895 * or held jobs in a printer or class.
898 int /* O - Job count */
899 cupsdGetPrinterJobCount(
900 const char *dest
) /* I - Printer or class name */
902 int count
; /* Job count */
903 cupsd_job_t
*job
; /* Current job */
906 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
908 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
909 if (job
->dest
&& !strcasecmp(job
->dest
, dest
))
917 * 'cupsdGetUserJobCount()' - Get the number of pending, processing,
918 * or held jobs for a user.
921 int /* O - Job count */
922 cupsdGetUserJobCount(
923 const char *username
) /* I - Username */
925 int count
; /* Job count */
926 cupsd_job_t
*job
; /* Current job */
929 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
931 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
932 if (!strcasecmp(job
->username
, username
))
940 * 'cupsdHoldJob()' - Hold the specified job.
944 cupsdHoldJob(cupsd_job_t
*job
) /* I - Job data */
946 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdHoldJob: id = %d", job
->id
);
948 if (job
->state_value
== IPP_JOB_PROCESSING
)
949 cupsdStopJob(job
, 0);
953 DEBUG_puts("cupsdHoldJob: setting state to held...");
955 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
956 job
->state_value
= IPP_JOB_HELD
;
957 job
->current_file
= 0;
960 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
965 * 'cupsdLoadAllJobs()' - Load all jobs from disk.
969 cupsdLoadAllJobs(void)
971 char filename
[1024]; /* Full filename of job.cache file */
972 struct stat fileinfo
, /* Information on job.cache file */
973 dirinfo
; /* Information on RequestRoot dir */
978 * Create the job arrays as needed...
982 Jobs
= cupsArrayNew(compare_jobs
, NULL
);
985 ActiveJobs
= cupsArrayNew(compare_active_jobs
, NULL
);
988 PrintingJobs
= cupsArrayNew(compare_jobs
, NULL
);
991 * See whether the job.cache file is older than the RequestRoot directory...
994 snprintf(filename
, sizeof(filename
), "%s/job.cache", CacheDir
);
996 if (stat(filename
, &fileinfo
))
998 fileinfo
.st_mtime
= 0;
1000 if (errno
!= ENOENT
)
1001 cupsdLogMessage(CUPSD_LOG_ERROR
,
1002 "Unable to get file information for \"%s\" - %s",
1003 filename
, strerror(errno
));
1006 if (stat(RequestRoot
, &dirinfo
))
1008 dirinfo
.st_mtime
= 0;
1010 if (errno
!= ENOENT
)
1011 cupsdLogMessage(CUPSD_LOG_ERROR
,
1012 "Unable to get directory information for \"%s\" - %s",
1013 RequestRoot
, strerror(errno
));
1017 * Load the most recent source for job data...
1020 if (dirinfo
.st_mtime
> fileinfo
.st_mtime
)
1022 load_request_root();
1024 load_next_job_id(filename
);
1027 load_job_cache(filename
);
1030 * Clean out old jobs as needed...
1033 if (MaxJobs
> 0 && cupsArrayCount(Jobs
) >= MaxJobs
)
1039 * 'cupsdLoadJob()' - Load a single job...
1043 cupsdLoadJob(cupsd_job_t
*job
) /* I - Job */
1045 char jobfile
[1024]; /* Job filename */
1046 cups_file_t
*fp
; /* Job file */
1047 int fileid
; /* Current file ID */
1048 ipp_attribute_t
*attr
; /* Job attribute */
1049 const char *dest
; /* Destination name */
1050 cupsd_printer_t
*destptr
; /* Pointer to destination */
1051 mime_type_t
**filetypes
; /* New filetypes array */
1052 int *compressions
; /* New compressions array */
1057 if (job
->state_value
> IPP_JOB_STOPPED
)
1058 job
->access_time
= time(NULL
);
1063 if ((job
->attrs
= ippNew()) == NULL
)
1065 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Ran out of memory for job attributes!");
1070 * Load job attributes...
1073 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Loading attributes...");
1075 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, job
->id
);
1076 if ((fp
= cupsFileOpen(jobfile
, "r")) == NULL
)
1078 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1079 "Unable to open job control file \"%s\" - %s!",
1080 jobfile
, strerror(errno
));
1081 ippDelete(job
->attrs
);
1086 if (ippReadIO(fp
, (ipp_iocb_t
)cupsFileRead
, 1, NULL
, job
->attrs
) != IPP_DATA
)
1088 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1089 "Unable to read job control file \"%s\"!",
1092 ippDelete(job
->attrs
);
1101 * Copy attribute data to the job object...
1104 if ((job
->state
= ippFindAttribute(job
->attrs
, "job-state",
1105 IPP_TAG_ENUM
)) == NULL
)
1107 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1108 "Missing or bad job-state attribute in control file!");
1109 ippDelete(job
->attrs
);
1115 job
->state_value
= (ipp_jstate_t
)job
->state
->values
[0].integer
;
1119 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1120 IPP_TAG_URI
)) == NULL
)
1122 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1123 "No job-printer-uri attribute in control file!");
1124 ippDelete(job
->attrs
);
1130 if ((dest
= cupsdValidateDest(attr
->values
[0].string
.text
, &(job
->dtype
),
1133 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1134 "Unable to queue job for destination \"%s\"!",
1135 attr
->values
[0].string
.text
);
1136 ippDelete(job
->attrs
);
1142 cupsdSetString(&job
->dest
, dest
);
1144 else if ((destptr
= cupsdFindDest(job
->dest
)) == NULL
)
1146 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1147 "Unable to queue job for destination \"%s\"!", job
->dest
);
1148 ippDelete(job
->attrs
);
1154 job
->sheets
= ippFindAttribute(job
->attrs
, "job-media-sheets-completed",
1156 job
->job_sheets
= ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_NAME
);
1160 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
1161 IPP_TAG_INTEGER
)) == NULL
)
1163 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1164 "Missing or bad job-priority attribute in control file!");
1165 ippDelete(job
->attrs
);
1171 job
->priority
= attr
->values
[0].integer
;
1176 if ((attr
= ippFindAttribute(job
->attrs
, "job-originating-user-name",
1177 IPP_TAG_NAME
)) == NULL
)
1179 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1180 "Missing or bad job-originating-user-name attribute in "
1182 ippDelete(job
->attrs
);
1188 cupsdSetString(&job
->username
, attr
->values
[0].string
.text
);
1192 * Set the job hold-until time and state...
1195 if (job
->state_value
== IPP_JOB_HELD
)
1197 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1198 IPP_TAG_KEYWORD
)) == NULL
)
1199 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1202 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
1205 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1206 job
->state_value
= IPP_JOB_PENDING
;
1209 else if (job
->state_value
== IPP_JOB_PROCESSING
)
1211 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1212 job
->state_value
= IPP_JOB_PENDING
;
1215 if (!job
->num_files
)
1218 * Find all the d##### files...
1221 for (fileid
= 1; fileid
< 10000; fileid
++)
1223 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
1226 if (access(jobfile
, 0))
1229 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
1230 "Auto-typing document file \"%s\"...", jobfile
);
1232 if (fileid
> job
->num_files
)
1234 if (job
->num_files
== 0)
1236 compressions
= (int *)calloc(fileid
, sizeof(int));
1237 filetypes
= (mime_type_t
**)calloc(fileid
, sizeof(mime_type_t
*));
1241 compressions
= (int *)realloc(job
->compressions
,
1242 sizeof(int) * fileid
);
1243 filetypes
= (mime_type_t
**)realloc(job
->filetypes
,
1244 sizeof(mime_type_t
*) *
1248 if (!compressions
|| !filetypes
)
1250 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1251 "Ran out of memory for job file types!");
1255 job
->compressions
= compressions
;
1256 job
->filetypes
= filetypes
;
1257 job
->num_files
= fileid
;
1260 job
->filetypes
[fileid
- 1] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
1261 job
->compressions
+ fileid
- 1);
1263 if (!job
->filetypes
[fileid
- 1])
1264 job
->filetypes
[fileid
- 1] = mimeType(MimeDatabase
, "application",
1270 * Load authentication information as needed...
1273 if (job
->state_value
< IPP_JOB_STOPPED
)
1275 snprintf(jobfile
, sizeof(jobfile
), "%s/a%05d", RequestRoot
, job
->id
);
1277 cupsdClearString(&job
->auth_username
);
1278 cupsdClearString(&job
->auth_domain
);
1279 cupsdClearString(&job
->auth_password
);
1281 if ((fp
= cupsFileOpen(jobfile
, "r")) != NULL
)
1283 int i
, /* Looping var */
1284 bytes
; /* Size of auth data */
1285 char line
[255], /* Line from file */
1286 data
[255]; /* Decoded data */
1290 i
< destptr
->num_auth_info_required
&&
1291 cupsFileGets(fp
, line
, sizeof(line
));
1294 bytes
= sizeof(data
);
1295 httpDecode64_2(data
, &bytes
, line
);
1297 if (!strcmp(destptr
->auth_info_required
[i
], "username"))
1298 cupsdSetStringf(&job
->auth_username
, "AUTH_USERNAME=%s", data
);
1299 else if (!strcmp(destptr
->auth_info_required
[i
], "domain"))
1300 cupsdSetStringf(&job
->auth_domain
, "AUTH_DOMAIN=%s", data
);
1301 else if (!strcmp(destptr
->auth_info_required
[i
], "password"))
1302 cupsdSetStringf(&job
->auth_password
, "AUTH_PASSWORD=%s", data
);
1308 job
->access_time
= time(NULL
);
1313 * 'cupsdMoveJob()' - Move the specified job to a different destination.
1317 cupsdMoveJob(cupsd_job_t
*job
, /* I - Job */
1318 cupsd_printer_t
*p
) /* I - Destination printer or class */
1320 ipp_attribute_t
*attr
; /* job-printer-uri attribute */
1321 const char *olddest
; /* Old destination */
1322 cupsd_printer_t
*oldp
; /* Old pointer */
1326 * Don't move completed jobs...
1329 if (job
->state_value
> IPP_JOB_STOPPED
)
1333 * Get the old destination...
1336 olddest
= job
->dest
;
1339 oldp
= job
->printer
;
1341 oldp
= cupsdFindDest(olddest
);
1344 * Change the destination information...
1347 if (job
->state_value
== IPP_JOB_PROCESSING
)
1348 cupsdStopJob(job
, 0);
1352 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, oldp
, job
,
1353 "Job #%d moved from %s to %s.", job
->id
, olddest
,
1356 cupsdSetString(&job
->dest
, p
->name
);
1357 job
->dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_REMOTE
|
1358 CUPS_PRINTER_IMPLICIT
);
1360 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1361 IPP_TAG_URI
)) != NULL
)
1362 cupsdSetString(&(attr
->values
[0].string
.text
), p
->uri
);
1364 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, p
, job
,
1365 "Job #%d moved from %s to %s.", job
->id
, olddest
,
1369 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1374 * 'cupsdReleaseJob()' - Release the specified job.
1378 cupsdReleaseJob(cupsd_job_t
*job
) /* I - Job */
1380 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdReleaseJob: id = %d", job
->id
);
1382 if (job
->state_value
== IPP_JOB_HELD
)
1385 * Add trailing banner as needed...
1388 if (job
->pending_timeout
)
1389 cupsdTimeoutJob(job
);
1391 DEBUG_puts("cupsdReleaseJob: setting state to pending...");
1393 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1394 job
->state_value
= IPP_JOB_PENDING
;
1396 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1402 * 'cupsdRestartJob()' - Restart the specified job.
1406 cupsdRestartJob(cupsd_job_t
*job
) /* I - Job */
1408 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdRestartJob: id = %d", job
->id
);
1410 if (job
->state_value
== IPP_JOB_STOPPED
|| job
->num_files
)
1412 ipp_jstate_t old_state
; /* Old job state */
1417 old_state
= job
->state_value
;
1420 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1421 job
->state_value
= IPP_JOB_PENDING
;
1424 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1426 if (old_state
> IPP_JOB_STOPPED
)
1427 cupsArrayAdd(ActiveJobs
, job
);
1433 * 'cupsdSaveAllJobs()' - Save a summary of all jobs to disk.
1437 cupsdSaveAllJobs(void)
1439 int i
; /* Looping var */
1440 cups_file_t
*fp
; /* Job cache file */
1441 char temp
[1024]; /* Temporary string */
1442 cupsd_job_t
*job
; /* Current job */
1443 time_t curtime
; /* Current time */
1444 struct tm
*curdate
; /* Current date */
1447 snprintf(temp
, sizeof(temp
), "%s/job.cache", CacheDir
);
1448 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
1450 cupsdLogMessage(CUPSD_LOG_ERROR
,
1451 "Unable to create job cache file \"%s\" - %s",
1452 temp
, strerror(errno
));
1456 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving job cache file \"%s\"...", temp
);
1459 * Restrict access to the file...
1462 fchown(cupsFileNumber(fp
), getuid(), Group
);
1463 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
1466 * Write a small header to the file...
1469 curtime
= time(NULL
);
1470 curdate
= localtime(&curtime
);
1471 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
1473 cupsFilePuts(fp
, "# Job cache file for " CUPS_SVERSION
"\n");
1474 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
1475 cupsFilePrintf(fp
, "NextJobId %d\n", NextJobId
);
1478 * Write each job known to the system...
1481 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1483 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1485 cupsFilePrintf(fp
, "<Job %d>\n", job
->id
);
1486 cupsFilePrintf(fp
, "State %d\n", job
->state_value
);
1487 cupsFilePrintf(fp
, "Priority %d\n", job
->priority
);
1488 cupsFilePrintf(fp
, "Username %s\n", job
->username
);
1489 cupsFilePrintf(fp
, "Destination %s\n", job
->dest
);
1490 cupsFilePrintf(fp
, "DestType %d\n", job
->dtype
);
1491 cupsFilePrintf(fp
, "NumFiles %d\n", job
->num_files
);
1492 for (i
= 0; i
< job
->num_files
; i
++)
1493 cupsFilePrintf(fp
, "File %d %s/%s %d\n", i
+ 1, job
->filetypes
[i
]->super
,
1494 job
->filetypes
[i
]->type
, job
->compressions
[i
]);
1495 cupsFilePuts(fp
, "</Job>\n");
1503 * 'cupsdSaveJob()' - Save a job to disk.
1507 cupsdSaveJob(cupsd_job_t
*job
) /* I - Job */
1509 char filename
[1024]; /* Job control filename */
1510 cups_file_t
*fp
; /* Job file */
1513 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSaveJob(job=%p(%d)): job->attrs=%p",
1514 job
, job
->id
, job
->attrs
);
1516 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
, job
->id
);
1518 if ((fp
= cupsFileOpen(filename
, "w")) == NULL
)
1520 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1521 "Unable to create job control file \"%s\" - %s.",
1522 filename
, strerror(errno
));
1526 fchmod(cupsFileNumber(fp
), 0600);
1527 fchown(cupsFileNumber(fp
), RunUser
, Group
);
1529 job
->attrs
->state
= IPP_IDLE
;
1531 if (ippWriteIO(fp
, (ipp_iocb_t
)cupsFileWrite
, 1, NULL
,
1532 job
->attrs
) != IPP_DATA
)
1533 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Unable to write job control file!");
1542 * 'cupsdSetJobHoldUntil()' - Set the hold time for a job...
1546 cupsdSetJobHoldUntil(cupsd_job_t
*job
, /* I - Job */
1547 const char *when
) /* I - When to resume */
1549 time_t curtime
; /* Current time */
1550 struct tm
*curdate
; /* Current date */
1551 int hour
; /* Hold hour */
1552 int minute
; /* Hold minute */
1553 int second
; /* Hold second */
1556 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetJobHoldUntil(%d, \"%s\")",
1561 if (!strcmp(when
, "indefinite") || !strcmp(when
, "auth-info-required"))
1564 * Hold indefinitely...
1567 job
->hold_until
= 0;
1569 else if (!strcmp(when
, "day-time"))
1572 * Hold to 6am the next morning unless local time is < 6pm.
1575 curtime
= time(NULL
);
1576 curdate
= localtime(&curtime
);
1578 if (curdate
->tm_hour
< 18)
1579 job
->hold_until
= curtime
;
1581 job
->hold_until
= curtime
+
1582 ((29 - curdate
->tm_hour
) * 60 + 59 -
1583 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1585 else if (!strcmp(when
, "evening") || !strcmp(when
, "night"))
1588 * Hold to 6pm unless local time is > 6pm or < 6am.
1591 curtime
= time(NULL
);
1592 curdate
= localtime(&curtime
);
1594 if (curdate
->tm_hour
< 6 || curdate
->tm_hour
>= 18)
1595 job
->hold_until
= curtime
;
1597 job
->hold_until
= curtime
+
1598 ((17 - curdate
->tm_hour
) * 60 + 59 -
1599 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1601 else if (!strcmp(when
, "second-shift"))
1604 * Hold to 4pm unless local time is > 4pm.
1607 curtime
= time(NULL
);
1608 curdate
= localtime(&curtime
);
1610 if (curdate
->tm_hour
>= 16)
1611 job
->hold_until
= curtime
;
1613 job
->hold_until
= curtime
+
1614 ((15 - curdate
->tm_hour
) * 60 + 59 -
1615 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1617 else if (!strcmp(when
, "third-shift"))
1620 * Hold to 12am unless local time is < 8am.
1623 curtime
= time(NULL
);
1624 curdate
= localtime(&curtime
);
1626 if (curdate
->tm_hour
< 8)
1627 job
->hold_until
= curtime
;
1629 job
->hold_until
= curtime
+
1630 ((23 - curdate
->tm_hour
) * 60 + 59 -
1631 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1633 else if (!strcmp(when
, "weekend"))
1636 * Hold to weekend unless we are in the weekend.
1639 curtime
= time(NULL
);
1640 curdate
= localtime(&curtime
);
1642 if (curdate
->tm_wday
|| curdate
->tm_wday
== 6)
1643 job
->hold_until
= curtime
;
1645 job
->hold_until
= curtime
+
1646 (((5 - curdate
->tm_wday
) * 24 +
1647 (17 - curdate
->tm_hour
)) * 60 + 59 -
1648 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1650 else if (sscanf(when
, "%d:%d:%d", &hour
, &minute
, &second
) >= 2)
1653 * Hold to specified GMT time (HH:MM or HH:MM:SS)...
1656 curtime
= time(NULL
);
1657 curdate
= gmtime(&curtime
);
1659 job
->hold_until
= curtime
+
1660 ((hour
- curdate
->tm_hour
) * 60 + minute
-
1661 curdate
->tm_min
) * 60 + second
- curdate
->tm_sec
;
1664 * Hold until next day as needed...
1667 if (job
->hold_until
< curtime
)
1668 job
->hold_until
+= 24 * 60 * 60;
1671 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetJobHoldUntil: hold_until = %d",
1672 (int)job
->hold_until
);
1677 * 'cupsdSetJobPriority()' - Set the priority of a job, moving it up/down in
1678 * the list as needed.
1682 cupsdSetJobPriority(
1683 cupsd_job_t
*job
, /* I - Job ID */
1684 int priority
) /* I - New priority (0 to 100) */
1686 ipp_attribute_t
*attr
; /* Job attribute */
1690 * Don't change completed jobs...
1693 if (job
->state_value
>= IPP_JOB_PROCESSING
)
1697 * Set the new priority and re-add the job into the active list...
1700 cupsArrayRemove(ActiveJobs
, job
);
1702 job
->priority
= priority
;
1704 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
1705 IPP_TAG_INTEGER
)) != NULL
)
1706 attr
->values
[0].integer
= priority
;
1708 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-priority",
1711 cupsArrayAdd(ActiveJobs
, job
);
1714 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1719 * 'cupsdStopAllJobs()' - Stop all print jobs.
1723 cupsdStopAllJobs(int force
) /* I - 1 = Force all filters to stop */
1725 cupsd_job_t
*job
; /* Current job */
1728 DEBUG_puts("cupsdStopAllJobs()");
1730 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
1732 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
1733 if (job
->state_value
== IPP_JOB_PROCESSING
)
1735 cupsdStopJob(job
, force
);
1736 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1737 job
->state_value
= IPP_JOB_PENDING
;
1743 * 'cupsdStopJob()' - Stop a print job.
1747 cupsdStopJob(cupsd_job_t
*job
, /* I - Job */
1748 int force
) /* I - 1 = Force all filters to stop */
1750 int i
; /* Looping var */
1753 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "cupsdStopJob: force = %d", force
);
1755 if (job
->state_value
!= IPP_JOB_PROCESSING
)
1758 FilterLevel
-= job
->cost
;
1760 if (job
->printer
->state
== IPP_PRINTER_PROCESSING
)
1761 cupsdSetPrinterState(job
->printer
, IPP_PRINTER_IDLE
, 0);
1763 job
->state
->values
[0].integer
= IPP_JOB_STOPPED
;
1764 job
->state_value
= IPP_JOB_STOPPED
;
1765 job
->printer
->job
= NULL
;
1766 job
->printer
= NULL
;
1768 job
->current_file
--;
1770 for (i
= 0; job
->filters
[i
]; i
++)
1771 if (job
->filters
[i
] > 0)
1773 cupsdEndProcess(job
->filters
[i
], force
);
1774 job
->filters
[i
] = 0;
1777 if (job
->backend
> 0)
1779 cupsdEndProcess(job
->backend
, force
);
1783 cupsdDestroyProfile(job
->profile
);
1784 job
->profile
= NULL
;
1786 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "Closing print pipes [ %d %d ]...",
1787 job
->print_pipes
[0], job
->print_pipes
[1]);
1789 cupsdClosePipe(job
->print_pipes
);
1791 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "Closing back pipes [ %d %d ]...",
1792 job
->back_pipes
[0], job
->back_pipes
[1]);
1794 cupsdClosePipe(job
->back_pipes
);
1796 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "Closing side pipes [ %d %d ]...",
1797 job
->side_pipes
[0], job
->side_pipes
[1]);
1799 cupsdClosePipe(job
->side_pipes
);
1801 if (job
->status_buffer
)
1804 * Close the pipe and clear the input bit.
1807 cupsdRemoveSelect(job
->status_buffer
->fd
);
1809 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "Closing status pipes [ %d %d ]...",
1810 job
->status_pipes
[0], job
->status_pipes
[1]);
1812 cupsdClosePipe(job
->status_pipes
);
1813 cupsdStatBufDelete(job
->status_buffer
);
1815 job
->status_buffer
= NULL
;
1821 * 'cupsdUnloadCompletedJobs()' - Flush completed job history from memory.
1825 cupsdUnloadCompletedJobs(void)
1827 cupsd_job_t
*job
; /* Current job */
1828 time_t expire
; /* Expiration time */
1831 expire
= time(NULL
) - 60;
1833 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1835 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1836 if (job
->attrs
&& job
->state_value
>= IPP_JOB_STOPPED
&&
1837 job
->access_time
< expire
)
1848 * 'compare_active_jobs()' - Compare the job IDs and priorities of two jobs.
1851 static int /* O - Difference */
1852 compare_active_jobs(void *first
, /* I - First job */
1853 void *second
, /* I - Second job */
1854 void *data
) /* I - App data (not used) */
1856 int diff
; /* Difference */
1859 if ((diff
= ((cupsd_job_t
*)second
)->priority
-
1860 ((cupsd_job_t
*)first
)->priority
) != 0)
1863 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
1868 * 'compare_jobs()' - Compare the job IDs of two jobs.
1871 static int /* O - Difference */
1872 compare_jobs(void *first
, /* I - First job */
1873 void *second
, /* I - Second job */
1874 void *data
) /* I - App data (not used) */
1876 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
1881 * 'ipp_length()' - Compute the size of the buffer needed to hold
1882 * the textual IPP attributes.
1885 static int /* O - Size of attribute buffer */
1886 ipp_length(ipp_t
*ipp
) /* I - IPP request */
1888 int bytes
; /* Number of bytes */
1889 int i
; /* Looping var */
1890 ipp_attribute_t
*attr
; /* Current attribute */
1894 * Loop through all attributes...
1899 for (attr
= ipp
->attrs
; attr
!= NULL
; attr
= attr
->next
)
1902 * Skip attributes that won't be sent to filters...
1905 if (attr
->value_tag
== IPP_TAG_MIMETYPE
||
1906 attr
->value_tag
== IPP_TAG_NAMELANG
||
1907 attr
->value_tag
== IPP_TAG_TEXTLANG
||
1908 attr
->value_tag
== IPP_TAG_URI
||
1909 attr
->value_tag
== IPP_TAG_URISCHEME
)
1912 if (strncmp(attr
->name
, "time-", 5) == 0)
1916 * Add space for a leading space and commas between each value.
1917 * For the first attribute, the leading space isn't used, so the
1918 * extra byte can be used as the nul terminator...
1921 bytes
++; /* " " separator */
1922 bytes
+= attr
->num_values
; /* "," separators */
1925 * Boolean attributes appear as "foo,nofoo,foo,nofoo", while
1926 * other attributes appear as "foo=value1,value2,...,valueN".
1929 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
1930 bytes
+= strlen(attr
->name
);
1932 bytes
+= attr
->num_values
* strlen(attr
->name
);
1935 * Now add the size required for each value in the attribute...
1938 switch (attr
->value_tag
)
1940 case IPP_TAG_INTEGER
:
1943 * Minimum value of a signed integer is -2147483647, or 11 digits.
1946 bytes
+= attr
->num_values
* 11;
1949 case IPP_TAG_BOOLEAN
:
1951 * Add two bytes for each false ("no") value...
1954 for (i
= 0; i
< attr
->num_values
; i
++)
1955 if (!attr
->values
[i
].boolean
)
1959 case IPP_TAG_RANGE
:
1961 * A range is two signed integers separated by a hyphen, or
1962 * 23 characters max.
1965 bytes
+= attr
->num_values
* 23;
1968 case IPP_TAG_RESOLUTION
:
1970 * A resolution is two signed integers separated by an "x" and
1971 * suffixed by the units, or 26 characters max.
1974 bytes
+= attr
->num_values
* 26;
1977 case IPP_TAG_STRING
:
1980 case IPP_TAG_KEYWORD
:
1981 case IPP_TAG_CHARSET
:
1982 case IPP_TAG_LANGUAGE
:
1985 * Strings can contain characters that need quoting. We need
1986 * at least 2 * len + 2 characters to cover the quotes and
1987 * any backslashes in the string.
1990 for (i
= 0; i
< attr
->num_values
; i
++)
1991 bytes
+= 2 * strlen(attr
->values
[i
].string
.text
) + 2;
1995 break; /* anti-compiler-warning-code */
2004 * 'load_job_cache()' - Load jobs from the job.cache file.
2008 load_job_cache(const char *filename
) /* I - job.cache filename */
2010 cups_file_t
*fp
; /* job.cache file */
2011 char line
[1024], /* Line buffer */
2012 *value
; /* Value on line */
2013 int linenum
; /* Line number in file */
2014 cupsd_job_t
*job
; /* Current job */
2015 int jobid
; /* Job ID */
2016 char jobfile
[1024]; /* Job filename */
2020 * Open the job.cache file...
2023 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
2025 if (errno
!= ENOENT
)
2026 cupsdLogMessage(CUPSD_LOG_ERROR
,
2027 "Unable to open job cache file \"%s\": %s",
2028 filename
, strerror(errno
));
2030 load_request_root();
2036 * Read entries from the job cache file and create jobs as needed.
2039 cupsdLogMessage(CUPSD_LOG_INFO
, "Loading job cache file \"%s\"...",
2045 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
2047 if (!strcasecmp(line
, "NextJobId"))
2050 NextJobId
= atoi(value
);
2052 else if (!strcasecmp(line
, "<Job"))
2056 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing </Job> directive on line %d!",
2063 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing job ID on line %d!", linenum
);
2067 jobid
= atoi(value
);
2071 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad job ID %d on line %d!", jobid
,
2076 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, jobid
);
2077 if (access(jobfile
, 0))
2079 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Job %d] Files have gone away!",
2084 job
= calloc(1, sizeof(cupsd_job_t
));
2087 cupsdLogMessage(CUPSD_LOG_EMERG
,
2088 "[Job %d] Unable to allocate memory for job!", jobid
);
2093 job
->back_pipes
[0] = -1;
2094 job
->back_pipes
[1] = -1;
2095 job
->print_pipes
[0] = -1;
2096 job
->print_pipes
[1] = -1;
2097 job
->side_pipes
[0] = -1;
2098 job
->side_pipes
[1] = -1;
2099 job
->status_pipes
[0] = -1;
2100 job
->status_pipes
[1] = -1;
2102 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Loading from cache...");
2106 cupsdLogMessage(CUPSD_LOG_ERROR
,
2107 "Missing <Job #> directive on line %d!", linenum
);
2110 else if (!strcasecmp(line
, "</Job>"))
2112 cupsArrayAdd(Jobs
, job
);
2114 if (job
->state_value
<= IPP_JOB_STOPPED
)
2116 cupsArrayAdd(ActiveJobs
, job
);
2124 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing value on line %d!", linenum
);
2127 else if (!strcasecmp(line
, "State"))
2129 job
->state_value
= (ipp_jstate_t
)atoi(value
);
2131 if (job
->state_value
< IPP_JOB_PENDING
)
2132 job
->state_value
= IPP_JOB_PENDING
;
2133 else if (job
->state_value
> IPP_JOB_COMPLETED
)
2134 job
->state_value
= IPP_JOB_COMPLETED
;
2136 else if (!strcasecmp(line
, "Priority"))
2138 job
->priority
= atoi(value
);
2140 else if (!strcasecmp(line
, "Username"))
2142 cupsdSetString(&job
->username
, value
);
2144 else if (!strcasecmp(line
, "Destination"))
2146 cupsdSetString(&job
->dest
, value
);
2148 else if (!strcasecmp(line
, "DestType"))
2150 job
->dtype
= (cups_ptype_t
)atoi(value
);
2152 else if (!strcasecmp(line
, "NumFiles"))
2154 job
->num_files
= atoi(value
);
2156 if (job
->num_files
< 0)
2158 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad NumFiles value %d on line %d!",
2159 job
->num_files
, linenum
);
2164 if (job
->num_files
> 0)
2166 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-001", RequestRoot
,
2168 if (access(jobfile
, 0))
2170 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Data files have gone away!");
2175 job
->filetypes
= calloc(job
->num_files
, sizeof(mime_type_t
*));
2176 job
->compressions
= calloc(job
->num_files
, sizeof(int));
2178 if (!job
->filetypes
|| !job
->compressions
)
2180 cupsdLogJob(job
, CUPSD_LOG_EMERG
,
2181 "Unable to allocate memory for %d files!",
2187 else if (!strcasecmp(line
, "File"))
2189 int number
, /* File number */
2190 compression
; /* Compression value */
2191 char super
[MIME_MAX_SUPER
], /* MIME super type */
2192 type
[MIME_MAX_TYPE
]; /* MIME type */
2195 if (sscanf(value
, "%d%*[ \t]%15[^/]/%255s%d", &number
, super
, type
,
2198 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File on line %d!", linenum
);
2202 if (number
< 1 || number
> job
->num_files
)
2204 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File number %d on line %d!",
2211 job
->compressions
[number
] = compression
;
2212 job
->filetypes
[number
] = mimeType(MimeDatabase
, super
, type
);
2214 if (!job
->filetypes
[number
])
2217 * If the original MIME type is unknown, auto-type it!
2220 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
2221 "Unknown MIME type %s/%s for file %d!",
2222 super
, type
, number
+ 1);
2224 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
2225 job
->id
, number
+ 1);
2226 job
->filetypes
[number
] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
2227 job
->compressions
+ number
);
2230 * If that didn't work, assume it is raw...
2233 if (!job
->filetypes
[number
])
2234 job
->filetypes
[number
] = mimeType(MimeDatabase
, "application",
2239 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unknown %s directive on line %d!",
2248 * 'load_next_job_id()' - Load the NextJobId value from the job.cache file.
2252 load_next_job_id(const char *filename
) /* I - job.cache filename */
2254 cups_file_t
*fp
; /* job.cache file */
2255 char line
[1024], /* Line buffer */
2256 *value
; /* Value on line */
2257 int linenum
; /* Line number in file */
2258 int next_job_id
; /* NextJobId value from line */
2262 * Read the NextJobId directive from the job.cache file and use
2263 * the value (if any).
2266 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
2268 if (errno
!= ENOENT
)
2269 cupsdLogMessage(CUPSD_LOG_ERROR
,
2270 "Unable to open job cache file \"%s\": %s",
2271 filename
, strerror(errno
));
2276 cupsdLogMessage(CUPSD_LOG_INFO
,
2277 "Loading NextJobId from job cache file \"%s\"...", filename
);
2281 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
2283 if (!strcasecmp(line
, "NextJobId"))
2287 next_job_id
= atoi(value
);
2289 if (next_job_id
> NextJobId
)
2290 NextJobId
= next_job_id
;
2301 * 'load_request_root()' - Load jobs from the RequestRoot directory.
2305 load_request_root(void)
2307 cups_dir_t
*dir
; /* Directory */
2308 cups_dentry_t
*dent
; /* Directory entry */
2309 cupsd_job_t
*job
; /* New job */
2313 * Open the requests directory...
2316 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Scanning %s for jobs...", RequestRoot
);
2318 if ((dir
= cupsDirOpen(RequestRoot
)) == NULL
)
2320 cupsdLogMessage(CUPSD_LOG_ERROR
,
2321 "Unable to open spool directory \"%s\": %s",
2322 RequestRoot
, strerror(errno
));
2327 * Read all the c##### files...
2330 while ((dent
= cupsDirRead(dir
)) != NULL
)
2331 if (strlen(dent
->filename
) >= 6 && dent
->filename
[0] == 'c')
2334 * Allocate memory for the job...
2337 if ((job
= calloc(sizeof(cupsd_job_t
), 1)) == NULL
)
2339 cupsdLogMessage(CUPSD_LOG_ERROR
, "Ran out of memory for jobs!");
2345 * Assign the job ID...
2348 job
->id
= atoi(dent
->filename
+ 1);
2349 job
->back_pipes
[0] = -1;
2350 job
->back_pipes
[1] = -1;
2351 job
->print_pipes
[0] = -1;
2352 job
->print_pipes
[1] = -1;
2353 job
->side_pipes
[0] = -1;
2354 job
->side_pipes
[1] = -1;
2355 job
->status_pipes
[0] = -1;
2356 job
->status_pipes
[1] = -1;
2358 if (job
->id
>= NextJobId
)
2359 NextJobId
= job
->id
+ 1;
2368 * Insert the job into the array, sorting by job priority and ID...
2371 cupsArrayAdd(Jobs
, job
);
2373 if (job
->state_value
<= IPP_JOB_STOPPED
)
2374 cupsArrayAdd(ActiveJobs
, job
);
2384 * 'set_time()' - Set one of the "time-at-xyz" attributes...
2388 set_time(cupsd_job_t
*job
, /* I - Job to update */
2389 const char *name
) /* I - Name of attribute */
2391 ipp_attribute_t
*attr
; /* Time attribute */
2394 if ((attr
= ippFindAttribute(job
->attrs
, name
, IPP_TAG_ZERO
)) != NULL
)
2396 attr
->value_tag
= IPP_TAG_INTEGER
;
2397 attr
->values
[0].integer
= time(NULL
);
2403 * 'set_hold_until()' - Set the hold time and update job-hold-until attribute...
2407 set_hold_until(cupsd_job_t
*job
, /* I - Job to update */
2408 time_t holdtime
) /* I - Hold until time */
2410 ipp_attribute_t
*attr
; /* job-hold-until attribute */
2411 struct tm
*holddate
; /* Hold date */
2412 char holdstr
[64]; /* Hold time */
2416 * Set the hold_until value and hold the job...
2419 cupsdLogMessage(CUPSD_LOG_DEBUG
, "set_hold_until: hold_until = %d",
2422 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
2423 job
->state_value
= IPP_JOB_HELD
;
2424 job
->hold_until
= holdtime
;
2427 * Update the job-hold-until attribute with a string representing GMT
2428 * time (HH:MM:SS)...
2431 holddate
= gmtime(&holdtime
);
2432 snprintf(holdstr
, sizeof(holdstr
), "%d:%d:%d", holddate
->tm_hour
,
2433 holddate
->tm_min
, holddate
->tm_sec
);
2435 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2436 IPP_TAG_KEYWORD
)) == NULL
)
2437 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2440 * Either add the attribute or update the value of the existing one
2444 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "job-hold-until",
2447 cupsdSetString(&attr
->values
[0].string
.text
, holdstr
);
2450 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2455 * 'start_job()' - Start a print job.
2459 start_job(cupsd_job_t
*job
, /* I - Job ID */
2460 cupsd_printer_t
*printer
) /* I - Printer to print job */
2462 int i
; /* Looping var */
2463 int slot
; /* Pipe slot */
2464 cups_array_t
*filters
, /* Filters for job */
2465 *prefilters
; /* Filters with prefilters */
2466 mime_filter_t
*filter
, /* Current filter */
2467 *prefilter
, /* Prefilter */
2468 port_monitor
; /* Port monitor filter */
2469 char method
[255], /* Method for output */
2470 *optptr
, /* Pointer to options */
2471 *valptr
; /* Pointer in value string */
2472 ipp_attribute_t
*attr
; /* Current attribute */
2473 struct stat backinfo
; /* Backend file information */
2474 int backroot
; /* Run backend as root? */
2475 int pid
; /* Process ID of new filter process */
2476 int banner_page
; /* 1 if banner page, 0 otherwise */
2477 int filterfds
[2][2];/* Pipes used between filters */
2478 int envc
; /* Number of environment variables */
2479 char **argv
, /* Filter command-line arguments */
2480 filename
[1024], /* Job filename */
2481 command
[1024], /* Full path to command */
2482 jobid
[255], /* Job ID string */
2483 title
[IPP_MAX_NAME
],
2484 /* Job title string */
2485 copies
[255], /* # copies string */
2486 *envp
[MAX_ENV
+ 17],
2487 /* Environment variables */
2488 charset
[255], /* CHARSET env variable */
2489 class_name
[255],/* CLASS env variable */
2490 classification
[1024],
2491 /* CLASSIFICATION env variable */
2493 /* CONTENT_TYPE env variable */
2495 /* DEVICE_URI env variable */
2496 final_content_type
[1024],
2497 /* FINAL_CONTENT_TYPE env variable */
2498 lang
[255], /* LANG env variable */
2500 apple_language
[255],
2501 /* APPLE_LANGUAGE env variable */
2502 #endif /* __APPLE__ */
2503 ppd
[1024], /* PPD env variable */
2505 /* PRINTER env variable */
2507 /* RIP_MAX_CACHE env variable */
2508 static char *options
= NULL
;/* Full list of options */
2509 static int optlength
= 0; /* Length of option buffer */
2512 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "start_job: file = %d/%d",
2513 job
->current_file
, job
->num_files
);
2515 if (job
->num_files
== 0)
2517 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "No files, canceling job!");
2518 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2523 * Figure out what filters are required to convert from
2524 * the source to the destination type...
2533 * Remote jobs and raw queues go directly to the printer without
2537 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Sending job to queue tagged as raw...");
2544 * Local jobs get filtered...
2547 filters
= mimeFilter(MimeDatabase
, job
->filetypes
[job
->current_file
],
2548 printer
->filetype
, &(job
->cost
));
2552 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
2553 "Unable to convert file %d to printable format!",
2555 cupsdLogMessage(CUPSD_LOG_INFO
,
2556 "Hint: Do you have Ghostscript installed?");
2558 if (LogLevel
< CUPSD_LOG_DEBUG
)
2559 cupsdLogMessage(CUPSD_LOG_INFO
,
2560 "Hint: Try setting the LogLevel to \"debug\".");
2562 job
->current_file
++;
2564 if (job
->current_file
== job
->num_files
)
2565 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2571 * Remove NULL ("-") filters...
2574 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
2576 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
2577 if (!strcmp(filter
->filter
, "-"))
2578 cupsArrayRemove(filters
, filter
);
2580 if (cupsArrayCount(filters
) == 0)
2582 cupsArrayDelete(filters
);
2587 * If this printer has any pre-filters, insert the required pre-filter
2588 * in the filters array...
2591 if (printer
->prefiltertype
&& filters
)
2593 prefilters
= cupsArrayNew(NULL
, NULL
);
2595 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
2597 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
2599 if ((prefilter
= mimeFilterLookup(MimeDatabase
, filter
->src
,
2600 printer
->prefiltertype
)))
2602 cupsArrayAdd(prefilters
, prefilter
);
2603 job
->cost
+= prefilter
->cost
;
2606 cupsArrayAdd(prefilters
, filter
);
2609 cupsArrayDelete(filters
);
2610 filters
= prefilters
;
2615 * Set a minimum cost of 100 for all jobs so that FilterLimit
2616 * works with raw queues and other low-cost paths.
2619 if (job
->cost
< 100)
2623 * See if the filter cost is too high...
2626 if ((FilterLevel
+ job
->cost
) > FilterLimit
&& FilterLevel
> 0 &&
2630 * Don't print this job quite yet...
2633 cupsArrayDelete(filters
);
2635 cupsdLogJob(job
, CUPSD_LOG_INFO
,
2636 "Holding because filter limit has been reached.");
2637 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
2638 "start_job: file=%d, cost=%d, level=%d, limit=%d",
2639 job
->current_file
, job
->cost
, FilterLevel
,
2644 FilterLevel
+= job
->cost
;
2647 * Add decompression/raw filter as needed...
2650 if ((!printer
->raw
&& job
->compressions
[job
->current_file
]) ||
2651 (!filters
&& !printer
->remote
&&
2652 (job
->num_files
> 1 || !strncmp(printer
->device_uri
, "file:", 5))))
2655 * Add gziptoany filter to the front of the list...
2659 filters
= cupsArrayNew(NULL
, NULL
);
2661 if (!cupsArrayInsert(filters
, &gziptoany_filter
))
2663 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
2664 "Unable to add decompression filter - %s", strerror(errno
));
2666 cupsArrayDelete(filters
);
2668 job
->current_file
++;
2670 if (job
->current_file
== job
->num_files
)
2671 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2678 * Add port monitor, if any...
2681 if (printer
->port_monitor
)
2684 * Add port monitor to the end of the list...
2688 filters
= cupsArrayNew(NULL
, NULL
);
2690 port_monitor
.src
= NULL
;
2691 port_monitor
.dst
= NULL
;
2692 port_monitor
.cost
= 0;
2694 snprintf(port_monitor
.filter
, sizeof(port_monitor
.filter
),
2695 "%s/monitor/%s", ServerBin
, printer
->port_monitor
);
2697 if (!cupsArrayAdd(filters
, &port_monitor
))
2699 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
2700 "Unable to add port monitor - %s", strerror(errno
));
2702 cupsArrayDelete(filters
);
2704 job
->current_file
++;
2706 if (job
->current_file
== job
->num_files
)
2707 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2714 * Make sure we don't go over the "MAX_FILTERS" limit...
2717 if (cupsArrayCount(filters
) > MAX_FILTERS
)
2719 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
2720 "Too many filters (%d > %d), unable to print!",
2721 cupsArrayCount(filters
), MAX_FILTERS
);
2723 cupsArrayDelete(filters
);
2724 cupsdCancelJob(job
, 0, IPP_JOB_STOPPED
);
2730 * Update the printer and job state to "processing"...
2733 job
->state
->values
[0].integer
= IPP_JOB_PROCESSING
;
2734 job
->state_value
= IPP_JOB_PROCESSING
;
2737 job
->printer
= printer
;
2740 cupsdSetPrinterState(printer
, IPP_PRINTER_PROCESSING
, 0);
2742 if (job
->current_file
== 0)
2745 * Add to the printing list...
2748 cupsArrayAdd(PrintingJobs
, job
);
2749 cupsdSetBusyState();
2752 * Set the processing time...
2755 set_time(job
, "time-at-processing");
2758 * Create the backchannel pipes and make them non-blocking...
2761 cupsdOpenPipe(job
->back_pipes
);
2763 fcntl(job
->back_pipes
[0], F_SETFL
,
2764 fcntl(job
->back_pipes
[0], F_GETFL
) | O_NONBLOCK
);
2766 fcntl(job
->back_pipes
[1], F_SETFL
,
2767 fcntl(job
->back_pipes
[1], F_GETFL
) | O_NONBLOCK
);
2770 * Create the side-channel pipes and make them non-blocking...
2773 socketpair(AF_LOCAL
, SOCK_STREAM
, 0, job
->side_pipes
);
2775 fcntl(job
->side_pipes
[0], F_SETFL
,
2776 fcntl(job
->side_pipes
[0], F_GETFL
) | O_NONBLOCK
);
2778 fcntl(job
->side_pipes
[1], F_SETFL
,
2779 fcntl(job
->side_pipes
[1], F_GETFL
) | O_NONBLOCK
);
2783 * Determine if we are printing a banner page or not...
2786 if (job
->job_sheets
== NULL
)
2788 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "No job-sheets attribute.");
2789 if ((job
->job_sheets
=
2790 ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_ZERO
)) != NULL
)
2791 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
2792 "... but someone added one without setting job_sheets!");
2794 else if (job
->job_sheets
->num_values
== 1)
2795 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "job-sheets=%s",
2796 job
->job_sheets
->values
[0].string
.text
);
2798 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "job-sheets=%s,%s",
2799 job
->job_sheets
->values
[0].string
.text
,
2800 job
->job_sheets
->values
[1].string
.text
);
2802 if (printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
))
2804 else if (job
->job_sheets
== NULL
)
2806 else if (strcasecmp(job
->job_sheets
->values
[0].string
.text
, "none") != 0 &&
2807 job
->current_file
== 0)
2809 else if (job
->job_sheets
->num_values
> 1 &&
2810 strcasecmp(job
->job_sheets
->values
[1].string
.text
, "none") != 0 &&
2811 job
->current_file
== (job
->num_files
- 1))
2816 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "banner_page = %d", banner_page
);
2819 * Building the options string is harder than it needs to be, but
2820 * for the moment we need to pass strings for command-line args and
2821 * not IPP attribute pointers... :)
2823 * First allocate/reallocate the option buffer as needed...
2826 i
= ipp_length(job
->attrs
);
2828 if (i
> optlength
|| !options
)
2833 optptr
= realloc(options
, i
);
2837 cupsdLogJob(job
, CUPSD_LOG_CRIT
,
2838 "Unable to allocate %d bytes for option buffer!", i
);
2840 cupsArrayDelete(filters
);
2842 FilterLevel
-= job
->cost
;
2844 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2853 * Now loop through the attributes and convert them to the textual
2854 * representation used by the filters...
2860 snprintf(title
, sizeof(title
), "%s-%d", printer
->name
, job
->id
);
2861 strcpy(copies
, "1");
2863 for (attr
= job
->attrs
->attrs
; attr
!= NULL
; attr
= attr
->next
)
2865 if (!strcmp(attr
->name
, "copies") &&
2866 attr
->value_tag
== IPP_TAG_INTEGER
)
2869 * Don't use the # copies attribute if we are printing the job sheets...
2873 sprintf(copies
, "%d", attr
->values
[0].integer
);
2875 else if (!strcmp(attr
->name
, "job-name") &&
2876 (attr
->value_tag
== IPP_TAG_NAME
||
2877 attr
->value_tag
== IPP_TAG_NAMELANG
))
2878 strlcpy(title
, attr
->values
[0].string
.text
, sizeof(title
));
2879 else if (attr
->group_tag
== IPP_TAG_JOB
)
2882 * Filter out other unwanted attributes...
2885 if (attr
->value_tag
== IPP_TAG_MIMETYPE
||
2886 attr
->value_tag
== IPP_TAG_NAMELANG
||
2887 attr
->value_tag
== IPP_TAG_TEXTLANG
||
2888 (attr
->value_tag
== IPP_TAG_URI
&& strcmp(attr
->name
, "job-uuid")) ||
2889 attr
->value_tag
== IPP_TAG_URISCHEME
||
2890 attr
->value_tag
== IPP_TAG_BEGIN_COLLECTION
) /* Not yet supported */
2893 if (!strncmp(attr
->name
, "time-", 5))
2896 if (!strncmp(attr
->name
, "job-", 4) &&
2897 strcmp(attr
->name
, "job-uuid") &&
2898 strcmp(attr
->name
, "job-impressions") &&
2899 strcmp(attr
->name
, "job-originating-host-name") &&
2900 !(printer
->type
& CUPS_PRINTER_REMOTE
))
2903 if ((!strcmp(attr
->name
, "job-impressions") ||
2904 !strcmp(attr
->name
, "page-label") ||
2905 !strcmp(attr
->name
, "page-border") ||
2906 !strncmp(attr
->name
, "number-up", 9) ||
2907 !strcmp(attr
->name
, "page-ranges") ||
2908 !strcmp(attr
->name
, "page-set") ||
2909 !strcasecmp(attr
->name
, "AP_FIRSTPAGE_InputSlot") ||
2910 !strcasecmp(attr
->name
, "AP_FIRSTPAGE_ManualFeed") ||
2911 !strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
2912 "PMTotalSidesImaged..n.") ||
2913 !strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
2914 "PMTotalBeginPages..n.")) &&
2919 * Otherwise add them to the list...
2922 if (optptr
> options
)
2923 strlcat(optptr
, " ", optlength
- (optptr
- options
));
2925 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
2927 strlcat(optptr
, attr
->name
, optlength
- (optptr
- options
));
2928 strlcat(optptr
, "=", optlength
- (optptr
- options
));
2931 for (i
= 0; i
< attr
->num_values
; i
++)
2934 strlcat(optptr
, ",", optlength
- (optptr
- options
));
2936 optptr
+= strlen(optptr
);
2938 switch (attr
->value_tag
)
2940 case IPP_TAG_INTEGER
:
2942 snprintf(optptr
, optlength
- (optptr
- options
),
2943 "%d", attr
->values
[i
].integer
);
2946 case IPP_TAG_BOOLEAN
:
2947 if (!attr
->values
[i
].boolean
)
2948 strlcat(optptr
, "no", optlength
- (optptr
- options
));
2950 case IPP_TAG_NOVALUE
:
2951 strlcat(optptr
, attr
->name
,
2952 optlength
- (optptr
- options
));
2955 case IPP_TAG_RANGE
:
2956 if (attr
->values
[i
].range
.lower
== attr
->values
[i
].range
.upper
)
2957 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2958 "%d", attr
->values
[i
].range
.lower
);
2960 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2961 "%d-%d", attr
->values
[i
].range
.lower
,
2962 attr
->values
[i
].range
.upper
);
2965 case IPP_TAG_RESOLUTION
:
2966 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2967 "%dx%d%s", attr
->values
[i
].resolution
.xres
,
2968 attr
->values
[i
].resolution
.yres
,
2969 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
2973 case IPP_TAG_STRING
:
2976 case IPP_TAG_KEYWORD
:
2977 case IPP_TAG_CHARSET
:
2978 case IPP_TAG_LANGUAGE
:
2980 for (valptr
= attr
->values
[i
].string
.text
; *valptr
;)
2982 if (strchr(" \t\n\\\'\"", *valptr
))
2984 *optptr
++ = *valptr
++;
2991 break; /* anti-compiler-warning-code */
2995 optptr
+= strlen(optptr
);
3000 * Build the command-line arguments for the filters. Each filter
3001 * has 6 or 7 arguments:
3005 * argv[2] = username
3007 * argv[4] = # copies
3009 * argv[6] = filename (optional; normally stdin)
3011 * This allows legacy printer drivers that use the old System V
3012 * printing interface to be used by CUPS.
3014 * For remote jobs, we send all of the files in the argument list.
3017 if (printer
->remote
&& job
->num_files
> 1)
3018 argv
= calloc(7 + job
->num_files
, sizeof(char *));
3020 argv
= calloc(8, sizeof(char *));
3024 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to allocate argument array!");
3025 cupsArrayDelete(filters
);
3027 FilterLevel
-= job
->cost
;
3029 cupsdStopPrinter(printer
, 0);
3033 sprintf(jobid
, "%d", job
->id
);
3035 argv
[0] = printer
->name
;
3037 argv
[2] = job
->username
;
3042 if (printer
->remote
&& job
->num_files
> 1)
3044 for (i
= 0; i
< job
->num_files
; i
++)
3046 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
3048 argv
[6 + i
] = strdup(filename
);
3053 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
3054 job
->id
, job
->current_file
+ 1);
3058 for (i
= 0; argv
[i
]; i
++)
3059 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
3060 "argv[%d]=\"%s\"", i
, argv
[i
]);
3063 * Create environment variable strings for the filters...
3066 attr
= ippFindAttribute(job
->attrs
, "attributes-natural-language",
3070 strcpy(apple_language
, "APPLE_LANGUAGE=");
3071 _cupsAppleLanguage(attr
->values
[0].string
.text
,
3072 apple_language
+ 15, sizeof(apple_language
) - 15);
3073 #endif /* __APPLE__ */
3075 switch (strlen(attr
->values
[0].string
.text
))
3079 * This is an unknown or badly formatted language code; use
3080 * the POSIX locale...
3083 strcpy(lang
, "LANG=C");
3088 * Just the language code (ll)...
3091 snprintf(lang
, sizeof(lang
), "LANG=%s.UTF8",
3092 attr
->values
[0].string
.text
);
3097 * Language and country code (ll-cc)...
3100 snprintf(lang
, sizeof(lang
), "LANG=%c%c_%c%c.UTF8",
3101 attr
->values
[0].string
.text
[0],
3102 attr
->values
[0].string
.text
[1],
3103 toupper(attr
->values
[0].string
.text
[3] & 255),
3104 toupper(attr
->values
[0].string
.text
[4] & 255));
3108 attr
= ippFindAttribute(job
->attrs
, "document-format",
3111 (optptr
= strstr(attr
->values
[0].string
.text
, "charset=")) != NULL
)
3112 snprintf(charset
, sizeof(charset
), "CHARSET=%s", optptr
+ 8);
3115 attr
= ippFindAttribute(job
->attrs
, "attributes-charset",
3117 snprintf(charset
, sizeof(charset
), "CHARSET=%s",
3118 attr
->values
[0].string
.text
);
3121 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s/%s",
3122 job
->filetypes
[job
->current_file
]->super
,
3123 job
->filetypes
[job
->current_file
]->type
);
3124 snprintf(device_uri
, sizeof(device_uri
), "DEVICE_URI=%s",
3125 printer
->device_uri
);
3126 snprintf(ppd
, sizeof(ppd
), "PPD=%s/ppd/%s.ppd", ServerRoot
, printer
->name
);
3127 snprintf(printer_name
, sizeof(printer_name
), "PRINTER=%s", printer
->name
);
3128 snprintf(rip_max_cache
, sizeof(rip_max_cache
), "RIP_MAX_CACHE=%s", RIPCache
);
3130 envc
= cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
3132 envp
[envc
++] = charset
;
3133 envp
[envc
++] = lang
;
3135 envp
[envc
++] = apple_language
;
3136 #endif /* __APPLE__ */
3137 envp
[envc
++] = ppd
;
3138 envp
[envc
++] = rip_max_cache
;
3139 envp
[envc
++] = content_type
;
3140 envp
[envc
++] = device_uri
;
3141 envp
[envc
++] = printer_name
;
3142 envp
[envc
++] = banner_page
? "CUPS_FILETYPE=job-sheet" :
3143 "CUPS_FILETYPE=document";
3145 if (!printer
->remote
&& !printer
->raw
)
3147 filter
= (mime_filter_t
*)cupsArrayLast(filters
);
3149 if (printer
->port_monitor
)
3150 filter
= (mime_filter_t
*)cupsArrayPrev(filters
);
3152 if (filter
&& filter
->dst
)
3154 snprintf(final_content_type
, sizeof(final_content_type
),
3155 "FINAL_CONTENT_TYPE=%s/%s",
3156 filter
->dst
->super
, filter
->dst
->type
);
3157 envp
[envc
++] = final_content_type
;
3161 if (Classification
&& !banner_page
)
3163 if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
3164 IPP_TAG_NAME
)) == NULL
)
3165 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3167 else if (attr
->num_values
> 1 &&
3168 strcmp(attr
->values
[1].string
.text
, "none") != 0)
3169 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3170 attr
->values
[1].string
.text
);
3172 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3173 attr
->values
[0].string
.text
);
3175 envp
[envc
++] = classification
;
3178 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
3180 snprintf(class_name
, sizeof(class_name
), "CLASS=%s", job
->dest
);
3181 envp
[envc
++] = class_name
;
3184 if (job
->auth_username
)
3185 envp
[envc
++] = job
->auth_username
;
3186 if (job
->auth_domain
)
3187 envp
[envc
++] = job
->auth_domain
;
3188 if (job
->auth_password
)
3189 envp
[envc
++] = job
->auth_password
;
3193 envp
[envc
++] = job
->ccname
;
3194 #endif /* HAVE_GSSAPI */
3198 for (i
= 0; i
< envc
; i
++)
3199 if (!strncmp(envp
[i
], "AUTH_", 5))
3200 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"AUTH_%c****\"", i
,
3202 else if (strncmp(envp
[i
], "DEVICE_URI=", 11))
3203 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"%s\"", i
, envp
[i
]);
3205 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"DEVICE_URI=%s\"", i
,
3206 printer
->sanitized_device_uri
);
3208 if (printer
->remote
)
3209 job
->current_file
= job
->num_files
;
3211 job
->current_file
++;
3214 * Now create processes for all of the filters...
3217 filterfds
[0][0] = -1;
3218 filterfds
[0][1] = -1;
3219 filterfds
[1][0] = -1;
3220 filterfds
[1][1] = -1;
3222 if (!job
->status_buffer
)
3224 if (cupsdOpenPipe(job
->status_pipes
))
3226 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
3227 "Unable to create job status pipes - %s.", strerror(errno
));
3228 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3229 "Unable to create status pipes - %s.", strerror(errno
));
3231 cupsdAddPrinterHistory(printer
);
3233 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3234 "Job canceled because the server could not create the job "
3240 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3241 "start_job: status_pipes = [ %d %d ]",
3242 job
->status_pipes
[0], job
->status_pipes
[1]);
3244 job
->status_buffer
= cupsdStatBufNew(job
->status_pipes
[0], NULL
);
3245 job
->status_level
= CUPSD_LOG_INFO
;
3249 memset(job
->filters
, 0, sizeof(job
->filters
));
3252 job
->profile
= cupsdCreateProfile(job
->id
);
3254 for (i
= 0, slot
= 0, filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
3256 i
++, filter
= (mime_filter_t
*)cupsArrayNext(filters
))
3258 if (filter
->filter
[0] != '/')
3259 snprintf(command
, sizeof(command
), "%s/filter/%s", ServerBin
,
3262 strlcpy(command
, filter
->filter
, sizeof(command
));
3264 if (i
< (cupsArrayCount(filters
) - 1))
3266 if (cupsdOpenPipe(filterfds
[slot
]))
3268 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
3269 "Unable to create job filter pipes - %s.", strerror(errno
));
3270 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3271 "Unable to create filter pipes - %s.", strerror(errno
));
3272 cupsdAddPrinterHistory(printer
);
3274 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3275 "Job canceled because the server could not create the "
3283 if (job
->current_file
== 1)
3285 if (strncmp(printer
->device_uri
, "file:", 5) != 0)
3287 if (cupsdOpenPipe(job
->print_pipes
))
3289 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
3290 "Unable to create job backend pipes - %s.",
3292 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3293 "Unable to create backend pipes - %s.", strerror(errno
));
3294 cupsdAddPrinterHistory(printer
);
3296 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3297 "Job canceled because the server could not create "
3298 "the backend pipes.");
3305 job
->print_pipes
[0] = -1;
3306 if (!strcmp(printer
->device_uri
, "file:/dev/null") ||
3307 !strcmp(printer
->device_uri
, "file:///dev/null"))
3308 job
->print_pipes
[1] = -1;
3311 if (!strncmp(printer
->device_uri
, "file:/dev/", 10))
3312 job
->print_pipes
[1] = open(printer
->device_uri
+ 5,
3314 else if (!strncmp(printer
->device_uri
, "file:///dev/", 12))
3315 job
->print_pipes
[1] = open(printer
->device_uri
+ 7,
3317 else if (!strncmp(printer
->device_uri
, "file:///", 8))
3318 job
->print_pipes
[1] = open(printer
->device_uri
+ 7,
3319 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
3321 job
->print_pipes
[1] = open(printer
->device_uri
+ 5,
3322 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
3324 if (job
->print_pipes
[1] < 0)
3326 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
3327 "Unable to open output file \"%s\" - %s.",
3328 printer
->device_uri
, strerror(errno
));
3329 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3330 "Unable to open output file \"%s\" - %s.",
3331 printer
->device_uri
, strerror(errno
));
3333 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3334 "Job canceled because the server could not open the "
3340 fcntl(job
->print_pipes
[1], F_SETFD
,
3341 fcntl(job
->print_pipes
[1], F_GETFD
) | FD_CLOEXEC
);
3345 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3346 "start_job: print_pipes = [ %d %d ]",
3347 job
->print_pipes
[0], job
->print_pipes
[1]);
3350 filterfds
[slot
][0] = job
->print_pipes
[0];
3351 filterfds
[slot
][1] = job
->print_pipes
[1];
3354 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "start_job: filter=\"%s\"", command
);
3355 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "start_job: filterfds[%d]=[ %d %d ]",
3356 slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3358 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
3359 filterfds
[slot
][1], job
->status_pipes
[1],
3360 job
->back_pipes
[0], job
->side_pipes
[0], 0,
3361 job
->profile
, job
->filters
+ i
);
3363 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3364 "start_job: Closing filter pipes for slot %d [ %d %d ]...",
3365 !slot
, filterfds
[!slot
][0], filterfds
[!slot
][1]);
3367 cupsdClosePipe(filterfds
[!slot
]);
3371 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Unable to start filter \"%s\" - %s.",
3372 filter
->filter
, strerror(errno
));
3373 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3374 "Unable to start filter \"%s\" - %s.",
3375 filter
->filter
, strerror(errno
));
3377 cupsdAddPrinterHistory(printer
);
3379 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3380 "Job canceled because the server could not execute a "
3386 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Started filter %s (PID %d)", command
,
3393 cupsArrayDelete(filters
);
3397 * Finally, pipe the final output into a backend process if needed...
3400 if (strncmp(printer
->device_uri
, "file:", 5) != 0)
3402 if (job
->current_file
== 1 || printer
->remote
)
3404 sscanf(printer
->device_uri
, "%254[^:]", method
);
3405 snprintf(command
, sizeof(command
), "%s/backend/%s", ServerBin
, method
);
3408 * See if the backend needs to run as root...
3413 else if (stat(command
, &backinfo
))
3416 backroot
= !(backinfo
.st_mode
& (S_IRWXG
| S_IRWXO
));
3418 argv
[0] = printer
->sanitized_device_uri
;
3420 filterfds
[slot
][0] = -1;
3421 filterfds
[slot
][1] = -1;
3423 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "start_job: backend=\"%s\"", command
);
3424 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "start_job: filterfds[%d] = [ %d %d ]",
3425 slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3427 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
3428 filterfds
[slot
][1], job
->status_pipes
[1],
3429 job
->back_pipes
[1], job
->side_pipes
[1],
3430 backroot
, job
->profile
, &(job
->backend
));
3434 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Unable to start backend \"%s\" - %s.",
3435 method
, strerror(errno
));
3436 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3437 "Unable to start backend \"%s\" - %s.", method
,
3440 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3441 "Job canceled because the server could not execute "
3448 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Started backend %s (PID %d)",
3453 if (job
->current_file
== job
->num_files
)
3455 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3456 "start_job: Closing print pipes [ %d %d ]...",
3457 job
->print_pipes
[0], job
->print_pipes
[1]);
3459 cupsdClosePipe(job
->print_pipes
);
3461 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3462 "start_job: Closing back pipes [ %d %d ]...",
3463 job
->back_pipes
[0], job
->back_pipes
[1]);
3465 cupsdClosePipe(job
->back_pipes
);
3467 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3468 "start_job: Closing side pipes [ %d %d ]...",
3469 job
->side_pipes
[0], job
->side_pipes
[1]);
3471 cupsdClosePipe(job
->side_pipes
);
3473 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3474 "start_job: Closing status output pipe %d...",
3475 job
->status_pipes
[1]);
3477 close(job
->status_pipes
[1]);
3478 job
->status_pipes
[1] = -1;
3483 filterfds
[slot
][0] = -1;
3484 filterfds
[slot
][1] = -1;
3486 if (job
->current_file
== job
->num_files
)
3488 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3489 "start_job: Closing print pipes [ %d %d ]...",
3490 job
->print_pipes
[0], job
->print_pipes
[1]);
3492 cupsdClosePipe(job
->print_pipes
);
3494 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3495 "start_job: Closing status output pipe %d...",
3496 job
->status_pipes
[1]);
3498 close(job
->status_pipes
[1]);
3499 job
->status_pipes
[1] = -1;
3503 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3504 "start_job: Closing filter pipes for slot %d [ %d %d ]...",
3505 slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3506 cupsdClosePipe(filterfds
[slot
]);
3508 if (printer
->remote
&& job
->num_files
> 1)
3510 for (i
= 0; i
< job
->num_files
; i
++)
3516 cupsdAddSelect(job
->status_buffer
->fd
, (cupsd_selfunc_t
)update_job
, NULL
,
3519 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
, "Job #%d started.",
3526 * If we get here, we need to abort the current job and close out all
3527 * files and pipes...
3532 for (slot
= 0; slot
< 2; slot
++)
3534 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3535 "start_job: Closing filter pipes for slot %d [ %d %d ]...",
3536 slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3537 cupsdClosePipe(filterfds
[slot
]);
3540 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3541 "start_job: Closing status pipes [ %d %d ]...",
3542 job
->status_pipes
[0], job
->status_pipes
[1]);
3543 cupsdClosePipe(job
->status_pipes
);
3544 cupsdStatBufDelete(job
->status_buffer
);
3546 job
->status_buffer
= NULL
;
3548 cupsArrayDelete(filters
);
3550 if (printer
->remote
&& job
->num_files
> 1)
3552 for (i
= 0; i
< job
->num_files
; i
++)
3558 cupsdStopJob(job
, 0);
3563 * 'unload_job()' - Unload a job from memory.
3567 unload_job(cupsd_job_t
*job
) /* I - Job */
3572 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Unloading...");
3574 ippDelete(job
->attrs
);
3579 job
->job_sheets
= NULL
;
3580 job
->printer_message
= NULL
;
3581 job
->printer_reasons
= NULL
;
3586 * 'update_job()' - Read a status update from a job's filters.
3590 update_job(cupsd_job_t
*job
) /* I - Job to check */
3592 int i
; /* Looping var */
3593 int copies
; /* Number of copies printed */
3594 char message
[1024], /* Message text */
3595 *ptr
; /* Pointer update... */
3596 int loglevel
, /* Log level for message */
3597 event
= 0; /* Events? */
3600 while ((ptr
= cupsdStatBufUpdate(job
->status_buffer
, &loglevel
,
3601 message
, sizeof(message
))) != NULL
)
3604 * Process page and printer state messages as needed...
3607 if (loglevel
== CUPSD_LOG_PAGE
)
3610 * Page message; send the message to the page_log file and update the
3611 * job sheet count...
3614 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "PAGE: %s", message
);
3618 if (!strncasecmp(message
, "total ", 6))
3621 * Got a total count of pages from a backend or filter...
3624 copies
= atoi(message
+ 6);
3625 copies
-= job
->sheets
->values
[0].integer
; /* Just track the delta */
3627 else if (!sscanf(message
, "%*d%d", &copies
))
3630 job
->sheets
->values
[0].integer
+= copies
;
3632 if (job
->printer
->page_limit
)
3634 cupsd_quota_t
*q
= cupsdUpdateQuota(job
->printer
, job
->username
,
3638 if (AppleQuotas
&& q
->page_count
== -3)
3641 * Quota limit exceeded, cancel job in progress immediately...
3644 cupsdLogJob(job
, CUPSD_LOG_INFO
,
3645 "Canceled because pages exceed user %s "
3646 "quota limit on printer %s (%s).",
3647 job
->username
, job
->printer
->name
,
3648 job
->printer
->info
);
3650 cupsdCancelJob(job
, 1, IPP_JOB_CANCELED
);
3655 #endif /* __APPLE__ */
3659 cupsdLogPage(job
, message
);
3662 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
3663 "Printed %d page(s).", job
->sheets
->values
[0].integer
);
3665 else if (loglevel
== CUPSD_LOG_STATE
)
3667 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "STATE: %s", message
);
3669 if (!strcmp(message
, "paused"))
3671 cupsdStopPrinter(job
->printer
, 1);
3676 cupsdSetPrinterReasons(job
->printer
, message
);
3677 cupsdAddPrinterHistory(job
->printer
);
3678 event
|= CUPSD_EVENT_PRINTER_STATE
;
3681 update_job_attrs(job
, 0);
3683 else if (loglevel
== CUPSD_LOG_ATTR
)
3686 * Set attribute(s)...
3689 int num_attrs
; /* Number of attributes */
3690 cups_option_t
*attrs
; /* Attributes */
3691 const char *attr
; /* Attribute */
3694 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "ATTR: %s", message
);
3696 num_attrs
= cupsParseOptions(message
, 0, &attrs
);
3698 if ((attr
= cupsGetOption("auth-info-required", num_attrs
,
3701 cupsdSetAuthInfoRequired(job
->printer
, attr
, NULL
);
3702 cupsdSetPrinterAttrs(job
->printer
);
3704 if (job
->printer
->type
& CUPS_PRINTER_DISCOVERED
)
3705 cupsdMarkDirty(CUPSD_DIRTY_REMOTE
);
3707 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3710 if ((attr
= cupsGetOption("job-media-progress", num_attrs
,
3713 int progress
= atoi(attr
);
3716 if (progress
>= 0 && progress
<= 100)
3718 job
->progress
= progress
;
3721 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
3722 "Printing page %d, %d%%",
3723 job
->sheets
->values
[0].integer
, job
->progress
);
3727 if ((attr
= cupsGetOption("printer-alert", num_attrs
, attrs
)) != NULL
)
3729 cupsdSetString(&job
->printer
->alert
, attr
);
3730 event
|= CUPSD_EVENT_PRINTER_STATE
;
3733 if ((attr
= cupsGetOption("printer-alert-description", num_attrs
,
3736 cupsdSetString(&job
->printer
->alert_description
, attr
);
3737 event
|= CUPSD_EVENT_PRINTER_STATE
;
3740 if ((attr
= cupsGetOption("marker-colors", num_attrs
, attrs
)) != NULL
)
3742 cupsdSetPrinterAttr(job
->printer
, "marker-colors", (char *)attr
);
3743 job
->printer
->marker_time
= time(NULL
);
3744 event
|= CUPSD_EVENT_PRINTER_STATE
;
3747 if ((attr
= cupsGetOption("marker-levels", num_attrs
, attrs
)) != NULL
)
3749 cupsdSetPrinterAttr(job
->printer
, "marker-levels", (char *)attr
);
3750 job
->printer
->marker_time
= time(NULL
);
3751 event
|= CUPSD_EVENT_PRINTER_STATE
;
3754 if ((attr
= cupsGetOption("marker-message", num_attrs
, attrs
)) != NULL
)
3756 cupsdSetPrinterAttr(job
->printer
, "marker-message", (char *)attr
);
3757 job
->printer
->marker_time
= time(NULL
);
3758 event
|= CUPSD_EVENT_PRINTER_STATE
;
3761 if ((attr
= cupsGetOption("marker-names", num_attrs
, attrs
)) != NULL
)
3763 cupsdSetPrinterAttr(job
->printer
, "marker-names", (char *)attr
);
3764 job
->printer
->marker_time
= time(NULL
);
3765 event
|= CUPSD_EVENT_PRINTER_STATE
;
3768 if ((attr
= cupsGetOption("marker-types", num_attrs
, attrs
)) != NULL
)
3770 cupsdSetPrinterAttr(job
->printer
, "marker-types", (char *)attr
);
3771 job
->printer
->marker_time
= time(NULL
);
3772 event
|= CUPSD_EVENT_PRINTER_STATE
;
3775 cupsFreeOptions(num_attrs
, attrs
);
3777 else if (loglevel
== CUPSD_LOG_PPD
)
3780 * Set attribute(s)...
3783 int num_keywords
; /* Number of keywords */
3784 cups_option_t
*keywords
; /* Keywords */
3787 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "PPD: %s", message
);
3789 num_keywords
= cupsParseOptions(message
, 0, &keywords
);
3791 if (cupsdUpdatePrinterPPD(job
->printer
, num_keywords
, keywords
))
3792 cupsdSetPrinterAttrs(job
->printer
);
3794 cupsFreeOptions(num_keywords
, keywords
);
3797 else if (!strncmp(message
, "recoverable:", 12))
3799 cupsdSetPrinterReasons(job
->printer
,
3800 "+com.apple.print.recoverable-warning");
3803 while (isspace(*ptr
& 255))
3806 cupsdSetString(&job
->printer
->recoverable
, ptr
);
3807 cupsdAddPrinterHistory(job
->printer
);
3808 event
|= CUPSD_EVENT_PRINTER_STATE
;
3810 else if (!strncmp(message
, "recovered:", 10))
3812 cupsdSetPrinterReasons(job
->printer
,
3813 "-com.apple.print.recoverable-warning");
3816 while (isspace(*ptr
& 255))
3819 cupsdSetString(&job
->printer
->recoverable
, ptr
);
3820 cupsdAddPrinterHistory(job
->printer
);
3821 event
|= CUPSD_EVENT_PRINTER_STATE
;
3823 #endif /* __APPLE__ */
3826 cupsdLogJob(job
, loglevel
, "%s", message
);
3828 if (loglevel
< CUPSD_LOG_DEBUG
)
3830 strlcpy(job
->printer
->state_message
, message
,
3831 sizeof(job
->printer
->state_message
));
3832 cupsdAddPrinterHistory(job
->printer
);
3834 event
|= CUPSD_EVENT_PRINTER_STATE
;
3836 if (loglevel
<= job
->status_level
)
3839 * Some messages show in the printer-state-message attribute...
3842 if (loglevel
!= CUPSD_LOG_NOTICE
)
3843 job
->status_level
= loglevel
;
3845 update_job_attrs(job
, 1);
3850 if (!strchr(job
->status_buffer
->buffer
, '\n'))
3854 if (event
& CUPSD_EVENT_PRINTER_STATE
)
3855 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, job
->printer
, NULL
,
3856 (job
->printer
->type
& CUPS_PRINTER_CLASS
) ?
3857 "Class \"%s\" state changed." :
3858 "Printer \"%s\" state changed.",
3859 job
->printer
->name
);
3861 if (ptr
== NULL
&& !job
->status_buffer
->bufused
)
3864 * See if all of the filters and the backend have returned their
3868 for (i
= 0; job
->filters
[i
] < 0; i
++);
3870 if (job
->filters
[i
])
3873 if (job
->current_file
>= job
->num_files
&& job
->backend
> 0)
3877 * Handle the end of job stuff...
3880 cupsdFinishJob(job
);
3886 * 'update_job_attrs()' - Update the job-printer-* attributes.
3890 update_job_attrs(cupsd_job_t
*job
, /* I - Job to update */
3891 int do_message
)/* I - 1 = update job-printer-state message */
3893 int i
; /* Looping var */
3894 int num_reasons
; /* Actual number of reasons */
3895 const char * const *reasons
; /* Reasons */
3896 static const char *none
= "none", /* "none" */
3902 * Get/create the job-printer-state-* attributes...
3905 if (!job
->printer_message
)
3907 if ((job
->printer_message
= ippFindAttribute(job
->attrs
,
3908 "job-printer-state-message",
3909 IPP_TAG_TEXT
)) == NULL
)
3910 job
->printer_message
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_TEXT
,
3911 "job-printer-state-message",
3915 if (!job
->printer_reasons
)
3916 job
->printer_reasons
= ippFindAttribute(job
->attrs
,
3917 "job-printer-state-reasons",
3921 * If the job isn't printing, return now...
3928 * Otherwise copy the printer-state-message value...
3931 if (job
->printer
->state_message
[0] &&
3932 (do_message
|| !job
->printer_message
->values
[0].string
.text
[0]))
3933 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
),
3934 job
->printer
->state_message
);
3937 * ... and the printer-state-reasons value...
3940 if (job
->printer
->num_reasons
== 0)
3943 reasons
= job
->printer
->state
== IPP_PRINTER_STOPPED
? &paused
: &none
;
3947 num_reasons
= job
->printer
->num_reasons
;
3948 reasons
= (const char * const *)job
->printer
->reasons
;
3951 if (!job
->printer_reasons
|| job
->printer_reasons
->num_values
!= num_reasons
)
3953 ippDeleteAttribute(job
->attrs
, job
->printer_reasons
);
3955 job
->printer_reasons
= ippAddStrings(job
->attrs
,
3956 IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
3957 "job-printer-state-reasons",
3958 num_reasons
, NULL
, NULL
);
3961 for (i
= 0; i
< num_reasons
; i
++)
3962 cupsdSetString(&(job
->printer_reasons
->values
[i
].string
.text
), reasons
[i
]);
3967 * End of "$Id: job.c 7902 2008-09-03 14:20:17Z mike $".