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