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 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 * cupsdFinishJob() - Finish a job.
25 * cupsdFreeAllJobs() - Free all jobs from memory.
26 * cupsdFindJob() - Find the specified job.
27 * cupsdGetPrinterJobCount() - Get the number of pending, processing,
28 * cupsdGetUserJobCount() - Get the number of pending, processing,
29 * cupsdHoldJob() - Hold the specified job.
30 * cupsdLoadAllJobs() - Load all jobs from disk.
31 * cupsdLoadJob() - Load a single job...
32 * cupsdMoveJob() - Move the specified job to a different
34 * cupsdReleaseJob() - Release the specified job.
35 * cupsdRestartJob() - Restart the specified job.
36 * cupsdSaveAllJobs() - Save a summary of all jobs to disk.
37 * cupsdSaveJob() - Save a job to disk.
38 * cupsdSetJobHoldUntil() - Set the hold time for a job...
39 * cupsdSetJobPriority() - Set the priority of a job, moving it
40 * up/down in the list as needed.
41 * cupsdStopAllJobs() - Stop all print jobs.
42 * cupsdStopJob() - Stop a print job.
43 * cupsdUnloadCompletedJobs() - Flush completed job history from memory.
44 * compare_active_jobs() - Compare the job IDs and priorities of two
46 * compare_jobs() - Compare the job IDs of two jobs.
47 * free_job() - Free all memory used by a job.
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 void free_job(cupsd_job_t
*job
);
93 static int ipp_length(ipp_t
*ipp
);
94 static void load_job_cache(const char *filename
);
95 static void load_next_job_id(const char *filename
);
96 static void load_request_root(void);
97 static void set_time(cupsd_job_t
*job
, const char *name
);
98 static void set_hold_until(cupsd_job_t
*job
, time_t holdtime
);
99 static void start_job(cupsd_job_t
*job
, cupsd_printer_t
*printer
);
100 static void unload_job(cupsd_job_t
*job
);
101 static void update_job(cupsd_job_t
*job
);
102 static void update_job_attrs(cupsd_job_t
*job
);
106 * 'cupsdAddJob()' - Add a new job to the job queue...
109 cupsd_job_t
* /* O - New job record */
110 cupsdAddJob(int priority
, /* I - Job priority */
111 const char *dest
) /* I - Job destination */
113 cupsd_job_t
*job
; /* New job record */
116 job
= calloc(sizeof(cupsd_job_t
), 1);
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 list...
216 cupsArrayRemove(ActiveJobs
, job
);
219 * Remove any authentication data...
222 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
223 if (cupsdRemoveFile(filename
) && errno
!= ENOENT
)
224 cupsdLogMessage(CUPSD_LOG_ERROR
,
225 "Unable to remove authentication cache: %s",
228 cupsdClearString(&job
->auth_username
);
229 cupsdClearString(&job
->auth_domain
);
230 cupsdClearString(&job
->auth_password
);
234 * Destroy the credential cache and clear the KRB5CCNAME env var string.
239 krb5_cc_destroy(KerberosContext
, job
->ccache
);
243 cupsdClearString(&job
->ccname
);
244 #endif /* HAVE_GSSAPI */
247 * Remove the print file for good if we aren't preserving jobs or
251 job
->current_file
= 0;
253 if (!JobHistory
|| !JobFiles
|| purge
)
255 for (i
= 1; i
<= job
->num_files
; i
++)
257 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
262 if (job
->num_files
> 0)
264 free(job
->filetypes
);
265 free(job
->compressions
);
268 job
->filetypes
= NULL
;
269 job
->compressions
= NULL
;
273 if (JobHistory
&& !purge
)
276 * Save job state info...
284 * Remove the job info file...
287 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
,
292 * Remove the job from the "all jobs" list...
295 cupsArrayRemove(Jobs
, job
);
298 * Free all memory used...
307 * 'cupsdCancelJobs()' - Cancel all jobs for the given destination/user...
311 cupsdCancelJobs(const char *dest
, /* I - Destination to cancel */
312 const char *username
, /* I - Username or NULL */
313 int purge
) /* I - Purge jobs? */
315 cupsd_job_t
*job
; /* Current job */
318 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
320 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
322 if (!job
->dest
|| !job
->username
)
325 if (!job
->dest
|| !job
->username
)
328 if ((dest
== NULL
|| !strcmp(job
->dest
, dest
)) &&
329 (username
== NULL
|| !strcmp(job
->username
, username
)))
332 * Cancel all jobs matching this destination/user...
335 cupsdCancelJob(job
, purge
, IPP_JOB_CANCELED
);
344 * 'cupsdCheckJobs()' - Check the pending jobs and start any if the destination
351 cupsd_job_t
*job
; /* Current job in queue */
352 cupsd_printer_t
*printer
, /* Printer destination */
353 *pclass
; /* Printer class destination */
356 DEBUG_puts("cupsdCheckJobs()");
358 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
359 "cupsdCheckJobs: %d active jobs, sleeping=%d, reload=%d",
360 cupsArrayCount(ActiveJobs
), Sleeping
, NeedReload
);
362 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
364 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
367 * Start held jobs if they are ready...
370 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
371 "cupsdCheckJobs: Job %d: state_value=%d, loaded=%s",
372 job
->id
, job
->state_value
, job
->attrs
? "yes" : "no");
374 if (job
->state_value
== IPP_JOB_HELD
&&
376 job
->hold_until
< time(NULL
))
378 if (job
->pending_timeout
)
379 cupsdTimeoutJob(job
); /* Add trailing banner as needed */
381 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
382 job
->state_value
= IPP_JOB_PENDING
;
386 * Start pending jobs if the destination is available...
389 if (job
->state_value
== IPP_JOB_PENDING
&& !NeedReload
&& !Sleeping
)
391 printer
= cupsdFindDest(job
->dest
);
395 (printer
->type
& (CUPS_PRINTER_IMPLICIT
| CUPS_PRINTER_CLASS
)))
398 * If the class is remote, just pass it to the remote server...
403 if (pclass
->state
== IPP_PRINTER_STOPPED
)
405 else if (pclass
->type
& CUPS_PRINTER_REMOTE
)
408 printer
= cupsdFindAvailablePrinter(printer
->name
);
411 if (!printer
&& !pclass
)
414 * Whoa, the printer and/or class for this destination went away;
418 cupsdLogMessage(CUPSD_LOG_WARN
,
419 "[Job %d] Printer/class %s has gone away; canceling job!",
422 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
423 "Job canceled because the destination printer/class has "
426 cupsdCancelJob(job
, 1, IPP_JOB_ABORTED
);
431 * See if the printer is available or remote and not printing a job;
432 * if so, start the job...
438 * Add/update a job-actual-printer-uri attribute for this job
439 * so that we know which printer actually printed the job...
442 ipp_attribute_t
*attr
; /* job-actual-printer-uri attribute */
445 if ((attr
= ippFindAttribute(job
->attrs
, "job-actual-printer-uri",
446 IPP_TAG_URI
)) != NULL
)
447 cupsdSetString(&attr
->values
[0].string
.text
, printer
->uri
);
449 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
,
450 "job-actual-printer-uri", NULL
, printer
->uri
);
453 if ((!(printer
->type
& CUPS_PRINTER_DISCOVERED
) && /* Printer is local */
454 printer
->state
== IPP_PRINTER_IDLE
) || /* and idle */
455 ((printer
->type
& CUPS_PRINTER_DISCOVERED
) && /* Printer is remote */
456 !printer
->job
)) /* and not printing */
459 * Clear any message and reasons for the queue...
462 printer
->state_message
[0] = '\0';
464 cupsdSetPrinterReasons(printer
, "none");
470 start_job(job
, printer
);
479 * 'cupsdCleanJobs()' - Clean out old jobs.
485 cupsd_job_t
*job
; /* Current job */
491 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
492 job
&& cupsArrayCount(Jobs
) >= MaxJobs
;
493 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
494 if (job
->state_value
>= IPP_JOB_CANCELED
)
495 cupsdCancelJob(job
, 1, IPP_JOB_CANCELED
);
500 * 'cupsdFinishJob()' - Finish a job.
504 cupsdFinishJob(cupsd_job_t
*job
) /* I - Job */
506 cupsd_printer_t
*printer
; /* Current printer */
507 ipp_attribute_t
*attr
; /* job-hold-until attribute */
510 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] File %d is complete.",
511 job
->id
, job
->current_file
- 1);
513 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
514 "[Job %d] cupsdFinishJob: job->status is %d",
515 job
->id
, job
->status
);
517 if (job
->status_buffer
&&
518 (job
->status
< 0 || job
->current_file
>= job
->num_files
))
521 * Close the pipe and clear the input bit.
524 cupsdRemoveSelect(job
->status_buffer
->fd
);
526 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
527 "[Job %d] cupsdFinishJob: Closing status pipes [ %d %d ]...",
528 job
->id
, job
->status_pipes
[0], job
->status_pipes
[1]);
530 cupsdClosePipe(job
->status_pipes
);
531 cupsdStatBufDelete(job
->status_buffer
);
533 job
->status_buffer
= NULL
;
536 printer
= job
->printer
;
538 update_job_attrs(job
);
543 * Backend had errors; stop it...
546 int exit_code
; /* Exit code from backend */
550 * Convert the status to an exit code. Due to the way the W* macros are
551 * implemented on MacOS X (bug?), we have to store the exit status in a
552 * variable first and then convert...
555 exit_code
= -job
->status
;
556 if (WIFEXITED(exit_code
))
557 exit_code
= WEXITSTATUS(exit_code
);
559 exit_code
= job
->status
;
561 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Backend returned status %d (%s)",
563 exit_code
== CUPS_BACKEND_FAILED
? "failed" :
564 exit_code
== CUPS_BACKEND_AUTH_REQUIRED
?
565 "authentication required" :
566 exit_code
== CUPS_BACKEND_HOLD
? "hold job" :
567 exit_code
== CUPS_BACKEND_STOP
? "stop printer" :
568 exit_code
== CUPS_BACKEND_CANCEL
? "cancel job" :
569 exit_code
< 0 ? "crashed" : "unknown");
572 * Do what needs to be done...
578 case CUPS_BACKEND_FAILED
:
580 * Backend failure, use the error-policy to determine how to
584 cupsdStopJob(job
, 0);
586 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
589 * Mark the job as pending again - we'll retry on another
593 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
594 job
->state_value
= IPP_JOB_PENDING
;
600 * If the job was queued to a class, try requeuing it... For
601 * faxes and retry-job queues, hold the current job for 5 minutes.
604 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
606 else if ((printer
->type
& CUPS_PRINTER_FAX
) ||
607 !strcmp(printer
->error_policy
, "retry-job"))
610 * See how many times we've tried to send the job; if more than
611 * the limit, cancel the job.
616 if (job
->tries
>= JobRetryLimit
)
622 cupsdLogMessage(CUPSD_LOG_ERROR
,
623 "[Job %d] Canceling job since it could not be "
624 "sent after %d tries.",
625 job
->id
, JobRetryLimit
);
627 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
632 * Try again in N seconds...
635 set_hold_until(job
, time(NULL
) + JobRetryInterval
);
637 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
,
638 "Job held due to fax errors; please consult "
639 "the error_log file for details.");
640 cupsdSetPrinterState(printer
, IPP_PRINTER_IDLE
, 0);
643 else if (!strcmp(printer
->error_policy
, "abort-job"))
644 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
647 cupsdSetPrinterState(printer
, IPP_PRINTER_STOPPED
, 1);
649 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
650 "Job stopped due to backend errors; please consult "
651 "the error_log file for details.");
655 case CUPS_BACKEND_CANCEL
:
660 cupsdCancelJob(job
, 0, IPP_JOB_CANCELED
);
663 case CUPS_BACKEND_HOLD
:
668 cupsdStopJob(job
, 0);
670 cupsdSetJobHoldUntil(job
, "indefinite");
672 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
673 job
->state_value
= IPP_JOB_HELD
;
677 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
678 "Job held due to backend errors; please consult "
679 "the error_log file for details.");
682 case CUPS_BACKEND_STOP
:
684 * Stop the printer...
687 cupsdStopJob(job
, 0);
689 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
690 job
->state_value
= IPP_JOB_PENDING
;
693 cupsdSetPrinterState(printer
, IPP_PRINTER_STOPPED
, 1);
695 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
696 "Job stopped due to backend errors; please consult "
697 "the error_log file for details.");
700 case CUPS_BACKEND_AUTH_REQUIRED
:
701 cupsdStopJob(job
, 0);
703 cupsdSetJobHoldUntil(job
, "auth-info-required");
705 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
706 IPP_TAG_KEYWORD
)) == NULL
)
707 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
711 attr
->value_tag
= IPP_TAG_KEYWORD
;
712 cupsdSetString(&(attr
->values
[0].string
.text
),
713 "auth-info-required");
716 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
717 job
->state_value
= IPP_JOB_HELD
;
721 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
722 "Authentication is required for job %d.", job
->id
);
727 * Try printing another job...
732 else if (job
->status
> 0)
735 * Filter had errors; stop job...
738 cupsdLogMessage(CUPSD_LOG_ERROR
,
739 "[Job %d] Job stopped due to filter errors.", job
->id
);
740 cupsdStopJob(job
, 1);
742 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
743 "Job stopped due to filter errors; please consult the "
744 "error_log file for details.");
750 * Job printed successfully; cancel it...
753 if (job
->current_file
< job
->num_files
)
756 * Start the next file in the job...
759 FilterLevel
-= job
->cost
;
760 start_job(job
, printer
);
765 * Close out this job...
768 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Completed successfully.",
770 cupsdCancelJob(job
, 0, IPP_JOB_COMPLETED
);
778 * 'cupsdFreeAllJobs()' - Free all jobs from memory.
782 cupsdFreeAllJobs(void)
784 cupsd_job_t
*job
; /* Current job */
795 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
797 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
799 cupsArrayRemove(Jobs
, job
);
800 cupsArrayRemove(ActiveJobs
, job
);
805 cupsdReleaseSignals();
810 * 'cupsdFindJob()' - Find the specified job.
813 cupsd_job_t
* /* O - Job data */
814 cupsdFindJob(int id
) /* I - Job ID */
816 cupsd_job_t key
; /* Search key */
821 return ((cupsd_job_t
*)cupsArrayFind(Jobs
, &key
));
826 * 'cupsdGetPrinterJobCount()' - Get the number of pending, processing,
827 * or held jobs in a printer or class.
830 int /* O - Job count */
831 cupsdGetPrinterJobCount(
832 const char *dest
) /* I - Printer or class name */
834 int count
; /* Job count */
835 cupsd_job_t
*job
; /* Current job */
838 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
840 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
841 if (job
->dest
&& !strcasecmp(job
->dest
, dest
))
849 * 'cupsdGetUserJobCount()' - Get the number of pending, processing,
850 * or held jobs for a user.
853 int /* O - Job count */
854 cupsdGetUserJobCount(
855 const char *username
) /* I - Username */
857 int count
; /* Job count */
858 cupsd_job_t
*job
; /* Current job */
861 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
863 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
864 if (!strcasecmp(job
->username
, username
))
872 * 'cupsdHoldJob()' - Hold the specified job.
876 cupsdHoldJob(cupsd_job_t
*job
) /* I - Job data */
878 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdHoldJob: id = %d", job
->id
);
880 if (job
->state_value
== IPP_JOB_PROCESSING
)
881 cupsdStopJob(job
, 0);
885 DEBUG_puts("cupsdHoldJob: setting state to held...");
887 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
888 job
->state_value
= IPP_JOB_HELD
;
889 job
->current_file
= 0;
898 * 'cupsdLoadAllJobs()' - Load all jobs from disk.
902 cupsdLoadAllJobs(void)
904 char filename
[1024]; /* Full filename of job.cache file */
905 struct stat fileinfo
, /* Information on job.cache file */
906 dirinfo
; /* Information on RequestRoot dir */
911 * Create the job arrays as needed...
915 Jobs
= cupsArrayNew(compare_jobs
, NULL
);
918 ActiveJobs
= cupsArrayNew(compare_active_jobs
, NULL
);
921 * See whether the job.cache file is older than the RequestRoot directory...
924 snprintf(filename
, sizeof(filename
), "%s/job.cache", CacheDir
);
926 if (stat(filename
, &fileinfo
))
928 fileinfo
.st_mtime
= 0;
931 cupsdLogMessage(CUPSD_LOG_ERROR
,
932 "Unable to get file information for \"%s\" - %s",
933 filename
, strerror(errno
));
936 if (stat(RequestRoot
, &dirinfo
))
938 dirinfo
.st_mtime
= 0;
941 cupsdLogMessage(CUPSD_LOG_ERROR
,
942 "Unable to get directory information for \"%s\" - %s",
943 RequestRoot
, strerror(errno
));
947 * Load the most recent source for job data...
950 if (dirinfo
.st_mtime
> fileinfo
.st_mtime
)
954 load_next_job_id(filename
);
957 load_job_cache(filename
);
960 * Clean out old jobs as needed...
963 if (MaxJobs
> 0 && cupsArrayCount(Jobs
) >= MaxJobs
)
969 * 'cupsdLoadJob()' - Load a single job...
973 cupsdLoadJob(cupsd_job_t
*job
) /* I - Job */
975 char jobfile
[1024]; /* Job filename */
976 cups_file_t
*fp
; /* Job file */
977 int fileid
; /* Current file ID */
978 ipp_attribute_t
*attr
; /* Job attribute */
979 const char *dest
; /* Destination name */
980 cupsd_printer_t
*destptr
; /* Pointer to destination */
981 mime_type_t
**filetypes
; /* New filetypes array */
982 int *compressions
; /* New compressions array */
987 if (job
->state_value
> IPP_JOB_STOPPED
)
988 job
->access_time
= time(NULL
);
993 if ((job
->attrs
= ippNew()) == NULL
)
995 cupsdLogMessage(CUPSD_LOG_ERROR
,
996 "[Job %d] Ran out of memory for job attributes!", job
->id
);
1001 * Load job attributes...
1004 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Loading attributes...", job
->id
);
1006 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, job
->id
);
1007 if ((fp
= cupsFileOpen(jobfile
, "r")) == NULL
)
1009 cupsdLogMessage(CUPSD_LOG_ERROR
,
1010 "[Job %d] Unable to open job control file \"%s\" - %s!",
1011 job
->id
, jobfile
, strerror(errno
));
1012 ippDelete(job
->attrs
);
1017 if (ippReadIO(fp
, (ipp_iocb_t
)cupsFileRead
, 1, NULL
, job
->attrs
) != IPP_DATA
)
1019 cupsdLogMessage(CUPSD_LOG_ERROR
,
1020 "[Job %d] Unable to read job control file \"%s\"!",
1023 ippDelete(job
->attrs
);
1032 * Copy attribute data to the job object...
1035 if ((job
->state
= ippFindAttribute(job
->attrs
, "job-state",
1036 IPP_TAG_ENUM
)) == NULL
)
1038 cupsdLogMessage(CUPSD_LOG_ERROR
,
1039 "[Job %d] Missing or bad job-state attribute in "
1042 ippDelete(job
->attrs
);
1048 job
->state_value
= (ipp_jstate_t
)job
->state
->values
[0].integer
;
1052 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1053 IPP_TAG_URI
)) == NULL
)
1055 cupsdLogMessage(CUPSD_LOG_ERROR
,
1056 "[Job %d] No job-printer-uri attribute in control file!",
1058 ippDelete(job
->attrs
);
1064 if ((dest
= cupsdValidateDest(attr
->values
[0].string
.text
, &(job
->dtype
),
1067 cupsdLogMessage(CUPSD_LOG_ERROR
,
1068 "[Job %d] Unable to queue job for destination \"%s\"!",
1069 job
->id
, attr
->values
[0].string
.text
);
1070 ippDelete(job
->attrs
);
1076 cupsdSetString(&job
->dest
, dest
);
1078 else if ((destptr
= cupsdFindDest(job
->dest
)) == NULL
)
1080 cupsdLogMessage(CUPSD_LOG_ERROR
,
1081 "[Job %d] Unable to queue job for destination \"%s\"!",
1082 job
->id
, job
->dest
);
1083 ippDelete(job
->attrs
);
1089 job
->sheets
= ippFindAttribute(job
->attrs
, "job-media-sheets-completed",
1091 job
->job_sheets
= ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_NAME
);
1095 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
1096 IPP_TAG_INTEGER
)) == NULL
)
1098 cupsdLogMessage(CUPSD_LOG_ERROR
,
1099 "[Job %d] Missing or bad job-priority attribute in "
1100 "control file!", job
->id
);
1101 ippDelete(job
->attrs
);
1107 job
->priority
= attr
->values
[0].integer
;
1112 if ((attr
= ippFindAttribute(job
->attrs
, "job-originating-user-name",
1113 IPP_TAG_NAME
)) == NULL
)
1115 cupsdLogMessage(CUPSD_LOG_ERROR
,
1116 "[Job %d] Missing or bad job-originating-user-name "
1117 "attribute in control file!", job
->id
);
1118 ippDelete(job
->attrs
);
1124 cupsdSetString(&job
->username
, attr
->values
[0].string
.text
);
1128 * Set the job hold-until time and state...
1131 if (job
->state_value
== IPP_JOB_HELD
)
1133 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1134 IPP_TAG_KEYWORD
)) == NULL
)
1135 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1138 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
1141 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1142 job
->state_value
= IPP_JOB_PENDING
;
1145 else if (job
->state_value
== IPP_JOB_PROCESSING
)
1147 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1148 job
->state_value
= IPP_JOB_PENDING
;
1151 if (!job
->num_files
)
1154 * Find all the d##### files...
1157 for (fileid
= 1; fileid
< 10000; fileid
++)
1159 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
1162 if (access(jobfile
, 0))
1165 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1166 "[Job %d] Auto-typing document file \"%s\"...",
1169 if (fileid
> job
->num_files
)
1171 if (job
->num_files
== 0)
1173 compressions
= (int *)calloc(fileid
, sizeof(int));
1174 filetypes
= (mime_type_t
**)calloc(fileid
, sizeof(mime_type_t
*));
1178 compressions
= (int *)realloc(job
->compressions
,
1179 sizeof(int) * fileid
);
1180 filetypes
= (mime_type_t
**)realloc(job
->filetypes
,
1181 sizeof(mime_type_t
*) *
1185 if (!compressions
|| !filetypes
)
1187 cupsdLogMessage(CUPSD_LOG_ERROR
,
1188 "[Job %d] Ran out of memory for job file types!",
1193 job
->compressions
= compressions
;
1194 job
->filetypes
= filetypes
;
1195 job
->num_files
= fileid
;
1198 job
->filetypes
[fileid
- 1] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
1199 job
->compressions
+ fileid
- 1);
1201 if (!job
->filetypes
[fileid
- 1])
1202 job
->filetypes
[fileid
- 1] = mimeType(MimeDatabase
, "application",
1208 * Load authentication information as needed...
1211 if (job
->state_value
< IPP_JOB_STOPPED
)
1213 snprintf(jobfile
, sizeof(jobfile
), "%s/a%05d", RequestRoot
, job
->id
);
1215 cupsdClearString(&job
->auth_username
);
1216 cupsdClearString(&job
->auth_domain
);
1217 cupsdClearString(&job
->auth_password
);
1219 if ((fp
= cupsFileOpen(jobfile
, "r")) != NULL
)
1221 int i
, /* Looping var */
1222 bytes
; /* Size of auth data */
1223 char line
[255], /* Line from file */
1224 data
[255]; /* Decoded data */
1228 i
< destptr
->num_auth_info_required
&&
1229 cupsFileGets(fp
, line
, sizeof(line
));
1232 bytes
= sizeof(data
);
1233 httpDecode64_2(data
, &bytes
, line
);
1235 if (!strcmp(destptr
->auth_info_required
[i
], "username"))
1236 cupsdSetStringf(&job
->auth_username
, "AUTH_USERNAME=%s", data
);
1237 else if (!strcmp(destptr
->auth_info_required
[i
], "domain"))
1238 cupsdSetStringf(&job
->auth_domain
, "AUTH_DOMAIN=%s", data
);
1239 else if (!strcmp(destptr
->auth_info_required
[i
], "password"))
1240 cupsdSetStringf(&job
->auth_password
, "AUTH_PASSWORD=%s", data
);
1246 job
->access_time
= time(NULL
);
1251 * 'cupsdMoveJob()' - Move the specified job to a different destination.
1255 cupsdMoveJob(cupsd_job_t
*job
, /* I - Job */
1256 cupsd_printer_t
*p
) /* I - Destination printer or class */
1258 ipp_attribute_t
*attr
; /* job-printer-uri attribute */
1259 const char *olddest
; /* Old destination */
1260 cupsd_printer_t
*oldp
; /* Old pointer */
1264 * Don't move completed jobs...
1267 if (job
->state_value
> IPP_JOB_STOPPED
)
1271 * Get the old destination...
1274 olddest
= job
->dest
;
1277 oldp
= job
->printer
;
1279 oldp
= cupsdFindDest(olddest
);
1282 * Change the destination information...
1285 if (job
->state_value
== IPP_JOB_PROCESSING
)
1286 cupsdStopJob(job
, 0);
1290 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, oldp
, job
,
1291 "Job #%d moved from %s to %s.", job
->id
, olddest
,
1294 cupsdSetString(&job
->dest
, p
->name
);
1295 job
->dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_REMOTE
|
1296 CUPS_PRINTER_IMPLICIT
);
1298 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1299 IPP_TAG_URI
)) != NULL
)
1300 cupsdSetString(&(attr
->values
[0].string
.text
), p
->uri
);
1302 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, p
, job
,
1303 "Job #%d moved from %s to %s.", job
->id
, olddest
,
1311 * 'cupsdReleaseJob()' - Release the specified job.
1315 cupsdReleaseJob(cupsd_job_t
*job
) /* I - Job */
1317 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdReleaseJob: id = %d", job
->id
);
1319 if (job
->state_value
== IPP_JOB_HELD
)
1321 DEBUG_puts("cupsdReleaseJob: setting state to pending...");
1323 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1324 job
->state_value
= IPP_JOB_PENDING
;
1332 * 'cupsdRestartJob()' - Restart the specified job.
1336 cupsdRestartJob(cupsd_job_t
*job
) /* I - Job */
1338 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdRestartJob: id = %d", job
->id
);
1340 if (job
->state_value
== IPP_JOB_STOPPED
|| job
->num_files
)
1342 ipp_jstate_t old_state
; /* Old job state */
1347 old_state
= job
->state_value
;
1350 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1351 job
->state_value
= IPP_JOB_PENDING
;
1355 if (old_state
> IPP_JOB_STOPPED
)
1356 cupsArrayAdd(ActiveJobs
, job
);
1364 * 'cupsdSaveAllJobs()' - Save a summary of all jobs to disk.
1368 cupsdSaveAllJobs(void)
1370 int i
; /* Looping var */
1371 cups_file_t
*fp
; /* Job cache file */
1372 char temp
[1024]; /* Temporary string */
1373 cupsd_job_t
*job
; /* Current job */
1374 time_t curtime
; /* Current time */
1375 struct tm
*curdate
; /* Current date */
1378 snprintf(temp
, sizeof(temp
), "%s/job.cache", CacheDir
);
1379 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
1381 cupsdLogMessage(CUPSD_LOG_ERROR
,
1382 "Unable to create job cache file \"%s\" - %s",
1383 temp
, strerror(errno
));
1387 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving job cache file \"%s\"...", temp
);
1390 * Restrict access to the file...
1393 fchown(cupsFileNumber(fp
), getuid(), Group
);
1394 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
1397 * Write a small header to the file...
1400 curtime
= time(NULL
);
1401 curdate
= localtime(&curtime
);
1402 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
1404 cupsFilePuts(fp
, "# Job cache file for " CUPS_SVERSION
"\n");
1405 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
1406 cupsFilePrintf(fp
, "NextJobId %d\n", NextJobId
);
1409 * Write each job known to the system...
1412 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1414 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1416 cupsFilePrintf(fp
, "<Job %d>\n", job
->id
);
1417 cupsFilePrintf(fp
, "State %d\n", job
->state_value
);
1418 cupsFilePrintf(fp
, "Priority %d\n", job
->priority
);
1419 cupsFilePrintf(fp
, "Username %s\n", job
->username
);
1420 cupsFilePrintf(fp
, "Destination %s\n", job
->dest
);
1421 cupsFilePrintf(fp
, "DestType %d\n", job
->dtype
);
1422 cupsFilePrintf(fp
, "NumFiles %d\n", job
->num_files
);
1423 for (i
= 0; i
< job
->num_files
; i
++)
1424 cupsFilePrintf(fp
, "File %d %s/%s %d\n", i
+ 1, job
->filetypes
[i
]->super
,
1425 job
->filetypes
[i
]->type
, job
->compressions
[i
]);
1426 cupsFilePuts(fp
, "</Job>\n");
1434 * 'cupsdSaveJob()' - Save a job to disk.
1438 cupsdSaveJob(cupsd_job_t
*job
) /* I - Job */
1440 char filename
[1024]; /* Job control filename */
1441 cups_file_t
*fp
; /* Job file */
1444 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSaveJob(job=%p(%d)): job->attrs=%p",
1445 job
, job
->id
, job
->attrs
);
1447 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
, job
->id
);
1449 if ((fp
= cupsFileOpen(filename
, "w")) == NULL
)
1451 cupsdLogMessage(CUPSD_LOG_ERROR
,
1452 "[Job %d] Unable to create job control file \"%s\" - %s.",
1453 job
->id
, filename
, strerror(errno
));
1457 fchmod(cupsFileNumber(fp
), 0600);
1458 fchown(cupsFileNumber(fp
), RunUser
, Group
);
1460 job
->attrs
->state
= IPP_IDLE
;
1462 if (ippWriteIO(fp
, (ipp_iocb_t
)cupsFileWrite
, 1, NULL
,
1463 job
->attrs
) != IPP_DATA
)
1464 cupsdLogMessage(CUPSD_LOG_ERROR
,
1465 "[Job %d] Unable to write job control file!", job
->id
);
1472 * 'cupsdSetJobHoldUntil()' - Set the hold time for a job...
1476 cupsdSetJobHoldUntil(cupsd_job_t
*job
, /* I - Job */
1477 const char *when
) /* I - When to resume */
1479 time_t curtime
; /* Current time */
1480 struct tm
*curdate
; /* Current date */
1481 int hour
; /* Hold hour */
1482 int minute
; /* Hold minute */
1483 int second
; /* Hold second */
1486 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetJobHoldUntil(%d, \"%s\")",
1491 if (!strcmp(when
, "indefinite") || !strcmp(when
, "auth-info-required"))
1494 * Hold indefinitely...
1497 job
->hold_until
= 0;
1499 else if (!strcmp(when
, "day-time"))
1502 * Hold to 6am the next morning unless local time is < 6pm.
1505 curtime
= time(NULL
);
1506 curdate
= localtime(&curtime
);
1508 if (curdate
->tm_hour
< 18)
1509 job
->hold_until
= curtime
;
1511 job
->hold_until
= curtime
+
1512 ((29 - curdate
->tm_hour
) * 60 + 59 -
1513 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1515 else if (!strcmp(when
, "evening") || !strcmp(when
, "night"))
1518 * Hold to 6pm unless local time is > 6pm or < 6am.
1521 curtime
= time(NULL
);
1522 curdate
= localtime(&curtime
);
1524 if (curdate
->tm_hour
< 6 || curdate
->tm_hour
>= 18)
1525 job
->hold_until
= curtime
;
1527 job
->hold_until
= curtime
+
1528 ((17 - curdate
->tm_hour
) * 60 + 59 -
1529 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1531 else if (!strcmp(when
, "second-shift"))
1534 * Hold to 4pm unless local time is > 4pm.
1537 curtime
= time(NULL
);
1538 curdate
= localtime(&curtime
);
1540 if (curdate
->tm_hour
>= 16)
1541 job
->hold_until
= curtime
;
1543 job
->hold_until
= curtime
+
1544 ((15 - curdate
->tm_hour
) * 60 + 59 -
1545 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1547 else if (!strcmp(when
, "third-shift"))
1550 * Hold to 12am unless local time is < 8am.
1553 curtime
= time(NULL
);
1554 curdate
= localtime(&curtime
);
1556 if (curdate
->tm_hour
< 8)
1557 job
->hold_until
= curtime
;
1559 job
->hold_until
= curtime
+
1560 ((23 - curdate
->tm_hour
) * 60 + 59 -
1561 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1563 else if (!strcmp(when
, "weekend"))
1566 * Hold to weekend unless we are in the weekend.
1569 curtime
= time(NULL
);
1570 curdate
= localtime(&curtime
);
1572 if (curdate
->tm_wday
|| curdate
->tm_wday
== 6)
1573 job
->hold_until
= curtime
;
1575 job
->hold_until
= curtime
+
1576 (((5 - curdate
->tm_wday
) * 24 +
1577 (17 - curdate
->tm_hour
)) * 60 + 59 -
1578 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1580 else if (sscanf(when
, "%d:%d:%d", &hour
, &minute
, &second
) >= 2)
1583 * Hold to specified GMT time (HH:MM or HH:MM:SS)...
1586 curtime
= time(NULL
);
1587 curdate
= gmtime(&curtime
);
1589 job
->hold_until
= curtime
+
1590 ((hour
- curdate
->tm_hour
) * 60 + minute
-
1591 curdate
->tm_min
) * 60 + second
- curdate
->tm_sec
;
1594 * Hold until next day as needed...
1597 if (job
->hold_until
< curtime
)
1598 job
->hold_until
+= 24 * 60 * 60;
1601 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetJobHoldUntil: hold_until = %d",
1602 (int)job
->hold_until
);
1607 * 'cupsdSetJobPriority()' - Set the priority of a job, moving it up/down in
1608 * the list as needed.
1612 cupsdSetJobPriority(
1613 cupsd_job_t
*job
, /* I - Job ID */
1614 int priority
) /* I - New priority (0 to 100) */
1616 ipp_attribute_t
*attr
; /* Job attribute */
1620 * Don't change completed jobs...
1623 if (job
->state_value
>= IPP_JOB_PROCESSING
)
1627 * Set the new priority and re-add the job into the active list...
1630 cupsArrayRemove(ActiveJobs
, job
);
1632 job
->priority
= priority
;
1634 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
1635 IPP_TAG_INTEGER
)) != NULL
)
1636 attr
->values
[0].integer
= priority
;
1638 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-priority",
1641 cupsArrayAdd(ActiveJobs
, job
);
1648 * 'cupsdStopAllJobs()' - Stop all print jobs.
1652 cupsdStopAllJobs(int force
) /* I - 1 = Force all filters to stop */
1654 cupsd_job_t
*job
; /* Current job */
1657 DEBUG_puts("cupsdStopAllJobs()");
1659 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
1661 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
1662 if (job
->state_value
== IPP_JOB_PROCESSING
)
1664 cupsdStopJob(job
, force
);
1665 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1666 job
->state_value
= IPP_JOB_PENDING
;
1672 * 'cupsdStopJob()' - Stop a print job.
1676 cupsdStopJob(cupsd_job_t
*job
, /* I - Job */
1677 int force
) /* I - 1 = Force all filters to stop */
1679 int i
; /* Looping var */
1682 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1683 "[Job %d] cupsdStopJob: force = %d", job
->id
, force
);
1685 if (job
->state_value
!= IPP_JOB_PROCESSING
)
1688 FilterLevel
-= job
->cost
;
1690 if (job
->printer
->state
== IPP_PRINTER_PROCESSING
)
1691 cupsdSetPrinterState(job
->printer
, IPP_PRINTER_IDLE
, 0);
1693 job
->state
->values
[0].integer
= IPP_JOB_STOPPED
;
1694 job
->state_value
= IPP_JOB_STOPPED
;
1695 job
->printer
->job
= NULL
;
1696 job
->printer
= NULL
;
1698 job
->current_file
--;
1700 for (i
= 0; job
->filters
[i
]; i
++)
1701 if (job
->filters
[i
] > 0)
1703 cupsdEndProcess(job
->filters
[i
], force
);
1704 job
->filters
[i
] = 0;
1707 if (job
->backend
> 0)
1709 cupsdEndProcess(job
->backend
, force
);
1713 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] Closing print pipes [ %d %d ]...",
1714 job
->id
, job
->print_pipes
[0], job
->print_pipes
[1]);
1716 cupsdClosePipe(job
->print_pipes
);
1718 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] Closing back pipes [ %d %d ]...",
1719 job
->id
, job
->back_pipes
[0], job
->back_pipes
[1]);
1721 cupsdClosePipe(job
->back_pipes
);
1723 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] Closing side pipes [ %d %d ]...",
1724 job
->id
, job
->side_pipes
[0], job
->side_pipes
[1]);
1726 cupsdClosePipe(job
->side_pipes
);
1728 if (job
->status_buffer
)
1731 * Close the pipe and clear the input bit.
1734 cupsdRemoveSelect(job
->status_buffer
->fd
);
1736 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1737 "[Job %d] Closing status pipes [ %d %d ]...", job
->id
,
1738 job
->status_pipes
[0], job
->status_pipes
[1]);
1740 cupsdClosePipe(job
->status_pipes
);
1741 cupsdStatBufDelete(job
->status_buffer
);
1743 job
->status_buffer
= NULL
;
1749 * 'cupsdUnloadCompletedJobs()' - Flush completed job history from memory.
1753 cupsdUnloadCompletedJobs(void)
1755 cupsd_job_t
*job
; /* Current job */
1756 time_t expire
; /* Expiration time */
1759 expire
= time(NULL
) - 60;
1761 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1763 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1764 if (job
->attrs
&& job
->state_value
>= IPP_JOB_STOPPED
&&
1765 job
->access_time
< expire
)
1771 * 'compare_active_jobs()' - Compare the job IDs and priorities of two jobs.
1774 static int /* O - Difference */
1775 compare_active_jobs(void *first
, /* I - First job */
1776 void *second
, /* I - Second job */
1777 void *data
) /* I - App data (not used) */
1779 int diff
; /* Difference */
1782 if ((diff
= ((cupsd_job_t
*)second
)->priority
-
1783 ((cupsd_job_t
*)first
)->priority
) != 0)
1786 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
1791 * 'compare_jobs()' - Compare the job IDs of two jobs.
1794 static int /* O - Difference */
1795 compare_jobs(void *first
, /* I - First job */
1796 void *second
, /* I - Second job */
1797 void *data
) /* I - App data (not used) */
1799 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
1804 * 'free_job()' - Free all memory used by a job.
1808 free_job(cupsd_job_t
*job
) /* I - Job */
1810 cupsdClearString(&job
->username
);
1811 cupsdClearString(&job
->dest
);
1812 cupsdClearString(&job
->auth_username
);
1813 cupsdClearString(&job
->auth_domain
);
1814 cupsdClearString(&job
->auth_password
);
1817 * Destroy the credential cache and clear the KRB5CCNAME env var string.
1822 krb5_cc_destroy(KerberosContext
, job
->ccache
);
1826 cupsdClearString(&job
->ccname
);
1827 #endif /* HAVE_GSSAPI */
1829 if (job
->num_files
> 0)
1831 free(job
->compressions
);
1832 free(job
->filetypes
);
1835 ippDelete(job
->attrs
);
1842 * 'ipp_length()' - Compute the size of the buffer needed to hold
1843 * the textual IPP attributes.
1846 static int /* O - Size of attribute buffer */
1847 ipp_length(ipp_t
*ipp
) /* I - IPP request */
1849 int bytes
; /* Number of bytes */
1850 int i
; /* Looping var */
1851 ipp_attribute_t
*attr
; /* Current attribute */
1855 * Loop through all attributes...
1860 for (attr
= ipp
->attrs
; attr
!= NULL
; attr
= attr
->next
)
1863 * Skip attributes that won't be sent to filters...
1866 if (attr
->value_tag
== IPP_TAG_MIMETYPE
||
1867 attr
->value_tag
== IPP_TAG_NAMELANG
||
1868 attr
->value_tag
== IPP_TAG_TEXTLANG
||
1869 attr
->value_tag
== IPP_TAG_URI
||
1870 attr
->value_tag
== IPP_TAG_URISCHEME
)
1873 if (strncmp(attr
->name
, "time-", 5) == 0)
1877 * Add space for a leading space and commas between each value.
1878 * For the first attribute, the leading space isn't used, so the
1879 * extra byte can be used as the nul terminator...
1882 bytes
++; /* " " separator */
1883 bytes
+= attr
->num_values
; /* "," separators */
1886 * Boolean attributes appear as "foo,nofoo,foo,nofoo", while
1887 * other attributes appear as "foo=value1,value2,...,valueN".
1890 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
1891 bytes
+= strlen(attr
->name
);
1893 bytes
+= attr
->num_values
* strlen(attr
->name
);
1896 * Now add the size required for each value in the attribute...
1899 switch (attr
->value_tag
)
1901 case IPP_TAG_INTEGER
:
1904 * Minimum value of a signed integer is -2147483647, or 11 digits.
1907 bytes
+= attr
->num_values
* 11;
1910 case IPP_TAG_BOOLEAN
:
1912 * Add two bytes for each false ("no") value...
1915 for (i
= 0; i
< attr
->num_values
; i
++)
1916 if (!attr
->values
[i
].boolean
)
1920 case IPP_TAG_RANGE
:
1922 * A range is two signed integers separated by a hyphen, or
1923 * 23 characters max.
1926 bytes
+= attr
->num_values
* 23;
1929 case IPP_TAG_RESOLUTION
:
1931 * A resolution is two signed integers separated by an "x" and
1932 * suffixed by the units, or 26 characters max.
1935 bytes
+= attr
->num_values
* 26;
1938 case IPP_TAG_STRING
:
1941 case IPP_TAG_KEYWORD
:
1942 case IPP_TAG_CHARSET
:
1943 case IPP_TAG_LANGUAGE
:
1946 * Strings can contain characters that need quoting. We need
1947 * at least 2 * len + 2 characters to cover the quotes and
1948 * any backslashes in the string.
1951 for (i
= 0; i
< attr
->num_values
; i
++)
1952 bytes
+= 2 * strlen(attr
->values
[i
].string
.text
) + 2;
1956 break; /* anti-compiler-warning-code */
1965 * 'load_job_cache()' - Load jobs from the job.cache file.
1969 load_job_cache(const char *filename
) /* I - job.cache filename */
1971 cups_file_t
*fp
; /* job.cache file */
1972 char line
[1024], /* Line buffer */
1973 *value
; /* Value on line */
1974 int linenum
; /* Line number in file */
1975 cupsd_job_t
*job
; /* Current job */
1976 int jobid
; /* Job ID */
1977 char jobfile
[1024]; /* Job filename */
1981 * Open the job.cache file...
1984 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
1986 if (errno
!= ENOENT
)
1987 cupsdLogMessage(CUPSD_LOG_ERROR
,
1988 "Unable to open job cache file \"%s\": %s",
1989 filename
, strerror(errno
));
1991 load_request_root();
1997 * Read entries from the job cache file and create jobs as needed.
2000 cupsdLogMessage(CUPSD_LOG_INFO
, "Loading job cache file \"%s\"...",
2006 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
2008 if (!strcasecmp(line
, "NextJobId"))
2011 NextJobId
= atoi(value
);
2013 else if (!strcasecmp(line
, "<Job"))
2017 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing </Job> directive on line %d!",
2024 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing job ID on line %d!", linenum
);
2028 jobid
= atoi(value
);
2032 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad job ID %d on line %d!", jobid
,
2037 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, jobid
);
2038 if (access(jobfile
, 0))
2040 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Job %d] Files have gone away!",
2045 job
= calloc(1, sizeof(cupsd_job_t
));
2048 cupsdLogMessage(CUPSD_LOG_EMERG
,
2049 "[Job %d] Unable to allocate memory for job!", jobid
);
2054 job
->back_pipes
[0] = -1;
2055 job
->back_pipes
[1] = -1;
2056 job
->print_pipes
[0] = -1;
2057 job
->print_pipes
[1] = -1;
2058 job
->side_pipes
[0] = -1;
2059 job
->side_pipes
[1] = -1;
2060 job
->status_pipes
[0] = -1;
2061 job
->status_pipes
[1] = -1;
2063 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Loading from cache...", job
->id
);
2067 cupsdLogMessage(CUPSD_LOG_ERROR
,
2068 "Missing <Job #> directive on line %d!", linenum
);
2071 else if (!strcasecmp(line
, "</Job>"))
2073 cupsArrayAdd(Jobs
, job
);
2075 if (job
->state_value
<= IPP_JOB_STOPPED
)
2077 cupsArrayAdd(ActiveJobs
, job
);
2085 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing value on line %d!", linenum
);
2088 else if (!strcasecmp(line
, "State"))
2090 job
->state_value
= (ipp_jstate_t
)atoi(value
);
2092 if (job
->state_value
< IPP_JOB_PENDING
)
2093 job
->state_value
= IPP_JOB_PENDING
;
2094 else if (job
->state_value
> IPP_JOB_COMPLETED
)
2095 job
->state_value
= IPP_JOB_COMPLETED
;
2097 else if (!strcasecmp(line
, "Priority"))
2099 job
->priority
= atoi(value
);
2101 else if (!strcasecmp(line
, "Username"))
2103 cupsdSetString(&job
->username
, value
);
2105 else if (!strcasecmp(line
, "Destination"))
2107 cupsdSetString(&job
->dest
, value
);
2109 else if (!strcasecmp(line
, "DestType"))
2111 job
->dtype
= (cups_ptype_t
)atoi(value
);
2113 else if (!strcasecmp(line
, "NumFiles"))
2115 job
->num_files
= atoi(value
);
2117 if (job
->num_files
< 0)
2119 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad NumFiles value %d on line %d!",
2120 job
->num_files
, linenum
);
2125 if (job
->num_files
> 0)
2127 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-001", RequestRoot
,
2129 if (access(jobfile
, 0))
2131 cupsdLogMessage(CUPSD_LOG_INFO
,
2132 "[Job %d] Data files have gone away!", job
->id
);
2137 job
->filetypes
= calloc(job
->num_files
, sizeof(mime_type_t
*));
2138 job
->compressions
= calloc(job
->num_files
, sizeof(int));
2140 if (!job
->filetypes
|| !job
->compressions
)
2142 cupsdLogMessage(CUPSD_LOG_EMERG
,
2143 "[Job %d] Unable to allocate memory for %d files!",
2144 job
->id
, job
->num_files
);
2149 else if (!strcasecmp(line
, "File"))
2151 int number
, /* File number */
2152 compression
; /* Compression value */
2153 char super
[MIME_MAX_SUPER
], /* MIME super type */
2154 type
[MIME_MAX_TYPE
]; /* MIME type */
2157 if (sscanf(value
, "%d%*[ \t]%15[^/]/%255s%d", &number
, super
, type
,
2160 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File on line %d!", linenum
);
2164 if (number
< 1 || number
> job
->num_files
)
2166 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File number %d on line %d!",
2173 job
->compressions
[number
] = compression
;
2174 job
->filetypes
[number
] = mimeType(MimeDatabase
, super
, type
);
2176 if (!job
->filetypes
[number
])
2179 * If the original MIME type is unknown, auto-type it!
2182 cupsdLogMessage(CUPSD_LOG_ERROR
,
2183 "[Job %d] Unknown MIME type %s/%s for file %d!",
2184 job
->id
, super
, type
, number
+ 1);
2186 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
2187 job
->id
, number
+ 1);
2188 job
->filetypes
[number
] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
2189 job
->compressions
+ number
);
2192 * If that didn't work, assume it is raw...
2195 if (!job
->filetypes
[number
])
2196 job
->filetypes
[number
] = mimeType(MimeDatabase
, "application",
2201 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unknown %s directive on line %d!",
2210 * 'load_next_job_id()' - Load the NextJobId value from the job.cache file.
2214 load_next_job_id(const char *filename
) /* I - job.cache filename */
2216 cups_file_t
*fp
; /* job.cache file */
2217 char line
[1024], /* Line buffer */
2218 *value
; /* Value on line */
2219 int linenum
; /* Line number in file */
2220 int next_job_id
; /* NextJobId value from line */
2224 * Read the NextJobId directive from the job.cache file and use
2225 * the value (if any).
2228 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
2230 if (errno
!= ENOENT
)
2231 cupsdLogMessage(CUPSD_LOG_ERROR
,
2232 "Unable to open job cache file \"%s\": %s",
2233 filename
, strerror(errno
));
2238 cupsdLogMessage(CUPSD_LOG_INFO
,
2239 "Loading NextJobId from job cache file \"%s\"...", filename
);
2243 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
2245 if (!strcasecmp(line
, "NextJobId"))
2249 next_job_id
= atoi(value
);
2251 if (next_job_id
> NextJobId
)
2252 NextJobId
= next_job_id
;
2263 * 'load_request_root()' - Load jobs from the RequestRoot directory.
2267 load_request_root(void)
2269 cups_dir_t
*dir
; /* Directory */
2270 cups_dentry_t
*dent
; /* Directory entry */
2271 cupsd_job_t
*job
; /* New job */
2275 * Open the requests directory...
2278 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Scanning %s for jobs...", RequestRoot
);
2280 if ((dir
= cupsDirOpen(RequestRoot
)) == NULL
)
2282 cupsdLogMessage(CUPSD_LOG_ERROR
,
2283 "Unable to open spool directory \"%s\": %s",
2284 RequestRoot
, strerror(errno
));
2289 * Read all the c##### files...
2292 while ((dent
= cupsDirRead(dir
)) != NULL
)
2293 if (strlen(dent
->filename
) >= 6 && dent
->filename
[0] == 'c')
2296 * Allocate memory for the job...
2299 if ((job
= calloc(sizeof(cupsd_job_t
), 1)) == NULL
)
2301 cupsdLogMessage(CUPSD_LOG_ERROR
, "Ran out of memory for jobs!");
2307 * Assign the job ID...
2310 job
->id
= atoi(dent
->filename
+ 1);
2311 job
->back_pipes
[0] = -1;
2312 job
->back_pipes
[1] = -1;
2313 job
->print_pipes
[0] = -1;
2314 job
->print_pipes
[1] = -1;
2315 job
->side_pipes
[0] = -1;
2316 job
->side_pipes
[1] = -1;
2317 job
->status_pipes
[0] = -1;
2318 job
->status_pipes
[1] = -1;
2320 if (job
->id
>= NextJobId
)
2321 NextJobId
= job
->id
+ 1;
2330 * Insert the job into the array, sorting by job priority and ID...
2333 cupsArrayAdd(Jobs
, job
);
2335 if (job
->state_value
<= IPP_JOB_STOPPED
)
2336 cupsArrayAdd(ActiveJobs
, job
);
2346 * 'set_time()' - Set one of the "time-at-xyz" attributes...
2350 set_time(cupsd_job_t
*job
, /* I - Job to update */
2351 const char *name
) /* I - Name of attribute */
2353 ipp_attribute_t
*attr
; /* Time attribute */
2356 if ((attr
= ippFindAttribute(job
->attrs
, name
, IPP_TAG_ZERO
)) != NULL
)
2358 attr
->value_tag
= IPP_TAG_INTEGER
;
2359 attr
->values
[0].integer
= time(NULL
);
2365 * 'set_hold_until()' - Set the hold time and update job-hold-until attribute...
2369 set_hold_until(cupsd_job_t
*job
, /* I - Job to update */
2370 time_t holdtime
) /* I - Hold until time */
2372 ipp_attribute_t
*attr
; /* job-hold-until attribute */
2373 struct tm
*holddate
; /* Hold date */
2374 char holdstr
[64]; /* Hold time */
2378 * Set the hold_until value and hold the job...
2381 cupsdLogMessage(CUPSD_LOG_DEBUG
, "set_hold_until: hold_until = %d",
2384 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
2385 job
->state_value
= IPP_JOB_HELD
;
2386 job
->hold_until
= holdtime
;
2389 * Update the job-hold-until attribute with a string representing GMT
2390 * time (HH:MM:SS)...
2393 holddate
= gmtime(&holdtime
);
2394 snprintf(holdstr
, sizeof(holdstr
), "%d:%d:%d", holddate
->tm_hour
,
2395 holddate
->tm_min
, holddate
->tm_sec
);
2397 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2398 IPP_TAG_KEYWORD
)) == NULL
)
2399 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2402 * Either add the attribute or update the value of the existing one
2406 attr
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
2407 "job-hold-until", NULL
, holdstr
);
2409 cupsdSetString(&attr
->values
[0].string
.text
, holdstr
);
2416 * 'start_job()' - Start a print job.
2420 start_job(cupsd_job_t
*job
, /* I - Job ID */
2421 cupsd_printer_t
*printer
) /* I - Printer to print job */
2423 int i
; /* Looping var */
2424 int slot
; /* Pipe slot */
2425 cups_array_t
*filters
, /* Filters for job */
2426 *prefilters
; /* Filters with prefilters */
2427 mime_filter_t
*filter
, /* Current filter */
2428 *prefilter
, /* Prefilter */
2429 port_monitor
; /* Port monitor filter */
2430 char method
[255], /* Method for output */
2431 *optptr
, /* Pointer to options */
2432 *valptr
; /* Pointer in value string */
2433 ipp_attribute_t
*attr
; /* Current attribute */
2434 struct stat backinfo
; /* Backend file information */
2435 int backroot
; /* Run backend as root? */
2436 int pid
; /* Process ID of new filter process */
2437 int banner_page
; /* 1 if banner page, 0 otherwise */
2438 int filterfds
[2][2];/* Pipes used between filters */
2439 int envc
; /* Number of environment variables */
2440 char **argv
, /* Filter command-line arguments */
2441 sani_uri
[1024], /* Sanitized DEVICE_URI env var */
2442 filename
[1024], /* Job filename */
2443 command
[1024], /* Full path to command */
2444 jobid
[255], /* Job ID string */
2445 title
[IPP_MAX_NAME
],
2446 /* Job title string */
2447 copies
[255], /* # copies string */
2448 *envp
[MAX_ENV
+ 16],
2449 /* Environment variables */
2450 charset
[255], /* CHARSET env variable */
2451 class_name
[255],/* CLASS env variable */
2452 classification
[1024],
2453 /* CLASSIFICATION env variable */
2455 /* CONTENT_TYPE env variable */
2457 /* DEVICE_URI env variable */
2458 final_content_type
[1024],
2459 /* FINAL_CONTENT_TYPE env variable */
2460 lang
[255], /* LANG env variable */
2462 apple_language
[255],
2463 /* APPLE_LANGUAGE env variable */
2464 #endif /* __APPLE__ */
2465 ppd
[1024], /* PPD env variable */
2467 /* PRINTER env variable */
2469 /* RIP_MAX_CACHE env variable */
2470 static char *options
= NULL
;/* Full list of options */
2471 static int optlength
= 0; /* Length of option buffer */
2474 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] start_job: file = %d/%d",
2475 job
->id
, job
->current_file
, job
->num_files
);
2477 if (job
->num_files
== 0)
2479 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Job %d] No files, canceling job!",
2482 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2487 * Figure out what filters are required to convert from
2488 * the source to the destination type...
2497 * Remote jobs and raw queues go directly to the printer without
2501 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2502 "[Job %d] Sending job to queue tagged as raw...", job
->id
);
2509 * Local jobs get filtered...
2512 filters
= mimeFilter(MimeDatabase
, job
->filetypes
[job
->current_file
],
2513 printer
->filetype
, &(job
->cost
));
2517 cupsdLogMessage(CUPSD_LOG_ERROR
,
2518 "[Job %d] Unable to convert file %d to printable format!",
2519 job
->current_file
, job
->id
);
2520 cupsdLogMessage(CUPSD_LOG_INFO
,
2521 "Hint: Do you have ESP Ghostscript installed?");
2523 if (LogLevel
< CUPSD_LOG_DEBUG
)
2524 cupsdLogMessage(CUPSD_LOG_INFO
,
2525 "Hint: Try setting the LogLevel to \"debug\".");
2527 job
->current_file
++;
2529 if (job
->current_file
== job
->num_files
)
2530 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2536 * Remove NULL ("-") filters...
2539 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
2541 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
2542 if (!strcmp(filter
->filter
, "-"))
2543 cupsArrayRemove(filters
, filter
);
2545 if (cupsArrayCount(filters
) == 0)
2547 cupsArrayDelete(filters
);
2552 * If this printer has any pre-filters, insert the required pre-filter
2553 * in the filters array...
2556 if (printer
->prefiltertype
&& filters
)
2558 prefilters
= cupsArrayNew(NULL
, NULL
);
2560 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
2562 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
2564 if ((prefilter
= mimeFilterLookup(MimeDatabase
, filter
->src
,
2565 printer
->prefiltertype
)))
2567 cupsArrayAdd(prefilters
, prefilter
);
2568 job
->cost
+= prefilter
->cost
;
2571 cupsArrayAdd(prefilters
, filter
);
2574 cupsArrayDelete(filters
);
2575 filters
= prefilters
;
2580 * Set a minimum cost of 100 for all jobs so that FilterLimit
2581 * works with raw queues and other low-cost paths.
2584 if (job
->cost
< 100)
2588 * See if the filter cost is too high...
2591 if ((FilterLevel
+ job
->cost
) > FilterLimit
&& FilterLevel
> 0 &&
2595 * Don't print this job quite yet...
2598 cupsArrayDelete(filters
);
2600 cupsdLogMessage(CUPSD_LOG_INFO
,
2601 "[Job %d] Holding because filter limit has been reached.",
2603 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2604 "[Job %d] start_job: file=%d, cost=%d, level=%d, limit=%d",
2605 job
->id
, job
->current_file
, job
->cost
, FilterLevel
,
2610 FilterLevel
+= job
->cost
;
2613 * Add decompression/raw filter as needed...
2616 if ((!printer
->raw
&& job
->compressions
[job
->current_file
]) ||
2617 (!filters
&& !printer
->remote
&&
2618 (job
->num_files
> 1 || !strncmp(printer
->device_uri
, "file:", 5))))
2621 * Add gziptoany filter to the front of the list...
2625 filters
= cupsArrayNew(NULL
, NULL
);
2627 if (!cupsArrayInsert(filters
, &gziptoany_filter
))
2629 cupsdLogMessage(CUPSD_LOG_ERROR
,
2630 "[Job %d] Unable to add decompression filter - %s",
2631 job
->id
, strerror(errno
));
2633 cupsArrayDelete(filters
);
2635 job
->current_file
++;
2637 if (job
->current_file
== job
->num_files
)
2638 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2645 * Add port monitor, if any...
2648 if (printer
->port_monitor
)
2651 * Add port monitor to the end of the list...
2655 filters
= cupsArrayNew(NULL
, NULL
);
2657 if (!cupsArrayAdd(filters
, &port_monitor
))
2659 cupsdLogMessage(CUPSD_LOG_ERROR
,
2660 "[Job %d] Unable to add port monitor - %s",
2661 job
->id
, strerror(errno
));
2663 cupsArrayDelete(filters
);
2665 job
->current_file
++;
2667 if (job
->current_file
== job
->num_files
)
2668 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2673 snprintf(port_monitor
.filter
, sizeof(port_monitor
.filter
),
2674 "%s/monitor/%s", ServerBin
, printer
->port_monitor
);
2678 * Update the printer and job state to "processing"...
2681 job
->state
->values
[0].integer
= IPP_JOB_PROCESSING
;
2682 job
->state_value
= IPP_JOB_PROCESSING
;
2685 job
->printer
= printer
;
2688 cupsdSetPrinterState(printer
, IPP_PRINTER_PROCESSING
, 0);
2690 if (job
->current_file
== 0)
2693 * Set the processing time...
2696 set_time(job
, "time-at-processing");
2699 * Create the backchannel pipes and make them non-blocking...
2702 cupsdOpenPipe(job
->back_pipes
);
2704 fcntl(job
->back_pipes
[0], F_SETFL
,
2705 fcntl(job
->back_pipes
[0], F_GETFL
) | O_NONBLOCK
);
2707 fcntl(job
->back_pipes
[1], F_SETFL
,
2708 fcntl(job
->back_pipes
[1], F_GETFL
) | O_NONBLOCK
);
2711 * Create the side-channel pipes and make them non-blocking...
2714 socketpair(AF_LOCAL
, SOCK_STREAM
, 0, job
->side_pipes
);
2716 fcntl(job
->side_pipes
[0], F_SETFL
,
2717 fcntl(job
->side_pipes
[0], F_GETFL
) | O_NONBLOCK
);
2719 fcntl(job
->side_pipes
[1], F_SETFL
,
2720 fcntl(job
->side_pipes
[1], F_GETFL
) | O_NONBLOCK
);
2724 * Determine if we are printing a banner page or not...
2727 if (job
->job_sheets
== NULL
)
2729 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] No job-sheets attribute.",
2731 if ((job
->job_sheets
=
2732 ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_ZERO
)) != NULL
)
2733 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2734 "[Job %d] ... but someone added one without setting "
2735 "job_sheets!", job
->id
);
2737 else if (job
->job_sheets
->num_values
== 1)
2738 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] job-sheets=%s", job
->id
,
2739 job
->job_sheets
->values
[0].string
.text
);
2741 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] job-sheets=%s,%s", job
->id
,
2742 job
->job_sheets
->values
[0].string
.text
,
2743 job
->job_sheets
->values
[1].string
.text
);
2745 if (printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
))
2747 else if (job
->job_sheets
== NULL
)
2749 else if (strcasecmp(job
->job_sheets
->values
[0].string
.text
, "none") != 0 &&
2750 job
->current_file
== 0)
2752 else if (job
->job_sheets
->num_values
> 1 &&
2753 strcasecmp(job
->job_sheets
->values
[1].string
.text
, "none") != 0 &&
2754 job
->current_file
== (job
->num_files
- 1))
2759 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] banner_page = %d", job
->id
,
2763 * Building the options string is harder than it needs to be, but
2764 * for the moment we need to pass strings for command-line args and
2765 * not IPP attribute pointers... :)
2767 * First allocate/reallocate the option buffer as needed...
2770 i
= ipp_length(job
->attrs
);
2777 optptr
= realloc(options
, i
);
2781 cupsdLogMessage(CUPSD_LOG_CRIT
,
2782 "[Job %d] Unable to allocate %d bytes for option buffer!",
2785 cupsArrayDelete(filters
);
2787 FilterLevel
-= job
->cost
;
2789 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2798 * Now loop through the attributes and convert them to the textual
2799 * representation used by the filters...
2805 snprintf(title
, sizeof(title
), "%s-%d", printer
->name
, job
->id
);
2806 strcpy(copies
, "1");
2808 for (attr
= job
->attrs
->attrs
; attr
!= NULL
; attr
= attr
->next
)
2810 if (!strcmp(attr
->name
, "copies") &&
2811 attr
->value_tag
== IPP_TAG_INTEGER
)
2814 * Don't use the # copies attribute if we are printing the job sheets...
2818 sprintf(copies
, "%d", attr
->values
[0].integer
);
2820 else if (!strcmp(attr
->name
, "job-name") &&
2821 (attr
->value_tag
== IPP_TAG_NAME
||
2822 attr
->value_tag
== IPP_TAG_NAMELANG
))
2823 strlcpy(title
, attr
->values
[0].string
.text
, sizeof(title
));
2824 else if (attr
->group_tag
== IPP_TAG_JOB
)
2827 * Filter out other unwanted attributes...
2830 if (attr
->value_tag
== IPP_TAG_MIMETYPE
||
2831 attr
->value_tag
== IPP_TAG_NAMELANG
||
2832 attr
->value_tag
== IPP_TAG_TEXTLANG
||
2833 (attr
->value_tag
== IPP_TAG_URI
&& strcmp(attr
->name
, "job-uuid")) ||
2834 attr
->value_tag
== IPP_TAG_URISCHEME
||
2835 attr
->value_tag
== IPP_TAG_BEGIN_COLLECTION
) /* Not yet supported */
2838 if (!strncmp(attr
->name
, "time-", 5))
2841 if (!strncmp(attr
->name
, "job-", 4) && strcmp(attr
->name
, "job-uuid") &&
2842 !(printer
->type
& CUPS_PRINTER_REMOTE
))
2845 if (!strncmp(attr
->name
, "job-", 4) &&
2846 strcmp(attr
->name
, "job-uuid") &&
2847 strcmp(attr
->name
, "job-billing") &&
2848 strcmp(attr
->name
, "job-sheets") &&
2849 strcmp(attr
->name
, "job-hold-until") &&
2850 strcmp(attr
->name
, "job-priority"))
2853 if ((!strcmp(attr
->name
, "page-label") ||
2854 !strcmp(attr
->name
, "page-border") ||
2855 !strncmp(attr
->name
, "number-up", 9) ||
2856 !strcmp(attr
->name
, "page-ranges") ||
2857 !strcmp(attr
->name
, "page-set") ||
2858 !strcasecmp(attr
->name
, "AP_FIRSTPAGE_InputSlot") ||
2859 !strcasecmp(attr
->name
, "AP_FIRSTPAGE_ManualFeed") ||
2860 !strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
2861 "PMTotalSidesImaged..n.") ||
2862 !strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
2863 "PMTotalBeginPages..n.")) &&
2868 * Otherwise add them to the list...
2871 if (optptr
> options
)
2872 strlcat(optptr
, " ", optlength
- (optptr
- options
));
2874 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
2876 strlcat(optptr
, attr
->name
, optlength
- (optptr
- options
));
2877 strlcat(optptr
, "=", optlength
- (optptr
- options
));
2880 for (i
= 0; i
< attr
->num_values
; i
++)
2883 strlcat(optptr
, ",", optlength
- (optptr
- options
));
2885 optptr
+= strlen(optptr
);
2887 switch (attr
->value_tag
)
2889 case IPP_TAG_INTEGER
:
2891 snprintf(optptr
, optlength
- (optptr
- options
),
2892 "%d", attr
->values
[i
].integer
);
2895 case IPP_TAG_BOOLEAN
:
2896 if (!attr
->values
[i
].boolean
)
2897 strlcat(optptr
, "no", optlength
- (optptr
- options
));
2899 case IPP_TAG_NOVALUE
:
2900 strlcat(optptr
, attr
->name
,
2901 optlength
- (optptr
- options
));
2904 case IPP_TAG_RANGE
:
2905 if (attr
->values
[i
].range
.lower
== attr
->values
[i
].range
.upper
)
2906 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2907 "%d", attr
->values
[i
].range
.lower
);
2909 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2910 "%d-%d", attr
->values
[i
].range
.lower
,
2911 attr
->values
[i
].range
.upper
);
2914 case IPP_TAG_RESOLUTION
:
2915 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2916 "%dx%d%s", attr
->values
[i
].resolution
.xres
,
2917 attr
->values
[i
].resolution
.yres
,
2918 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
2922 case IPP_TAG_STRING
:
2925 case IPP_TAG_KEYWORD
:
2926 case IPP_TAG_CHARSET
:
2927 case IPP_TAG_LANGUAGE
:
2929 for (valptr
= attr
->values
[i
].string
.text
; *valptr
;)
2931 if (strchr(" \t\n\\\'\"", *valptr
))
2933 *optptr
++ = *valptr
++;
2940 break; /* anti-compiler-warning-code */
2944 optptr
+= strlen(optptr
);
2949 * Build the command-line arguments for the filters. Each filter
2950 * has 6 or 7 arguments:
2954 * argv[2] = username
2956 * argv[4] = # copies
2958 * argv[6] = filename (optional; normally stdin)
2960 * This allows legacy printer drivers that use the old System V
2961 * printing interface to be used by CUPS.
2963 * For remote jobs, we send all of the files in the argument list.
2966 if (printer
->remote
&& job
->num_files
> 1)
2967 argv
= calloc(7 + job
->num_files
, sizeof(char *));
2969 argv
= calloc(8, sizeof(char *));
2971 sprintf(jobid
, "%d", job
->id
);
2973 argv
[0] = printer
->name
;
2975 argv
[2] = job
->username
;
2980 if (printer
->remote
&& job
->num_files
> 1)
2982 for (i
= 0; i
< job
->num_files
; i
++)
2984 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
2986 argv
[6 + i
] = strdup(filename
);
2991 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
2992 job
->id
, job
->current_file
+ 1);
2996 for (i
= 0; argv
[i
]; i
++)
2997 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2998 "[Job %d] argv[%d]=\"%s\"", job
->id
, i
, argv
[i
]);
3001 * Create environment variable strings for the filters...
3004 attr
= ippFindAttribute(job
->attrs
, "attributes-natural-language",
3008 strcpy(apple_language
, "APPLE_LANGUAGE");
3009 _cupsAppleLanguage(attr
->values
[0].string
.text
,
3010 apple_language
+ 15, sizeof(apple_language
) - 15);
3011 #endif /* __APPLE__ */
3013 switch (strlen(attr
->values
[0].string
.text
))
3017 * This is an unknown or badly formatted language code; use
3018 * the POSIX locale...
3021 strcpy(lang
, "LANG=C");
3026 * Just the language code (ll)...
3029 snprintf(lang
, sizeof(lang
), "LANG=%s.UTF8",
3030 attr
->values
[0].string
.text
);
3035 * Language and country code (ll-cc)...
3038 snprintf(lang
, sizeof(lang
), "LANG=%c%c_%c%c.UTF8",
3039 attr
->values
[0].string
.text
[0],
3040 attr
->values
[0].string
.text
[1],
3041 toupper(attr
->values
[0].string
.text
[3] & 255),
3042 toupper(attr
->values
[0].string
.text
[4] & 255));
3046 attr
= ippFindAttribute(job
->attrs
, "document-format",
3049 (optptr
= strstr(attr
->values
[0].string
.text
, "charset=")) != NULL
)
3050 snprintf(charset
, sizeof(charset
), "CHARSET=%s", optptr
+ 8);
3053 attr
= ippFindAttribute(job
->attrs
, "attributes-charset",
3055 snprintf(charset
, sizeof(charset
), "CHARSET=%s",
3056 attr
->values
[0].string
.text
);
3059 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s/%s",
3060 job
->filetypes
[job
->current_file
]->super
,
3061 job
->filetypes
[job
->current_file
]->type
);
3062 snprintf(device_uri
, sizeof(device_uri
), "DEVICE_URI=%s",
3063 printer
->device_uri
);
3064 cupsdSanitizeURI(printer
->device_uri
, sani_uri
, sizeof(sani_uri
));
3065 snprintf(ppd
, sizeof(ppd
), "PPD=%s/ppd/%s.ppd", ServerRoot
, printer
->name
);
3066 snprintf(printer_name
, sizeof(printer_name
), "PRINTER=%s", printer
->name
);
3067 snprintf(rip_max_cache
, sizeof(rip_max_cache
), "RIP_MAX_CACHE=%s", RIPCache
);
3069 envc
= cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
3071 envp
[envc
++] = charset
;
3072 envp
[envc
++] = lang
;
3074 envp
[envc
++] = apple_language
;
3075 #endif /* __APPLE__ */
3076 envp
[envc
++] = ppd
;
3077 envp
[envc
++] = rip_max_cache
;
3078 envp
[envc
++] = content_type
;
3079 envp
[envc
++] = device_uri
;
3080 envp
[envc
++] = printer_name
;
3082 if (!printer
->remote
&& !printer
->raw
&&
3083 (filter
= (mime_filter_t
*)cupsArrayLast(filters
)) != NULL
&&
3086 snprintf(final_content_type
, sizeof(final_content_type
),
3087 "FINAL_CONTENT_TYPE=%s/%s",
3088 filter
->dst
->super
, filter
->dst
->type
);
3089 envp
[envc
++] = final_content_type
;
3092 if (Classification
&& !banner_page
)
3094 if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
3095 IPP_TAG_NAME
)) == NULL
)
3096 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3098 else if (attr
->num_values
> 1 &&
3099 strcmp(attr
->values
[1].string
.text
, "none") != 0)
3100 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3101 attr
->values
[1].string
.text
);
3103 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3104 attr
->values
[0].string
.text
);
3106 envp
[envc
++] = classification
;
3109 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
3111 snprintf(class_name
, sizeof(class_name
), "CLASS=%s", job
->dest
);
3112 envp
[envc
++] = class_name
;
3115 if (job
->auth_username
)
3116 envp
[envc
++] = job
->auth_username
;
3117 if (job
->auth_domain
)
3118 envp
[envc
++] = job
->auth_domain
;
3119 if (job
->auth_password
)
3120 envp
[envc
++] = job
->auth_password
;
3124 envp
[envc
++] = job
->ccname
;
3125 #endif /* HAVE_GSSAPI */
3129 for (i
= 0; i
< envc
; i
++)
3130 if (!strncmp(envp
[i
], "AUTH_", 5))
3131 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] envp[%d]=\"AUTH_%c****\"",
3132 job
->id
, i
, envp
[i
][5]);
3133 else if (strncmp(envp
[i
], "DEVICE_URI=", 11))
3134 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] envp[%d]=\"%s\"",
3135 job
->id
, i
, envp
[i
]);
3137 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] envp[%d]=\"DEVICE_URI=%s\"",
3138 job
->id
, i
, sani_uri
);
3140 if (printer
->remote
)
3141 job
->current_file
= job
->num_files
;
3143 job
->current_file
++;
3146 * Now create processes for all of the filters...
3149 filterfds
[0][0] = -1;
3150 filterfds
[0][1] = -1;
3151 filterfds
[1][0] = -1;
3152 filterfds
[1][1] = -1;
3154 if (!job
->status_buffer
)
3156 if (cupsdOpenPipe(job
->status_pipes
))
3158 cupsdLogMessage(CUPSD_LOG_ERROR
,
3159 "[Job %d] Unable to create job status pipes - %s.",
3160 job
->id
, strerror(errno
));
3161 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3162 "Unable to create status pipes - %s.", strerror(errno
));
3164 cupsdAddPrinterHistory(printer
);
3166 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3167 "Job canceled because the server could not create the job "
3173 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3174 "[Job %d] start_job: status_pipes = [ %d %d ]",
3175 job
->id
, job
->status_pipes
[0], job
->status_pipes
[1]);
3177 job
->status_buffer
= cupsdStatBufNew(job
->status_pipes
[0], "[Job %d]",
3179 job
->status_level
= CUPSD_LOG_INFO
;
3183 memset(job
->filters
, 0, sizeof(job
->filters
));
3185 for (i
= 0, slot
= 0, filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
3187 i
++, filter
= (mime_filter_t
*)cupsArrayNext(filters
))
3189 if (filter
->filter
[0] != '/')
3190 snprintf(command
, sizeof(command
), "%s/filter/%s", ServerBin
,
3193 strlcpy(command
, filter
->filter
, sizeof(command
));
3195 if (i
< (cupsArrayCount(filters
) - 1))
3197 if (cupsdOpenPipe(filterfds
[slot
]))
3199 cupsdLogMessage(CUPSD_LOG_ERROR
,
3200 "[Job %d] Unable to create job filter pipes - %s.",
3201 job
->id
, strerror(errno
));
3202 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3203 "Unable to create filter pipes - %s.", strerror(errno
));
3204 cupsdAddPrinterHistory(printer
);
3206 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3207 "Job canceled because the server could not create the "
3215 if (job
->current_file
== 1)
3217 if (strncmp(printer
->device_uri
, "file:", 5) != 0)
3219 if (cupsdOpenPipe(job
->print_pipes
))
3221 cupsdLogMessage(CUPSD_LOG_ERROR
,
3222 "[Job %d] Unable to create job backend pipes - %s.",
3223 job
->id
, strerror(errno
));
3224 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3225 "Unable to create backend pipes - %s.", strerror(errno
));
3226 cupsdAddPrinterHistory(printer
);
3228 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3229 "Job canceled because the server could not create "
3230 "the backend pipes.");
3237 job
->print_pipes
[0] = -1;
3238 if (!strcmp(printer
->device_uri
, "file:/dev/null") ||
3239 !strcmp(printer
->device_uri
, "file:///dev/null"))
3240 job
->print_pipes
[1] = -1;
3243 if (!strncmp(printer
->device_uri
, "file:/dev/", 10))
3244 job
->print_pipes
[1] = open(printer
->device_uri
+ 5,
3246 else if (!strncmp(printer
->device_uri
, "file:///dev/", 12))
3247 job
->print_pipes
[1] = open(printer
->device_uri
+ 7,
3249 else if (!strncmp(printer
->device_uri
, "file:///", 8))
3250 job
->print_pipes
[1] = open(printer
->device_uri
+ 7,
3251 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
3253 job
->print_pipes
[1] = open(printer
->device_uri
+ 5,
3254 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
3256 if (job
->print_pipes
[1] < 0)
3258 cupsdLogMessage(CUPSD_LOG_ERROR
,
3259 "[Job %d] Unable to open output file \"%s\" - %s.",
3260 job
->id
, printer
->device_uri
, strerror(errno
));
3261 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3262 "Unable to open output file \"%s\" - %s.",
3263 printer
->device_uri
, strerror(errno
));
3265 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3266 "Job canceled because the server could not open the "
3272 fcntl(job
->print_pipes
[1], F_SETFD
,
3273 fcntl(job
->print_pipes
[1], F_GETFD
) | FD_CLOEXEC
);
3277 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3278 "[Job %d] start_job: print_pipes = [ %d %d ]",
3279 job
->id
, job
->print_pipes
[0], job
->print_pipes
[1]);
3282 filterfds
[slot
][0] = job
->print_pipes
[0];
3283 filterfds
[slot
][1] = job
->print_pipes
[1];
3286 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] start_job: filter=\"%s\"",
3288 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3289 "[Job %d] start_job: filterfds[%d]=[ %d %d ]",
3290 job
->id
, slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3292 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
3293 filterfds
[slot
][1], job
->status_pipes
[1],
3294 job
->back_pipes
[0], job
->side_pipes
[0], 0,
3297 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3298 "[Job %d] start_job: Closing filter pipes for slot %d "
3300 job
->id
, !slot
, filterfds
[!slot
][0], filterfds
[!slot
][1]);
3302 cupsdClosePipe(filterfds
[!slot
]);
3306 cupsdLogMessage(CUPSD_LOG_ERROR
,
3307 "[Job %d] Unable to start filter \"%s\" - %s.",
3308 job
->id
, filter
->filter
, strerror(errno
));
3309 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3310 "Unable to start filter \"%s\" - %s.",
3311 filter
->filter
, strerror(errno
));
3313 cupsdAddPrinterHistory(printer
);
3315 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3316 "Job canceled because the server could not execute a "
3322 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Started filter %s (PID %d)",
3323 job
->id
, command
, pid
);
3329 cupsArrayDelete(filters
);
3332 * Finally, pipe the final output into a backend process if needed...
3335 if (strncmp(printer
->device_uri
, "file:", 5) != 0)
3337 if (job
->current_file
== 1)
3339 sscanf(printer
->device_uri
, "%254[^:]", method
);
3340 snprintf(command
, sizeof(command
), "%s/backend/%s", ServerBin
, method
);
3343 * See if the backend needs to run as root...
3348 else if (stat(command
, &backinfo
))
3351 backroot
= !(backinfo
.st_mode
& (S_IRWXG
| S_IRWXO
));
3355 filterfds
[slot
][0] = -1;
3356 filterfds
[slot
][1] = -1;
3358 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] start_job: backend=\"%s\"",
3360 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3361 "[Job %d] start_job: filterfds[%d] = [ %d %d ]", job
->id
,
3362 slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3364 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
3365 filterfds
[slot
][1], job
->status_pipes
[1],
3366 job
->back_pipes
[1], job
->side_pipes
[1],
3367 backroot
, &(job
->backend
));
3371 cupsdLogMessage(CUPSD_LOG_ERROR
,
3372 "[Job %d] Unable to start backend \"%s\" - %s.",
3373 job
->id
, method
, strerror(errno
));
3374 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3375 "Unable to start backend \"%s\" - %s.", method
,
3378 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3379 "Job canceled because the server could not execute "
3386 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Started backend %s (PID %d)",
3387 job
->id
, command
, pid
);
3391 if (job
->current_file
== job
->num_files
)
3393 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3394 "[Job %d] start_job: Closing print pipes [ %d %d ]...",
3395 job
->id
, job
->print_pipes
[0], job
->print_pipes
[1]);
3397 cupsdClosePipe(job
->print_pipes
);
3399 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3400 "[Job %d] start_job: Closing back pipes [ %d %d ]...",
3401 job
->id
, job
->back_pipes
[0], job
->back_pipes
[1]);
3403 cupsdClosePipe(job
->back_pipes
);
3405 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3406 "[Job %d] start_job: Closing side pipes [ %d %d ]...",
3407 job
->id
, job
->side_pipes
[0], job
->side_pipes
[1]);
3409 cupsdClosePipe(job
->side_pipes
);
3411 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3412 "[Job %d] start_job: Closing status output pipe %d...",
3413 job
->id
, job
->status_pipes
[1]);
3415 close(job
->status_pipes
[1]);
3416 job
->status_pipes
[1] = -1;
3421 filterfds
[slot
][0] = -1;
3422 filterfds
[slot
][1] = -1;
3424 if (job
->current_file
== job
->num_files
)
3426 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3427 "[Job %d] start_job: Closing print pipes [ %d %d ]...",
3428 job
->id
, job
->print_pipes
[0], job
->print_pipes
[1]);
3430 cupsdClosePipe(job
->print_pipes
);
3432 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3433 "[Job %d] start_job: Closing status output pipe %d...",
3434 job
->id
, job
->status_pipes
[1]);
3436 close(job
->status_pipes
[1]);
3437 job
->status_pipes
[1] = -1;
3441 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3442 "[Job %d] start_job: Closing filter pipes for slot %d "
3444 job
->id
, slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3445 cupsdClosePipe(filterfds
[slot
]);
3447 if (printer
->remote
&& job
->num_files
> 1)
3449 for (i
= 0; i
< job
->num_files
; i
++)
3455 cupsdAddSelect(job
->status_buffer
->fd
, (cupsd_selfunc_t
)update_job
, NULL
,
3458 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
, "Job #%d started.",
3465 * If we get here, we need to abort the current job and close out all
3466 * files and pipes...
3471 for (slot
= 0; slot
< 2; slot
++)
3473 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3474 "[Job %d] start_job: Closing filter pipes for slot %d "
3476 job
->id
, slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3477 cupsdClosePipe(filterfds
[slot
]);
3480 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3481 "[Job %d] start_job: Closing status pipes [ %d %d ]...",
3482 job
->id
, job
->status_pipes
[0], job
->status_pipes
[1]);
3483 cupsdClosePipe(job
->status_pipes
);
3484 cupsdStatBufDelete(job
->status_buffer
);
3486 job
->status_buffer
= NULL
;
3488 cupsArrayDelete(filters
);
3490 if (printer
->remote
&& job
->num_files
> 1)
3492 for (i
= 0; i
< job
->num_files
; i
++)
3498 cupsdStopJob(job
, 0);
3503 * 'unload_job()' - Unload a job from memory.
3507 unload_job(cupsd_job_t
*job
) /* I - Job */
3512 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Unloading...", job
->id
);
3514 ippDelete(job
->attrs
);
3519 job
->job_sheets
= NULL
;
3520 job
->printer_message
= NULL
;
3521 job
->printer_reasons
= NULL
;
3526 * 'update_job()' - Read a status update from a job's filters.
3530 update_job(cupsd_job_t
*job
) /* I - Job to check */
3532 int i
; /* Looping var */
3533 int copies
; /* Number of copies printed */
3534 char message
[1024], /* Message text */
3535 *ptr
; /* Pointer update... */
3536 int loglevel
, /* Log level for message */
3537 event
= 0; /* Events? */
3540 while ((ptr
= cupsdStatBufUpdate(job
->status_buffer
, &loglevel
,
3541 message
, sizeof(message
))) != NULL
)
3544 * Process page and printer state messages as needed...
3547 if (loglevel
== CUPSD_LOG_PAGE
)
3550 * Page message; send the message to the page_log file and update the
3551 * job sheet count...
3554 if (job
->sheets
!= NULL
)
3556 if (!strncasecmp(message
, "total ", 6))
3559 * Got a total count of pages from a backend or filter...
3562 copies
= atoi(message
+ 6);
3563 copies
-= job
->sheets
->values
[0].integer
; /* Just track the delta */
3565 else if (!sscanf(message
, "%*d%d", &copies
))
3568 job
->sheets
->values
[0].integer
+= copies
;
3570 if (job
->printer
->page_limit
)
3572 cupsd_quota_t
*q
= cupsdUpdateQuota(job
->printer
, job
->username
,
3576 if (AppleQuotas
&& q
->page_count
== -3)
3579 * Quota limit exceeded, cancel job in progress immediately...
3582 cupsdLogMessage(CUPSD_LOG_INFO
,
3583 "[Job %d] Canceled because pages exceed user %s "
3584 "quota limit on printer %s (%s).",
3585 job
->id
, job
->username
, job
->printer
->name
,
3586 job
->printer
->info
);
3588 cupsdCancelJob(job
, 1, IPP_JOB_CANCELED
);
3593 #endif /* __APPLE__ */
3597 cupsdLogPage(job
, message
);
3599 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
3600 "Printed %d page(s).", job
->sheets
->values
[0].integer
);
3602 else if (loglevel
== CUPSD_LOG_STATE
)
3604 if (!strcmp(message
, "paused"))
3606 cupsdStopPrinter(job
->printer
, 1);
3611 cupsdSetPrinterReasons(job
->printer
, message
);
3612 cupsdAddPrinterHistory(job
->printer
);
3613 event
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
3616 update_job_attrs(job
);
3618 else if (loglevel
== CUPSD_LOG_ATTR
)
3621 * Set attribute(s)...
3624 int num_attrs
; /* Number of attributes */
3625 cups_option_t
*attrs
; /* Attributes */
3626 const char *attr
; /* Attribute */
3629 num_attrs
= cupsParseOptions(message
, 0, &attrs
);
3631 if ((attr
= cupsGetOption("auth-info-required", num_attrs
,
3634 cupsdSetAuthInfoRequired(job
->printer
, attr
, NULL
);
3635 cupsdSetPrinterAttrs(job
->printer
);
3636 cupsdSaveAllPrinters();
3639 if ((attr
= cupsGetOption("printer-alert", num_attrs
, attrs
)) != NULL
)
3641 cupsdSetString(&job
->printer
->alert
, attr
);
3642 event
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
3645 if ((attr
= cupsGetOption("printer-alert-description", num_attrs
,
3648 cupsdSetString(&job
->printer
->alert_description
, attr
);
3649 event
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
3652 cupsFreeOptions(num_attrs
, attrs
);
3655 else if (!strncmp(message
, "recoverable:", 12))
3657 cupsdSetPrinterReasons(job
->printer
,
3658 "+com.apple.print.recoverable-warning");
3661 while (isspace(*ptr
& 255))
3664 cupsdSetString(&job
->printer
->recoverable
, ptr
);
3665 cupsdAddPrinterHistory(job
->printer
);
3666 event
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
3668 else if (!strncmp(message
, "recovered:", 10))
3670 cupsdSetPrinterReasons(job
->printer
,
3671 "-com.apple.print.recoverable-warning");
3674 while (isspace(*ptr
& 255))
3677 cupsdSetString(&job
->printer
->recoverable
, ptr
);
3678 cupsdAddPrinterHistory(job
->printer
);
3679 event
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
3681 #endif /* __APPLE__ */
3682 else if (loglevel
<= job
->status_level
)
3685 * Some message to show in the printer-state-message attribute...
3688 if (loglevel
!= CUPSD_LOG_NOTICE
)
3689 job
->status_level
= loglevel
;
3691 strlcpy(job
->printer
->state_message
, message
,
3692 sizeof(job
->printer
->state_message
));
3693 cupsdAddPrinterHistory(job
->printer
);
3694 event
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
3696 update_job_attrs(job
);
3699 if (!strchr(job
->status_buffer
->buffer
, '\n'))
3703 if ((event
& CUPSD_EVENT_PRINTER_STATE_CHANGED
))
3704 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE_CHANGED
, job
->printer
, NULL
,
3705 (job
->printer
->type
& CUPS_PRINTER_CLASS
) ?
3706 "Class \"%s\" state changed." :
3707 "Printer \"%s\" state changed.",
3708 job
->printer
->name
);
3710 if (ptr
== NULL
&& !job
->status_buffer
->bufused
)
3713 * See if all of the filters and the backend have returned their
3717 for (i
= 0; job
->filters
[i
] < 0; i
++);
3719 if (job
->filters
[i
])
3722 if (job
->current_file
>= job
->num_files
&& job
->backend
> 0)
3726 * Handle the end of job stuff...
3729 cupsdFinishJob(job
);
3735 * 'update_job_attrs()' - Update the job-printer-* attributes.
3739 update_job_attrs(cupsd_job_t
*job
) /* I - Job to update */
3741 int i
; /* Looping var */
3742 int num_reasons
; /* Actual number of reasons */
3743 const char * const *reasons
; /* Reasons */
3744 static const char *none
= "none", /* "none" */
3750 * Get/create the job-printer-state-* attributes...
3753 if (!job
->printer_message
)
3755 if ((job
->printer_message
= ippFindAttribute(job
->attrs
,
3756 "job-printer-state-message",
3757 IPP_TAG_TEXT
)) == NULL
)
3758 job
->printer_message
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_TEXT
,
3759 "job-printer-state-message",
3763 if (!job
->printer_reasons
)
3764 job
->printer_reasons
= ippFindAttribute(job
->attrs
,
3765 "job-printer-state-reasons",
3769 * If the job isn't printing, return now...
3776 * Otherwise copy the printer-state-message value...
3779 if (job
->printer
->state_message
[0])
3780 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
),
3781 job
->printer
->state_message
);
3784 * ... and the printer-state-reasons value...
3787 if (job
->printer
->num_reasons
== 0)
3790 reasons
= job
->printer
->state
== IPP_PRINTER_STOPPED
? &paused
: &none
;
3794 num_reasons
= job
->printer
->num_reasons
;
3795 reasons
= (const char * const *)job
->printer
->reasons
;
3798 if (!job
->printer_reasons
|| job
->printer_reasons
->num_values
!= num_reasons
)
3800 ippDeleteAttribute(job
->attrs
, job
->printer_reasons
);
3802 job
->printer_reasons
= ippAddStrings(job
->attrs
,
3803 IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
3804 "job-printer-state-reasons",
3805 num_reasons
, NULL
, NULL
);
3808 for (i
= 0; i
< num_reasons
; i
++)
3809 cupsdSetString(&(job
->printer_reasons
->values
[i
].string
.text
), reasons
[i
]);
3814 * End of "$Id: job.c 7005 2007-10-01 23:45:48Z mike $".