2 * "$Id: job.c 7005 2007-10-01 23:45:48Z mike $"
4 * Job management routines for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2008 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
17 * cupsdAddJob() - Add a new job to the job queue...
18 * cupsdCancelJob() - Cancel the specified print job.
19 * cupsdCancelJobs() - Cancel all jobs for the given
21 * cupsdCheckJobs() - Check the pending jobs and start any if
22 * the destination is available.
23 * cupsdCleanJobs() - Clean out old jobs.
24 * cupsdDeleteJob() - Free all memory used by a job.
25 * cupsdFinishJob() - Finish a job.
26 * cupsdFreeAllJobs() - Free all jobs from memory.
27 * cupsdFindJob() - Find the specified job.
28 * cupsdGetPrinterJobCount() - Get the number of pending, processing,
29 * cupsdGetUserJobCount() - Get the number of pending, processing,
30 * cupsdHoldJob() - Hold the specified job.
31 * cupsdLoadAllJobs() - Load all jobs from disk.
32 * cupsdLoadJob() - Load a single job...
33 * cupsdMoveJob() - Move the specified job to a different
35 * cupsdReleaseJob() - Release the specified job.
36 * cupsdRestartJob() - Restart the specified job.
37 * cupsdSaveAllJobs() - Save a summary of all jobs to disk.
38 * cupsdSaveJob() - Save a job to disk.
39 * cupsdSetJobHoldUntil() - Set the hold time for a job...
40 * cupsdSetJobPriority() - Set the priority of a job, moving it
41 * up/down in the list as needed.
42 * cupsdStopAllJobs() - Stop all print jobs.
43 * cupsdStopJob() - Stop a print job.
44 * cupsdUnloadCompletedJobs() - Flush completed job history from memory.
45 * compare_active_jobs() - Compare the job IDs and priorities of two
47 * compare_jobs() - Compare the job IDs of two jobs.
48 * ipp_length() - Compute the size of the buffer needed to
49 * hold the textual IPP attributes.
50 * load_job_cache() - Load jobs from the job.cache file.
51 * load_next_job_id() - Load the NextJobId value from the
53 * load_request_root() - Load jobs from the RequestRoot directory.
54 * set_time() - Set one of the "time-at-xyz" attributes...
55 * set_hold_until() - Set the hold time and update job-hold-until
57 * start_job() - Start a print job.
58 * unload_job() - Unload a job from memory.
59 * update_job() - Read a status update from a jobs filters.
60 * update_job_attrs() - Update the job-printer-* attributes.
64 * Include necessary headers...
69 #include <cups/backend.h>
77 static mime_filter_t gziptoany_filter
=
79 NULL
, /* Source type */
80 NULL
, /* Destination type */
82 "gziptoany" /* Filter program to run */
90 static int compare_active_jobs(void *first
, void *second
, void *data
);
91 static int compare_jobs(void *first
, void *second
, void *data
);
92 static int ipp_length(ipp_t
*ipp
);
93 static void load_job_cache(const char *filename
);
94 static void load_next_job_id(const char *filename
);
95 static void load_request_root(void);
96 static void set_time(cupsd_job_t
*job
, const char *name
);
97 static void set_hold_until(cupsd_job_t
*job
, time_t holdtime
);
98 static void start_job(cupsd_job_t
*job
, cupsd_printer_t
*printer
);
99 static void unload_job(cupsd_job_t
*job
);
100 static void update_job(cupsd_job_t
*job
);
101 static void update_job_attrs(cupsd_job_t
*job
);
105 * 'cupsdAddJob()' - Add a new job to the job queue...
108 cupsd_job_t
* /* O - New job record */
109 cupsdAddJob(int priority
, /* I - Job priority */
110 const char *dest
) /* I - Job destination */
112 cupsd_job_t
*job
; /* New job record */
115 if ((job
= calloc(sizeof(cupsd_job_t
), 1)) == NULL
)
118 job
->id
= NextJobId
++;
119 job
->priority
= priority
;
120 job
->back_pipes
[0] = -1;
121 job
->back_pipes
[1] = -1;
122 job
->print_pipes
[0] = -1;
123 job
->print_pipes
[1] = -1;
124 job
->side_pipes
[0] = -1;
125 job
->side_pipes
[1] = -1;
126 job
->status_pipes
[0] = -1;
127 job
->status_pipes
[1] = -1;
129 cupsdSetString(&job
->dest
, dest
);
132 * Add the new job to the "all jobs" and "active jobs" lists...
135 cupsArrayAdd(Jobs
, job
);
136 cupsArrayAdd(ActiveJobs
, job
);
143 * 'cupsdCancelJob()' - Cancel the specified print job.
147 cupsdCancelJob(cupsd_job_t
*job
, /* I - Job to cancel */
148 int purge
, /* I - Purge jobs? */
149 ipp_jstate_t newstate
) /* I - New job state */
151 int i
; /* Looping var */
152 char filename
[1024]; /* Job filename */
153 cupsd_printer_t
*printer
; /* Printer used by job */
156 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdCancelJob: id = %d", job
->id
);
159 * Stop any processes that are working on the current job...
162 printer
= job
->printer
;
164 if (job
->state_value
== IPP_JOB_PROCESSING
)
165 cupsdStopJob(job
, 0);
170 job
->state
->values
[0].integer
= newstate
;
172 job
->state_value
= newstate
;
174 set_time(job
, "time-at-completed");
177 * Send any pending notifications and then expire them...
185 case IPP_JOB_CANCELED
:
186 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, printer
, job
,
187 purge
? "Job purged." : "Job canceled.");
190 case IPP_JOB_ABORTED
:
191 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, printer
, job
,
192 "Job aborted; please consult the error_log file "
196 case IPP_JOB_COMPLETED
:
198 * Clear the printer's printer-state-message and move on...
201 printer
->state_message
[0] = '\0';
203 cupsdSetPrinterState(printer
, IPP_PRINTER_IDLE
, 0);
205 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, printer
, job
,
210 cupsdExpireSubscriptions(NULL
, job
);
213 * Remove the job from the active and printing lists...
216 cupsArrayRemove(ActiveJobs
, job
);
217 cupsArrayRemove(PrintingJobs
, job
);
220 * Remove any authentication data...
223 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
224 if (cupsdRemoveFile(filename
) && errno
!= ENOENT
)
225 cupsdLogMessage(CUPSD_LOG_ERROR
,
226 "Unable to remove authentication cache: %s",
229 cupsdClearString(&job
->auth_username
);
230 cupsdClearString(&job
->auth_domain
);
231 cupsdClearString(&job
->auth_password
);
235 * Destroy the credential cache and clear the KRB5CCNAME env var string.
240 krb5_cc_destroy(KerberosContext
, job
->ccache
);
244 cupsdClearString(&job
->ccname
);
245 #endif /* HAVE_GSSAPI */
248 * Remove the print file for good if we aren't preserving jobs or
252 job
->current_file
= 0;
254 if (!JobHistory
|| !JobFiles
|| purge
)
256 for (i
= 1; i
<= job
->num_files
; i
++)
258 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
263 if (job
->num_files
> 0)
265 free(job
->filetypes
);
266 free(job
->compressions
);
269 job
->filetypes
= NULL
;
270 job
->compressions
= NULL
;
274 if (JobHistory
&& !purge
)
277 * Save job state info...
281 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
286 * Remove the job info file...
289 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
,
294 * Remove the job from the "all jobs" list...
297 cupsArrayRemove(Jobs
, job
);
300 * Free all memory used...
311 * 'cupsdCancelJobs()' - Cancel all jobs for the given destination/user...
315 cupsdCancelJobs(const char *dest
, /* I - Destination to cancel */
316 const char *username
, /* I - Username or NULL */
317 int purge
) /* I - Purge jobs? */
319 cupsd_job_t
*job
; /* Current job */
322 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
324 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
326 if (!job
->dest
|| !job
->username
)
329 if (!job
->dest
|| !job
->username
)
332 if ((dest
== NULL
|| !strcmp(job
->dest
, dest
)) &&
333 (username
== NULL
|| !strcmp(job
->username
, username
)))
336 * Cancel all jobs matching this destination/user...
339 cupsdCancelJob(job
, purge
, IPP_JOB_CANCELED
);
348 * 'cupsdCheckJobs()' - Check the pending jobs and start any if the destination
355 cupsd_job_t
*job
; /* Current job in queue */
356 cupsd_printer_t
*printer
, /* Printer destination */
357 *pclass
; /* Printer class destination */
358 ipp_attribute_t
*attr
; /* Job attribute */
361 DEBUG_puts("cupsdCheckJobs()");
363 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
364 "cupsdCheckJobs: %d active jobs, sleeping=%d, reload=%d",
365 cupsArrayCount(ActiveJobs
), Sleeping
, NeedReload
);
367 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
369 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
372 * Start held jobs if they are ready...
375 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
376 "cupsdCheckJobs: Job %d: state_value=%d, loaded=%s",
377 job
->id
, job
->state_value
, job
->attrs
? "yes" : "no");
379 if (job
->state_value
== IPP_JOB_HELD
&&
381 job
->hold_until
< time(NULL
))
383 if (job
->pending_timeout
)
385 /* Add trailing banner as needed */
386 if (cupsdTimeoutJob(job
))
390 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
391 job
->state_value
= IPP_JOB_PENDING
;
393 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
394 IPP_TAG_KEYWORD
)) == NULL
)
395 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
399 attr
->value_tag
= IPP_TAG_KEYWORD
;
400 cupsdSetString(&(attr
->values
[0].string
.text
), "no-hold");
402 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
407 * Start pending jobs if the destination is available...
410 if (job
->state_value
== IPP_JOB_PENDING
&& !NeedReload
&& !Sleeping
)
412 printer
= cupsdFindDest(job
->dest
);
416 (printer
->type
& (CUPS_PRINTER_IMPLICIT
| CUPS_PRINTER_CLASS
)))
419 * If the class is remote, just pass it to the remote server...
424 if (pclass
->state
== IPP_PRINTER_STOPPED
)
426 else if (pclass
->type
& CUPS_PRINTER_REMOTE
)
429 printer
= cupsdFindAvailablePrinter(printer
->name
);
432 if (!printer
&& !pclass
)
435 * Whoa, the printer and/or class for this destination went away;
439 cupsdLogMessage(CUPSD_LOG_WARN
,
440 "[Job %d] Printer/class %s has gone away; canceling job!",
443 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
444 "Job canceled because the destination printer/class has "
447 cupsdCancelJob(job
, 1, IPP_JOB_ABORTED
);
452 * See if the printer is available or remote and not printing a job;
453 * if so, start the job...
459 * Add/update a job-actual-printer-uri attribute for this job
460 * so that we know which printer actually printed the job...
463 if ((attr
= ippFindAttribute(job
->attrs
, "job-actual-printer-uri",
464 IPP_TAG_URI
)) != NULL
)
465 cupsdSetString(&attr
->values
[0].string
.text
, printer
->uri
);
467 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
,
468 "job-actual-printer-uri", NULL
, printer
->uri
);
471 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
474 if ((!(printer
->type
& CUPS_PRINTER_DISCOVERED
) && /* Printer is local */
475 printer
->state
== IPP_PRINTER_IDLE
) || /* and idle */
476 ((printer
->type
& CUPS_PRINTER_DISCOVERED
) && /* Printer is remote */
477 !printer
->job
)) /* and not printing */
480 * Clear any message and reasons for the queue...
483 printer
->state_message
[0] = '\0';
485 cupsdSetPrinterReasons(printer
, "none");
491 start_job(job
, printer
);
500 * 'cupsdCleanJobs()' - Clean out old jobs.
506 cupsd_job_t
*job
; /* Current job */
512 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
513 job
&& cupsArrayCount(Jobs
) >= MaxJobs
;
514 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
515 if (job
->state_value
>= IPP_JOB_CANCELED
)
516 cupsdCancelJob(job
, 1, IPP_JOB_CANCELED
);
521 * 'cupsdDeleteJob()' - Free all memory used by a job.
525 cupsdDeleteJob(cupsd_job_t
*job
) /* I - Job */
527 cupsdClearString(&job
->username
);
528 cupsdClearString(&job
->dest
);
529 cupsdClearString(&job
->auth_username
);
530 cupsdClearString(&job
->auth_domain
);
531 cupsdClearString(&job
->auth_password
);
535 * Destroy the credential cache and clear the KRB5CCNAME env var string.
540 krb5_cc_destroy(KerberosContext
, job
->ccache
);
544 cupsdClearString(&job
->ccname
);
545 #endif /* HAVE_GSSAPI */
547 if (job
->num_files
> 0)
549 free(job
->compressions
);
550 free(job
->filetypes
);
553 ippDelete(job
->attrs
);
560 * 'cupsdFinishJob()' - Finish a job.
564 cupsdFinishJob(cupsd_job_t
*job
) /* I - Job */
566 cupsd_printer_t
*printer
; /* Current printer */
567 ipp_attribute_t
*attr
; /* job-hold-until attribute */
570 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] File %d is complete.",
571 job
->id
, job
->current_file
- 1);
573 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
574 "[Job %d] cupsdFinishJob: job->status is %d",
575 job
->id
, job
->status
);
577 if (job
->status_buffer
&&
578 (job
->status
< 0 || job
->current_file
>= job
->num_files
))
581 * Close the pipe and clear the input bit.
584 cupsdRemoveSelect(job
->status_buffer
->fd
);
586 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
587 "[Job %d] cupsdFinishJob: Closing status pipes [ %d %d ]...",
588 job
->id
, job
->status_pipes
[0], job
->status_pipes
[1]);
590 cupsdClosePipe(job
->status_pipes
);
591 cupsdStatBufDelete(job
->status_buffer
);
593 job
->status_buffer
= NULL
;
596 printer
= job
->printer
;
598 update_job_attrs(job
);
603 * Backend had errors; stop it...
606 int exit_code
; /* Exit code from backend */
610 * Convert the status to an exit code. Due to the way the W* macros are
611 * implemented on MacOS X (bug?), we have to store the exit status in a
612 * variable first and then convert...
615 exit_code
= -job
->status
;
616 if (WIFEXITED(exit_code
))
617 exit_code
= WEXITSTATUS(exit_code
);
619 exit_code
= job
->status
;
621 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Backend returned status %d (%s)",
623 exit_code
== CUPS_BACKEND_FAILED
? "failed" :
624 exit_code
== CUPS_BACKEND_AUTH_REQUIRED
?
625 "authentication required" :
626 exit_code
== CUPS_BACKEND_HOLD
? "hold job" :
627 exit_code
== CUPS_BACKEND_STOP
? "stop printer" :
628 exit_code
== CUPS_BACKEND_CANCEL
? "cancel job" :
629 exit_code
< 0 ? "crashed" : "unknown");
632 * Do what needs to be done...
638 case CUPS_BACKEND_FAILED
:
640 * Backend failure, use the error-policy to determine how to
644 cupsdStopJob(job
, 0);
646 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
649 * Mark the job as pending again - we'll retry on another
653 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
654 job
->state_value
= IPP_JOB_PENDING
;
658 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
661 * If the job was queued to a class, try requeuing it... For
662 * faxes and retry-job queues, hold the current job for 5 minutes.
665 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
667 else if ((printer
->type
& CUPS_PRINTER_FAX
) ||
668 !strcmp(printer
->error_policy
, "retry-job"))
671 * See how many times we've tried to send the job; if more than
672 * the limit, cancel the job.
677 if (job
->tries
>= JobRetryLimit
)
683 cupsdLogMessage(CUPSD_LOG_ERROR
,
684 "[Job %d] Canceling job since it could not be "
685 "sent after %d tries.",
686 job
->id
, JobRetryLimit
);
688 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
693 * Try again in N seconds...
696 set_hold_until(job
, time(NULL
) + JobRetryInterval
);
698 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
,
699 "Job held due to fax errors; please consult "
700 "the error_log file for details.");
701 cupsdSetPrinterState(printer
, IPP_PRINTER_IDLE
, 0);
704 else if (!strcmp(printer
->error_policy
, "abort-job"))
705 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
708 cupsdSetPrinterState(printer
, IPP_PRINTER_STOPPED
, 1);
710 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
711 "Job stopped due to backend errors; please consult "
712 "the error_log file for details.");
716 case CUPS_BACKEND_CANCEL
:
721 cupsdCancelJob(job
, 0, IPP_JOB_CANCELED
);
724 case CUPS_BACKEND_HOLD
:
729 cupsdStopJob(job
, 0);
731 cupsdSetJobHoldUntil(job
, "indefinite");
733 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
734 job
->state_value
= IPP_JOB_HELD
;
737 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
739 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
740 "Job held due to backend errors; please consult "
741 "the error_log file for details.");
744 case CUPS_BACKEND_STOP
:
746 * Stop the printer...
749 cupsdStopJob(job
, 0);
751 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
752 job
->state_value
= IPP_JOB_PENDING
;
755 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
757 cupsdSetPrinterState(printer
, IPP_PRINTER_STOPPED
, 1);
759 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
760 "Job stopped due to backend errors; please consult "
761 "the error_log file for details.");
764 case CUPS_BACKEND_AUTH_REQUIRED
:
765 cupsdStopJob(job
, 0);
767 cupsdSetJobHoldUntil(job
, "auth-info-required");
769 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
770 IPP_TAG_KEYWORD
)) == NULL
)
771 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
775 attr
->value_tag
= IPP_TAG_KEYWORD
;
776 cupsdSetString(&(attr
->values
[0].string
.text
),
777 "auth-info-required");
780 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
781 job
->state_value
= IPP_JOB_HELD
;
784 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
786 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
787 "Authentication is required for job %d.", job
->id
);
792 * Try printing another job...
797 else if (job
->status
> 0)
800 * Filter had errors; stop job...
803 cupsdLogMessage(CUPSD_LOG_ERROR
,
804 "[Job %d] Job stopped due to filter errors.", job
->id
);
805 cupsdStopJob(job
, 1);
807 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
808 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
809 "Job stopped due to filter errors; please consult the "
810 "error_log file for details.");
816 * Job printed successfully; cancel it...
819 if (job
->current_file
< job
->num_files
)
822 * Start the next file in the job...
825 FilterLevel
-= job
->cost
;
826 start_job(job
, printer
);
831 * Close out this job...
834 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Completed successfully.",
836 cupsdCancelJob(job
, 0, IPP_JOB_COMPLETED
);
844 * 'cupsdFreeAllJobs()' - Free all jobs from memory.
848 cupsdFreeAllJobs(void)
850 cupsd_job_t
*job
; /* Current job */
861 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
863 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
865 cupsArrayRemove(Jobs
, job
);
866 cupsArrayRemove(ActiveJobs
, job
);
867 cupsArrayRemove(PrintingJobs
, job
);
872 cupsdReleaseSignals();
877 * 'cupsdFindJob()' - Find the specified job.
880 cupsd_job_t
* /* O - Job data */
881 cupsdFindJob(int id
) /* I - Job ID */
883 cupsd_job_t key
; /* Search key */
888 return ((cupsd_job_t
*)cupsArrayFind(Jobs
, &key
));
893 * 'cupsdGetPrinterJobCount()' - Get the number of pending, processing,
894 * or held jobs in a printer or class.
897 int /* O - Job count */
898 cupsdGetPrinterJobCount(
899 const char *dest
) /* I - Printer or class name */
901 int count
; /* Job count */
902 cupsd_job_t
*job
; /* Current job */
905 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
907 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
908 if (job
->dest
&& !strcasecmp(job
->dest
, dest
))
916 * 'cupsdGetUserJobCount()' - Get the number of pending, processing,
917 * or held jobs for a user.
920 int /* O - Job count */
921 cupsdGetUserJobCount(
922 const char *username
) /* I - Username */
924 int count
; /* Job count */
925 cupsd_job_t
*job
; /* Current job */
928 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
930 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
931 if (!strcasecmp(job
->username
, username
))
939 * 'cupsdHoldJob()' - Hold the specified job.
943 cupsdHoldJob(cupsd_job_t
*job
) /* I - Job data */
945 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdHoldJob: id = %d", job
->id
);
947 if (job
->state_value
== IPP_JOB_PROCESSING
)
948 cupsdStopJob(job
, 0);
952 DEBUG_puts("cupsdHoldJob: setting state to held...");
954 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
955 job
->state_value
= IPP_JOB_HELD
;
956 job
->current_file
= 0;
959 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
966 * 'cupsdLoadAllJobs()' - Load all jobs from disk.
970 cupsdLoadAllJobs(void)
972 char filename
[1024]; /* Full filename of job.cache file */
973 struct stat fileinfo
, /* Information on job.cache file */
974 dirinfo
; /* Information on RequestRoot dir */
979 * Create the job arrays as needed...
983 Jobs
= cupsArrayNew(compare_jobs
, NULL
);
986 ActiveJobs
= cupsArrayNew(compare_active_jobs
, NULL
);
989 PrintingJobs
= cupsArrayNew(compare_jobs
, NULL
);
992 * See whether the job.cache file is older than the RequestRoot directory...
995 snprintf(filename
, sizeof(filename
), "%s/job.cache", CacheDir
);
997 if (stat(filename
, &fileinfo
))
999 fileinfo
.st_mtime
= 0;
1001 if (errno
!= ENOENT
)
1002 cupsdLogMessage(CUPSD_LOG_ERROR
,
1003 "Unable to get file information for \"%s\" - %s",
1004 filename
, strerror(errno
));
1007 if (stat(RequestRoot
, &dirinfo
))
1009 dirinfo
.st_mtime
= 0;
1011 if (errno
!= ENOENT
)
1012 cupsdLogMessage(CUPSD_LOG_ERROR
,
1013 "Unable to get directory information for \"%s\" - %s",
1014 RequestRoot
, strerror(errno
));
1018 * Load the most recent source for job data...
1021 if (dirinfo
.st_mtime
> fileinfo
.st_mtime
)
1023 load_request_root();
1025 load_next_job_id(filename
);
1028 load_job_cache(filename
);
1031 * Clean out old jobs as needed...
1034 if (MaxJobs
> 0 && cupsArrayCount(Jobs
) >= MaxJobs
)
1040 * 'cupsdLoadJob()' - Load a single job...
1044 cupsdLoadJob(cupsd_job_t
*job
) /* I - Job */
1046 char jobfile
[1024]; /* Job filename */
1047 cups_file_t
*fp
; /* Job file */
1048 int fileid
; /* Current file ID */
1049 ipp_attribute_t
*attr
; /* Job attribute */
1050 const char *dest
; /* Destination name */
1051 cupsd_printer_t
*destptr
; /* Pointer to destination */
1052 mime_type_t
**filetypes
; /* New filetypes array */
1053 int *compressions
; /* New compressions array */
1058 if (job
->state_value
> IPP_JOB_STOPPED
)
1059 job
->access_time
= time(NULL
);
1064 if ((job
->attrs
= ippNew()) == NULL
)
1066 cupsdLogMessage(CUPSD_LOG_ERROR
,
1067 "[Job %d] Ran out of memory for job attributes!", job
->id
);
1072 * Load job attributes...
1075 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Loading attributes...", job
->id
);
1077 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, job
->id
);
1078 if ((fp
= cupsFileOpen(jobfile
, "r")) == NULL
)
1080 cupsdLogMessage(CUPSD_LOG_ERROR
,
1081 "[Job %d] Unable to open job control file \"%s\" - %s!",
1082 job
->id
, jobfile
, strerror(errno
));
1083 ippDelete(job
->attrs
);
1088 if (ippReadIO(fp
, (ipp_iocb_t
)cupsFileRead
, 1, NULL
, job
->attrs
) != IPP_DATA
)
1090 cupsdLogMessage(CUPSD_LOG_ERROR
,
1091 "[Job %d] Unable to read job control file \"%s\"!",
1094 ippDelete(job
->attrs
);
1103 * Copy attribute data to the job object...
1106 if ((job
->state
= ippFindAttribute(job
->attrs
, "job-state",
1107 IPP_TAG_ENUM
)) == NULL
)
1109 cupsdLogMessage(CUPSD_LOG_ERROR
,
1110 "[Job %d] Missing or bad job-state attribute in "
1113 ippDelete(job
->attrs
);
1119 job
->state_value
= (ipp_jstate_t
)job
->state
->values
[0].integer
;
1123 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1124 IPP_TAG_URI
)) == NULL
)
1126 cupsdLogMessage(CUPSD_LOG_ERROR
,
1127 "[Job %d] No job-printer-uri attribute in control file!",
1129 ippDelete(job
->attrs
);
1135 if ((dest
= cupsdValidateDest(attr
->values
[0].string
.text
, &(job
->dtype
),
1138 cupsdLogMessage(CUPSD_LOG_ERROR
,
1139 "[Job %d] Unable to queue job for destination \"%s\"!",
1140 job
->id
, attr
->values
[0].string
.text
);
1141 ippDelete(job
->attrs
);
1147 cupsdSetString(&job
->dest
, dest
);
1149 else if ((destptr
= cupsdFindDest(job
->dest
)) == NULL
)
1151 cupsdLogMessage(CUPSD_LOG_ERROR
,
1152 "[Job %d] Unable to queue job for destination \"%s\"!",
1153 job
->id
, job
->dest
);
1154 ippDelete(job
->attrs
);
1160 job
->sheets
= ippFindAttribute(job
->attrs
, "job-media-sheets-completed",
1162 job
->job_sheets
= ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_NAME
);
1166 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
1167 IPP_TAG_INTEGER
)) == NULL
)
1169 cupsdLogMessage(CUPSD_LOG_ERROR
,
1170 "[Job %d] Missing or bad job-priority attribute in "
1171 "control file!", job
->id
);
1172 ippDelete(job
->attrs
);
1178 job
->priority
= attr
->values
[0].integer
;
1183 if ((attr
= ippFindAttribute(job
->attrs
, "job-originating-user-name",
1184 IPP_TAG_NAME
)) == NULL
)
1186 cupsdLogMessage(CUPSD_LOG_ERROR
,
1187 "[Job %d] Missing or bad job-originating-user-name "
1188 "attribute in control file!", job
->id
);
1189 ippDelete(job
->attrs
);
1195 cupsdSetString(&job
->username
, attr
->values
[0].string
.text
);
1199 * Set the job hold-until time and state...
1202 if (job
->state_value
== IPP_JOB_HELD
)
1204 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1205 IPP_TAG_KEYWORD
)) == NULL
)
1206 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1209 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
1212 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1213 job
->state_value
= IPP_JOB_PENDING
;
1216 else if (job
->state_value
== IPP_JOB_PROCESSING
)
1218 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1219 job
->state_value
= IPP_JOB_PENDING
;
1222 if (!job
->num_files
)
1225 * Find all the d##### files...
1228 for (fileid
= 1; fileid
< 10000; fileid
++)
1230 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
1233 if (access(jobfile
, 0))
1236 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1237 "[Job %d] Auto-typing document file \"%s\"...",
1240 if (fileid
> job
->num_files
)
1242 if (job
->num_files
== 0)
1244 compressions
= (int *)calloc(fileid
, sizeof(int));
1245 filetypes
= (mime_type_t
**)calloc(fileid
, sizeof(mime_type_t
*));
1249 compressions
= (int *)realloc(job
->compressions
,
1250 sizeof(int) * fileid
);
1251 filetypes
= (mime_type_t
**)realloc(job
->filetypes
,
1252 sizeof(mime_type_t
*) *
1256 if (!compressions
|| !filetypes
)
1258 cupsdLogMessage(CUPSD_LOG_ERROR
,
1259 "[Job %d] Ran out of memory for job file types!",
1264 job
->compressions
= compressions
;
1265 job
->filetypes
= filetypes
;
1266 job
->num_files
= fileid
;
1269 job
->filetypes
[fileid
- 1] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
1270 job
->compressions
+ fileid
- 1);
1272 if (!job
->filetypes
[fileid
- 1])
1273 job
->filetypes
[fileid
- 1] = mimeType(MimeDatabase
, "application",
1279 * Load authentication information as needed...
1282 if (job
->state_value
< IPP_JOB_STOPPED
)
1284 snprintf(jobfile
, sizeof(jobfile
), "%s/a%05d", RequestRoot
, job
->id
);
1286 cupsdClearString(&job
->auth_username
);
1287 cupsdClearString(&job
->auth_domain
);
1288 cupsdClearString(&job
->auth_password
);
1290 if ((fp
= cupsFileOpen(jobfile
, "r")) != NULL
)
1292 int i
, /* Looping var */
1293 bytes
; /* Size of auth data */
1294 char line
[255], /* Line from file */
1295 data
[255]; /* Decoded data */
1299 i
< destptr
->num_auth_info_required
&&
1300 cupsFileGets(fp
, line
, sizeof(line
));
1303 bytes
= sizeof(data
);
1304 httpDecode64_2(data
, &bytes
, line
);
1306 if (!strcmp(destptr
->auth_info_required
[i
], "username"))
1307 cupsdSetStringf(&job
->auth_username
, "AUTH_USERNAME=%s", data
);
1308 else if (!strcmp(destptr
->auth_info_required
[i
], "domain"))
1309 cupsdSetStringf(&job
->auth_domain
, "AUTH_DOMAIN=%s", data
);
1310 else if (!strcmp(destptr
->auth_info_required
[i
], "password"))
1311 cupsdSetStringf(&job
->auth_password
, "AUTH_PASSWORD=%s", data
);
1317 job
->access_time
= time(NULL
);
1322 * 'cupsdMoveJob()' - Move the specified job to a different destination.
1326 cupsdMoveJob(cupsd_job_t
*job
, /* I - Job */
1327 cupsd_printer_t
*p
) /* I - Destination printer or class */
1329 ipp_attribute_t
*attr
; /* job-printer-uri attribute */
1330 const char *olddest
; /* Old destination */
1331 cupsd_printer_t
*oldp
; /* Old pointer */
1335 * Don't move completed jobs...
1338 if (job
->state_value
> IPP_JOB_STOPPED
)
1342 * Get the old destination...
1345 olddest
= job
->dest
;
1348 oldp
= job
->printer
;
1350 oldp
= cupsdFindDest(olddest
);
1353 * Change the destination information...
1356 if (job
->state_value
== IPP_JOB_PROCESSING
)
1357 cupsdStopJob(job
, 0);
1361 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, oldp
, job
,
1362 "Job #%d moved from %s to %s.", job
->id
, olddest
,
1365 cupsdSetString(&job
->dest
, p
->name
);
1366 job
->dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_REMOTE
|
1367 CUPS_PRINTER_IMPLICIT
);
1369 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1370 IPP_TAG_URI
)) != NULL
)
1371 cupsdSetString(&(attr
->values
[0].string
.text
), p
->uri
);
1373 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, p
, job
,
1374 "Job #%d moved from %s to %s.", job
->id
, olddest
,
1378 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1383 * 'cupsdReleaseJob()' - Release the specified job.
1387 cupsdReleaseJob(cupsd_job_t
*job
) /* I - Job */
1389 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdReleaseJob: id = %d", job
->id
);
1391 if (job
->state_value
== IPP_JOB_HELD
)
1393 DEBUG_puts("cupsdReleaseJob: setting state to pending...");
1395 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1396 job
->state_value
= IPP_JOB_PENDING
;
1398 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1405 * 'cupsdRestartJob()' - Restart the specified job.
1409 cupsdRestartJob(cupsd_job_t
*job
) /* I - Job */
1411 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdRestartJob: id = %d", job
->id
);
1413 if (job
->state_value
== IPP_JOB_STOPPED
|| job
->num_files
)
1415 ipp_jstate_t old_state
; /* Old job state */
1420 old_state
= job
->state_value
;
1423 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1424 job
->state_value
= IPP_JOB_PENDING
;
1427 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1429 if (old_state
> IPP_JOB_STOPPED
)
1430 cupsArrayAdd(ActiveJobs
, job
);
1438 * 'cupsdSaveAllJobs()' - Save a summary of all jobs to disk.
1442 cupsdSaveAllJobs(void)
1444 int i
; /* Looping var */
1445 cups_file_t
*fp
; /* Job cache file */
1446 char temp
[1024]; /* Temporary string */
1447 cupsd_job_t
*job
; /* Current job */
1448 time_t curtime
; /* Current time */
1449 struct tm
*curdate
; /* Current date */
1452 snprintf(temp
, sizeof(temp
), "%s/job.cache", CacheDir
);
1453 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
1455 cupsdLogMessage(CUPSD_LOG_ERROR
,
1456 "Unable to create job cache file \"%s\" - %s",
1457 temp
, strerror(errno
));
1461 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving job cache file \"%s\"...", temp
);
1464 * Restrict access to the file...
1467 fchown(cupsFileNumber(fp
), getuid(), Group
);
1468 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
1471 * Write a small header to the file...
1474 curtime
= time(NULL
);
1475 curdate
= localtime(&curtime
);
1476 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
1478 cupsFilePuts(fp
, "# Job cache file for " CUPS_SVERSION
"\n");
1479 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
1480 cupsFilePrintf(fp
, "NextJobId %d\n", NextJobId
);
1483 * Write each job known to the system...
1486 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1488 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1490 cupsFilePrintf(fp
, "<Job %d>\n", job
->id
);
1491 cupsFilePrintf(fp
, "State %d\n", job
->state_value
);
1492 cupsFilePrintf(fp
, "Priority %d\n", job
->priority
);
1493 cupsFilePrintf(fp
, "Username %s\n", job
->username
);
1494 cupsFilePrintf(fp
, "Destination %s\n", job
->dest
);
1495 cupsFilePrintf(fp
, "DestType %d\n", job
->dtype
);
1496 cupsFilePrintf(fp
, "NumFiles %d\n", job
->num_files
);
1497 for (i
= 0; i
< job
->num_files
; i
++)
1498 cupsFilePrintf(fp
, "File %d %s/%s %d\n", i
+ 1, job
->filetypes
[i
]->super
,
1499 job
->filetypes
[i
]->type
, job
->compressions
[i
]);
1500 cupsFilePuts(fp
, "</Job>\n");
1508 * 'cupsdSaveJob()' - Save a job to disk.
1512 cupsdSaveJob(cupsd_job_t
*job
) /* I - Job */
1514 char filename
[1024]; /* Job control filename */
1515 cups_file_t
*fp
; /* Job file */
1518 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSaveJob(job=%p(%d)): job->attrs=%p",
1519 job
, job
->id
, job
->attrs
);
1521 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
, job
->id
);
1523 if ((fp
= cupsFileOpen(filename
, "w")) == NULL
)
1525 cupsdLogMessage(CUPSD_LOG_ERROR
,
1526 "[Job %d] Unable to create job control file \"%s\" - %s.",
1527 job
->id
, filename
, strerror(errno
));
1531 fchmod(cupsFileNumber(fp
), 0600);
1532 fchown(cupsFileNumber(fp
), RunUser
, Group
);
1534 job
->attrs
->state
= IPP_IDLE
;
1536 if (ippWriteIO(fp
, (ipp_iocb_t
)cupsFileWrite
, 1, NULL
,
1537 job
->attrs
) != IPP_DATA
)
1538 cupsdLogMessage(CUPSD_LOG_ERROR
,
1539 "[Job %d] Unable to write job control file!", job
->id
);
1548 * 'cupsdSetJobHoldUntil()' - Set the hold time for a job...
1552 cupsdSetJobHoldUntil(cupsd_job_t
*job
, /* I - Job */
1553 const char *when
) /* I - When to resume */
1555 time_t curtime
; /* Current time */
1556 struct tm
*curdate
; /* Current date */
1557 int hour
; /* Hold hour */
1558 int minute
; /* Hold minute */
1559 int second
; /* Hold second */
1562 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetJobHoldUntil(%d, \"%s\")",
1567 if (!strcmp(when
, "indefinite") || !strcmp(when
, "auth-info-required"))
1570 * Hold indefinitely...
1573 job
->hold_until
= 0;
1575 else if (!strcmp(when
, "day-time"))
1578 * Hold to 6am the next morning unless local time is < 6pm.
1581 curtime
= time(NULL
);
1582 curdate
= localtime(&curtime
);
1584 if (curdate
->tm_hour
< 18)
1585 job
->hold_until
= curtime
;
1587 job
->hold_until
= curtime
+
1588 ((29 - curdate
->tm_hour
) * 60 + 59 -
1589 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1591 else if (!strcmp(when
, "evening") || !strcmp(when
, "night"))
1594 * Hold to 6pm unless local time is > 6pm or < 6am.
1597 curtime
= time(NULL
);
1598 curdate
= localtime(&curtime
);
1600 if (curdate
->tm_hour
< 6 || curdate
->tm_hour
>= 18)
1601 job
->hold_until
= curtime
;
1603 job
->hold_until
= curtime
+
1604 ((17 - curdate
->tm_hour
) * 60 + 59 -
1605 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1607 else if (!strcmp(when
, "second-shift"))
1610 * Hold to 4pm unless local time is > 4pm.
1613 curtime
= time(NULL
);
1614 curdate
= localtime(&curtime
);
1616 if (curdate
->tm_hour
>= 16)
1617 job
->hold_until
= curtime
;
1619 job
->hold_until
= curtime
+
1620 ((15 - curdate
->tm_hour
) * 60 + 59 -
1621 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1623 else if (!strcmp(when
, "third-shift"))
1626 * Hold to 12am unless local time is < 8am.
1629 curtime
= time(NULL
);
1630 curdate
= localtime(&curtime
);
1632 if (curdate
->tm_hour
< 8)
1633 job
->hold_until
= curtime
;
1635 job
->hold_until
= curtime
+
1636 ((23 - curdate
->tm_hour
) * 60 + 59 -
1637 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1639 else if (!strcmp(when
, "weekend"))
1642 * Hold to weekend unless we are in the weekend.
1645 curtime
= time(NULL
);
1646 curdate
= localtime(&curtime
);
1648 if (curdate
->tm_wday
|| curdate
->tm_wday
== 6)
1649 job
->hold_until
= curtime
;
1651 job
->hold_until
= curtime
+
1652 (((5 - curdate
->tm_wday
) * 24 +
1653 (17 - curdate
->tm_hour
)) * 60 + 59 -
1654 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1656 else if (sscanf(when
, "%d:%d:%d", &hour
, &minute
, &second
) >= 2)
1659 * Hold to specified GMT time (HH:MM or HH:MM:SS)...
1662 curtime
= time(NULL
);
1663 curdate
= gmtime(&curtime
);
1665 job
->hold_until
= curtime
+
1666 ((hour
- curdate
->tm_hour
) * 60 + minute
-
1667 curdate
->tm_min
) * 60 + second
- curdate
->tm_sec
;
1670 * Hold until next day as needed...
1673 if (job
->hold_until
< curtime
)
1674 job
->hold_until
+= 24 * 60 * 60;
1677 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetJobHoldUntil: hold_until = %d",
1678 (int)job
->hold_until
);
1683 * 'cupsdSetJobPriority()' - Set the priority of a job, moving it up/down in
1684 * the list as needed.
1688 cupsdSetJobPriority(
1689 cupsd_job_t
*job
, /* I - Job ID */
1690 int priority
) /* I - New priority (0 to 100) */
1692 ipp_attribute_t
*attr
; /* Job attribute */
1696 * Don't change completed jobs...
1699 if (job
->state_value
>= IPP_JOB_PROCESSING
)
1703 * Set the new priority and re-add the job into the active list...
1706 cupsArrayRemove(ActiveJobs
, job
);
1708 job
->priority
= priority
;
1710 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
1711 IPP_TAG_INTEGER
)) != NULL
)
1712 attr
->values
[0].integer
= priority
;
1714 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-priority",
1717 cupsArrayAdd(ActiveJobs
, job
);
1720 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1725 * 'cupsdStopAllJobs()' - Stop all print jobs.
1729 cupsdStopAllJobs(int force
) /* I - 1 = Force all filters to stop */
1731 cupsd_job_t
*job
; /* Current job */
1734 DEBUG_puts("cupsdStopAllJobs()");
1736 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
1738 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
1739 if (job
->state_value
== IPP_JOB_PROCESSING
)
1741 cupsdStopJob(job
, force
);
1742 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1743 job
->state_value
= IPP_JOB_PENDING
;
1749 * 'cupsdStopJob()' - Stop a print job.
1753 cupsdStopJob(cupsd_job_t
*job
, /* I - Job */
1754 int force
) /* I - 1 = Force all filters to stop */
1756 int i
; /* Looping var */
1759 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1760 "[Job %d] cupsdStopJob: force = %d", job
->id
, force
);
1762 if (job
->state_value
!= IPP_JOB_PROCESSING
)
1765 FilterLevel
-= job
->cost
;
1767 if (job
->printer
->state
== IPP_PRINTER_PROCESSING
)
1768 cupsdSetPrinterState(job
->printer
, IPP_PRINTER_IDLE
, 0);
1770 job
->state
->values
[0].integer
= IPP_JOB_STOPPED
;
1771 job
->state_value
= IPP_JOB_STOPPED
;
1772 job
->printer
->job
= NULL
;
1773 job
->printer
= NULL
;
1775 job
->current_file
--;
1777 for (i
= 0; job
->filters
[i
]; i
++)
1778 if (job
->filters
[i
] > 0)
1780 cupsdEndProcess(job
->filters
[i
], force
);
1781 job
->filters
[i
] = 0;
1784 if (job
->backend
> 0)
1786 cupsdEndProcess(job
->backend
, force
);
1790 cupsdDestroyProfile(job
->profile
);
1791 job
->profile
= NULL
;
1793 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] Closing print pipes [ %d %d ]...",
1794 job
->id
, job
->print_pipes
[0], job
->print_pipes
[1]);
1796 cupsdClosePipe(job
->print_pipes
);
1798 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] Closing back pipes [ %d %d ]...",
1799 job
->id
, job
->back_pipes
[0], job
->back_pipes
[1]);
1801 cupsdClosePipe(job
->back_pipes
);
1803 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] Closing side pipes [ %d %d ]...",
1804 job
->id
, job
->side_pipes
[0], job
->side_pipes
[1]);
1806 cupsdClosePipe(job
->side_pipes
);
1808 if (job
->status_buffer
)
1811 * Close the pipe and clear the input bit.
1814 cupsdRemoveSelect(job
->status_buffer
->fd
);
1816 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1817 "[Job %d] Closing status pipes [ %d %d ]...", job
->id
,
1818 job
->status_pipes
[0], job
->status_pipes
[1]);
1820 cupsdClosePipe(job
->status_pipes
);
1821 cupsdStatBufDelete(job
->status_buffer
);
1823 job
->status_buffer
= NULL
;
1829 * 'cupsdUnloadCompletedJobs()' - Flush completed job history from memory.
1833 cupsdUnloadCompletedJobs(void)
1835 cupsd_job_t
*job
; /* Current job */
1836 time_t expire
; /* Expiration time */
1839 expire
= time(NULL
) - 60;
1841 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1843 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1844 if (job
->attrs
&& job
->state_value
>= IPP_JOB_STOPPED
&&
1845 job
->access_time
< expire
)
1856 * 'compare_active_jobs()' - Compare the job IDs and priorities of two jobs.
1859 static int /* O - Difference */
1860 compare_active_jobs(void *first
, /* I - First job */
1861 void *second
, /* I - Second job */
1862 void *data
) /* I - App data (not used) */
1864 int diff
; /* Difference */
1867 if ((diff
= ((cupsd_job_t
*)second
)->priority
-
1868 ((cupsd_job_t
*)first
)->priority
) != 0)
1871 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
1876 * 'compare_jobs()' - Compare the job IDs of two jobs.
1879 static int /* O - Difference */
1880 compare_jobs(void *first
, /* I - First job */
1881 void *second
, /* I - Second job */
1882 void *data
) /* I - App data (not used) */
1884 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
1889 * 'ipp_length()' - Compute the size of the buffer needed to hold
1890 * the textual IPP attributes.
1893 static int /* O - Size of attribute buffer */
1894 ipp_length(ipp_t
*ipp
) /* I - IPP request */
1896 int bytes
; /* Number of bytes */
1897 int i
; /* Looping var */
1898 ipp_attribute_t
*attr
; /* Current attribute */
1902 * Loop through all attributes...
1907 for (attr
= ipp
->attrs
; attr
!= NULL
; attr
= attr
->next
)
1910 * Skip attributes that won't be sent to filters...
1913 if (attr
->value_tag
== IPP_TAG_MIMETYPE
||
1914 attr
->value_tag
== IPP_TAG_NAMELANG
||
1915 attr
->value_tag
== IPP_TAG_TEXTLANG
||
1916 attr
->value_tag
== IPP_TAG_URI
||
1917 attr
->value_tag
== IPP_TAG_URISCHEME
)
1920 if (strncmp(attr
->name
, "time-", 5) == 0)
1924 * Add space for a leading space and commas between each value.
1925 * For the first attribute, the leading space isn't used, so the
1926 * extra byte can be used as the nul terminator...
1929 bytes
++; /* " " separator */
1930 bytes
+= attr
->num_values
; /* "," separators */
1933 * Boolean attributes appear as "foo,nofoo,foo,nofoo", while
1934 * other attributes appear as "foo=value1,value2,...,valueN".
1937 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
1938 bytes
+= strlen(attr
->name
);
1940 bytes
+= attr
->num_values
* strlen(attr
->name
);
1943 * Now add the size required for each value in the attribute...
1946 switch (attr
->value_tag
)
1948 case IPP_TAG_INTEGER
:
1951 * Minimum value of a signed integer is -2147483647, or 11 digits.
1954 bytes
+= attr
->num_values
* 11;
1957 case IPP_TAG_BOOLEAN
:
1959 * Add two bytes for each false ("no") value...
1962 for (i
= 0; i
< attr
->num_values
; i
++)
1963 if (!attr
->values
[i
].boolean
)
1967 case IPP_TAG_RANGE
:
1969 * A range is two signed integers separated by a hyphen, or
1970 * 23 characters max.
1973 bytes
+= attr
->num_values
* 23;
1976 case IPP_TAG_RESOLUTION
:
1978 * A resolution is two signed integers separated by an "x" and
1979 * suffixed by the units, or 26 characters max.
1982 bytes
+= attr
->num_values
* 26;
1985 case IPP_TAG_STRING
:
1988 case IPP_TAG_KEYWORD
:
1989 case IPP_TAG_CHARSET
:
1990 case IPP_TAG_LANGUAGE
:
1993 * Strings can contain characters that need quoting. We need
1994 * at least 2 * len + 2 characters to cover the quotes and
1995 * any backslashes in the string.
1998 for (i
= 0; i
< attr
->num_values
; i
++)
1999 bytes
+= 2 * strlen(attr
->values
[i
].string
.text
) + 2;
2003 break; /* anti-compiler-warning-code */
2012 * 'load_job_cache()' - Load jobs from the job.cache file.
2016 load_job_cache(const char *filename
) /* I - job.cache filename */
2018 cups_file_t
*fp
; /* job.cache file */
2019 char line
[1024], /* Line buffer */
2020 *value
; /* Value on line */
2021 int linenum
; /* Line number in file */
2022 cupsd_job_t
*job
; /* Current job */
2023 int jobid
; /* Job ID */
2024 char jobfile
[1024]; /* Job filename */
2028 * Open the job.cache file...
2031 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
2033 if (errno
!= ENOENT
)
2034 cupsdLogMessage(CUPSD_LOG_ERROR
,
2035 "Unable to open job cache file \"%s\": %s",
2036 filename
, strerror(errno
));
2038 load_request_root();
2044 * Read entries from the job cache file and create jobs as needed.
2047 cupsdLogMessage(CUPSD_LOG_INFO
, "Loading job cache file \"%s\"...",
2053 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
2055 if (!strcasecmp(line
, "NextJobId"))
2058 NextJobId
= atoi(value
);
2060 else if (!strcasecmp(line
, "<Job"))
2064 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing </Job> directive on line %d!",
2071 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing job ID on line %d!", linenum
);
2075 jobid
= atoi(value
);
2079 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad job ID %d on line %d!", jobid
,
2084 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, jobid
);
2085 if (access(jobfile
, 0))
2087 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Job %d] Files have gone away!",
2092 job
= calloc(1, sizeof(cupsd_job_t
));
2095 cupsdLogMessage(CUPSD_LOG_EMERG
,
2096 "[Job %d] Unable to allocate memory for job!", jobid
);
2101 job
->back_pipes
[0] = -1;
2102 job
->back_pipes
[1] = -1;
2103 job
->print_pipes
[0] = -1;
2104 job
->print_pipes
[1] = -1;
2105 job
->side_pipes
[0] = -1;
2106 job
->side_pipes
[1] = -1;
2107 job
->status_pipes
[0] = -1;
2108 job
->status_pipes
[1] = -1;
2110 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Loading from cache...", job
->id
);
2114 cupsdLogMessage(CUPSD_LOG_ERROR
,
2115 "Missing <Job #> directive on line %d!", linenum
);
2118 else if (!strcasecmp(line
, "</Job>"))
2120 cupsArrayAdd(Jobs
, job
);
2122 if (job
->state_value
<= IPP_JOB_STOPPED
)
2124 cupsArrayAdd(ActiveJobs
, job
);
2132 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing value on line %d!", linenum
);
2135 else if (!strcasecmp(line
, "State"))
2137 job
->state_value
= (ipp_jstate_t
)atoi(value
);
2139 if (job
->state_value
< IPP_JOB_PENDING
)
2140 job
->state_value
= IPP_JOB_PENDING
;
2141 else if (job
->state_value
> IPP_JOB_COMPLETED
)
2142 job
->state_value
= IPP_JOB_COMPLETED
;
2144 else if (!strcasecmp(line
, "Priority"))
2146 job
->priority
= atoi(value
);
2148 else if (!strcasecmp(line
, "Username"))
2150 cupsdSetString(&job
->username
, value
);
2152 else if (!strcasecmp(line
, "Destination"))
2154 cupsdSetString(&job
->dest
, value
);
2156 else if (!strcasecmp(line
, "DestType"))
2158 job
->dtype
= (cups_ptype_t
)atoi(value
);
2160 else if (!strcasecmp(line
, "NumFiles"))
2162 job
->num_files
= atoi(value
);
2164 if (job
->num_files
< 0)
2166 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad NumFiles value %d on line %d!",
2167 job
->num_files
, linenum
);
2172 if (job
->num_files
> 0)
2174 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-001", RequestRoot
,
2176 if (access(jobfile
, 0))
2178 cupsdLogMessage(CUPSD_LOG_INFO
,
2179 "[Job %d] Data files have gone away!", job
->id
);
2184 job
->filetypes
= calloc(job
->num_files
, sizeof(mime_type_t
*));
2185 job
->compressions
= calloc(job
->num_files
, sizeof(int));
2187 if (!job
->filetypes
|| !job
->compressions
)
2189 cupsdLogMessage(CUPSD_LOG_EMERG
,
2190 "[Job %d] Unable to allocate memory for %d files!",
2191 job
->id
, job
->num_files
);
2196 else if (!strcasecmp(line
, "File"))
2198 int number
, /* File number */
2199 compression
; /* Compression value */
2200 char super
[MIME_MAX_SUPER
], /* MIME super type */
2201 type
[MIME_MAX_TYPE
]; /* MIME type */
2204 if (sscanf(value
, "%d%*[ \t]%15[^/]/%255s%d", &number
, super
, type
,
2207 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File on line %d!", linenum
);
2211 if (number
< 1 || number
> job
->num_files
)
2213 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File number %d on line %d!",
2220 job
->compressions
[number
] = compression
;
2221 job
->filetypes
[number
] = mimeType(MimeDatabase
, super
, type
);
2223 if (!job
->filetypes
[number
])
2226 * If the original MIME type is unknown, auto-type it!
2229 cupsdLogMessage(CUPSD_LOG_ERROR
,
2230 "[Job %d] Unknown MIME type %s/%s for file %d!",
2231 job
->id
, super
, type
, number
+ 1);
2233 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
2234 job
->id
, number
+ 1);
2235 job
->filetypes
[number
] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
2236 job
->compressions
+ number
);
2239 * If that didn't work, assume it is raw...
2242 if (!job
->filetypes
[number
])
2243 job
->filetypes
[number
] = mimeType(MimeDatabase
, "application",
2248 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unknown %s directive on line %d!",
2257 * 'load_next_job_id()' - Load the NextJobId value from the job.cache file.
2261 load_next_job_id(const char *filename
) /* I - job.cache filename */
2263 cups_file_t
*fp
; /* job.cache file */
2264 char line
[1024], /* Line buffer */
2265 *value
; /* Value on line */
2266 int linenum
; /* Line number in file */
2267 int next_job_id
; /* NextJobId value from line */
2271 * Read the NextJobId directive from the job.cache file and use
2272 * the value (if any).
2275 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
2277 if (errno
!= ENOENT
)
2278 cupsdLogMessage(CUPSD_LOG_ERROR
,
2279 "Unable to open job cache file \"%s\": %s",
2280 filename
, strerror(errno
));
2285 cupsdLogMessage(CUPSD_LOG_INFO
,
2286 "Loading NextJobId from job cache file \"%s\"...", filename
);
2290 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
2292 if (!strcasecmp(line
, "NextJobId"))
2296 next_job_id
= atoi(value
);
2298 if (next_job_id
> NextJobId
)
2299 NextJobId
= next_job_id
;
2310 * 'load_request_root()' - Load jobs from the RequestRoot directory.
2314 load_request_root(void)
2316 cups_dir_t
*dir
; /* Directory */
2317 cups_dentry_t
*dent
; /* Directory entry */
2318 cupsd_job_t
*job
; /* New job */
2322 * Open the requests directory...
2325 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Scanning %s for jobs...", RequestRoot
);
2327 if ((dir
= cupsDirOpen(RequestRoot
)) == NULL
)
2329 cupsdLogMessage(CUPSD_LOG_ERROR
,
2330 "Unable to open spool directory \"%s\": %s",
2331 RequestRoot
, strerror(errno
));
2336 * Read all the c##### files...
2339 while ((dent
= cupsDirRead(dir
)) != NULL
)
2340 if (strlen(dent
->filename
) >= 6 && dent
->filename
[0] == 'c')
2343 * Allocate memory for the job...
2346 if ((job
= calloc(sizeof(cupsd_job_t
), 1)) == NULL
)
2348 cupsdLogMessage(CUPSD_LOG_ERROR
, "Ran out of memory for jobs!");
2354 * Assign the job ID...
2357 job
->id
= atoi(dent
->filename
+ 1);
2358 job
->back_pipes
[0] = -1;
2359 job
->back_pipes
[1] = -1;
2360 job
->print_pipes
[0] = -1;
2361 job
->print_pipes
[1] = -1;
2362 job
->side_pipes
[0] = -1;
2363 job
->side_pipes
[1] = -1;
2364 job
->status_pipes
[0] = -1;
2365 job
->status_pipes
[1] = -1;
2367 if (job
->id
>= NextJobId
)
2368 NextJobId
= job
->id
+ 1;
2377 * Insert the job into the array, sorting by job priority and ID...
2380 cupsArrayAdd(Jobs
, job
);
2382 if (job
->state_value
<= IPP_JOB_STOPPED
)
2383 cupsArrayAdd(ActiveJobs
, job
);
2393 * 'set_time()' - Set one of the "time-at-xyz" attributes...
2397 set_time(cupsd_job_t
*job
, /* I - Job to update */
2398 const char *name
) /* I - Name of attribute */
2400 ipp_attribute_t
*attr
; /* Time attribute */
2403 if ((attr
= ippFindAttribute(job
->attrs
, name
, IPP_TAG_ZERO
)) != NULL
)
2405 attr
->value_tag
= IPP_TAG_INTEGER
;
2406 attr
->values
[0].integer
= time(NULL
);
2412 * 'set_hold_until()' - Set the hold time and update job-hold-until attribute...
2416 set_hold_until(cupsd_job_t
*job
, /* I - Job to update */
2417 time_t holdtime
) /* I - Hold until time */
2419 ipp_attribute_t
*attr
; /* job-hold-until attribute */
2420 struct tm
*holddate
; /* Hold date */
2421 char holdstr
[64]; /* Hold time */
2425 * Set the hold_until value and hold the job...
2428 cupsdLogMessage(CUPSD_LOG_DEBUG
, "set_hold_until: hold_until = %d",
2431 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
2432 job
->state_value
= IPP_JOB_HELD
;
2433 job
->hold_until
= holdtime
;
2436 * Update the job-hold-until attribute with a string representing GMT
2437 * time (HH:MM:SS)...
2440 holddate
= gmtime(&holdtime
);
2441 snprintf(holdstr
, sizeof(holdstr
), "%d:%d:%d", holddate
->tm_hour
,
2442 holddate
->tm_min
, holddate
->tm_sec
);
2444 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2445 IPP_TAG_KEYWORD
)) == NULL
)
2446 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2449 * Either add the attribute or update the value of the existing one
2453 attr
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
2454 "job-hold-until", NULL
, holdstr
);
2456 cupsdSetString(&attr
->values
[0].string
.text
, holdstr
);
2459 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2464 * 'start_job()' - Start a print job.
2468 start_job(cupsd_job_t
*job
, /* I - Job ID */
2469 cupsd_printer_t
*printer
) /* I - Printer to print job */
2471 int i
; /* Looping var */
2472 int slot
; /* Pipe slot */
2473 cups_array_t
*filters
, /* Filters for job */
2474 *prefilters
; /* Filters with prefilters */
2475 mime_filter_t
*filter
, /* Current filter */
2476 *prefilter
, /* Prefilter */
2477 port_monitor
; /* Port monitor filter */
2478 char method
[255], /* Method for output */
2479 *optptr
, /* Pointer to options */
2480 *valptr
; /* Pointer in value string */
2481 ipp_attribute_t
*attr
; /* Current attribute */
2482 struct stat backinfo
; /* Backend file information */
2483 int backroot
; /* Run backend as root? */
2484 int pid
; /* Process ID of new filter process */
2485 int banner_page
; /* 1 if banner page, 0 otherwise */
2486 int filterfds
[2][2];/* Pipes used between filters */
2487 int envc
; /* Number of environment variables */
2488 char **argv
, /* Filter command-line arguments */
2489 sani_uri
[1024], /* Sanitized DEVICE_URI env var */
2490 filename
[1024], /* Job filename */
2491 command
[1024], /* Full path to command */
2492 jobid
[255], /* Job ID string */
2493 title
[IPP_MAX_NAME
],
2494 /* Job title string */
2495 copies
[255], /* # copies string */
2496 *envp
[MAX_ENV
+ 16],
2497 /* Environment variables */
2498 charset
[255], /* CHARSET env variable */
2499 class_name
[255],/* CLASS env variable */
2500 classification
[1024],
2501 /* CLASSIFICATION env variable */
2503 /* CONTENT_TYPE env variable */
2505 /* DEVICE_URI env variable */
2506 final_content_type
[1024],
2507 /* FINAL_CONTENT_TYPE env variable */
2508 lang
[255], /* LANG env variable */
2510 apple_language
[255],
2511 /* APPLE_LANGUAGE env variable */
2512 #endif /* __APPLE__ */
2513 ppd
[1024], /* PPD env variable */
2515 /* PRINTER env variable */
2517 /* RIP_MAX_CACHE env variable */
2518 static char *options
= NULL
;/* Full list of options */
2519 static int optlength
= 0; /* Length of option buffer */
2522 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] start_job: file = %d/%d",
2523 job
->id
, job
->current_file
, job
->num_files
);
2525 if (job
->num_files
== 0)
2527 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Job %d] No files, canceling job!",
2530 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2535 * Figure out what filters are required to convert from
2536 * the source to the destination type...
2545 * Remote jobs and raw queues go directly to the printer without
2549 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2550 "[Job %d] Sending job to queue tagged as raw...", job
->id
);
2557 * Local jobs get filtered...
2560 filters
= mimeFilter(MimeDatabase
, job
->filetypes
[job
->current_file
],
2561 printer
->filetype
, &(job
->cost
));
2565 cupsdLogMessage(CUPSD_LOG_ERROR
,
2566 "[Job %d] Unable to convert file %d to printable format!",
2567 job
->current_file
, job
->id
);
2568 cupsdLogMessage(CUPSD_LOG_INFO
,
2569 "Hint: Do you have Ghostscript installed?");
2571 if (LogLevel
< CUPSD_LOG_DEBUG
)
2572 cupsdLogMessage(CUPSD_LOG_INFO
,
2573 "Hint: Try setting the LogLevel to \"debug\".");
2575 job
->current_file
++;
2577 if (job
->current_file
== job
->num_files
)
2578 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2584 * Remove NULL ("-") filters...
2587 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
2589 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
2590 if (!strcmp(filter
->filter
, "-"))
2591 cupsArrayRemove(filters
, filter
);
2593 if (cupsArrayCount(filters
) == 0)
2595 cupsArrayDelete(filters
);
2600 * If this printer has any pre-filters, insert the required pre-filter
2601 * in the filters array...
2604 if (printer
->prefiltertype
&& filters
)
2606 prefilters
= cupsArrayNew(NULL
, NULL
);
2608 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
2610 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
2612 if ((prefilter
= mimeFilterLookup(MimeDatabase
, filter
->src
,
2613 printer
->prefiltertype
)))
2615 cupsArrayAdd(prefilters
, prefilter
);
2616 job
->cost
+= prefilter
->cost
;
2619 cupsArrayAdd(prefilters
, filter
);
2622 cupsArrayDelete(filters
);
2623 filters
= prefilters
;
2628 * Set a minimum cost of 100 for all jobs so that FilterLimit
2629 * works with raw queues and other low-cost paths.
2632 if (job
->cost
< 100)
2636 * See if the filter cost is too high...
2639 if ((FilterLevel
+ job
->cost
) > FilterLimit
&& FilterLevel
> 0 &&
2643 * Don't print this job quite yet...
2646 cupsArrayDelete(filters
);
2648 cupsdLogMessage(CUPSD_LOG_INFO
,
2649 "[Job %d] Holding because filter limit has been reached.",
2651 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2652 "[Job %d] start_job: file=%d, cost=%d, level=%d, limit=%d",
2653 job
->id
, job
->current_file
, job
->cost
, FilterLevel
,
2658 FilterLevel
+= job
->cost
;
2661 * Add decompression/raw filter as needed...
2664 if ((!printer
->raw
&& job
->compressions
[job
->current_file
]) ||
2665 (!filters
&& !printer
->remote
&&
2666 (job
->num_files
> 1 || !strncmp(printer
->device_uri
, "file:", 5))))
2669 * Add gziptoany filter to the front of the list...
2673 filters
= cupsArrayNew(NULL
, NULL
);
2675 if (!cupsArrayInsert(filters
, &gziptoany_filter
))
2677 cupsdLogMessage(CUPSD_LOG_ERROR
,
2678 "[Job %d] Unable to add decompression filter - %s",
2679 job
->id
, strerror(errno
));
2681 cupsArrayDelete(filters
);
2683 job
->current_file
++;
2685 if (job
->current_file
== job
->num_files
)
2686 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2693 * Add port monitor, if any...
2696 if (printer
->port_monitor
)
2699 * Add port monitor to the end of the list...
2703 filters
= cupsArrayNew(NULL
, NULL
);
2705 port_monitor
.src
= NULL
;
2706 port_monitor
.dst
= NULL
;
2707 port_monitor
.cost
= 0;
2709 snprintf(port_monitor
.filter
, sizeof(port_monitor
.filter
),
2710 "%s/monitor/%s", ServerBin
, printer
->port_monitor
);
2712 if (!cupsArrayAdd(filters
, &port_monitor
))
2714 cupsdLogMessage(CUPSD_LOG_ERROR
,
2715 "[Job %d] Unable to add port monitor - %s",
2716 job
->id
, strerror(errno
));
2718 cupsArrayDelete(filters
);
2720 job
->current_file
++;
2722 if (job
->current_file
== job
->num_files
)
2723 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2730 * Make sure we don't go over the "MAX_FILTERS" limit...
2733 if (cupsArrayCount(filters
) > MAX_FILTERS
)
2735 cupsdLogMessage(CUPSD_LOG_ERROR
,
2736 "[Job %d] Too many filters (%d > %d), unable to print!",
2737 job
->id
, cupsArrayCount(filters
), MAX_FILTERS
);
2739 cupsArrayDelete(filters
);
2740 cupsdCancelJob(job
, 0, IPP_JOB_STOPPED
);
2746 * Update the printer and job state to "processing"...
2749 job
->state
->values
[0].integer
= IPP_JOB_PROCESSING
;
2750 job
->state_value
= IPP_JOB_PROCESSING
;
2753 job
->printer
= printer
;
2756 cupsdSetPrinterState(printer
, IPP_PRINTER_PROCESSING
, 0);
2758 if (job
->current_file
== 0)
2761 * Add to the printing list...
2764 cupsArrayAdd(PrintingJobs
, job
);
2765 cupsdSetBusyState();
2768 * Set the processing time...
2771 set_time(job
, "time-at-processing");
2774 * Create the backchannel pipes and make them non-blocking...
2777 cupsdOpenPipe(job
->back_pipes
);
2779 fcntl(job
->back_pipes
[0], F_SETFL
,
2780 fcntl(job
->back_pipes
[0], F_GETFL
) | O_NONBLOCK
);
2782 fcntl(job
->back_pipes
[1], F_SETFL
,
2783 fcntl(job
->back_pipes
[1], F_GETFL
) | O_NONBLOCK
);
2786 * Create the side-channel pipes and make them non-blocking...
2789 socketpair(AF_LOCAL
, SOCK_STREAM
, 0, job
->side_pipes
);
2791 fcntl(job
->side_pipes
[0], F_SETFL
,
2792 fcntl(job
->side_pipes
[0], F_GETFL
) | O_NONBLOCK
);
2794 fcntl(job
->side_pipes
[1], F_SETFL
,
2795 fcntl(job
->side_pipes
[1], F_GETFL
) | O_NONBLOCK
);
2799 * Determine if we are printing a banner page or not...
2802 if (job
->job_sheets
== NULL
)
2804 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] No job-sheets attribute.",
2806 if ((job
->job_sheets
=
2807 ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_ZERO
)) != NULL
)
2808 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2809 "[Job %d] ... but someone added one without setting "
2810 "job_sheets!", job
->id
);
2812 else if (job
->job_sheets
->num_values
== 1)
2813 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] job-sheets=%s", job
->id
,
2814 job
->job_sheets
->values
[0].string
.text
);
2816 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] job-sheets=%s,%s", job
->id
,
2817 job
->job_sheets
->values
[0].string
.text
,
2818 job
->job_sheets
->values
[1].string
.text
);
2820 if (printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
))
2822 else if (job
->job_sheets
== NULL
)
2824 else if (strcasecmp(job
->job_sheets
->values
[0].string
.text
, "none") != 0 &&
2825 job
->current_file
== 0)
2827 else if (job
->job_sheets
->num_values
> 1 &&
2828 strcasecmp(job
->job_sheets
->values
[1].string
.text
, "none") != 0 &&
2829 job
->current_file
== (job
->num_files
- 1))
2834 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] banner_page = %d", job
->id
,
2838 * Building the options string is harder than it needs to be, but
2839 * for the moment we need to pass strings for command-line args and
2840 * not IPP attribute pointers... :)
2842 * First allocate/reallocate the option buffer as needed...
2845 i
= ipp_length(job
->attrs
);
2852 optptr
= realloc(options
, i
);
2856 cupsdLogMessage(CUPSD_LOG_CRIT
,
2857 "[Job %d] Unable to allocate %d bytes for option buffer!",
2860 cupsArrayDelete(filters
);
2862 FilterLevel
-= job
->cost
;
2864 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2873 * Now loop through the attributes and convert them to the textual
2874 * representation used by the filters...
2880 snprintf(title
, sizeof(title
), "%s-%d", printer
->name
, job
->id
);
2881 strcpy(copies
, "1");
2883 for (attr
= job
->attrs
->attrs
; attr
!= NULL
; attr
= attr
->next
)
2885 if (!strcmp(attr
->name
, "copies") &&
2886 attr
->value_tag
== IPP_TAG_INTEGER
)
2889 * Don't use the # copies attribute if we are printing the job sheets...
2893 sprintf(copies
, "%d", attr
->values
[0].integer
);
2895 else if (!strcmp(attr
->name
, "job-name") &&
2896 (attr
->value_tag
== IPP_TAG_NAME
||
2897 attr
->value_tag
== IPP_TAG_NAMELANG
))
2898 strlcpy(title
, attr
->values
[0].string
.text
, sizeof(title
));
2899 else if (attr
->group_tag
== IPP_TAG_JOB
)
2902 * Filter out other unwanted attributes...
2905 if (attr
->value_tag
== IPP_TAG_MIMETYPE
||
2906 attr
->value_tag
== IPP_TAG_NAMELANG
||
2907 attr
->value_tag
== IPP_TAG_TEXTLANG
||
2908 (attr
->value_tag
== IPP_TAG_URI
&& strcmp(attr
->name
, "job-uuid")) ||
2909 attr
->value_tag
== IPP_TAG_URISCHEME
||
2910 attr
->value_tag
== IPP_TAG_BEGIN_COLLECTION
) /* Not yet supported */
2913 if (!strncmp(attr
->name
, "time-", 5))
2916 if (!strncmp(attr
->name
, "job-", 4) && strcmp(attr
->name
, "job-uuid") &&
2917 !(printer
->type
& CUPS_PRINTER_REMOTE
))
2920 if (!strncmp(attr
->name
, "job-", 4) &&
2921 strcmp(attr
->name
, "job-uuid") &&
2922 strcmp(attr
->name
, "job-billing") &&
2923 strcmp(attr
->name
, "job-sheets") &&
2924 strcmp(attr
->name
, "job-hold-until") &&
2925 strcmp(attr
->name
, "job-priority"))
2928 if ((!strcmp(attr
->name
, "page-label") ||
2929 !strcmp(attr
->name
, "page-border") ||
2930 !strncmp(attr
->name
, "number-up", 9) ||
2931 !strcmp(attr
->name
, "page-ranges") ||
2932 !strcmp(attr
->name
, "page-set") ||
2933 !strcasecmp(attr
->name
, "AP_FIRSTPAGE_InputSlot") ||
2934 !strcasecmp(attr
->name
, "AP_FIRSTPAGE_ManualFeed") ||
2935 !strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
2936 "PMTotalSidesImaged..n.") ||
2937 !strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
2938 "PMTotalBeginPages..n.")) &&
2943 * Otherwise add them to the list...
2946 if (optptr
> options
)
2947 strlcat(optptr
, " ", optlength
- (optptr
- options
));
2949 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
2951 strlcat(optptr
, attr
->name
, optlength
- (optptr
- options
));
2952 strlcat(optptr
, "=", optlength
- (optptr
- options
));
2955 for (i
= 0; i
< attr
->num_values
; i
++)
2958 strlcat(optptr
, ",", optlength
- (optptr
- options
));
2960 optptr
+= strlen(optptr
);
2962 switch (attr
->value_tag
)
2964 case IPP_TAG_INTEGER
:
2966 snprintf(optptr
, optlength
- (optptr
- options
),
2967 "%d", attr
->values
[i
].integer
);
2970 case IPP_TAG_BOOLEAN
:
2971 if (!attr
->values
[i
].boolean
)
2972 strlcat(optptr
, "no", optlength
- (optptr
- options
));
2974 case IPP_TAG_NOVALUE
:
2975 strlcat(optptr
, attr
->name
,
2976 optlength
- (optptr
- options
));
2979 case IPP_TAG_RANGE
:
2980 if (attr
->values
[i
].range
.lower
== attr
->values
[i
].range
.upper
)
2981 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2982 "%d", attr
->values
[i
].range
.lower
);
2984 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2985 "%d-%d", attr
->values
[i
].range
.lower
,
2986 attr
->values
[i
].range
.upper
);
2989 case IPP_TAG_RESOLUTION
:
2990 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2991 "%dx%d%s", attr
->values
[i
].resolution
.xres
,
2992 attr
->values
[i
].resolution
.yres
,
2993 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
2997 case IPP_TAG_STRING
:
3000 case IPP_TAG_KEYWORD
:
3001 case IPP_TAG_CHARSET
:
3002 case IPP_TAG_LANGUAGE
:
3004 for (valptr
= attr
->values
[i
].string
.text
; *valptr
;)
3006 if (strchr(" \t\n\\\'\"", *valptr
))
3008 *optptr
++ = *valptr
++;
3015 break; /* anti-compiler-warning-code */
3019 optptr
+= strlen(optptr
);
3024 * Build the command-line arguments for the filters. Each filter
3025 * has 6 or 7 arguments:
3029 * argv[2] = username
3031 * argv[4] = # copies
3033 * argv[6] = filename (optional; normally stdin)
3035 * This allows legacy printer drivers that use the old System V
3036 * printing interface to be used by CUPS.
3038 * For remote jobs, we send all of the files in the argument list.
3041 if (printer
->remote
&& job
->num_files
> 1)
3042 argv
= calloc(7 + job
->num_files
, sizeof(char *));
3044 argv
= calloc(8, sizeof(char *));
3048 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to allocate argument array!");
3049 cupsArrayDelete(filters
);
3051 FilterLevel
-= job
->cost
;
3053 cupsdStopPrinter(printer
, 0);
3057 sprintf(jobid
, "%d", job
->id
);
3059 argv
[0] = printer
->name
;
3061 argv
[2] = job
->username
;
3066 if (printer
->remote
&& job
->num_files
> 1)
3068 for (i
= 0; i
< job
->num_files
; i
++)
3070 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
3072 argv
[6 + i
] = strdup(filename
);
3077 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
3078 job
->id
, job
->current_file
+ 1);
3082 for (i
= 0; argv
[i
]; i
++)
3083 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3084 "[Job %d] argv[%d]=\"%s\"", job
->id
, i
, argv
[i
]);
3087 * Create environment variable strings for the filters...
3090 attr
= ippFindAttribute(job
->attrs
, "attributes-natural-language",
3094 strcpy(apple_language
, "APPLE_LANGUAGE=");
3095 _cupsAppleLanguage(attr
->values
[0].string
.text
,
3096 apple_language
+ 15, sizeof(apple_language
) - 15);
3097 #endif /* __APPLE__ */
3099 switch (strlen(attr
->values
[0].string
.text
))
3103 * This is an unknown or badly formatted language code; use
3104 * the POSIX locale...
3107 strcpy(lang
, "LANG=C");
3112 * Just the language code (ll)...
3115 snprintf(lang
, sizeof(lang
), "LANG=%s.UTF8",
3116 attr
->values
[0].string
.text
);
3121 * Language and country code (ll-cc)...
3124 snprintf(lang
, sizeof(lang
), "LANG=%c%c_%c%c.UTF8",
3125 attr
->values
[0].string
.text
[0],
3126 attr
->values
[0].string
.text
[1],
3127 toupper(attr
->values
[0].string
.text
[3] & 255),
3128 toupper(attr
->values
[0].string
.text
[4] & 255));
3132 attr
= ippFindAttribute(job
->attrs
, "document-format",
3135 (optptr
= strstr(attr
->values
[0].string
.text
, "charset=")) != NULL
)
3136 snprintf(charset
, sizeof(charset
), "CHARSET=%s", optptr
+ 8);
3139 attr
= ippFindAttribute(job
->attrs
, "attributes-charset",
3141 snprintf(charset
, sizeof(charset
), "CHARSET=%s",
3142 attr
->values
[0].string
.text
);
3145 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s/%s",
3146 job
->filetypes
[job
->current_file
]->super
,
3147 job
->filetypes
[job
->current_file
]->type
);
3148 snprintf(device_uri
, sizeof(device_uri
), "DEVICE_URI=%s",
3149 printer
->device_uri
);
3150 cupsdSanitizeURI(printer
->device_uri
, sani_uri
, sizeof(sani_uri
));
3151 snprintf(ppd
, sizeof(ppd
), "PPD=%s/ppd/%s.ppd", ServerRoot
, printer
->name
);
3152 snprintf(printer_name
, sizeof(printer_name
), "PRINTER=%s", printer
->name
);
3153 snprintf(rip_max_cache
, sizeof(rip_max_cache
), "RIP_MAX_CACHE=%s", RIPCache
);
3155 envc
= cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
3157 envp
[envc
++] = charset
;
3158 envp
[envc
++] = lang
;
3160 envp
[envc
++] = apple_language
;
3161 #endif /* __APPLE__ */
3162 envp
[envc
++] = ppd
;
3163 envp
[envc
++] = rip_max_cache
;
3164 envp
[envc
++] = content_type
;
3165 envp
[envc
++] = device_uri
;
3166 envp
[envc
++] = printer_name
;
3168 if (!printer
->remote
&& !printer
->raw
)
3170 filter
= (mime_filter_t
*)cupsArrayLast(filters
);
3172 if (printer
->port_monitor
)
3173 filter
= (mime_filter_t
*)cupsArrayPrev(filters
);
3175 if (filter
&& filter
->dst
)
3177 snprintf(final_content_type
, sizeof(final_content_type
),
3178 "FINAL_CONTENT_TYPE=%s/%s",
3179 filter
->dst
->super
, filter
->dst
->type
);
3180 envp
[envc
++] = final_content_type
;
3184 if (Classification
&& !banner_page
)
3186 if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
3187 IPP_TAG_NAME
)) == NULL
)
3188 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3190 else if (attr
->num_values
> 1 &&
3191 strcmp(attr
->values
[1].string
.text
, "none") != 0)
3192 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3193 attr
->values
[1].string
.text
);
3195 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3196 attr
->values
[0].string
.text
);
3198 envp
[envc
++] = classification
;
3201 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
3203 snprintf(class_name
, sizeof(class_name
), "CLASS=%s", job
->dest
);
3204 envp
[envc
++] = class_name
;
3207 if (job
->auth_username
)
3208 envp
[envc
++] = job
->auth_username
;
3209 if (job
->auth_domain
)
3210 envp
[envc
++] = job
->auth_domain
;
3211 if (job
->auth_password
)
3212 envp
[envc
++] = job
->auth_password
;
3216 envp
[envc
++] = job
->ccname
;
3217 #endif /* HAVE_GSSAPI */
3221 for (i
= 0; i
< envc
; i
++)
3222 if (!strncmp(envp
[i
], "AUTH_", 5))
3223 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] envp[%d]=\"AUTH_%c****\"",
3224 job
->id
, i
, envp
[i
][5]);
3225 else if (strncmp(envp
[i
], "DEVICE_URI=", 11))
3226 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] envp[%d]=\"%s\"",
3227 job
->id
, i
, envp
[i
]);
3229 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] envp[%d]=\"DEVICE_URI=%s\"",
3230 job
->id
, i
, sani_uri
);
3232 if (printer
->remote
)
3233 job
->current_file
= job
->num_files
;
3235 job
->current_file
++;
3238 * Now create processes for all of the filters...
3241 filterfds
[0][0] = -1;
3242 filterfds
[0][1] = -1;
3243 filterfds
[1][0] = -1;
3244 filterfds
[1][1] = -1;
3246 if (!job
->status_buffer
)
3248 if (cupsdOpenPipe(job
->status_pipes
))
3250 cupsdLogMessage(CUPSD_LOG_ERROR
,
3251 "[Job %d] Unable to create job status pipes - %s.",
3252 job
->id
, strerror(errno
));
3253 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3254 "Unable to create status pipes - %s.", strerror(errno
));
3256 cupsdAddPrinterHistory(printer
);
3258 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3259 "Job canceled because the server could not create the job "
3265 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3266 "[Job %d] start_job: status_pipes = [ %d %d ]",
3267 job
->id
, job
->status_pipes
[0], job
->status_pipes
[1]);
3269 job
->status_buffer
= cupsdStatBufNew(job
->status_pipes
[0], "[Job %d]",
3271 job
->status_level
= CUPSD_LOG_INFO
;
3275 memset(job
->filters
, 0, sizeof(job
->filters
));
3278 job
->profile
= cupsdCreateProfile(job
->id
);
3280 for (i
= 0, slot
= 0, filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
3282 i
++, filter
= (mime_filter_t
*)cupsArrayNext(filters
))
3284 if (filter
->filter
[0] != '/')
3285 snprintf(command
, sizeof(command
), "%s/filter/%s", ServerBin
,
3288 strlcpy(command
, filter
->filter
, sizeof(command
));
3290 if (i
< (cupsArrayCount(filters
) - 1))
3292 if (cupsdOpenPipe(filterfds
[slot
]))
3294 cupsdLogMessage(CUPSD_LOG_ERROR
,
3295 "[Job %d] Unable to create job filter pipes - %s.",
3296 job
->id
, strerror(errno
));
3297 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3298 "Unable to create filter pipes - %s.", strerror(errno
));
3299 cupsdAddPrinterHistory(printer
);
3301 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3302 "Job canceled because the server could not create the "
3310 if (job
->current_file
== 1)
3312 if (strncmp(printer
->device_uri
, "file:", 5) != 0)
3314 if (cupsdOpenPipe(job
->print_pipes
))
3316 cupsdLogMessage(CUPSD_LOG_ERROR
,
3317 "[Job %d] Unable to create job backend pipes - %s.",
3318 job
->id
, strerror(errno
));
3319 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3320 "Unable to create backend pipes - %s.", strerror(errno
));
3321 cupsdAddPrinterHistory(printer
);
3323 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3324 "Job canceled because the server could not create "
3325 "the backend pipes.");
3332 job
->print_pipes
[0] = -1;
3333 if (!strcmp(printer
->device_uri
, "file:/dev/null") ||
3334 !strcmp(printer
->device_uri
, "file:///dev/null"))
3335 job
->print_pipes
[1] = -1;
3338 if (!strncmp(printer
->device_uri
, "file:/dev/", 10))
3339 job
->print_pipes
[1] = open(printer
->device_uri
+ 5,
3341 else if (!strncmp(printer
->device_uri
, "file:///dev/", 12))
3342 job
->print_pipes
[1] = open(printer
->device_uri
+ 7,
3344 else if (!strncmp(printer
->device_uri
, "file:///", 8))
3345 job
->print_pipes
[1] = open(printer
->device_uri
+ 7,
3346 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
3348 job
->print_pipes
[1] = open(printer
->device_uri
+ 5,
3349 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
3351 if (job
->print_pipes
[1] < 0)
3353 cupsdLogMessage(CUPSD_LOG_ERROR
,
3354 "[Job %d] Unable to open output file \"%s\" - %s.",
3355 job
->id
, printer
->device_uri
, strerror(errno
));
3356 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3357 "Unable to open output file \"%s\" - %s.",
3358 printer
->device_uri
, strerror(errno
));
3360 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3361 "Job canceled because the server could not open the "
3367 fcntl(job
->print_pipes
[1], F_SETFD
,
3368 fcntl(job
->print_pipes
[1], F_GETFD
) | FD_CLOEXEC
);
3372 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3373 "[Job %d] start_job: print_pipes = [ %d %d ]",
3374 job
->id
, job
->print_pipes
[0], job
->print_pipes
[1]);
3377 filterfds
[slot
][0] = job
->print_pipes
[0];
3378 filterfds
[slot
][1] = job
->print_pipes
[1];
3381 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] start_job: filter=\"%s\"",
3383 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3384 "[Job %d] start_job: filterfds[%d]=[ %d %d ]",
3385 job
->id
, slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3387 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
3388 filterfds
[slot
][1], job
->status_pipes
[1],
3389 job
->back_pipes
[0], job
->side_pipes
[0], 0,
3390 job
->profile
, job
->filters
+ i
);
3392 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3393 "[Job %d] start_job: Closing filter pipes for slot %d "
3395 job
->id
, !slot
, filterfds
[!slot
][0], filterfds
[!slot
][1]);
3397 cupsdClosePipe(filterfds
[!slot
]);
3401 cupsdLogMessage(CUPSD_LOG_ERROR
,
3402 "[Job %d] Unable to start filter \"%s\" - %s.",
3403 job
->id
, filter
->filter
, strerror(errno
));
3404 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3405 "Unable to start filter \"%s\" - %s.",
3406 filter
->filter
, strerror(errno
));
3408 cupsdAddPrinterHistory(printer
);
3410 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3411 "Job canceled because the server could not execute a "
3417 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Started filter %s (PID %d)",
3418 job
->id
, command
, pid
);
3424 cupsArrayDelete(filters
);
3427 * Finally, pipe the final output into a backend process if needed...
3430 if (strncmp(printer
->device_uri
, "file:", 5) != 0)
3432 if (job
->current_file
== 1 || printer
->remote
)
3434 sscanf(printer
->device_uri
, "%254[^:]", method
);
3435 snprintf(command
, sizeof(command
), "%s/backend/%s", ServerBin
, method
);
3438 * See if the backend needs to run as root...
3443 else if (stat(command
, &backinfo
))
3446 backroot
= !(backinfo
.st_mode
& (S_IRWXG
| S_IRWXO
));
3450 filterfds
[slot
][0] = -1;
3451 filterfds
[slot
][1] = -1;
3453 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] start_job: backend=\"%s\"",
3455 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3456 "[Job %d] start_job: filterfds[%d] = [ %d %d ]", job
->id
,
3457 slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3459 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
3460 filterfds
[slot
][1], job
->status_pipes
[1],
3461 job
->back_pipes
[1], job
->side_pipes
[1],
3462 backroot
, job
->profile
, &(job
->backend
));
3466 cupsdLogMessage(CUPSD_LOG_ERROR
,
3467 "[Job %d] Unable to start backend \"%s\" - %s.",
3468 job
->id
, method
, strerror(errno
));
3469 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3470 "Unable to start backend \"%s\" - %s.", method
,
3473 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3474 "Job canceled because the server could not execute "
3481 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Started backend %s (PID %d)",
3482 job
->id
, command
, pid
);
3486 if (job
->current_file
== job
->num_files
)
3488 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3489 "[Job %d] start_job: Closing print pipes [ %d %d ]...",
3490 job
->id
, job
->print_pipes
[0], job
->print_pipes
[1]);
3492 cupsdClosePipe(job
->print_pipes
);
3494 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3495 "[Job %d] start_job: Closing back pipes [ %d %d ]...",
3496 job
->id
, job
->back_pipes
[0], job
->back_pipes
[1]);
3498 cupsdClosePipe(job
->back_pipes
);
3500 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3501 "[Job %d] start_job: Closing side pipes [ %d %d ]...",
3502 job
->id
, job
->side_pipes
[0], job
->side_pipes
[1]);
3504 cupsdClosePipe(job
->side_pipes
);
3506 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3507 "[Job %d] start_job: Closing status output pipe %d...",
3508 job
->id
, job
->status_pipes
[1]);
3510 close(job
->status_pipes
[1]);
3511 job
->status_pipes
[1] = -1;
3516 filterfds
[slot
][0] = -1;
3517 filterfds
[slot
][1] = -1;
3519 if (job
->current_file
== job
->num_files
)
3521 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3522 "[Job %d] start_job: Closing print pipes [ %d %d ]...",
3523 job
->id
, job
->print_pipes
[0], job
->print_pipes
[1]);
3525 cupsdClosePipe(job
->print_pipes
);
3527 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3528 "[Job %d] start_job: Closing status output pipe %d...",
3529 job
->id
, job
->status_pipes
[1]);
3531 close(job
->status_pipes
[1]);
3532 job
->status_pipes
[1] = -1;
3536 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3537 "[Job %d] start_job: Closing filter pipes for slot %d "
3539 job
->id
, 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 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3569 "[Job %d] start_job: Closing filter pipes for slot %d "
3571 job
->id
, slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3572 cupsdClosePipe(filterfds
[slot
]);
3575 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3576 "[Job %d] start_job: Closing status pipes [ %d %d ]...",
3577 job
->id
, job
->status_pipes
[0], job
->status_pipes
[1]);
3578 cupsdClosePipe(job
->status_pipes
);
3579 cupsdStatBufDelete(job
->status_buffer
);
3581 job
->status_buffer
= NULL
;
3583 cupsArrayDelete(filters
);
3585 if (printer
->remote
&& job
->num_files
> 1)
3587 for (i
= 0; i
< job
->num_files
; i
++)
3593 cupsdStopJob(job
, 0);
3598 * 'unload_job()' - Unload a job from memory.
3602 unload_job(cupsd_job_t
*job
) /* I - Job */
3607 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Unloading...", job
->id
);
3609 ippDelete(job
->attrs
);
3614 job
->job_sheets
= NULL
;
3615 job
->printer_message
= NULL
;
3616 job
->printer_reasons
= NULL
;
3621 * 'update_job()' - Read a status update from a job's filters.
3625 update_job(cupsd_job_t
*job
) /* I - Job to check */
3627 int i
; /* Looping var */
3628 int copies
; /* Number of copies printed */
3629 char message
[1024], /* Message text */
3630 *ptr
; /* Pointer update... */
3631 int loglevel
, /* Log level for message */
3632 event
= 0; /* Events? */
3635 while ((ptr
= cupsdStatBufUpdate(job
->status_buffer
, &loglevel
,
3636 message
, sizeof(message
))) != NULL
)
3639 * Process page and printer state messages as needed...
3642 if (loglevel
== CUPSD_LOG_PAGE
)
3645 * Page message; send the message to the page_log file and update the
3646 * job sheet count...
3651 if (!strncasecmp(message
, "total ", 6))
3654 * Got a total count of pages from a backend or filter...
3657 copies
= atoi(message
+ 6);
3658 copies
-= job
->sheets
->values
[0].integer
; /* Just track the delta */
3660 else if (!sscanf(message
, "%*d%d", &copies
))
3663 job
->sheets
->values
[0].integer
+= copies
;
3665 if (job
->printer
->page_limit
)
3667 cupsd_quota_t
*q
= cupsdUpdateQuota(job
->printer
, job
->username
,
3671 if (AppleQuotas
&& q
->page_count
== -3)
3674 * Quota limit exceeded, cancel job in progress immediately...
3677 cupsdLogMessage(CUPSD_LOG_INFO
,
3678 "[Job %d] Canceled because pages exceed user %s "
3679 "quota limit on printer %s (%s).",
3680 job
->id
, job
->username
, job
->printer
->name
,
3681 job
->printer
->info
);
3683 cupsdCancelJob(job
, 1, IPP_JOB_CANCELED
);
3688 #endif /* __APPLE__ */
3692 cupsdLogPage(job
, message
);
3695 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
3696 "Printed %d page(s).", job
->sheets
->values
[0].integer
);
3698 else if (loglevel
== CUPSD_LOG_STATE
)
3700 if (!strcmp(message
, "paused"))
3702 cupsdStopPrinter(job
->printer
, 1);
3707 cupsdSetPrinterReasons(job
->printer
, message
);
3708 cupsdAddPrinterHistory(job
->printer
);
3709 event
|= CUPSD_EVENT_PRINTER_STATE
;
3712 update_job_attrs(job
);
3714 else if (loglevel
== CUPSD_LOG_ATTR
)
3717 * Set attribute(s)...
3720 int num_attrs
; /* Number of attributes */
3721 cups_option_t
*attrs
; /* Attributes */
3722 const char *attr
; /* Attribute */
3725 num_attrs
= cupsParseOptions(message
, 0, &attrs
);
3727 if ((attr
= cupsGetOption("auth-info-required", num_attrs
,
3730 cupsdSetAuthInfoRequired(job
->printer
, attr
, NULL
);
3731 cupsdSetPrinterAttrs(job
->printer
);
3733 if (job
->printer
->type
& CUPS_PRINTER_DISCOVERED
)
3734 cupsdMarkDirty(CUPSD_DIRTY_REMOTE
);
3736 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3739 if ((attr
= cupsGetOption("printer-alert", num_attrs
, attrs
)) != NULL
)
3741 cupsdSetString(&job
->printer
->alert
, attr
);
3742 event
|= CUPSD_EVENT_PRINTER_STATE
;
3745 if ((attr
= cupsGetOption("printer-alert-description", num_attrs
,
3748 cupsdSetString(&job
->printer
->alert_description
, attr
);
3749 event
|= CUPSD_EVENT_PRINTER_STATE
;
3752 if ((attr
= cupsGetOption("marker-colors", num_attrs
, attrs
)) != NULL
)
3754 cupsdSetPrinterAttr(job
->printer
, "marker-colors", (char *)attr
);
3755 job
->printer
->marker_time
= time(NULL
);
3756 event
|= CUPSD_EVENT_PRINTER_STATE
;
3759 if ((attr
= cupsGetOption("marker-levels", num_attrs
, attrs
)) != NULL
)
3761 cupsdSetPrinterAttr(job
->printer
, "marker-levels", (char *)attr
);
3762 job
->printer
->marker_time
= time(NULL
);
3763 event
|= CUPSD_EVENT_PRINTER_STATE
;
3766 if ((attr
= cupsGetOption("marker-names", num_attrs
, attrs
)) != NULL
)
3768 cupsdSetPrinterAttr(job
->printer
, "marker-names", (char *)attr
);
3769 job
->printer
->marker_time
= time(NULL
);
3770 event
|= CUPSD_EVENT_PRINTER_STATE
;
3773 if ((attr
= cupsGetOption("marker-types", num_attrs
, attrs
)) != NULL
)
3775 cupsdSetPrinterAttr(job
->printer
, "marker-types", (char *)attr
);
3776 job
->printer
->marker_time
= time(NULL
);
3777 event
|= CUPSD_EVENT_PRINTER_STATE
;
3780 cupsFreeOptions(num_attrs
, attrs
);
3782 else if (loglevel
== CUPSD_LOG_PPD
)
3785 * Set attribute(s)...
3788 int num_keywords
; /* Number of keywords */
3789 cups_option_t
*keywords
; /* Keywords */
3792 num_keywords
= cupsParseOptions(message
, 0, &keywords
);
3794 if (cupsdUpdatePrinterPPD(job
->printer
, num_keywords
, keywords
))
3795 cupsdSetPrinterAttrs(job
->printer
);
3797 cupsFreeOptions(num_keywords
, keywords
);
3800 else if (!strncmp(message
, "recoverable:", 12))
3802 cupsdSetPrinterReasons(job
->printer
,
3803 "+com.apple.print.recoverable-warning");
3806 while (isspace(*ptr
& 255))
3809 cupsdSetString(&job
->printer
->recoverable
, ptr
);
3810 cupsdAddPrinterHistory(job
->printer
);
3811 event
|= CUPSD_EVENT_PRINTER_STATE
;
3813 else if (!strncmp(message
, "recovered:", 10))
3815 cupsdSetPrinterReasons(job
->printer
,
3816 "-com.apple.print.recoverable-warning");
3819 while (isspace(*ptr
& 255))
3822 cupsdSetString(&job
->printer
->recoverable
, ptr
);
3823 cupsdAddPrinterHistory(job
->printer
);
3824 event
|= CUPSD_EVENT_PRINTER_STATE
;
3826 #endif /* __APPLE__ */
3827 else if (loglevel
<= job
->status_level
)
3830 * Some message to show in the printer-state-message attribute...
3833 if (loglevel
!= CUPSD_LOG_NOTICE
)
3834 job
->status_level
= loglevel
;
3836 strlcpy(job
->printer
->state_message
, message
,
3837 sizeof(job
->printer
->state_message
));
3838 cupsdAddPrinterHistory(job
->printer
);
3839 event
|= CUPSD_EVENT_PRINTER_STATE
;
3841 update_job_attrs(job
);
3844 if (!strchr(job
->status_buffer
->buffer
, '\n'))
3848 if (event
& CUPSD_EVENT_PRINTER_STATE
)
3849 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, job
->printer
, NULL
,
3850 (job
->printer
->type
& CUPS_PRINTER_CLASS
) ?
3851 "Class \"%s\" state changed." :
3852 "Printer \"%s\" state changed.",
3853 job
->printer
->name
);
3855 if (ptr
== NULL
&& !job
->status_buffer
->bufused
)
3858 * See if all of the filters and the backend have returned their
3862 for (i
= 0; job
->filters
[i
] < 0; i
++);
3864 if (job
->filters
[i
])
3867 if (job
->current_file
>= job
->num_files
&& job
->backend
> 0)
3871 * Handle the end of job stuff...
3874 cupsdFinishJob(job
);
3880 * 'update_job_attrs()' - Update the job-printer-* attributes.
3884 update_job_attrs(cupsd_job_t
*job
) /* I - Job to update */
3886 int i
; /* Looping var */
3887 int num_reasons
; /* Actual number of reasons */
3888 const char * const *reasons
; /* Reasons */
3889 static const char *none
= "none", /* "none" */
3895 * Get/create the job-printer-state-* attributes...
3898 if (!job
->printer_message
)
3900 if ((job
->printer_message
= ippFindAttribute(job
->attrs
,
3901 "job-printer-state-message",
3902 IPP_TAG_TEXT
)) == NULL
)
3903 job
->printer_message
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_TEXT
,
3904 "job-printer-state-message",
3908 if (!job
->printer_reasons
)
3909 job
->printer_reasons
= ippFindAttribute(job
->attrs
,
3910 "job-printer-state-reasons",
3914 * If the job isn't printing, return now...
3921 * Otherwise copy the printer-state-message value...
3924 if (job
->printer
->state_message
[0])
3925 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
),
3926 job
->printer
->state_message
);
3929 * ... and the printer-state-reasons value...
3932 if (job
->printer
->num_reasons
== 0)
3935 reasons
= job
->printer
->state
== IPP_PRINTER_STOPPED
? &paused
: &none
;
3939 num_reasons
= job
->printer
->num_reasons
;
3940 reasons
= (const char * const *)job
->printer
->reasons
;
3943 if (!job
->printer_reasons
|| job
->printer_reasons
->num_values
!= num_reasons
)
3945 ippDeleteAttribute(job
->attrs
, job
->printer_reasons
);
3947 job
->printer_reasons
= ippAddStrings(job
->attrs
,
3948 IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
3949 "job-printer-state-reasons",
3950 num_reasons
, NULL
, NULL
);
3953 for (i
= 0; i
< num_reasons
; i
++)
3954 cupsdSetString(&(job
->printer_reasons
->values
[i
].string
.text
), reasons
[i
]);
3959 * End of "$Id: job.c 7005 2007-10-01 23:45:48Z mike $".