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