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 the
22 * 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, or held
29 * jobs in a printer or class.
30 * cupsdGetUserJobCount() - Get the number of pending, processing, or held
32 * cupsdHoldJob() - Hold the specified job.
33 * cupsdLoadAllJobs() - Load all jobs from disk.
34 * cupsdLoadJob() - Load a single job...
35 * cupsdMoveJob() - Move the specified job to a different
37 * cupsdReleaseJob() - Release the specified job.
38 * cupsdRestartJob() - Restart the specified job.
39 * cupsdSaveAllJobs() - Save a summary of all jobs to disk.
40 * cupsdSaveJob() - Save a job to disk.
41 * cupsdSetJobHoldUntil() - Set the hold time for a job...
42 * cupsdSetJobPriority() - Set the priority of a job, moving it up/down
43 * in the list as needed.
44 * cupsdStopAllJobs() - Stop all print jobs.
45 * cupsdStopJob() - Stop a print job.
46 * cupsdUnloadCompletedJobs() - Flush completed job history from memory.
47 * compare_active_jobs() - Compare the job IDs and priorities of two
49 * compare_jobs() - Compare the job IDs of two jobs.
50 * ipp_length() - Compute the size of the buffer needed to hold
51 * the textual IPP attributes.
52 * load_job_cache() - Load jobs from the job.cache file.
53 * load_next_job_id() - Load the NextJobId value from the job.cache
55 * load_request_root() - Load jobs from the RequestRoot directory.
56 * set_time() - Set one of the "time-at-xyz" attributes...
57 * set_hold_until() - Set the hold time and update job-hold-until
59 * start_job() - Start a print job.
60 * unload_job() - Unload a job from memory.
61 * update_job() - Read a status update from a job's filters.
62 * update_job_attrs() - Update the job-printer-* attributes.
66 * Include necessary headers...
71 #include <cups/backend.h>
79 static mime_filter_t gziptoany_filter
=
81 NULL
, /* Source type */
82 NULL
, /* Destination type */
84 "gziptoany" /* Filter program to run */
92 static int compare_active_jobs(void *first
, void *second
, void *data
);
93 static int compare_jobs(void *first
, void *second
, void *data
);
94 static int ipp_length(ipp_t
*ipp
);
95 static void load_job_cache(const char *filename
);
96 static void load_next_job_id(const char *filename
);
97 static void load_request_root(void);
98 static void set_time(cupsd_job_t
*job
, const char *name
);
99 static void set_hold_until(cupsd_job_t
*job
, time_t holdtime
);
100 static void start_job(cupsd_job_t
*job
, cupsd_printer_t
*printer
);
101 static void unload_job(cupsd_job_t
*job
);
102 static void update_job(cupsd_job_t
*job
);
103 static void update_job_attrs(cupsd_job_t
*job
, int do_message
);
107 * 'cupsdAddJob()' - Add a new job to the job queue...
110 cupsd_job_t
* /* O - New job record */
111 cupsdAddJob(int priority
, /* I - Job priority */
112 const char *dest
) /* I - Job destination */
114 cupsd_job_t
*job
; /* New job record */
117 if ((job
= calloc(sizeof(cupsd_job_t
), 1)) == NULL
)
120 job
->id
= NextJobId
++;
121 job
->priority
= priority
;
122 job
->back_pipes
[0] = -1;
123 job
->back_pipes
[1] = -1;
124 job
->print_pipes
[0] = -1;
125 job
->print_pipes
[1] = -1;
126 job
->side_pipes
[0] = -1;
127 job
->side_pipes
[1] = -1;
128 job
->status_pipes
[0] = -1;
129 job
->status_pipes
[1] = -1;
131 cupsdSetString(&job
->dest
, dest
);
134 * Add the new job to the "all jobs" and "active jobs" lists...
137 cupsArrayAdd(Jobs
, job
);
138 cupsArrayAdd(ActiveJobs
, job
);
145 * 'cupsdCancelJob()' - Cancel the specified print job.
149 cupsdCancelJob(cupsd_job_t
*job
, /* I - Job to cancel */
150 int purge
, /* I - Purge jobs? */
151 ipp_jstate_t newstate
) /* I - New job state */
153 int i
; /* Looping var */
154 char filename
[1024]; /* Job filename */
155 cupsd_printer_t
*printer
; /* Printer used by job */
158 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdCancelJob: id = %d", job
->id
);
161 * Stop any processes that are working on the current job...
164 printer
= job
->printer
;
166 if (job
->state_value
== IPP_JOB_PROCESSING
)
167 cupsdStopJob(job
, 0);
172 job
->state
->values
[0].integer
= newstate
;
174 job
->state_value
= newstate
;
176 set_time(job
, "time-at-completed");
179 * Send any pending notifications and then expire them...
187 case IPP_JOB_CANCELED
:
188 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, printer
, job
,
189 purge
? "Job purged." : "Job canceled.");
192 case IPP_JOB_ABORTED
:
193 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, printer
, job
,
194 "Job aborted; please consult the error_log file "
198 case IPP_JOB_COMPLETED
:
200 * Clear the printer's printer-state-message and move on...
203 printer
->state_message
[0] = '\0';
205 cupsdSetPrinterState(printer
, IPP_PRINTER_IDLE
, 0);
207 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, printer
, job
,
212 cupsdExpireSubscriptions(NULL
, job
);
215 * Remove the job from the active and printing lists...
218 cupsArrayRemove(ActiveJobs
, job
);
219 cupsArrayRemove(PrintingJobs
, job
);
223 * If we are going to sleep and the PrintingJobs count is now 0, allow the
224 * sleep to happen immediately...
227 if (Sleeping
&& cupsArrayCount(PrintingJobs
) == 0)
229 #endif /* __APPLE__ */
232 * Remove any authentication data...
235 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
236 if (cupsdRemoveFile(filename
) && errno
!= ENOENT
)
237 cupsdLogMessage(CUPSD_LOG_ERROR
,
238 "Unable to remove authentication cache: %s",
241 cupsdClearString(&job
->auth_username
);
242 cupsdClearString(&job
->auth_domain
);
243 cupsdClearString(&job
->auth_password
);
247 * Destroy the credential cache and clear the KRB5CCNAME env var string.
252 krb5_cc_destroy(KerberosContext
, job
->ccache
);
256 cupsdClearString(&job
->ccname
);
257 #endif /* HAVE_GSSAPI */
260 * Remove the print file for good if we aren't preserving jobs or
264 job
->current_file
= 0;
266 if (!JobHistory
|| !JobFiles
|| purge
)
268 for (i
= 1; i
<= job
->num_files
; i
++)
270 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
275 if (job
->num_files
> 0)
277 free(job
->filetypes
);
278 free(job
->compressions
);
281 job
->filetypes
= NULL
;
282 job
->compressions
= NULL
;
286 if (JobHistory
&& !purge
)
289 * Save job state info...
293 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
298 * Remove the job info file...
301 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
,
306 * Remove the job from the "all jobs" list...
309 cupsArrayRemove(Jobs
, job
);
312 * Free all memory used...
323 * 'cupsdCancelJobs()' - Cancel all jobs for the given destination/user...
327 cupsdCancelJobs(const char *dest
, /* I - Destination to cancel */
328 const char *username
, /* I - Username or NULL */
329 int purge
) /* I - Purge jobs? */
331 cupsd_job_t
*job
; /* Current job */
334 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
336 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
338 if (!job
->dest
|| !job
->username
)
341 if (!job
->dest
|| !job
->username
)
344 if ((dest
== NULL
|| !strcmp(job
->dest
, dest
)) &&
345 (username
== NULL
|| !strcmp(job
->username
, username
)))
348 * Cancel all jobs matching this destination/user...
351 cupsdCancelJob(job
, purge
, IPP_JOB_CANCELED
);
360 * 'cupsdCheckJobs()' - Check the pending jobs and start any if the destination
367 cupsd_job_t
*job
; /* Current job in queue */
368 cupsd_printer_t
*printer
, /* Printer destination */
369 *pclass
; /* Printer class destination */
370 ipp_attribute_t
*attr
; /* Job attribute */
373 DEBUG_puts("cupsdCheckJobs()");
375 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
376 "cupsdCheckJobs: %d active jobs, sleeping=%d, reload=%d",
377 cupsArrayCount(ActiveJobs
), Sleeping
, NeedReload
);
379 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
381 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
384 * Start held jobs if they are ready...
387 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
388 "cupsdCheckJobs: Job %d: dest=%s, dtype=%x, "
389 "state_value=%d, loaded=%s", job
->id
, job
->dest
, job
->dtype
,
390 job
->state_value
, job
->attrs
? "yes" : "no");
392 if (job
->state_value
== IPP_JOB_HELD
&&
394 job
->hold_until
< time(NULL
))
396 if (job
->pending_timeout
)
398 /* Add trailing banner as needed */
399 if (cupsdTimeoutJob(job
))
403 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
404 job
->state_value
= IPP_JOB_PENDING
;
406 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
407 IPP_TAG_KEYWORD
)) == NULL
)
408 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
412 attr
->value_tag
= IPP_TAG_KEYWORD
;
413 cupsdSetString(&(attr
->values
[0].string
.text
), "no-hold");
415 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
420 * Start pending jobs if the destination is available...
423 if (job
->state_value
== IPP_JOB_PENDING
&& !NeedReload
&& !Sleeping
)
425 printer
= cupsdFindDest(job
->dest
);
429 (printer
->type
& (CUPS_PRINTER_IMPLICIT
| CUPS_PRINTER_CLASS
)))
432 * If the class is remote, just pass it to the remote server...
437 if (pclass
->state
== IPP_PRINTER_STOPPED
)
439 else if (pclass
->type
& CUPS_PRINTER_REMOTE
)
442 printer
= cupsdFindAvailablePrinter(printer
->name
);
445 if (!printer
&& !pclass
)
448 * Whoa, the printer and/or class for this destination went away;
452 cupsdLogMessage(CUPSD_LOG_WARN
,
453 "[Job %d] Printer/class %s has gone away; canceling "
454 "job!", job
->id
, job
->dest
);
456 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
457 "Job canceled because the destination printer/class has "
460 cupsdCancelJob(job
, 1, IPP_JOB_ABORTED
);
462 else if (printer
&& !printer
->holding_new_jobs
)
465 * See if the printer is available or remote and not printing a job;
466 * if so, start the job...
472 * Add/update a job-actual-printer-uri attribute for this job
473 * so that we know which printer actually printed the job...
476 if ((attr
= ippFindAttribute(job
->attrs
, "job-actual-printer-uri",
477 IPP_TAG_URI
)) != NULL
)
478 cupsdSetString(&attr
->values
[0].string
.text
, printer
->uri
);
480 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
,
481 "job-actual-printer-uri", NULL
, printer
->uri
);
484 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
487 if ((!(printer
->type
& CUPS_PRINTER_DISCOVERED
) && /* Printer is local */
488 printer
->state
== IPP_PRINTER_IDLE
) || /* and idle, OR */
489 ((printer
->type
& CUPS_PRINTER_DISCOVERED
) && /* Printer is remote */
490 !printer
->job
)) /* and not printing */
493 * Clear any message for the queue...
496 printer
->state_message
[0] = '\0';
502 start_job(job
, printer
);
511 * 'cupsdCleanJobs()' - Clean out old jobs.
517 cupsd_job_t
*job
; /* Current job */
523 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
524 job
&& cupsArrayCount(Jobs
) >= MaxJobs
;
525 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
526 if (job
->state_value
>= IPP_JOB_CANCELED
)
527 cupsdCancelJob(job
, 1, IPP_JOB_CANCELED
);
532 * 'cupsdDeleteJob()' - Free all memory used by a job.
536 cupsdDeleteJob(cupsd_job_t
*job
) /* I - Job */
538 cupsdClearString(&job
->username
);
539 cupsdClearString(&job
->dest
);
540 cupsdClearString(&job
->auth_username
);
541 cupsdClearString(&job
->auth_domain
);
542 cupsdClearString(&job
->auth_password
);
546 * Destroy the credential cache and clear the KRB5CCNAME env var string.
551 krb5_cc_destroy(KerberosContext
, job
->ccache
);
555 cupsdClearString(&job
->ccname
);
556 #endif /* HAVE_GSSAPI */
558 if (job
->num_files
> 0)
560 free(job
->compressions
);
561 free(job
->filetypes
);
564 ippDelete(job
->attrs
);
571 * 'cupsdFinishJob()' - Finish a job.
575 cupsdFinishJob(cupsd_job_t
*job
) /* I - Job */
577 cupsd_printer_t
*printer
; /* Current printer */
578 ipp_attribute_t
*attr
; /* job-hold-until attribute */
581 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "File %d is complete.",
582 job
->current_file
- 1);
584 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
585 "cupsdFinishJob: job->status is %d",
588 if (job
->status_buffer
&&
589 (job
->status
< 0 || job
->current_file
>= job
->num_files
))
592 * Close the pipe and clear the input bit.
595 cupsdRemoveSelect(job
->status_buffer
->fd
);
597 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
598 "cupsdFinishJob: Closing status pipes [ %d %d ]...",
599 job
->status_pipes
[0], job
->status_pipes
[1]);
601 cupsdClosePipe(job
->status_pipes
);
602 cupsdStatBufDelete(job
->status_buffer
);
604 job
->status_buffer
= NULL
;
607 printer
= job
->printer
;
609 update_job_attrs(job
, 0);
614 * Backend had errors; stop it...
617 int exit_code
; /* Exit code from backend */
621 * Convert the status to an exit code. Due to the way the W* macros are
622 * implemented on MacOS X (bug?), we have to store the exit status in a
623 * variable first and then convert...
626 exit_code
= -job
->status
;
627 if (WIFEXITED(exit_code
))
628 exit_code
= WEXITSTATUS(exit_code
);
630 exit_code
= job
->status
;
632 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Backend returned status %d (%s)",
634 exit_code
== CUPS_BACKEND_FAILED
? "failed" :
635 exit_code
== CUPS_BACKEND_AUTH_REQUIRED
?
636 "authentication required" :
637 exit_code
== CUPS_BACKEND_HOLD
? "hold job" :
638 exit_code
== CUPS_BACKEND_STOP
? "stop printer" :
639 exit_code
== CUPS_BACKEND_CANCEL
? "cancel job" :
640 exit_code
< 0 ? "crashed" : "unknown");
643 * Do what needs to be done...
649 case CUPS_BACKEND_FAILED
:
651 * Backend failure, use the error-policy to determine how to
655 cupsdStopJob(job
, 0);
657 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
660 * Mark the job as pending again - we'll retry on another
664 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
665 job
->state_value
= IPP_JOB_PENDING
;
669 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
672 * If the job was queued to a class or the error policy is
673 * "retry-current-job", try requeuing it... For faxes and retry-job
674 * queues, hold the current job for 5 minutes.
677 if ((job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
)) ||
678 !strcmp(printer
->error_policy
, "retry-current-job"))
680 else if ((printer
->type
& CUPS_PRINTER_FAX
) ||
681 !strcmp(printer
->error_policy
, "retry-job"))
684 * See how many times we've tried to send the job; if more than
685 * the limit, cancel the job.
690 if (job
->tries
>= JobRetryLimit
)
696 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
697 "Canceling job since it could not be "
698 "sent after %d tries.",
701 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
706 * Try again in N seconds...
709 set_hold_until(job
, time(NULL
) + JobRetryInterval
);
711 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
,
712 "Job held due to fax errors; please consult "
713 "the error_log file for details.");
714 cupsdSetPrinterState(printer
, IPP_PRINTER_IDLE
, 0);
717 else if (!strcmp(printer
->error_policy
, "abort-job"))
718 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
721 cupsdSetPrinterState(printer
, IPP_PRINTER_STOPPED
, 1);
723 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
724 "Job stopped due to backend errors; please consult "
725 "the error_log file for details.");
729 case CUPS_BACKEND_CANCEL
:
734 cupsdCancelJob(job
, 0, IPP_JOB_CANCELED
);
737 case CUPS_BACKEND_HOLD
:
742 cupsdStopJob(job
, 0);
744 cupsdSetJobHoldUntil(job
, "indefinite");
746 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
747 job
->state_value
= IPP_JOB_HELD
;
750 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
752 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
753 "Job held due to backend errors; please consult "
754 "the error_log file for details.");
757 case CUPS_BACKEND_STOP
:
759 * Stop the printer...
762 cupsdStopJob(job
, 0);
764 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
765 job
->state_value
= IPP_JOB_PENDING
;
768 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
770 cupsdSetPrinterState(printer
, IPP_PRINTER_STOPPED
, 1);
772 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
773 "Job stopped due to backend errors; please consult "
774 "the error_log file for details.");
777 case CUPS_BACKEND_AUTH_REQUIRED
:
778 cupsdStopJob(job
, 0);
780 cupsdSetJobHoldUntil(job
, "auth-info-required");
782 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
783 IPP_TAG_KEYWORD
)) == NULL
)
784 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
788 attr
->value_tag
= IPP_TAG_KEYWORD
;
789 cupsdSetString(&(attr
->values
[0].string
.text
),
790 "auth-info-required");
793 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
794 job
->state_value
= IPP_JOB_HELD
;
797 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
799 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
800 "Authentication is required for job %d.", job
->id
);
805 * Try printing another job...
810 else if (job
->status
> 0)
813 * Filter had errors; stop job...
816 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Job stopped due to filter errors.");
817 cupsdStopJob(job
, 1);
819 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
820 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
821 "Job stopped due to filter errors; please consult the "
822 "error_log file for details.");
828 * Job printed successfully; cancel it...
831 if (job
->current_file
< job
->num_files
)
834 * Start the next file in the job...
837 FilterLevel
-= job
->cost
;
838 start_job(job
, printer
);
843 * Close out this job...
846 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Completed successfully.");
847 cupsdCancelJob(job
, 0, IPP_JOB_COMPLETED
);
855 * 'cupsdFreeAllJobs()' - Free all jobs from memory.
859 cupsdFreeAllJobs(void)
861 cupsd_job_t
*job
; /* Current job */
872 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
874 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
876 cupsArrayRemove(Jobs
, job
);
877 cupsArrayRemove(ActiveJobs
, job
);
878 cupsArrayRemove(PrintingJobs
, job
);
883 cupsdReleaseSignals();
888 * 'cupsdFindJob()' - Find the specified job.
891 cupsd_job_t
* /* O - Job data */
892 cupsdFindJob(int id
) /* I - Job ID */
894 cupsd_job_t key
; /* Search key */
899 return ((cupsd_job_t
*)cupsArrayFind(Jobs
, &key
));
904 * 'cupsdGetPrinterJobCount()' - Get the number of pending, processing,
905 * or held jobs in a printer or class.
908 int /* O - Job count */
909 cupsdGetPrinterJobCount(
910 const char *dest
) /* I - Printer or class name */
912 int count
; /* Job count */
913 cupsd_job_t
*job
; /* Current job */
916 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
918 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
919 if (job
->dest
&& !strcasecmp(job
->dest
, dest
))
927 * 'cupsdGetUserJobCount()' - Get the number of pending, processing,
928 * or held jobs for a user.
931 int /* O - Job count */
932 cupsdGetUserJobCount(
933 const char *username
) /* I - Username */
935 int count
; /* Job count */
936 cupsd_job_t
*job
; /* Current job */
939 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
941 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
942 if (!strcasecmp(job
->username
, username
))
950 * 'cupsdHoldJob()' - Hold the specified job.
954 cupsdHoldJob(cupsd_job_t
*job
) /* I - Job data */
956 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdHoldJob: id = %d", job
->id
);
958 if (job
->state_value
== IPP_JOB_PROCESSING
)
959 cupsdStopJob(job
, 0);
963 DEBUG_puts("cupsdHoldJob: setting state to held...");
965 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
966 job
->state_value
= IPP_JOB_HELD
;
967 job
->current_file
= 0;
970 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
975 * 'cupsdLoadAllJobs()' - Load all jobs from disk.
979 cupsdLoadAllJobs(void)
981 char filename
[1024]; /* Full filename of job.cache file */
982 struct stat fileinfo
, /* Information on job.cache file */
983 dirinfo
; /* Information on RequestRoot dir */
988 * Create the job arrays as needed...
992 Jobs
= cupsArrayNew(compare_jobs
, NULL
);
995 ActiveJobs
= cupsArrayNew(compare_active_jobs
, NULL
);
998 PrintingJobs
= cupsArrayNew(compare_jobs
, NULL
);
1001 * See whether the job.cache file is older than the RequestRoot directory...
1004 snprintf(filename
, sizeof(filename
), "%s/job.cache", CacheDir
);
1006 if (stat(filename
, &fileinfo
))
1008 fileinfo
.st_mtime
= 0;
1010 if (errno
!= ENOENT
)
1011 cupsdLogMessage(CUPSD_LOG_ERROR
,
1012 "Unable to get file information for \"%s\" - %s",
1013 filename
, strerror(errno
));
1016 if (stat(RequestRoot
, &dirinfo
))
1018 dirinfo
.st_mtime
= 0;
1020 if (errno
!= ENOENT
)
1021 cupsdLogMessage(CUPSD_LOG_ERROR
,
1022 "Unable to get directory information for \"%s\" - %s",
1023 RequestRoot
, strerror(errno
));
1027 * Load the most recent source for job data...
1030 if (dirinfo
.st_mtime
> fileinfo
.st_mtime
)
1032 load_request_root();
1034 load_next_job_id(filename
);
1037 load_job_cache(filename
);
1040 * Clean out old jobs as needed...
1043 if (MaxJobs
> 0 && cupsArrayCount(Jobs
) >= MaxJobs
)
1049 * 'cupsdLoadJob()' - Load a single job...
1053 cupsdLoadJob(cupsd_job_t
*job
) /* I - Job */
1055 char jobfile
[1024]; /* Job filename */
1056 cups_file_t
*fp
; /* Job file */
1057 int fileid
; /* Current file ID */
1058 ipp_attribute_t
*attr
; /* Job attribute */
1059 const char *dest
; /* Destination name */
1060 cupsd_printer_t
*destptr
; /* Pointer to destination */
1061 mime_type_t
**filetypes
; /* New filetypes array */
1062 int *compressions
; /* New compressions array */
1067 if (job
->state_value
> IPP_JOB_STOPPED
)
1068 job
->access_time
= time(NULL
);
1073 if ((job
->attrs
= ippNew()) == NULL
)
1075 cupsdLogMessage(CUPSD_LOG_ERROR
,
1076 "[Job %d] Ran out of memory for job attributes!", job
->id
);
1081 * Load job attributes...
1084 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Loading attributes...", job
->id
);
1086 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, job
->id
);
1087 if ((fp
= cupsFileOpen(jobfile
, "r")) == NULL
)
1089 cupsdLogMessage(CUPSD_LOG_ERROR
,
1090 "[Job %d] Unable to open job control file \"%s\" - %s!",
1091 job
->id
, jobfile
, strerror(errno
));
1092 ippDelete(job
->attrs
);
1097 if (ippReadIO(fp
, (ipp_iocb_t
)cupsFileRead
, 1, NULL
, job
->attrs
) != IPP_DATA
)
1099 cupsdLogMessage(CUPSD_LOG_ERROR
,
1100 "[Job %d] Unable to read job control file \"%s\"!", job
->id
,
1103 ippDelete(job
->attrs
);
1112 * Copy attribute data to the job object...
1115 if (!ippFindAttribute(job
->attrs
, "time-at-creation", IPP_TAG_INTEGER
))
1117 cupsdLogMessage(CUPSD_LOG_ERROR
,
1118 "[Job %d] Missing or bad time-at-creation attribute in "
1119 "control file!", job
->id
);
1120 ippDelete(job
->attrs
);
1126 if ((job
->state
= ippFindAttribute(job
->attrs
, "job-state",
1127 IPP_TAG_ENUM
)) == NULL
)
1129 cupsdLogMessage(CUPSD_LOG_ERROR
,
1130 "[Job %d] Missing or bad job-state attribute in control "
1132 ippDelete(job
->attrs
);
1138 job
->state_value
= (ipp_jstate_t
)job
->state
->values
[0].integer
;
1142 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1143 IPP_TAG_URI
)) == NULL
)
1145 cupsdLogMessage(CUPSD_LOG_ERROR
,
1146 "[Job %d] No job-printer-uri attribute in control file!",
1148 ippDelete(job
->attrs
);
1154 if ((dest
= cupsdValidateDest(attr
->values
[0].string
.text
, &(job
->dtype
),
1157 cupsdLogMessage(CUPSD_LOG_ERROR
,
1158 "[Job %d] Unable to queue job for destination \"%s\"!",
1159 job
->id
, attr
->values
[0].string
.text
);
1160 ippDelete(job
->attrs
);
1166 cupsdSetString(&job
->dest
, dest
);
1168 else if ((destptr
= cupsdFindDest(job
->dest
)) == NULL
)
1170 cupsdLogMessage(CUPSD_LOG_ERROR
,
1171 "[Job %d] Unable to queue job for destination \"%s\"!",
1172 job
->id
, job
->dest
);
1173 ippDelete(job
->attrs
);
1179 job
->sheets
= ippFindAttribute(job
->attrs
, "job-media-sheets-completed",
1181 job
->job_sheets
= ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_NAME
);
1185 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
1186 IPP_TAG_INTEGER
)) == NULL
)
1188 cupsdLogMessage(CUPSD_LOG_ERROR
,
1189 "[Job %d] Missing or bad job-priority attribute in "
1190 "control file!", job
->id
);
1191 ippDelete(job
->attrs
);
1197 job
->priority
= attr
->values
[0].integer
;
1202 if ((attr
= ippFindAttribute(job
->attrs
, "job-originating-user-name",
1203 IPP_TAG_NAME
)) == NULL
)
1205 cupsdLogMessage(CUPSD_LOG_ERROR
,
1206 "[Job %d] Missing or bad job-originating-user-name "
1207 "attribute in control file!", job
->id
);
1208 ippDelete(job
->attrs
);
1214 cupsdSetString(&job
->username
, attr
->values
[0].string
.text
);
1218 * Set the job hold-until time and state...
1221 if (job
->state_value
== IPP_JOB_HELD
)
1223 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1224 IPP_TAG_KEYWORD
)) == NULL
)
1225 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1228 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
1231 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1232 job
->state_value
= IPP_JOB_PENDING
;
1235 else if (job
->state_value
== IPP_JOB_PROCESSING
)
1237 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1238 job
->state_value
= IPP_JOB_PENDING
;
1241 if (!job
->num_files
)
1244 * Find all the d##### files...
1247 for (fileid
= 1; fileid
< 10000; fileid
++)
1249 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
1252 if (access(jobfile
, 0))
1255 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1256 "[Job %d] Auto-typing document file \"%s\"...", job
->id
,
1259 if (fileid
> job
->num_files
)
1261 if (job
->num_files
== 0)
1263 compressions
= (int *)calloc(fileid
, sizeof(int));
1264 filetypes
= (mime_type_t
**)calloc(fileid
, sizeof(mime_type_t
*));
1268 compressions
= (int *)realloc(job
->compressions
,
1269 sizeof(int) * fileid
);
1270 filetypes
= (mime_type_t
**)realloc(job
->filetypes
,
1271 sizeof(mime_type_t
*) *
1275 if (!compressions
|| !filetypes
)
1277 cupsdLogMessage(CUPSD_LOG_ERROR
,
1278 "[Job %d] Ran out of memory for job file types!",
1283 job
->compressions
= compressions
;
1284 job
->filetypes
= filetypes
;
1285 job
->num_files
= fileid
;
1288 job
->filetypes
[fileid
- 1] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
1289 job
->compressions
+ fileid
- 1);
1291 if (!job
->filetypes
[fileid
- 1])
1292 job
->filetypes
[fileid
- 1] = mimeType(MimeDatabase
, "application",
1298 * Load authentication information as needed...
1301 if (job
->state_value
< IPP_JOB_STOPPED
)
1303 snprintf(jobfile
, sizeof(jobfile
), "%s/a%05d", RequestRoot
, job
->id
);
1305 cupsdClearString(&job
->auth_username
);
1306 cupsdClearString(&job
->auth_domain
);
1307 cupsdClearString(&job
->auth_password
);
1309 if ((fp
= cupsFileOpen(jobfile
, "r")) != NULL
)
1311 int i
, /* Looping var */
1312 bytes
; /* Size of auth data */
1313 char line
[255], /* Line from file */
1314 data
[255]; /* Decoded data */
1318 i
< destptr
->num_auth_info_required
&&
1319 cupsFileGets(fp
, line
, sizeof(line
));
1322 bytes
= sizeof(data
);
1323 httpDecode64_2(data
, &bytes
, line
);
1325 if (!strcmp(destptr
->auth_info_required
[i
], "username"))
1326 cupsdSetStringf(&job
->auth_username
, "AUTH_USERNAME=%s", data
);
1327 else if (!strcmp(destptr
->auth_info_required
[i
], "domain"))
1328 cupsdSetStringf(&job
->auth_domain
, "AUTH_DOMAIN=%s", data
);
1329 else if (!strcmp(destptr
->auth_info_required
[i
], "password"))
1330 cupsdSetStringf(&job
->auth_password
, "AUTH_PASSWORD=%s", data
);
1337 job
->access_time
= time(NULL
);
1342 * 'cupsdMoveJob()' - Move the specified job to a different destination.
1346 cupsdMoveJob(cupsd_job_t
*job
, /* I - Job */
1347 cupsd_printer_t
*p
) /* I - Destination printer or class */
1349 ipp_attribute_t
*attr
; /* job-printer-uri attribute */
1350 const char *olddest
; /* Old destination */
1351 cupsd_printer_t
*oldp
; /* Old pointer */
1355 * Don't move completed jobs...
1358 if (job
->state_value
> IPP_JOB_STOPPED
)
1362 * Get the old destination...
1365 olddest
= job
->dest
;
1368 oldp
= job
->printer
;
1370 oldp
= cupsdFindDest(olddest
);
1373 * Change the destination information...
1376 if (job
->state_value
== IPP_JOB_PROCESSING
)
1377 cupsdStopJob(job
, 0);
1381 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, oldp
, job
,
1382 "Job #%d moved from %s to %s.", job
->id
, olddest
,
1385 cupsdSetString(&job
->dest
, p
->name
);
1386 job
->dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_REMOTE
|
1387 CUPS_PRINTER_IMPLICIT
);
1389 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1390 IPP_TAG_URI
)) != NULL
)
1391 cupsdSetString(&(attr
->values
[0].string
.text
), p
->uri
);
1393 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, p
, job
,
1394 "Job #%d moved from %s to %s.", job
->id
, olddest
,
1398 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1403 * 'cupsdReleaseJob()' - Release the specified job.
1407 cupsdReleaseJob(cupsd_job_t
*job
) /* I - Job */
1409 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdReleaseJob: id = %d", job
->id
);
1411 if (job
->state_value
== IPP_JOB_HELD
)
1414 * Add trailing banner as needed...
1417 if (job
->pending_timeout
)
1418 cupsdTimeoutJob(job
);
1420 DEBUG_puts("cupsdReleaseJob: setting state to pending...");
1422 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1423 job
->state_value
= IPP_JOB_PENDING
;
1425 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1431 * 'cupsdRestartJob()' - Restart the specified job.
1435 cupsdRestartJob(cupsd_job_t
*job
) /* I - Job */
1437 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdRestartJob: id = %d", job
->id
);
1439 if (job
->state_value
== IPP_JOB_STOPPED
|| job
->num_files
)
1441 ipp_jstate_t old_state
; /* Old job state */
1446 old_state
= job
->state_value
;
1449 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1450 job
->state_value
= IPP_JOB_PENDING
;
1453 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1455 if (old_state
> IPP_JOB_STOPPED
)
1456 cupsArrayAdd(ActiveJobs
, job
);
1462 * 'cupsdSaveAllJobs()' - Save a summary of all jobs to disk.
1466 cupsdSaveAllJobs(void)
1468 int i
; /* Looping var */
1469 cups_file_t
*fp
; /* Job cache file */
1470 char temp
[1024]; /* Temporary string */
1471 cupsd_job_t
*job
; /* Current job */
1472 time_t curtime
; /* Current time */
1473 struct tm
*curdate
; /* Current date */
1476 snprintf(temp
, sizeof(temp
), "%s/job.cache", CacheDir
);
1477 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
1479 cupsdLogMessage(CUPSD_LOG_ERROR
,
1480 "Unable to create job cache file \"%s\" - %s",
1481 temp
, strerror(errno
));
1485 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving job cache file \"%s\"...", temp
);
1488 * Restrict access to the file...
1491 fchown(cupsFileNumber(fp
), getuid(), Group
);
1492 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
1495 * Write a small header to the file...
1498 curtime
= time(NULL
);
1499 curdate
= localtime(&curtime
);
1500 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
1502 cupsFilePuts(fp
, "# Job cache file for " CUPS_SVERSION
"\n");
1503 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
1504 cupsFilePrintf(fp
, "NextJobId %d\n", NextJobId
);
1507 * Write each job known to the system...
1510 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1512 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1514 cupsFilePrintf(fp
, "<Job %d>\n", job
->id
);
1515 cupsFilePrintf(fp
, "State %d\n", job
->state_value
);
1516 cupsFilePrintf(fp
, "Priority %d\n", job
->priority
);
1517 cupsFilePrintf(fp
, "Username %s\n", job
->username
);
1518 cupsFilePrintf(fp
, "Destination %s\n", job
->dest
);
1519 cupsFilePrintf(fp
, "DestType %d\n", job
->dtype
);
1520 cupsFilePrintf(fp
, "NumFiles %d\n", job
->num_files
);
1521 for (i
= 0; i
< job
->num_files
; i
++)
1522 cupsFilePrintf(fp
, "File %d %s/%s %d\n", i
+ 1, job
->filetypes
[i
]->super
,
1523 job
->filetypes
[i
]->type
, job
->compressions
[i
]);
1524 cupsFilePuts(fp
, "</Job>\n");
1532 * 'cupsdSaveJob()' - Save a job to disk.
1536 cupsdSaveJob(cupsd_job_t
*job
) /* I - Job */
1538 char filename
[1024]; /* Job control filename */
1539 cups_file_t
*fp
; /* Job file */
1542 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSaveJob(job=%p(%d)): job->attrs=%p",
1543 job
, job
->id
, job
->attrs
);
1545 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
, job
->id
);
1547 if ((fp
= cupsFileOpen(filename
, "w")) == NULL
)
1549 cupsdLogMessage(CUPSD_LOG_ERROR
,
1550 "[Job %d] Unable to create job control file \"%s\" - %s.",
1551 job
->id
, filename
, strerror(errno
));
1555 fchmod(cupsFileNumber(fp
), 0600);
1556 fchown(cupsFileNumber(fp
), RunUser
, Group
);
1558 job
->attrs
->state
= IPP_IDLE
;
1560 if (ippWriteIO(fp
, (ipp_iocb_t
)cupsFileWrite
, 1, NULL
,
1561 job
->attrs
) != IPP_DATA
)
1562 cupsdLogMessage(CUPSD_LOG_ERROR
,
1563 "[Job %d] Unable to write job control file!", job
->id
);
1572 * 'cupsdSetJobHoldUntil()' - Set the hold time for a job...
1576 cupsdSetJobHoldUntil(cupsd_job_t
*job
, /* I - Job */
1577 const char *when
) /* I - When to resume */
1579 time_t curtime
; /* Current time */
1580 struct tm
*curdate
; /* Current date */
1581 int hour
; /* Hold hour */
1582 int minute
; /* Hold minute */
1583 int second
; /* Hold second */
1586 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetJobHoldUntil(%d, \"%s\")",
1591 if (!strcmp(when
, "indefinite") || !strcmp(when
, "auth-info-required"))
1594 * Hold indefinitely...
1597 job
->hold_until
= 0;
1599 else if (!strcmp(when
, "day-time"))
1602 * Hold to 6am the next morning unless local time is < 6pm.
1605 curtime
= time(NULL
);
1606 curdate
= localtime(&curtime
);
1608 if (curdate
->tm_hour
< 18)
1609 job
->hold_until
= curtime
;
1611 job
->hold_until
= curtime
+
1612 ((29 - curdate
->tm_hour
) * 60 + 59 -
1613 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1615 else if (!strcmp(when
, "evening") || !strcmp(when
, "night"))
1618 * Hold to 6pm unless local time is > 6pm or < 6am.
1621 curtime
= time(NULL
);
1622 curdate
= localtime(&curtime
);
1624 if (curdate
->tm_hour
< 6 || curdate
->tm_hour
>= 18)
1625 job
->hold_until
= curtime
;
1627 job
->hold_until
= curtime
+
1628 ((17 - curdate
->tm_hour
) * 60 + 59 -
1629 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1631 else if (!strcmp(when
, "second-shift"))
1634 * Hold to 4pm unless local time is > 4pm.
1637 curtime
= time(NULL
);
1638 curdate
= localtime(&curtime
);
1640 if (curdate
->tm_hour
>= 16)
1641 job
->hold_until
= curtime
;
1643 job
->hold_until
= curtime
+
1644 ((15 - curdate
->tm_hour
) * 60 + 59 -
1645 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1647 else if (!strcmp(when
, "third-shift"))
1650 * Hold to 12am unless local time is < 8am.
1653 curtime
= time(NULL
);
1654 curdate
= localtime(&curtime
);
1656 if (curdate
->tm_hour
< 8)
1657 job
->hold_until
= curtime
;
1659 job
->hold_until
= curtime
+
1660 ((23 - curdate
->tm_hour
) * 60 + 59 -
1661 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1663 else if (!strcmp(when
, "weekend"))
1666 * Hold to weekend unless we are in the weekend.
1669 curtime
= time(NULL
);
1670 curdate
= localtime(&curtime
);
1672 if (curdate
->tm_wday
== 0 || curdate
->tm_wday
== 6)
1673 job
->hold_until
= curtime
;
1675 job
->hold_until
= curtime
+
1676 (((5 - curdate
->tm_wday
) * 24 +
1677 (17 - curdate
->tm_hour
)) * 60 + 59 -
1678 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1680 else if (sscanf(when
, "%d:%d:%d", &hour
, &minute
, &second
) >= 2)
1683 * Hold to specified GMT time (HH:MM or HH:MM:SS)...
1686 curtime
= time(NULL
);
1687 curdate
= gmtime(&curtime
);
1689 job
->hold_until
= curtime
+
1690 ((hour
- curdate
->tm_hour
) * 60 + minute
-
1691 curdate
->tm_min
) * 60 + second
- curdate
->tm_sec
;
1694 * Hold until next day as needed...
1697 if (job
->hold_until
< curtime
)
1698 job
->hold_until
+= 24 * 60 * 60;
1701 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetJobHoldUntil: hold_until = %d",
1702 (int)job
->hold_until
);
1707 * 'cupsdSetJobPriority()' - Set the priority of a job, moving it up/down in
1708 * the list as needed.
1712 cupsdSetJobPriority(
1713 cupsd_job_t
*job
, /* I - Job ID */
1714 int priority
) /* I - New priority (0 to 100) */
1716 ipp_attribute_t
*attr
; /* Job attribute */
1720 * Don't change completed jobs...
1723 if (job
->state_value
>= IPP_JOB_PROCESSING
)
1727 * Set the new priority and re-add the job into the active list...
1730 cupsArrayRemove(ActiveJobs
, job
);
1732 job
->priority
= priority
;
1734 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
1735 IPP_TAG_INTEGER
)) != NULL
)
1736 attr
->values
[0].integer
= priority
;
1738 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-priority",
1741 cupsArrayAdd(ActiveJobs
, job
);
1744 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1749 * 'cupsdStopAllJobs()' - Stop all print jobs.
1753 cupsdStopAllJobs(int force
) /* I - 1 = Force all filters to stop */
1755 cupsd_job_t
*job
; /* Current job */
1758 DEBUG_puts("cupsdStopAllJobs()");
1760 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
1762 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
1763 if (job
->state_value
== IPP_JOB_PROCESSING
)
1765 cupsdStopJob(job
, force
);
1766 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1767 job
->state_value
= IPP_JOB_PENDING
;
1773 * 'cupsdStopJob()' - Stop a print job.
1777 cupsdStopJob(cupsd_job_t
*job
, /* I - Job */
1778 int force
) /* I - 1 = Force all filters to stop */
1780 int i
; /* Looping var */
1783 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] cupsdStopJob: force = %d",
1786 if (job
->state_value
!= IPP_JOB_PROCESSING
)
1789 FilterLevel
-= job
->cost
;
1791 if (job
->printer
->state
== IPP_PRINTER_PROCESSING
)
1792 cupsdSetPrinterState(job
->printer
, IPP_PRINTER_IDLE
, 0);
1794 job
->state
->values
[0].integer
= IPP_JOB_STOPPED
;
1795 job
->state_value
= IPP_JOB_STOPPED
;
1796 job
->printer
->job
= NULL
;
1797 job
->printer
= NULL
;
1799 job
->current_file
--;
1801 for (i
= 0; job
->filters
[i
]; i
++)
1802 if (job
->filters
[i
] > 0)
1804 cupsdEndProcess(job
->filters
[i
], force
);
1805 job
->filters
[i
] = 0;
1808 if (job
->backend
> 0)
1810 cupsdEndProcess(job
->backend
, force
);
1814 cupsdDestroyProfile(job
->profile
);
1815 job
->profile
= NULL
;
1817 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] Closing print pipes [ %d %d ]...",
1818 job
->id
, job
->print_pipes
[0], job
->print_pipes
[1]);
1820 cupsdClosePipe(job
->print_pipes
);
1822 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] Closing back pipes [ %d %d ]...",
1823 job
->id
, job
->back_pipes
[0], job
->back_pipes
[1]);
1825 cupsdClosePipe(job
->back_pipes
);
1827 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] Closing side pipes [ %d %d ]...",
1828 job
->id
, job
->side_pipes
[0], job
->side_pipes
[1]);
1830 cupsdClosePipe(job
->side_pipes
);
1832 if (job
->status_buffer
)
1835 * Close the pipe and clear the input bit.
1838 cupsdRemoveSelect(job
->status_buffer
->fd
);
1840 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1841 "[Job %d] Closing status pipes [ %d %d ]...",
1842 job
->id
, job
->status_pipes
[0], job
->status_pipes
[1]);
1844 cupsdClosePipe(job
->status_pipes
);
1845 cupsdStatBufDelete(job
->status_buffer
);
1847 job
->status_buffer
= NULL
;
1853 * 'cupsdUnloadCompletedJobs()' - Flush completed job history from memory.
1857 cupsdUnloadCompletedJobs(void)
1859 cupsd_job_t
*job
; /* Current job */
1860 time_t expire
; /* Expiration time */
1863 expire
= time(NULL
) - 60;
1865 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1867 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1868 if (job
->attrs
&& job
->state_value
>= IPP_JOB_STOPPED
&&
1869 job
->access_time
< expire
)
1880 * 'compare_active_jobs()' - Compare the job IDs and priorities of two jobs.
1883 static int /* O - Difference */
1884 compare_active_jobs(void *first
, /* I - First job */
1885 void *second
, /* I - Second job */
1886 void *data
) /* I - App data (not used) */
1888 int diff
; /* Difference */
1891 if ((diff
= ((cupsd_job_t
*)second
)->priority
-
1892 ((cupsd_job_t
*)first
)->priority
) != 0)
1895 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
1900 * 'compare_jobs()' - Compare the job IDs of two jobs.
1903 static int /* O - Difference */
1904 compare_jobs(void *first
, /* I - First job */
1905 void *second
, /* I - Second job */
1906 void *data
) /* I - App data (not used) */
1908 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
1913 * 'ipp_length()' - Compute the size of the buffer needed to hold
1914 * the textual IPP attributes.
1917 static int /* O - Size of attribute buffer */
1918 ipp_length(ipp_t
*ipp
) /* I - IPP request */
1920 int bytes
; /* Number of bytes */
1921 int i
; /* Looping var */
1922 ipp_attribute_t
*attr
; /* Current attribute */
1926 * Loop through all attributes...
1931 for (attr
= ipp
->attrs
; attr
!= NULL
; attr
= attr
->next
)
1934 * Skip attributes that won't be sent to filters...
1937 if (attr
->value_tag
== IPP_TAG_MIMETYPE
||
1938 attr
->value_tag
== IPP_TAG_NAMELANG
||
1939 attr
->value_tag
== IPP_TAG_TEXTLANG
||
1940 attr
->value_tag
== IPP_TAG_URI
||
1941 attr
->value_tag
== IPP_TAG_URISCHEME
)
1944 if (strncmp(attr
->name
, "time-", 5) == 0)
1948 * Add space for a leading space and commas between each value.
1949 * For the first attribute, the leading space isn't used, so the
1950 * extra byte can be used as the nul terminator...
1953 bytes
++; /* " " separator */
1954 bytes
+= attr
->num_values
; /* "," separators */
1957 * Boolean attributes appear as "foo,nofoo,foo,nofoo", while
1958 * other attributes appear as "foo=value1,value2,...,valueN".
1961 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
1962 bytes
+= strlen(attr
->name
);
1964 bytes
+= attr
->num_values
* strlen(attr
->name
);
1967 * Now add the size required for each value in the attribute...
1970 switch (attr
->value_tag
)
1972 case IPP_TAG_INTEGER
:
1975 * Minimum value of a signed integer is -2147483647, or 11 digits.
1978 bytes
+= attr
->num_values
* 11;
1981 case IPP_TAG_BOOLEAN
:
1983 * Add two bytes for each false ("no") value...
1986 for (i
= 0; i
< attr
->num_values
; i
++)
1987 if (!attr
->values
[i
].boolean
)
1991 case IPP_TAG_RANGE
:
1993 * A range is two signed integers separated by a hyphen, or
1994 * 23 characters max.
1997 bytes
+= attr
->num_values
* 23;
2000 case IPP_TAG_RESOLUTION
:
2002 * A resolution is two signed integers separated by an "x" and
2003 * suffixed by the units, or 26 characters max.
2006 bytes
+= attr
->num_values
* 26;
2009 case IPP_TAG_STRING
:
2012 case IPP_TAG_KEYWORD
:
2013 case IPP_TAG_CHARSET
:
2014 case IPP_TAG_LANGUAGE
:
2017 * Strings can contain characters that need quoting. We need
2018 * at least 2 * len + 2 characters to cover the quotes and
2019 * any backslashes in the string.
2022 for (i
= 0; i
< attr
->num_values
; i
++)
2023 bytes
+= 2 * strlen(attr
->values
[i
].string
.text
) + 2;
2027 break; /* anti-compiler-warning-code */
2036 * 'load_job_cache()' - Load jobs from the job.cache file.
2040 load_job_cache(const char *filename
) /* I - job.cache filename */
2042 cups_file_t
*fp
; /* job.cache file */
2043 char line
[1024], /* Line buffer */
2044 *value
; /* Value on line */
2045 int linenum
; /* Line number in file */
2046 cupsd_job_t
*job
; /* Current job */
2047 int jobid
; /* Job ID */
2048 char jobfile
[1024]; /* Job filename */
2052 * Open the job.cache file...
2055 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
2057 if (errno
!= ENOENT
)
2058 cupsdLogMessage(CUPSD_LOG_ERROR
,
2059 "Unable to open job cache file \"%s\": %s",
2060 filename
, strerror(errno
));
2062 load_request_root();
2068 * Read entries from the job cache file and create jobs as needed.
2071 cupsdLogMessage(CUPSD_LOG_INFO
, "Loading job cache file \"%s\"...",
2077 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
2079 if (!strcasecmp(line
, "NextJobId"))
2082 NextJobId
= atoi(value
);
2084 else if (!strcasecmp(line
, "<Job"))
2088 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing </Job> directive on line %d!",
2095 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing job ID on line %d!", linenum
);
2099 jobid
= atoi(value
);
2103 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad job ID %d on line %d!", jobid
,
2108 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, jobid
);
2109 if (access(jobfile
, 0))
2111 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Job %d] Files have gone away!",
2116 job
= calloc(1, sizeof(cupsd_job_t
));
2119 cupsdLogMessage(CUPSD_LOG_EMERG
,
2120 "[Job %d] Unable to allocate memory for job!", jobid
);
2125 job
->back_pipes
[0] = -1;
2126 job
->back_pipes
[1] = -1;
2127 job
->print_pipes
[0] = -1;
2128 job
->print_pipes
[1] = -1;
2129 job
->side_pipes
[0] = -1;
2130 job
->side_pipes
[1] = -1;
2131 job
->status_pipes
[0] = -1;
2132 job
->status_pipes
[1] = -1;
2134 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Loading from cache...",
2139 cupsdLogMessage(CUPSD_LOG_ERROR
,
2140 "Missing <Job #> directive on line %d!", linenum
);
2143 else if (!strcasecmp(line
, "</Job>"))
2145 cupsArrayAdd(Jobs
, job
);
2147 if (job
->state_value
<= IPP_JOB_STOPPED
)
2149 cupsArrayAdd(ActiveJobs
, job
);
2157 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing value on line %d!", linenum
);
2160 else if (!strcasecmp(line
, "State"))
2162 job
->state_value
= (ipp_jstate_t
)atoi(value
);
2164 if (job
->state_value
< IPP_JOB_PENDING
)
2165 job
->state_value
= IPP_JOB_PENDING
;
2166 else if (job
->state_value
> IPP_JOB_COMPLETED
)
2167 job
->state_value
= IPP_JOB_COMPLETED
;
2169 else if (!strcasecmp(line
, "Priority"))
2171 job
->priority
= atoi(value
);
2173 else if (!strcasecmp(line
, "Username"))
2175 cupsdSetString(&job
->username
, value
);
2177 else if (!strcasecmp(line
, "Destination"))
2179 cupsdSetString(&job
->dest
, value
);
2181 else if (!strcasecmp(line
, "DestType"))
2183 job
->dtype
= (cups_ptype_t
)atoi(value
);
2185 else if (!strcasecmp(line
, "NumFiles"))
2187 job
->num_files
= atoi(value
);
2189 if (job
->num_files
< 0)
2191 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad NumFiles value %d on line %d!",
2192 job
->num_files
, linenum
);
2197 if (job
->num_files
> 0)
2199 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-001", RequestRoot
,
2201 if (access(jobfile
, 0))
2203 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Data files have gone away!",
2209 job
->filetypes
= calloc(job
->num_files
, sizeof(mime_type_t
*));
2210 job
->compressions
= calloc(job
->num_files
, sizeof(int));
2212 if (!job
->filetypes
|| !job
->compressions
)
2214 cupsdLogMessage(CUPSD_LOG_EMERG
,
2215 "[Job %d] Unable to allocate memory for %d files!",
2216 job
->id
, job
->num_files
);
2221 else if (!strcasecmp(line
, "File"))
2223 int number
, /* File number */
2224 compression
; /* Compression value */
2225 char super
[MIME_MAX_SUPER
], /* MIME super type */
2226 type
[MIME_MAX_TYPE
]; /* MIME type */
2229 if (sscanf(value
, "%d%*[ \t]%15[^/]/%255s%d", &number
, super
, type
,
2232 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File on line %d!", linenum
);
2236 if (number
< 1 || number
> job
->num_files
)
2238 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File number %d on line %d!",
2245 job
->compressions
[number
] = compression
;
2246 job
->filetypes
[number
] = mimeType(MimeDatabase
, super
, type
);
2248 if (!job
->filetypes
[number
])
2251 * If the original MIME type is unknown, auto-type it!
2254 cupsdLogMessage(CUPSD_LOG_ERROR
,
2255 "[Job %d] Unknown MIME type %s/%s for file %d!",
2256 job
->id
, super
, type
, number
+ 1);
2258 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
2259 job
->id
, number
+ 1);
2260 job
->filetypes
[number
] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
2261 job
->compressions
+ number
);
2264 * If that didn't work, assume it is raw...
2267 if (!job
->filetypes
[number
])
2268 job
->filetypes
[number
] = mimeType(MimeDatabase
, "application",
2273 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unknown %s directive on line %d!",
2282 * 'load_next_job_id()' - Load the NextJobId value from the job.cache file.
2286 load_next_job_id(const char *filename
) /* I - job.cache filename */
2288 cups_file_t
*fp
; /* job.cache file */
2289 char line
[1024], /* Line buffer */
2290 *value
; /* Value on line */
2291 int linenum
; /* Line number in file */
2292 int next_job_id
; /* NextJobId value from line */
2296 * Read the NextJobId directive from the job.cache file and use
2297 * the value (if any).
2300 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
2302 if (errno
!= ENOENT
)
2303 cupsdLogMessage(CUPSD_LOG_ERROR
,
2304 "Unable to open job cache file \"%s\": %s",
2305 filename
, strerror(errno
));
2310 cupsdLogMessage(CUPSD_LOG_INFO
,
2311 "Loading NextJobId from job cache file \"%s\"...", filename
);
2315 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
2317 if (!strcasecmp(line
, "NextJobId"))
2321 next_job_id
= atoi(value
);
2323 if (next_job_id
> NextJobId
)
2324 NextJobId
= next_job_id
;
2335 * 'load_request_root()' - Load jobs from the RequestRoot directory.
2339 load_request_root(void)
2341 cups_dir_t
*dir
; /* Directory */
2342 cups_dentry_t
*dent
; /* Directory entry */
2343 cupsd_job_t
*job
; /* New job */
2347 * Open the requests directory...
2350 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Scanning %s for jobs...", RequestRoot
);
2352 if ((dir
= cupsDirOpen(RequestRoot
)) == NULL
)
2354 cupsdLogMessage(CUPSD_LOG_ERROR
,
2355 "Unable to open spool directory \"%s\": %s",
2356 RequestRoot
, strerror(errno
));
2361 * Read all the c##### files...
2364 while ((dent
= cupsDirRead(dir
)) != NULL
)
2365 if (strlen(dent
->filename
) >= 6 && dent
->filename
[0] == 'c')
2368 * Allocate memory for the job...
2371 if ((job
= calloc(sizeof(cupsd_job_t
), 1)) == NULL
)
2373 cupsdLogMessage(CUPSD_LOG_ERROR
, "Ran out of memory for jobs!");
2379 * Assign the job ID...
2382 job
->id
= atoi(dent
->filename
+ 1);
2383 job
->back_pipes
[0] = -1;
2384 job
->back_pipes
[1] = -1;
2385 job
->print_pipes
[0] = -1;
2386 job
->print_pipes
[1] = -1;
2387 job
->side_pipes
[0] = -1;
2388 job
->side_pipes
[1] = -1;
2389 job
->status_pipes
[0] = -1;
2390 job
->status_pipes
[1] = -1;
2392 if (job
->id
>= NextJobId
)
2393 NextJobId
= job
->id
+ 1;
2402 * Insert the job into the array, sorting by job priority and ID...
2405 cupsArrayAdd(Jobs
, job
);
2407 if (job
->state_value
<= IPP_JOB_STOPPED
)
2408 cupsArrayAdd(ActiveJobs
, job
);
2418 * 'set_time()' - Set one of the "time-at-xyz" attributes...
2422 set_time(cupsd_job_t
*job
, /* I - Job to update */
2423 const char *name
) /* I - Name of attribute */
2425 ipp_attribute_t
*attr
; /* Time attribute */
2428 if ((attr
= ippFindAttribute(job
->attrs
, name
, IPP_TAG_ZERO
)) != NULL
)
2430 attr
->value_tag
= IPP_TAG_INTEGER
;
2431 attr
->values
[0].integer
= time(NULL
);
2437 * 'set_hold_until()' - Set the hold time and update job-hold-until attribute...
2441 set_hold_until(cupsd_job_t
*job
, /* I - Job to update */
2442 time_t holdtime
) /* I - Hold until time */
2444 ipp_attribute_t
*attr
; /* job-hold-until attribute */
2445 struct tm
*holddate
; /* Hold date */
2446 char holdstr
[64]; /* Hold time */
2450 * Set the hold_until value and hold the job...
2453 cupsdLogMessage(CUPSD_LOG_DEBUG
, "set_hold_until: hold_until = %d",
2456 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
2457 job
->state_value
= IPP_JOB_HELD
;
2458 job
->hold_until
= holdtime
;
2461 * Update the job-hold-until attribute with a string representing GMT
2462 * time (HH:MM:SS)...
2465 holddate
= gmtime(&holdtime
);
2466 snprintf(holdstr
, sizeof(holdstr
), "%d:%d:%d", holddate
->tm_hour
,
2467 holddate
->tm_min
, holddate
->tm_sec
);
2469 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2470 IPP_TAG_KEYWORD
)) == NULL
)
2471 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2474 * Either add the attribute or update the value of the existing one
2478 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "job-hold-until",
2481 cupsdSetString(&attr
->values
[0].string
.text
, holdstr
);
2484 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2489 * 'start_job()' - Start a print job.
2493 start_job(cupsd_job_t
*job
, /* I - Job ID */
2494 cupsd_printer_t
*printer
) /* I - Printer to print job */
2496 int i
; /* Looping var */
2497 int slot
; /* Pipe slot */
2498 cups_array_t
*filters
, /* Filters for job */
2499 *prefilters
; /* Filters with prefilters */
2500 mime_filter_t
*filter
, /* Current filter */
2501 *prefilter
, /* Prefilter */
2502 port_monitor
; /* Port monitor filter */
2503 char method
[255], /* Method for output */
2504 *optptr
, /* Pointer to options */
2505 *valptr
; /* Pointer in value string */
2506 ipp_attribute_t
*attr
; /* Current attribute */
2507 struct stat backinfo
; /* Backend file information */
2508 int backroot
; /* Run backend as root? */
2509 int pid
; /* Process ID of new filter process */
2510 int banner_page
; /* 1 if banner page, 0 otherwise */
2511 int filterfds
[2][2];/* Pipes used between filters */
2512 int envc
; /* Number of environment variables */
2513 char **argv
, /* Filter command-line arguments */
2514 filename
[1024], /* Job filename */
2515 command
[1024], /* Full path to command */
2516 jobid
[255], /* Job ID string */
2517 title
[IPP_MAX_NAME
],
2518 /* Job title string */
2519 copies
[255], /* # copies string */
2520 *envp
[MAX_ENV
+ 19],
2521 /* Environment variables */
2522 charset
[255], /* CHARSET env variable */
2523 class_name
[255],/* CLASS env variable */
2524 classification
[1024],
2525 /* CLASSIFICATION env variable */
2527 /* CONTENT_TYPE env variable */
2529 /* DEVICE_URI env variable */
2530 final_content_type
[1024],
2531 /* FINAL_CONTENT_TYPE env variable */
2532 lang
[255], /* LANG env variable */
2534 apple_language
[255],
2535 /* APPLE_LANGUAGE env variable */
2536 #endif /* __APPLE__ */
2537 ppd
[1024], /* PPD env variable */
2539 /* PRINTER_INFO env variable */
2540 printer_location
[255],
2541 /* PRINTER_LOCATION env variable */
2543 /* PRINTER env variable */
2545 /* RIP_MAX_CACHE env variable */
2546 static char *options
= NULL
;/* Full list of options */
2547 static int optlength
= 0; /* Length of option buffer */
2550 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "start_job: file = %d/%d",
2551 job
->current_file
, job
->num_files
);
2553 if (job
->num_files
== 0)
2555 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "No files, canceling job!");
2556 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2561 * Figure out what filters are required to convert from
2562 * the source to the destination type...
2571 * Remote jobs and raw queues go directly to the printer without
2575 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Sending job to queue tagged as raw...");
2582 * Local jobs get filtered...
2585 filters
= mimeFilter(MimeDatabase
, job
->filetypes
[job
->current_file
],
2586 printer
->filetype
, &(job
->cost
));
2590 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
2591 "Unable to convert file %d to printable format!",
2593 cupsdLogMessage(CUPSD_LOG_INFO
,
2594 "Hint: Do you have Ghostscript installed?");
2596 if (LogLevel
< CUPSD_LOG_DEBUG
)
2597 cupsdLogMessage(CUPSD_LOG_INFO
,
2598 "Hint: Try setting the LogLevel to \"debug\".");
2600 job
->current_file
++;
2602 if (job
->current_file
== job
->num_files
)
2603 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2609 * Remove NULL ("-") filters...
2612 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
2614 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
2615 if (!strcmp(filter
->filter
, "-"))
2616 cupsArrayRemove(filters
, filter
);
2618 if (cupsArrayCount(filters
) == 0)
2620 cupsArrayDelete(filters
);
2625 * If this printer has any pre-filters, insert the required pre-filter
2626 * in the filters array...
2629 if (printer
->prefiltertype
&& filters
)
2631 prefilters
= cupsArrayNew(NULL
, NULL
);
2633 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
2635 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
2637 if ((prefilter
= mimeFilterLookup(MimeDatabase
, filter
->src
,
2638 printer
->prefiltertype
)))
2640 cupsArrayAdd(prefilters
, prefilter
);
2641 job
->cost
+= prefilter
->cost
;
2644 cupsArrayAdd(prefilters
, filter
);
2647 cupsArrayDelete(filters
);
2648 filters
= prefilters
;
2653 * Set a minimum cost of 100 for all jobs so that FilterLimit
2654 * works with raw queues and other low-cost paths.
2657 if (job
->cost
< 100)
2661 * See if the filter cost is too high...
2664 if ((FilterLevel
+ job
->cost
) > FilterLimit
&& FilterLevel
> 0 &&
2668 * Don't print this job quite yet...
2671 cupsArrayDelete(filters
);
2673 cupsdLogJob(job
, CUPSD_LOG_INFO
,
2674 "Holding because filter limit has been reached.");
2675 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
2676 "start_job: file=%d, cost=%d, level=%d, limit=%d",
2677 job
->current_file
, job
->cost
, FilterLevel
,
2682 FilterLevel
+= job
->cost
;
2685 * Add decompression/raw filter as needed...
2688 if ((!printer
->raw
&& job
->compressions
[job
->current_file
]) ||
2689 (!filters
&& !printer
->remote
&&
2690 (job
->num_files
> 1 || !strncmp(printer
->device_uri
, "file:", 5))))
2693 * Add gziptoany filter to the front of the list...
2697 filters
= cupsArrayNew(NULL
, NULL
);
2699 if (!cupsArrayInsert(filters
, &gziptoany_filter
))
2701 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
2702 "Unable to add decompression filter - %s", strerror(errno
));
2704 cupsArrayDelete(filters
);
2706 job
->current_file
++;
2708 if (job
->current_file
== job
->num_files
)
2709 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2716 * Add port monitor, if any...
2719 if (printer
->port_monitor
)
2722 * Add port monitor to the end of the list...
2726 filters
= cupsArrayNew(NULL
, NULL
);
2728 port_monitor
.src
= NULL
;
2729 port_monitor
.dst
= NULL
;
2730 port_monitor
.cost
= 0;
2732 snprintf(port_monitor
.filter
, sizeof(port_monitor
.filter
),
2733 "%s/monitor/%s", ServerBin
, printer
->port_monitor
);
2735 if (!cupsArrayAdd(filters
, &port_monitor
))
2737 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
2738 "Unable to add port monitor - %s", strerror(errno
));
2740 cupsArrayDelete(filters
);
2742 job
->current_file
++;
2744 if (job
->current_file
== job
->num_files
)
2745 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2752 * Make sure we don't go over the "MAX_FILTERS" limit...
2755 if (cupsArrayCount(filters
) > MAX_FILTERS
)
2757 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
2758 "Too many filters (%d > %d), unable to print!",
2759 cupsArrayCount(filters
), MAX_FILTERS
);
2761 cupsArrayDelete(filters
);
2762 cupsdCancelJob(job
, 0, IPP_JOB_STOPPED
);
2768 * Update the printer and job state to "processing"...
2771 job
->state
->values
[0].integer
= IPP_JOB_PROCESSING
;
2772 job
->state_value
= IPP_JOB_PROCESSING
;
2775 job
->printer
= printer
;
2778 cupsdSetPrinterState(printer
, IPP_PRINTER_PROCESSING
, 0);
2780 if (job
->current_file
== 0)
2783 * Add to the printing list...
2786 cupsArrayAdd(PrintingJobs
, job
);
2787 cupsdSetBusyState();
2790 * Set the processing time...
2793 set_time(job
, "time-at-processing");
2796 * Create the backchannel pipes and make them non-blocking...
2799 cupsdOpenPipe(job
->back_pipes
);
2801 fcntl(job
->back_pipes
[0], F_SETFL
,
2802 fcntl(job
->back_pipes
[0], F_GETFL
) | O_NONBLOCK
);
2804 fcntl(job
->back_pipes
[1], F_SETFL
,
2805 fcntl(job
->back_pipes
[1], F_GETFL
) | O_NONBLOCK
);
2808 * Create the side-channel pipes and make them non-blocking...
2811 socketpair(AF_LOCAL
, SOCK_STREAM
, 0, job
->side_pipes
);
2813 fcntl(job
->side_pipes
[0], F_SETFL
,
2814 fcntl(job
->side_pipes
[0], F_GETFL
) | O_NONBLOCK
);
2816 fcntl(job
->side_pipes
[1], F_SETFL
,
2817 fcntl(job
->side_pipes
[1], F_GETFL
) | O_NONBLOCK
);
2821 * Determine if we are printing a banner page or not...
2824 if (job
->job_sheets
== NULL
)
2826 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "No job-sheets attribute.");
2827 if ((job
->job_sheets
=
2828 ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_ZERO
)) != NULL
)
2829 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
2830 "... but someone added one without setting job_sheets!");
2832 else if (job
->job_sheets
->num_values
== 1)
2833 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "job-sheets=%s",
2834 job
->job_sheets
->values
[0].string
.text
);
2836 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "job-sheets=%s,%s",
2837 job
->job_sheets
->values
[0].string
.text
,
2838 job
->job_sheets
->values
[1].string
.text
);
2840 if (printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
))
2842 else if (job
->job_sheets
== NULL
)
2844 else if (strcasecmp(job
->job_sheets
->values
[0].string
.text
, "none") != 0 &&
2845 job
->current_file
== 0)
2847 else if (job
->job_sheets
->num_values
> 1 &&
2848 strcasecmp(job
->job_sheets
->values
[1].string
.text
, "none") != 0 &&
2849 job
->current_file
== (job
->num_files
- 1))
2854 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "banner_page = %d", banner_page
);
2857 * Building the options string is harder than it needs to be, but
2858 * for the moment we need to pass strings for command-line args and
2859 * not IPP attribute pointers... :)
2861 * First allocate/reallocate the option buffer as needed...
2864 i
= ipp_length(job
->attrs
);
2866 if (i
> optlength
|| !options
)
2871 optptr
= realloc(options
, i
);
2875 cupsdLogJob(job
, CUPSD_LOG_CRIT
,
2876 "Unable to allocate %d bytes for option buffer!", i
);
2878 cupsArrayDelete(filters
);
2880 FilterLevel
-= job
->cost
;
2882 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2891 * Now loop through the attributes and convert them to the textual
2892 * representation used by the filters...
2898 snprintf(title
, sizeof(title
), "%s-%d", printer
->name
, job
->id
);
2899 strcpy(copies
, "1");
2901 for (attr
= job
->attrs
->attrs
; attr
!= NULL
; attr
= attr
->next
)
2903 if (!strcmp(attr
->name
, "copies") &&
2904 attr
->value_tag
== IPP_TAG_INTEGER
)
2907 * Don't use the # copies attribute if we are printing the job sheets...
2911 sprintf(copies
, "%d", attr
->values
[0].integer
);
2913 else if (!strcmp(attr
->name
, "job-name") &&
2914 (attr
->value_tag
== IPP_TAG_NAME
||
2915 attr
->value_tag
== IPP_TAG_NAMELANG
))
2916 strlcpy(title
, attr
->values
[0].string
.text
, sizeof(title
));
2917 else if (attr
->group_tag
== IPP_TAG_JOB
)
2920 * Filter out other unwanted attributes...
2923 if (attr
->value_tag
== IPP_TAG_MIMETYPE
||
2924 attr
->value_tag
== IPP_TAG_NAMELANG
||
2925 attr
->value_tag
== IPP_TAG_TEXTLANG
||
2926 (attr
->value_tag
== IPP_TAG_URI
&& strcmp(attr
->name
, "job-uuid")) ||
2927 attr
->value_tag
== IPP_TAG_URISCHEME
||
2928 attr
->value_tag
== IPP_TAG_BEGIN_COLLECTION
) /* Not yet supported */
2931 if (!strncmp(attr
->name
, "time-", 5))
2934 if (!strncmp(attr
->name
, "job-", 4) &&
2935 strcmp(attr
->name
, "job-billing") &&
2936 strcmp(attr
->name
, "job-impressions") &&
2937 strcmp(attr
->name
, "job-originating-host-name") &&
2938 strcmp(attr
->name
, "job-uuid") &&
2939 !(printer
->type
& CUPS_PRINTER_REMOTE
))
2942 if ((!strcmp(attr
->name
, "job-impressions") ||
2943 !strcmp(attr
->name
, "page-label") ||
2944 !strcmp(attr
->name
, "page-border") ||
2945 !strncmp(attr
->name
, "number-up", 9) ||
2946 !strcmp(attr
->name
, "page-ranges") ||
2947 !strcmp(attr
->name
, "page-set") ||
2948 !strcasecmp(attr
->name
, "AP_FIRSTPAGE_InputSlot") ||
2949 !strcasecmp(attr
->name
, "AP_FIRSTPAGE_ManualFeed") ||
2950 !strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
2951 "PMTotalSidesImaged..n.") ||
2952 !strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
2953 "PMTotalBeginPages..n.")) &&
2958 * Otherwise add them to the list...
2961 if (optptr
> options
)
2962 strlcat(optptr
, " ", optlength
- (optptr
- options
));
2964 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
2966 strlcat(optptr
, attr
->name
, optlength
- (optptr
- options
));
2967 strlcat(optptr
, "=", optlength
- (optptr
- options
));
2970 for (i
= 0; i
< attr
->num_values
; i
++)
2973 strlcat(optptr
, ",", optlength
- (optptr
- options
));
2975 optptr
+= strlen(optptr
);
2977 switch (attr
->value_tag
)
2979 case IPP_TAG_INTEGER
:
2981 snprintf(optptr
, optlength
- (optptr
- options
),
2982 "%d", attr
->values
[i
].integer
);
2985 case IPP_TAG_BOOLEAN
:
2986 if (!attr
->values
[i
].boolean
)
2987 strlcat(optptr
, "no", optlength
- (optptr
- options
));
2989 case IPP_TAG_NOVALUE
:
2990 strlcat(optptr
, attr
->name
,
2991 optlength
- (optptr
- options
));
2994 case IPP_TAG_RANGE
:
2995 if (attr
->values
[i
].range
.lower
== attr
->values
[i
].range
.upper
)
2996 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2997 "%d", attr
->values
[i
].range
.lower
);
2999 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
3000 "%d-%d", attr
->values
[i
].range
.lower
,
3001 attr
->values
[i
].range
.upper
);
3004 case IPP_TAG_RESOLUTION
:
3005 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
3006 "%dx%d%s", attr
->values
[i
].resolution
.xres
,
3007 attr
->values
[i
].resolution
.yres
,
3008 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
3012 case IPP_TAG_STRING
:
3015 case IPP_TAG_KEYWORD
:
3016 case IPP_TAG_CHARSET
:
3017 case IPP_TAG_LANGUAGE
:
3019 for (valptr
= attr
->values
[i
].string
.text
; *valptr
;)
3021 if (strchr(" \t\n\\\'\"", *valptr
))
3023 *optptr
++ = *valptr
++;
3030 break; /* anti-compiler-warning-code */
3034 optptr
+= strlen(optptr
);
3039 * Build the command-line arguments for the filters. Each filter
3040 * has 6 or 7 arguments:
3044 * argv[2] = username
3046 * argv[4] = # copies
3048 * argv[6] = filename (optional; normally stdin)
3050 * This allows legacy printer drivers that use the old System V
3051 * printing interface to be used by CUPS.
3053 * For remote jobs, we send all of the files in the argument list.
3056 if (printer
->remote
&& job
->num_files
> 1)
3057 argv
= calloc(7 + job
->num_files
, sizeof(char *));
3059 argv
= calloc(8, sizeof(char *));
3063 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to allocate argument array!");
3064 cupsArrayDelete(filters
);
3066 FilterLevel
-= job
->cost
;
3068 cupsdStopPrinter(printer
, 0);
3072 sprintf(jobid
, "%d", job
->id
);
3074 argv
[0] = printer
->name
;
3076 argv
[2] = job
->username
;
3081 if (printer
->remote
&& job
->num_files
> 1)
3083 for (i
= 0; i
< job
->num_files
; i
++)
3085 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
3087 argv
[6 + i
] = strdup(filename
);
3092 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
3093 job
->id
, job
->current_file
+ 1);
3097 for (i
= 0; argv
[i
]; i
++)
3098 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
3099 "argv[%d]=\"%s\"", i
, argv
[i
]);
3102 * Create environment variable strings for the filters...
3105 attr
= ippFindAttribute(job
->attrs
, "attributes-natural-language",
3109 strcpy(apple_language
, "APPLE_LANGUAGE=");
3110 _cupsAppleLanguage(attr
->values
[0].string
.text
,
3111 apple_language
+ 15, sizeof(apple_language
) - 15);
3112 #endif /* __APPLE__ */
3114 switch (strlen(attr
->values
[0].string
.text
))
3118 * This is an unknown or badly formatted language code; use
3119 * the POSIX locale...
3122 strcpy(lang
, "LANG=C");
3127 * Just the language code (ll)...
3130 snprintf(lang
, sizeof(lang
), "LANG=%s.UTF8",
3131 attr
->values
[0].string
.text
);
3136 * Language and country code (ll-cc)...
3139 snprintf(lang
, sizeof(lang
), "LANG=%c%c_%c%c.UTF8",
3140 attr
->values
[0].string
.text
[0],
3141 attr
->values
[0].string
.text
[1],
3142 toupper(attr
->values
[0].string
.text
[3] & 255),
3143 toupper(attr
->values
[0].string
.text
[4] & 255));
3147 attr
= ippFindAttribute(job
->attrs
, "document-format",
3150 (optptr
= strstr(attr
->values
[0].string
.text
, "charset=")) != NULL
)
3151 snprintf(charset
, sizeof(charset
), "CHARSET=%s", optptr
+ 8);
3154 attr
= ippFindAttribute(job
->attrs
, "attributes-charset",
3156 snprintf(charset
, sizeof(charset
), "CHARSET=%s",
3157 attr
->values
[0].string
.text
);
3160 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s/%s",
3161 job
->filetypes
[job
->current_file
]->super
,
3162 job
->filetypes
[job
->current_file
]->type
);
3163 snprintf(device_uri
, sizeof(device_uri
), "DEVICE_URI=%s",
3164 printer
->device_uri
);
3165 snprintf(ppd
, sizeof(ppd
), "PPD=%s/ppd/%s.ppd", ServerRoot
, printer
->name
);
3166 snprintf(printer_info
, sizeof(printer_name
), "PRINTER_INFO=%s",
3167 printer
->info
? printer
->info
: "");
3168 snprintf(printer_location
, sizeof(printer_name
), "PRINTER_LOCATION=%s",
3169 printer
->location
? printer
->location
: "");
3170 snprintf(printer_name
, sizeof(printer_name
), "PRINTER=%s", printer
->name
);
3171 snprintf(rip_max_cache
, sizeof(rip_max_cache
), "RIP_MAX_CACHE=%s", RIPCache
);
3173 envc
= cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
3175 envp
[envc
++] = charset
;
3176 envp
[envc
++] = lang
;
3178 envp
[envc
++] = apple_language
;
3179 #endif /* __APPLE__ */
3180 envp
[envc
++] = ppd
;
3181 envp
[envc
++] = rip_max_cache
;
3182 envp
[envc
++] = content_type
;
3183 envp
[envc
++] = device_uri
;
3184 envp
[envc
++] = printer_info
;
3185 envp
[envc
++] = printer_location
;
3186 envp
[envc
++] = printer_name
;
3187 envp
[envc
++] = banner_page
? "CUPS_FILETYPE=job-sheet" :
3188 "CUPS_FILETYPE=document";
3190 if (!printer
->remote
&& !printer
->raw
)
3192 filter
= (mime_filter_t
*)cupsArrayLast(filters
);
3194 if (printer
->port_monitor
)
3195 filter
= (mime_filter_t
*)cupsArrayPrev(filters
);
3197 if (filter
&& filter
->dst
)
3199 snprintf(final_content_type
, sizeof(final_content_type
),
3200 "FINAL_CONTENT_TYPE=%s/%s",
3201 filter
->dst
->super
, filter
->dst
->type
);
3202 envp
[envc
++] = final_content_type
;
3206 if (Classification
&& !banner_page
)
3208 if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
3209 IPP_TAG_NAME
)) == NULL
)
3210 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3212 else if (attr
->num_values
> 1 &&
3213 strcmp(attr
->values
[1].string
.text
, "none") != 0)
3214 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3215 attr
->values
[1].string
.text
);
3217 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3218 attr
->values
[0].string
.text
);
3220 envp
[envc
++] = classification
;
3223 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
3225 snprintf(class_name
, sizeof(class_name
), "CLASS=%s", job
->dest
);
3226 envp
[envc
++] = class_name
;
3229 if (job
->auth_username
)
3230 envp
[envc
++] = job
->auth_username
;
3231 if (job
->auth_domain
)
3232 envp
[envc
++] = job
->auth_domain
;
3233 if (job
->auth_password
)
3234 envp
[envc
++] = job
->auth_password
;
3238 envp
[envc
++] = job
->ccname
;
3239 #endif /* HAVE_GSSAPI */
3243 for (i
= 0; i
< envc
; i
++)
3244 if (!strncmp(envp
[i
], "AUTH_", 5))
3245 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"AUTH_%c****\"", i
,
3247 else if (strncmp(envp
[i
], "DEVICE_URI=", 11))
3248 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"%s\"", i
, envp
[i
]);
3250 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"DEVICE_URI=%s\"", i
,
3251 printer
->sanitized_device_uri
);
3253 if (printer
->remote
)
3254 job
->current_file
= job
->num_files
;
3256 job
->current_file
++;
3259 * Now create processes for all of the filters...
3262 filterfds
[0][0] = -1;
3263 filterfds
[0][1] = -1;
3264 filterfds
[1][0] = -1;
3265 filterfds
[1][1] = -1;
3267 if (!job
->status_buffer
)
3269 if (cupsdOpenPipe(job
->status_pipes
))
3271 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
3272 "Unable to create job status pipes - %s.", strerror(errno
));
3273 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3274 "Unable to create status pipes - %s.", strerror(errno
));
3276 cupsdAddPrinterHistory(printer
);
3278 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3279 "Job canceled because the server could not create the job "
3285 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3286 "start_job: status_pipes = [ %d %d ]",
3287 job
->status_pipes
[0], job
->status_pipes
[1]);
3289 job
->status_buffer
= cupsdStatBufNew(job
->status_pipes
[0], NULL
);
3290 job
->status_level
= CUPSD_LOG_INFO
;
3294 memset(job
->filters
, 0, sizeof(job
->filters
));
3297 job
->profile
= cupsdCreateProfile(job
->id
);
3299 for (i
= 0, slot
= 0, filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
3301 i
++, filter
= (mime_filter_t
*)cupsArrayNext(filters
))
3303 if (filter
->filter
[0] != '/')
3304 snprintf(command
, sizeof(command
), "%s/filter/%s", ServerBin
,
3307 strlcpy(command
, filter
->filter
, sizeof(command
));
3309 if (i
< (cupsArrayCount(filters
) - 1))
3311 if (cupsdOpenPipe(filterfds
[slot
]))
3313 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
3314 "Unable to create job filter pipes - %s.", strerror(errno
));
3315 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3316 "Unable to create filter pipes - %s.", strerror(errno
));
3317 cupsdAddPrinterHistory(printer
);
3319 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3320 "Job canceled because the server could not create the "
3328 if (job
->current_file
== 1)
3330 if (strncmp(printer
->device_uri
, "file:", 5) != 0)
3332 if (cupsdOpenPipe(job
->print_pipes
))
3334 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
3335 "Unable to create job backend pipes - %s.",
3337 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3338 "Unable to create backend pipes - %s.", strerror(errno
));
3339 cupsdAddPrinterHistory(printer
);
3341 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3342 "Job canceled because the server could not create "
3343 "the backend pipes.");
3350 job
->print_pipes
[0] = -1;
3351 if (!strcmp(printer
->device_uri
, "file:/dev/null") ||
3352 !strcmp(printer
->device_uri
, "file:///dev/null"))
3353 job
->print_pipes
[1] = -1;
3356 if (!strncmp(printer
->device_uri
, "file:/dev/", 10))
3357 job
->print_pipes
[1] = open(printer
->device_uri
+ 5,
3359 else if (!strncmp(printer
->device_uri
, "file:///dev/", 12))
3360 job
->print_pipes
[1] = open(printer
->device_uri
+ 7,
3362 else if (!strncmp(printer
->device_uri
, "file:///", 8))
3363 job
->print_pipes
[1] = open(printer
->device_uri
+ 7,
3364 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
3366 job
->print_pipes
[1] = open(printer
->device_uri
+ 5,
3367 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
3369 if (job
->print_pipes
[1] < 0)
3371 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
3372 "Unable to open output file \"%s\" - %s.",
3373 printer
->device_uri
, strerror(errno
));
3374 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3375 "Unable to open output file \"%s\" - %s.",
3376 printer
->device_uri
, strerror(errno
));
3378 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3379 "Job canceled because the server could not open the "
3385 fcntl(job
->print_pipes
[1], F_SETFD
,
3386 fcntl(job
->print_pipes
[1], F_GETFD
) | FD_CLOEXEC
);
3390 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3391 "start_job: print_pipes = [ %d %d ]",
3392 job
->print_pipes
[0], job
->print_pipes
[1]);
3395 filterfds
[slot
][0] = job
->print_pipes
[0];
3396 filterfds
[slot
][1] = job
->print_pipes
[1];
3399 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "start_job: filter=\"%s\"", command
);
3400 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "start_job: filterfds[%d]=[ %d %d ]",
3401 slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3403 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
3404 filterfds
[slot
][1], job
->status_pipes
[1],
3405 job
->back_pipes
[0], job
->side_pipes
[0], 0,
3406 job
->profile
, job
->filters
+ i
);
3408 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3409 "start_job: Closing filter pipes for slot %d [ %d %d ]...",
3410 !slot
, filterfds
[!slot
][0], filterfds
[!slot
][1]);
3412 cupsdClosePipe(filterfds
[!slot
]);
3416 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Unable to start filter \"%s\" - %s.",
3417 filter
->filter
, strerror(errno
));
3418 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3419 "Unable to start filter \"%s\" - %s.",
3420 filter
->filter
, strerror(errno
));
3422 cupsdAddPrinterHistory(printer
);
3424 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3425 "Job canceled because the server could not execute a "
3431 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Started filter %s (PID %d)", command
,
3438 cupsArrayDelete(filters
);
3442 * Finally, pipe the final output into a backend process if needed...
3445 if (strncmp(printer
->device_uri
, "file:", 5) != 0)
3447 if (job
->current_file
== 1 || printer
->remote
)
3449 sscanf(printer
->device_uri
, "%254[^:]", method
);
3450 snprintf(command
, sizeof(command
), "%s/backend/%s", ServerBin
, method
);
3453 * See if the backend needs to run as root...
3458 else if (stat(command
, &backinfo
))
3461 backroot
= !(backinfo
.st_mode
& (S_IRWXG
| S_IRWXO
));
3463 argv
[0] = printer
->sanitized_device_uri
;
3465 filterfds
[slot
][0] = -1;
3466 filterfds
[slot
][1] = -1;
3468 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "start_job: backend=\"%s\"", command
);
3469 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "start_job: filterfds[%d] = [ %d %d ]",
3470 slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3472 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
3473 filterfds
[slot
][1], job
->status_pipes
[1],
3474 job
->back_pipes
[1], job
->side_pipes
[1],
3475 backroot
, job
->profile
, &(job
->backend
));
3479 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Unable to start backend \"%s\" - %s.",
3480 method
, strerror(errno
));
3481 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3482 "Unable to start backend \"%s\" - %s.", method
,
3485 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3486 "Job canceled because the server could not execute "
3493 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Started backend %s (PID %d)",
3498 if (job
->current_file
== job
->num_files
)
3500 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3501 "start_job: Closing print pipes [ %d %d ]...",
3502 job
->print_pipes
[0], job
->print_pipes
[1]);
3504 cupsdClosePipe(job
->print_pipes
);
3506 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3507 "start_job: Closing back pipes [ %d %d ]...",
3508 job
->back_pipes
[0], job
->back_pipes
[1]);
3510 cupsdClosePipe(job
->back_pipes
);
3512 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3513 "start_job: Closing side pipes [ %d %d ]...",
3514 job
->side_pipes
[0], job
->side_pipes
[1]);
3516 cupsdClosePipe(job
->side_pipes
);
3518 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3519 "start_job: Closing status output pipe %d...",
3520 job
->status_pipes
[1]);
3522 close(job
->status_pipes
[1]);
3523 job
->status_pipes
[1] = -1;
3528 filterfds
[slot
][0] = -1;
3529 filterfds
[slot
][1] = -1;
3531 if (job
->current_file
== job
->num_files
)
3533 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3534 "start_job: Closing print pipes [ %d %d ]...",
3535 job
->print_pipes
[0], job
->print_pipes
[1]);
3537 cupsdClosePipe(job
->print_pipes
);
3539 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3540 "start_job: Closing status output pipe %d...",
3541 job
->status_pipes
[1]);
3543 close(job
->status_pipes
[1]);
3544 job
->status_pipes
[1] = -1;
3548 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3549 "start_job: Closing filter pipes for slot %d [ %d %d ]...",
3550 slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3551 cupsdClosePipe(filterfds
[slot
]);
3553 if (printer
->remote
&& job
->num_files
> 1)
3555 for (i
= 0; i
< job
->num_files
; i
++)
3561 cupsdAddSelect(job
->status_buffer
->fd
, (cupsd_selfunc_t
)update_job
, NULL
,
3564 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
, "Job #%d started.",
3571 * If we get here, we need to abort the current job and close out all
3572 * files and pipes...
3577 for (slot
= 0; slot
< 2; slot
++)
3579 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3580 "start_job: Closing filter pipes for slot %d [ %d %d ]...",
3581 slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3582 cupsdClosePipe(filterfds
[slot
]);
3585 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3586 "start_job: Closing status pipes [ %d %d ]...",
3587 job
->status_pipes
[0], job
->status_pipes
[1]);
3588 cupsdClosePipe(job
->status_pipes
);
3589 cupsdStatBufDelete(job
->status_buffer
);
3591 job
->status_buffer
= NULL
;
3593 cupsArrayDelete(filters
);
3595 if (printer
->remote
&& job
->num_files
> 1)
3597 for (i
= 0; i
< job
->num_files
; i
++)
3603 cupsdStopJob(job
, 0);
3608 * 'unload_job()' - Unload a job from memory.
3612 unload_job(cupsd_job_t
*job
) /* I - Job */
3617 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Unloading...", job
->id
);
3619 ippDelete(job
->attrs
);
3624 job
->job_sheets
= NULL
;
3625 job
->printer_message
= NULL
;
3626 job
->printer_reasons
= NULL
;
3631 * 'update_job()' - Read a status update from a job's filters.
3635 update_job(cupsd_job_t
*job
) /* I - Job to check */
3637 int i
; /* Looping var */
3638 int copies
; /* Number of copies printed */
3639 char message
[1024], /* Message text */
3640 *ptr
; /* Pointer update... */
3641 int loglevel
, /* Log level for message */
3642 event
= 0; /* Events? */
3645 while ((ptr
= cupsdStatBufUpdate(job
->status_buffer
, &loglevel
,
3646 message
, sizeof(message
))) != NULL
)
3649 * Process page and printer state messages as needed...
3652 if (loglevel
== CUPSD_LOG_PAGE
)
3655 * Page message; send the message to the page_log file and update the
3656 * job sheet count...
3659 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "PAGE: %s", message
);
3663 if (!strncasecmp(message
, "total ", 6))
3666 * Got a total count of pages from a backend or filter...
3669 copies
= atoi(message
+ 6);
3670 copies
-= job
->sheets
->values
[0].integer
; /* Just track the delta */
3672 else if (!sscanf(message
, "%*d%d", &copies
))
3675 job
->sheets
->values
[0].integer
+= copies
;
3677 if (job
->printer
->page_limit
)
3679 cupsd_quota_t
*q
= cupsdUpdateQuota(job
->printer
, job
->username
,
3683 if (AppleQuotas
&& q
->page_count
== -3)
3686 * Quota limit exceeded, cancel job in progress immediately...
3689 cupsdLogJob(job
, CUPSD_LOG_INFO
,
3690 "Canceled because pages exceed user %s "
3691 "quota limit on printer %s (%s).",
3692 job
->username
, job
->printer
->name
,
3693 job
->printer
->info
);
3695 cupsdCancelJob(job
, 1, IPP_JOB_CANCELED
);
3700 #endif /* __APPLE__ */
3704 cupsdLogPage(job
, message
);
3707 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
3708 "Printed %d page(s).", job
->sheets
->values
[0].integer
);
3710 else if (loglevel
== CUPSD_LOG_STATE
)
3712 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "STATE: %s", message
);
3714 if (!strcmp(message
, "paused"))
3716 cupsdStopPrinter(job
->printer
, 1);
3721 cupsdSetPrinterReasons(job
->printer
, message
);
3722 cupsdAddPrinterHistory(job
->printer
);
3723 event
|= CUPSD_EVENT_PRINTER_STATE
;
3726 update_job_attrs(job
, 0);
3728 else if (loglevel
== CUPSD_LOG_ATTR
)
3731 * Set attribute(s)...
3734 int num_attrs
; /* Number of attributes */
3735 cups_option_t
*attrs
; /* Attributes */
3736 const char *attr
; /* Attribute */
3739 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "ATTR: %s", message
);
3741 num_attrs
= cupsParseOptions(message
, 0, &attrs
);
3743 if ((attr
= cupsGetOption("auth-info-required", num_attrs
,
3746 cupsdSetAuthInfoRequired(job
->printer
, attr
, NULL
);
3747 cupsdSetPrinterAttrs(job
->printer
);
3749 if (job
->printer
->type
& CUPS_PRINTER_DISCOVERED
)
3750 cupsdMarkDirty(CUPSD_DIRTY_REMOTE
);
3752 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3755 if ((attr
= cupsGetOption("job-media-progress", num_attrs
,
3758 int progress
= atoi(attr
);
3761 if (progress
>= 0 && progress
<= 100)
3763 job
->progress
= progress
;
3766 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
3767 "Printing page %d, %d%%",
3768 job
->sheets
->values
[0].integer
, job
->progress
);
3772 if ((attr
= cupsGetOption("printer-alert", num_attrs
, attrs
)) != NULL
)
3774 cupsdSetString(&job
->printer
->alert
, attr
);
3775 event
|= CUPSD_EVENT_PRINTER_STATE
;
3778 if ((attr
= cupsGetOption("printer-alert-description", num_attrs
,
3781 cupsdSetString(&job
->printer
->alert_description
, attr
);
3782 event
|= CUPSD_EVENT_PRINTER_STATE
;
3785 if ((attr
= cupsGetOption("marker-colors", num_attrs
, attrs
)) != NULL
)
3787 cupsdSetPrinterAttr(job
->printer
, "marker-colors", (char *)attr
);
3788 job
->printer
->marker_time
= time(NULL
);
3789 event
|= CUPSD_EVENT_PRINTER_STATE
;
3790 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3793 if ((attr
= cupsGetOption("marker-levels", num_attrs
, attrs
)) != NULL
)
3795 cupsdSetPrinterAttr(job
->printer
, "marker-levels", (char *)attr
);
3796 job
->printer
->marker_time
= time(NULL
);
3797 event
|= CUPSD_EVENT_PRINTER_STATE
;
3798 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3801 if ((attr
= cupsGetOption("marker-low-levels", num_attrs
, attrs
)) != NULL
)
3803 cupsdSetPrinterAttr(job
->printer
, "marker-low-levels", (char *)attr
);
3804 job
->printer
->marker_time
= time(NULL
);
3805 event
|= CUPSD_EVENT_PRINTER_STATE
;
3806 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3809 if ((attr
= cupsGetOption("marker-high-levels", num_attrs
, attrs
)) != NULL
)
3811 cupsdSetPrinterAttr(job
->printer
, "marker-high-levels", (char *)attr
);
3812 job
->printer
->marker_time
= time(NULL
);
3813 event
|= CUPSD_EVENT_PRINTER_STATE
;
3814 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3817 if ((attr
= cupsGetOption("marker-message", num_attrs
, attrs
)) != NULL
)
3819 cupsdSetPrinterAttr(job
->printer
, "marker-message", (char *)attr
);
3820 job
->printer
->marker_time
= time(NULL
);
3821 event
|= CUPSD_EVENT_PRINTER_STATE
;
3822 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3825 if ((attr
= cupsGetOption("marker-names", num_attrs
, attrs
)) != NULL
)
3827 cupsdSetPrinterAttr(job
->printer
, "marker-names", (char *)attr
);
3828 job
->printer
->marker_time
= time(NULL
);
3829 event
|= CUPSD_EVENT_PRINTER_STATE
;
3830 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3833 if ((attr
= cupsGetOption("marker-types", num_attrs
, attrs
)) != NULL
)
3835 cupsdSetPrinterAttr(job
->printer
, "marker-types", (char *)attr
);
3836 job
->printer
->marker_time
= time(NULL
);
3837 event
|= CUPSD_EVENT_PRINTER_STATE
;
3838 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3841 cupsFreeOptions(num_attrs
, attrs
);
3843 else if (loglevel
== CUPSD_LOG_PPD
)
3846 * Set attribute(s)...
3849 int num_keywords
; /* Number of keywords */
3850 cups_option_t
*keywords
; /* Keywords */
3853 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "PPD: %s", message
);
3855 num_keywords
= cupsParseOptions(message
, 0, &keywords
);
3857 if (cupsdUpdatePrinterPPD(job
->printer
, num_keywords
, keywords
))
3858 cupsdSetPrinterAttrs(job
->printer
);
3860 cupsFreeOptions(num_keywords
, keywords
);
3863 else if (!strncmp(message
, "recoverable:", 12))
3865 cupsdSetPrinterReasons(job
->printer
,
3866 "+com.apple.print.recoverable-warning");
3869 while (isspace(*ptr
& 255))
3872 cupsdSetString(&job
->printer
->recoverable
, ptr
);
3873 cupsdAddPrinterHistory(job
->printer
);
3874 event
|= CUPSD_EVENT_PRINTER_STATE
;
3876 else if (!strncmp(message
, "recovered:", 10))
3878 cupsdSetPrinterReasons(job
->printer
,
3879 "-com.apple.print.recoverable-warning");
3882 while (isspace(*ptr
& 255))
3885 cupsdSetString(&job
->printer
->recoverable
, ptr
);
3886 cupsdAddPrinterHistory(job
->printer
);
3887 event
|= CUPSD_EVENT_PRINTER_STATE
;
3889 #endif /* __APPLE__ */
3892 cupsdLogJob(job
, loglevel
, "%s", message
);
3894 if (loglevel
< CUPSD_LOG_DEBUG
)
3896 strlcpy(job
->printer
->state_message
, message
,
3897 sizeof(job
->printer
->state_message
));
3898 cupsdAddPrinterHistory(job
->printer
);
3900 event
|= CUPSD_EVENT_PRINTER_STATE
;
3902 if (loglevel
<= job
->status_level
)
3905 * Some messages show in the printer-state-message attribute...
3908 if (loglevel
!= CUPSD_LOG_NOTICE
)
3909 job
->status_level
= loglevel
;
3911 update_job_attrs(job
, 1);
3916 if (!strchr(job
->status_buffer
->buffer
, '\n'))
3920 if (event
& CUPSD_EVENT_PRINTER_STATE
)
3921 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, job
->printer
, NULL
,
3922 (job
->printer
->type
& CUPS_PRINTER_CLASS
) ?
3923 "Class \"%s\" state changed." :
3924 "Printer \"%s\" state changed.",
3925 job
->printer
->name
);
3927 if (ptr
== NULL
&& !job
->status_buffer
->bufused
)
3930 * See if all of the filters and the backend have returned their
3934 for (i
= 0; job
->filters
[i
] < 0; i
++);
3936 if (job
->filters
[i
])
3939 if (job
->current_file
>= job
->num_files
&& job
->backend
> 0)
3943 * Handle the end of job stuff...
3946 cupsdFinishJob(job
);
3952 * 'update_job_attrs()' - Update the job-printer-* attributes.
3956 update_job_attrs(cupsd_job_t
*job
, /* I - Job to update */
3957 int do_message
)/* I - 1 = update job-printer-state message */
3959 int i
; /* Looping var */
3960 int num_reasons
; /* Actual number of reasons */
3961 const char * const *reasons
; /* Reasons */
3962 static const char *none
= "none", /* "none" */
3968 * Get/create the job-printer-state-* attributes...
3971 if (!job
->printer_message
)
3973 if ((job
->printer_message
= ippFindAttribute(job
->attrs
,
3974 "job-printer-state-message",
3975 IPP_TAG_TEXT
)) == NULL
)
3976 job
->printer_message
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_TEXT
,
3977 "job-printer-state-message",
3981 if (!job
->printer_reasons
)
3982 job
->printer_reasons
= ippFindAttribute(job
->attrs
,
3983 "job-printer-state-reasons",
3987 * If the job isn't printing, return now...
3994 * Otherwise copy the printer-state-message value...
3997 if (job
->printer
->state_message
[0] &&
3998 (do_message
|| !job
->printer_message
->values
[0].string
.text
[0]))
3999 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
),
4000 job
->printer
->state_message
);
4003 * ... and the printer-state-reasons value...
4006 if (job
->printer
->num_reasons
== 0)
4009 reasons
= job
->printer
->state
== IPP_PRINTER_STOPPED
? &paused
: &none
;
4013 num_reasons
= job
->printer
->num_reasons
;
4014 reasons
= (const char * const *)job
->printer
->reasons
;
4017 if (!job
->printer_reasons
|| job
->printer_reasons
->num_values
!= num_reasons
)
4019 ippDeleteAttribute(job
->attrs
, job
->printer_reasons
);
4021 job
->printer_reasons
= ippAddStrings(job
->attrs
,
4022 IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
4023 "job-printer-state-reasons",
4024 num_reasons
, NULL
, NULL
);
4027 for (i
= 0; i
< num_reasons
; i
++)
4028 cupsdSetString(&(job
->printer_reasons
->values
[i
].string
.text
), reasons
[i
]);
4033 * End of "$Id: job.c 7902 2008-09-03 14:20:17Z mike $".