]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/job.c
Merge changes from CUPS 1.6svn-r10437.
[thirdparty/cups.git] / scheduler / job.c
1 /*
2 * "$Id: job.c 7902 2008-09-03 14:20:17Z mike $"
3 *
4 * Job management routines for the CUPS scheduler.
5 *
6 * Copyright 2007-2012 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
8 *
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/".
14 *
15 * Contents:
16 *
17 * cupsdAddJob() - Add a new job to the job queue.
18 * cupsdCancelJobs() - Cancel all jobs for the given
19 * destination/user.
20 * cupsdCheckJobs() - Check the pending jobs and start any if the
21 * destination is available.
22 * cupsdCleanJobs() - Clean out old jobs.
23 * cupsdContinueJob() - Continue printing with the next file in a
24 * job.
25 * cupsdDeleteJob() - Free all memory used by a job.
26 * cupsdFreeAllJobs() - Free all jobs from memory.
27 * cupsdFindJob() - Find the specified job.
28 * cupsdGetPrinterJobCount() - Get the number of pending, processing, or
29 * held jobs in a printer or class.
30 * cupsdGetUserJobCount() - Get the number of pending, processing, or
31 * held jobs for a user.
32 * cupsdLoadAllJobs() - Load all jobs from disk.
33 * cupsdLoadJob() - Load a single job.
34 * cupsdMoveJob() - Move the specified job to a different
35 * destination.
36 * cupsdReleaseJob() - Release the specified job.
37 * cupsdRestartJob() - Restart the specified job.
38 * cupsdSaveAllJobs() - Save a summary of all jobs to disk.
39 * cupsdSaveJob() - Save a job to disk.
40 * cupsdSetJobHoldUntil() - Set the hold time for a job.
41 * cupsdSetJobPriority() - Set the priority of a job, moving it up/down
42 * in the list as needed.
43 * cupsdSetJobState() - Set the state of the specified print job.
44 * cupsdStopAllJobs() - Stop all print jobs.
45 * cupsdUnloadCompletedJobs() - Flush completed job history from memory.
46 * cupsdUpdateJobs() - Update the history/file files for all jobs.
47 * compare_active_jobs() - Compare the job IDs and priorities of two
48 * jobs.
49 * compare_jobs() - Compare the job IDs of two jobs.
50 * dump_job_history() - Dump any debug messages for a job.
51 * free_job_history() - Free any log history.
52 * finalize_job() - Cleanup after job filter processes and
53 * support data.
54 * get_options() - Get a string containing the job options.
55 * ipp_length() - Compute the size of the buffer needed to hold
56 * the textual IPP attributes.
57 * load_job_cache() - Load jobs from the job.cache file.
58 * load_next_job_id() - Load the NextJobId value from the job.cache
59 * file.
60 * load_request_root() - Load jobs from the RequestRoot directory.
61 * remove_job_files() - Remove the document files for a job.
62 * remove_job_history() - Remove the control file for a job.
63 * set_time() - Set one of the "time-at-xyz" attributes.
64 * start_job() - Start a print job.
65 * stop_job() - Stop a print job.
66 * unload_job() - Unload a job from memory.
67 * update_job() - Read a status update from a job's filters.
68 * update_job_attrs() - Update the job-printer-* attributes.
69 */
70
71 /*
72 * Include necessary headers...
73 */
74
75 #include "cupsd.h"
76 #include <grp.h>
77 #include <cups/backend.h>
78 #include <cups/dir.h>
79 #ifdef __APPLE__
80 # include <IOKit/pwr_mgt/IOPMLib.h>
81 # ifdef HAVE_IOKIT_PWR_MGT_IOPMLIBPRIVATE_H
82 # include <IOKit/pwr_mgt/IOPMLibPrivate.h>
83 # endif /* HAVE_IOKIT_PWR_MGT_IOPMLIBPRIVATE_H */
84 #endif /* __APPLE__ */
85
86
87 /*
88 * Design Notes for Job Management
89 * -------------------------------
90 *
91 * STATE CHANGES
92 *
93 * pending Do nothing/check jobs
94 * pending-held Send SIGTERM to filters and backend
95 * processing Do nothing/start job
96 * stopped Send SIGKILL to filters and backend
97 * canceled Send SIGTERM to filters and backend
98 * aborted Finalize
99 * completed Finalize
100 *
101 * Finalize clears the printer <-> job association, deletes the status
102 * buffer, closes all of the pipes, etc. and doesn't get run until all of
103 * the print processes are finished.
104 *
105 * UNLOADING OF JOBS (cupsdUnloadCompletedJobs)
106 *
107 * We unload the job attributes when they are not needed to reduce overall
108 * memory consumption. We don't unload jobs where job->state_value <
109 * IPP_JOB_STOPPED, job->printer != NULL, or job->access_time is recent.
110 *
111 * STARTING OF JOBS (start_job)
112 *
113 * When a job is started, a status buffer, several pipes, a security
114 * profile, and a backend process are created for the life of that job.
115 * These are shared for every file in a job. For remote print jobs, the
116 * IPP backend is provided with every file in the job and no filters are
117 * run.
118 *
119 * The job->printer member tracks which printer is printing a job, which
120 * can be different than the destination in job->dest for classes. The
121 * printer object also has a job pointer to track which job is being
122 * printed.
123 *
124 * PRINTING OF JOB FILES (cupsdContinueJob)
125 *
126 * Each file in a job is filtered by 0 or more programs. After getting the
127 * list of filters needed and the total cost, the job is either passed or
128 * put back to the processing state until the current FilterLevel comes down
129 * enough to allow printing.
130 *
131 * If we can print, we build a string for the print options and run each of
132 * the filters, piping the output from one into the next.
133 *
134 * JOB STATUS UPDATES (update_job)
135 *
136 * The update_job function gets called whenever there are pending messages
137 * on the status pipe. These generally are updates to the marker-*,
138 * printer-state-message, or printer-state-reasons attributes. On EOF,
139 * finalize_job is called to clean up.
140 *
141 * FINALIZING JOBS (finalize_job)
142 *
143 * When all filters and the backend are done, we set the job state to
144 * completed (no errors), aborted (filter errors or abort-job policy),
145 * pending-held (auth required or retry-job policy), or pending
146 * (retry-current-job or stop-printer policies) as appropriate.
147 *
148 * Then we close the pipes and free the status buffers and profiles.
149 *
150 * JOB FILE COMPLETION (process_children in main.c)
151 *
152 * For multiple-file jobs, process_children (in main.c) sees that all
153 * filters have exited and calls in to print the next file if there are
154 * more files in the job, otherwise it waits for the backend to exit and
155 * update_job to do the cleanup.
156 */
157
158
159 /*
160 * Local globals...
161 */
162
163 static mime_filter_t gziptoany_filter =
164 {
165 NULL, /* Source type */
166 NULL, /* Destination type */
167 0, /* Cost */
168 "gziptoany" /* Filter program to run */
169 };
170
171
172 /*
173 * Local functions...
174 */
175
176 static int compare_active_jobs(void *first, void *second, void *data);
177 static int compare_jobs(void *first, void *second, void *data);
178 static void dump_job_history(cupsd_job_t *job);
179 static void finalize_job(cupsd_job_t *job, int set_job_state);
180 static void free_job_history(cupsd_job_t *job);
181 static char *get_options(cupsd_job_t *job, int banner_page, char *copies,
182 size_t copies_size, char *title,
183 size_t title_size);
184 static size_t ipp_length(ipp_t *ipp);
185 static void load_job_cache(const char *filename);
186 static void load_next_job_id(const char *filename);
187 static void load_request_root(void);
188 static void remove_job_files(cupsd_job_t *job);
189 static void remove_job_history(cupsd_job_t *job);
190 static void set_time(cupsd_job_t *job, const char *name);
191 static void start_job(cupsd_job_t *job, cupsd_printer_t *printer);
192 static void stop_job(cupsd_job_t *job, cupsd_jobaction_t action);
193 static void unload_job(cupsd_job_t *job);
194 static void update_job(cupsd_job_t *job);
195 static void update_job_attrs(cupsd_job_t *job, int do_message);
196
197
198 /*
199 * 'cupsdAddJob()' - Add a new job to the job queue.
200 */
201
202 cupsd_job_t * /* O - New job record */
203 cupsdAddJob(int priority, /* I - Job priority */
204 const char *dest) /* I - Job destination */
205 {
206 cupsd_job_t *job; /* New job record */
207
208
209 if ((job = calloc(sizeof(cupsd_job_t), 1)) == NULL)
210 return (NULL);
211
212 job->id = NextJobId ++;
213 job->priority = priority;
214 job->back_pipes[0] = -1;
215 job->back_pipes[1] = -1;
216 job->print_pipes[0] = -1;
217 job->print_pipes[1] = -1;
218 job->side_pipes[0] = -1;
219 job->side_pipes[1] = -1;
220 job->status_pipes[0] = -1;
221 job->status_pipes[1] = -1;
222
223 cupsdSetString(&job->dest, dest);
224
225 /*
226 * Add the new job to the "all jobs" and "active jobs" lists...
227 */
228
229 cupsArrayAdd(Jobs, job);
230 cupsArrayAdd(ActiveJobs, job);
231
232 return (job);
233 }
234
235
236 /*
237 * 'cupsdCancelJobs()' - Cancel all jobs for the given destination/user.
238 */
239
240 void
241 cupsdCancelJobs(const char *dest, /* I - Destination to cancel */
242 const char *username, /* I - Username or NULL */
243 int purge) /* I - Purge jobs? */
244 {
245 cupsd_job_t *job; /* Current job */
246
247
248 for (job = (cupsd_job_t *)cupsArrayFirst(Jobs);
249 job;
250 job = (cupsd_job_t *)cupsArrayNext(Jobs))
251 {
252 if ((!job->dest || !job->username) && !cupsdLoadJob(job))
253 continue;
254
255 if ((!dest || !strcmp(job->dest, dest)) &&
256 (!username || !strcmp(job->username, username)))
257 {
258 /*
259 * Cancel all jobs matching this destination/user...
260 */
261
262 if (purge)
263 cupsdSetJobState(job, IPP_JOB_CANCELED, CUPSD_JOB_PURGE,
264 "Job purged by user.");
265 else if (job->state_value < IPP_JOB_CANCELED)
266 cupsdSetJobState(job, IPP_JOB_CANCELED, CUPSD_JOB_DEFAULT,
267 "Job canceled by user.");
268 }
269 }
270
271 cupsdCheckJobs();
272 }
273
274
275 /*
276 * 'cupsdCheckJobs()' - Check the pending jobs and start any if the destination
277 * is available.
278 */
279
280 void
281 cupsdCheckJobs(void)
282 {
283 cupsd_job_t *job; /* Current job in queue */
284 cupsd_printer_t *printer, /* Printer destination */
285 *pclass; /* Printer class destination */
286 ipp_attribute_t *attr; /* Job attribute */
287 time_t curtime; /* Current time */
288
289
290 cupsdLogMessage(CUPSD_LOG_DEBUG2,
291 "cupsdCheckJobs: %d active jobs, sleeping=%d, reload=%d",
292 cupsArrayCount(ActiveJobs), Sleeping, NeedReload);
293
294 curtime = time(NULL);
295
296 for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs);
297 job;
298 job = (cupsd_job_t *)cupsArrayNext(ActiveJobs))
299 {
300 /*
301 * Kill jobs if they are unresponsive...
302 */
303
304 if (job->kill_time && job->kill_time <= curtime)
305 {
306 cupsdLogMessage(CUPSD_LOG_ERROR, "[Job %d] Stopping unresponsive job.",
307 job->id);
308
309 stop_job(job, CUPSD_JOB_FORCE);
310 continue;
311 }
312
313 /*
314 * Cancel stuck jobs...
315 */
316
317 if (job->cancel_time && job->cancel_time <= curtime)
318 {
319 cupsdSetJobState(job, IPP_JOB_CANCELED, CUPSD_JOB_DEFAULT,
320 "Canceling stuck job after %d seconds.", MaxJobTime);
321 continue;
322 }
323
324 /*
325 * Start held jobs if they are ready...
326 */
327
328 if (job->state_value == IPP_JOB_HELD &&
329 job->hold_until &&
330 job->hold_until < curtime)
331 {
332 if (job->pending_timeout)
333 {
334 /*
335 * This job is pending; check that we don't have an active Send-Document
336 * operation in progress on any of the client connections, then timeout
337 * the job so we can start printing...
338 */
339
340 cupsd_client_t *con; /* Current client connection */
341
342
343 for (con = (cupsd_client_t *)cupsArrayFirst(Clients);
344 con;
345 con = (cupsd_client_t *)cupsArrayNext(Clients))
346 if (con->request &&
347 con->request->request.op.operation_id == IPP_SEND_DOCUMENT)
348 break;
349
350 if (con)
351 continue;
352
353 if (cupsdTimeoutJob(job))
354 continue;
355 }
356
357 cupsdSetJobState(job, IPP_JOB_PENDING, CUPSD_JOB_DEFAULT,
358 "Job submission timed out.");
359 }
360
361 /*
362 * Continue jobs that are waiting on the FilterLimit...
363 */
364
365 if (job->pending_cost > 0 &&
366 ((FilterLevel + job->pending_cost) < FilterLimit || FilterLevel == 0))
367 cupsdContinueJob(job);
368
369 /*
370 * Start pending jobs if the destination is available...
371 */
372
373 if (job->state_value == IPP_JOB_PENDING && !NeedReload &&
374 #ifndef kIOPMAssertionTypeDenySystemSleep
375 !Sleeping &&
376 #endif /* !kIOPMAssertionTypeDenySystemSleep */
377 !DoingShutdown && !job->printer)
378 {
379 printer = cupsdFindDest(job->dest);
380 pclass = NULL;
381
382 while (printer && (printer->type & CUPS_PRINTER_CLASS))
383 {
384 /*
385 * If the class is remote, just pass it to the remote server...
386 */
387
388 pclass = printer;
389
390 if (pclass->state == IPP_PRINTER_STOPPED)
391 printer = NULL;
392 else if (pclass->type & CUPS_PRINTER_REMOTE)
393 break;
394 else
395 printer = cupsdFindAvailablePrinter(printer->name);
396 }
397
398 if (!printer && !pclass)
399 {
400 /*
401 * Whoa, the printer and/or class for this destination went away;
402 * cancel the job...
403 */
404
405 cupsdSetJobState(job, IPP_JOB_ABORTED, CUPSD_JOB_PURGE,
406 "Job aborted because the destination printer/class "
407 "has gone away.");
408 }
409 else if (printer && !printer->holding_new_jobs)
410 {
411 /*
412 * See if the printer is available or remote and not printing a job;
413 * if so, start the job...
414 */
415
416 if (pclass)
417 {
418 /*
419 * Add/update a job-actual-printer-uri attribute for this job
420 * so that we know which printer actually printed the job...
421 */
422
423 if ((attr = ippFindAttribute(job->attrs, "job-actual-printer-uri",
424 IPP_TAG_URI)) != NULL)
425 cupsdSetString(&attr->values[0].string.text, printer->uri);
426 else
427 ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_URI,
428 "job-actual-printer-uri", NULL, printer->uri);
429
430 job->dirty = 1;
431 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
432 }
433
434 if (printer->state == IPP_PRINTER_IDLE)
435 {
436 /*
437 * Start the job...
438 */
439
440 start_job(job, printer);
441 }
442 }
443 }
444 }
445 }
446
447
448 /*
449 * 'cupsdCleanJobs()' - Clean out old jobs.
450 */
451
452 void
453 cupsdCleanJobs(void)
454 {
455 cupsd_job_t *job; /* Current job */
456 time_t curtime; /* Current time */
457
458
459 cupsdLogMessage(CUPSD_LOG_DEBUG2,
460 "cupsdCleanJobs: MaxJobs=%d, JobHistory=%d, JobFiles=%d",
461 MaxJobs, JobHistory, JobFiles);
462
463 if (MaxJobs <= 0 && JobHistory == INT_MAX && JobFiles == INT_MAX)
464 return;
465
466 curtime = time(NULL);
467 JobHistoryUpdate = 0;
468
469 for (job = (cupsd_job_t *)cupsArrayFirst(Jobs);
470 job;
471 job = (cupsd_job_t *)cupsArrayNext(Jobs))
472 {
473 if (job->state_value >= IPP_JOB_CANCELED && !job->printer)
474 {
475 /*
476 * Expire old jobs (or job files)...
477 */
478
479 if ((MaxJobs > 0 && cupsArrayCount(Jobs) >= MaxJobs) ||
480 (job->history_time && job->history_time <= curtime))
481 {
482 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Removing from history.");
483 cupsdDeleteJob(job, CUPSD_JOB_PURGE);
484 }
485 else if (job->file_time && job->file_time <= curtime)
486 {
487 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Removing document files.");
488 remove_job_files(job);
489
490 if (job->history_time < JobHistoryUpdate || !JobHistoryUpdate)
491 JobHistoryUpdate = job->history_time;
492 }
493 else
494 {
495 if (job->history_time < JobHistoryUpdate || !JobHistoryUpdate)
496 JobHistoryUpdate = job->history_time;
497
498 if (job->file_time < JobHistoryUpdate || !JobHistoryUpdate)
499 JobHistoryUpdate = job->file_time;
500 }
501 }
502 }
503
504 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdCleanJobs: JobHistoryUpdate=%ld",
505 (long)JobHistoryUpdate);
506 }
507
508
509 /*
510 * 'cupsdContinueJob()' - Continue printing with the next file in a job.
511 */
512
513 void
514 cupsdContinueJob(cupsd_job_t *job) /* I - Job */
515 {
516 int i; /* Looping var */
517 int slot; /* Pipe slot */
518 cups_array_t *filters = NULL,/* Filters for job */
519 *prefilters; /* Filters with prefilters */
520 mime_filter_t *filter, /* Current filter */
521 *prefilter, /* Prefilter */
522 port_monitor; /* Port monitor filter */
523 char scheme[255]; /* Device URI scheme */
524 ipp_attribute_t *attr; /* Current attribute */
525 const char *ptr, /* Pointer into value */
526 *abort_message; /* Abort message */
527 ipp_jstate_t abort_state = IPP_JOB_STOPPED;
528 /* New job state on abort */
529 struct stat backinfo; /* Backend file information */
530 int backroot; /* Run backend as root? */
531 int pid; /* Process ID of new filter process */
532 int banner_page; /* 1 if banner page, 0 otherwise */
533 int filterfds[2][2] = { { -1, -1 }, { -1, -1 } };
534 /* Pipes used between filters */
535 int envc; /* Number of environment variables */
536 struct stat fileinfo; /* Job file information */
537 char **argv = NULL, /* Filter command-line arguments */
538 filename[1024], /* Job filename */
539 command[1024], /* Full path to command */
540 jobid[255], /* Job ID string */
541 title[IPP_MAX_NAME],
542 /* Job title string */
543 copies[255], /* # copies string */
544 *options, /* Options string */
545 *envp[MAX_ENV + 21],
546 /* Environment variables */
547 charset[255], /* CHARSET env variable */
548 class_name[255],/* CLASS env variable */
549 classification[1024],
550 /* CLASSIFICATION env variable */
551 content_type[1024],
552 /* CONTENT_TYPE env variable */
553 device_uri[1024],
554 /* DEVICE_URI env variable */
555 final_content_type[1024],
556 /* FINAL_CONTENT_TYPE env variable */
557 lang[255], /* LANG env variable */
558 #ifdef __APPLE__
559 apple_language[255],
560 /* APPLE_LANGUAGE env variable */
561 #endif /* __APPLE__ */
562 auth_info_required[255],
563 /* AUTH_INFO_REQUIRED env variable */
564 ppd[1024], /* PPD env variable */
565 printer_info[255],
566 /* PRINTER_INFO env variable */
567 printer_location[255],
568 /* PRINTER_LOCATION env variable */
569 printer_name[255],
570 /* PRINTER env variable */
571 *printer_state_reasons = NULL,
572 /* PRINTER_STATE_REASONS env var */
573 rip_max_cache[255];
574 /* RIP_MAX_CACHE env variable */
575
576
577 cupsdLogMessage(CUPSD_LOG_DEBUG2,
578 "cupsdContinueJob(job=%p(%d)): current_file=%d, num_files=%d",
579 job, job->id, job->current_file, job->num_files);
580
581 /*
582 * Figure out what filters are required to convert from
583 * the source to the destination type...
584 */
585
586 FilterLevel -= job->cost;
587
588 job->cost = 0;
589 job->pending_cost = 0;
590
591 memset(job->filters, 0, sizeof(job->filters));
592
593
594 if (job->printer->raw)
595 {
596 /*
597 * Remote jobs and raw queues go directly to the printer without
598 * filtering...
599 */
600
601 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Sending job to queue tagged as raw...");
602 }
603 else
604 {
605 /*
606 * Local jobs get filtered...
607 */
608
609 snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot,
610 job->id, job->current_file + 1);
611 if (stat(filename, &fileinfo))
612 fileinfo.st_size = 0;
613
614 filters = mimeFilter2(MimeDatabase, job->filetypes[job->current_file],
615 fileinfo.st_size, job->printer->filetype,
616 &(job->cost));
617
618 if (!filters)
619 {
620 cupsdLogJob(job, CUPSD_LOG_ERROR,
621 "Unable to convert file %d to printable format.",
622 job->current_file);
623
624 abort_message = "Aborting job because it cannot be printed.";
625 abort_state = IPP_JOB_ABORTED;
626
627 ippSetString(job->attrs, &job->reasons, 0, "document-unprintable-error");
628 goto abort_job;
629 }
630
631 /*
632 * Remove NULL ("-") filters...
633 */
634
635 for (filter = (mime_filter_t *)cupsArrayFirst(filters);
636 filter;
637 filter = (mime_filter_t *)cupsArrayNext(filters))
638 if (!strcmp(filter->filter, "-"))
639 cupsArrayRemove(filters, filter);
640
641 if (cupsArrayCount(filters) == 0)
642 {
643 cupsArrayDelete(filters);
644 filters = NULL;
645 }
646
647 /*
648 * If this printer has any pre-filters, insert the required pre-filter
649 * in the filters array...
650 */
651
652 if (job->printer->prefiltertype && filters)
653 {
654 prefilters = cupsArrayNew(NULL, NULL);
655
656 for (filter = (mime_filter_t *)cupsArrayFirst(filters);
657 filter;
658 filter = (mime_filter_t *)cupsArrayNext(filters))
659 {
660 if ((prefilter = mimeFilterLookup(MimeDatabase, filter->src,
661 job->printer->prefiltertype)))
662 {
663 cupsArrayAdd(prefilters, prefilter);
664 job->cost += prefilter->cost;
665 }
666
667 cupsArrayAdd(prefilters, filter);
668 }
669
670 cupsArrayDelete(filters);
671 filters = prefilters;
672 }
673 }
674
675 /*
676 * Set a minimum cost of 100 for all jobs so that FilterLimit
677 * works with raw queues and other low-cost paths.
678 */
679
680 if (job->cost < 100)
681 job->cost = 100;
682
683 /*
684 * See if the filter cost is too high...
685 */
686
687 if ((FilterLevel + job->cost) > FilterLimit && FilterLevel > 0 &&
688 FilterLimit > 0)
689 {
690 /*
691 * Don't print this job quite yet...
692 */
693
694 cupsArrayDelete(filters);
695
696 cupsdLogJob(job, CUPSD_LOG_INFO,
697 "Holding because filter limit has been reached.");
698 cupsdLogJob(job, CUPSD_LOG_DEBUG2,
699 "cupsdContinueJob: file=%d, cost=%d, level=%d, limit=%d",
700 job->current_file, job->cost, FilterLevel,
701 FilterLimit);
702
703 job->pending_cost = job->cost;
704 job->cost = 0;
705 return;
706 }
707
708 FilterLevel += job->cost;
709
710 /*
711 * Add decompression/raw filter as needed...
712 */
713
714 if ((!job->printer->raw && job->compressions[job->current_file]) ||
715 (!filters && !job->printer->remote &&
716 (job->num_files > 1 || !strncmp(job->printer->device_uri, "file:", 5))))
717 {
718 /*
719 * Add gziptoany filter to the front of the list...
720 */
721
722 if (!filters)
723 filters = cupsArrayNew(NULL, NULL);
724
725 if (!cupsArrayInsert(filters, &gziptoany_filter))
726 {
727 cupsdLogJob(job, CUPSD_LOG_DEBUG,
728 "Unable to add decompression filter - %s", strerror(errno));
729
730 cupsArrayDelete(filters);
731
732 abort_message = "Stopping job because the scheduler ran out of memory.";
733
734 goto abort_job;
735 }
736 }
737
738 /*
739 * Add port monitor, if any...
740 */
741
742 if (job->printer->port_monitor)
743 {
744 /*
745 * Add port monitor to the end of the list...
746 */
747
748 if (!filters)
749 filters = cupsArrayNew(NULL, NULL);
750
751 port_monitor.src = NULL;
752 port_monitor.dst = NULL;
753 port_monitor.cost = 0;
754
755 snprintf(port_monitor.filter, sizeof(port_monitor.filter),
756 "%s/monitor/%s", ServerBin, job->printer->port_monitor);
757
758 if (!cupsArrayAdd(filters, &port_monitor))
759 {
760 cupsdLogJob(job, CUPSD_LOG_DEBUG,
761 "Unable to add port monitor - %s", strerror(errno));
762
763 abort_message = "Stopping job because the scheduler ran out of memory.";
764
765 goto abort_job;
766 }
767 }
768
769 /*
770 * Make sure we don't go over the "MAX_FILTERS" limit...
771 */
772
773 if (cupsArrayCount(filters) > MAX_FILTERS)
774 {
775 cupsdLogJob(job, CUPSD_LOG_DEBUG,
776 "Too many filters (%d > %d), unable to print.",
777 cupsArrayCount(filters), MAX_FILTERS);
778
779 abort_message = "Aborting job because it needs too many filters to print.";
780 abort_state = IPP_JOB_ABORTED;
781
782 ippSetString(job->attrs, &job->reasons, 0, "document-unprintable-error");
783
784 goto abort_job;
785 }
786
787 /*
788 * Determine if we are printing a banner page or not...
789 */
790
791 if (job->job_sheets == NULL)
792 {
793 cupsdLogJob(job, CUPSD_LOG_DEBUG, "No job-sheets attribute.");
794 if ((job->job_sheets =
795 ippFindAttribute(job->attrs, "job-sheets", IPP_TAG_ZERO)) != NULL)
796 cupsdLogJob(job, CUPSD_LOG_DEBUG,
797 "... but someone added one without setting job_sheets.");
798 }
799 else if (job->job_sheets->num_values == 1)
800 cupsdLogJob(job, CUPSD_LOG_DEBUG, "job-sheets=%s",
801 job->job_sheets->values[0].string.text);
802 else
803 cupsdLogJob(job, CUPSD_LOG_DEBUG, "job-sheets=%s,%s",
804 job->job_sheets->values[0].string.text,
805 job->job_sheets->values[1].string.text);
806
807 if (job->printer->type & CUPS_PRINTER_REMOTE)
808 banner_page = 0;
809 else if (job->job_sheets == NULL)
810 banner_page = 0;
811 else if (_cups_strcasecmp(job->job_sheets->values[0].string.text, "none") != 0 &&
812 job->current_file == 0)
813 banner_page = 1;
814 else if (job->job_sheets->num_values > 1 &&
815 _cups_strcasecmp(job->job_sheets->values[1].string.text, "none") != 0 &&
816 job->current_file == (job->num_files - 1))
817 banner_page = 1;
818 else
819 banner_page = 0;
820
821 if ((options = get_options(job, banner_page, copies, sizeof(copies), title,
822 sizeof(title))) == NULL)
823 {
824 abort_message = "Stopping job because the scheduler ran out of memory.";
825
826 goto abort_job;
827 }
828
829 /*
830 * Build the command-line arguments for the filters. Each filter
831 * has 6 or 7 arguments:
832 *
833 * argv[0] = printer
834 * argv[1] = job ID
835 * argv[2] = username
836 * argv[3] = title
837 * argv[4] = # copies
838 * argv[5] = options
839 * argv[6] = filename (optional; normally stdin)
840 *
841 * This allows legacy printer drivers that use the old System V
842 * printing interface to be used by CUPS.
843 *
844 * For remote jobs, we send all of the files in the argument list.
845 */
846
847 if (job->printer->remote)
848 argv = calloc(7 + job->num_files, sizeof(char *));
849 else
850 argv = calloc(8, sizeof(char *));
851
852 if (!argv)
853 {
854 cupsdLogMessage(CUPSD_LOG_DEBUG, "Unable to allocate argument array - %s",
855 strerror(errno));
856
857 abort_message = "Stopping job because the scheduler ran out of memory.";
858
859 goto abort_job;
860 }
861
862 sprintf(jobid, "%d", job->id);
863
864 argv[0] = job->printer->name;
865 argv[1] = jobid;
866 argv[2] = job->username;
867 argv[3] = title;
868 argv[4] = copies;
869 argv[5] = options;
870
871 if (job->printer->remote && job->num_files > 1)
872 {
873 for (i = 0; i < job->num_files; i ++)
874 {
875 snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot,
876 job->id, i + 1);
877 argv[6 + i] = strdup(filename);
878 }
879 }
880 else
881 {
882 snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot,
883 job->id, job->current_file + 1);
884 argv[6] = filename;
885 }
886
887 for (i = 0; argv[i]; i ++)
888 cupsdLogJob(job, CUPSD_LOG_DEBUG, "argv[%d]=\"%s\"", i, argv[i]);
889
890 /*
891 * Create environment variable strings for the filters...
892 */
893
894 attr = ippFindAttribute(job->attrs, "attributes-natural-language",
895 IPP_TAG_LANGUAGE);
896
897 #ifdef __APPLE__
898 strcpy(apple_language, "APPLE_LANGUAGE=");
899 _cupsAppleLanguage(attr->values[0].string.text,
900 apple_language + 15, sizeof(apple_language) - 15);
901 #endif /* __APPLE__ */
902
903 switch (strlen(attr->values[0].string.text))
904 {
905 default :
906 /*
907 * This is an unknown or badly formatted language code; use
908 * the POSIX locale...
909 */
910
911 strcpy(lang, "LANG=C");
912 break;
913
914 case 2 :
915 /*
916 * Just the language code (ll)...
917 */
918
919 snprintf(lang, sizeof(lang), "LANG=%s.UTF-8",
920 attr->values[0].string.text);
921 break;
922
923 case 5 :
924 /*
925 * Language and country code (ll-cc)...
926 */
927
928 snprintf(lang, sizeof(lang), "LANG=%c%c_%c%c.UTF-8",
929 attr->values[0].string.text[0],
930 attr->values[0].string.text[1],
931 toupper(attr->values[0].string.text[3] & 255),
932 toupper(attr->values[0].string.text[4] & 255));
933 break;
934 }
935
936 if ((attr = ippFindAttribute(job->attrs, "document-format",
937 IPP_TAG_MIMETYPE)) != NULL &&
938 (ptr = strstr(attr->values[0].string.text, "charset=")) != NULL)
939 snprintf(charset, sizeof(charset), "CHARSET=%s", ptr + 8);
940 else
941 strlcpy(charset, "CHARSET=utf-8", sizeof(charset));
942
943 snprintf(content_type, sizeof(content_type), "CONTENT_TYPE=%s/%s",
944 job->filetypes[job->current_file]->super,
945 job->filetypes[job->current_file]->type);
946 snprintf(device_uri, sizeof(device_uri), "DEVICE_URI=%s",
947 job->printer->device_uri);
948 snprintf(ppd, sizeof(ppd), "PPD=%s/ppd/%s.ppd", ServerRoot,
949 job->printer->name);
950 snprintf(printer_info, sizeof(printer_name), "PRINTER_INFO=%s",
951 job->printer->info ? job->printer->info : "");
952 snprintf(printer_location, sizeof(printer_name), "PRINTER_LOCATION=%s",
953 job->printer->location ? job->printer->location : "");
954 snprintf(printer_name, sizeof(printer_name), "PRINTER=%s", job->printer->name);
955 if (job->printer->num_reasons > 0)
956 {
957 char *psrptr; /* Pointer into PRINTER_STATE_REASONS */
958 size_t psrlen; /* Size of PRINTER_STATE_REASONS */
959
960 for (psrlen = 22, i = 0; i < job->printer->num_reasons; i ++)
961 psrlen += strlen(job->printer->reasons[i]) + 1;
962
963 if ((printer_state_reasons = malloc(psrlen)) != NULL)
964 {
965 /*
966 * All of these strcpy's are safe because we allocated the psr string...
967 */
968
969 strcpy(printer_state_reasons, "PRINTER_STATE_REASONS=");
970 for (psrptr = printer_state_reasons + 22, i = 0;
971 i < job->printer->num_reasons;
972 i ++)
973 {
974 if (i)
975 *psrptr++ = ',';
976 strcpy(psrptr, job->printer->reasons[i]);
977 psrptr += strlen(psrptr);
978 }
979 }
980 }
981 snprintf(rip_max_cache, sizeof(rip_max_cache), "RIP_MAX_CACHE=%s", RIPCache);
982
983 if (job->printer->num_auth_info_required == 1)
984 snprintf(auth_info_required, sizeof(auth_info_required),
985 "AUTH_INFO_REQUIRED=%s",
986 job->printer->auth_info_required[0]);
987 else if (job->printer->num_auth_info_required == 2)
988 snprintf(auth_info_required, sizeof(auth_info_required),
989 "AUTH_INFO_REQUIRED=%s,%s",
990 job->printer->auth_info_required[0],
991 job->printer->auth_info_required[1]);
992 else if (job->printer->num_auth_info_required == 3)
993 snprintf(auth_info_required, sizeof(auth_info_required),
994 "AUTH_INFO_REQUIRED=%s,%s,%s",
995 job->printer->auth_info_required[0],
996 job->printer->auth_info_required[1],
997 job->printer->auth_info_required[2]);
998 else if (job->printer->num_auth_info_required == 4)
999 snprintf(auth_info_required, sizeof(auth_info_required),
1000 "AUTH_INFO_REQUIRED=%s,%s,%s,%s",
1001 job->printer->auth_info_required[0],
1002 job->printer->auth_info_required[1],
1003 job->printer->auth_info_required[2],
1004 job->printer->auth_info_required[3]);
1005 else
1006 strlcpy(auth_info_required, "AUTH_INFO_REQUIRED=none",
1007 sizeof(auth_info_required));
1008
1009 envc = cupsdLoadEnv(envp, (int)(sizeof(envp) / sizeof(envp[0])));
1010
1011 envp[envc ++] = charset;
1012 envp[envc ++] = lang;
1013 #ifdef __APPLE__
1014 envp[envc ++] = apple_language;
1015 #endif /* __APPLE__ */
1016 envp[envc ++] = ppd;
1017 envp[envc ++] = rip_max_cache;
1018 envp[envc ++] = content_type;
1019 envp[envc ++] = device_uri;
1020 envp[envc ++] = printer_info;
1021 envp[envc ++] = printer_location;
1022 envp[envc ++] = printer_name;
1023 envp[envc ++] = printer_state_reasons ? printer_state_reasons :
1024 "PRINTER_STATE_REASONS=none";
1025 envp[envc ++] = banner_page ? "CUPS_FILETYPE=job-sheet" :
1026 "CUPS_FILETYPE=document";
1027
1028 if (!job->printer->remote && !job->printer->raw)
1029 {
1030 filter = (mime_filter_t *)cupsArrayLast(filters);
1031
1032 if (job->printer->port_monitor)
1033 filter = (mime_filter_t *)cupsArrayPrev(filters);
1034
1035 if (filter && filter->dst)
1036 {
1037 if ((ptr = strchr(filter->dst->type, '/')) != NULL)
1038 snprintf(final_content_type, sizeof(final_content_type),
1039 "FINAL_CONTENT_TYPE=%s", ptr + 1);
1040 else
1041 snprintf(final_content_type, sizeof(final_content_type),
1042 "FINAL_CONTENT_TYPE=%s/%s", filter->dst->super,
1043 filter->dst->type);
1044 envp[envc ++] = final_content_type;
1045 }
1046 }
1047
1048 if (Classification && !banner_page)
1049 {
1050 if ((attr = ippFindAttribute(job->attrs, "job-sheets",
1051 IPP_TAG_NAME)) == NULL)
1052 snprintf(classification, sizeof(classification), "CLASSIFICATION=%s",
1053 Classification);
1054 else if (attr->num_values > 1 &&
1055 strcmp(attr->values[1].string.text, "none") != 0)
1056 snprintf(classification, sizeof(classification), "CLASSIFICATION=%s",
1057 attr->values[1].string.text);
1058 else
1059 snprintf(classification, sizeof(classification), "CLASSIFICATION=%s",
1060 attr->values[0].string.text);
1061
1062 envp[envc ++] = classification;
1063 }
1064
1065 if (job->dtype & CUPS_PRINTER_CLASS)
1066 {
1067 snprintf(class_name, sizeof(class_name), "CLASS=%s", job->dest);
1068 envp[envc ++] = class_name;
1069 }
1070
1071 envp[envc ++] = auth_info_required;
1072
1073 for (i = 0;
1074 i < (int)(sizeof(job->auth_env) / sizeof(job->auth_env[0]));
1075 i ++)
1076 if (job->auth_env[i])
1077 envp[envc ++] = job->auth_env[i];
1078 else
1079 break;
1080
1081 if (job->auth_uid)
1082 envp[envc ++] = job->auth_uid;
1083
1084 envp[envc] = NULL;
1085
1086 for (i = 0; i < envc; i ++)
1087 if (!strncmp(envp[i], "AUTH_", 5))
1088 cupsdLogJob(job, CUPSD_LOG_DEBUG, "envp[%d]=\"AUTH_%c****\"", i,
1089 envp[i][5]);
1090 else if (strncmp(envp[i], "DEVICE_URI=", 11))
1091 cupsdLogJob(job, CUPSD_LOG_DEBUG, "envp[%d]=\"%s\"", i, envp[i]);
1092 else
1093 cupsdLogJob(job, CUPSD_LOG_DEBUG, "envp[%d]=\"DEVICE_URI=%s\"", i,
1094 job->printer->sanitized_device_uri);
1095
1096 if (job->printer->remote)
1097 job->current_file = job->num_files;
1098 else
1099 job->current_file ++;
1100
1101 /*
1102 * Now create processes for all of the filters...
1103 */
1104
1105 cupsdSetPrinterReasons(job->printer, "-cups-missing-filter-warning,"
1106 "cups-insecure-filter-warning");
1107
1108 for (i = 0, slot = 0, filter = (mime_filter_t *)cupsArrayFirst(filters);
1109 filter;
1110 i ++, filter = (mime_filter_t *)cupsArrayNext(filters))
1111 {
1112 if (filter->filter[0] != '/')
1113 snprintf(command, sizeof(command), "%s/filter/%s", ServerBin,
1114 filter->filter);
1115 else
1116 strlcpy(command, filter->filter, sizeof(command));
1117
1118 if (i < (cupsArrayCount(filters) - 1))
1119 {
1120 if (cupsdOpenPipe(filterfds[slot]))
1121 {
1122 abort_message = "Stopping job because the scheduler could not create "
1123 "the filter pipes.";
1124
1125 goto abort_job;
1126 }
1127 }
1128 else
1129 {
1130 if (job->current_file == 1 ||
1131 (job->printer->pc && job->printer->pc->single_file))
1132 {
1133 if (strncmp(job->printer->device_uri, "file:", 5) != 0)
1134 {
1135 if (cupsdOpenPipe(job->print_pipes))
1136 {
1137 abort_message = "Stopping job because the scheduler could not "
1138 "create the backend pipes.";
1139
1140 goto abort_job;
1141 }
1142 }
1143 else
1144 {
1145 job->print_pipes[0] = -1;
1146 if (!strcmp(job->printer->device_uri, "file:/dev/null") ||
1147 !strcmp(job->printer->device_uri, "file:///dev/null"))
1148 job->print_pipes[1] = -1;
1149 else
1150 {
1151 if (!strncmp(job->printer->device_uri, "file:/dev/", 10))
1152 job->print_pipes[1] = open(job->printer->device_uri + 5,
1153 O_WRONLY | O_EXCL);
1154 else if (!strncmp(job->printer->device_uri, "file:///dev/", 12))
1155 job->print_pipes[1] = open(job->printer->device_uri + 7,
1156 O_WRONLY | O_EXCL);
1157 else if (!strncmp(job->printer->device_uri, "file:///", 8))
1158 job->print_pipes[1] = open(job->printer->device_uri + 7,
1159 O_WRONLY | O_CREAT | O_TRUNC, 0600);
1160 else
1161 job->print_pipes[1] = open(job->printer->device_uri + 5,
1162 O_WRONLY | O_CREAT | O_TRUNC, 0600);
1163
1164 if (job->print_pipes[1] < 0)
1165 {
1166 abort_message = "Stopping job because the scheduler could not "
1167 "open the output file.";
1168
1169 goto abort_job;
1170 }
1171
1172 fcntl(job->print_pipes[1], F_SETFD,
1173 fcntl(job->print_pipes[1], F_GETFD) | FD_CLOEXEC);
1174 }
1175 }
1176 }
1177
1178 filterfds[slot][0] = job->print_pipes[0];
1179 filterfds[slot][1] = job->print_pipes[1];
1180 }
1181
1182 pid = cupsdStartProcess(command, argv, envp, filterfds[!slot][0],
1183 filterfds[slot][1], job->status_pipes[1],
1184 job->back_pipes[0], job->side_pipes[0], 0,
1185 job->profile, job, job->filters + i);
1186
1187 cupsdClosePipe(filterfds[!slot]);
1188
1189 if (pid == 0)
1190 {
1191 cupsdLogJob(job, CUPSD_LOG_ERROR, "Unable to start filter \"%s\" - %s.",
1192 filter->filter, strerror(errno));
1193
1194 abort_message = "Stopping job because the scheduler could not execute a "
1195 "filter.";
1196
1197 goto abort_job;
1198 }
1199
1200 cupsdLogJob(job, CUPSD_LOG_INFO, "Started filter %s (PID %d)", command,
1201 pid);
1202
1203 argv[6] = NULL;
1204 slot = !slot;
1205 }
1206
1207 cupsArrayDelete(filters);
1208 filters = NULL;
1209
1210 /*
1211 * Finally, pipe the final output into a backend process if needed...
1212 */
1213
1214 if (strncmp(job->printer->device_uri, "file:", 5) != 0)
1215 {
1216 if (job->current_file == 1 || job->printer->remote ||
1217 (job->printer->pc && job->printer->pc->single_file))
1218 {
1219 sscanf(job->printer->device_uri, "%254[^:]", scheme);
1220 snprintf(command, sizeof(command), "%s/backend/%s", ServerBin, scheme);
1221
1222 /*
1223 * See if the backend needs to run as root...
1224 */
1225
1226 if (RunUser)
1227 backroot = 0;
1228 else if (stat(command, &backinfo))
1229 backroot = 0;
1230 else
1231 backroot = !(backinfo.st_mode & (S_IRWXG | S_IRWXO));
1232
1233 argv[0] = job->printer->sanitized_device_uri;
1234
1235 filterfds[slot][0] = -1;
1236 filterfds[slot][1] = -1;
1237
1238 pid = cupsdStartProcess(command, argv, envp, filterfds[!slot][0],
1239 filterfds[slot][1], job->status_pipes[1],
1240 job->back_pipes[1], job->side_pipes[1],
1241 backroot, job->profile, job, &(job->backend));
1242
1243 if (pid == 0)
1244 {
1245 abort_message = "Stopping job because the sheduler could not execute "
1246 "the backend.";
1247
1248 goto abort_job;
1249 }
1250 else
1251 {
1252 cupsdLogJob(job, CUPSD_LOG_INFO, "Started backend %s (PID %d)",
1253 command, pid);
1254 }
1255 }
1256
1257 if (job->current_file == job->num_files ||
1258 (job->printer->pc && job->printer->pc->single_file))
1259 cupsdClosePipe(job->print_pipes);
1260
1261 if (job->current_file == job->num_files)
1262 {
1263 cupsdClosePipe(job->back_pipes);
1264 cupsdClosePipe(job->side_pipes);
1265
1266 close(job->status_pipes[1]);
1267 job->status_pipes[1] = -1;
1268 }
1269 }
1270 else
1271 {
1272 filterfds[slot][0] = -1;
1273 filterfds[slot][1] = -1;
1274
1275 if (job->current_file == job->num_files ||
1276 (job->printer->pc && job->printer->pc->single_file))
1277 cupsdClosePipe(job->print_pipes);
1278
1279 if (job->current_file == job->num_files)
1280 {
1281 close(job->status_pipes[1]);
1282 job->status_pipes[1] = -1;
1283 }
1284 }
1285
1286 cupsdClosePipe(filterfds[slot]);
1287
1288 if (job->printer->remote && job->num_files > 1)
1289 {
1290 for (i = 0; i < job->num_files; i ++)
1291 free(argv[i + 6]);
1292 }
1293
1294 free(argv);
1295 if (printer_state_reasons)
1296 free(printer_state_reasons);
1297
1298 cupsdAddSelect(job->status_buffer->fd, (cupsd_selfunc_t)update_job, NULL,
1299 job);
1300
1301 cupsdAddEvent(CUPSD_EVENT_JOB_STATE, job->printer, job, "Job #%d started.",
1302 job->id);
1303
1304 return;
1305
1306
1307 /*
1308 * If we get here, we need to abort the current job and close out all
1309 * files and pipes...
1310 */
1311
1312 abort_job:
1313
1314 FilterLevel -= job->cost;
1315 job->cost = 0;
1316
1317 for (slot = 0; slot < 2; slot ++)
1318 cupsdClosePipe(filterfds[slot]);
1319
1320 cupsArrayDelete(filters);
1321
1322 if (argv)
1323 {
1324 if (job->printer->remote && job->num_files > 1)
1325 {
1326 for (i = 0; i < job->num_files; i ++)
1327 free(argv[i + 6]);
1328 }
1329
1330 free(argv);
1331 }
1332
1333 if (printer_state_reasons)
1334 free(printer_state_reasons);
1335
1336 cupsdClosePipe(job->print_pipes);
1337 cupsdClosePipe(job->back_pipes);
1338 cupsdClosePipe(job->side_pipes);
1339
1340 cupsdRemoveSelect(job->status_pipes[0]);
1341 cupsdClosePipe(job->status_pipes);
1342 cupsdStatBufDelete(job->status_buffer);
1343 job->status_buffer = NULL;
1344
1345 /*
1346 * Update the printer and job state.
1347 */
1348
1349 cupsdSetJobState(job, abort_state, CUPSD_JOB_DEFAULT, "%s", abort_message);
1350 cupsdSetPrinterState(job->printer, IPP_PRINTER_IDLE, 0);
1351 update_job_attrs(job, 0);
1352
1353 if (job->history)
1354 free_job_history(job);
1355
1356 cupsArrayRemove(PrintingJobs, job);
1357
1358 /*
1359 * Clear the printer <-> job association...
1360 */
1361
1362 job->printer->job = NULL;
1363 job->printer = NULL;
1364 }
1365
1366
1367 /*
1368 * 'cupsdDeleteJob()' - Free all memory used by a job.
1369 */
1370
1371 void
1372 cupsdDeleteJob(cupsd_job_t *job, /* I - Job */
1373 cupsd_jobaction_t action)/* I - Action */
1374 {
1375 int i; /* Looping var */
1376
1377
1378 if (job->printer)
1379 finalize_job(job, 1);
1380
1381 if (action == CUPSD_JOB_PURGE)
1382 remove_job_history(job);
1383
1384 cupsdClearString(&job->username);
1385 cupsdClearString(&job->dest);
1386 for (i = 0;
1387 i < (int)(sizeof(job->auth_env) / sizeof(job->auth_env[0]));
1388 i ++)
1389 cupsdClearString(job->auth_env + i);
1390 cupsdClearString(&job->auth_uid);
1391
1392 if (action == CUPSD_JOB_PURGE)
1393 remove_job_files(job);
1394 else if (job->num_files > 0)
1395 {
1396 free(job->compressions);
1397 free(job->filetypes);
1398
1399 job->num_files = 0;
1400 }
1401
1402 if (job->history)
1403 free_job_history(job);
1404
1405 unload_job(job);
1406
1407 cupsArrayRemove(Jobs, job);
1408 cupsArrayRemove(ActiveJobs, job);
1409 cupsArrayRemove(PrintingJobs, job);
1410
1411 free(job);
1412 }
1413
1414
1415 /*
1416 * 'cupsdFreeAllJobs()' - Free all jobs from memory.
1417 */
1418
1419 void
1420 cupsdFreeAllJobs(void)
1421 {
1422 cupsd_job_t *job; /* Current job */
1423
1424
1425 if (!Jobs)
1426 return;
1427
1428 cupsdHoldSignals();
1429
1430 cupsdStopAllJobs(CUPSD_JOB_FORCE, 0);
1431 cupsdSaveAllJobs();
1432
1433 for (job = (cupsd_job_t *)cupsArrayFirst(Jobs);
1434 job;
1435 job = (cupsd_job_t *)cupsArrayNext(Jobs))
1436 cupsdDeleteJob(job, CUPSD_JOB_DEFAULT);
1437
1438 cupsdReleaseSignals();
1439 }
1440
1441
1442 /*
1443 * 'cupsdFindJob()' - Find the specified job.
1444 */
1445
1446 cupsd_job_t * /* O - Job data */
1447 cupsdFindJob(int id) /* I - Job ID */
1448 {
1449 cupsd_job_t key; /* Search key */
1450
1451
1452 key.id = id;
1453
1454 return ((cupsd_job_t *)cupsArrayFind(Jobs, &key));
1455 }
1456
1457
1458 /*
1459 * 'cupsdGetPrinterJobCount()' - Get the number of pending, processing,
1460 * or held jobs in a printer or class.
1461 */
1462
1463 int /* O - Job count */
1464 cupsdGetPrinterJobCount(
1465 const char *dest) /* I - Printer or class name */
1466 {
1467 int count; /* Job count */
1468 cupsd_job_t *job; /* Current job */
1469
1470
1471 for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs), count = 0;
1472 job;
1473 job = (cupsd_job_t *)cupsArrayNext(ActiveJobs))
1474 if (job->dest && !_cups_strcasecmp(job->dest, dest))
1475 count ++;
1476
1477 return (count);
1478 }
1479
1480
1481 /*
1482 * 'cupsdGetUserJobCount()' - Get the number of pending, processing,
1483 * or held jobs for a user.
1484 */
1485
1486 int /* O - Job count */
1487 cupsdGetUserJobCount(
1488 const char *username) /* I - Username */
1489 {
1490 int count; /* Job count */
1491 cupsd_job_t *job; /* Current job */
1492
1493
1494 for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs), count = 0;
1495 job;
1496 job = (cupsd_job_t *)cupsArrayNext(ActiveJobs))
1497 if (!_cups_strcasecmp(job->username, username))
1498 count ++;
1499
1500 return (count);
1501 }
1502
1503
1504 /*
1505 * 'cupsdLoadAllJobs()' - Load all jobs from disk.
1506 */
1507
1508 void
1509 cupsdLoadAllJobs(void)
1510 {
1511 char filename[1024]; /* Full filename of job.cache file */
1512 struct stat fileinfo, /* Information on job.cache file */
1513 dirinfo; /* Information on RequestRoot dir */
1514
1515
1516
1517 /*
1518 * Create the job arrays as needed...
1519 */
1520
1521 if (!Jobs)
1522 Jobs = cupsArrayNew(compare_jobs, NULL);
1523
1524 if (!ActiveJobs)
1525 ActiveJobs = cupsArrayNew(compare_active_jobs, NULL);
1526
1527 if (!PrintingJobs)
1528 PrintingJobs = cupsArrayNew(compare_jobs, NULL);
1529
1530 /*
1531 * See whether the job.cache file is older than the RequestRoot directory...
1532 */
1533
1534 snprintf(filename, sizeof(filename), "%s/job.cache", CacheDir);
1535
1536 if (stat(filename, &fileinfo))
1537 {
1538 fileinfo.st_mtime = 0;
1539
1540 if (errno != ENOENT)
1541 cupsdLogMessage(CUPSD_LOG_ERROR,
1542 "Unable to get file information for \"%s\" - %s",
1543 filename, strerror(errno));
1544 }
1545
1546 if (stat(RequestRoot, &dirinfo))
1547 {
1548 dirinfo.st_mtime = 0;
1549
1550 if (errno != ENOENT)
1551 cupsdLogMessage(CUPSD_LOG_ERROR,
1552 "Unable to get directory information for \"%s\" - %s",
1553 RequestRoot, strerror(errno));
1554 }
1555
1556 /*
1557 * Load the most recent source for job data...
1558 */
1559
1560 if (dirinfo.st_mtime > fileinfo.st_mtime)
1561 {
1562 load_request_root();
1563
1564 load_next_job_id(filename);
1565 }
1566 else
1567 load_job_cache(filename);
1568
1569 /*
1570 * Clean out old jobs as needed...
1571 */
1572
1573 if (MaxJobs > 0 && cupsArrayCount(Jobs) >= MaxJobs)
1574 cupsdCleanJobs();
1575 }
1576
1577
1578 /*
1579 * 'cupsdLoadJob()' - Load a single job.
1580 */
1581
1582 int /* O - 1 on success, 0 on failure */
1583 cupsdLoadJob(cupsd_job_t *job) /* I - Job */
1584 {
1585 int i; /* Looping var */
1586 char jobfile[1024]; /* Job filename */
1587 cups_file_t *fp; /* Job file */
1588 int fileid; /* Current file ID */
1589 ipp_attribute_t *attr; /* Job attribute */
1590 const char *dest; /* Destination name */
1591 cupsd_printer_t *destptr; /* Pointer to destination */
1592 mime_type_t **filetypes; /* New filetypes array */
1593 int *compressions; /* New compressions array */
1594
1595
1596 if (job->attrs)
1597 {
1598 if (job->state_value > IPP_JOB_STOPPED)
1599 job->access_time = time(NULL);
1600
1601 return (1);
1602 }
1603
1604 if ((job->attrs = ippNew()) == NULL)
1605 {
1606 cupsdLogJob(job, CUPSD_LOG_ERROR, "Ran out of memory for job attributes.");
1607 return (0);
1608 }
1609
1610 /*
1611 * Load job attributes...
1612 */
1613
1614 cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] Loading attributes...", job->id);
1615
1616 snprintf(jobfile, sizeof(jobfile), "%s/c%05d", RequestRoot, job->id);
1617 if ((fp = cupsdOpenConfFile(jobfile)) == NULL)
1618 goto error;
1619
1620 if (ippReadIO(fp, (ipp_iocb_t)cupsFileRead, 1, NULL, job->attrs) != IPP_DATA)
1621 {
1622 cupsdLogMessage(CUPSD_LOG_ERROR,
1623 "[Job %d] Unable to read job control file \"%s\".", job->id,
1624 jobfile);
1625 cupsFileClose(fp);
1626 goto error;
1627 }
1628
1629 cupsFileClose(fp);
1630
1631 /*
1632 * Copy attribute data to the job object...
1633 */
1634
1635 if (!ippFindAttribute(job->attrs, "time-at-creation", IPP_TAG_INTEGER))
1636 {
1637 cupsdLogMessage(CUPSD_LOG_ERROR,
1638 "[Job %d] Missing or bad time-at-creation attribute in "
1639 "control file.", job->id);
1640 goto error;
1641 }
1642
1643 if ((job->state = ippFindAttribute(job->attrs, "job-state",
1644 IPP_TAG_ENUM)) == NULL)
1645 {
1646 cupsdLogMessage(CUPSD_LOG_ERROR,
1647 "[Job %d] Missing or bad job-state attribute in control "
1648 "file.", job->id);
1649 goto error;
1650 }
1651
1652 job->state_value = (ipp_jstate_t)job->state->values[0].integer;
1653 job->file_time = 0;
1654 job->history_time = 0;
1655
1656 if (job->state_value >= IPP_JOB_CANCELED &&
1657 (attr = ippFindAttribute(job->attrs, "time-at-completed",
1658 IPP_TAG_INTEGER)) != NULL)
1659 {
1660 if (JobHistory < INT_MAX)
1661 job->history_time = attr->values[0].integer + JobHistory;
1662 else
1663 job->history_time = INT_MAX;
1664
1665 if (job->history_time < time(NULL))
1666 goto error; /* Expired, remove from history */
1667
1668 if (job->history_time < JobHistoryUpdate || !JobHistoryUpdate)
1669 JobHistoryUpdate = job->history_time;
1670
1671 if (JobFiles < INT_MAX)
1672 job->file_time = attr->values[0].integer + JobFiles;
1673 else
1674 job->file_time = INT_MAX;
1675
1676 if (job->file_time < JobHistoryUpdate || !JobHistoryUpdate)
1677 JobHistoryUpdate = job->file_time;
1678
1679 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdLoadJob: JobHistoryUpdate=%ld",
1680 (long)JobHistoryUpdate);
1681 }
1682
1683 if (!job->dest)
1684 {
1685 if ((attr = ippFindAttribute(job->attrs, "job-printer-uri",
1686 IPP_TAG_URI)) == NULL)
1687 {
1688 cupsdLogMessage(CUPSD_LOG_ERROR,
1689 "[Job %d] No job-printer-uri attribute in control file.",
1690 job->id);
1691 goto error;
1692 }
1693
1694 if ((dest = cupsdValidateDest(attr->values[0].string.text, &(job->dtype),
1695 &destptr)) == NULL)
1696 {
1697 cupsdLogMessage(CUPSD_LOG_ERROR,
1698 "[Job %d] Unable to queue job for destination \"%s\".",
1699 job->id, attr->values[0].string.text);
1700 goto error;
1701 }
1702
1703 cupsdSetString(&job->dest, dest);
1704 }
1705 else if ((destptr = cupsdFindDest(job->dest)) == NULL)
1706 {
1707 cupsdLogMessage(CUPSD_LOG_ERROR,
1708 "[Job %d] Unable to queue job for destination \"%s\".",
1709 job->id, job->dest);
1710 goto error;
1711 }
1712
1713 if ((job->reasons = ippFindAttribute(job->attrs, "job-state-reasons",
1714 IPP_TAG_KEYWORD)) == NULL)
1715 {
1716 const char *reason; /* job-state-reason keyword */
1717
1718 cupsdLogMessage(CUPSD_LOG_DEBUG,
1719 "[Job %d] Adding missing job-state-reasons attribute to "
1720 " control file.", job->id);
1721
1722 switch (job->state_value)
1723 {
1724 default :
1725 case IPP_JOB_PENDING :
1726 if (destptr->state == IPP_PRINTER_STOPPED)
1727 reason = "printer-stopped";
1728 else
1729 reason = "none";
1730 break;
1731
1732 case IPP_JOB_HELD :
1733 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
1734 IPP_TAG_ZERO)) != NULL &&
1735 (attr->value_tag == IPP_TAG_NAME ||
1736 attr->value_tag == IPP_TAG_NAMELANG ||
1737 attr->value_tag == IPP_TAG_KEYWORD) &&
1738 strcmp(attr->values[0].string.text, "no-hold"))
1739 reason = "job-hold-until-specified";
1740 else
1741 reason = "job-incoming";
1742 break;
1743
1744 case IPP_JOB_PROCESSING :
1745 reason = "job-printing";
1746 break;
1747
1748 case IPP_JOB_STOPPED :
1749 reason = "job-stopped";
1750 break;
1751
1752 case IPP_JOB_CANCELED :
1753 reason = "job-canceled-by-user";
1754 break;
1755
1756 case IPP_JOB_ABORTED :
1757 reason = "aborted-by-system";
1758 break;
1759
1760 case IPP_JOB_COMPLETED :
1761 reason = "job-completed-successfully";
1762 break;
1763 }
1764
1765 job->reasons = ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_KEYWORD,
1766 "job-state-reasons", NULL, reason);
1767 }
1768 else if (job->state_value == IPP_JOB_PENDING)
1769 {
1770 if (destptr->state == IPP_PRINTER_STOPPED)
1771 ippSetString(job->attrs, &job->reasons, 0, "printer-stopped");
1772 else
1773 ippSetString(job->attrs, &job->reasons, 0, "none");
1774 }
1775
1776 job->sheets = ippFindAttribute(job->attrs, "job-media-sheets-completed",
1777 IPP_TAG_INTEGER);
1778 job->job_sheets = ippFindAttribute(job->attrs, "job-sheets", IPP_TAG_NAME);
1779
1780 if (!job->priority)
1781 {
1782 if ((attr = ippFindAttribute(job->attrs, "job-priority",
1783 IPP_TAG_INTEGER)) == NULL)
1784 {
1785 cupsdLogMessage(CUPSD_LOG_ERROR,
1786 "[Job %d] Missing or bad job-priority attribute in "
1787 "control file.", job->id);
1788 goto error;
1789 }
1790
1791 job->priority = attr->values[0].integer;
1792 }
1793
1794 if (!job->username)
1795 {
1796 if ((attr = ippFindAttribute(job->attrs, "job-originating-user-name",
1797 IPP_TAG_NAME)) == NULL)
1798 {
1799 cupsdLogMessage(CUPSD_LOG_ERROR,
1800 "[Job %d] Missing or bad job-originating-user-name "
1801 "attribute in control file.", job->id);
1802 goto error;
1803 }
1804
1805 cupsdSetString(&job->username, attr->values[0].string.text);
1806 }
1807
1808 /*
1809 * Set the job hold-until time and state...
1810 */
1811
1812 if (job->state_value == IPP_JOB_HELD)
1813 {
1814 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
1815 IPP_TAG_KEYWORD)) == NULL)
1816 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
1817
1818 if (attr)
1819 cupsdSetJobHoldUntil(job, attr->values[0].string.text, CUPSD_JOB_DEFAULT);
1820 else
1821 {
1822 job->state->values[0].integer = IPP_JOB_PENDING;
1823 job->state_value = IPP_JOB_PENDING;
1824 }
1825 }
1826 else if (job->state_value == IPP_JOB_PROCESSING)
1827 {
1828 job->state->values[0].integer = IPP_JOB_PENDING;
1829 job->state_value = IPP_JOB_PENDING;
1830 }
1831
1832 if (!job->num_files)
1833 {
1834 /*
1835 * Find all the d##### files...
1836 */
1837
1838 for (fileid = 1; fileid < 10000; fileid ++)
1839 {
1840 snprintf(jobfile, sizeof(jobfile), "%s/d%05d-%03d", RequestRoot,
1841 job->id, fileid);
1842
1843 if (access(jobfile, 0))
1844 break;
1845
1846 cupsdLogMessage(CUPSD_LOG_DEBUG,
1847 "[Job %d] Auto-typing document file \"%s\"...", job->id,
1848 jobfile);
1849
1850 if (fileid > job->num_files)
1851 {
1852 if (job->num_files == 0)
1853 {
1854 compressions = (int *)calloc(fileid, sizeof(int));
1855 filetypes = (mime_type_t **)calloc(fileid, sizeof(mime_type_t *));
1856 }
1857 else
1858 {
1859 compressions = (int *)realloc(job->compressions,
1860 sizeof(int) * fileid);
1861 filetypes = (mime_type_t **)realloc(job->filetypes,
1862 sizeof(mime_type_t *) *
1863 fileid);
1864 }
1865
1866 if (!compressions || !filetypes)
1867 {
1868 cupsdLogMessage(CUPSD_LOG_ERROR,
1869 "[Job %d] Ran out of memory for job file types.",
1870 job->id);
1871
1872 ippDelete(job->attrs);
1873 job->attrs = NULL;
1874
1875 if (compressions)
1876 free(compressions);
1877
1878 if (filetypes)
1879 free(filetypes);
1880
1881 if (job->compressions)
1882 {
1883 free(job->compressions);
1884 job->compressions = NULL;
1885 }
1886
1887 if (job->filetypes)
1888 {
1889 free(job->filetypes);
1890 job->filetypes = NULL;
1891 }
1892
1893 job->num_files = 0;
1894 return (0);
1895 }
1896
1897 job->compressions = compressions;
1898 job->filetypes = filetypes;
1899 job->num_files = fileid;
1900 }
1901
1902 job->filetypes[fileid - 1] = mimeFileType(MimeDatabase, jobfile, NULL,
1903 job->compressions + fileid - 1);
1904
1905 if (!job->filetypes[fileid - 1])
1906 job->filetypes[fileid - 1] = mimeType(MimeDatabase, "application",
1907 "vnd.cups-raw");
1908 }
1909 }
1910
1911 /*
1912 * Load authentication information as needed...
1913 */
1914
1915 if (job->state_value < IPP_JOB_STOPPED)
1916 {
1917 snprintf(jobfile, sizeof(jobfile), "%s/a%05d", RequestRoot, job->id);
1918
1919 for (i = 0;
1920 i < (int)(sizeof(job->auth_env) / sizeof(job->auth_env[0]));
1921 i ++)
1922 cupsdClearString(job->auth_env + i);
1923 cupsdClearString(&job->auth_uid);
1924
1925 if ((fp = cupsFileOpen(jobfile, "r")) != NULL)
1926 {
1927 int bytes, /* Size of auth data */
1928 linenum = 1; /* Current line number */
1929 char line[65536], /* Line from file */
1930 *value, /* Value from line */
1931 data[65536]; /* Decoded data */
1932
1933
1934 if (cupsFileGets(fp, line, sizeof(line)) &&
1935 !strcmp(line, "CUPSD-AUTH-V2"))
1936 {
1937 i = 0;
1938 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
1939 {
1940 /*
1941 * Decode value...
1942 */
1943
1944 bytes = sizeof(data);
1945 httpDecode64_2(data, &bytes, value);
1946
1947 /*
1948 * Assign environment variables...
1949 */
1950
1951 if (!strcmp(line, "uid"))
1952 {
1953 cupsdSetStringf(&job->auth_uid, "AUTH_UID=%s", value);
1954 continue;
1955 }
1956 else if (i >= (int)(sizeof(job->auth_env) / sizeof(job->auth_env[0])))
1957 break;
1958
1959 if (!strcmp(line, "username"))
1960 cupsdSetStringf(job->auth_env + i, "AUTH_USERNAME=%s", data);
1961 else if (!strcmp(line, "domain"))
1962 cupsdSetStringf(job->auth_env + i, "AUTH_DOMAIN=%s", data);
1963 else if (!strcmp(line, "password"))
1964 cupsdSetStringf(job->auth_env + i, "AUTH_PASSWORD=%s", data);
1965 else if (!strcmp(line, "negotiate"))
1966 cupsdSetStringf(job->auth_env + i, "AUTH_NEGOTIATE=%s", data);
1967 else
1968 continue;
1969
1970 i ++;
1971 }
1972 }
1973
1974 cupsFileClose(fp);
1975 }
1976 }
1977
1978 job->access_time = time(NULL);
1979 return (1);
1980
1981 /*
1982 * If we get here then something bad happened...
1983 */
1984
1985 error:
1986
1987 ippDelete(job->attrs);
1988 job->attrs = NULL;
1989
1990 remove_job_history(job);
1991 remove_job_files(job);
1992
1993 return (0);
1994 }
1995
1996
1997 /*
1998 * 'cupsdMoveJob()' - Move the specified job to a different destination.
1999 */
2000
2001 void
2002 cupsdMoveJob(cupsd_job_t *job, /* I - Job */
2003 cupsd_printer_t *p) /* I - Destination printer or class */
2004 {
2005 ipp_attribute_t *attr; /* job-printer-uri attribute */
2006 const char *olddest; /* Old destination */
2007 cupsd_printer_t *oldp; /* Old pointer */
2008
2009
2010 /*
2011 * Don't move completed jobs...
2012 */
2013
2014 if (job->state_value > IPP_JOB_STOPPED)
2015 return;
2016
2017 /*
2018 * Get the old destination...
2019 */
2020
2021 olddest = job->dest;
2022
2023 if (job->printer)
2024 oldp = job->printer;
2025 else
2026 oldp = cupsdFindDest(olddest);
2027
2028 /*
2029 * Change the destination information...
2030 */
2031
2032 if (job->state_value > IPP_JOB_HELD)
2033 cupsdSetJobState(job, IPP_JOB_PENDING, CUPSD_JOB_DEFAULT,
2034 "Stopping job prior to move.");
2035
2036 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED, oldp, job,
2037 "Job #%d moved from %s to %s.", job->id, olddest,
2038 p->name);
2039
2040 cupsdSetString(&job->dest, p->name);
2041 job->dtype = p->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_REMOTE);
2042
2043 if ((attr = ippFindAttribute(job->attrs, "job-printer-uri",
2044 IPP_TAG_URI)) != NULL)
2045 cupsdSetString(&(attr->values[0].string.text), p->uri);
2046
2047 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED, p, job,
2048 "Job #%d moved from %s to %s.", job->id, olddest,
2049 p->name);
2050
2051 job->dirty = 1;
2052 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
2053 }
2054
2055
2056 /*
2057 * 'cupsdReleaseJob()' - Release the specified job.
2058 */
2059
2060 void
2061 cupsdReleaseJob(cupsd_job_t *job) /* I - Job */
2062 {
2063 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdReleaseJob(job=%p(%d))", job,
2064 job->id);
2065
2066 if (job->state_value == IPP_JOB_HELD)
2067 {
2068 /*
2069 * Add trailing banner as needed...
2070 */
2071
2072 if (job->pending_timeout)
2073 cupsdTimeoutJob(job);
2074
2075 cupsdSetJobState(job, IPP_JOB_PENDING, CUPSD_JOB_DEFAULT,
2076 "Job released by user.");
2077 }
2078 }
2079
2080
2081 /*
2082 * 'cupsdRestartJob()' - Restart the specified job.
2083 */
2084
2085 void
2086 cupsdRestartJob(cupsd_job_t *job) /* I - Job */
2087 {
2088 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdRestartJob(job=%p(%d))", job,
2089 job->id);
2090
2091 if (job->state_value == IPP_JOB_STOPPED || job->num_files)
2092 cupsdSetJobState(job, IPP_JOB_PENDING, CUPSD_JOB_DEFAULT,
2093 "Job restarted by user.");
2094 }
2095
2096
2097 /*
2098 * 'cupsdSaveAllJobs()' - Save a summary of all jobs to disk.
2099 */
2100
2101 void
2102 cupsdSaveAllJobs(void)
2103 {
2104 int i; /* Looping var */
2105 cups_file_t *fp; /* job.cache file */
2106 char filename[1024], /* job.cache filename */
2107 temp[1024]; /* Temporary string */
2108 cupsd_job_t *job; /* Current job */
2109 time_t curtime; /* Current time */
2110 struct tm *curdate; /* Current date */
2111
2112
2113 snprintf(filename, sizeof(filename), "%s/job.cache", CacheDir);
2114 if ((fp = cupsdCreateConfFile(filename, ConfigFilePerm)) == NULL)
2115 return;
2116
2117 cupsdLogMessage(CUPSD_LOG_INFO, "Saving job.cache...");
2118
2119 /*
2120 * Write a small header to the file...
2121 */
2122
2123 curtime = time(NULL);
2124 curdate = localtime(&curtime);
2125 strftime(temp, sizeof(temp) - 1, "%Y-%m-%d %H:%M", curdate);
2126
2127 cupsFilePuts(fp, "# Job cache file for " CUPS_SVERSION "\n");
2128 cupsFilePrintf(fp, "# Written by cupsd on %s\n", temp);
2129 cupsFilePrintf(fp, "NextJobId %d\n", NextJobId);
2130
2131 /*
2132 * Write each job known to the system...
2133 */
2134
2135 for (job = (cupsd_job_t *)cupsArrayFirst(Jobs);
2136 job;
2137 job = (cupsd_job_t *)cupsArrayNext(Jobs))
2138 {
2139 cupsFilePrintf(fp, "<Job %d>\n", job->id);
2140 cupsFilePrintf(fp, "State %d\n", job->state_value);
2141 cupsFilePrintf(fp, "Priority %d\n", job->priority);
2142 cupsFilePrintf(fp, "HoldUntil %d\n", (int)job->hold_until);
2143 cupsFilePrintf(fp, "Username %s\n", job->username);
2144 cupsFilePrintf(fp, "Destination %s\n", job->dest);
2145 cupsFilePrintf(fp, "DestType %d\n", job->dtype);
2146 cupsFilePrintf(fp, "NumFiles %d\n", job->num_files);
2147 for (i = 0; i < job->num_files; i ++)
2148 cupsFilePrintf(fp, "File %d %s/%s %d\n", i + 1, job->filetypes[i]->super,
2149 job->filetypes[i]->type, job->compressions[i]);
2150 cupsFilePuts(fp, "</Job>\n");
2151 }
2152
2153 cupsdCloseCreatedConfFile(fp, filename);
2154 }
2155
2156
2157 /*
2158 * 'cupsdSaveJob()' - Save a job to disk.
2159 */
2160
2161 void
2162 cupsdSaveJob(cupsd_job_t *job) /* I - Job */
2163 {
2164 char filename[1024]; /* Job control filename */
2165 cups_file_t *fp; /* Job file */
2166
2167
2168 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdSaveJob(job=%p(%d)): job->attrs=%p",
2169 job, job->id, job->attrs);
2170
2171 snprintf(filename, sizeof(filename), "%s/c%05d", RequestRoot, job->id);
2172
2173 if ((fp = cupsdCreateConfFile(filename, ConfigFilePerm & 0600)) == NULL)
2174 return;
2175
2176 fchown(cupsFileNumber(fp), RunUser, Group);
2177
2178 job->attrs->state = IPP_IDLE;
2179
2180 if (ippWriteIO(fp, (ipp_iocb_t)cupsFileWrite, 1, NULL,
2181 job->attrs) != IPP_DATA)
2182 {
2183 cupsdLogMessage(CUPSD_LOG_ERROR,
2184 "[Job %d] Unable to write job control file.", job->id);
2185 cupsFileClose(fp);
2186 return;
2187 }
2188
2189 if (!cupsdCloseCreatedConfFile(fp, filename))
2190 job->dirty = 0;
2191 }
2192
2193
2194 /*
2195 * 'cupsdSetJobHoldUntil()' - Set the hold time for a job.
2196 */
2197
2198 void
2199 cupsdSetJobHoldUntil(cupsd_job_t *job, /* I - Job */
2200 const char *when, /* I - When to resume */
2201 int update)/* I - Update job-hold-until attr? */
2202 {
2203 time_t curtime; /* Current time */
2204 struct tm *curdate; /* Current date */
2205 int hour; /* Hold hour */
2206 int minute; /* Hold minute */
2207 int second = 0; /* Hold second */
2208
2209
2210 cupsdLogMessage(CUPSD_LOG_DEBUG2,
2211 "cupsdSetJobHoldUntil(job=%p(%d), when=\"%s\", update=%d)",
2212 job, job->id, when, update);
2213
2214 if (update)
2215 {
2216 /*
2217 * Update the job-hold-until attribute...
2218 */
2219
2220 ipp_attribute_t *attr; /* job-hold-until attribute */
2221
2222 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
2223 IPP_TAG_KEYWORD)) == NULL)
2224 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
2225
2226 if (attr)
2227 cupsdSetString(&(attr->values[0].string.text), when);
2228 else
2229 attr = ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_KEYWORD,
2230 "job-hold-until", NULL, when);
2231
2232 if (attr)
2233 {
2234 if (isdigit(when[0] & 255))
2235 attr->value_tag = IPP_TAG_NAME;
2236 else
2237 attr->value_tag = IPP_TAG_KEYWORD;
2238
2239 job->dirty = 1;
2240 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
2241 }
2242
2243 ippSetString(job->attrs, &job->reasons, 0, "job-hold-until-specified");
2244 }
2245
2246 /*
2247 * Update the hold time...
2248 */
2249
2250 job->cancel_time = 0;
2251
2252 if (!strcmp(when, "indefinite") || !strcmp(when, "auth-info-required"))
2253 {
2254 /*
2255 * Hold indefinitely...
2256 */
2257
2258 job->hold_until = 0;
2259
2260 if (MaxHoldTime > 0)
2261 job->cancel_time = time(NULL) + MaxHoldTime;
2262 }
2263 else if (!strcmp(when, "day-time"))
2264 {
2265 /*
2266 * Hold to 6am the next morning unless local time is < 6pm.
2267 */
2268
2269 curtime = time(NULL);
2270 curdate = localtime(&curtime);
2271
2272 if (curdate->tm_hour < 18)
2273 job->hold_until = curtime;
2274 else
2275 job->hold_until = curtime +
2276 ((29 - curdate->tm_hour) * 60 + 59 -
2277 curdate->tm_min) * 60 + 60 - curdate->tm_sec;
2278 }
2279 else if (!strcmp(when, "evening") || !strcmp(when, "night"))
2280 {
2281 /*
2282 * Hold to 6pm unless local time is > 6pm or < 6am.
2283 */
2284
2285 curtime = time(NULL);
2286 curdate = localtime(&curtime);
2287
2288 if (curdate->tm_hour < 6 || curdate->tm_hour >= 18)
2289 job->hold_until = curtime;
2290 else
2291 job->hold_until = curtime +
2292 ((17 - curdate->tm_hour) * 60 + 59 -
2293 curdate->tm_min) * 60 + 60 - curdate->tm_sec;
2294 }
2295 else if (!strcmp(when, "second-shift"))
2296 {
2297 /*
2298 * Hold to 4pm unless local time is > 4pm.
2299 */
2300
2301 curtime = time(NULL);
2302 curdate = localtime(&curtime);
2303
2304 if (curdate->tm_hour >= 16)
2305 job->hold_until = curtime;
2306 else
2307 job->hold_until = curtime +
2308 ((15 - curdate->tm_hour) * 60 + 59 -
2309 curdate->tm_min) * 60 + 60 - curdate->tm_sec;
2310 }
2311 else if (!strcmp(when, "third-shift"))
2312 {
2313 /*
2314 * Hold to 12am unless local time is < 8am.
2315 */
2316
2317 curtime = time(NULL);
2318 curdate = localtime(&curtime);
2319
2320 if (curdate->tm_hour < 8)
2321 job->hold_until = curtime;
2322 else
2323 job->hold_until = curtime +
2324 ((23 - curdate->tm_hour) * 60 + 59 -
2325 curdate->tm_min) * 60 + 60 - curdate->tm_sec;
2326 }
2327 else if (!strcmp(when, "weekend"))
2328 {
2329 /*
2330 * Hold to weekend unless we are in the weekend.
2331 */
2332
2333 curtime = time(NULL);
2334 curdate = localtime(&curtime);
2335
2336 if (curdate->tm_wday == 0 || curdate->tm_wday == 6)
2337 job->hold_until = curtime;
2338 else
2339 job->hold_until = curtime +
2340 (((5 - curdate->tm_wday) * 24 +
2341 (17 - curdate->tm_hour)) * 60 + 59 -
2342 curdate->tm_min) * 60 + 60 - curdate->tm_sec;
2343 }
2344 else if (sscanf(when, "%d:%d:%d", &hour, &minute, &second) >= 2)
2345 {
2346 /*
2347 * Hold to specified GMT time (HH:MM or HH:MM:SS)...
2348 */
2349
2350 curtime = time(NULL);
2351 curdate = gmtime(&curtime);
2352
2353 job->hold_until = curtime +
2354 ((hour - curdate->tm_hour) * 60 + minute -
2355 curdate->tm_min) * 60 + second - curdate->tm_sec;
2356
2357 /*
2358 * Hold until next day as needed...
2359 */
2360
2361 if (job->hold_until < curtime)
2362 job->hold_until += 24 * 60 * 60;
2363 }
2364
2365 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdSetJobHoldUntil: hold_until=%d",
2366 (int)job->hold_until);
2367 }
2368
2369
2370 /*
2371 * 'cupsdSetJobPriority()' - Set the priority of a job, moving it up/down in
2372 * the list as needed.
2373 */
2374
2375 void
2376 cupsdSetJobPriority(
2377 cupsd_job_t *job, /* I - Job ID */
2378 int priority) /* I - New priority (0 to 100) */
2379 {
2380 ipp_attribute_t *attr; /* Job attribute */
2381
2382
2383 /*
2384 * Don't change completed jobs...
2385 */
2386
2387 if (job->state_value >= IPP_JOB_PROCESSING)
2388 return;
2389
2390 /*
2391 * Set the new priority and re-add the job into the active list...
2392 */
2393
2394 cupsArrayRemove(ActiveJobs, job);
2395
2396 job->priority = priority;
2397
2398 if ((attr = ippFindAttribute(job->attrs, "job-priority",
2399 IPP_TAG_INTEGER)) != NULL)
2400 attr->values[0].integer = priority;
2401 else
2402 ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-priority",
2403 priority);
2404
2405 cupsArrayAdd(ActiveJobs, job);
2406
2407 job->dirty = 1;
2408 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
2409 }
2410
2411
2412 /*
2413 * 'cupsdSetJobState()' - Set the state of the specified print job.
2414 */
2415
2416 void
2417 cupsdSetJobState(
2418 cupsd_job_t *job, /* I - Job to cancel */
2419 ipp_jstate_t newstate, /* I - New job state */
2420 cupsd_jobaction_t action, /* I - Action to take */
2421 const char *message, /* I - Message to log */
2422 ...) /* I - Additional arguments as needed */
2423 {
2424 int i; /* Looping var */
2425 ipp_jstate_t oldstate; /* Old state */
2426 char filename[1024]; /* Job filename */
2427 ipp_attribute_t *attr; /* Job attribute */
2428
2429
2430 cupsdLogMessage(CUPSD_LOG_DEBUG2,
2431 "cupsdSetJobState(job=%p(%d), state=%d, newstate=%d, "
2432 "action=%d, message=\"%s\")", job, job->id, job->state_value,
2433 newstate, action, message ? message : "(null)");
2434
2435
2436 /*
2437 * Make sure we have the job attributes...
2438 */
2439
2440 if (!cupsdLoadJob(job))
2441 return;
2442
2443 /*
2444 * Don't do anything if the state is unchanged and we aren't purging the
2445 * job...
2446 */
2447
2448 oldstate = job->state_value;
2449 if (newstate == oldstate && action != CUPSD_JOB_PURGE)
2450 return;
2451
2452 /*
2453 * Stop any processes that are working on the current job...
2454 */
2455
2456 if (oldstate == IPP_JOB_PROCESSING)
2457 stop_job(job, action);
2458
2459 /*
2460 * Set the new job state...
2461 */
2462
2463 job->state->values[0].integer = newstate;
2464 job->state_value = newstate;
2465
2466 switch (newstate)
2467 {
2468 case IPP_JOB_PENDING :
2469 /*
2470 * Update job-hold-until as needed...
2471 */
2472
2473 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
2474 IPP_TAG_KEYWORD)) == NULL)
2475 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
2476
2477 if (attr)
2478 {
2479 attr->value_tag = IPP_TAG_KEYWORD;
2480 cupsdSetString(&(attr->values[0].string.text), "no-hold");
2481 }
2482
2483 default :
2484 break;
2485
2486 case IPP_JOB_ABORTED :
2487 case IPP_JOB_CANCELED :
2488 case IPP_JOB_COMPLETED :
2489 set_time(job, "time-at-completed");
2490 ippSetString(job->attrs, &job->reasons, 0, "processing-to-stop-point");
2491 break;
2492 }
2493
2494 /*
2495 * Log message as needed...
2496 */
2497
2498 if (message)
2499 {
2500 char buffer[2048]; /* Message buffer */
2501 va_list ap; /* Pointer to additional arguments */
2502
2503 va_start(ap, message);
2504 vsnprintf(buffer, sizeof(buffer), message, ap);
2505 va_end(ap);
2506
2507 if (newstate > IPP_JOB_STOPPED)
2508 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job, "%s", buffer);
2509 else
2510 cupsdAddEvent(CUPSD_EVENT_JOB_STATE, job->printer, job, "%s", buffer);
2511
2512 if (newstate == IPP_JOB_STOPPED || newstate == IPP_JOB_ABORTED)
2513 cupsdLogJob(job, CUPSD_LOG_ERROR, "%s", buffer);
2514 else
2515 cupsdLogJob(job, CUPSD_LOG_INFO, "%s", buffer);
2516 }
2517
2518 /*
2519 * Handle post-state-change actions...
2520 */
2521
2522 switch (newstate)
2523 {
2524 case IPP_JOB_PROCESSING :
2525 /*
2526 * Add the job to the "printing" list...
2527 */
2528
2529 if (!cupsArrayFind(PrintingJobs, job))
2530 cupsArrayAdd(PrintingJobs, job);
2531
2532 /*
2533 * Set the processing time...
2534 */
2535
2536 set_time(job, "time-at-processing");
2537
2538 case IPP_JOB_PENDING :
2539 case IPP_JOB_HELD :
2540 case IPP_JOB_STOPPED :
2541 /*
2542 * Make sure the job is in the active list...
2543 */
2544
2545 if (!cupsArrayFind(ActiveJobs, job))
2546 cupsArrayAdd(ActiveJobs, job);
2547
2548 /*
2549 * Save the job state to disk...
2550 */
2551
2552 job->dirty = 1;
2553 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
2554 break;
2555
2556 case IPP_JOB_ABORTED :
2557 case IPP_JOB_CANCELED :
2558 case IPP_JOB_COMPLETED :
2559 if (newstate == IPP_JOB_CANCELED)
2560 {
2561 /*
2562 * Remove the job from the active list if there are no processes still
2563 * running for it...
2564 */
2565
2566 for (i = 0; job->filters[i] < 0; i++);
2567
2568 if (!job->filters[i] && job->backend <= 0)
2569 cupsArrayRemove(ActiveJobs, job);
2570 }
2571 else
2572 {
2573 /*
2574 * Otherwise just remove the job from the active list immediately...
2575 */
2576
2577 cupsArrayRemove(ActiveJobs, job);
2578 }
2579
2580 /*
2581 * Expire job subscriptions since the job is now "completed"...
2582 */
2583
2584 cupsdExpireSubscriptions(NULL, job);
2585
2586 #ifdef __APPLE__
2587 /*
2588 * If we are going to sleep and the PrintingJobs count is now 0, allow the
2589 * sleep to happen immediately...
2590 */
2591
2592 if (Sleeping && cupsArrayCount(PrintingJobs) == 0)
2593 cupsdAllowSleep();
2594 #endif /* __APPLE__ */
2595
2596 /*
2597 * Remove any authentication data...
2598 */
2599
2600 snprintf(filename, sizeof(filename), "%s/a%05d", RequestRoot, job->id);
2601 if (cupsdRemoveFile(filename) && errno != ENOENT)
2602 cupsdLogMessage(CUPSD_LOG_ERROR,
2603 "Unable to remove authentication cache: %s",
2604 strerror(errno));
2605
2606 for (i = 0;
2607 i < (int)(sizeof(job->auth_env) / sizeof(job->auth_env[0]));
2608 i ++)
2609 cupsdClearString(job->auth_env + i);
2610
2611 cupsdClearString(&job->auth_uid);
2612
2613 /*
2614 * Remove the print file for good if we aren't preserving jobs or
2615 * files...
2616 */
2617
2618 if (!JobHistory || !JobFiles || action == CUPSD_JOB_PURGE)
2619 remove_job_files(job);
2620
2621 if (JobHistory && action != CUPSD_JOB_PURGE)
2622 {
2623 /*
2624 * Save job state info...
2625 */
2626
2627 job->dirty = 1;
2628 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
2629 }
2630 else if (!job->printer)
2631 {
2632 /*
2633 * Delete the job immediately if not actively printing...
2634 */
2635
2636 cupsdDeleteJob(job, CUPSD_JOB_PURGE);
2637 job = NULL;
2638 }
2639 break;
2640 }
2641
2642 /*
2643 * Finalize the job immediately if we forced things...
2644 */
2645
2646 if (action >= CUPSD_JOB_FORCE && job && job->printer)
2647 finalize_job(job, 0);
2648
2649 /*
2650 * Update the server "busy" state...
2651 */
2652
2653 cupsdSetBusyState();
2654 }
2655
2656
2657 /*
2658 * 'cupsdStopAllJobs()' - Stop all print jobs.
2659 */
2660
2661 void
2662 cupsdStopAllJobs(
2663 cupsd_jobaction_t action, /* I - Action */
2664 int kill_delay) /* I - Number of seconds before we kill */
2665 {
2666 cupsd_job_t *job; /* Current job */
2667
2668
2669 DEBUG_puts("cupsdStopAllJobs()");
2670
2671 for (job = (cupsd_job_t *)cupsArrayFirst(PrintingJobs);
2672 job;
2673 job = (cupsd_job_t *)cupsArrayNext(PrintingJobs))
2674 {
2675 if (kill_delay)
2676 job->kill_time = time(NULL) + kill_delay;
2677
2678 cupsdSetJobState(job, IPP_JOB_PENDING, action, NULL);
2679 }
2680 }
2681
2682
2683 /*
2684 * 'cupsdUnloadCompletedJobs()' - Flush completed job history from memory.
2685 */
2686
2687 void
2688 cupsdUnloadCompletedJobs(void)
2689 {
2690 cupsd_job_t *job; /* Current job */
2691 time_t expire; /* Expiration time */
2692
2693
2694 expire = time(NULL) - 60;
2695
2696 for (job = (cupsd_job_t *)cupsArrayFirst(Jobs);
2697 job;
2698 job = (cupsd_job_t *)cupsArrayNext(Jobs))
2699 if (job->attrs && job->state_value >= IPP_JOB_STOPPED && !job->printer &&
2700 job->access_time < expire)
2701 {
2702 if (job->dirty)
2703 cupsdSaveJob(job);
2704
2705 unload_job(job);
2706 }
2707 }
2708
2709
2710 /*
2711 * 'cupsdUpdateJobs()' - Update the history/file files for all jobs.
2712 */
2713
2714 void
2715 cupsdUpdateJobs(void)
2716 {
2717 cupsd_job_t *job; /* Current job */
2718 time_t curtime; /* Current time */
2719 ipp_attribute_t *attr; /* time-at-completed attribute */
2720
2721
2722 curtime = time(NULL);
2723 JobHistoryUpdate = 0;
2724
2725 for (job = (cupsd_job_t *)cupsArrayFirst(Jobs);
2726 job;
2727 job = (cupsd_job_t *)cupsArrayNext(Jobs))
2728 {
2729 if (job->state_value >= IPP_JOB_CANCELED &&
2730 (attr = ippFindAttribute(job->attrs, "time-at-completed",
2731 IPP_TAG_INTEGER)) != NULL)
2732 {
2733 /*
2734 * Update history/file expiration times...
2735 */
2736
2737 if (JobHistory < INT_MAX)
2738 job->history_time = attr->values[0].integer + JobHistory;
2739 else
2740 job->history_time = INT_MAX;
2741
2742 if (job->history_time < curtime)
2743 {
2744 cupsdDeleteJob(job, CUPSD_JOB_PURGE);
2745 continue;
2746 }
2747
2748 if (job->history_time < JobHistoryUpdate || !JobHistoryUpdate)
2749 JobHistoryUpdate = job->history_time;
2750
2751 if (JobFiles < INT_MAX)
2752 job->file_time = attr->values[0].integer + JobFiles;
2753 else
2754 job->file_time = INT_MAX;
2755
2756 if (job->file_time < JobHistoryUpdate || !JobHistoryUpdate)
2757 JobHistoryUpdate = job->file_time;
2758 }
2759 }
2760
2761 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdUpdateAllJobs: JobHistoryUpdate=%ld",
2762 (long)JobHistoryUpdate);
2763 }
2764
2765
2766 /*
2767 * 'compare_active_jobs()' - Compare the job IDs and priorities of two jobs.
2768 */
2769
2770 static int /* O - Difference */
2771 compare_active_jobs(void *first, /* I - First job */
2772 void *second, /* I - Second job */
2773 void *data) /* I - App data (not used) */
2774 {
2775 int diff; /* Difference */
2776
2777
2778 (void)data;
2779
2780 if ((diff = ((cupsd_job_t *)second)->priority -
2781 ((cupsd_job_t *)first)->priority) != 0)
2782 return (diff);
2783 else
2784 return (((cupsd_job_t *)first)->id - ((cupsd_job_t *)second)->id);
2785 }
2786
2787
2788 /*
2789 * 'compare_jobs()' - Compare the job IDs of two jobs.
2790 */
2791
2792 static int /* O - Difference */
2793 compare_jobs(void *first, /* I - First job */
2794 void *second, /* I - Second job */
2795 void *data) /* I - App data (not used) */
2796 {
2797 (void)data;
2798
2799 return (((cupsd_job_t *)first)->id - ((cupsd_job_t *)second)->id);
2800 }
2801
2802
2803 /*
2804 * 'dump_job_history()' - Dump any debug messages for a job.
2805 */
2806
2807 static void
2808 dump_job_history(cupsd_job_t *job) /* I - Job */
2809 {
2810 int i, /* Looping var */
2811 oldsize; /* Current MaxLogSize */
2812 struct tm *date; /* Date/time value */
2813 cupsd_joblog_t *message; /* Current message */
2814 char temp[2048], /* Log message */
2815 *ptr, /* Pointer into log message */
2816 start[256], /* Start time */
2817 end[256]; /* End time */
2818 cupsd_printer_t *printer; /* Printer for job */
2819
2820
2821 /*
2822 * See if we have anything to dump...
2823 */
2824
2825 if (!job->history)
2826 return;
2827
2828 /*
2829 * Disable log rotation temporarily...
2830 */
2831
2832 oldsize = MaxLogSize;
2833 MaxLogSize = 0;
2834
2835 /*
2836 * Copy the debug messages to the log...
2837 */
2838
2839 message = (cupsd_joblog_t *)cupsArrayFirst(job->history);
2840 date = localtime(&(message->time));
2841 strftime(start, sizeof(start), "%X", date);
2842
2843 message = (cupsd_joblog_t *)cupsArrayLast(job->history);
2844 date = localtime(&(message->time));
2845 strftime(end, sizeof(end), "%X", date);
2846
2847 snprintf(temp, sizeof(temp),
2848 "[Job %d] The following messages were recorded from %s to %s",
2849 job->id, start, end);
2850 cupsdWriteErrorLog(CUPSD_LOG_DEBUG, temp);
2851
2852 for (message = (cupsd_joblog_t *)cupsArrayFirst(job->history);
2853 message;
2854 message = (cupsd_joblog_t *)cupsArrayNext(job->history))
2855 cupsdWriteErrorLog(CUPSD_LOG_DEBUG, message->message);
2856
2857 snprintf(temp, sizeof(temp), "[Job %d] End of messages", job->id);
2858 cupsdWriteErrorLog(CUPSD_LOG_DEBUG, temp);
2859
2860 /*
2861 * Log the printer state values...
2862 */
2863
2864 if ((printer = job->printer) == NULL)
2865 printer = cupsdFindDest(job->dest);
2866
2867 if (printer)
2868 {
2869 snprintf(temp, sizeof(temp), "[Job %d] printer-state=%d(%s)", job->id,
2870 printer->state,
2871 printer->state == IPP_PRINTER_IDLE ? "idle" :
2872 printer->state == IPP_PRINTER_PROCESSING ? "processing" :
2873 "stopped");
2874 cupsdWriteErrorLog(CUPSD_LOG_DEBUG, temp);
2875
2876 snprintf(temp, sizeof(temp), "[Job %d] printer-state-message=\"%s\"",
2877 job->id, printer->state_message);
2878 cupsdWriteErrorLog(CUPSD_LOG_DEBUG, temp);
2879
2880 snprintf(temp, sizeof(temp), "[Job %d] printer-state-reasons=", job->id);
2881 ptr = temp + strlen(temp);
2882 if (printer->num_reasons == 0)
2883 strlcpy(ptr, "none", sizeof(temp) - (ptr - temp));
2884 else
2885 {
2886 for (i = 0;
2887 i < printer->num_reasons && ptr < (temp + sizeof(temp) - 2);
2888 i ++)
2889 {
2890 if (i)
2891 *ptr++ = ',';
2892
2893 strlcpy(ptr, printer->reasons[i], sizeof(temp) - (ptr - temp));
2894 ptr += strlen(ptr);
2895 }
2896 }
2897 cupsdWriteErrorLog(CUPSD_LOG_DEBUG, temp);
2898 }
2899
2900 /*
2901 * Restore log file rotation...
2902 */
2903
2904 MaxLogSize = oldsize;
2905
2906 /*
2907 * Free all messages...
2908 */
2909
2910 free_job_history(job);
2911 }
2912
2913
2914 /*
2915 * 'free_job_history()' - Free any log history.
2916 */
2917
2918 static void
2919 free_job_history(cupsd_job_t *job) /* I - Job */
2920 {
2921 char *message; /* Current message */
2922
2923
2924 if (!job->history)
2925 return;
2926
2927 for (message = (char *)cupsArrayFirst(job->history);
2928 message;
2929 message = (char *)cupsArrayNext(job->history))
2930 free(message);
2931
2932 cupsArrayDelete(job->history);
2933 job->history = NULL;
2934 }
2935
2936
2937 /*
2938 * 'finalize_job()' - Cleanup after job filter processes and support data.
2939 */
2940
2941 static void
2942 finalize_job(cupsd_job_t *job, /* I - Job */
2943 int set_job_state) /* I - 1 = set the job state */
2944 {
2945 ipp_pstate_t printer_state; /* New printer state value */
2946 ipp_jstate_t job_state; /* New job state value */
2947 const char *message; /* Message for job state */
2948 char buffer[1024]; /* Buffer for formatted messages */
2949
2950
2951 cupsdLogMessage(CUPSD_LOG_DEBUG2, "finalize_job(job=%p(%d))", job, job->id);
2952
2953 /*
2954 * Clear the "connecting-to-device" reason, which is only valid when a printer
2955 * is processing, along with any remote printing job state...
2956 */
2957
2958 cupsdSetPrinterReasons(job->printer, "-connecting-to-device,"
2959 "cups-remote-pending,"
2960 "cups-remote-pending-held,"
2961 "cups-remote-processing,"
2962 "cups-remote-stopped,"
2963 "cups-remote-canceled,"
2964 "cups-remote-aborted,"
2965 "cups-remote-completed");
2966
2967 /*
2968 * Similarly, clear the "offline-report" reason for non-USB devices since we
2969 * rarely have current information for network devices...
2970 */
2971
2972 if (strncmp(job->printer->device_uri, "usb:", 4))
2973 cupsdSetPrinterReasons(job->printer, "-offline-report");
2974
2975 /*
2976 * Free the security profile...
2977 */
2978
2979 cupsdDestroyProfile(job->profile);
2980 job->profile = NULL;
2981
2982 /*
2983 * Clear the unresponsive job watchdog timer...
2984 */
2985
2986 job->kill_time = 0;
2987
2988 /*
2989 * Close pipes and status buffer...
2990 */
2991
2992 cupsdClosePipe(job->print_pipes);
2993 cupsdClosePipe(job->back_pipes);
2994 cupsdClosePipe(job->side_pipes);
2995
2996 cupsdRemoveSelect(job->status_pipes[0]);
2997 cupsdClosePipe(job->status_pipes);
2998 cupsdStatBufDelete(job->status_buffer);
2999 job->status_buffer = NULL;
3000
3001 /*
3002 * Process the exit status...
3003 */
3004
3005 if (job->printer->state == IPP_PRINTER_PROCESSING)
3006 printer_state = IPP_PRINTER_IDLE;
3007 else
3008 printer_state = job->printer->state;
3009
3010 switch (job_state = job->state_value)
3011 {
3012 case IPP_JOB_PENDING :
3013 message = "Job paused.";
3014 break;
3015
3016 case IPP_JOB_HELD :
3017 message = "Job held.";
3018 break;
3019
3020 default :
3021 case IPP_JOB_PROCESSING :
3022 case IPP_JOB_COMPLETED :
3023 job_state = IPP_JOB_COMPLETED;
3024 message = "Job completed.";
3025
3026 ippSetString(job->attrs, &job->reasons, 0,
3027 "job-completed-successfully");
3028 break;
3029
3030 case IPP_JOB_STOPPED :
3031 message = "Job stopped.";
3032
3033 ippSetString(job->attrs, &job->reasons, 0, "job-stopped");
3034 break;
3035
3036 case IPP_JOB_CANCELED :
3037 message = "Job canceled.";
3038
3039 ippSetString(job->attrs, &job->reasons, 0, "job-canceled-by-user");
3040 break;
3041
3042 case IPP_JOB_ABORTED :
3043 message = "Job aborted.";
3044 break;
3045 }
3046
3047 if (job->status < 0)
3048 {
3049 /*
3050 * Backend had errors...
3051 */
3052
3053 int exit_code; /* Exit code from backend */
3054
3055 /*
3056 * Convert the status to an exit code. Due to the way the W* macros are
3057 * implemented on MacOS X (bug?), we have to store the exit status in a
3058 * variable first and then convert...
3059 */
3060
3061 exit_code = -job->status;
3062 if (WIFEXITED(exit_code))
3063 exit_code = WEXITSTATUS(exit_code);
3064 else
3065 {
3066 ippSetString(job->attrs, &job->reasons, 0, "cups-backend-crashed");
3067 exit_code = job->status;
3068 }
3069
3070 cupsdLogJob(job, CUPSD_LOG_INFO, "Backend returned status %d (%s)",
3071 exit_code,
3072 exit_code == CUPS_BACKEND_FAILED ? "failed" :
3073 exit_code == CUPS_BACKEND_AUTH_REQUIRED ?
3074 "authentication required" :
3075 exit_code == CUPS_BACKEND_HOLD ? "hold job" :
3076 exit_code == CUPS_BACKEND_STOP ? "stop printer" :
3077 exit_code == CUPS_BACKEND_CANCEL ? "cancel job" :
3078 exit_code < 0 ? "crashed" : "unknown");
3079
3080 /*
3081 * Do what needs to be done...
3082 */
3083
3084 switch (exit_code)
3085 {
3086 default :
3087 case CUPS_BACKEND_FAILED :
3088 /*
3089 * Backend failure, use the error-policy to determine how to
3090 * act...
3091 */
3092
3093 if (job->dtype & CUPS_PRINTER_CLASS)
3094 {
3095 /*
3096 * Queued on a class - mark the job as pending and we'll retry on
3097 * another printer...
3098 */
3099
3100 if (job_state == IPP_JOB_COMPLETED)
3101 {
3102 job_state = IPP_JOB_PENDING;
3103 message = "Retrying job on another printer.";
3104
3105 ippSetString(job->attrs, &job->reasons, 0,
3106 "resources-are-not-ready");
3107 }
3108 }
3109 else if (!strcmp(job->printer->error_policy, "retry-current-job"))
3110 {
3111 /*
3112 * The error policy is "retry-current-job" - mark the job as pending
3113 * and we'll retry on the same printer...
3114 */
3115
3116 if (job_state == IPP_JOB_COMPLETED)
3117 {
3118 job_state = IPP_JOB_PENDING;
3119 message = "Retrying job on same printer.";
3120
3121 ippSetString(job->attrs, &job->reasons, 0, "none");
3122 }
3123 }
3124 else if ((job->printer->type & CUPS_PRINTER_FAX) ||
3125 !strcmp(job->printer->error_policy, "retry-job"))
3126 {
3127 if (job_state == IPP_JOB_COMPLETED)
3128 {
3129 /*
3130 * The job was queued on a fax or the error policy is "retry-job" -
3131 * hold the job if the number of retries is less than the
3132 * JobRetryLimit, otherwise abort the job.
3133 */
3134
3135 job->tries ++;
3136
3137 if (job->tries > JobRetryLimit && JobRetryLimit > 0)
3138 {
3139 /*
3140 * Too many tries...
3141 */
3142
3143 snprintf(buffer, sizeof(buffer),
3144 "Job aborted after %d unsuccessful attempts.",
3145 JobRetryLimit);
3146 job_state = IPP_JOB_ABORTED;
3147 message = buffer;
3148
3149 ippSetString(job->attrs, &job->reasons, 0, "aborted-by-system");
3150 }
3151 else
3152 {
3153 /*
3154 * Try again in N seconds...
3155 */
3156
3157 snprintf(buffer, sizeof(buffer),
3158 "Job held for %d seconds since it could not be sent.",
3159 JobRetryInterval);
3160
3161 job->hold_until = time(NULL) + JobRetryInterval;
3162 job_state = IPP_JOB_HELD;
3163 message = buffer;
3164
3165 ippSetString(job->attrs, &job->reasons, 0,
3166 "resources-are-not-ready");
3167 }
3168 }
3169 }
3170 else if (!strcmp(job->printer->error_policy, "abort-job") &&
3171 job_state == IPP_JOB_COMPLETED)
3172 {
3173 job_state = IPP_JOB_ABORTED;
3174 message = "Job aborted due to backend errors; please consult "
3175 "the error_log file for details.";
3176
3177 ippSetString(job->attrs, &job->reasons, 0, "aborted-by-system");
3178 }
3179 else if (job->state_value == IPP_JOB_PROCESSING)
3180 {
3181 job_state = IPP_JOB_PENDING;
3182 printer_state = IPP_PRINTER_STOPPED;
3183 message = "Printer stopped due to backend errors; please "
3184 "consult the error_log file for details.";
3185
3186 ippSetString(job->attrs, &job->reasons, 0, "none");
3187 }
3188 break;
3189
3190 case CUPS_BACKEND_CANCEL :
3191 /*
3192 * Cancel the job...
3193 */
3194
3195 if (job_state == IPP_JOB_COMPLETED)
3196 {
3197 job_state = IPP_JOB_CANCELED;
3198 message = "Job canceled at printer.";
3199
3200 ippSetString(job->attrs, &job->reasons, 0, "canceled-at-device");
3201 }
3202 break;
3203
3204 case CUPS_BACKEND_HOLD :
3205 if (job_state == IPP_JOB_COMPLETED)
3206 {
3207 /*
3208 * Hold the job...
3209 */
3210
3211 cupsdSetJobHoldUntil(job, "indefinite", 1);
3212 ippSetString(job->attrs, &job->reasons, 0,
3213 "job-hold-until-specified");
3214
3215 job_state = IPP_JOB_HELD;
3216 message = "Job held indefinitely due to backend errors; please "
3217 "consult the error_log file for details.";
3218 }
3219 break;
3220
3221 case CUPS_BACKEND_STOP :
3222 /*
3223 * Stop the printer...
3224 */
3225
3226 printer_state = IPP_PRINTER_STOPPED;
3227 message = "Printer stopped due to backend errors; please "
3228 "consult the error_log file for details.";
3229
3230 if (job_state == IPP_JOB_COMPLETED)
3231 {
3232 job_state = IPP_JOB_PENDING;
3233
3234 ippSetString(job->attrs, &job->reasons, 0,
3235 "resources-are-not-ready");
3236 }
3237 break;
3238
3239 case CUPS_BACKEND_AUTH_REQUIRED :
3240 /*
3241 * Hold the job for authentication...
3242 */
3243
3244 if (job_state == IPP_JOB_COMPLETED)
3245 {
3246 cupsdSetJobHoldUntil(job, "auth-info-required", 1);
3247
3248 job_state = IPP_JOB_HELD;
3249 message = "Job held for authentication.";
3250
3251 ippSetString(job->attrs, &job->reasons, 0,
3252 "cups-held-for-authentication");
3253 }
3254 break;
3255
3256 case CUPS_BACKEND_RETRY :
3257 if (job_state == IPP_JOB_COMPLETED)
3258 {
3259 /*
3260 * Hold the job if the number of retries is less than the
3261 * JobRetryLimit, otherwise abort the job.
3262 */
3263
3264 job->tries ++;
3265
3266 if (job->tries > JobRetryLimit && JobRetryLimit > 0)
3267 {
3268 /*
3269 * Too many tries...
3270 */
3271
3272 snprintf(buffer, sizeof(buffer),
3273 "Job aborted after %d unsuccessful attempts.",
3274 JobRetryLimit);
3275 job_state = IPP_JOB_ABORTED;
3276 message = buffer;
3277
3278 ippSetString(job->attrs, &job->reasons, 0, "aborted-by-system");
3279 }
3280 else
3281 {
3282 /*
3283 * Try again in N seconds...
3284 */
3285
3286 snprintf(buffer, sizeof(buffer),
3287 "Job held for %d seconds since it could not be sent.",
3288 JobRetryInterval);
3289
3290 job->hold_until = time(NULL) + JobRetryInterval;
3291 job_state = IPP_JOB_HELD;
3292 message = buffer;
3293
3294 ippSetString(job->attrs, &job->reasons, 0,
3295 "resources-are-not-ready");
3296 }
3297 }
3298 break;
3299
3300 case CUPS_BACKEND_RETRY_CURRENT :
3301 /*
3302 * Mark the job as pending and retry on the same printer...
3303 */
3304
3305 if (job_state == IPP_JOB_COMPLETED)
3306 {
3307 job_state = IPP_JOB_PENDING;
3308 message = "Retrying job on same printer.";
3309
3310 ippSetString(job->attrs, &job->reasons, 0, "none");
3311 }
3312 break;
3313 }
3314 }
3315 else if (job->status > 0)
3316 {
3317 /*
3318 * Filter had errors; stop job...
3319 */
3320
3321 if (job_state == IPP_JOB_COMPLETED)
3322 {
3323 job_state = IPP_JOB_STOPPED;
3324 message = "Job stopped due to filter errors; please consult the "
3325 "error_log file for details.";
3326
3327 if (WIFSIGNALED(job->status))
3328 ippSetString(job->attrs, &job->reasons, 0, "cups-filter-crashed");
3329 else
3330 ippSetString(job->attrs, &job->reasons, 0, "job-completed-with-errors");
3331 }
3332 }
3333
3334 /*
3335 * Update the printer and job state.
3336 */
3337
3338 if (set_job_state && job_state != job->state_value)
3339 cupsdSetJobState(job, job_state, CUPSD_JOB_DEFAULT, "%s", message);
3340
3341 cupsdSetPrinterState(job->printer, printer_state,
3342 printer_state == IPP_PRINTER_STOPPED);
3343 update_job_attrs(job, 0);
3344
3345 if (job->history)
3346 {
3347 if (job->status &&
3348 (job->state_value == IPP_JOB_ABORTED ||
3349 job->state_value == IPP_JOB_STOPPED))
3350 dump_job_history(job);
3351 else
3352 free_job_history(job);
3353 }
3354
3355 cupsArrayRemove(PrintingJobs, job);
3356
3357 /*
3358 * Clear the printer <-> job association...
3359 */
3360
3361 job->printer->job = NULL;
3362 job->printer = NULL;
3363
3364 /*
3365 * Try printing another job...
3366 */
3367
3368 if (printer_state != IPP_PRINTER_STOPPED)
3369 cupsdCheckJobs();
3370 }
3371
3372
3373 /*
3374 * 'get_options()' - Get a string containing the job options.
3375 */
3376
3377 static char * /* O - Options string */
3378 get_options(cupsd_job_t *job, /* I - Job */
3379 int banner_page, /* I - Printing a banner page? */
3380 char *copies, /* I - Copies buffer */
3381 size_t copies_size, /* I - Size of copies buffer */
3382 char *title, /* I - Title buffer */
3383 size_t title_size) /* I - Size of title buffer */
3384 {
3385 int i; /* Looping var */
3386 size_t newlength; /* New option buffer length */
3387 char *optptr, /* Pointer to options */
3388 *valptr; /* Pointer in value string */
3389 ipp_attribute_t *attr; /* Current attribute */
3390 _ppd_cache_t *pc; /* PPD cache and mapping data */
3391 int num_pwgppds; /* Number of PWG->PPD options */
3392 cups_option_t *pwgppds, /* PWG->PPD options */
3393 *pwgppd, /* Current PWG->PPD option */
3394 *preset; /* Current preset option */
3395 int print_color_mode,
3396 /* Output mode (if any) */
3397 print_quality; /* Print quality (if any) */
3398 const char *ppd; /* PPD option choice */
3399 int exact; /* Did we get an exact match? */
3400 static char *options = NULL;/* Full list of options */
3401 static size_t optlength = 0; /* Length of option buffer */
3402
3403
3404 /*
3405 * Building the options string is harder than it needs to be, but for the
3406 * moment we need to pass strings for command-line args and not IPP attribute
3407 * pointers... :)
3408 *
3409 * First build an options array for any PWG->PPD mapped option/choice pairs.
3410 */
3411
3412 pc = job->printer->pc;
3413 num_pwgppds = 0;
3414 pwgppds = NULL;
3415
3416 if (pc &&
3417 !ippFindAttribute(job->attrs,
3418 "com.apple.print.DocumentTicket.PMSpoolFormat",
3419 IPP_TAG_ZERO) &&
3420 !ippFindAttribute(job->attrs, "APPrinterPreset", IPP_TAG_ZERO) &&
3421 (ippFindAttribute(job->attrs, "output-mode", IPP_TAG_ZERO) ||
3422 ippFindAttribute(job->attrs, "print-color-mode", IPP_TAG_ZERO) ||
3423 ippFindAttribute(job->attrs, "print-quality", IPP_TAG_ZERO)))
3424 {
3425 /*
3426 * Map output-mode and print-quality to a preset...
3427 */
3428
3429 if ((attr = ippFindAttribute(job->attrs, "print-color-mode",
3430 IPP_TAG_KEYWORD)) == NULL)
3431 attr = ippFindAttribute(job->attrs, "output-mode", IPP_TAG_KEYWORD);
3432
3433 if (attr && !strcmp(attr->values[0].string.text, "monochrome"))
3434 print_color_mode = _PWG_PRINT_COLOR_MODE_MONOCHROME;
3435 else
3436 print_color_mode = _PWG_PRINT_COLOR_MODE_COLOR;
3437
3438 if ((attr = ippFindAttribute(job->attrs, "print-quality",
3439 IPP_TAG_ENUM)) != NULL &&
3440 attr->values[0].integer >= IPP_QUALITY_DRAFT &&
3441 attr->values[0].integer <= IPP_QUALITY_HIGH)
3442 print_quality = attr->values[0].integer - IPP_QUALITY_DRAFT;
3443 else
3444 print_quality = _PWG_PRINT_QUALITY_NORMAL;
3445
3446 if (pc->num_presets[print_color_mode][print_quality] == 0)
3447 {
3448 /*
3449 * Try to find a preset that works so that we maximize the chances of us
3450 * getting a good print using IPP attributes.
3451 */
3452
3453 if (pc->num_presets[print_color_mode][_PWG_PRINT_QUALITY_NORMAL] > 0)
3454 print_quality = _PWG_PRINT_QUALITY_NORMAL;
3455 else if (pc->num_presets[_PWG_PRINT_COLOR_MODE_COLOR][print_quality] > 0)
3456 print_color_mode = _PWG_PRINT_COLOR_MODE_COLOR;
3457 else
3458 {
3459 print_quality = _PWG_PRINT_QUALITY_NORMAL;
3460 print_color_mode = _PWG_PRINT_COLOR_MODE_COLOR;
3461 }
3462 }
3463
3464 if (pc->num_presets[print_color_mode][print_quality] > 0)
3465 {
3466 /*
3467 * Copy the preset options as long as the corresponding names are not
3468 * already defined in the IPP request...
3469 */
3470
3471 for (i = pc->num_presets[print_color_mode][print_quality],
3472 preset = pc->presets[print_color_mode][print_quality];
3473 i > 0;
3474 i --, preset ++)
3475 {
3476 if (!ippFindAttribute(job->attrs, preset->name, IPP_TAG_ZERO))
3477 num_pwgppds = cupsAddOption(preset->name, preset->value, num_pwgppds,
3478 &pwgppds);
3479 }
3480 }
3481 }
3482
3483 if (pc)
3484 {
3485 if (!ippFindAttribute(job->attrs, "InputSlot", IPP_TAG_ZERO) &&
3486 !ippFindAttribute(job->attrs, "HPPaperSource", IPP_TAG_ZERO))
3487 {
3488 if ((ppd = _ppdCacheGetInputSlot(pc, job->attrs, NULL)) != NULL)
3489 num_pwgppds = cupsAddOption(pc->source_option, ppd, num_pwgppds,
3490 &pwgppds);
3491 }
3492 if (!ippFindAttribute(job->attrs, "MediaType", IPP_TAG_ZERO) &&
3493 (ppd = _ppdCacheGetMediaType(pc, job->attrs, NULL)) != NULL)
3494 num_pwgppds = cupsAddOption("MediaType", ppd, num_pwgppds, &pwgppds);
3495
3496 if (!ippFindAttribute(job->attrs, "PageRegion", IPP_TAG_ZERO) &&
3497 !ippFindAttribute(job->attrs, "PageSize", IPP_TAG_ZERO) &&
3498 (ppd = _ppdCacheGetPageSize(pc, job->attrs, NULL, &exact)) != NULL)
3499 {
3500 num_pwgppds = cupsAddOption("PageSize", ppd, num_pwgppds, &pwgppds);
3501
3502 if (!ippFindAttribute(job->attrs, "media", IPP_TAG_ZERO))
3503 num_pwgppds = cupsAddOption("media", ppd, num_pwgppds, &pwgppds);
3504 }
3505
3506 if (!ippFindAttribute(job->attrs, "OutputBin", IPP_TAG_ZERO) &&
3507 (attr = ippFindAttribute(job->attrs, "output-bin",
3508 IPP_TAG_ZERO)) != NULL &&
3509 (attr->value_tag == IPP_TAG_KEYWORD ||
3510 attr->value_tag == IPP_TAG_NAME) &&
3511 (ppd = _ppdCacheGetOutputBin(pc, attr->values[0].string.text)) != NULL)
3512 {
3513 /*
3514 * Map output-bin to OutputBin option...
3515 */
3516
3517 num_pwgppds = cupsAddOption("OutputBin", ppd, num_pwgppds, &pwgppds);
3518 }
3519
3520 if (pc->sides_option &&
3521 !ippFindAttribute(job->attrs, pc->sides_option, IPP_TAG_ZERO) &&
3522 (attr = ippFindAttribute(job->attrs, "sides", IPP_TAG_KEYWORD)) != NULL)
3523 {
3524 /*
3525 * Map sides to duplex option...
3526 */
3527
3528 if (!strcmp(attr->values[0].string.text, "one-sided"))
3529 num_pwgppds = cupsAddOption(pc->sides_option, pc->sides_1sided,
3530 num_pwgppds, &pwgppds);
3531 else if (!strcmp(attr->values[0].string.text, "two-sided-long-edge"))
3532 num_pwgppds = cupsAddOption(pc->sides_option, pc->sides_2sided_long,
3533 num_pwgppds, &pwgppds);
3534 else if (!strcmp(attr->values[0].string.text, "two-sided-short-edge"))
3535 num_pwgppds = cupsAddOption(pc->sides_option, pc->sides_2sided_short,
3536 num_pwgppds, &pwgppds);
3537 }
3538
3539 /*
3540 * Map finishings values...
3541 */
3542
3543 num_pwgppds = _ppdCacheGetFinishingOptions(pc, job->attrs,
3544 IPP_FINISHINGS_NONE, num_pwgppds,
3545 &pwgppds);
3546 }
3547
3548 /*
3549 * Figure out how much room we need...
3550 */
3551
3552 newlength = ipp_length(job->attrs);
3553
3554 for (i = num_pwgppds, pwgppd = pwgppds; i > 0; i --, pwgppd ++)
3555 newlength += 1 + strlen(pwgppd->name) + 1 + strlen(pwgppd->value);
3556
3557 /*
3558 * Then allocate/reallocate the option buffer as needed...
3559 */
3560
3561 if (newlength == 0) /* This can never happen, but Clang */
3562 newlength = 1; /* thinks it can... */
3563
3564 if (newlength > optlength || !options)
3565 {
3566 if (!options)
3567 optptr = malloc(newlength);
3568 else
3569 optptr = realloc(options, newlength);
3570
3571 if (!optptr)
3572 {
3573 cupsdLogJob(job, CUPSD_LOG_CRIT,
3574 "Unable to allocate " CUPS_LLFMT " bytes for option buffer.",
3575 CUPS_LLCAST newlength);
3576 return (NULL);
3577 }
3578
3579 options = optptr;
3580 optlength = newlength;
3581 }
3582
3583 /*
3584 * Now loop through the attributes and convert them to the textual
3585 * representation used by the filters...
3586 */
3587
3588 optptr = options;
3589 *optptr = '\0';
3590
3591 snprintf(title, title_size, "%s-%d", job->printer->name, job->id);
3592 strlcpy(copies, "1", copies_size);
3593
3594 for (attr = job->attrs->attrs; attr != NULL; attr = attr->next)
3595 {
3596 if (!strcmp(attr->name, "copies") &&
3597 attr->value_tag == IPP_TAG_INTEGER)
3598 {
3599 /*
3600 * Don't use the # copies attribute if we are printing the job sheets...
3601 */
3602
3603 if (!banner_page)
3604 snprintf(copies, copies_size, "%d", attr->values[0].integer);
3605 }
3606 else if (!strcmp(attr->name, "job-name") &&
3607 (attr->value_tag == IPP_TAG_NAME ||
3608 attr->value_tag == IPP_TAG_NAMELANG))
3609 strlcpy(title, attr->values[0].string.text, title_size);
3610 else if (attr->group_tag == IPP_TAG_JOB)
3611 {
3612 /*
3613 * Filter out other unwanted attributes...
3614 */
3615
3616 if (attr->value_tag == IPP_TAG_NOVALUE ||
3617 attr->value_tag == IPP_TAG_MIMETYPE ||
3618 attr->value_tag == IPP_TAG_NAMELANG ||
3619 attr->value_tag == IPP_TAG_TEXTLANG ||
3620 (attr->value_tag == IPP_TAG_URI && strcmp(attr->name, "job-uuid")) ||
3621 attr->value_tag == IPP_TAG_URISCHEME ||
3622 attr->value_tag == IPP_TAG_BEGIN_COLLECTION) /* Not yet supported */
3623 continue;
3624
3625 if (!strcmp(attr->name, "job-hold-until"))
3626 continue;
3627
3628 if (!strncmp(attr->name, "job-", 4) &&
3629 strcmp(attr->name, "job-billing") &&
3630 strcmp(attr->name, "job-impressions") &&
3631 strcmp(attr->name, "job-originating-host-name") &&
3632 strcmp(attr->name, "job-uuid") &&
3633 !(job->printer->type & CUPS_PRINTER_REMOTE))
3634 continue;
3635
3636 if ((!strcmp(attr->name, "job-impressions") ||
3637 !strcmp(attr->name, "page-label") ||
3638 !strcmp(attr->name, "page-border") ||
3639 !strncmp(attr->name, "number-up", 9) ||
3640 !strcmp(attr->name, "page-ranges") ||
3641 !strcmp(attr->name, "page-set") ||
3642 !_cups_strcasecmp(attr->name, "AP_FIRSTPAGE_InputSlot") ||
3643 !_cups_strcasecmp(attr->name, "AP_FIRSTPAGE_ManualFeed") ||
3644 !_cups_strcasecmp(attr->name, "com.apple.print.PrintSettings."
3645 "PMTotalSidesImaged..n.") ||
3646 !_cups_strcasecmp(attr->name, "com.apple.print.PrintSettings."
3647 "PMTotalBeginPages..n.")) &&
3648 banner_page)
3649 continue;
3650
3651 /*
3652 * Otherwise add them to the list...
3653 */
3654
3655 if (optptr > options)
3656 strlcat(optptr, " ", optlength - (optptr - options));
3657
3658 if (attr->value_tag != IPP_TAG_BOOLEAN)
3659 {
3660 strlcat(optptr, attr->name, optlength - (optptr - options));
3661 strlcat(optptr, "=", optlength - (optptr - options));
3662 }
3663
3664 for (i = 0; i < attr->num_values; i ++)
3665 {
3666 if (i)
3667 strlcat(optptr, ",", optlength - (optptr - options));
3668
3669 optptr += strlen(optptr);
3670
3671 switch (attr->value_tag)
3672 {
3673 case IPP_TAG_INTEGER :
3674 case IPP_TAG_ENUM :
3675 snprintf(optptr, optlength - (optptr - options),
3676 "%d", attr->values[i].integer);
3677 break;
3678
3679 case IPP_TAG_BOOLEAN :
3680 if (!attr->values[i].boolean)
3681 strlcat(optptr, "no", optlength - (optptr - options));
3682
3683 strlcat(optptr, attr->name,
3684 optlength - (optptr - options));
3685 break;
3686
3687 case IPP_TAG_RANGE :
3688 if (attr->values[i].range.lower == attr->values[i].range.upper)
3689 snprintf(optptr, optlength - (optptr - options) - 1,
3690 "%d", attr->values[i].range.lower);
3691 else
3692 snprintf(optptr, optlength - (optptr - options) - 1,
3693 "%d-%d", attr->values[i].range.lower,
3694 attr->values[i].range.upper);
3695 break;
3696
3697 case IPP_TAG_RESOLUTION :
3698 snprintf(optptr, optlength - (optptr - options) - 1,
3699 "%dx%d%s", attr->values[i].resolution.xres,
3700 attr->values[i].resolution.yres,
3701 attr->values[i].resolution.units == IPP_RES_PER_INCH ?
3702 "dpi" : "dpcm");
3703 break;
3704
3705 case IPP_TAG_STRING :
3706 case IPP_TAG_TEXT :
3707 case IPP_TAG_NAME :
3708 case IPP_TAG_KEYWORD :
3709 case IPP_TAG_CHARSET :
3710 case IPP_TAG_LANGUAGE :
3711 case IPP_TAG_URI :
3712 for (valptr = attr->values[i].string.text; *valptr;)
3713 {
3714 if (strchr(" \t\n\\\'\"", *valptr))
3715 *optptr++ = '\\';
3716 *optptr++ = *valptr++;
3717 }
3718
3719 *optptr = '\0';
3720 break;
3721
3722 default :
3723 break; /* anti-compiler-warning-code */
3724 }
3725 }
3726
3727 optptr += strlen(optptr);
3728 }
3729 }
3730
3731 /*
3732 * Finally loop through the PWG->PPD mapped options and add them...
3733 */
3734
3735 for (i = num_pwgppds, pwgppd = pwgppds; i > 0; i --, pwgppd ++)
3736 {
3737 *optptr++ = ' ';
3738 strcpy(optptr, pwgppd->name);
3739 optptr += strlen(optptr);
3740 *optptr++ = '=';
3741 strcpy(optptr, pwgppd->value);
3742 optptr += strlen(optptr);
3743 }
3744
3745 cupsFreeOptions(num_pwgppds, pwgppds);
3746
3747 /*
3748 * Return the options string...
3749 */
3750
3751 return (options);
3752 }
3753
3754
3755 /*
3756 * 'ipp_length()' - Compute the size of the buffer needed to hold
3757 * the textual IPP attributes.
3758 */
3759
3760 static size_t /* O - Size of attribute buffer */
3761 ipp_length(ipp_t *ipp) /* I - IPP request */
3762 {
3763 size_t bytes; /* Number of bytes */
3764 int i; /* Looping var */
3765 ipp_attribute_t *attr; /* Current attribute */
3766
3767
3768 /*
3769 * Loop through all attributes...
3770 */
3771
3772 bytes = 0;
3773
3774 for (attr = ipp->attrs; attr != NULL; attr = attr->next)
3775 {
3776 /*
3777 * Skip attributes that won't be sent to filters...
3778 */
3779
3780 if (attr->value_tag == IPP_TAG_NOVALUE ||
3781 attr->value_tag == IPP_TAG_MIMETYPE ||
3782 attr->value_tag == IPP_TAG_NAMELANG ||
3783 attr->value_tag == IPP_TAG_TEXTLANG ||
3784 attr->value_tag == IPP_TAG_URI ||
3785 attr->value_tag == IPP_TAG_URISCHEME)
3786 continue;
3787
3788 /*
3789 * Add space for a leading space and commas between each value.
3790 * For the first attribute, the leading space isn't used, so the
3791 * extra byte can be used as the nul terminator...
3792 */
3793
3794 bytes ++; /* " " separator */
3795 bytes += attr->num_values; /* "," separators */
3796
3797 /*
3798 * Boolean attributes appear as "foo,nofoo,foo,nofoo", while
3799 * other attributes appear as "foo=value1,value2,...,valueN".
3800 */
3801
3802 if (attr->value_tag != IPP_TAG_BOOLEAN)
3803 bytes += strlen(attr->name);
3804 else
3805 bytes += attr->num_values * strlen(attr->name);
3806
3807 /*
3808 * Now add the size required for each value in the attribute...
3809 */
3810
3811 switch (attr->value_tag)
3812 {
3813 case IPP_TAG_INTEGER :
3814 case IPP_TAG_ENUM :
3815 /*
3816 * Minimum value of a signed integer is -2147483647, or 11 digits.
3817 */
3818
3819 bytes += attr->num_values * 11;
3820 break;
3821
3822 case IPP_TAG_BOOLEAN :
3823 /*
3824 * Add two bytes for each false ("no") value...
3825 */
3826
3827 for (i = 0; i < attr->num_values; i ++)
3828 if (!attr->values[i].boolean)
3829 bytes += 2;
3830 break;
3831
3832 case IPP_TAG_RANGE :
3833 /*
3834 * A range is two signed integers separated by a hyphen, or
3835 * 23 characters max.
3836 */
3837
3838 bytes += attr->num_values * 23;
3839 break;
3840
3841 case IPP_TAG_RESOLUTION :
3842 /*
3843 * A resolution is two signed integers separated by an "x" and
3844 * suffixed by the units, or 26 characters max.
3845 */
3846
3847 bytes += attr->num_values * 26;
3848 break;
3849
3850 case IPP_TAG_STRING :
3851 case IPP_TAG_TEXT :
3852 case IPP_TAG_NAME :
3853 case IPP_TAG_KEYWORD :
3854 case IPP_TAG_CHARSET :
3855 case IPP_TAG_LANGUAGE :
3856 case IPP_TAG_URI :
3857 /*
3858 * Strings can contain characters that need quoting. We need
3859 * at least 2 * len + 2 characters to cover the quotes and
3860 * any backslashes in the string.
3861 */
3862
3863 for (i = 0; i < attr->num_values; i ++)
3864 bytes += 2 * strlen(attr->values[i].string.text) + 2;
3865 break;
3866
3867 default :
3868 break; /* anti-compiler-warning-code */
3869 }
3870 }
3871
3872 return (bytes);
3873 }
3874
3875
3876 /*
3877 * 'load_job_cache()' - Load jobs from the job.cache file.
3878 */
3879
3880 static void
3881 load_job_cache(const char *filename) /* I - job.cache filename */
3882 {
3883 cups_file_t *fp; /* job.cache file */
3884 char line[1024], /* Line buffer */
3885 *value; /* Value on line */
3886 int linenum; /* Line number in file */
3887 cupsd_job_t *job; /* Current job */
3888 int jobid; /* Job ID */
3889 char jobfile[1024]; /* Job filename */
3890
3891
3892 /*
3893 * Open the job.cache file...
3894 */
3895
3896 if ((fp = cupsdOpenConfFile(filename)) == NULL)
3897 {
3898 load_request_root();
3899 return;
3900 }
3901
3902 /*
3903 * Read entries from the job cache file and create jobs as needed.
3904 */
3905
3906 cupsdLogMessage(CUPSD_LOG_INFO, "Loading job cache file \"%s\"...",
3907 filename);
3908
3909 linenum = 0;
3910 job = NULL;
3911
3912 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
3913 {
3914 if (!_cups_strcasecmp(line, "NextJobId"))
3915 {
3916 if (value)
3917 NextJobId = atoi(value);
3918 }
3919 else if (!_cups_strcasecmp(line, "<Job"))
3920 {
3921 if (job)
3922 {
3923 cupsdLogMessage(CUPSD_LOG_ERROR, "Missing </Job> directive on line %d.",
3924 linenum);
3925 continue;
3926 }
3927
3928 if (!value)
3929 {
3930 cupsdLogMessage(CUPSD_LOG_ERROR, "Missing job ID on line %d.", linenum);
3931 continue;
3932 }
3933
3934 jobid = atoi(value);
3935
3936 if (jobid < 1)
3937 {
3938 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad job ID %d on line %d.", jobid,
3939 linenum);
3940 continue;
3941 }
3942
3943 snprintf(jobfile, sizeof(jobfile), "%s/c%05d", RequestRoot, jobid);
3944 if (access(jobfile, 0))
3945 {
3946 snprintf(jobfile, sizeof(jobfile), "%s/c%05d.N", RequestRoot, jobid);
3947 if (access(jobfile, 0))
3948 {
3949 cupsdLogMessage(CUPSD_LOG_ERROR, "[Job %d] Files have gone away.",
3950 jobid);
3951 continue;
3952 }
3953 }
3954
3955 job = calloc(1, sizeof(cupsd_job_t));
3956 if (!job)
3957 {
3958 cupsdLogMessage(CUPSD_LOG_EMERG,
3959 "[Job %d] Unable to allocate memory for job.", jobid);
3960 break;
3961 }
3962
3963 job->id = jobid;
3964 job->back_pipes[0] = -1;
3965 job->back_pipes[1] = -1;
3966 job->print_pipes[0] = -1;
3967 job->print_pipes[1] = -1;
3968 job->side_pipes[0] = -1;
3969 job->side_pipes[1] = -1;
3970 job->status_pipes[0] = -1;
3971 job->status_pipes[1] = -1;
3972
3973 cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] Loading from cache...",
3974 job->id);
3975 }
3976 else if (!job)
3977 {
3978 cupsdLogMessage(CUPSD_LOG_ERROR,
3979 "Missing <Job #> directive on line %d.", linenum);
3980 continue;
3981 }
3982 else if (!_cups_strcasecmp(line, "</Job>"))
3983 {
3984 cupsArrayAdd(Jobs, job);
3985
3986 if (job->state_value <= IPP_JOB_STOPPED && cupsdLoadJob(job))
3987 cupsArrayAdd(ActiveJobs, job);
3988
3989 job = NULL;
3990 }
3991 else if (!value)
3992 {
3993 cupsdLogMessage(CUPSD_LOG_ERROR, "Missing value on line %d.", linenum);
3994 continue;
3995 }
3996 else if (!_cups_strcasecmp(line, "State"))
3997 {
3998 job->state_value = (ipp_jstate_t)atoi(value);
3999
4000 if (job->state_value < IPP_JOB_PENDING)
4001 job->state_value = IPP_JOB_PENDING;
4002 else if (job->state_value > IPP_JOB_COMPLETED)
4003 job->state_value = IPP_JOB_COMPLETED;
4004 }
4005 else if (!_cups_strcasecmp(line, "HoldUntil"))
4006 {
4007 job->hold_until = atoi(value);
4008 }
4009 else if (!_cups_strcasecmp(line, "Priority"))
4010 {
4011 job->priority = atoi(value);
4012 }
4013 else if (!_cups_strcasecmp(line, "Username"))
4014 {
4015 cupsdSetString(&job->username, value);
4016 }
4017 else if (!_cups_strcasecmp(line, "Destination"))
4018 {
4019 cupsdSetString(&job->dest, value);
4020 }
4021 else if (!_cups_strcasecmp(line, "DestType"))
4022 {
4023 job->dtype = (cups_ptype_t)atoi(value);
4024 }
4025 else if (!_cups_strcasecmp(line, "NumFiles"))
4026 {
4027 job->num_files = atoi(value);
4028
4029 if (job->num_files < 0)
4030 {
4031 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad NumFiles value %d on line %d.",
4032 job->num_files, linenum);
4033 job->num_files = 0;
4034 continue;
4035 }
4036
4037 if (job->num_files > 0)
4038 {
4039 snprintf(jobfile, sizeof(jobfile), "%s/d%05d-001", RequestRoot,
4040 job->id);
4041 if (access(jobfile, 0))
4042 {
4043 cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Data files have gone away.",
4044 job->id);
4045 job->num_files = 0;
4046 continue;
4047 }
4048
4049 job->filetypes = calloc(job->num_files, sizeof(mime_type_t *));
4050 job->compressions = calloc(job->num_files, sizeof(int));
4051
4052 if (!job->filetypes || !job->compressions)
4053 {
4054 cupsdLogMessage(CUPSD_LOG_EMERG,
4055 "[Job %d] Unable to allocate memory for %d files.",
4056 job->id, job->num_files);
4057 break;
4058 }
4059 }
4060 }
4061 else if (!_cups_strcasecmp(line, "File"))
4062 {
4063 int number, /* File number */
4064 compression; /* Compression value */
4065 char super[MIME_MAX_SUPER], /* MIME super type */
4066 type[MIME_MAX_TYPE]; /* MIME type */
4067
4068
4069 if (sscanf(value, "%d%*[ \t]%15[^/]/%255s%d", &number, super, type,
4070 &compression) != 4)
4071 {
4072 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad File on line %d.", linenum);
4073 continue;
4074 }
4075
4076 if (number < 1 || number > job->num_files)
4077 {
4078 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad File number %d on line %d.",
4079 number, linenum);
4080 continue;
4081 }
4082
4083 number --;
4084
4085 job->compressions[number] = compression;
4086 job->filetypes[number] = mimeType(MimeDatabase, super, type);
4087
4088 if (!job->filetypes[number])
4089 {
4090 /*
4091 * If the original MIME type is unknown, auto-type it!
4092 */
4093
4094 cupsdLogMessage(CUPSD_LOG_ERROR,
4095 "[Job %d] Unknown MIME type %s/%s for file %d.",
4096 job->id, super, type, number + 1);
4097
4098 snprintf(jobfile, sizeof(jobfile), "%s/d%05d-%03d", RequestRoot,
4099 job->id, number + 1);
4100 job->filetypes[number] = mimeFileType(MimeDatabase, jobfile, NULL,
4101 job->compressions + number);
4102
4103 /*
4104 * If that didn't work, assume it is raw...
4105 */
4106
4107 if (!job->filetypes[number])
4108 job->filetypes[number] = mimeType(MimeDatabase, "application",
4109 "vnd.cups-raw");
4110 }
4111 }
4112 else
4113 cupsdLogMessage(CUPSD_LOG_ERROR, "Unknown %s directive on line %d.",
4114 line, linenum);
4115 }
4116
4117 cupsFileClose(fp);
4118 }
4119
4120
4121 /*
4122 * 'load_next_job_id()' - Load the NextJobId value from the job.cache file.
4123 */
4124
4125 static void
4126 load_next_job_id(const char *filename) /* I - job.cache filename */
4127 {
4128 cups_file_t *fp; /* job.cache file */
4129 char line[1024], /* Line buffer */
4130 *value; /* Value on line */
4131 int linenum; /* Line number in file */
4132 int next_job_id; /* NextJobId value from line */
4133
4134
4135 /*
4136 * Read the NextJobId directive from the job.cache file and use
4137 * the value (if any).
4138 */
4139
4140 if ((fp = cupsFileOpen(filename, "r")) == NULL)
4141 {
4142 if (errno != ENOENT)
4143 cupsdLogMessage(CUPSD_LOG_ERROR,
4144 "Unable to open job cache file \"%s\": %s",
4145 filename, strerror(errno));
4146
4147 return;
4148 }
4149
4150 cupsdLogMessage(CUPSD_LOG_INFO,
4151 "Loading NextJobId from job cache file \"%s\"...", filename);
4152
4153 linenum = 0;
4154
4155 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
4156 {
4157 if (!_cups_strcasecmp(line, "NextJobId"))
4158 {
4159 if (value)
4160 {
4161 next_job_id = atoi(value);
4162
4163 if (next_job_id > NextJobId)
4164 NextJobId = next_job_id;
4165 }
4166 break;
4167 }
4168 }
4169
4170 cupsFileClose(fp);
4171 }
4172
4173
4174 /*
4175 * 'load_request_root()' - Load jobs from the RequestRoot directory.
4176 */
4177
4178 static void
4179 load_request_root(void)
4180 {
4181 cups_dir_t *dir; /* Directory */
4182 cups_dentry_t *dent; /* Directory entry */
4183 cupsd_job_t *job; /* New job */
4184
4185
4186 /*
4187 * Open the requests directory...
4188 */
4189
4190 cupsdLogMessage(CUPSD_LOG_DEBUG, "Scanning %s for jobs...", RequestRoot);
4191
4192 if ((dir = cupsDirOpen(RequestRoot)) == NULL)
4193 {
4194 cupsdLogMessage(CUPSD_LOG_ERROR,
4195 "Unable to open spool directory \"%s\": %s",
4196 RequestRoot, strerror(errno));
4197 return;
4198 }
4199
4200 /*
4201 * Read all the c##### files...
4202 */
4203
4204 while ((dent = cupsDirRead(dir)) != NULL)
4205 if (strlen(dent->filename) >= 6 && dent->filename[0] == 'c')
4206 {
4207 /*
4208 * Allocate memory for the job...
4209 */
4210
4211 if ((job = calloc(sizeof(cupsd_job_t), 1)) == NULL)
4212 {
4213 cupsdLogMessage(CUPSD_LOG_ERROR, "Ran out of memory for jobs.");
4214 cupsDirClose(dir);
4215 return;
4216 }
4217
4218 /*
4219 * Assign the job ID...
4220 */
4221
4222 job->id = atoi(dent->filename + 1);
4223 job->back_pipes[0] = -1;
4224 job->back_pipes[1] = -1;
4225 job->print_pipes[0] = -1;
4226 job->print_pipes[1] = -1;
4227 job->side_pipes[0] = -1;
4228 job->side_pipes[1] = -1;
4229 job->status_pipes[0] = -1;
4230 job->status_pipes[1] = -1;
4231
4232 if (job->id >= NextJobId)
4233 NextJobId = job->id + 1;
4234
4235 /*
4236 * Load the job...
4237 */
4238
4239 if (cupsdLoadJob(job))
4240 {
4241 /*
4242 * Insert the job into the array, sorting by job priority and ID...
4243 */
4244
4245 cupsArrayAdd(Jobs, job);
4246
4247 if (job->state_value <= IPP_JOB_STOPPED)
4248 cupsArrayAdd(ActiveJobs, job);
4249 else
4250 unload_job(job);
4251 }
4252 }
4253
4254 cupsDirClose(dir);
4255 }
4256
4257
4258 /*
4259 * 'remove_job_files()' - Remove the document files for a job.
4260 */
4261
4262 static void
4263 remove_job_files(cupsd_job_t *job) /* I - Job */
4264 {
4265 int i; /* Looping var */
4266 char filename[1024]; /* Document filename */
4267
4268
4269 if (job->num_files <= 0)
4270 return;
4271
4272 for (i = 1; i <= job->num_files; i ++)
4273 {
4274 snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot,
4275 job->id, i);
4276 if (Classification)
4277 cupsdRemoveFile(filename);
4278 else
4279 unlink(filename);
4280 }
4281
4282 free(job->filetypes);
4283 free(job->compressions);
4284
4285 job->file_time = 0;
4286 job->num_files = 0;
4287 job->filetypes = NULL;
4288 job->compressions = NULL;
4289
4290 LastEvent |= CUPSD_EVENT_PRINTER_STATE_CHANGED;
4291 }
4292
4293
4294 /*
4295 * 'remove_job_history()' - Remove the control file for a job.
4296 */
4297
4298 static void
4299 remove_job_history(cupsd_job_t *job) /* I - Job */
4300 {
4301 char filename[1024]; /* Control filename */
4302
4303
4304 /*
4305 * Remove the job info file...
4306 */
4307
4308 snprintf(filename, sizeof(filename), "%s/c%05d", RequestRoot,
4309 job->id);
4310 if (Classification)
4311 cupsdRemoveFile(filename);
4312 else
4313 unlink(filename);
4314
4315 LastEvent |= CUPSD_EVENT_PRINTER_STATE_CHANGED;
4316 }
4317
4318
4319 /*
4320 * 'set_time()' - Set one of the "time-at-xyz" attributes.
4321 */
4322
4323 static void
4324 set_time(cupsd_job_t *job, /* I - Job to update */
4325 const char *name) /* I - Name of attribute */
4326 {
4327 ipp_attribute_t *attr; /* Time attribute */
4328 time_t curtime; /* Current time */
4329
4330
4331 curtime = time(NULL);
4332
4333 cupsdLogJob(job, CUPSD_LOG_DEBUG, "%s=%ld", name, (long)curtime);
4334
4335 if ((attr = ippFindAttribute(job->attrs, name, IPP_TAG_ZERO)) != NULL)
4336 {
4337 attr->value_tag = IPP_TAG_INTEGER;
4338 attr->values[0].integer = curtime;
4339 }
4340
4341 if (!strcmp(name, "time-at-completed"))
4342 {
4343 if (JobHistory < INT_MAX)
4344 job->history_time = attr->values[0].integer + JobHistory;
4345 else
4346 job->history_time = INT_MAX;
4347
4348 if (job->history_time < JobHistoryUpdate || !JobHistoryUpdate)
4349 JobHistoryUpdate = job->history_time;
4350
4351 if (JobFiles < INT_MAX)
4352 job->file_time = attr->values[0].integer + JobFiles;
4353 else
4354 job->file_time = INT_MAX;
4355
4356 if (job->file_time < JobHistoryUpdate || !JobHistoryUpdate)
4357 JobHistoryUpdate = job->file_time;
4358
4359 cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_time: JobHistoryUpdate=%ld",
4360 (long)JobHistoryUpdate);
4361 }
4362 }
4363
4364
4365 /*
4366 * 'start_job()' - Start a print job.
4367 */
4368
4369 static void
4370 start_job(cupsd_job_t *job, /* I - Job ID */
4371 cupsd_printer_t *printer) /* I - Printer to print job */
4372 {
4373 cupsdLogMessage(CUPSD_LOG_DEBUG2, "start_job(job=%p(%d), printer=%p(%s))",
4374 job, job->id, printer, printer->name);
4375
4376 /*
4377 * Make sure we have some files around before we try to print...
4378 */
4379
4380 if (job->num_files == 0)
4381 {
4382 ippSetString(job->attrs, &job->reasons, 0, "aborted-by-system");
4383 cupsdSetJobState(job, IPP_JOB_ABORTED, CUPSD_JOB_DEFAULT,
4384 "Aborting job because it has no files.");
4385 return;
4386 }
4387
4388 /*
4389 * Update the printer and job state to "processing"...
4390 */
4391
4392 if (!cupsdLoadJob(job))
4393 return;
4394
4395 if (!job->printer_message)
4396 job->printer_message = ippFindAttribute(job->attrs,
4397 "job-printer-state-message",
4398 IPP_TAG_TEXT);
4399 if (job->printer_message)
4400 cupsdSetString(&(job->printer_message->values[0].string.text), "");
4401
4402 ippSetString(job->attrs, &job->reasons, 0, "job-printing");
4403 cupsdSetJobState(job, IPP_JOB_PROCESSING, CUPSD_JOB_DEFAULT, NULL);
4404 cupsdSetPrinterState(printer, IPP_PRINTER_PROCESSING, 0);
4405 cupsdSetPrinterReasons(printer, "-cups-remote-pending,"
4406 "cups-remote-pending-held,"
4407 "cups-remote-processing,"
4408 "cups-remote-stopped,"
4409 "cups-remote-canceled,"
4410 "cups-remote-aborted,"
4411 "cups-remote-completed");
4412
4413 job->cost = 0;
4414 job->current_file = 0;
4415 job->file_time = 0;
4416 job->history_time = 0;
4417 job->progress = 0;
4418 job->printer = printer;
4419 printer->job = job;
4420
4421 if (MaxJobTime > 0)
4422 job->cancel_time = time(NULL) + MaxJobTime;
4423 else
4424 job->cancel_time = 0;
4425
4426 /*
4427 * Setup the last exit status and security profiles...
4428 */
4429
4430 job->status = 0;
4431 job->profile = cupsdCreateProfile(job->id);
4432
4433 /*
4434 * Create the status pipes and buffer...
4435 */
4436
4437 if (cupsdOpenPipe(job->status_pipes))
4438 {
4439 cupsdLogJob(job, CUPSD_LOG_DEBUG,
4440 "Unable to create job status pipes - %s.", strerror(errno));
4441
4442 cupsdSetJobState(job, IPP_JOB_STOPPED, CUPSD_JOB_DEFAULT,
4443 "Job stopped because the scheduler could not create the "
4444 "job status pipes.");
4445
4446 cupsdDestroyProfile(job->profile);
4447 job->profile = NULL;
4448 return;
4449 }
4450
4451 job->status_buffer = cupsdStatBufNew(job->status_pipes[0], NULL);
4452 job->status_level = CUPSD_LOG_INFO;
4453
4454 /*
4455 * Create the backchannel pipes and make them non-blocking...
4456 */
4457
4458 if (cupsdOpenPipe(job->back_pipes))
4459 {
4460 cupsdLogJob(job, CUPSD_LOG_DEBUG,
4461 "Unable to create back-channel pipes - %s.", strerror(errno));
4462
4463 cupsdSetJobState(job, IPP_JOB_STOPPED, CUPSD_JOB_DEFAULT,
4464 "Job stopped because the scheduler could not create the "
4465 "back-channel pipes.");
4466
4467 cupsdClosePipe(job->status_pipes);
4468 cupsdStatBufDelete(job->status_buffer);
4469 job->status_buffer = NULL;
4470
4471 cupsdDestroyProfile(job->profile);
4472 job->profile = NULL;
4473 return;
4474 }
4475
4476 fcntl(job->back_pipes[0], F_SETFL,
4477 fcntl(job->back_pipes[0], F_GETFL) | O_NONBLOCK);
4478 fcntl(job->back_pipes[1], F_SETFL,
4479 fcntl(job->back_pipes[1], F_GETFL) | O_NONBLOCK);
4480
4481 /*
4482 * Create the side-channel pipes and make them non-blocking...
4483 */
4484
4485 if (socketpair(AF_LOCAL, SOCK_STREAM, 0, job->side_pipes))
4486 {
4487 cupsdLogJob(job, CUPSD_LOG_DEBUG,
4488 "Unable to create side-channel pipes - %s.", strerror(errno));
4489
4490 cupsdSetJobState(job, IPP_JOB_STOPPED, CUPSD_JOB_DEFAULT,
4491 "Job stopped because the scheduler could not create the "
4492 "side-channel pipes.");
4493
4494 cupsdClosePipe(job->back_pipes);
4495
4496 cupsdClosePipe(job->status_pipes);
4497 cupsdStatBufDelete(job->status_buffer);
4498 job->status_buffer = NULL;
4499
4500 cupsdDestroyProfile(job->profile);
4501 job->profile = NULL;
4502 return;
4503 }
4504
4505 fcntl(job->side_pipes[0], F_SETFL,
4506 fcntl(job->side_pipes[0], F_GETFL) | O_NONBLOCK);
4507 fcntl(job->side_pipes[1], F_SETFL,
4508 fcntl(job->side_pipes[1], F_GETFL) | O_NONBLOCK);
4509
4510 fcntl(job->side_pipes[0], F_SETFD,
4511 fcntl(job->side_pipes[0], F_GETFD) | FD_CLOEXEC);
4512 fcntl(job->side_pipes[1], F_SETFD,
4513 fcntl(job->side_pipes[1], F_GETFD) | FD_CLOEXEC);
4514
4515 /*
4516 * Now start the first file in the job...
4517 */
4518
4519 cupsdContinueJob(job);
4520 }
4521
4522
4523 /*
4524 * 'stop_job()' - Stop a print job.
4525 */
4526
4527 static void
4528 stop_job(cupsd_job_t *job, /* I - Job */
4529 cupsd_jobaction_t action) /* I - Action */
4530 {
4531 int i; /* Looping var */
4532
4533
4534 cupsdLogMessage(CUPSD_LOG_DEBUG2, "stop_job(job=%p(%d), action=%d)", job,
4535 job->id, action);
4536
4537 FilterLevel -= job->cost;
4538 job->cost = 0;
4539
4540 if (action == CUPSD_JOB_DEFAULT && !job->kill_time)
4541 job->kill_time = time(NULL) + JobKillDelay;
4542 else if (action >= CUPSD_JOB_FORCE)
4543 job->kill_time = 0;
4544
4545 for (i = 0; job->filters[i]; i ++)
4546 if (job->filters[i] > 0)
4547 {
4548 cupsdEndProcess(job->filters[i], action >= CUPSD_JOB_FORCE);
4549
4550 if (action >= CUPSD_JOB_FORCE)
4551 job->filters[i] = -job->filters[i];
4552 }
4553
4554 if (job->backend > 0)
4555 {
4556 cupsdEndProcess(job->backend, action >= CUPSD_JOB_FORCE);
4557
4558 if (action >= CUPSD_JOB_FORCE)
4559 job->backend = -job->backend;
4560 }
4561
4562 if (action >= CUPSD_JOB_FORCE)
4563 {
4564 /*
4565 * Clear job status...
4566 */
4567
4568 job->status = 0;
4569 }
4570 }
4571
4572
4573 /*
4574 * 'unload_job()' - Unload a job from memory.
4575 */
4576
4577 static void
4578 unload_job(cupsd_job_t *job) /* I - Job */
4579 {
4580 if (!job->attrs)
4581 return;
4582
4583 cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] Unloading...", job->id);
4584
4585 ippDelete(job->attrs);
4586
4587 job->attrs = NULL;
4588 job->state = NULL;
4589 job->reasons = NULL;
4590 job->sheets = NULL;
4591 job->job_sheets = NULL;
4592 job->printer_message = NULL;
4593 job->printer_reasons = NULL;
4594 }
4595
4596
4597 /*
4598 * 'update_job()' - Read a status update from a job's filters.
4599 */
4600
4601 void
4602 update_job(cupsd_job_t *job) /* I - Job to check */
4603 {
4604 int i; /* Looping var */
4605 int copies; /* Number of copies printed */
4606 char message[CUPSD_SB_BUFFER_SIZE],
4607 /* Message text */
4608 *ptr; /* Pointer update... */
4609 int loglevel, /* Log level for message */
4610 event = 0; /* Events? */
4611 static const char * const levels[] = /* Log levels */
4612 {
4613 "NONE",
4614 "EMERG",
4615 "ALERT",
4616 "CRIT",
4617 "ERROR",
4618 "WARN",
4619 "NOTICE",
4620 "INFO",
4621 "DEBUG",
4622 "DEBUG2"
4623 };
4624
4625
4626 /*
4627 * Get the printer associated with this job; if the printer is stopped for
4628 * any reason then job->printer will be reset to NULL, so make sure we have
4629 * a valid pointer...
4630 */
4631
4632 while ((ptr = cupsdStatBufUpdate(job->status_buffer, &loglevel,
4633 message, sizeof(message))) != NULL)
4634 {
4635 /*
4636 * Process page and printer state messages as needed...
4637 */
4638
4639 if (loglevel == CUPSD_LOG_PAGE)
4640 {
4641 /*
4642 * Page message; send the message to the page_log file and update the
4643 * job sheet count...
4644 */
4645
4646 cupsdLogJob(job, CUPSD_LOG_DEBUG, "PAGE: %s", message);
4647
4648 if (job->sheets)
4649 {
4650 if (!_cups_strncasecmp(message, "total ", 6))
4651 {
4652 /*
4653 * Got a total count of pages from a backend or filter...
4654 */
4655
4656 copies = atoi(message + 6);
4657 copies -= job->sheets->values[0].integer; /* Just track the delta */
4658 }
4659 else if (!sscanf(message, "%*d%d", &copies))
4660 copies = 1;
4661
4662 job->sheets->values[0].integer += copies;
4663
4664 if (job->printer->page_limit)
4665 cupsdUpdateQuota(job->printer, job->username, copies, 0);
4666 }
4667
4668 cupsdLogPage(job, message);
4669
4670 if (job->sheets)
4671 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS, job->printer, job,
4672 "Printed %d page(s).", job->sheets->values[0].integer);
4673 }
4674 else if (loglevel == CUPSD_LOG_STATE)
4675 {
4676 cupsdLogJob(job, CUPSD_LOG_DEBUG, "STATE: %s", message);
4677
4678 if (!strcmp(message, "paused"))
4679 {
4680 cupsdStopPrinter(job->printer, 1);
4681 return;
4682 }
4683 else if (cupsdSetPrinterReasons(job->printer, message))
4684 {
4685 event |= CUPSD_EVENT_PRINTER_STATE;
4686
4687 if (MaxJobTime > 0 && strstr(message, "connecting-to-device") != NULL)
4688 {
4689 /*
4690 * Reset cancel time after connecting to the device...
4691 */
4692
4693 for (i = 0; i < job->printer->num_reasons; i ++)
4694 if (!strcmp(job->printer->reasons[i], "connecting-to-device"))
4695 break;
4696
4697 if (i >= job->printer->num_reasons)
4698 job->cancel_time = time(NULL) + MaxJobTime;
4699 }
4700 }
4701
4702 update_job_attrs(job, 0);
4703 }
4704 else if (loglevel == CUPSD_LOG_ATTR)
4705 {
4706 /*
4707 * Set attribute(s)...
4708 */
4709
4710 int num_attrs; /* Number of attributes */
4711 cups_option_t *attrs; /* Attributes */
4712 const char *attr; /* Attribute */
4713
4714
4715 cupsdLogJob(job, CUPSD_LOG_DEBUG, "ATTR: %s", message);
4716
4717 num_attrs = cupsParseOptions(message, 0, &attrs);
4718
4719 if ((attr = cupsGetOption("auth-info-required", num_attrs,
4720 attrs)) != NULL)
4721 {
4722 cupsdSetAuthInfoRequired(job->printer, attr, NULL);
4723 cupsdSetPrinterAttrs(job->printer);
4724
4725 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
4726 }
4727
4728 if ((attr = cupsGetOption("job-media-progress", num_attrs,
4729 attrs)) != NULL)
4730 {
4731 int progress = atoi(attr);
4732
4733
4734 if (progress >= 0 && progress <= 100)
4735 {
4736 job->progress = progress;
4737
4738 if (job->sheets)
4739 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS, job->printer, job,
4740 "Printing page %d, %d%%",
4741 job->sheets->values[0].integer, job->progress);
4742 }
4743 }
4744
4745 if ((attr = cupsGetOption("printer-alert", num_attrs, attrs)) != NULL)
4746 {
4747 cupsdSetString(&job->printer->alert, attr);
4748 event |= CUPSD_EVENT_PRINTER_STATE;
4749 }
4750
4751 if ((attr = cupsGetOption("printer-alert-description", num_attrs,
4752 attrs)) != NULL)
4753 {
4754 cupsdSetString(&job->printer->alert_description, attr);
4755 event |= CUPSD_EVENT_PRINTER_STATE;
4756 }
4757
4758 if ((attr = cupsGetOption("marker-colors", num_attrs, attrs)) != NULL)
4759 {
4760 cupsdSetPrinterAttr(job->printer, "marker-colors", (char *)attr);
4761 job->printer->marker_time = time(NULL);
4762 event |= CUPSD_EVENT_PRINTER_STATE;
4763 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
4764 }
4765
4766 if ((attr = cupsGetOption("marker-levels", num_attrs, attrs)) != NULL)
4767 {
4768 cupsdSetPrinterAttr(job->printer, "marker-levels", (char *)attr);
4769 job->printer->marker_time = time(NULL);
4770 event |= CUPSD_EVENT_PRINTER_STATE;
4771 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
4772 }
4773
4774 if ((attr = cupsGetOption("marker-low-levels", num_attrs, attrs)) != NULL)
4775 {
4776 cupsdSetPrinterAttr(job->printer, "marker-low-levels", (char *)attr);
4777 job->printer->marker_time = time(NULL);
4778 event |= CUPSD_EVENT_PRINTER_STATE;
4779 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
4780 }
4781
4782 if ((attr = cupsGetOption("marker-high-levels", num_attrs, attrs)) != NULL)
4783 {
4784 cupsdSetPrinterAttr(job->printer, "marker-high-levels", (char *)attr);
4785 job->printer->marker_time = time(NULL);
4786 event |= CUPSD_EVENT_PRINTER_STATE;
4787 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
4788 }
4789
4790 if ((attr = cupsGetOption("marker-message", num_attrs, attrs)) != NULL)
4791 {
4792 cupsdSetPrinterAttr(job->printer, "marker-message", (char *)attr);
4793 job->printer->marker_time = time(NULL);
4794 event |= CUPSD_EVENT_PRINTER_STATE;
4795 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
4796 }
4797
4798 if ((attr = cupsGetOption("marker-names", num_attrs, attrs)) != NULL)
4799 {
4800 cupsdSetPrinterAttr(job->printer, "marker-names", (char *)attr);
4801 job->printer->marker_time = time(NULL);
4802 event |= CUPSD_EVENT_PRINTER_STATE;
4803 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
4804 }
4805
4806 if ((attr = cupsGetOption("marker-types", num_attrs, attrs)) != NULL)
4807 {
4808 cupsdSetPrinterAttr(job->printer, "marker-types", (char *)attr);
4809 job->printer->marker_time = time(NULL);
4810 event |= CUPSD_EVENT_PRINTER_STATE;
4811 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
4812 }
4813
4814 cupsFreeOptions(num_attrs, attrs);
4815 }
4816 else if (loglevel == CUPSD_LOG_PPD)
4817 {
4818 /*
4819 * Set attribute(s)...
4820 */
4821
4822 int num_keywords; /* Number of keywords */
4823 cups_option_t *keywords; /* Keywords */
4824
4825
4826 cupsdLogJob(job, CUPSD_LOG_DEBUG, "PPD: %s", message);
4827
4828 num_keywords = cupsParseOptions(message, 0, &keywords);
4829
4830 if (cupsdUpdatePrinterPPD(job->printer, num_keywords, keywords))
4831 cupsdSetPrinterAttrs(job->printer);
4832
4833 cupsFreeOptions(num_keywords, keywords);
4834 }
4835 else
4836 {
4837 /*
4838 * Strip legacy message prefix...
4839 */
4840
4841 if (!strncmp(message, "recoverable:", 12))
4842 {
4843 ptr = message + 12;
4844 while (isspace(*ptr & 255))
4845 ptr ++;
4846 }
4847 else if (!strncmp(message, "recovered:", 10))
4848 {
4849 ptr = message + 10;
4850 while (isspace(*ptr & 255))
4851 ptr ++;
4852 }
4853 else
4854 ptr = message;
4855
4856 if (*ptr)
4857 cupsdLogJob(job, loglevel, "%s", ptr);
4858
4859 if (loglevel < CUPSD_LOG_DEBUG &&
4860 strcmp(job->printer->state_message, ptr))
4861 {
4862 strlcpy(job->printer->state_message, ptr,
4863 sizeof(job->printer->state_message));
4864
4865 event |= CUPSD_EVENT_PRINTER_STATE | CUPSD_EVENT_JOB_PROGRESS;
4866
4867 if (loglevel <= job->status_level && job->status_level > CUPSD_LOG_ERROR)
4868 {
4869 /*
4870 * Some messages show in the job-printer-state-message attribute...
4871 */
4872
4873 if (loglevel != CUPSD_LOG_NOTICE)
4874 job->status_level = loglevel;
4875
4876 update_job_attrs(job, 1);
4877
4878 cupsdLogJob(job, CUPSD_LOG_DEBUG,
4879 "Set job-printer-state-message to \"%s\", "
4880 "current level=%s",
4881 job->printer_message->values[0].string.text,
4882 levels[job->status_level]);
4883 }
4884 }
4885 }
4886
4887 if (!strchr(job->status_buffer->buffer, '\n'))
4888 break;
4889 }
4890
4891 if (event & CUPSD_EVENT_JOB_PROGRESS)
4892 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS, job->printer, job,
4893 "%s", job->printer->state_message);
4894 if (event & CUPSD_EVENT_PRINTER_STATE)
4895 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, job->printer, NULL,
4896 (job->printer->type & CUPS_PRINTER_CLASS) ?
4897 "Class \"%s\" state changed." :
4898 "Printer \"%s\" state changed.",
4899 job->printer->name);
4900
4901
4902 if (ptr == NULL && !job->status_buffer->bufused)
4903 {
4904 /*
4905 * See if all of the filters and the backend have returned their
4906 * exit statuses.
4907 */
4908
4909 for (i = 0; job->filters[i] < 0; i ++);
4910
4911 if (job->filters[i])
4912 {
4913 /*
4914 * EOF but we haven't collected the exit status of all filters...
4915 */
4916
4917 cupsdCheckProcess();
4918 return;
4919 }
4920
4921 if (job->current_file >= job->num_files && job->backend > 0)
4922 {
4923 /*
4924 * EOF but we haven't collected the exit status of the backend...
4925 */
4926
4927 cupsdCheckProcess();
4928 return;
4929 }
4930
4931 /*
4932 * Handle the end of job stuff...
4933 */
4934
4935 finalize_job(job, 1);
4936
4937 /*
4938 * Check for new jobs...
4939 */
4940
4941 cupsdCheckJobs();
4942 }
4943 }
4944
4945
4946 /*
4947 * 'update_job_attrs()' - Update the job-printer-* attributes.
4948 */
4949
4950 void
4951 update_job_attrs(cupsd_job_t *job, /* I - Job to update */
4952 int do_message)/* I - 1 = copy job-printer-state message */
4953 {
4954 int i; /* Looping var */
4955 int num_reasons; /* Actual number of reasons */
4956 const char * const *reasons; /* Reasons */
4957 static const char *none = "none"; /* "none" reason */
4958
4959
4960 /*
4961 * Get/create the job-printer-state-* attributes...
4962 */
4963
4964 if (!job->printer_message)
4965 {
4966 if ((job->printer_message = ippFindAttribute(job->attrs,
4967 "job-printer-state-message",
4968 IPP_TAG_TEXT)) == NULL)
4969 job->printer_message = ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_TEXT,
4970 "job-printer-state-message",
4971 NULL, "");
4972 }
4973
4974 if (!job->printer_reasons)
4975 job->printer_reasons = ippFindAttribute(job->attrs,
4976 "job-printer-state-reasons",
4977 IPP_TAG_KEYWORD);
4978
4979 /*
4980 * Copy or clear the printer-state-message value as needed...
4981 */
4982
4983 if (job->state_value != IPP_JOB_PROCESSING &&
4984 job->status_level == CUPSD_LOG_INFO)
4985 {
4986 cupsdSetString(&(job->printer_message->values[0].string.text), "");
4987
4988 job->dirty = 1;
4989 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
4990 }
4991 else if (job->printer->state_message[0] && do_message)
4992 {
4993 cupsdSetString(&(job->printer_message->values[0].string.text),
4994 job->printer->state_message);
4995
4996 job->dirty = 1;
4997 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
4998 }
4999
5000 /*
5001 * ... and the printer-state-reasons value...
5002 */
5003
5004 if (job->printer->num_reasons == 0)
5005 {
5006 num_reasons = 1;
5007 reasons = &none;
5008 }
5009 else
5010 {
5011 num_reasons = job->printer->num_reasons;
5012 reasons = (const char * const *)job->printer->reasons;
5013 }
5014
5015 if (!job->printer_reasons || job->printer_reasons->num_values != num_reasons)
5016 {
5017 /*
5018 * Replace/create a job-printer-state-reasons attribute...
5019 */
5020
5021 ippDeleteAttribute(job->attrs, job->printer_reasons);
5022
5023 job->printer_reasons = ippAddStrings(job->attrs,
5024 IPP_TAG_JOB, IPP_TAG_KEYWORD,
5025 "job-printer-state-reasons",
5026 num_reasons, NULL, NULL);
5027 }
5028 else
5029 {
5030 /*
5031 * Don't bother clearing the reason strings if they are the same...
5032 */
5033
5034 for (i = 0; i < num_reasons; i ++)
5035 if (strcmp(job->printer_reasons->values[i].string.text, reasons[i]))
5036 break;
5037
5038 if (i >= num_reasons)
5039 return;
5040
5041 /*
5042 * Not the same, so free the current strings...
5043 */
5044
5045 for (i = 0; i < num_reasons; i ++)
5046 _cupsStrFree(job->printer_reasons->values[i].string.text);
5047 }
5048
5049 /*
5050 * Copy the reasons...
5051 */
5052
5053 for (i = 0; i < num_reasons; i ++)
5054 job->printer_reasons->values[i].string.text = _cupsStrAlloc(reasons[i]);
5055
5056 job->dirty = 1;
5057 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
5058 }
5059
5060
5061 /*
5062 * End of "$Id: job.c 7902 2008-09-03 14:20:17Z mike $".
5063 */