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