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