2 * "$Id: job.c 7682 2008-06-21 00:06:02Z mike $"
4 * Job management routines for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2008 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
17 * cupsdAddJob() - Add a new job to the job queue...
18 * cupsdCancelJob() - Cancel the specified print job.
19 * cupsdCancelJobs() - Cancel all jobs for the given
21 * cupsdCheckJobs() - Check the pending jobs and start any if
22 * the destination is available.
23 * cupsdCleanJobs() - Clean out old jobs.
24 * cupsdDeleteJob() - Free all memory used by a job.
25 * cupsdFinishJob() - Finish a job.
26 * cupsdFreeAllJobs() - Free all jobs from memory.
27 * cupsdFindJob() - Find the specified job.
28 * cupsdGetPrinterJobCount() - Get the number of pending, processing,
29 * cupsdGetUserJobCount() - Get the number of pending, processing,
30 * cupsdHoldJob() - Hold the specified job.
31 * cupsdLoadAllJobs() - Load all jobs from disk.
32 * cupsdLoadJob() - Load a single job...
33 * cupsdMoveJob() - Move the specified job to a different
35 * cupsdReleaseJob() - Release the specified job.
36 * cupsdRestartJob() - Restart the specified job.
37 * cupsdSaveAllJobs() - Save a summary of all jobs to disk.
38 * cupsdSaveJob() - Save a job to disk.
39 * cupsdSetJobHoldUntil() - Set the hold time for a job...
40 * cupsdSetJobPriority() - Set the priority of a job, moving it
41 * up/down in the list as needed.
42 * cupsdStopAllJobs() - Stop all print jobs.
43 * cupsdStopJob() - Stop a print job.
44 * cupsdUnloadCompletedJobs() - Flush completed job history from memory.
45 * compare_active_jobs() - Compare the job IDs and priorities of two
47 * compare_jobs() - Compare the job IDs of two jobs.
48 * ipp_length() - Compute the size of the buffer needed to
49 * hold the textual IPP attributes.
50 * load_job_cache() - Load jobs from the job.cache file.
51 * load_next_job_id() - Load the NextJobId value from the
53 * load_request_root() - Load jobs from the RequestRoot directory.
54 * set_time() - Set one of the "time-at-xyz" attributes...
55 * set_hold_until() - Set the hold time and update job-hold-until
57 * start_job() - Start a print job.
58 * unload_job() - Unload a job from memory.
59 * update_job() - Read a status update from a jobs filters.
60 * update_job_attrs() - Update the job-printer-* attributes.
64 * Include necessary headers...
69 #include <cups/backend.h>
77 static mime_filter_t gziptoany_filter
=
79 NULL
, /* Source type */
80 NULL
, /* Destination type */
82 "gziptoany" /* Filter program to run */
90 static int compare_active_jobs(void *first
, void *second
, void *data
);
91 static int compare_jobs(void *first
, void *second
, void *data
);
92 static int ipp_length(ipp_t
*ipp
);
93 static void load_job_cache(const char *filename
);
94 static void load_next_job_id(const char *filename
);
95 static void load_request_root(void);
96 static void set_time(cupsd_job_t
*job
, const char *name
);
97 static void set_hold_until(cupsd_job_t
*job
, time_t holdtime
);
98 static void start_job(cupsd_job_t
*job
, cupsd_printer_t
*printer
);
99 static void unload_job(cupsd_job_t
*job
);
100 static void update_job(cupsd_job_t
*job
);
101 static void update_job_attrs(cupsd_job_t
*job
, int do_message
);
105 * 'cupsdAddJob()' - Add a new job to the job queue...
108 cupsd_job_t
* /* O - New job record */
109 cupsdAddJob(int priority
, /* I - Job priority */
110 const char *dest
) /* I - Job destination */
112 cupsd_job_t
*job
; /* New job record */
115 if ((job
= calloc(sizeof(cupsd_job_t
), 1)) == NULL
)
118 job
->id
= NextJobId
++;
119 job
->priority
= priority
;
120 job
->back_pipes
[0] = -1;
121 job
->back_pipes
[1] = -1;
122 job
->print_pipes
[0] = -1;
123 job
->print_pipes
[1] = -1;
124 job
->side_pipes
[0] = -1;
125 job
->side_pipes
[1] = -1;
126 job
->status_pipes
[0] = -1;
127 job
->status_pipes
[1] = -1;
129 cupsdSetString(&job
->dest
, dest
);
132 * Add the new job to the "all jobs" and "active jobs" lists...
135 cupsArrayAdd(Jobs
, job
);
136 cupsArrayAdd(ActiveJobs
, job
);
143 * 'cupsdCancelJob()' - Cancel the specified print job.
147 cupsdCancelJob(cupsd_job_t
*job
, /* I - Job to cancel */
148 int purge
, /* I - Purge jobs? */
149 ipp_jstate_t newstate
) /* I - New job state */
151 int i
; /* Looping var */
152 char filename
[1024]; /* Job filename */
153 cupsd_printer_t
*printer
; /* Printer used by job */
156 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdCancelJob: id = %d", job
->id
);
159 * Stop any processes that are working on the current job...
162 printer
= job
->printer
;
164 if (job
->state_value
== IPP_JOB_PROCESSING
)
165 cupsdStopJob(job
, 0);
170 job
->state
->values
[0].integer
= newstate
;
172 job
->state_value
= newstate
;
174 set_time(job
, "time-at-completed");
177 * Send any pending notifications and then expire them...
185 case IPP_JOB_CANCELED
:
186 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, printer
, job
,
187 purge
? "Job purged." : "Job canceled.");
190 case IPP_JOB_ABORTED
:
191 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, printer
, job
,
192 "Job aborted; please consult the error_log file "
196 case IPP_JOB_COMPLETED
:
198 * Clear the printer's printer-state-message and move on...
201 printer
->state_message
[0] = '\0';
203 cupsdSetPrinterState(printer
, IPP_PRINTER_IDLE
, 0);
205 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, printer
, job
,
210 cupsdExpireSubscriptions(NULL
, job
);
213 * Remove the job from the active and printing lists...
216 cupsArrayRemove(ActiveJobs
, job
);
217 cupsArrayRemove(PrintingJobs
, job
);
220 * Remove any authentication data...
223 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
224 if (cupsdRemoveFile(filename
) && errno
!= ENOENT
)
225 cupsdLogMessage(CUPSD_LOG_ERROR
,
226 "Unable to remove authentication cache: %s",
229 cupsdClearString(&job
->auth_username
);
230 cupsdClearString(&job
->auth_domain
);
231 cupsdClearString(&job
->auth_password
);
235 * Destroy the credential cache and clear the KRB5CCNAME env var string.
240 krb5_cc_destroy(KerberosContext
, job
->ccache
);
244 cupsdClearString(&job
->ccname
);
245 #endif /* HAVE_GSSAPI */
248 * Remove the print file for good if we aren't preserving jobs or
252 job
->current_file
= 0;
254 if (!JobHistory
|| !JobFiles
|| purge
)
256 for (i
= 1; i
<= job
->num_files
; i
++)
258 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
263 if (job
->num_files
> 0)
265 free(job
->filetypes
);
266 free(job
->compressions
);
269 job
->filetypes
= NULL
;
270 job
->compressions
= NULL
;
274 if (JobHistory
&& !purge
)
277 * Save job state info...
281 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
286 * Remove the job info file...
289 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
,
294 * Remove the job from the "all jobs" list...
297 cupsArrayRemove(Jobs
, job
);
300 * Free all memory used...
311 * 'cupsdCancelJobs()' - Cancel all jobs for the given destination/user...
315 cupsdCancelJobs(const char *dest
, /* I - Destination to cancel */
316 const char *username
, /* I - Username or NULL */
317 int purge
) /* I - Purge jobs? */
319 cupsd_job_t
*job
; /* Current job */
322 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
324 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
326 if (!job
->dest
|| !job
->username
)
329 if (!job
->dest
|| !job
->username
)
332 if ((dest
== NULL
|| !strcmp(job
->dest
, dest
)) &&
333 (username
== NULL
|| !strcmp(job
->username
, username
)))
336 * Cancel all jobs matching this destination/user...
339 cupsdCancelJob(job
, purge
, IPP_JOB_CANCELED
);
348 * 'cupsdCheckJobs()' - Check the pending jobs and start any if the destination
355 cupsd_job_t
*job
; /* Current job in queue */
356 cupsd_printer_t
*printer
, /* Printer destination */
357 *pclass
; /* Printer class destination */
358 ipp_attribute_t
*attr
; /* Job attribute */
361 DEBUG_puts("cupsdCheckJobs()");
363 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
364 "cupsdCheckJobs: %d active jobs, sleeping=%d, reload=%d",
365 cupsArrayCount(ActiveJobs
), Sleeping
, NeedReload
);
367 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
369 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
372 * Start held jobs if they are ready...
375 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
376 "cupsdCheckJobs: Job %d: 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 cupsdLogJob(job
, CUPSD_LOG_WARN
,
440 "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 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "File %d is complete.",
571 job
->current_file
- 1);
573 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
574 "cupsdFinishJob: job->status is %d",
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 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
587 "cupsdFinishJob: Closing status pipes [ %d %d ]...",
588 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
, 0);
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 cupsdLogJob(job
, CUPSD_LOG_INFO
, "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 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
684 "Canceling job since it could not be "
685 "sent after %d tries.",
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 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Job stopped due to filter errors.");
804 cupsdStopJob(job
, 1);
806 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
807 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
808 "Job stopped due to filter errors; please consult the "
809 "error_log file for details.");
815 * Job printed successfully; cancel it...
818 if (job
->current_file
< job
->num_files
)
821 * Start the next file in the job...
824 FilterLevel
-= job
->cost
;
825 start_job(job
, printer
);
830 * Close out this job...
833 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Completed successfully.");
834 cupsdCancelJob(job
, 0, IPP_JOB_COMPLETED
);
842 * 'cupsdFreeAllJobs()' - Free all jobs from memory.
846 cupsdFreeAllJobs(void)
848 cupsd_job_t
*job
; /* Current job */
859 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
861 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
863 cupsArrayRemove(Jobs
, job
);
864 cupsArrayRemove(ActiveJobs
, job
);
865 cupsArrayRemove(PrintingJobs
, job
);
870 cupsdReleaseSignals();
875 * 'cupsdFindJob()' - Find the specified job.
878 cupsd_job_t
* /* O - Job data */
879 cupsdFindJob(int id
) /* I - Job ID */
881 cupsd_job_t key
; /* Search key */
886 return ((cupsd_job_t
*)cupsArrayFind(Jobs
, &key
));
891 * 'cupsdGetPrinterJobCount()' - Get the number of pending, processing,
892 * or held jobs in a printer or class.
895 int /* O - Job count */
896 cupsdGetPrinterJobCount(
897 const char *dest
) /* I - Printer or class name */
899 int count
; /* Job count */
900 cupsd_job_t
*job
; /* Current job */
903 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
905 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
906 if (job
->dest
&& !strcasecmp(job
->dest
, dest
))
914 * 'cupsdGetUserJobCount()' - Get the number of pending, processing,
915 * or held jobs for a user.
918 int /* O - Job count */
919 cupsdGetUserJobCount(
920 const char *username
) /* I - Username */
922 int count
; /* Job count */
923 cupsd_job_t
*job
; /* Current job */
926 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
928 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
929 if (!strcasecmp(job
->username
, username
))
937 * 'cupsdHoldJob()' - Hold the specified job.
941 cupsdHoldJob(cupsd_job_t
*job
) /* I - Job data */
943 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdHoldJob: id = %d", job
->id
);
945 if (job
->state_value
== IPP_JOB_PROCESSING
)
946 cupsdStopJob(job
, 0);
950 DEBUG_puts("cupsdHoldJob: setting state to held...");
952 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
953 job
->state_value
= IPP_JOB_HELD
;
954 job
->current_file
= 0;
957 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
964 * 'cupsdLoadAllJobs()' - Load all jobs from disk.
968 cupsdLoadAllJobs(void)
970 char filename
[1024]; /* Full filename of job.cache file */
971 struct stat fileinfo
, /* Information on job.cache file */
972 dirinfo
; /* Information on RequestRoot dir */
977 * Create the job arrays as needed...
981 Jobs
= cupsArrayNew(compare_jobs
, NULL
);
984 ActiveJobs
= cupsArrayNew(compare_active_jobs
, NULL
);
987 PrintingJobs
= cupsArrayNew(compare_jobs
, NULL
);
990 * See whether the job.cache file is older than the RequestRoot directory...
993 snprintf(filename
, sizeof(filename
), "%s/job.cache", CacheDir
);
995 if (stat(filename
, &fileinfo
))
997 fileinfo
.st_mtime
= 0;
1000 cupsdLogMessage(CUPSD_LOG_ERROR
,
1001 "Unable to get file information for \"%s\" - %s",
1002 filename
, strerror(errno
));
1005 if (stat(RequestRoot
, &dirinfo
))
1007 dirinfo
.st_mtime
= 0;
1009 if (errno
!= ENOENT
)
1010 cupsdLogMessage(CUPSD_LOG_ERROR
,
1011 "Unable to get directory information for \"%s\" - %s",
1012 RequestRoot
, strerror(errno
));
1016 * Load the most recent source for job data...
1019 if (dirinfo
.st_mtime
> fileinfo
.st_mtime
)
1021 load_request_root();
1023 load_next_job_id(filename
);
1026 load_job_cache(filename
);
1029 * Clean out old jobs as needed...
1032 if (MaxJobs
> 0 && cupsArrayCount(Jobs
) >= MaxJobs
)
1038 * 'cupsdLoadJob()' - Load a single job...
1042 cupsdLoadJob(cupsd_job_t
*job
) /* I - Job */
1044 char jobfile
[1024]; /* Job filename */
1045 cups_file_t
*fp
; /* Job file */
1046 int fileid
; /* Current file ID */
1047 ipp_attribute_t
*attr
; /* Job attribute */
1048 const char *dest
; /* Destination name */
1049 cupsd_printer_t
*destptr
; /* Pointer to destination */
1050 mime_type_t
**filetypes
; /* New filetypes array */
1051 int *compressions
; /* New compressions array */
1056 if (job
->state_value
> IPP_JOB_STOPPED
)
1057 job
->access_time
= time(NULL
);
1062 if ((job
->attrs
= ippNew()) == NULL
)
1064 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Ran out of memory for job attributes!");
1069 * Load job attributes...
1072 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Loading attributes...");
1074 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, job
->id
);
1075 if ((fp
= cupsFileOpen(jobfile
, "r")) == NULL
)
1077 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1078 "Unable to open job control file \"%s\" - %s!",
1079 jobfile
, strerror(errno
));
1080 ippDelete(job
->attrs
);
1085 if (ippReadIO(fp
, (ipp_iocb_t
)cupsFileRead
, 1, NULL
, job
->attrs
) != IPP_DATA
)
1087 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1088 "Unable to read job control file \"%s\"!",
1091 ippDelete(job
->attrs
);
1100 * Copy attribute data to the job object...
1103 if ((job
->state
= ippFindAttribute(job
->attrs
, "job-state",
1104 IPP_TAG_ENUM
)) == NULL
)
1106 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1107 "Missing or bad job-state attribute in control file!");
1108 ippDelete(job
->attrs
);
1114 job
->state_value
= (ipp_jstate_t
)job
->state
->values
[0].integer
;
1118 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1119 IPP_TAG_URI
)) == NULL
)
1121 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1122 "No job-printer-uri attribute in control file!");
1123 ippDelete(job
->attrs
);
1129 if ((dest
= cupsdValidateDest(attr
->values
[0].string
.text
, &(job
->dtype
),
1132 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1133 "Unable to queue job for destination \"%s\"!",
1134 attr
->values
[0].string
.text
);
1135 ippDelete(job
->attrs
);
1141 cupsdSetString(&job
->dest
, dest
);
1143 else if ((destptr
= cupsdFindDest(job
->dest
)) == NULL
)
1145 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1146 "Unable to queue job for destination \"%s\"!", job
->dest
);
1147 ippDelete(job
->attrs
);
1153 job
->sheets
= ippFindAttribute(job
->attrs
, "job-media-sheets-completed",
1155 job
->job_sheets
= ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_NAME
);
1159 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
1160 IPP_TAG_INTEGER
)) == NULL
)
1162 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1163 "Missing or bad job-priority attribute in control file!");
1164 ippDelete(job
->attrs
);
1170 job
->priority
= attr
->values
[0].integer
;
1175 if ((attr
= ippFindAttribute(job
->attrs
, "job-originating-user-name",
1176 IPP_TAG_NAME
)) == NULL
)
1178 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1179 "Missing or bad job-originating-user-name attribute in "
1181 ippDelete(job
->attrs
);
1187 cupsdSetString(&job
->username
, attr
->values
[0].string
.text
);
1191 * Set the job hold-until time and state...
1194 if (job
->state_value
== IPP_JOB_HELD
)
1196 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1197 IPP_TAG_KEYWORD
)) == NULL
)
1198 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1201 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
1204 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1205 job
->state_value
= IPP_JOB_PENDING
;
1208 else if (job
->state_value
== IPP_JOB_PROCESSING
)
1210 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1211 job
->state_value
= IPP_JOB_PENDING
;
1214 if (!job
->num_files
)
1217 * Find all the d##### files...
1220 for (fileid
= 1; fileid
< 10000; fileid
++)
1222 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
1225 if (access(jobfile
, 0))
1228 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
1229 "Auto-typing document file \"%s\"...", jobfile
);
1231 if (fileid
> job
->num_files
)
1233 if (job
->num_files
== 0)
1235 compressions
= (int *)calloc(fileid
, sizeof(int));
1236 filetypes
= (mime_type_t
**)calloc(fileid
, sizeof(mime_type_t
*));
1240 compressions
= (int *)realloc(job
->compressions
,
1241 sizeof(int) * fileid
);
1242 filetypes
= (mime_type_t
**)realloc(job
->filetypes
,
1243 sizeof(mime_type_t
*) *
1247 if (!compressions
|| !filetypes
)
1249 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1250 "Ran out of memory for job file types!");
1254 job
->compressions
= compressions
;
1255 job
->filetypes
= filetypes
;
1256 job
->num_files
= fileid
;
1259 job
->filetypes
[fileid
- 1] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
1260 job
->compressions
+ fileid
- 1);
1262 if (!job
->filetypes
[fileid
- 1])
1263 job
->filetypes
[fileid
- 1] = mimeType(MimeDatabase
, "application",
1269 * Load authentication information as needed...
1272 if (job
->state_value
< IPP_JOB_STOPPED
)
1274 snprintf(jobfile
, sizeof(jobfile
), "%s/a%05d", RequestRoot
, job
->id
);
1276 cupsdClearString(&job
->auth_username
);
1277 cupsdClearString(&job
->auth_domain
);
1278 cupsdClearString(&job
->auth_password
);
1280 if ((fp
= cupsFileOpen(jobfile
, "r")) != NULL
)
1282 int i
, /* Looping var */
1283 bytes
; /* Size of auth data */
1284 char line
[255], /* Line from file */
1285 data
[255]; /* Decoded data */
1289 i
< destptr
->num_auth_info_required
&&
1290 cupsFileGets(fp
, line
, sizeof(line
));
1293 bytes
= sizeof(data
);
1294 httpDecode64_2(data
, &bytes
, line
);
1296 if (!strcmp(destptr
->auth_info_required
[i
], "username"))
1297 cupsdSetStringf(&job
->auth_username
, "AUTH_USERNAME=%s", data
);
1298 else if (!strcmp(destptr
->auth_info_required
[i
], "domain"))
1299 cupsdSetStringf(&job
->auth_domain
, "AUTH_DOMAIN=%s", data
);
1300 else if (!strcmp(destptr
->auth_info_required
[i
], "password"))
1301 cupsdSetStringf(&job
->auth_password
, "AUTH_PASSWORD=%s", data
);
1307 job
->access_time
= time(NULL
);
1312 * 'cupsdMoveJob()' - Move the specified job to a different destination.
1316 cupsdMoveJob(cupsd_job_t
*job
, /* I - Job */
1317 cupsd_printer_t
*p
) /* I - Destination printer or class */
1319 ipp_attribute_t
*attr
; /* job-printer-uri attribute */
1320 const char *olddest
; /* Old destination */
1321 cupsd_printer_t
*oldp
; /* Old pointer */
1325 * Don't move completed jobs...
1328 if (job
->state_value
> IPP_JOB_STOPPED
)
1332 * Get the old destination...
1335 olddest
= job
->dest
;
1338 oldp
= job
->printer
;
1340 oldp
= cupsdFindDest(olddest
);
1343 * Change the destination information...
1346 if (job
->state_value
== IPP_JOB_PROCESSING
)
1347 cupsdStopJob(job
, 0);
1351 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, oldp
, job
,
1352 "Job #%d moved from %s to %s.", job
->id
, olddest
,
1355 cupsdSetString(&job
->dest
, p
->name
);
1356 job
->dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_REMOTE
|
1357 CUPS_PRINTER_IMPLICIT
);
1359 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1360 IPP_TAG_URI
)) != NULL
)
1361 cupsdSetString(&(attr
->values
[0].string
.text
), p
->uri
);
1363 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, p
, job
,
1364 "Job #%d moved from %s to %s.", job
->id
, olddest
,
1368 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1373 * 'cupsdReleaseJob()' - Release the specified job.
1377 cupsdReleaseJob(cupsd_job_t
*job
) /* I - Job */
1379 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdReleaseJob: id = %d", job
->id
);
1381 if (job
->state_value
== IPP_JOB_HELD
)
1383 DEBUG_puts("cupsdReleaseJob: setting state to pending...");
1385 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1386 job
->state_value
= IPP_JOB_PENDING
;
1388 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1395 * 'cupsdRestartJob()' - Restart the specified job.
1399 cupsdRestartJob(cupsd_job_t
*job
) /* I - Job */
1401 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdRestartJob: id = %d", job
->id
);
1403 if (job
->state_value
== IPP_JOB_STOPPED
|| job
->num_files
)
1405 ipp_jstate_t old_state
; /* Old job state */
1410 old_state
= job
->state_value
;
1413 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1414 job
->state_value
= IPP_JOB_PENDING
;
1417 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1419 if (old_state
> IPP_JOB_STOPPED
)
1420 cupsArrayAdd(ActiveJobs
, job
);
1428 * 'cupsdSaveAllJobs()' - Save a summary of all jobs to disk.
1432 cupsdSaveAllJobs(void)
1434 int i
; /* Looping var */
1435 cups_file_t
*fp
; /* Job cache file */
1436 char temp
[1024]; /* Temporary string */
1437 cupsd_job_t
*job
; /* Current job */
1438 time_t curtime
; /* Current time */
1439 struct tm
*curdate
; /* Current date */
1442 snprintf(temp
, sizeof(temp
), "%s/job.cache", CacheDir
);
1443 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
1445 cupsdLogMessage(CUPSD_LOG_ERROR
,
1446 "Unable to create job cache file \"%s\" - %s",
1447 temp
, strerror(errno
));
1451 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving job cache file \"%s\"...", temp
);
1454 * Restrict access to the file...
1457 fchown(cupsFileNumber(fp
), getuid(), Group
);
1458 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
1461 * Write a small header to the file...
1464 curtime
= time(NULL
);
1465 curdate
= localtime(&curtime
);
1466 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
1468 cupsFilePuts(fp
, "# Job cache file for " CUPS_SVERSION
"\n");
1469 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
1470 cupsFilePrintf(fp
, "NextJobId %d\n", NextJobId
);
1473 * Write each job known to the system...
1476 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1478 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1480 cupsFilePrintf(fp
, "<Job %d>\n", job
->id
);
1481 cupsFilePrintf(fp
, "State %d\n", job
->state_value
);
1482 cupsFilePrintf(fp
, "Priority %d\n", job
->priority
);
1483 cupsFilePrintf(fp
, "Username %s\n", job
->username
);
1484 cupsFilePrintf(fp
, "Destination %s\n", job
->dest
);
1485 cupsFilePrintf(fp
, "DestType %d\n", job
->dtype
);
1486 cupsFilePrintf(fp
, "NumFiles %d\n", job
->num_files
);
1487 for (i
= 0; i
< job
->num_files
; i
++)
1488 cupsFilePrintf(fp
, "File %d %s/%s %d\n", i
+ 1, job
->filetypes
[i
]->super
,
1489 job
->filetypes
[i
]->type
, job
->compressions
[i
]);
1490 cupsFilePuts(fp
, "</Job>\n");
1498 * 'cupsdSaveJob()' - Save a job to disk.
1502 cupsdSaveJob(cupsd_job_t
*job
) /* I - Job */
1504 char filename
[1024]; /* Job control filename */
1505 cups_file_t
*fp
; /* Job file */
1508 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSaveJob(job=%p(%d)): job->attrs=%p",
1509 job
, job
->id
, job
->attrs
);
1511 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
, job
->id
);
1513 if ((fp
= cupsFileOpen(filename
, "w")) == NULL
)
1515 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
1516 "Unable to create job control file \"%s\" - %s.",
1517 filename
, strerror(errno
));
1521 fchmod(cupsFileNumber(fp
), 0600);
1522 fchown(cupsFileNumber(fp
), RunUser
, Group
);
1524 job
->attrs
->state
= IPP_IDLE
;
1526 if (ippWriteIO(fp
, (ipp_iocb_t
)cupsFileWrite
, 1, NULL
,
1527 job
->attrs
) != IPP_DATA
)
1528 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Unable to write job control file!");
1537 * 'cupsdSetJobHoldUntil()' - Set the hold time for a job...
1541 cupsdSetJobHoldUntil(cupsd_job_t
*job
, /* I - Job */
1542 const char *when
) /* I - When to resume */
1544 time_t curtime
; /* Current time */
1545 struct tm
*curdate
; /* Current date */
1546 int hour
; /* Hold hour */
1547 int minute
; /* Hold minute */
1548 int second
; /* Hold second */
1551 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetJobHoldUntil(%d, \"%s\")",
1556 if (!strcmp(when
, "indefinite") || !strcmp(when
, "auth-info-required"))
1559 * Hold indefinitely...
1562 job
->hold_until
= 0;
1564 else if (!strcmp(when
, "day-time"))
1567 * Hold to 6am the next morning unless local time is < 6pm.
1570 curtime
= time(NULL
);
1571 curdate
= localtime(&curtime
);
1573 if (curdate
->tm_hour
< 18)
1574 job
->hold_until
= curtime
;
1576 job
->hold_until
= curtime
+
1577 ((29 - curdate
->tm_hour
) * 60 + 59 -
1578 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1580 else if (!strcmp(when
, "evening") || !strcmp(when
, "night"))
1583 * Hold to 6pm unless local time is > 6pm or < 6am.
1586 curtime
= time(NULL
);
1587 curdate
= localtime(&curtime
);
1589 if (curdate
->tm_hour
< 6 || curdate
->tm_hour
>= 18)
1590 job
->hold_until
= curtime
;
1592 job
->hold_until
= curtime
+
1593 ((17 - curdate
->tm_hour
) * 60 + 59 -
1594 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1596 else if (!strcmp(when
, "second-shift"))
1599 * Hold to 4pm unless local time is > 4pm.
1602 curtime
= time(NULL
);
1603 curdate
= localtime(&curtime
);
1605 if (curdate
->tm_hour
>= 16)
1606 job
->hold_until
= curtime
;
1608 job
->hold_until
= curtime
+
1609 ((15 - curdate
->tm_hour
) * 60 + 59 -
1610 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1612 else if (!strcmp(when
, "third-shift"))
1615 * Hold to 12am unless local time is < 8am.
1618 curtime
= time(NULL
);
1619 curdate
= localtime(&curtime
);
1621 if (curdate
->tm_hour
< 8)
1622 job
->hold_until
= curtime
;
1624 job
->hold_until
= curtime
+
1625 ((23 - curdate
->tm_hour
) * 60 + 59 -
1626 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1628 else if (!strcmp(when
, "weekend"))
1631 * Hold to weekend unless we are in the weekend.
1634 curtime
= time(NULL
);
1635 curdate
= localtime(&curtime
);
1637 if (curdate
->tm_wday
|| curdate
->tm_wday
== 6)
1638 job
->hold_until
= curtime
;
1640 job
->hold_until
= curtime
+
1641 (((5 - curdate
->tm_wday
) * 24 +
1642 (17 - curdate
->tm_hour
)) * 60 + 59 -
1643 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1645 else if (sscanf(when
, "%d:%d:%d", &hour
, &minute
, &second
) >= 2)
1648 * Hold to specified GMT time (HH:MM or HH:MM:SS)...
1651 curtime
= time(NULL
);
1652 curdate
= gmtime(&curtime
);
1654 job
->hold_until
= curtime
+
1655 ((hour
- curdate
->tm_hour
) * 60 + minute
-
1656 curdate
->tm_min
) * 60 + second
- curdate
->tm_sec
;
1659 * Hold until next day as needed...
1662 if (job
->hold_until
< curtime
)
1663 job
->hold_until
+= 24 * 60 * 60;
1666 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetJobHoldUntil: hold_until = %d",
1667 (int)job
->hold_until
);
1672 * 'cupsdSetJobPriority()' - Set the priority of a job, moving it up/down in
1673 * the list as needed.
1677 cupsdSetJobPriority(
1678 cupsd_job_t
*job
, /* I - Job ID */
1679 int priority
) /* I - New priority (0 to 100) */
1681 ipp_attribute_t
*attr
; /* Job attribute */
1685 * Don't change completed jobs...
1688 if (job
->state_value
>= IPP_JOB_PROCESSING
)
1692 * Set the new priority and re-add the job into the active list...
1695 cupsArrayRemove(ActiveJobs
, job
);
1697 job
->priority
= priority
;
1699 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
1700 IPP_TAG_INTEGER
)) != NULL
)
1701 attr
->values
[0].integer
= priority
;
1703 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-priority",
1706 cupsArrayAdd(ActiveJobs
, job
);
1709 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1714 * 'cupsdStopAllJobs()' - Stop all print jobs.
1718 cupsdStopAllJobs(int force
) /* I - 1 = Force all filters to stop */
1720 cupsd_job_t
*job
; /* Current job */
1723 DEBUG_puts("cupsdStopAllJobs()");
1725 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
1727 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
1728 if (job
->state_value
== IPP_JOB_PROCESSING
)
1730 cupsdStopJob(job
, force
);
1731 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1732 job
->state_value
= IPP_JOB_PENDING
;
1738 * 'cupsdStopJob()' - Stop a print job.
1742 cupsdStopJob(cupsd_job_t
*job
, /* I - Job */
1743 int force
) /* I - 1 = Force all filters to stop */
1745 int i
; /* Looping var */
1748 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "cupsdStopJob: force = %d", force
);
1750 if (job
->state_value
!= IPP_JOB_PROCESSING
)
1753 FilterLevel
-= job
->cost
;
1755 if (job
->printer
->state
== IPP_PRINTER_PROCESSING
)
1756 cupsdSetPrinterState(job
->printer
, IPP_PRINTER_IDLE
, 0);
1758 job
->state
->values
[0].integer
= IPP_JOB_STOPPED
;
1759 job
->state_value
= IPP_JOB_STOPPED
;
1760 job
->printer
->job
= NULL
;
1761 job
->printer
= NULL
;
1763 job
->current_file
--;
1765 for (i
= 0; job
->filters
[i
]; i
++)
1766 if (job
->filters
[i
] > 0)
1768 cupsdEndProcess(job
->filters
[i
], force
);
1769 job
->filters
[i
] = 0;
1772 if (job
->backend
> 0)
1774 cupsdEndProcess(job
->backend
, force
);
1778 cupsdDestroyProfile(job
->profile
);
1779 job
->profile
= NULL
;
1781 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "Closing print pipes [ %d %d ]...",
1782 job
->print_pipes
[0], job
->print_pipes
[1]);
1784 cupsdClosePipe(job
->print_pipes
);
1786 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "Closing back pipes [ %d %d ]...",
1787 job
->back_pipes
[0], job
->back_pipes
[1]);
1789 cupsdClosePipe(job
->back_pipes
);
1791 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "Closing side pipes [ %d %d ]...",
1792 job
->side_pipes
[0], job
->side_pipes
[1]);
1794 cupsdClosePipe(job
->side_pipes
);
1796 if (job
->status_buffer
)
1799 * Close the pipe and clear the input bit.
1802 cupsdRemoveSelect(job
->status_buffer
->fd
);
1804 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "Closing status pipes [ %d %d ]...",
1805 job
->status_pipes
[0], job
->status_pipes
[1]);
1807 cupsdClosePipe(job
->status_pipes
);
1808 cupsdStatBufDelete(job
->status_buffer
);
1810 job
->status_buffer
= NULL
;
1816 * 'cupsdUnloadCompletedJobs()' - Flush completed job history from memory.
1820 cupsdUnloadCompletedJobs(void)
1822 cupsd_job_t
*job
; /* Current job */
1823 time_t expire
; /* Expiration time */
1826 expire
= time(NULL
) - 60;
1828 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1830 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1831 if (job
->attrs
&& job
->state_value
>= IPP_JOB_STOPPED
&&
1832 job
->access_time
< expire
)
1843 * 'compare_active_jobs()' - Compare the job IDs and priorities of two jobs.
1846 static int /* O - Difference */
1847 compare_active_jobs(void *first
, /* I - First job */
1848 void *second
, /* I - Second job */
1849 void *data
) /* I - App data (not used) */
1851 int diff
; /* Difference */
1854 if ((diff
= ((cupsd_job_t
*)second
)->priority
-
1855 ((cupsd_job_t
*)first
)->priority
) != 0)
1858 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
1863 * 'compare_jobs()' - Compare the job IDs of two jobs.
1866 static int /* O - Difference */
1867 compare_jobs(void *first
, /* I - First job */
1868 void *second
, /* I - Second job */
1869 void *data
) /* I - App data (not used) */
1871 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
1876 * 'ipp_length()' - Compute the size of the buffer needed to hold
1877 * the textual IPP attributes.
1880 static int /* O - Size of attribute buffer */
1881 ipp_length(ipp_t
*ipp
) /* I - IPP request */
1883 int bytes
; /* Number of bytes */
1884 int i
; /* Looping var */
1885 ipp_attribute_t
*attr
; /* Current attribute */
1889 * Loop through all attributes...
1894 for (attr
= ipp
->attrs
; attr
!= NULL
; attr
= attr
->next
)
1897 * Skip attributes that won't be sent to filters...
1900 if (attr
->value_tag
== IPP_TAG_MIMETYPE
||
1901 attr
->value_tag
== IPP_TAG_NAMELANG
||
1902 attr
->value_tag
== IPP_TAG_TEXTLANG
||
1903 attr
->value_tag
== IPP_TAG_URI
||
1904 attr
->value_tag
== IPP_TAG_URISCHEME
)
1907 if (strncmp(attr
->name
, "time-", 5) == 0)
1911 * Add space for a leading space and commas between each value.
1912 * For the first attribute, the leading space isn't used, so the
1913 * extra byte can be used as the nul terminator...
1916 bytes
++; /* " " separator */
1917 bytes
+= attr
->num_values
; /* "," separators */
1920 * Boolean attributes appear as "foo,nofoo,foo,nofoo", while
1921 * other attributes appear as "foo=value1,value2,...,valueN".
1924 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
1925 bytes
+= strlen(attr
->name
);
1927 bytes
+= attr
->num_values
* strlen(attr
->name
);
1930 * Now add the size required for each value in the attribute...
1933 switch (attr
->value_tag
)
1935 case IPP_TAG_INTEGER
:
1938 * Minimum value of a signed integer is -2147483647, or 11 digits.
1941 bytes
+= attr
->num_values
* 11;
1944 case IPP_TAG_BOOLEAN
:
1946 * Add two bytes for each false ("no") value...
1949 for (i
= 0; i
< attr
->num_values
; i
++)
1950 if (!attr
->values
[i
].boolean
)
1954 case IPP_TAG_RANGE
:
1956 * A range is two signed integers separated by a hyphen, or
1957 * 23 characters max.
1960 bytes
+= attr
->num_values
* 23;
1963 case IPP_TAG_RESOLUTION
:
1965 * A resolution is two signed integers separated by an "x" and
1966 * suffixed by the units, or 26 characters max.
1969 bytes
+= attr
->num_values
* 26;
1972 case IPP_TAG_STRING
:
1975 case IPP_TAG_KEYWORD
:
1976 case IPP_TAG_CHARSET
:
1977 case IPP_TAG_LANGUAGE
:
1980 * Strings can contain characters that need quoting. We need
1981 * at least 2 * len + 2 characters to cover the quotes and
1982 * any backslashes in the string.
1985 for (i
= 0; i
< attr
->num_values
; i
++)
1986 bytes
+= 2 * strlen(attr
->values
[i
].string
.text
) + 2;
1990 break; /* anti-compiler-warning-code */
1999 * 'load_job_cache()' - Load jobs from the job.cache file.
2003 load_job_cache(const char *filename
) /* I - job.cache filename */
2005 cups_file_t
*fp
; /* job.cache file */
2006 char line
[1024], /* Line buffer */
2007 *value
; /* Value on line */
2008 int linenum
; /* Line number in file */
2009 cupsd_job_t
*job
; /* Current job */
2010 int jobid
; /* Job ID */
2011 char jobfile
[1024]; /* Job filename */
2015 * Open the job.cache file...
2018 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
2020 if (errno
!= ENOENT
)
2021 cupsdLogMessage(CUPSD_LOG_ERROR
,
2022 "Unable to open job cache file \"%s\": %s",
2023 filename
, strerror(errno
));
2025 load_request_root();
2031 * Read entries from the job cache file and create jobs as needed.
2034 cupsdLogMessage(CUPSD_LOG_INFO
, "Loading job cache file \"%s\"...",
2040 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
2042 if (!strcasecmp(line
, "NextJobId"))
2045 NextJobId
= atoi(value
);
2047 else if (!strcasecmp(line
, "<Job"))
2051 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing </Job> directive on line %d!",
2058 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing job ID on line %d!", linenum
);
2062 jobid
= atoi(value
);
2066 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad job ID %d on line %d!", jobid
,
2071 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, jobid
);
2072 if (access(jobfile
, 0))
2074 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Job %d] Files have gone away!",
2079 job
= calloc(1, sizeof(cupsd_job_t
));
2082 cupsdLogMessage(CUPSD_LOG_EMERG
,
2083 "[Job %d] Unable to allocate memory for job!", jobid
);
2088 job
->back_pipes
[0] = -1;
2089 job
->back_pipes
[1] = -1;
2090 job
->print_pipes
[0] = -1;
2091 job
->print_pipes
[1] = -1;
2092 job
->side_pipes
[0] = -1;
2093 job
->side_pipes
[1] = -1;
2094 job
->status_pipes
[0] = -1;
2095 job
->status_pipes
[1] = -1;
2097 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Loading from cache...");
2101 cupsdLogMessage(CUPSD_LOG_ERROR
,
2102 "Missing <Job #> directive on line %d!", linenum
);
2105 else if (!strcasecmp(line
, "</Job>"))
2107 cupsArrayAdd(Jobs
, job
);
2109 if (job
->state_value
<= IPP_JOB_STOPPED
)
2111 cupsArrayAdd(ActiveJobs
, job
);
2119 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing value on line %d!", linenum
);
2122 else if (!strcasecmp(line
, "State"))
2124 job
->state_value
= (ipp_jstate_t
)atoi(value
);
2126 if (job
->state_value
< IPP_JOB_PENDING
)
2127 job
->state_value
= IPP_JOB_PENDING
;
2128 else if (job
->state_value
> IPP_JOB_COMPLETED
)
2129 job
->state_value
= IPP_JOB_COMPLETED
;
2131 else if (!strcasecmp(line
, "Priority"))
2133 job
->priority
= atoi(value
);
2135 else if (!strcasecmp(line
, "Username"))
2137 cupsdSetString(&job
->username
, value
);
2139 else if (!strcasecmp(line
, "Destination"))
2141 cupsdSetString(&job
->dest
, value
);
2143 else if (!strcasecmp(line
, "DestType"))
2145 job
->dtype
= (cups_ptype_t
)atoi(value
);
2147 else if (!strcasecmp(line
, "NumFiles"))
2149 job
->num_files
= atoi(value
);
2151 if (job
->num_files
< 0)
2153 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad NumFiles value %d on line %d!",
2154 job
->num_files
, linenum
);
2159 if (job
->num_files
> 0)
2161 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-001", RequestRoot
,
2163 if (access(jobfile
, 0))
2165 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Data files have gone away!");
2170 job
->filetypes
= calloc(job
->num_files
, sizeof(mime_type_t
*));
2171 job
->compressions
= calloc(job
->num_files
, sizeof(int));
2173 if (!job
->filetypes
|| !job
->compressions
)
2175 cupsdLogJob(job
, CUPSD_LOG_EMERG
,
2176 "Unable to allocate memory for %d files!",
2182 else if (!strcasecmp(line
, "File"))
2184 int number
, /* File number */
2185 compression
; /* Compression value */
2186 char super
[MIME_MAX_SUPER
], /* MIME super type */
2187 type
[MIME_MAX_TYPE
]; /* MIME type */
2190 if (sscanf(value
, "%d%*[ \t]%15[^/]/%255s%d", &number
, super
, type
,
2193 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File on line %d!", linenum
);
2197 if (number
< 1 || number
> job
->num_files
)
2199 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File number %d on line %d!",
2206 job
->compressions
[number
] = compression
;
2207 job
->filetypes
[number
] = mimeType(MimeDatabase
, super
, type
);
2209 if (!job
->filetypes
[number
])
2212 * If the original MIME type is unknown, auto-type it!
2215 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
2216 "Unknown MIME type %s/%s for file %d!",
2217 super
, type
, number
+ 1);
2219 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
2220 job
->id
, number
+ 1);
2221 job
->filetypes
[number
] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
2222 job
->compressions
+ number
);
2225 * If that didn't work, assume it is raw...
2228 if (!job
->filetypes
[number
])
2229 job
->filetypes
[number
] = mimeType(MimeDatabase
, "application",
2234 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unknown %s directive on line %d!",
2243 * 'load_next_job_id()' - Load the NextJobId value from the job.cache file.
2247 load_next_job_id(const char *filename
) /* I - job.cache filename */
2249 cups_file_t
*fp
; /* job.cache file */
2250 char line
[1024], /* Line buffer */
2251 *value
; /* Value on line */
2252 int linenum
; /* Line number in file */
2253 int next_job_id
; /* NextJobId value from line */
2257 * Read the NextJobId directive from the job.cache file and use
2258 * the value (if any).
2261 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
2263 if (errno
!= ENOENT
)
2264 cupsdLogMessage(CUPSD_LOG_ERROR
,
2265 "Unable to open job cache file \"%s\": %s",
2266 filename
, strerror(errno
));
2271 cupsdLogMessage(CUPSD_LOG_INFO
,
2272 "Loading NextJobId from job cache file \"%s\"...", filename
);
2276 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
2278 if (!strcasecmp(line
, "NextJobId"))
2282 next_job_id
= atoi(value
);
2284 if (next_job_id
> NextJobId
)
2285 NextJobId
= next_job_id
;
2296 * 'load_request_root()' - Load jobs from the RequestRoot directory.
2300 load_request_root(void)
2302 cups_dir_t
*dir
; /* Directory */
2303 cups_dentry_t
*dent
; /* Directory entry */
2304 cupsd_job_t
*job
; /* New job */
2308 * Open the requests directory...
2311 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Scanning %s for jobs...", RequestRoot
);
2313 if ((dir
= cupsDirOpen(RequestRoot
)) == NULL
)
2315 cupsdLogMessage(CUPSD_LOG_ERROR
,
2316 "Unable to open spool directory \"%s\": %s",
2317 RequestRoot
, strerror(errno
));
2322 * Read all the c##### files...
2325 while ((dent
= cupsDirRead(dir
)) != NULL
)
2326 if (strlen(dent
->filename
) >= 6 && dent
->filename
[0] == 'c')
2329 * Allocate memory for the job...
2332 if ((job
= calloc(sizeof(cupsd_job_t
), 1)) == NULL
)
2334 cupsdLogMessage(CUPSD_LOG_ERROR
, "Ran out of memory for jobs!");
2340 * Assign the job ID...
2343 job
->id
= atoi(dent
->filename
+ 1);
2344 job
->back_pipes
[0] = -1;
2345 job
->back_pipes
[1] = -1;
2346 job
->print_pipes
[0] = -1;
2347 job
->print_pipes
[1] = -1;
2348 job
->side_pipes
[0] = -1;
2349 job
->side_pipes
[1] = -1;
2350 job
->status_pipes
[0] = -1;
2351 job
->status_pipes
[1] = -1;
2353 if (job
->id
>= NextJobId
)
2354 NextJobId
= job
->id
+ 1;
2363 * Insert the job into the array, sorting by job priority and ID...
2366 cupsArrayAdd(Jobs
, job
);
2368 if (job
->state_value
<= IPP_JOB_STOPPED
)
2369 cupsArrayAdd(ActiveJobs
, job
);
2379 * 'set_time()' - Set one of the "time-at-xyz" attributes...
2383 set_time(cupsd_job_t
*job
, /* I - Job to update */
2384 const char *name
) /* I - Name of attribute */
2386 ipp_attribute_t
*attr
; /* Time attribute */
2389 if ((attr
= ippFindAttribute(job
->attrs
, name
, IPP_TAG_ZERO
)) != NULL
)
2391 attr
->value_tag
= IPP_TAG_INTEGER
;
2392 attr
->values
[0].integer
= time(NULL
);
2398 * 'set_hold_until()' - Set the hold time and update job-hold-until attribute...
2402 set_hold_until(cupsd_job_t
*job
, /* I - Job to update */
2403 time_t holdtime
) /* I - Hold until time */
2405 ipp_attribute_t
*attr
; /* job-hold-until attribute */
2406 struct tm
*holddate
; /* Hold date */
2407 char holdstr
[64]; /* Hold time */
2411 * Set the hold_until value and hold the job...
2414 cupsdLogMessage(CUPSD_LOG_DEBUG
, "set_hold_until: hold_until = %d",
2417 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
2418 job
->state_value
= IPP_JOB_HELD
;
2419 job
->hold_until
= holdtime
;
2422 * Update the job-hold-until attribute with a string representing GMT
2423 * time (HH:MM:SS)...
2426 holddate
= gmtime(&holdtime
);
2427 snprintf(holdstr
, sizeof(holdstr
), "%d:%d:%d", holddate
->tm_hour
,
2428 holddate
->tm_min
, holddate
->tm_sec
);
2430 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2431 IPP_TAG_KEYWORD
)) == NULL
)
2432 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2435 * Either add the attribute or update the value of the existing one
2439 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "job-hold-until",
2442 cupsdSetString(&attr
->values
[0].string
.text
, holdstr
);
2445 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2450 * 'start_job()' - Start a print job.
2454 start_job(cupsd_job_t
*job
, /* I - Job ID */
2455 cupsd_printer_t
*printer
) /* I - Printer to print job */
2457 int i
; /* Looping var */
2458 int slot
; /* Pipe slot */
2459 cups_array_t
*filters
, /* Filters for job */
2460 *prefilters
; /* Filters with prefilters */
2461 mime_filter_t
*filter
, /* Current filter */
2462 *prefilter
, /* Prefilter */
2463 port_monitor
; /* Port monitor filter */
2464 char method
[255], /* Method for output */
2465 *optptr
, /* Pointer to options */
2466 *valptr
; /* Pointer in value string */
2467 ipp_attribute_t
*attr
; /* Current attribute */
2468 struct stat backinfo
; /* Backend file information */
2469 int backroot
; /* Run backend as root? */
2470 int pid
; /* Process ID of new filter process */
2471 int banner_page
; /* 1 if banner page, 0 otherwise */
2472 int filterfds
[2][2];/* Pipes used between filters */
2473 int envc
; /* Number of environment variables */
2474 char **argv
, /* Filter command-line arguments */
2475 sani_uri
[1024], /* Sanitized DEVICE_URI env var */
2476 filename
[1024], /* Job filename */
2477 command
[1024], /* Full path to command */
2478 jobid
[255], /* Job ID string */
2479 title
[IPP_MAX_NAME
],
2480 /* Job title string */
2481 copies
[255], /* # copies string */
2482 *envp
[MAX_ENV
+ 16],
2483 /* Environment variables */
2484 charset
[255], /* CHARSET env variable */
2485 class_name
[255],/* CLASS env variable */
2486 classification
[1024],
2487 /* CLASSIFICATION env variable */
2489 /* CONTENT_TYPE env variable */
2491 /* DEVICE_URI env variable */
2492 final_content_type
[1024],
2493 /* FINAL_CONTENT_TYPE env variable */
2494 lang
[255], /* LANG env variable */
2496 apple_language
[255],
2497 /* APPLE_LANGUAGE env variable */
2498 #endif /* __APPLE__ */
2499 ppd
[1024], /* PPD env variable */
2501 /* PRINTER env variable */
2503 /* RIP_MAX_CACHE env variable */
2504 static char *options
= NULL
;/* Full list of options */
2505 static int optlength
= 0; /* Length of option buffer */
2508 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "start_job: file = %d/%d",
2509 job
->current_file
, job
->num_files
);
2511 if (job
->num_files
== 0)
2513 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "No files, canceling job!");
2514 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2519 * Figure out what filters are required to convert from
2520 * the source to the destination type...
2529 * Remote jobs and raw queues go directly to the printer without
2533 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Sending job to queue tagged as raw...");
2540 * Local jobs get filtered...
2543 filters
= mimeFilter(MimeDatabase
, job
->filetypes
[job
->current_file
],
2544 printer
->filetype
, &(job
->cost
));
2548 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
2549 "Unable to convert file %d to printable format!",
2551 cupsdLogMessage(CUPSD_LOG_INFO
,
2552 "Hint: Do you have Ghostscript installed?");
2554 if (LogLevel
< CUPSD_LOG_DEBUG
)
2555 cupsdLogMessage(CUPSD_LOG_INFO
,
2556 "Hint: Try setting the LogLevel to \"debug\".");
2558 job
->current_file
++;
2560 if (job
->current_file
== job
->num_files
)
2561 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2567 * Remove NULL ("-") filters...
2570 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
2572 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
2573 if (!strcmp(filter
->filter
, "-"))
2574 cupsArrayRemove(filters
, filter
);
2576 if (cupsArrayCount(filters
) == 0)
2578 cupsArrayDelete(filters
);
2583 * If this printer has any pre-filters, insert the required pre-filter
2584 * in the filters array...
2587 if (printer
->prefiltertype
&& filters
)
2589 prefilters
= cupsArrayNew(NULL
, NULL
);
2591 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
2593 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
2595 if ((prefilter
= mimeFilterLookup(MimeDatabase
, filter
->src
,
2596 printer
->prefiltertype
)))
2598 cupsArrayAdd(prefilters
, prefilter
);
2599 job
->cost
+= prefilter
->cost
;
2602 cupsArrayAdd(prefilters
, filter
);
2605 cupsArrayDelete(filters
);
2606 filters
= prefilters
;
2611 * Set a minimum cost of 100 for all jobs so that FilterLimit
2612 * works with raw queues and other low-cost paths.
2615 if (job
->cost
< 100)
2619 * See if the filter cost is too high...
2622 if ((FilterLevel
+ job
->cost
) > FilterLimit
&& FilterLevel
> 0 &&
2626 * Don't print this job quite yet...
2629 cupsArrayDelete(filters
);
2631 cupsdLogJob(job
, CUPSD_LOG_INFO
,
2632 "Holding because filter limit has been reached.");
2633 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
2634 "start_job: file=%d, cost=%d, level=%d, limit=%d",
2635 job
->current_file
, job
->cost
, FilterLevel
,
2640 FilterLevel
+= job
->cost
;
2643 * Add decompression/raw filter as needed...
2646 if ((!printer
->raw
&& job
->compressions
[job
->current_file
]) ||
2647 (!filters
&& !printer
->remote
&&
2648 (job
->num_files
> 1 || !strncmp(printer
->device_uri
, "file:", 5))))
2651 * Add gziptoany filter to the front of the list...
2655 filters
= cupsArrayNew(NULL
, NULL
);
2657 if (!cupsArrayInsert(filters
, &gziptoany_filter
))
2659 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
2660 "Unable to add decompression filter - %s", strerror(errno
));
2662 cupsArrayDelete(filters
);
2664 job
->current_file
++;
2666 if (job
->current_file
== job
->num_files
)
2667 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2674 * Add port monitor, if any...
2677 if (printer
->port_monitor
)
2680 * Add port monitor to the end of the list...
2684 filters
= cupsArrayNew(NULL
, NULL
);
2686 port_monitor
.src
= NULL
;
2687 port_monitor
.dst
= NULL
;
2688 port_monitor
.cost
= 0;
2690 snprintf(port_monitor
.filter
, sizeof(port_monitor
.filter
),
2691 "%s/monitor/%s", ServerBin
, printer
->port_monitor
);
2693 if (!cupsArrayAdd(filters
, &port_monitor
))
2695 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
2696 "Unable to add port monitor - %s", strerror(errno
));
2698 cupsArrayDelete(filters
);
2700 job
->current_file
++;
2702 if (job
->current_file
== job
->num_files
)
2703 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2710 * Make sure we don't go over the "MAX_FILTERS" limit...
2713 if (cupsArrayCount(filters
) > MAX_FILTERS
)
2715 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
2716 "Too many filters (%d > %d), unable to print!",
2717 cupsArrayCount(filters
), MAX_FILTERS
);
2719 cupsArrayDelete(filters
);
2720 cupsdCancelJob(job
, 0, IPP_JOB_STOPPED
);
2726 * Update the printer and job state to "processing"...
2729 job
->state
->values
[0].integer
= IPP_JOB_PROCESSING
;
2730 job
->state_value
= IPP_JOB_PROCESSING
;
2733 job
->printer
= printer
;
2736 cupsdSetPrinterState(printer
, IPP_PRINTER_PROCESSING
, 0);
2738 if (job
->current_file
== 0)
2741 * Add to the printing list...
2744 cupsArrayAdd(PrintingJobs
, job
);
2745 cupsdSetBusyState();
2748 * Set the processing time...
2751 set_time(job
, "time-at-processing");
2754 * Create the backchannel pipes and make them non-blocking...
2757 cupsdOpenPipe(job
->back_pipes
);
2759 fcntl(job
->back_pipes
[0], F_SETFL
,
2760 fcntl(job
->back_pipes
[0], F_GETFL
) | O_NONBLOCK
);
2762 fcntl(job
->back_pipes
[1], F_SETFL
,
2763 fcntl(job
->back_pipes
[1], F_GETFL
) | O_NONBLOCK
);
2766 * Create the side-channel pipes and make them non-blocking...
2769 socketpair(AF_LOCAL
, SOCK_STREAM
, 0, job
->side_pipes
);
2771 fcntl(job
->side_pipes
[0], F_SETFL
,
2772 fcntl(job
->side_pipes
[0], F_GETFL
) | O_NONBLOCK
);
2774 fcntl(job
->side_pipes
[1], F_SETFL
,
2775 fcntl(job
->side_pipes
[1], F_GETFL
) | O_NONBLOCK
);
2779 * Determine if we are printing a banner page or not...
2782 if (job
->job_sheets
== NULL
)
2784 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "No job-sheets attribute.");
2785 if ((job
->job_sheets
=
2786 ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_ZERO
)) != NULL
)
2787 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
2788 "... but someone added one without setting job_sheets!");
2790 else if (job
->job_sheets
->num_values
== 1)
2791 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "job-sheets=%s",
2792 job
->job_sheets
->values
[0].string
.text
);
2794 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "job-sheets=%s,%s",
2795 job
->job_sheets
->values
[0].string
.text
,
2796 job
->job_sheets
->values
[1].string
.text
);
2798 if (printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
))
2800 else if (job
->job_sheets
== NULL
)
2802 else if (strcasecmp(job
->job_sheets
->values
[0].string
.text
, "none") != 0 &&
2803 job
->current_file
== 0)
2805 else if (job
->job_sheets
->num_values
> 1 &&
2806 strcasecmp(job
->job_sheets
->values
[1].string
.text
, "none") != 0 &&
2807 job
->current_file
== (job
->num_files
- 1))
2812 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "banner_page = %d", banner_page
);
2815 * Building the options string is harder than it needs to be, but
2816 * for the moment we need to pass strings for command-line args and
2817 * not IPP attribute pointers... :)
2819 * First allocate/reallocate the option buffer as needed...
2822 i
= ipp_length(job
->attrs
);
2824 if (i
> optlength
|| !options
)
2829 optptr
= realloc(options
, i
);
2833 cupsdLogJob(job
, CUPSD_LOG_CRIT
,
2834 "Unable to allocate %d bytes for option buffer!", i
);
2836 cupsArrayDelete(filters
);
2838 FilterLevel
-= job
->cost
;
2840 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2849 * Now loop through the attributes and convert them to the textual
2850 * representation used by the filters...
2856 snprintf(title
, sizeof(title
), "%s-%d", printer
->name
, job
->id
);
2857 strcpy(copies
, "1");
2859 for (attr
= job
->attrs
->attrs
; attr
!= NULL
; attr
= attr
->next
)
2861 if (!strcmp(attr
->name
, "copies") &&
2862 attr
->value_tag
== IPP_TAG_INTEGER
)
2865 * Don't use the # copies attribute if we are printing the job sheets...
2869 sprintf(copies
, "%d", attr
->values
[0].integer
);
2871 else if (!strcmp(attr
->name
, "job-name") &&
2872 (attr
->value_tag
== IPP_TAG_NAME
||
2873 attr
->value_tag
== IPP_TAG_NAMELANG
))
2874 strlcpy(title
, attr
->values
[0].string
.text
, sizeof(title
));
2875 else if (attr
->group_tag
== IPP_TAG_JOB
)
2878 * Filter out other unwanted attributes...
2881 if (attr
->value_tag
== IPP_TAG_MIMETYPE
||
2882 attr
->value_tag
== IPP_TAG_NAMELANG
||
2883 attr
->value_tag
== IPP_TAG_TEXTLANG
||
2884 (attr
->value_tag
== IPP_TAG_URI
&& strcmp(attr
->name
, "job-uuid")) ||
2885 attr
->value_tag
== IPP_TAG_URISCHEME
||
2886 attr
->value_tag
== IPP_TAG_BEGIN_COLLECTION
) /* Not yet supported */
2889 if (!strncmp(attr
->name
, "time-", 5))
2892 if (!strncmp(attr
->name
, "job-", 4) && strcmp(attr
->name
, "job-uuid") &&
2893 strcmp(attr
->name
, "job-impressions") &&
2894 strcmp(attr
->name
, "job-originating-host-name") &&
2895 !(printer
->type
& CUPS_PRINTER_REMOTE
))
2898 if (!strncmp(attr
->name
, "job-", 4) &&
2899 strcmp(attr
->name
, "job-uuid") &&
2900 strcmp(attr
->name
, "job-billing") &&
2901 strcmp(attr
->name
, "job-impressions") &&
2902 strcmp(attr
->name
, "job-sheets") &&
2903 strcmp(attr
->name
, "job-hold-until") &&
2904 strcmp(attr
->name
, "job-priority"))
2907 if ((!strcmp(attr
->name
, "job-impressions") ||
2908 !strcmp(attr
->name
, "page-label") ||
2909 !strcmp(attr
->name
, "page-border") ||
2910 !strncmp(attr
->name
, "number-up", 9) ||
2911 !strcmp(attr
->name
, "page-ranges") ||
2912 !strcmp(attr
->name
, "page-set") ||
2913 !strcasecmp(attr
->name
, "AP_FIRSTPAGE_InputSlot") ||
2914 !strcasecmp(attr
->name
, "AP_FIRSTPAGE_ManualFeed") ||
2915 !strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
2916 "PMTotalSidesImaged..n.") ||
2917 !strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
2918 "PMTotalBeginPages..n.")) &&
2923 * Otherwise add them to the list...
2926 if (optptr
> options
)
2927 strlcat(optptr
, " ", optlength
- (optptr
- options
));
2929 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
2931 strlcat(optptr
, attr
->name
, optlength
- (optptr
- options
));
2932 strlcat(optptr
, "=", optlength
- (optptr
- options
));
2935 for (i
= 0; i
< attr
->num_values
; i
++)
2938 strlcat(optptr
, ",", optlength
- (optptr
- options
));
2940 optptr
+= strlen(optptr
);
2942 switch (attr
->value_tag
)
2944 case IPP_TAG_INTEGER
:
2946 snprintf(optptr
, optlength
- (optptr
- options
),
2947 "%d", attr
->values
[i
].integer
);
2950 case IPP_TAG_BOOLEAN
:
2951 if (!attr
->values
[i
].boolean
)
2952 strlcat(optptr
, "no", optlength
- (optptr
- options
));
2954 case IPP_TAG_NOVALUE
:
2955 strlcat(optptr
, attr
->name
,
2956 optlength
- (optptr
- options
));
2959 case IPP_TAG_RANGE
:
2960 if (attr
->values
[i
].range
.lower
== attr
->values
[i
].range
.upper
)
2961 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2962 "%d", attr
->values
[i
].range
.lower
);
2964 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2965 "%d-%d", attr
->values
[i
].range
.lower
,
2966 attr
->values
[i
].range
.upper
);
2969 case IPP_TAG_RESOLUTION
:
2970 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2971 "%dx%d%s", attr
->values
[i
].resolution
.xres
,
2972 attr
->values
[i
].resolution
.yres
,
2973 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
2977 case IPP_TAG_STRING
:
2980 case IPP_TAG_KEYWORD
:
2981 case IPP_TAG_CHARSET
:
2982 case IPP_TAG_LANGUAGE
:
2984 for (valptr
= attr
->values
[i
].string
.text
; *valptr
;)
2986 if (strchr(" \t\n\\\'\"", *valptr
))
2988 *optptr
++ = *valptr
++;
2995 break; /* anti-compiler-warning-code */
2999 optptr
+= strlen(optptr
);
3004 * Build the command-line arguments for the filters. Each filter
3005 * has 6 or 7 arguments:
3009 * argv[2] = username
3011 * argv[4] = # copies
3013 * argv[6] = filename (optional; normally stdin)
3015 * This allows legacy printer drivers that use the old System V
3016 * printing interface to be used by CUPS.
3018 * For remote jobs, we send all of the files in the argument list.
3021 if (printer
->remote
&& job
->num_files
> 1)
3022 argv
= calloc(7 + job
->num_files
, sizeof(char *));
3024 argv
= calloc(8, sizeof(char *));
3028 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to allocate argument array!");
3029 cupsArrayDelete(filters
);
3031 FilterLevel
-= job
->cost
;
3033 cupsdStopPrinter(printer
, 0);
3037 sprintf(jobid
, "%d", job
->id
);
3039 argv
[0] = printer
->name
;
3041 argv
[2] = job
->username
;
3046 if (printer
->remote
&& job
->num_files
> 1)
3048 for (i
= 0; i
< job
->num_files
; i
++)
3050 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
3052 argv
[6 + i
] = strdup(filename
);
3057 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
3058 job
->id
, job
->current_file
+ 1);
3062 for (i
= 0; argv
[i
]; i
++)
3063 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
3064 "argv[%d]=\"%s\"", i
, argv
[i
]);
3067 * Create environment variable strings for the filters...
3070 attr
= ippFindAttribute(job
->attrs
, "attributes-natural-language",
3074 strcpy(apple_language
, "APPLE_LANGUAGE=");
3075 _cupsAppleLanguage(attr
->values
[0].string
.text
,
3076 apple_language
+ 15, sizeof(apple_language
) - 15);
3077 #endif /* __APPLE__ */
3079 switch (strlen(attr
->values
[0].string
.text
))
3083 * This is an unknown or badly formatted language code; use
3084 * the POSIX locale...
3087 strcpy(lang
, "LANG=C");
3092 * Just the language code (ll)...
3095 snprintf(lang
, sizeof(lang
), "LANG=%s.UTF8",
3096 attr
->values
[0].string
.text
);
3101 * Language and country code (ll-cc)...
3104 snprintf(lang
, sizeof(lang
), "LANG=%c%c_%c%c.UTF8",
3105 attr
->values
[0].string
.text
[0],
3106 attr
->values
[0].string
.text
[1],
3107 toupper(attr
->values
[0].string
.text
[3] & 255),
3108 toupper(attr
->values
[0].string
.text
[4] & 255));
3112 attr
= ippFindAttribute(job
->attrs
, "document-format",
3115 (optptr
= strstr(attr
->values
[0].string
.text
, "charset=")) != NULL
)
3116 snprintf(charset
, sizeof(charset
), "CHARSET=%s", optptr
+ 8);
3119 attr
= ippFindAttribute(job
->attrs
, "attributes-charset",
3121 snprintf(charset
, sizeof(charset
), "CHARSET=%s",
3122 attr
->values
[0].string
.text
);
3125 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s/%s",
3126 job
->filetypes
[job
->current_file
]->super
,
3127 job
->filetypes
[job
->current_file
]->type
);
3128 snprintf(device_uri
, sizeof(device_uri
), "DEVICE_URI=%s",
3129 printer
->device_uri
);
3130 cupsdSanitizeURI(printer
->device_uri
, sani_uri
, sizeof(sani_uri
));
3131 snprintf(ppd
, sizeof(ppd
), "PPD=%s/ppd/%s.ppd", ServerRoot
, printer
->name
);
3132 snprintf(printer_name
, sizeof(printer_name
), "PRINTER=%s", printer
->name
);
3133 snprintf(rip_max_cache
, sizeof(rip_max_cache
), "RIP_MAX_CACHE=%s", RIPCache
);
3135 envc
= cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
3137 envp
[envc
++] = charset
;
3138 envp
[envc
++] = lang
;
3140 envp
[envc
++] = apple_language
;
3141 #endif /* __APPLE__ */
3142 envp
[envc
++] = ppd
;
3143 envp
[envc
++] = rip_max_cache
;
3144 envp
[envc
++] = content_type
;
3145 envp
[envc
++] = device_uri
;
3146 envp
[envc
++] = printer_name
;
3148 if (!printer
->remote
&& !printer
->raw
)
3150 filter
= (mime_filter_t
*)cupsArrayLast(filters
);
3152 if (printer
->port_monitor
)
3153 filter
= (mime_filter_t
*)cupsArrayPrev(filters
);
3155 if (filter
&& filter
->dst
)
3157 snprintf(final_content_type
, sizeof(final_content_type
),
3158 "FINAL_CONTENT_TYPE=%s/%s",
3159 filter
->dst
->super
, filter
->dst
->type
);
3160 envp
[envc
++] = final_content_type
;
3164 if (Classification
&& !banner_page
)
3166 if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
3167 IPP_TAG_NAME
)) == NULL
)
3168 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3170 else if (attr
->num_values
> 1 &&
3171 strcmp(attr
->values
[1].string
.text
, "none") != 0)
3172 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3173 attr
->values
[1].string
.text
);
3175 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3176 attr
->values
[0].string
.text
);
3178 envp
[envc
++] = classification
;
3181 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
3183 snprintf(class_name
, sizeof(class_name
), "CLASS=%s", job
->dest
);
3184 envp
[envc
++] = class_name
;
3187 if (job
->auth_username
)
3188 envp
[envc
++] = job
->auth_username
;
3189 if (job
->auth_domain
)
3190 envp
[envc
++] = job
->auth_domain
;
3191 if (job
->auth_password
)
3192 envp
[envc
++] = job
->auth_password
;
3196 envp
[envc
++] = job
->ccname
;
3197 #endif /* HAVE_GSSAPI */
3201 for (i
= 0; i
< envc
; i
++)
3202 if (!strncmp(envp
[i
], "AUTH_", 5))
3203 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"AUTH_%c****\"", i
,
3205 else if (strncmp(envp
[i
], "DEVICE_URI=", 11))
3206 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"%s\"", i
, envp
[i
]);
3208 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"DEVICE_URI=%s\"", i
,
3211 if (printer
->remote
)
3212 job
->current_file
= job
->num_files
;
3214 job
->current_file
++;
3217 * Now create processes for all of the filters...
3220 filterfds
[0][0] = -1;
3221 filterfds
[0][1] = -1;
3222 filterfds
[1][0] = -1;
3223 filterfds
[1][1] = -1;
3225 if (!job
->status_buffer
)
3227 if (cupsdOpenPipe(job
->status_pipes
))
3229 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
3230 "Unable to create job status pipes - %s.", strerror(errno
));
3231 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3232 "Unable to create status pipes - %s.", strerror(errno
));
3234 cupsdAddPrinterHistory(printer
);
3236 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3237 "Job canceled because the server could not create the job "
3243 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3244 "start_job: status_pipes = [ %d %d ]",
3245 job
->status_pipes
[0], job
->status_pipes
[1]);
3247 job
->status_buffer
= cupsdStatBufNew(job
->status_pipes
[0], NULL
);
3248 job
->status_level
= CUPSD_LOG_INFO
;
3252 memset(job
->filters
, 0, sizeof(job
->filters
));
3255 job
->profile
= cupsdCreateProfile(job
->id
);
3257 for (i
= 0, slot
= 0, filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
3259 i
++, filter
= (mime_filter_t
*)cupsArrayNext(filters
))
3261 if (filter
->filter
[0] != '/')
3262 snprintf(command
, sizeof(command
), "%s/filter/%s", ServerBin
,
3265 strlcpy(command
, filter
->filter
, sizeof(command
));
3267 if (i
< (cupsArrayCount(filters
) - 1))
3269 if (cupsdOpenPipe(filterfds
[slot
]))
3271 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
3272 "Unable to create job filter pipes - %s.", strerror(errno
));
3273 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3274 "Unable to create filter pipes - %s.", strerror(errno
));
3275 cupsdAddPrinterHistory(printer
);
3277 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3278 "Job canceled because the server could not create the "
3286 if (job
->current_file
== 1)
3288 if (strncmp(printer
->device_uri
, "file:", 5) != 0)
3290 if (cupsdOpenPipe(job
->print_pipes
))
3292 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
3293 "Unable to create job backend pipes - %s.",
3295 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3296 "Unable to create backend pipes - %s.", strerror(errno
));
3297 cupsdAddPrinterHistory(printer
);
3299 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3300 "Job canceled because the server could not create "
3301 "the backend pipes.");
3308 job
->print_pipes
[0] = -1;
3309 if (!strcmp(printer
->device_uri
, "file:/dev/null") ||
3310 !strcmp(printer
->device_uri
, "file:///dev/null"))
3311 job
->print_pipes
[1] = -1;
3314 if (!strncmp(printer
->device_uri
, "file:/dev/", 10))
3315 job
->print_pipes
[1] = open(printer
->device_uri
+ 5,
3317 else if (!strncmp(printer
->device_uri
, "file:///dev/", 12))
3318 job
->print_pipes
[1] = open(printer
->device_uri
+ 7,
3320 else if (!strncmp(printer
->device_uri
, "file:///", 8))
3321 job
->print_pipes
[1] = open(printer
->device_uri
+ 7,
3322 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
3324 job
->print_pipes
[1] = open(printer
->device_uri
+ 5,
3325 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
3327 if (job
->print_pipes
[1] < 0)
3329 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
3330 "Unable to open output file \"%s\" - %s.",
3331 printer
->device_uri
, strerror(errno
));
3332 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3333 "Unable to open output file \"%s\" - %s.",
3334 printer
->device_uri
, strerror(errno
));
3336 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3337 "Job canceled because the server could not open the "
3343 fcntl(job
->print_pipes
[1], F_SETFD
,
3344 fcntl(job
->print_pipes
[1], F_GETFD
) | FD_CLOEXEC
);
3348 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3349 "start_job: print_pipes = [ %d %d ]",
3350 job
->print_pipes
[0], job
->print_pipes
[1]);
3353 filterfds
[slot
][0] = job
->print_pipes
[0];
3354 filterfds
[slot
][1] = job
->print_pipes
[1];
3357 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "start_job: filter=\"%s\"", command
);
3358 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "start_job: filterfds[%d]=[ %d %d ]",
3359 slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3361 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
3362 filterfds
[slot
][1], job
->status_pipes
[1],
3363 job
->back_pipes
[0], job
->side_pipes
[0], 0,
3364 job
->profile
, job
->filters
+ i
);
3366 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3367 "start_job: Closing filter pipes for slot %d [ %d %d ]...",
3368 !slot
, filterfds
[!slot
][0], filterfds
[!slot
][1]);
3370 cupsdClosePipe(filterfds
[!slot
]);
3374 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Unable to start filter \"%s\" - %s.",
3375 filter
->filter
, strerror(errno
));
3376 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3377 "Unable to start filter \"%s\" - %s.",
3378 filter
->filter
, strerror(errno
));
3380 cupsdAddPrinterHistory(printer
);
3382 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3383 "Job canceled because the server could not execute a "
3389 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Started filter %s (PID %d)", command
,
3396 cupsArrayDelete(filters
);
3400 * Finally, pipe the final output into a backend process if needed...
3403 if (strncmp(printer
->device_uri
, "file:", 5) != 0)
3405 if (job
->current_file
== 1 || printer
->remote
)
3407 sscanf(printer
->device_uri
, "%254[^:]", method
);
3408 snprintf(command
, sizeof(command
), "%s/backend/%s", ServerBin
, method
);
3411 * See if the backend needs to run as root...
3416 else if (stat(command
, &backinfo
))
3419 backroot
= !(backinfo
.st_mode
& (S_IRWXG
| S_IRWXO
));
3423 filterfds
[slot
][0] = -1;
3424 filterfds
[slot
][1] = -1;
3426 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "start_job: backend=\"%s\"", command
);
3427 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "start_job: filterfds[%d] = [ %d %d ]",
3428 slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3430 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
3431 filterfds
[slot
][1], job
->status_pipes
[1],
3432 job
->back_pipes
[1], job
->side_pipes
[1],
3433 backroot
, job
->profile
, &(job
->backend
));
3437 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Unable to start backend \"%s\" - %s.",
3438 method
, strerror(errno
));
3439 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3440 "Unable to start backend \"%s\" - %s.", method
,
3443 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3444 "Job canceled because the server could not execute "
3451 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Started backend %s (PID %d)",
3456 if (job
->current_file
== job
->num_files
)
3458 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3459 "start_job: Closing print pipes [ %d %d ]...",
3460 job
->print_pipes
[0], job
->print_pipes
[1]);
3462 cupsdClosePipe(job
->print_pipes
);
3464 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3465 "start_job: Closing back pipes [ %d %d ]...",
3466 job
->back_pipes
[0], job
->back_pipes
[1]);
3468 cupsdClosePipe(job
->back_pipes
);
3470 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3471 "start_job: Closing side pipes [ %d %d ]...",
3472 job
->side_pipes
[0], job
->side_pipes
[1]);
3474 cupsdClosePipe(job
->side_pipes
);
3476 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3477 "start_job: Closing status output pipe %d...",
3478 job
->status_pipes
[1]);
3480 close(job
->status_pipes
[1]);
3481 job
->status_pipes
[1] = -1;
3486 filterfds
[slot
][0] = -1;
3487 filterfds
[slot
][1] = -1;
3489 if (job
->current_file
== job
->num_files
)
3491 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3492 "start_job: Closing print pipes [ %d %d ]...",
3493 job
->print_pipes
[0], job
->print_pipes
[1]);
3495 cupsdClosePipe(job
->print_pipes
);
3497 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3498 "start_job: Closing status output pipe %d...",
3499 job
->status_pipes
[1]);
3501 close(job
->status_pipes
[1]);
3502 job
->status_pipes
[1] = -1;
3506 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3507 "start_job: Closing filter pipes for slot %d [ %d %d ]...",
3508 slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3509 cupsdClosePipe(filterfds
[slot
]);
3511 if (printer
->remote
&& job
->num_files
> 1)
3513 for (i
= 0; i
< job
->num_files
; i
++)
3519 cupsdAddSelect(job
->status_buffer
->fd
, (cupsd_selfunc_t
)update_job
, NULL
,
3522 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
, "Job #%d started.",
3529 * If we get here, we need to abort the current job and close out all
3530 * files and pipes...
3535 for (slot
= 0; slot
< 2; slot
++)
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
]);
3543 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3544 "start_job: Closing status pipes [ %d %d ]...",
3545 job
->status_pipes
[0], job
->status_pipes
[1]);
3546 cupsdClosePipe(job
->status_pipes
);
3547 cupsdStatBufDelete(job
->status_buffer
);
3549 job
->status_buffer
= NULL
;
3551 cupsArrayDelete(filters
);
3553 if (printer
->remote
&& job
->num_files
> 1)
3555 for (i
= 0; i
< job
->num_files
; i
++)
3561 cupsdStopJob(job
, 0);
3566 * 'unload_job()' - Unload a job from memory.
3570 unload_job(cupsd_job_t
*job
) /* I - Job */
3575 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Unloading...");
3577 ippDelete(job
->attrs
);
3582 job
->job_sheets
= NULL
;
3583 job
->printer_message
= NULL
;
3584 job
->printer_reasons
= NULL
;
3589 * 'update_job()' - Read a status update from a job's filters.
3593 update_job(cupsd_job_t
*job
) /* I - Job to check */
3595 int i
; /* Looping var */
3596 int copies
; /* Number of copies printed */
3597 char message
[1024], /* Message text */
3598 *ptr
; /* Pointer update... */
3599 int loglevel
, /* Log level for message */
3600 event
= 0; /* Events? */
3603 while ((ptr
= cupsdStatBufUpdate(job
->status_buffer
, &loglevel
,
3604 message
, sizeof(message
))) != NULL
)
3607 * Process page and printer state messages as needed...
3610 if (loglevel
== CUPSD_LOG_PAGE
)
3613 * Page message; send the message to the page_log file and update the
3614 * job sheet count...
3617 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "PAGE: %s", message
);
3621 if (!strncasecmp(message
, "total ", 6))
3624 * Got a total count of pages from a backend or filter...
3627 copies
= atoi(message
+ 6);
3628 copies
-= job
->sheets
->values
[0].integer
; /* Just track the delta */
3630 else if (!sscanf(message
, "%*d%d", &copies
))
3633 job
->sheets
->values
[0].integer
+= copies
;
3635 if (job
->printer
->page_limit
)
3637 cupsd_quota_t
*q
= cupsdUpdateQuota(job
->printer
, job
->username
,
3641 if (AppleQuotas
&& q
->page_count
== -3)
3644 * Quota limit exceeded, cancel job in progress immediately...
3647 cupsdLogJob(job
, CUPSD_LOG_INFO
,
3648 "Canceled because pages exceed user %s "
3649 "quota limit on printer %s (%s).",
3650 job
->username
, job
->printer
->name
,
3651 job
->printer
->info
);
3653 cupsdCancelJob(job
, 1, IPP_JOB_CANCELED
);
3658 #endif /* __APPLE__ */
3662 cupsdLogPage(job
, message
);
3665 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
3666 "Printed %d page(s).", job
->sheets
->values
[0].integer
);
3668 else if (loglevel
== CUPSD_LOG_STATE
)
3670 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "STATE: %s", message
);
3672 if (!strcmp(message
, "paused"))
3674 cupsdStopPrinter(job
->printer
, 1);
3679 cupsdSetPrinterReasons(job
->printer
, message
);
3680 cupsdAddPrinterHistory(job
->printer
);
3681 event
|= CUPSD_EVENT_PRINTER_STATE
;
3684 update_job_attrs(job
, 0);
3686 else if (loglevel
== CUPSD_LOG_ATTR
)
3689 * Set attribute(s)...
3692 int num_attrs
; /* Number of attributes */
3693 cups_option_t
*attrs
; /* Attributes */
3694 const char *attr
; /* Attribute */
3697 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "ATTR: %s", message
);
3699 num_attrs
= cupsParseOptions(message
, 0, &attrs
);
3701 if ((attr
= cupsGetOption("auth-info-required", num_attrs
,
3704 cupsdSetAuthInfoRequired(job
->printer
, attr
, NULL
);
3705 cupsdSetPrinterAttrs(job
->printer
);
3707 if (job
->printer
->type
& CUPS_PRINTER_DISCOVERED
)
3708 cupsdMarkDirty(CUPSD_DIRTY_REMOTE
);
3710 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3713 if ((attr
= cupsGetOption("printer-alert", num_attrs
, attrs
)) != NULL
)
3715 cupsdSetString(&job
->printer
->alert
, attr
);
3716 event
|= CUPSD_EVENT_PRINTER_STATE
;
3719 if ((attr
= cupsGetOption("printer-alert-description", num_attrs
,
3722 cupsdSetString(&job
->printer
->alert_description
, attr
);
3723 event
|= CUPSD_EVENT_PRINTER_STATE
;
3726 if ((attr
= cupsGetOption("marker-colors", num_attrs
, attrs
)) != NULL
)
3728 cupsdSetPrinterAttr(job
->printer
, "marker-colors", (char *)attr
);
3729 job
->printer
->marker_time
= time(NULL
);
3730 event
|= CUPSD_EVENT_PRINTER_STATE
;
3733 if ((attr
= cupsGetOption("marker-levels", num_attrs
, attrs
)) != NULL
)
3735 cupsdSetPrinterAttr(job
->printer
, "marker-levels", (char *)attr
);
3736 job
->printer
->marker_time
= time(NULL
);
3737 event
|= CUPSD_EVENT_PRINTER_STATE
;
3740 if ((attr
= cupsGetOption("marker-message", num_attrs
, attrs
)) != NULL
)
3742 cupsdSetPrinterAttr(job
->printer
, "marker-message", (char *)attr
);
3743 job
->printer
->marker_time
= time(NULL
);
3744 event
|= CUPSD_EVENT_PRINTER_STATE
;
3747 if ((attr
= cupsGetOption("marker-names", num_attrs
, attrs
)) != NULL
)
3749 cupsdSetPrinterAttr(job
->printer
, "marker-names", (char *)attr
);
3750 job
->printer
->marker_time
= time(NULL
);
3751 event
|= CUPSD_EVENT_PRINTER_STATE
;
3754 if ((attr
= cupsGetOption("marker-types", num_attrs
, attrs
)) != NULL
)
3756 cupsdSetPrinterAttr(job
->printer
, "marker-types", (char *)attr
);
3757 job
->printer
->marker_time
= time(NULL
);
3758 event
|= CUPSD_EVENT_PRINTER_STATE
;
3761 cupsFreeOptions(num_attrs
, attrs
);
3763 else if (loglevel
== CUPSD_LOG_PPD
)
3766 * Set attribute(s)...
3769 int num_keywords
; /* Number of keywords */
3770 cups_option_t
*keywords
; /* Keywords */
3773 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "PPD: %s", message
);
3775 num_keywords
= cupsParseOptions(message
, 0, &keywords
);
3777 if (cupsdUpdatePrinterPPD(job
->printer
, num_keywords
, keywords
))
3778 cupsdSetPrinterAttrs(job
->printer
);
3780 cupsFreeOptions(num_keywords
, keywords
);
3783 else if (!strncmp(message
, "recoverable:", 12))
3785 cupsdSetPrinterReasons(job
->printer
,
3786 "+com.apple.print.recoverable-warning");
3789 while (isspace(*ptr
& 255))
3792 cupsdSetString(&job
->printer
->recoverable
, ptr
);
3793 cupsdAddPrinterHistory(job
->printer
);
3794 event
|= CUPSD_EVENT_PRINTER_STATE
;
3796 else if (!strncmp(message
, "recovered:", 10))
3798 cupsdSetPrinterReasons(job
->printer
,
3799 "-com.apple.print.recoverable-warning");
3802 while (isspace(*ptr
& 255))
3805 cupsdSetString(&job
->printer
->recoverable
, ptr
);
3806 cupsdAddPrinterHistory(job
->printer
);
3807 event
|= CUPSD_EVENT_PRINTER_STATE
;
3809 #endif /* __APPLE__ */
3812 cupsdLogJob(job
, loglevel
, "%s", message
);
3814 if (loglevel
< CUPSD_LOG_DEBUG
)
3816 strlcpy(job
->printer
->state_message
, message
,
3817 sizeof(job
->printer
->state_message
));
3818 cupsdAddPrinterHistory(job
->printer
);
3820 event
|= CUPSD_EVENT_PRINTER_STATE
;
3822 if (loglevel
<= job
->status_level
)
3825 * Some messages show in the printer-state-message attribute...
3828 if (loglevel
!= CUPSD_LOG_NOTICE
)
3829 job
->status_level
= loglevel
;
3831 update_job_attrs(job
, 1);
3836 if (!strchr(job
->status_buffer
->buffer
, '\n'))
3840 if (event
& CUPSD_EVENT_PRINTER_STATE
)
3841 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, job
->printer
, NULL
,
3842 (job
->printer
->type
& CUPS_PRINTER_CLASS
) ?
3843 "Class \"%s\" state changed." :
3844 "Printer \"%s\" state changed.",
3845 job
->printer
->name
);
3847 if (ptr
== NULL
&& !job
->status_buffer
->bufused
)
3850 * See if all of the filters and the backend have returned their
3854 for (i
= 0; job
->filters
[i
] < 0; i
++);
3856 if (job
->filters
[i
])
3859 if (job
->current_file
>= job
->num_files
&& job
->backend
> 0)
3863 * Handle the end of job stuff...
3866 cupsdFinishJob(job
);
3872 * 'update_job_attrs()' - Update the job-printer-* attributes.
3876 update_job_attrs(cupsd_job_t
*job
, /* I - Job to update */
3877 int do_message
)/* I - 1 = update job-printer-state message */
3879 int i
; /* Looping var */
3880 int num_reasons
; /* Actual number of reasons */
3881 const char * const *reasons
; /* Reasons */
3882 static const char *none
= "none", /* "none" */
3888 * Get/create the job-printer-state-* attributes...
3891 if (!job
->printer_message
)
3893 if ((job
->printer_message
= ippFindAttribute(job
->attrs
,
3894 "job-printer-state-message",
3895 IPP_TAG_TEXT
)) == NULL
)
3896 job
->printer_message
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_TEXT
,
3897 "job-printer-state-message",
3901 if (!job
->printer_reasons
)
3902 job
->printer_reasons
= ippFindAttribute(job
->attrs
,
3903 "job-printer-state-reasons",
3907 * If the job isn't printing, return now...
3914 * Otherwise copy the printer-state-message value...
3917 if (job
->printer
->state_message
[0] &&
3918 (do_message
|| !job
->printer_message
->values
[0].string
.text
[0]))
3919 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
),
3920 job
->printer
->state_message
);
3923 * ... and the printer-state-reasons value...
3926 if (job
->printer
->num_reasons
== 0)
3929 reasons
= job
->printer
->state
== IPP_PRINTER_STOPPED
? &paused
: &none
;
3933 num_reasons
= job
->printer
->num_reasons
;
3934 reasons
= (const char * const *)job
->printer
->reasons
;
3937 if (!job
->printer_reasons
|| job
->printer_reasons
->num_values
!= num_reasons
)
3939 ippDeleteAttribute(job
->attrs
, job
->printer_reasons
);
3941 job
->printer_reasons
= ippAddStrings(job
->attrs
,
3942 IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
3943 "job-printer-state-reasons",
3944 num_reasons
, NULL
, NULL
);
3947 for (i
= 0; i
< num_reasons
; i
++)
3948 cupsdSetString(&(job
->printer_reasons
->values
[i
].string
.text
), reasons
[i
]);
3953 * End of "$Id: job.c 7682 2008-06-21 00:06:02Z mike $".