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