]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/job.c
Update to CUPS trunk r6695.
[thirdparty/cups.git] / scheduler / job.c
1 /*
2 * "$Id: job.c 6689 2007-07-18 23:52:15Z mike $"
3 *
4 * Job management routines for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 2007 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 * cupsdCancelJob() - Cancel the specified print job.
19 * cupsdCancelJobs() - Cancel all jobs for the given
20 * destination/user...
21 * cupsdCheckJobs() - Check the pending jobs and start any if
22 * the destination is available.
23 * cupsdCleanJobs() - Clean out old jobs.
24 * cupsdFinishJob() - Finish a job.
25 * cupsdFreeAllJobs() - Free all jobs from memory.
26 * cupsdFindJob() - Find the specified job.
27 * cupsdGetPrinterJobCount() - Get the number of pending, processing,
28 * cupsdGetUserJobCount() - Get the number of pending, processing,
29 * cupsdHoldJob() - Hold the specified job.
30 * cupsdLoadAllJobs() - Load all jobs from disk.
31 * cupsdLoadJob() - Load a single job...
32 * cupsdMoveJob() - Move the specified job to a different
33 * destination.
34 * cupsdReleaseJob() - Release the specified job.
35 * cupsdRestartJob() - Restart the specified job.
36 * cupsdSaveAllJobs() - Save a summary of all jobs to disk.
37 * cupsdSaveJob() - Save a job to disk.
38 * cupsdSetJobHoldUntil() - Set the hold time for a job...
39 * cupsdSetJobPriority() - Set the priority of a job, moving it
40 * up/down in the list as needed.
41 * cupsdStopAllJobs() - Stop all print jobs.
42 * cupsdStopJob() - Stop a print job.
43 * cupsdUnloadCompletedJobs() - Flush completed job history from memory.
44 * compare_active_jobs() - Compare the job IDs and priorities of two
45 * jobs.
46 * compare_jobs() - Compare the job IDs of two jobs.
47 * free_job() - Free all memory used by a job.
48 * ipp_length() - Compute the size of the buffer needed to
49 * hold the textual IPP attributes.
50 * load_job_cache() - Load jobs from the job.cache file.
51 * load_next_job_id() - Load the NextJobId value from the
52 * job.cache file.
53 * load_request_root() - Load jobs from the RequestRoot directory.
54 * set_time() - Set one of the "time-at-xyz" attributes...
55 * set_hold_until() - Set the hold time and update job-hold-until
56 * attribute...
57 * start_job() - Start a print job.
58 * unload_job() - Unload a job from memory.
59 * update_job() - Read a status update from a jobs filters.
60 * update_job_attrs() - Update the job-printer-* attributes.
61 */
62
63 /*
64 * Include necessary headers...
65 */
66
67 #include "cupsd.h"
68 #include <grp.h>
69 #include <cups/backend.h>
70 #include <cups/dir.h>
71
72
73 /*
74 * Local globals...
75 */
76
77 static mime_filter_t gziptoany_filter =
78 {
79 NULL, /* Source type */
80 NULL, /* Destination type */
81 0, /* Cost */
82 "gziptoany" /* Filter program to run */
83 };
84
85
86 /*
87 * Local functions...
88 */
89
90 static int compare_active_jobs(void *first, void *second, void *data);
91 static int compare_jobs(void *first, void *second, void *data);
92 static void free_job(cupsd_job_t *job);
93 static int ipp_length(ipp_t *ipp);
94 static void load_job_cache(const char *filename);
95 static void load_next_job_id(const char *filename);
96 static void load_request_root(void);
97 static void set_time(cupsd_job_t *job, const char *name);
98 static void set_hold_until(cupsd_job_t *job, time_t holdtime);
99 static void start_job(cupsd_job_t *job, cupsd_printer_t *printer);
100 static void unload_job(cupsd_job_t *job);
101 static void update_job(cupsd_job_t *job);
102 static void update_job_attrs(cupsd_job_t *job);
103
104
105 /*
106 * 'cupsdAddJob()' - Add a new job to the job queue...
107 */
108
109 cupsd_job_t * /* O - New job record */
110 cupsdAddJob(int priority, /* I - Job priority */
111 const char *dest) /* I - Job destination */
112 {
113 cupsd_job_t *job; /* New job record */
114
115
116 job = calloc(sizeof(cupsd_job_t), 1);
117
118 job->id = NextJobId ++;
119 job->priority = priority;
120 job->back_pipes[0] = -1;
121 job->back_pipes[1] = -1;
122 job->print_pipes[0] = -1;
123 job->print_pipes[1] = -1;
124 job->side_pipes[0] = -1;
125 job->side_pipes[1] = -1;
126 job->status_pipes[0] = -1;
127 job->status_pipes[1] = -1;
128
129 cupsdSetString(&job->dest, dest);
130
131 /*
132 * Add the new job to the "all jobs" and "active jobs" lists...
133 */
134
135 cupsArrayAdd(Jobs, job);
136 cupsArrayAdd(ActiveJobs, job);
137
138 return (job);
139 }
140
141
142 /*
143 * 'cupsdCancelJob()' - Cancel the specified print job.
144 */
145
146 void
147 cupsdCancelJob(cupsd_job_t *job, /* I - Job to cancel */
148 int purge, /* I - Purge jobs? */
149 ipp_jstate_t newstate) /* I - New job state */
150 {
151 int i; /* Looping var */
152 char filename[1024]; /* Job filename */
153 cupsd_printer_t *printer; /* Printer used by job */
154
155
156 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdCancelJob: id = %d", job->id);
157
158 /*
159 * Stop any processes that are working on the current job...
160 */
161
162 printer = job->printer;
163
164 if (job->state_value == IPP_JOB_PROCESSING)
165 cupsdStopJob(job, 0);
166
167 cupsdLoadJob(job);
168
169 if (job->attrs)
170 job->state->values[0].integer = newstate;
171
172 job->state_value = newstate;
173
174 set_time(job, "time-at-completed");
175
176 /*
177 * Send any pending notifications and then expire them...
178 */
179
180 switch (newstate)
181 {
182 default :
183 break;
184
185 case IPP_JOB_CANCELED :
186 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, printer, job,
187 purge ? "Job purged." : "Job canceled.");
188 break;
189
190 case IPP_JOB_ABORTED :
191 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, printer, job,
192 "Job aborted; please consult the error_log file "
193 "for details.");
194 break;
195
196 case IPP_JOB_COMPLETED :
197 /*
198 * Clear the printer's printer-state-message and move on...
199 */
200
201 printer->state_message[0] = '\0';
202
203 cupsdSetPrinterState(printer, IPP_PRINTER_IDLE, 0);
204
205 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, printer, job,
206 "Job completed.");
207 break;
208 }
209
210 cupsdExpireSubscriptions(NULL, job);
211
212 /*
213 * Remove the job from the active list...
214 */
215
216 cupsArrayRemove(ActiveJobs, job);
217
218 /*
219 * Remove any authentication data...
220 */
221
222 snprintf(filename, sizeof(filename), "%s/a%05d", RequestRoot, job->id);
223 if (cupsdRemoveFile(filename))
224 cupsdLogMessage(CUPSD_LOG_ERROR,
225 "Unable to remove authentication cache: %s",
226 strerror(errno));
227
228 cupsdClearString(&job->auth_username);
229 cupsdClearString(&job->auth_domain);
230 cupsdClearString(&job->auth_password);
231
232 /*
233 * Remove the print file for good if we aren't preserving jobs or
234 * files...
235 */
236
237 job->current_file = 0;
238
239 if (!JobHistory || !JobFiles || purge || (job->dtype & CUPS_PRINTER_REMOTE))
240 {
241 for (i = 1; i <= job->num_files; i ++)
242 {
243 snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot,
244 job->id, i);
245 unlink(filename);
246 }
247
248 if (job->num_files > 0)
249 {
250 free(job->filetypes);
251 free(job->compressions);
252
253 job->num_files = 0;
254 job->filetypes = NULL;
255 job->compressions = NULL;
256 }
257 }
258
259 if (JobHistory && !purge && !(job->dtype & CUPS_PRINTER_REMOTE))
260 {
261 /*
262 * Save job state info...
263 */
264
265 cupsdSaveJob(job);
266 }
267 else
268 {
269 /*
270 * Remove the job info file...
271 */
272
273 snprintf(filename, sizeof(filename), "%s/c%05d", RequestRoot,
274 job->id);
275 unlink(filename);
276
277 /*
278 * Remove the job from the "all jobs" list...
279 */
280
281 cupsArrayRemove(Jobs, job);
282
283 /*
284 * Free all memory used...
285 */
286
287 free_job(job);
288 }
289 }
290
291
292 /*
293 * 'cupsdCancelJobs()' - Cancel all jobs for the given destination/user...
294 */
295
296 void
297 cupsdCancelJobs(const char *dest, /* I - Destination to cancel */
298 const char *username, /* I - Username or NULL */
299 int purge) /* I - Purge jobs? */
300 {
301 cupsd_job_t *job; /* Current job */
302
303
304 for (job = (cupsd_job_t *)cupsArrayFirst(Jobs);
305 job;
306 job = (cupsd_job_t *)cupsArrayNext(Jobs))
307 if ((dest == NULL || !strcmp(job->dest, dest)) &&
308 (username == NULL || !strcmp(job->username, username)))
309 {
310 /*
311 * Cancel all jobs matching this destination/user...
312 */
313
314 cupsdCancelJob(job, purge, IPP_JOB_CANCELED);
315 }
316
317 cupsdCheckJobs();
318 }
319
320
321 /*
322 * 'cupsdCheckJobs()' - Check the pending jobs and start any if the destination
323 * is available.
324 */
325
326 void
327 cupsdCheckJobs(void)
328 {
329 cupsd_job_t *job; /* Current job in queue */
330 cupsd_printer_t *printer, /* Printer destination */
331 *pclass; /* Printer class destination */
332
333
334 DEBUG_puts("cupsdCheckJobs()");
335
336 cupsdLogMessage(CUPSD_LOG_DEBUG2,
337 "cupsdCheckJobs: %d active jobs, sleeping=%d, reload=%d",
338 cupsArrayCount(ActiveJobs), Sleeping, NeedReload);
339
340 for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs);
341 job;
342 job = (cupsd_job_t *)cupsArrayNext(ActiveJobs))
343 {
344 /*
345 * Start held jobs if they are ready...
346 */
347
348 cupsdLogMessage(CUPSD_LOG_DEBUG2,
349 "cupsdCheckJobs: Job %d: state_value=%d, loaded=%s",
350 job->id, job->state_value, job->attrs ? "yes" : "no");
351
352 if (job->state_value == IPP_JOB_HELD &&
353 job->hold_until &&
354 job->hold_until < time(NULL))
355 {
356 if (job->pending_timeout)
357 cupsdTimeoutJob(job); /* Add trailing banner as needed */
358
359 job->state->values[0].integer = IPP_JOB_PENDING;
360 job->state_value = IPP_JOB_PENDING;
361 }
362
363 /*
364 * Start pending jobs if the destination is available...
365 */
366
367 if (job->state_value == IPP_JOB_PENDING && !NeedReload && !Sleeping)
368 {
369 printer = cupsdFindDest(job->dest);
370 pclass = NULL;
371
372 while (printer &&
373 (printer->type & (CUPS_PRINTER_IMPLICIT | CUPS_PRINTER_CLASS)))
374 {
375 /*
376 * If the class is remote, just pass it to the remote server...
377 */
378
379 pclass = printer;
380
381 if (pclass->state == IPP_PRINTER_STOPPED)
382 printer = NULL;
383 else if (pclass->type & CUPS_PRINTER_REMOTE)
384 break;
385 else
386 printer = cupsdFindAvailablePrinter(printer->name);
387 }
388
389 if (!printer && !pclass)
390 {
391 /*
392 * Whoa, the printer and/or class for this destination went away;
393 * cancel the job...
394 */
395
396 cupsdLogMessage(CUPSD_LOG_WARN,
397 "[Job %d] Printer/class %s has gone away; canceling job!",
398 job->id, job->dest);
399
400 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
401 "Job canceled because the destination printer/class has "
402 "gone away.");
403
404 cupsdCancelJob(job, 1, IPP_JOB_ABORTED);
405 }
406 else if (printer)
407 {
408 /*
409 * See if the printer is available or remote and not printing a job;
410 * if so, start the job...
411 */
412
413 if (pclass)
414 {
415 /*
416 * Add/update a job-actual-printer-uri attribute for this job
417 * so that we know which printer actually printed the job...
418 */
419
420 ipp_attribute_t *attr; /* job-actual-printer-uri attribute */
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
431 if ((!(printer->type & CUPS_PRINTER_DISCOVERED) && /* Printer is local */
432 printer->state == IPP_PRINTER_IDLE) || /* and idle */
433 ((printer->type & CUPS_PRINTER_DISCOVERED) && /* Printer is remote */
434 !printer->job)) /* and not printing */
435 {
436 /*
437 * Clear any message and reasons for the queue...
438 */
439
440 printer->state_message[0] = '\0';
441
442 cupsdSetPrinterReasons(printer, "none");
443
444 /*
445 * Start the job...
446 */
447
448 start_job(job, printer);
449 }
450 }
451 }
452 }
453 }
454
455
456 /*
457 * 'cupsdCleanJobs()' - Clean out old jobs.
458 */
459
460 void
461 cupsdCleanJobs(void)
462 {
463 cupsd_job_t *job; /* Current job */
464
465
466 if (MaxJobs <= 0)
467 return;
468
469 for (job = (cupsd_job_t *)cupsArrayFirst(Jobs);
470 job && cupsArrayCount(Jobs) >= MaxJobs;
471 job = (cupsd_job_t *)cupsArrayNext(Jobs))
472 if (job->state_value >= IPP_JOB_CANCELED)
473 cupsdCancelJob(job, 1, IPP_JOB_CANCELED);
474 }
475
476
477 /*
478 * 'cupsdFinishJob()' - Finish a job.
479 */
480
481 void
482 cupsdFinishJob(cupsd_job_t *job) /* I - Job */
483 {
484 cupsd_printer_t *printer; /* Current printer */
485 ipp_attribute_t *attr; /* job-hold-until attribute */
486
487
488 cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] File %d is complete.",
489 job->id, job->current_file - 1);
490
491 cupsdLogMessage(CUPSD_LOG_DEBUG2,
492 "[Job %d] cupsdFinishJob: job->status is %d",
493 job->id, job->status);
494
495 if (job->status_buffer &&
496 (job->status < 0 || job->current_file >= job->num_files))
497 {
498 /*
499 * Close the pipe and clear the input bit.
500 */
501
502 cupsdRemoveSelect(job->status_buffer->fd);
503
504 cupsdLogMessage(CUPSD_LOG_DEBUG2,
505 "[Job %d] cupsdFinishJob: Closing status pipes [ %d %d ]...",
506 job->id, job->status_pipes[0], job->status_pipes[1]);
507
508 cupsdClosePipe(job->status_pipes);
509 cupsdStatBufDelete(job->status_buffer);
510
511 job->status_buffer = NULL;
512 }
513
514 printer = job->printer;
515
516 update_job_attrs(job);
517
518 if (job->status < 0)
519 {
520 /*
521 * Backend had errors; stop it...
522 */
523
524 int exit_code; /* Exit code from backend */
525
526
527 /*
528 * Convert the status to an exit code. Due to the way the W* macros are
529 * implemented on MacOS X (bug?), we have to store the exit status in a
530 * variable first and then convert...
531 */
532
533 exit_code = -job->status;
534 if (WIFEXITED(exit_code))
535 exit_code = WEXITSTATUS(exit_code);
536 else
537 exit_code = job->status;
538
539 cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Backend returned status %d (%s)",
540 job->id, exit_code,
541 exit_code == CUPS_BACKEND_FAILED ? "failed" :
542 exit_code == CUPS_BACKEND_AUTH_REQUIRED ?
543 "authentication required" :
544 exit_code == CUPS_BACKEND_HOLD ? "hold job" :
545 exit_code == CUPS_BACKEND_STOP ? "stop printer" :
546 exit_code == CUPS_BACKEND_CANCEL ? "cancel job" :
547 exit_code < 0 ? "crashed" : "unknown");
548
549 /*
550 * Do what needs to be done...
551 */
552
553 switch (exit_code)
554 {
555 default :
556 case CUPS_BACKEND_FAILED :
557 /*
558 * Backend failure, use the error-policy to determine how to
559 * act...
560 */
561
562 cupsdStopJob(job, 0);
563
564 if (job->dtype & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT))
565 {
566 /*
567 * Mark the job as pending again - we'll retry on another
568 * printer...
569 */
570
571 job->state->values[0].integer = IPP_JOB_PENDING;
572 job->state_value = IPP_JOB_PENDING;
573 }
574
575 cupsdSaveJob(job);
576
577 /*
578 * If the job was queued to a class, try requeuing it... For
579 * faxes and retry-job queues, hold the current job for 5 minutes.
580 */
581
582 if (job->dtype & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT))
583 cupsdCheckJobs();
584 else if ((printer->type & CUPS_PRINTER_FAX) ||
585 !strcmp(printer->error_policy, "retry-job"))
586 {
587 /*
588 * See how many times we've tried to send the job; if more than
589 * the limit, cancel the job.
590 */
591
592 job->tries ++;
593
594 if (job->tries >= JobRetryLimit)
595 {
596 /*
597 * Too many tries...
598 */
599
600 cupsdLogMessage(CUPSD_LOG_ERROR,
601 "[Job %d] Canceling job since it could not be "
602 "sent after %d tries.",
603 job->id, JobRetryLimit);
604
605 cupsdCancelJob(job, 0, IPP_JOB_ABORTED);
606 }
607 else
608 {
609 /*
610 * Try again in N seconds...
611 */
612
613 set_hold_until(job, time(NULL) + JobRetryInterval);
614
615 cupsdAddEvent(CUPSD_EVENT_JOB_STATE, job->printer, job,
616 "Job held due to fax errors; please consult "
617 "the error_log file for details.");
618 cupsdSetPrinterState(printer, IPP_PRINTER_IDLE, 0);
619 }
620 }
621 else if (!strcmp(printer->error_policy, "abort-job"))
622 cupsdCancelJob(job, 0, IPP_JOB_ABORTED);
623 else
624 {
625 cupsdSetPrinterState(printer, IPP_PRINTER_STOPPED, 1);
626
627 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED, printer, job,
628 "Job stopped due to backend errors; please consult "
629 "the error_log file for details.");
630 }
631 break;
632
633 case CUPS_BACKEND_CANCEL :
634 /*
635 * Cancel the job...
636 */
637
638 cupsdCancelJob(job, 0, IPP_JOB_CANCELED);
639 break;
640
641 case CUPS_BACKEND_HOLD :
642 /*
643 * Hold the job...
644 */
645
646 cupsdStopJob(job, 0);
647
648 cupsdSetJobHoldUntil(job, "indefinite");
649
650 job->state->values[0].integer = IPP_JOB_HELD;
651 job->state_value = IPP_JOB_HELD;
652
653 cupsdSaveJob(job);
654
655 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED, printer, job,
656 "Job held due to backend errors; please consult "
657 "the error_log file for details.");
658 break;
659
660 case CUPS_BACKEND_STOP :
661 /*
662 * Stop the printer...
663 */
664
665 cupsdStopJob(job, 0);
666
667 job->state->values[0].integer = IPP_JOB_PENDING;
668 job->state_value = IPP_JOB_PENDING;
669
670 cupsdSaveJob(job);
671 cupsdSetPrinterState(printer, IPP_PRINTER_STOPPED, 1);
672
673 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED, printer, job,
674 "Job stopped due to backend errors; please consult "
675 "the error_log file for details.");
676 break;
677
678 case CUPS_BACKEND_AUTH_REQUIRED :
679 cupsdStopJob(job, 0);
680
681 cupsdSetJobHoldUntil(job, "auth-info-required");
682
683 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
684 IPP_TAG_KEYWORD)) == NULL)
685 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
686
687 if (attr)
688 {
689 attr->value_tag = IPP_TAG_KEYWORD;
690 cupsdSetString(&(attr->values[0].string.text),
691 "auth-info-required");
692 }
693
694 job->state->values[0].integer = IPP_JOB_HELD;
695 job->state_value = IPP_JOB_HELD;
696
697 cupsdSaveJob(job);
698
699 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED, printer, job,
700 "Authentication is required for job %d.", job->id);
701 break;
702 }
703
704 /*
705 * Try printing another job...
706 */
707
708 cupsdCheckJobs();
709 }
710 else if (job->status > 0)
711 {
712 /*
713 * Filter had errors; stop job...
714 */
715
716 cupsdStopJob(job, 1);
717 cupsdSaveJob(job);
718 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED, printer, job,
719 "Job stopped due to filter errors; please consult the "
720 "error_log file for details.");
721 cupsdCheckJobs();
722 }
723 else
724 {
725 /*
726 * Job printed successfully; cancel it...
727 */
728
729 if (job->current_file < job->num_files)
730 {
731 /*
732 * Start the next file in the job...
733 */
734
735 FilterLevel -= job->cost;
736 start_job(job, printer);
737 }
738 else
739 {
740 /*
741 * Close out this job...
742 */
743
744 cupsdCancelJob(job, 0, IPP_JOB_COMPLETED);
745 cupsdCheckJobs();
746 }
747 }
748 }
749
750
751 /*
752 * 'cupsdFreeAllJobs()' - Free all jobs from memory.
753 */
754
755 void
756 cupsdFreeAllJobs(void)
757 {
758 cupsd_job_t *job; /* Current job */
759
760
761 if (!Jobs)
762 return;
763
764 cupsdHoldSignals();
765
766 cupsdStopAllJobs(1);
767 cupsdSaveAllJobs();
768
769 for (job = (cupsd_job_t *)cupsArrayFirst(Jobs);
770 job;
771 job = (cupsd_job_t *)cupsArrayNext(Jobs))
772 {
773 cupsArrayRemove(Jobs, job);
774 cupsArrayRemove(ActiveJobs, job);
775
776 free_job(job);
777 }
778
779 cupsdReleaseSignals();
780 }
781
782
783 /*
784 * 'cupsdFindJob()' - Find the specified job.
785 */
786
787 cupsd_job_t * /* O - Job data */
788 cupsdFindJob(int id) /* I - Job ID */
789 {
790 cupsd_job_t key; /* Search key */
791
792
793 key.id = id;
794
795 return ((cupsd_job_t *)cupsArrayFind(Jobs, &key));
796 }
797
798
799 /*
800 * 'cupsdGetPrinterJobCount()' - Get the number of pending, processing,
801 * or held jobs in a printer or class.
802 */
803
804 int /* O - Job count */
805 cupsdGetPrinterJobCount(
806 const char *dest) /* I - Printer or class name */
807 {
808 int count; /* Job count */
809 cupsd_job_t *job; /* Current job */
810
811
812 for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs), count = 0;
813 job;
814 job = (cupsd_job_t *)cupsArrayNext(ActiveJobs))
815 if (job->dest && !strcasecmp(job->dest, dest))
816 count ++;
817
818 return (count);
819 }
820
821
822 /*
823 * 'cupsdGetUserJobCount()' - Get the number of pending, processing,
824 * or held jobs for a user.
825 */
826
827 int /* O - Job count */
828 cupsdGetUserJobCount(
829 const char *username) /* I - Username */
830 {
831 int count; /* Job count */
832 cupsd_job_t *job; /* Current job */
833
834
835 for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs), count = 0;
836 job;
837 job = (cupsd_job_t *)cupsArrayNext(ActiveJobs))
838 if (!strcasecmp(job->username, username))
839 count ++;
840
841 return (count);
842 }
843
844
845 /*
846 * 'cupsdHoldJob()' - Hold the specified job.
847 */
848
849 void
850 cupsdHoldJob(cupsd_job_t *job) /* I - Job data */
851 {
852 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdHoldJob: id = %d", job->id);
853
854 if (job->state_value == IPP_JOB_PROCESSING)
855 cupsdStopJob(job, 0);
856 else
857 cupsdLoadJob(job);
858
859 DEBUG_puts("cupsdHoldJob: setting state to held...");
860
861 job->state->values[0].integer = IPP_JOB_HELD;
862 job->state_value = IPP_JOB_HELD;
863
864 cupsdSaveJob(job);
865
866 cupsdCheckJobs();
867 }
868
869
870 /*
871 * 'cupsdLoadAllJobs()' - Load all jobs from disk.
872 */
873
874 void
875 cupsdLoadAllJobs(void)
876 {
877 char filename[1024]; /* Full filename of job.cache file */
878 struct stat fileinfo, /* Information on job.cache file */
879 dirinfo; /* Information on RequestRoot dir */
880
881
882
883 /*
884 * Create the job arrays as needed...
885 */
886
887 if (!Jobs)
888 Jobs = cupsArrayNew(compare_jobs, NULL);
889
890 if (!ActiveJobs)
891 ActiveJobs = cupsArrayNew(compare_active_jobs, NULL);
892
893 /*
894 * See whether the job.cache file is older than the RequestRoot directory...
895 */
896
897 snprintf(filename, sizeof(filename), "%s/job.cache", CacheDir);
898
899 if (stat(filename, &fileinfo))
900 {
901 fileinfo.st_mtime = 0;
902
903 if (errno != ENOENT)
904 cupsdLogMessage(CUPSD_LOG_ERROR,
905 "Unable to get file information for \"%s\" - %s",
906 filename, strerror(errno));
907 }
908
909 if (stat(RequestRoot, &dirinfo))
910 {
911 dirinfo.st_mtime = 0;
912
913 if (errno != ENOENT)
914 cupsdLogMessage(CUPSD_LOG_ERROR,
915 "Unable to get directory information for \"%s\" - %s",
916 RequestRoot, strerror(errno));
917 }
918
919 /*
920 * Load the most recent source for job data...
921 */
922
923 if (dirinfo.st_mtime > fileinfo.st_mtime)
924 {
925 load_request_root();
926
927 load_next_job_id(filename);
928 }
929 else
930 load_job_cache(filename);
931
932 /*
933 * Clean out old jobs as needed...
934 */
935
936 if (MaxJobs > 0 && cupsArrayCount(Jobs) >= MaxJobs)
937 cupsdCleanJobs();
938 }
939
940
941 /*
942 * 'cupsdLoadJob()' - Load a single job...
943 */
944
945 void
946 cupsdLoadJob(cupsd_job_t *job) /* I - Job */
947 {
948 char jobfile[1024]; /* Job filename */
949 cups_file_t *fp; /* Job file */
950 int fileid; /* Current file ID */
951 ipp_attribute_t *attr; /* Job attribute */
952 const char *dest; /* Destination name */
953 cupsd_printer_t *destptr; /* Pointer to destination */
954 mime_type_t **filetypes; /* New filetypes array */
955 int *compressions; /* New compressions array */
956
957
958 if (job->attrs)
959 {
960 if (job->state_value > IPP_JOB_STOPPED)
961 job->access_time = time(NULL);
962
963 return;
964 }
965
966 if ((job->attrs = ippNew()) == NULL)
967 {
968 cupsdLogMessage(CUPSD_LOG_ERROR,
969 "[Job %d] Ran out of memory for job attributes!", job->id);
970 return;
971 }
972
973 /*
974 * Load job attributes...
975 */
976
977 cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] Loading attributes...", job->id);
978
979 snprintf(jobfile, sizeof(jobfile), "%s/c%05d", RequestRoot, job->id);
980 if ((fp = cupsFileOpen(jobfile, "r")) == NULL)
981 {
982 cupsdLogMessage(CUPSD_LOG_ERROR,
983 "[Job %d] Unable to open job control file \"%s\" - %s!",
984 job->id, jobfile, strerror(errno));
985 ippDelete(job->attrs);
986 job->attrs = NULL;
987 return;
988 }
989
990 if (ippReadIO(fp, (ipp_iocb_t)cupsFileRead, 1, NULL, job->attrs) != IPP_DATA)
991 {
992 cupsdLogMessage(CUPSD_LOG_ERROR,
993 "[Job %d] Unable to read job control file \"%s\"!",
994 job->id, jobfile);
995 cupsFileClose(fp);
996 ippDelete(job->attrs);
997 job->attrs = NULL;
998 unlink(jobfile);
999 return;
1000 }
1001
1002 cupsFileClose(fp);
1003
1004 /*
1005 * Copy attribute data to the job object...
1006 */
1007
1008 if ((job->state = ippFindAttribute(job->attrs, "job-state",
1009 IPP_TAG_ENUM)) == NULL)
1010 {
1011 cupsdLogMessage(CUPSD_LOG_ERROR,
1012 "[Job %d] Missing or bad job-state attribute in "
1013 "control file!",
1014 job->id);
1015 ippDelete(job->attrs);
1016 job->attrs = NULL;
1017 unlink(jobfile);
1018 return;
1019 }
1020
1021 job->state_value = (ipp_jstate_t)job->state->values[0].integer;
1022
1023 if (!job->dest)
1024 {
1025 if ((attr = ippFindAttribute(job->attrs, "job-printer-uri",
1026 IPP_TAG_URI)) == NULL)
1027 {
1028 cupsdLogMessage(CUPSD_LOG_ERROR,
1029 "[Job %d] No job-printer-uri attribute in control file!",
1030 job->id);
1031 ippDelete(job->attrs);
1032 job->attrs = NULL;
1033 unlink(jobfile);
1034 return;
1035 }
1036
1037 if ((dest = cupsdValidateDest(attr->values[0].string.text, &(job->dtype),
1038 &destptr)) == NULL)
1039 {
1040 cupsdLogMessage(CUPSD_LOG_ERROR,
1041 "[Job %d] Unable to queue job for destination \"%s\"!",
1042 job->id, attr->values[0].string.text);
1043 ippDelete(job->attrs);
1044 job->attrs = NULL;
1045 unlink(jobfile);
1046 return;
1047 }
1048
1049 cupsdSetString(&job->dest, dest);
1050 }
1051 else if ((destptr = cupsdFindDest(job->dest)) == NULL)
1052 {
1053 cupsdLogMessage(CUPSD_LOG_ERROR,
1054 "[Job %d] Unable to queue job for destination \"%s\"!",
1055 job->id, job->dest);
1056 ippDelete(job->attrs);
1057 job->attrs = NULL;
1058 unlink(jobfile);
1059 return;
1060 }
1061
1062 job->sheets = ippFindAttribute(job->attrs, "job-media-sheets-completed",
1063 IPP_TAG_INTEGER);
1064 job->job_sheets = ippFindAttribute(job->attrs, "job-sheets", IPP_TAG_NAME);
1065
1066 if (!job->priority)
1067 {
1068 if ((attr = ippFindAttribute(job->attrs, "job-priority",
1069 IPP_TAG_INTEGER)) == NULL)
1070 {
1071 cupsdLogMessage(CUPSD_LOG_ERROR,
1072 "[Job %d] Missing or bad job-priority attribute in "
1073 "control file!", job->id);
1074 ippDelete(job->attrs);
1075 job->attrs = NULL;
1076 unlink(jobfile);
1077 return;
1078 }
1079
1080 job->priority = attr->values[0].integer;
1081 }
1082
1083 if (!job->username)
1084 {
1085 if ((attr = ippFindAttribute(job->attrs, "job-originating-user-name",
1086 IPP_TAG_NAME)) == NULL)
1087 {
1088 cupsdLogMessage(CUPSD_LOG_ERROR,
1089 "[Job %d] Missing or bad job-originating-user-name "
1090 "attribute in control file!", job->id);
1091 ippDelete(job->attrs);
1092 job->attrs = NULL;
1093 unlink(jobfile);
1094 return;
1095 }
1096
1097 cupsdSetString(&job->username, attr->values[0].string.text);
1098 }
1099
1100 /*
1101 * Set the job hold-until time and state...
1102 */
1103
1104 if (job->state_value == IPP_JOB_HELD)
1105 {
1106 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
1107 IPP_TAG_KEYWORD)) == NULL)
1108 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
1109
1110 if (attr)
1111 cupsdSetJobHoldUntil(job, attr->values[0].string.text);
1112 else
1113 {
1114 job->state->values[0].integer = IPP_JOB_PENDING;
1115 job->state_value = IPP_JOB_PENDING;
1116 }
1117 }
1118 else if (job->state_value == IPP_JOB_PROCESSING)
1119 {
1120 job->state->values[0].integer = IPP_JOB_PENDING;
1121 job->state_value = IPP_JOB_PENDING;
1122 }
1123
1124 if (!job->num_files)
1125 {
1126 /*
1127 * Find all the d##### files...
1128 */
1129
1130 for (fileid = 1; fileid < 10000; fileid ++)
1131 {
1132 snprintf(jobfile, sizeof(jobfile), "%s/d%05d-%03d", RequestRoot,
1133 job->id, fileid);
1134
1135 if (access(jobfile, 0))
1136 break;
1137
1138 cupsdLogMessage(CUPSD_LOG_DEBUG,
1139 "[Job %d] Auto-typing document file \"%s\"...",
1140 job->id, jobfile);
1141
1142 if (fileid > job->num_files)
1143 {
1144 if (job->num_files == 0)
1145 {
1146 compressions = (int *)calloc(fileid, sizeof(int));
1147 filetypes = (mime_type_t **)calloc(fileid, sizeof(mime_type_t *));
1148 }
1149 else
1150 {
1151 compressions = (int *)realloc(job->compressions,
1152 sizeof(int) * fileid);
1153 filetypes = (mime_type_t **)realloc(job->filetypes,
1154 sizeof(mime_type_t *) *
1155 fileid);
1156 }
1157
1158 if (!compressions || !filetypes)
1159 {
1160 cupsdLogMessage(CUPSD_LOG_ERROR,
1161 "[Job %d] Ran out of memory for job file types!",
1162 job->id);
1163 return;
1164 }
1165
1166 job->compressions = compressions;
1167 job->filetypes = filetypes;
1168 job->num_files = fileid;
1169 }
1170
1171 job->filetypes[fileid - 1] = mimeFileType(MimeDatabase, jobfile, NULL,
1172 job->compressions + fileid - 1);
1173
1174 if (!job->filetypes[fileid - 1])
1175 job->filetypes[fileid - 1] = mimeType(MimeDatabase, "application",
1176 "vnd.cups-raw");
1177 }
1178 }
1179
1180 /*
1181 * Load authentication information as needed...
1182 */
1183
1184 if (job->state_value < IPP_JOB_STOPPED)
1185 {
1186 snprintf(jobfile, sizeof(jobfile), "%s/a%05d", RequestRoot, job->id);
1187
1188 cupsdClearString(&job->auth_username);
1189 cupsdClearString(&job->auth_domain);
1190 cupsdClearString(&job->auth_password);
1191
1192 if ((fp = cupsFileOpen(jobfile, "r")) != NULL)
1193 {
1194 int i, /* Looping var */
1195 bytes; /* Size of auth data */
1196 char line[255], /* Line from file */
1197 data[255]; /* Decoded data */
1198
1199
1200 for (i = 0;
1201 i < destptr->num_auth_info_required &&
1202 cupsFileGets(fp, line, sizeof(line));
1203 i ++)
1204 {
1205 bytes = sizeof(data);
1206 httpDecode64_2(data, &bytes, line);
1207
1208 if (!strcmp(destptr->auth_info_required[i], "username"))
1209 cupsdSetStringf(&job->auth_username, "AUTH_USERNAME=%s", data);
1210 else if (!strcmp(destptr->auth_info_required[i], "domain"))
1211 cupsdSetStringf(&job->auth_domain, "AUTH_DOMAIN=%s", data);
1212 else if (!strcmp(destptr->auth_info_required[i], "password"))
1213 cupsdSetStringf(&job->auth_password, "AUTH_PASSWORD=%s", data);
1214 }
1215
1216 cupsFileClose(fp);
1217 }
1218 }
1219 job->access_time = time(NULL);
1220 }
1221
1222
1223 /*
1224 * 'cupsdMoveJob()' - Move the specified job to a different destination.
1225 */
1226
1227 void
1228 cupsdMoveJob(cupsd_job_t *job, /* I - Job */
1229 cupsd_printer_t *p) /* I - Destination printer or class */
1230 {
1231 ipp_attribute_t *attr; /* job-printer-uri attribute */
1232 const char *olddest; /* Old destination */
1233 cupsd_printer_t *oldp; /* Old pointer */
1234
1235
1236 /*
1237 * Don't move completed jobs...
1238 */
1239
1240 if (job->state_value > IPP_JOB_STOPPED)
1241 return;
1242
1243 /*
1244 * Get the old destination...
1245 */
1246
1247 olddest = job->dest;
1248
1249 if (job->printer)
1250 oldp = job->printer;
1251 else
1252 oldp = cupsdFindDest(olddest);
1253
1254 /*
1255 * Change the destination information...
1256 */
1257
1258 cupsdLoadJob(job);
1259
1260 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED, oldp, job,
1261 "Job #%d moved from %s to %s.", job->id, olddest,
1262 p->name);
1263
1264 cupsdSetString(&job->dest, p->name);
1265 job->dtype = p->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_REMOTE |
1266 CUPS_PRINTER_IMPLICIT);
1267
1268 if ((attr = ippFindAttribute(job->attrs, "job-printer-uri",
1269 IPP_TAG_URI)) != NULL)
1270 cupsdSetString(&(attr->values[0].string.text), p->uri);
1271
1272 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED, p, job,
1273 "Job #%d moved from %s to %s.", job->id, olddest,
1274 p->name);
1275
1276 cupsdSaveJob(job);
1277 }
1278
1279
1280 /*
1281 * 'cupsdReleaseJob()' - Release the specified job.
1282 */
1283
1284 void
1285 cupsdReleaseJob(cupsd_job_t *job) /* I - Job */
1286 {
1287 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdReleaseJob: id = %d", job->id);
1288
1289 if (job->state_value == IPP_JOB_HELD)
1290 {
1291 DEBUG_puts("cupsdReleaseJob: setting state to pending...");
1292
1293 job->state->values[0].integer = IPP_JOB_PENDING;
1294 job->state_value = IPP_JOB_PENDING;
1295 cupsdSaveJob(job);
1296 cupsdCheckJobs();
1297 }
1298 }
1299
1300
1301 /*
1302 * 'cupsdRestartJob()' - Restart the specified job.
1303 */
1304
1305 void
1306 cupsdRestartJob(cupsd_job_t *job) /* I - Job */
1307 {
1308 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdRestartJob: id = %d", job->id);
1309
1310 if (job->state_value == IPP_JOB_STOPPED || job->num_files)
1311 {
1312 ipp_jstate_t old_state; /* Old job state */
1313
1314
1315 cupsdLoadJob(job);
1316
1317 old_state = job->state_value;
1318
1319 job->tries = 0;
1320 job->state->values[0].integer = IPP_JOB_PENDING;
1321 job->state_value = IPP_JOB_PENDING;
1322
1323 cupsdSaveJob(job);
1324
1325 if (old_state > IPP_JOB_STOPPED)
1326 cupsArrayAdd(ActiveJobs, job);
1327
1328 cupsdCheckJobs();
1329 }
1330 }
1331
1332
1333 /*
1334 * 'cupsdSaveAllJobs()' - Save a summary of all jobs to disk.
1335 */
1336
1337 void
1338 cupsdSaveAllJobs(void)
1339 {
1340 int i; /* Looping var */
1341 cups_file_t *fp; /* Job cache file */
1342 char temp[1024]; /* Temporary string */
1343 cupsd_job_t *job; /* Current job */
1344 time_t curtime; /* Current time */
1345 struct tm *curdate; /* Current date */
1346
1347
1348 snprintf(temp, sizeof(temp), "%s/job.cache", CacheDir);
1349 if ((fp = cupsFileOpen(temp, "w")) == NULL)
1350 {
1351 cupsdLogMessage(CUPSD_LOG_ERROR,
1352 "Unable to create job cache file \"%s\" - %s",
1353 temp, strerror(errno));
1354 return;
1355 }
1356
1357 cupsdLogMessage(CUPSD_LOG_INFO, "Saving job cache file \"%s\"...", temp);
1358
1359 /*
1360 * Restrict access to the file...
1361 */
1362
1363 fchown(cupsFileNumber(fp), getuid(), Group);
1364 fchmod(cupsFileNumber(fp), ConfigFilePerm);
1365
1366 /*
1367 * Write a small header to the file...
1368 */
1369
1370 curtime = time(NULL);
1371 curdate = localtime(&curtime);
1372 strftime(temp, sizeof(temp) - 1, "%Y-%m-%d %H:%M", curdate);
1373
1374 cupsFilePuts(fp, "# Job cache file for " CUPS_SVERSION "\n");
1375 cupsFilePrintf(fp, "# Written by cupsd on %s\n", temp);
1376 cupsFilePrintf(fp, "NextJobId %d\n", NextJobId);
1377
1378 /*
1379 * Write each job known to the system...
1380 */
1381
1382 for (job = (cupsd_job_t *)cupsArrayFirst(Jobs);
1383 job;
1384 job = (cupsd_job_t *)cupsArrayNext(Jobs))
1385 {
1386 cupsFilePrintf(fp, "<Job %d>\n", job->id);
1387 cupsFilePrintf(fp, "State %d\n", job->state_value);
1388 cupsFilePrintf(fp, "Priority %d\n", job->priority);
1389 cupsFilePrintf(fp, "Username %s\n", job->username);
1390 cupsFilePrintf(fp, "Destination %s\n", job->dest);
1391 cupsFilePrintf(fp, "DestType %d\n", job->dtype);
1392 cupsFilePrintf(fp, "NumFiles %d\n", job->num_files);
1393 for (i = 0; i < job->num_files; i ++)
1394 cupsFilePrintf(fp, "File %d %s/%s %d\n", i + 1, job->filetypes[i]->super,
1395 job->filetypes[i]->type, job->compressions[i]);
1396 cupsFilePuts(fp, "</Job>\n");
1397 }
1398
1399 cupsFileClose(fp);
1400 }
1401
1402
1403 /*
1404 * 'cupsdSaveJob()' - Save a job to disk.
1405 */
1406
1407 void
1408 cupsdSaveJob(cupsd_job_t *job) /* I - Job */
1409 {
1410 char filename[1024]; /* Job control filename */
1411 cups_file_t *fp; /* Job file */
1412
1413
1414 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdSaveJob(job=%p(%d)): job->attrs=%p",
1415 job, job->id, job->attrs);
1416
1417 snprintf(filename, sizeof(filename), "%s/c%05d", RequestRoot, job->id);
1418
1419 if ((fp = cupsFileOpen(filename, "w")) == NULL)
1420 {
1421 cupsdLogMessage(CUPSD_LOG_ERROR,
1422 "[Job %d] Unable to create job control file \"%s\" - %s.",
1423 job->id, filename, strerror(errno));
1424 return;
1425 }
1426
1427 fchmod(cupsFileNumber(fp), 0600);
1428 fchown(cupsFileNumber(fp), RunUser, Group);
1429
1430 job->attrs->state = IPP_IDLE;
1431
1432 if (ippWriteIO(fp, (ipp_iocb_t)cupsFileWrite, 1, NULL,
1433 job->attrs) != IPP_DATA)
1434 cupsdLogMessage(CUPSD_LOG_ERROR,
1435 "[Job %d] Unable to write job control file!", job->id);
1436
1437 cupsFileClose(fp);
1438 }
1439
1440
1441 /*
1442 * 'cupsdSetJobHoldUntil()' - Set the hold time for a job...
1443 */
1444
1445 void
1446 cupsdSetJobHoldUntil(cupsd_job_t *job, /* I - Job */
1447 const char *when) /* I - When to resume */
1448 {
1449 time_t curtime; /* Current time */
1450 struct tm *curdate; /* Current date */
1451 int hour; /* Hold hour */
1452 int minute; /* Hold minute */
1453 int second; /* Hold second */
1454
1455
1456 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdSetJobHoldUntil(%d, \"%s\")",
1457 job->id, when);
1458
1459 second = 0;
1460
1461 if (!strcmp(when, "indefinite") || !strcmp(when, "auth-info-required"))
1462 {
1463 /*
1464 * Hold indefinitely...
1465 */
1466
1467 job->hold_until = 0;
1468 }
1469 else if (!strcmp(when, "day-time"))
1470 {
1471 /*
1472 * Hold to 6am the next morning unless local time is < 6pm.
1473 */
1474
1475 curtime = time(NULL);
1476 curdate = localtime(&curtime);
1477
1478 if (curdate->tm_hour < 18)
1479 job->hold_until = curtime;
1480 else
1481 job->hold_until = curtime +
1482 ((29 - curdate->tm_hour) * 60 + 59 -
1483 curdate->tm_min) * 60 + 60 - curdate->tm_sec;
1484 }
1485 else if (!strcmp(when, "evening") || !strcmp(when, "night"))
1486 {
1487 /*
1488 * Hold to 6pm unless local time is > 6pm or < 6am.
1489 */
1490
1491 curtime = time(NULL);
1492 curdate = localtime(&curtime);
1493
1494 if (curdate->tm_hour < 6 || curdate->tm_hour >= 18)
1495 job->hold_until = curtime;
1496 else
1497 job->hold_until = curtime +
1498 ((17 - curdate->tm_hour) * 60 + 59 -
1499 curdate->tm_min) * 60 + 60 - curdate->tm_sec;
1500 }
1501 else if (!strcmp(when, "second-shift"))
1502 {
1503 /*
1504 * Hold to 4pm unless local time is > 4pm.
1505 */
1506
1507 curtime = time(NULL);
1508 curdate = localtime(&curtime);
1509
1510 if (curdate->tm_hour >= 16)
1511 job->hold_until = curtime;
1512 else
1513 job->hold_until = curtime +
1514 ((15 - curdate->tm_hour) * 60 + 59 -
1515 curdate->tm_min) * 60 + 60 - curdate->tm_sec;
1516 }
1517 else if (!strcmp(when, "third-shift"))
1518 {
1519 /*
1520 * Hold to 12am unless local time is < 8am.
1521 */
1522
1523 curtime = time(NULL);
1524 curdate = localtime(&curtime);
1525
1526 if (curdate->tm_hour < 8)
1527 job->hold_until = curtime;
1528 else
1529 job->hold_until = curtime +
1530 ((23 - curdate->tm_hour) * 60 + 59 -
1531 curdate->tm_min) * 60 + 60 - curdate->tm_sec;
1532 }
1533 else if (!strcmp(when, "weekend"))
1534 {
1535 /*
1536 * Hold to weekend unless we are in the weekend.
1537 */
1538
1539 curtime = time(NULL);
1540 curdate = localtime(&curtime);
1541
1542 if (curdate->tm_wday || curdate->tm_wday == 6)
1543 job->hold_until = curtime;
1544 else
1545 job->hold_until = curtime +
1546 (((5 - curdate->tm_wday) * 24 +
1547 (17 - curdate->tm_hour)) * 60 + 59 -
1548 curdate->tm_min) * 60 + 60 - curdate->tm_sec;
1549 }
1550 else if (sscanf(when, "%d:%d:%d", &hour, &minute, &second) >= 2)
1551 {
1552 /*
1553 * Hold to specified GMT time (HH:MM or HH:MM:SS)...
1554 */
1555
1556 curtime = time(NULL);
1557 curdate = gmtime(&curtime);
1558
1559 job->hold_until = curtime +
1560 ((hour - curdate->tm_hour) * 60 + minute -
1561 curdate->tm_min) * 60 + second - curdate->tm_sec;
1562
1563 /*
1564 * Hold until next day as needed...
1565 */
1566
1567 if (job->hold_until < curtime)
1568 job->hold_until += 24 * 60 * 60;
1569 }
1570
1571 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdSetJobHoldUntil: hold_until = %d",
1572 (int)job->hold_until);
1573 }
1574
1575
1576 /*
1577 * 'cupsdSetJobPriority()' - Set the priority of a job, moving it up/down in
1578 * the list as needed.
1579 */
1580
1581 void
1582 cupsdSetJobPriority(
1583 cupsd_job_t *job, /* I - Job ID */
1584 int priority) /* I - New priority (0 to 100) */
1585 {
1586 ipp_attribute_t *attr; /* Job attribute */
1587
1588
1589 /*
1590 * Don't change completed jobs...
1591 */
1592
1593 if (job->state_value >= IPP_JOB_PROCESSING)
1594 return;
1595
1596 /*
1597 * Set the new priority and re-add the job into the active list...
1598 */
1599
1600 cupsArrayRemove(ActiveJobs, job);
1601
1602 job->priority = priority;
1603
1604 if ((attr = ippFindAttribute(job->attrs, "job-priority",
1605 IPP_TAG_INTEGER)) != NULL)
1606 attr->values[0].integer = priority;
1607 else
1608 ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-priority",
1609 priority);
1610
1611 cupsArrayAdd(ActiveJobs, job);
1612
1613 cupsdSaveJob(job);
1614 }
1615
1616
1617 /*
1618 * 'cupsdStopAllJobs()' - Stop all print jobs.
1619 */
1620
1621 void
1622 cupsdStopAllJobs(int force) /* I - 1 = Force all filters to stop */
1623 {
1624 cupsd_job_t *job; /* Current job */
1625
1626
1627 DEBUG_puts("cupsdStopAllJobs()");
1628
1629 for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs);
1630 job;
1631 job = (cupsd_job_t *)cupsArrayNext(ActiveJobs))
1632 if (job->state_value == IPP_JOB_PROCESSING)
1633 {
1634 cupsdStopJob(job, force);
1635 job->state->values[0].integer = IPP_JOB_PENDING;
1636 job->state_value = IPP_JOB_PENDING;
1637 }
1638 }
1639
1640
1641 /*
1642 * 'cupsdStopJob()' - Stop a print job.
1643 */
1644
1645 void
1646 cupsdStopJob(cupsd_job_t *job, /* I - Job */
1647 int force) /* I - 1 = Force all filters to stop */
1648 {
1649 int i; /* Looping var */
1650
1651
1652 cupsdLogMessage(CUPSD_LOG_DEBUG2,
1653 "[Job %d] cupsdStopJob: force = %d", job->id, force);
1654
1655 if (job->state_value != IPP_JOB_PROCESSING)
1656 return;
1657
1658 FilterLevel -= job->cost;
1659
1660 if (job->printer->state == IPP_PRINTER_PROCESSING)
1661 cupsdSetPrinterState(job->printer, IPP_PRINTER_IDLE, 0);
1662
1663 job->state->values[0].integer = IPP_JOB_STOPPED;
1664 job->state_value = IPP_JOB_STOPPED;
1665 job->printer->job = NULL;
1666 job->printer = NULL;
1667
1668 job->current_file --;
1669
1670 for (i = 0; job->filters[i]; i ++)
1671 if (job->filters[i] > 0)
1672 {
1673 cupsdEndProcess(job->filters[i], force);
1674 job->filters[i] = 0;
1675 }
1676
1677 if (job->backend > 0)
1678 {
1679 cupsdEndProcess(job->backend, force);
1680 job->backend = 0;
1681 }
1682
1683 cupsdLogMessage(CUPSD_LOG_DEBUG2, "[Job %d] Closing print pipes [ %d %d ]...",
1684 job->id, job->print_pipes[0], job->print_pipes[1]);
1685
1686 cupsdClosePipe(job->print_pipes);
1687
1688 cupsdLogMessage(CUPSD_LOG_DEBUG2, "[Job %d] Closing back pipes [ %d %d ]...",
1689 job->id, job->back_pipes[0], job->back_pipes[1]);
1690
1691 cupsdClosePipe(job->back_pipes);
1692
1693 cupsdLogMessage(CUPSD_LOG_DEBUG2, "[Job %d] Closing side pipes [ %d %d ]...",
1694 job->id, job->side_pipes[0], job->side_pipes[1]);
1695
1696 cupsdClosePipe(job->side_pipes);
1697
1698 if (job->status_buffer)
1699 {
1700 /*
1701 * Close the pipe and clear the input bit.
1702 */
1703
1704 cupsdRemoveSelect(job->status_buffer->fd);
1705
1706 cupsdLogMessage(CUPSD_LOG_DEBUG2,
1707 "[Job %d] Closing status pipes [ %d %d ]...", job->id,
1708 job->status_pipes[0], job->status_pipes[1]);
1709
1710 cupsdClosePipe(job->status_pipes);
1711 cupsdStatBufDelete(job->status_buffer);
1712
1713 job->status_buffer = NULL;
1714 }
1715 }
1716
1717
1718 /*
1719 * 'cupsdUnloadCompletedJobs()' - Flush completed job history from memory.
1720 */
1721
1722 void
1723 cupsdUnloadCompletedJobs(void)
1724 {
1725 cupsd_job_t *job; /* Current job */
1726 time_t expire; /* Expiration time */
1727
1728
1729 expire = time(NULL) - 60;
1730
1731 for (job = (cupsd_job_t *)cupsArrayFirst(Jobs);
1732 job;
1733 job = (cupsd_job_t *)cupsArrayNext(Jobs))
1734 if (job->attrs && job->state_value >= IPP_JOB_STOPPED &&
1735 job->access_time < expire)
1736 unload_job(job);
1737 }
1738
1739
1740 /*
1741 * 'compare_active_jobs()' - Compare the job IDs and priorities of two jobs.
1742 */
1743
1744 static int /* O - Difference */
1745 compare_active_jobs(void *first, /* I - First job */
1746 void *second, /* I - Second job */
1747 void *data) /* I - App data (not used) */
1748 {
1749 int diff; /* Difference */
1750
1751
1752 if ((diff = ((cupsd_job_t *)second)->priority -
1753 ((cupsd_job_t *)first)->priority) != 0)
1754 return (diff);
1755 else
1756 return (((cupsd_job_t *)first)->id - ((cupsd_job_t *)second)->id);
1757 }
1758
1759
1760 /*
1761 * 'compare_jobs()' - Compare the job IDs of two jobs.
1762 */
1763
1764 static int /* O - Difference */
1765 compare_jobs(void *first, /* I - First job */
1766 void *second, /* I - Second job */
1767 void *data) /* I - App data (not used) */
1768 {
1769 return (((cupsd_job_t *)first)->id - ((cupsd_job_t *)second)->id);
1770 }
1771
1772
1773 /*
1774 * 'free_job()' - Free all memory used by a job.
1775 */
1776
1777 static void
1778 free_job(cupsd_job_t *job) /* I - Job */
1779 {
1780 cupsdClearString(&job->username);
1781 cupsdClearString(&job->dest);
1782 cupsdClearString(&job->auth_username);
1783 cupsdClearString(&job->auth_domain);
1784 cupsdClearString(&job->auth_password);
1785 #ifdef HAVE_GSSAPI
1786 if (job->ccname)
1787 {
1788 /*
1789 * First erase the credential cache file, then clear the string referencing
1790 * it. We know the filename since the string will be of the form
1791 * "KRB5CCNAME=FILE:/foo/bar"...
1792 */
1793
1794 if (cupsdRemoveFile(job->ccname + 16))
1795 cupsdLogMessage(CUPSD_LOG_ERROR,
1796 "Unable to remove Kerberos credential cache: %s",
1797 strerror(errno));
1798
1799 cupsdClearString(&job->ccname);
1800 }
1801 #endif /* HAVE_GSSAPI */
1802
1803 if (job->num_files > 0)
1804 {
1805 free(job->compressions);
1806 free(job->filetypes);
1807 }
1808
1809 ippDelete(job->attrs);
1810
1811 free(job);
1812 }
1813
1814
1815 /*
1816 * 'ipp_length()' - Compute the size of the buffer needed to hold
1817 * the textual IPP attributes.
1818 */
1819
1820 static int /* O - Size of attribute buffer */
1821 ipp_length(ipp_t *ipp) /* I - IPP request */
1822 {
1823 int bytes; /* Number of bytes */
1824 int i; /* Looping var */
1825 ipp_attribute_t *attr; /* Current attribute */
1826
1827
1828 /*
1829 * Loop through all attributes...
1830 */
1831
1832 bytes = 0;
1833
1834 for (attr = ipp->attrs; attr != NULL; attr = attr->next)
1835 {
1836 /*
1837 * Skip attributes that won't be sent to filters...
1838 */
1839
1840 if (attr->value_tag == IPP_TAG_MIMETYPE ||
1841 attr->value_tag == IPP_TAG_NAMELANG ||
1842 attr->value_tag == IPP_TAG_TEXTLANG ||
1843 attr->value_tag == IPP_TAG_URI ||
1844 attr->value_tag == IPP_TAG_URISCHEME)
1845 continue;
1846
1847 if (strncmp(attr->name, "time-", 5) == 0)
1848 continue;
1849
1850 /*
1851 * Add space for a leading space and commas between each value.
1852 * For the first attribute, the leading space isn't used, so the
1853 * extra byte can be used as the nul terminator...
1854 */
1855
1856 bytes ++; /* " " separator */
1857 bytes += attr->num_values; /* "," separators */
1858
1859 /*
1860 * Boolean attributes appear as "foo,nofoo,foo,nofoo", while
1861 * other attributes appear as "foo=value1,value2,...,valueN".
1862 */
1863
1864 if (attr->value_tag != IPP_TAG_BOOLEAN)
1865 bytes += strlen(attr->name);
1866 else
1867 bytes += attr->num_values * strlen(attr->name);
1868
1869 /*
1870 * Now add the size required for each value in the attribute...
1871 */
1872
1873 switch (attr->value_tag)
1874 {
1875 case IPP_TAG_INTEGER :
1876 case IPP_TAG_ENUM :
1877 /*
1878 * Minimum value of a signed integer is -2147483647, or 11 digits.
1879 */
1880
1881 bytes += attr->num_values * 11;
1882 break;
1883
1884 case IPP_TAG_BOOLEAN :
1885 /*
1886 * Add two bytes for each false ("no") value...
1887 */
1888
1889 for (i = 0; i < attr->num_values; i ++)
1890 if (!attr->values[i].boolean)
1891 bytes += 2;
1892 break;
1893
1894 case IPP_TAG_RANGE :
1895 /*
1896 * A range is two signed integers separated by a hyphen, or
1897 * 23 characters max.
1898 */
1899
1900 bytes += attr->num_values * 23;
1901 break;
1902
1903 case IPP_TAG_RESOLUTION :
1904 /*
1905 * A resolution is two signed integers separated by an "x" and
1906 * suffixed by the units, or 26 characters max.
1907 */
1908
1909 bytes += attr->num_values * 26;
1910 break;
1911
1912 case IPP_TAG_STRING :
1913 case IPP_TAG_TEXT :
1914 case IPP_TAG_NAME :
1915 case IPP_TAG_KEYWORD :
1916 case IPP_TAG_CHARSET :
1917 case IPP_TAG_LANGUAGE :
1918 case IPP_TAG_URI :
1919 /*
1920 * Strings can contain characters that need quoting. We need
1921 * at least 2 * len + 2 characters to cover the quotes and
1922 * any backslashes in the string.
1923 */
1924
1925 for (i = 0; i < attr->num_values; i ++)
1926 bytes += 2 * strlen(attr->values[i].string.text) + 2;
1927 break;
1928
1929 default :
1930 break; /* anti-compiler-warning-code */
1931 }
1932 }
1933
1934 return (bytes);
1935 }
1936
1937
1938 /*
1939 * 'load_job_cache()' - Load jobs from the job.cache file.
1940 */
1941
1942 static void
1943 load_job_cache(const char *filename) /* I - job.cache filename */
1944 {
1945 cups_file_t *fp; /* job.cache file */
1946 char line[1024], /* Line buffer */
1947 *value; /* Value on line */
1948 int linenum; /* Line number in file */
1949 cupsd_job_t *job; /* Current job */
1950 int jobid; /* Job ID */
1951 char jobfile[1024]; /* Job filename */
1952
1953
1954 /*
1955 * Open the job.cache file...
1956 */
1957
1958 if ((fp = cupsFileOpen(filename, "r")) == NULL)
1959 {
1960 if (errno != ENOENT)
1961 cupsdLogMessage(CUPSD_LOG_ERROR,
1962 "Unable to open job cache file \"%s\": %s",
1963 filename, strerror(errno));
1964
1965 load_request_root();
1966
1967 return;
1968 }
1969
1970 /*
1971 * Read entries from the job cache file and create jobs as needed.
1972 */
1973
1974 cupsdLogMessage(CUPSD_LOG_INFO, "Loading job cache file \"%s\"...",
1975 filename);
1976
1977 linenum = 0;
1978 job = NULL;
1979
1980 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
1981 {
1982 if (!strcasecmp(line, "NextJobId"))
1983 {
1984 if (value)
1985 NextJobId = atoi(value);
1986 }
1987 else if (!strcasecmp(line, "<Job"))
1988 {
1989 if (job)
1990 {
1991 cupsdLogMessage(CUPSD_LOG_ERROR, "Missing </Job> directive on line %d!",
1992 linenum);
1993 continue;
1994 }
1995
1996 if (!value)
1997 {
1998 cupsdLogMessage(CUPSD_LOG_ERROR, "Missing job ID on line %d!", linenum);
1999 continue;
2000 }
2001
2002 jobid = atoi(value);
2003
2004 if (jobid < 1)
2005 {
2006 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad job ID %d on line %d!", jobid,
2007 linenum);
2008 continue;
2009 }
2010
2011 snprintf(jobfile, sizeof(jobfile), "%s/c%05d", RequestRoot, jobid);
2012 if (access(jobfile, 0))
2013 {
2014 cupsdLogMessage(CUPSD_LOG_ERROR, "[Job %d] Files have gone away!",
2015 jobid);
2016 continue;
2017 }
2018
2019 job = calloc(1, sizeof(cupsd_job_t));
2020 if (!job)
2021 {
2022 cupsdLogMessage(CUPSD_LOG_EMERG,
2023 "[Job %d] Unable to allocate memory for job!", jobid);
2024 break;
2025 }
2026
2027 job->id = jobid;
2028 job->back_pipes[0] = -1;
2029 job->back_pipes[1] = -1;
2030 job->print_pipes[0] = -1;
2031 job->print_pipes[1] = -1;
2032 job->side_pipes[0] = -1;
2033 job->side_pipes[1] = -1;
2034 job->status_pipes[0] = -1;
2035 job->status_pipes[1] = -1;
2036
2037 cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] Loading from cache...", job->id);
2038 }
2039 else if (!job)
2040 {
2041 cupsdLogMessage(CUPSD_LOG_ERROR,
2042 "Missing <Job #> directive on line %d!", linenum);
2043 continue;
2044 }
2045 else if (!strcasecmp(line, "</Job>"))
2046 {
2047 cupsArrayAdd(Jobs, job);
2048
2049 if (job->state_value <= IPP_JOB_STOPPED)
2050 {
2051 cupsArrayAdd(ActiveJobs, job);
2052 cupsdLoadJob(job);
2053 }
2054
2055 job = NULL;
2056 }
2057 else if (!value)
2058 {
2059 cupsdLogMessage(CUPSD_LOG_ERROR, "Missing value on line %d!", linenum);
2060 continue;
2061 }
2062 else if (!strcasecmp(line, "State"))
2063 {
2064 job->state_value = (ipp_jstate_t)atoi(value);
2065
2066 if (job->state_value < IPP_JOB_PENDING)
2067 job->state_value = IPP_JOB_PENDING;
2068 else if (job->state_value > IPP_JOB_COMPLETED)
2069 job->state_value = IPP_JOB_COMPLETED;
2070 }
2071 else if (!strcasecmp(line, "Priority"))
2072 {
2073 job->priority = atoi(value);
2074 }
2075 else if (!strcasecmp(line, "Username"))
2076 {
2077 cupsdSetString(&job->username, value);
2078 }
2079 else if (!strcasecmp(line, "Destination"))
2080 {
2081 cupsdSetString(&job->dest, value);
2082 }
2083 else if (!strcasecmp(line, "DestType"))
2084 {
2085 job->dtype = (cups_ptype_t)atoi(value);
2086 }
2087 else if (!strcasecmp(line, "NumFiles"))
2088 {
2089 job->num_files = atoi(value);
2090
2091 if (job->num_files < 0)
2092 {
2093 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad NumFiles value %d on line %d!",
2094 job->num_files, linenum);
2095 job->num_files = 0;
2096 continue;
2097 }
2098
2099 if (job->num_files > 0)
2100 {
2101 snprintf(jobfile, sizeof(jobfile), "%s/d%05d-001", RequestRoot,
2102 job->id);
2103 if (access(jobfile, 0))
2104 {
2105 cupsdLogMessage(CUPSD_LOG_INFO,
2106 "[Job %d] Data files have gone away!", job->id);
2107 job->num_files = 0;
2108 continue;
2109 }
2110
2111 job->filetypes = calloc(job->num_files, sizeof(mime_type_t *));
2112 job->compressions = calloc(job->num_files, sizeof(int));
2113
2114 if (!job->filetypes || !job->compressions)
2115 {
2116 cupsdLogMessage(CUPSD_LOG_EMERG,
2117 "[Job %d] Unable to allocate memory for %d files!",
2118 job->id, job->num_files);
2119 break;
2120 }
2121 }
2122 }
2123 else if (!strcasecmp(line, "File"))
2124 {
2125 int number, /* File number */
2126 compression; /* Compression value */
2127 char super[MIME_MAX_SUPER], /* MIME super type */
2128 type[MIME_MAX_TYPE]; /* MIME type */
2129
2130
2131 if (sscanf(value, "%d%*[ \t]%15[^/]/%255s%d", &number, super, type,
2132 &compression) != 4)
2133 {
2134 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad File on line %d!", linenum);
2135 continue;
2136 }
2137
2138 if (number < 1 || number > job->num_files)
2139 {
2140 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad File number %d on line %d!",
2141 number, linenum);
2142 continue;
2143 }
2144
2145 number --;
2146
2147 job->compressions[number] = compression;
2148 job->filetypes[number] = mimeType(MimeDatabase, super, type);
2149
2150 if (!job->filetypes[number])
2151 {
2152 /*
2153 * If the original MIME type is unknown, auto-type it!
2154 */
2155
2156 cupsdLogMessage(CUPSD_LOG_ERROR,
2157 "[Job %d] Unknown MIME type %s/%s for file %d!",
2158 job->id, super, type, number + 1);
2159
2160 snprintf(jobfile, sizeof(jobfile), "%s/d%05d-%03d", RequestRoot,
2161 job->id, number + 1);
2162 job->filetypes[number] = mimeFileType(MimeDatabase, jobfile, NULL,
2163 job->compressions + number);
2164
2165 /*
2166 * If that didn't work, assume it is raw...
2167 */
2168
2169 if (!job->filetypes[number])
2170 job->filetypes[number] = mimeType(MimeDatabase, "application",
2171 "vnd.cups-raw");
2172 }
2173 }
2174 else
2175 cupsdLogMessage(CUPSD_LOG_ERROR, "Unknown %s directive on line %d!",
2176 line, linenum);
2177 }
2178
2179 cupsFileClose(fp);
2180 }
2181
2182
2183 /*
2184 * 'load_next_job_id()' - Load the NextJobId value from the job.cache file.
2185 */
2186
2187 static void
2188 load_next_job_id(const char *filename) /* I - job.cache filename */
2189 {
2190 cups_file_t *fp; /* job.cache file */
2191 char line[1024], /* Line buffer */
2192 *value; /* Value on line */
2193 int linenum; /* Line number in file */
2194 int next_job_id; /* NextJobId value from line */
2195
2196
2197 /*
2198 * Read the NextJobId directive from the job.cache file and use
2199 * the value (if any).
2200 */
2201
2202 if ((fp = cupsFileOpen(filename, "r")) == NULL)
2203 {
2204 if (errno != ENOENT)
2205 cupsdLogMessage(CUPSD_LOG_ERROR,
2206 "Unable to open job cache file \"%s\": %s",
2207 filename, strerror(errno));
2208
2209 return;
2210 }
2211
2212 cupsdLogMessage(CUPSD_LOG_INFO,
2213 "Loading NextJobId from job cache file \"%s\"...", filename);
2214
2215 linenum = 0;
2216
2217 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
2218 {
2219 if (!strcasecmp(line, "NextJobId"))
2220 {
2221 if (value)
2222 {
2223 next_job_id = atoi(value);
2224
2225 if (next_job_id > NextJobId)
2226 NextJobId = next_job_id;
2227 }
2228 break;
2229 }
2230 }
2231
2232 cupsFileClose(fp);
2233 }
2234
2235
2236 /*
2237 * 'load_request_root()' - Load jobs from the RequestRoot directory.
2238 */
2239
2240 static void
2241 load_request_root(void)
2242 {
2243 cups_dir_t *dir; /* Directory */
2244 cups_dentry_t *dent; /* Directory entry */
2245 cupsd_job_t *job; /* New job */
2246
2247
2248 /*
2249 * Open the requests directory...
2250 */
2251
2252 cupsdLogMessage(CUPSD_LOG_DEBUG, "Scanning %s for jobs...", RequestRoot);
2253
2254 if ((dir = cupsDirOpen(RequestRoot)) == NULL)
2255 {
2256 cupsdLogMessage(CUPSD_LOG_ERROR,
2257 "Unable to open spool directory \"%s\": %s",
2258 RequestRoot, strerror(errno));
2259 return;
2260 }
2261
2262 /*
2263 * Read all the c##### files...
2264 */
2265
2266 while ((dent = cupsDirRead(dir)) != NULL)
2267 if (strlen(dent->filename) >= 6 && dent->filename[0] == 'c')
2268 {
2269 /*
2270 * Allocate memory for the job...
2271 */
2272
2273 if ((job = calloc(sizeof(cupsd_job_t), 1)) == NULL)
2274 {
2275 cupsdLogMessage(CUPSD_LOG_ERROR, "Ran out of memory for jobs!");
2276 cupsDirClose(dir);
2277 return;
2278 }
2279
2280 /*
2281 * Assign the job ID...
2282 */
2283
2284 job->id = atoi(dent->filename + 1);
2285 job->back_pipes[0] = -1;
2286 job->back_pipes[1] = -1;
2287 job->print_pipes[0] = -1;
2288 job->print_pipes[1] = -1;
2289 job->side_pipes[0] = -1;
2290 job->side_pipes[1] = -1;
2291 job->status_pipes[0] = -1;
2292 job->status_pipes[1] = -1;
2293
2294 if (job->id >= NextJobId)
2295 NextJobId = job->id + 1;
2296
2297 /*
2298 * Load the job...
2299 */
2300
2301 cupsdLoadJob(job);
2302
2303 /*
2304 * Insert the job into the array, sorting by job priority and ID...
2305 */
2306
2307 cupsArrayAdd(Jobs, job);
2308
2309 if (job->state_value <= IPP_JOB_STOPPED)
2310 cupsArrayAdd(ActiveJobs, job);
2311 else
2312 unload_job(job);
2313 }
2314
2315 cupsDirClose(dir);
2316 }
2317
2318
2319 /*
2320 * 'set_time()' - Set one of the "time-at-xyz" attributes...
2321 */
2322
2323 static void
2324 set_time(cupsd_job_t *job, /* I - Job to update */
2325 const char *name) /* I - Name of attribute */
2326 {
2327 ipp_attribute_t *attr; /* Time attribute */
2328
2329
2330 if ((attr = ippFindAttribute(job->attrs, name, IPP_TAG_ZERO)) != NULL)
2331 {
2332 attr->value_tag = IPP_TAG_INTEGER;
2333 attr->values[0].integer = time(NULL);
2334 }
2335 }
2336
2337
2338 /*
2339 * 'set_hold_until()' - Set the hold time and update job-hold-until attribute...
2340 */
2341
2342 static void
2343 set_hold_until(cupsd_job_t *job, /* I - Job to update */
2344 time_t holdtime) /* I - Hold until time */
2345 {
2346 ipp_attribute_t *attr; /* job-hold-until attribute */
2347 struct tm *holddate; /* Hold date */
2348 char holdstr[64]; /* Hold time */
2349
2350
2351 /*
2352 * Set the hold_until value and hold the job...
2353 */
2354
2355 cupsdLogMessage(CUPSD_LOG_DEBUG, "set_hold_until: hold_until = %d",
2356 (int)holdtime);
2357
2358 job->state->values[0].integer = IPP_JOB_HELD;
2359 job->state_value = IPP_JOB_HELD;
2360 job->hold_until = holdtime;
2361
2362 /*
2363 * Update the job-hold-until attribute with a string representing GMT
2364 * time (HH:MM:SS)...
2365 */
2366
2367 holddate = gmtime(&holdtime);
2368 snprintf(holdstr, sizeof(holdstr), "%d:%d:%d", holddate->tm_hour,
2369 holddate->tm_min, holddate->tm_sec);
2370
2371 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
2372 IPP_TAG_KEYWORD)) == NULL)
2373 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
2374
2375 /*
2376 * Either add the attribute or update the value of the existing one
2377 */
2378
2379 if (attr == NULL)
2380 attr = ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_KEYWORD,
2381 "job-hold-until", NULL, holdstr);
2382 else
2383 cupsdSetString(&attr->values[0].string.text, holdstr);
2384
2385 cupsdSaveJob(job);
2386 }
2387
2388
2389 /*
2390 * 'start_job()' - Start a print job.
2391 */
2392
2393 static void
2394 start_job(cupsd_job_t *job, /* I - Job ID */
2395 cupsd_printer_t *printer) /* I - Printer to print job */
2396 {
2397 int i; /* Looping var */
2398 int slot; /* Pipe slot */
2399 cups_array_t *filters, /* Filters for job */
2400 *prefilters; /* Filters with prefilters */
2401 mime_filter_t *filter, /* Current filter */
2402 *prefilter, /* Prefilter */
2403 port_monitor; /* Port monitor filter */
2404 char method[255], /* Method for output */
2405 *optptr, /* Pointer to options */
2406 *valptr; /* Pointer in value string */
2407 ipp_attribute_t *attr; /* Current attribute */
2408 struct stat backinfo; /* Backend file information */
2409 int backroot; /* Run backend as root? */
2410 int pid; /* Process ID of new filter process */
2411 int banner_page; /* 1 if banner page, 0 otherwise */
2412 int filterfds[2][2];/* Pipes used between filters */
2413 int envc; /* Number of environment variables */
2414 char **argv, /* Filter command-line arguments */
2415 sani_uri[1024], /* Sanitized DEVICE_URI env var */
2416 filename[1024], /* Job filename */
2417 command[1024], /* Full path to command */
2418 jobid[255], /* Job ID string */
2419 title[IPP_MAX_NAME],
2420 /* Job title string */
2421 copies[255], /* # copies string */
2422 *envp[MAX_ENV + 15],
2423 /* Environment variables */
2424 charset[255], /* CHARSET env variable */
2425 class_name[255],/* CLASS env variable */
2426 classification[1024],
2427 /* CLASSIFICATION env variable */
2428 content_type[1024],
2429 /* CONTENT_TYPE env variable */
2430 device_uri[1024],
2431 /* DEVICE_URI env variable */
2432 final_content_type[1024],
2433 /* FINAL_CONTENT_TYPE env variable */
2434 lang[255], /* LANG env variable */
2435 ppd[1024], /* PPD env variable */
2436 printer_name[255],
2437 /* PRINTER env variable */
2438 rip_max_cache[255];
2439 /* RIP_MAX_CACHE env variable */
2440 static char *options = NULL;/* Full list of options */
2441 static int optlength = 0; /* Length of option buffer */
2442
2443
2444 cupsdLogMessage(CUPSD_LOG_DEBUG2, "[Job %d] start_job: file = %d/%d",
2445 job->id, job->current_file, job->num_files);
2446
2447 if (job->num_files == 0)
2448 {
2449 cupsdLogMessage(CUPSD_LOG_ERROR, "[Job %d] No files, canceling job!",
2450 job->id);
2451
2452 cupsdCancelJob(job, 0, IPP_JOB_ABORTED);
2453 return;
2454 }
2455
2456 /*
2457 * Figure out what filters are required to convert from
2458 * the source to the destination type...
2459 */
2460
2461 filters = NULL;
2462 job->cost = 0;
2463
2464 if (printer->raw)
2465 {
2466 /*
2467 * Remote jobs and raw queues go directly to the printer without
2468 * filtering...
2469 */
2470
2471 cupsdLogMessage(CUPSD_LOG_DEBUG,
2472 "[Job %d] Sending job to queue tagged as raw...", job->id);
2473
2474 filters = NULL;
2475 }
2476 else
2477 {
2478 /*
2479 * Local jobs get filtered...
2480 */
2481
2482 filters = mimeFilter(MimeDatabase, job->filetypes[job->current_file],
2483 printer->filetype, &(job->cost));
2484
2485 if (!filters)
2486 {
2487 cupsdLogMessage(CUPSD_LOG_ERROR,
2488 "[Job %d] Unable to convert file %d to printable format!",
2489 job->current_file, job->id);
2490 cupsdLogMessage(CUPSD_LOG_INFO,
2491 "Hint: Do you have ESP Ghostscript installed?");
2492
2493 if (LogLevel < CUPSD_LOG_DEBUG)
2494 cupsdLogMessage(CUPSD_LOG_INFO,
2495 "Hint: Try setting the LogLevel to \"debug\".");
2496
2497 job->current_file ++;
2498
2499 if (job->current_file == job->num_files)
2500 cupsdCancelJob(job, 0, IPP_JOB_ABORTED);
2501
2502 return;
2503 }
2504
2505 /*
2506 * Remove NULL ("-") filters...
2507 */
2508
2509 for (filter = (mime_filter_t *)cupsArrayFirst(filters);
2510 filter;
2511 filter = (mime_filter_t *)cupsArrayNext(filters))
2512 if (!strcmp(filter->filter, "-"))
2513 cupsArrayRemove(filters, filter);
2514
2515 if (cupsArrayCount(filters) == 0)
2516 {
2517 cupsArrayDelete(filters);
2518 filters = NULL;
2519 }
2520
2521 /*
2522 * If this printer has any pre-filters, insert the required pre-filter
2523 * in the filters array...
2524 */
2525
2526 if (printer->prefiltertype && filters)
2527 {
2528 prefilters = cupsArrayNew(NULL, NULL);
2529
2530 for (filter = (mime_filter_t *)cupsArrayFirst(filters);
2531 filter;
2532 filter = (mime_filter_t *)cupsArrayNext(filters))
2533 {
2534 if ((prefilter = mimeFilterLookup(MimeDatabase, filter->src,
2535 printer->prefiltertype)))
2536 {
2537 cupsArrayAdd(prefilters, prefilter);
2538 job->cost += prefilter->cost;
2539 }
2540
2541 cupsArrayAdd(prefilters, filter);
2542 }
2543
2544 cupsArrayDelete(filters);
2545 filters = prefilters;
2546 }
2547 }
2548
2549 /*
2550 * Set a minimum cost of 100 for all jobs so that FilterLimit
2551 * works with raw queues and other low-cost paths.
2552 */
2553
2554 if (job->cost < 100)
2555 job->cost = 100;
2556
2557 /*
2558 * See if the filter cost is too high...
2559 */
2560
2561 if ((FilterLevel + job->cost) > FilterLimit && FilterLevel > 0 &&
2562 FilterLimit > 0)
2563 {
2564 /*
2565 * Don't print this job quite yet...
2566 */
2567
2568 cupsArrayDelete(filters);
2569
2570 cupsdLogMessage(CUPSD_LOG_INFO,
2571 "[Job %d] Holding because filter limit has been reached.",
2572 job->id);
2573 cupsdLogMessage(CUPSD_LOG_DEBUG2,
2574 "[Job %d] start_job: file=%d, cost=%d, level=%d, limit=%d",
2575 job->id, job->current_file, job->cost, FilterLevel,
2576 FilterLimit);
2577 return;
2578 }
2579
2580 FilterLevel += job->cost;
2581
2582 /*
2583 * Add decompression/raw filter as needed...
2584 */
2585
2586 if ((!printer->raw && job->compressions[job->current_file]) ||
2587 (!filters && !printer->remote &&
2588 (job->num_files > 1 || !strncmp(printer->device_uri, "file:", 5))))
2589 {
2590 /*
2591 * Add gziptoany filter to the front of the list...
2592 */
2593
2594 if (!filters)
2595 filters = cupsArrayNew(NULL, NULL);
2596
2597 if (!cupsArrayInsert(filters, &gziptoany_filter))
2598 {
2599 cupsdLogMessage(CUPSD_LOG_ERROR,
2600 "[Job %d] Unable to add decompression filter - %s",
2601 job->id, strerror(errno));
2602
2603 cupsArrayDelete(filters);
2604
2605 job->current_file ++;
2606
2607 if (job->current_file == job->num_files)
2608 cupsdCancelJob(job, 0, IPP_JOB_ABORTED);
2609
2610 return;
2611 }
2612 }
2613
2614 /*
2615 * Add port monitor, if any...
2616 */
2617
2618 if (printer->port_monitor)
2619 {
2620 /*
2621 * Add port monitor to the end of the list...
2622 */
2623
2624 if (!filters)
2625 filters = cupsArrayNew(NULL, NULL);
2626
2627 if (!cupsArrayAdd(filters, &port_monitor))
2628 {
2629 cupsdLogMessage(CUPSD_LOG_ERROR,
2630 "[Job %d] Unable to add port monitor - %s",
2631 job->id, strerror(errno));
2632
2633 cupsArrayDelete(filters);
2634
2635 job->current_file ++;
2636
2637 if (job->current_file == job->num_files)
2638 cupsdCancelJob(job, 0, IPP_JOB_ABORTED);
2639
2640 return;
2641 }
2642
2643 snprintf(port_monitor.filter, sizeof(port_monitor.filter),
2644 "%s/monitor/%s", ServerBin, printer->port_monitor);
2645 }
2646
2647 /*
2648 * Update the printer and job state to "processing"...
2649 */
2650
2651 job->state->values[0].integer = IPP_JOB_PROCESSING;
2652 job->state_value = IPP_JOB_PROCESSING;
2653
2654 job->status = 0;
2655 job->printer = printer;
2656 printer->job = job;
2657
2658 cupsdSetPrinterState(printer, IPP_PRINTER_PROCESSING, 0);
2659
2660 if (job->current_file == 0)
2661 {
2662 /*
2663 * Set the processing time...
2664 */
2665
2666 set_time(job, "time-at-processing");
2667
2668 /*
2669 * Create the backchannel pipes and make them non-blocking...
2670 */
2671
2672 cupsdOpenPipe(job->back_pipes);
2673
2674 fcntl(job->back_pipes[0], F_SETFL,
2675 fcntl(job->back_pipes[0], F_GETFL) | O_NONBLOCK);
2676
2677 fcntl(job->back_pipes[1], F_SETFL,
2678 fcntl(job->back_pipes[1], F_GETFL) | O_NONBLOCK);
2679
2680 /*
2681 * Create the side-channel pipes and make them non-blocking...
2682 */
2683
2684 socketpair(AF_LOCAL, SOCK_STREAM, 0, job->side_pipes);
2685
2686 fcntl(job->side_pipes[0], F_SETFL,
2687 fcntl(job->side_pipes[0], F_GETFL) | O_NONBLOCK);
2688
2689 fcntl(job->side_pipes[1], F_SETFL,
2690 fcntl(job->side_pipes[1], F_GETFL) | O_NONBLOCK);
2691 }
2692
2693 /*
2694 * Determine if we are printing a banner page or not...
2695 */
2696
2697 if (job->job_sheets == NULL)
2698 {
2699 cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] No job-sheets attribute.",
2700 job->id);
2701 if ((job->job_sheets =
2702 ippFindAttribute(job->attrs, "job-sheets", IPP_TAG_ZERO)) != NULL)
2703 cupsdLogMessage(CUPSD_LOG_DEBUG,
2704 "[Job %d] ... but someone added one without setting "
2705 "job_sheets!", job->id);
2706 }
2707 else if (job->job_sheets->num_values == 1)
2708 cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] job-sheets=%s", job->id,
2709 job->job_sheets->values[0].string.text);
2710 else
2711 cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] job-sheets=%s,%s", job->id,
2712 job->job_sheets->values[0].string.text,
2713 job->job_sheets->values[1].string.text);
2714
2715 if (printer->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT))
2716 banner_page = 0;
2717 else if (job->job_sheets == NULL)
2718 banner_page = 0;
2719 else if (strcasecmp(job->job_sheets->values[0].string.text, "none") != 0 &&
2720 job->current_file == 0)
2721 banner_page = 1;
2722 else if (job->job_sheets->num_values > 1 &&
2723 strcasecmp(job->job_sheets->values[1].string.text, "none") != 0 &&
2724 job->current_file == (job->num_files - 1))
2725 banner_page = 1;
2726 else
2727 banner_page = 0;
2728
2729 cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] banner_page = %d", job->id,
2730 banner_page);
2731
2732 /*
2733 * Building the options string is harder than it needs to be, but
2734 * for the moment we need to pass strings for command-line args and
2735 * not IPP attribute pointers... :)
2736 *
2737 * First allocate/reallocate the option buffer as needed...
2738 */
2739
2740 i = ipp_length(job->attrs);
2741
2742 if (i > optlength)
2743 {
2744 if (optlength == 0)
2745 optptr = malloc(i);
2746 else
2747 optptr = realloc(options, i);
2748
2749 if (optptr == NULL)
2750 {
2751 cupsdLogMessage(CUPSD_LOG_CRIT,
2752 "[Job %d] Unable to allocate %d bytes for option buffer!",
2753 job->id, i);
2754
2755 cupsArrayDelete(filters);
2756
2757 FilterLevel -= job->cost;
2758
2759 cupsdCancelJob(job, 0, IPP_JOB_ABORTED);
2760 return;
2761 }
2762
2763 options = optptr;
2764 optlength = i;
2765 }
2766
2767 /*
2768 * Now loop through the attributes and convert them to the textual
2769 * representation used by the filters...
2770 */
2771
2772 optptr = options;
2773 *optptr = '\0';
2774
2775 snprintf(title, sizeof(title), "%s-%d", printer->name, job->id);
2776 strcpy(copies, "1");
2777
2778 for (attr = job->attrs->attrs; attr != NULL; attr = attr->next)
2779 {
2780 if (!strcmp(attr->name, "copies") &&
2781 attr->value_tag == IPP_TAG_INTEGER)
2782 {
2783 /*
2784 * Don't use the # copies attribute if we are printing the job sheets...
2785 */
2786
2787 if (!banner_page)
2788 sprintf(copies, "%d", attr->values[0].integer);
2789 }
2790 else if (!strcmp(attr->name, "job-name") &&
2791 (attr->value_tag == IPP_TAG_NAME ||
2792 attr->value_tag == IPP_TAG_NAMELANG))
2793 strlcpy(title, attr->values[0].string.text, sizeof(title));
2794 else if (attr->group_tag == IPP_TAG_JOB)
2795 {
2796 /*
2797 * Filter out other unwanted attributes...
2798 */
2799
2800 if (attr->value_tag == IPP_TAG_MIMETYPE ||
2801 attr->value_tag == IPP_TAG_NAMELANG ||
2802 attr->value_tag == IPP_TAG_TEXTLANG ||
2803 (attr->value_tag == IPP_TAG_URI && strcmp(attr->name, "job-uuid")) ||
2804 attr->value_tag == IPP_TAG_URISCHEME ||
2805 attr->value_tag == IPP_TAG_BEGIN_COLLECTION) /* Not yet supported */
2806 continue;
2807
2808 if (!strncmp(attr->name, "time-", 5))
2809 continue;
2810
2811 if (!strncmp(attr->name, "job-", 4) && strcmp(attr->name, "job-uuid") &&
2812 !(printer->type & CUPS_PRINTER_REMOTE))
2813 continue;
2814
2815 if (!strncmp(attr->name, "job-", 4) &&
2816 strcmp(attr->name, "job-uuid") &&
2817 strcmp(attr->name, "job-billing") &&
2818 strcmp(attr->name, "job-sheets") &&
2819 strcmp(attr->name, "job-hold-until") &&
2820 strcmp(attr->name, "job-priority"))
2821 continue;
2822
2823 if ((!strcmp(attr->name, "page-label") ||
2824 !strcmp(attr->name, "page-border") ||
2825 !strncmp(attr->name, "number-up", 9) ||
2826 !strcmp(attr->name, "page-ranges") ||
2827 !strcmp(attr->name, "page-set") ||
2828 !strcasecmp(attr->name, "AP_FIRSTPAGE_InputSlot") ||
2829 !strcasecmp(attr->name, "AP_FIRSTPAGE_ManualFeed")) &&
2830 banner_page)
2831 continue;
2832
2833 /*
2834 * Otherwise add them to the list...
2835 */
2836
2837 if (optptr > options)
2838 strlcat(optptr, " ", optlength - (optptr - options));
2839
2840 if (attr->value_tag != IPP_TAG_BOOLEAN)
2841 {
2842 strlcat(optptr, attr->name, optlength - (optptr - options));
2843 strlcat(optptr, "=", optlength - (optptr - options));
2844 }
2845
2846 for (i = 0; i < attr->num_values; i ++)
2847 {
2848 if (i)
2849 strlcat(optptr, ",", optlength - (optptr - options));
2850
2851 optptr += strlen(optptr);
2852
2853 switch (attr->value_tag)
2854 {
2855 case IPP_TAG_INTEGER :
2856 case IPP_TAG_ENUM :
2857 snprintf(optptr, optlength - (optptr - options),
2858 "%d", attr->values[i].integer);
2859 break;
2860
2861 case IPP_TAG_BOOLEAN :
2862 if (!attr->values[i].boolean)
2863 strlcat(optptr, "no", optlength - (optptr - options));
2864
2865 case IPP_TAG_NOVALUE :
2866 strlcat(optptr, attr->name,
2867 optlength - (optptr - options));
2868 break;
2869
2870 case IPP_TAG_RANGE :
2871 if (attr->values[i].range.lower == attr->values[i].range.upper)
2872 snprintf(optptr, optlength - (optptr - options) - 1,
2873 "%d", attr->values[i].range.lower);
2874 else
2875 snprintf(optptr, optlength - (optptr - options) - 1,
2876 "%d-%d", attr->values[i].range.lower,
2877 attr->values[i].range.upper);
2878 break;
2879
2880 case IPP_TAG_RESOLUTION :
2881 snprintf(optptr, optlength - (optptr - options) - 1,
2882 "%dx%d%s", attr->values[i].resolution.xres,
2883 attr->values[i].resolution.yres,
2884 attr->values[i].resolution.units == IPP_RES_PER_INCH ?
2885 "dpi" : "dpc");
2886 break;
2887
2888 case IPP_TAG_STRING :
2889 case IPP_TAG_TEXT :
2890 case IPP_TAG_NAME :
2891 case IPP_TAG_KEYWORD :
2892 case IPP_TAG_CHARSET :
2893 case IPP_TAG_LANGUAGE :
2894 case IPP_TAG_URI :
2895 for (valptr = attr->values[i].string.text; *valptr;)
2896 {
2897 if (strchr(" \t\n\\\'\"", *valptr))
2898 *optptr++ = '\\';
2899 *optptr++ = *valptr++;
2900 }
2901
2902 *optptr = '\0';
2903 break;
2904
2905 default :
2906 break; /* anti-compiler-warning-code */
2907 }
2908 }
2909
2910 optptr += strlen(optptr);
2911 }
2912 }
2913
2914 /*
2915 * Build the command-line arguments for the filters. Each filter
2916 * has 6 or 7 arguments:
2917 *
2918 * argv[0] = printer
2919 * argv[1] = job ID
2920 * argv[2] = username
2921 * argv[3] = title
2922 * argv[4] = # copies
2923 * argv[5] = options
2924 * argv[6] = filename (optional; normally stdin)
2925 *
2926 * This allows legacy printer drivers that use the old System V
2927 * printing interface to be used by CUPS.
2928 *
2929 * For remote jobs, we send all of the files in the argument list.
2930 */
2931
2932 if (printer->remote && job->num_files > 1)
2933 argv = calloc(7 + job->num_files, sizeof(char *));
2934 else
2935 argv = calloc(8, sizeof(char *));
2936
2937 sprintf(jobid, "%d", job->id);
2938
2939 argv[0] = printer->name;
2940 argv[1] = jobid;
2941 argv[2] = job->username;
2942 argv[3] = title;
2943 argv[4] = copies;
2944 argv[5] = options;
2945
2946 if (printer->remote && job->num_files > 1)
2947 {
2948 for (i = 0; i < job->num_files; i ++)
2949 {
2950 snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot,
2951 job->id, i + 1);
2952 argv[6 + i] = strdup(filename);
2953 }
2954 }
2955 else
2956 {
2957 snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot,
2958 job->id, job->current_file + 1);
2959 argv[6] = filename;
2960 }
2961
2962 for (i = 0; argv[i]; i ++)
2963 cupsdLogMessage(CUPSD_LOG_DEBUG,
2964 "[Job %d] argv[%d]=\"%s\"", job->id, i, argv[i]);
2965
2966 /*
2967 * Create environment variable strings for the filters...
2968 */
2969
2970 attr = ippFindAttribute(job->attrs, "attributes-natural-language",
2971 IPP_TAG_LANGUAGE);
2972
2973 switch (strlen(attr->values[0].string.text))
2974 {
2975 default :
2976 /*
2977 * This is an unknown or badly formatted language code; use
2978 * the POSIX locale...
2979 */
2980
2981 strcpy(lang, "LANG=C");
2982 break;
2983
2984 case 2 :
2985 /*
2986 * Just the language code (ll)...
2987 */
2988
2989 snprintf(lang, sizeof(lang), "LANG=%s",
2990 attr->values[0].string.text);
2991 break;
2992
2993 case 5 :
2994 /*
2995 * Language and country code (ll-cc)...
2996 */
2997
2998 snprintf(lang, sizeof(lang), "LANG=%c%c_%c%c",
2999 attr->values[0].string.text[0],
3000 attr->values[0].string.text[1],
3001 toupper(attr->values[0].string.text[3] & 255),
3002 toupper(attr->values[0].string.text[4] & 255));
3003 break;
3004 }
3005
3006 attr = ippFindAttribute(job->attrs, "document-format",
3007 IPP_TAG_MIMETYPE);
3008 if (attr != NULL &&
3009 (optptr = strstr(attr->values[0].string.text, "charset=")) != NULL)
3010 snprintf(charset, sizeof(charset), "CHARSET=%s", optptr + 8);
3011 else
3012 {
3013 attr = ippFindAttribute(job->attrs, "attributes-charset",
3014 IPP_TAG_CHARSET);
3015 snprintf(charset, sizeof(charset), "CHARSET=%s",
3016 attr->values[0].string.text);
3017 }
3018
3019 snprintf(content_type, sizeof(content_type), "CONTENT_TYPE=%s/%s",
3020 job->filetypes[job->current_file]->super,
3021 job->filetypes[job->current_file]->type);
3022 snprintf(device_uri, sizeof(device_uri), "DEVICE_URI=%s",
3023 printer->device_uri);
3024 cupsdSanitizeURI(printer->device_uri, sani_uri, sizeof(sani_uri));
3025 snprintf(ppd, sizeof(ppd), "PPD=%s/ppd/%s.ppd", ServerRoot, printer->name);
3026 snprintf(printer_name, sizeof(printer_name), "PRINTER=%s", printer->name);
3027 snprintf(rip_max_cache, sizeof(rip_max_cache), "RIP_MAX_CACHE=%s", RIPCache);
3028
3029 envc = cupsdLoadEnv(envp, (int)(sizeof(envp) / sizeof(envp[0])));
3030
3031 envp[envc ++] = charset;
3032 envp[envc ++] = lang;
3033 envp[envc ++] = ppd;
3034 envp[envc ++] = rip_max_cache;
3035 envp[envc ++] = content_type;
3036 envp[envc ++] = device_uri;
3037 envp[envc ++] = printer_name;
3038
3039 if (!printer->remote && !printer->raw &&
3040 (filter = (mime_filter_t *)cupsArrayLast(filters)) != NULL &&
3041 filter->dst)
3042 {
3043 snprintf(final_content_type, sizeof(final_content_type),
3044 "FINAL_CONTENT_TYPE=%s/%s",
3045 filter->dst->super, filter->dst->type);
3046 envp[envc ++] = final_content_type;
3047 }
3048
3049 if (Classification && !banner_page)
3050 {
3051 if ((attr = ippFindAttribute(job->attrs, "job-sheets",
3052 IPP_TAG_NAME)) == NULL)
3053 snprintf(classification, sizeof(classification), "CLASSIFICATION=%s",
3054 Classification);
3055 else if (attr->num_values > 1 &&
3056 strcmp(attr->values[1].string.text, "none") != 0)
3057 snprintf(classification, sizeof(classification), "CLASSIFICATION=%s",
3058 attr->values[1].string.text);
3059 else
3060 snprintf(classification, sizeof(classification), "CLASSIFICATION=%s",
3061 attr->values[0].string.text);
3062
3063 envp[envc ++] = classification;
3064 }
3065
3066 if (job->dtype & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT))
3067 {
3068 snprintf(class_name, sizeof(class_name), "CLASS=%s", job->dest);
3069 envp[envc ++] = class_name;
3070 }
3071
3072 if (job->auth_username)
3073 envp[envc ++] = job->auth_username;
3074 if (job->auth_domain)
3075 envp[envc ++] = job->auth_domain;
3076 if (job->auth_password)
3077 envp[envc ++] = job->auth_password;
3078
3079 #ifdef HAVE_GSSAPI
3080 if (job->ccname)
3081 envp[envc ++] = job->ccname;
3082 #endif /* HAVE_GSSAPI */
3083
3084 envp[envc] = NULL;
3085
3086 for (i = 0; i < envc; i ++)
3087 if (!strncmp(envp[i], "AUTH_", 5))
3088 cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] envp[%d]=\"AUTH_%c****\"",
3089 job->id, i, envp[i][5]);
3090 else if (strncmp(envp[i], "DEVICE_URI=", 11))
3091 cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] envp[%d]=\"%s\"",
3092 job->id, i, envp[i]);
3093 else
3094 cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] envp[%d]=\"DEVICE_URI=%s\"",
3095 job->id, i, sani_uri);
3096
3097 if (printer->remote)
3098 job->current_file = job->num_files;
3099 else
3100 job->current_file ++;
3101
3102 /*
3103 * Now create processes for all of the filters...
3104 */
3105
3106 filterfds[0][0] = -1;
3107 filterfds[0][1] = -1;
3108 filterfds[1][0] = -1;
3109 filterfds[1][1] = -1;
3110
3111 if (!job->status_buffer)
3112 {
3113 if (cupsdOpenPipe(job->status_pipes))
3114 {
3115 cupsdLogMessage(CUPSD_LOG_ERROR,
3116 "[Job %d] Unable to create job status pipes - %s.",
3117 job->id, strerror(errno));
3118 snprintf(printer->state_message, sizeof(printer->state_message),
3119 "Unable to create status pipes - %s.", strerror(errno));
3120
3121 cupsdAddPrinterHistory(printer);
3122
3123 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
3124 "Job canceled because the server could not create the job "
3125 "status pipes.");
3126
3127 goto abort_job;
3128 }
3129
3130 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3131 "[Job %d] start_job: status_pipes = [ %d %d ]",
3132 job->id, job->status_pipes[0], job->status_pipes[1]);
3133
3134 job->status_buffer = cupsdStatBufNew(job->status_pipes[0], "[Job %d]",
3135 job->id);
3136 job->status_level = CUPSD_LOG_INFO;
3137 }
3138
3139 job->status = 0;
3140 memset(job->filters, 0, sizeof(job->filters));
3141
3142 for (i = 0, slot = 0, filter = (mime_filter_t *)cupsArrayFirst(filters);
3143 filter;
3144 i ++, filter = (mime_filter_t *)cupsArrayNext(filters))
3145 {
3146 if (filter->filter[0] != '/')
3147 snprintf(command, sizeof(command), "%s/filter/%s", ServerBin,
3148 filter->filter);
3149 else
3150 strlcpy(command, filter->filter, sizeof(command));
3151
3152 if (i < (cupsArrayCount(filters) - 1))
3153 {
3154 if (cupsdOpenPipe(filterfds[slot]))
3155 {
3156 cupsdLogMessage(CUPSD_LOG_ERROR,
3157 "[Job %d] Unable to create job filter pipes - %s.",
3158 job->id, strerror(errno));
3159 snprintf(printer->state_message, sizeof(printer->state_message),
3160 "Unable to create filter pipes - %s.", strerror(errno));
3161 cupsdAddPrinterHistory(printer);
3162
3163 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
3164 "Job canceled because the server could not create the "
3165 "filter pipes.");
3166
3167 goto abort_job;
3168 }
3169 }
3170 else
3171 {
3172 if (job->current_file == 1)
3173 {
3174 if (strncmp(printer->device_uri, "file:", 5) != 0)
3175 {
3176 if (cupsdOpenPipe(job->print_pipes))
3177 {
3178 cupsdLogMessage(CUPSD_LOG_ERROR,
3179 "[Job %d] Unable to create job backend pipes - %s.",
3180 job->id, strerror(errno));
3181 snprintf(printer->state_message, sizeof(printer->state_message),
3182 "Unable to create backend pipes - %s.", strerror(errno));
3183 cupsdAddPrinterHistory(printer);
3184
3185 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
3186 "Job canceled because the server could not create "
3187 "the backend pipes.");
3188
3189 goto abort_job;
3190 }
3191 }
3192 else
3193 {
3194 job->print_pipes[0] = -1;
3195 if (!strcmp(printer->device_uri, "file:/dev/null") ||
3196 !strcmp(printer->device_uri, "file:///dev/null"))
3197 job->print_pipes[1] = -1;
3198 else
3199 {
3200 if (!strncmp(printer->device_uri, "file:/dev/", 10))
3201 job->print_pipes[1] = open(printer->device_uri + 5,
3202 O_WRONLY | O_EXCL);
3203 else if (!strncmp(printer->device_uri, "file:///dev/", 12))
3204 job->print_pipes[1] = open(printer->device_uri + 7,
3205 O_WRONLY | O_EXCL);
3206 else if (!strncmp(printer->device_uri, "file:///", 8))
3207 job->print_pipes[1] = open(printer->device_uri + 7,
3208 O_WRONLY | O_CREAT | O_TRUNC, 0600);
3209 else
3210 job->print_pipes[1] = open(printer->device_uri + 5,
3211 O_WRONLY | O_CREAT | O_TRUNC, 0600);
3212
3213 if (job->print_pipes[1] < 0)
3214 {
3215 cupsdLogMessage(CUPSD_LOG_ERROR,
3216 "[Job %d] Unable to open output file \"%s\" - %s.",
3217 job->id, printer->device_uri, strerror(errno));
3218 snprintf(printer->state_message, sizeof(printer->state_message),
3219 "Unable to open output file \"%s\" - %s.",
3220 printer->device_uri, strerror(errno));
3221
3222 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
3223 "Job canceled because the server could not open the "
3224 "output file.");
3225
3226 goto abort_job;
3227 }
3228
3229 fcntl(job->print_pipes[1], F_SETFD,
3230 fcntl(job->print_pipes[1], F_GETFD) | FD_CLOEXEC);
3231 }
3232 }
3233
3234 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3235 "[Job %d] start_job: print_pipes = [ %d %d ]",
3236 job->id, job->print_pipes[0], job->print_pipes[1]);
3237 }
3238
3239 filterfds[slot][0] = job->print_pipes[0];
3240 filterfds[slot][1] = job->print_pipes[1];
3241 }
3242
3243 cupsdLogMessage(CUPSD_LOG_DEBUG2, "[Job %d] start_job: filter=\"%s\"",
3244 job->id, command);
3245 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3246 "[Job %d] start_job: filterfds[%d]=[ %d %d ]",
3247 job->id, slot, filterfds[slot][0], filterfds[slot][1]);
3248
3249 pid = cupsdStartProcess(command, argv, envp, filterfds[!slot][0],
3250 filterfds[slot][1], job->status_pipes[1],
3251 job->back_pipes[0], job->side_pipes[0], 0,
3252 job->filters + i);
3253
3254 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3255 "[Job %d] start_job: Closing filter pipes for slot %d "
3256 "[ %d %d ]...",
3257 job->id, !slot, filterfds[!slot][0], filterfds[!slot][1]);
3258
3259 cupsdClosePipe(filterfds[!slot]);
3260
3261 if (pid == 0)
3262 {
3263 cupsdLogMessage(CUPSD_LOG_ERROR,
3264 "[Job %d] Unable to start filter \"%s\" - %s.",
3265 job->id, filter->filter, strerror(errno));
3266 snprintf(printer->state_message, sizeof(printer->state_message),
3267 "Unable to start filter \"%s\" - %s.",
3268 filter->filter, strerror(errno));
3269
3270 cupsdAddPrinterHistory(printer);
3271
3272 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
3273 "Job canceled because the server could not execute a "
3274 "filter.");
3275
3276 goto abort_job;
3277 }
3278
3279 cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Started filter %s (PID %d)",
3280 job->id, command, pid);
3281
3282 argv[6] = NULL;
3283 slot = !slot;
3284 }
3285
3286 cupsArrayDelete(filters);
3287
3288 /*
3289 * Finally, pipe the final output into a backend process if needed...
3290 */
3291
3292 if (strncmp(printer->device_uri, "file:", 5) != 0)
3293 {
3294 if (job->current_file == 1)
3295 {
3296 sscanf(printer->device_uri, "%254[^:]", method);
3297 snprintf(command, sizeof(command), "%s/backend/%s", ServerBin, method);
3298
3299 /*
3300 * See if the backend needs to run as root...
3301 */
3302
3303 if (RunUser)
3304 backroot = 0;
3305 else if (stat(command, &backinfo))
3306 backroot = 0;
3307 else
3308 backroot = !(backinfo.st_mode & (S_IRWXG | S_IRWXO));
3309
3310 argv[0] = sani_uri;
3311
3312 filterfds[slot][0] = -1;
3313 filterfds[slot][1] = -1;
3314
3315 cupsdLogMessage(CUPSD_LOG_DEBUG2, "[Job %d] start_job: backend=\"%s\"",
3316 job->id, command);
3317 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3318 "[Job %d] start_job: filterfds[%d] = [ %d %d ]", job->id,
3319 slot, filterfds[slot][0], filterfds[slot][1]);
3320
3321 pid = cupsdStartProcess(command, argv, envp, filterfds[!slot][0],
3322 filterfds[slot][1], job->status_pipes[1],
3323 job->back_pipes[1], job->side_pipes[1],
3324 backroot, &(job->backend));
3325
3326 if (pid == 0)
3327 {
3328 cupsdLogMessage(CUPSD_LOG_ERROR,
3329 "[Job %d] Unable to start backend \"%s\" - %s.",
3330 job->id, method, strerror(errno));
3331 snprintf(printer->state_message, sizeof(printer->state_message),
3332 "Unable to start backend \"%s\" - %s.", method,
3333 strerror(errno));
3334
3335 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
3336 "Job canceled because the server could not execute "
3337 "the backend.");
3338
3339 goto abort_job;
3340 }
3341 else
3342 {
3343 cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Started backend %s (PID %d)",
3344 job->id, command, pid);
3345 }
3346 }
3347
3348 if (job->current_file == job->num_files)
3349 {
3350 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3351 "[Job %d] start_job: Closing print pipes [ %d %d ]...",
3352 job->id, job->print_pipes[0], job->print_pipes[1]);
3353
3354 cupsdClosePipe(job->print_pipes);
3355
3356 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3357 "[Job %d] start_job: Closing back pipes [ %d %d ]...",
3358 job->id, job->back_pipes[0], job->back_pipes[1]);
3359
3360 cupsdClosePipe(job->back_pipes);
3361
3362 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3363 "[Job %d] start_job: Closing side pipes [ %d %d ]...",
3364 job->id, job->side_pipes[0], job->side_pipes[1]);
3365
3366 cupsdClosePipe(job->side_pipes);
3367
3368 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3369 "[Job %d] start_job: Closing status output pipe %d...",
3370 job->id, job->status_pipes[1]);
3371
3372 close(job->status_pipes[1]);
3373 job->status_pipes[1] = -1;
3374 }
3375 }
3376 else
3377 {
3378 filterfds[slot][0] = -1;
3379 filterfds[slot][1] = -1;
3380
3381 if (job->current_file == job->num_files)
3382 {
3383 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3384 "[Job %d] start_job: Closing print pipes [ %d %d ]...",
3385 job->id, job->print_pipes[0], job->print_pipes[1]);
3386
3387 cupsdClosePipe(job->print_pipes);
3388
3389 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3390 "[Job %d] start_job: Closing status output pipe %d...",
3391 job->id, job->status_pipes[1]);
3392
3393 close(job->status_pipes[1]);
3394 job->status_pipes[1] = -1;
3395 }
3396 }
3397
3398 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3399 "[Job %d] start_job: Closing filter pipes for slot %d "
3400 "[ %d %d ]...",
3401 job->id, slot, filterfds[slot][0], filterfds[slot][1]);
3402 cupsdClosePipe(filterfds[slot]);
3403
3404 if (printer->remote && job->num_files > 1)
3405 {
3406 for (i = 0; i < job->num_files; i ++)
3407 free(argv[i + 6]);
3408 }
3409
3410 free(argv);
3411
3412 cupsdAddSelect(job->status_buffer->fd, (cupsd_selfunc_t)update_job, NULL,
3413 job);
3414
3415 cupsdAddEvent(CUPSD_EVENT_JOB_STATE, job->printer, job, "Job #%d started.",
3416 job->id);
3417
3418 return;
3419
3420
3421 /*
3422 * If we get here, we need to abort the current job and close out all
3423 * files and pipes...
3424 */
3425
3426 abort_job:
3427
3428 for (slot = 0; slot < 2; slot ++)
3429 {
3430 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3431 "[Job %d] start_job: Closing filter pipes for slot %d "
3432 "[ %d %d ]...",
3433 job->id, slot, filterfds[slot][0], filterfds[slot][1]);
3434 cupsdClosePipe(filterfds[slot]);
3435 }
3436
3437 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3438 "[Job %d] start_job: Closing status pipes [ %d %d ]...",
3439 job->id, job->status_pipes[0], job->status_pipes[1]);
3440 cupsdClosePipe(job->status_pipes);
3441 cupsdStatBufDelete(job->status_buffer);
3442
3443 job->status_buffer = NULL;
3444
3445 cupsArrayDelete(filters);
3446
3447 if (printer->remote && job->num_files > 1)
3448 {
3449 for (i = 0; i < job->num_files; i ++)
3450 free(argv[i + 6]);
3451 }
3452
3453 free(argv);
3454
3455 cupsdStopJob(job, 0);
3456 }
3457
3458
3459 /*
3460 * 'unload_job()' - Unload a job from memory.
3461 */
3462
3463 static void
3464 unload_job(cupsd_job_t *job) /* I - Job */
3465 {
3466 if (!job->attrs)
3467 return;
3468
3469 cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] Unloading...", job->id);
3470
3471 ippDelete(job->attrs);
3472
3473 job->attrs = NULL;
3474 job->state = NULL;
3475 job->sheets = NULL;
3476 job->job_sheets = NULL;
3477 job->printer_message = NULL;
3478 job->printer_reasons = NULL;
3479 }
3480
3481
3482 /*
3483 * 'update_job()' - Read a status update from a job's filters.
3484 */
3485
3486 void
3487 update_job(cupsd_job_t *job) /* I - Job to check */
3488 {
3489 int i; /* Looping var */
3490 int copies; /* Number of copies printed */
3491 char message[1024], /* Message text */
3492 *ptr; /* Pointer update... */
3493 int loglevel, /* Log level for message */
3494 event = 0; /* Events? */
3495
3496
3497 while ((ptr = cupsdStatBufUpdate(job->status_buffer, &loglevel,
3498 message, sizeof(message))) != NULL)
3499 {
3500 /*
3501 * Process page and printer state messages as needed...
3502 */
3503
3504 if (loglevel == CUPSD_LOG_PAGE)
3505 {
3506 /*
3507 * Page message; send the message to the page_log file and update the
3508 * job sheet count...
3509 */
3510
3511 if (job->sheets != NULL)
3512 {
3513 if (!strncasecmp(message, "total ", 6))
3514 {
3515 /*
3516 * Got a total count of pages from a backend or filter...
3517 */
3518
3519 copies = atoi(message + 6);
3520 copies -= job->sheets->values[0].integer; /* Just track the delta */
3521 }
3522 else if (!sscanf(message, "%*d%d", &copies))
3523 copies = 1;
3524
3525 job->sheets->values[0].integer += copies;
3526
3527 if (job->printer->page_limit)
3528 {
3529 cupsd_quota_t *q = cupsdUpdateQuota(job->printer, job->username,
3530 copies, 0);
3531
3532 #ifdef __APPLE__
3533 if (AppleQuotas && q->page_count == -3)
3534 {
3535 /*
3536 * Quota limit exceeded, cancel job in progress immediately...
3537 */
3538
3539 cupsdLogMessage(CUPSD_LOG_INFO,
3540 "[Job %d] Canceled because pages exceed user %s "
3541 "quota limit on printer %s (%s).",
3542 job->id, job->username, job->printer->name,
3543 job->printer->info);
3544
3545 cupsdCancelJob(job, 1, IPP_JOB_CANCELED);
3546 return;
3547 }
3548 #else
3549 (void)q;
3550 #endif /* __APPLE__ */
3551 }
3552 }
3553
3554 cupsdLogPage(job, message);
3555
3556 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS, job->printer, job,
3557 "Printed %d page(s).", job->sheets->values[0].integer);
3558 }
3559 else if (loglevel == CUPSD_LOG_STATE)
3560 {
3561 if (!strcmp(message, "paused"))
3562 cupsdStopPrinter(job->printer, 1);
3563 else
3564 {
3565 cupsdSetPrinterReasons(job->printer, message);
3566 cupsdAddPrinterHistory(job->printer);
3567 event |= CUPSD_EVENT_PRINTER_STATE_CHANGED;
3568 }
3569
3570 update_job_attrs(job);
3571 }
3572 else if (loglevel == CUPSD_LOG_ATTR)
3573 {
3574 /*
3575 * Set attribute(s)...
3576 */
3577
3578 int num_attrs; /* Number of attributes */
3579 cups_option_t *attrs; /* Attributes */
3580 const char *attr; /* Attribute */
3581
3582
3583 num_attrs = cupsParseOptions(message, 0, &attrs);
3584
3585 if ((attr = cupsGetOption("auth-info-required", num_attrs,
3586 attrs)) != NULL)
3587 cupsdSetAuthInfoRequired(job->printer, attr, NULL);
3588
3589 if ((attr = cupsGetOption("printer-alert", num_attrs, attrs)) != NULL)
3590 {
3591 cupsdSetString(&job->printer->alert, attr);
3592 event |= CUPSD_EVENT_PRINTER_STATE_CHANGED;
3593 }
3594
3595 if ((attr = cupsGetOption("printer-alert-description", num_attrs,
3596 attrs)) != NULL)
3597 {
3598 cupsdSetString(&job->printer->alert_description, attr);
3599 event |= CUPSD_EVENT_PRINTER_STATE_CHANGED;
3600 }
3601
3602 cupsFreeOptions(num_attrs, attrs);
3603 }
3604 #ifdef __APPLE__
3605 else if (!strncmp(message, "recoverable:", 12))
3606 {
3607 cupsdSetPrinterReasons(job->printer,
3608 "+com.apple.print.recoverable-warning");
3609
3610 ptr = message + 12;
3611 while (isspace(*ptr & 255))
3612 ptr ++;
3613
3614 cupsdSetString(&job->printer->recoverable, ptr);
3615 cupsdAddPrinterHistory(job->printer);
3616 event |= CUPSD_EVENT_PRINTER_STATE_CHANGED;
3617 }
3618 else if (!strncmp(message, "recovered:", 10))
3619 {
3620 cupsdSetPrinterReasons(job->printer,
3621 "-com.apple.print.recoverable-warning");
3622
3623 ptr = message + 10;
3624 while (isspace(*ptr & 255))
3625 ptr ++;
3626
3627 cupsdSetString(&job->printer->recoverable, ptr);
3628 cupsdAddPrinterHistory(job->printer);
3629 event |= CUPSD_EVENT_PRINTER_STATE_CHANGED;
3630 }
3631 #endif /* __APPLE__ */
3632 else if (loglevel <= job->status_level)
3633 {
3634 /*
3635 * Some message to show in the printer-state-message attribute...
3636 */
3637
3638 if (loglevel != CUPSD_LOG_NOTICE)
3639 job->status_level = loglevel;
3640
3641 strlcpy(job->printer->state_message, message,
3642 sizeof(job->printer->state_message));
3643 cupsdAddPrinterHistory(job->printer);
3644 event |= CUPSD_EVENT_PRINTER_STATE_CHANGED;
3645
3646 update_job_attrs(job);
3647 }
3648
3649 if (!strchr(job->status_buffer->buffer, '\n'))
3650 break;
3651 }
3652
3653 if ((event & CUPSD_EVENT_PRINTER_STATE_CHANGED))
3654 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE_CHANGED, job->printer, NULL,
3655 (job->printer->type & CUPS_PRINTER_CLASS) ?
3656 "Class \"%s\" state changed." :
3657 "Printer \"%s\" state changed.",
3658 job->printer->name);
3659
3660 if (ptr == NULL && !job->status_buffer->bufused)
3661 {
3662 /*
3663 * See if all of the filters and the backend have returned their
3664 * exit statuses.
3665 */
3666
3667 for (i = 0; job->filters[i] < 0; i ++);
3668
3669 if (job->filters[i])
3670 return;
3671
3672 if (job->current_file >= job->num_files && job->backend > 0)
3673 return;
3674
3675 /*
3676 * Handle the end of job stuff...
3677 */
3678
3679 cupsdFinishJob(job);
3680 }
3681 }
3682
3683
3684 /*
3685 * 'update_job_attrs()' - Update the job-printer-* attributes.
3686 */
3687
3688 void
3689 update_job_attrs(cupsd_job_t *job) /* I - Job to update */
3690 {
3691 int i; /* Looping var */
3692 int num_reasons; /* Actual number of reasons */
3693 const char * const *reasons; /* Reasons */
3694 static const char *none = "none", /* "none" */
3695 *paused = "paused";
3696 /* "paused" */
3697
3698
3699 /*
3700 * Get/create the job-printer-state-* attributes...
3701 */
3702
3703 if (!job->printer_message)
3704 {
3705 if ((job->printer_message = ippFindAttribute(job->attrs,
3706 "job-printer-state-message",
3707 IPP_TAG_TEXT)) == NULL)
3708 job->printer_message = ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_TEXT,
3709 "job-printer-state-message",
3710 NULL, "");
3711 }
3712
3713 if (!job->printer_reasons)
3714 job->printer_reasons = ippFindAttribute(job->attrs,
3715 "job-printer-state-reasons",
3716 IPP_TAG_KEYWORD);
3717
3718 /*
3719 * If the job isn't printing, return now...
3720 */
3721
3722 if (!job->printer)
3723 return;
3724
3725 /*
3726 * Otherwise copy the printer-state-message value...
3727 */
3728
3729 if (job->printer->state_message[0])
3730 cupsdSetString(&(job->printer_message->values[0].string.text),
3731 job->printer->state_message);
3732
3733 /*
3734 * ... and the printer-state-reasons value...
3735 */
3736
3737 if (job->printer->num_reasons == 0)
3738 {
3739 num_reasons = 1;
3740 reasons = job->printer->state == IPP_PRINTER_STOPPED ? &paused : &none;
3741 }
3742 else
3743 {
3744 num_reasons = job->printer->num_reasons;
3745 reasons = (const char * const *)job->printer->reasons;
3746 }
3747
3748 if (!job->printer_reasons || job->printer_reasons->num_values != num_reasons)
3749 {
3750 ippDeleteAttribute(job->attrs, job->printer_reasons);
3751
3752 job->printer_reasons = ippAddStrings(job->attrs,
3753 IPP_TAG_JOB, IPP_TAG_KEYWORD,
3754 "job-printer-state-reasons",
3755 num_reasons, NULL, NULL);
3756 }
3757
3758 for (i = 0; i < num_reasons; i ++)
3759 cupsdSetString(&(job->printer_reasons->values[i].string.text), reasons[i]);
3760 }
3761
3762
3763 /*
3764 * End of "$Id: job.c 6689 2007-07-18 23:52:15Z mike $".
3765 */