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
);
221 * If we are going to sleep and the PrintingJobs count is now 0, allow the
222 * sleep to happen immediately...
225 if (Sleeping
&& cupsArrayCount(PrintingJobs
) == 0)
227 #endif /* __APPLE__ */
230 * Remove any authentication data...
233 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
234 if (cupsdRemoveFile(filename
) && errno
!= ENOENT
)
235 cupsdLogMessage(CUPSD_LOG_ERROR
,
236 "Unable to remove authentication cache: %s",
239 cupsdClearString(&job
->auth_username
);
240 cupsdClearString(&job
->auth_domain
);
241 cupsdClearString(&job
->auth_password
);
245 * Destroy the credential cache and clear the KRB5CCNAME env var string.
250 krb5_cc_destroy(KerberosContext
, job
->ccache
);
254 cupsdClearString(&job
->ccname
);
255 #endif /* HAVE_GSSAPI */
258 * Remove the print file for good if we aren't preserving jobs or
262 job
->current_file
= 0;
264 if (!JobHistory
|| !JobFiles
|| purge
)
266 for (i
= 1; i
<= job
->num_files
; i
++)
268 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
273 if (job
->num_files
> 0)
275 free(job
->filetypes
);
276 free(job
->compressions
);
279 job
->filetypes
= NULL
;
280 job
->compressions
= NULL
;
284 if (JobHistory
&& !purge
)
287 * Save job state info...
291 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
296 * Remove the job info file...
299 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
,
304 * Remove the job from the "all jobs" list...
307 cupsArrayRemove(Jobs
, job
);
310 * Free all memory used...
321 * 'cupsdCancelJobs()' - Cancel all jobs for the given destination/user...
325 cupsdCancelJobs(const char *dest
, /* I - Destination to cancel */
326 const char *username
, /* I - Username or NULL */
327 int purge
) /* I - Purge jobs? */
329 cupsd_job_t
*job
; /* Current job */
332 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
334 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
336 if (!job
->dest
|| !job
->username
)
339 if (!job
->dest
|| !job
->username
)
342 if ((dest
== NULL
|| !strcmp(job
->dest
, dest
)) &&
343 (username
== NULL
|| !strcmp(job
->username
, username
)))
346 * Cancel all jobs matching this destination/user...
349 cupsdCancelJob(job
, purge
, IPP_JOB_CANCELED
);
358 * 'cupsdCheckJobs()' - Check the pending jobs and start any if the destination
365 cupsd_job_t
*job
; /* Current job in queue */
366 cupsd_printer_t
*printer
, /* Printer destination */
367 *pclass
; /* Printer class destination */
368 ipp_attribute_t
*attr
; /* Job attribute */
371 DEBUG_puts("cupsdCheckJobs()");
373 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
374 "cupsdCheckJobs: %d active jobs, sleeping=%d, reload=%d",
375 cupsArrayCount(ActiveJobs
), Sleeping
, NeedReload
);
377 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
379 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
382 * Start held jobs if they are ready...
385 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
386 "cupsdCheckJobs: Job %d: dest=%s, dtype=%x, "
387 "state_value=%d, loaded=%s", job
->id
, job
->dest
, job
->dtype
,
388 job
->state_value
, job
->attrs
? "yes" : "no");
390 if (job
->state_value
== IPP_JOB_HELD
&&
392 job
->hold_until
< time(NULL
))
394 if (job
->pending_timeout
)
396 /* Add trailing banner as needed */
397 if (cupsdTimeoutJob(job
))
401 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
402 job
->state_value
= IPP_JOB_PENDING
;
404 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
405 IPP_TAG_KEYWORD
)) == NULL
)
406 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
410 attr
->value_tag
= IPP_TAG_KEYWORD
;
411 cupsdSetString(&(attr
->values
[0].string
.text
), "no-hold");
413 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
418 * Start pending jobs if the destination is available...
421 if (job
->state_value
== IPP_JOB_PENDING
&& !NeedReload
&& !Sleeping
)
423 printer
= cupsdFindDest(job
->dest
);
427 (printer
->type
& (CUPS_PRINTER_IMPLICIT
| CUPS_PRINTER_CLASS
)))
430 * If the class is remote, just pass it to the remote server...
435 if (pclass
->state
== IPP_PRINTER_STOPPED
)
437 else if (pclass
->type
& CUPS_PRINTER_REMOTE
)
440 printer
= cupsdFindAvailablePrinter(printer
->name
);
443 if (!printer
&& !pclass
)
446 * Whoa, the printer and/or class for this destination went away;
450 cupsdLogJob(job
, CUPSD_LOG_WARN
,
451 "Printer/class %s has gone away; canceling job!",
454 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
455 "Job canceled because the destination printer/class has "
458 cupsdCancelJob(job
, 1, IPP_JOB_ABORTED
);
460 else if (printer
&& !printer
->holding_new_jobs
)
463 * See if the printer is available or remote and not printing a job;
464 * if so, start the job...
470 * Add/update a job-actual-printer-uri attribute for this job
471 * so that we know which printer actually printed the job...
474 if ((attr
= ippFindAttribute(job
->attrs
, "job-actual-printer-uri",
475 IPP_TAG_URI
)) != NULL
)
476 cupsdSetString(&attr
->values
[0].string
.text
, printer
->uri
);
478 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
,
479 "job-actual-printer-uri", NULL
, printer
->uri
);
482 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
485 if ((!(printer
->type
& CUPS_PRINTER_DISCOVERED
) && /* Printer is local */
486 printer
->state
== IPP_PRINTER_IDLE
) || /* and idle, OR */
487 ((printer
->type
& CUPS_PRINTER_DISCOVERED
) && /* Printer is remote */
488 !printer
->job
)) /* and not printing */
491 * Clear any message for the queue...
494 printer
->state_message
[0] = '\0';
500 start_job(job
, printer
);
509 * 'cupsdCleanJobs()' - Clean out old jobs.
515 cupsd_job_t
*job
; /* Current job */
521 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
522 job
&& cupsArrayCount(Jobs
) >= MaxJobs
;
523 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
524 if (job
->state_value
>= IPP_JOB_CANCELED
)
525 cupsdCancelJob(job
, 1, IPP_JOB_CANCELED
);
530 * 'cupsdDeleteJob()' - Free all memory used by a job.
534 cupsdDeleteJob(cupsd_job_t
*job
) /* I - Job */
536 cupsdClearString(&job
->username
);
537 cupsdClearString(&job
->dest
);
538 cupsdClearString(&job
->auth_username
);
539 cupsdClearString(&job
->auth_domain
);
540 cupsdClearString(&job
->auth_password
);
544 * Destroy the credential cache and clear the KRB5CCNAME env var string.
549 krb5_cc_destroy(KerberosContext
, job
->ccache
);
553 cupsdClearString(&job
->ccname
);
554 #endif /* HAVE_GSSAPI */
556 if (job
->num_files
> 0)
558 free(job
->compressions
);
559 free(job
->filetypes
);
562 ippDelete(job
->attrs
);
569 * 'cupsdFinishJob()' - Finish a job.
573 cupsdFinishJob(cupsd_job_t
*job
) /* I - Job */
575 cupsd_printer_t
*printer
; /* Current printer */
576 ipp_attribute_t
*attr
; /* job-hold-until attribute */
579 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "File %d is complete.",
580 job
->current_file
- 1);
582 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
583 "cupsdFinishJob: job->status is %d",
586 if (job
->status_buffer
&&
587 (job
->status
< 0 || job
->current_file
>= job
->num_files
))
590 * Close the pipe and clear the input bit.
593 cupsdRemoveSelect(job
->status_buffer
->fd
);
595 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
596 "cupsdFinishJob: Closing status pipes [ %d %d ]...",
597 job
->status_pipes
[0], job
->status_pipes
[1]);
599 cupsdClosePipe(job
->status_pipes
);
600 cupsdStatBufDelete(job
->status_buffer
);
602 job
->status_buffer
= NULL
;
605 printer
= job
->printer
;
607 update_job_attrs(job
, 0);
612 * Backend had errors; stop it...
615 int exit_code
; /* Exit code from backend */
619 * Convert the status to an exit code. Due to the way the W* macros are
620 * implemented on MacOS X (bug?), we have to store the exit status in a
621 * variable first and then convert...
624 exit_code
= -job
->status
;
625 if (WIFEXITED(exit_code
))
626 exit_code
= WEXITSTATUS(exit_code
);
628 exit_code
= job
->status
;
630 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Backend returned status %d (%s)",
632 exit_code
== CUPS_BACKEND_FAILED
? "failed" :
633 exit_code
== CUPS_BACKEND_AUTH_REQUIRED
?
634 "authentication required" :
635 exit_code
== CUPS_BACKEND_HOLD
? "hold job" :
636 exit_code
== CUPS_BACKEND_STOP
? "stop printer" :
637 exit_code
== CUPS_BACKEND_CANCEL
? "cancel job" :
638 exit_code
< 0 ? "crashed" : "unknown");
641 * Do what needs to be done...
647 case CUPS_BACKEND_FAILED
:
649 * Backend failure, use the error-policy to determine how to
653 cupsdStopJob(job
, 0);
655 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
658 * Mark the job as pending again - we'll retry on another
662 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
663 job
->state_value
= IPP_JOB_PENDING
;
667 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
670 * If the job was queued to a class or the error policy is
671 * "retry-current-job", try requeuing it... For faxes and retry-job
672 * queues, hold the current job for 5 minutes.
675 if ((job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
)) ||
676 !strcmp(printer
->error_policy
, "retry-current-job"))
678 else if ((printer
->type
& CUPS_PRINTER_FAX
) ||
679 !strcmp(printer
->error_policy
, "retry-job"))
682 * See how many times we've tried to send the job; if more than
683 * the limit, cancel the job.
688 if (job
->tries
>= JobRetryLimit
)
694 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
695 "Canceling job since it could not be "
696 "sent after %d tries.",
699 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
704 * Try again in N seconds...
707 set_hold_until(job
, time(NULL
) + JobRetryInterval
);
709 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
,
710 "Job held due to fax errors; please consult "
711 "the error_log file for details.");
712 cupsdSetPrinterState(printer
, IPP_PRINTER_IDLE
, 0);
715 else if (!strcmp(printer
->error_policy
, "abort-job"))
716 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
719 cupsdSetPrinterState(printer
, IPP_PRINTER_STOPPED
, 1);
721 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
722 "Job stopped due to backend errors; please consult "
723 "the error_log file for details.");
727 case CUPS_BACKEND_CANCEL
:
732 cupsdCancelJob(job
, 0, IPP_JOB_CANCELED
);
735 case CUPS_BACKEND_HOLD
:
740 cupsdStopJob(job
, 0);
742 cupsdSetJobHoldUntil(job
, "indefinite");
744 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
745 job
->state_value
= IPP_JOB_HELD
;
748 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
750 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
751 "Job held due to backend errors; please consult "
752 "the error_log file for details.");
755 case CUPS_BACKEND_STOP
:
757 * Stop the printer...
760 cupsdStopJob(job
, 0);
762 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
763 job
->state_value
= IPP_JOB_PENDING
;
766 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
768 cupsdSetPrinterState(printer
, IPP_PRINTER_STOPPED
, 1);
770 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
771 "Job stopped due to backend errors; please consult "
772 "the error_log file for details.");
775 case CUPS_BACKEND_AUTH_REQUIRED
:
776 cupsdStopJob(job
, 0);
778 cupsdSetJobHoldUntil(job
, "auth-info-required");
780 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
781 IPP_TAG_KEYWORD
)) == NULL
)
782 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
786 attr
->value_tag
= IPP_TAG_KEYWORD
;
787 cupsdSetString(&(attr
->values
[0].string
.text
),
788 "auth-info-required");
791 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
792 job
->state_value
= IPP_JOB_HELD
;
795 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
797 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
798 "Authentication is required for job %d.", job
->id
);
803 * Try printing another job...
808 else if (job
->status
> 0)
811 * Filter had errors; stop job...
814 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Job stopped due to filter errors.");
815 cupsdStopJob(job
, 1);
817 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
818 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
819 "Job stopped due to filter errors; please consult the "
820 "error_log file for details.");
826 * Job printed successfully; cancel it...
829 if (job
->current_file
< job
->num_files
)
832 * Start the next file in the job...
835 FilterLevel
-= job
->cost
;
836 start_job(job
, printer
);
841 * Close out this job...
844 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Completed successfully.");
845 cupsdCancelJob(job
, 0, IPP_JOB_COMPLETED
);
853 * 'cupsdFreeAllJobs()' - Free all jobs from memory.
857 cupsdFreeAllJobs(void)
859 cupsd_job_t
*job
; /* Current job */
870 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
872 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
874 cupsArrayRemove(Jobs
, job
);
875 cupsArrayRemove(ActiveJobs
, job
);
876 cupsArrayRemove(PrintingJobs
, job
);
881 cupsdReleaseSignals();
886 * 'cupsdFindJob()' - Find the specified job.
889 cupsd_job_t
* /* O - Job data */
890 cupsdFindJob(int id
) /* I - Job ID */
892 cupsd_job_t key
; /* Search key */
897 return ((cupsd_job_t
*)cupsArrayFind(Jobs
, &key
));
902 * 'cupsdGetPrinterJobCount()' - Get the number of pending, processing,
903 * or held jobs in a printer or class.
906 int /* O - Job count */
907 cupsdGetPrinterJobCount(
908 const char *dest
) /* I - Printer or class name */
910 int count
; /* Job count */
911 cupsd_job_t
*job
; /* Current job */
914 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
916 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
917 if (job
->dest
&& !strcasecmp(job
->dest
, dest
))
925 * 'cupsdGetUserJobCount()' - Get the number of pending, processing,
926 * or held jobs for a user.
929 int /* O - Job count */
930 cupsdGetUserJobCount(
931 const char *username
) /* I - Username */
933 int count
; /* Job count */
934 cupsd_job_t
*job
; /* Current job */
937 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
939 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
940 if (!strcasecmp(job
->username
, username
))
948 * 'cupsdHoldJob()' - Hold the specified job.
952 cupsdHoldJob(cupsd_job_t
*job
) /* I - Job data */
954 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdHoldJob: id = %d", job
->id
);
956 if (job
->state_value
== IPP_JOB_PROCESSING
)
957 cupsdStopJob(job
, 0);
961 DEBUG_puts("cupsdHoldJob: setting state to held...");
963 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
964 job
->state_value
= IPP_JOB_HELD
;
965 job
->current_file
= 0;
968 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
973 * 'cupsdLoadAllJobs()' - Load all jobs from disk.
977 cupsdLoadAllJobs(void)
979 char filename
[1024]; /* Full filename of job.cache file */
980 struct stat fileinfo
, /* Information on job.cache file */
981 dirinfo
; /* Information on RequestRoot dir */
986 * Create the job arrays as needed...
990 Jobs
= cupsArrayNew(compare_jobs
, NULL
);
993 ActiveJobs
= cupsArrayNew(compare_active_jobs
, NULL
);
996 PrintingJobs
= cupsArrayNew(compare_jobs
, NULL
);
999 * See whether the job.cache file is older than the RequestRoot directory...
1002 snprintf(filename
, sizeof(filename
), "%s/job.cache", CacheDir
);
1004 if (stat(filename
, &fileinfo
))
1006 fileinfo
.st_mtime
= 0;
1008 if (errno
!= ENOENT
)
1009 cupsdLogMessage(CUPSD_LOG_ERROR
,
1010 "Unable to get file information for \"%s\" - %s",
1011 filename
, strerror(errno
));
1014 if (stat(RequestRoot
, &dirinfo
))
1016 dirinfo
.st_mtime
= 0;
1018 if (errno
!= ENOENT
)
1019 cupsdLogMessage(CUPSD_LOG_ERROR
,
1020 "Unable to get directory information for \"%s\" - %s",
1021 RequestRoot
, strerror(errno
));
1025 * Load the most recent source for job data...
1028 if (dirinfo
.st_mtime
> fileinfo
.st_mtime
)
1030 load_request_root();
1032 load_next_job_id(filename
);
1035 load_job_cache(filename
);
1038 * Clean out old jobs as needed...
1041 if (MaxJobs
> 0 && cupsArrayCount(Jobs
) >= MaxJobs
)
1047 * 'cupsdLoadJob()' - Load a single job...
1051 cupsdLoadJob(cupsd_job_t
*job
) /* I - Job */
1053 char jobfile
[1024]; /* Job filename */
1054 cups_file_t
*fp
; /* Job file */
1055 int fileid
; /* Current file ID */
1056 ipp_attribute_t
*attr
; /* Job attribute */
1057 const char *dest
; /* Destination name */
1058 cupsd_printer_t
*destptr
; /* Pointer to destination */
1059 mime_type_t
**filetypes
; /* New filetypes array */
1060 int *compressions
; /* New compressions array */
1065 if (job
->state_value
> IPP_JOB_STOPPED
)
1066 job
->access_time
= time(NULL
);
1071 if ((job
->attrs
= ippNew()) == NULL
)
1073 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Ran out of memory for job attributes!");
1078 * Load job attributes...
1081 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Loading attributes...");
1083 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, job
->id
);
1084 if ((fp
= cupsFileOpen(jobfile
, "r")) == NULL
)
1086 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1087 "Unable to open job control file \"%s\" - %s!",
1088 jobfile
, strerror(errno
));
1089 ippDelete(job
->attrs
);
1094 if (ippReadIO(fp
, (ipp_iocb_t
)cupsFileRead
, 1, NULL
, job
->attrs
) != IPP_DATA
)
1096 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1097 "Unable to read job control file \"%s\"!",
1100 ippDelete(job
->attrs
);
1109 * Copy attribute data to the job object...
1112 if ((job
->state
= ippFindAttribute(job
->attrs
, "job-state",
1113 IPP_TAG_ENUM
)) == NULL
)
1115 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1116 "Missing or bad job-state attribute in control file!");
1117 ippDelete(job
->attrs
);
1123 job
->state_value
= (ipp_jstate_t
)job
->state
->values
[0].integer
;
1127 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1128 IPP_TAG_URI
)) == NULL
)
1130 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1131 "No job-printer-uri attribute in control file!");
1132 ippDelete(job
->attrs
);
1138 if ((dest
= cupsdValidateDest(attr
->values
[0].string
.text
, &(job
->dtype
),
1141 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1142 "Unable to queue job for destination \"%s\"!",
1143 attr
->values
[0].string
.text
);
1144 ippDelete(job
->attrs
);
1150 cupsdSetString(&job
->dest
, dest
);
1152 else if ((destptr
= cupsdFindDest(job
->dest
)) == NULL
)
1154 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1155 "Unable to queue job for destination \"%s\"!", job
->dest
);
1156 ippDelete(job
->attrs
);
1162 job
->sheets
= ippFindAttribute(job
->attrs
, "job-media-sheets-completed",
1164 job
->job_sheets
= ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_NAME
);
1168 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
1169 IPP_TAG_INTEGER
)) == NULL
)
1171 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1172 "Missing or bad job-priority attribute in control file!");
1173 ippDelete(job
->attrs
);
1179 job
->priority
= attr
->values
[0].integer
;
1184 if ((attr
= ippFindAttribute(job
->attrs
, "job-originating-user-name",
1185 IPP_TAG_NAME
)) == NULL
)
1187 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1188 "Missing or bad job-originating-user-name attribute in "
1190 ippDelete(job
->attrs
);
1196 cupsdSetString(&job
->username
, attr
->values
[0].string
.text
);
1200 * Set the job hold-until time and state...
1203 if (job
->state_value
== IPP_JOB_HELD
)
1205 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1206 IPP_TAG_KEYWORD
)) == NULL
)
1207 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1210 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
1213 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1214 job
->state_value
= IPP_JOB_PENDING
;
1217 else if (job
->state_value
== IPP_JOB_PROCESSING
)
1219 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1220 job
->state_value
= IPP_JOB_PENDING
;
1223 if (!job
->num_files
)
1226 * Find all the d##### files...
1229 for (fileid
= 1; fileid
< 10000; fileid
++)
1231 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
1234 if (access(jobfile
, 0))
1237 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
1238 "Auto-typing document file \"%s\"...", jobfile
);
1240 if (fileid
> job
->num_files
)
1242 if (job
->num_files
== 0)
1244 compressions
= (int *)calloc(fileid
, sizeof(int));
1245 filetypes
= (mime_type_t
**)calloc(fileid
, sizeof(mime_type_t
*));
1249 compressions
= (int *)realloc(job
->compressions
,
1250 sizeof(int) * fileid
);
1251 filetypes
= (mime_type_t
**)realloc(job
->filetypes
,
1252 sizeof(mime_type_t
*) *
1256 if (!compressions
|| !filetypes
)
1258 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1259 "Ran out of memory for job file types!");
1263 job
->compressions
= compressions
;
1264 job
->filetypes
= filetypes
;
1265 job
->num_files
= fileid
;
1268 job
->filetypes
[fileid
- 1] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
1269 job
->compressions
+ fileid
- 1);
1271 if (!job
->filetypes
[fileid
- 1])
1272 job
->filetypes
[fileid
- 1] = mimeType(MimeDatabase
, "application",
1278 * Load authentication information as needed...
1281 if (job
->state_value
< IPP_JOB_STOPPED
)
1283 snprintf(jobfile
, sizeof(jobfile
), "%s/a%05d", RequestRoot
, job
->id
);
1285 cupsdClearString(&job
->auth_username
);
1286 cupsdClearString(&job
->auth_domain
);
1287 cupsdClearString(&job
->auth_password
);
1289 if ((fp
= cupsFileOpen(jobfile
, "r")) != NULL
)
1291 int i
, /* Looping var */
1292 bytes
; /* Size of auth data */
1293 char line
[255], /* Line from file */
1294 data
[255]; /* Decoded data */
1298 i
< destptr
->num_auth_info_required
&&
1299 cupsFileGets(fp
, line
, sizeof(line
));
1302 bytes
= sizeof(data
);
1303 httpDecode64_2(data
, &bytes
, line
);
1305 if (!strcmp(destptr
->auth_info_required
[i
], "username"))
1306 cupsdSetStringf(&job
->auth_username
, "AUTH_USERNAME=%s", data
);
1307 else if (!strcmp(destptr
->auth_info_required
[i
], "domain"))
1308 cupsdSetStringf(&job
->auth_domain
, "AUTH_DOMAIN=%s", data
);
1309 else if (!strcmp(destptr
->auth_info_required
[i
], "password"))
1310 cupsdSetStringf(&job
->auth_password
, "AUTH_PASSWORD=%s", data
);
1316 job
->access_time
= time(NULL
);
1321 * 'cupsdMoveJob()' - Move the specified job to a different destination.
1325 cupsdMoveJob(cupsd_job_t
*job
, /* I - Job */
1326 cupsd_printer_t
*p
) /* I - Destination printer or class */
1328 ipp_attribute_t
*attr
; /* job-printer-uri attribute */
1329 const char *olddest
; /* Old destination */
1330 cupsd_printer_t
*oldp
; /* Old pointer */
1334 * Don't move completed jobs...
1337 if (job
->state_value
> IPP_JOB_STOPPED
)
1341 * Get the old destination...
1344 olddest
= job
->dest
;
1347 oldp
= job
->printer
;
1349 oldp
= cupsdFindDest(olddest
);
1352 * Change the destination information...
1355 if (job
->state_value
== IPP_JOB_PROCESSING
)
1356 cupsdStopJob(job
, 0);
1360 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, oldp
, job
,
1361 "Job #%d moved from %s to %s.", job
->id
, olddest
,
1364 cupsdSetString(&job
->dest
, p
->name
);
1365 job
->dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_REMOTE
|
1366 CUPS_PRINTER_IMPLICIT
);
1368 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1369 IPP_TAG_URI
)) != NULL
)
1370 cupsdSetString(&(attr
->values
[0].string
.text
), p
->uri
);
1372 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, p
, job
,
1373 "Job #%d moved from %s to %s.", job
->id
, olddest
,
1377 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1382 * 'cupsdReleaseJob()' - Release the specified job.
1386 cupsdReleaseJob(cupsd_job_t
*job
) /* I - Job */
1388 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdReleaseJob: id = %d", job
->id
);
1390 if (job
->state_value
== IPP_JOB_HELD
)
1393 * Add trailing banner as needed...
1396 if (job
->pending_timeout
)
1397 cupsdTimeoutJob(job
);
1399 DEBUG_puts("cupsdReleaseJob: setting state to pending...");
1401 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1402 job
->state_value
= IPP_JOB_PENDING
;
1404 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1410 * 'cupsdRestartJob()' - Restart the specified job.
1414 cupsdRestartJob(cupsd_job_t
*job
) /* I - Job */
1416 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdRestartJob: id = %d", job
->id
);
1418 if (job
->state_value
== IPP_JOB_STOPPED
|| job
->num_files
)
1420 ipp_jstate_t old_state
; /* Old job state */
1425 old_state
= job
->state_value
;
1428 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1429 job
->state_value
= IPP_JOB_PENDING
;
1432 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1434 if (old_state
> IPP_JOB_STOPPED
)
1435 cupsArrayAdd(ActiveJobs
, job
);
1441 * 'cupsdSaveAllJobs()' - Save a summary of all jobs to disk.
1445 cupsdSaveAllJobs(void)
1447 int i
; /* Looping var */
1448 cups_file_t
*fp
; /* Job cache file */
1449 char temp
[1024]; /* Temporary string */
1450 cupsd_job_t
*job
; /* Current job */
1451 time_t curtime
; /* Current time */
1452 struct tm
*curdate
; /* Current date */
1455 snprintf(temp
, sizeof(temp
), "%s/job.cache", CacheDir
);
1456 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
1458 cupsdLogMessage(CUPSD_LOG_ERROR
,
1459 "Unable to create job cache file \"%s\" - %s",
1460 temp
, strerror(errno
));
1464 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving job cache file \"%s\"...", temp
);
1467 * Restrict access to the file...
1470 fchown(cupsFileNumber(fp
), getuid(), Group
);
1471 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
1474 * Write a small header to the file...
1477 curtime
= time(NULL
);
1478 curdate
= localtime(&curtime
);
1479 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
1481 cupsFilePuts(fp
, "# Job cache file for " CUPS_SVERSION
"\n");
1482 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
1483 cupsFilePrintf(fp
, "NextJobId %d\n", NextJobId
);
1486 * Write each job known to the system...
1489 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1491 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1493 cupsFilePrintf(fp
, "<Job %d>\n", job
->id
);
1494 cupsFilePrintf(fp
, "State %d\n", job
->state_value
);
1495 cupsFilePrintf(fp
, "Priority %d\n", job
->priority
);
1496 cupsFilePrintf(fp
, "Username %s\n", job
->username
);
1497 cupsFilePrintf(fp
, "Destination %s\n", job
->dest
);
1498 cupsFilePrintf(fp
, "DestType %d\n", job
->dtype
);
1499 cupsFilePrintf(fp
, "NumFiles %d\n", job
->num_files
);
1500 for (i
= 0; i
< job
->num_files
; i
++)
1501 cupsFilePrintf(fp
, "File %d %s/%s %d\n", i
+ 1, job
->filetypes
[i
]->super
,
1502 job
->filetypes
[i
]->type
, job
->compressions
[i
]);
1503 cupsFilePuts(fp
, "</Job>\n");
1511 * 'cupsdSaveJob()' - Save a job to disk.
1515 cupsdSaveJob(cupsd_job_t
*job
) /* I - Job */
1517 char filename
[1024]; /* Job control filename */
1518 cups_file_t
*fp
; /* Job file */
1521 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSaveJob(job=%p(%d)): job->attrs=%p",
1522 job
, job
->id
, job
->attrs
);
1524 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
, job
->id
);
1526 if ((fp
= cupsFileOpen(filename
, "w")) == NULL
)
1528 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1529 "Unable to create job control file \"%s\" - %s.",
1530 filename
, strerror(errno
));
1534 fchmod(cupsFileNumber(fp
), 0600);
1535 fchown(cupsFileNumber(fp
), RunUser
, Group
);
1537 job
->attrs
->state
= IPP_IDLE
;
1539 if (ippWriteIO(fp
, (ipp_iocb_t
)cupsFileWrite
, 1, NULL
,
1540 job
->attrs
) != IPP_DATA
)
1541 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Unable to write job control file!");
1550 * 'cupsdSetJobHoldUntil()' - Set the hold time for a job...
1554 cupsdSetJobHoldUntil(cupsd_job_t
*job
, /* I - Job */
1555 const char *when
) /* I - When to resume */
1557 time_t curtime
; /* Current time */
1558 struct tm
*curdate
; /* Current date */
1559 int hour
; /* Hold hour */
1560 int minute
; /* Hold minute */
1561 int second
; /* Hold second */
1564 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetJobHoldUntil(%d, \"%s\")",
1569 if (!strcmp(when
, "indefinite") || !strcmp(when
, "auth-info-required"))
1572 * Hold indefinitely...
1575 job
->hold_until
= 0;
1577 else if (!strcmp(when
, "day-time"))
1580 * Hold to 6am the next morning unless local time is < 6pm.
1583 curtime
= time(NULL
);
1584 curdate
= localtime(&curtime
);
1586 if (curdate
->tm_hour
< 18)
1587 job
->hold_until
= curtime
;
1589 job
->hold_until
= curtime
+
1590 ((29 - curdate
->tm_hour
) * 60 + 59 -
1591 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1593 else if (!strcmp(when
, "evening") || !strcmp(when
, "night"))
1596 * Hold to 6pm unless local time is > 6pm or < 6am.
1599 curtime
= time(NULL
);
1600 curdate
= localtime(&curtime
);
1602 if (curdate
->tm_hour
< 6 || curdate
->tm_hour
>= 18)
1603 job
->hold_until
= curtime
;
1605 job
->hold_until
= curtime
+
1606 ((17 - curdate
->tm_hour
) * 60 + 59 -
1607 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1609 else if (!strcmp(when
, "second-shift"))
1612 * Hold to 4pm unless local time is > 4pm.
1615 curtime
= time(NULL
);
1616 curdate
= localtime(&curtime
);
1618 if (curdate
->tm_hour
>= 16)
1619 job
->hold_until
= curtime
;
1621 job
->hold_until
= curtime
+
1622 ((15 - curdate
->tm_hour
) * 60 + 59 -
1623 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1625 else if (!strcmp(when
, "third-shift"))
1628 * Hold to 12am unless local time is < 8am.
1631 curtime
= time(NULL
);
1632 curdate
= localtime(&curtime
);
1634 if (curdate
->tm_hour
< 8)
1635 job
->hold_until
= curtime
;
1637 job
->hold_until
= curtime
+
1638 ((23 - curdate
->tm_hour
) * 60 + 59 -
1639 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1641 else if (!strcmp(when
, "weekend"))
1644 * Hold to weekend unless we are in the weekend.
1647 curtime
= time(NULL
);
1648 curdate
= localtime(&curtime
);
1650 if (curdate
->tm_wday
|| curdate
->tm_wday
== 6)
1651 job
->hold_until
= curtime
;
1653 job
->hold_until
= curtime
+
1654 (((5 - curdate
->tm_wday
) * 24 +
1655 (17 - curdate
->tm_hour
)) * 60 + 59 -
1656 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1658 else if (sscanf(when
, "%d:%d:%d", &hour
, &minute
, &second
) >= 2)
1661 * Hold to specified GMT time (HH:MM or HH:MM:SS)...
1664 curtime
= time(NULL
);
1665 curdate
= gmtime(&curtime
);
1667 job
->hold_until
= curtime
+
1668 ((hour
- curdate
->tm_hour
) * 60 + minute
-
1669 curdate
->tm_min
) * 60 + second
- curdate
->tm_sec
;
1672 * Hold until next day as needed...
1675 if (job
->hold_until
< curtime
)
1676 job
->hold_until
+= 24 * 60 * 60;
1679 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetJobHoldUntil: hold_until = %d",
1680 (int)job
->hold_until
);
1685 * 'cupsdSetJobPriority()' - Set the priority of a job, moving it up/down in
1686 * the list as needed.
1690 cupsdSetJobPriority(
1691 cupsd_job_t
*job
, /* I - Job ID */
1692 int priority
) /* I - New priority (0 to 100) */
1694 ipp_attribute_t
*attr
; /* Job attribute */
1698 * Don't change completed jobs...
1701 if (job
->state_value
>= IPP_JOB_PROCESSING
)
1705 * Set the new priority and re-add the job into the active list...
1708 cupsArrayRemove(ActiveJobs
, job
);
1710 job
->priority
= priority
;
1712 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
1713 IPP_TAG_INTEGER
)) != NULL
)
1714 attr
->values
[0].integer
= priority
;
1716 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-priority",
1719 cupsArrayAdd(ActiveJobs
, job
);
1722 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1727 * 'cupsdStopAllJobs()' - Stop all print jobs.
1731 cupsdStopAllJobs(int force
) /* I - 1 = Force all filters to stop */
1733 cupsd_job_t
*job
; /* Current job */
1736 DEBUG_puts("cupsdStopAllJobs()");
1738 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
1740 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
1741 if (job
->state_value
== IPP_JOB_PROCESSING
)
1743 cupsdStopJob(job
, force
);
1744 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1745 job
->state_value
= IPP_JOB_PENDING
;
1751 * 'cupsdStopJob()' - Stop a print job.
1755 cupsdStopJob(cupsd_job_t
*job
, /* I - Job */
1756 int force
) /* I - 1 = Force all filters to stop */
1758 int i
; /* Looping var */
1761 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "cupsdStopJob: force = %d", force
);
1763 if (job
->state_value
!= IPP_JOB_PROCESSING
)
1766 FilterLevel
-= job
->cost
;
1768 if (job
->printer
->state
== IPP_PRINTER_PROCESSING
)
1769 cupsdSetPrinterState(job
->printer
, IPP_PRINTER_IDLE
, 0);
1771 job
->state
->values
[0].integer
= IPP_JOB_STOPPED
;
1772 job
->state_value
= IPP_JOB_STOPPED
;
1773 job
->printer
->job
= NULL
;
1774 job
->printer
= NULL
;
1776 job
->current_file
--;
1778 for (i
= 0; job
->filters
[i
]; i
++)
1779 if (job
->filters
[i
] > 0)
1781 cupsdEndProcess(job
->filters
[i
], force
);
1782 job
->filters
[i
] = 0;
1785 if (job
->backend
> 0)
1787 cupsdEndProcess(job
->backend
, force
);
1791 cupsdDestroyProfile(job
->profile
);
1792 job
->profile
= NULL
;
1794 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "Closing print pipes [ %d %d ]...",
1795 job
->print_pipes
[0], job
->print_pipes
[1]);
1797 cupsdClosePipe(job
->print_pipes
);
1799 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "Closing back pipes [ %d %d ]...",
1800 job
->back_pipes
[0], job
->back_pipes
[1]);
1802 cupsdClosePipe(job
->back_pipes
);
1804 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "Closing side pipes [ %d %d ]...",
1805 job
->side_pipes
[0], job
->side_pipes
[1]);
1807 cupsdClosePipe(job
->side_pipes
);
1809 if (job
->status_buffer
)
1812 * Close the pipe and clear the input bit.
1815 cupsdRemoveSelect(job
->status_buffer
->fd
);
1817 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "Closing status pipes [ %d %d ]...",
1818 job
->status_pipes
[0], job
->status_pipes
[1]);
1820 cupsdClosePipe(job
->status_pipes
);
1821 cupsdStatBufDelete(job
->status_buffer
);
1823 job
->status_buffer
= NULL
;
1829 * 'cupsdUnloadCompletedJobs()' - Flush completed job history from memory.
1833 cupsdUnloadCompletedJobs(void)
1835 cupsd_job_t
*job
; /* Current job */
1836 time_t expire
; /* Expiration time */
1839 expire
= time(NULL
) - 60;
1841 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1843 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1844 if (job
->attrs
&& job
->state_value
>= IPP_JOB_STOPPED
&&
1845 job
->access_time
< expire
)
1856 * 'compare_active_jobs()' - Compare the job IDs and priorities of two jobs.
1859 static int /* O - Difference */
1860 compare_active_jobs(void *first
, /* I - First job */
1861 void *second
, /* I - Second job */
1862 void *data
) /* I - App data (not used) */
1864 int diff
; /* Difference */
1867 if ((diff
= ((cupsd_job_t
*)second
)->priority
-
1868 ((cupsd_job_t
*)first
)->priority
) != 0)
1871 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
1876 * 'compare_jobs()' - Compare the job IDs of two jobs.
1879 static int /* O - Difference */
1880 compare_jobs(void *first
, /* I - First job */
1881 void *second
, /* I - Second job */
1882 void *data
) /* I - App data (not used) */
1884 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
1889 * 'ipp_length()' - Compute the size of the buffer needed to hold
1890 * the textual IPP attributes.
1893 static int /* O - Size of attribute buffer */
1894 ipp_length(ipp_t
*ipp
) /* I - IPP request */
1896 int bytes
; /* Number of bytes */
1897 int i
; /* Looping var */
1898 ipp_attribute_t
*attr
; /* Current attribute */
1902 * Loop through all attributes...
1907 for (attr
= ipp
->attrs
; attr
!= NULL
; attr
= attr
->next
)
1910 * Skip attributes that won't be sent to filters...
1913 if (attr
->value_tag
== IPP_TAG_MIMETYPE
||
1914 attr
->value_tag
== IPP_TAG_NAMELANG
||
1915 attr
->value_tag
== IPP_TAG_TEXTLANG
||
1916 attr
->value_tag
== IPP_TAG_URI
||
1917 attr
->value_tag
== IPP_TAG_URISCHEME
)
1920 if (strncmp(attr
->name
, "time-", 5) == 0)
1924 * Add space for a leading space and commas between each value.
1925 * For the first attribute, the leading space isn't used, so the
1926 * extra byte can be used as the nul terminator...
1929 bytes
++; /* " " separator */
1930 bytes
+= attr
->num_values
; /* "," separators */
1933 * Boolean attributes appear as "foo,nofoo,foo,nofoo", while
1934 * other attributes appear as "foo=value1,value2,...,valueN".
1937 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
1938 bytes
+= strlen(attr
->name
);
1940 bytes
+= attr
->num_values
* strlen(attr
->name
);
1943 * Now add the size required for each value in the attribute...
1946 switch (attr
->value_tag
)
1948 case IPP_TAG_INTEGER
:
1951 * Minimum value of a signed integer is -2147483647, or 11 digits.
1954 bytes
+= attr
->num_values
* 11;
1957 case IPP_TAG_BOOLEAN
:
1959 * Add two bytes for each false ("no") value...
1962 for (i
= 0; i
< attr
->num_values
; i
++)
1963 if (!attr
->values
[i
].boolean
)
1967 case IPP_TAG_RANGE
:
1969 * A range is two signed integers separated by a hyphen, or
1970 * 23 characters max.
1973 bytes
+= attr
->num_values
* 23;
1976 case IPP_TAG_RESOLUTION
:
1978 * A resolution is two signed integers separated by an "x" and
1979 * suffixed by the units, or 26 characters max.
1982 bytes
+= attr
->num_values
* 26;
1985 case IPP_TAG_STRING
:
1988 case IPP_TAG_KEYWORD
:
1989 case IPP_TAG_CHARSET
:
1990 case IPP_TAG_LANGUAGE
:
1993 * Strings can contain characters that need quoting. We need
1994 * at least 2 * len + 2 characters to cover the quotes and
1995 * any backslashes in the string.
1998 for (i
= 0; i
< attr
->num_values
; i
++)
1999 bytes
+= 2 * strlen(attr
->values
[i
].string
.text
) + 2;
2003 break; /* anti-compiler-warning-code */
2012 * 'load_job_cache()' - Load jobs from the job.cache file.
2016 load_job_cache(const char *filename
) /* I - job.cache filename */
2018 cups_file_t
*fp
; /* job.cache file */
2019 char line
[1024], /* Line buffer */
2020 *value
; /* Value on line */
2021 int linenum
; /* Line number in file */
2022 cupsd_job_t
*job
; /* Current job */
2023 int jobid
; /* Job ID */
2024 char jobfile
[1024]; /* Job filename */
2028 * Open the job.cache file...
2031 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
2033 if (errno
!= ENOENT
)
2034 cupsdLogMessage(CUPSD_LOG_ERROR
,
2035 "Unable to open job cache file \"%s\": %s",
2036 filename
, strerror(errno
));
2038 load_request_root();
2044 * Read entries from the job cache file and create jobs as needed.
2047 cupsdLogMessage(CUPSD_LOG_INFO
, "Loading job cache file \"%s\"...",
2053 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
2055 if (!strcasecmp(line
, "NextJobId"))
2058 NextJobId
= atoi(value
);
2060 else if (!strcasecmp(line
, "<Job"))
2064 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing </Job> directive on line %d!",
2071 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing job ID on line %d!", linenum
);
2075 jobid
= atoi(value
);
2079 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad job ID %d on line %d!", jobid
,
2084 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, jobid
);
2085 if (access(jobfile
, 0))
2087 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Job %d] Files have gone away!",
2092 job
= calloc(1, sizeof(cupsd_job_t
));
2095 cupsdLogMessage(CUPSD_LOG_EMERG
,
2096 "[Job %d] Unable to allocate memory for job!", jobid
);
2101 job
->back_pipes
[0] = -1;
2102 job
->back_pipes
[1] = -1;
2103 job
->print_pipes
[0] = -1;
2104 job
->print_pipes
[1] = -1;
2105 job
->side_pipes
[0] = -1;
2106 job
->side_pipes
[1] = -1;
2107 job
->status_pipes
[0] = -1;
2108 job
->status_pipes
[1] = -1;
2110 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Loading from cache...");
2114 cupsdLogMessage(CUPSD_LOG_ERROR
,
2115 "Missing <Job #> directive on line %d!", linenum
);
2118 else if (!strcasecmp(line
, "</Job>"))
2120 cupsArrayAdd(Jobs
, job
);
2122 if (job
->state_value
<= IPP_JOB_STOPPED
)
2124 cupsArrayAdd(ActiveJobs
, job
);
2132 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing value on line %d!", linenum
);
2135 else if (!strcasecmp(line
, "State"))
2137 job
->state_value
= (ipp_jstate_t
)atoi(value
);
2139 if (job
->state_value
< IPP_JOB_PENDING
)
2140 job
->state_value
= IPP_JOB_PENDING
;
2141 else if (job
->state_value
> IPP_JOB_COMPLETED
)
2142 job
->state_value
= IPP_JOB_COMPLETED
;
2144 else if (!strcasecmp(line
, "Priority"))
2146 job
->priority
= atoi(value
);
2148 else if (!strcasecmp(line
, "Username"))
2150 cupsdSetString(&job
->username
, value
);
2152 else if (!strcasecmp(line
, "Destination"))
2154 cupsdSetString(&job
->dest
, value
);
2156 else if (!strcasecmp(line
, "DestType"))
2158 job
->dtype
= (cups_ptype_t
)atoi(value
);
2160 else if (!strcasecmp(line
, "NumFiles"))
2162 job
->num_files
= atoi(value
);
2164 if (job
->num_files
< 0)
2166 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad NumFiles value %d on line %d!",
2167 job
->num_files
, linenum
);
2172 if (job
->num_files
> 0)
2174 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-001", RequestRoot
,
2176 if (access(jobfile
, 0))
2178 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Data files have gone away!");
2183 job
->filetypes
= calloc(job
->num_files
, sizeof(mime_type_t
*));
2184 job
->compressions
= calloc(job
->num_files
, sizeof(int));
2186 if (!job
->filetypes
|| !job
->compressions
)
2188 cupsdLogJob(job
, CUPSD_LOG_EMERG
,
2189 "Unable to allocate memory for %d files!",
2195 else if (!strcasecmp(line
, "File"))
2197 int number
, /* File number */
2198 compression
; /* Compression value */
2199 char super
[MIME_MAX_SUPER
], /* MIME super type */
2200 type
[MIME_MAX_TYPE
]; /* MIME type */
2203 if (sscanf(value
, "%d%*[ \t]%15[^/]/%255s%d", &number
, super
, type
,
2206 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File on line %d!", linenum
);
2210 if (number
< 1 || number
> job
->num_files
)
2212 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File number %d on line %d!",
2219 job
->compressions
[number
] = compression
;
2220 job
->filetypes
[number
] = mimeType(MimeDatabase
, super
, type
);
2222 if (!job
->filetypes
[number
])
2225 * If the original MIME type is unknown, auto-type it!
2228 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
2229 "Unknown MIME type %s/%s for file %d!",
2230 super
, type
, number
+ 1);
2232 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
2233 job
->id
, number
+ 1);
2234 job
->filetypes
[number
] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
2235 job
->compressions
+ number
);
2238 * If that didn't work, assume it is raw...
2241 if (!job
->filetypes
[number
])
2242 job
->filetypes
[number
] = mimeType(MimeDatabase
, "application",
2247 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unknown %s directive on line %d!",
2256 * 'load_next_job_id()' - Load the NextJobId value from the job.cache file.
2260 load_next_job_id(const char *filename
) /* I - job.cache filename */
2262 cups_file_t
*fp
; /* job.cache file */
2263 char line
[1024], /* Line buffer */
2264 *value
; /* Value on line */
2265 int linenum
; /* Line number in file */
2266 int next_job_id
; /* NextJobId value from line */
2270 * Read the NextJobId directive from the job.cache file and use
2271 * the value (if any).
2274 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
2276 if (errno
!= ENOENT
)
2277 cupsdLogMessage(CUPSD_LOG_ERROR
,
2278 "Unable to open job cache file \"%s\": %s",
2279 filename
, strerror(errno
));
2284 cupsdLogMessage(CUPSD_LOG_INFO
,
2285 "Loading NextJobId from job cache file \"%s\"...", filename
);
2289 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
2291 if (!strcasecmp(line
, "NextJobId"))
2295 next_job_id
= atoi(value
);
2297 if (next_job_id
> NextJobId
)
2298 NextJobId
= next_job_id
;
2309 * 'load_request_root()' - Load jobs from the RequestRoot directory.
2313 load_request_root(void)
2315 cups_dir_t
*dir
; /* Directory */
2316 cups_dentry_t
*dent
; /* Directory entry */
2317 cupsd_job_t
*job
; /* New job */
2321 * Open the requests directory...
2324 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Scanning %s for jobs...", RequestRoot
);
2326 if ((dir
= cupsDirOpen(RequestRoot
)) == NULL
)
2328 cupsdLogMessage(CUPSD_LOG_ERROR
,
2329 "Unable to open spool directory \"%s\": %s",
2330 RequestRoot
, strerror(errno
));
2335 * Read all the c##### files...
2338 while ((dent
= cupsDirRead(dir
)) != NULL
)
2339 if (strlen(dent
->filename
) >= 6 && dent
->filename
[0] == 'c')
2342 * Allocate memory for the job...
2345 if ((job
= calloc(sizeof(cupsd_job_t
), 1)) == NULL
)
2347 cupsdLogMessage(CUPSD_LOG_ERROR
, "Ran out of memory for jobs!");
2353 * Assign the job ID...
2356 job
->id
= atoi(dent
->filename
+ 1);
2357 job
->back_pipes
[0] = -1;
2358 job
->back_pipes
[1] = -1;
2359 job
->print_pipes
[0] = -1;
2360 job
->print_pipes
[1] = -1;
2361 job
->side_pipes
[0] = -1;
2362 job
->side_pipes
[1] = -1;
2363 job
->status_pipes
[0] = -1;
2364 job
->status_pipes
[1] = -1;
2366 if (job
->id
>= NextJobId
)
2367 NextJobId
= job
->id
+ 1;
2376 * Insert the job into the array, sorting by job priority and ID...
2379 cupsArrayAdd(Jobs
, job
);
2381 if (job
->state_value
<= IPP_JOB_STOPPED
)
2382 cupsArrayAdd(ActiveJobs
, job
);
2392 * 'set_time()' - Set one of the "time-at-xyz" attributes...
2396 set_time(cupsd_job_t
*job
, /* I - Job to update */
2397 const char *name
) /* I - Name of attribute */
2399 ipp_attribute_t
*attr
; /* Time attribute */
2402 if ((attr
= ippFindAttribute(job
->attrs
, name
, IPP_TAG_ZERO
)) != NULL
)
2404 attr
->value_tag
= IPP_TAG_INTEGER
;
2405 attr
->values
[0].integer
= time(NULL
);
2411 * 'set_hold_until()' - Set the hold time and update job-hold-until attribute...
2415 set_hold_until(cupsd_job_t
*job
, /* I - Job to update */
2416 time_t holdtime
) /* I - Hold until time */
2418 ipp_attribute_t
*attr
; /* job-hold-until attribute */
2419 struct tm
*holddate
; /* Hold date */
2420 char holdstr
[64]; /* Hold time */
2424 * Set the hold_until value and hold the job...
2427 cupsdLogMessage(CUPSD_LOG_DEBUG
, "set_hold_until: hold_until = %d",
2430 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
2431 job
->state_value
= IPP_JOB_HELD
;
2432 job
->hold_until
= holdtime
;
2435 * Update the job-hold-until attribute with a string representing GMT
2436 * time (HH:MM:SS)...
2439 holddate
= gmtime(&holdtime
);
2440 snprintf(holdstr
, sizeof(holdstr
), "%d:%d:%d", holddate
->tm_hour
,
2441 holddate
->tm_min
, holddate
->tm_sec
);
2443 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2444 IPP_TAG_KEYWORD
)) == NULL
)
2445 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2448 * Either add the attribute or update the value of the existing one
2452 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "job-hold-until",
2455 cupsdSetString(&attr
->values
[0].string
.text
, holdstr
);
2458 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2463 * 'start_job()' - Start a print job.
2467 start_job(cupsd_job_t
*job
, /* I - Job ID */
2468 cupsd_printer_t
*printer
) /* I - Printer to print job */
2470 int i
; /* Looping var */
2471 int slot
; /* Pipe slot */
2472 cups_array_t
*filters
, /* Filters for job */
2473 *prefilters
; /* Filters with prefilters */
2474 mime_filter_t
*filter
, /* Current filter */
2475 *prefilter
, /* Prefilter */
2476 port_monitor
; /* Port monitor filter */
2477 char method
[255], /* Method for output */
2478 *optptr
, /* Pointer to options */
2479 *valptr
; /* Pointer in value string */
2480 ipp_attribute_t
*attr
; /* Current attribute */
2481 struct stat backinfo
; /* Backend file information */
2482 int backroot
; /* Run backend as root? */
2483 int pid
; /* Process ID of new filter process */
2484 int banner_page
; /* 1 if banner page, 0 otherwise */
2485 int filterfds
[2][2];/* Pipes used between filters */
2486 int envc
; /* Number of environment variables */
2487 char **argv
, /* Filter command-line arguments */
2488 filename
[1024], /* Job filename */
2489 command
[1024], /* Full path to command */
2490 jobid
[255], /* Job ID string */
2491 title
[IPP_MAX_NAME
],
2492 /* Job title string */
2493 copies
[255], /* # copies string */
2494 *envp
[MAX_ENV
+ 19],
2495 /* Environment variables */
2496 charset
[255], /* CHARSET env variable */
2497 class_name
[255],/* CLASS env variable */
2498 classification
[1024],
2499 /* CLASSIFICATION env variable */
2501 /* CONTENT_TYPE env variable */
2503 /* DEVICE_URI env variable */
2504 final_content_type
[1024],
2505 /* FINAL_CONTENT_TYPE env variable */
2506 lang
[255], /* LANG env variable */
2508 apple_language
[255],
2509 /* APPLE_LANGUAGE env variable */
2510 #endif /* __APPLE__ */
2511 ppd
[1024], /* PPD env variable */
2513 /* PRINTER_INFO env variable */
2514 printer_location
[255],
2515 /* PRINTER_LOCATION env variable */
2517 /* PRINTER env variable */
2519 /* RIP_MAX_CACHE env variable */
2520 static char *options
= NULL
;/* Full list of options */
2521 static int optlength
= 0; /* Length of option buffer */
2524 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "start_job: file = %d/%d",
2525 job
->current_file
, job
->num_files
);
2527 if (job
->num_files
== 0)
2529 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "No files, canceling job!");
2530 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2535 * Figure out what filters are required to convert from
2536 * the source to the destination type...
2545 * Remote jobs and raw queues go directly to the printer without
2549 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Sending job to queue tagged as raw...");
2556 * Local jobs get filtered...
2559 filters
= mimeFilter(MimeDatabase
, job
->filetypes
[job
->current_file
],
2560 printer
->filetype
, &(job
->cost
));
2564 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
2565 "Unable to convert file %d to printable format!",
2567 cupsdLogMessage(CUPSD_LOG_INFO
,
2568 "Hint: Do you have Ghostscript installed?");
2570 if (LogLevel
< CUPSD_LOG_DEBUG
)
2571 cupsdLogMessage(CUPSD_LOG_INFO
,
2572 "Hint: Try setting the LogLevel to \"debug\".");
2574 job
->current_file
++;
2576 if (job
->current_file
== job
->num_files
)
2577 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2583 * Remove NULL ("-") filters...
2586 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
2588 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
2589 if (!strcmp(filter
->filter
, "-"))
2590 cupsArrayRemove(filters
, filter
);
2592 if (cupsArrayCount(filters
) == 0)
2594 cupsArrayDelete(filters
);
2599 * If this printer has any pre-filters, insert the required pre-filter
2600 * in the filters array...
2603 if (printer
->prefiltertype
&& filters
)
2605 prefilters
= cupsArrayNew(NULL
, NULL
);
2607 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
2609 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
2611 if ((prefilter
= mimeFilterLookup(MimeDatabase
, filter
->src
,
2612 printer
->prefiltertype
)))
2614 cupsArrayAdd(prefilters
, prefilter
);
2615 job
->cost
+= prefilter
->cost
;
2618 cupsArrayAdd(prefilters
, filter
);
2621 cupsArrayDelete(filters
);
2622 filters
= prefilters
;
2627 * Set a minimum cost of 100 for all jobs so that FilterLimit
2628 * works with raw queues and other low-cost paths.
2631 if (job
->cost
< 100)
2635 * See if the filter cost is too high...
2638 if ((FilterLevel
+ job
->cost
) > FilterLimit
&& FilterLevel
> 0 &&
2642 * Don't print this job quite yet...
2645 cupsArrayDelete(filters
);
2647 cupsdLogJob(job
, CUPSD_LOG_INFO
,
2648 "Holding because filter limit has been reached.");
2649 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
2650 "start_job: file=%d, cost=%d, level=%d, limit=%d",
2651 job
->current_file
, job
->cost
, FilterLevel
,
2656 FilterLevel
+= job
->cost
;
2659 * Add decompression/raw filter as needed...
2662 if ((!printer
->raw
&& job
->compressions
[job
->current_file
]) ||
2663 (!filters
&& !printer
->remote
&&
2664 (job
->num_files
> 1 || !strncmp(printer
->device_uri
, "file:", 5))))
2667 * Add gziptoany filter to the front of the list...
2671 filters
= cupsArrayNew(NULL
, NULL
);
2673 if (!cupsArrayInsert(filters
, &gziptoany_filter
))
2675 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
2676 "Unable to add decompression filter - %s", strerror(errno
));
2678 cupsArrayDelete(filters
);
2680 job
->current_file
++;
2682 if (job
->current_file
== job
->num_files
)
2683 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2690 * Add port monitor, if any...
2693 if (printer
->port_monitor
)
2696 * Add port monitor to the end of the list...
2700 filters
= cupsArrayNew(NULL
, NULL
);
2702 port_monitor
.src
= NULL
;
2703 port_monitor
.dst
= NULL
;
2704 port_monitor
.cost
= 0;
2706 snprintf(port_monitor
.filter
, sizeof(port_monitor
.filter
),
2707 "%s/monitor/%s", ServerBin
, printer
->port_monitor
);
2709 if (!cupsArrayAdd(filters
, &port_monitor
))
2711 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
2712 "Unable to add port monitor - %s", strerror(errno
));
2714 cupsArrayDelete(filters
);
2716 job
->current_file
++;
2718 if (job
->current_file
== job
->num_files
)
2719 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2726 * Make sure we don't go over the "MAX_FILTERS" limit...
2729 if (cupsArrayCount(filters
) > MAX_FILTERS
)
2731 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
2732 "Too many filters (%d > %d), unable to print!",
2733 cupsArrayCount(filters
), MAX_FILTERS
);
2735 cupsArrayDelete(filters
);
2736 cupsdCancelJob(job
, 0, IPP_JOB_STOPPED
);
2742 * Update the printer and job state to "processing"...
2745 job
->state
->values
[0].integer
= IPP_JOB_PROCESSING
;
2746 job
->state_value
= IPP_JOB_PROCESSING
;
2749 job
->printer
= printer
;
2752 cupsdSetPrinterState(printer
, IPP_PRINTER_PROCESSING
, 0);
2754 if (job
->current_file
== 0)
2757 * Add to the printing list...
2760 cupsArrayAdd(PrintingJobs
, job
);
2761 cupsdSetBusyState();
2764 * Set the processing time...
2767 set_time(job
, "time-at-processing");
2770 * Create the backchannel pipes and make them non-blocking...
2773 cupsdOpenPipe(job
->back_pipes
);
2775 fcntl(job
->back_pipes
[0], F_SETFL
,
2776 fcntl(job
->back_pipes
[0], F_GETFL
) | O_NONBLOCK
);
2778 fcntl(job
->back_pipes
[1], F_SETFL
,
2779 fcntl(job
->back_pipes
[1], F_GETFL
) | O_NONBLOCK
);
2782 * Create the side-channel pipes and make them non-blocking...
2785 socketpair(AF_LOCAL
, SOCK_STREAM
, 0, job
->side_pipes
);
2787 fcntl(job
->side_pipes
[0], F_SETFL
,
2788 fcntl(job
->side_pipes
[0], F_GETFL
) | O_NONBLOCK
);
2790 fcntl(job
->side_pipes
[1], F_SETFL
,
2791 fcntl(job
->side_pipes
[1], F_GETFL
) | O_NONBLOCK
);
2795 * Determine if we are printing a banner page or not...
2798 if (job
->job_sheets
== NULL
)
2800 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "No job-sheets attribute.");
2801 if ((job
->job_sheets
=
2802 ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_ZERO
)) != NULL
)
2803 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
2804 "... but someone added one without setting job_sheets!");
2806 else if (job
->job_sheets
->num_values
== 1)
2807 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "job-sheets=%s",
2808 job
->job_sheets
->values
[0].string
.text
);
2810 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "job-sheets=%s,%s",
2811 job
->job_sheets
->values
[0].string
.text
,
2812 job
->job_sheets
->values
[1].string
.text
);
2814 if (printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
))
2816 else if (job
->job_sheets
== NULL
)
2818 else if (strcasecmp(job
->job_sheets
->values
[0].string
.text
, "none") != 0 &&
2819 job
->current_file
== 0)
2821 else if (job
->job_sheets
->num_values
> 1 &&
2822 strcasecmp(job
->job_sheets
->values
[1].string
.text
, "none") != 0 &&
2823 job
->current_file
== (job
->num_files
- 1))
2828 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "banner_page = %d", banner_page
);
2831 * Building the options string is harder than it needs to be, but
2832 * for the moment we need to pass strings for command-line args and
2833 * not IPP attribute pointers... :)
2835 * First allocate/reallocate the option buffer as needed...
2838 i
= ipp_length(job
->attrs
);
2840 if (i
> optlength
|| !options
)
2845 optptr
= realloc(options
, i
);
2849 cupsdLogJob(job
, CUPSD_LOG_CRIT
,
2850 "Unable to allocate %d bytes for option buffer!", i
);
2852 cupsArrayDelete(filters
);
2854 FilterLevel
-= job
->cost
;
2856 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2865 * Now loop through the attributes and convert them to the textual
2866 * representation used by the filters...
2872 snprintf(title
, sizeof(title
), "%s-%d", printer
->name
, job
->id
);
2873 strcpy(copies
, "1");
2875 for (attr
= job
->attrs
->attrs
; attr
!= NULL
; attr
= attr
->next
)
2877 if (!strcmp(attr
->name
, "copies") &&
2878 attr
->value_tag
== IPP_TAG_INTEGER
)
2881 * Don't use the # copies attribute if we are printing the job sheets...
2885 sprintf(copies
, "%d", attr
->values
[0].integer
);
2887 else if (!strcmp(attr
->name
, "job-name") &&
2888 (attr
->value_tag
== IPP_TAG_NAME
||
2889 attr
->value_tag
== IPP_TAG_NAMELANG
))
2890 strlcpy(title
, attr
->values
[0].string
.text
, sizeof(title
));
2891 else if (attr
->group_tag
== IPP_TAG_JOB
)
2894 * Filter out other unwanted attributes...
2897 if (attr
->value_tag
== IPP_TAG_MIMETYPE
||
2898 attr
->value_tag
== IPP_TAG_NAMELANG
||
2899 attr
->value_tag
== IPP_TAG_TEXTLANG
||
2900 (attr
->value_tag
== IPP_TAG_URI
&& strcmp(attr
->name
, "job-uuid")) ||
2901 attr
->value_tag
== IPP_TAG_URISCHEME
||
2902 attr
->value_tag
== IPP_TAG_BEGIN_COLLECTION
) /* Not yet supported */
2905 if (!strncmp(attr
->name
, "time-", 5))
2908 if (!strncmp(attr
->name
, "job-", 4) &&
2909 strcmp(attr
->name
, "job-billing") &&
2910 strcmp(attr
->name
, "job-impressions") &&
2911 strcmp(attr
->name
, "job-originating-host-name") &&
2912 strcmp(attr
->name
, "job-uuid") &&
2913 !(printer
->type
& CUPS_PRINTER_REMOTE
))
2916 if ((!strcmp(attr
->name
, "job-impressions") ||
2917 !strcmp(attr
->name
, "page-label") ||
2918 !strcmp(attr
->name
, "page-border") ||
2919 !strncmp(attr
->name
, "number-up", 9) ||
2920 !strcmp(attr
->name
, "page-ranges") ||
2921 !strcmp(attr
->name
, "page-set") ||
2922 !strcasecmp(attr
->name
, "AP_FIRSTPAGE_InputSlot") ||
2923 !strcasecmp(attr
->name
, "AP_FIRSTPAGE_ManualFeed") ||
2924 !strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
2925 "PMTotalSidesImaged..n.") ||
2926 !strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
2927 "PMTotalBeginPages..n.")) &&
2932 * Otherwise add them to the list...
2935 if (optptr
> options
)
2936 strlcat(optptr
, " ", optlength
- (optptr
- options
));
2938 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
2940 strlcat(optptr
, attr
->name
, optlength
- (optptr
- options
));
2941 strlcat(optptr
, "=", optlength
- (optptr
- options
));
2944 for (i
= 0; i
< attr
->num_values
; i
++)
2947 strlcat(optptr
, ",", optlength
- (optptr
- options
));
2949 optptr
+= strlen(optptr
);
2951 switch (attr
->value_tag
)
2953 case IPP_TAG_INTEGER
:
2955 snprintf(optptr
, optlength
- (optptr
- options
),
2956 "%d", attr
->values
[i
].integer
);
2959 case IPP_TAG_BOOLEAN
:
2960 if (!attr
->values
[i
].boolean
)
2961 strlcat(optptr
, "no", optlength
- (optptr
- options
));
2963 case IPP_TAG_NOVALUE
:
2964 strlcat(optptr
, attr
->name
,
2965 optlength
- (optptr
- options
));
2968 case IPP_TAG_RANGE
:
2969 if (attr
->values
[i
].range
.lower
== attr
->values
[i
].range
.upper
)
2970 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2971 "%d", attr
->values
[i
].range
.lower
);
2973 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2974 "%d-%d", attr
->values
[i
].range
.lower
,
2975 attr
->values
[i
].range
.upper
);
2978 case IPP_TAG_RESOLUTION
:
2979 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2980 "%dx%d%s", attr
->values
[i
].resolution
.xres
,
2981 attr
->values
[i
].resolution
.yres
,
2982 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
2986 case IPP_TAG_STRING
:
2989 case IPP_TAG_KEYWORD
:
2990 case IPP_TAG_CHARSET
:
2991 case IPP_TAG_LANGUAGE
:
2993 for (valptr
= attr
->values
[i
].string
.text
; *valptr
;)
2995 if (strchr(" \t\n\\\'\"", *valptr
))
2997 *optptr
++ = *valptr
++;
3004 break; /* anti-compiler-warning-code */
3008 optptr
+= strlen(optptr
);
3013 * Build the command-line arguments for the filters. Each filter
3014 * has 6 or 7 arguments:
3018 * argv[2] = username
3020 * argv[4] = # copies
3022 * argv[6] = filename (optional; normally stdin)
3024 * This allows legacy printer drivers that use the old System V
3025 * printing interface to be used by CUPS.
3027 * For remote jobs, we send all of the files in the argument list.
3030 if (printer
->remote
&& job
->num_files
> 1)
3031 argv
= calloc(7 + job
->num_files
, sizeof(char *));
3033 argv
= calloc(8, sizeof(char *));
3037 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to allocate argument array!");
3038 cupsArrayDelete(filters
);
3040 FilterLevel
-= job
->cost
;
3042 cupsdStopPrinter(printer
, 0);
3046 sprintf(jobid
, "%d", job
->id
);
3048 argv
[0] = printer
->name
;
3050 argv
[2] = job
->username
;
3055 if (printer
->remote
&& job
->num_files
> 1)
3057 for (i
= 0; i
< job
->num_files
; i
++)
3059 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
3061 argv
[6 + i
] = strdup(filename
);
3066 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
3067 job
->id
, job
->current_file
+ 1);
3071 for (i
= 0; argv
[i
]; i
++)
3072 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
3073 "argv[%d]=\"%s\"", i
, argv
[i
]);
3076 * Create environment variable strings for the filters...
3079 attr
= ippFindAttribute(job
->attrs
, "attributes-natural-language",
3083 strcpy(apple_language
, "APPLE_LANGUAGE=");
3084 _cupsAppleLanguage(attr
->values
[0].string
.text
,
3085 apple_language
+ 15, sizeof(apple_language
) - 15);
3086 #endif /* __APPLE__ */
3088 switch (strlen(attr
->values
[0].string
.text
))
3092 * This is an unknown or badly formatted language code; use
3093 * the POSIX locale...
3096 strcpy(lang
, "LANG=C");
3101 * Just the language code (ll)...
3104 snprintf(lang
, sizeof(lang
), "LANG=%s.UTF8",
3105 attr
->values
[0].string
.text
);
3110 * Language and country code (ll-cc)...
3113 snprintf(lang
, sizeof(lang
), "LANG=%c%c_%c%c.UTF8",
3114 attr
->values
[0].string
.text
[0],
3115 attr
->values
[0].string
.text
[1],
3116 toupper(attr
->values
[0].string
.text
[3] & 255),
3117 toupper(attr
->values
[0].string
.text
[4] & 255));
3121 attr
= ippFindAttribute(job
->attrs
, "document-format",
3124 (optptr
= strstr(attr
->values
[0].string
.text
, "charset=")) != NULL
)
3125 snprintf(charset
, sizeof(charset
), "CHARSET=%s", optptr
+ 8);
3128 attr
= ippFindAttribute(job
->attrs
, "attributes-charset",
3130 snprintf(charset
, sizeof(charset
), "CHARSET=%s",
3131 attr
->values
[0].string
.text
);
3134 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s/%s",
3135 job
->filetypes
[job
->current_file
]->super
,
3136 job
->filetypes
[job
->current_file
]->type
);
3137 snprintf(device_uri
, sizeof(device_uri
), "DEVICE_URI=%s",
3138 printer
->device_uri
);
3139 snprintf(ppd
, sizeof(ppd
), "PPD=%s/ppd/%s.ppd", ServerRoot
, printer
->name
);
3140 snprintf(printer_info
, sizeof(printer_name
), "PRINTER_INFO=%s",
3141 printer
->info
? printer
->info
: "");
3142 snprintf(printer_location
, sizeof(printer_name
), "PRINTER_LOCATION=%s",
3143 printer
->location
? printer
->location
: "");
3144 snprintf(printer_name
, sizeof(printer_name
), "PRINTER=%s", printer
->name
);
3145 snprintf(rip_max_cache
, sizeof(rip_max_cache
), "RIP_MAX_CACHE=%s", RIPCache
);
3147 envc
= cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
3149 envp
[envc
++] = charset
;
3150 envp
[envc
++] = lang
;
3152 envp
[envc
++] = apple_language
;
3153 #endif /* __APPLE__ */
3154 envp
[envc
++] = ppd
;
3155 envp
[envc
++] = rip_max_cache
;
3156 envp
[envc
++] = content_type
;
3157 envp
[envc
++] = device_uri
;
3158 envp
[envc
++] = printer_info
;
3159 envp
[envc
++] = printer_location
;
3160 envp
[envc
++] = printer_name
;
3161 envp
[envc
++] = banner_page
? "CUPS_FILETYPE=job-sheet" :
3162 "CUPS_FILETYPE=document";
3164 if (!printer
->remote
&& !printer
->raw
)
3166 filter
= (mime_filter_t
*)cupsArrayLast(filters
);
3168 if (printer
->port_monitor
)
3169 filter
= (mime_filter_t
*)cupsArrayPrev(filters
);
3171 if (filter
&& filter
->dst
)
3173 snprintf(final_content_type
, sizeof(final_content_type
),
3174 "FINAL_CONTENT_TYPE=%s/%s",
3175 filter
->dst
->super
, filter
->dst
->type
);
3176 envp
[envc
++] = final_content_type
;
3180 if (Classification
&& !banner_page
)
3182 if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
3183 IPP_TAG_NAME
)) == NULL
)
3184 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3186 else if (attr
->num_values
> 1 &&
3187 strcmp(attr
->values
[1].string
.text
, "none") != 0)
3188 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3189 attr
->values
[1].string
.text
);
3191 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3192 attr
->values
[0].string
.text
);
3194 envp
[envc
++] = classification
;
3197 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
3199 snprintf(class_name
, sizeof(class_name
), "CLASS=%s", job
->dest
);
3200 envp
[envc
++] = class_name
;
3203 if (job
->auth_username
)
3204 envp
[envc
++] = job
->auth_username
;
3205 if (job
->auth_domain
)
3206 envp
[envc
++] = job
->auth_domain
;
3207 if (job
->auth_password
)
3208 envp
[envc
++] = job
->auth_password
;
3212 envp
[envc
++] = job
->ccname
;
3213 #endif /* HAVE_GSSAPI */
3217 for (i
= 0; i
< envc
; i
++)
3218 if (!strncmp(envp
[i
], "AUTH_", 5))
3219 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"AUTH_%c****\"", i
,
3221 else if (strncmp(envp
[i
], "DEVICE_URI=", 11))
3222 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"%s\"", i
, envp
[i
]);
3224 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"DEVICE_URI=%s\"", i
,
3225 printer
->sanitized_device_uri
);
3227 if (printer
->remote
)
3228 job
->current_file
= job
->num_files
;
3230 job
->current_file
++;
3233 * Now create processes for all of the filters...
3236 filterfds
[0][0] = -1;
3237 filterfds
[0][1] = -1;
3238 filterfds
[1][0] = -1;
3239 filterfds
[1][1] = -1;
3241 if (!job
->status_buffer
)
3243 if (cupsdOpenPipe(job
->status_pipes
))
3245 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
3246 "Unable to create job status pipes - %s.", strerror(errno
));
3247 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3248 "Unable to create status pipes - %s.", strerror(errno
));
3250 cupsdAddPrinterHistory(printer
);
3252 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3253 "Job canceled because the server could not create the job "
3259 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3260 "start_job: status_pipes = [ %d %d ]",
3261 job
->status_pipes
[0], job
->status_pipes
[1]);
3263 job
->status_buffer
= cupsdStatBufNew(job
->status_pipes
[0], NULL
);
3264 job
->status_level
= CUPSD_LOG_INFO
;
3268 memset(job
->filters
, 0, sizeof(job
->filters
));
3271 job
->profile
= cupsdCreateProfile(job
->id
);
3273 for (i
= 0, slot
= 0, filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
3275 i
++, filter
= (mime_filter_t
*)cupsArrayNext(filters
))
3277 if (filter
->filter
[0] != '/')
3278 snprintf(command
, sizeof(command
), "%s/filter/%s", ServerBin
,
3281 strlcpy(command
, filter
->filter
, sizeof(command
));
3283 if (i
< (cupsArrayCount(filters
) - 1))
3285 if (cupsdOpenPipe(filterfds
[slot
]))
3287 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
3288 "Unable to create job filter pipes - %s.", strerror(errno
));
3289 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3290 "Unable to create filter pipes - %s.", strerror(errno
));
3291 cupsdAddPrinterHistory(printer
);
3293 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3294 "Job canceled because the server could not create the "
3302 if (job
->current_file
== 1)
3304 if (strncmp(printer
->device_uri
, "file:", 5) != 0)
3306 if (cupsdOpenPipe(job
->print_pipes
))
3308 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
3309 "Unable to create job backend pipes - %s.",
3311 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3312 "Unable to create backend pipes - %s.", strerror(errno
));
3313 cupsdAddPrinterHistory(printer
);
3315 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3316 "Job canceled because the server could not create "
3317 "the backend pipes.");
3324 job
->print_pipes
[0] = -1;
3325 if (!strcmp(printer
->device_uri
, "file:/dev/null") ||
3326 !strcmp(printer
->device_uri
, "file:///dev/null"))
3327 job
->print_pipes
[1] = -1;
3330 if (!strncmp(printer
->device_uri
, "file:/dev/", 10))
3331 job
->print_pipes
[1] = open(printer
->device_uri
+ 5,
3333 else if (!strncmp(printer
->device_uri
, "file:///dev/", 12))
3334 job
->print_pipes
[1] = open(printer
->device_uri
+ 7,
3336 else if (!strncmp(printer
->device_uri
, "file:///", 8))
3337 job
->print_pipes
[1] = open(printer
->device_uri
+ 7,
3338 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
3340 job
->print_pipes
[1] = open(printer
->device_uri
+ 5,
3341 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
3343 if (job
->print_pipes
[1] < 0)
3345 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
3346 "Unable to open output file \"%s\" - %s.",
3347 printer
->device_uri
, strerror(errno
));
3348 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3349 "Unable to open output file \"%s\" - %s.",
3350 printer
->device_uri
, strerror(errno
));
3352 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3353 "Job canceled because the server could not open the "
3359 fcntl(job
->print_pipes
[1], F_SETFD
,
3360 fcntl(job
->print_pipes
[1], F_GETFD
) | FD_CLOEXEC
);
3364 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3365 "start_job: print_pipes = [ %d %d ]",
3366 job
->print_pipes
[0], job
->print_pipes
[1]);
3369 filterfds
[slot
][0] = job
->print_pipes
[0];
3370 filterfds
[slot
][1] = job
->print_pipes
[1];
3373 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "start_job: filter=\"%s\"", command
);
3374 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "start_job: filterfds[%d]=[ %d %d ]",
3375 slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3377 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
3378 filterfds
[slot
][1], job
->status_pipes
[1],
3379 job
->back_pipes
[0], job
->side_pipes
[0], 0,
3380 job
->profile
, job
->filters
+ i
);
3382 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3383 "start_job: Closing filter pipes for slot %d [ %d %d ]...",
3384 !slot
, filterfds
[!slot
][0], filterfds
[!slot
][1]);
3386 cupsdClosePipe(filterfds
[!slot
]);
3390 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Unable to start filter \"%s\" - %s.",
3391 filter
->filter
, strerror(errno
));
3392 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3393 "Unable to start filter \"%s\" - %s.",
3394 filter
->filter
, strerror(errno
));
3396 cupsdAddPrinterHistory(printer
);
3398 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3399 "Job canceled because the server could not execute a "
3405 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Started filter %s (PID %d)", command
,
3412 cupsArrayDelete(filters
);
3416 * Finally, pipe the final output into a backend process if needed...
3419 if (strncmp(printer
->device_uri
, "file:", 5) != 0)
3421 if (job
->current_file
== 1 || printer
->remote
)
3423 sscanf(printer
->device_uri
, "%254[^:]", method
);
3424 snprintf(command
, sizeof(command
), "%s/backend/%s", ServerBin
, method
);
3427 * See if the backend needs to run as root...
3432 else if (stat(command
, &backinfo
))
3435 backroot
= !(backinfo
.st_mode
& (S_IRWXG
| S_IRWXO
));
3437 argv
[0] = printer
->sanitized_device_uri
;
3439 filterfds
[slot
][0] = -1;
3440 filterfds
[slot
][1] = -1;
3442 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "start_job: backend=\"%s\"", command
);
3443 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "start_job: filterfds[%d] = [ %d %d ]",
3444 slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3446 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
3447 filterfds
[slot
][1], job
->status_pipes
[1],
3448 job
->back_pipes
[1], job
->side_pipes
[1],
3449 backroot
, job
->profile
, &(job
->backend
));
3453 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Unable to start backend \"%s\" - %s.",
3454 method
, strerror(errno
));
3455 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3456 "Unable to start backend \"%s\" - %s.", method
,
3459 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3460 "Job canceled because the server could not execute "
3467 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Started backend %s (PID %d)",
3472 if (job
->current_file
== job
->num_files
)
3474 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3475 "start_job: Closing print pipes [ %d %d ]...",
3476 job
->print_pipes
[0], job
->print_pipes
[1]);
3478 cupsdClosePipe(job
->print_pipes
);
3480 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3481 "start_job: Closing back pipes [ %d %d ]...",
3482 job
->back_pipes
[0], job
->back_pipes
[1]);
3484 cupsdClosePipe(job
->back_pipes
);
3486 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3487 "start_job: Closing side pipes [ %d %d ]...",
3488 job
->side_pipes
[0], job
->side_pipes
[1]);
3490 cupsdClosePipe(job
->side_pipes
);
3492 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3493 "start_job: Closing status output pipe %d...",
3494 job
->status_pipes
[1]);
3496 close(job
->status_pipes
[1]);
3497 job
->status_pipes
[1] = -1;
3502 filterfds
[slot
][0] = -1;
3503 filterfds
[slot
][1] = -1;
3505 if (job
->current_file
== job
->num_files
)
3507 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3508 "start_job: Closing print pipes [ %d %d ]...",
3509 job
->print_pipes
[0], job
->print_pipes
[1]);
3511 cupsdClosePipe(job
->print_pipes
);
3513 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3514 "start_job: Closing status output pipe %d...",
3515 job
->status_pipes
[1]);
3517 close(job
->status_pipes
[1]);
3518 job
->status_pipes
[1] = -1;
3522 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3523 "start_job: Closing filter pipes for slot %d [ %d %d ]...",
3524 slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3525 cupsdClosePipe(filterfds
[slot
]);
3527 if (printer
->remote
&& job
->num_files
> 1)
3529 for (i
= 0; i
< job
->num_files
; i
++)
3535 cupsdAddSelect(job
->status_buffer
->fd
, (cupsd_selfunc_t
)update_job
, NULL
,
3538 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
, "Job #%d started.",
3545 * If we get here, we need to abort the current job and close out all
3546 * files and pipes...
3551 for (slot
= 0; slot
< 2; slot
++)
3553 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3554 "start_job: Closing filter pipes for slot %d [ %d %d ]...",
3555 slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3556 cupsdClosePipe(filterfds
[slot
]);
3559 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3560 "start_job: Closing status pipes [ %d %d ]...",
3561 job
->status_pipes
[0], job
->status_pipes
[1]);
3562 cupsdClosePipe(job
->status_pipes
);
3563 cupsdStatBufDelete(job
->status_buffer
);
3565 job
->status_buffer
= NULL
;
3567 cupsArrayDelete(filters
);
3569 if (printer
->remote
&& job
->num_files
> 1)
3571 for (i
= 0; i
< job
->num_files
; i
++)
3577 cupsdStopJob(job
, 0);
3582 * 'unload_job()' - Unload a job from memory.
3586 unload_job(cupsd_job_t
*job
) /* I - Job */
3591 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Unloading...");
3593 ippDelete(job
->attrs
);
3598 job
->job_sheets
= NULL
;
3599 job
->printer_message
= NULL
;
3600 job
->printer_reasons
= NULL
;
3605 * 'update_job()' - Read a status update from a job's filters.
3609 update_job(cupsd_job_t
*job
) /* I - Job to check */
3611 int i
; /* Looping var */
3612 int copies
; /* Number of copies printed */
3613 char message
[1024], /* Message text */
3614 *ptr
; /* Pointer update... */
3615 int loglevel
, /* Log level for message */
3616 event
= 0; /* Events? */
3619 while ((ptr
= cupsdStatBufUpdate(job
->status_buffer
, &loglevel
,
3620 message
, sizeof(message
))) != NULL
)
3623 * Process page and printer state messages as needed...
3626 if (loglevel
== CUPSD_LOG_PAGE
)
3629 * Page message; send the message to the page_log file and update the
3630 * job sheet count...
3633 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "PAGE: %s", message
);
3637 if (!strncasecmp(message
, "total ", 6))
3640 * Got a total count of pages from a backend or filter...
3643 copies
= atoi(message
+ 6);
3644 copies
-= job
->sheets
->values
[0].integer
; /* Just track the delta */
3646 else if (!sscanf(message
, "%*d%d", &copies
))
3649 job
->sheets
->values
[0].integer
+= copies
;
3651 if (job
->printer
->page_limit
)
3653 cupsd_quota_t
*q
= cupsdUpdateQuota(job
->printer
, job
->username
,
3657 if (AppleQuotas
&& q
->page_count
== -3)
3660 * Quota limit exceeded, cancel job in progress immediately...
3663 cupsdLogJob(job
, CUPSD_LOG_INFO
,
3664 "Canceled because pages exceed user %s "
3665 "quota limit on printer %s (%s).",
3666 job
->username
, job
->printer
->name
,
3667 job
->printer
->info
);
3669 cupsdCancelJob(job
, 1, IPP_JOB_CANCELED
);
3674 #endif /* __APPLE__ */
3678 cupsdLogPage(job
, message
);
3681 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
3682 "Printed %d page(s).", job
->sheets
->values
[0].integer
);
3684 else if (loglevel
== CUPSD_LOG_STATE
)
3686 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "STATE: %s", message
);
3688 if (!strcmp(message
, "paused"))
3690 cupsdStopPrinter(job
->printer
, 1);
3695 cupsdSetPrinterReasons(job
->printer
, message
);
3696 cupsdAddPrinterHistory(job
->printer
);
3697 event
|= CUPSD_EVENT_PRINTER_STATE
;
3700 update_job_attrs(job
, 0);
3702 else if (loglevel
== CUPSD_LOG_ATTR
)
3705 * Set attribute(s)...
3708 int num_attrs
; /* Number of attributes */
3709 cups_option_t
*attrs
; /* Attributes */
3710 const char *attr
; /* Attribute */
3713 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "ATTR: %s", message
);
3715 num_attrs
= cupsParseOptions(message
, 0, &attrs
);
3717 if ((attr
= cupsGetOption("auth-info-required", num_attrs
,
3720 cupsdSetAuthInfoRequired(job
->printer
, attr
, NULL
);
3721 cupsdSetPrinterAttrs(job
->printer
);
3723 if (job
->printer
->type
& CUPS_PRINTER_DISCOVERED
)
3724 cupsdMarkDirty(CUPSD_DIRTY_REMOTE
);
3726 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3729 if ((attr
= cupsGetOption("job-media-progress", num_attrs
,
3732 int progress
= atoi(attr
);
3735 if (progress
>= 0 && progress
<= 100)
3737 job
->progress
= progress
;
3740 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
3741 "Printing page %d, %d%%",
3742 job
->sheets
->values
[0].integer
, job
->progress
);
3746 if ((attr
= cupsGetOption("printer-alert", num_attrs
, attrs
)) != NULL
)
3748 cupsdSetString(&job
->printer
->alert
, attr
);
3749 event
|= CUPSD_EVENT_PRINTER_STATE
;
3752 if ((attr
= cupsGetOption("printer-alert-description", num_attrs
,
3755 cupsdSetString(&job
->printer
->alert_description
, attr
);
3756 event
|= CUPSD_EVENT_PRINTER_STATE
;
3759 if ((attr
= cupsGetOption("marker-colors", num_attrs
, attrs
)) != NULL
)
3761 cupsdSetPrinterAttr(job
->printer
, "marker-colors", (char *)attr
);
3762 job
->printer
->marker_time
= time(NULL
);
3763 event
|= CUPSD_EVENT_PRINTER_STATE
;
3766 if ((attr
= cupsGetOption("marker-levels", num_attrs
, attrs
)) != NULL
)
3768 cupsdSetPrinterAttr(job
->printer
, "marker-levels", (char *)attr
);
3769 job
->printer
->marker_time
= time(NULL
);
3770 event
|= CUPSD_EVENT_PRINTER_STATE
;
3773 if ((attr
= cupsGetOption("marker-message", num_attrs
, attrs
)) != NULL
)
3775 cupsdSetPrinterAttr(job
->printer
, "marker-message", (char *)attr
);
3776 job
->printer
->marker_time
= time(NULL
);
3777 event
|= CUPSD_EVENT_PRINTER_STATE
;
3780 if ((attr
= cupsGetOption("marker-names", num_attrs
, attrs
)) != NULL
)
3782 cupsdSetPrinterAttr(job
->printer
, "marker-names", (char *)attr
);
3783 job
->printer
->marker_time
= time(NULL
);
3784 event
|= CUPSD_EVENT_PRINTER_STATE
;
3787 if ((attr
= cupsGetOption("marker-types", num_attrs
, attrs
)) != NULL
)
3789 cupsdSetPrinterAttr(job
->printer
, "marker-types", (char *)attr
);
3790 job
->printer
->marker_time
= time(NULL
);
3791 event
|= CUPSD_EVENT_PRINTER_STATE
;
3794 cupsFreeOptions(num_attrs
, attrs
);
3796 else if (loglevel
== CUPSD_LOG_PPD
)
3799 * Set attribute(s)...
3802 int num_keywords
; /* Number of keywords */
3803 cups_option_t
*keywords
; /* Keywords */
3806 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "PPD: %s", message
);
3808 num_keywords
= cupsParseOptions(message
, 0, &keywords
);
3810 if (cupsdUpdatePrinterPPD(job
->printer
, num_keywords
, keywords
))
3811 cupsdSetPrinterAttrs(job
->printer
);
3813 cupsFreeOptions(num_keywords
, keywords
);
3816 else if (!strncmp(message
, "recoverable:", 12))
3818 cupsdSetPrinterReasons(job
->printer
,
3819 "+com.apple.print.recoverable-warning");
3822 while (isspace(*ptr
& 255))
3825 cupsdSetString(&job
->printer
->recoverable
, ptr
);
3826 cupsdAddPrinterHistory(job
->printer
);
3827 event
|= CUPSD_EVENT_PRINTER_STATE
;
3829 else if (!strncmp(message
, "recovered:", 10))
3831 cupsdSetPrinterReasons(job
->printer
,
3832 "-com.apple.print.recoverable-warning");
3835 while (isspace(*ptr
& 255))
3838 cupsdSetString(&job
->printer
->recoverable
, ptr
);
3839 cupsdAddPrinterHistory(job
->printer
);
3840 event
|= CUPSD_EVENT_PRINTER_STATE
;
3842 #endif /* __APPLE__ */
3845 cupsdLogJob(job
, loglevel
, "%s", message
);
3847 if (loglevel
< CUPSD_LOG_DEBUG
)
3849 strlcpy(job
->printer
->state_message
, message
,
3850 sizeof(job
->printer
->state_message
));
3851 cupsdAddPrinterHistory(job
->printer
);
3853 event
|= CUPSD_EVENT_PRINTER_STATE
;
3855 if (loglevel
<= job
->status_level
)
3858 * Some messages show in the printer-state-message attribute...
3861 if (loglevel
!= CUPSD_LOG_NOTICE
)
3862 job
->status_level
= loglevel
;
3864 update_job_attrs(job
, 1);
3869 if (!strchr(job
->status_buffer
->buffer
, '\n'))
3873 if (event
& CUPSD_EVENT_PRINTER_STATE
)
3874 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, job
->printer
, NULL
,
3875 (job
->printer
->type
& CUPS_PRINTER_CLASS
) ?
3876 "Class \"%s\" state changed." :
3877 "Printer \"%s\" state changed.",
3878 job
->printer
->name
);
3880 if (ptr
== NULL
&& !job
->status_buffer
->bufused
)
3883 * See if all of the filters and the backend have returned their
3887 for (i
= 0; job
->filters
[i
] < 0; i
++);
3889 if (job
->filters
[i
])
3892 if (job
->current_file
>= job
->num_files
&& job
->backend
> 0)
3896 * Handle the end of job stuff...
3899 cupsdFinishJob(job
);
3905 * 'update_job_attrs()' - Update the job-printer-* attributes.
3909 update_job_attrs(cupsd_job_t
*job
, /* I - Job to update */
3910 int do_message
)/* I - 1 = update job-printer-state message */
3912 int i
; /* Looping var */
3913 int num_reasons
; /* Actual number of reasons */
3914 const char * const *reasons
; /* Reasons */
3915 static const char *none
= "none", /* "none" */
3921 * Get/create the job-printer-state-* attributes...
3924 if (!job
->printer_message
)
3926 if ((job
->printer_message
= ippFindAttribute(job
->attrs
,
3927 "job-printer-state-message",
3928 IPP_TAG_TEXT
)) == NULL
)
3929 job
->printer_message
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_TEXT
,
3930 "job-printer-state-message",
3934 if (!job
->printer_reasons
)
3935 job
->printer_reasons
= ippFindAttribute(job
->attrs
,
3936 "job-printer-state-reasons",
3940 * If the job isn't printing, return now...
3947 * Otherwise copy the printer-state-message value...
3950 if (job
->printer
->state_message
[0] &&
3951 (do_message
|| !job
->printer_message
->values
[0].string
.text
[0]))
3952 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
),
3953 job
->printer
->state_message
);
3956 * ... and the printer-state-reasons value...
3959 if (job
->printer
->num_reasons
== 0)
3962 reasons
= job
->printer
->state
== IPP_PRINTER_STOPPED
? &paused
: &none
;
3966 num_reasons
= job
->printer
->num_reasons
;
3967 reasons
= (const char * const *)job
->printer
->reasons
;
3970 if (!job
->printer_reasons
|| job
->printer_reasons
->num_values
!= num_reasons
)
3972 ippDeleteAttribute(job
->attrs
, job
->printer_reasons
);
3974 job
->printer_reasons
= ippAddStrings(job
->attrs
,
3975 IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
3976 "job-printer-state-reasons",
3977 num_reasons
, NULL
, NULL
);
3980 for (i
= 0; i
< num_reasons
; i
++)
3981 cupsdSetString(&(job
->printer_reasons
->values
[i
].string
.text
), reasons
[i
]);
3986 * End of "$Id: job.c 7902 2008-09-03 14:20:17Z mike $".