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? */
154 int i
; /* Looping var */
155 char filename
[1024]; /* Job filename */
158 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdCancelJob: id = %d", job
->id
);
161 * Stop any processes that are working on the current job...
164 if (job
->state_value
== IPP_JOB_PROCESSING
)
165 cupsdStopJob(job
, 0);
170 job
->state
->values
[0].integer
= IPP_JOB_CANCELLED
;
172 job
->state_value
= IPP_JOB_CANCELLED
;
174 set_time(job
, "time-at-completed");
176 cupsdExpireSubscriptions(NULL
, job
);
179 * Remove the job from the active list...
182 cupsArrayRemove(ActiveJobs
, job
);
185 * Remove any authentication data...
188 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
192 * Remove the print file for good if we aren't preserving jobs or
196 job
->current_file
= 0;
198 if (!JobHistory
|| !JobFiles
|| purge
|| (job
->dtype
& CUPS_PRINTER_REMOTE
))
200 for (i
= 1; i
<= job
->num_files
; i
++)
202 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
207 if (job
->num_files
> 0)
209 free(job
->filetypes
);
210 free(job
->compressions
);
213 job
->filetypes
= NULL
;
214 job
->compressions
= NULL
;
218 if (JobHistory
&& !purge
&& !(job
->dtype
& CUPS_PRINTER_REMOTE
))
221 * Save job state info...
229 * Remove the job info file...
232 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
,
237 * Remove the job from the "all jobs" list...
240 cupsArrayRemove(Jobs
, job
);
243 * Free all memory used...
252 * 'cupsdCancelJobs()' - Cancel all jobs for the given destination/user...
256 cupsdCancelJobs(const char *dest
, /* I - Destination to cancel */
257 const char *username
, /* I - Username or NULL */
258 int purge
) /* I - Purge jobs? */
260 cupsd_job_t
*job
; /* Current job */
263 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
265 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
266 if ((dest
== NULL
|| !strcmp(job
->dest
, dest
)) &&
267 (username
== NULL
|| !strcmp(job
->username
, username
)))
270 * Cancel all jobs matching this destination/user...
273 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
274 purge
? "Job purged." : "Job canceled.");
276 cupsdCancelJob(job
, purge
);
284 * 'cupsdCheckJobs()' - Check the pending jobs and start any if the destination
291 cupsd_job_t
*job
; /* Current job in queue */
292 cupsd_printer_t
*printer
, /* Printer destination */
293 *pclass
; /* Printer class destination */
296 DEBUG_puts("cupsdCheckJobs()");
298 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
299 "cupsdCheckJobs: %d active jobs, sleeping=%d, reload=%d",
300 cupsArrayCount(ActiveJobs
), Sleeping
, NeedReload
);
302 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
304 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
307 * Start held jobs if they are ready...
310 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
311 "cupsdCheckJobs: Job %d: state_value=%d, loaded=%s",
312 job
->id
, job
->state_value
, job
->attrs
? "yes" : "no");
314 if (job
->state_value
== IPP_JOB_HELD
&&
316 job
->hold_until
< time(NULL
))
318 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
319 job
->state_value
= IPP_JOB_PENDING
;
323 * Start pending jobs if the destination is available...
326 if (job
->state_value
== IPP_JOB_PENDING
&& !NeedReload
&& !Sleeping
)
328 printer
= cupsdFindDest(job
->dest
);
332 (printer
->type
& (CUPS_PRINTER_IMPLICIT
| CUPS_PRINTER_CLASS
)))
335 * If the class is remote, just pass it to the remote server...
340 if (!(pclass
->type
& CUPS_PRINTER_REMOTE
))
342 if (pclass
->state
!= IPP_PRINTER_STOPPED
)
343 printer
= cupsdFindAvailablePrinter(job
->dest
);
349 if (!printer
&& !pclass
)
352 * Whoa, the printer and/or class for this destination went away;
356 cupsdLogMessage(CUPSD_LOG_WARN
,
357 "Printer/class %s has gone away; cancelling job %d!",
360 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
361 "Job canceled because the destination printer/class has "
364 cupsdCancelJob(job
, 1);
369 * See if the printer is available or remote and not printing a job;
370 * if so, start the job...
376 * Add/update a job-actual-printer-uri attribute for this job
377 * so that we know which printer actually printed the job...
380 ipp_attribute_t
*attr
; /* job-actual-printer-uri attribute */
383 if ((attr
= ippFindAttribute(job
->attrs
, "job-actual-printer-uri",
384 IPP_TAG_URI
)) != NULL
)
385 cupsdSetString(&attr
->values
[0].string
.text
, printer
->uri
);
387 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
,
388 "job-actual-printer-uri", NULL
, printer
->uri
);
391 if ((!(printer
->type
& CUPS_PRINTER_REMOTE
) && /* Printer is local */
392 printer
->state
== IPP_PRINTER_IDLE
) || /* and idle */
393 ((printer
->type
& CUPS_PRINTER_REMOTE
) && /* Printer is remote */
394 !printer
->job
)) /* and not printing */
395 start_job(job
, printer
);
403 * 'cupsdCleanJobs()' - Clean out old jobs.
409 cupsd_job_t
*job
; /* Current job */
415 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
416 job
&& cupsArrayCount(Jobs
) >= MaxJobs
;
417 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
418 if (job
->state_value
>= IPP_JOB_CANCELLED
)
419 cupsdCancelJob(job
, 1);
424 * 'cupsdFinishJob()' - Finish a job.
428 cupsdFinishJob(cupsd_job_t
*job
) /* I - Job */
430 int job_history
; /* Did cupsdCancelJob() keep the job? */
431 cupsd_printer_t
*printer
; /* Current printer */
434 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] File %d is complete.",
435 job
->id
, job
->current_file
- 1);
437 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFinishJob: job->status is %d",
440 if (job
->status_buffer
&& job
->current_file
>= job
->num_files
)
443 * Close the pipe and clear the input bit.
446 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
447 "cupsdFinishJob: Removing fd %d from InputSet...",
448 job
->status_buffer
->fd
);
450 FD_CLR(job
->status_buffer
->fd
, InputSet
);
452 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
453 "cupsdFinishJob: Closing status pipes [ %d %d ]...",
454 job
->status_pipes
[0], job
->status_pipes
[1]);
456 cupsdClosePipe(job
->status_pipes
);
457 cupsdStatBufDelete(job
->status_buffer
);
459 job
->status_buffer
= NULL
;
462 printer
= job
->printer
;
467 * Backend had errors; stop it...
470 int exit_code
; /* Exit code from backend */
474 * Convert the status to an exit code. Due to the way the W* macros are
475 * implemented on MacOS X (bug?), we have to store the exit status in a
476 * variable first and then convert...
479 exit_code
= -job
->status
;
480 if (WIFEXITED(exit_code
))
481 exit_code
= WEXITSTATUS(exit_code
);
483 exit_code
= job
->status
;
485 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Backend returned status %d (%s)",
487 exit_code
== CUPS_BACKEND_FAILED
? "failed" :
488 exit_code
== CUPS_BACKEND_AUTH_REQUIRED
?
489 "authentication required" :
490 exit_code
== CUPS_BACKEND_HOLD
? "hold job" :
491 exit_code
== CUPS_BACKEND_STOP
? "stop printer" :
492 exit_code
== CUPS_BACKEND_CANCEL
? "cancel job" :
493 exit_code
< 0 ? "crashed" : "unknown");
496 * Do what needs to be done...
502 case CUPS_BACKEND_FAILED
:
504 * Backend failure, use the error-policy to determine how to
508 cupsdStopJob(job
, 0);
509 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
510 job
->state_value
= IPP_JOB_PENDING
;
514 * If the job was queued to a class, try requeuing it... For
515 * faxes and retry-job queues, hold the current job for 5 minutes.
518 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
520 else if ((printer
->type
& CUPS_PRINTER_FAX
) ||
521 !strcmp(printer
->error_policy
, "retry-job"))
524 * See how many times we've tried to send the job; if more than
525 * the limit, cancel the job.
530 if (job
->tries
>= JobRetryLimit
)
536 cupsdLogMessage(CUPSD_LOG_ERROR
,
537 "Canceling job %d since it could not be sent "
539 job
->id
, JobRetryLimit
);
541 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, printer
, job
,
542 "Job canceled since it could not be sent after %d "
546 cupsdCancelJob(job
, 0);
551 * Try again in N seconds...
554 set_hold_until(job
, time(NULL
) + JobRetryInterval
);
557 else if (!strcmp(printer
->error_policy
, "abort-job"))
558 cupsdCancelJob(job
, 0);
561 case CUPS_BACKEND_CANCEL
:
566 cupsdCancelJob(job
, 0);
569 case CUPS_BACKEND_HOLD
:
574 cupsdStopJob(job
, 0);
575 cupsdSetJobHoldUntil(job
, "indefinite");
579 case CUPS_BACKEND_STOP
:
581 * Stop the printer...
584 cupsdStopJob(job
, 0);
586 cupsdSetPrinterState(printer
, IPP_PRINTER_STOPPED
, 1);
589 case CUPS_BACKEND_AUTH_REQUIRED
:
590 cupsdStopJob(job
, 0);
591 cupsdSetJobHoldUntil(job
, "authenticated");
594 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
595 "Authentication is required for job %d.", job
->id
);
600 * Try printing another job...
605 else if (job
->status
> 0)
608 * Filter had errors; stop job...
611 cupsdStopJob(job
, 1);
613 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
614 "Job stopped due to filter errors; please consult the "
615 "error_log file for details.");
621 * Job printed successfully; cancel it...
624 if (job
->current_file
< job
->num_files
)
627 * Start the next file in the job...
630 FilterLevel
-= job
->cost
;
631 start_job(job
, printer
);
636 * Close out this job...
639 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, printer
, job
,
640 "Job completed successfully.");
642 job_history
= JobHistory
&& !(job
->dtype
& CUPS_PRINTER_REMOTE
);
644 cupsdCancelJob(job
, 0);
648 job
->state
->values
[0].integer
= IPP_JOB_COMPLETED
;
649 job
->state_value
= IPP_JOB_COMPLETED
;
654 * Clear the printer's state_message and state_reasons and move on...
657 printer
->state_message
[0] = '\0';
659 cupsdSetPrinterReasons(printer
, "");
668 * 'cupsdFreeAllJobs()' - Free all jobs from memory.
672 cupsdFreeAllJobs(void)
674 cupsd_job_t
*job
; /* Current job */
685 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
687 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
689 cupsArrayRemove(Jobs
, job
);
690 cupsArrayRemove(ActiveJobs
, job
);
695 cupsdReleaseSignals();
700 * 'cupsdFindJob()' - Find the specified job.
703 cupsd_job_t
* /* O - Job data */
704 cupsdFindJob(int id
) /* I - Job ID */
706 cupsd_job_t key
; /* Search key */
711 return ((cupsd_job_t
*)cupsArrayFind(Jobs
, &key
));
716 * 'cupsdGetPrinterJobCount()' - Get the number of pending, processing,
717 * or held jobs in a printer or class.
720 int /* O - Job count */
721 cupsdGetPrinterJobCount(
722 const char *dest
) /* I - Printer or class name */
724 int count
; /* Job count */
725 cupsd_job_t
*job
; /* Current job */
728 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
730 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
731 if (job
->dest
&& !strcasecmp(job
->dest
, dest
))
739 * 'cupsdGetUserJobCount()' - Get the number of pending, processing,
740 * or held jobs for a user.
743 int /* O - Job count */
744 cupsdGetUserJobCount(
745 const char *username
) /* I - Username */
747 int count
; /* Job count */
748 cupsd_job_t
*job
; /* Current job */
751 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
753 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
754 if (!strcasecmp(job
->username
, username
))
762 * 'cupsdHoldJob()' - Hold the specified job.
766 cupsdHoldJob(cupsd_job_t
*job
) /* I - Job data */
768 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdHoldJob: id = %d", job
->id
);
770 if (job
->state_value
== IPP_JOB_PROCESSING
)
771 cupsdStopJob(job
, 0);
775 DEBUG_puts("cupsdHoldJob: setting state to held...");
777 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
778 job
->state_value
= IPP_JOB_HELD
;
787 * 'cupsdLoadAllJobs()' - Load all jobs from disk.
791 cupsdLoadAllJobs(void)
793 char filename
[1024]; /* Full filename of job.cache file */
794 struct stat fileinfo
, /* Information on job.cache file */
795 dirinfo
; /* Information on RequestRoot dir */
800 * Create the job arrays as needed...
804 Jobs
= cupsArrayNew(compare_jobs
, NULL
);
807 ActiveJobs
= cupsArrayNew(compare_active_jobs
, NULL
);
810 * See whether the job.cache file is older than the RequestRoot directory...
813 snprintf(filename
, sizeof(filename
), "%s/job.cache", CacheDir
);
815 if (stat(filename
, &fileinfo
))
817 fileinfo
.st_mtime
= 0;
820 cupsdLogMessage(CUPSD_LOG_ERROR
,
821 "Unable to get file information for \"%s\" - %s",
822 filename
, strerror(errno
));
825 if (stat(RequestRoot
, &dirinfo
))
827 dirinfo
.st_mtime
= 0;
830 cupsdLogMessage(CUPSD_LOG_ERROR
,
831 "Unable to get directory information for \"%s\" - %s",
832 RequestRoot
, strerror(errno
));
836 * Load the most recent source for job data...
839 if (dirinfo
.st_mtime
> fileinfo
.st_mtime
)
843 load_next_job_id(filename
);
846 load_job_cache(filename
);
849 * Clean out old jobs as needed...
852 if (MaxJobs
> 0 && cupsArrayCount(Jobs
) >= MaxJobs
)
858 * 'cupsdLoadJob()' - Load a single job...
862 cupsdLoadJob(cupsd_job_t
*job
) /* I - Job */
864 char jobfile
[1024]; /* Job filename */
865 cups_file_t
*fp
; /* Job file */
866 int fileid
; /* Current file ID */
867 ipp_attribute_t
*attr
; /* Job attribute */
868 char scheme
[32], /* Scheme portion of URI */
869 username
[64], /* Username portion of URI */
871 /* Host portion of URI */
872 resource
[HTTP_MAX_URI
];
873 /* Resource portion of URI */
874 int port
; /* Port portion of URI */
875 const char *dest
; /* Destination */
876 mime_type_t
**filetypes
; /* New filetypes array */
877 int *compressions
; /* New compressions array */
882 if (job
->state_value
> IPP_JOB_STOPPED
)
883 job
->access_time
= time(NULL
);
888 if ((job
->attrs
= ippNew()) == NULL
)
890 cupsdLogMessage(CUPSD_LOG_ERROR
, "Ran out of memory for job attributes!");
895 * Load job attributes...
898 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Loading attributes for job %d...",
901 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, job
->id
);
902 if ((fp
= cupsFileOpen(jobfile
, "r")) == NULL
)
904 cupsdLogMessage(CUPSD_LOG_ERROR
,
905 "Unable to open job control file \"%s\" - %s!",
906 jobfile
, strerror(errno
));
907 ippDelete(job
->attrs
);
912 if (ippReadIO(fp
, (ipp_iocb_t
)cupsFileRead
, 1, NULL
, job
->attrs
) != IPP_DATA
)
914 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to read job control file \"%s\"!",
917 ippDelete(job
->attrs
);
926 * Copy attribute data to the job object...
929 if ((job
->state
= ippFindAttribute(job
->attrs
, "job-state",
930 IPP_TAG_ENUM
)) == NULL
)
932 cupsdLogMessage(CUPSD_LOG_ERROR
,
933 "Missing or bad job-state attribute in control "
936 ippDelete(job
->attrs
);
942 job
->state_value
= (ipp_jstate_t
)job
->state
->values
[0].integer
;
946 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
947 IPP_TAG_URI
)) == NULL
)
949 cupsdLogMessage(CUPSD_LOG_ERROR
,
950 "No job-printer-uri attribute in control file \"%s\"!",
952 ippDelete(job
->attrs
);
958 httpSeparateURI(HTTP_URI_CODING_ALL
, attr
->values
[0].string
.text
, scheme
,
959 sizeof(scheme
), username
, sizeof(username
), host
,
960 sizeof(host
), &port
, resource
, sizeof(resource
));
962 if ((dest
= cupsdValidateDest(host
, resource
, &(job
->dtype
),
965 cupsdLogMessage(CUPSD_LOG_ERROR
,
966 "Unable to queue job for destination \"%s\"!",
967 attr
->values
[0].string
.text
);
968 ippDelete(job
->attrs
);
974 cupsdSetString(&job
->dest
, dest
);
977 job
->sheets
= ippFindAttribute(job
->attrs
, "job-media-sheets-completed",
979 job
->job_sheets
= ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_NAME
);
983 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
984 IPP_TAG_INTEGER
)) == NULL
)
986 cupsdLogMessage(CUPSD_LOG_ERROR
,
987 "Missing or bad job-priority attribute in control "
988 "file \"%s\"!", jobfile
);
989 ippDelete(job
->attrs
);
995 job
->priority
= attr
->values
[0].integer
;
1000 if ((attr
= ippFindAttribute(job
->attrs
, "job-originating-user-name",
1001 IPP_TAG_NAME
)) == NULL
)
1003 cupsdLogMessage(CUPSD_LOG_ERROR
,
1004 "Missing or bad job-originating-user-name attribute "
1005 "in control file \"%s\"!", jobfile
);
1006 ippDelete(job
->attrs
);
1012 cupsdSetString(&job
->username
, attr
->values
[0].string
.text
);
1016 * Set the job hold-until time and state...
1019 if (job
->state_value
== IPP_JOB_HELD
)
1021 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1022 IPP_TAG_KEYWORD
)) == NULL
)
1023 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1026 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
1029 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1030 job
->state_value
= IPP_JOB_PENDING
;
1033 else if (job
->state_value
== IPP_JOB_PROCESSING
)
1035 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1036 job
->state_value
= IPP_JOB_PENDING
;
1039 if (!job
->num_files
)
1042 * Find all the d##### files...
1045 for (fileid
= 1; fileid
< 10000; fileid
++)
1047 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
1050 if (access(jobfile
, 0))
1053 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Auto-typing document file \"%s\"...",
1056 if (fileid
> job
->num_files
)
1058 if (job
->num_files
== 0)
1060 compressions
= (int *)calloc(fileid
, sizeof(int));
1061 filetypes
= (mime_type_t
**)calloc(fileid
, sizeof(mime_type_t
*));
1065 compressions
= (int *)realloc(job
->compressions
,
1066 sizeof(int) * fileid
);
1067 filetypes
= (mime_type_t
**)realloc(job
->filetypes
,
1068 sizeof(mime_type_t
*) *
1072 if (!compressions
|| !filetypes
)
1074 cupsdLogMessage(CUPSD_LOG_ERROR
,
1075 "Ran out of memory for job file types!");
1079 job
->compressions
= compressions
;
1080 job
->filetypes
= filetypes
;
1081 job
->num_files
= fileid
;
1084 job
->filetypes
[fileid
- 1] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
1085 job
->compressions
+ fileid
- 1);
1087 if (!job
->filetypes
[fileid
- 1])
1088 job
->filetypes
[fileid
- 1] = mimeType(MimeDatabase
, "application",
1093 job
->access_time
= time(NULL
);
1098 * 'cupsdMoveJob()' - Move the specified job to a different destination.
1102 cupsdMoveJob(cupsd_job_t
*job
, /* I - Job */
1103 cupsd_printer_t
*p
) /* I - Destination printer or class */
1105 ipp_attribute_t
*attr
; /* job-printer-uri attribute */
1106 const char *olddest
; /* Old destination */
1107 cupsd_printer_t
*oldp
; /* Old pointer */
1111 * Don't move completed jobs...
1114 if (job
->state_value
> IPP_JOB_STOPPED
)
1118 * Get the old destination...
1121 olddest
= job
->dest
;
1124 oldp
= job
->printer
;
1126 oldp
= cupsdFindDest(olddest
);
1129 * Change the destination information...
1134 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, oldp
, job
,
1135 "Job #%d moved from %s to %s.", job
->id
, olddest
,
1138 cupsdSetString(&job
->dest
, p
->name
);
1139 job
->dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_REMOTE
|
1140 CUPS_PRINTER_IMPLICIT
);
1142 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1143 IPP_TAG_URI
)) != NULL
)
1144 cupsdSetString(&(attr
->values
[0].string
.text
), p
->uri
);
1146 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, p
, job
,
1147 "Job #%d moved from %s to %s.", job
->id
, olddest
,
1155 * 'cupsdReleaseJob()' - Release the specified job.
1159 cupsdReleaseJob(cupsd_job_t
*job
) /* I - Job */
1161 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdReleaseJob: id = %d", job
->id
);
1163 if (job
->state_value
== IPP_JOB_HELD
)
1165 DEBUG_puts("cupsdReleaseJob: setting state to pending...");
1167 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1168 job
->state_value
= IPP_JOB_PENDING
;
1176 * 'cupsdRestartJob()' - Restart the specified job.
1180 cupsdRestartJob(cupsd_job_t
*job
) /* I - Job */
1182 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdRestartJob: id = %d", job
->id
);
1184 if (job
->state_value
== IPP_JOB_STOPPED
|| job
->num_files
)
1189 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1190 job
->state_value
= IPP_JOB_PENDING
;
1198 * 'cupsdSaveAllJobs()' - Save a summary of all jobs to disk.
1202 cupsdSaveAllJobs(void)
1204 int i
; /* Looping var */
1205 cups_file_t
*fp
; /* Job cache file */
1206 char temp
[1024]; /* Temporary string */
1207 cupsd_job_t
*job
; /* Current job */
1208 time_t curtime
; /* Current time */
1209 struct tm
*curdate
; /* Current date */
1212 snprintf(temp
, sizeof(temp
), "%s/job.cache", CacheDir
);
1213 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
1215 cupsdLogMessage(CUPSD_LOG_ERROR
,
1216 "Unable to create job cache file \"%s\" - %s",
1217 temp
, strerror(errno
));
1221 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving job cache file \"%s\"...", temp
);
1224 * Restrict access to the file...
1227 fchown(cupsFileNumber(fp
), getuid(), Group
);
1228 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
1231 * Write a small header to the file...
1234 curtime
= time(NULL
);
1235 curdate
= localtime(&curtime
);
1236 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
1238 cupsFilePuts(fp
, "# Job cache file for " CUPS_SVERSION
"\n");
1239 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
1240 cupsFilePrintf(fp
, "NextJobId %d\n", NextJobId
);
1243 * Write each job known to the system...
1246 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1248 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1250 cupsFilePrintf(fp
, "<Job %d>\n", job
->id
);
1251 cupsFilePrintf(fp
, "State %d\n", job
->state_value
);
1252 cupsFilePrintf(fp
, "Priority %d\n", job
->priority
);
1253 cupsFilePrintf(fp
, "Username %s\n", job
->username
);
1254 cupsFilePrintf(fp
, "Destination %s\n", job
->dest
);
1255 cupsFilePrintf(fp
, "DestType %d\n", job
->dtype
);
1256 cupsFilePrintf(fp
, "NumFiles %d\n", job
->num_files
);
1257 for (i
= 0; i
< job
->num_files
; i
++)
1258 cupsFilePrintf(fp
, "File %d %s/%s %d\n", i
+ 1, job
->filetypes
[i
]->super
,
1259 job
->filetypes
[i
]->type
, job
->compressions
[i
]);
1260 cupsFilePuts(fp
, "</Job>\n");
1268 * 'cupsdSaveJob()' - Save a job to disk.
1272 cupsdSaveJob(cupsd_job_t
*job
) /* I - Job */
1274 char filename
[1024]; /* Job control filename */
1275 cups_file_t
*fp
; /* Job file */
1278 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSaveJob(job=%p(%d)): job->attrs=%p",
1279 job
, job
->id
, job
->attrs
);
1281 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
, job
->id
);
1283 if ((fp
= cupsFileOpen(filename
, "w")) == NULL
)
1285 cupsdLogMessage(CUPSD_LOG_ERROR
,
1286 "Unable to create job control file \"%s\" - %s.",
1287 filename
, strerror(errno
));
1291 fchmod(cupsFileNumber(fp
), 0600);
1292 fchown(cupsFileNumber(fp
), RunUser
, Group
);
1294 job
->attrs
->state
= IPP_IDLE
;
1296 if (ippWriteIO(fp
, (ipp_iocb_t
)cupsFileWrite
, 1, NULL
,
1297 job
->attrs
) != IPP_DATA
)
1298 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to write job control file \"%s\"!",
1306 * 'cupsdSetJobHoldUntil()' - Set the hold time for a job...
1310 cupsdSetJobHoldUntil(cupsd_job_t
*job
, /* I - Job */
1311 const char *when
) /* I - When to resume */
1313 time_t curtime
; /* Current time */
1314 struct tm
*curdate
; /* Current date */
1315 int hour
; /* Hold hour */
1316 int minute
; /* Hold minute */
1317 int second
; /* Hold second */
1320 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetJobHoldUntil(%d, \"%s\")",
1325 if (!strcmp(when
, "indefinite") || !strcmp(when
, "authenticated"))
1328 * Hold indefinitely...
1331 job
->hold_until
= 0;
1333 else if (!strcmp(when
, "day-time"))
1336 * Hold to 6am the next morning unless local time is < 6pm.
1339 curtime
= time(NULL
);
1340 curdate
= localtime(&curtime
);
1342 if (curdate
->tm_hour
< 18)
1343 job
->hold_until
= curtime
;
1345 job
->hold_until
= curtime
+
1346 ((29 - curdate
->tm_hour
) * 60 + 59 -
1347 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1349 else if (!strcmp(when
, "evening") || !strcmp(when
, "night"))
1352 * Hold to 6pm unless local time is > 6pm or < 6am.
1355 curtime
= time(NULL
);
1356 curdate
= localtime(&curtime
);
1358 if (curdate
->tm_hour
< 6 || curdate
->tm_hour
>= 18)
1359 job
->hold_until
= curtime
;
1361 job
->hold_until
= curtime
+
1362 ((17 - curdate
->tm_hour
) * 60 + 59 -
1363 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1365 else if (!strcmp(when
, "second-shift"))
1368 * Hold to 4pm unless local time is > 4pm.
1371 curtime
= time(NULL
);
1372 curdate
= localtime(&curtime
);
1374 if (curdate
->tm_hour
>= 16)
1375 job
->hold_until
= curtime
;
1377 job
->hold_until
= curtime
+
1378 ((15 - curdate
->tm_hour
) * 60 + 59 -
1379 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1381 else if (!strcmp(when
, "third-shift"))
1384 * Hold to 12am unless local time is < 8am.
1387 curtime
= time(NULL
);
1388 curdate
= localtime(&curtime
);
1390 if (curdate
->tm_hour
< 8)
1391 job
->hold_until
= curtime
;
1393 job
->hold_until
= curtime
+
1394 ((23 - curdate
->tm_hour
) * 60 + 59 -
1395 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1397 else if (!strcmp(when
, "weekend"))
1400 * Hold to weekend unless we are in the weekend.
1403 curtime
= time(NULL
);
1404 curdate
= localtime(&curtime
);
1406 if (curdate
->tm_wday
|| curdate
->tm_wday
== 6)
1407 job
->hold_until
= curtime
;
1409 job
->hold_until
= curtime
+
1410 (((5 - curdate
->tm_wday
) * 24 +
1411 (17 - curdate
->tm_hour
)) * 60 + 59 -
1412 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1414 else if (sscanf(when
, "%d:%d:%d", &hour
, &minute
, &second
) >= 2)
1417 * Hold to specified GMT time (HH:MM or HH:MM:SS)...
1420 curtime
= time(NULL
);
1421 curdate
= gmtime(&curtime
);
1423 job
->hold_until
= curtime
+
1424 ((hour
- curdate
->tm_hour
) * 60 + minute
-
1425 curdate
->tm_min
) * 60 + second
- curdate
->tm_sec
;
1428 * Hold until next day as needed...
1431 if (job
->hold_until
< curtime
)
1432 job
->hold_until
+= 24 * 60 * 60 * 60;
1435 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetJobHoldUntil: hold_until = %d",
1436 (int)job
->hold_until
);
1441 * 'cupsdSetJobPriority()' - Set the priority of a job, moving it up/down in
1442 * the list as needed.
1446 cupsdSetJobPriority(
1447 cupsd_job_t
*job
, /* I - Job ID */
1448 int priority
) /* I - New priority (0 to 100) */
1450 ipp_attribute_t
*attr
; /* Job attribute */
1454 * Don't change completed jobs...
1457 if (job
->state_value
>= IPP_JOB_PROCESSING
)
1461 * Set the new priority and re-add the job into the active list...
1464 cupsArrayRemove(ActiveJobs
, job
);
1466 job
->priority
= priority
;
1468 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
1469 IPP_TAG_INTEGER
)) != NULL
)
1470 attr
->values
[0].integer
= priority
;
1472 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-priority",
1475 cupsArrayAdd(ActiveJobs
, job
);
1482 * 'cupsdStopAllJobs()' - Stop all print jobs.
1486 cupsdStopAllJobs(void)
1488 cupsd_job_t
*job
; /* Current job */
1491 DEBUG_puts("cupsdStopAllJobs()");
1493 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
1495 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
1496 if (job
->state_value
== IPP_JOB_PROCESSING
)
1498 cupsdStopJob(job
, 1);
1499 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1500 job
->state_value
= IPP_JOB_PENDING
;
1506 * 'cupsdStopJob()' - Stop a print job.
1510 cupsdStopJob(cupsd_job_t
*job
, /* I - Job */
1511 int force
) /* I - 1 = Force all filters to stop */
1513 int i
; /* Looping var */
1516 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdStopJob: id = %d, force = %d",
1519 if (job
->state_value
!= IPP_JOB_PROCESSING
)
1522 FilterLevel
-= job
->cost
;
1524 if (job
->status
< 0 &&
1525 !(job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
)) &&
1526 !(job
->printer
->type
& CUPS_PRINTER_FAX
) &&
1527 !strcmp(job
->printer
->error_policy
, "stop-printer"))
1528 cupsdSetPrinterState(job
->printer
, IPP_PRINTER_STOPPED
, 1);
1529 else if (job
->printer
->state
!= IPP_PRINTER_STOPPED
)
1530 cupsdSetPrinterState(job
->printer
, IPP_PRINTER_IDLE
, 0);
1532 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdStopJob: printer state is %d",
1533 job
->printer
->state
);
1535 job
->state
->values
[0].integer
= IPP_JOB_STOPPED
;
1536 job
->state_value
= IPP_JOB_STOPPED
;
1537 job
->printer
->job
= NULL
;
1538 job
->printer
= NULL
;
1540 job
->current_file
--;
1542 for (i
= 0; job
->filters
[i
]; i
++)
1543 if (job
->filters
[i
] > 0)
1545 cupsdEndProcess(job
->filters
[i
], force
);
1546 job
->filters
[i
] = 0;
1549 if (job
->backend
> 0)
1551 cupsdEndProcess(job
->backend
, force
);
1555 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1556 "cupsdStopJob: Closing print pipes [ %d %d ]...",
1557 job
->print_pipes
[0], job
->print_pipes
[1]);
1559 cupsdClosePipe(job
->print_pipes
);
1561 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1562 "cupsdStopJob: Closing back pipes [ %d %d ]...",
1563 job
->back_pipes
[0], job
->back_pipes
[1]);
1565 cupsdClosePipe(job
->back_pipes
);
1567 if (job
->status_buffer
)
1570 * Close the pipe and clear the input bit.
1573 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1574 "cupsdStopJob: Removing fd %d from InputSet...",
1575 job
->status_buffer
->fd
);
1577 FD_CLR(job
->status_buffer
->fd
, InputSet
);
1579 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1580 "cupsdStopJob: Closing status pipes [ %d %d ]...",
1581 job
->status_pipes
[0], job
->status_pipes
[1]);
1583 cupsdClosePipe(job
->status_pipes
);
1584 cupsdStatBufDelete(job
->status_buffer
);
1586 job
->status_buffer
= NULL
;
1592 * 'cupsdUnloadCompletedJobs()' - Flush completed job history from memory.
1596 cupsdUnloadCompletedJobs(void)
1598 cupsd_job_t
*job
; /* Current job */
1599 time_t expire
; /* Expiration time */
1602 expire
= time(NULL
) - 60;
1604 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1606 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1607 if (job
->attrs
&& job
->state_value
>= IPP_JOB_STOPPED
&&
1608 job
->access_time
< expire
)
1614 * 'cupsdUpdateJob()' - Read a status update from a job's filters.
1618 cupsdUpdateJob(cupsd_job_t
*job
) /* I - Job to check */
1620 int i
; /* Looping var */
1621 int copies
; /* Number of copies printed */
1622 char message
[1024], /* Message text */
1623 *ptr
; /* Pointer update... */
1624 int loglevel
; /* Log level for message */
1627 while ((ptr
= cupsdStatBufUpdate(job
->status_buffer
, &loglevel
,
1628 message
, sizeof(message
))) != NULL
)
1631 * Process page and printer state messages as needed...
1634 if (loglevel
== CUPSD_LOG_PAGE
)
1637 * Page message; send the message to the page_log file and update the
1638 * job sheet count...
1641 if (job
->sheets
!= NULL
)
1643 if (!strncasecmp(message
, "total ", 6))
1646 * Got a total count of pages from a backend or filter...
1649 copies
= atoi(message
+ 6);
1650 copies
-= job
->sheets
->values
[0].integer
; /* Just track the delta */
1652 else if (!sscanf(message
, "%*d%d", &copies
))
1655 job
->sheets
->values
[0].integer
+= copies
;
1657 if (job
->printer
->page_limit
)
1658 cupsdUpdateQuota(job
->printer
, job
->username
, copies
, 0);
1661 cupsdLogPage(job
, message
);
1663 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
1664 "Printed %d page(s).", job
->sheets
->values
[0].integer
);
1666 else if (loglevel
== CUPSD_LOG_STATE
)
1668 cupsdSetPrinterReasons(job
->printer
, message
);
1669 cupsdAddPrinterHistory(job
->printer
);
1671 else if (loglevel
== CUPSD_LOG_ATTR
)
1674 * Set attribute(s)...
1680 else if (!strncmp(message
, "recoverable:", 12))
1682 cupsdSetPrinterReasons(job
->printer
,
1683 "+com.apple.print.recoverable-warning");
1686 while (isspace(*ptr
& 255))
1689 cupsdSetString(&job
->printer
->recoverable
, ptr
);
1690 cupsdAddPrinterHistory(job
->printer
);
1692 else if (!strncmp(message
, "recovered:", 10))
1694 cupsdSetPrinterReasons(job
->printer
,
1695 "-com.apple.print.recoverable-warning");
1698 while (isspace(*ptr
& 255))
1701 cupsdSetString(&job
->printer
->recoverable
, ptr
);
1702 cupsdAddPrinterHistory(job
->printer
);
1704 #endif /* __APPLE__ */
1705 else if (loglevel
<= CUPSD_LOG_INFO
)
1708 * Some message to show in the printer-state-message attribute...
1711 strlcpy(job
->printer
->state_message
, message
,
1712 sizeof(job
->printer
->state_message
));
1713 cupsdAddPrinterHistory(job
->printer
);
1716 if (!strchr(job
->status_buffer
->buffer
, '\n'))
1723 * See if all of the filters and the backend have returned their
1727 for (i
= 0; job
->filters
[i
] < 0; i
++);
1729 if (job
->filters
[i
])
1732 if (job
->current_file
>= job
->num_files
&& job
->backend
> 0)
1736 * Handle the end of job stuff...
1739 cupsdFinishJob(job
);
1745 * 'compare_active_jobs()' - Compare the job IDs and priorities of two jobs.
1748 static int /* O - Difference */
1749 compare_active_jobs(void *first
, /* I - First job */
1750 void *second
, /* I - Second job */
1751 void *data
) /* I - App data (not used) */
1753 int diff
; /* Difference */
1756 if ((diff
= ((cupsd_job_t
*)first
)->priority
-
1757 ((cupsd_job_t
*)second
)->priority
) != 0)
1760 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
1765 * 'compare_jobs()' - Compare the job IDs of two jobs.
1768 static int /* O - Difference */
1769 compare_jobs(void *first
, /* I - First job */
1770 void *second
, /* I - Second job */
1771 void *data
) /* I - App data (not used) */
1773 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
1778 * 'free_job()' - Free all memory used by a job.
1782 free_job(cupsd_job_t
*job
) /* I - Job */
1784 cupsdClearString(&job
->username
);
1785 cupsdClearString(&job
->dest
);
1787 if (job
->num_files
> 0)
1789 free(job
->compressions
);
1790 free(job
->filetypes
);
1793 ippDelete(job
->attrs
);
1800 * 'ipp_length()' - Compute the size of the buffer needed to hold
1801 * the textual IPP attributes.
1804 static int /* O - Size of attribute buffer */
1805 ipp_length(ipp_t
*ipp
) /* I - IPP request */
1807 int bytes
; /* Number of bytes */
1808 int i
; /* Looping var */
1809 ipp_attribute_t
*attr
; /* Current attribute */
1813 * Loop through all attributes...
1818 for (attr
= ipp
->attrs
; attr
!= NULL
; attr
= attr
->next
)
1821 * Skip attributes that won't be sent to filters...
1824 if (attr
->value_tag
== IPP_TAG_MIMETYPE
||
1825 attr
->value_tag
== IPP_TAG_NAMELANG
||
1826 attr
->value_tag
== IPP_TAG_TEXTLANG
||
1827 attr
->value_tag
== IPP_TAG_URI
||
1828 attr
->value_tag
== IPP_TAG_URISCHEME
)
1831 if (strncmp(attr
->name
, "time-", 5) == 0)
1835 * Add space for a leading space and commas between each value.
1836 * For the first attribute, the leading space isn't used, so the
1837 * extra byte can be used as the nul terminator...
1840 bytes
++; /* " " separator */
1841 bytes
+= attr
->num_values
; /* "," separators */
1844 * Boolean attributes appear as "foo,nofoo,foo,nofoo", while
1845 * other attributes appear as "foo=value1,value2,...,valueN".
1848 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
1849 bytes
+= strlen(attr
->name
);
1851 bytes
+= attr
->num_values
* strlen(attr
->name
);
1854 * Now add the size required for each value in the attribute...
1857 switch (attr
->value_tag
)
1859 case IPP_TAG_INTEGER
:
1862 * Minimum value of a signed integer is -2147483647, or 11 digits.
1865 bytes
+= attr
->num_values
* 11;
1868 case IPP_TAG_BOOLEAN
:
1870 * Add two bytes for each false ("no") value...
1873 for (i
= 0; i
< attr
->num_values
; i
++)
1874 if (!attr
->values
[i
].boolean
)
1878 case IPP_TAG_RANGE
:
1880 * A range is two signed integers separated by a hyphen, or
1881 * 23 characters max.
1884 bytes
+= attr
->num_values
* 23;
1887 case IPP_TAG_RESOLUTION
:
1889 * A resolution is two signed integers separated by an "x" and
1890 * suffixed by the units, or 26 characters max.
1893 bytes
+= attr
->num_values
* 26;
1896 case IPP_TAG_STRING
:
1899 case IPP_TAG_KEYWORD
:
1900 case IPP_TAG_CHARSET
:
1901 case IPP_TAG_LANGUAGE
:
1904 * Strings can contain characters that need quoting. We need
1905 * at least 2 * len + 2 characters to cover the quotes and
1906 * any backslashes in the string.
1909 for (i
= 0; i
< attr
->num_values
; i
++)
1910 bytes
+= 2 * strlen(attr
->values
[i
].string
.text
) + 2;
1914 break; /* anti-compiler-warning-code */
1923 * 'load_job_cache()' - Load jobs from the job.cache file.
1927 load_job_cache(const char *filename
) /* I - job.cache filename */
1929 cups_file_t
*fp
; /* job.cache file */
1930 char line
[1024], /* Line buffer */
1931 *value
; /* Value on line */
1932 int linenum
; /* Line number in file */
1933 cupsd_job_t
*job
; /* Current job */
1934 int jobid
; /* Job ID */
1935 char jobfile
[1024]; /* Job filename */
1939 * Open the job.cache file...
1942 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
1944 if (errno
!= ENOENT
)
1945 cupsdLogMessage(CUPSD_LOG_ERROR
,
1946 "Unable to open job cache file \"%s\": %s",
1947 filename
, strerror(errno
));
1949 load_request_root();
1955 * Read entries from the job cache file and create jobs as needed.
1958 cupsdLogMessage(CUPSD_LOG_INFO
, "Loading job cache file \"%s\"...",
1964 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
1966 if (!strcasecmp(line
, "NextJobId"))
1969 NextJobId
= atoi(value
);
1971 else if (!strcasecmp(line
, "<Job"))
1975 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing </Job> directive on line %d!",
1982 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing job ID on line %d!", linenum
);
1986 jobid
= atoi(value
);
1990 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad job ID %d on line %d!", jobid
,
1995 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, jobid
);
1996 if (access(jobfile
, 0))
1998 cupsdLogMessage(CUPSD_LOG_ERROR
, "Job %d files have gone away!", jobid
);
2002 job
= calloc(1, sizeof(cupsd_job_t
));
2005 cupsdLogMessage(CUPSD_LOG_EMERG
,
2006 "Unable to allocate memory for job %d!", jobid
);
2011 job
->back_pipes
[0] = -1;
2012 job
->back_pipes
[1] = -1;
2013 job
->print_pipes
[0] = -1;
2014 job
->print_pipes
[1] = -1;
2015 job
->status_pipes
[0] = -1;
2016 job
->status_pipes
[1] = -1;
2018 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Loading job %d from cache...", job
->id
);
2022 cupsdLogMessage(CUPSD_LOG_ERROR
,
2023 "Missing <Job #> directive on line %d!", linenum
);
2026 else if (!strcasecmp(line
, "</Job>"))
2028 cupsArrayAdd(Jobs
, job
);
2030 if (job
->state_value
<= IPP_JOB_STOPPED
)
2032 cupsArrayAdd(ActiveJobs
, job
);
2040 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing value on line %d!", linenum
);
2043 else if (!strcasecmp(line
, "State"))
2045 job
->state_value
= atoi(value
);
2047 if (job
->state_value
< IPP_JOB_PENDING
)
2048 job
->state_value
= IPP_JOB_PENDING
;
2049 else if (job
->state_value
> IPP_JOB_COMPLETED
)
2050 job
->state_value
= IPP_JOB_COMPLETED
;
2052 else if (!strcasecmp(line
, "Priority"))
2054 job
->priority
= atoi(value
);
2056 else if (!strcasecmp(line
, "Username"))
2058 cupsdSetString(&job
->username
, value
);
2060 else if (!strcasecmp(line
, "Destination"))
2062 cupsdSetString(&job
->dest
, value
);
2064 else if (!strcasecmp(line
, "DestType"))
2066 job
->dtype
= (cups_ptype_t
)atoi(value
);
2068 else if (!strcasecmp(line
, "NumFiles"))
2070 job
->num_files
= atoi(value
);
2072 if (job
->num_files
< 0)
2074 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad NumFiles value %d on line %d!",
2075 job
->num_files
, linenum
);
2080 if (job
->num_files
> 0)
2082 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-001", RequestRoot
,
2084 if (access(jobfile
, 0))
2086 cupsdLogMessage(CUPSD_LOG_INFO
,
2087 "Data files for job %d have gone away!", job
->id
);
2092 job
->filetypes
= calloc(job
->num_files
, sizeof(mime_type_t
*));
2093 job
->compressions
= calloc(job
->num_files
, sizeof(int));
2095 if (!job
->filetypes
|| !job
->compressions
)
2097 cupsdLogMessage(CUPSD_LOG_EMERG
,
2098 "Unable to allocate memory for %d files!",
2104 else if (!strcasecmp(line
, "File"))
2106 int number
, /* File number */
2107 compression
; /* Compression value */
2108 char super
[MIME_MAX_SUPER
], /* MIME super type */
2109 type
[MIME_MAX_TYPE
]; /* MIME type */
2112 if (sscanf(value
, "%d%*[ \t]%15[^/]/%255s%d", &number
, super
, type
,
2115 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File on line %d!", linenum
);
2119 if (number
< 1 || number
> job
->num_files
)
2121 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File number %d on line %d!",
2128 job
->compressions
[number
] = compression
;
2129 job
->filetypes
[number
] = mimeType(MimeDatabase
, super
, type
);
2131 if (!job
->filetypes
[number
])
2134 * If the original MIME type is unknown, auto-type it!
2137 cupsdLogMessage(CUPSD_LOG_ERROR
,
2138 "Unknown MIME type %s/%s for file %d of job %d!",
2139 super
, type
, number
+ 1, job
->id
);
2141 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
2142 job
->id
, number
+ 1);
2143 job
->filetypes
[number
] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
2144 job
->compressions
+ number
);
2147 * If that didn't work, assume it is raw...
2150 if (!job
->filetypes
[number
])
2151 job
->filetypes
[number
] = mimeType(MimeDatabase
, "application",
2156 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unknown %s directive on line %d!",
2165 * 'load_next_job_id()' - Load the NextJobId value from the job.cache file.
2169 load_next_job_id(const char *filename
) /* I - job.cache filename */
2171 cups_file_t
*fp
; /* job.cache file */
2172 char line
[1024], /* Line buffer */
2173 *value
; /* Value on line */
2174 int linenum
; /* Line number in file */
2175 int next_job_id
; /* NextJobId value from line */
2179 * Read the NextJobId directive from the job.cache file and use
2180 * the value (if any).
2183 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
2185 if (errno
!= ENOENT
)
2186 cupsdLogMessage(CUPSD_LOG_ERROR
,
2187 "Unable to open job cache file \"%s\": %s",
2188 filename
, strerror(errno
));
2193 cupsdLogMessage(CUPSD_LOG_INFO
,
2194 "Loading NextJobId from job cache file \"%s\"...", filename
);
2198 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
2200 if (!strcasecmp(line
, "NextJobId"))
2204 next_job_id
= atoi(value
);
2206 if (next_job_id
> NextJobId
)
2207 NextJobId
= next_job_id
;
2218 * 'load_request_root()' - Load jobs from the RequestRoot directory.
2222 load_request_root(void)
2224 cups_dir_t
*dir
; /* Directory */
2225 cups_dentry_t
*dent
; /* Directory entry */
2226 cupsd_job_t
*job
; /* New job */
2230 * Open the requests directory...
2233 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Scanning %s for jobs...", RequestRoot
);
2235 if ((dir
= cupsDirOpen(RequestRoot
)) == NULL
)
2237 cupsdLogMessage(CUPSD_LOG_ERROR
,
2238 "Unable to open spool directory \"%s\": %s",
2239 RequestRoot
, strerror(errno
));
2244 * Read all the c##### files...
2247 while ((dent
= cupsDirRead(dir
)) != NULL
)
2248 if (strlen(dent
->filename
) >= 6 && dent
->filename
[0] == 'c')
2251 * Allocate memory for the job...
2254 if ((job
= calloc(sizeof(cupsd_job_t
), 1)) == NULL
)
2256 cupsdLogMessage(CUPSD_LOG_ERROR
, "Ran out of memory for jobs!");
2262 * Assign the job ID...
2265 job
->id
= atoi(dent
->filename
+ 1);
2266 job
->back_pipes
[0] = -1;
2267 job
->back_pipes
[1] = -1;
2268 job
->print_pipes
[0] = -1;
2269 job
->print_pipes
[1] = -1;
2270 job
->status_pipes
[0] = -1;
2271 job
->status_pipes
[1] = -1;
2273 if (job
->id
>= NextJobId
)
2274 NextJobId
= job
->id
+ 1;
2283 * Insert the job into the array, sorting by job priority and ID...
2286 cupsArrayAdd(Jobs
, job
);
2288 if (job
->state_value
<= IPP_JOB_STOPPED
)
2289 cupsArrayAdd(ActiveJobs
,job
);
2299 * 'set_time()' - Set one of the "time-at-xyz" attributes...
2303 set_time(cupsd_job_t
*job
, /* I - Job to update */
2304 const char *name
) /* I - Name of attribute */
2306 ipp_attribute_t
*attr
; /* Time attribute */
2309 if ((attr
= ippFindAttribute(job
->attrs
, name
, IPP_TAG_ZERO
)) != NULL
)
2311 attr
->value_tag
= IPP_TAG_INTEGER
;
2312 attr
->values
[0].integer
= time(NULL
);
2318 * 'set_hold_until()' - Set the hold time and update job-hold-until attribute...
2322 set_hold_until(cupsd_job_t
*job
, /* I - Job to update */
2323 time_t holdtime
) /* I - Hold until time */
2325 ipp_attribute_t
*attr
; /* job-hold-until attribute */
2326 struct tm
*holddate
; /* Hold date */
2327 char holdstr
[64]; /* Hold time */
2331 * Set the hold_until value and hold the job...
2334 cupsdLogMessage(CUPSD_LOG_DEBUG
, "set_hold_until: hold_until = %d",
2337 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
2338 job
->state_value
= IPP_JOB_HELD
;
2339 job
->hold_until
= holdtime
;
2342 * Update the job-hold-until attribute with a string representing GMT
2343 * time (HH:MM:SS)...
2346 holddate
= gmtime(&holdtime
);
2347 snprintf(holdstr
, sizeof(holdstr
), "%d:%d:%d", holddate
->tm_hour
,
2348 holddate
->tm_min
, holddate
->tm_sec
);
2350 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2351 IPP_TAG_KEYWORD
)) == NULL
)
2352 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2355 * Either add the attribute or update the value of the existing one
2359 attr
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
2360 "job-hold-until", NULL
, holdstr
);
2362 cupsdSetString(&attr
->values
[0].string
.text
, holdstr
);
2369 * 'start_job()' - Start a print job.
2373 start_job(cupsd_job_t
*job
, /* I - Job ID */
2374 cupsd_printer_t
*printer
) /* I - Printer to print job */
2376 int i
; /* Looping var */
2377 int slot
; /* Pipe slot */
2378 cups_array_t
*filters
; /* Filters for job */
2379 mime_filter_t
*filter
, /* Current filter */
2380 port_monitor
; /* Port monitor filter */
2381 char method
[255], /* Method for output */
2382 *optptr
, /* Pointer to options */
2383 *valptr
; /* Pointer in value string */
2384 ipp_attribute_t
*attr
; /* Current attribute */
2385 struct stat backinfo
; /* Backend file information */
2386 int backroot
; /* Run backend as root? */
2387 int pid
; /* Process ID of new filter process */
2388 int banner_page
; /* 1 if banner page, 0 otherwise */
2389 int filterfds
[2][2];/* Pipes used between filters */
2390 int envc
; /* Number of environment variables */
2391 char **argv
, /* Filter command-line arguments */
2392 sani_uri
[1024], /* Sanitized DEVICE_URI env var */
2393 filename
[1024], /* Job filename */
2394 command
[1024], /* Full path to command */
2395 jobid
[255], /* Job ID string */
2396 title
[IPP_MAX_NAME
],
2397 /* Job title string */
2398 copies
[255], /* # copies string */
2399 *envp
[MAX_ENV
+ 11],
2400 /* Environment variables */
2401 charset
[255], /* CHARSET env variable */
2402 class_name
[255],/* CLASS env variable */
2403 classification
[1024],
2404 /* CLASSIFICATION env variable */
2406 /* CONTENT_TYPE env variable */
2408 /* DEVICE_URI env variable */
2409 final_content_type
[1024],
2410 /* FINAL_CONTENT_TYPE env variable */
2411 lang
[255], /* LANG env variable */
2412 ppd
[1024], /* PPD env variable */
2414 /* PRINTER env variable */
2416 /* RIP_MAX_CACHE env variable */
2417 int remote_job
; /* Remote print job? */
2418 static char *options
= NULL
;/* Full list of options */
2419 static int optlength
= 0; /* Length of option buffer */
2422 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "start_job: id = %d, file = %d/%d",
2423 job
->id
, job
->current_file
, job
->num_files
);
2425 if (job
->num_files
== 0)
2427 cupsdLogMessage(CUPSD_LOG_ERROR
, "Job ID %d has no files! Cancelling it!",
2430 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
2431 "Job canceled because it has no files.");
2433 cupsdCancelJob(job
, 0);
2437 if (printer
->raw
&& !strncmp(printer
->device_uri
, "file:", 5))
2439 cupsdLogMessage(CUPSD_LOG_ERROR
,
2440 "Job ID %d cannot be printed to raw queue pointing to "
2444 strlcpy(printer
->state_message
, "Raw printers cannot use file: devices!",
2445 sizeof(printer
->state_message
));
2446 cupsdStopPrinter(printer
, 1);
2447 cupsdAddPrinterHistory(printer
);
2452 * Figure out what filters are required to convert from
2453 * the source to the destination type...
2462 * Remote jobs and raw queues go directly to the printer without
2466 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2467 "[Job %d] Sending job to queue tagged as raw...", job
->id
);
2474 * Local jobs get filtered...
2477 filters
= mimeFilter(MimeDatabase
, job
->filetypes
[job
->current_file
],
2478 printer
->filetype
, &(job
->cost
));
2482 cupsdLogMessage(CUPSD_LOG_ERROR
,
2483 "Unable to convert file %d to printable format for "
2485 job
->current_file
, job
->id
);
2486 cupsdLogMessage(CUPSD_LOG_INFO
,
2487 "Hint: Do you have ESP Ghostscript installed?");
2489 if (LogLevel
< CUPSD_LOG_DEBUG
)
2490 cupsdLogMessage(CUPSD_LOG_INFO
,
2491 "Hint: Try setting the LogLevel to \"debug\".");
2493 job
->current_file
++;
2495 if (job
->current_file
== job
->num_files
)
2497 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
2498 "Job canceled because it has no files that can be "
2501 cupsdCancelJob(job
, 0);
2508 * Remove NULL ("-") filters...
2511 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
2513 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
2514 if (!strcmp(filter
->filter
, "-"))
2515 cupsArrayRemove(filters
, filter
);
2517 if (cupsArrayCount(filters
) == 0)
2519 cupsArrayDelete(filters
);
2525 * Set a minimum cost of 100 for all jobs so that FilterLimit
2526 * works with raw queues and other low-cost paths.
2529 if (job
->cost
< 100)
2533 * See if the filter cost is too high...
2536 if ((FilterLevel
+ job
->cost
) > FilterLimit
&& FilterLevel
> 0 &&
2540 * Don't print this job quite yet...
2543 cupsArrayDelete(filters
);
2545 cupsdLogMessage(CUPSD_LOG_INFO
,
2546 "Holding job %d because filter limit has been reached.",
2548 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2549 "start_job: id=%d, file=%d, cost=%d, level=%d, "
2551 job
->id
, job
->current_file
, job
->cost
, FilterLevel
,
2556 FilterLevel
+= job
->cost
;
2559 * Determine if we are printing to a remote printer...
2562 remote_job
= printer
->raw
&& job
->num_files
> 1 &&
2563 !strncmp(printer
->device_uri
, "ipp://", 6);
2566 * Add decompression filters, if any...
2569 if (!remote_job
&& job
->compressions
[job
->current_file
])
2572 * Add gziptoany filter to the front of the list...
2575 if (!cupsArrayInsert(filters
, &gziptoany_filter
))
2577 cupsdLogMessage(CUPSD_LOG_ERROR
,
2578 "Unable to add decompression filter - %s",
2581 cupsArrayDelete(filters
);
2583 job
->current_file
++;
2585 if (job
->current_file
== job
->num_files
)
2587 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
2588 "Job canceled because the print file could not be "
2591 cupsdCancelJob(job
, 0);
2599 * Add port monitor, if any...
2602 if (printer
->port_monitor
)
2605 * Add port monitor to the end of the list...
2608 if (!cupsArrayAdd(filters
, &port_monitor
))
2610 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to add port monitor - %s",
2613 cupsArrayDelete(filters
);
2615 job
->current_file
++;
2617 if (job
->current_file
== job
->num_files
)
2619 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
2620 "Job canceled because the port monitor could not be "
2623 cupsdCancelJob(job
, 0);
2629 snprintf(port_monitor
.filter
, sizeof(port_monitor
.filter
),
2630 "%s/monitor/%s", ServerBin
, printer
->port_monitor
);
2634 * Update the printer and job state to "processing"...
2637 job
->state
->values
[0].integer
= IPP_JOB_PROCESSING
;
2638 job
->state_value
= IPP_JOB_PROCESSING
;
2640 job
->printer
= printer
;
2642 cupsdSetPrinterState(printer
, IPP_PRINTER_PROCESSING
, 0);
2644 if (job
->current_file
== 0)
2647 * Set the processing time...
2650 set_time(job
, "time-at-processing");
2653 * Create the backchannel pipes and make them non-blocking...
2656 cupsdOpenPipe(job
->back_pipes
);
2658 fcntl(job
->back_pipes
[0], F_SETFL
,
2659 fcntl(job
->back_pipes
[0], F_GETFL
) | O_NONBLOCK
);
2661 fcntl(job
->back_pipes
[1], F_SETFL
,
2662 fcntl(job
->back_pipes
[1], F_GETFL
) | O_NONBLOCK
);
2666 * Determine if we are printing a banner page or not...
2669 if (job
->job_sheets
== NULL
)
2671 cupsdLogMessage(CUPSD_LOG_DEBUG
, "No job-sheets attribute.");
2672 if ((job
->job_sheets
=
2673 ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_ZERO
)) != NULL
)
2674 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2675 "... but someone added one without setting job_sheets!");
2677 else if (job
->job_sheets
->num_values
== 1)
2678 cupsdLogMessage(CUPSD_LOG_DEBUG
, "job-sheets=%s",
2679 job
->job_sheets
->values
[0].string
.text
);
2681 cupsdLogMessage(CUPSD_LOG_DEBUG
, "job-sheets=%s,%s",
2682 job
->job_sheets
->values
[0].string
.text
,
2683 job
->job_sheets
->values
[1].string
.text
);
2685 if (printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
))
2687 else if (job
->job_sheets
== NULL
)
2689 else if (strcasecmp(job
->job_sheets
->values
[0].string
.text
, "none") != 0 &&
2690 job
->current_file
== 0)
2692 else if (job
->job_sheets
->num_values
> 1 &&
2693 strcasecmp(job
->job_sheets
->values
[1].string
.text
, "none") != 0 &&
2694 job
->current_file
== (job
->num_files
- 1))
2699 cupsdLogMessage(CUPSD_LOG_DEBUG
, "banner_page = %d", banner_page
);
2702 * Building the options string is harder than it needs to be, but
2703 * for the moment we need to pass strings for command-line args and
2704 * not IPP attribute pointers... :)
2706 * First allocate/reallocate the option buffer as needed...
2709 i
= ipp_length(job
->attrs
);
2716 optptr
= realloc(options
, i
);
2720 cupsdLogMessage(CUPSD_LOG_CRIT
,
2721 "Unable to allocate %d bytes for option buffer for "
2722 "job %d!", i
, job
->id
);
2724 cupsArrayDelete(filters
);
2726 FilterLevel
-= job
->cost
;
2728 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
2729 "Job canceled because the server ran out of memory.");
2731 cupsdCancelJob(job
, 0);
2740 * Now loop through the attributes and convert them to the textual
2741 * representation used by the filters...
2747 snprintf(title
, sizeof(title
), "%s-%d", printer
->name
, job
->id
);
2748 strcpy(copies
, "1");
2750 for (attr
= job
->attrs
->attrs
; attr
!= NULL
; attr
= attr
->next
)
2752 if (!strcmp(attr
->name
, "copies") &&
2753 attr
->value_tag
== IPP_TAG_INTEGER
)
2756 * Don't use the # copies attribute if we are printing the job sheets...
2760 sprintf(copies
, "%d", attr
->values
[0].integer
);
2762 else if (!strcmp(attr
->name
, "job-name") &&
2763 (attr
->value_tag
== IPP_TAG_NAME
||
2764 attr
->value_tag
== IPP_TAG_NAMELANG
))
2765 strlcpy(title
, attr
->values
[0].string
.text
, sizeof(title
));
2766 else if (attr
->group_tag
== IPP_TAG_JOB
)
2769 * Filter out other unwanted attributes...
2772 if (attr
->value_tag
== IPP_TAG_MIMETYPE
||
2773 attr
->value_tag
== IPP_TAG_NAMELANG
||
2774 attr
->value_tag
== IPP_TAG_TEXTLANG
||
2775 (attr
->value_tag
== IPP_TAG_URI
&& strcmp(attr
->name
, "job-uuid")) ||
2776 attr
->value_tag
== IPP_TAG_URISCHEME
||
2777 attr
->value_tag
== IPP_TAG_BEGIN_COLLECTION
) /* Not yet supported */
2780 if (!strncmp(attr
->name
, "time-", 5))
2783 if (!strncmp(attr
->name
, "job-", 4) && strcmp(attr
->name
, "job-uuid") &&
2784 !(printer
->type
& CUPS_PRINTER_REMOTE
))
2787 if (!strncmp(attr
->name
, "job-", 4) &&
2788 strcmp(attr
->name
, "job-uuid") &&
2789 strcmp(attr
->name
, "job-billing") &&
2790 strcmp(attr
->name
, "job-sheets") &&
2791 strcmp(attr
->name
, "job-hold-until") &&
2792 strcmp(attr
->name
, "job-priority"))
2795 if ((!strcmp(attr
->name
, "page-label") ||
2796 !strcmp(attr
->name
, "page-border") ||
2797 !strncmp(attr
->name
, "number-up", 9) ||
2798 !strcmp(attr
->name
, "page-set") ||
2799 !strcasecmp(attr
->name
, "AP_FIRSTPAGE_InputSlot") ||
2800 !strcasecmp(attr
->name
, "AP_FIRSTPAGE_ManualFeed")) &&
2805 * Otherwise add them to the list...
2808 if (optptr
> options
)
2809 strlcat(optptr
, " ", optlength
- (optptr
- options
));
2811 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
2813 strlcat(optptr
, attr
->name
, optlength
- (optptr
- options
));
2814 strlcat(optptr
, "=", optlength
- (optptr
- options
));
2817 for (i
= 0; i
< attr
->num_values
; i
++)
2820 strlcat(optptr
, ",", optlength
- (optptr
- options
));
2822 optptr
+= strlen(optptr
);
2824 switch (attr
->value_tag
)
2826 case IPP_TAG_INTEGER
:
2828 snprintf(optptr
, optlength
- (optptr
- options
),
2829 "%d", attr
->values
[i
].integer
);
2832 case IPP_TAG_BOOLEAN
:
2833 if (!attr
->values
[i
].boolean
)
2834 strlcat(optptr
, "no", optlength
- (optptr
- options
));
2836 case IPP_TAG_NOVALUE
:
2837 strlcat(optptr
, attr
->name
,
2838 optlength
- (optptr
- options
));
2841 case IPP_TAG_RANGE
:
2842 if (attr
->values
[i
].range
.lower
== attr
->values
[i
].range
.upper
)
2843 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2844 "%d", attr
->values
[i
].range
.lower
);
2846 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2847 "%d-%d", attr
->values
[i
].range
.lower
,
2848 attr
->values
[i
].range
.upper
);
2851 case IPP_TAG_RESOLUTION
:
2852 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
2853 "%dx%d%s", attr
->values
[i
].resolution
.xres
,
2854 attr
->values
[i
].resolution
.yres
,
2855 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
2859 case IPP_TAG_STRING
:
2862 case IPP_TAG_KEYWORD
:
2863 case IPP_TAG_CHARSET
:
2864 case IPP_TAG_LANGUAGE
:
2866 for (valptr
= attr
->values
[i
].string
.text
; *valptr
;)
2868 if (strchr(" \t\n\\\'\"", *valptr
))
2870 *optptr
++ = *valptr
++;
2877 break; /* anti-compiler-warning-code */
2881 optptr
+= strlen(optptr
);
2886 * Build the command-line arguments for the filters. Each filter
2887 * has 6 or 7 arguments:
2891 * argv[2] = username
2893 * argv[4] = # copies
2895 * argv[6] = filename (optional; normally stdin)
2897 * This allows legacy printer drivers that use the old System V
2898 * printing interface to be used by CUPS.
2900 * For remote jobs, we send all of the files in the argument list.
2904 argv
= calloc(7 + job
->num_files
, sizeof(char *));
2906 argv
= calloc(8, sizeof(char *));
2908 sprintf(jobid
, "%d", job
->id
);
2910 argv
[0] = printer
->name
;
2912 argv
[2] = job
->username
;
2919 for (i
= 0; i
< job
->num_files
; i
++)
2921 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
2923 argv
[6 + i
] = strdup(filename
);
2928 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
2929 job
->id
, job
->current_file
+ 1);
2933 for (i
= 0; argv
[i
]; i
++)
2934 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2935 "[Job %d] argv[%d]=\"%s\"", job
->id
, i
, argv
[i
]);
2938 * Create environment variable strings for the filters...
2941 attr
= ippFindAttribute(job
->attrs
, "attributes-natural-language",
2944 switch (strlen(attr
->values
[0].string
.text
))
2948 * This is an unknown or badly formatted language code; use
2949 * the POSIX locale...
2952 strcpy(lang
, "LANG=C");
2957 * Just the language code (ll)...
2960 snprintf(lang
, sizeof(lang
), "LANG=%s",
2961 attr
->values
[0].string
.text
);
2966 * Language and country code (ll-cc)...
2969 snprintf(lang
, sizeof(lang
), "LANG=%c%c_%c%c",
2970 attr
->values
[0].string
.text
[0],
2971 attr
->values
[0].string
.text
[1],
2972 toupper(attr
->values
[0].string
.text
[3] & 255),
2973 toupper(attr
->values
[0].string
.text
[4] & 255));
2977 attr
= ippFindAttribute(job
->attrs
, "document-format",
2980 (optptr
= strstr(attr
->values
[0].string
.text
, "charset=")) != NULL
)
2981 snprintf(charset
, sizeof(charset
), "CHARSET=%s", optptr
+ 8);
2984 attr
= ippFindAttribute(job
->attrs
, "attributes-charset",
2986 snprintf(charset
, sizeof(charset
), "CHARSET=%s",
2987 attr
->values
[0].string
.text
);
2990 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s/%s",
2991 job
->filetypes
[job
->current_file
]->super
,
2992 job
->filetypes
[job
->current_file
]->type
);
2993 snprintf(device_uri
, sizeof(device_uri
), "DEVICE_URI=%s",
2994 printer
->device_uri
);
2995 cupsdSanitizeURI(printer
->device_uri
, sani_uri
, sizeof(sani_uri
));
2996 snprintf(ppd
, sizeof(ppd
), "PPD=%s/ppd/%s.ppd", ServerRoot
, printer
->name
);
2997 snprintf(printer_name
, sizeof(printer_name
), "PRINTER=%s", printer
->name
);
2998 snprintf(rip_max_cache
, sizeof(rip_max_cache
), "RIP_MAX_CACHE=%s", RIPCache
);
3000 envc
= cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
3002 envp
[envc
++] = charset
;
3003 envp
[envc
++] = lang
;
3004 envp
[envc
++] = ppd
;
3005 envp
[envc
++] = rip_max_cache
;
3006 envp
[envc
++] = content_type
;
3007 envp
[envc
++] = device_uri
;
3008 envp
[envc
++] = printer_name
;
3010 if ((filter
= (mime_filter_t
*)cupsArrayLast(filters
)) != NULL
)
3012 snprintf(final_content_type
, sizeof(final_content_type
),
3013 "FINAL_CONTENT_TYPE=%s/%s",
3014 filter
->dst
->super
, filter
->dst
->type
);
3015 envp
[envc
++] = final_content_type
;
3018 if (Classification
&& !banner_page
)
3020 if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
3021 IPP_TAG_NAME
)) == NULL
)
3022 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3024 else if (attr
->num_values
> 1 &&
3025 strcmp(attr
->values
[1].string
.text
, "none") != 0)
3026 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3027 attr
->values
[1].string
.text
);
3029 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3030 attr
->values
[0].string
.text
);
3032 envp
[envc
++] = classification
;
3035 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
3037 snprintf(class_name
, sizeof(class_name
), "CLASS=%s", job
->dest
);
3038 envp
[envc
++] = class_name
;
3043 for (i
= 0; i
< envc
; i
++)
3044 if (strncmp(envp
[i
], "DEVICE_URI=", 11))
3045 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] envp[%d]=\"%s\"",
3046 job
->id
, i
, envp
[i
]);
3048 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] envp[%d]=\"DEVICE_URI=%s\"",
3049 job
->id
, i
, sani_uri
);
3052 job
->current_file
= job
->num_files
;
3054 job
->current_file
++;
3057 * Now create processes for all of the filters...
3060 filterfds
[0][0] = -1;
3061 filterfds
[0][1] = -1;
3062 filterfds
[1][0] = -1;
3063 filterfds
[1][1] = -1;
3065 if (!job
->status_buffer
)
3067 if (cupsdOpenPipe(job
->status_pipes
))
3069 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create job status pipes - %s.",
3071 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3072 "Unable to create status pipes - %s.", strerror(errno
));
3074 cupsdAddPrinterHistory(printer
);
3076 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3077 "Job canceled because the server could not create the job "
3083 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "start_job: status_pipes = [ %d %d ]",
3084 job
->status_pipes
[0], job
->status_pipes
[1]);
3086 job
->status_buffer
= cupsdStatBufNew(job
->status_pipes
[0], "[Job %d]",
3091 memset(job
->filters
, 0, sizeof(job
->filters
));
3093 filterfds
[1][0] = open("/dev/null", O_RDONLY
);
3095 if (filterfds
[1][0] < 0)
3097 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"/dev/null\" - %s.",
3099 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3100 "Unable to open \"/dev/null\" - %s.", strerror(errno
));
3102 cupsdAddPrinterHistory(printer
);
3104 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3105 "Job canceled because the server could not open /dev/null.");
3110 fcntl(filterfds
[1][0], F_SETFD
, fcntl(filterfds
[1][0], F_GETFD
) | FD_CLOEXEC
);
3112 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "start_job: filterfds[%d] = [ %d %d ]",
3113 1, filterfds
[1][0], filterfds
[1][1]);
3115 for (i
= 0, slot
= 0, filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
3117 i
++, filter
= (mime_filter_t
*)cupsArrayNext(filters
))
3119 if (filter
->filter
[0] != '/')
3120 snprintf(command
, sizeof(command
), "%s/filter/%s", ServerBin
,
3123 strlcpy(command
, filter
->filter
, sizeof(command
));
3125 if (i
< (cupsArrayCount(filters
) - 1))
3127 if (cupsdOpenPipe(filterfds
[slot
]))
3129 cupsdLogMessage(CUPSD_LOG_ERROR
,
3130 "Unable to create job filter pipes - %s.",
3132 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3133 "Unable to create filter pipes - %s.", strerror(errno
));
3134 cupsdAddPrinterHistory(printer
);
3136 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3137 "Job canceled because the server could not create the "
3145 if (job
->current_file
== 1)
3147 if (strncmp(printer
->device_uri
, "file:", 5) != 0)
3149 if (cupsdOpenPipe(job
->print_pipes
))
3151 cupsdLogMessage(CUPSD_LOG_ERROR
,
3152 "Unable to create job backend pipes - %s.",
3154 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3155 "Unable to create backend pipes - %s.", strerror(errno
));
3156 cupsdAddPrinterHistory(printer
);
3158 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3159 "Job canceled because the server could not create "
3160 "the backend pipes.");
3167 job
->print_pipes
[0] = -1;
3168 if (!strncmp(printer
->device_uri
, "file:/dev/", 10) &&
3169 strcmp(printer
->device_uri
, "file:/dev/null"))
3170 job
->print_pipes
[1] = open(printer
->device_uri
+ 5,
3172 else if (!strncmp(printer
->device_uri
, "file:///dev/", 12) &&
3173 strcmp(printer
->device_uri
, "file:///dev/null"))
3174 job
->print_pipes
[1] = open(printer
->device_uri
+ 7,
3177 job
->print_pipes
[1] = open(printer
->device_uri
+ 5,
3178 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
3180 if (job
->print_pipes
[1] < 0)
3182 cupsdLogMessage(CUPSD_LOG_ERROR
,
3183 "Unable to open output file \"%s\" - %s.",
3184 printer
->device_uri
, strerror(errno
));
3185 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3186 "Unable to open output file \"%s\" - %s.",
3187 printer
->device_uri
, strerror(errno
));
3189 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3190 "Job canceled because the server could not open the "
3196 fcntl(job
->print_pipes
[1], F_SETFD
,
3197 fcntl(job
->print_pipes
[1], F_GETFD
) | FD_CLOEXEC
);
3200 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3201 "start_job: print_pipes = [ %d %d ]",
3202 job
->print_pipes
[0], job
->print_pipes
[1]);
3205 filterfds
[slot
][0] = job
->print_pipes
[0];
3206 filterfds
[slot
][1] = job
->print_pipes
[1];
3209 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "start_job: filter=\"%s\"",
3211 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3212 "start_job: filterfds[%d]=[ %d %d ]",
3213 slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3215 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
3216 filterfds
[slot
][1], job
->status_pipes
[1],
3217 job
->back_pipes
[0], 0, job
->filters
+ i
);
3219 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3220 "start_job: Closing filter pipes for slot %d "
3222 !slot
, filterfds
[!slot
][0], filterfds
[!slot
][1]);
3224 cupsdClosePipe(filterfds
[!slot
]);
3228 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to start filter \"%s\" - %s.",
3229 filter
->filter
, strerror(errno
));
3230 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3231 "Unable to start filter \"%s\" - %s.",
3232 filter
->filter
, strerror(errno
));
3234 cupsdAddPrinterHistory(printer
);
3236 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3237 "Job canceled because the server could not execute a "
3243 cupsdLogMessage(CUPSD_LOG_INFO
, "Started filter %s (PID %d) for job %d.",
3244 command
, pid
, job
->id
);
3250 cupsArrayDelete(filters
);
3253 * Finally, pipe the final output into a backend process if needed...
3256 if (strncmp(printer
->device_uri
, "file:", 5) != 0)
3258 if (job
->current_file
== 1)
3260 sscanf(printer
->device_uri
, "%254[^:]", method
);
3261 snprintf(command
, sizeof(command
), "%s/backend/%s", ServerBin
, method
);
3264 * See if the backend needs to run as root...
3269 else if (stat(command
, &backinfo
))
3272 backroot
= !(backinfo
.st_mode
& (S_IRWXG
| S_IRWXO
));
3276 filterfds
[slot
][0] = -1;
3277 filterfds
[slot
][1] = open("/dev/null", O_WRONLY
);
3279 if (filterfds
[slot
][1] < 0)
3281 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"/dev/null\" - %s.",
3283 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3284 "Unable to open \"/dev/null\" - %s.", strerror(errno
));
3286 cupsdAddPrinterHistory(printer
);
3288 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3289 "Job canceled because the server could not open a file.");
3294 fcntl(filterfds
[slot
][1], F_SETFD
,
3295 fcntl(filterfds
[slot
][1], F_GETFD
) | FD_CLOEXEC
);
3297 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "start_job: backend=\"%s\"",
3299 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3300 "start_job: filterfds[%d] = [ %d %d ]",
3301 slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3303 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
3304 filterfds
[slot
][1], job
->status_pipes
[1],
3305 job
->back_pipes
[1], backroot
,
3310 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to start backend \"%s\" - %s.",
3311 method
, strerror(errno
));
3312 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3313 "Unable to start backend \"%s\" - %s.", method
,
3316 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3317 "Job canceled because the server could not execute "
3324 cupsdLogMessage(CUPSD_LOG_INFO
,
3325 "Started backend %s (PID %d) for job %d.",
3326 command
, pid
, job
->id
);
3330 if (job
->current_file
== job
->num_files
)
3332 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3333 "start_job: Closing print pipes [ %d %d ]...",
3334 job
->print_pipes
[0], job
->print_pipes
[1]);
3336 cupsdClosePipe(job
->print_pipes
);
3338 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3339 "start_job: Closing back pipes [ %d %d ]...",
3340 job
->back_pipes
[0], job
->back_pipes
[1]);
3342 cupsdClosePipe(job
->back_pipes
);
3344 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3345 "start_job: Closing status output pipe %d...",
3346 job
->status_pipes
[1]);
3348 close(job
->status_pipes
[1]);
3349 job
->status_pipes
[1] = -1;
3354 filterfds
[slot
][0] = -1;
3355 filterfds
[slot
][1] = -1;
3357 if (job
->current_file
== job
->num_files
)
3359 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3360 "start_job: Closing print pipes [ %d %d ]...",
3361 job
->print_pipes
[0], job
->print_pipes
[1]);
3363 cupsdClosePipe(job
->print_pipes
);
3365 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3366 "start_job: Closing status output pipe %d...",
3367 job
->status_pipes
[1]);
3369 close(job
->status_pipes
[1]);
3370 job
->status_pipes
[1] = -1;
3374 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3375 "start_job: Closing filter pipes for slot %d "
3377 slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3378 cupsdClosePipe(filterfds
[slot
]);
3382 for (i
= 0; i
< job
->num_files
; i
++)
3388 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3389 "start_job: Adding fd %d to InputSet...",
3390 job
->status_buffer
->fd
);
3392 FD_SET(job
->status_buffer
->fd
, InputSet
);
3394 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
, "Job #%d started.",
3401 * If we get here, we need to abort the current job and close out all
3402 * files and pipes...
3407 for (slot
= 0; slot
< 2; slot
++)
3409 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3410 "start_job: Closing filter pipes for slot %d "
3412 slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3413 cupsdClosePipe(filterfds
[slot
]);
3416 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3417 "start_job: Closing status pipes [ %d %d ]...",
3418 job
->status_pipes
[0], job
->status_pipes
[1]);
3419 cupsdClosePipe(job
->status_pipes
);
3420 cupsdStatBufDelete(job
->status_buffer
);
3422 cupsArrayDelete(filters
);
3426 for (i
= 0; i
< job
->num_files
; i
++)
3432 cupsdStopJob(job
, 0);
3437 * 'unload_job()' - Unload a job from memory.
3441 unload_job(cupsd_job_t
*job
) /* I - Job */
3446 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Unloading job %d...", job
->id
);
3448 ippDelete(job
->attrs
);
3453 job
->job_sheets
= NULL
;