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 ((job
->state
= ippFindAttribute(job
->attrs
, "job-state",
1116 IPP_TAG_ENUM
)) == NULL
)
1118 cupsdLogMessage(CUPSD_LOG_ERROR
,
1119 "[Job %d] Missing or bad job-state attribute in control "
1121 ippDelete(job
->attrs
);
1127 job
->state_value
= (ipp_jstate_t
)job
->state
->values
[0].integer
;
1131 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1132 IPP_TAG_URI
)) == NULL
)
1134 cupsdLogMessage(CUPSD_LOG_ERROR
,
1135 "[Job %d] No job-printer-uri attribute in control file!",
1137 ippDelete(job
->attrs
);
1143 if ((dest
= cupsdValidateDest(attr
->values
[0].string
.text
, &(job
->dtype
),
1146 cupsdLogMessage(CUPSD_LOG_ERROR
,
1147 "[Job %d] Unable to queue job for destination \"%s\"!",
1148 job
->id
, attr
->values
[0].string
.text
);
1149 ippDelete(job
->attrs
);
1155 cupsdSetString(&job
->dest
, dest
);
1157 else if ((destptr
= cupsdFindDest(job
->dest
)) == NULL
)
1159 cupsdLogMessage(CUPSD_LOG_ERROR
,
1160 "[Job %d] Unable to queue job for destination \"%s\"!",
1161 job
->id
, job
->dest
);
1162 ippDelete(job
->attrs
);
1168 job
->sheets
= ippFindAttribute(job
->attrs
, "job-media-sheets-completed",
1170 job
->job_sheets
= ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_NAME
);
1174 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
1175 IPP_TAG_INTEGER
)) == NULL
)
1177 cupsdLogMessage(CUPSD_LOG_ERROR
,
1178 "[Job %d] Missing or bad job-priority attribute in "
1179 "control file!", job
->id
);
1180 ippDelete(job
->attrs
);
1186 job
->priority
= attr
->values
[0].integer
;
1191 if ((attr
= ippFindAttribute(job
->attrs
, "job-originating-user-name",
1192 IPP_TAG_NAME
)) == NULL
)
1194 cupsdLogMessage(CUPSD_LOG_ERROR
,
1195 "[Job %d] Missing or bad job-originating-user-name "
1196 "attribute in control file!", job
->id
);
1197 ippDelete(job
->attrs
);
1203 cupsdSetString(&job
->username
, attr
->values
[0].string
.text
);
1207 * Set the job hold-until time and state...
1210 if (job
->state_value
== IPP_JOB_HELD
)
1212 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1213 IPP_TAG_KEYWORD
)) == NULL
)
1214 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1217 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
1220 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1221 job
->state_value
= IPP_JOB_PENDING
;
1224 else if (job
->state_value
== IPP_JOB_PROCESSING
)
1226 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1227 job
->state_value
= IPP_JOB_PENDING
;
1230 if (!job
->num_files
)
1233 * Find all the d##### files...
1236 for (fileid
= 1; fileid
< 10000; fileid
++)
1238 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
1241 if (access(jobfile
, 0))
1244 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1245 "[Job %d] Auto-typing document file \"%s\"...", job
->id
,
1248 if (fileid
> job
->num_files
)
1250 if (job
->num_files
== 0)
1252 compressions
= (int *)calloc(fileid
, sizeof(int));
1253 filetypes
= (mime_type_t
**)calloc(fileid
, sizeof(mime_type_t
*));
1257 compressions
= (int *)realloc(job
->compressions
,
1258 sizeof(int) * fileid
);
1259 filetypes
= (mime_type_t
**)realloc(job
->filetypes
,
1260 sizeof(mime_type_t
*) *
1264 if (!compressions
|| !filetypes
)
1266 cupsdLogMessage(CUPSD_LOG_ERROR
,
1267 "[Job %d] Ran out of memory for job file types!",
1272 job
->compressions
= compressions
;
1273 job
->filetypes
= filetypes
;
1274 job
->num_files
= fileid
;
1277 job
->filetypes
[fileid
- 1] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
1278 job
->compressions
+ fileid
- 1);
1280 if (!job
->filetypes
[fileid
- 1])
1281 job
->filetypes
[fileid
- 1] = mimeType(MimeDatabase
, "application",
1287 * Load authentication information as needed...
1290 if (job
->state_value
< IPP_JOB_STOPPED
)
1292 snprintf(jobfile
, sizeof(jobfile
), "%s/a%05d", RequestRoot
, job
->id
);
1294 cupsdClearString(&job
->auth_username
);
1295 cupsdClearString(&job
->auth_domain
);
1296 cupsdClearString(&job
->auth_password
);
1298 if ((fp
= cupsFileOpen(jobfile
, "r")) != NULL
)
1300 int i
, /* Looping var */
1301 bytes
; /* Size of auth data */
1302 char line
[255], /* Line from file */
1303 data
[255]; /* Decoded data */
1307 i
< destptr
->num_auth_info_required
&&
1308 cupsFileGets(fp
, line
, sizeof(line
));
1311 bytes
= sizeof(data
);
1312 httpDecode64_2(data
, &bytes
, line
);
1314 if (!strcmp(destptr
->auth_info_required
[i
], "username"))
1315 cupsdSetStringf(&job
->auth_username
, "AUTH_USERNAME=%s", data
);
1316 else if (!strcmp(destptr
->auth_info_required
[i
], "domain"))
1317 cupsdSetStringf(&job
->auth_domain
, "AUTH_DOMAIN=%s", data
);
1318 else if (!strcmp(destptr
->auth_info_required
[i
], "password"))
1319 cupsdSetStringf(&job
->auth_password
, "AUTH_PASSWORD=%s", data
);
1326 job
->access_time
= time(NULL
);
1331 * 'cupsdMoveJob()' - Move the specified job to a different destination.
1335 cupsdMoveJob(cupsd_job_t
*job
, /* I - Job */
1336 cupsd_printer_t
*p
) /* I - Destination printer or class */
1338 ipp_attribute_t
*attr
; /* job-printer-uri attribute */
1339 const char *olddest
; /* Old destination */
1340 cupsd_printer_t
*oldp
; /* Old pointer */
1344 * Don't move completed jobs...
1347 if (job
->state_value
> IPP_JOB_STOPPED
)
1351 * Get the old destination...
1354 olddest
= job
->dest
;
1357 oldp
= job
->printer
;
1359 oldp
= cupsdFindDest(olddest
);
1362 * Change the destination information...
1365 if (job
->state_value
== IPP_JOB_PROCESSING
)
1366 cupsdStopJob(job
, 0);
1370 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, oldp
, job
,
1371 "Job #%d moved from %s to %s.", job
->id
, olddest
,
1374 cupsdSetString(&job
->dest
, p
->name
);
1375 job
->dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_REMOTE
|
1376 CUPS_PRINTER_IMPLICIT
);
1378 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1379 IPP_TAG_URI
)) != NULL
)
1380 cupsdSetString(&(attr
->values
[0].string
.text
), p
->uri
);
1382 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, p
, job
,
1383 "Job #%d moved from %s to %s.", job
->id
, olddest
,
1387 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1392 * 'cupsdReleaseJob()' - Release the specified job.
1396 cupsdReleaseJob(cupsd_job_t
*job
) /* I - Job */
1398 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdReleaseJob: id = %d", job
->id
);
1400 if (job
->state_value
== IPP_JOB_HELD
)
1403 * Add trailing banner as needed...
1406 if (job
->pending_timeout
)
1407 cupsdTimeoutJob(job
);
1409 DEBUG_puts("cupsdReleaseJob: setting state to pending...");
1411 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1412 job
->state_value
= IPP_JOB_PENDING
;
1414 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1420 * 'cupsdRestartJob()' - Restart the specified job.
1424 cupsdRestartJob(cupsd_job_t
*job
) /* I - Job */
1426 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdRestartJob: id = %d", job
->id
);
1428 if (job
->state_value
== IPP_JOB_STOPPED
|| job
->num_files
)
1430 ipp_jstate_t old_state
; /* Old job state */
1435 old_state
= job
->state_value
;
1438 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1439 job
->state_value
= IPP_JOB_PENDING
;
1442 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1444 if (old_state
> IPP_JOB_STOPPED
)
1445 cupsArrayAdd(ActiveJobs
, job
);
1451 * 'cupsdSaveAllJobs()' - Save a summary of all jobs to disk.
1455 cupsdSaveAllJobs(void)
1457 int i
; /* Looping var */
1458 cups_file_t
*fp
; /* Job cache file */
1459 char temp
[1024]; /* Temporary string */
1460 cupsd_job_t
*job
; /* Current job */
1461 time_t curtime
; /* Current time */
1462 struct tm
*curdate
; /* Current date */
1465 snprintf(temp
, sizeof(temp
), "%s/job.cache", CacheDir
);
1466 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
1468 cupsdLogMessage(CUPSD_LOG_ERROR
,
1469 "Unable to create job cache file \"%s\" - %s",
1470 temp
, strerror(errno
));
1474 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving job cache file \"%s\"...", temp
);
1477 * Restrict access to the file...
1480 fchown(cupsFileNumber(fp
), getuid(), Group
);
1481 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
1484 * Write a small header to the file...
1487 curtime
= time(NULL
);
1488 curdate
= localtime(&curtime
);
1489 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
1491 cupsFilePuts(fp
, "# Job cache file for " CUPS_SVERSION
"\n");
1492 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
1493 cupsFilePrintf(fp
, "NextJobId %d\n", NextJobId
);
1496 * Write each job known to the system...
1499 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1501 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1503 cupsFilePrintf(fp
, "<Job %d>\n", job
->id
);
1504 cupsFilePrintf(fp
, "State %d\n", job
->state_value
);
1505 cupsFilePrintf(fp
, "Priority %d\n", job
->priority
);
1506 cupsFilePrintf(fp
, "Username %s\n", job
->username
);
1507 cupsFilePrintf(fp
, "Destination %s\n", job
->dest
);
1508 cupsFilePrintf(fp
, "DestType %d\n", job
->dtype
);
1509 cupsFilePrintf(fp
, "NumFiles %d\n", job
->num_files
);
1510 for (i
= 0; i
< job
->num_files
; i
++)
1511 cupsFilePrintf(fp
, "File %d %s/%s %d\n", i
+ 1, job
->filetypes
[i
]->super
,
1512 job
->filetypes
[i
]->type
, job
->compressions
[i
]);
1513 cupsFilePuts(fp
, "</Job>\n");
1521 * 'cupsdSaveJob()' - Save a job to disk.
1525 cupsdSaveJob(cupsd_job_t
*job
) /* I - Job */
1527 char filename
[1024]; /* Job control filename */
1528 cups_file_t
*fp
; /* Job file */
1531 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSaveJob(job=%p(%d)): job->attrs=%p",
1532 job
, job
->id
, job
->attrs
);
1534 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
, job
->id
);
1536 if ((fp
= cupsFileOpen(filename
, "w")) == NULL
)
1538 cupsdLogMessage(CUPSD_LOG_ERROR
,
1539 "[Job %d] Unable to create job control file \"%s\" - %s.",
1540 job
->id
, filename
, strerror(errno
));
1544 fchmod(cupsFileNumber(fp
), 0600);
1545 fchown(cupsFileNumber(fp
), RunUser
, Group
);
1547 job
->attrs
->state
= IPP_IDLE
;
1549 if (ippWriteIO(fp
, (ipp_iocb_t
)cupsFileWrite
, 1, NULL
,
1550 job
->attrs
) != IPP_DATA
)
1551 cupsdLogMessage(CUPSD_LOG_ERROR
,
1552 "[Job %d] Unable to write job control file!", job
->id
);
1561 * 'cupsdSetJobHoldUntil()' - Set the hold time for a job...
1565 cupsdSetJobHoldUntil(cupsd_job_t
*job
, /* I - Job */
1566 const char *when
) /* I - When to resume */
1568 time_t curtime
; /* Current time */
1569 struct tm
*curdate
; /* Current date */
1570 int hour
; /* Hold hour */
1571 int minute
; /* Hold minute */
1572 int second
; /* Hold second */
1575 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetJobHoldUntil(%d, \"%s\")",
1580 if (!strcmp(when
, "indefinite") || !strcmp(when
, "auth-info-required"))
1583 * Hold indefinitely...
1586 job
->hold_until
= 0;
1588 else if (!strcmp(when
, "day-time"))
1591 * Hold to 6am the next morning unless local time is < 6pm.
1594 curtime
= time(NULL
);
1595 curdate
= localtime(&curtime
);
1597 if (curdate
->tm_hour
< 18)
1598 job
->hold_until
= curtime
;
1600 job
->hold_until
= curtime
+
1601 ((29 - curdate
->tm_hour
) * 60 + 59 -
1602 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1604 else if (!strcmp(when
, "evening") || !strcmp(when
, "night"))
1607 * Hold to 6pm unless local time is > 6pm or < 6am.
1610 curtime
= time(NULL
);
1611 curdate
= localtime(&curtime
);
1613 if (curdate
->tm_hour
< 6 || curdate
->tm_hour
>= 18)
1614 job
->hold_until
= curtime
;
1616 job
->hold_until
= curtime
+
1617 ((17 - curdate
->tm_hour
) * 60 + 59 -
1618 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1620 else if (!strcmp(when
, "second-shift"))
1623 * Hold to 4pm unless local time is > 4pm.
1626 curtime
= time(NULL
);
1627 curdate
= localtime(&curtime
);
1629 if (curdate
->tm_hour
>= 16)
1630 job
->hold_until
= curtime
;
1632 job
->hold_until
= curtime
+
1633 ((15 - curdate
->tm_hour
) * 60 + 59 -
1634 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1636 else if (!strcmp(when
, "third-shift"))
1639 * Hold to 12am unless local time is < 8am.
1642 curtime
= time(NULL
);
1643 curdate
= localtime(&curtime
);
1645 if (curdate
->tm_hour
< 8)
1646 job
->hold_until
= curtime
;
1648 job
->hold_until
= curtime
+
1649 ((23 - curdate
->tm_hour
) * 60 + 59 -
1650 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1652 else if (!strcmp(when
, "weekend"))
1655 * Hold to weekend unless we are in the weekend.
1658 curtime
= time(NULL
);
1659 curdate
= localtime(&curtime
);
1661 if (curdate
->tm_wday
== 0 || curdate
->tm_wday
== 6)
1662 job
->hold_until
= curtime
;
1664 job
->hold_until
= curtime
+
1665 (((5 - curdate
->tm_wday
) * 24 +
1666 (17 - curdate
->tm_hour
)) * 60 + 59 -
1667 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1669 else if (sscanf(when
, "%d:%d:%d", &hour
, &minute
, &second
) >= 2)
1672 * Hold to specified GMT time (HH:MM or HH:MM:SS)...
1675 curtime
= time(NULL
);
1676 curdate
= gmtime(&curtime
);
1678 job
->hold_until
= curtime
+
1679 ((hour
- curdate
->tm_hour
) * 60 + minute
-
1680 curdate
->tm_min
) * 60 + second
- curdate
->tm_sec
;
1683 * Hold until next day as needed...
1686 if (job
->hold_until
< curtime
)
1687 job
->hold_until
+= 24 * 60 * 60;
1690 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetJobHoldUntil: hold_until = %d",
1691 (int)job
->hold_until
);
1696 * 'cupsdSetJobPriority()' - Set the priority of a job, moving it up/down in
1697 * the list as needed.
1701 cupsdSetJobPriority(
1702 cupsd_job_t
*job
, /* I - Job ID */
1703 int priority
) /* I - New priority (0 to 100) */
1705 ipp_attribute_t
*attr
; /* Job attribute */
1709 * Don't change completed jobs...
1712 if (job
->state_value
>= IPP_JOB_PROCESSING
)
1716 * Set the new priority and re-add the job into the active list...
1719 cupsArrayRemove(ActiveJobs
, job
);
1721 job
->priority
= priority
;
1723 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
1724 IPP_TAG_INTEGER
)) != NULL
)
1725 attr
->values
[0].integer
= priority
;
1727 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-priority",
1730 cupsArrayAdd(ActiveJobs
, job
);
1733 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1738 * 'cupsdStopAllJobs()' - Stop all print jobs.
1742 cupsdStopAllJobs(int force
) /* I - 1 = Force all filters to stop */
1744 cupsd_job_t
*job
; /* Current job */
1747 DEBUG_puts("cupsdStopAllJobs()");
1749 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
1751 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
1752 if (job
->state_value
== IPP_JOB_PROCESSING
)
1754 cupsdStopJob(job
, force
);
1755 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1756 job
->state_value
= IPP_JOB_PENDING
;
1762 * 'cupsdStopJob()' - Stop a print job.
1766 cupsdStopJob(cupsd_job_t
*job
, /* I - Job */
1767 int force
) /* I - 1 = Force all filters to stop */
1769 int i
; /* Looping var */
1772 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] cupsdStopJob: force = %d",
1775 if (job
->state_value
!= IPP_JOB_PROCESSING
)
1778 FilterLevel
-= job
->cost
;
1780 if (job
->printer
->state
== IPP_PRINTER_PROCESSING
)
1781 cupsdSetPrinterState(job
->printer
, IPP_PRINTER_IDLE
, 0);
1783 job
->state
->values
[0].integer
= IPP_JOB_STOPPED
;
1784 job
->state_value
= IPP_JOB_STOPPED
;
1785 job
->printer
->job
= NULL
;
1786 job
->printer
= NULL
;
1788 job
->current_file
--;
1790 for (i
= 0; job
->filters
[i
]; i
++)
1791 if (job
->filters
[i
] > 0)
1793 cupsdEndProcess(job
->filters
[i
], force
);
1794 job
->filters
[i
] = 0;
1797 if (job
->backend
> 0)
1799 cupsdEndProcess(job
->backend
, force
);
1803 cupsdDestroyProfile(job
->profile
);
1804 job
->profile
= NULL
;
1806 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] Closing print pipes [ %d %d ]...",
1807 job
->id
, job
->print_pipes
[0], job
->print_pipes
[1]);
1809 cupsdClosePipe(job
->print_pipes
);
1811 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] Closing back pipes [ %d %d ]...",
1812 job
->id
, job
->back_pipes
[0], job
->back_pipes
[1]);
1814 cupsdClosePipe(job
->back_pipes
);
1816 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] Closing side pipes [ %d %d ]...",
1817 job
->id
, job
->side_pipes
[0], job
->side_pipes
[1]);
1819 cupsdClosePipe(job
->side_pipes
);
1821 if (job
->status_buffer
)
1824 * Close the pipe and clear the input bit.
1827 cupsdRemoveSelect(job
->status_buffer
->fd
);
1829 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1830 "[Job %d] Closing status pipes [ %d %d ]...",
1831 job
->id
, job
->status_pipes
[0], job
->status_pipes
[1]);
1833 cupsdClosePipe(job
->status_pipes
);
1834 cupsdStatBufDelete(job
->status_buffer
);
1836 job
->status_buffer
= NULL
;
1842 * 'cupsdUnloadCompletedJobs()' - Flush completed job history from memory.
1846 cupsdUnloadCompletedJobs(void)
1848 cupsd_job_t
*job
; /* Current job */
1849 time_t expire
; /* Expiration time */
1852 expire
= time(NULL
) - 60;
1854 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1856 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1857 if (job
->attrs
&& job
->state_value
>= IPP_JOB_STOPPED
&&
1858 job
->access_time
< expire
)
1869 * 'compare_active_jobs()' - Compare the job IDs and priorities of two jobs.
1872 static int /* O - Difference */
1873 compare_active_jobs(void *first
, /* I - First job */
1874 void *second
, /* I - Second job */
1875 void *data
) /* I - App data (not used) */
1877 int diff
; /* Difference */
1880 if ((diff
= ((cupsd_job_t
*)second
)->priority
-
1881 ((cupsd_job_t
*)first
)->priority
) != 0)
1884 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
1889 * 'compare_jobs()' - Compare the job IDs of two jobs.
1892 static int /* O - Difference */
1893 compare_jobs(void *first
, /* I - First job */
1894 void *second
, /* I - Second job */
1895 void *data
) /* I - App data (not used) */
1897 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
1902 * 'ipp_length()' - Compute the size of the buffer needed to hold
1903 * the textual IPP attributes.
1906 static int /* O - Size of attribute buffer */
1907 ipp_length(ipp_t
*ipp
) /* I - IPP request */
1909 int bytes
; /* Number of bytes */
1910 int i
; /* Looping var */
1911 ipp_attribute_t
*attr
; /* Current attribute */
1915 * Loop through all attributes...
1920 for (attr
= ipp
->attrs
; attr
!= NULL
; attr
= attr
->next
)
1923 * Skip attributes that won't be sent to filters...
1926 if (attr
->value_tag
== IPP_TAG_MIMETYPE
||
1927 attr
->value_tag
== IPP_TAG_NAMELANG
||
1928 attr
->value_tag
== IPP_TAG_TEXTLANG
||
1929 attr
->value_tag
== IPP_TAG_URI
||
1930 attr
->value_tag
== IPP_TAG_URISCHEME
)
1933 if (strncmp(attr
->name
, "time-", 5) == 0)
1937 * Add space for a leading space and commas between each value.
1938 * For the first attribute, the leading space isn't used, so the
1939 * extra byte can be used as the nul terminator...
1942 bytes
++; /* " " separator */
1943 bytes
+= attr
->num_values
; /* "," separators */
1946 * Boolean attributes appear as "foo,nofoo,foo,nofoo", while
1947 * other attributes appear as "foo=value1,value2,...,valueN".
1950 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
1951 bytes
+= strlen(attr
->name
);
1953 bytes
+= attr
->num_values
* strlen(attr
->name
);
1956 * Now add the size required for each value in the attribute...
1959 switch (attr
->value_tag
)
1961 case IPP_TAG_INTEGER
:
1964 * Minimum value of a signed integer is -2147483647, or 11 digits.
1967 bytes
+= attr
->num_values
* 11;
1970 case IPP_TAG_BOOLEAN
:
1972 * Add two bytes for each false ("no") value...
1975 for (i
= 0; i
< attr
->num_values
; i
++)
1976 if (!attr
->values
[i
].boolean
)
1980 case IPP_TAG_RANGE
:
1982 * A range is two signed integers separated by a hyphen, or
1983 * 23 characters max.
1986 bytes
+= attr
->num_values
* 23;
1989 case IPP_TAG_RESOLUTION
:
1991 * A resolution is two signed integers separated by an "x" and
1992 * suffixed by the units, or 26 characters max.
1995 bytes
+= attr
->num_values
* 26;
1998 case IPP_TAG_STRING
:
2001 case IPP_TAG_KEYWORD
:
2002 case IPP_TAG_CHARSET
:
2003 case IPP_TAG_LANGUAGE
:
2006 * Strings can contain characters that need quoting. We need
2007 * at least 2 * len + 2 characters to cover the quotes and
2008 * any backslashes in the string.
2011 for (i
= 0; i
< attr
->num_values
; i
++)
2012 bytes
+= 2 * strlen(attr
->values
[i
].string
.text
) + 2;
2016 break; /* anti-compiler-warning-code */
2025 * 'load_job_cache()' - Load jobs from the job.cache file.
2029 load_job_cache(const char *filename
) /* I - job.cache filename */
2031 cups_file_t
*fp
; /* job.cache file */
2032 char line
[1024], /* Line buffer */
2033 *value
; /* Value on line */
2034 int linenum
; /* Line number in file */
2035 cupsd_job_t
*job
; /* Current job */
2036 int jobid
; /* Job ID */
2037 char jobfile
[1024]; /* Job filename */
2041 * Open the job.cache file...
2044 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
2046 if (errno
!= ENOENT
)
2047 cupsdLogMessage(CUPSD_LOG_ERROR
,
2048 "Unable to open job cache file \"%s\": %s",
2049 filename
, strerror(errno
));
2051 load_request_root();
2057 * Read entries from the job cache file and create jobs as needed.
2060 cupsdLogMessage(CUPSD_LOG_INFO
, "Loading job cache file \"%s\"...",
2066 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
2068 if (!strcasecmp(line
, "NextJobId"))
2071 NextJobId
= atoi(value
);
2073 else if (!strcasecmp(line
, "<Job"))
2077 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing </Job> directive on line %d!",
2084 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing job ID on line %d!", linenum
);
2088 jobid
= atoi(value
);
2092 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad job ID %d on line %d!", jobid
,
2097 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, jobid
);
2098 if (access(jobfile
, 0))
2100 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Job %d] Files have gone away!",
2105 job
= calloc(1, sizeof(cupsd_job_t
));
2108 cupsdLogMessage(CUPSD_LOG_EMERG
,
2109 "[Job %d] Unable to allocate memory for job!", jobid
);
2114 job
->back_pipes
[0] = -1;
2115 job
->back_pipes
[1] = -1;
2116 job
->print_pipes
[0] = -1;
2117 job
->print_pipes
[1] = -1;
2118 job
->side_pipes
[0] = -1;
2119 job
->side_pipes
[1] = -1;
2120 job
->status_pipes
[0] = -1;
2121 job
->status_pipes
[1] = -1;
2123 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Loading from cache...",
2128 cupsdLogMessage(CUPSD_LOG_ERROR
,
2129 "Missing <Job #> directive on line %d!", linenum
);
2132 else if (!strcasecmp(line
, "</Job>"))
2134 cupsArrayAdd(Jobs
, job
);
2136 if (job
->state_value
<= IPP_JOB_STOPPED
)
2138 cupsArrayAdd(ActiveJobs
, job
);
2146 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing value on line %d!", linenum
);
2149 else if (!strcasecmp(line
, "State"))
2151 job
->state_value
= (ipp_jstate_t
)atoi(value
);
2153 if (job
->state_value
< IPP_JOB_PENDING
)
2154 job
->state_value
= IPP_JOB_PENDING
;
2155 else if (job
->state_value
> IPP_JOB_COMPLETED
)
2156 job
->state_value
= IPP_JOB_COMPLETED
;
2158 else if (!strcasecmp(line
, "Priority"))
2160 job
->priority
= atoi(value
);
2162 else if (!strcasecmp(line
, "Username"))
2164 cupsdSetString(&job
->username
, value
);
2166 else if (!strcasecmp(line
, "Destination"))
2168 cupsdSetString(&job
->dest
, value
);
2170 else if (!strcasecmp(line
, "DestType"))
2172 job
->dtype
= (cups_ptype_t
)atoi(value
);
2174 else if (!strcasecmp(line
, "NumFiles"))
2176 job
->num_files
= atoi(value
);
2178 if (job
->num_files
< 0)
2180 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad NumFiles value %d on line %d!",
2181 job
->num_files
, linenum
);
2186 if (job
->num_files
> 0)
2188 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-001", RequestRoot
,
2190 if (access(jobfile
, 0))
2192 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Data files have gone away!",
2198 job
->filetypes
= calloc(job
->num_files
, sizeof(mime_type_t
*));
2199 job
->compressions
= calloc(job
->num_files
, sizeof(int));
2201 if (!job
->filetypes
|| !job
->compressions
)
2203 cupsdLogMessage(CUPSD_LOG_EMERG
,
2204 "[Job %d] Unable to allocate memory for %d files!",
2205 job
->id
, job
->num_files
);
2210 else if (!strcasecmp(line
, "File"))
2212 int number
, /* File number */
2213 compression
; /* Compression value */
2214 char super
[MIME_MAX_SUPER
], /* MIME super type */
2215 type
[MIME_MAX_TYPE
]; /* MIME type */
2218 if (sscanf(value
, "%d%*[ \t]%15[^/]/%255s%d", &number
, super
, type
,
2221 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File on line %d!", linenum
);
2225 if (number
< 1 || number
> job
->num_files
)
2227 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File number %d on line %d!",
2234 job
->compressions
[number
] = compression
;
2235 job
->filetypes
[number
] = mimeType(MimeDatabase
, super
, type
);
2237 if (!job
->filetypes
[number
])
2240 * If the original MIME type is unknown, auto-type it!
2243 cupsdLogMessage(CUPSD_LOG_ERROR
,
2244 "[Job %d] Unknown MIME type %s/%s for file %d!",
2245 job
->id
, super
, type
, number
+ 1);
2247 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
2248 job
->id
, number
+ 1);
2249 job
->filetypes
[number
] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
2250 job
->compressions
+ number
);
2253 * If that didn't work, assume it is raw...
2256 if (!job
->filetypes
[number
])
2257 job
->filetypes
[number
] = mimeType(MimeDatabase
, "application",
2262 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unknown %s directive on line %d!",
2271 * 'load_next_job_id()' - Load the NextJobId value from the job.cache file.
2275 load_next_job_id(const char *filename
) /* I - job.cache filename */
2277 cups_file_t
*fp
; /* job.cache file */
2278 char line
[1024], /* Line buffer */
2279 *value
; /* Value on line */
2280 int linenum
; /* Line number in file */
2281 int next_job_id
; /* NextJobId value from line */
2285 * Read the NextJobId directive from the job.cache file and use
2286 * the value (if any).
2289 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
2291 if (errno
!= ENOENT
)
2292 cupsdLogMessage(CUPSD_LOG_ERROR
,
2293 "Unable to open job cache file \"%s\": %s",
2294 filename
, strerror(errno
));
2299 cupsdLogMessage(CUPSD_LOG_INFO
,
2300 "Loading NextJobId from job cache file \"%s\"...", filename
);
2304 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
2306 if (!strcasecmp(line
, "NextJobId"))
2310 next_job_id
= atoi(value
);
2312 if (next_job_id
> NextJobId
)
2313 NextJobId
= next_job_id
;
2324 * 'load_request_root()' - Load jobs from the RequestRoot directory.
2328 load_request_root(void)
2330 cups_dir_t
*dir
; /* Directory */
2331 cups_dentry_t
*dent
; /* Directory entry */
2332 cupsd_job_t
*job
; /* New job */
2336 * Open the requests directory...
2339 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Scanning %s for jobs...", RequestRoot
);
2341 if ((dir
= cupsDirOpen(RequestRoot
)) == NULL
)
2343 cupsdLogMessage(CUPSD_LOG_ERROR
,
2344 "Unable to open spool directory \"%s\": %s",
2345 RequestRoot
, strerror(errno
));
2350 * Read all the c##### files...
2353 while ((dent
= cupsDirRead(dir
)) != NULL
)
2354 if (strlen(dent
->filename
) >= 6 && dent
->filename
[0] == 'c')
2357 * Allocate memory for the job...
2360 if ((job
= calloc(sizeof(cupsd_job_t
), 1)) == NULL
)
2362 cupsdLogMessage(CUPSD_LOG_ERROR
, "Ran out of memory for jobs!");
2368 * Assign the job ID...
2371 job
->id
= atoi(dent
->filename
+ 1);
2372 job
->back_pipes
[0] = -1;
2373 job
->back_pipes
[1] = -1;
2374 job
->print_pipes
[0] = -1;
2375 job
->print_pipes
[1] = -1;
2376 job
->side_pipes
[0] = -1;
2377 job
->side_pipes
[1] = -1;
2378 job
->status_pipes
[0] = -1;
2379 job
->status_pipes
[1] = -1;
2381 if (job
->id
>= NextJobId
)
2382 NextJobId
= job
->id
+ 1;
2391 * Insert the job into the array, sorting by job priority and ID...
2394 cupsArrayAdd(Jobs
, job
);
2396 if (job
->state_value
<= IPP_JOB_STOPPED
)
2397 cupsArrayAdd(ActiveJobs
, job
);
2407 * 'set_time()' - Set one of the "time-at-xyz" attributes...
2411 set_time(cupsd_job_t
*job
, /* I - Job to update */
2412 const char *name
) /* I - Name of attribute */
2414 ipp_attribute_t
*attr
; /* Time attribute */
2417 if ((attr
= ippFindAttribute(job
->attrs
, name
, IPP_TAG_ZERO
)) != NULL
)
2419 attr
->value_tag
= IPP_TAG_INTEGER
;
2420 attr
->values
[0].integer
= time(NULL
);
2426 * 'set_hold_until()' - Set the hold time and update job-hold-until attribute...
2430 set_hold_until(cupsd_job_t
*job
, /* I - Job to update */
2431 time_t holdtime
) /* I - Hold until time */
2433 ipp_attribute_t
*attr
; /* job-hold-until attribute */
2434 struct tm
*holddate
; /* Hold date */
2435 char holdstr
[64]; /* Hold time */
2439 * Set the hold_until value and hold the job...
2442 cupsdLogMessage(CUPSD_LOG_DEBUG
, "set_hold_until: hold_until = %d",
2445 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
2446 job
->state_value
= IPP_JOB_HELD
;
2447 job
->hold_until
= holdtime
;
2450 * Update the job-hold-until attribute with a string representing GMT
2451 * time (HH:MM:SS)...
2454 holddate
= gmtime(&holdtime
);
2455 snprintf(holdstr
, sizeof(holdstr
), "%d:%d:%d", holddate
->tm_hour
,
2456 holddate
->tm_min
, holddate
->tm_sec
);
2458 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2459 IPP_TAG_KEYWORD
)) == NULL
)
2460 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2463 * Either add the attribute or update the value of the existing one
2467 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "job-hold-until",
2470 cupsdSetString(&attr
->values
[0].string
.text
, holdstr
);
2473 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2478 * 'start_job()' - Start a print job.
2482 start_job(cupsd_job_t
*job
, /* I - Job ID */
2483 cupsd_printer_t
*printer
) /* I - Printer to print job */
2485 int i
; /* Looping var */
2486 int slot
; /* Pipe slot */
2487 cups_array_t
*filters
, /* Filters for job */
2488 *prefilters
; /* Filters with prefilters */
2489 mime_filter_t
*filter
, /* Current filter */
2490 *prefilter
, /* Prefilter */
2491 port_monitor
; /* Port monitor filter */
2492 char method
[255], /* Method for output */
2493 *optptr
, /* Pointer to options */
2494 *valptr
; /* Pointer in value string */
2495 ipp_attribute_t
*attr
; /* Current attribute */
2496 struct stat backinfo
; /* Backend file information */
2497 int backroot
; /* Run backend as root? */
2498 int pid
; /* Process ID of new filter process */
2499 int banner_page
; /* 1 if banner page, 0 otherwise */
2500 int filterfds
[2][2];/* Pipes used between filters */
2501 int envc
; /* Number of environment variables */
2502 char **argv
, /* Filter command-line arguments */
2503 filename
[1024], /* Job filename */
2504 command
[1024], /* Full path to command */
2505 jobid
[255], /* Job ID string */
2506 title
[IPP_MAX_NAME
],
2507 /* Job title string */
2508 copies
[255], /* # copies string */
2509 *envp
[MAX_ENV
+ 19],
2510 /* Environment variables */
2511 charset
[255], /* CHARSET env variable */
2512 class_name
[255],/* CLASS env variable */
2513 classification
[1024],
2514 /* CLASSIFICATION env variable */
2516 /* CONTENT_TYPE env variable */
2518 /* DEVICE_URI env variable */
2519 final_content_type
[1024],
2520 /* FINAL_CONTENT_TYPE env variable */
2521 lang
[255], /* LANG env variable */
2523 apple_language
[255],
2524 /* APPLE_LANGUAGE env variable */
2525 #endif /* __APPLE__ */
2526 ppd
[1024], /* PPD env variable */
2528 /* PRINTER_INFO env variable */
2529 printer_location
[255],
2530 /* PRINTER_LOCATION env variable */
2532 /* PRINTER env variable */
2534 /* RIP_MAX_CACHE env variable */
2535 static char *options
= NULL
;/* Full list of options */
2536 static int optlength
= 0; /* Length of option buffer */
2539 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "start_job: file = %d/%d",
2540 job
->current_file
, job
->num_files
);
2542 if (job
->num_files
== 0)
2544 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "No files, canceling job!");
2545 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2550 * Figure out what filters are required to convert from
2551 * the source to the destination type...
2560 * Remote jobs and raw queues go directly to the printer without
2564 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Sending job to queue tagged as raw...");
2571 * Local jobs get filtered...
2574 filters
= mimeFilter(MimeDatabase
, job
->filetypes
[job
->current_file
],
2575 printer
->filetype
, &(job
->cost
));
2579 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
2580 "Unable to convert file %d to printable format!",
2582 cupsdLogMessage(CUPSD_LOG_INFO
,
2583 "Hint: Do you have Ghostscript installed?");
2585 if (LogLevel
< CUPSD_LOG_DEBUG
)
2586 cupsdLogMessage(CUPSD_LOG_INFO
,
2587 "Hint: Try setting the LogLevel to \"debug\".");
2589 job
->current_file
++;
2591 if (job
->current_file
== job
->num_files
)
2592 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2598 * Remove NULL ("-") filters...
2601 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
2603 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
2604 if (!strcmp(filter
->filter
, "-"))
2605 cupsArrayRemove(filters
, filter
);
2607 if (cupsArrayCount(filters
) == 0)
2609 cupsArrayDelete(filters
);
2614 * If this printer has any pre-filters, insert the required pre-filter
2615 * in the filters array...
2618 if (printer
->prefiltertype
&& filters
)
2620 prefilters
= cupsArrayNew(NULL
, NULL
);
2622 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
2624 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
2626 if ((prefilter
= mimeFilterLookup(MimeDatabase
, filter
->src
,
2627 printer
->prefiltertype
)))
2629 cupsArrayAdd(prefilters
, prefilter
);
2630 job
->cost
+= prefilter
->cost
;
2633 cupsArrayAdd(prefilters
, filter
);
2636 cupsArrayDelete(filters
);
2637 filters
= prefilters
;
2642 * Set a minimum cost of 100 for all jobs so that FilterLimit
2643 * works with raw queues and other low-cost paths.
2646 if (job
->cost
< 100)
2650 * See if the filter cost is too high...
2653 if ((FilterLevel
+ job
->cost
) > FilterLimit
&& FilterLevel
> 0 &&
2657 * Don't print this job quite yet...
2660 cupsArrayDelete(filters
);
2662 cupsdLogJob(job
, CUPSD_LOG_INFO
,
2663 "Holding because filter limit has been reached.");
2664 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
2665 "start_job: file=%d, cost=%d, level=%d, limit=%d",
2666 job
->current_file
, job
->cost
, FilterLevel
,
2671 FilterLevel
+= job
->cost
;
2674 * Add decompression/raw filter as needed...
2677 if ((!printer
->raw
&& job
->compressions
[job
->current_file
]) ||
2678 (!filters
&& !printer
->remote
&&
2679 (job
->num_files
> 1 || !strncmp(printer
->device_uri
, "file:", 5))))
2682 * Add gziptoany filter to the front of the list...
2686 filters
= cupsArrayNew(NULL
, NULL
);
2688 if (!cupsArrayInsert(filters
, &gziptoany_filter
))
2690 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
2691 "Unable to add decompression filter - %s", strerror(errno
));
2693 cupsArrayDelete(filters
);
2695 job
->current_file
++;
2697 if (job
->current_file
== job
->num_files
)
2698 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2705 * Add port monitor, if any...
2708 if (printer
->port_monitor
)
2711 * Add port monitor to the end of the list...
2715 filters
= cupsArrayNew(NULL
, NULL
);
2717 port_monitor
.src
= NULL
;
2718 port_monitor
.dst
= NULL
;
2719 port_monitor
.cost
= 0;
2721 snprintf(port_monitor
.filter
, sizeof(port_monitor
.filter
),
2722 "%s/monitor/%s", ServerBin
, printer
->port_monitor
);
2724 if (!cupsArrayAdd(filters
, &port_monitor
))
2726 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
2727 "Unable to add port monitor - %s", strerror(errno
));
2729 cupsArrayDelete(filters
);
2731 job
->current_file
++;
2733 if (job
->current_file
== job
->num_files
)
2734 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2741 * Make sure we don't go over the "MAX_FILTERS" limit...
2744 if (cupsArrayCount(filters
) > MAX_FILTERS
)
2746 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
2747 "Too many filters (%d > %d), unable to print!",
2748 cupsArrayCount(filters
), MAX_FILTERS
);
2750 cupsArrayDelete(filters
);
2751 cupsdCancelJob(job
, 0, IPP_JOB_STOPPED
);
2757 * Update the printer and job state to "processing"...
2760 job
->state
->values
[0].integer
= IPP_JOB_PROCESSING
;
2761 job
->state_value
= IPP_JOB_PROCESSING
;
2764 job
->printer
= printer
;
2767 cupsdSetPrinterState(printer
, IPP_PRINTER_PROCESSING
, 0);
2769 if (job
->current_file
== 0)
2772 * Add to the printing list...
2775 cupsArrayAdd(PrintingJobs
, job
);
2776 cupsdSetBusyState();
2779 * Set the processing time...
2782 set_time(job
, "time-at-processing");
2785 * Create the backchannel pipes and make them non-blocking...
2788 cupsdOpenPipe(job
->back_pipes
);
2790 fcntl(job
->back_pipes
[0], F_SETFL
,
2791 fcntl(job
->back_pipes
[0], F_GETFL
) | O_NONBLOCK
);
2793 fcntl(job
->back_pipes
[1], F_SETFL
,
2794 fcntl(job
->back_pipes
[1], F_GETFL
) | O_NONBLOCK
);
2797 * Create the side-channel pipes and make them non-blocking...
2800 socketpair(AF_LOCAL
, SOCK_STREAM
, 0, job
->side_pipes
);
2802 fcntl(job
->side_pipes
[0], F_SETFL
,
2803 fcntl(job
->side_pipes
[0], F_GETFL
) | O_NONBLOCK
);
2805 fcntl(job
->side_pipes
[1], F_SETFL
,
2806 fcntl(job
->side_pipes
[1], F_GETFL
) | O_NONBLOCK
);
2810 * Determine if we are printing a banner page or not...
2813 if (job
->job_sheets
== NULL
)
2815 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "No job-sheets attribute.");
2816 if ((job
->job_sheets
=
2817 ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_ZERO
)) != NULL
)
2818 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
2819 "... but someone added one without setting job_sheets!");
2821 else if (job
->job_sheets
->num_values
== 1)
2822 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "job-sheets=%s",
2823 job
->job_sheets
->values
[0].string
.text
);
2825 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "job-sheets=%s,%s",
2826 job
->job_sheets
->values
[0].string
.text
,
2827 job
->job_sheets
->values
[1].string
.text
);
2829 if (printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
))
2831 else if (job
->job_sheets
== NULL
)
2833 else if (strcasecmp(job
->job_sheets
->values
[0].string
.text
, "none") != 0 &&
2834 job
->current_file
== 0)
2836 else if (job
->job_sheets
->num_values
> 1 &&
2837 strcasecmp(job
->job_sheets
->values
[1].string
.text
, "none") != 0 &&
2838 job
->current_file
== (job
->num_files
- 1))
2843 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "banner_page = %d", banner_page
);
2846 * Building the options string is harder than it needs to be, but
2847 * for the moment we need to pass strings for command-line args and
2848 * not IPP attribute pointers... :)
2850 * First allocate/reallocate the option buffer as needed...
2853 i
= ipp_length(job
->attrs
);
2855 if (i
> optlength
|| !options
)
2860 optptr
= realloc(options
, i
);
2864 cupsdLogJob(job
, CUPSD_LOG_CRIT
,
2865 "Unable to allocate %d bytes for option buffer!", i
);
2867 cupsArrayDelete(filters
);
2869 FilterLevel
-= job
->cost
;
2871 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2880 * Now loop through the attributes and convert them to the textual
2881 * representation used by the filters...
2887 snprintf(title
, sizeof(title
), "%s-%d", printer
->name
, job
->id
);
2888 strcpy(copies
, "1");
2890 for (attr
= job
->attrs
->attrs
; attr
!= NULL
; attr
= attr
->next
)
2892 if (!strcmp(attr
->name
, "copies") &&
2893 attr
->value_tag
== IPP_TAG_INTEGER
)
2896 * Don't use the # copies attribute if we are printing the job sheets...
2900 sprintf(copies
, "%d", attr
->values
[0].integer
);
2902 else if (!strcmp(attr
->name
, "job-name") &&
2903 (attr
->value_tag
== IPP_TAG_NAME
||
2904 attr
->value_tag
== IPP_TAG_NAMELANG
))
2905 strlcpy(title
, attr
->values
[0].string
.text
, sizeof(title
));
2906 else if (attr
->group_tag
== IPP_TAG_JOB
)
2909 * Filter out other unwanted attributes...
2912 if (attr
->value_tag
== IPP_TAG_MIMETYPE
||
2913 attr
->value_tag
== IPP_TAG_NAMELANG
||
2914 attr
->value_tag
== IPP_TAG_TEXTLANG
||
2915 (attr
->value_tag
== IPP_TAG_URI
&& strcmp(attr
->name
, "job-uuid")) ||
2916 attr
->value_tag
== IPP_TAG_URISCHEME
||
2917 attr
->value_tag
== IPP_TAG_BEGIN_COLLECTION
) /* Not yet supported */
2920 if (!strncmp(attr
->name
, "time-", 5))
2923 if (!strncmp(attr
->name
, "job-", 4) &&
2924 strcmp(attr
->name
, "job-billing") &&
2925 strcmp(attr
->name
, "job-impressions") &&
2926 strcmp(attr
->name
, "job-originating-host-name") &&
2927 strcmp(attr
->name
, "job-uuid") &&
2928 !(printer
->type
& CUPS_PRINTER_REMOTE
))
2931 if ((!strcmp(attr
->name
, "job-impressions") ||
2932 !strcmp(attr
->name
, "page-label") ||
2933 !strcmp(attr
->name
, "page-border") ||
2934 !strncmp(attr
->name
, "number-up", 9) ||
2935 !strcmp(attr
->name
, "page-ranges") ||
2936 !strcmp(attr
->name
, "page-set") ||
2937 !strcasecmp(attr
->name
, "AP_FIRSTPAGE_InputSlot") ||
2938 !strcasecmp(attr
->name
, "AP_FIRSTPAGE_ManualFeed") ||
2939 !strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
2940 "PMTotalSidesImaged..n.") ||
2941 !strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
2942 "PMTotalBeginPages..n.")) &&
2947 * Otherwise add them to the list...
2950 if (optptr
> options
)
2951 strlcat(optptr
, " ", optlength
- (optptr
- options
));
2953 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
2955 strlcat(optptr
, attr
->name
, optlength
- (optptr
- options
));
2956 strlcat(optptr
, "=", optlength
- (optptr
- options
));
2959 for (i
= 0; i
< attr
->num_values
; i
++)
2962 strlcat(optptr
, ",", optlength
- (optptr
- options
));
2964 optptr
+= strlen(optptr
);
2966 switch (attr
->value_tag
)
2968 case IPP_TAG_INTEGER
:
2970 snprintf(optptr
, optlength
- (optptr
- options
),
2971 "%d", attr
->values
[i
].integer
);
2974 case IPP_TAG_BOOLEAN
:
2975 if (!attr
->values
[i
].boolean
)
2976 strlcat(optptr
, "no", optlength
- (optptr
- options
));
2978 case IPP_TAG_NOVALUE
:
2979 strlcat(optptr
, attr
->name
,
2980 optlength
- (optptr
- options
));
2983 case IPP_TAG_RANGE
:
2984 if (attr
->values
[i
].range
.lower
== attr
->values
[i
].range
.upper
)
2985 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2986 "%d", attr
->values
[i
].range
.lower
);
2988 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2989 "%d-%d", attr
->values
[i
].range
.lower
,
2990 attr
->values
[i
].range
.upper
);
2993 case IPP_TAG_RESOLUTION
:
2994 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2995 "%dx%d%s", attr
->values
[i
].resolution
.xres
,
2996 attr
->values
[i
].resolution
.yres
,
2997 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
3001 case IPP_TAG_STRING
:
3004 case IPP_TAG_KEYWORD
:
3005 case IPP_TAG_CHARSET
:
3006 case IPP_TAG_LANGUAGE
:
3008 for (valptr
= attr
->values
[i
].string
.text
; *valptr
;)
3010 if (strchr(" \t\n\\\'\"", *valptr
))
3012 *optptr
++ = *valptr
++;
3019 break; /* anti-compiler-warning-code */
3023 optptr
+= strlen(optptr
);
3028 * Build the command-line arguments for the filters. Each filter
3029 * has 6 or 7 arguments:
3033 * argv[2] = username
3035 * argv[4] = # copies
3037 * argv[6] = filename (optional; normally stdin)
3039 * This allows legacy printer drivers that use the old System V
3040 * printing interface to be used by CUPS.
3042 * For remote jobs, we send all of the files in the argument list.
3045 if (printer
->remote
&& job
->num_files
> 1)
3046 argv
= calloc(7 + job
->num_files
, sizeof(char *));
3048 argv
= calloc(8, sizeof(char *));
3052 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to allocate argument array!");
3053 cupsArrayDelete(filters
);
3055 FilterLevel
-= job
->cost
;
3057 cupsdStopPrinter(printer
, 0);
3061 sprintf(jobid
, "%d", job
->id
);
3063 argv
[0] = printer
->name
;
3065 argv
[2] = job
->username
;
3070 if (printer
->remote
&& job
->num_files
> 1)
3072 for (i
= 0; i
< job
->num_files
; i
++)
3074 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
3076 argv
[6 + i
] = strdup(filename
);
3081 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
3082 job
->id
, job
->current_file
+ 1);
3086 for (i
= 0; argv
[i
]; i
++)
3087 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
3088 "argv[%d]=\"%s\"", i
, argv
[i
]);
3091 * Create environment variable strings for the filters...
3094 attr
= ippFindAttribute(job
->attrs
, "attributes-natural-language",
3098 strcpy(apple_language
, "APPLE_LANGUAGE=");
3099 _cupsAppleLanguage(attr
->values
[0].string
.text
,
3100 apple_language
+ 15, sizeof(apple_language
) - 15);
3101 #endif /* __APPLE__ */
3103 switch (strlen(attr
->values
[0].string
.text
))
3107 * This is an unknown or badly formatted language code; use
3108 * the POSIX locale...
3111 strcpy(lang
, "LANG=C");
3116 * Just the language code (ll)...
3119 snprintf(lang
, sizeof(lang
), "LANG=%s.UTF8",
3120 attr
->values
[0].string
.text
);
3125 * Language and country code (ll-cc)...
3128 snprintf(lang
, sizeof(lang
), "LANG=%c%c_%c%c.UTF8",
3129 attr
->values
[0].string
.text
[0],
3130 attr
->values
[0].string
.text
[1],
3131 toupper(attr
->values
[0].string
.text
[3] & 255),
3132 toupper(attr
->values
[0].string
.text
[4] & 255));
3136 attr
= ippFindAttribute(job
->attrs
, "document-format",
3139 (optptr
= strstr(attr
->values
[0].string
.text
, "charset=")) != NULL
)
3140 snprintf(charset
, sizeof(charset
), "CHARSET=%s", optptr
+ 8);
3143 attr
= ippFindAttribute(job
->attrs
, "attributes-charset",
3145 snprintf(charset
, sizeof(charset
), "CHARSET=%s",
3146 attr
->values
[0].string
.text
);
3149 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s/%s",
3150 job
->filetypes
[job
->current_file
]->super
,
3151 job
->filetypes
[job
->current_file
]->type
);
3152 snprintf(device_uri
, sizeof(device_uri
), "DEVICE_URI=%s",
3153 printer
->device_uri
);
3154 snprintf(ppd
, sizeof(ppd
), "PPD=%s/ppd/%s.ppd", ServerRoot
, printer
->name
);
3155 snprintf(printer_info
, sizeof(printer_name
), "PRINTER_INFO=%s",
3156 printer
->info
? printer
->info
: "");
3157 snprintf(printer_location
, sizeof(printer_name
), "PRINTER_LOCATION=%s",
3158 printer
->location
? printer
->location
: "");
3159 snprintf(printer_name
, sizeof(printer_name
), "PRINTER=%s", printer
->name
);
3160 snprintf(rip_max_cache
, sizeof(rip_max_cache
), "RIP_MAX_CACHE=%s", RIPCache
);
3162 envc
= cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
3164 envp
[envc
++] = charset
;
3165 envp
[envc
++] = lang
;
3167 envp
[envc
++] = apple_language
;
3168 #endif /* __APPLE__ */
3169 envp
[envc
++] = ppd
;
3170 envp
[envc
++] = rip_max_cache
;
3171 envp
[envc
++] = content_type
;
3172 envp
[envc
++] = device_uri
;
3173 envp
[envc
++] = printer_info
;
3174 envp
[envc
++] = printer_location
;
3175 envp
[envc
++] = printer_name
;
3176 envp
[envc
++] = banner_page
? "CUPS_FILETYPE=job-sheet" :
3177 "CUPS_FILETYPE=document";
3179 if (!printer
->remote
&& !printer
->raw
)
3181 filter
= (mime_filter_t
*)cupsArrayLast(filters
);
3183 if (printer
->port_monitor
)
3184 filter
= (mime_filter_t
*)cupsArrayPrev(filters
);
3186 if (filter
&& filter
->dst
)
3188 snprintf(final_content_type
, sizeof(final_content_type
),
3189 "FINAL_CONTENT_TYPE=%s/%s",
3190 filter
->dst
->super
, filter
->dst
->type
);
3191 envp
[envc
++] = final_content_type
;
3195 if (Classification
&& !banner_page
)
3197 if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
3198 IPP_TAG_NAME
)) == NULL
)
3199 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3201 else if (attr
->num_values
> 1 &&
3202 strcmp(attr
->values
[1].string
.text
, "none") != 0)
3203 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3204 attr
->values
[1].string
.text
);
3206 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3207 attr
->values
[0].string
.text
);
3209 envp
[envc
++] = classification
;
3212 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
3214 snprintf(class_name
, sizeof(class_name
), "CLASS=%s", job
->dest
);
3215 envp
[envc
++] = class_name
;
3218 if (job
->auth_username
)
3219 envp
[envc
++] = job
->auth_username
;
3220 if (job
->auth_domain
)
3221 envp
[envc
++] = job
->auth_domain
;
3222 if (job
->auth_password
)
3223 envp
[envc
++] = job
->auth_password
;
3227 envp
[envc
++] = job
->ccname
;
3228 #endif /* HAVE_GSSAPI */
3232 for (i
= 0; i
< envc
; i
++)
3233 if (!strncmp(envp
[i
], "AUTH_", 5))
3234 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"AUTH_%c****\"", i
,
3236 else if (strncmp(envp
[i
], "DEVICE_URI=", 11))
3237 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"%s\"", i
, envp
[i
]);
3239 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"DEVICE_URI=%s\"", i
,
3240 printer
->sanitized_device_uri
);
3242 if (printer
->remote
)
3243 job
->current_file
= job
->num_files
;
3245 job
->current_file
++;
3248 * Now create processes for all of the filters...
3251 filterfds
[0][0] = -1;
3252 filterfds
[0][1] = -1;
3253 filterfds
[1][0] = -1;
3254 filterfds
[1][1] = -1;
3256 if (!job
->status_buffer
)
3258 if (cupsdOpenPipe(job
->status_pipes
))
3260 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
3261 "Unable to create job status pipes - %s.", strerror(errno
));
3262 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3263 "Unable to create status pipes - %s.", strerror(errno
));
3265 cupsdAddPrinterHistory(printer
);
3267 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3268 "Job canceled because the server could not create the job "
3274 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3275 "start_job: status_pipes = [ %d %d ]",
3276 job
->status_pipes
[0], job
->status_pipes
[1]);
3278 job
->status_buffer
= cupsdStatBufNew(job
->status_pipes
[0], NULL
);
3279 job
->status_level
= CUPSD_LOG_INFO
;
3283 memset(job
->filters
, 0, sizeof(job
->filters
));
3286 job
->profile
= cupsdCreateProfile(job
->id
);
3288 for (i
= 0, slot
= 0, filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
3290 i
++, filter
= (mime_filter_t
*)cupsArrayNext(filters
))
3292 if (filter
->filter
[0] != '/')
3293 snprintf(command
, sizeof(command
), "%s/filter/%s", ServerBin
,
3296 strlcpy(command
, filter
->filter
, sizeof(command
));
3298 if (i
< (cupsArrayCount(filters
) - 1))
3300 if (cupsdOpenPipe(filterfds
[slot
]))
3302 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
3303 "Unable to create job filter pipes - %s.", strerror(errno
));
3304 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3305 "Unable to create filter pipes - %s.", strerror(errno
));
3306 cupsdAddPrinterHistory(printer
);
3308 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3309 "Job canceled because the server could not create the "
3317 if (job
->current_file
== 1)
3319 if (strncmp(printer
->device_uri
, "file:", 5) != 0)
3321 if (cupsdOpenPipe(job
->print_pipes
))
3323 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
3324 "Unable to create job backend pipes - %s.",
3326 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3327 "Unable to create backend pipes - %s.", strerror(errno
));
3328 cupsdAddPrinterHistory(printer
);
3330 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3331 "Job canceled because the server could not create "
3332 "the backend pipes.");
3339 job
->print_pipes
[0] = -1;
3340 if (!strcmp(printer
->device_uri
, "file:/dev/null") ||
3341 !strcmp(printer
->device_uri
, "file:///dev/null"))
3342 job
->print_pipes
[1] = -1;
3345 if (!strncmp(printer
->device_uri
, "file:/dev/", 10))
3346 job
->print_pipes
[1] = open(printer
->device_uri
+ 5,
3348 else if (!strncmp(printer
->device_uri
, "file:///dev/", 12))
3349 job
->print_pipes
[1] = open(printer
->device_uri
+ 7,
3351 else if (!strncmp(printer
->device_uri
, "file:///", 8))
3352 job
->print_pipes
[1] = open(printer
->device_uri
+ 7,
3353 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
3355 job
->print_pipes
[1] = open(printer
->device_uri
+ 5,
3356 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
3358 if (job
->print_pipes
[1] < 0)
3360 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
3361 "Unable to open output file \"%s\" - %s.",
3362 printer
->device_uri
, strerror(errno
));
3363 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3364 "Unable to open output file \"%s\" - %s.",
3365 printer
->device_uri
, strerror(errno
));
3367 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3368 "Job canceled because the server could not open the "
3374 fcntl(job
->print_pipes
[1], F_SETFD
,
3375 fcntl(job
->print_pipes
[1], F_GETFD
) | FD_CLOEXEC
);
3379 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3380 "start_job: print_pipes = [ %d %d ]",
3381 job
->print_pipes
[0], job
->print_pipes
[1]);
3384 filterfds
[slot
][0] = job
->print_pipes
[0];
3385 filterfds
[slot
][1] = job
->print_pipes
[1];
3388 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "start_job: filter=\"%s\"", command
);
3389 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "start_job: filterfds[%d]=[ %d %d ]",
3390 slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3392 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
3393 filterfds
[slot
][1], job
->status_pipes
[1],
3394 job
->back_pipes
[0], job
->side_pipes
[0], 0,
3395 job
->profile
, job
->filters
+ i
);
3397 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3398 "start_job: Closing filter pipes for slot %d [ %d %d ]...",
3399 !slot
, filterfds
[!slot
][0], filterfds
[!slot
][1]);
3401 cupsdClosePipe(filterfds
[!slot
]);
3405 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Unable to start filter \"%s\" - %s.",
3406 filter
->filter
, strerror(errno
));
3407 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3408 "Unable to start filter \"%s\" - %s.",
3409 filter
->filter
, strerror(errno
));
3411 cupsdAddPrinterHistory(printer
);
3413 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3414 "Job canceled because the server could not execute a "
3420 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Started filter %s (PID %d)", command
,
3427 cupsArrayDelete(filters
);
3431 * Finally, pipe the final output into a backend process if needed...
3434 if (strncmp(printer
->device_uri
, "file:", 5) != 0)
3436 if (job
->current_file
== 1 || printer
->remote
)
3438 sscanf(printer
->device_uri
, "%254[^:]", method
);
3439 snprintf(command
, sizeof(command
), "%s/backend/%s", ServerBin
, method
);
3442 * See if the backend needs to run as root...
3447 else if (stat(command
, &backinfo
))
3450 backroot
= !(backinfo
.st_mode
& (S_IRWXG
| S_IRWXO
));
3452 argv
[0] = printer
->sanitized_device_uri
;
3454 filterfds
[slot
][0] = -1;
3455 filterfds
[slot
][1] = -1;
3457 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "start_job: backend=\"%s\"", command
);
3458 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "start_job: filterfds[%d] = [ %d %d ]",
3459 slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3461 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
3462 filterfds
[slot
][1], job
->status_pipes
[1],
3463 job
->back_pipes
[1], job
->side_pipes
[1],
3464 backroot
, job
->profile
, &(job
->backend
));
3468 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Unable to start backend \"%s\" - %s.",
3469 method
, strerror(errno
));
3470 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3471 "Unable to start backend \"%s\" - %s.", method
,
3474 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3475 "Job canceled because the server could not execute "
3482 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Started backend %s (PID %d)",
3487 if (job
->current_file
== job
->num_files
)
3489 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3490 "start_job: Closing print pipes [ %d %d ]...",
3491 job
->print_pipes
[0], job
->print_pipes
[1]);
3493 cupsdClosePipe(job
->print_pipes
);
3495 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3496 "start_job: Closing back pipes [ %d %d ]...",
3497 job
->back_pipes
[0], job
->back_pipes
[1]);
3499 cupsdClosePipe(job
->back_pipes
);
3501 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3502 "start_job: Closing side pipes [ %d %d ]...",
3503 job
->side_pipes
[0], job
->side_pipes
[1]);
3505 cupsdClosePipe(job
->side_pipes
);
3507 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3508 "start_job: Closing status output pipe %d...",
3509 job
->status_pipes
[1]);
3511 close(job
->status_pipes
[1]);
3512 job
->status_pipes
[1] = -1;
3517 filterfds
[slot
][0] = -1;
3518 filterfds
[slot
][1] = -1;
3520 if (job
->current_file
== job
->num_files
)
3522 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3523 "start_job: Closing print pipes [ %d %d ]...",
3524 job
->print_pipes
[0], job
->print_pipes
[1]);
3526 cupsdClosePipe(job
->print_pipes
);
3528 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3529 "start_job: Closing status output pipe %d...",
3530 job
->status_pipes
[1]);
3532 close(job
->status_pipes
[1]);
3533 job
->status_pipes
[1] = -1;
3537 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3538 "start_job: Closing filter pipes for slot %d [ %d %d ]...",
3539 slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3540 cupsdClosePipe(filterfds
[slot
]);
3542 if (printer
->remote
&& job
->num_files
> 1)
3544 for (i
= 0; i
< job
->num_files
; i
++)
3550 cupsdAddSelect(job
->status_buffer
->fd
, (cupsd_selfunc_t
)update_job
, NULL
,
3553 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
, "Job #%d started.",
3560 * If we get here, we need to abort the current job and close out all
3561 * files and pipes...
3566 for (slot
= 0; slot
< 2; slot
++)
3568 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3569 "start_job: Closing filter pipes for slot %d [ %d %d ]...",
3570 slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3571 cupsdClosePipe(filterfds
[slot
]);
3574 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3575 "start_job: Closing status pipes [ %d %d ]...",
3576 job
->status_pipes
[0], job
->status_pipes
[1]);
3577 cupsdClosePipe(job
->status_pipes
);
3578 cupsdStatBufDelete(job
->status_buffer
);
3580 job
->status_buffer
= NULL
;
3582 cupsArrayDelete(filters
);
3584 if (printer
->remote
&& job
->num_files
> 1)
3586 for (i
= 0; i
< job
->num_files
; i
++)
3592 cupsdStopJob(job
, 0);
3597 * 'unload_job()' - Unload a job from memory.
3601 unload_job(cupsd_job_t
*job
) /* I - Job */
3606 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Unloading...", job
->id
);
3608 ippDelete(job
->attrs
);
3613 job
->job_sheets
= NULL
;
3614 job
->printer_message
= NULL
;
3615 job
->printer_reasons
= NULL
;
3620 * 'update_job()' - Read a status update from a job's filters.
3624 update_job(cupsd_job_t
*job
) /* I - Job to check */
3626 int i
; /* Looping var */
3627 int copies
; /* Number of copies printed */
3628 char message
[1024], /* Message text */
3629 *ptr
; /* Pointer update... */
3630 int loglevel
, /* Log level for message */
3631 event
= 0; /* Events? */
3634 while ((ptr
= cupsdStatBufUpdate(job
->status_buffer
, &loglevel
,
3635 message
, sizeof(message
))) != NULL
)
3638 * Process page and printer state messages as needed...
3641 if (loglevel
== CUPSD_LOG_PAGE
)
3644 * Page message; send the message to the page_log file and update the
3645 * job sheet count...
3648 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "PAGE: %s", message
);
3652 if (!strncasecmp(message
, "total ", 6))
3655 * Got a total count of pages from a backend or filter...
3658 copies
= atoi(message
+ 6);
3659 copies
-= job
->sheets
->values
[0].integer
; /* Just track the delta */
3661 else if (!sscanf(message
, "%*d%d", &copies
))
3664 job
->sheets
->values
[0].integer
+= copies
;
3666 if (job
->printer
->page_limit
)
3668 cupsd_quota_t
*q
= cupsdUpdateQuota(job
->printer
, job
->username
,
3672 if (AppleQuotas
&& q
->page_count
== -3)
3675 * Quota limit exceeded, cancel job in progress immediately...
3678 cupsdLogJob(job
, CUPSD_LOG_INFO
,
3679 "Canceled because pages exceed user %s "
3680 "quota limit on printer %s (%s).",
3681 job
->username
, job
->printer
->name
,
3682 job
->printer
->info
);
3684 cupsdCancelJob(job
, 1, IPP_JOB_CANCELED
);
3689 #endif /* __APPLE__ */
3693 cupsdLogPage(job
, message
);
3696 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
3697 "Printed %d page(s).", job
->sheets
->values
[0].integer
);
3699 else if (loglevel
== CUPSD_LOG_STATE
)
3701 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "STATE: %s", message
);
3703 if (!strcmp(message
, "paused"))
3705 cupsdStopPrinter(job
->printer
, 1);
3710 cupsdSetPrinterReasons(job
->printer
, message
);
3711 cupsdAddPrinterHistory(job
->printer
);
3712 event
|= CUPSD_EVENT_PRINTER_STATE
;
3715 update_job_attrs(job
, 0);
3717 else if (loglevel
== CUPSD_LOG_ATTR
)
3720 * Set attribute(s)...
3723 int num_attrs
; /* Number of attributes */
3724 cups_option_t
*attrs
; /* Attributes */
3725 const char *attr
; /* Attribute */
3728 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "ATTR: %s", message
);
3730 num_attrs
= cupsParseOptions(message
, 0, &attrs
);
3732 if ((attr
= cupsGetOption("auth-info-required", num_attrs
,
3735 cupsdSetAuthInfoRequired(job
->printer
, attr
, NULL
);
3736 cupsdSetPrinterAttrs(job
->printer
);
3738 if (job
->printer
->type
& CUPS_PRINTER_DISCOVERED
)
3739 cupsdMarkDirty(CUPSD_DIRTY_REMOTE
);
3741 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3744 if ((attr
= cupsGetOption("job-media-progress", num_attrs
,
3747 int progress
= atoi(attr
);
3750 if (progress
>= 0 && progress
<= 100)
3752 job
->progress
= progress
;
3755 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
3756 "Printing page %d, %d%%",
3757 job
->sheets
->values
[0].integer
, job
->progress
);
3761 if ((attr
= cupsGetOption("printer-alert", num_attrs
, attrs
)) != NULL
)
3763 cupsdSetString(&job
->printer
->alert
, attr
);
3764 event
|= CUPSD_EVENT_PRINTER_STATE
;
3767 if ((attr
= cupsGetOption("printer-alert-description", num_attrs
,
3770 cupsdSetString(&job
->printer
->alert_description
, attr
);
3771 event
|= CUPSD_EVENT_PRINTER_STATE
;
3774 if ((attr
= cupsGetOption("marker-colors", num_attrs
, attrs
)) != NULL
)
3776 cupsdSetPrinterAttr(job
->printer
, "marker-colors", (char *)attr
);
3777 job
->printer
->marker_time
= time(NULL
);
3778 event
|= CUPSD_EVENT_PRINTER_STATE
;
3779 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3782 if ((attr
= cupsGetOption("marker-levels", num_attrs
, attrs
)) != NULL
)
3784 cupsdSetPrinterAttr(job
->printer
, "marker-levels", (char *)attr
);
3785 job
->printer
->marker_time
= time(NULL
);
3786 event
|= CUPSD_EVENT_PRINTER_STATE
;
3787 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3790 if ((attr
= cupsGetOption("marker-low-levels", num_attrs
, attrs
)) != NULL
)
3792 cupsdSetPrinterAttr(job
->printer
, "marker-low-levels", (char *)attr
);
3793 job
->printer
->marker_time
= time(NULL
);
3794 event
|= CUPSD_EVENT_PRINTER_STATE
;
3795 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3798 if ((attr
= cupsGetOption("marker-high-levels", num_attrs
, attrs
)) != NULL
)
3800 cupsdSetPrinterAttr(job
->printer
, "marker-high-levels", (char *)attr
);
3801 job
->printer
->marker_time
= time(NULL
);
3802 event
|= CUPSD_EVENT_PRINTER_STATE
;
3803 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3806 if ((attr
= cupsGetOption("marker-message", num_attrs
, attrs
)) != NULL
)
3808 cupsdSetPrinterAttr(job
->printer
, "marker-message", (char *)attr
);
3809 job
->printer
->marker_time
= time(NULL
);
3810 event
|= CUPSD_EVENT_PRINTER_STATE
;
3811 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3814 if ((attr
= cupsGetOption("marker-names", num_attrs
, attrs
)) != NULL
)
3816 cupsdSetPrinterAttr(job
->printer
, "marker-names", (char *)attr
);
3817 job
->printer
->marker_time
= time(NULL
);
3818 event
|= CUPSD_EVENT_PRINTER_STATE
;
3819 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3822 if ((attr
= cupsGetOption("marker-types", num_attrs
, attrs
)) != NULL
)
3824 cupsdSetPrinterAttr(job
->printer
, "marker-types", (char *)attr
);
3825 job
->printer
->marker_time
= time(NULL
);
3826 event
|= CUPSD_EVENT_PRINTER_STATE
;
3827 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3830 cupsFreeOptions(num_attrs
, attrs
);
3832 else if (loglevel
== CUPSD_LOG_PPD
)
3835 * Set attribute(s)...
3838 int num_keywords
; /* Number of keywords */
3839 cups_option_t
*keywords
; /* Keywords */
3842 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "PPD: %s", message
);
3844 num_keywords
= cupsParseOptions(message
, 0, &keywords
);
3846 if (cupsdUpdatePrinterPPD(job
->printer
, num_keywords
, keywords
))
3847 cupsdSetPrinterAttrs(job
->printer
);
3849 cupsFreeOptions(num_keywords
, keywords
);
3852 else if (!strncmp(message
, "recoverable:", 12))
3854 cupsdSetPrinterReasons(job
->printer
,
3855 "+com.apple.print.recoverable-warning");
3858 while (isspace(*ptr
& 255))
3861 cupsdSetString(&job
->printer
->recoverable
, ptr
);
3862 cupsdAddPrinterHistory(job
->printer
);
3863 event
|= CUPSD_EVENT_PRINTER_STATE
;
3865 else if (!strncmp(message
, "recovered:", 10))
3867 cupsdSetPrinterReasons(job
->printer
,
3868 "-com.apple.print.recoverable-warning");
3871 while (isspace(*ptr
& 255))
3874 cupsdSetString(&job
->printer
->recoverable
, ptr
);
3875 cupsdAddPrinterHistory(job
->printer
);
3876 event
|= CUPSD_EVENT_PRINTER_STATE
;
3878 #endif /* __APPLE__ */
3881 cupsdLogJob(job
, loglevel
, "%s", message
);
3883 if (loglevel
< CUPSD_LOG_DEBUG
)
3885 strlcpy(job
->printer
->state_message
, message
,
3886 sizeof(job
->printer
->state_message
));
3887 cupsdAddPrinterHistory(job
->printer
);
3889 event
|= CUPSD_EVENT_PRINTER_STATE
;
3891 if (loglevel
<= job
->status_level
)
3894 * Some messages show in the printer-state-message attribute...
3897 if (loglevel
!= CUPSD_LOG_NOTICE
)
3898 job
->status_level
= loglevel
;
3900 update_job_attrs(job
, 1);
3905 if (!strchr(job
->status_buffer
->buffer
, '\n'))
3909 if (event
& CUPSD_EVENT_PRINTER_STATE
)
3910 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, job
->printer
, NULL
,
3911 (job
->printer
->type
& CUPS_PRINTER_CLASS
) ?
3912 "Class \"%s\" state changed." :
3913 "Printer \"%s\" state changed.",
3914 job
->printer
->name
);
3916 if (ptr
== NULL
&& !job
->status_buffer
->bufused
)
3919 * See if all of the filters and the backend have returned their
3923 for (i
= 0; job
->filters
[i
] < 0; i
++);
3925 if (job
->filters
[i
])
3928 if (job
->current_file
>= job
->num_files
&& job
->backend
> 0)
3932 * Handle the end of job stuff...
3935 cupsdFinishJob(job
);
3941 * 'update_job_attrs()' - Update the job-printer-* attributes.
3945 update_job_attrs(cupsd_job_t
*job
, /* I - Job to update */
3946 int do_message
)/* I - 1 = update job-printer-state message */
3948 int i
; /* Looping var */
3949 int num_reasons
; /* Actual number of reasons */
3950 const char * const *reasons
; /* Reasons */
3951 static const char *none
= "none", /* "none" */
3957 * Get/create the job-printer-state-* attributes...
3960 if (!job
->printer_message
)
3962 if ((job
->printer_message
= ippFindAttribute(job
->attrs
,
3963 "job-printer-state-message",
3964 IPP_TAG_TEXT
)) == NULL
)
3965 job
->printer_message
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_TEXT
,
3966 "job-printer-state-message",
3970 if (!job
->printer_reasons
)
3971 job
->printer_reasons
= ippFindAttribute(job
->attrs
,
3972 "job-printer-state-reasons",
3976 * If the job isn't printing, return now...
3983 * Otherwise copy the printer-state-message value...
3986 if (job
->printer
->state_message
[0] &&
3987 (do_message
|| !job
->printer_message
->values
[0].string
.text
[0]))
3988 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
),
3989 job
->printer
->state_message
);
3992 * ... and the printer-state-reasons value...
3995 if (job
->printer
->num_reasons
== 0)
3998 reasons
= job
->printer
->state
== IPP_PRINTER_STOPPED
? &paused
: &none
;
4002 num_reasons
= job
->printer
->num_reasons
;
4003 reasons
= (const char * const *)job
->printer
->reasons
;
4006 if (!job
->printer_reasons
|| job
->printer_reasons
->num_values
!= num_reasons
)
4008 ippDeleteAttribute(job
->attrs
, job
->printer_reasons
);
4010 job
->printer_reasons
= ippAddStrings(job
->attrs
,
4011 IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
4012 "job-printer-state-reasons",
4013 num_reasons
, NULL
, NULL
);
4016 for (i
= 0; i
< num_reasons
; i
++)
4017 cupsdSetString(&(job
->printer_reasons
->values
[i
].string
.text
), reasons
[i
]);
4022 * End of "$Id: job.c 7902 2008-09-03 14:20:17Z mike $".