]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/job.c
53968b897b034f55637eef7f4eb3c34d6be5aa60
[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", line);
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 * Abort the job...
3193 */
3194
3195 if (job_state == IPP_JOB_COMPLETED)
3196 {
3197 job_state = IPP_JOB_ABORTED;
3198 message = "Job aborted due to backend errors; please consult "
3199 "the error_log file for details.";
3200
3201 ippSetString(job->attrs, &job->reasons, 0, "aborted-by-system");
3202 }
3203 break;
3204
3205 case CUPS_BACKEND_HOLD :
3206 if (job_state == IPP_JOB_COMPLETED)
3207 {
3208 /*
3209 * Hold the job...
3210 */
3211
3212 cupsdSetJobHoldUntil(job, "indefinite", 1);
3213 ippSetString(job->attrs, &job->reasons, 0,
3214 "job-hold-until-specified");
3215
3216 job_state = IPP_JOB_HELD;
3217 message = "Job held indefinitely due to backend errors; please "
3218 "consult the error_log file for details.";
3219 }
3220 break;
3221
3222 case CUPS_BACKEND_STOP :
3223 /*
3224 * Stop the printer...
3225 */
3226
3227 printer_state = IPP_PRINTER_STOPPED;
3228 message = "Printer stopped due to backend errors; please "
3229 "consult the error_log file for details.";
3230
3231 if (job_state == IPP_JOB_COMPLETED)
3232 {
3233 job_state = IPP_JOB_PENDING;
3234
3235 ippSetString(job->attrs, &job->reasons, 0,
3236 "resources-are-not-ready");
3237 }
3238 break;
3239
3240 case CUPS_BACKEND_AUTH_REQUIRED :
3241 /*
3242 * Hold the job for authentication...
3243 */
3244
3245 if (job_state == IPP_JOB_COMPLETED)
3246 {
3247 cupsdSetJobHoldUntil(job, "auth-info-required", 1);
3248
3249 job_state = IPP_JOB_HELD;
3250 message = "Job held for authentication.";
3251
3252 ippSetString(job->attrs, &job->reasons, 0,
3253 "cups-held-for-authentication");
3254 }
3255 break;
3256
3257 case CUPS_BACKEND_RETRY :
3258 if (job_state == IPP_JOB_COMPLETED)
3259 {
3260 /*
3261 * Hold the job if the number of retries is less than the
3262 * JobRetryLimit, otherwise abort the job.
3263 */
3264
3265 job->tries ++;
3266
3267 if (job->tries > JobRetryLimit && JobRetryLimit > 0)
3268 {
3269 /*
3270 * Too many tries...
3271 */
3272
3273 snprintf(buffer, sizeof(buffer),
3274 "Job aborted after %d unsuccessful attempts.",
3275 JobRetryLimit);
3276 job_state = IPP_JOB_ABORTED;
3277 message = buffer;
3278
3279 ippSetString(job->attrs, &job->reasons, 0, "aborted-by-system");
3280 }
3281 else
3282 {
3283 /*
3284 * Try again in N seconds...
3285 */
3286
3287 snprintf(buffer, sizeof(buffer),
3288 "Job held for %d seconds since it could not be sent.",
3289 JobRetryInterval);
3290
3291 job->hold_until = time(NULL) + JobRetryInterval;
3292 job_state = IPP_JOB_HELD;
3293 message = buffer;
3294
3295 ippSetString(job->attrs, &job->reasons, 0,
3296 "resources-are-not-ready");
3297 }
3298 }
3299 break;
3300
3301 case CUPS_BACKEND_RETRY_CURRENT :
3302 /*
3303 * Mark the job as pending and retry on the same printer...
3304 */
3305
3306 if (job_state == IPP_JOB_COMPLETED)
3307 {
3308 job_state = IPP_JOB_PENDING;
3309 message = "Retrying job on same printer.";
3310
3311 ippSetString(job->attrs, &job->reasons, 0, "none");
3312 }
3313 break;
3314 }
3315 }
3316 else if (job->status > 0)
3317 {
3318 /*
3319 * Filter had errors; stop job...
3320 */
3321
3322 if (job_state == IPP_JOB_COMPLETED)
3323 {
3324 job_state = IPP_JOB_STOPPED;
3325 message = "Job stopped due to filter errors; please consult the "
3326 "error_log file for details.";
3327
3328 if (WIFSIGNALED(job->status))
3329 ippSetString(job->attrs, &job->reasons, 0, "cups-filter-crashed");
3330 else
3331 ippSetString(job->attrs, &job->reasons, 0, "job-completed-with-errors");
3332 }
3333 }
3334
3335 /*
3336 * Update the printer and job state.
3337 */
3338
3339 if (set_job_state && job_state != job->state_value)
3340 cupsdSetJobState(job, job_state, CUPSD_JOB_DEFAULT, "%s", message);
3341
3342 cupsdSetPrinterState(job->printer, printer_state,
3343 printer_state == IPP_PRINTER_STOPPED);
3344 update_job_attrs(job, 0);
3345
3346 if (job->history)
3347 {
3348 if (job->status &&
3349 (job->state_value == IPP_JOB_ABORTED ||
3350 job->state_value == IPP_JOB_STOPPED))
3351 dump_job_history(job);
3352 else
3353 free_job_history(job);
3354 }
3355
3356 cupsArrayRemove(PrintingJobs, job);
3357
3358 /*
3359 * Clear the printer <-> job association...
3360 */
3361
3362 job->printer->job = NULL;
3363 job->printer = NULL;
3364
3365 /*
3366 * Try printing another job...
3367 */
3368
3369 if (printer_state != IPP_PRINTER_STOPPED)
3370 cupsdCheckJobs();
3371 }
3372
3373
3374 /*
3375 * 'get_options()' - Get a string containing the job options.
3376 */
3377
3378 static char * /* O - Options string */
3379 get_options(cupsd_job_t *job, /* I - Job */
3380 int banner_page, /* I - Printing a banner page? */
3381 char *copies, /* I - Copies buffer */
3382 size_t copies_size, /* I - Size of copies buffer */
3383 char *title, /* I - Title buffer */
3384 size_t title_size) /* I - Size of title buffer */
3385 {
3386 int i; /* Looping var */
3387 size_t newlength; /* New option buffer length */
3388 char *optptr, /* Pointer to options */
3389 *valptr; /* Pointer in value string */
3390 ipp_attribute_t *attr; /* Current attribute */
3391 _ppd_cache_t *pc; /* PPD cache and mapping data */
3392 int num_pwgppds; /* Number of PWG->PPD options */
3393 cups_option_t *pwgppds, /* PWG->PPD options */
3394 *pwgppd, /* Current PWG->PPD option */
3395 *preset; /* Current preset option */
3396 int print_color_mode,
3397 /* Output mode (if any) */
3398 print_quality; /* Print quality (if any) */
3399 const char *ppd; /* PPD option choice */
3400 int exact; /* Did we get an exact match? */
3401 static char *options = NULL;/* Full list of options */
3402 static size_t optlength = 0; /* Length of option buffer */
3403
3404
3405 /*
3406 * Building the options string is harder than it needs to be, but for the
3407 * moment we need to pass strings for command-line args and not IPP attribute
3408 * pointers... :)
3409 *
3410 * First build an options array for any PWG->PPD mapped option/choice pairs.
3411 */
3412
3413 pc = job->printer->pc;
3414 num_pwgppds = 0;
3415 pwgppds = NULL;
3416
3417 if (pc &&
3418 !ippFindAttribute(job->attrs,
3419 "com.apple.print.DocumentTicket.PMSpoolFormat",
3420 IPP_TAG_ZERO) &&
3421 !ippFindAttribute(job->attrs, "APPrinterPreset", IPP_TAG_ZERO) &&
3422 (ippFindAttribute(job->attrs, "output-mode", IPP_TAG_ZERO) ||
3423 ippFindAttribute(job->attrs, "print-color-mode", IPP_TAG_ZERO) ||
3424 ippFindAttribute(job->attrs, "print-quality", IPP_TAG_ZERO)))
3425 {
3426 /*
3427 * Map output-mode and print-quality to a preset...
3428 */
3429
3430 if ((attr = ippFindAttribute(job->attrs, "print-color-mode",
3431 IPP_TAG_KEYWORD)) == NULL)
3432 attr = ippFindAttribute(job->attrs, "output-mode", IPP_TAG_KEYWORD);
3433
3434 if (attr && !strcmp(attr->values[0].string.text, "monochrome"))
3435 print_color_mode = _PWG_PRINT_COLOR_MODE_MONOCHROME;
3436 else
3437 print_color_mode = _PWG_PRINT_COLOR_MODE_COLOR;
3438
3439 if ((attr = ippFindAttribute(job->attrs, "print-quality",
3440 IPP_TAG_ENUM)) != NULL &&
3441 attr->values[0].integer >= IPP_QUALITY_DRAFT &&
3442 attr->values[0].integer <= IPP_QUALITY_HIGH)
3443 print_quality = attr->values[0].integer - IPP_QUALITY_DRAFT;
3444 else
3445 print_quality = _PWG_PRINT_QUALITY_NORMAL;
3446
3447 if (pc->num_presets[print_color_mode][print_quality] == 0)
3448 {
3449 /*
3450 * Try to find a preset that works so that we maximize the chances of us
3451 * getting a good print using IPP attributes.
3452 */
3453
3454 if (pc->num_presets[print_color_mode][_PWG_PRINT_QUALITY_NORMAL] > 0)
3455 print_quality = _PWG_PRINT_QUALITY_NORMAL;
3456 else if (pc->num_presets[_PWG_PRINT_COLOR_MODE_COLOR][print_quality] > 0)
3457 print_color_mode = _PWG_PRINT_COLOR_MODE_COLOR;
3458 else
3459 {
3460 print_quality = _PWG_PRINT_QUALITY_NORMAL;
3461 print_color_mode = _PWG_PRINT_COLOR_MODE_COLOR;
3462 }
3463 }
3464
3465 if (pc->num_presets[print_color_mode][print_quality] > 0)
3466 {
3467 /*
3468 * Copy the preset options as long as the corresponding names are not
3469 * already defined in the IPP request...
3470 */
3471
3472 for (i = pc->num_presets[print_color_mode][print_quality],
3473 preset = pc->presets[print_color_mode][print_quality];
3474 i > 0;
3475 i --, preset ++)
3476 {
3477 if (!ippFindAttribute(job->attrs, preset->name, IPP_TAG_ZERO))
3478 num_pwgppds = cupsAddOption(preset->name, preset->value, num_pwgppds,
3479 &pwgppds);
3480 }
3481 }
3482 }
3483
3484 if (pc)
3485 {
3486 if (!ippFindAttribute(job->attrs, "InputSlot", IPP_TAG_ZERO) &&
3487 !ippFindAttribute(job->attrs, "HPPaperSource", IPP_TAG_ZERO))
3488 {
3489 if ((ppd = _ppdCacheGetInputSlot(pc, job->attrs, NULL)) != NULL)
3490 num_pwgppds = cupsAddOption(pc->source_option, ppd, num_pwgppds,
3491 &pwgppds);
3492 }
3493 if (!ippFindAttribute(job->attrs, "MediaType", IPP_TAG_ZERO) &&
3494 (ppd = _ppdCacheGetMediaType(pc, job->attrs, NULL)) != NULL)
3495 num_pwgppds = cupsAddOption("MediaType", ppd, num_pwgppds, &pwgppds);
3496
3497 if (!ippFindAttribute(job->attrs, "PageRegion", IPP_TAG_ZERO) &&
3498 !ippFindAttribute(job->attrs, "PageSize", IPP_TAG_ZERO) &&
3499 (ppd = _ppdCacheGetPageSize(pc, job->attrs, NULL, &exact)) != NULL)
3500 {
3501 num_pwgppds = cupsAddOption("PageSize", ppd, num_pwgppds, &pwgppds);
3502
3503 if (!ippFindAttribute(job->attrs, "media", IPP_TAG_ZERO))
3504 num_pwgppds = cupsAddOption("media", ppd, num_pwgppds, &pwgppds);
3505 }
3506
3507 if (!ippFindAttribute(job->attrs, "OutputBin", IPP_TAG_ZERO) &&
3508 (attr = ippFindAttribute(job->attrs, "output-bin",
3509 IPP_TAG_ZERO)) != NULL &&
3510 (attr->value_tag == IPP_TAG_KEYWORD ||
3511 attr->value_tag == IPP_TAG_NAME) &&
3512 (ppd = _ppdCacheGetOutputBin(pc, attr->values[0].string.text)) != NULL)
3513 {
3514 /*
3515 * Map output-bin to OutputBin option...
3516 */
3517
3518 num_pwgppds = cupsAddOption("OutputBin", ppd, num_pwgppds, &pwgppds);
3519 }
3520
3521 if (pc->sides_option &&
3522 !ippFindAttribute(job->attrs, pc->sides_option, IPP_TAG_ZERO) &&
3523 (attr = ippFindAttribute(job->attrs, "sides", IPP_TAG_KEYWORD)) != NULL)
3524 {
3525 /*
3526 * Map sides to duplex option...
3527 */
3528
3529 if (!strcmp(attr->values[0].string.text, "one-sided"))
3530 num_pwgppds = cupsAddOption(pc->sides_option, pc->sides_1sided,
3531 num_pwgppds, &pwgppds);
3532 else if (!strcmp(attr->values[0].string.text, "two-sided-long-edge"))
3533 num_pwgppds = cupsAddOption(pc->sides_option, pc->sides_2sided_long,
3534 num_pwgppds, &pwgppds);
3535 else if (!strcmp(attr->values[0].string.text, "two-sided-short-edge"))
3536 num_pwgppds = cupsAddOption(pc->sides_option, pc->sides_2sided_short,
3537 num_pwgppds, &pwgppds);
3538 }
3539
3540 /*
3541 * Map finishings values...
3542 */
3543
3544 num_pwgppds = _ppdCacheGetFinishingOptions(pc, job->attrs,
3545 IPP_FINISHINGS_NONE, num_pwgppds,
3546 &pwgppds);
3547 }
3548
3549 /*
3550 * Figure out how much room we need...
3551 */
3552
3553 newlength = ipp_length(job->attrs);
3554
3555 for (i = num_pwgppds, pwgppd = pwgppds; i > 0; i --, pwgppd ++)
3556 newlength += 1 + strlen(pwgppd->name) + 1 + strlen(pwgppd->value);
3557
3558 /*
3559 * Then allocate/reallocate the option buffer as needed...
3560 */
3561
3562 if (newlength == 0) /* This can never happen, but Clang */
3563 newlength = 1; /* thinks it can... */
3564
3565 if (newlength > optlength || !options)
3566 {
3567 if (!options)
3568 optptr = malloc(newlength);
3569 else
3570 optptr = realloc(options, newlength);
3571
3572 if (!optptr)
3573 {
3574 cupsdLogJob(job, CUPSD_LOG_CRIT,
3575 "Unable to allocate " CUPS_LLFMT " bytes for option buffer.",
3576 CUPS_LLCAST newlength);
3577 return (NULL);
3578 }
3579
3580 options = optptr;
3581 optlength = newlength;
3582 }
3583
3584 /*
3585 * Now loop through the attributes and convert them to the textual
3586 * representation used by the filters...
3587 */
3588
3589 optptr = options;
3590 *optptr = '\0';
3591
3592 snprintf(title, title_size, "%s-%d", job->printer->name, job->id);
3593 strlcpy(copies, "1", copies_size);
3594
3595 for (attr = job->attrs->attrs; attr != NULL; attr = attr->next)
3596 {
3597 if (!strcmp(attr->name, "copies") &&
3598 attr->value_tag == IPP_TAG_INTEGER)
3599 {
3600 /*
3601 * Don't use the # copies attribute if we are printing the job sheets...
3602 */
3603
3604 if (!banner_page)
3605 snprintf(copies, copies_size, "%d", attr->values[0].integer);
3606 }
3607 else if (!strcmp(attr->name, "job-name") &&
3608 (attr->value_tag == IPP_TAG_NAME ||
3609 attr->value_tag == IPP_TAG_NAMELANG))
3610 strlcpy(title, attr->values[0].string.text, title_size);
3611 else if (attr->group_tag == IPP_TAG_JOB)
3612 {
3613 /*
3614 * Filter out other unwanted attributes...
3615 */
3616
3617 if (attr->value_tag == IPP_TAG_NOVALUE ||
3618 attr->value_tag == IPP_TAG_MIMETYPE ||
3619 attr->value_tag == IPP_TAG_NAMELANG ||
3620 attr->value_tag == IPP_TAG_TEXTLANG ||
3621 (attr->value_tag == IPP_TAG_URI && strcmp(attr->name, "job-uuid")) ||
3622 attr->value_tag == IPP_TAG_URISCHEME ||
3623 attr->value_tag == IPP_TAG_BEGIN_COLLECTION) /* Not yet supported */
3624 continue;
3625
3626 if (!strcmp(attr->name, "job-hold-until"))
3627 continue;
3628
3629 if (!strncmp(attr->name, "job-", 4) &&
3630 strcmp(attr->name, "job-billing") &&
3631 strcmp(attr->name, "job-impressions") &&
3632 strcmp(attr->name, "job-originating-host-name") &&
3633 strcmp(attr->name, "job-uuid") &&
3634 !(job->printer->type & CUPS_PRINTER_REMOTE))
3635 continue;
3636
3637 if ((!strcmp(attr->name, "job-impressions") ||
3638 !strcmp(attr->name, "page-label") ||
3639 !strcmp(attr->name, "page-border") ||
3640 !strncmp(attr->name, "number-up", 9) ||
3641 !strcmp(attr->name, "page-ranges") ||
3642 !strcmp(attr->name, "page-set") ||
3643 !_cups_strcasecmp(attr->name, "AP_FIRSTPAGE_InputSlot") ||
3644 !_cups_strcasecmp(attr->name, "AP_FIRSTPAGE_ManualFeed") ||
3645 !_cups_strcasecmp(attr->name, "com.apple.print.PrintSettings."
3646 "PMTotalSidesImaged..n.") ||
3647 !_cups_strcasecmp(attr->name, "com.apple.print.PrintSettings."
3648 "PMTotalBeginPages..n.")) &&
3649 banner_page)
3650 continue;
3651
3652 /*
3653 * Otherwise add them to the list...
3654 */
3655
3656 if (optptr > options)
3657 strlcat(optptr, " ", optlength - (optptr - options));
3658
3659 if (attr->value_tag != IPP_TAG_BOOLEAN)
3660 {
3661 strlcat(optptr, attr->name, optlength - (optptr - options));
3662 strlcat(optptr, "=", optlength - (optptr - options));
3663 }
3664
3665 for (i = 0; i < attr->num_values; i ++)
3666 {
3667 if (i)
3668 strlcat(optptr, ",", optlength - (optptr - options));
3669
3670 optptr += strlen(optptr);
3671
3672 switch (attr->value_tag)
3673 {
3674 case IPP_TAG_INTEGER :
3675 case IPP_TAG_ENUM :
3676 snprintf(optptr, optlength - (optptr - options),
3677 "%d", attr->values[i].integer);
3678 break;
3679
3680 case IPP_TAG_BOOLEAN :
3681 if (!attr->values[i].boolean)
3682 strlcat(optptr, "no", optlength - (optptr - options));
3683
3684 strlcat(optptr, attr->name,
3685 optlength - (optptr - options));
3686 break;
3687
3688 case IPP_TAG_RANGE :
3689 if (attr->values[i].range.lower == attr->values[i].range.upper)
3690 snprintf(optptr, optlength - (optptr - options) - 1,
3691 "%d", attr->values[i].range.lower);
3692 else
3693 snprintf(optptr, optlength - (optptr - options) - 1,
3694 "%d-%d", attr->values[i].range.lower,
3695 attr->values[i].range.upper);
3696 break;
3697
3698 case IPP_TAG_RESOLUTION :
3699 snprintf(optptr, optlength - (optptr - options) - 1,
3700 "%dx%d%s", attr->values[i].resolution.xres,
3701 attr->values[i].resolution.yres,
3702 attr->values[i].resolution.units == IPP_RES_PER_INCH ?
3703 "dpi" : "dpcm");
3704 break;
3705
3706 case IPP_TAG_STRING :
3707 case IPP_TAG_TEXT :
3708 case IPP_TAG_NAME :
3709 case IPP_TAG_KEYWORD :
3710 case IPP_TAG_CHARSET :
3711 case IPP_TAG_LANGUAGE :
3712 case IPP_TAG_URI :
3713 for (valptr = attr->values[i].string.text; *valptr;)
3714 {
3715 if (strchr(" \t\n\\\'\"", *valptr))
3716 *optptr++ = '\\';
3717 *optptr++ = *valptr++;
3718 }
3719
3720 *optptr = '\0';
3721 break;
3722
3723 default :
3724 break; /* anti-compiler-warning-code */
3725 }
3726 }
3727
3728 optptr += strlen(optptr);
3729 }
3730 }
3731
3732 /*
3733 * Finally loop through the PWG->PPD mapped options and add them...
3734 */
3735
3736 for (i = num_pwgppds, pwgppd = pwgppds; i > 0; i --, pwgppd ++)
3737 {
3738 *optptr++ = ' ';
3739 strcpy(optptr, pwgppd->name);
3740 optptr += strlen(optptr);
3741 *optptr++ = '=';
3742 strcpy(optptr, pwgppd->value);
3743 optptr += strlen(optptr);
3744 }
3745
3746 cupsFreeOptions(num_pwgppds, pwgppds);
3747
3748 /*
3749 * Return the options string...
3750 */
3751
3752 return (options);
3753 }
3754
3755
3756 /*
3757 * 'ipp_length()' - Compute the size of the buffer needed to hold
3758 * the textual IPP attributes.
3759 */
3760
3761 static size_t /* O - Size of attribute buffer */
3762 ipp_length(ipp_t *ipp) /* I - IPP request */
3763 {
3764 size_t bytes; /* Number of bytes */
3765 int i; /* Looping var */
3766 ipp_attribute_t *attr; /* Current attribute */
3767
3768
3769 /*
3770 * Loop through all attributes...
3771 */
3772
3773 bytes = 0;
3774
3775 for (attr = ipp->attrs; attr != NULL; attr = attr->next)
3776 {
3777 /*
3778 * Skip attributes that won't be sent to filters...
3779 */
3780
3781 if (attr->value_tag == IPP_TAG_NOVALUE ||
3782 attr->value_tag == IPP_TAG_MIMETYPE ||
3783 attr->value_tag == IPP_TAG_NAMELANG ||
3784 attr->value_tag == IPP_TAG_TEXTLANG ||
3785 attr->value_tag == IPP_TAG_URI ||
3786 attr->value_tag == IPP_TAG_URISCHEME)
3787 continue;
3788
3789 /*
3790 * Add space for a leading space and commas between each value.
3791 * For the first attribute, the leading space isn't used, so the
3792 * extra byte can be used as the nul terminator...
3793 */
3794
3795 bytes ++; /* " " separator */
3796 bytes += attr->num_values; /* "," separators */
3797
3798 /*
3799 * Boolean attributes appear as "foo,nofoo,foo,nofoo", while
3800 * other attributes appear as "foo=value1,value2,...,valueN".
3801 */
3802
3803 if (attr->value_tag != IPP_TAG_BOOLEAN)
3804 bytes += strlen(attr->name);
3805 else
3806 bytes += attr->num_values * strlen(attr->name);
3807
3808 /*
3809 * Now add the size required for each value in the attribute...
3810 */
3811
3812 switch (attr->value_tag)
3813 {
3814 case IPP_TAG_INTEGER :
3815 case IPP_TAG_ENUM :
3816 /*
3817 * Minimum value of a signed integer is -2147483647, or 11 digits.
3818 */
3819
3820 bytes += attr->num_values * 11;
3821 break;
3822
3823 case IPP_TAG_BOOLEAN :
3824 /*
3825 * Add two bytes for each false ("no") value...
3826 */
3827
3828 for (i = 0; i < attr->num_values; i ++)
3829 if (!attr->values[i].boolean)
3830 bytes += 2;
3831 break;
3832
3833 case IPP_TAG_RANGE :
3834 /*
3835 * A range is two signed integers separated by a hyphen, or
3836 * 23 characters max.
3837 */
3838
3839 bytes += attr->num_values * 23;
3840 break;
3841
3842 case IPP_TAG_RESOLUTION :
3843 /*
3844 * A resolution is two signed integers separated by an "x" and
3845 * suffixed by the units, or 26 characters max.
3846 */
3847
3848 bytes += attr->num_values * 26;
3849 break;
3850
3851 case IPP_TAG_STRING :
3852 case IPP_TAG_TEXT :
3853 case IPP_TAG_NAME :
3854 case IPP_TAG_KEYWORD :
3855 case IPP_TAG_CHARSET :
3856 case IPP_TAG_LANGUAGE :
3857 case IPP_TAG_URI :
3858 /*
3859 * Strings can contain characters that need quoting. We need
3860 * at least 2 * len + 2 characters to cover the quotes and
3861 * any backslashes in the string.
3862 */
3863
3864 for (i = 0; i < attr->num_values; i ++)
3865 bytes += 2 * strlen(attr->values[i].string.text) + 2;
3866 break;
3867
3868 default :
3869 break; /* anti-compiler-warning-code */
3870 }
3871 }
3872
3873 return (bytes);
3874 }
3875
3876
3877 /*
3878 * 'load_job_cache()' - Load jobs from the job.cache file.
3879 */
3880
3881 static void
3882 load_job_cache(const char *filename) /* I - job.cache filename */
3883 {
3884 cups_file_t *fp; /* job.cache file */
3885 char line[1024], /* Line buffer */
3886 *value; /* Value on line */
3887 int linenum; /* Line number in file */
3888 cupsd_job_t *job; /* Current job */
3889 int jobid; /* Job ID */
3890 char jobfile[1024]; /* Job filename */
3891
3892
3893 /*
3894 * Open the job.cache file...
3895 */
3896
3897 if ((fp = cupsdOpenConfFile(filename)) == NULL)
3898 {
3899 load_request_root();
3900 return;
3901 }
3902
3903 /*
3904 * Read entries from the job cache file and create jobs as needed.
3905 */
3906
3907 cupsdLogMessage(CUPSD_LOG_INFO, "Loading job cache file \"%s\"...",
3908 filename);
3909
3910 linenum = 0;
3911 job = NULL;
3912
3913 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
3914 {
3915 if (!_cups_strcasecmp(line, "NextJobId"))
3916 {
3917 if (value)
3918 NextJobId = atoi(value);
3919 }
3920 else if (!_cups_strcasecmp(line, "<Job"))
3921 {
3922 if (job)
3923 {
3924 cupsdLogMessage(CUPSD_LOG_ERROR, "Missing </Job> directive on line %d.",
3925 linenum);
3926 continue;
3927 }
3928
3929 if (!value)
3930 {
3931 cupsdLogMessage(CUPSD_LOG_ERROR, "Missing job ID on line %d.", linenum);
3932 continue;
3933 }
3934
3935 jobid = atoi(value);
3936
3937 if (jobid < 1)
3938 {
3939 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad job ID %d on line %d.", jobid,
3940 linenum);
3941 continue;
3942 }
3943
3944 snprintf(jobfile, sizeof(jobfile), "%s/c%05d", RequestRoot, jobid);
3945 if (access(jobfile, 0))
3946 {
3947 snprintf(jobfile, sizeof(jobfile), "%s/c%05d.N", RequestRoot, jobid);
3948 if (access(jobfile, 0))
3949 {
3950 cupsdLogMessage(CUPSD_LOG_ERROR, "[Job %d] Files have gone away.",
3951 jobid);
3952 continue;
3953 }
3954 }
3955
3956 job = calloc(1, sizeof(cupsd_job_t));
3957 if (!job)
3958 {
3959 cupsdLogMessage(CUPSD_LOG_EMERG,
3960 "[Job %d] Unable to allocate memory for job.", jobid);
3961 break;
3962 }
3963
3964 job->id = jobid;
3965 job->back_pipes[0] = -1;
3966 job->back_pipes[1] = -1;
3967 job->print_pipes[0] = -1;
3968 job->print_pipes[1] = -1;
3969 job->side_pipes[0] = -1;
3970 job->side_pipes[1] = -1;
3971 job->status_pipes[0] = -1;
3972 job->status_pipes[1] = -1;
3973
3974 cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] Loading from cache...",
3975 job->id);
3976 }
3977 else if (!job)
3978 {
3979 cupsdLogMessage(CUPSD_LOG_ERROR,
3980 "Missing <Job #> directive on line %d.", linenum);
3981 continue;
3982 }
3983 else if (!_cups_strcasecmp(line, "</Job>"))
3984 {
3985 cupsArrayAdd(Jobs, job);
3986
3987 if (job->state_value <= IPP_JOB_STOPPED && cupsdLoadJob(job))
3988 cupsArrayAdd(ActiveJobs, job);
3989
3990 job = NULL;
3991 }
3992 else if (!value)
3993 {
3994 cupsdLogMessage(CUPSD_LOG_ERROR, "Missing value on line %d.", linenum);
3995 continue;
3996 }
3997 else if (!_cups_strcasecmp(line, "State"))
3998 {
3999 job->state_value = (ipp_jstate_t)atoi(value);
4000
4001 if (job->state_value < IPP_JOB_PENDING)
4002 job->state_value = IPP_JOB_PENDING;
4003 else if (job->state_value > IPP_JOB_COMPLETED)
4004 job->state_value = IPP_JOB_COMPLETED;
4005 }
4006 else if (!_cups_strcasecmp(line, "HoldUntil"))
4007 {
4008 job->hold_until = atoi(value);
4009 }
4010 else if (!_cups_strcasecmp(line, "Priority"))
4011 {
4012 job->priority = atoi(value);
4013 }
4014 else if (!_cups_strcasecmp(line, "Username"))
4015 {
4016 cupsdSetString(&job->username, value);
4017 }
4018 else if (!_cups_strcasecmp(line, "Destination"))
4019 {
4020 cupsdSetString(&job->dest, value);
4021 }
4022 else if (!_cups_strcasecmp(line, "DestType"))
4023 {
4024 job->dtype = (cups_ptype_t)atoi(value);
4025 }
4026 else if (!_cups_strcasecmp(line, "NumFiles"))
4027 {
4028 job->num_files = atoi(value);
4029
4030 if (job->num_files < 0)
4031 {
4032 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad NumFiles value %d on line %d.",
4033 job->num_files, linenum);
4034 job->num_files = 0;
4035 continue;
4036 }
4037
4038 if (job->num_files > 0)
4039 {
4040 snprintf(jobfile, sizeof(jobfile), "%s/d%05d-001", RequestRoot,
4041 job->id);
4042 if (access(jobfile, 0))
4043 {
4044 cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Data files have gone away.",
4045 job->id);
4046 job->num_files = 0;
4047 continue;
4048 }
4049
4050 job->filetypes = calloc(job->num_files, sizeof(mime_type_t *));
4051 job->compressions = calloc(job->num_files, sizeof(int));
4052
4053 if (!job->filetypes || !job->compressions)
4054 {
4055 cupsdLogMessage(CUPSD_LOG_EMERG,
4056 "[Job %d] Unable to allocate memory for %d files.",
4057 job->id, job->num_files);
4058 break;
4059 }
4060 }
4061 }
4062 else if (!_cups_strcasecmp(line, "File"))
4063 {
4064 int number, /* File number */
4065 compression; /* Compression value */
4066 char super[MIME_MAX_SUPER], /* MIME super type */
4067 type[MIME_MAX_TYPE]; /* MIME type */
4068
4069
4070 if (sscanf(value, "%d%*[ \t]%15[^/]/%255s%d", &number, super, type,
4071 &compression) != 4)
4072 {
4073 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad File on line %d.", linenum);
4074 continue;
4075 }
4076
4077 if (number < 1 || number > job->num_files)
4078 {
4079 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad File number %d on line %d.",
4080 number, linenum);
4081 continue;
4082 }
4083
4084 number --;
4085
4086 job->compressions[number] = compression;
4087 job->filetypes[number] = mimeType(MimeDatabase, super, type);
4088
4089 if (!job->filetypes[number])
4090 {
4091 /*
4092 * If the original MIME type is unknown, auto-type it!
4093 */
4094
4095 cupsdLogMessage(CUPSD_LOG_ERROR,
4096 "[Job %d] Unknown MIME type %s/%s for file %d.",
4097 job->id, super, type, number + 1);
4098
4099 snprintf(jobfile, sizeof(jobfile), "%s/d%05d-%03d", RequestRoot,
4100 job->id, number + 1);
4101 job->filetypes[number] = mimeFileType(MimeDatabase, jobfile, NULL,
4102 job->compressions + number);
4103
4104 /*
4105 * If that didn't work, assume it is raw...
4106 */
4107
4108 if (!job->filetypes[number])
4109 job->filetypes[number] = mimeType(MimeDatabase, "application",
4110 "vnd.cups-raw");
4111 }
4112 }
4113 else
4114 cupsdLogMessage(CUPSD_LOG_ERROR, "Unknown %s directive on line %d.",
4115 line, linenum);
4116 }
4117
4118 cupsFileClose(fp);
4119 }
4120
4121
4122 /*
4123 * 'load_next_job_id()' - Load the NextJobId value from the job.cache file.
4124 */
4125
4126 static void
4127 load_next_job_id(const char *filename) /* I - job.cache filename */
4128 {
4129 cups_file_t *fp; /* job.cache file */
4130 char line[1024], /* Line buffer */
4131 *value; /* Value on line */
4132 int linenum; /* Line number in file */
4133 int next_job_id; /* NextJobId value from line */
4134
4135
4136 /*
4137 * Read the NextJobId directive from the job.cache file and use
4138 * the value (if any).
4139 */
4140
4141 if ((fp = cupsFileOpen(filename, "r")) == NULL)
4142 {
4143 if (errno != ENOENT)
4144 cupsdLogMessage(CUPSD_LOG_ERROR,
4145 "Unable to open job cache file \"%s\": %s",
4146 filename, strerror(errno));
4147
4148 return;
4149 }
4150
4151 cupsdLogMessage(CUPSD_LOG_INFO,
4152 "Loading NextJobId from job cache file \"%s\"...", filename);
4153
4154 linenum = 0;
4155
4156 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
4157 {
4158 if (!_cups_strcasecmp(line, "NextJobId"))
4159 {
4160 if (value)
4161 {
4162 next_job_id = atoi(value);
4163
4164 if (next_job_id > NextJobId)
4165 NextJobId = next_job_id;
4166 }
4167 break;
4168 }
4169 }
4170
4171 cupsFileClose(fp);
4172 }
4173
4174
4175 /*
4176 * 'load_request_root()' - Load jobs from the RequestRoot directory.
4177 */
4178
4179 static void
4180 load_request_root(void)
4181 {
4182 cups_dir_t *dir; /* Directory */
4183 cups_dentry_t *dent; /* Directory entry */
4184 cupsd_job_t *job; /* New job */
4185
4186
4187 /*
4188 * Open the requests directory...
4189 */
4190
4191 cupsdLogMessage(CUPSD_LOG_DEBUG, "Scanning %s for jobs...", RequestRoot);
4192
4193 if ((dir = cupsDirOpen(RequestRoot)) == NULL)
4194 {
4195 cupsdLogMessage(CUPSD_LOG_ERROR,
4196 "Unable to open spool directory \"%s\": %s",
4197 RequestRoot, strerror(errno));
4198 return;
4199 }
4200
4201 /*
4202 * Read all the c##### files...
4203 */
4204
4205 while ((dent = cupsDirRead(dir)) != NULL)
4206 if (strlen(dent->filename) >= 6 && dent->filename[0] == 'c')
4207 {
4208 /*
4209 * Allocate memory for the job...
4210 */
4211
4212 if ((job = calloc(sizeof(cupsd_job_t), 1)) == NULL)
4213 {
4214 cupsdLogMessage(CUPSD_LOG_ERROR, "Ran out of memory for jobs.");
4215 cupsDirClose(dir);
4216 return;
4217 }
4218
4219 /*
4220 * Assign the job ID...
4221 */
4222
4223 job->id = atoi(dent->filename + 1);
4224 job->back_pipes[0] = -1;
4225 job->back_pipes[1] = -1;
4226 job->print_pipes[0] = -1;
4227 job->print_pipes[1] = -1;
4228 job->side_pipes[0] = -1;
4229 job->side_pipes[1] = -1;
4230 job->status_pipes[0] = -1;
4231 job->status_pipes[1] = -1;
4232
4233 if (job->id >= NextJobId)
4234 NextJobId = job->id + 1;
4235
4236 /*
4237 * Load the job...
4238 */
4239
4240 if (cupsdLoadJob(job))
4241 {
4242 /*
4243 * Insert the job into the array, sorting by job priority and ID...
4244 */
4245
4246 cupsArrayAdd(Jobs, job);
4247
4248 if (job->state_value <= IPP_JOB_STOPPED)
4249 cupsArrayAdd(ActiveJobs, job);
4250 else
4251 unload_job(job);
4252 }
4253 }
4254
4255 cupsDirClose(dir);
4256 }
4257
4258
4259 /*
4260 * 'remove_job_files()' - Remove the document files for a job.
4261 */
4262
4263 static void
4264 remove_job_files(cupsd_job_t *job) /* I - Job */
4265 {
4266 int i; /* Looping var */
4267 char filename[1024]; /* Document filename */
4268
4269
4270 if (job->num_files <= 0)
4271 return;
4272
4273 for (i = 1; i <= job->num_files; i ++)
4274 {
4275 snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot,
4276 job->id, i);
4277 if (Classification)
4278 cupsdRemoveFile(filename);
4279 else
4280 unlink(filename);
4281 }
4282
4283 free(job->filetypes);
4284 free(job->compressions);
4285
4286 job->file_time = 0;
4287 job->num_files = 0;
4288 job->filetypes = NULL;
4289 job->compressions = NULL;
4290
4291 LastEvent |= CUPSD_EVENT_PRINTER_STATE_CHANGED;
4292 }
4293
4294
4295 /*
4296 * 'remove_job_history()' - Remove the control file for a job.
4297 */
4298
4299 static void
4300 remove_job_history(cupsd_job_t *job) /* I - Job */
4301 {
4302 char filename[1024]; /* Control filename */
4303
4304
4305 /*
4306 * Remove the job info file...
4307 */
4308
4309 snprintf(filename, sizeof(filename), "%s/c%05d", RequestRoot,
4310 job->id);
4311 if (Classification)
4312 cupsdRemoveFile(filename);
4313 else
4314 unlink(filename);
4315
4316 LastEvent |= CUPSD_EVENT_PRINTER_STATE_CHANGED;
4317 }
4318
4319
4320 /*
4321 * 'set_time()' - Set one of the "time-at-xyz" attributes.
4322 */
4323
4324 static void
4325 set_time(cupsd_job_t *job, /* I - Job to update */
4326 const char *name) /* I - Name of attribute */
4327 {
4328 ipp_attribute_t *attr; /* Time attribute */
4329 time_t curtime; /* Current time */
4330
4331
4332 curtime = time(NULL);
4333
4334 cupsdLogJob(job, CUPSD_LOG_DEBUG, "%s=%ld", name, (long)curtime);
4335
4336 if ((attr = ippFindAttribute(job->attrs, name, IPP_TAG_ZERO)) != NULL)
4337 {
4338 attr->value_tag = IPP_TAG_INTEGER;
4339 attr->values[0].integer = curtime;
4340 }
4341
4342 if (!strcmp(name, "time-at-completed"))
4343 {
4344 if (JobHistory < INT_MAX)
4345 job->history_time = attr->values[0].integer + JobHistory;
4346 else
4347 job->history_time = INT_MAX;
4348
4349 if (job->history_time < JobHistoryUpdate || !JobHistoryUpdate)
4350 JobHistoryUpdate = job->history_time;
4351
4352 if (JobFiles < INT_MAX)
4353 job->file_time = attr->values[0].integer + JobFiles;
4354 else
4355 job->file_time = INT_MAX;
4356
4357 if (job->file_time < JobHistoryUpdate || !JobHistoryUpdate)
4358 JobHistoryUpdate = job->file_time;
4359
4360 cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_time: JobHistoryUpdate=%ld",
4361 (long)JobHistoryUpdate);
4362 }
4363 }
4364
4365
4366 /*
4367 * 'start_job()' - Start a print job.
4368 */
4369
4370 static void
4371 start_job(cupsd_job_t *job, /* I - Job ID */
4372 cupsd_printer_t *printer) /* I - Printer to print job */
4373 {
4374 cupsdLogMessage(CUPSD_LOG_DEBUG2, "start_job(job=%p(%d), printer=%p(%s))",
4375 job, job->id, printer, printer->name);
4376
4377 /*
4378 * Make sure we have some files around before we try to print...
4379 */
4380
4381 if (job->num_files == 0)
4382 {
4383 ippSetString(job->attrs, &job->reasons, 0, "aborted-by-system");
4384 cupsdSetJobState(job, IPP_JOB_ABORTED, CUPSD_JOB_DEFAULT,
4385 "Aborting job because it has no files.");
4386 return;
4387 }
4388
4389 /*
4390 * Update the printer and job state to "processing"...
4391 */
4392
4393 if (!cupsdLoadJob(job))
4394 return;
4395
4396 if (!job->printer_message)
4397 job->printer_message = ippFindAttribute(job->attrs,
4398 "job-printer-state-message",
4399 IPP_TAG_TEXT);
4400 if (job->printer_message)
4401 cupsdSetString(&(job->printer_message->values[0].string.text), "");
4402
4403 ippSetString(job->attrs, &job->reasons, 0, "job-printing");
4404 cupsdSetJobState(job, IPP_JOB_PROCESSING, CUPSD_JOB_DEFAULT, NULL);
4405 cupsdSetPrinterState(printer, IPP_PRINTER_PROCESSING, 0);
4406 cupsdSetPrinterReasons(printer, "-cups-remote-pending,"
4407 "cups-remote-pending-held,"
4408 "cups-remote-processing,"
4409 "cups-remote-stopped,"
4410 "cups-remote-canceled,"
4411 "cups-remote-aborted,"
4412 "cups-remote-completed");
4413
4414 job->cost = 0;
4415 job->current_file = 0;
4416 job->file_time = 0;
4417 job->history_time = 0;
4418 job->progress = 0;
4419 job->printer = printer;
4420 printer->job = job;
4421
4422 if (MaxJobTime > 0)
4423 job->cancel_time = time(NULL) + MaxJobTime;
4424 else
4425 job->cancel_time = 0;
4426
4427 /*
4428 * Setup the last exit status and security profiles...
4429 */
4430
4431 job->status = 0;
4432 job->profile = cupsdCreateProfile(job->id);
4433
4434 /*
4435 * Create the status pipes and buffer...
4436 */
4437
4438 if (cupsdOpenPipe(job->status_pipes))
4439 {
4440 cupsdLogJob(job, CUPSD_LOG_DEBUG,
4441 "Unable to create job status pipes - %s.", strerror(errno));
4442
4443 cupsdSetJobState(job, IPP_JOB_STOPPED, CUPSD_JOB_DEFAULT,
4444 "Job stopped because the scheduler could not create the "
4445 "job status pipes.");
4446
4447 cupsdDestroyProfile(job->profile);
4448 job->profile = NULL;
4449 return;
4450 }
4451
4452 job->status_buffer = cupsdStatBufNew(job->status_pipes[0], NULL);
4453 job->status_level = CUPSD_LOG_INFO;
4454
4455 /*
4456 * Create the backchannel pipes and make them non-blocking...
4457 */
4458
4459 if (cupsdOpenPipe(job->back_pipes))
4460 {
4461 cupsdLogJob(job, CUPSD_LOG_DEBUG,
4462 "Unable to create back-channel pipes - %s.", strerror(errno));
4463
4464 cupsdSetJobState(job, IPP_JOB_STOPPED, CUPSD_JOB_DEFAULT,
4465 "Job stopped because the scheduler could not create the "
4466 "back-channel pipes.");
4467
4468 cupsdClosePipe(job->status_pipes);
4469 cupsdStatBufDelete(job->status_buffer);
4470 job->status_buffer = NULL;
4471
4472 cupsdDestroyProfile(job->profile);
4473 job->profile = NULL;
4474 return;
4475 }
4476
4477 fcntl(job->back_pipes[0], F_SETFL,
4478 fcntl(job->back_pipes[0], F_GETFL) | O_NONBLOCK);
4479 fcntl(job->back_pipes[1], F_SETFL,
4480 fcntl(job->back_pipes[1], F_GETFL) | O_NONBLOCK);
4481
4482 /*
4483 * Create the side-channel pipes and make them non-blocking...
4484 */
4485
4486 if (socketpair(AF_LOCAL, SOCK_STREAM, 0, job->side_pipes))
4487 {
4488 cupsdLogJob(job, CUPSD_LOG_DEBUG,
4489 "Unable to create side-channel pipes - %s.", strerror(errno));
4490
4491 cupsdSetJobState(job, IPP_JOB_STOPPED, CUPSD_JOB_DEFAULT,
4492 "Job stopped because the scheduler could not create the "
4493 "side-channel pipes.");
4494
4495 cupsdClosePipe(job->back_pipes);
4496
4497 cupsdClosePipe(job->status_pipes);
4498 cupsdStatBufDelete(job->status_buffer);
4499 job->status_buffer = NULL;
4500
4501 cupsdDestroyProfile(job->profile);
4502 job->profile = NULL;
4503 return;
4504 }
4505
4506 fcntl(job->side_pipes[0], F_SETFL,
4507 fcntl(job->side_pipes[0], F_GETFL) | O_NONBLOCK);
4508 fcntl(job->side_pipes[1], F_SETFL,
4509 fcntl(job->side_pipes[1], F_GETFL) | O_NONBLOCK);
4510
4511 fcntl(job->side_pipes[0], F_SETFD,
4512 fcntl(job->side_pipes[0], F_GETFD) | FD_CLOEXEC);
4513 fcntl(job->side_pipes[1], F_SETFD,
4514 fcntl(job->side_pipes[1], F_GETFD) | FD_CLOEXEC);
4515
4516 /*
4517 * Now start the first file in the job...
4518 */
4519
4520 cupsdContinueJob(job);
4521 }
4522
4523
4524 /*
4525 * 'stop_job()' - Stop a print job.
4526 */
4527
4528 static void
4529 stop_job(cupsd_job_t *job, /* I - Job */
4530 cupsd_jobaction_t action) /* I - Action */
4531 {
4532 int i; /* Looping var */
4533
4534
4535 cupsdLogMessage(CUPSD_LOG_DEBUG2, "stop_job(job=%p(%d), action=%d)", job,
4536 job->id, action);
4537
4538 FilterLevel -= job->cost;
4539 job->cost = 0;
4540
4541 if (action == CUPSD_JOB_DEFAULT && !job->kill_time)
4542 job->kill_time = time(NULL) + JobKillDelay;
4543 else if (action >= CUPSD_JOB_FORCE)
4544 job->kill_time = 0;
4545
4546 for (i = 0; job->filters[i]; i ++)
4547 if (job->filters[i] > 0)
4548 {
4549 cupsdEndProcess(job->filters[i], action >= CUPSD_JOB_FORCE);
4550
4551 if (action >= CUPSD_JOB_FORCE)
4552 job->filters[i] = -job->filters[i];
4553 }
4554
4555 if (job->backend > 0)
4556 {
4557 cupsdEndProcess(job->backend, action >= CUPSD_JOB_FORCE);
4558
4559 if (action >= CUPSD_JOB_FORCE)
4560 job->backend = -job->backend;
4561 }
4562
4563 if (action >= CUPSD_JOB_FORCE)
4564 {
4565 /*
4566 * Clear job status...
4567 */
4568
4569 job->status = 0;
4570 }
4571 }
4572
4573
4574 /*
4575 * 'unload_job()' - Unload a job from memory.
4576 */
4577
4578 static void
4579 unload_job(cupsd_job_t *job) /* I - Job */
4580 {
4581 if (!job->attrs)
4582 return;
4583
4584 cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] Unloading...", job->id);
4585
4586 ippDelete(job->attrs);
4587
4588 job->attrs = NULL;
4589 job->state = NULL;
4590 job->reasons = NULL;
4591 job->sheets = NULL;
4592 job->job_sheets = NULL;
4593 job->printer_message = NULL;
4594 job->printer_reasons = NULL;
4595 }
4596
4597
4598 /*
4599 * 'update_job()' - Read a status update from a job's filters.
4600 */
4601
4602 void
4603 update_job(cupsd_job_t *job) /* I - Job to check */
4604 {
4605 int i; /* Looping var */
4606 int copies; /* Number of copies printed */
4607 char message[CUPSD_SB_BUFFER_SIZE],
4608 /* Message text */
4609 *ptr; /* Pointer update... */
4610 int loglevel, /* Log level for message */
4611 event = 0; /* Events? */
4612 static const char * const levels[] = /* Log levels */
4613 {
4614 "NONE",
4615 "EMERG",
4616 "ALERT",
4617 "CRIT",
4618 "ERROR",
4619 "WARN",
4620 "NOTICE",
4621 "INFO",
4622 "DEBUG",
4623 "DEBUG2"
4624 };
4625
4626
4627 /*
4628 * Get the printer associated with this job; if the printer is stopped for
4629 * any reason then job->printer will be reset to NULL, so make sure we have
4630 * a valid pointer...
4631 */
4632
4633 while ((ptr = cupsdStatBufUpdate(job->status_buffer, &loglevel,
4634 message, sizeof(message))) != NULL)
4635 {
4636 /*
4637 * Process page and printer state messages as needed...
4638 */
4639
4640 if (loglevel == CUPSD_LOG_PAGE)
4641 {
4642 /*
4643 * Page message; send the message to the page_log file and update the
4644 * job sheet count...
4645 */
4646
4647 cupsdLogJob(job, CUPSD_LOG_DEBUG, "PAGE: %s", message);
4648
4649 if (job->sheets)
4650 {
4651 if (!_cups_strncasecmp(message, "total ", 6))
4652 {
4653 /*
4654 * Got a total count of pages from a backend or filter...
4655 */
4656
4657 copies = atoi(message + 6);
4658 copies -= job->sheets->values[0].integer; /* Just track the delta */
4659 }
4660 else if (!sscanf(message, "%*d%d", &copies))
4661 copies = 1;
4662
4663 job->sheets->values[0].integer += copies;
4664
4665 if (job->printer->page_limit)
4666 cupsdUpdateQuota(job->printer, job->username, copies, 0);
4667 }
4668
4669 cupsdLogPage(job, message);
4670
4671 if (job->sheets)
4672 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS, job->printer, job,
4673 "Printed %d page(s).", job->sheets->values[0].integer);
4674 }
4675 else if (loglevel == CUPSD_LOG_STATE)
4676 {
4677 cupsdLogJob(job, CUPSD_LOG_DEBUG, "STATE: %s", message);
4678
4679 if (!strcmp(message, "paused"))
4680 {
4681 cupsdStopPrinter(job->printer, 1);
4682 return;
4683 }
4684 else if (cupsdSetPrinterReasons(job->printer, message))
4685 {
4686 event |= CUPSD_EVENT_PRINTER_STATE;
4687
4688 if (MaxJobTime > 0 && strstr(message, "connecting-to-device") != NULL)
4689 {
4690 /*
4691 * Reset cancel time after connecting to the device...
4692 */
4693
4694 for (i = 0; i < job->printer->num_reasons; i ++)
4695 if (!strcmp(job->printer->reasons[i], "connecting-to-device"))
4696 break;
4697
4698 if (i >= job->printer->num_reasons)
4699 job->cancel_time = time(NULL) + MaxJobTime;
4700 }
4701 }
4702
4703 update_job_attrs(job, 0);
4704 }
4705 else if (loglevel == CUPSD_LOG_ATTR)
4706 {
4707 /*
4708 * Set attribute(s)...
4709 */
4710
4711 int num_attrs; /* Number of attributes */
4712 cups_option_t *attrs; /* Attributes */
4713 const char *attr; /* Attribute */
4714
4715
4716 cupsdLogJob(job, CUPSD_LOG_DEBUG, "ATTR: %s", message);
4717
4718 num_attrs = cupsParseOptions(message, 0, &attrs);
4719
4720 if ((attr = cupsGetOption("auth-info-required", num_attrs,
4721 attrs)) != NULL)
4722 {
4723 cupsdSetAuthInfoRequired(job->printer, attr, NULL);
4724 cupsdSetPrinterAttrs(job->printer);
4725
4726 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
4727 }
4728
4729 if ((attr = cupsGetOption("job-media-progress", num_attrs,
4730 attrs)) != NULL)
4731 {
4732 int progress = atoi(attr);
4733
4734
4735 if (progress >= 0 && progress <= 100)
4736 {
4737 job->progress = progress;
4738
4739 if (job->sheets)
4740 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS, job->printer, job,
4741 "Printing page %d, %d%%",
4742 job->sheets->values[0].integer, job->progress);
4743 }
4744 }
4745
4746 if ((attr = cupsGetOption("printer-alert", num_attrs, attrs)) != NULL)
4747 {
4748 cupsdSetString(&job->printer->alert, attr);
4749 event |= CUPSD_EVENT_PRINTER_STATE;
4750 }
4751
4752 if ((attr = cupsGetOption("printer-alert-description", num_attrs,
4753 attrs)) != NULL)
4754 {
4755 cupsdSetString(&job->printer->alert_description, attr);
4756 event |= CUPSD_EVENT_PRINTER_STATE;
4757 }
4758
4759 if ((attr = cupsGetOption("marker-colors", num_attrs, attrs)) != NULL)
4760 {
4761 cupsdSetPrinterAttr(job->printer, "marker-colors", (char *)attr);
4762 job->printer->marker_time = time(NULL);
4763 event |= CUPSD_EVENT_PRINTER_STATE;
4764 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
4765 }
4766
4767 if ((attr = cupsGetOption("marker-levels", num_attrs, attrs)) != NULL)
4768 {
4769 cupsdSetPrinterAttr(job->printer, "marker-levels", (char *)attr);
4770 job->printer->marker_time = time(NULL);
4771 event |= CUPSD_EVENT_PRINTER_STATE;
4772 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
4773 }
4774
4775 if ((attr = cupsGetOption("marker-low-levels", num_attrs, attrs)) != NULL)
4776 {
4777 cupsdSetPrinterAttr(job->printer, "marker-low-levels", (char *)attr);
4778 job->printer->marker_time = time(NULL);
4779 event |= CUPSD_EVENT_PRINTER_STATE;
4780 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
4781 }
4782
4783 if ((attr = cupsGetOption("marker-high-levels", num_attrs, attrs)) != NULL)
4784 {
4785 cupsdSetPrinterAttr(job->printer, "marker-high-levels", (char *)attr);
4786 job->printer->marker_time = time(NULL);
4787 event |= CUPSD_EVENT_PRINTER_STATE;
4788 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
4789 }
4790
4791 if ((attr = cupsGetOption("marker-message", num_attrs, attrs)) != NULL)
4792 {
4793 cupsdSetPrinterAttr(job->printer, "marker-message", (char *)attr);
4794 job->printer->marker_time = time(NULL);
4795 event |= CUPSD_EVENT_PRINTER_STATE;
4796 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
4797 }
4798
4799 if ((attr = cupsGetOption("marker-names", num_attrs, attrs)) != NULL)
4800 {
4801 cupsdSetPrinterAttr(job->printer, "marker-names", (char *)attr);
4802 job->printer->marker_time = time(NULL);
4803 event |= CUPSD_EVENT_PRINTER_STATE;
4804 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
4805 }
4806
4807 if ((attr = cupsGetOption("marker-types", num_attrs, attrs)) != NULL)
4808 {
4809 cupsdSetPrinterAttr(job->printer, "marker-types", (char *)attr);
4810 job->printer->marker_time = time(NULL);
4811 event |= CUPSD_EVENT_PRINTER_STATE;
4812 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
4813 }
4814
4815 cupsFreeOptions(num_attrs, attrs);
4816 }
4817 else if (loglevel == CUPSD_LOG_PPD)
4818 {
4819 /*
4820 * Set attribute(s)...
4821 */
4822
4823 int num_keywords; /* Number of keywords */
4824 cups_option_t *keywords; /* Keywords */
4825
4826
4827 cupsdLogJob(job, CUPSD_LOG_DEBUG, "PPD: %s", message);
4828
4829 num_keywords = cupsParseOptions(message, 0, &keywords);
4830
4831 if (cupsdUpdatePrinterPPD(job->printer, num_keywords, keywords))
4832 cupsdSetPrinterAttrs(job->printer);
4833
4834 cupsFreeOptions(num_keywords, keywords);
4835 }
4836 else
4837 {
4838 /*
4839 * Strip legacy message prefix...
4840 */
4841
4842 if (!strncmp(message, "recoverable:", 12))
4843 {
4844 ptr = message + 12;
4845 while (isspace(*ptr & 255))
4846 ptr ++;
4847 }
4848 else if (!strncmp(message, "recovered:", 10))
4849 {
4850 ptr = message + 10;
4851 while (isspace(*ptr & 255))
4852 ptr ++;
4853 }
4854 else
4855 ptr = message;
4856
4857 if (*ptr)
4858 cupsdLogJob(job, loglevel, "%s", ptr);
4859
4860 if (loglevel < CUPSD_LOG_DEBUG &&
4861 strcmp(job->printer->state_message, ptr))
4862 {
4863 strlcpy(job->printer->state_message, ptr,
4864 sizeof(job->printer->state_message));
4865
4866 event |= CUPSD_EVENT_PRINTER_STATE | CUPSD_EVENT_JOB_PROGRESS;
4867
4868 if (loglevel <= job->status_level && job->status_level > CUPSD_LOG_ERROR)
4869 {
4870 /*
4871 * Some messages show in the job-printer-state-message attribute...
4872 */
4873
4874 if (loglevel != CUPSD_LOG_NOTICE)
4875 job->status_level = loglevel;
4876
4877 update_job_attrs(job, 1);
4878
4879 cupsdLogJob(job, CUPSD_LOG_DEBUG,
4880 "Set job-printer-state-message to \"%s\", "
4881 "current level=%s",
4882 job->printer_message->values[0].string.text,
4883 levels[job->status_level]);
4884 }
4885 }
4886 }
4887
4888 if (!strchr(job->status_buffer->buffer, '\n'))
4889 break;
4890 }
4891
4892 if (event & CUPSD_EVENT_JOB_PROGRESS)
4893 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS, job->printer, job,
4894 "%s", job->printer->state_message);
4895 if (event & CUPSD_EVENT_PRINTER_STATE)
4896 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, job->printer, NULL,
4897 (job->printer->type & CUPS_PRINTER_CLASS) ?
4898 "Class \"%s\" state changed." :
4899 "Printer \"%s\" state changed.",
4900 job->printer->name);
4901
4902
4903 if (ptr == NULL && !job->status_buffer->bufused)
4904 {
4905 /*
4906 * See if all of the filters and the backend have returned their
4907 * exit statuses.
4908 */
4909
4910 for (i = 0; job->filters[i] < 0; i ++);
4911
4912 if (job->filters[i])
4913 {
4914 /*
4915 * EOF but we haven't collected the exit status of all filters...
4916 */
4917
4918 cupsdCheckProcess();
4919 return;
4920 }
4921
4922 if (job->current_file >= job->num_files && job->backend > 0)
4923 {
4924 /*
4925 * EOF but we haven't collected the exit status of the backend...
4926 */
4927
4928 cupsdCheckProcess();
4929 return;
4930 }
4931
4932 /*
4933 * Handle the end of job stuff...
4934 */
4935
4936 finalize_job(job, 1);
4937
4938 /*
4939 * Check for new jobs...
4940 */
4941
4942 cupsdCheckJobs();
4943 }
4944 }
4945
4946
4947 /*
4948 * 'update_job_attrs()' - Update the job-printer-* attributes.
4949 */
4950
4951 void
4952 update_job_attrs(cupsd_job_t *job, /* I - Job to update */
4953 int do_message)/* I - 1 = copy job-printer-state message */
4954 {
4955 int i; /* Looping var */
4956 int num_reasons; /* Actual number of reasons */
4957 const char * const *reasons; /* Reasons */
4958 static const char *none = "none"; /* "none" reason */
4959
4960
4961 /*
4962 * Get/create the job-printer-state-* attributes...
4963 */
4964
4965 if (!job->printer_message)
4966 {
4967 if ((job->printer_message = ippFindAttribute(job->attrs,
4968 "job-printer-state-message",
4969 IPP_TAG_TEXT)) == NULL)
4970 job->printer_message = ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_TEXT,
4971 "job-printer-state-message",
4972 NULL, "");
4973 }
4974
4975 if (!job->printer_reasons)
4976 job->printer_reasons = ippFindAttribute(job->attrs,
4977 "job-printer-state-reasons",
4978 IPP_TAG_KEYWORD);
4979
4980 /*
4981 * Copy or clear the printer-state-message value as needed...
4982 */
4983
4984 if (job->state_value != IPP_JOB_PROCESSING &&
4985 job->status_level == CUPSD_LOG_INFO)
4986 {
4987 cupsdSetString(&(job->printer_message->values[0].string.text), "");
4988
4989 job->dirty = 1;
4990 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
4991 }
4992 else if (job->printer->state_message[0] && do_message)
4993 {
4994 cupsdSetString(&(job->printer_message->values[0].string.text),
4995 job->printer->state_message);
4996
4997 job->dirty = 1;
4998 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
4999 }
5000
5001 /*
5002 * ... and the printer-state-reasons value...
5003 */
5004
5005 if (job->printer->num_reasons == 0)
5006 {
5007 num_reasons = 1;
5008 reasons = &none;
5009 }
5010 else
5011 {
5012 num_reasons = job->printer->num_reasons;
5013 reasons = (const char * const *)job->printer->reasons;
5014 }
5015
5016 if (!job->printer_reasons || job->printer_reasons->num_values != num_reasons)
5017 {
5018 /*
5019 * Replace/create a job-printer-state-reasons attribute...
5020 */
5021
5022 ippDeleteAttribute(job->attrs, job->printer_reasons);
5023
5024 job->printer_reasons = ippAddStrings(job->attrs,
5025 IPP_TAG_JOB, IPP_TAG_KEYWORD,
5026 "job-printer-state-reasons",
5027 num_reasons, NULL, NULL);
5028 }
5029 else
5030 {
5031 /*
5032 * Don't bother clearing the reason strings if they are the same...
5033 */
5034
5035 for (i = 0; i < num_reasons; i ++)
5036 if (strcmp(job->printer_reasons->values[i].string.text, reasons[i]))
5037 break;
5038
5039 if (i >= num_reasons)
5040 return;
5041
5042 /*
5043 * Not the same, so free the current strings...
5044 */
5045
5046 for (i = 0; i < num_reasons; i ++)
5047 _cupsStrFree(job->printer_reasons->values[i].string.text);
5048 }
5049
5050 /*
5051 * Copy the reasons...
5052 */
5053
5054 for (i = 0; i < num_reasons; i ++)
5055 job->printer_reasons->values[i].string.text = _cupsStrAlloc(reasons[i]);
5056
5057 job->dirty = 1;
5058 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
5059 }
5060
5061
5062 /*
5063 * End of "$Id: job.c 7902 2008-09-03 14:20:17Z mike $".
5064 */