2 * "$Id: job.c 7902 2008-09-03 14:20:17Z mike $"
4 * Job management routines for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2009 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
17 * cupsdAddJob() - Add a new job to the job queue...
18 * cupsdCancelJob() - Cancel the specified print job.
19 * cupsdCancelJobs() - Cancel all jobs for the given
21 * cupsdCheckJobs() - Check the pending jobs and start any if the
22 * destination is available.
23 * cupsdCleanJobs() - Clean out old jobs.
24 * cupsdDeleteJob() - Free all memory used by a job.
25 * cupsdFinishJob() - Finish a job.
26 * cupsdFreeAllJobs() - Free all jobs from memory.
27 * cupsdFindJob() - Find the specified job.
28 * cupsdGetPrinterJobCount() - Get the number of pending, processing, or held
29 * jobs in a printer or class.
30 * cupsdGetUserJobCount() - Get the number of pending, processing, or held
32 * cupsdHoldJob() - Hold the specified job.
33 * cupsdLoadAllJobs() - Load all jobs from disk.
34 * cupsdLoadJob() - Load a single job...
35 * cupsdMoveJob() - Move the specified job to a different
37 * cupsdReleaseJob() - Release the specified job.
38 * cupsdRestartJob() - Restart the specified job.
39 * cupsdSaveAllJobs() - Save a summary of all jobs to disk.
40 * cupsdSaveJob() - Save a job to disk.
41 * cupsdSetJobHoldUntil() - Set the hold time for a job...
42 * cupsdSetJobPriority() - Set the priority of a job, moving it up/down
43 * in the list as needed.
44 * cupsdStopAllJobs() - Stop all print jobs.
45 * cupsdStopJob() - Stop a print job.
46 * cupsdUnloadCompletedJobs() - Flush completed job history from memory.
47 * compare_active_jobs() - Compare the job IDs and priorities of two
49 * compare_jobs() - Compare the job IDs of two jobs.
50 * ipp_length() - Compute the size of the buffer needed to hold
51 * the textual IPP attributes.
52 * load_job_cache() - Load jobs from the job.cache file.
53 * load_next_job_id() - Load the NextJobId value from the job.cache
55 * load_request_root() - Load jobs from the RequestRoot directory.
56 * set_time() - Set one of the "time-at-xyz" attributes...
57 * set_hold_until() - Set the hold time and update job-hold-until
59 * start_job() - Start a print job.
60 * unload_job() - Unload a job from memory.
61 * update_job() - Read a status update from a job's filters.
62 * update_job_attrs() - Update the job-printer-* attributes.
66 * Include necessary headers...
71 #include <cups/backend.h>
79 static mime_filter_t gziptoany_filter
=
81 NULL
, /* Source type */
82 NULL
, /* Destination type */
84 "gziptoany" /* Filter program to run */
92 static int compare_active_jobs(void *first
, void *second
, void *data
);
93 static int compare_jobs(void *first
, void *second
, void *data
);
94 static int ipp_length(ipp_t
*ipp
);
95 static void load_job_cache(const char *filename
);
96 static void load_next_job_id(const char *filename
);
97 static void load_request_root(void);
98 static void set_time(cupsd_job_t
*job
, const char *name
);
99 static void set_hold_until(cupsd_job_t
*job
, time_t holdtime
);
100 static void start_job(cupsd_job_t
*job
, cupsd_printer_t
*printer
);
101 static void unload_job(cupsd_job_t
*job
);
102 static void update_job(cupsd_job_t
*job
);
103 static void update_job_attrs(cupsd_job_t
*job
, int do_message
);
107 * 'cupsdAddJob()' - Add a new job to the job queue...
110 cupsd_job_t
* /* O - New job record */
111 cupsdAddJob(int priority
, /* I - Job priority */
112 const char *dest
) /* I - Job destination */
114 cupsd_job_t
*job
; /* New job record */
117 if ((job
= calloc(sizeof(cupsd_job_t
), 1)) == NULL
)
120 job
->id
= NextJobId
++;
121 job
->priority
= priority
;
122 job
->back_pipes
[0] = -1;
123 job
->back_pipes
[1] = -1;
124 job
->print_pipes
[0] = -1;
125 job
->print_pipes
[1] = -1;
126 job
->side_pipes
[0] = -1;
127 job
->side_pipes
[1] = -1;
128 job
->status_pipes
[0] = -1;
129 job
->status_pipes
[1] = -1;
131 cupsdSetString(&job
->dest
, dest
);
134 * Add the new job to the "all jobs" and "active jobs" lists...
137 cupsArrayAdd(Jobs
, job
);
138 cupsArrayAdd(ActiveJobs
, job
);
145 * 'cupsdCancelJob()' - Cancel the specified print job.
149 cupsdCancelJob(cupsd_job_t
*job
, /* I - Job to cancel */
150 int purge
, /* I - Purge jobs? */
151 ipp_jstate_t newstate
) /* I - New job state */
153 int i
; /* Looping var */
154 char filename
[1024]; /* Job filename */
155 cupsd_printer_t
*printer
; /* Printer used by job */
158 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdCancelJob: id = %d", job
->id
);
161 * Stop any processes that are working on the current job...
164 printer
= job
->printer
;
166 if (job
->state_value
== IPP_JOB_PROCESSING
)
167 cupsdStopJob(job
, 0);
172 job
->state
->values
[0].integer
= newstate
;
174 job
->state_value
= newstate
;
176 set_time(job
, "time-at-completed");
179 * Send any pending notifications and then expire them...
187 case IPP_JOB_CANCELED
:
188 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, printer
, job
,
189 purge
? "Job purged." : "Job canceled.");
192 case IPP_JOB_ABORTED
:
193 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, printer
, job
,
194 "Job aborted; please consult the error_log file "
198 case IPP_JOB_COMPLETED
:
200 * Clear the printer's printer-state-message and move on...
203 printer
->state_message
[0] = '\0';
205 cupsdSetPrinterState(printer
, IPP_PRINTER_IDLE
, 0);
207 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, printer
, job
,
212 cupsdExpireSubscriptions(NULL
, job
);
215 * Remove the job from the active and printing lists...
218 cupsArrayRemove(ActiveJobs
, job
);
219 cupsArrayRemove(PrintingJobs
, job
);
223 * If we are going to sleep and the PrintingJobs count is now 0, allow the
224 * sleep to happen immediately...
227 if (Sleeping
&& cupsArrayCount(PrintingJobs
) == 0)
229 #endif /* __APPLE__ */
232 * Remove any authentication data...
235 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
236 if (cupsdRemoveFile(filename
) && errno
!= ENOENT
)
237 cupsdLogMessage(CUPSD_LOG_ERROR
,
238 "Unable to remove authentication cache: %s",
241 cupsdClearString(&job
->auth_username
);
242 cupsdClearString(&job
->auth_domain
);
243 cupsdClearString(&job
->auth_password
);
247 * Destroy the credential cache and clear the KRB5CCNAME env var string.
252 krb5_cc_destroy(KerberosContext
, job
->ccache
);
256 cupsdClearString(&job
->ccname
);
257 #endif /* HAVE_GSSAPI */
260 * Remove the print file for good if we aren't preserving jobs or
264 job
->current_file
= 0;
266 if (!JobHistory
|| !JobFiles
|| purge
)
268 for (i
= 1; i
<= job
->num_files
; i
++)
270 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
275 if (job
->num_files
> 0)
277 free(job
->filetypes
);
278 free(job
->compressions
);
281 job
->filetypes
= NULL
;
282 job
->compressions
= NULL
;
286 if (JobHistory
&& !purge
)
289 * Save job state info...
293 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
298 * Remove the job info file...
301 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
,
306 * Remove the job from the "all jobs" list...
309 cupsArrayRemove(Jobs
, job
);
312 * Free all memory used...
323 * 'cupsdCancelJobs()' - Cancel all jobs for the given destination/user...
327 cupsdCancelJobs(const char *dest
, /* I - Destination to cancel */
328 const char *username
, /* I - Username or NULL */
329 int purge
) /* I - Purge jobs? */
331 cupsd_job_t
*job
; /* Current job */
334 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
336 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
338 if (!job
->dest
|| !job
->username
)
341 if (!job
->dest
|| !job
->username
)
344 if ((dest
== NULL
|| !strcmp(job
->dest
, dest
)) &&
345 (username
== NULL
|| !strcmp(job
->username
, username
)))
348 * Cancel all jobs matching this destination/user...
351 cupsdCancelJob(job
, purge
, IPP_JOB_CANCELED
);
360 * 'cupsdCheckJobs()' - Check the pending jobs and start any if the destination
367 cupsd_job_t
*job
; /* Current job in queue */
368 cupsd_printer_t
*printer
, /* Printer destination */
369 *pclass
; /* Printer class destination */
370 ipp_attribute_t
*attr
; /* Job attribute */
373 DEBUG_puts("cupsdCheckJobs()");
375 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
376 "cupsdCheckJobs: %d active jobs, sleeping=%d, reload=%d",
377 cupsArrayCount(ActiveJobs
), Sleeping
, NeedReload
);
379 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
381 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
384 * Start held jobs if they are ready...
387 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
388 "cupsdCheckJobs: Job %d: dest=%s, dtype=%x, "
389 "state_value=%d, loaded=%s", job
->id
, job
->dest
, job
->dtype
,
390 job
->state_value
, job
->attrs
? "yes" : "no");
392 if (job
->state_value
== IPP_JOB_HELD
&&
394 job
->hold_until
< time(NULL
))
396 if (job
->pending_timeout
)
399 * This job is pending; check that we don't have an active Send-Document
400 * operation in progress on any of the client connections, then timeout
401 * the job so we can start printing...
404 cupsd_client_t
*con
; /* Current client connection */
407 for (con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
409 con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
411 con
->request
->request
.op
.operation_id
== IPP_SEND_DOCUMENT
)
417 if (cupsdTimeoutJob(job
))
421 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
422 job
->state_value
= IPP_JOB_PENDING
;
424 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
425 IPP_TAG_KEYWORD
)) == NULL
)
426 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
430 attr
->value_tag
= IPP_TAG_KEYWORD
;
431 cupsdSetString(&(attr
->values
[0].string
.text
), "no-hold");
433 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
438 * Start pending jobs if the destination is available...
441 if (job
->state_value
== IPP_JOB_PENDING
&& !NeedReload
&& !Sleeping
)
443 printer
= cupsdFindDest(job
->dest
);
447 (printer
->type
& (CUPS_PRINTER_IMPLICIT
| CUPS_PRINTER_CLASS
)))
450 * If the class is remote, just pass it to the remote server...
455 if (pclass
->state
== IPP_PRINTER_STOPPED
)
457 else if (pclass
->type
& CUPS_PRINTER_REMOTE
)
460 printer
= cupsdFindAvailablePrinter(printer
->name
);
463 if (!printer
&& !pclass
)
466 * Whoa, the printer and/or class for this destination went away;
470 cupsdLogMessage(CUPSD_LOG_WARN
,
471 "[Job %d] Printer/class %s has gone away; canceling "
472 "job!", job
->id
, job
->dest
);
474 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
475 "Job canceled because the destination printer/class has "
478 cupsdCancelJob(job
, 1, IPP_JOB_ABORTED
);
480 else if (printer
&& !printer
->holding_new_jobs
)
483 * See if the printer is available or remote and not printing a job;
484 * if so, start the job...
490 * Add/update a job-actual-printer-uri attribute for this job
491 * so that we know which printer actually printed the job...
494 if ((attr
= ippFindAttribute(job
->attrs
, "job-actual-printer-uri",
495 IPP_TAG_URI
)) != NULL
)
496 cupsdSetString(&attr
->values
[0].string
.text
, printer
->uri
);
498 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
,
499 "job-actual-printer-uri", NULL
, printer
->uri
);
502 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
505 if ((!(printer
->type
& CUPS_PRINTER_DISCOVERED
) && /* Printer is local */
506 printer
->state
== IPP_PRINTER_IDLE
) || /* and idle, OR */
507 ((printer
->type
& CUPS_PRINTER_DISCOVERED
) && /* Printer is remote */
508 !printer
->job
)) /* and not printing */
511 * Clear any message for the queue...
514 printer
->state_message
[0] = '\0';
520 start_job(job
, printer
);
529 * 'cupsdCleanJobs()' - Clean out old jobs.
535 cupsd_job_t
*job
; /* Current job */
541 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
542 job
&& cupsArrayCount(Jobs
) >= MaxJobs
;
543 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
544 if (job
->state_value
>= IPP_JOB_CANCELED
)
545 cupsdCancelJob(job
, 1, IPP_JOB_CANCELED
);
550 * 'cupsdDeleteJob()' - Free all memory used by a job.
554 cupsdDeleteJob(cupsd_job_t
*job
) /* I - Job */
556 cupsdClearString(&job
->username
);
557 cupsdClearString(&job
->dest
);
558 cupsdClearString(&job
->auth_username
);
559 cupsdClearString(&job
->auth_domain
);
560 cupsdClearString(&job
->auth_password
);
564 * Destroy the credential cache and clear the KRB5CCNAME env var string.
569 krb5_cc_destroy(KerberosContext
, job
->ccache
);
573 cupsdClearString(&job
->ccname
);
574 #endif /* HAVE_GSSAPI */
576 if (job
->num_files
> 0)
578 free(job
->compressions
);
579 free(job
->filetypes
);
582 ippDelete(job
->attrs
);
589 * 'cupsdFinishJob()' - Finish a job.
593 cupsdFinishJob(cupsd_job_t
*job
) /* I - Job */
595 cupsd_printer_t
*printer
; /* Current printer */
596 ipp_attribute_t
*attr
; /* job-hold-until attribute */
599 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "File %d is complete.",
600 job
->current_file
- 1);
602 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
603 "cupsdFinishJob: job->status is %d",
606 if (job
->status_buffer
&&
607 (job
->status
< 0 || job
->current_file
>= job
->num_files
))
610 * Close the pipe and clear the input bit.
613 cupsdRemoveSelect(job
->status_buffer
->fd
);
615 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
616 "cupsdFinishJob: Closing status pipes [ %d %d ]...",
617 job
->status_pipes
[0], job
->status_pipes
[1]);
619 cupsdClosePipe(job
->status_pipes
);
620 cupsdStatBufDelete(job
->status_buffer
);
622 job
->status_buffer
= NULL
;
625 printer
= job
->printer
;
627 update_job_attrs(job
, 0);
630 * Clear the "connecting-to-device" reason, which is only valid when a
631 * printer is processing...
634 cupsdSetPrinterReasons(printer
, "-connecting-to-device");
637 * Similarly, clear the "offline-report" reason for non-USB devices since we
638 * rarely have current information for network devices...
641 if (strncmp(printer
->device_uri
, "usb:", 4))
642 cupsdSetPrinterReasons(printer
, "-offline-report");
647 * Backend had errors; stop it...
650 int exit_code
; /* Exit code from backend */
654 * Convert the status to an exit code. Due to the way the W* macros are
655 * implemented on MacOS X (bug?), we have to store the exit status in a
656 * variable first and then convert...
659 exit_code
= -job
->status
;
660 if (WIFEXITED(exit_code
))
661 exit_code
= WEXITSTATUS(exit_code
);
663 exit_code
= job
->status
;
665 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Backend returned status %d (%s)",
667 exit_code
== CUPS_BACKEND_FAILED
? "failed" :
668 exit_code
== CUPS_BACKEND_AUTH_REQUIRED
?
669 "authentication required" :
670 exit_code
== CUPS_BACKEND_HOLD
? "hold job" :
671 exit_code
== CUPS_BACKEND_STOP
? "stop printer" :
672 exit_code
== CUPS_BACKEND_CANCEL
? "cancel job" :
673 exit_code
< 0 ? "crashed" : "unknown");
676 * Do what needs to be done...
682 case CUPS_BACKEND_FAILED
:
684 * Backend failure, use the error-policy to determine how to
688 cupsdStopJob(job
, 0);
690 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
693 * Mark the job as pending again - we'll retry on another
697 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
698 job
->state_value
= IPP_JOB_PENDING
;
702 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
705 * If the job was queued to a class or the error policy is
706 * "retry-current-job", try requeuing it... For faxes and retry-job
707 * queues, hold the current job for 5 minutes.
710 if ((job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
)) ||
711 !strcmp(printer
->error_policy
, "retry-current-job"))
713 else if ((printer
->type
& CUPS_PRINTER_FAX
) ||
714 !strcmp(printer
->error_policy
, "retry-job"))
717 * See how many times we've tried to send the job; if more than
718 * the limit, cancel the job.
723 if (job
->tries
>= JobRetryLimit
)
729 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
730 "Canceling job since it could not be "
731 "sent after %d tries.",
734 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
739 * Try again in N seconds...
742 set_hold_until(job
, time(NULL
) + JobRetryInterval
);
744 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
,
745 "Job held due to fax errors; please consult "
746 "the error_log file for details.");
747 cupsdSetPrinterState(printer
, IPP_PRINTER_IDLE
, 0);
750 else if (!strcmp(printer
->error_policy
, "abort-job"))
751 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
754 cupsdSetPrinterState(printer
, IPP_PRINTER_STOPPED
, 1);
756 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
757 "Job stopped due to backend errors; please consult "
758 "the error_log file for details.");
762 case CUPS_BACKEND_CANCEL
:
767 cupsdCancelJob(job
, 0, IPP_JOB_CANCELED
);
770 case CUPS_BACKEND_HOLD
:
775 cupsdStopJob(job
, 0);
777 cupsdSetJobHoldUntil(job
, "indefinite");
779 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
780 job
->state_value
= IPP_JOB_HELD
;
783 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
785 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
786 "Job held due to backend errors; please consult "
787 "the error_log file for details.");
790 case CUPS_BACKEND_STOP
:
792 * Stop the printer...
795 cupsdStopJob(job
, 0);
797 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
798 job
->state_value
= IPP_JOB_PENDING
;
801 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
803 cupsdSetPrinterState(printer
, IPP_PRINTER_STOPPED
, 1);
805 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
806 "Job stopped due to backend errors; please consult "
807 "the error_log file for details.");
810 case CUPS_BACKEND_AUTH_REQUIRED
:
811 cupsdStopJob(job
, 0);
813 cupsdSetJobHoldUntil(job
, "auth-info-required");
815 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
816 IPP_TAG_KEYWORD
)) == NULL
)
817 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
821 attr
->value_tag
= IPP_TAG_KEYWORD
;
822 cupsdSetString(&(attr
->values
[0].string
.text
),
823 "auth-info-required");
826 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
827 job
->state_value
= IPP_JOB_HELD
;
830 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
832 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
833 "Authentication is required for job %d.", job
->id
);
838 * Try printing another job...
843 else if (job
->status
> 0)
846 * Filter had errors; stop job...
849 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Job stopped due to filter errors.");
850 cupsdStopJob(job
, 1);
852 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
853 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, printer
, job
,
854 "Job stopped due to filter errors; please consult the "
855 "error_log file for details.");
861 * Job printed successfully; cancel it...
864 if (job
->current_file
< job
->num_files
)
867 * Start the next file in the job...
870 FilterLevel
-= job
->cost
;
871 start_job(job
, printer
);
876 * Close out this job...
879 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Completed successfully.");
880 cupsdCancelJob(job
, 0, IPP_JOB_COMPLETED
);
888 * 'cupsdFreeAllJobs()' - Free all jobs from memory.
892 cupsdFreeAllJobs(void)
894 cupsd_job_t
*job
; /* Current job */
905 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
907 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
909 cupsArrayRemove(Jobs
, job
);
910 cupsArrayRemove(ActiveJobs
, job
);
911 cupsArrayRemove(PrintingJobs
, job
);
916 cupsdReleaseSignals();
921 * 'cupsdFindJob()' - Find the specified job.
924 cupsd_job_t
* /* O - Job data */
925 cupsdFindJob(int id
) /* I - Job ID */
927 cupsd_job_t key
; /* Search key */
932 return ((cupsd_job_t
*)cupsArrayFind(Jobs
, &key
));
937 * 'cupsdGetPrinterJobCount()' - Get the number of pending, processing,
938 * or held jobs in a printer or class.
941 int /* O - Job count */
942 cupsdGetPrinterJobCount(
943 const char *dest
) /* I - Printer or class name */
945 int count
; /* Job count */
946 cupsd_job_t
*job
; /* Current job */
949 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
951 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
952 if (job
->dest
&& !strcasecmp(job
->dest
, dest
))
960 * 'cupsdGetUserJobCount()' - Get the number of pending, processing,
961 * or held jobs for a user.
964 int /* O - Job count */
965 cupsdGetUserJobCount(
966 const char *username
) /* I - Username */
968 int count
; /* Job count */
969 cupsd_job_t
*job
; /* Current job */
972 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
), count
= 0;
974 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
975 if (!strcasecmp(job
->username
, username
))
983 * 'cupsdHoldJob()' - Hold the specified job.
987 cupsdHoldJob(cupsd_job_t
*job
) /* I - Job data */
989 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdHoldJob: id = %d", job
->id
);
991 if (job
->state_value
== IPP_JOB_PROCESSING
)
992 cupsdStopJob(job
, 0);
996 DEBUG_puts("cupsdHoldJob: setting state to held...");
998 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
999 job
->state_value
= IPP_JOB_HELD
;
1000 job
->current_file
= 0;
1003 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1008 * 'cupsdLoadAllJobs()' - Load all jobs from disk.
1012 cupsdLoadAllJobs(void)
1014 char filename
[1024]; /* Full filename of job.cache file */
1015 struct stat fileinfo
, /* Information on job.cache file */
1016 dirinfo
; /* Information on RequestRoot dir */
1021 * Create the job arrays as needed...
1025 Jobs
= cupsArrayNew(compare_jobs
, NULL
);
1028 ActiveJobs
= cupsArrayNew(compare_active_jobs
, NULL
);
1031 PrintingJobs
= cupsArrayNew(compare_jobs
, NULL
);
1034 * See whether the job.cache file is older than the RequestRoot directory...
1037 snprintf(filename
, sizeof(filename
), "%s/job.cache", CacheDir
);
1039 if (stat(filename
, &fileinfo
))
1041 fileinfo
.st_mtime
= 0;
1043 if (errno
!= ENOENT
)
1044 cupsdLogMessage(CUPSD_LOG_ERROR
,
1045 "Unable to get file information for \"%s\" - %s",
1046 filename
, strerror(errno
));
1049 if (stat(RequestRoot
, &dirinfo
))
1051 dirinfo
.st_mtime
= 0;
1053 if (errno
!= ENOENT
)
1054 cupsdLogMessage(CUPSD_LOG_ERROR
,
1055 "Unable to get directory information for \"%s\" - %s",
1056 RequestRoot
, strerror(errno
));
1060 * Load the most recent source for job data...
1063 if (dirinfo
.st_mtime
> fileinfo
.st_mtime
)
1065 load_request_root();
1067 load_next_job_id(filename
);
1070 load_job_cache(filename
);
1073 * Clean out old jobs as needed...
1076 if (MaxJobs
> 0 && cupsArrayCount(Jobs
) >= MaxJobs
)
1082 * 'cupsdLoadJob()' - Load a single job...
1085 int /* O - 1 on success, 0 on failure */
1086 cupsdLoadJob(cupsd_job_t
*job
) /* I - Job */
1088 char jobfile
[1024]; /* Job filename */
1089 cups_file_t
*fp
; /* Job file */
1090 int fileid
; /* Current file ID */
1091 ipp_attribute_t
*attr
; /* Job attribute */
1092 const char *dest
; /* Destination name */
1093 cupsd_printer_t
*destptr
; /* Pointer to destination */
1094 mime_type_t
**filetypes
; /* New filetypes array */
1095 int *compressions
; /* New compressions array */
1100 if (job
->state_value
> IPP_JOB_STOPPED
)
1101 job
->access_time
= time(NULL
);
1106 if ((job
->attrs
= ippNew()) == NULL
)
1108 cupsdLogMessage(CUPSD_LOG_ERROR
,
1109 "[Job %d] Ran out of memory for job attributes!", job
->id
);
1114 * Load job attributes...
1117 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Loading attributes...", job
->id
);
1119 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, job
->id
);
1120 if ((fp
= cupsFileOpen(jobfile
, "r")) == NULL
)
1122 cupsdLogMessage(CUPSD_LOG_ERROR
,
1123 "[Job %d] Unable to open job control file \"%s\" - %s!",
1124 job
->id
, jobfile
, strerror(errno
));
1128 if (ippReadIO(fp
, (ipp_iocb_t
)cupsFileRead
, 1, NULL
, job
->attrs
) != IPP_DATA
)
1130 cupsdLogMessage(CUPSD_LOG_ERROR
,
1131 "[Job %d] Unable to read job control file \"%s\"!", job
->id
,
1140 * Copy attribute data to the job object...
1143 if (!ippFindAttribute(job
->attrs
, "time-at-creation", IPP_TAG_INTEGER
))
1145 cupsdLogMessage(CUPSD_LOG_ERROR
,
1146 "[Job %d] Missing or bad time-at-creation attribute in "
1147 "control file!", job
->id
);
1151 if ((job
->state
= ippFindAttribute(job
->attrs
, "job-state",
1152 IPP_TAG_ENUM
)) == NULL
)
1154 cupsdLogMessage(CUPSD_LOG_ERROR
,
1155 "[Job %d] Missing or bad job-state attribute in control "
1160 job
->state_value
= (ipp_jstate_t
)job
->state
->values
[0].integer
;
1164 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1165 IPP_TAG_URI
)) == NULL
)
1167 cupsdLogMessage(CUPSD_LOG_ERROR
,
1168 "[Job %d] No job-printer-uri attribute in control file!",
1173 if ((dest
= cupsdValidateDest(attr
->values
[0].string
.text
, &(job
->dtype
),
1176 cupsdLogMessage(CUPSD_LOG_ERROR
,
1177 "[Job %d] Unable to queue job for destination \"%s\"!",
1178 job
->id
, attr
->values
[0].string
.text
);
1182 cupsdSetString(&job
->dest
, dest
);
1184 else if ((destptr
= cupsdFindDest(job
->dest
)) == NULL
)
1186 cupsdLogMessage(CUPSD_LOG_ERROR
,
1187 "[Job %d] Unable to queue job for destination \"%s\"!",
1188 job
->id
, job
->dest
);
1192 job
->sheets
= ippFindAttribute(job
->attrs
, "job-media-sheets-completed",
1194 job
->job_sheets
= ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_NAME
);
1198 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
1199 IPP_TAG_INTEGER
)) == NULL
)
1201 cupsdLogMessage(CUPSD_LOG_ERROR
,
1202 "[Job %d] Missing or bad job-priority attribute in "
1203 "control file!", job
->id
);
1207 job
->priority
= attr
->values
[0].integer
;
1212 if ((attr
= ippFindAttribute(job
->attrs
, "job-originating-user-name",
1213 IPP_TAG_NAME
)) == NULL
)
1215 cupsdLogMessage(CUPSD_LOG_ERROR
,
1216 "[Job %d] Missing or bad job-originating-user-name "
1217 "attribute in control file!", job
->id
);
1221 cupsdSetString(&job
->username
, attr
->values
[0].string
.text
);
1225 * Set the job hold-until time and state...
1228 if (job
->state_value
== IPP_JOB_HELD
)
1230 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1231 IPP_TAG_KEYWORD
)) == NULL
)
1232 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1235 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
1238 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1239 job
->state_value
= IPP_JOB_PENDING
;
1242 else if (job
->state_value
== IPP_JOB_PROCESSING
)
1244 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1245 job
->state_value
= IPP_JOB_PENDING
;
1248 if (!job
->num_files
)
1251 * Find all the d##### files...
1254 for (fileid
= 1; fileid
< 10000; fileid
++)
1256 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
1259 if (access(jobfile
, 0))
1262 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1263 "[Job %d] Auto-typing document file \"%s\"...", job
->id
,
1266 if (fileid
> job
->num_files
)
1268 if (job
->num_files
== 0)
1270 compressions
= (int *)calloc(fileid
, sizeof(int));
1271 filetypes
= (mime_type_t
**)calloc(fileid
, sizeof(mime_type_t
*));
1275 compressions
= (int *)realloc(job
->compressions
,
1276 sizeof(int) * fileid
);
1277 filetypes
= (mime_type_t
**)realloc(job
->filetypes
,
1278 sizeof(mime_type_t
*) *
1282 if (!compressions
|| !filetypes
)
1284 cupsdLogMessage(CUPSD_LOG_ERROR
,
1285 "[Job %d] Ran out of memory for job file types!",
1290 job
->compressions
= compressions
;
1291 job
->filetypes
= filetypes
;
1292 job
->num_files
= fileid
;
1295 job
->filetypes
[fileid
- 1] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
1296 job
->compressions
+ fileid
- 1);
1298 if (!job
->filetypes
[fileid
- 1])
1299 job
->filetypes
[fileid
- 1] = mimeType(MimeDatabase
, "application",
1305 * Load authentication information as needed...
1308 if (job
->state_value
< IPP_JOB_STOPPED
)
1310 snprintf(jobfile
, sizeof(jobfile
), "%s/a%05d", RequestRoot
, job
->id
);
1312 cupsdClearString(&job
->auth_username
);
1313 cupsdClearString(&job
->auth_domain
);
1314 cupsdClearString(&job
->auth_password
);
1316 if ((fp
= cupsFileOpen(jobfile
, "r")) != NULL
)
1318 int i
, /* Looping var */
1319 bytes
; /* Size of auth data */
1320 char line
[255], /* Line from file */
1321 data
[255]; /* Decoded data */
1325 i
< destptr
->num_auth_info_required
&&
1326 cupsFileGets(fp
, line
, sizeof(line
));
1329 bytes
= sizeof(data
);
1330 httpDecode64_2(data
, &bytes
, line
);
1332 if (!strcmp(destptr
->auth_info_required
[i
], "username"))
1333 cupsdSetStringf(&job
->auth_username
, "AUTH_USERNAME=%s", data
);
1334 else if (!strcmp(destptr
->auth_info_required
[i
], "domain"))
1335 cupsdSetStringf(&job
->auth_domain
, "AUTH_DOMAIN=%s", data
);
1336 else if (!strcmp(destptr
->auth_info_required
[i
], "password"))
1337 cupsdSetStringf(&job
->auth_password
, "AUTH_PASSWORD=%s", data
);
1344 job
->access_time
= time(NULL
);
1348 * If we get here then something bad happened...
1353 ippDelete(job
->attrs
);
1361 * 'cupsdMoveJob()' - Move the specified job to a different destination.
1365 cupsdMoveJob(cupsd_job_t
*job
, /* I - Job */
1366 cupsd_printer_t
*p
) /* I - Destination printer or class */
1368 ipp_attribute_t
*attr
; /* job-printer-uri attribute */
1369 const char *olddest
; /* Old destination */
1370 cupsd_printer_t
*oldp
; /* Old pointer */
1374 * Don't move completed jobs...
1377 if (job
->state_value
> IPP_JOB_STOPPED
)
1381 * Get the old destination...
1384 olddest
= job
->dest
;
1387 oldp
= job
->printer
;
1389 oldp
= cupsdFindDest(olddest
);
1392 * Change the destination information...
1395 if (job
->state_value
== IPP_JOB_PROCESSING
)
1396 cupsdStopJob(job
, 0);
1400 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, oldp
, job
,
1401 "Job #%d moved from %s to %s.", job
->id
, olddest
,
1404 cupsdSetString(&job
->dest
, p
->name
);
1405 job
->dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_REMOTE
|
1406 CUPS_PRINTER_IMPLICIT
);
1408 if ((attr
= ippFindAttribute(job
->attrs
, "job-printer-uri",
1409 IPP_TAG_URI
)) != NULL
)
1410 cupsdSetString(&(attr
->values
[0].string
.text
), p
->uri
);
1412 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, p
, job
,
1413 "Job #%d moved from %s to %s.", job
->id
, olddest
,
1417 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1422 * 'cupsdReleaseJob()' - Release the specified job.
1426 cupsdReleaseJob(cupsd_job_t
*job
) /* I - Job */
1428 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdReleaseJob: id = %d", job
->id
);
1430 if (job
->state_value
== IPP_JOB_HELD
)
1433 * Add trailing banner as needed...
1436 if (job
->pending_timeout
)
1437 cupsdTimeoutJob(job
);
1439 DEBUG_puts("cupsdReleaseJob: setting state to pending...");
1441 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1442 job
->state_value
= IPP_JOB_PENDING
;
1444 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1450 * 'cupsdRestartJob()' - Restart the specified job.
1454 cupsdRestartJob(cupsd_job_t
*job
) /* I - Job */
1456 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdRestartJob: id = %d", job
->id
);
1458 if (job
->state_value
== IPP_JOB_STOPPED
|| job
->num_files
)
1460 ipp_jstate_t old_state
; /* Old job state */
1465 old_state
= job
->state_value
;
1468 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1469 job
->state_value
= IPP_JOB_PENDING
;
1472 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1474 if (old_state
> IPP_JOB_STOPPED
)
1475 cupsArrayAdd(ActiveJobs
, job
);
1481 * 'cupsdSaveAllJobs()' - Save a summary of all jobs to disk.
1485 cupsdSaveAllJobs(void)
1487 int i
; /* Looping var */
1488 cups_file_t
*fp
; /* Job cache file */
1489 char temp
[1024]; /* Temporary string */
1490 cupsd_job_t
*job
; /* Current job */
1491 time_t curtime
; /* Current time */
1492 struct tm
*curdate
; /* Current date */
1495 snprintf(temp
, sizeof(temp
), "%s/job.cache", CacheDir
);
1496 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
1498 cupsdLogMessage(CUPSD_LOG_ERROR
,
1499 "Unable to create job cache file \"%s\" - %s",
1500 temp
, strerror(errno
));
1504 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving job cache file \"%s\"...", temp
);
1507 * Restrict access to the file...
1510 fchown(cupsFileNumber(fp
), getuid(), Group
);
1511 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
1514 * Write a small header to the file...
1517 curtime
= time(NULL
);
1518 curdate
= localtime(&curtime
);
1519 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
1521 cupsFilePuts(fp
, "# Job cache file for " CUPS_SVERSION
"\n");
1522 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
1523 cupsFilePrintf(fp
, "NextJobId %d\n", NextJobId
);
1526 * Write each job known to the system...
1529 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1531 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1533 cupsFilePrintf(fp
, "<Job %d>\n", job
->id
);
1534 cupsFilePrintf(fp
, "State %d\n", job
->state_value
);
1535 cupsFilePrintf(fp
, "Priority %d\n", job
->priority
);
1536 cupsFilePrintf(fp
, "Username %s\n", job
->username
);
1537 cupsFilePrintf(fp
, "Destination %s\n", job
->dest
);
1538 cupsFilePrintf(fp
, "DestType %d\n", job
->dtype
);
1539 cupsFilePrintf(fp
, "NumFiles %d\n", job
->num_files
);
1540 for (i
= 0; i
< job
->num_files
; i
++)
1541 cupsFilePrintf(fp
, "File %d %s/%s %d\n", i
+ 1, job
->filetypes
[i
]->super
,
1542 job
->filetypes
[i
]->type
, job
->compressions
[i
]);
1543 cupsFilePuts(fp
, "</Job>\n");
1551 * 'cupsdSaveJob()' - Save a job to disk.
1555 cupsdSaveJob(cupsd_job_t
*job
) /* I - Job */
1557 char filename
[1024]; /* Job control filename */
1558 cups_file_t
*fp
; /* Job file */
1561 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSaveJob(job=%p(%d)): job->attrs=%p",
1562 job
, job
->id
, job
->attrs
);
1564 snprintf(filename
, sizeof(filename
), "%s/c%05d", RequestRoot
, job
->id
);
1566 if ((fp
= cupsFileOpen(filename
, "w")) == NULL
)
1568 cupsdLogMessage(CUPSD_LOG_ERROR
,
1569 "[Job %d] Unable to create job control file \"%s\" - %s.",
1570 job
->id
, filename
, strerror(errno
));
1574 fchmod(cupsFileNumber(fp
), 0600);
1575 fchown(cupsFileNumber(fp
), RunUser
, Group
);
1577 job
->attrs
->state
= IPP_IDLE
;
1579 if (ippWriteIO(fp
, (ipp_iocb_t
)cupsFileWrite
, 1, NULL
,
1580 job
->attrs
) != IPP_DATA
)
1581 cupsdLogMessage(CUPSD_LOG_ERROR
,
1582 "[Job %d] Unable to write job control file!", job
->id
);
1591 * 'cupsdSetJobHoldUntil()' - Set the hold time for a job...
1595 cupsdSetJobHoldUntil(cupsd_job_t
*job
, /* I - Job */
1596 const char *when
) /* I - When to resume */
1598 time_t curtime
; /* Current time */
1599 struct tm
*curdate
; /* Current date */
1600 int hour
; /* Hold hour */
1601 int minute
; /* Hold minute */
1602 int second
; /* Hold second */
1605 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetJobHoldUntil(%d, \"%s\")",
1610 if (!strcmp(when
, "indefinite") || !strcmp(when
, "auth-info-required"))
1613 * Hold indefinitely...
1616 job
->hold_until
= 0;
1618 else if (!strcmp(when
, "day-time"))
1621 * Hold to 6am the next morning unless local time is < 6pm.
1624 curtime
= time(NULL
);
1625 curdate
= localtime(&curtime
);
1627 if (curdate
->tm_hour
< 18)
1628 job
->hold_until
= curtime
;
1630 job
->hold_until
= curtime
+
1631 ((29 - curdate
->tm_hour
) * 60 + 59 -
1632 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1634 else if (!strcmp(when
, "evening") || !strcmp(when
, "night"))
1637 * Hold to 6pm unless local time is > 6pm or < 6am.
1640 curtime
= time(NULL
);
1641 curdate
= localtime(&curtime
);
1643 if (curdate
->tm_hour
< 6 || curdate
->tm_hour
>= 18)
1644 job
->hold_until
= curtime
;
1646 job
->hold_until
= curtime
+
1647 ((17 - curdate
->tm_hour
) * 60 + 59 -
1648 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1650 else if (!strcmp(when
, "second-shift"))
1653 * Hold to 4pm unless local time is > 4pm.
1656 curtime
= time(NULL
);
1657 curdate
= localtime(&curtime
);
1659 if (curdate
->tm_hour
>= 16)
1660 job
->hold_until
= curtime
;
1662 job
->hold_until
= curtime
+
1663 ((15 - curdate
->tm_hour
) * 60 + 59 -
1664 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1666 else if (!strcmp(when
, "third-shift"))
1669 * Hold to 12am unless local time is < 8am.
1672 curtime
= time(NULL
);
1673 curdate
= localtime(&curtime
);
1675 if (curdate
->tm_hour
< 8)
1676 job
->hold_until
= curtime
;
1678 job
->hold_until
= curtime
+
1679 ((23 - curdate
->tm_hour
) * 60 + 59 -
1680 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1682 else if (!strcmp(when
, "weekend"))
1685 * Hold to weekend unless we are in the weekend.
1688 curtime
= time(NULL
);
1689 curdate
= localtime(&curtime
);
1691 if (curdate
->tm_wday
== 0 || curdate
->tm_wday
== 6)
1692 job
->hold_until
= curtime
;
1694 job
->hold_until
= curtime
+
1695 (((5 - curdate
->tm_wday
) * 24 +
1696 (17 - curdate
->tm_hour
)) * 60 + 59 -
1697 curdate
->tm_min
) * 60 + 60 - curdate
->tm_sec
;
1699 else if (sscanf(when
, "%d:%d:%d", &hour
, &minute
, &second
) >= 2)
1702 * Hold to specified GMT time (HH:MM or HH:MM:SS)...
1705 curtime
= time(NULL
);
1706 curdate
= gmtime(&curtime
);
1708 job
->hold_until
= curtime
+
1709 ((hour
- curdate
->tm_hour
) * 60 + minute
-
1710 curdate
->tm_min
) * 60 + second
- curdate
->tm_sec
;
1713 * Hold until next day as needed...
1716 if (job
->hold_until
< curtime
)
1717 job
->hold_until
+= 24 * 60 * 60;
1720 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetJobHoldUntil: hold_until = %d",
1721 (int)job
->hold_until
);
1726 * 'cupsdSetJobPriority()' - Set the priority of a job, moving it up/down in
1727 * the list as needed.
1731 cupsdSetJobPriority(
1732 cupsd_job_t
*job
, /* I - Job ID */
1733 int priority
) /* I - New priority (0 to 100) */
1735 ipp_attribute_t
*attr
; /* Job attribute */
1739 * Don't change completed jobs...
1742 if (job
->state_value
>= IPP_JOB_PROCESSING
)
1746 * Set the new priority and re-add the job into the active list...
1749 cupsArrayRemove(ActiveJobs
, job
);
1751 job
->priority
= priority
;
1753 if ((attr
= ippFindAttribute(job
->attrs
, "job-priority",
1754 IPP_TAG_INTEGER
)) != NULL
)
1755 attr
->values
[0].integer
= priority
;
1757 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-priority",
1760 cupsArrayAdd(ActiveJobs
, job
);
1763 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1768 * 'cupsdStopAllJobs()' - Stop all print jobs.
1772 cupsdStopAllJobs(int force
) /* I - 1 = Force all filters to stop */
1774 cupsd_job_t
*job
; /* Current job */
1777 DEBUG_puts("cupsdStopAllJobs()");
1779 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
1781 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
1782 if (job
->state_value
== IPP_JOB_PROCESSING
)
1784 cupsdStopJob(job
, force
);
1785 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1786 job
->state_value
= IPP_JOB_PENDING
;
1792 * 'cupsdStopJob()' - Stop a print job.
1796 cupsdStopJob(cupsd_job_t
*job
, /* I - Job */
1797 int force
) /* I - 1 = Force all filters to stop */
1799 int i
; /* Looping var */
1802 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] cupsdStopJob: force = %d",
1805 if (job
->state_value
!= IPP_JOB_PROCESSING
)
1808 FilterLevel
-= job
->cost
;
1810 if (job
->printer
->state
== IPP_PRINTER_PROCESSING
)
1811 cupsdSetPrinterState(job
->printer
, IPP_PRINTER_IDLE
, 0);
1813 job
->state
->values
[0].integer
= IPP_JOB_STOPPED
;
1814 job
->state_value
= IPP_JOB_STOPPED
;
1815 job
->printer
->job
= NULL
;
1816 job
->printer
= NULL
;
1818 job
->current_file
--;
1820 for (i
= 0; job
->filters
[i
]; i
++)
1821 if (job
->filters
[i
] > 0)
1823 cupsdEndProcess(job
->filters
[i
], force
);
1825 job
->filters
[i
] = 0;
1828 if (job
->backend
> 0)
1830 cupsdEndProcess(job
->backend
, force
);
1838 cupsdDestroyProfile(job
->profile
);
1839 job
->profile
= NULL
;
1841 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] Closing print pipes [ %d %d ]...",
1842 job
->id
, job
->print_pipes
[0], job
->print_pipes
[1]);
1844 cupsdClosePipe(job
->print_pipes
);
1846 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] Closing back pipes [ %d %d ]...",
1847 job
->id
, job
->back_pipes
[0], job
->back_pipes
[1]);
1849 cupsdClosePipe(job
->back_pipes
);
1851 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Job %d] Closing side pipes [ %d %d ]...",
1852 job
->id
, job
->side_pipes
[0], job
->side_pipes
[1]);
1854 cupsdClosePipe(job
->side_pipes
);
1856 if (job
->status_buffer
)
1859 * Close the pipe and clear the input bit.
1862 cupsdRemoveSelect(job
->status_buffer
->fd
);
1864 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1865 "[Job %d] Closing status pipes [ %d %d ]...",
1866 job
->id
, job
->status_pipes
[0], job
->status_pipes
[1]);
1868 cupsdClosePipe(job
->status_pipes
);
1869 cupsdStatBufDelete(job
->status_buffer
);
1871 job
->status_buffer
= NULL
;
1877 * 'cupsdUnloadCompletedJobs()' - Flush completed job history from memory.
1881 cupsdUnloadCompletedJobs(void)
1883 cupsd_job_t
*job
; /* Current job */
1884 time_t expire
; /* Expiration time */
1887 expire
= time(NULL
) - 60;
1889 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
1891 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
1892 if (job
->attrs
&& job
->state_value
>= IPP_JOB_STOPPED
&&
1893 job
->access_time
< expire
)
1904 * 'compare_active_jobs()' - Compare the job IDs and priorities of two jobs.
1907 static int /* O - Difference */
1908 compare_active_jobs(void *first
, /* I - First job */
1909 void *second
, /* I - Second job */
1910 void *data
) /* I - App data (not used) */
1912 int diff
; /* Difference */
1915 if ((diff
= ((cupsd_job_t
*)second
)->priority
-
1916 ((cupsd_job_t
*)first
)->priority
) != 0)
1919 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
1924 * 'compare_jobs()' - Compare the job IDs of two jobs.
1927 static int /* O - Difference */
1928 compare_jobs(void *first
, /* I - First job */
1929 void *second
, /* I - Second job */
1930 void *data
) /* I - App data (not used) */
1932 return (((cupsd_job_t
*)first
)->id
- ((cupsd_job_t
*)second
)->id
);
1937 * 'ipp_length()' - Compute the size of the buffer needed to hold
1938 * the textual IPP attributes.
1941 static int /* O - Size of attribute buffer */
1942 ipp_length(ipp_t
*ipp
) /* I - IPP request */
1944 int bytes
; /* Number of bytes */
1945 int i
; /* Looping var */
1946 ipp_attribute_t
*attr
; /* Current attribute */
1950 * Loop through all attributes...
1955 for (attr
= ipp
->attrs
; attr
!= NULL
; attr
= attr
->next
)
1958 * Skip attributes that won't be sent to filters...
1961 if (attr
->value_tag
== IPP_TAG_MIMETYPE
||
1962 attr
->value_tag
== IPP_TAG_NAMELANG
||
1963 attr
->value_tag
== IPP_TAG_TEXTLANG
||
1964 attr
->value_tag
== IPP_TAG_URI
||
1965 attr
->value_tag
== IPP_TAG_URISCHEME
)
1968 if (strncmp(attr
->name
, "time-", 5) == 0)
1972 * Add space for a leading space and commas between each value.
1973 * For the first attribute, the leading space isn't used, so the
1974 * extra byte can be used as the nul terminator...
1977 bytes
++; /* " " separator */
1978 bytes
+= attr
->num_values
; /* "," separators */
1981 * Boolean attributes appear as "foo,nofoo,foo,nofoo", while
1982 * other attributes appear as "foo=value1,value2,...,valueN".
1985 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
1986 bytes
+= strlen(attr
->name
);
1988 bytes
+= attr
->num_values
* strlen(attr
->name
);
1991 * Now add the size required for each value in the attribute...
1994 switch (attr
->value_tag
)
1996 case IPP_TAG_INTEGER
:
1999 * Minimum value of a signed integer is -2147483647, or 11 digits.
2002 bytes
+= attr
->num_values
* 11;
2005 case IPP_TAG_BOOLEAN
:
2007 * Add two bytes for each false ("no") value...
2010 for (i
= 0; i
< attr
->num_values
; i
++)
2011 if (!attr
->values
[i
].boolean
)
2015 case IPP_TAG_RANGE
:
2017 * A range is two signed integers separated by a hyphen, or
2018 * 23 characters max.
2021 bytes
+= attr
->num_values
* 23;
2024 case IPP_TAG_RESOLUTION
:
2026 * A resolution is two signed integers separated by an "x" and
2027 * suffixed by the units, or 26 characters max.
2030 bytes
+= attr
->num_values
* 26;
2033 case IPP_TAG_STRING
:
2036 case IPP_TAG_KEYWORD
:
2037 case IPP_TAG_CHARSET
:
2038 case IPP_TAG_LANGUAGE
:
2041 * Strings can contain characters that need quoting. We need
2042 * at least 2 * len + 2 characters to cover the quotes and
2043 * any backslashes in the string.
2046 for (i
= 0; i
< attr
->num_values
; i
++)
2047 bytes
+= 2 * strlen(attr
->values
[i
].string
.text
) + 2;
2051 break; /* anti-compiler-warning-code */
2060 * 'load_job_cache()' - Load jobs from the job.cache file.
2064 load_job_cache(const char *filename
) /* I - job.cache filename */
2066 cups_file_t
*fp
; /* job.cache file */
2067 char line
[1024], /* Line buffer */
2068 *value
; /* Value on line */
2069 int linenum
; /* Line number in file */
2070 cupsd_job_t
*job
; /* Current job */
2071 int jobid
; /* Job ID */
2072 char jobfile
[1024]; /* Job filename */
2076 * Open the job.cache file...
2079 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
2081 if (errno
!= ENOENT
)
2082 cupsdLogMessage(CUPSD_LOG_ERROR
,
2083 "Unable to open job cache file \"%s\": %s",
2084 filename
, strerror(errno
));
2086 load_request_root();
2092 * Read entries from the job cache file and create jobs as needed.
2095 cupsdLogMessage(CUPSD_LOG_INFO
, "Loading job cache file \"%s\"...",
2101 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
2103 if (!strcasecmp(line
, "NextJobId"))
2106 NextJobId
= atoi(value
);
2108 else if (!strcasecmp(line
, "<Job"))
2112 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing </Job> directive on line %d!",
2119 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing job ID on line %d!", linenum
);
2123 jobid
= atoi(value
);
2127 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad job ID %d on line %d!", jobid
,
2132 snprintf(jobfile
, sizeof(jobfile
), "%s/c%05d", RequestRoot
, jobid
);
2133 if (access(jobfile
, 0))
2135 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Job %d] Files have gone away!",
2140 job
= calloc(1, sizeof(cupsd_job_t
));
2143 cupsdLogMessage(CUPSD_LOG_EMERG
,
2144 "[Job %d] Unable to allocate memory for job!", jobid
);
2149 job
->back_pipes
[0] = -1;
2150 job
->back_pipes
[1] = -1;
2151 job
->print_pipes
[0] = -1;
2152 job
->print_pipes
[1] = -1;
2153 job
->side_pipes
[0] = -1;
2154 job
->side_pipes
[1] = -1;
2155 job
->status_pipes
[0] = -1;
2156 job
->status_pipes
[1] = -1;
2158 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Loading from cache...",
2163 cupsdLogMessage(CUPSD_LOG_ERROR
,
2164 "Missing <Job #> directive on line %d!", linenum
);
2167 else if (!strcasecmp(line
, "</Job>"))
2169 cupsArrayAdd(Jobs
, job
);
2171 if (job
->state_value
<= IPP_JOB_STOPPED
)
2173 cupsArrayAdd(ActiveJobs
, job
);
2181 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing value on line %d!", linenum
);
2184 else if (!strcasecmp(line
, "State"))
2186 job
->state_value
= (ipp_jstate_t
)atoi(value
);
2188 if (job
->state_value
< IPP_JOB_PENDING
)
2189 job
->state_value
= IPP_JOB_PENDING
;
2190 else if (job
->state_value
> IPP_JOB_COMPLETED
)
2191 job
->state_value
= IPP_JOB_COMPLETED
;
2193 else if (!strcasecmp(line
, "Priority"))
2195 job
->priority
= atoi(value
);
2197 else if (!strcasecmp(line
, "Username"))
2199 cupsdSetString(&job
->username
, value
);
2201 else if (!strcasecmp(line
, "Destination"))
2203 cupsdSetString(&job
->dest
, value
);
2205 else if (!strcasecmp(line
, "DestType"))
2207 job
->dtype
= (cups_ptype_t
)atoi(value
);
2209 else if (!strcasecmp(line
, "NumFiles"))
2211 job
->num_files
= atoi(value
);
2213 if (job
->num_files
< 0)
2215 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad NumFiles value %d on line %d!",
2216 job
->num_files
, linenum
);
2221 if (job
->num_files
> 0)
2223 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-001", RequestRoot
,
2225 if (access(jobfile
, 0))
2227 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Data files have gone away!",
2233 job
->filetypes
= calloc(job
->num_files
, sizeof(mime_type_t
*));
2234 job
->compressions
= calloc(job
->num_files
, sizeof(int));
2236 if (!job
->filetypes
|| !job
->compressions
)
2238 cupsdLogMessage(CUPSD_LOG_EMERG
,
2239 "[Job %d] Unable to allocate memory for %d files!",
2240 job
->id
, job
->num_files
);
2245 else if (!strcasecmp(line
, "File"))
2247 int number
, /* File number */
2248 compression
; /* Compression value */
2249 char super
[MIME_MAX_SUPER
], /* MIME super type */
2250 type
[MIME_MAX_TYPE
]; /* MIME type */
2253 if (sscanf(value
, "%d%*[ \t]%15[^/]/%255s%d", &number
, super
, type
,
2256 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File on line %d!", linenum
);
2260 if (number
< 1 || number
> job
->num_files
)
2262 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad File number %d on line %d!",
2269 job
->compressions
[number
] = compression
;
2270 job
->filetypes
[number
] = mimeType(MimeDatabase
, super
, type
);
2272 if (!job
->filetypes
[number
])
2275 * If the original MIME type is unknown, auto-type it!
2278 cupsdLogMessage(CUPSD_LOG_ERROR
,
2279 "[Job %d] Unknown MIME type %s/%s for file %d!",
2280 job
->id
, super
, type
, number
+ 1);
2282 snprintf(jobfile
, sizeof(jobfile
), "%s/d%05d-%03d", RequestRoot
,
2283 job
->id
, number
+ 1);
2284 job
->filetypes
[number
] = mimeFileType(MimeDatabase
, jobfile
, NULL
,
2285 job
->compressions
+ number
);
2288 * If that didn't work, assume it is raw...
2291 if (!job
->filetypes
[number
])
2292 job
->filetypes
[number
] = mimeType(MimeDatabase
, "application",
2297 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unknown %s directive on line %d!",
2306 * 'load_next_job_id()' - Load the NextJobId value from the job.cache file.
2310 load_next_job_id(const char *filename
) /* I - job.cache filename */
2312 cups_file_t
*fp
; /* job.cache file */
2313 char line
[1024], /* Line buffer */
2314 *value
; /* Value on line */
2315 int linenum
; /* Line number in file */
2316 int next_job_id
; /* NextJobId value from line */
2320 * Read the NextJobId directive from the job.cache file and use
2321 * the value (if any).
2324 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
2326 if (errno
!= ENOENT
)
2327 cupsdLogMessage(CUPSD_LOG_ERROR
,
2328 "Unable to open job cache file \"%s\": %s",
2329 filename
, strerror(errno
));
2334 cupsdLogMessage(CUPSD_LOG_INFO
,
2335 "Loading NextJobId from job cache file \"%s\"...", filename
);
2339 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
2341 if (!strcasecmp(line
, "NextJobId"))
2345 next_job_id
= atoi(value
);
2347 if (next_job_id
> NextJobId
)
2348 NextJobId
= next_job_id
;
2359 * 'load_request_root()' - Load jobs from the RequestRoot directory.
2363 load_request_root(void)
2365 cups_dir_t
*dir
; /* Directory */
2366 cups_dentry_t
*dent
; /* Directory entry */
2367 cupsd_job_t
*job
; /* New job */
2371 * Open the requests directory...
2374 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Scanning %s for jobs...", RequestRoot
);
2376 if ((dir
= cupsDirOpen(RequestRoot
)) == NULL
)
2378 cupsdLogMessage(CUPSD_LOG_ERROR
,
2379 "Unable to open spool directory \"%s\": %s",
2380 RequestRoot
, strerror(errno
));
2385 * Read all the c##### files...
2388 while ((dent
= cupsDirRead(dir
)) != NULL
)
2389 if (strlen(dent
->filename
) >= 6 && dent
->filename
[0] == 'c')
2392 * Allocate memory for the job...
2395 if ((job
= calloc(sizeof(cupsd_job_t
), 1)) == NULL
)
2397 cupsdLogMessage(CUPSD_LOG_ERROR
, "Ran out of memory for jobs!");
2403 * Assign the job ID...
2406 job
->id
= atoi(dent
->filename
+ 1);
2407 job
->back_pipes
[0] = -1;
2408 job
->back_pipes
[1] = -1;
2409 job
->print_pipes
[0] = -1;
2410 job
->print_pipes
[1] = -1;
2411 job
->side_pipes
[0] = -1;
2412 job
->side_pipes
[1] = -1;
2413 job
->status_pipes
[0] = -1;
2414 job
->status_pipes
[1] = -1;
2416 if (job
->id
>= NextJobId
)
2417 NextJobId
= job
->id
+ 1;
2426 * Insert the job into the array, sorting by job priority and ID...
2429 cupsArrayAdd(Jobs
, job
);
2431 if (job
->state_value
<= IPP_JOB_STOPPED
)
2432 cupsArrayAdd(ActiveJobs
, job
);
2442 * 'set_time()' - Set one of the "time-at-xyz" attributes...
2446 set_time(cupsd_job_t
*job
, /* I - Job to update */
2447 const char *name
) /* I - Name of attribute */
2449 ipp_attribute_t
*attr
; /* Time attribute */
2452 if ((attr
= ippFindAttribute(job
->attrs
, name
, IPP_TAG_ZERO
)) != NULL
)
2454 attr
->value_tag
= IPP_TAG_INTEGER
;
2455 attr
->values
[0].integer
= time(NULL
);
2461 * 'set_hold_until()' - Set the hold time and update job-hold-until attribute...
2465 set_hold_until(cupsd_job_t
*job
, /* I - Job to update */
2466 time_t holdtime
) /* I - Hold until time */
2468 ipp_attribute_t
*attr
; /* job-hold-until attribute */
2469 struct tm
*holddate
; /* Hold date */
2470 char holdstr
[64]; /* Hold time */
2474 * Set the hold_until value and hold the job...
2477 cupsdLogMessage(CUPSD_LOG_DEBUG
, "set_hold_until: hold_until = %d",
2480 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
2481 job
->state_value
= IPP_JOB_HELD
;
2482 job
->hold_until
= holdtime
;
2485 * Update the job-hold-until attribute with a string representing GMT
2486 * time (HH:MM:SS)...
2489 holddate
= gmtime(&holdtime
);
2490 snprintf(holdstr
, sizeof(holdstr
), "%d:%d:%d", holddate
->tm_hour
,
2491 holddate
->tm_min
, holddate
->tm_sec
);
2493 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
2494 IPP_TAG_KEYWORD
)) == NULL
)
2495 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
2498 * Either add the attribute or update the value of the existing one
2502 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "job-hold-until",
2505 cupsdSetString(&attr
->values
[0].string
.text
, holdstr
);
2508 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2513 * 'start_job()' - Start a print job.
2517 start_job(cupsd_job_t
*job
, /* I - Job ID */
2518 cupsd_printer_t
*printer
) /* I - Printer to print job */
2520 int i
; /* Looping var */
2521 int slot
; /* Pipe slot */
2522 cups_array_t
*filters
, /* Filters for job */
2523 *prefilters
; /* Filters with prefilters */
2524 mime_filter_t
*filter
, /* Current filter */
2525 *prefilter
, /* Prefilter */
2526 port_monitor
; /* Port monitor filter */
2527 char method
[255], /* Method for output */
2528 *optptr
, /* Pointer to options */
2529 *valptr
; /* Pointer in value string */
2530 ipp_attribute_t
*attr
; /* Current attribute */
2531 struct stat backinfo
; /* Backend file information */
2532 int backroot
; /* Run backend as root? */
2533 int pid
; /* Process ID of new filter process */
2534 int banner_page
; /* 1 if banner page, 0 otherwise */
2535 int filterfds
[2][2];/* Pipes used between filters */
2536 int envc
; /* Number of environment variables */
2537 char **argv
, /* Filter command-line arguments */
2538 filename
[1024], /* Job filename */
2539 command
[1024], /* Full path to command */
2540 jobid
[255], /* Job ID string */
2541 title
[IPP_MAX_NAME
],
2542 /* Job title string */
2543 copies
[255], /* # copies string */
2544 *envp
[MAX_ENV
+ 19],
2545 /* Environment variables */
2546 charset
[255], /* CHARSET env variable */
2547 class_name
[255],/* CLASS env variable */
2548 classification
[1024],
2549 /* CLASSIFICATION env variable */
2551 /* CONTENT_TYPE env variable */
2553 /* DEVICE_URI env variable */
2554 final_content_type
[1024],
2555 /* FINAL_CONTENT_TYPE env variable */
2556 lang
[255], /* LANG env variable */
2558 apple_language
[255],
2559 /* APPLE_LANGUAGE env variable */
2560 #endif /* __APPLE__ */
2561 ppd
[1024], /* PPD env variable */
2563 /* PRINTER_INFO env variable */
2564 printer_location
[255],
2565 /* PRINTER_LOCATION env variable */
2567 /* PRINTER env variable */
2569 /* RIP_MAX_CACHE env variable */
2570 static char *options
= NULL
;/* Full list of options */
2571 static int optlength
= 0; /* Length of option buffer */
2574 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "start_job: file = %d/%d",
2575 job
->current_file
, job
->num_files
);
2577 if (job
->num_files
== 0)
2579 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "No files, canceling job!");
2580 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2585 * Figure out what filters are required to convert from
2586 * the source to the destination type...
2595 * Remote jobs and raw queues go directly to the printer without
2599 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Sending job to queue tagged as raw...");
2606 * Local jobs get filtered...
2609 filters
= mimeFilter(MimeDatabase
, job
->filetypes
[job
->current_file
],
2610 printer
->filetype
, &(job
->cost
));
2614 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
2615 "Unable to convert file %d to printable format!",
2617 cupsdLogMessage(CUPSD_LOG_INFO
,
2618 "Hint: Do you have Ghostscript installed?");
2620 if (LogLevel
< CUPSD_LOG_DEBUG
)
2621 cupsdLogMessage(CUPSD_LOG_INFO
,
2622 "Hint: Try setting the LogLevel to \"debug\".");
2624 job
->current_file
++;
2626 if (job
->current_file
== job
->num_files
)
2627 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2633 * Remove NULL ("-") filters...
2636 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
2638 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
2639 if (!strcmp(filter
->filter
, "-"))
2640 cupsArrayRemove(filters
, filter
);
2642 if (cupsArrayCount(filters
) == 0)
2644 cupsArrayDelete(filters
);
2649 * If this printer has any pre-filters, insert the required pre-filter
2650 * in the filters array...
2653 if (printer
->prefiltertype
&& filters
)
2655 prefilters
= cupsArrayNew(NULL
, NULL
);
2657 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
2659 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
2661 if ((prefilter
= mimeFilterLookup(MimeDatabase
, filter
->src
,
2662 printer
->prefiltertype
)))
2664 cupsArrayAdd(prefilters
, prefilter
);
2665 job
->cost
+= prefilter
->cost
;
2668 cupsArrayAdd(prefilters
, filter
);
2671 cupsArrayDelete(filters
);
2672 filters
= prefilters
;
2677 * Set a minimum cost of 100 for all jobs so that FilterLimit
2678 * works with raw queues and other low-cost paths.
2681 if (job
->cost
< 100)
2685 * See if the filter cost is too high...
2688 if ((FilterLevel
+ job
->cost
) > FilterLimit
&& FilterLevel
> 0 &&
2692 * Don't print this job quite yet...
2695 cupsArrayDelete(filters
);
2697 cupsdLogJob(job
, CUPSD_LOG_INFO
,
2698 "Holding because filter limit has been reached.");
2699 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
2700 "start_job: file=%d, cost=%d, level=%d, limit=%d",
2701 job
->current_file
, job
->cost
, FilterLevel
,
2706 FilterLevel
+= job
->cost
;
2709 * Add decompression/raw filter as needed...
2712 if ((!printer
->raw
&& job
->compressions
[job
->current_file
]) ||
2713 (!filters
&& !printer
->remote
&&
2714 (job
->num_files
> 1 || !strncmp(printer
->device_uri
, "file:", 5))))
2717 * Add gziptoany filter to the front of the list...
2721 filters
= cupsArrayNew(NULL
, NULL
);
2723 if (!cupsArrayInsert(filters
, &gziptoany_filter
))
2725 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
2726 "Unable to add decompression filter - %s", strerror(errno
));
2728 cupsArrayDelete(filters
);
2730 job
->current_file
++;
2732 if (job
->current_file
== job
->num_files
)
2733 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2740 * Add port monitor, if any...
2743 if (printer
->port_monitor
)
2746 * Add port monitor to the end of the list...
2750 filters
= cupsArrayNew(NULL
, NULL
);
2752 port_monitor
.src
= NULL
;
2753 port_monitor
.dst
= NULL
;
2754 port_monitor
.cost
= 0;
2756 snprintf(port_monitor
.filter
, sizeof(port_monitor
.filter
),
2757 "%s/monitor/%s", ServerBin
, printer
->port_monitor
);
2759 if (!cupsArrayAdd(filters
, &port_monitor
))
2761 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
2762 "Unable to add port monitor - %s", strerror(errno
));
2764 cupsArrayDelete(filters
);
2766 job
->current_file
++;
2768 if (job
->current_file
== job
->num_files
)
2769 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2776 * Make sure we don't go over the "MAX_FILTERS" limit...
2779 if (cupsArrayCount(filters
) > MAX_FILTERS
)
2781 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
2782 "Too many filters (%d > %d), unable to print!",
2783 cupsArrayCount(filters
), MAX_FILTERS
);
2785 cupsArrayDelete(filters
);
2786 cupsdCancelJob(job
, 0, IPP_JOB_STOPPED
);
2792 * Update the printer and job state to "processing"...
2795 job
->state
->values
[0].integer
= IPP_JOB_PROCESSING
;
2796 job
->state_value
= IPP_JOB_PROCESSING
;
2799 job
->printer
= printer
;
2802 cupsdSetPrinterState(printer
, IPP_PRINTER_PROCESSING
, 0);
2804 if (job
->current_file
== 0)
2807 * Add to the printing list...
2810 cupsArrayAdd(PrintingJobs
, job
);
2811 cupsdSetBusyState();
2814 * Set the processing time...
2817 set_time(job
, "time-at-processing");
2820 * Create the backchannel pipes and make them non-blocking...
2823 cupsdOpenPipe(job
->back_pipes
);
2825 fcntl(job
->back_pipes
[0], F_SETFL
,
2826 fcntl(job
->back_pipes
[0], F_GETFL
) | O_NONBLOCK
);
2828 fcntl(job
->back_pipes
[1], F_SETFL
,
2829 fcntl(job
->back_pipes
[1], F_GETFL
) | O_NONBLOCK
);
2832 * Create the side-channel pipes and make them non-blocking...
2835 socketpair(AF_LOCAL
, SOCK_STREAM
, 0, job
->side_pipes
);
2837 fcntl(job
->side_pipes
[0], F_SETFL
,
2838 fcntl(job
->side_pipes
[0], F_GETFL
) | O_NONBLOCK
);
2840 fcntl(job
->side_pipes
[1], F_SETFL
,
2841 fcntl(job
->side_pipes
[1], F_GETFL
) | O_NONBLOCK
);
2845 * Determine if we are printing a banner page or not...
2848 if (job
->job_sheets
== NULL
)
2850 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "No job-sheets attribute.");
2851 if ((job
->job_sheets
=
2852 ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_ZERO
)) != NULL
)
2853 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
2854 "... but someone added one without setting job_sheets!");
2856 else if (job
->job_sheets
->num_values
== 1)
2857 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "job-sheets=%s",
2858 job
->job_sheets
->values
[0].string
.text
);
2860 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "job-sheets=%s,%s",
2861 job
->job_sheets
->values
[0].string
.text
,
2862 job
->job_sheets
->values
[1].string
.text
);
2864 if (printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
))
2866 else if (job
->job_sheets
== NULL
)
2868 else if (strcasecmp(job
->job_sheets
->values
[0].string
.text
, "none") != 0 &&
2869 job
->current_file
== 0)
2871 else if (job
->job_sheets
->num_values
> 1 &&
2872 strcasecmp(job
->job_sheets
->values
[1].string
.text
, "none") != 0 &&
2873 job
->current_file
== (job
->num_files
- 1))
2878 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "banner_page = %d", banner_page
);
2881 * Building the options string is harder than it needs to be, but
2882 * for the moment we need to pass strings for command-line args and
2883 * not IPP attribute pointers... :)
2885 * First allocate/reallocate the option buffer as needed...
2888 i
= ipp_length(job
->attrs
);
2890 if (i
> optlength
|| !options
)
2895 optptr
= realloc(options
, i
);
2899 cupsdLogJob(job
, CUPSD_LOG_CRIT
,
2900 "Unable to allocate %d bytes for option buffer!", i
);
2902 cupsArrayDelete(filters
);
2904 FilterLevel
-= job
->cost
;
2906 cupsdCancelJob(job
, 0, IPP_JOB_ABORTED
);
2915 * Now loop through the attributes and convert them to the textual
2916 * representation used by the filters...
2922 snprintf(title
, sizeof(title
), "%s-%d", printer
->name
, job
->id
);
2923 strcpy(copies
, "1");
2925 for (attr
= job
->attrs
->attrs
; attr
!= NULL
; attr
= attr
->next
)
2927 if (!strcmp(attr
->name
, "copies") &&
2928 attr
->value_tag
== IPP_TAG_INTEGER
)
2931 * Don't use the # copies attribute if we are printing the job sheets...
2935 sprintf(copies
, "%d", attr
->values
[0].integer
);
2937 else if (!strcmp(attr
->name
, "job-name") &&
2938 (attr
->value_tag
== IPP_TAG_NAME
||
2939 attr
->value_tag
== IPP_TAG_NAMELANG
))
2940 strlcpy(title
, attr
->values
[0].string
.text
, sizeof(title
));
2941 else if (attr
->group_tag
== IPP_TAG_JOB
)
2944 * Filter out other unwanted attributes...
2947 if (attr
->value_tag
== IPP_TAG_MIMETYPE
||
2948 attr
->value_tag
== IPP_TAG_NAMELANG
||
2949 attr
->value_tag
== IPP_TAG_TEXTLANG
||
2950 (attr
->value_tag
== IPP_TAG_URI
&& strcmp(attr
->name
, "job-uuid")) ||
2951 attr
->value_tag
== IPP_TAG_URISCHEME
||
2952 attr
->value_tag
== IPP_TAG_BEGIN_COLLECTION
) /* Not yet supported */
2955 if (!strncmp(attr
->name
, "time-", 5))
2958 if (!strncmp(attr
->name
, "job-", 4) &&
2959 strcmp(attr
->name
, "job-billing") &&
2960 strcmp(attr
->name
, "job-impressions") &&
2961 strcmp(attr
->name
, "job-originating-host-name") &&
2962 strcmp(attr
->name
, "job-uuid") &&
2963 !(printer
->type
& CUPS_PRINTER_REMOTE
))
2966 if ((!strcmp(attr
->name
, "job-impressions") ||
2967 !strcmp(attr
->name
, "page-label") ||
2968 !strcmp(attr
->name
, "page-border") ||
2969 !strncmp(attr
->name
, "number-up", 9) ||
2970 !strcmp(attr
->name
, "page-ranges") ||
2971 !strcmp(attr
->name
, "page-set") ||
2972 !strcasecmp(attr
->name
, "AP_FIRSTPAGE_InputSlot") ||
2973 !strcasecmp(attr
->name
, "AP_FIRSTPAGE_ManualFeed") ||
2974 !strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
2975 "PMTotalSidesImaged..n.") ||
2976 !strcasecmp(attr
->name
, "com.apple.print.PrintSettings."
2977 "PMTotalBeginPages..n.")) &&
2982 * Otherwise add them to the list...
2985 if (optptr
> options
)
2986 strlcat(optptr
, " ", optlength
- (optptr
- options
));
2988 if (attr
->value_tag
!= IPP_TAG_BOOLEAN
)
2990 strlcat(optptr
, attr
->name
, optlength
- (optptr
- options
));
2991 strlcat(optptr
, "=", optlength
- (optptr
- options
));
2994 for (i
= 0; i
< attr
->num_values
; i
++)
2997 strlcat(optptr
, ",", optlength
- (optptr
- options
));
2999 optptr
+= strlen(optptr
);
3001 switch (attr
->value_tag
)
3003 case IPP_TAG_INTEGER
:
3005 snprintf(optptr
, optlength
- (optptr
- options
),
3006 "%d", attr
->values
[i
].integer
);
3009 case IPP_TAG_BOOLEAN
:
3010 if (!attr
->values
[i
].boolean
)
3011 strlcat(optptr
, "no", optlength
- (optptr
- options
));
3013 case IPP_TAG_NOVALUE
:
3014 strlcat(optptr
, attr
->name
,
3015 optlength
- (optptr
- options
));
3018 case IPP_TAG_RANGE
:
3019 if (attr
->values
[i
].range
.lower
== attr
->values
[i
].range
.upper
)
3020 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
3021 "%d", attr
->values
[i
].range
.lower
);
3023 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
3024 "%d-%d", attr
->values
[i
].range
.lower
,
3025 attr
->values
[i
].range
.upper
);
3028 case IPP_TAG_RESOLUTION
:
3029 snprintf(optptr
, optlength
- (optptr
- options
) - 1,
3030 "%dx%d%s", attr
->values
[i
].resolution
.xres
,
3031 attr
->values
[i
].resolution
.yres
,
3032 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
3036 case IPP_TAG_STRING
:
3039 case IPP_TAG_KEYWORD
:
3040 case IPP_TAG_CHARSET
:
3041 case IPP_TAG_LANGUAGE
:
3043 for (valptr
= attr
->values
[i
].string
.text
; *valptr
;)
3045 if (strchr(" \t\n\\\'\"", *valptr
))
3047 *optptr
++ = *valptr
++;
3054 break; /* anti-compiler-warning-code */
3058 optptr
+= strlen(optptr
);
3063 * Build the command-line arguments for the filters. Each filter
3064 * has 6 or 7 arguments:
3068 * argv[2] = username
3070 * argv[4] = # copies
3072 * argv[6] = filename (optional; normally stdin)
3074 * This allows legacy printer drivers that use the old System V
3075 * printing interface to be used by CUPS.
3077 * For remote jobs, we send all of the files in the argument list.
3080 if (printer
->remote
&& job
->num_files
> 1)
3081 argv
= calloc(7 + job
->num_files
, sizeof(char *));
3083 argv
= calloc(8, sizeof(char *));
3087 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to allocate argument array!");
3088 cupsArrayDelete(filters
);
3090 FilterLevel
-= job
->cost
;
3092 cupsdStopPrinter(printer
, 0);
3096 sprintf(jobid
, "%d", job
->id
);
3098 argv
[0] = printer
->name
;
3100 argv
[2] = job
->username
;
3105 if (printer
->remote
&& job
->num_files
> 1)
3107 for (i
= 0; i
< job
->num_files
; i
++)
3109 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
3111 argv
[6 + i
] = strdup(filename
);
3116 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
,
3117 job
->id
, job
->current_file
+ 1);
3121 for (i
= 0; argv
[i
]; i
++)
3122 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
3123 "argv[%d]=\"%s\"", i
, argv
[i
]);
3126 * Create environment variable strings for the filters...
3129 attr
= ippFindAttribute(job
->attrs
, "attributes-natural-language",
3133 strcpy(apple_language
, "APPLE_LANGUAGE=");
3134 _cupsAppleLanguage(attr
->values
[0].string
.text
,
3135 apple_language
+ 15, sizeof(apple_language
) - 15);
3136 #endif /* __APPLE__ */
3138 switch (strlen(attr
->values
[0].string
.text
))
3142 * This is an unknown or badly formatted language code; use
3143 * the POSIX locale...
3146 strcpy(lang
, "LANG=C");
3151 * Just the language code (ll)...
3154 snprintf(lang
, sizeof(lang
), "LANG=%s.UTF8",
3155 attr
->values
[0].string
.text
);
3160 * Language and country code (ll-cc)...
3163 snprintf(lang
, sizeof(lang
), "LANG=%c%c_%c%c.UTF8",
3164 attr
->values
[0].string
.text
[0],
3165 attr
->values
[0].string
.text
[1],
3166 toupper(attr
->values
[0].string
.text
[3] & 255),
3167 toupper(attr
->values
[0].string
.text
[4] & 255));
3171 attr
= ippFindAttribute(job
->attrs
, "document-format",
3174 (optptr
= strstr(attr
->values
[0].string
.text
, "charset=")) != NULL
)
3175 snprintf(charset
, sizeof(charset
), "CHARSET=%s", optptr
+ 8);
3178 attr
= ippFindAttribute(job
->attrs
, "attributes-charset",
3180 snprintf(charset
, sizeof(charset
), "CHARSET=%s",
3181 attr
->values
[0].string
.text
);
3184 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s/%s",
3185 job
->filetypes
[job
->current_file
]->super
,
3186 job
->filetypes
[job
->current_file
]->type
);
3187 snprintf(device_uri
, sizeof(device_uri
), "DEVICE_URI=%s",
3188 printer
->device_uri
);
3189 snprintf(ppd
, sizeof(ppd
), "PPD=%s/ppd/%s.ppd", ServerRoot
, printer
->name
);
3190 snprintf(printer_info
, sizeof(printer_name
), "PRINTER_INFO=%s",
3191 printer
->info
? printer
->info
: "");
3192 snprintf(printer_location
, sizeof(printer_name
), "PRINTER_LOCATION=%s",
3193 printer
->location
? printer
->location
: "");
3194 snprintf(printer_name
, sizeof(printer_name
), "PRINTER=%s", printer
->name
);
3195 snprintf(rip_max_cache
, sizeof(rip_max_cache
), "RIP_MAX_CACHE=%s", RIPCache
);
3197 envc
= cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
3199 envp
[envc
++] = charset
;
3200 envp
[envc
++] = lang
;
3202 envp
[envc
++] = apple_language
;
3203 #endif /* __APPLE__ */
3204 envp
[envc
++] = ppd
;
3205 envp
[envc
++] = rip_max_cache
;
3206 envp
[envc
++] = content_type
;
3207 envp
[envc
++] = device_uri
;
3208 envp
[envc
++] = printer_info
;
3209 envp
[envc
++] = printer_location
;
3210 envp
[envc
++] = printer_name
;
3211 envp
[envc
++] = banner_page
? "CUPS_FILETYPE=job-sheet" :
3212 "CUPS_FILETYPE=document";
3214 if (!printer
->remote
&& !printer
->raw
)
3216 filter
= (mime_filter_t
*)cupsArrayLast(filters
);
3218 if (printer
->port_monitor
)
3219 filter
= (mime_filter_t
*)cupsArrayPrev(filters
);
3221 if (filter
&& filter
->dst
)
3223 snprintf(final_content_type
, sizeof(final_content_type
),
3224 "FINAL_CONTENT_TYPE=%s/%s",
3225 filter
->dst
->super
, filter
->dst
->type
);
3226 envp
[envc
++] = final_content_type
;
3230 if (Classification
&& !banner_page
)
3232 if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
3233 IPP_TAG_NAME
)) == NULL
)
3234 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3236 else if (attr
->num_values
> 1 &&
3237 strcmp(attr
->values
[1].string
.text
, "none") != 0)
3238 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3239 attr
->values
[1].string
.text
);
3241 snprintf(classification
, sizeof(classification
), "CLASSIFICATION=%s",
3242 attr
->values
[0].string
.text
);
3244 envp
[envc
++] = classification
;
3247 if (job
->dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
3249 snprintf(class_name
, sizeof(class_name
), "CLASS=%s", job
->dest
);
3250 envp
[envc
++] = class_name
;
3253 if (job
->auth_username
)
3254 envp
[envc
++] = job
->auth_username
;
3255 if (job
->auth_domain
)
3256 envp
[envc
++] = job
->auth_domain
;
3257 if (job
->auth_password
)
3258 envp
[envc
++] = job
->auth_password
;
3262 envp
[envc
++] = job
->ccname
;
3263 #endif /* HAVE_GSSAPI */
3267 for (i
= 0; i
< envc
; i
++)
3268 if (!strncmp(envp
[i
], "AUTH_", 5))
3269 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"AUTH_%c****\"", i
,
3271 else if (strncmp(envp
[i
], "DEVICE_URI=", 11))
3272 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"%s\"", i
, envp
[i
]);
3274 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "envp[%d]=\"DEVICE_URI=%s\"", i
,
3275 printer
->sanitized_device_uri
);
3277 if (printer
->remote
)
3278 job
->current_file
= job
->num_files
;
3280 job
->current_file
++;
3283 * Now create processes for all of the filters...
3286 filterfds
[0][0] = -1;
3287 filterfds
[0][1] = -1;
3288 filterfds
[1][0] = -1;
3289 filterfds
[1][1] = -1;
3291 if (!job
->status_buffer
)
3293 if (cupsdOpenPipe(job
->status_pipes
))
3295 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
3296 "Unable to create job status pipes - %s.", strerror(errno
));
3297 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3298 "Unable to create status pipes - %s.", strerror(errno
));
3300 cupsdAddPrinterHistory(printer
);
3302 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3303 "Job canceled because the server could not create the job "
3309 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3310 "start_job: status_pipes = [ %d %d ]",
3311 job
->status_pipes
[0], job
->status_pipes
[1]);
3313 job
->status_buffer
= cupsdStatBufNew(job
->status_pipes
[0], NULL
);
3314 job
->status_level
= CUPSD_LOG_INFO
;
3318 memset(job
->filters
, 0, sizeof(job
->filters
));
3321 job
->profile
= cupsdCreateProfile(job
->id
);
3323 for (i
= 0, slot
= 0, filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
3325 i
++, filter
= (mime_filter_t
*)cupsArrayNext(filters
))
3327 if (filter
->filter
[0] != '/')
3328 snprintf(command
, sizeof(command
), "%s/filter/%s", ServerBin
,
3331 strlcpy(command
, filter
->filter
, sizeof(command
));
3333 if (i
< (cupsArrayCount(filters
) - 1))
3335 if (cupsdOpenPipe(filterfds
[slot
]))
3337 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
3338 "Unable to create job filter pipes - %s.", strerror(errno
));
3339 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3340 "Unable to create filter pipes - %s.", strerror(errno
));
3341 cupsdAddPrinterHistory(printer
);
3343 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3344 "Job canceled because the server could not create the "
3352 if (job
->current_file
== 1)
3354 if (strncmp(printer
->device_uri
, "file:", 5) != 0)
3356 if (cupsdOpenPipe(job
->print_pipes
))
3358 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
3359 "Unable to create job backend pipes - %s.",
3361 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3362 "Unable to create backend pipes - %s.", strerror(errno
));
3363 cupsdAddPrinterHistory(printer
);
3365 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3366 "Job canceled because the server could not create "
3367 "the backend pipes.");
3374 job
->print_pipes
[0] = -1;
3375 if (!strcmp(printer
->device_uri
, "file:/dev/null") ||
3376 !strcmp(printer
->device_uri
, "file:///dev/null"))
3377 job
->print_pipes
[1] = -1;
3380 if (!strncmp(printer
->device_uri
, "file:/dev/", 10))
3381 job
->print_pipes
[1] = open(printer
->device_uri
+ 5,
3383 else if (!strncmp(printer
->device_uri
, "file:///dev/", 12))
3384 job
->print_pipes
[1] = open(printer
->device_uri
+ 7,
3386 else if (!strncmp(printer
->device_uri
, "file:///", 8))
3387 job
->print_pipes
[1] = open(printer
->device_uri
+ 7,
3388 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
3390 job
->print_pipes
[1] = open(printer
->device_uri
+ 5,
3391 O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
3393 if (job
->print_pipes
[1] < 0)
3395 cupsdLogJob(job
, CUPSD_LOG_ERROR
,
3396 "Unable to open output file \"%s\" - %s.",
3397 printer
->device_uri
, strerror(errno
));
3398 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3399 "Unable to open output file \"%s\" - %s.",
3400 printer
->device_uri
, strerror(errno
));
3402 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3403 "Job canceled because the server could not open the "
3409 fcntl(job
->print_pipes
[1], F_SETFD
,
3410 fcntl(job
->print_pipes
[1], F_GETFD
) | FD_CLOEXEC
);
3414 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3415 "start_job: print_pipes = [ %d %d ]",
3416 job
->print_pipes
[0], job
->print_pipes
[1]);
3419 filterfds
[slot
][0] = job
->print_pipes
[0];
3420 filterfds
[slot
][1] = job
->print_pipes
[1];
3423 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "start_job: filter=\"%s\"", command
);
3424 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "start_job: filterfds[%d]=[ %d %d ]",
3425 slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3427 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
3428 filterfds
[slot
][1], job
->status_pipes
[1],
3429 job
->back_pipes
[0], job
->side_pipes
[0], 0,
3430 job
->profile
, job
->filters
+ i
);
3432 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3433 "start_job: Closing filter pipes for slot %d [ %d %d ]...",
3434 !slot
, filterfds
[!slot
][0], filterfds
[!slot
][1]);
3436 cupsdClosePipe(filterfds
[!slot
]);
3440 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Unable to start filter \"%s\" - %s.",
3441 filter
->filter
, strerror(errno
));
3442 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3443 "Unable to start filter \"%s\" - %s.",
3444 filter
->filter
, strerror(errno
));
3446 cupsdAddPrinterHistory(printer
);
3448 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3449 "Job canceled because the server could not execute a "
3455 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Started filter %s (PID %d)", command
,
3462 cupsArrayDelete(filters
);
3466 * Finally, pipe the final output into a backend process if needed...
3469 if (strncmp(printer
->device_uri
, "file:", 5) != 0)
3471 if (job
->current_file
== 1 || printer
->remote
)
3473 sscanf(printer
->device_uri
, "%254[^:]", method
);
3474 snprintf(command
, sizeof(command
), "%s/backend/%s", ServerBin
, method
);
3477 * See if the backend needs to run as root...
3482 else if (stat(command
, &backinfo
))
3485 backroot
= !(backinfo
.st_mode
& (S_IRWXG
| S_IRWXO
));
3487 argv
[0] = printer
->sanitized_device_uri
;
3489 filterfds
[slot
][0] = -1;
3490 filterfds
[slot
][1] = -1;
3492 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "start_job: backend=\"%s\"", command
);
3493 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "start_job: filterfds[%d] = [ %d %d ]",
3494 slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3496 pid
= cupsdStartProcess(command
, argv
, envp
, filterfds
[!slot
][0],
3497 filterfds
[slot
][1], job
->status_pipes
[1],
3498 job
->back_pipes
[1], job
->side_pipes
[1],
3499 backroot
, job
->profile
, &(job
->backend
));
3503 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Unable to start backend \"%s\" - %s.",
3504 method
, strerror(errno
));
3505 snprintf(printer
->state_message
, sizeof(printer
->state_message
),
3506 "Unable to start backend \"%s\" - %s.", method
,
3509 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED
, job
->printer
, job
,
3510 "Job canceled because the server could not execute "
3517 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Started backend %s (PID %d)",
3522 if (job
->current_file
== job
->num_files
)
3524 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3525 "start_job: Closing print pipes [ %d %d ]...",
3526 job
->print_pipes
[0], job
->print_pipes
[1]);
3528 cupsdClosePipe(job
->print_pipes
);
3530 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3531 "start_job: Closing back pipes [ %d %d ]...",
3532 job
->back_pipes
[0], job
->back_pipes
[1]);
3534 cupsdClosePipe(job
->back_pipes
);
3536 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3537 "start_job: Closing side pipes [ %d %d ]...",
3538 job
->side_pipes
[0], job
->side_pipes
[1]);
3540 cupsdClosePipe(job
->side_pipes
);
3542 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3543 "start_job: Closing status output pipe %d...",
3544 job
->status_pipes
[1]);
3546 close(job
->status_pipes
[1]);
3547 job
->status_pipes
[1] = -1;
3552 filterfds
[slot
][0] = -1;
3553 filterfds
[slot
][1] = -1;
3555 if (job
->current_file
== job
->num_files
)
3557 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3558 "start_job: Closing print pipes [ %d %d ]...",
3559 job
->print_pipes
[0], job
->print_pipes
[1]);
3561 cupsdClosePipe(job
->print_pipes
);
3563 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3564 "start_job: Closing status output pipe %d...",
3565 job
->status_pipes
[1]);
3567 close(job
->status_pipes
[1]);
3568 job
->status_pipes
[1] = -1;
3572 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3573 "start_job: Closing filter pipes for slot %d [ %d %d ]...",
3574 slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3575 cupsdClosePipe(filterfds
[slot
]);
3577 if (printer
->remote
&& job
->num_files
> 1)
3579 for (i
= 0; i
< job
->num_files
; i
++)
3585 cupsdAddSelect(job
->status_buffer
->fd
, (cupsd_selfunc_t
)update_job
, NULL
,
3588 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, job
->printer
, job
, "Job #%d started.",
3595 * If we get here, we need to abort the current job and close out all
3596 * files and pipes...
3601 for (slot
= 0; slot
< 2; slot
++)
3603 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3604 "start_job: Closing filter pipes for slot %d [ %d %d ]...",
3605 slot
, filterfds
[slot
][0], filterfds
[slot
][1]);
3606 cupsdClosePipe(filterfds
[slot
]);
3609 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
,
3610 "start_job: Closing status pipes [ %d %d ]...",
3611 job
->status_pipes
[0], job
->status_pipes
[1]);
3612 cupsdClosePipe(job
->status_pipes
);
3613 cupsdStatBufDelete(job
->status_buffer
);
3615 job
->status_buffer
= NULL
;
3617 cupsArrayDelete(filters
);
3619 if (printer
->remote
&& job
->num_files
> 1)
3621 for (i
= 0; i
< job
->num_files
; i
++)
3627 cupsdStopJob(job
, 0);
3632 * 'unload_job()' - Unload a job from memory.
3636 unload_job(cupsd_job_t
*job
) /* I - Job */
3641 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job %d] Unloading...", job
->id
);
3643 ippDelete(job
->attrs
);
3648 job
->job_sheets
= NULL
;
3649 job
->printer_message
= NULL
;
3650 job
->printer_reasons
= NULL
;
3655 * 'update_job()' - Read a status update from a job's filters.
3659 update_job(cupsd_job_t
*job
) /* I - Job to check */
3661 int i
; /* Looping var */
3662 int copies
; /* Number of copies printed */
3663 char message
[1024], /* Message text */
3664 *ptr
; /* Pointer update... */
3665 int loglevel
, /* Log level for message */
3666 event
= 0; /* Events? */
3669 while ((ptr
= cupsdStatBufUpdate(job
->status_buffer
, &loglevel
,
3670 message
, sizeof(message
))) != NULL
)
3673 * Process page and printer state messages as needed...
3676 if (loglevel
== CUPSD_LOG_PAGE
)
3679 * Page message; send the message to the page_log file and update the
3680 * job sheet count...
3683 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "PAGE: %s", message
);
3687 if (!strncasecmp(message
, "total ", 6))
3690 * Got a total count of pages from a backend or filter...
3693 copies
= atoi(message
+ 6);
3694 copies
-= job
->sheets
->values
[0].integer
; /* Just track the delta */
3696 else if (!sscanf(message
, "%*d%d", &copies
))
3699 job
->sheets
->values
[0].integer
+= copies
;
3701 if (job
->printer
->page_limit
)
3703 cupsd_quota_t
*q
= cupsdUpdateQuota(job
->printer
, job
->username
,
3707 if (AppleQuotas
&& q
->page_count
== -3)
3710 * Quota limit exceeded, cancel job in progress immediately...
3713 cupsdLogJob(job
, CUPSD_LOG_INFO
,
3714 "Canceled because pages exceed user %s "
3715 "quota limit on printer %s (%s).",
3716 job
->username
, job
->printer
->name
,
3717 job
->printer
->info
);
3719 cupsdCancelJob(job
, 1, IPP_JOB_CANCELED
);
3724 #endif /* __APPLE__ */
3728 cupsdLogPage(job
, message
);
3731 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
3732 "Printed %d page(s).", job
->sheets
->values
[0].integer
);
3734 else if (loglevel
== CUPSD_LOG_STATE
)
3736 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "STATE: %s", message
);
3738 if (!strcmp(message
, "paused"))
3740 cupsdStopPrinter(job
->printer
, 1);
3745 cupsdSetPrinterReasons(job
->printer
, message
);
3746 cupsdAddPrinterHistory(job
->printer
);
3747 event
|= CUPSD_EVENT_PRINTER_STATE
;
3750 update_job_attrs(job
, 0);
3752 else if (loglevel
== CUPSD_LOG_ATTR
)
3755 * Set attribute(s)...
3758 int num_attrs
; /* Number of attributes */
3759 cups_option_t
*attrs
; /* Attributes */
3760 const char *attr
; /* Attribute */
3763 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "ATTR: %s", message
);
3765 num_attrs
= cupsParseOptions(message
, 0, &attrs
);
3767 if ((attr
= cupsGetOption("auth-info-required", num_attrs
,
3770 cupsdSetAuthInfoRequired(job
->printer
, attr
, NULL
);
3771 cupsdSetPrinterAttrs(job
->printer
);
3773 if (job
->printer
->type
& CUPS_PRINTER_DISCOVERED
)
3774 cupsdMarkDirty(CUPSD_DIRTY_REMOTE
);
3776 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3779 if ((attr
= cupsGetOption("job-media-progress", num_attrs
,
3782 int progress
= atoi(attr
);
3785 if (progress
>= 0 && progress
<= 100)
3787 job
->progress
= progress
;
3790 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS
, job
->printer
, job
,
3791 "Printing page %d, %d%%",
3792 job
->sheets
->values
[0].integer
, job
->progress
);
3796 if ((attr
= cupsGetOption("printer-alert", num_attrs
, attrs
)) != NULL
)
3798 cupsdSetString(&job
->printer
->alert
, attr
);
3799 event
|= CUPSD_EVENT_PRINTER_STATE
;
3802 if ((attr
= cupsGetOption("printer-alert-description", num_attrs
,
3805 cupsdSetString(&job
->printer
->alert_description
, attr
);
3806 event
|= CUPSD_EVENT_PRINTER_STATE
;
3809 if ((attr
= cupsGetOption("marker-colors", num_attrs
, attrs
)) != NULL
)
3811 cupsdSetPrinterAttr(job
->printer
, "marker-colors", (char *)attr
);
3812 job
->printer
->marker_time
= time(NULL
);
3813 event
|= CUPSD_EVENT_PRINTER_STATE
;
3814 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3817 if ((attr
= cupsGetOption("marker-levels", num_attrs
, attrs
)) != NULL
)
3819 cupsdSetPrinterAttr(job
->printer
, "marker-levels", (char *)attr
);
3820 job
->printer
->marker_time
= time(NULL
);
3821 event
|= CUPSD_EVENT_PRINTER_STATE
;
3822 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3825 if ((attr
= cupsGetOption("marker-low-levels", num_attrs
, attrs
)) != NULL
)
3827 cupsdSetPrinterAttr(job
->printer
, "marker-low-levels", (char *)attr
);
3828 job
->printer
->marker_time
= time(NULL
);
3829 event
|= CUPSD_EVENT_PRINTER_STATE
;
3830 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3833 if ((attr
= cupsGetOption("marker-high-levels", num_attrs
, attrs
)) != NULL
)
3835 cupsdSetPrinterAttr(job
->printer
, "marker-high-levels", (char *)attr
);
3836 job
->printer
->marker_time
= time(NULL
);
3837 event
|= CUPSD_EVENT_PRINTER_STATE
;
3838 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3841 if ((attr
= cupsGetOption("marker-message", num_attrs
, attrs
)) != NULL
)
3843 cupsdSetPrinterAttr(job
->printer
, "marker-message", (char *)attr
);
3844 job
->printer
->marker_time
= time(NULL
);
3845 event
|= CUPSD_EVENT_PRINTER_STATE
;
3846 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3849 if ((attr
= cupsGetOption("marker-names", num_attrs
, attrs
)) != NULL
)
3851 cupsdSetPrinterAttr(job
->printer
, "marker-names", (char *)attr
);
3852 job
->printer
->marker_time
= time(NULL
);
3853 event
|= CUPSD_EVENT_PRINTER_STATE
;
3854 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3857 if ((attr
= cupsGetOption("marker-types", num_attrs
, attrs
)) != NULL
)
3859 cupsdSetPrinterAttr(job
->printer
, "marker-types", (char *)attr
);
3860 job
->printer
->marker_time
= time(NULL
);
3861 event
|= CUPSD_EVENT_PRINTER_STATE
;
3862 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3865 cupsFreeOptions(num_attrs
, attrs
);
3867 else if (loglevel
== CUPSD_LOG_PPD
)
3870 * Set attribute(s)...
3873 int num_keywords
; /* Number of keywords */
3874 cups_option_t
*keywords
; /* Keywords */
3877 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "PPD: %s", message
);
3879 num_keywords
= cupsParseOptions(message
, 0, &keywords
);
3881 if (cupsdUpdatePrinterPPD(job
->printer
, num_keywords
, keywords
))
3882 cupsdSetPrinterAttrs(job
->printer
);
3884 cupsFreeOptions(num_keywords
, keywords
);
3887 else if (!strncmp(message
, "recoverable:", 12))
3890 while (isspace(*ptr
& 255))
3895 cupsdSetPrinterReasons(job
->printer
,
3896 "+com.apple.print.recoverable-warning");
3897 cupsdSetString(&job
->printer
->recoverable
, ptr
);
3898 cupsdAddPrinterHistory(job
->printer
);
3899 event
|= CUPSD_EVENT_PRINTER_STATE
;
3902 else if (!strncmp(message
, "recovered:", 10))
3904 cupsdSetPrinterReasons(job
->printer
,
3905 "-com.apple.print.recoverable-warning");
3908 while (isspace(*ptr
& 255))
3911 cupsdSetString(&job
->printer
->recoverable
, ptr
);
3912 cupsdAddPrinterHistory(job
->printer
);
3913 event
|= CUPSD_EVENT_PRINTER_STATE
;
3915 #endif /* __APPLE__ */
3918 cupsdLogJob(job
, loglevel
, "%s", message
);
3920 if (loglevel
< CUPSD_LOG_DEBUG
)
3922 strlcpy(job
->printer
->state_message
, message
,
3923 sizeof(job
->printer
->state_message
));
3924 cupsdAddPrinterHistory(job
->printer
);
3926 event
|= CUPSD_EVENT_PRINTER_STATE
;
3928 if (loglevel
<= job
->status_level
)
3931 * Some messages show in the printer-state-message attribute...
3934 if (loglevel
!= CUPSD_LOG_NOTICE
)
3935 job
->status_level
= loglevel
;
3937 update_job_attrs(job
, 1);
3942 if (!strchr(job
->status_buffer
->buffer
, '\n'))
3946 if (event
& CUPSD_EVENT_PRINTER_STATE
)
3947 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, job
->printer
, NULL
,
3948 (job
->printer
->type
& CUPS_PRINTER_CLASS
) ?
3949 "Class \"%s\" state changed." :
3950 "Printer \"%s\" state changed.",
3951 job
->printer
->name
);
3953 if (ptr
== NULL
&& !job
->status_buffer
->bufused
)
3956 * See if all of the filters and the backend have returned their
3960 for (i
= 0; job
->filters
[i
] < 0; i
++);
3962 if (job
->filters
[i
])
3965 if (job
->current_file
>= job
->num_files
&& job
->backend
> 0)
3969 * Handle the end of job stuff...
3972 cupsdFinishJob(job
);
3978 * 'update_job_attrs()' - Update the job-printer-* attributes.
3982 update_job_attrs(cupsd_job_t
*job
, /* I - Job to update */
3983 int do_message
)/* I - 1 = update job-printer-state message */
3985 int i
; /* Looping var */
3986 int num_reasons
; /* Actual number of reasons */
3987 const char * const *reasons
; /* Reasons */
3988 static const char *none
= "none", /* "none" */
3994 * Get/create the job-printer-state-* attributes...
3997 if (!job
->printer_message
)
3999 if ((job
->printer_message
= ippFindAttribute(job
->attrs
,
4000 "job-printer-state-message",
4001 IPP_TAG_TEXT
)) == NULL
)
4002 job
->printer_message
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_TEXT
,
4003 "job-printer-state-message",
4007 if (!job
->printer_reasons
)
4008 job
->printer_reasons
= ippFindAttribute(job
->attrs
,
4009 "job-printer-state-reasons",
4013 * If the job isn't printing, return now...
4020 * Otherwise copy the printer-state-message value...
4023 if (job
->printer
->state_message
[0] &&
4024 (do_message
|| !job
->printer_message
->values
[0].string
.text
[0]))
4025 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
),
4026 job
->printer
->state_message
);
4029 * ... and the printer-state-reasons value...
4032 if (job
->printer
->num_reasons
== 0)
4035 reasons
= job
->printer
->state
== IPP_PRINTER_STOPPED
? &paused
: &none
;
4039 num_reasons
= job
->printer
->num_reasons
;
4040 reasons
= (const char * const *)job
->printer
->reasons
;
4043 if (!job
->printer_reasons
|| job
->printer_reasons
->num_values
!= num_reasons
)
4045 ippDeleteAttribute(job
->attrs
, job
->printer_reasons
);
4047 job
->printer_reasons
= ippAddStrings(job
->attrs
,
4048 IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
4049 "job-printer-state-reasons",
4050 num_reasons
, NULL
, NULL
);
4052 else if (job
->printer_reasons
)
4054 for (i
= 0; i
< job
->printer_reasons
->num_values
; i
++)
4055 _cupsStrFree(job
->printer_reasons
->values
[i
].string
.text
);
4058 for (i
= 0; i
< num_reasons
; i
++)
4059 job
->printer_reasons
->values
[i
].string
.text
= _cupsStrAlloc(reasons
[i
]);
4064 * End of "$Id: job.c 7902 2008-09-03 14:20:17Z mike $".