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