2 * "$Id: job.c 5131 2006-02-18 05:31:36Z 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 * cupsdStartJob() - Start a print job.
51 * cupsdStopAllJobs() - Stop all print jobs.
52 * cupsdStopJob() - Stop a print job.
53 * cupsdUnloadCompletedJobs() - Flush completed job history from memory.
54 * cupsdUnloadJob() - Unload a job from memory.
55 * cupsdUpdateJob() - Read a status update from a jobs filters.
56 * compare_active_jobs() - Compare the job IDs and priorities of two
58 * compare_jobs() - Compare the job IDs of two jobs.
59 * free_job() - Free all memory used by a job.
60 * ipp_length() - Compute the size of the buffer needed to
61 * hold the textual IPP attributes.
62 * load_job_cache() - Load jobs from the job.cache file.
63 * load_next_job_id() - Load the NextJobId value from the
65 * load_request_root() - Load jobs from the RequestRoot directory.
66 * set_time() - Set one of the "time-at-xyz" attributes...
67 * set_hold_until() - Set the hold time and update job-hold-until
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
);
110 * 'cupsdAddJob()' - Add a new job to the job queue...
113 cupsd_job_t
* /* O - New job record */
114 cupsdAddJob(int priority
, /* I - Job priority */
115 const char *dest
) /* I - Job destination */
117 cupsd_job_t
*job
; /* New job record */
120 job
= calloc(sizeof(cupsd_job_t
), 1);
122 job
->id
= NextJobId
++;
123 job
->priority
= priority
;
124 job
->back_pipes
[0] = -1;
125 job
->back_pipes
[1] = -1;
126 job
->print_pipes
[0] = -1;
127 job
->print_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? */
150 int i
; /* Looping var */
151 char filename
[1024]; /* Job filename */
154 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdCancelJob: id = %d", job
->id
);
157 * Stop any processes that are working on the current job...
160 if (job
->state_value
== IPP_JOB_PROCESSING
)
161 cupsdStopJob(job
, 0);
166 job
->state
->values
[0].integer
= IPP_JOB_CANCELLED
;
168 job
->state_value
= IPP_JOB_CANCELLED
;
170 set_time(job
, "time-at-completed");
172 cupsdExpireSubscriptions(NULL
, job
);
175 * Remove the job from the active list...
178 cupsArrayRemove(ActiveJobs
, job
);
181 * Remove any authentication data...
184 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
188 * Remove the print file for good if we aren't preserving jobs or
192 job
->current_file
= 0;
194 if (!JobHistory
|| !JobFiles
|| purge
|| (job
->dtype
& CUPS_PRINTER_REMOTE
))
196 for (i
= 1; i
<= job
->num_files
; i
++)
198 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
203 if (job
->num_files
> 0)
205 free(job
->filetypes
);
206 free(job
->compressions
);
209 job
->filetypes
= NULL
;
210 job
->compressions
= NULL
;
214 if (JobHistory
&& !purge
&& !(job
->dtype
& CUPS_PRINTER_REMOTE
))
217 * Save job state info...
225 * Remove the job info file...
228 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
,
233 * Remove the job from the "all jobs" list...
236 cupsArrayRemove(Jobs
, job
);
239 * Free all memory used...
248 * 'cupsdCancelJobs()' - Cancel all jobs for the given destination/user...
252 cupsdCancelJobs(const char *dest
, /* I - Destination to cancel */
253 const char *username
, /* I - Username or NULL */
254 int purge
) /* I - Purge jobs? */
256 cupsd_job_t
*job
; /* Current job */
259 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
261 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
262 if ((dest
== NULL
|| !strcmp(job
->dest
, dest
)) &&
263 (username
== NULL
|| !strcmp(job
->username
, username
)))
266 * Cancel all jobs matching this destination/user...
269 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
270 purge
? "Job purged." : "Job canceled.");
272 cupsdCancelJob(job
, purge
);
280 * 'cupsdCheckJobs()' - Check the pending jobs and start any if the destination
287 cupsd_job_t
*job
; /* Current job in queue */
288 cupsd_printer_t
*printer
, /* Printer destination */
289 *pclass
; /* Printer class destination */
292 DEBUG_puts("cupsdCheckJobs()");
294 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
295 "cupsdCheckJobs: %d active jobs, sleeping=%d, reload=%d",
296 cupsArrayCount(ActiveJobs
), Sleeping
, NeedReload
);
298 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
300 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
303 * Start held jobs if they are ready...
306 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
307 "cupsdCheckJobs: Job %d: state_value=%d, loaded=%s",
308 job
->id
, job
->state_value
, job
->attrs
? "yes" : "no");
310 if (job
->state_value
== IPP_JOB_HELD
&&
312 job
->hold_until
< time(NULL
))
314 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
315 job
->state_value
= IPP_JOB_PENDING
;
319 * Start pending jobs if the destination is available...
322 if (job
->state_value
== IPP_JOB_PENDING
&& !NeedReload
&& !Sleeping
)
324 printer
= cupsdFindDest(job
->dest
);
328 (printer
->type
& (CUPS_PRINTER_IMPLICIT
| CUPS_PRINTER_CLASS
)))
331 * If the class is remote, just pass it to the remote server...
336 if (!(pclass
->type
& CUPS_PRINTER_REMOTE
))
338 if (pclass
->state
!= IPP_PRINTER_STOPPED
)
339 printer
= cupsdFindAvailablePrinter(job
->dest
);
345 if (!printer
&& !pclass
)
348 * Whoa, the printer and/or class for this destination went away;
352 cupsdLogMessage(CUPSD_LOG_WARN
,
353 "Printer/class %s has gone away; cancelling job %d!",
356 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
357 "Job canceled because the destination printer/class has "
360 cupsdCancelJob(job
, 1);
365 * See if the printer is available or remote and not printing a job;
366 * if so, start the job...
372 * Add/update a job-actual-printer-uri attribute for this job
373 * so that we know which printer actually printed the job...
376 ipp_attribute_t
*attr
; /* job-actual-printer-uri attribute */
379 if ((attr
= ippFindAttribute(job
->attrs
, "job-actual-printer-uri",
380 IPP_TAG_URI
)) != NULL
)
381 cupsdSetString(&attr
->values
[0].string
.text
, printer
->uri
);
383 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
,
384 "job-actual-printer-uri", NULL
, printer
->uri
);
387 if (printer
->state
== IPP_PRINTER_IDLE
|| /* Printer is idle */
388 ((printer
->type
& CUPS_PRINTER_REMOTE
) && /* Printer is remote */
389 !printer
->job
)) /* and not printing */
390 cupsdStartJob(job
, printer
);
398 * 'cupsdCleanJobs()' - Clean out old jobs.
404 cupsd_job_t
*job
; /* Current job */
410 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
411 job
&& cupsArrayCount(Jobs
) >= MaxJobs
;
412 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
413 if (job
->state_value
>= IPP_JOB_CANCELLED
)
414 cupsdCancelJob(job
, 1);
419 * 'cupsdFinishJob()' - Finish a job.
423 cupsdFinishJob(cupsd_job_t
*job
) /* I - Job */
425 int job_history
; /* Did cupsdCancelJob() keep the job? */
426 cupsd_printer_t
*printer
; /* Current printer */
429 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] File %d is complete.",
430 job
->id
, job
->current_file
- 1);
432 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFinishJob: job->status is %d",
435 if (job
->status_buffer
&& job
->current_file
>= job
->num_files
)
438 * Close the pipe and clear the input bit.
441 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
442 "cupsdFinishJob: Removing fd %d from InputSet...",
443 job
->status_buffer
->fd
);
445 FD_CLR(job
->status_buffer
->fd
, InputSet
);
447 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
448 "cupsdFinishJob: Closing status input pipe %d...",
449 job
->status_buffer
->fd
);
451 cupsdStatBufDelete(job
->status_buffer
);
453 job
->status_buffer
= NULL
;
456 printer
= job
->printer
;
461 * Backend had errors; stop it...
464 switch (-job
->status
)
467 case CUPS_BACKEND_FAILED
:
469 * Backend failure, use the error-policy to determine how to
473 cupsdStopJob(job
, 0);
474 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
475 job
->state_value
= IPP_JOB_PENDING
;
479 * If the job was queued to a class, try requeuing it... For
480 * faxes and retry-job queues, hold the current job for 5 minutes.
483 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
485 else if ((printer
->type
& CUPS_PRINTER_FAX
) ||
486 !strcmp(printer
->error_policy
, "retry-job"))
489 * See how many times we've tried to send the job; if more than
490 * the limit, cancel the job.
495 if (job
->tries
>= JobRetryLimit
)
501 cupsdLogMessage(CUPSD_LOG_ERROR
,
502 "Canceling job %d since it could not be sent "
504 job
->id
, JobRetryLimit
);
506 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, printer
, job
,
507 "Job canceled since it could not be sent after %d "
511 cupsdCancelJob(job
, 0);
516 * Try again in N seconds...
519 set_hold_until(job
, time(NULL
) + JobRetryInterval
);
522 else if (!strcmp(printer
->error_policy
, "abort-job"))
523 cupsdCancelJob(job
, 0);
526 case CUPS_BACKEND_CANCEL
:
531 cupsdCancelJob(job
, 0);
534 case CUPS_BACKEND_HOLD
:
539 cupsdStopJob(job
, 0);
540 cupsdSetJobHoldUntil(job
, "indefinite");
544 case CUPS_BACKEND_STOP
:
546 * Stop the printer...
549 cupsdStopJob(job
, 0);
551 cupsdSetPrinterState(printer
, IPP_PRINTER_STOPPED
, 1);
554 case CUPS_BACKEND_AUTH_REQUIRED
:
555 cupsdStopJob(job
, 0);
556 cupsdSetJobHoldUntil(job
, "authenticated");
559 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
560 "Authentication is required for job %d.", job
->id
);
565 * Try printing another job...
570 else if (job
->status
> 0)
573 * Filter had errors; stop job...
576 cupsdStopJob(job
, 1);
578 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
579 "Job stopped due to filter errors; please consult the "
580 "error_log file for details.");
586 * Job printed successfully; cancel it...
589 if (job
->current_file
< job
->num_files
)
592 * Start the next file in the job...
595 FilterLevel
-= job
->cost
;
596 cupsdStartJob(job
, printer
);
601 * Close out this job...
604 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, printer
, job
,
605 "Job completed successfully.");
607 job_history
= JobHistory
&& !(job
->dtype
& CUPS_PRINTER_REMOTE
);
609 cupsdCancelJob(job
, 0);
613 job
->state
->values
[0].integer
= IPP_JOB_COMPLETED
;
614 job
->state_value
= IPP_JOB_COMPLETED
;
619 * Clear the printer's state_message and state_reasons and move on...
622 printer
->state_message
[0] = '\0';
624 cupsdSetPrinterReasons(printer
, "");
633 * 'cupsdFreeAllJobs()' - Free all jobs from memory.
637 cupsdFreeAllJobs(void)
639 cupsd_job_t
*job
; /* Current job */
650 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
652 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
654 cupsArrayRemove(Jobs
, job
);
655 cupsArrayRemove(ActiveJobs
, job
);
660 cupsdReleaseSignals();
665 * 'cupsdFindJob()' - Find the specified job.
668 cupsd_job_t
* /* O - Job data */
669 cupsdFindJob(int id
) /* I - Job ID */
671 cupsd_job_t key
; /* Search key */
676 return ((cupsd_job_t
*)cupsArrayFind(Jobs
, &key
));
681 * 'cupsdGetPrinterJobCount()' - Get the number of pending, processing,
682 * or held jobs in a printer or class.
685 int /* O - Job count */
686 cupsdGetPrinterJobCount(
687 const char *dest
) /* I - Printer or class name */
689 int count
; /* Job count */
690 cupsd_job_t
*job
; /* Current job */
693 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
695 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
696 if (job
->dest
&& !strcasecmp(job
->dest
, dest
))
704 * 'cupsdGetUserJobCount()' - Get the number of pending, processing,
705 * or held jobs for a user.
708 int /* O - Job count */
709 cupsdGetUserJobCount(
710 const char *username
) /* I - Username */
712 int count
; /* Job count */
713 cupsd_job_t
*job
; /* Current job */
716 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
718 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
719 if (!strcasecmp(job
->username
, username
))
727 * 'cupsdHoldJob()' - Hold the specified job.
731 cupsdHoldJob(cupsd_job_t
*job
) /* I - Job data */
733 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdHoldJob: id = %d", job
->id
);
735 if (job
->state_value
== IPP_JOB_PROCESSING
)
736 cupsdStopJob(job
, 0);
740 DEBUG_puts("cupsdHoldJob: setting state to held...");
742 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
743 job
->state_value
= IPP_JOB_HELD
;
752 * 'cupsdLoadAllJobs()' - Load all jobs from disk.
756 cupsdLoadAllJobs(void)
758 char filename
[1024]; /* Full filename of job.cache file */
759 struct stat fileinfo
, /* Information on job.cache file */
760 dirinfo
; /* Information on RequestRoot dir */
765 * Create the job arrays as needed...
769 Jobs
= cupsArrayNew(compare_jobs
, NULL
);
772 ActiveJobs
= cupsArrayNew(compare_active_jobs
, NULL
);
775 * See whether the job.cache file is older than the RequestRoot directory...
778 snprintf(filename
, sizeof(filename
), "%s/job.cache", CacheDir
);
780 if (stat(filename
, &fileinfo
))
782 fileinfo
.st_mtime
= 0;
785 cupsdLogMessage(CUPSD_LOG_ERROR
,
786 "Unable to get file information for \"%s\" - %s",
787 filename
, strerror(errno
));
790 if (stat(RequestRoot
, &dirinfo
))
792 dirinfo
.st_mtime
= 0;
795 cupsdLogMessage(CUPSD_LOG_ERROR
,
796 "Unable to get directory information for \"%s\" - %s",
797 RequestRoot
, strerror(errno
));
801 * Load the most recent source for job data...
804 if (dirinfo
.st_mtime
> fileinfo
.st_mtime
)
808 load_next_job_id(filename
);
811 load_job_cache(filename
);
814 * Clean out old jobs as needed...
817 if (MaxJobs
> 0 && cupsArrayCount(Jobs
) >= MaxJobs
)
823 * 'cupsdLoadJob()' - Load a single job...
827 cupsdLoadJob(cupsd_job_t
*job
) /* I - Job */
829 char jobfile
[1024]; /* Job filename */
830 cups_file_t
*fp
; /* Job file */
831 int fileid
; /* Current file ID */
832 ipp_attribute_t
*attr
; /* Job attribute */
833 char scheme
[32], /* Scheme portion of URI */
834 username
[64], /* Username portion of URI */
836 /* Host portion of URI */
837 resource
[HTTP_MAX_URI
];
838 /* Resource portion of URI */
839 int port
; /* Port portion of URI */
840 const char *dest
; /* Destination */
841 mime_type_t
**filetypes
; /* New filetypes array */
842 int *compressions
; /* New compressions array */
847 if (job
->state_value
>= IPP_JOB_STOPPED
)
848 job
->access_time
= time(NULL
);
853 if ((job
->attrs
= ippNew()) == NULL
)
855 cupsdLogMessage(CUPSD_LOG_ERROR
, "Ran out of memory for job attributes!");
860 * Load job attributes...
863 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Loading attributes for job %d...",
866 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, job
->id
);
867 if ((fp
= cupsFileOpen(jobfile
, "r")) == NULL
)
869 cupsdLogMessage(CUPSD_LOG_ERROR
,
870 "Unable to open job control file \"%s\" - %s!",
871 jobfile
, strerror(errno
));
872 ippDelete(job
->attrs
);
877 if (ippReadIO(fp
, (ipp_iocb_t
)cupsFileRead
, 1, NULL
, job
->attrs
) != IPP_DATA
)
879 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to read job control file \"%s\"!",
882 ippDelete(job
->attrs
);
891 * Copy attribute data to the job object...
894 if ((job
->state
= ippFindAttribute(job
->attrs
, "job-state",
895 IPP_TAG_ENUM
)) == NULL
)
897 cupsdLogMessage(CUPSD_LOG_ERROR
,
898 "Missing or bad job-state attribute in control "
901 ippDelete(job
->attrs
);
907 job
->state_value
= (ipp_jstate_t
)job
->state
->values
[0].integer
;
911 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
912 IPP_TAG_URI
)) == NULL
)
914 cupsdLogMessage(CUPSD_LOG_ERROR
,
915 "No job-printer-uri attribute in control file \"%s\"!",
917 ippDelete(job
->attrs
);
923 httpSeparateURI(HTTP_URI_CODING_ALL
, attr
->values
[0].string
.text
, scheme
,
924 sizeof(scheme
), username
, sizeof(username
), host
,
925 sizeof(host
), &port
, resource
, sizeof(resource
));
927 if ((dest
= cupsdValidateDest(host
, resource
, &(job
->dtype
),
930 cupsdLogMessage(CUPSD_LOG_ERROR
,
931 "Unable to queue job for destination \"%s\"!",
932 attr
->values
[0].string
.text
);
933 ippDelete(job
->attrs
);
939 cupsdSetString(&job
->dest
, dest
);
942 job
->sheets
= ippFindAttribute(job
->attrs
, "job-media-sheets-completed",
944 job
->job_sheets
= ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_NAME
);
948 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
949 IPP_TAG_INTEGER
)) == NULL
)
951 cupsdLogMessage(CUPSD_LOG_ERROR
,
952 "Missing or bad job-priority attribute in control "
953 "file \"%s\"!", jobfile
);
954 ippDelete(job
->attrs
);
960 job
->priority
= attr
->values
[0].integer
;
965 if ((attr
= ippFindAttribute(job
->attrs
, "job-originating-user-name",
966 IPP_TAG_NAME
)) == NULL
)
968 cupsdLogMessage(CUPSD_LOG_ERROR
,
969 "Missing or bad job-originating-user-name attribute "
970 "in control file \"%s\"!", jobfile
);
971 ippDelete(job
->attrs
);
977 cupsdSetString(&job
->username
, attr
->values
[0].string
.text
);
981 * Set the job hold-until time and state...
984 if (job
->state_value
== IPP_JOB_HELD
)
986 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
987 IPP_TAG_KEYWORD
)) == NULL
)
988 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
991 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
994 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
995 job
->state_value
= IPP_JOB_PENDING
;
998 else if (job
->state_value
== IPP_JOB_PROCESSING
)
1000 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1001 job
->state_value
= IPP_JOB_PENDING
;
1004 if (!job
->num_files
)
1007 * Find all the d##### files...
1010 for (fileid
= 1; fileid
< 10000; fileid
++)
1012 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
1015 if (access(jobfile
, 0))
1018 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Auto-typing document file \"%s\"...",
1021 if (fileid
> job
->num_files
)
1023 if (job
->num_files
== 0)
1025 compressions
= (int *)calloc(fileid
, sizeof(int));
1026 filetypes
= (mime_type_t
**)calloc(fileid
, sizeof(mime_type_t
*));
1030 compressions
= (int *)realloc(job
->compressions
,
1031 sizeof(int) * fileid
);
1032 filetypes
= (mime_type_t
**)realloc(job
->filetypes
,
1033 sizeof(mime_type_t
*) *
1037 if (!compressions
|| !filetypes
)
1039 cupsdLogMessage(CUPSD_LOG_ERROR
,
1040 "Ran out of memory for job file types!");
1044 job
->compressions
= compressions
;
1045 job
->filetypes
= filetypes
;
1046 job
->num_files
= fileid
;
1049 job
->filetypes
[fileid
- 1] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
1050 job
->compressions
+ fileid
- 1);
1052 if (!job
->filetypes
[fileid
- 1])
1053 job
->filetypes
[fileid
- 1] = mimeType(MimeDatabase
, "application",
1058 job
->access_time
= time(NULL
);
1063 * 'cupsdMoveJob()' - Move the specified job to a different destination.
1067 cupsdMoveJob(cupsd_job_t
*job
, /* I - Job */
1068 const char *dest
) /* I - Destination */
1070 ipp_attribute_t
*attr
; /* job-printer-uri attribute */
1071 cupsd_printer_t
*p
; /* Destination printer or class */
1075 * Find the printer...
1078 if ((p
= cupsdFindDest(dest
)) == NULL
)
1082 * Don't move completed jobs...
1085 if (job
->state_value
>= IPP_JOB_PROCESSING
)
1089 * Change the destination information...
1094 cupsdSetString(&job
->dest
, dest
);
1095 job
->dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_REMOTE
|
1096 CUPS_PRINTER_IMPLICIT
);
1098 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1099 IPP_TAG_URI
)) != NULL
)
1100 cupsdSetString(&(attr
->values
[0].string
.text
), p
->uri
);
1107 * 'cupsdReleaseJob()' - Release the specified job.
1111 cupsdReleaseJob(cupsd_job_t
*job
) /* I - Job */
1113 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdReleaseJob: id = %d", job
->id
);
1115 if (job
->state_value
== IPP_JOB_HELD
)
1117 DEBUG_puts("cupsdReleaseJob: setting state to pending...");
1119 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1120 job
->state_value
= IPP_JOB_PENDING
;
1128 * 'cupsdRestartJob()' - Restart the specified job.
1132 cupsdRestartJob(cupsd_job_t
*job
) /* I - Job */
1134 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdRestartJob: id = %d", job
->id
);
1136 if (job
->state_value
== IPP_JOB_STOPPED
|| job
->num_files
)
1141 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1142 job
->state_value
= IPP_JOB_PENDING
;
1150 * 'cupsdSaveAllJobs()' - Save a summary of all jobs to disk.
1154 cupsdSaveAllJobs(void)
1156 int i
; /* Looping var */
1157 cups_file_t
*fp
; /* Job cache file */
1158 char temp
[1024]; /* Temporary string */
1159 cupsd_job_t
*job
; /* Current job */
1160 time_t curtime
; /* Current time */
1161 struct tm
*curdate
; /* Current date */
1164 snprintf(temp
, sizeof(temp
), "%s/job.cache", CacheDir
);
1165 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
1167 cupsdLogMessage(CUPSD_LOG_ERROR
,
1168 "Unable to create job cache file \"%s\" - %s",
1169 temp
, strerror(errno
));
1173 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving job cache file \"%s\"...", temp
);
1176 * Restrict access to the file...
1179 fchown(cupsFileNumber(fp
), getuid(), Group
);
1180 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
1183 * Write a small header to the file...
1186 curtime
= time(NULL
);
1187 curdate
= localtime(&curtime
);
1188 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
1190 cupsFilePuts(fp
, "# Job cache file for " CUPS_SVERSION
"\n");
1191 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
1192 cupsFilePrintf(fp
, "NextJobId %d\n", NextJobId
);
1195 * Write each job known to the system...
1198 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1200 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1202 cupsFilePrintf(fp
, "<Job %d>\n", job
->id
);
1203 cupsFilePrintf(fp
, "State %d\n", job
->state_value
);
1204 cupsFilePrintf(fp
, "Priority %d\n", job
->priority
);
1205 cupsFilePrintf(fp
, "Username %s\n", job
->username
);
1206 cupsFilePrintf(fp
, "Destination %s\n", job
->dest
);
1207 cupsFilePrintf(fp
, "DestType %d\n", job
->dtype
);
1208 cupsFilePrintf(fp
, "NumFiles %d\n", job
->num_files
);
1209 for (i
= 0; i
< job
->num_files
; i
++)
1210 cupsFilePrintf(fp
, "File %d %s/%s %d\n", i
+ 1, job
->filetypes
[i
]->super
,
1211 job
->filetypes
[i
]->type
, job
->compressions
[i
]);
1212 cupsFilePuts(fp
, "</Job>\n");
1220 * 'cupsdSaveJob()' - Save a job to disk.
1224 cupsdSaveJob(cupsd_job_t
*job
) /* I - Job */
1226 char filename
[1024]; /* Job control filename */
1227 cups_file_t
*fp
; /* Job file */
1230 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSaveJob(job=%p(%d)): job->attrs=%p",
1231 job
, job
->id
, job
->attrs
);
1233 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
, job
->id
);
1235 if ((fp
= cupsFileOpen(filename
, "w")) == NULL
)
1237 cupsdLogMessage(CUPSD_LOG_ERROR
,
1238 "Unable to create job control file \"%s\" - %s.",
1239 filename
, strerror(errno
));
1243 fchmod(cupsFileNumber(fp
), 0600);
1244 fchown(cupsFileNumber(fp
), RunUser
, Group
);
1246 job
->attrs
->state
= IPP_IDLE
;
1248 if (ippWriteIO(fp
, (ipp_iocb_t
)cupsFileWrite
, 1, NULL
,
1249 job
->attrs
) != IPP_DATA
)
1250 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to write job control file \"%s\"!",
1258 * 'cupsdSetJobHoldUntil()' - Set the hold time for a job...
1262 cupsdSetJobHoldUntil(cupsd_job_t
*job
, /* I - Job */
1263 const char *when
) /* I - When to resume */
1265 time_t curtime
; /* Current time */
1266 struct tm
*curdate
; /* Current date */
1267 int hour
; /* Hold hour */
1268 int minute
; /* Hold minute */
1269 int second
; /* Hold second */
1272 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetJobHoldUntil(%d, \"%s\")",
1277 if (!strcmp(when
, "indefinite") || !strcmp(when
, "authenticated"))
1280 * Hold indefinitely...
1283 job
->hold_until
= 0;
1285 else if (!strcmp(when
, "day-time"))
1288 * Hold to 6am the next morning unless local time is < 6pm.
1291 curtime
= time(NULL
);
1292 curdate
= localtime(&curtime
);
1294 if (curdate
->tm_hour
< 18)
1295 job
->hold_until
= curtime
;
1297 job
->hold_until
= curtime
+
1298 ((29 - curdate
->tm_hour
) * 60 + 59 -
1299 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1301 else if (!strcmp(when
, "evening") || strcmp(when
, "night"))
1304 * Hold to 6pm unless local time is > 6pm or < 6am.
1307 curtime
= time(NULL
);
1308 curdate
= localtime(&curtime
);
1310 if (curdate
->tm_hour
< 6 || curdate
->tm_hour
>= 18)
1311 job
->hold_until
= curtime
;
1313 job
->hold_until
= curtime
+
1314 ((17 - curdate
->tm_hour
) * 60 + 59 -
1315 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1317 else if (!strcmp(when
, "second-shift"))
1320 * Hold to 4pm unless local time is > 4pm.
1323 curtime
= time(NULL
);
1324 curdate
= localtime(&curtime
);
1326 if (curdate
->tm_hour
>= 16)
1327 job
->hold_until
= curtime
;
1329 job
->hold_until
= curtime
+
1330 ((15 - curdate
->tm_hour
) * 60 + 59 -
1331 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1333 else if (!strcmp(when
, "third-shift"))
1336 * Hold to 12am unless local time is < 8am.
1339 curtime
= time(NULL
);
1340 curdate
= localtime(&curtime
);
1342 if (curdate
->tm_hour
< 8)
1343 job
->hold_until
= curtime
;
1345 job
->hold_until
= curtime
+
1346 ((23 - curdate
->tm_hour
) * 60 + 59 -
1347 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1349 else if (!strcmp(when
, "weekend"))
1352 * Hold to weekend unless we are in the weekend.
1355 curtime
= time(NULL
);
1356 curdate
= localtime(&curtime
);
1358 if (curdate
->tm_wday
|| curdate
->tm_wday
== 6)
1359 job
->hold_until
= curtime
;
1361 job
->hold_until
= curtime
+
1362 (((5 - curdate
->tm_wday
) * 24 +
1363 (17 - curdate
->tm_hour
)) * 60 + 59 -
1364 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1366 else if (sscanf(when
, "%d:%d:%d", &hour
, &minute
, &second
) >= 2)
1369 * Hold to specified GMT time (HH:MM or HH:MM:SS)...
1372 curtime
= time(NULL
);
1373 curdate
= gmtime(&curtime
);
1375 job
->hold_until
= curtime
+
1376 ((hour
- curdate
->tm_hour
) * 60 + minute
-
1377 curdate
->tm_min
) * 60 + second
- curdate
->tm_sec
;
1380 * Hold until next day as needed...
1383 if (job
->hold_until
< curtime
)
1384 job
->hold_until
+= 24 * 60 * 60 * 60;
1387 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetJobHoldUntil: hold_until = %d",
1388 (int)job
->hold_until
);
1393 * 'cupsdSetJobPriority()' - Set the priority of a job, moving it up/down in
1394 * the list as needed.
1398 cupsdSetJobPriority(
1399 cupsd_job_t
*job
, /* I - Job ID */
1400 int priority
) /* I - New priority (0 to 100) */
1402 ipp_attribute_t
*attr
; /* Job attribute */
1406 * Don't change completed jobs...
1409 if (job
->state_value
>= IPP_JOB_PROCESSING
)
1413 * Set the new priority and re-add the job into the active list...
1416 cupsArrayRemove(ActiveJobs
, job
);
1418 job
->priority
= priority
;
1420 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
1421 IPP_TAG_INTEGER
)) != NULL
)
1422 attr
->values
[0].integer
= priority
;
1424 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-priority",
1427 cupsArrayAdd(ActiveJobs
, job
);
1434 * 'cupsdStartJob()' - Start a print job.
1438 cupsdStartJob(cupsd_job_t
*job
, /* I - Job ID */
1439 cupsd_printer_t
*printer
) /* I - Printer to print job */
1441 int i
; /* Looping var */
1442 int slot
; /* Pipe slot */
1443 cups_array_t
*filters
; /* Filters for job */
1444 mime_filter_t
*filter
, /* Current filter */
1445 port_monitor
; /* Port monitor filter */
1446 char method
[255], /* Method for output */
1447 *optptr
, /* Pointer to options */
1448 *valptr
; /* Pointer in value string */
1449 ipp_attribute_t
*attr
; /* Current attribute */
1450 struct stat backinfo
; /* Backend file information */
1451 int backroot
; /* Run backend as root? */
1452 int pid
; /* Process ID of new filter process */
1453 int banner_page
; /* 1 if banner page, 0 otherwise */
1454 int statusfds
[2], /* Pipes used with the scheduler */
1455 filterfds
[2][2];/* Pipes used between filters */
1456 int envc
; /* Number of environment variables */
1457 char **argv
, /* Filter command-line arguments */
1458 sani_uri
[1024], /* Sanitized DEVICE_URI env var */
1459 filename
[1024], /* Job filename */
1460 command
[1024], /* Full path to command */
1461 jobid
[255], /* Job ID string */
1462 title
[IPP_MAX_NAME
],
1463 /* Job title string */
1464 copies
[255], /* # copies string */
1465 *envp
[MAX_ENV
+ 11],
1466 /* Environment variables */
1467 charset
[255], /* CHARSET env variable */
1468 class_name
[255],/* CLASS env variable */
1469 classification
[1024],
1470 /* CLASSIFICATION env variable */
1472 /* CONTENT_TYPE env variable */
1474 /* DEVICE_URI env variable */
1475 final_content_type
[1024],
1476 /* FINAL_CONTENT_TYPE env variable */
1477 lang
[255], /* LANG env variable */
1478 ppd
[1024], /* PPD env variable */
1480 /* PRINTER env variable */
1482 /* RIP_MAX_CACHE env variable */
1483 int remote_job
; /* Remote print job? */
1484 static char *options
= NULL
;/* Full list of options */
1485 static int optlength
= 0; /* Length of option buffer */
1488 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdStartJob: id = %d, file = %d/%d",
1489 job
->id
, job
->current_file
, job
->num_files
);
1491 if (job
->num_files
== 0)
1493 cupsdLogMessage(CUPSD_LOG_ERROR
, "Job ID %d has no files! Cancelling it!",
1496 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
1497 "Job canceled because it has no files.");
1499 cupsdCancelJob(job
, 0);
1504 * Figure out what filters are required to convert from
1505 * the source to the destination type...
1514 * Remote jobs and raw queues go directly to the printer without
1518 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1519 "[Job %d] Sending job to queue tagged as raw...", job
->id
);
1526 * Local jobs get filtered...
1529 filters
= mimeFilter(MimeDatabase
, job
->filetypes
[job
->current_file
],
1530 printer
->filetype
, &(job
->cost
));
1534 cupsdLogMessage(CUPSD_LOG_ERROR
,
1535 "Unable to convert file %d to printable format for "
1537 job
->current_file
, job
->id
);
1538 cupsdLogMessage(CUPSD_LOG_INFO
,
1539 "Hint: Do you have ESP Ghostscript installed?");
1541 if (LogLevel
< CUPSD_LOG_DEBUG
)
1542 cupsdLogMessage(CUPSD_LOG_INFO
,
1543 "Hint: Try setting the LogLevel to \"debug\".");
1545 job
->current_file
++;
1547 if (job
->current_file
== job
->num_files
)
1549 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
1550 "Job canceled because it has no files that can be "
1553 cupsdCancelJob(job
, 0);
1560 * Remove NULL ("-") filters...
1563 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
1565 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
1566 if (!strcmp(filter
->filter
, "-"))
1567 cupsArrayRemove(filters
, filter
);
1569 if (cupsArrayCount(filters
) == 0)
1571 cupsArrayDelete(filters
);
1577 * See if the filter cost is too high...
1580 if ((FilterLevel
+ job
->cost
) > FilterLimit
&& FilterLevel
> 0 &&
1584 * Don't print this job quite yet...
1587 cupsArrayDelete(filters
);
1589 cupsdLogMessage(CUPSD_LOG_INFO
,
1590 "Holding job %d because filter limit has been reached.",
1592 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1593 "cupsdStartJob: id=%d, file=%d, cost=%d, level=%d, "
1595 job
->id
, job
->current_file
, job
->cost
, FilterLevel
,
1600 FilterLevel
+= job
->cost
;
1603 * Determine if we are printing to a remote printer...
1606 remote_job
= printer
->raw
&& job
->num_files
> 1 &&
1607 !strncmp(printer
->device_uri
, "ipp://", 6);
1610 * Add decompression filters, if any...
1613 if (!remote_job
&& job
->compressions
[job
->current_file
])
1616 * Add gziptoany filter to the front of the list...
1619 if (!cupsArrayInsert(filters
, &gziptoany_filter
))
1621 cupsdLogMessage(CUPSD_LOG_ERROR
,
1622 "Unable to add decompression filter - %s",
1625 cupsArrayDelete(filters
);
1627 job
->current_file
++;
1629 if (job
->current_file
== job
->num_files
)
1631 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
1632 "Job canceled because the print file could not be "
1635 cupsdCancelJob(job
, 0);
1643 * Add port monitor, if any...
1646 if (printer
->port_monitor
)
1649 * Add port monitor to the end of the list...
1652 if (!cupsArrayAdd(filters
, &port_monitor
))
1654 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to add port monitor - %s",
1657 cupsArrayDelete(filters
);
1659 job
->current_file
++;
1661 if (job
->current_file
== job
->num_files
)
1663 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
1664 "Job canceled because the port monitor could not be "
1667 cupsdCancelJob(job
, 0);
1673 snprintf(port_monitor
.filter
, sizeof(port_monitor
.filter
),
1674 "%s/monitor/%s", ServerBin
, printer
->port_monitor
);
1678 * Update the printer and job state to "processing"...
1681 job
->state
->values
[0].integer
= IPP_JOB_PROCESSING
;
1682 job
->state_value
= IPP_JOB_PROCESSING
;
1684 job
->printer
= printer
;
1686 cupsdSetPrinterState(printer
, IPP_PRINTER_PROCESSING
, 0);
1688 if (job
->current_file
== 0)
1690 set_time(job
, "time-at-processing");
1691 cupsdOpenPipe(job
->back_pipes
);
1695 * Determine if we are printing a banner page or not...
1698 if (job
->job_sheets
== NULL
)
1700 cupsdLogMessage(CUPSD_LOG_DEBUG
, "No job-sheets attribute.");
1701 if ((job
->job_sheets
=
1702 ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_ZERO
)) != NULL
)
1703 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1704 "... but someone added one without setting job_sheets!");
1706 else if (job
->job_sheets
->num_values
== 1)
1707 cupsdLogMessage(CUPSD_LOG_DEBUG
, "job-sheets=%s",
1708 job
->job_sheets
->values
[0].string
.text
);
1710 cupsdLogMessage(CUPSD_LOG_DEBUG
, "job-sheets=%s,%s",
1711 job
->job_sheets
->values
[0].string
.text
,
1712 job
->job_sheets
->values
[1].string
.text
);
1714 if (printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
))
1716 else if (job
->job_sheets
== NULL
)
1718 else if (strcasecmp(job
->job_sheets
->values
[0].string
.text
, "none") != 0 &&
1719 job
->current_file
== 0)
1721 else if (job
->job_sheets
->num_values
> 1 &&
1722 strcasecmp(job
->job_sheets
->values
[1].string
.text
, "none") != 0 &&
1723 job
->current_file
== (job
->num_files
- 1))
1728 cupsdLogMessage(CUPSD_LOG_DEBUG
, "banner_page = %d", banner_page
);
1731 * Building the options string is harder than it needs to be, but
1732 * for the moment we need to pass strings for command-line args and
1733 * not IPP attribute pointers... :)
1735 * First allocate/reallocate the option buffer as needed...
1738 i
= ipp_length(job
->attrs
);
1745 optptr
= realloc(options
, i
);
1749 cupsdLogMessage(CUPSD_LOG_CRIT
,
1750 "Unable to allocate %d bytes for option buffer for "
1751 "job %d!", i
, job
->id
);
1753 cupsArrayDelete(filters
);
1755 FilterLevel
-= job
->cost
;
1757 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
1758 "Job canceled because the server ran out of memory.");
1760 cupsdCancelJob(job
, 0);
1769 * Now loop through the attributes and convert them to the textual
1770 * representation used by the filters...
1776 snprintf(title
, sizeof(title
), "%s-%d", printer
->name
, job
->id
);
1777 strcpy(copies
, "1");
1779 for (attr
= job
->attrs
->attrs
; attr
!= NULL
; attr
= attr
->next
)
1781 if (!strcmp(attr
->name
, "copies") &&
1782 attr
->value_tag
== IPP_TAG_INTEGER
)
1785 * Don't use the # copies attribute if we are printing the job sheets...
1789 sprintf(copies
, "%d", attr
->values
[0].integer
);
1791 else if (!strcmp(attr
->name
, "job-name") &&
1792 (attr
->value_tag
== IPP_TAG_NAME
||
1793 attr
->value_tag
== IPP_TAG_NAMELANG
))
1794 strlcpy(title
, attr
->values
[0].string
.text
, sizeof(title
));
1795 else if (attr
->group_tag
== IPP_TAG_JOB
)
1798 * Filter out other unwanted attributes...
1801 if (attr
->value_tag
== IPP_TAG_MIMETYPE
||
1802 attr
->value_tag
== IPP_TAG_NAMELANG
||
1803 attr
->value_tag
== IPP_TAG_TEXTLANG
||
1804 (attr
->value_tag
== IPP_TAG_URI
&& strcmp(attr
->name
, "job-uuid")) ||
1805 attr
->value_tag
== IPP_TAG_URISCHEME
||
1806 attr
->value_tag
== IPP_TAG_BEGIN_COLLECTION
) /* Not yet supported */
1809 if (!strncmp(attr
->name
, "time-", 5))
1812 if (!strncmp(attr
->name
, "job-", 4) && strcmp(attr
->name
, "job-uuid") &&
1813 !(printer
->type
& CUPS_PRINTER_REMOTE
))
1816 if (!strncmp(attr
->name
, "job-", 4) &&
1817 strcmp(attr
->name
, "job-uuid") &&
1818 strcmp(attr
->name
, "job-billing") &&
1819 strcmp(attr
->name
, "job-sheets") &&
1820 strcmp(attr
->name
, "job-hold-until") &&
1821 strcmp(attr
->name
, "job-priority"))
1824 if ((!strcmp(attr
->name
, "page-label") ||
1825 !strcmp(attr
->name
, "page-border") ||
1826 !strncmp(attr
->name
, "number-up", 9) ||
1827 !strcmp(attr
->name
, "page-set") ||
1828 !strcasecmp(attr
->name
, "AP_FIRSTPAGE_InputSlot") ||
1829 !strcasecmp(attr
->name
, "AP_FIRSTPAGE_ManualFeed")) &&
1834 * Otherwise add them to the list...
1837 if (optptr
> options
)
1838 strlcat(optptr
, " ", optlength
- (optptr
- options
));
1840 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
1842 strlcat(optptr
, attr
->name
, optlength
- (optptr
- options
));
1843 strlcat(optptr
, "=", optlength
- (optptr
- options
));
1846 for (i
= 0; i
< attr
->num_values
; i
++)
1849 strlcat(optptr
, ",", optlength
- (optptr
- options
));
1851 optptr
+= strlen(optptr
);
1853 switch (attr
->value_tag
)
1855 case IPP_TAG_INTEGER
:
1857 snprintf(optptr
, optlength
- (optptr
- options
),
1858 "%d", attr
->values
[i
].integer
);
1861 case IPP_TAG_BOOLEAN
:
1862 if (!attr
->values
[i
].boolean
)
1863 strlcat(optptr
, "no", optlength
- (optptr
- options
));
1865 case IPP_TAG_NOVALUE
:
1866 strlcat(optptr
, attr
->name
,
1867 optlength
- (optptr
- options
));
1870 case IPP_TAG_RANGE
:
1871 if (attr
->values
[i
].range
.lower
== attr
->values
[i
].range
.upper
)
1872 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
1873 "%d", attr
->values
[i
].range
.lower
);
1875 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
1876 "%d-%d", attr
->values
[i
].range
.lower
,
1877 attr
->values
[i
].range
.upper
);
1880 case IPP_TAG_RESOLUTION
:
1881 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
1882 "%dx%d%s", attr
->values
[i
].resolution
.xres
,
1883 attr
->values
[i
].resolution
.yres
,
1884 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
1888 case IPP_TAG_STRING
:
1891 case IPP_TAG_KEYWORD
:
1892 case IPP_TAG_CHARSET
:
1893 case IPP_TAG_LANGUAGE
:
1895 for (valptr
= attr
->values
[i
].string
.text
; *valptr
;)
1897 if (strchr(" \t\n\\\'\"", *valptr
))
1899 *optptr
++ = *valptr
++;
1906 break; /* anti-compiler-warning-code */
1910 optptr
+= strlen(optptr
);
1915 * Build the command-line arguments for the filters. Each filter
1916 * has 6 or 7 arguments:
1920 * argv[2] = username
1922 * argv[4] = # copies
1924 * argv[6] = filename (optional; normally stdin)
1926 * This allows legacy printer drivers that use the old System V
1927 * printing interface to be used by CUPS.
1929 * For remote jobs, we send all of the files in the argument list.
1933 argv
= calloc(7 + job
->num_files
, sizeof(char *));
1935 argv
= calloc(8, sizeof(char *));
1937 sprintf(jobid
, "%d", job
->id
);
1939 argv
[0] = printer
->name
;
1941 argv
[2] = job
->username
;
1948 for (i
= 0; i
< job
->num_files
; i
++)
1950 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
1952 argv
[6 + i
] = strdup(filename
);
1957 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
1958 job
->id
, job
->current_file
+ 1);
1962 for (i
= 0; argv
[i
]; i
++)
1963 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1964 "[Job %d] argv[%d]=\"%s\"", job
->id
, i
, argv
[i
]);
1967 * Create environment variable strings for the filters...
1970 attr
= ippFindAttribute(job
->attrs
, "attributes-natural-language",
1973 switch (strlen(attr
->values
[0].string
.text
))
1977 * This is an unknown or badly formatted language code; use
1978 * the POSIX locale...
1981 strcpy(lang
, "LANG=C");
1986 * Just the language code (ll)...
1989 snprintf(lang
, sizeof(lang
), "LANG=%s",
1990 attr
->values
[0].string
.text
);
1995 * Language and country code (ll-cc)...
1998 snprintf(lang
, sizeof(lang
), "LANG=%c%c_%c%c",
1999 attr
->values
[0].string
.text
[0],
2000 attr
->values
[0].string
.text
[1],
2001 toupper(attr
->values
[0].string
.text
[3] & 255),
2002 toupper(attr
->values
[0].string
.text
[4] & 255));
2006 attr
= ippFindAttribute(job
->attrs
, "document-format",
2009 (optptr
= strstr(attr
->values
[0].string
.text
, "charset=")) != NULL
)
2010 snprintf(charset
, sizeof(charset
), "CHARSET=%s", optptr
+ 8);
2013 attr
= ippFindAttribute(job
->attrs
, "attributes-charset",
2015 snprintf(charset
, sizeof(charset
), "CHARSET=%s",
2016 attr
->values
[0].string
.text
);
2019 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s/%s",
2020 job
->filetypes
[job
->current_file
]->super
,
2021 job
->filetypes
[job
->current_file
]->type
);
2022 snprintf(device_uri
, sizeof(device_uri
), "DEVICE_URI=%s",
2023 printer
->device_uri
);
2024 cupsdSanitizeURI(printer
->device_uri
, sani_uri
, sizeof(sani_uri
));
2025 snprintf(ppd
, sizeof(ppd
), "PPD=%s/ppd/%s.ppd", ServerRoot
, printer
->name
);
2026 snprintf(printer_name
, sizeof(printer_name
), "PRINTER=%s", printer
->name
);
2027 snprintf(rip_max_cache
, sizeof(rip_max_cache
), "RIP_MAX_CACHE=%s", RIPCache
);
2029 envc
= cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
2031 envp
[envc
++] = charset
;
2032 envp
[envc
++] = lang
;
2033 envp
[envc
++] = ppd
;
2034 envp
[envc
++] = rip_max_cache
;
2035 envp
[envc
++] = content_type
;
2036 envp
[envc
++] = device_uri
;
2037 envp
[envc
++] = printer_name
;
2039 if ((filter
= (mime_filter_t
*)cupsArrayLast(filters
)) != NULL
)
2041 snprintf(final_content_type
, sizeof(final_content_type
),
2042 "FINAL_CONTENT_TYPE=%s/%s",
2043 filter
->dst
->super
, filter
->dst
->type
);
2044 envp
[envc
++] = final_content_type
;
2047 if (Classification
&& !banner_page
)
2049 if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
2050 IPP_TAG_NAME
)) == NULL
)
2051 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
2053 else if (attr
->num_values
> 1 &&
2054 strcmp(attr
->values
[1].string
.text
, "none") != 0)
2055 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
2056 attr
->values
[1].string
.text
);
2058 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
2059 attr
->values
[0].string
.text
);
2061 envp
[envc
++] = classification
;
2064 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
2066 snprintf(class_name
, sizeof(class_name
), "CLASS=%s", job
->dest
);
2067 envp
[envc
++] = class_name
;
2072 for (i
= 0; i
< envc
; i
++)
2073 if (strncmp(envp
[i
], "DEVICE_URI=", 11))
2074 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] envp[%d]=\"%s\"",
2075 job
->id
, i
, envp
[i
]);
2077 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] envp[%d]=\"DEVICE_URI=%s\"",
2078 job
->id
, i
, sani_uri
);
2081 job
->current_file
= job
->num_files
;
2083 job
->current_file
++;
2086 * Now create processes for all of the filters...
2089 if (cupsdOpenPipe(statusfds
))
2091 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create job status pipes - %s.",
2093 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
2094 "Unable to create status pipes - %s.", strerror(errno
));
2096 cupsdAddPrinterHistory(printer
);
2098 cupsArrayDelete(filters
);
2100 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
2101 "Job canceled because the server could not create the job "
2106 for (i
= 0; i
< job
->num_files
; i
++)
2112 cupsdCancelJob(job
, 0);
2116 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdStartJob: statusfds = [ %d %d ]",
2117 statusfds
[0], statusfds
[1]);
2120 fcntl(statusfds
[0], F_SETFD
, FD_CLOEXEC
);
2121 fcntl(statusfds
[1], F_SETFD
, FD_CLOEXEC
);
2122 #endif /* FD_CLOEXEC */
2124 job
->status_buffer
= cupsdStatBufNew(statusfds
[0], "[Job %d]",
2127 memset(job
->filters
, 0, sizeof(job
->filters
));
2129 filterfds
[1][0] = open("/dev/null", O_RDONLY
);
2130 filterfds
[1][1] = -1;
2132 if (filterfds
[1][0] < 0)
2134 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"/dev/null\" - %s.",
2136 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
2137 "Unable to open \"/dev/null\" - %s.", strerror(errno
));
2139 cupsdAddPrinterHistory(printer
);
2141 cupsArrayDelete(filters
);
2143 cupsdClosePipe(statusfds
);
2147 for (i
= 0; i
< job
->num_files
; i
++)
2153 cupsdCancelJob(job
, 0);
2157 fcntl(filterfds
[1][0], F_SETFD
, fcntl(filterfds
[1][0], F_GETFD
) | FD_CLOEXEC
);
2159 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdStartJob: filterfds[%d] = [ %d %d ]",
2160 1, filterfds
[1][0], filterfds
[1][1]);
2162 for (i
= 0, slot
= 0, filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
2164 i
++, filter
= (mime_filter_t
*)cupsArrayNext(filters
))
2166 if (filter
->filter
[0] != '/')
2167 snprintf(command
, sizeof(command
), "%s/filter/%s", ServerBin
,
2170 strlcpy(command
, filter
->filter
, sizeof(command
));
2172 if (i
< (cupsArrayCount(filters
) - 1))
2174 if (cupsdOpenPipe(filterfds
[slot
]))
2176 cupsdLogMessage(CUPSD_LOG_ERROR
,
2177 "Unable to create job filter pipes - %s.",
2179 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
2180 "Unable to create filter pipes - %s.", strerror(errno
));
2181 cupsdAddPrinterHistory(printer
);
2183 cupsArrayDelete(filters
);
2185 cupsdClosePipe(statusfds
);
2186 cupsdClosePipe(filterfds
[!slot
]);
2188 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
2189 "Job canceled because the server could not create the "
2194 for (i
= 0; i
< job
->num_files
; i
++)
2200 cupsdCancelJob(job
, 0);
2206 if (job
->current_file
== 1)
2208 if (strncmp(printer
->device_uri
, "file:", 5) != 0)
2210 if (cupsdOpenPipe(job
->print_pipes
))
2212 cupsdLogMessage(CUPSD_LOG_ERROR
,
2213 "Unable to create job backend pipes - %s.",
2215 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
2216 "Unable to create backend pipes - %s.", strerror(errno
));
2217 cupsdAddPrinterHistory(printer
);
2219 cupsArrayDelete(filters
);
2221 cupsdClosePipe(statusfds
);
2222 cupsdClosePipe(filterfds
[!slot
]);
2224 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
2225 "Job canceled because the server could not create "
2226 "the backend pipes.");
2230 for (i
= 0; i
< job
->num_files
; i
++)
2236 cupsdCancelJob(job
, 0);
2242 job
->print_pipes
[0] = -1;
2243 if (!strncmp(printer
->device_uri
, "file:/dev/", 10) &&
2244 strcmp(printer
->device_uri
, "file:/dev/null"))
2245 job
->print_pipes
[1] = open(printer
->device_uri
+ 5,
2247 else if (!strncmp(printer
->device_uri
, "file:///dev/", 12) &&
2248 strcmp(printer
->device_uri
, "file:///dev/null"))
2249 job
->print_pipes
[1] = open(printer
->device_uri
+ 7,
2252 job
->print_pipes
[1] = open(printer
->device_uri
+ 5,
2253 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
2255 if (job
->print_pipes
[1] < 0)
2257 cupsdLogMessage(CUPSD_LOG_ERROR
,
2258 "Unable to open output file \"%s\" - %s.",
2259 printer
->device_uri
, strerror(errno
));
2260 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
2261 "Unable to open output file \"%s\" - %s.",
2262 printer
->device_uri
, strerror(errno
));
2264 cupsdAddPrinterHistory(printer
);
2266 cupsArrayDelete(filters
);
2268 cupsdClosePipe(statusfds
);
2269 cupsdClosePipe(filterfds
[!slot
]);
2271 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
2272 "Job canceled because the server could not open the "
2277 for (i
= 0; i
< job
->num_files
; i
++)
2283 cupsdCancelJob(job
, 0);
2287 fcntl(job
->print_pipes
[1], F_SETFD
,
2288 fcntl(job
->print_pipes
[1], F_GETFD
) | FD_CLOEXEC
);
2291 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2292 "cupsdStartJob: print_pipes = [ %d %d ]",
2293 job
->print_pipes
[0], job
->print_pipes
[1]);
2296 filterfds
[slot
][0] = job
->print_pipes
[0];
2297 filterfds
[slot
][1] = job
->print_pipes
[1];
2300 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdStartJob: filter=\"%s\"",
2302 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2303 "cupsdStartJob: filterfds[%d]=[ %d %d ]",
2304 slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
2306 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
2307 filterfds
[slot
][1], statusfds
[1],
2308 job
->back_pipes
[0], 0, job
->filters
+ i
);
2310 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2311 "cupsdStartJob: Closing filter pipes for slot %d "
2313 !slot
, filterfds
[!slot
][0], filterfds
[!slot
][1]);
2315 cupsdClosePipe(filterfds
[!slot
]);
2319 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to start filter \"%s\" - %s.",
2320 filter
->filter
, strerror(errno
));
2321 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
2322 "Unable to start filter \"%s\" - %s.",
2323 filter
->filter
, strerror(errno
));
2325 cupsdAddPrinterHistory(printer
);
2327 cupsArrayDelete(filters
);
2329 cupsdAddPrinterHistory(printer
);
2331 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
2332 "Job canceled because the server could not execute a "
2337 for (i
= 0; i
< job
->num_files
; i
++)
2343 cupsdCancelJob(job
, 0);
2347 cupsdLogMessage(CUPSD_LOG_INFO
, "Started filter %s (PID %d) for job %d.",
2348 command
, pid
, job
->id
);
2354 cupsArrayDelete(filters
);
2357 * Finally, pipe the final output into a backend process if needed...
2360 if (strncmp(printer
->device_uri
, "file:", 5) != 0)
2362 if (job
->current_file
== 1)
2364 sscanf(printer
->device_uri
, "%254[^:]", method
);
2365 snprintf(command
, sizeof(command
), "%s/backend/%s", ServerBin
, method
);
2368 * See if the backend needs to run as root...
2373 else if (lstat(command
, &backinfo
))
2376 backroot
= !(backinfo
.st_mode
& (S_IRWXG
| S_IRWXO
));
2380 filterfds
[slot
][0] = -1;
2381 filterfds
[slot
][1] = open("/dev/null", O_WRONLY
);
2383 if (filterfds
[slot
][1] < 0)
2385 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"/dev/null\" - %s.",
2387 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
2388 "Unable to open \"/dev/null\" - %s.", strerror(errno
));
2390 cupsdAddPrinterHistory(printer
);
2392 cupsdClosePipe(statusfds
);
2394 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
2395 "Job canceled because the server could not open a file.");
2399 for (i
= 0; i
< job
->num_files
; i
++)
2405 cupsdCancelJob(job
, 0);
2409 fcntl(filterfds
[slot
][1], F_SETFD
,
2410 fcntl(filterfds
[slot
][1], F_GETFD
) | FD_CLOEXEC
);
2412 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdStartJob: backend=\"%s\"",
2414 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2415 "cupsdStartJob: filterfds[%d] = [ %d %d ]",
2416 slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
2418 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
2419 filterfds
[slot
][1], statusfds
[1],
2420 job
->back_pipes
[1], backroot
,
2425 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to start backend \"%s\" - %s.",
2426 method
, strerror(errno
));
2427 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
2428 "Unable to start backend \"%s\" - %s.", method
,
2431 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2432 "cupsdStartJob: Closing print pipes [ %d %d ]...",
2433 job
->print_pipes
[0], job
->print_pipes
[1]);
2435 cupsdClosePipe(job
->print_pipes
);
2437 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2438 "cupsdStartJob: Closing back pipes [ %d %d ]...",
2439 job
->back_pipes
[0], job
->back_pipes
[1]);
2441 cupsdClosePipe(job
->back_pipes
);
2443 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
2444 "Job canceled because the server could not execute "
2449 for (i
= 0; i
< job
->num_files
; i
++)
2455 cupsdCancelJob(job
, 0);
2460 cupsdLogMessage(CUPSD_LOG_INFO
,
2461 "Started backend %s (PID %d) for job %d.",
2462 command
, pid
, job
->id
);
2466 if (job
->current_file
== job
->num_files
)
2468 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2469 "cupsdStartJob: Closing print pipes [ %d %d ]...",
2470 job
->print_pipes
[0], job
->print_pipes
[1]);
2472 cupsdClosePipe(job
->print_pipes
);
2474 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2475 "cupsdStartJob: Closing back pipes [ %d %d ]...",
2476 job
->back_pipes
[0], job
->back_pipes
[1]);
2478 cupsdClosePipe(job
->back_pipes
);
2483 filterfds
[slot
][0] = -1;
2484 filterfds
[slot
][1] = -1;
2486 if (job
->current_file
== job
->num_files
)
2488 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2489 "cupsdStartJob: Closing print pipes [ %d %d ]...",
2490 job
->print_pipes
[0], job
->print_pipes
[1]);
2492 cupsdClosePipe(job
->print_pipes
);
2498 for (i
= 0; i
< job
->num_files
; i
++)
2504 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2505 "cupsdStartJob: Closing filter pipes for slot %d "
2507 slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
2509 cupsdClosePipe(filterfds
[slot
]);
2511 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2512 "cupsdStartJob: Closing status output pipe %d...",
2515 close(statusfds
[1]);
2517 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2518 "cupsdStartJob: Adding fd %d to InputSet...",
2519 job
->status_buffer
->fd
);
2521 FD_SET(job
->status_buffer
->fd
, InputSet
);
2523 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
, "Job #%d started.",
2529 * 'cupsdStopAllJobs()' - Stop all print jobs.
2533 cupsdStopAllJobs(void)
2535 cupsd_job_t
*job
; /* Current job */
2538 DEBUG_puts("cupsdStopAllJobs()");
2540 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
2542 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
2543 if (job
->state_value
== IPP_JOB_PROCESSING
)
2545 cupsdStopJob(job
, 1);
2546 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
2547 job
->state_value
= IPP_JOB_PENDING
;
2553 * 'cupsdStopJob()' - Stop a print job.
2557 cupsdStopJob(cupsd_job_t
*job
, /* I - Job */
2558 int force
) /* I - 1 = Force all filters to stop */
2560 int i
; /* Looping var */
2563 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdStopJob: id = %d, force = %d",
2566 if (job
->state_value
!= IPP_JOB_PROCESSING
)
2569 FilterLevel
-= job
->cost
;
2571 if (job
->status
< 0 &&
2572 !(job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
)) &&
2573 !(job
->printer
->type
& CUPS_PRINTER_FAX
) &&
2574 !strcmp(job
->printer
->error_policy
, "stop-printer"))
2575 cupsdSetPrinterState(job
->printer
, IPP_PRINTER_STOPPED
, 1);
2576 else if (job
->printer
->state
!= IPP_PRINTER_STOPPED
)
2577 cupsdSetPrinterState(job
->printer
, IPP_PRINTER_IDLE
, 0);
2579 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdStopJob: printer state is %d",
2580 job
->printer
->state
);
2582 job
->state
->values
[0].integer
= IPP_JOB_STOPPED
;
2583 job
->state_value
= IPP_JOB_STOPPED
;
2584 job
->printer
->job
= NULL
;
2585 job
->printer
= NULL
;
2587 job
->current_file
--;
2589 for (i
= 0; job
->filters
[i
]; i
++)
2590 if (job
->filters
[i
] > 0)
2592 cupsdEndProcess(job
->filters
[i
], force
);
2593 job
->filters
[i
] = 0;
2596 if (job
->backend
> 0)
2598 cupsdEndProcess(job
->backend
, force
);
2602 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2603 "cupsdStopJob: Closing print pipes [ %d %d ]...",
2604 job
->print_pipes
[0], job
->print_pipes
[1]);
2606 cupsdClosePipe(job
->print_pipes
);
2608 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2609 "cupsdStopJob: Closing back pipes [ %d %d ]...",
2610 job
->back_pipes
[0], job
->back_pipes
[1]);
2612 cupsdClosePipe(job
->back_pipes
);
2614 if (job
->status_buffer
)
2617 * Close the pipe and clear the input bit.
2620 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2621 "cupsdStopJob: Removing fd %d from InputSet...",
2622 job
->status_buffer
->fd
);
2624 FD_CLR(job
->status_buffer
->fd
, InputSet
);
2626 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2627 "cupsdStopJob: Closing status input pipe %d...",
2628 job
->status_buffer
->fd
);
2630 cupsdStatBufDelete(job
->status_buffer
);
2632 job
->status_buffer
= NULL
;
2638 * 'cupsdUnloadCompletedJobs()' - Flush completed job history from memory.
2642 cupsdUnloadCompletedJobs(void)
2644 cupsd_job_t
*job
; /* Current job */
2645 time_t expire
; /* Expiration time */
2648 expire
= time(NULL
) - 60;
2650 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
2652 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
2653 if (job
->attrs
&& job
->state_value
>= IPP_JOB_STOPPED
&&
2654 job
->access_time
< expire
)
2655 cupsdUnloadJob(job
);
2660 * 'cupsdUnloadJob()' - Unload a job from memory.
2664 cupsdUnloadJob(cupsd_job_t
*job
) /* I - Job */
2669 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Unloading job %d...", job
->id
);
2671 ippDelete(job
->attrs
);
2676 job
->job_sheets
= NULL
;
2681 * 'cupsdUpdateJob()' - Read a status update from a job's filters.
2685 cupsdUpdateJob(cupsd_job_t
*job
) /* I - Job to check */
2687 int i
; /* Looping var */
2688 int copies
; /* Number of copies printed */
2689 char message
[1024], /* Message text */
2690 *ptr
; /* Pointer update... */
2691 int loglevel
; /* Log level for message */
2694 while ((ptr
= cupsdStatBufUpdate(job
->status_buffer
, &loglevel
,
2695 message
, sizeof(message
))) != NULL
)
2698 * Process page and printer state messages as needed...
2701 if (loglevel
== CUPSD_LOG_PAGE
)
2704 * Page message; send the message to the page_log file and update the
2705 * job sheet count...
2708 if (job
->sheets
!= NULL
)
2710 if (!strncasecmp(message
, "total ", 6))
2713 * Got a total count of pages from a backend or filter...
2716 copies
= atoi(message
+ 6);
2717 copies
-= job
->sheets
->values
[0].integer
; /* Just track the delta */
2719 else if (!sscanf(message
, "%*d%d", &copies
))
2722 job
->sheets
->values
[0].integer
+= copies
;
2724 if (job
->printer
->page_limit
)
2725 cupsdUpdateQuota(job
->printer
, job
->username
, copies
, 0);
2728 cupsdLogPage(job
, message
);
2730 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
2731 "Printed %d page(s).", job
->sheets
->values
[0].integer
);
2733 else if (loglevel
== CUPSD_LOG_STATE
)
2735 cupsdSetPrinterReasons(job
->printer
, message
);
2736 cupsdAddPrinterHistory(job
->printer
);
2738 else if (loglevel
== CUPSD_LOG_ATTR
)
2741 * Set attribute(s)...
2747 else if (!strncmp(message
, "recoverable:", 12))
2749 cupsdSetPrinterReasons(job
->printer
,
2750 "+com.apple.print.recoverable-warning");
2753 while (isspace(*ptr
& 255))
2756 cupsdSetString(&job
->printer
->recoverable
, ptr
);
2757 cupsdAddPrinterHistory(job
->printer
);
2759 else if (!strncmp(message
, "recovered:", 10))
2761 cupsdSetPrinterReasons(job
->printer
,
2762 "-com.apple.print.recoverable-warning");
2765 while (isspace(*ptr
& 255))
2768 cupsdSetString(&job
->printer
->recoverable
, ptr
);
2769 cupsdAddPrinterHistory(job
->printer
);
2771 #endif /* __APPLE__ */
2772 else if (loglevel
<= CUPSD_LOG_INFO
)
2775 * Some message to show in the printer-state-message attribute...
2778 strlcpy(job
->printer
->state_message
, message
,
2779 sizeof(job
->printer
->state_message
));
2780 cupsdAddPrinterHistory(job
->printer
);
2783 if (!strchr(job
->status_buffer
->buffer
, '\n'))
2790 * See if all of the filters and the backend have returned their
2794 for (i
= 0; job
->filters
[i
] < 0; i
++);
2796 if (job
->filters
[i
])
2799 if (job
->current_file
>= job
->num_files
&& job
->backend
> 0)
2803 * Handle the end of job stuff...
2806 cupsdFinishJob(job
);
2812 * 'compare_active_jobs()' - Compare the job IDs and priorities of two jobs.
2815 static int /* O - Difference */
2816 compare_active_jobs(void *first
, /* I - First job */
2817 void *second
, /* I - Second job */
2818 void *data
) /* I - App data (not used) */
2820 int diff
; /* Difference */
2823 if ((diff
= ((cupsd_job_t
*)first
)->priority
-
2824 ((cupsd_job_t
*)second
)->priority
) != 0)
2827 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
2832 * 'compare_jobs()' - Compare the job IDs of two jobs.
2835 static int /* O - Difference */
2836 compare_jobs(void *first
, /* I - First job */
2837 void *second
, /* I - Second job */
2838 void *data
) /* I - App data (not used) */
2840 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
2845 * 'free_job()' - Free all memory used by a job.
2849 free_job(cupsd_job_t
*job
) /* I - Job */
2851 cupsdClearString(&job
->username
);
2852 cupsdClearString(&job
->dest
);
2854 if (job
->num_files
> 0)
2856 free(job
->compressions
);
2857 free(job
->filetypes
);
2860 ippDelete(job
->attrs
);
2867 * 'ipp_length()' - Compute the size of the buffer needed to hold
2868 * the textual IPP attributes.
2871 int /* O - Size of attribute buffer */
2872 ipp_length(ipp_t
*ipp
) /* I - IPP request */
2874 int bytes
; /* Number of bytes */
2875 int i
; /* Looping var */
2876 ipp_attribute_t
*attr
; /* Current attribute */
2880 * Loop through all attributes...
2885 for (attr
= ipp
->attrs
; attr
!= NULL
; attr
= attr
->next
)
2888 * Skip attributes that won't be sent to filters...
2891 if (attr
->value_tag
== IPP_TAG_MIMETYPE
||
2892 attr
->value_tag
== IPP_TAG_NAMELANG
||
2893 attr
->value_tag
== IPP_TAG_TEXTLANG
||
2894 attr
->value_tag
== IPP_TAG_URI
||
2895 attr
->value_tag
== IPP_TAG_URISCHEME
)
2898 if (strncmp(attr
->name
, "time-", 5) == 0)
2902 * Add space for a leading space and commas between each value.
2903 * For the first attribute, the leading space isn't used, so the
2904 * extra byte can be used as the nul terminator...
2907 bytes
++; /* " " separator */
2908 bytes
+= attr
->num_values
; /* "," separators */
2911 * Boolean attributes appear as "foo,nofoo,foo,nofoo", while
2912 * other attributes appear as "foo=value1,value2,...,valueN".
2915 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
2916 bytes
+= strlen(attr
->name
);
2918 bytes
+= attr
->num_values
* strlen(attr
->name
);
2921 * Now add the size required for each value in the attribute...
2924 switch (attr
->value_tag
)
2926 case IPP_TAG_INTEGER
:
2929 * Minimum value of a signed integer is -2147483647, or 11 digits.
2932 bytes
+= attr
->num_values
* 11;
2935 case IPP_TAG_BOOLEAN
:
2937 * Add two bytes for each false ("no") value...
2940 for (i
= 0; i
< attr
->num_values
; i
++)
2941 if (!attr
->values
[i
].boolean
)
2945 case IPP_TAG_RANGE
:
2947 * A range is two signed integers separated by a hyphen, or
2948 * 23 characters max.
2951 bytes
+= attr
->num_values
* 23;
2954 case IPP_TAG_RESOLUTION
:
2956 * A resolution is two signed integers separated by an "x" and
2957 * suffixed by the units, or 26 characters max.
2960 bytes
+= attr
->num_values
* 26;
2963 case IPP_TAG_STRING
:
2966 case IPP_TAG_KEYWORD
:
2967 case IPP_TAG_CHARSET
:
2968 case IPP_TAG_LANGUAGE
:
2971 * Strings can contain characters that need quoting. We need
2972 * at least 2 * len + 2 characters to cover the quotes and
2973 * any backslashes in the string.
2976 for (i
= 0; i
< attr
->num_values
; i
++)
2977 bytes
+= 2 * strlen(attr
->values
[i
].string
.text
) + 2;
2981 break; /* anti-compiler-warning-code */
2990 * 'load_job_cache()' - Load jobs from the job.cache file.
2994 load_job_cache(const char *filename
) /* I - job.cache filename */
2996 cups_file_t
*fp
; /* job.cache file */
2997 char line
[1024], /* Line buffer */
2998 *value
; /* Value on line */
2999 int linenum
; /* Line number in file */
3000 cupsd_job_t
*job
; /* Current job */
3001 int jobid
; /* Job ID */
3002 char jobfile
[1024]; /* Job filename */
3006 * Open the job.cache file...
3009 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
3011 if (errno
!= ENOENT
)
3012 cupsdLogMessage(CUPSD_LOG_ERROR
,
3013 "Unable to open job cache file \"%s\": %s",
3014 filename
, strerror(errno
));
3016 load_request_root();
3022 * Read entries from the job cache file and create jobs as needed.
3025 cupsdLogMessage(CUPSD_LOG_INFO
, "Loading job cache file \"%s\"...",
3031 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
3033 if (!strcasecmp(line
, "NextJobId"))
3036 NextJobId
= atoi(value
);
3038 else if (!strcasecmp(line
, "<Job"))
3042 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing </Job> directive on line %d!",
3049 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing job ID on line %d!", linenum
);
3053 jobid
= atoi(value
);
3057 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad job ID %d on line %d!", jobid
,
3062 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, jobid
);
3063 if (access(jobfile
, 0))
3065 cupsdLogMessage(CUPSD_LOG_ERROR
, "Job %d files have gone away!", jobid
);
3069 job
= calloc(1, sizeof(cupsd_job_t
));
3072 cupsdLogMessage(CUPSD_LOG_EMERG
,
3073 "Unable to allocate memory for job %d!", jobid
);
3078 job
->back_pipes
[0] = -1;
3079 job
->back_pipes
[1] = -1;
3080 job
->print_pipes
[0] = -1;
3081 job
->print_pipes
[1] = -1;
3083 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Loading job %d from cache...", job
->id
);
3087 cupsdLogMessage(CUPSD_LOG_ERROR
,
3088 "Missing <Job #> directive on line %d!", linenum
);
3091 else if (!strcasecmp(line
, "</Job>"))
3093 cupsArrayAdd(Jobs
, job
);
3095 if (job
->state_value
< IPP_JOB_STOPPED
)
3097 cupsArrayAdd(ActiveJobs
, job
);
3105 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing value on line %d!", linenum
);
3108 else if (!strcasecmp(line
, "State"))
3110 job
->state_value
= atoi(value
);
3112 if (job
->state_value
< IPP_JOB_PENDING
)
3113 job
->state_value
= IPP_JOB_PENDING
;
3114 else if (job
->state_value
> IPP_JOB_COMPLETED
)
3115 job
->state_value
= IPP_JOB_COMPLETED
;
3117 else if (!strcasecmp(line
, "Priority"))
3119 job
->priority
= atoi(value
);
3121 else if (!strcasecmp(line
, "Username"))
3123 cupsdSetString(&job
->username
, value
);
3125 else if (!strcasecmp(line
, "Destination"))
3127 cupsdSetString(&job
->dest
, value
);
3129 else if (!strcasecmp(line
, "DestType"))
3131 job
->dtype
= (cups_ptype_t
)atoi(value
);
3133 else if (!strcasecmp(line
, "NumFiles"))
3135 job
->num_files
= atoi(value
);
3137 if (job
->num_files
< 0)
3139 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad NumFiles value %d on line %d!",
3140 job
->num_files
, linenum
);
3145 if (job
->num_files
> 0)
3147 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-001", RequestRoot
,
3149 if (access(jobfile
, 0))
3151 cupsdLogMessage(CUPSD_LOG_INFO
,
3152 "Data files for job %d have gone away!", job
->id
);
3157 job
->filetypes
= calloc(job
->num_files
, sizeof(mime_type_t
*));
3158 job
->compressions
= calloc(job
->num_files
, sizeof(int));
3160 if (!job
->filetypes
|| !job
->compressions
)
3162 cupsdLogMessage(CUPSD_LOG_EMERG
,
3163 "Unable to allocate memory for %d files!",
3169 else if (!strcasecmp(line
, "File"))
3171 int number
, /* File number */
3172 compression
; /* Compression value */
3173 char super
[MIME_MAX_SUPER
], /* MIME super type */
3174 type
[MIME_MAX_TYPE
]; /* MIME type */
3177 if (sscanf(value
, "%d%*[ \t]%15[^/]/%255s%d", &number
, super
, type
,
3180 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File on line %d!", linenum
);
3184 if (number
< 1 || number
> job
->num_files
)
3186 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File number %d on line %d!",
3193 job
->compressions
[number
] = compression
;
3194 job
->filetypes
[number
] = mimeType(MimeDatabase
, super
, type
);
3196 if (!job
->filetypes
[number
])
3199 * If the original MIME type is unknown, auto-type it!
3202 cupsdLogMessage(CUPSD_LOG_ERROR
,
3203 "Unknown MIME type %s/%s for file %d of job %d!",
3204 super
, type
, number
+ 1, job
->id
);
3206 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
3207 job
->id
, number
+ 1);
3208 job
->filetypes
[number
] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
3209 job
->compressions
+ number
);
3212 * If that didn't work, assume it is raw...
3215 if (!job
->filetypes
[number
])
3216 job
->filetypes
[number
] = mimeType(MimeDatabase
, "application",
3221 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unknown %s directive on line %d!",
3230 * 'load_next_job_id()' - Load the NextJobId value from the job.cache file.
3234 load_next_job_id(const char *filename
) /* I - job.cache filename */
3236 cups_file_t
*fp
; /* job.cache file */
3237 char line
[1024], /* Line buffer */
3238 *value
; /* Value on line */
3239 int linenum
; /* Line number in file */
3243 * Read the NextJobId directive from the job.cache file and use
3244 * the value (if any).
3247 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
3249 if (errno
!= ENOENT
)
3250 cupsdLogMessage(CUPSD_LOG_ERROR
,
3251 "Unable to open job cache file \"%s\": %s",
3252 filename
, strerror(errno
));
3257 cupsdLogMessage(CUPSD_LOG_INFO
,
3258 "Loading NextJobId from job cache file \"%s\"...", filename
);
3262 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
3264 if (!strcasecmp(line
, "NextJobId"))
3267 NextJobId
= atoi(value
);
3278 * 'load_request_root()' - Load jobs from the RequestRoot directory.
3282 load_request_root(void)
3284 cups_dir_t
*dir
; /* Directory */
3285 cups_dentry_t
*dent
; /* Directory entry */
3286 cupsd_job_t
*job
; /* New job */
3290 * Open the requests directory...
3293 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Scanning %s for jobs...", RequestRoot
);
3295 if ((dir
= cupsDirOpen(RequestRoot
)) == NULL
)
3297 cupsdLogMessage(CUPSD_LOG_ERROR
,
3298 "Unable to open spool directory \"%s\": %s",
3299 RequestRoot
, strerror(errno
));
3304 * Read all the c##### files...
3307 while ((dent
= cupsDirRead(dir
)) != NULL
)
3308 if (strlen(dent
->filename
) >= 6 && dent
->filename
[0] == 'c')
3311 * Allocate memory for the job...
3314 if ((job
= calloc(sizeof(cupsd_job_t
), 1)) == NULL
)
3316 cupsdLogMessage(CUPSD_LOG_ERROR
, "Ran out of memory for jobs!");
3322 * Assign the job ID...
3325 job
->id
= atoi(dent
->filename
+ 1);
3326 job
->back_pipes
[0] = -1;
3327 job
->back_pipes
[1] = -1;
3328 job
->print_pipes
[0] = -1;
3329 job
->print_pipes
[1] = -1;
3331 if (job
->id
>= NextJobId
)
3332 NextJobId
= job
->id
+ 1;
3341 * Insert the job into the array, sorting by job priority and ID...
3344 cupsArrayAdd(Jobs
, job
);
3346 if (job
->state_value
< IPP_JOB_STOPPED
)
3347 cupsArrayAdd(ActiveJobs
,job
);
3349 cupsdUnloadJob(job
);
3357 * 'set_time()' - Set one of the "time-at-xyz" attributes...
3361 set_time(cupsd_job_t
*job
, /* I - Job to update */
3362 const char *name
) /* I - Name of attribute */
3364 ipp_attribute_t
*attr
; /* Time attribute */
3367 if ((attr
= ippFindAttribute(job
->attrs
, name
, IPP_TAG_ZERO
)) != NULL
)
3369 attr
->value_tag
= IPP_TAG_INTEGER
;
3370 attr
->values
[0].integer
= time(NULL
);
3376 * 'set_hold_until()' - Set the hold time and update job-hold-until attribute...
3380 set_hold_until(cupsd_job_t
*job
, /* I - Job to update */
3381 time_t holdtime
) /* I - Hold until time */
3383 ipp_attribute_t
*attr
; /* job-hold-until attribute */
3384 struct tm
*holddate
; /* Hold date */
3385 char holdstr
[64]; /* Hold time */
3389 * Set the hold_until value and hold the job...
3392 cupsdLogMessage(CUPSD_LOG_DEBUG
, "set_hold_until: hold_until = %d",
3395 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
3396 job
->state_value
= IPP_JOB_HELD
;
3397 job
->hold_until
= holdtime
;
3400 * Update the job-hold-until attribute with a string representing GMT
3401 * time (HH:MM:SS)...
3404 holddate
= gmtime(&holdtime
);
3405 snprintf(holdstr
, sizeof(holdstr
), "%d:%d:%d", holddate
->tm_hour
,
3406 holddate
->tm_min
, holddate
->tm_sec
);
3408 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
3409 IPP_TAG_KEYWORD
)) == NULL
)
3410 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
3413 * Either add the attribute or update the value of the existing one
3417 attr
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
3418 "job-hold-until", NULL
, holdstr
);
3420 cupsdSetString(&attr
->values
[0].string
.text
, holdstr
);
3427 * End of "$Id: job.c 5131 2006-02-18 05:31:36Z mike $".