2 * "$Id: job.c 5974 2006-09-20 18:42:37Z mike $"
4 * Job management routines for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
26 * cupsdAddJob() - Add a new job to the job queue...
27 * cupsdCancelJob() - Cancel the specified print job.
28 * cupsdCancelJobs() - Cancel all jobs for the given
30 * cupsdCheckJobs() - Check the pending jobs and start any if
31 * the destination is available.
32 * cupsdCleanJobs() - Clean out old jobs.
33 * cupsdFinishJob() - Finish a job.
34 * cupsdFreeAllJobs() - Free all jobs from memory.
35 * cupsdFindJob() - Find the specified job.
36 * cupsdGetPrinterJobCount() - Get the number of pending, processing,
37 * cupsdGetUserJobCount() - Get the number of pending, processing,
38 * cupsdHoldJob() - Hold the specified job.
39 * cupsdLoadAllJobs() - Load all jobs from disk.
40 * cupsdLoadJob() - Load a single job...
41 * cupsdMoveJob() - Move the specified job to a different
43 * cupsdReleaseJob() - Release the specified job.
44 * cupsdRestartJob() - Restart the specified job.
45 * cupsdSaveAllJobs() - Save a summary of all jobs to disk.
46 * cupsdSaveJob() - Save a job to disk.
47 * cupsdSetJobHoldUntil() - Set the hold time for a job...
48 * cupsdSetJobPriority() - Set the priority of a job, moving it
49 * up/down in the list as needed.
50 * cupsdStopAllJobs() - Stop all print jobs.
51 * cupsdStopJob() - Stop a print job.
52 * cupsdUnloadCompletedJobs() - Flush completed job history from memory.
53 * cupsdUpdateJob() - Read a status update from a jobs filters.
54 * compare_active_jobs() - Compare the job IDs and priorities of two
56 * compare_jobs() - Compare the job IDs of two jobs.
57 * free_job() - Free all memory used by a job.
58 * ipp_length() - Compute the size of the buffer needed to
59 * hold the textual IPP attributes.
60 * load_job_cache() - Load jobs from the job.cache file.
61 * load_next_job_id() - Load the NextJobId value from the
63 * load_request_root() - Load jobs from the RequestRoot directory.
64 * set_time() - Set one of the "time-at-xyz" attributes...
65 * set_hold_until() - Set the hold time and update job-hold-until
67 * start_job() - Start a print job.
68 * unload_job() - Unload a job from memory.
72 * Include necessary headers...
77 #include <cups/backend.h>
85 static mime_filter_t gziptoany_filter
=
87 NULL
, /* Source type */
88 NULL
, /* Destination type */
90 "gziptoany" /* Filter program to run */
98 static int compare_active_jobs(void *first
, void *second
, void *data
);
99 static int compare_jobs(void *first
, void *second
, void *data
);
100 static void free_job(cupsd_job_t
*job
);
101 static int ipp_length(ipp_t
*ipp
);
102 static void load_job_cache(const char *filename
);
103 static void load_next_job_id(const char *filename
);
104 static void load_request_root(void);
105 static void set_time(cupsd_job_t
*job
, const char *name
);
106 static void set_hold_until(cupsd_job_t
*job
, time_t holdtime
);
107 static void start_job(cupsd_job_t
*job
, cupsd_printer_t
*printer
);
108 static void unload_job(cupsd_job_t
*job
);
112 * 'cupsdAddJob()' - Add a new job to the job queue...
115 cupsd_job_t
* /* O - New job record */
116 cupsdAddJob(int priority
, /* I - Job priority */
117 const char *dest
) /* I - Job destination */
119 cupsd_job_t
*job
; /* New job record */
122 job
= calloc(sizeof(cupsd_job_t
), 1);
124 job
->id
= NextJobId
++;
125 job
->priority
= priority
;
126 job
->back_pipes
[0] = -1;
127 job
->back_pipes
[1] = -1;
128 job
->print_pipes
[0] = -1;
129 job
->print_pipes
[1] = -1;
130 job
->status_pipes
[0] = -1;
131 job
->status_pipes
[1] = -1;
133 cupsdSetString(&job
->dest
, dest
);
136 * Add the new job to the "all jobs" and "active jobs" lists...
139 cupsArrayAdd(Jobs
, job
);
140 cupsArrayAdd(ActiveJobs
, job
);
147 * 'cupsdCancelJob()' - Cancel the specified print job.
151 cupsdCancelJob(cupsd_job_t
*job
, /* I - Job to cancel */
152 int purge
, /* I - Purge jobs? */
153 ipp_jstate_t newstate
) /* I - New job state */
155 int i
; /* Looping var */
156 char filename
[1024]; /* Job filename */
157 cupsd_printer_t
*printer
; /* Printer used by job */
160 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdCancelJob: id = %d", job
->id
);
163 * Stop any processes that are working on the current job...
166 printer
= job
->printer
;
168 if (job
->state_value
== IPP_JOB_PROCESSING
)
169 cupsdStopJob(job
, 0);
174 job
->state
->values
[0].integer
= newstate
;
176 job
->state_value
= newstate
;
178 set_time(job
, "time-at-completed");
181 * Send any pending notifications and then expire them...
189 case IPP_JOB_CANCELED
:
190 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, printer
, job
,
191 purge
? "Job purged." : "Job canceled.");
194 case IPP_JOB_ABORTED
:
195 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, printer
, job
,
196 "Job aborted; please consult the error_log file "
200 case IPP_JOB_COMPLETED
:
202 * Clear the printer's state_message and state_reasons and move on...
205 printer
->state_message
[0] = '\0';
207 cupsdSetPrinterReasons(printer
, "");
208 cupsdSetPrinterState(printer
, IPP_PRINTER_IDLE
, 0);
210 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, printer
, job
,
215 cupsdExpireSubscriptions(NULL
, job
);
218 * Remove the job from the active list...
221 cupsArrayRemove(ActiveJobs
, job
);
224 * Remove any authentication data...
227 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
231 * Remove the print file for good if we aren't preserving jobs or
235 job
->current_file
= 0;
237 if (!JobHistory
|| !JobFiles
|| purge
|| (job
->dtype
& CUPS_PRINTER_REMOTE
))
239 for (i
= 1; i
<= job
->num_files
; i
++)
241 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
246 if (job
->num_files
> 0)
248 free(job
->filetypes
);
249 free(job
->compressions
);
252 job
->filetypes
= NULL
;
253 job
->compressions
= NULL
;
257 if (JobHistory
&& !purge
&& !(job
->dtype
& CUPS_PRINTER_REMOTE
))
260 * Save job state info...
268 * Remove the job info file...
271 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
,
276 * Remove the job from the "all jobs" list...
279 cupsArrayRemove(Jobs
, job
);
282 * Free all memory used...
291 * 'cupsdCancelJobs()' - Cancel all jobs for the given destination/user...
295 cupsdCancelJobs(const char *dest
, /* I - Destination to cancel */
296 const char *username
, /* I - Username or NULL */
297 int purge
) /* I - Purge jobs? */
299 cupsd_job_t
*job
; /* Current job */
302 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
304 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
305 if ((dest
== NULL
|| !strcmp(job
->dest
, dest
)) &&
306 (username
== NULL
|| !strcmp(job
->username
, username
)))
309 * Cancel all jobs matching this destination/user...
312 cupsdCancelJob(job
, purge
, IPP_JOB_CANCELED
);
320 * 'cupsdCheckJobs()' - Check the pending jobs and start any if the destination
327 cupsd_job_t
*job
; /* Current job in queue */
328 cupsd_printer_t
*printer
, /* Printer destination */
329 *pclass
; /* Printer class destination */
332 DEBUG_puts("cupsdCheckJobs()");
334 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
335 "cupsdCheckJobs: %d active jobs, sleeping=%d, reload=%d",
336 cupsArrayCount(ActiveJobs
), Sleeping
, NeedReload
);
338 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
340 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
343 * Start held jobs if they are ready...
346 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
347 "cupsdCheckJobs: Job %d: state_value=%d, loaded=%s",
348 job
->id
, job
->state_value
, job
->attrs
? "yes" : "no");
350 if (job
->state_value
== IPP_JOB_HELD
&&
352 job
->hold_until
< time(NULL
))
354 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
355 job
->state_value
= IPP_JOB_PENDING
;
359 * Start pending jobs if the destination is available...
362 if (job
->state_value
== IPP_JOB_PENDING
&& !NeedReload
&& !Sleeping
)
364 printer
= cupsdFindDest(job
->dest
);
368 (printer
->type
& (CUPS_PRINTER_IMPLICIT
| CUPS_PRINTER_CLASS
)))
371 * If the class is remote, just pass it to the remote server...
376 if (!(pclass
->type
& CUPS_PRINTER_REMOTE
))
378 if (pclass
->state
!= IPP_PRINTER_STOPPED
)
379 printer
= cupsdFindAvailablePrinter(job
->dest
);
385 if (!printer
&& !pclass
)
388 * Whoa, the printer and/or class for this destination went away;
392 cupsdLogMessage(CUPSD_LOG_WARN
,
393 "Printer/class %s has gone away; canceling job %d!",
396 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
397 "Job canceled because the destination printer/class has "
400 cupsdCancelJob(job
, 1, IPP_JOB_ABORTED
);
405 * See if the printer is available or remote and not printing a job;
406 * if so, start the job...
412 * Add/update a job-actual-printer-uri attribute for this job
413 * so that we know which printer actually printed the job...
416 ipp_attribute_t
*attr
; /* job-actual-printer-uri attribute */
419 if ((attr
= ippFindAttribute(job
->attrs
, "job-actual-printer-uri",
420 IPP_TAG_URI
)) != NULL
)
421 cupsdSetString(&attr
->values
[0].string
.text
, printer
->uri
);
423 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
,
424 "job-actual-printer-uri", NULL
, printer
->uri
);
427 if ((!(printer
->type
& CUPS_PRINTER_REMOTE
) && /* Printer is local */
428 printer
->state
== IPP_PRINTER_IDLE
) || /* and idle */
429 ((printer
->type
& CUPS_PRINTER_REMOTE
) && /* Printer is remote */
430 !printer
->job
)) /* and not printing */
431 start_job(job
, printer
);
439 * 'cupsdCleanJobs()' - Clean out old jobs.
445 cupsd_job_t
*job
; /* Current job */
451 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
452 job
&& cupsArrayCount(Jobs
) >= MaxJobs
;
453 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
454 if (job
->state_value
>= IPP_JOB_CANCELED
)
455 cupsdCancelJob(job
, 1, IPP_JOB_CANCELED
);
460 * 'cupsdFinishJob()' - Finish a job.
464 cupsdFinishJob(cupsd_job_t
*job
) /* I - Job */
466 int job_history
; /* Did cupsdCancelJob() keep the job? */
467 cupsd_printer_t
*printer
; /* Current printer */
470 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] File %d is complete.",
471 job
->id
, job
->current_file
- 1);
473 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFinishJob: job->status is %d",
476 if (job
->status_buffer
&&
477 (job
->status
< 0 || job
->current_file
>= job
->num_files
))
480 * Close the pipe and clear the input bit.
483 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
484 "cupsdFinishJob: Removing fd %d from InputSet...",
485 job
->status_buffer
->fd
);
487 FD_CLR(job
->status_buffer
->fd
, InputSet
);
489 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
490 "cupsdFinishJob: Closing status pipes [ %d %d ]...",
491 job
->status_pipes
[0], job
->status_pipes
[1]);
493 cupsdClosePipe(job
->status_pipes
);
494 cupsdStatBufDelete(job
->status_buffer
);
496 job
->status_buffer
= NULL
;
499 printer
= job
->printer
;
504 * Backend had errors; stop it...
507 int exit_code
; /* Exit code from backend */
511 * Convert the status to an exit code. Due to the way the W* macros are
512 * implemented on MacOS X (bug?), we have to store the exit status in a
513 * variable first and then convert...
516 exit_code
= -job
->status
;
517 if (WIFEXITED(exit_code
))
518 exit_code
= WEXITSTATUS(exit_code
);
520 exit_code
= job
->status
;
522 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Backend returned status %d (%s)",
524 exit_code
== CUPS_BACKEND_FAILED
? "failed" :
525 exit_code
== CUPS_BACKEND_AUTH_REQUIRED
?
526 "authentication required" :
527 exit_code
== CUPS_BACKEND_HOLD
? "hold job" :
528 exit_code
== CUPS_BACKEND_STOP
? "stop printer" :
529 exit_code
== CUPS_BACKEND_CANCEL
? "cancel job" :
530 exit_code
< 0 ? "crashed" : "unknown");
533 * Do what needs to be done...
539 case CUPS_BACKEND_FAILED
:
541 * Backend failure, use the error-policy to determine how to
545 cupsdStopJob(job
, 0);
547 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
550 * Mark the job as pending again - we'll retry on another
554 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
555 job
->state_value
= IPP_JOB_PENDING
;
561 * If the job was queued to a class, try requeuing it... For
562 * faxes and retry-job queues, hold the current job for 5 minutes.
565 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
567 else if ((printer
->type
& CUPS_PRINTER_FAX
) ||
568 !strcmp(printer
->error_policy
, "retry-job"))
571 * See how many times we've tried to send the job; if more than
572 * the limit, cancel the job.
577 if (job
->tries
>= JobRetryLimit
)
583 cupsdLogMessage(CUPSD_LOG_ERROR
,
584 "Canceling job %d since it could not be sent "
586 job
->id
, JobRetryLimit
);
588 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
593 * Try again in N seconds...
596 set_hold_until(job
, time(NULL
) + JobRetryInterval
);
598 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
,
599 "Job held due to fax errors; please consult "
600 "the error_log file for details.");
601 cupsdSetPrinterState(printer
, IPP_PRINTER_IDLE
, 0);
604 else if (!strcmp(printer
->error_policy
, "abort-job"))
605 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
608 cupsdSetPrinterState(printer
, IPP_PRINTER_STOPPED
, 1);
610 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
611 "Job stopped due to backend errors; please consult "
612 "the error_log file for details.");
616 case CUPS_BACKEND_CANCEL
:
621 cupsdCancelJob(job
, 0, IPP_JOB_CANCELED
);
624 case CUPS_BACKEND_HOLD
:
629 cupsdStopJob(job
, 0);
631 cupsdSetJobHoldUntil(job
, "indefinite");
633 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
634 job
->state_value
= IPP_JOB_HELD
;
638 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
639 "Job held due to backend errors; please consult "
640 "the error_log file for details.");
643 case CUPS_BACKEND_STOP
:
645 * Stop the printer...
648 cupsdStopJob(job
, 0);
650 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
651 job
->state_value
= IPP_JOB_PENDING
;
654 cupsdSetPrinterState(printer
, IPP_PRINTER_STOPPED
, 1);
656 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
657 "Job stopped due to backend errors; please consult "
658 "the error_log file for details.");
661 case CUPS_BACKEND_AUTH_REQUIRED
:
662 cupsdStopJob(job
, 0);
664 cupsdSetJobHoldUntil(job
, "authenticated");
666 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
667 job
->state_value
= IPP_JOB_HELD
;
671 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
672 "Authentication is required for job %d.", job
->id
);
677 * Try printing another job...
682 else if (job
->status
> 0)
685 * Filter had errors; stop job...
688 cupsdStopJob(job
, 1);
690 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
691 "Job stopped due to filter errors; please consult the "
692 "error_log file for details.");
698 * Job printed successfully; cancel it...
701 if (job
->current_file
< job
->num_files
)
704 * Start the next file in the job...
707 FilterLevel
-= job
->cost
;
708 start_job(job
, printer
);
713 * Close out this job...
716 job_history
= JobHistory
&& !(job
->dtype
& CUPS_PRINTER_REMOTE
);
718 cupsdCancelJob(job
, 0, IPP_JOB_COMPLETED
);
726 * 'cupsdFreeAllJobs()' - Free all jobs from memory.
730 cupsdFreeAllJobs(void)
732 cupsd_job_t
*job
; /* Current job */
743 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
745 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
747 cupsArrayRemove(Jobs
, job
);
748 cupsArrayRemove(ActiveJobs
, job
);
753 cupsdReleaseSignals();
758 * 'cupsdFindJob()' - Find the specified job.
761 cupsd_job_t
* /* O - Job data */
762 cupsdFindJob(int id
) /* I - Job ID */
764 cupsd_job_t key
; /* Search key */
769 return ((cupsd_job_t
*)cupsArrayFind(Jobs
, &key
));
774 * 'cupsdGetPrinterJobCount()' - Get the number of pending, processing,
775 * or held jobs in a printer or class.
778 int /* O - Job count */
779 cupsdGetPrinterJobCount(
780 const char *dest
) /* I - Printer or class name */
782 int count
; /* Job count */
783 cupsd_job_t
*job
; /* Current job */
786 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
788 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
789 if (job
->dest
&& !strcasecmp(job
->dest
, dest
))
797 * 'cupsdGetUserJobCount()' - Get the number of pending, processing,
798 * or held jobs for a user.
801 int /* O - Job count */
802 cupsdGetUserJobCount(
803 const char *username
) /* I - Username */
805 int count
; /* Job count */
806 cupsd_job_t
*job
; /* Current job */
809 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
811 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
812 if (!strcasecmp(job
->username
, username
))
820 * 'cupsdHoldJob()' - Hold the specified job.
824 cupsdHoldJob(cupsd_job_t
*job
) /* I - Job data */
826 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdHoldJob: id = %d", job
->id
);
828 if (job
->state_value
== IPP_JOB_PROCESSING
)
829 cupsdStopJob(job
, 0);
833 DEBUG_puts("cupsdHoldJob: setting state to held...");
835 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
836 job
->state_value
= IPP_JOB_HELD
;
845 * 'cupsdLoadAllJobs()' - Load all jobs from disk.
849 cupsdLoadAllJobs(void)
851 char filename
[1024]; /* Full filename of job.cache file */
852 struct stat fileinfo
, /* Information on job.cache file */
853 dirinfo
; /* Information on RequestRoot dir */
858 * Create the job arrays as needed...
862 Jobs
= cupsArrayNew(compare_jobs
, NULL
);
865 ActiveJobs
= cupsArrayNew(compare_active_jobs
, NULL
);
868 * See whether the job.cache file is older than the RequestRoot directory...
871 snprintf(filename
, sizeof(filename
), "%s/job.cache", CacheDir
);
873 if (stat(filename
, &fileinfo
))
875 fileinfo
.st_mtime
= 0;
878 cupsdLogMessage(CUPSD_LOG_ERROR
,
879 "Unable to get file information for \"%s\" - %s",
880 filename
, strerror(errno
));
883 if (stat(RequestRoot
, &dirinfo
))
885 dirinfo
.st_mtime
= 0;
888 cupsdLogMessage(CUPSD_LOG_ERROR
,
889 "Unable to get directory information for \"%s\" - %s",
890 RequestRoot
, strerror(errno
));
894 * Load the most recent source for job data...
897 if (dirinfo
.st_mtime
> fileinfo
.st_mtime
)
901 load_next_job_id(filename
);
904 load_job_cache(filename
);
907 * Clean out old jobs as needed...
910 if (MaxJobs
> 0 && cupsArrayCount(Jobs
) >= MaxJobs
)
916 * 'cupsdLoadJob()' - Load a single job...
920 cupsdLoadJob(cupsd_job_t
*job
) /* I - Job */
922 char jobfile
[1024]; /* Job filename */
923 cups_file_t
*fp
; /* Job file */
924 int fileid
; /* Current file ID */
925 ipp_attribute_t
*attr
; /* Job attribute */
926 char scheme
[32], /* Scheme portion of URI */
927 username
[64], /* Username portion of URI */
929 /* Host portion of URI */
930 resource
[HTTP_MAX_URI
];
931 /* Resource portion of URI */
932 int port
; /* Port portion of URI */
933 const char *dest
; /* Destination */
934 mime_type_t
**filetypes
; /* New filetypes array */
935 int *compressions
; /* New compressions array */
940 if (job
->state_value
> IPP_JOB_STOPPED
)
941 job
->access_time
= time(NULL
);
946 if ((job
->attrs
= ippNew()) == NULL
)
948 cupsdLogMessage(CUPSD_LOG_ERROR
, "Ran out of memory for job attributes!");
953 * Load job attributes...
956 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Loading attributes for job %d...",
959 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, job
->id
);
960 if ((fp
= cupsFileOpen(jobfile
, "r")) == NULL
)
962 cupsdLogMessage(CUPSD_LOG_ERROR
,
963 "Unable to open job control file \"%s\" - %s!",
964 jobfile
, strerror(errno
));
965 ippDelete(job
->attrs
);
970 if (ippReadIO(fp
, (ipp_iocb_t
)cupsFileRead
, 1, NULL
, job
->attrs
) != IPP_DATA
)
972 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to read job control file \"%s\"!",
975 ippDelete(job
->attrs
);
984 * Copy attribute data to the job object...
987 if ((job
->state
= ippFindAttribute(job
->attrs
, "job-state",
988 IPP_TAG_ENUM
)) == NULL
)
990 cupsdLogMessage(CUPSD_LOG_ERROR
,
991 "Missing or bad job-state attribute in control "
994 ippDelete(job
->attrs
);
1000 job
->state_value
= (ipp_jstate_t
)job
->state
->values
[0].integer
;
1004 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1005 IPP_TAG_URI
)) == NULL
)
1007 cupsdLogMessage(CUPSD_LOG_ERROR
,
1008 "No job-printer-uri attribute in control file \"%s\"!",
1010 ippDelete(job
->attrs
);
1016 httpSeparateURI(HTTP_URI_CODING_ALL
, attr
->values
[0].string
.text
, scheme
,
1017 sizeof(scheme
), username
, sizeof(username
), host
,
1018 sizeof(host
), &port
, resource
, sizeof(resource
));
1020 if ((dest
= cupsdValidateDest(host
, resource
, &(job
->dtype
),
1023 cupsdLogMessage(CUPSD_LOG_ERROR
,
1024 "Unable to queue job for destination \"%s\"!",
1025 attr
->values
[0].string
.text
);
1026 ippDelete(job
->attrs
);
1032 cupsdSetString(&job
->dest
, dest
);
1035 job
->sheets
= ippFindAttribute(job
->attrs
, "job-media-sheets-completed",
1037 job
->job_sheets
= ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_NAME
);
1041 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
1042 IPP_TAG_INTEGER
)) == NULL
)
1044 cupsdLogMessage(CUPSD_LOG_ERROR
,
1045 "Missing or bad job-priority attribute in control "
1046 "file \"%s\"!", jobfile
);
1047 ippDelete(job
->attrs
);
1053 job
->priority
= attr
->values
[0].integer
;
1058 if ((attr
= ippFindAttribute(job
->attrs
, "job-originating-user-name",
1059 IPP_TAG_NAME
)) == NULL
)
1061 cupsdLogMessage(CUPSD_LOG_ERROR
,
1062 "Missing or bad job-originating-user-name attribute "
1063 "in control file \"%s\"!", jobfile
);
1064 ippDelete(job
->attrs
);
1070 cupsdSetString(&job
->username
, attr
->values
[0].string
.text
);
1074 * Set the job hold-until time and state...
1077 if (job
->state_value
== IPP_JOB_HELD
)
1079 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1080 IPP_TAG_KEYWORD
)) == NULL
)
1081 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1084 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
1087 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1088 job
->state_value
= IPP_JOB_PENDING
;
1091 else if (job
->state_value
== IPP_JOB_PROCESSING
)
1093 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1094 job
->state_value
= IPP_JOB_PENDING
;
1097 if (!job
->num_files
)
1100 * Find all the d##### files...
1103 for (fileid
= 1; fileid
< 10000; fileid
++)
1105 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
1108 if (access(jobfile
, 0))
1111 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Auto-typing document file \"%s\"...",
1114 if (fileid
> job
->num_files
)
1116 if (job
->num_files
== 0)
1118 compressions
= (int *)calloc(fileid
, sizeof(int));
1119 filetypes
= (mime_type_t
**)calloc(fileid
, sizeof(mime_type_t
*));
1123 compressions
= (int *)realloc(job
->compressions
,
1124 sizeof(int) * fileid
);
1125 filetypes
= (mime_type_t
**)realloc(job
->filetypes
,
1126 sizeof(mime_type_t
*) *
1130 if (!compressions
|| !filetypes
)
1132 cupsdLogMessage(CUPSD_LOG_ERROR
,
1133 "Ran out of memory for job file types!");
1137 job
->compressions
= compressions
;
1138 job
->filetypes
= filetypes
;
1139 job
->num_files
= fileid
;
1142 job
->filetypes
[fileid
- 1] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
1143 job
->compressions
+ fileid
- 1);
1145 if (!job
->filetypes
[fileid
- 1])
1146 job
->filetypes
[fileid
- 1] = mimeType(MimeDatabase
, "application",
1151 job
->access_time
= time(NULL
);
1156 * 'cupsdMoveJob()' - Move the specified job to a different destination.
1160 cupsdMoveJob(cupsd_job_t
*job
, /* I - Job */
1161 cupsd_printer_t
*p
) /* I - Destination printer or class */
1163 ipp_attribute_t
*attr
; /* job-printer-uri attribute */
1164 const char *olddest
; /* Old destination */
1165 cupsd_printer_t
*oldp
; /* Old pointer */
1169 * Don't move completed jobs...
1172 if (job
->state_value
> IPP_JOB_STOPPED
)
1176 * Get the old destination...
1179 olddest
= job
->dest
;
1182 oldp
= job
->printer
;
1184 oldp
= cupsdFindDest(olddest
);
1187 * Change the destination information...
1192 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, oldp
, job
,
1193 "Job #%d moved from %s to %s.", job
->id
, olddest
,
1196 cupsdSetString(&job
->dest
, p
->name
);
1197 job
->dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_REMOTE
|
1198 CUPS_PRINTER_IMPLICIT
);
1200 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1201 IPP_TAG_URI
)) != NULL
)
1202 cupsdSetString(&(attr
->values
[0].string
.text
), p
->uri
);
1204 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, p
, job
,
1205 "Job #%d moved from %s to %s.", job
->id
, olddest
,
1213 * 'cupsdReleaseJob()' - Release the specified job.
1217 cupsdReleaseJob(cupsd_job_t
*job
) /* I - Job */
1219 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdReleaseJob: id = %d", job
->id
);
1221 if (job
->state_value
== IPP_JOB_HELD
)
1223 DEBUG_puts("cupsdReleaseJob: setting state to pending...");
1225 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1226 job
->state_value
= IPP_JOB_PENDING
;
1234 * 'cupsdRestartJob()' - Restart the specified job.
1238 cupsdRestartJob(cupsd_job_t
*job
) /* I - Job */
1240 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdRestartJob: id = %d", job
->id
);
1242 if (job
->state_value
== IPP_JOB_STOPPED
|| job
->num_files
)
1244 ipp_jstate_t old_state
; /* Old job state */
1249 old_state
= job
->state_value
;
1252 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1253 job
->state_value
= IPP_JOB_PENDING
;
1257 if (old_state
> IPP_JOB_STOPPED
)
1258 cupsArrayAdd(ActiveJobs
, job
);
1266 * 'cupsdSaveAllJobs()' - Save a summary of all jobs to disk.
1270 cupsdSaveAllJobs(void)
1272 int i
; /* Looping var */
1273 cups_file_t
*fp
; /* Job cache file */
1274 char temp
[1024]; /* Temporary string */
1275 cupsd_job_t
*job
; /* Current job */
1276 time_t curtime
; /* Current time */
1277 struct tm
*curdate
; /* Current date */
1280 snprintf(temp
, sizeof(temp
), "%s/job.cache", CacheDir
);
1281 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
1283 cupsdLogMessage(CUPSD_LOG_ERROR
,
1284 "Unable to create job cache file \"%s\" - %s",
1285 temp
, strerror(errno
));
1289 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving job cache file \"%s\"...", temp
);
1292 * Restrict access to the file...
1295 fchown(cupsFileNumber(fp
), getuid(), Group
);
1296 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
1299 * Write a small header to the file...
1302 curtime
= time(NULL
);
1303 curdate
= localtime(&curtime
);
1304 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
1306 cupsFilePuts(fp
, "# Job cache file for " CUPS_SVERSION
"\n");
1307 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
1308 cupsFilePrintf(fp
, "NextJobId %d\n", NextJobId
);
1311 * Write each job known to the system...
1314 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1316 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1318 cupsFilePrintf(fp
, "<Job %d>\n", job
->id
);
1319 cupsFilePrintf(fp
, "State %d\n", job
->state_value
);
1320 cupsFilePrintf(fp
, "Priority %d\n", job
->priority
);
1321 cupsFilePrintf(fp
, "Username %s\n", job
->username
);
1322 cupsFilePrintf(fp
, "Destination %s\n", job
->dest
);
1323 cupsFilePrintf(fp
, "DestType %d\n", job
->dtype
);
1324 cupsFilePrintf(fp
, "NumFiles %d\n", job
->num_files
);
1325 for (i
= 0; i
< job
->num_files
; i
++)
1326 cupsFilePrintf(fp
, "File %d %s/%s %d\n", i
+ 1, job
->filetypes
[i
]->super
,
1327 job
->filetypes
[i
]->type
, job
->compressions
[i
]);
1328 cupsFilePuts(fp
, "</Job>\n");
1336 * 'cupsdSaveJob()' - Save a job to disk.
1340 cupsdSaveJob(cupsd_job_t
*job
) /* I - Job */
1342 char filename
[1024]; /* Job control filename */
1343 cups_file_t
*fp
; /* Job file */
1346 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSaveJob(job=%p(%d)): job->attrs=%p",
1347 job
, job
->id
, job
->attrs
);
1349 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
, job
->id
);
1351 if ((fp
= cupsFileOpen(filename
, "w")) == NULL
)
1353 cupsdLogMessage(CUPSD_LOG_ERROR
,
1354 "Unable to create job control file \"%s\" - %s.",
1355 filename
, strerror(errno
));
1359 fchmod(cupsFileNumber(fp
), 0600);
1360 fchown(cupsFileNumber(fp
), RunUser
, Group
);
1362 job
->attrs
->state
= IPP_IDLE
;
1364 if (ippWriteIO(fp
, (ipp_iocb_t
)cupsFileWrite
, 1, NULL
,
1365 job
->attrs
) != IPP_DATA
)
1366 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to write job control file \"%s\"!",
1374 * 'cupsdSetJobHoldUntil()' - Set the hold time for a job...
1378 cupsdSetJobHoldUntil(cupsd_job_t
*job
, /* I - Job */
1379 const char *when
) /* I - When to resume */
1381 time_t curtime
; /* Current time */
1382 struct tm
*curdate
; /* Current date */
1383 int hour
; /* Hold hour */
1384 int minute
; /* Hold minute */
1385 int second
; /* Hold second */
1388 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetJobHoldUntil(%d, \"%s\")",
1393 if (!strcmp(when
, "indefinite") || !strcmp(when
, "authenticated"))
1396 * Hold indefinitely...
1399 job
->hold_until
= 0;
1401 else if (!strcmp(when
, "day-time"))
1404 * Hold to 6am the next morning unless local time is < 6pm.
1407 curtime
= time(NULL
);
1408 curdate
= localtime(&curtime
);
1410 if (curdate
->tm_hour
< 18)
1411 job
->hold_until
= curtime
;
1413 job
->hold_until
= curtime
+
1414 ((29 - curdate
->tm_hour
) * 60 + 59 -
1415 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1417 else if (!strcmp(when
, "evening") || !strcmp(when
, "night"))
1420 * Hold to 6pm unless local time is > 6pm or < 6am.
1423 curtime
= time(NULL
);
1424 curdate
= localtime(&curtime
);
1426 if (curdate
->tm_hour
< 6 || curdate
->tm_hour
>= 18)
1427 job
->hold_until
= curtime
;
1429 job
->hold_until
= curtime
+
1430 ((17 - curdate
->tm_hour
) * 60 + 59 -
1431 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1433 else if (!strcmp(when
, "second-shift"))
1436 * Hold to 4pm unless local time is > 4pm.
1439 curtime
= time(NULL
);
1440 curdate
= localtime(&curtime
);
1442 if (curdate
->tm_hour
>= 16)
1443 job
->hold_until
= curtime
;
1445 job
->hold_until
= curtime
+
1446 ((15 - curdate
->tm_hour
) * 60 + 59 -
1447 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1449 else if (!strcmp(when
, "third-shift"))
1452 * Hold to 12am unless local time is < 8am.
1455 curtime
= time(NULL
);
1456 curdate
= localtime(&curtime
);
1458 if (curdate
->tm_hour
< 8)
1459 job
->hold_until
= curtime
;
1461 job
->hold_until
= curtime
+
1462 ((23 - curdate
->tm_hour
) * 60 + 59 -
1463 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1465 else if (!strcmp(when
, "weekend"))
1468 * Hold to weekend unless we are in the weekend.
1471 curtime
= time(NULL
);
1472 curdate
= localtime(&curtime
);
1474 if (curdate
->tm_wday
|| curdate
->tm_wday
== 6)
1475 job
->hold_until
= curtime
;
1477 job
->hold_until
= curtime
+
1478 (((5 - curdate
->tm_wday
) * 24 +
1479 (17 - curdate
->tm_hour
)) * 60 + 59 -
1480 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1482 else if (sscanf(when
, "%d:%d:%d", &hour
, &minute
, &second
) >= 2)
1485 * Hold to specified GMT time (HH:MM or HH:MM:SS)...
1488 curtime
= time(NULL
);
1489 curdate
= gmtime(&curtime
);
1491 job
->hold_until
= curtime
+
1492 ((hour
- curdate
->tm_hour
) * 60 + minute
-
1493 curdate
->tm_min
) * 60 + second
- curdate
->tm_sec
;
1496 * Hold until next day as needed...
1499 if (job
->hold_until
< curtime
)
1500 job
->hold_until
+= 24 * 60 * 60 * 60;
1503 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetJobHoldUntil: hold_until = %d",
1504 (int)job
->hold_until
);
1509 * 'cupsdSetJobPriority()' - Set the priority of a job, moving it up/down in
1510 * the list as needed.
1514 cupsdSetJobPriority(
1515 cupsd_job_t
*job
, /* I - Job ID */
1516 int priority
) /* I - New priority (0 to 100) */
1518 ipp_attribute_t
*attr
; /* Job attribute */
1522 * Don't change completed jobs...
1525 if (job
->state_value
>= IPP_JOB_PROCESSING
)
1529 * Set the new priority and re-add the job into the active list...
1532 cupsArrayRemove(ActiveJobs
, job
);
1534 job
->priority
= priority
;
1536 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
1537 IPP_TAG_INTEGER
)) != NULL
)
1538 attr
->values
[0].integer
= priority
;
1540 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-priority",
1543 cupsArrayAdd(ActiveJobs
, job
);
1550 * 'cupsdStopAllJobs()' - Stop all print jobs.
1554 cupsdStopAllJobs(int force
) /* I - 1 = Force all filters to stop */
1556 cupsd_job_t
*job
; /* Current job */
1559 DEBUG_puts("cupsdStopAllJobs()");
1561 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
1563 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
1564 if (job
->state_value
== IPP_JOB_PROCESSING
)
1566 cupsdStopJob(job
, force
);
1567 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1568 job
->state_value
= IPP_JOB_PENDING
;
1574 * 'cupsdStopJob()' - Stop a print job.
1578 cupsdStopJob(cupsd_job_t
*job
, /* I - Job */
1579 int force
) /* I - 1 = Force all filters to stop */
1581 int i
; /* Looping var */
1584 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdStopJob: id = %d, force = %d",
1587 if (job
->state_value
!= IPP_JOB_PROCESSING
)
1590 FilterLevel
-= job
->cost
;
1592 if (job
->printer
->state
== IPP_PRINTER_PROCESSING
)
1593 cupsdSetPrinterState(job
->printer
, IPP_PRINTER_IDLE
, 0);
1595 job
->state
->values
[0].integer
= IPP_JOB_STOPPED
;
1596 job
->state_value
= IPP_JOB_STOPPED
;
1597 job
->printer
->job
= NULL
;
1598 job
->printer
= NULL
;
1600 job
->current_file
--;
1602 for (i
= 0; job
->filters
[i
]; i
++)
1603 if (job
->filters
[i
] > 0)
1605 cupsdEndProcess(job
->filters
[i
], force
);
1606 job
->filters
[i
] = 0;
1609 if (job
->backend
> 0)
1611 cupsdEndProcess(job
->backend
, force
);
1615 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1616 "cupsdStopJob: Closing print pipes [ %d %d ]...",
1617 job
->print_pipes
[0], job
->print_pipes
[1]);
1619 cupsdClosePipe(job
->print_pipes
);
1621 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1622 "cupsdStopJob: Closing back pipes [ %d %d ]...",
1623 job
->back_pipes
[0], job
->back_pipes
[1]);
1625 cupsdClosePipe(job
->back_pipes
);
1627 if (job
->status_buffer
)
1630 * Close the pipe and clear the input bit.
1633 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1634 "cupsdStopJob: Removing fd %d from InputSet...",
1635 job
->status_buffer
->fd
);
1637 FD_CLR(job
->status_buffer
->fd
, InputSet
);
1639 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1640 "cupsdStopJob: Closing status pipes [ %d %d ]...",
1641 job
->status_pipes
[0], job
->status_pipes
[1]);
1643 cupsdClosePipe(job
->status_pipes
);
1644 cupsdStatBufDelete(job
->status_buffer
);
1646 job
->status_buffer
= NULL
;
1652 * 'cupsdUnloadCompletedJobs()' - Flush completed job history from memory.
1656 cupsdUnloadCompletedJobs(void)
1658 cupsd_job_t
*job
; /* Current job */
1659 time_t expire
; /* Expiration time */
1662 expire
= time(NULL
) - 60;
1664 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1666 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1667 if (job
->attrs
&& job
->state_value
>= IPP_JOB_STOPPED
&&
1668 job
->access_time
< expire
)
1674 * 'cupsdUpdateJob()' - Read a status update from a job's filters.
1678 cupsdUpdateJob(cupsd_job_t
*job
) /* I - Job to check */
1680 int i
; /* Looping var */
1681 int copies
; /* Number of copies printed */
1682 char message
[1024], /* Message text */
1683 *ptr
; /* Pointer update... */
1684 int loglevel
, /* Log level for message */
1685 event
= 0; /* Events? */
1688 while ((ptr
= cupsdStatBufUpdate(job
->status_buffer
, &loglevel
,
1689 message
, sizeof(message
))) != NULL
)
1692 * Process page and printer state messages as needed...
1695 if (loglevel
== CUPSD_LOG_PAGE
)
1698 * Page message; send the message to the page_log file and update the
1699 * job sheet count...
1702 if (job
->sheets
!= NULL
)
1704 if (!strncasecmp(message
, "total ", 6))
1707 * Got a total count of pages from a backend or filter...
1710 copies
= atoi(message
+ 6);
1711 copies
-= job
->sheets
->values
[0].integer
; /* Just track the delta */
1713 else if (!sscanf(message
, "%*d%d", &copies
))
1716 job
->sheets
->values
[0].integer
+= copies
;
1718 if (job
->printer
->page_limit
)
1719 cupsdUpdateQuota(job
->printer
, job
->username
, copies
, 0);
1722 cupsdLogPage(job
, message
);
1724 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
1725 "Printed %d page(s).", job
->sheets
->values
[0].integer
);
1727 else if (loglevel
== CUPSD_LOG_STATE
)
1729 cupsdSetPrinterReasons(job
->printer
, message
);
1730 cupsdAddPrinterHistory(job
->printer
);
1731 event
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
1733 else if (loglevel
== CUPSD_LOG_ATTR
)
1736 * Set attribute(s)...
1742 else if (!strncmp(message
, "recoverable:", 12))
1744 cupsdSetPrinterReasons(job
->printer
,
1745 "+com.apple.print.recoverable-warning");
1748 while (isspace(*ptr
& 255))
1751 cupsdSetString(&job
->printer
->recoverable
, ptr
);
1752 cupsdAddPrinterHistory(job
->printer
);
1753 event
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
1755 else if (!strncmp(message
, "recovered:", 10))
1757 cupsdSetPrinterReasons(job
->printer
,
1758 "-com.apple.print.recoverable-warning");
1761 while (isspace(*ptr
& 255))
1764 cupsdSetString(&job
->printer
->recoverable
, ptr
);
1765 cupsdAddPrinterHistory(job
->printer
);
1766 event
|= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
1768 #endif /* __APPLE__ */
1769 else if (loglevel
<= CUPSD_LOG_INFO
)
1772 * Some message to show in the printer-state-message attribute...
1775 strlcpy(job
->printer
->state_message
, message
,
1776 sizeof(job
->printer
->state_message
));
1777 cupsdAddPrinterHistory(job
->printer
);
1780 if (!strchr(job
->status_buffer
->buffer
, '\n'))
1784 if ((event
& CUPSD_EVENT_PRINTER_STATE_CHANGED
))
1785 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE_CHANGED
, job
->printer
, NULL
,
1786 (job
->printer
->type
& CUPS_PRINTER_CLASS
) ?
1787 "Class \"%s\" state changed." :
1788 "Printer \"%s\" state changed.",
1789 job
->printer
->name
);
1791 if (ptr
== NULL
&& !job
->status_buffer
->bufused
)
1794 * See if all of the filters and the backend have returned their
1798 for (i
= 0; job
->filters
[i
] < 0; i
++);
1800 if (job
->filters
[i
])
1803 if (job
->current_file
>= job
->num_files
&& job
->backend
> 0)
1807 * Handle the end of job stuff...
1810 cupsdFinishJob(job
);
1816 * 'compare_active_jobs()' - Compare the job IDs and priorities of two jobs.
1819 static int /* O - Difference */
1820 compare_active_jobs(void *first
, /* I - First job */
1821 void *second
, /* I - Second job */
1822 void *data
) /* I - App data (not used) */
1824 int diff
; /* Difference */
1827 if ((diff
= ((cupsd_job_t
*)second
)->priority
-
1828 ((cupsd_job_t
*)first
)->priority
) != 0)
1831 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
1836 * 'compare_jobs()' - Compare the job IDs of two jobs.
1839 static int /* O - Difference */
1840 compare_jobs(void *first
, /* I - First job */
1841 void *second
, /* I - Second job */
1842 void *data
) /* I - App data (not used) */
1844 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
1849 * 'free_job()' - Free all memory used by a job.
1853 free_job(cupsd_job_t
*job
) /* I - Job */
1855 cupsdClearString(&job
->username
);
1856 cupsdClearString(&job
->dest
);
1858 if (job
->num_files
> 0)
1860 free(job
->compressions
);
1861 free(job
->filetypes
);
1864 ippDelete(job
->attrs
);
1871 * 'ipp_length()' - Compute the size of the buffer needed to hold
1872 * the textual IPP attributes.
1875 static int /* O - Size of attribute buffer */
1876 ipp_length(ipp_t
*ipp
) /* I - IPP request */
1878 int bytes
; /* Number of bytes */
1879 int i
; /* Looping var */
1880 ipp_attribute_t
*attr
; /* Current attribute */
1884 * Loop through all attributes...
1889 for (attr
= ipp
->attrs
; attr
!= NULL
; attr
= attr
->next
)
1892 * Skip attributes that won't be sent to filters...
1895 if (attr
->value_tag
== IPP_TAG_MIMETYPE
||
1896 attr
->value_tag
== IPP_TAG_NAMELANG
||
1897 attr
->value_tag
== IPP_TAG_TEXTLANG
||
1898 attr
->value_tag
== IPP_TAG_URI
||
1899 attr
->value_tag
== IPP_TAG_URISCHEME
)
1902 if (strncmp(attr
->name
, "time-", 5) == 0)
1906 * Add space for a leading space and commas between each value.
1907 * For the first attribute, the leading space isn't used, so the
1908 * extra byte can be used as the nul terminator...
1911 bytes
++; /* " " separator */
1912 bytes
+= attr
->num_values
; /* "," separators */
1915 * Boolean attributes appear as "foo,nofoo,foo,nofoo", while
1916 * other attributes appear as "foo=value1,value2,...,valueN".
1919 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
1920 bytes
+= strlen(attr
->name
);
1922 bytes
+= attr
->num_values
* strlen(attr
->name
);
1925 * Now add the size required for each value in the attribute...
1928 switch (attr
->value_tag
)
1930 case IPP_TAG_INTEGER
:
1933 * Minimum value of a signed integer is -2147483647, or 11 digits.
1936 bytes
+= attr
->num_values
* 11;
1939 case IPP_TAG_BOOLEAN
:
1941 * Add two bytes for each false ("no") value...
1944 for (i
= 0; i
< attr
->num_values
; i
++)
1945 if (!attr
->values
[i
].boolean
)
1949 case IPP_TAG_RANGE
:
1951 * A range is two signed integers separated by a hyphen, or
1952 * 23 characters max.
1955 bytes
+= attr
->num_values
* 23;
1958 case IPP_TAG_RESOLUTION
:
1960 * A resolution is two signed integers separated by an "x" and
1961 * suffixed by the units, or 26 characters max.
1964 bytes
+= attr
->num_values
* 26;
1967 case IPP_TAG_STRING
:
1970 case IPP_TAG_KEYWORD
:
1971 case IPP_TAG_CHARSET
:
1972 case IPP_TAG_LANGUAGE
:
1975 * Strings can contain characters that need quoting. We need
1976 * at least 2 * len + 2 characters to cover the quotes and
1977 * any backslashes in the string.
1980 for (i
= 0; i
< attr
->num_values
; i
++)
1981 bytes
+= 2 * strlen(attr
->values
[i
].string
.text
) + 2;
1985 break; /* anti-compiler-warning-code */
1994 * 'load_job_cache()' - Load jobs from the job.cache file.
1998 load_job_cache(const char *filename
) /* I - job.cache filename */
2000 cups_file_t
*fp
; /* job.cache file */
2001 char line
[1024], /* Line buffer */
2002 *value
; /* Value on line */
2003 int linenum
; /* Line number in file */
2004 cupsd_job_t
*job
; /* Current job */
2005 int jobid
; /* Job ID */
2006 char jobfile
[1024]; /* Job filename */
2010 * Open the job.cache file...
2013 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
2015 if (errno
!= ENOENT
)
2016 cupsdLogMessage(CUPSD_LOG_ERROR
,
2017 "Unable to open job cache file \"%s\": %s",
2018 filename
, strerror(errno
));
2020 load_request_root();
2026 * Read entries from the job cache file and create jobs as needed.
2029 cupsdLogMessage(CUPSD_LOG_INFO
, "Loading job cache file \"%s\"...",
2035 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
2037 if (!strcasecmp(line
, "NextJobId"))
2040 NextJobId
= atoi(value
);
2042 else if (!strcasecmp(line
, "<Job"))
2046 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing </Job> directive on line %d!",
2053 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing job ID on line %d!", linenum
);
2057 jobid
= atoi(value
);
2061 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad job ID %d on line %d!", jobid
,
2066 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, jobid
);
2067 if (access(jobfile
, 0))
2069 cupsdLogMessage(CUPSD_LOG_ERROR
, "Job %d files have gone away!", jobid
);
2073 job
= calloc(1, sizeof(cupsd_job_t
));
2076 cupsdLogMessage(CUPSD_LOG_EMERG
,
2077 "Unable to allocate memory for job %d!", jobid
);
2082 job
->back_pipes
[0] = -1;
2083 job
->back_pipes
[1] = -1;
2084 job
->print_pipes
[0] = -1;
2085 job
->print_pipes
[1] = -1;
2086 job
->status_pipes
[0] = -1;
2087 job
->status_pipes
[1] = -1;
2089 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Loading job %d from cache...", job
->id
);
2093 cupsdLogMessage(CUPSD_LOG_ERROR
,
2094 "Missing <Job #> directive on line %d!", linenum
);
2097 else if (!strcasecmp(line
, "</Job>"))
2099 cupsArrayAdd(Jobs
, job
);
2101 if (job
->state_value
<= IPP_JOB_STOPPED
)
2103 cupsArrayAdd(ActiveJobs
, job
);
2111 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing value on line %d!", linenum
);
2114 else if (!strcasecmp(line
, "State"))
2116 job
->state_value
= (ipp_jstate_t
)atoi(value
);
2118 if (job
->state_value
< IPP_JOB_PENDING
)
2119 job
->state_value
= IPP_JOB_PENDING
;
2120 else if (job
->state_value
> IPP_JOB_COMPLETED
)
2121 job
->state_value
= IPP_JOB_COMPLETED
;
2123 else if (!strcasecmp(line
, "Priority"))
2125 job
->priority
= atoi(value
);
2127 else if (!strcasecmp(line
, "Username"))
2129 cupsdSetString(&job
->username
, value
);
2131 else if (!strcasecmp(line
, "Destination"))
2133 cupsdSetString(&job
->dest
, value
);
2135 else if (!strcasecmp(line
, "DestType"))
2137 job
->dtype
= (cups_ptype_t
)atoi(value
);
2139 else if (!strcasecmp(line
, "NumFiles"))
2141 job
->num_files
= atoi(value
);
2143 if (job
->num_files
< 0)
2145 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad NumFiles value %d on line %d!",
2146 job
->num_files
, linenum
);
2151 if (job
->num_files
> 0)
2153 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-001", RequestRoot
,
2155 if (access(jobfile
, 0))
2157 cupsdLogMessage(CUPSD_LOG_INFO
,
2158 "Data files for job %d have gone away!", job
->id
);
2163 job
->filetypes
= calloc(job
->num_files
, sizeof(mime_type_t
*));
2164 job
->compressions
= calloc(job
->num_files
, sizeof(int));
2166 if (!job
->filetypes
|| !job
->compressions
)
2168 cupsdLogMessage(CUPSD_LOG_EMERG
,
2169 "Unable to allocate memory for %d files!",
2175 else if (!strcasecmp(line
, "File"))
2177 int number
, /* File number */
2178 compression
; /* Compression value */
2179 char super
[MIME_MAX_SUPER
], /* MIME super type */
2180 type
[MIME_MAX_TYPE
]; /* MIME type */
2183 if (sscanf(value
, "%d%*[ \t]%15[^/]/%255s%d", &number
, super
, type
,
2186 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File on line %d!", linenum
);
2190 if (number
< 1 || number
> job
->num_files
)
2192 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File number %d on line %d!",
2199 job
->compressions
[number
] = compression
;
2200 job
->filetypes
[number
] = mimeType(MimeDatabase
, super
, type
);
2202 if (!job
->filetypes
[number
])
2205 * If the original MIME type is unknown, auto-type it!
2208 cupsdLogMessage(CUPSD_LOG_ERROR
,
2209 "Unknown MIME type %s/%s for file %d of job %d!",
2210 super
, type
, number
+ 1, job
->id
);
2212 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
2213 job
->id
, number
+ 1);
2214 job
->filetypes
[number
] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
2215 job
->compressions
+ number
);
2218 * If that didn't work, assume it is raw...
2221 if (!job
->filetypes
[number
])
2222 job
->filetypes
[number
] = mimeType(MimeDatabase
, "application",
2227 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unknown %s directive on line %d!",
2236 * 'load_next_job_id()' - Load the NextJobId value from the job.cache file.
2240 load_next_job_id(const char *filename
) /* I - job.cache filename */
2242 cups_file_t
*fp
; /* job.cache file */
2243 char line
[1024], /* Line buffer */
2244 *value
; /* Value on line */
2245 int linenum
; /* Line number in file */
2246 int next_job_id
; /* NextJobId value from line */
2250 * Read the NextJobId directive from the job.cache file and use
2251 * the value (if any).
2254 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
2256 if (errno
!= ENOENT
)
2257 cupsdLogMessage(CUPSD_LOG_ERROR
,
2258 "Unable to open job cache file \"%s\": %s",
2259 filename
, strerror(errno
));
2264 cupsdLogMessage(CUPSD_LOG_INFO
,
2265 "Loading NextJobId from job cache file \"%s\"...", filename
);
2269 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
2271 if (!strcasecmp(line
, "NextJobId"))
2275 next_job_id
= atoi(value
);
2277 if (next_job_id
> NextJobId
)
2278 NextJobId
= next_job_id
;
2289 * 'load_request_root()' - Load jobs from the RequestRoot directory.
2293 load_request_root(void)
2295 cups_dir_t
*dir
; /* Directory */
2296 cups_dentry_t
*dent
; /* Directory entry */
2297 cupsd_job_t
*job
; /* New job */
2301 * Open the requests directory...
2304 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Scanning %s for jobs...", RequestRoot
);
2306 if ((dir
= cupsDirOpen(RequestRoot
)) == NULL
)
2308 cupsdLogMessage(CUPSD_LOG_ERROR
,
2309 "Unable to open spool directory \"%s\": %s",
2310 RequestRoot
, strerror(errno
));
2315 * Read all the c##### files...
2318 while ((dent
= cupsDirRead(dir
)) != NULL
)
2319 if (strlen(dent
->filename
) >= 6 && dent
->filename
[0] == 'c')
2322 * Allocate memory for the job...
2325 if ((job
= calloc(sizeof(cupsd_job_t
), 1)) == NULL
)
2327 cupsdLogMessage(CUPSD_LOG_ERROR
, "Ran out of memory for jobs!");
2333 * Assign the job ID...
2336 job
->id
= atoi(dent
->filename
+ 1);
2337 job
->back_pipes
[0] = -1;
2338 job
->back_pipes
[1] = -1;
2339 job
->print_pipes
[0] = -1;
2340 job
->print_pipes
[1] = -1;
2341 job
->status_pipes
[0] = -1;
2342 job
->status_pipes
[1] = -1;
2344 if (job
->id
>= NextJobId
)
2345 NextJobId
= job
->id
+ 1;
2354 * Insert the job into the array, sorting by job priority and ID...
2357 cupsArrayAdd(Jobs
, job
);
2359 if (job
->state_value
<= IPP_JOB_STOPPED
)
2360 cupsArrayAdd(ActiveJobs
, job
);
2370 * 'set_time()' - Set one of the "time-at-xyz" attributes...
2374 set_time(cupsd_job_t
*job
, /* I - Job to update */
2375 const char *name
) /* I - Name of attribute */
2377 ipp_attribute_t
*attr
; /* Time attribute */
2380 if ((attr
= ippFindAttribute(job
->attrs
, name
, IPP_TAG_ZERO
)) != NULL
)
2382 attr
->value_tag
= IPP_TAG_INTEGER
;
2383 attr
->values
[0].integer
= time(NULL
);
2389 * 'set_hold_until()' - Set the hold time and update job-hold-until attribute...
2393 set_hold_until(cupsd_job_t
*job
, /* I - Job to update */
2394 time_t holdtime
) /* I - Hold until time */
2396 ipp_attribute_t
*attr
; /* job-hold-until attribute */
2397 struct tm
*holddate
; /* Hold date */
2398 char holdstr
[64]; /* Hold time */
2402 * Set the hold_until value and hold the job...
2405 cupsdLogMessage(CUPSD_LOG_DEBUG
, "set_hold_until: hold_until = %d",
2408 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
2409 job
->state_value
= IPP_JOB_HELD
;
2410 job
->hold_until
= holdtime
;
2413 * Update the job-hold-until attribute with a string representing GMT
2414 * time (HH:MM:SS)...
2417 holddate
= gmtime(&holdtime
);
2418 snprintf(holdstr
, sizeof(holdstr
), "%d:%d:%d", holddate
->tm_hour
,
2419 holddate
->tm_min
, holddate
->tm_sec
);
2421 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2422 IPP_TAG_KEYWORD
)) == NULL
)
2423 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2426 * Either add the attribute or update the value of the existing one
2430 attr
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
2431 "job-hold-until", NULL
, holdstr
);
2433 cupsdSetString(&attr
->values
[0].string
.text
, holdstr
);
2440 * 'start_job()' - Start a print job.
2444 start_job(cupsd_job_t
*job
, /* I - Job ID */
2445 cupsd_printer_t
*printer
) /* I - Printer to print job */
2447 int i
; /* Looping var */
2448 int slot
; /* Pipe slot */
2449 cups_array_t
*filters
; /* Filters for job */
2450 mime_filter_t
*filter
, /* Current filter */
2451 port_monitor
; /* Port monitor filter */
2452 char method
[255], /* Method for output */
2453 *optptr
, /* Pointer to options */
2454 *valptr
; /* Pointer in value string */
2455 ipp_attribute_t
*attr
; /* Current attribute */
2456 struct stat backinfo
; /* Backend file information */
2457 int backroot
; /* Run backend as root? */
2458 int pid
; /* Process ID of new filter process */
2459 int banner_page
; /* 1 if banner page, 0 otherwise */
2460 int filterfds
[2][2];/* Pipes used between filters */
2461 int envc
; /* Number of environment variables */
2462 char **argv
, /* Filter command-line arguments */
2463 sani_uri
[1024], /* Sanitized DEVICE_URI env var */
2464 filename
[1024], /* Job filename */
2465 command
[1024], /* Full path to command */
2466 jobid
[255], /* Job ID string */
2467 title
[IPP_MAX_NAME
],
2468 /* Job title string */
2469 copies
[255], /* # copies string */
2470 *envp
[MAX_ENV
+ 11],
2471 /* Environment variables */
2472 charset
[255], /* CHARSET env variable */
2473 class_name
[255],/* CLASS env variable */
2474 classification
[1024],
2475 /* CLASSIFICATION env variable */
2477 /* CONTENT_TYPE env variable */
2479 /* DEVICE_URI env variable */
2480 final_content_type
[1024],
2481 /* FINAL_CONTENT_TYPE env variable */
2482 lang
[255], /* LANG env variable */
2483 ppd
[1024], /* PPD env variable */
2485 /* PRINTER env variable */
2487 /* RIP_MAX_CACHE env variable */
2488 static char *options
= NULL
;/* Full list of options */
2489 static int optlength
= 0; /* Length of option buffer */
2492 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "start_job: id = %d, file = %d/%d",
2493 job
->id
, job
->current_file
, job
->num_files
);
2495 if (job
->num_files
== 0)
2497 cupsdLogMessage(CUPSD_LOG_ERROR
, "Job ID %d has no files! Canceling it!",
2500 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2504 if (printer
->raw
&& !strncmp(printer
->device_uri
, "file:", 5))
2506 cupsdLogMessage(CUPSD_LOG_ERROR
,
2507 "Job ID %d cannot be printed to raw queue pointing to "
2511 strlcpy(printer
->state_message
, "Raw printers cannot use file: devices!",
2512 sizeof(printer
->state_message
));
2513 cupsdStopPrinter(printer
, 1);
2514 cupsdAddPrinterHistory(printer
);
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 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2534 "[Job %d] Sending job to queue tagged as raw...", job
->id
);
2541 * Local jobs get filtered...
2544 filters
= mimeFilter(MimeDatabase
, job
->filetypes
[job
->current_file
],
2545 printer
->filetype
, &(job
->cost
));
2549 cupsdLogMessage(CUPSD_LOG_ERROR
,
2550 "Unable to convert file %d to printable format for "
2552 job
->current_file
, job
->id
);
2553 cupsdLogMessage(CUPSD_LOG_INFO
,
2554 "Hint: Do you have ESP Ghostscript installed?");
2556 if (LogLevel
< CUPSD_LOG_DEBUG
)
2557 cupsdLogMessage(CUPSD_LOG_INFO
,
2558 "Hint: Try setting the LogLevel to \"debug\".");
2560 job
->current_file
++;
2562 if (job
->current_file
== job
->num_files
)
2563 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2569 * Remove NULL ("-") filters...
2572 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
2574 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
2575 if (!strcmp(filter
->filter
, "-"))
2576 cupsArrayRemove(filters
, filter
);
2578 if (cupsArrayCount(filters
) == 0)
2580 cupsArrayDelete(filters
);
2586 * Set a minimum cost of 100 for all jobs so that FilterLimit
2587 * works with raw queues and other low-cost paths.
2590 if (job
->cost
< 100)
2594 * See if the filter cost is too high...
2597 if ((FilterLevel
+ job
->cost
) > FilterLimit
&& FilterLevel
> 0 &&
2601 * Don't print this job quite yet...
2604 cupsArrayDelete(filters
);
2606 cupsdLogMessage(CUPSD_LOG_INFO
,
2607 "Holding job %d because filter limit has been reached.",
2609 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2610 "start_job: id=%d, file=%d, cost=%d, level=%d, "
2612 job
->id
, job
->current_file
, job
->cost
, FilterLevel
,
2617 FilterLevel
+= job
->cost
;
2620 * Add decompression filters, if any...
2623 if (!printer
->raw
&& job
->compressions
[job
->current_file
])
2626 * Add gziptoany filter to the front of the list...
2629 if (!cupsArrayInsert(filters
, &gziptoany_filter
))
2631 cupsdLogMessage(CUPSD_LOG_ERROR
,
2632 "Unable to add decompression filter - %s",
2635 cupsArrayDelete(filters
);
2637 job
->current_file
++;
2639 if (job
->current_file
== job
->num_files
)
2640 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2647 * Add port monitor, if any...
2650 if (printer
->port_monitor
)
2653 * Add port monitor to the end of the list...
2656 if (!cupsArrayAdd(filters
, &port_monitor
))
2658 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to add port monitor - %s",
2661 cupsArrayDelete(filters
);
2663 job
->current_file
++;
2665 if (job
->current_file
== job
->num_files
)
2666 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2671 snprintf(port_monitor
.filter
, sizeof(port_monitor
.filter
),
2672 "%s/monitor/%s", ServerBin
, printer
->port_monitor
);
2676 * Update the printer and job state to "processing"...
2679 job
->state
->values
[0].integer
= IPP_JOB_PROCESSING
;
2680 job
->state_value
= IPP_JOB_PROCESSING
;
2682 job
->printer
= printer
;
2684 cupsdSetPrinterState(printer
, IPP_PRINTER_PROCESSING
, 0);
2686 if (job
->current_file
== 0)
2689 * Set the processing time...
2692 set_time(job
, "time-at-processing");
2695 * Create the backchannel pipes and make them non-blocking...
2698 cupsdOpenPipe(job
->back_pipes
);
2700 fcntl(job
->back_pipes
[0], F_SETFL
,
2701 fcntl(job
->back_pipes
[0], F_GETFL
) | O_NONBLOCK
);
2703 fcntl(job
->back_pipes
[1], F_SETFL
,
2704 fcntl(job
->back_pipes
[1], F_GETFL
) | O_NONBLOCK
);
2708 * Determine if we are printing a banner page or not...
2711 if (job
->job_sheets
== NULL
)
2713 cupsdLogMessage(CUPSD_LOG_DEBUG
, "No job-sheets attribute.");
2714 if ((job
->job_sheets
=
2715 ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_ZERO
)) != NULL
)
2716 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2717 "... but someone added one without setting job_sheets!");
2719 else if (job
->job_sheets
->num_values
== 1)
2720 cupsdLogMessage(CUPSD_LOG_DEBUG
, "job-sheets=%s",
2721 job
->job_sheets
->values
[0].string
.text
);
2723 cupsdLogMessage(CUPSD_LOG_DEBUG
, "job-sheets=%s,%s",
2724 job
->job_sheets
->values
[0].string
.text
,
2725 job
->job_sheets
->values
[1].string
.text
);
2727 if (printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
))
2729 else if (job
->job_sheets
== NULL
)
2731 else if (strcasecmp(job
->job_sheets
->values
[0].string
.text
, "none") != 0 &&
2732 job
->current_file
== 0)
2734 else if (job
->job_sheets
->num_values
> 1 &&
2735 strcasecmp(job
->job_sheets
->values
[1].string
.text
, "none") != 0 &&
2736 job
->current_file
== (job
->num_files
- 1))
2741 cupsdLogMessage(CUPSD_LOG_DEBUG
, "banner_page = %d", banner_page
);
2744 * Building the options string is harder than it needs to be, but
2745 * for the moment we need to pass strings for command-line args and
2746 * not IPP attribute pointers... :)
2748 * First allocate/reallocate the option buffer as needed...
2751 i
= ipp_length(job
->attrs
);
2758 optptr
= realloc(options
, i
);
2762 cupsdLogMessage(CUPSD_LOG_CRIT
,
2763 "Unable to allocate %d bytes for option buffer for "
2764 "job %d!", i
, job
->id
);
2766 cupsArrayDelete(filters
);
2768 FilterLevel
-= job
->cost
;
2770 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2779 * Now loop through the attributes and convert them to the textual
2780 * representation used by the filters...
2786 snprintf(title
, sizeof(title
), "%s-%d", printer
->name
, job
->id
);
2787 strcpy(copies
, "1");
2789 for (attr
= job
->attrs
->attrs
; attr
!= NULL
; attr
= attr
->next
)
2791 if (!strcmp(attr
->name
, "copies") &&
2792 attr
->value_tag
== IPP_TAG_INTEGER
)
2795 * Don't use the # copies attribute if we are printing the job sheets...
2799 sprintf(copies
, "%d", attr
->values
[0].integer
);
2801 else if (!strcmp(attr
->name
, "job-name") &&
2802 (attr
->value_tag
== IPP_TAG_NAME
||
2803 attr
->value_tag
== IPP_TAG_NAMELANG
))
2804 strlcpy(title
, attr
->values
[0].string
.text
, sizeof(title
));
2805 else if (attr
->group_tag
== IPP_TAG_JOB
)
2808 * Filter out other unwanted attributes...
2811 if (attr
->value_tag
== IPP_TAG_MIMETYPE
||
2812 attr
->value_tag
== IPP_TAG_NAMELANG
||
2813 attr
->value_tag
== IPP_TAG_TEXTLANG
||
2814 (attr
->value_tag
== IPP_TAG_URI
&& strcmp(attr
->name
, "job-uuid")) ||
2815 attr
->value_tag
== IPP_TAG_URISCHEME
||
2816 attr
->value_tag
== IPP_TAG_BEGIN_COLLECTION
) /* Not yet supported */
2819 if (!strncmp(attr
->name
, "time-", 5))
2822 if (!strncmp(attr
->name
, "job-", 4) && strcmp(attr
->name
, "job-uuid") &&
2823 !(printer
->type
& CUPS_PRINTER_REMOTE
))
2826 if (!strncmp(attr
->name
, "job-", 4) &&
2827 strcmp(attr
->name
, "job-uuid") &&
2828 strcmp(attr
->name
, "job-billing") &&
2829 strcmp(attr
->name
, "job-sheets") &&
2830 strcmp(attr
->name
, "job-hold-until") &&
2831 strcmp(attr
->name
, "job-priority"))
2834 if ((!strcmp(attr
->name
, "page-label") ||
2835 !strcmp(attr
->name
, "page-border") ||
2836 !strncmp(attr
->name
, "number-up", 9) ||
2837 !strcmp(attr
->name
, "page-set") ||
2838 !strcasecmp(attr
->name
, "AP_FIRSTPAGE_InputSlot") ||
2839 !strcasecmp(attr
->name
, "AP_FIRSTPAGE_ManualFeed")) &&
2844 * Otherwise add them to the list...
2847 if (optptr
> options
)
2848 strlcat(optptr
, " ", optlength
- (optptr
- options
));
2850 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
2852 strlcat(optptr
, attr
->name
, optlength
- (optptr
- options
));
2853 strlcat(optptr
, "=", optlength
- (optptr
- options
));
2856 for (i
= 0; i
< attr
->num_values
; i
++)
2859 strlcat(optptr
, ",", optlength
- (optptr
- options
));
2861 optptr
+= strlen(optptr
);
2863 switch (attr
->value_tag
)
2865 case IPP_TAG_INTEGER
:
2867 snprintf(optptr
, optlength
- (optptr
- options
),
2868 "%d", attr
->values
[i
].integer
);
2871 case IPP_TAG_BOOLEAN
:
2872 if (!attr
->values
[i
].boolean
)
2873 strlcat(optptr
, "no", optlength
- (optptr
- options
));
2875 case IPP_TAG_NOVALUE
:
2876 strlcat(optptr
, attr
->name
,
2877 optlength
- (optptr
- options
));
2880 case IPP_TAG_RANGE
:
2881 if (attr
->values
[i
].range
.lower
== attr
->values
[i
].range
.upper
)
2882 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2883 "%d", attr
->values
[i
].range
.lower
);
2885 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2886 "%d-%d", attr
->values
[i
].range
.lower
,
2887 attr
->values
[i
].range
.upper
);
2890 case IPP_TAG_RESOLUTION
:
2891 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2892 "%dx%d%s", attr
->values
[i
].resolution
.xres
,
2893 attr
->values
[i
].resolution
.yres
,
2894 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
2898 case IPP_TAG_STRING
:
2901 case IPP_TAG_KEYWORD
:
2902 case IPP_TAG_CHARSET
:
2903 case IPP_TAG_LANGUAGE
:
2905 for (valptr
= attr
->values
[i
].string
.text
; *valptr
;)
2907 if (strchr(" \t\n\\\'\"", *valptr
))
2909 *optptr
++ = *valptr
++;
2916 break; /* anti-compiler-warning-code */
2920 optptr
+= strlen(optptr
);
2925 * Build the command-line arguments for the filters. Each filter
2926 * has 6 or 7 arguments:
2930 * argv[2] = username
2932 * argv[4] = # copies
2934 * argv[6] = filename (optional; normally stdin)
2936 * This allows legacy printer drivers that use the old System V
2937 * printing interface to be used by CUPS.
2939 * For remote jobs, we send all of the files in the argument list.
2942 if (printer
->remote
&& job
->num_files
> 1)
2943 argv
= calloc(7 + job
->num_files
, sizeof(char *));
2945 argv
= calloc(8, sizeof(char *));
2947 sprintf(jobid
, "%d", job
->id
);
2949 argv
[0] = printer
->name
;
2951 argv
[2] = job
->username
;
2956 if (printer
->remote
&& job
->num_files
> 1)
2958 for (i
= 0; i
< job
->num_files
; i
++)
2960 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
2962 argv
[6 + i
] = strdup(filename
);
2967 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
2968 job
->id
, job
->current_file
+ 1);
2972 for (i
= 0; argv
[i
]; i
++)
2973 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2974 "[Job %d] argv[%d]=\"%s\"", job
->id
, i
, argv
[i
]);
2977 * Create environment variable strings for the filters...
2980 attr
= ippFindAttribute(job
->attrs
, "attributes-natural-language",
2983 switch (strlen(attr
->values
[0].string
.text
))
2987 * This is an unknown or badly formatted language code; use
2988 * the POSIX locale...
2991 strcpy(lang
, "LANG=C");
2996 * Just the language code (ll)...
2999 snprintf(lang
, sizeof(lang
), "LANG=%s",
3000 attr
->values
[0].string
.text
);
3005 * Language and country code (ll-cc)...
3008 snprintf(lang
, sizeof(lang
), "LANG=%c%c_%c%c",
3009 attr
->values
[0].string
.text
[0],
3010 attr
->values
[0].string
.text
[1],
3011 toupper(attr
->values
[0].string
.text
[3] & 255),
3012 toupper(attr
->values
[0].string
.text
[4] & 255));
3016 attr
= ippFindAttribute(job
->attrs
, "document-format",
3019 (optptr
= strstr(attr
->values
[0].string
.text
, "charset=")) != NULL
)
3020 snprintf(charset
, sizeof(charset
), "CHARSET=%s", optptr
+ 8);
3023 attr
= ippFindAttribute(job
->attrs
, "attributes-charset",
3025 snprintf(charset
, sizeof(charset
), "CHARSET=%s",
3026 attr
->values
[0].string
.text
);
3029 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s/%s",
3030 job
->filetypes
[job
->current_file
]->super
,
3031 job
->filetypes
[job
->current_file
]->type
);
3032 snprintf(device_uri
, sizeof(device_uri
), "DEVICE_URI=%s",
3033 printer
->device_uri
);
3034 cupsdSanitizeURI(printer
->device_uri
, sani_uri
, sizeof(sani_uri
));
3035 snprintf(ppd
, sizeof(ppd
), "PPD=%s/ppd/%s.ppd", ServerRoot
, printer
->name
);
3036 snprintf(printer_name
, sizeof(printer_name
), "PRINTER=%s", printer
->name
);
3037 snprintf(rip_max_cache
, sizeof(rip_max_cache
), "RIP_MAX_CACHE=%s", RIPCache
);
3039 envc
= cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
3041 envp
[envc
++] = charset
;
3042 envp
[envc
++] = lang
;
3043 envp
[envc
++] = ppd
;
3044 envp
[envc
++] = rip_max_cache
;
3045 envp
[envc
++] = content_type
;
3046 envp
[envc
++] = device_uri
;
3047 envp
[envc
++] = printer_name
;
3049 if (!printer
->remote
&&
3050 (filter
= (mime_filter_t
*)cupsArrayLast(filters
)) != NULL
)
3052 snprintf(final_content_type
, sizeof(final_content_type
),
3053 "FINAL_CONTENT_TYPE=%s/%s",
3054 filter
->dst
->super
, filter
->dst
->type
);
3055 envp
[envc
++] = final_content_type
;
3058 if (Classification
&& !banner_page
)
3060 if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
3061 IPP_TAG_NAME
)) == NULL
)
3062 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3064 else if (attr
->num_values
> 1 &&
3065 strcmp(attr
->values
[1].string
.text
, "none") != 0)
3066 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3067 attr
->values
[1].string
.text
);
3069 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3070 attr
->values
[0].string
.text
);
3072 envp
[envc
++] = classification
;
3075 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
3077 snprintf(class_name
, sizeof(class_name
), "CLASS=%s", job
->dest
);
3078 envp
[envc
++] = class_name
;
3083 for (i
= 0; i
< envc
; i
++)
3084 if (strncmp(envp
[i
], "DEVICE_URI=", 11))
3085 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] envp[%d]=\"%s\"",
3086 job
->id
, i
, envp
[i
]);
3088 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] envp[%d]=\"DEVICE_URI=%s\"",
3089 job
->id
, i
, sani_uri
);
3091 if (printer
->remote
)
3092 job
->current_file
= job
->num_files
;
3094 job
->current_file
++;
3097 * Now create processes for all of the filters...
3100 filterfds
[0][0] = -1;
3101 filterfds
[0][1] = -1;
3102 filterfds
[1][0] = -1;
3103 filterfds
[1][1] = -1;
3105 if (!job
->status_buffer
)
3107 if (cupsdOpenPipe(job
->status_pipes
))
3109 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create job status pipes - %s.",
3111 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3112 "Unable to create status pipes - %s.", strerror(errno
));
3114 cupsdAddPrinterHistory(printer
);
3116 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3117 "Job canceled because the server could not create the job "
3123 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "start_job: status_pipes = [ %d %d ]",
3124 job
->status_pipes
[0], job
->status_pipes
[1]);
3126 job
->status_buffer
= cupsdStatBufNew(job
->status_pipes
[0], "[Job %d]",
3131 memset(job
->filters
, 0, sizeof(job
->filters
));
3133 for (i
= 0, slot
= 0, filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
3135 i
++, filter
= (mime_filter_t
*)cupsArrayNext(filters
))
3137 if (filter
->filter
[0] != '/')
3138 snprintf(command
, sizeof(command
), "%s/filter/%s", ServerBin
,
3141 strlcpy(command
, filter
->filter
, sizeof(command
));
3143 if (i
< (cupsArrayCount(filters
) - 1))
3145 if (cupsdOpenPipe(filterfds
[slot
]))
3147 cupsdLogMessage(CUPSD_LOG_ERROR
,
3148 "Unable to create job filter pipes - %s.",
3150 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3151 "Unable to create filter pipes - %s.", strerror(errno
));
3152 cupsdAddPrinterHistory(printer
);
3154 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3155 "Job canceled because the server could not create the "
3163 if (job
->current_file
== 1)
3165 if (strncmp(printer
->device_uri
, "file:", 5) != 0)
3167 if (cupsdOpenPipe(job
->print_pipes
))
3169 cupsdLogMessage(CUPSD_LOG_ERROR
,
3170 "Unable to create job backend pipes - %s.",
3172 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3173 "Unable to create backend pipes - %s.", strerror(errno
));
3174 cupsdAddPrinterHistory(printer
);
3176 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3177 "Job canceled because the server could not create "
3178 "the backend pipes.");
3185 job
->print_pipes
[0] = -1;
3186 if (!strcmp(printer
->device_uri
, "file:/dev/null") ||
3187 !strcmp(printer
->device_uri
, "file:///dev/null"))
3188 job
->print_pipes
[1] = -1;
3191 if (!strncmp(printer
->device_uri
, "file:/dev/", 10))
3192 job
->print_pipes
[1] = open(printer
->device_uri
+ 5,
3194 else if (!strncmp(printer
->device_uri
, "file:///dev/", 12))
3195 job
->print_pipes
[1] = open(printer
->device_uri
+ 7,
3197 else if (!strncmp(printer
->device_uri
, "file:///", 8))
3198 job
->print_pipes
[1] = open(printer
->device_uri
+ 7,
3199 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
3201 job
->print_pipes
[1] = open(printer
->device_uri
+ 5,
3202 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
3204 if (job
->print_pipes
[1] < 0)
3206 cupsdLogMessage(CUPSD_LOG_ERROR
,
3207 "Unable to open output file \"%s\" - %s.",
3208 printer
->device_uri
, strerror(errno
));
3209 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3210 "Unable to open output file \"%s\" - %s.",
3211 printer
->device_uri
, strerror(errno
));
3213 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3214 "Job canceled because the server could not open the "
3220 fcntl(job
->print_pipes
[1], F_SETFD
,
3221 fcntl(job
->print_pipes
[1], F_GETFD
) | FD_CLOEXEC
);
3225 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3226 "start_job: print_pipes = [ %d %d ]",
3227 job
->print_pipes
[0], job
->print_pipes
[1]);
3230 filterfds
[slot
][0] = job
->print_pipes
[0];
3231 filterfds
[slot
][1] = job
->print_pipes
[1];
3234 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "start_job: filter=\"%s\"",
3236 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3237 "start_job: filterfds[%d]=[ %d %d ]",
3238 slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3240 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
3241 filterfds
[slot
][1], job
->status_pipes
[1],
3242 job
->back_pipes
[0], 0, job
->filters
+ i
);
3244 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3245 "start_job: Closing filter pipes for slot %d "
3247 !slot
, filterfds
[!slot
][0], filterfds
[!slot
][1]);
3249 cupsdClosePipe(filterfds
[!slot
]);
3253 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to start filter \"%s\" - %s.",
3254 filter
->filter
, strerror(errno
));
3255 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3256 "Unable to start filter \"%s\" - %s.",
3257 filter
->filter
, strerror(errno
));
3259 cupsdAddPrinterHistory(printer
);
3261 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3262 "Job canceled because the server could not execute a "
3268 cupsdLogMessage(CUPSD_LOG_INFO
, "Started filter %s (PID %d) for job %d.",
3269 command
, pid
, job
->id
);
3275 cupsArrayDelete(filters
);
3278 * Finally, pipe the final output into a backend process if needed...
3281 if (strncmp(printer
->device_uri
, "file:", 5) != 0)
3283 if (job
->current_file
== 1)
3285 sscanf(printer
->device_uri
, "%254[^:]", method
);
3286 snprintf(command
, sizeof(command
), "%s/backend/%s", ServerBin
, method
);
3289 * See if the backend needs to run as root...
3294 else if (stat(command
, &backinfo
))
3297 backroot
= !(backinfo
.st_mode
& (S_IRWXG
| S_IRWXO
));
3301 filterfds
[slot
][0] = -1;
3302 filterfds
[slot
][1] = -1;
3304 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "start_job: backend=\"%s\"",
3306 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3307 "start_job: filterfds[%d] = [ %d %d ]",
3308 slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3310 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
3311 filterfds
[slot
][1], job
->status_pipes
[1],
3312 job
->back_pipes
[1], backroot
,
3317 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to start backend \"%s\" - %s.",
3318 method
, strerror(errno
));
3319 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3320 "Unable to start backend \"%s\" - %s.", method
,
3323 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3324 "Job canceled because the server could not execute "
3331 cupsdLogMessage(CUPSD_LOG_INFO
,
3332 "Started backend %s (PID %d) for job %d.",
3333 command
, pid
, job
->id
);
3337 if (job
->current_file
== job
->num_files
)
3339 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3340 "start_job: Closing print pipes [ %d %d ]...",
3341 job
->print_pipes
[0], job
->print_pipes
[1]);
3343 cupsdClosePipe(job
->print_pipes
);
3345 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3346 "start_job: Closing back pipes [ %d %d ]...",
3347 job
->back_pipes
[0], job
->back_pipes
[1]);
3349 cupsdClosePipe(job
->back_pipes
);
3351 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3352 "start_job: Closing status output pipe %d...",
3353 job
->status_pipes
[1]);
3355 close(job
->status_pipes
[1]);
3356 job
->status_pipes
[1] = -1;
3361 filterfds
[slot
][0] = -1;
3362 filterfds
[slot
][1] = -1;
3364 if (job
->current_file
== job
->num_files
)
3366 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3367 "start_job: Closing print pipes [ %d %d ]...",
3368 job
->print_pipes
[0], job
->print_pipes
[1]);
3370 cupsdClosePipe(job
->print_pipes
);
3372 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3373 "start_job: Closing status output pipe %d...",
3374 job
->status_pipes
[1]);
3376 close(job
->status_pipes
[1]);
3377 job
->status_pipes
[1] = -1;
3381 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3382 "start_job: Closing filter pipes for slot %d "
3384 slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3385 cupsdClosePipe(filterfds
[slot
]);
3387 if (printer
->remote
&& job
->num_files
> 1)
3389 for (i
= 0; i
< job
->num_files
; i
++)
3395 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3396 "start_job: Adding fd %d to InputSet...",
3397 job
->status_buffer
->fd
);
3399 FD_SET(job
->status_buffer
->fd
, InputSet
);
3401 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
, "Job #%d started.",
3408 * If we get here, we need to abort the current job and close out all
3409 * files and pipes...
3414 for (slot
= 0; slot
< 2; slot
++)
3416 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3417 "start_job: Closing filter pipes for slot %d "
3419 slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3420 cupsdClosePipe(filterfds
[slot
]);
3423 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3424 "start_job: Closing status pipes [ %d %d ]...",
3425 job
->status_pipes
[0], job
->status_pipes
[1]);
3426 cupsdClosePipe(job
->status_pipes
);
3427 cupsdStatBufDelete(job
->status_buffer
);
3429 cupsArrayDelete(filters
);
3431 if (printer
->remote
&& job
->num_files
> 1)
3433 for (i
= 0; i
< job
->num_files
; i
++)
3439 cupsdStopJob(job
, 0);
3444 * 'unload_job()' - Unload a job from memory.
3448 unload_job(cupsd_job_t
*job
) /* I - Job */
3453 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Unloading job %d...", job
->id
);
3455 ippDelete(job
->attrs
);
3460 job
->job_sheets
= NULL
;
3465 * End of "$Id: job.c 5974 2006-09-20 18:42:37Z mike $".