]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/job.c
Merge changes from CUPS trunk, r6739.
[thirdparty/cups.git] / scheduler / job.c
1 /*
2 * "$Id: job.c 6735 2007-07-26 18:38:29Z 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 * First erase the credential cache file, then clear the string referencing
1797 * it. We know the filename since the string will be of the form
1798 * "KRB5CCNAME=FILE:/foo/bar"...
1799 */
1800
1801 if (cupsdRemoveFile(job->ccname + 16) && errno != ENOENT)
1802 cupsdLogMessage(CUPSD_LOG_ERROR,
1803 "[Job %d] Unable to remove Kerberos credential cache: %s",
1804 job->id, strerror(errno));
1805
1806 cupsdClearString(&job->ccname);
1807 }
1808 #endif /* HAVE_GSSAPI */
1809
1810 if (job->num_files > 0)
1811 {
1812 free(job->compressions);
1813 free(job->filetypes);
1814 }
1815
1816 ippDelete(job->attrs);
1817
1818 free(job);
1819 }
1820
1821
1822 /*
1823 * 'ipp_length()' - Compute the size of the buffer needed to hold
1824 * the textual IPP attributes.
1825 */
1826
1827 static int /* O - Size of attribute buffer */
1828 ipp_length(ipp_t *ipp) /* I - IPP request */
1829 {
1830 int bytes; /* Number of bytes */
1831 int i; /* Looping var */
1832 ipp_attribute_t *attr; /* Current attribute */
1833
1834
1835 /*
1836 * Loop through all attributes...
1837 */
1838
1839 bytes = 0;
1840
1841 for (attr = ipp->attrs; attr != NULL; attr = attr->next)
1842 {
1843 /*
1844 * Skip attributes that won't be sent to filters...
1845 */
1846
1847 if (attr->value_tag == IPP_TAG_MIMETYPE ||
1848 attr->value_tag == IPP_TAG_NAMELANG ||
1849 attr->value_tag == IPP_TAG_TEXTLANG ||
1850 attr->value_tag == IPP_TAG_URI ||
1851 attr->value_tag == IPP_TAG_URISCHEME)
1852 continue;
1853
1854 if (strncmp(attr->name, "time-", 5) == 0)
1855 continue;
1856
1857 /*
1858 * Add space for a leading space and commas between each value.
1859 * For the first attribute, the leading space isn't used, so the
1860 * extra byte can be used as the nul terminator...
1861 */
1862
1863 bytes ++; /* " " separator */
1864 bytes += attr->num_values; /* "," separators */
1865
1866 /*
1867 * Boolean attributes appear as "foo,nofoo,foo,nofoo", while
1868 * other attributes appear as "foo=value1,value2,...,valueN".
1869 */
1870
1871 if (attr->value_tag != IPP_TAG_BOOLEAN)
1872 bytes += strlen(attr->name);
1873 else
1874 bytes += attr->num_values * strlen(attr->name);
1875
1876 /*
1877 * Now add the size required for each value in the attribute...
1878 */
1879
1880 switch (attr->value_tag)
1881 {
1882 case IPP_TAG_INTEGER :
1883 case IPP_TAG_ENUM :
1884 /*
1885 * Minimum value of a signed integer is -2147483647, or 11 digits.
1886 */
1887
1888 bytes += attr->num_values * 11;
1889 break;
1890
1891 case IPP_TAG_BOOLEAN :
1892 /*
1893 * Add two bytes for each false ("no") value...
1894 */
1895
1896 for (i = 0; i < attr->num_values; i ++)
1897 if (!attr->values[i].boolean)
1898 bytes += 2;
1899 break;
1900
1901 case IPP_TAG_RANGE :
1902 /*
1903 * A range is two signed integers separated by a hyphen, or
1904 * 23 characters max.
1905 */
1906
1907 bytes += attr->num_values * 23;
1908 break;
1909
1910 case IPP_TAG_RESOLUTION :
1911 /*
1912 * A resolution is two signed integers separated by an "x" and
1913 * suffixed by the units, or 26 characters max.
1914 */
1915
1916 bytes += attr->num_values * 26;
1917 break;
1918
1919 case IPP_TAG_STRING :
1920 case IPP_TAG_TEXT :
1921 case IPP_TAG_NAME :
1922 case IPP_TAG_KEYWORD :
1923 case IPP_TAG_CHARSET :
1924 case IPP_TAG_LANGUAGE :
1925 case IPP_TAG_URI :
1926 /*
1927 * Strings can contain characters that need quoting. We need
1928 * at least 2 * len + 2 characters to cover the quotes and
1929 * any backslashes in the string.
1930 */
1931
1932 for (i = 0; i < attr->num_values; i ++)
1933 bytes += 2 * strlen(attr->values[i].string.text) + 2;
1934 break;
1935
1936 default :
1937 break; /* anti-compiler-warning-code */
1938 }
1939 }
1940
1941 return (bytes);
1942 }
1943
1944
1945 /*
1946 * 'load_job_cache()' - Load jobs from the job.cache file.
1947 */
1948
1949 static void
1950 load_job_cache(const char *filename) /* I - job.cache filename */
1951 {
1952 cups_file_t *fp; /* job.cache file */
1953 char line[1024], /* Line buffer */
1954 *value; /* Value on line */
1955 int linenum; /* Line number in file */
1956 cupsd_job_t *job; /* Current job */
1957 int jobid; /* Job ID */
1958 char jobfile[1024]; /* Job filename */
1959
1960
1961 /*
1962 * Open the job.cache file...
1963 */
1964
1965 if ((fp = cupsFileOpen(filename, "r")) == NULL)
1966 {
1967 if (errno != ENOENT)
1968 cupsdLogMessage(CUPSD_LOG_ERROR,
1969 "Unable to open job cache file \"%s\": %s",
1970 filename, strerror(errno));
1971
1972 load_request_root();
1973
1974 return;
1975 }
1976
1977 /*
1978 * Read entries from the job cache file and create jobs as needed.
1979 */
1980
1981 cupsdLogMessage(CUPSD_LOG_INFO, "Loading job cache file \"%s\"...",
1982 filename);
1983
1984 linenum = 0;
1985 job = NULL;
1986
1987 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
1988 {
1989 if (!strcasecmp(line, "NextJobId"))
1990 {
1991 if (value)
1992 NextJobId = atoi(value);
1993 }
1994 else if (!strcasecmp(line, "<Job"))
1995 {
1996 if (job)
1997 {
1998 cupsdLogMessage(CUPSD_LOG_ERROR, "Missing </Job> directive on line %d!",
1999 linenum);
2000 continue;
2001 }
2002
2003 if (!value)
2004 {
2005 cupsdLogMessage(CUPSD_LOG_ERROR, "Missing job ID on line %d!", linenum);
2006 continue;
2007 }
2008
2009 jobid = atoi(value);
2010
2011 if (jobid < 1)
2012 {
2013 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad job ID %d on line %d!", jobid,
2014 linenum);
2015 continue;
2016 }
2017
2018 snprintf(jobfile, sizeof(jobfile), "%s/c%05d", RequestRoot, jobid);
2019 if (access(jobfile, 0))
2020 {
2021 cupsdLogMessage(CUPSD_LOG_ERROR, "[Job %d] Files have gone away!",
2022 jobid);
2023 continue;
2024 }
2025
2026 job = calloc(1, sizeof(cupsd_job_t));
2027 if (!job)
2028 {
2029 cupsdLogMessage(CUPSD_LOG_EMERG,
2030 "[Job %d] Unable to allocate memory for job!", jobid);
2031 break;
2032 }
2033
2034 job->id = jobid;
2035 job->back_pipes[0] = -1;
2036 job->back_pipes[1] = -1;
2037 job->print_pipes[0] = -1;
2038 job->print_pipes[1] = -1;
2039 job->side_pipes[0] = -1;
2040 job->side_pipes[1] = -1;
2041 job->status_pipes[0] = -1;
2042 job->status_pipes[1] = -1;
2043
2044 cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] Loading from cache...", job->id);
2045 }
2046 else if (!job)
2047 {
2048 cupsdLogMessage(CUPSD_LOG_ERROR,
2049 "Missing <Job #> directive on line %d!", linenum);
2050 continue;
2051 }
2052 else if (!strcasecmp(line, "</Job>"))
2053 {
2054 cupsArrayAdd(Jobs, job);
2055
2056 if (job->state_value <= IPP_JOB_STOPPED)
2057 {
2058 cupsArrayAdd(ActiveJobs, job);
2059 cupsdLoadJob(job);
2060 }
2061
2062 job = NULL;
2063 }
2064 else if (!value)
2065 {
2066 cupsdLogMessage(CUPSD_LOG_ERROR, "Missing value on line %d!", linenum);
2067 continue;
2068 }
2069 else if (!strcasecmp(line, "State"))
2070 {
2071 job->state_value = (ipp_jstate_t)atoi(value);
2072
2073 if (job->state_value < IPP_JOB_PENDING)
2074 job->state_value = IPP_JOB_PENDING;
2075 else if (job->state_value > IPP_JOB_COMPLETED)
2076 job->state_value = IPP_JOB_COMPLETED;
2077 }
2078 else if (!strcasecmp(line, "Priority"))
2079 {
2080 job->priority = atoi(value);
2081 }
2082 else if (!strcasecmp(line, "Username"))
2083 {
2084 cupsdSetString(&job->username, value);
2085 }
2086 else if (!strcasecmp(line, "Destination"))
2087 {
2088 cupsdSetString(&job->dest, value);
2089 }
2090 else if (!strcasecmp(line, "DestType"))
2091 {
2092 job->dtype = (cups_ptype_t)atoi(value);
2093 }
2094 else if (!strcasecmp(line, "NumFiles"))
2095 {
2096 job->num_files = atoi(value);
2097
2098 if (job->num_files < 0)
2099 {
2100 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad NumFiles value %d on line %d!",
2101 job->num_files, linenum);
2102 job->num_files = 0;
2103 continue;
2104 }
2105
2106 if (job->num_files > 0)
2107 {
2108 snprintf(jobfile, sizeof(jobfile), "%s/d%05d-001", RequestRoot,
2109 job->id);
2110 if (access(jobfile, 0))
2111 {
2112 cupsdLogMessage(CUPSD_LOG_INFO,
2113 "[Job %d] Data files have gone away!", job->id);
2114 job->num_files = 0;
2115 continue;
2116 }
2117
2118 job->filetypes = calloc(job->num_files, sizeof(mime_type_t *));
2119 job->compressions = calloc(job->num_files, sizeof(int));
2120
2121 if (!job->filetypes || !job->compressions)
2122 {
2123 cupsdLogMessage(CUPSD_LOG_EMERG,
2124 "[Job %d] Unable to allocate memory for %d files!",
2125 job->id, job->num_files);
2126 break;
2127 }
2128 }
2129 }
2130 else if (!strcasecmp(line, "File"))
2131 {
2132 int number, /* File number */
2133 compression; /* Compression value */
2134 char super[MIME_MAX_SUPER], /* MIME super type */
2135 type[MIME_MAX_TYPE]; /* MIME type */
2136
2137
2138 if (sscanf(value, "%d%*[ \t]%15[^/]/%255s%d", &number, super, type,
2139 &compression) != 4)
2140 {
2141 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad File on line %d!", linenum);
2142 continue;
2143 }
2144
2145 if (number < 1 || number > job->num_files)
2146 {
2147 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad File number %d on line %d!",
2148 number, linenum);
2149 continue;
2150 }
2151
2152 number --;
2153
2154 job->compressions[number] = compression;
2155 job->filetypes[number] = mimeType(MimeDatabase, super, type);
2156
2157 if (!job->filetypes[number])
2158 {
2159 /*
2160 * If the original MIME type is unknown, auto-type it!
2161 */
2162
2163 cupsdLogMessage(CUPSD_LOG_ERROR,
2164 "[Job %d] Unknown MIME type %s/%s for file %d!",
2165 job->id, super, type, number + 1);
2166
2167 snprintf(jobfile, sizeof(jobfile), "%s/d%05d-%03d", RequestRoot,
2168 job->id, number + 1);
2169 job->filetypes[number] = mimeFileType(MimeDatabase, jobfile, NULL,
2170 job->compressions + number);
2171
2172 /*
2173 * If that didn't work, assume it is raw...
2174 */
2175
2176 if (!job->filetypes[number])
2177 job->filetypes[number] = mimeType(MimeDatabase, "application",
2178 "vnd.cups-raw");
2179 }
2180 }
2181 else
2182 cupsdLogMessage(CUPSD_LOG_ERROR, "Unknown %s directive on line %d!",
2183 line, linenum);
2184 }
2185
2186 cupsFileClose(fp);
2187 }
2188
2189
2190 /*
2191 * 'load_next_job_id()' - Load the NextJobId value from the job.cache file.
2192 */
2193
2194 static void
2195 load_next_job_id(const char *filename) /* I - job.cache filename */
2196 {
2197 cups_file_t *fp; /* job.cache file */
2198 char line[1024], /* Line buffer */
2199 *value; /* Value on line */
2200 int linenum; /* Line number in file */
2201 int next_job_id; /* NextJobId value from line */
2202
2203
2204 /*
2205 * Read the NextJobId directive from the job.cache file and use
2206 * the value (if any).
2207 */
2208
2209 if ((fp = cupsFileOpen(filename, "r")) == NULL)
2210 {
2211 if (errno != ENOENT)
2212 cupsdLogMessage(CUPSD_LOG_ERROR,
2213 "Unable to open job cache file \"%s\": %s",
2214 filename, strerror(errno));
2215
2216 return;
2217 }
2218
2219 cupsdLogMessage(CUPSD_LOG_INFO,
2220 "Loading NextJobId from job cache file \"%s\"...", filename);
2221
2222 linenum = 0;
2223
2224 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
2225 {
2226 if (!strcasecmp(line, "NextJobId"))
2227 {
2228 if (value)
2229 {
2230 next_job_id = atoi(value);
2231
2232 if (next_job_id > NextJobId)
2233 NextJobId = next_job_id;
2234 }
2235 break;
2236 }
2237 }
2238
2239 cupsFileClose(fp);
2240 }
2241
2242
2243 /*
2244 * 'load_request_root()' - Load jobs from the RequestRoot directory.
2245 */
2246
2247 static void
2248 load_request_root(void)
2249 {
2250 cups_dir_t *dir; /* Directory */
2251 cups_dentry_t *dent; /* Directory entry */
2252 cupsd_job_t *job; /* New job */
2253
2254
2255 /*
2256 * Open the requests directory...
2257 */
2258
2259 cupsdLogMessage(CUPSD_LOG_DEBUG, "Scanning %s for jobs...", RequestRoot);
2260
2261 if ((dir = cupsDirOpen(RequestRoot)) == NULL)
2262 {
2263 cupsdLogMessage(CUPSD_LOG_ERROR,
2264 "Unable to open spool directory \"%s\": %s",
2265 RequestRoot, strerror(errno));
2266 return;
2267 }
2268
2269 /*
2270 * Read all the c##### files...
2271 */
2272
2273 while ((dent = cupsDirRead(dir)) != NULL)
2274 if (strlen(dent->filename) >= 6 && dent->filename[0] == 'c')
2275 {
2276 /*
2277 * Allocate memory for the job...
2278 */
2279
2280 if ((job = calloc(sizeof(cupsd_job_t), 1)) == NULL)
2281 {
2282 cupsdLogMessage(CUPSD_LOG_ERROR, "Ran out of memory for jobs!");
2283 cupsDirClose(dir);
2284 return;
2285 }
2286
2287 /*
2288 * Assign the job ID...
2289 */
2290
2291 job->id = atoi(dent->filename + 1);
2292 job->back_pipes[0] = -1;
2293 job->back_pipes[1] = -1;
2294 job->print_pipes[0] = -1;
2295 job->print_pipes[1] = -1;
2296 job->side_pipes[0] = -1;
2297 job->side_pipes[1] = -1;
2298 job->status_pipes[0] = -1;
2299 job->status_pipes[1] = -1;
2300
2301 if (job->id >= NextJobId)
2302 NextJobId = job->id + 1;
2303
2304 /*
2305 * Load the job...
2306 */
2307
2308 cupsdLoadJob(job);
2309
2310 /*
2311 * Insert the job into the array, sorting by job priority and ID...
2312 */
2313
2314 cupsArrayAdd(Jobs, job);
2315
2316 if (job->state_value <= IPP_JOB_STOPPED)
2317 cupsArrayAdd(ActiveJobs, job);
2318 else
2319 unload_job(job);
2320 }
2321
2322 cupsDirClose(dir);
2323 }
2324
2325
2326 /*
2327 * 'set_time()' - Set one of the "time-at-xyz" attributes...
2328 */
2329
2330 static void
2331 set_time(cupsd_job_t *job, /* I - Job to update */
2332 const char *name) /* I - Name of attribute */
2333 {
2334 ipp_attribute_t *attr; /* Time attribute */
2335
2336
2337 if ((attr = ippFindAttribute(job->attrs, name, IPP_TAG_ZERO)) != NULL)
2338 {
2339 attr->value_tag = IPP_TAG_INTEGER;
2340 attr->values[0].integer = time(NULL);
2341 }
2342 }
2343
2344
2345 /*
2346 * 'set_hold_until()' - Set the hold time and update job-hold-until attribute...
2347 */
2348
2349 static void
2350 set_hold_until(cupsd_job_t *job, /* I - Job to update */
2351 time_t holdtime) /* I - Hold until time */
2352 {
2353 ipp_attribute_t *attr; /* job-hold-until attribute */
2354 struct tm *holddate; /* Hold date */
2355 char holdstr[64]; /* Hold time */
2356
2357
2358 /*
2359 * Set the hold_until value and hold the job...
2360 */
2361
2362 cupsdLogMessage(CUPSD_LOG_DEBUG, "set_hold_until: hold_until = %d",
2363 (int)holdtime);
2364
2365 job->state->values[0].integer = IPP_JOB_HELD;
2366 job->state_value = IPP_JOB_HELD;
2367 job->hold_until = holdtime;
2368
2369 /*
2370 * Update the job-hold-until attribute with a string representing GMT
2371 * time (HH:MM:SS)...
2372 */
2373
2374 holddate = gmtime(&holdtime);
2375 snprintf(holdstr, sizeof(holdstr), "%d:%d:%d", holddate->tm_hour,
2376 holddate->tm_min, holddate->tm_sec);
2377
2378 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
2379 IPP_TAG_KEYWORD)) == NULL)
2380 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
2381
2382 /*
2383 * Either add the attribute or update the value of the existing one
2384 */
2385
2386 if (attr == NULL)
2387 attr = ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_KEYWORD,
2388 "job-hold-until", NULL, holdstr);
2389 else
2390 cupsdSetString(&attr->values[0].string.text, holdstr);
2391
2392 cupsdSaveJob(job);
2393 }
2394
2395
2396 /*
2397 * 'start_job()' - Start a print job.
2398 */
2399
2400 static void
2401 start_job(cupsd_job_t *job, /* I - Job ID */
2402 cupsd_printer_t *printer) /* I - Printer to print job */
2403 {
2404 int i; /* Looping var */
2405 int slot; /* Pipe slot */
2406 cups_array_t *filters, /* Filters for job */
2407 *prefilters; /* Filters with prefilters */
2408 mime_filter_t *filter, /* Current filter */
2409 *prefilter, /* Prefilter */
2410 port_monitor; /* Port monitor filter */
2411 char method[255], /* Method for output */
2412 *optptr, /* Pointer to options */
2413 *valptr; /* Pointer in value string */
2414 ipp_attribute_t *attr; /* Current attribute */
2415 struct stat backinfo; /* Backend file information */
2416 int backroot; /* Run backend as root? */
2417 int pid; /* Process ID of new filter process */
2418 int banner_page; /* 1 if banner page, 0 otherwise */
2419 int filterfds[2][2];/* Pipes used between filters */
2420 int envc; /* Number of environment variables */
2421 char **argv, /* Filter command-line arguments */
2422 sani_uri[1024], /* Sanitized DEVICE_URI env var */
2423 filename[1024], /* Job filename */
2424 command[1024], /* Full path to command */
2425 jobid[255], /* Job ID string */
2426 title[IPP_MAX_NAME],
2427 /* Job title string */
2428 copies[255], /* # copies string */
2429 *envp[MAX_ENV + 15],
2430 /* Environment variables */
2431 charset[255], /* CHARSET env variable */
2432 class_name[255],/* CLASS env variable */
2433 classification[1024],
2434 /* CLASSIFICATION env variable */
2435 content_type[1024],
2436 /* CONTENT_TYPE env variable */
2437 device_uri[1024],
2438 /* DEVICE_URI env variable */
2439 final_content_type[1024],
2440 /* FINAL_CONTENT_TYPE env variable */
2441 lang[255], /* LANG env variable */
2442 ppd[1024], /* PPD env variable */
2443 printer_name[255],
2444 /* PRINTER env variable */
2445 rip_max_cache[255];
2446 /* RIP_MAX_CACHE env variable */
2447 static char *options = NULL;/* Full list of options */
2448 static int optlength = 0; /* Length of option buffer */
2449
2450
2451 cupsdLogMessage(CUPSD_LOG_DEBUG2, "[Job %d] start_job: file = %d/%d",
2452 job->id, job->current_file, job->num_files);
2453
2454 if (job->num_files == 0)
2455 {
2456 cupsdLogMessage(CUPSD_LOG_ERROR, "[Job %d] No files, canceling job!",
2457 job->id);
2458
2459 cupsdCancelJob(job, 0, IPP_JOB_ABORTED);
2460 return;
2461 }
2462
2463 /*
2464 * Figure out what filters are required to convert from
2465 * the source to the destination type...
2466 */
2467
2468 filters = NULL;
2469 job->cost = 0;
2470
2471 if (printer->raw)
2472 {
2473 /*
2474 * Remote jobs and raw queues go directly to the printer without
2475 * filtering...
2476 */
2477
2478 cupsdLogMessage(CUPSD_LOG_DEBUG,
2479 "[Job %d] Sending job to queue tagged as raw...", job->id);
2480
2481 filters = NULL;
2482 }
2483 else
2484 {
2485 /*
2486 * Local jobs get filtered...
2487 */
2488
2489 filters = mimeFilter(MimeDatabase, job->filetypes[job->current_file],
2490 printer->filetype, &(job->cost));
2491
2492 if (!filters)
2493 {
2494 cupsdLogMessage(CUPSD_LOG_ERROR,
2495 "[Job %d] Unable to convert file %d to printable format!",
2496 job->current_file, job->id);
2497 cupsdLogMessage(CUPSD_LOG_INFO,
2498 "Hint: Do you have ESP Ghostscript installed?");
2499
2500 if (LogLevel < CUPSD_LOG_DEBUG)
2501 cupsdLogMessage(CUPSD_LOG_INFO,
2502 "Hint: Try setting the LogLevel to \"debug\".");
2503
2504 job->current_file ++;
2505
2506 if (job->current_file == job->num_files)
2507 cupsdCancelJob(job, 0, IPP_JOB_ABORTED);
2508
2509 return;
2510 }
2511
2512 /*
2513 * Remove NULL ("-") filters...
2514 */
2515
2516 for (filter = (mime_filter_t *)cupsArrayFirst(filters);
2517 filter;
2518 filter = (mime_filter_t *)cupsArrayNext(filters))
2519 if (!strcmp(filter->filter, "-"))
2520 cupsArrayRemove(filters, filter);
2521
2522 if (cupsArrayCount(filters) == 0)
2523 {
2524 cupsArrayDelete(filters);
2525 filters = NULL;
2526 }
2527
2528 /*
2529 * If this printer has any pre-filters, insert the required pre-filter
2530 * in the filters array...
2531 */
2532
2533 if (printer->prefiltertype && filters)
2534 {
2535 prefilters = cupsArrayNew(NULL, NULL);
2536
2537 for (filter = (mime_filter_t *)cupsArrayFirst(filters);
2538 filter;
2539 filter = (mime_filter_t *)cupsArrayNext(filters))
2540 {
2541 if ((prefilter = mimeFilterLookup(MimeDatabase, filter->src,
2542 printer->prefiltertype)))
2543 {
2544 cupsArrayAdd(prefilters, prefilter);
2545 job->cost += prefilter->cost;
2546 }
2547
2548 cupsArrayAdd(prefilters, filter);
2549 }
2550
2551 cupsArrayDelete(filters);
2552 filters = prefilters;
2553 }
2554 }
2555
2556 /*
2557 * Set a minimum cost of 100 for all jobs so that FilterLimit
2558 * works with raw queues and other low-cost paths.
2559 */
2560
2561 if (job->cost < 100)
2562 job->cost = 100;
2563
2564 /*
2565 * See if the filter cost is too high...
2566 */
2567
2568 if ((FilterLevel + job->cost) > FilterLimit && FilterLevel > 0 &&
2569 FilterLimit > 0)
2570 {
2571 /*
2572 * Don't print this job quite yet...
2573 */
2574
2575 cupsArrayDelete(filters);
2576
2577 cupsdLogMessage(CUPSD_LOG_INFO,
2578 "[Job %d] Holding because filter limit has been reached.",
2579 job->id);
2580 cupsdLogMessage(CUPSD_LOG_DEBUG2,
2581 "[Job %d] start_job: file=%d, cost=%d, level=%d, limit=%d",
2582 job->id, job->current_file, job->cost, FilterLevel,
2583 FilterLimit);
2584 return;
2585 }
2586
2587 FilterLevel += job->cost;
2588
2589 /*
2590 * Add decompression/raw filter as needed...
2591 */
2592
2593 if ((!printer->raw && job->compressions[job->current_file]) ||
2594 (!filters && !printer->remote &&
2595 (job->num_files > 1 || !strncmp(printer->device_uri, "file:", 5))))
2596 {
2597 /*
2598 * Add gziptoany filter to the front of the list...
2599 */
2600
2601 if (!filters)
2602 filters = cupsArrayNew(NULL, NULL);
2603
2604 if (!cupsArrayInsert(filters, &gziptoany_filter))
2605 {
2606 cupsdLogMessage(CUPSD_LOG_ERROR,
2607 "[Job %d] Unable to add decompression filter - %s",
2608 job->id, strerror(errno));
2609
2610 cupsArrayDelete(filters);
2611
2612 job->current_file ++;
2613
2614 if (job->current_file == job->num_files)
2615 cupsdCancelJob(job, 0, IPP_JOB_ABORTED);
2616
2617 return;
2618 }
2619 }
2620
2621 /*
2622 * Add port monitor, if any...
2623 */
2624
2625 if (printer->port_monitor)
2626 {
2627 /*
2628 * Add port monitor to the end of the list...
2629 */
2630
2631 if (!filters)
2632 filters = cupsArrayNew(NULL, NULL);
2633
2634 if (!cupsArrayAdd(filters, &port_monitor))
2635 {
2636 cupsdLogMessage(CUPSD_LOG_ERROR,
2637 "[Job %d] Unable to add port monitor - %s",
2638 job->id, strerror(errno));
2639
2640 cupsArrayDelete(filters);
2641
2642 job->current_file ++;
2643
2644 if (job->current_file == job->num_files)
2645 cupsdCancelJob(job, 0, IPP_JOB_ABORTED);
2646
2647 return;
2648 }
2649
2650 snprintf(port_monitor.filter, sizeof(port_monitor.filter),
2651 "%s/monitor/%s", ServerBin, printer->port_monitor);
2652 }
2653
2654 /*
2655 * Update the printer and job state to "processing"...
2656 */
2657
2658 job->state->values[0].integer = IPP_JOB_PROCESSING;
2659 job->state_value = IPP_JOB_PROCESSING;
2660
2661 job->status = 0;
2662 job->printer = printer;
2663 printer->job = job;
2664
2665 cupsdSetPrinterState(printer, IPP_PRINTER_PROCESSING, 0);
2666
2667 if (job->current_file == 0)
2668 {
2669 /*
2670 * Set the processing time...
2671 */
2672
2673 set_time(job, "time-at-processing");
2674
2675 /*
2676 * Create the backchannel pipes and make them non-blocking...
2677 */
2678
2679 cupsdOpenPipe(job->back_pipes);
2680
2681 fcntl(job->back_pipes[0], F_SETFL,
2682 fcntl(job->back_pipes[0], F_GETFL) | O_NONBLOCK);
2683
2684 fcntl(job->back_pipes[1], F_SETFL,
2685 fcntl(job->back_pipes[1], F_GETFL) | O_NONBLOCK);
2686
2687 /*
2688 * Create the side-channel pipes and make them non-blocking...
2689 */
2690
2691 socketpair(AF_LOCAL, SOCK_STREAM, 0, job->side_pipes);
2692
2693 fcntl(job->side_pipes[0], F_SETFL,
2694 fcntl(job->side_pipes[0], F_GETFL) | O_NONBLOCK);
2695
2696 fcntl(job->side_pipes[1], F_SETFL,
2697 fcntl(job->side_pipes[1], F_GETFL) | O_NONBLOCK);
2698 }
2699
2700 /*
2701 * Determine if we are printing a banner page or not...
2702 */
2703
2704 if (job->job_sheets == NULL)
2705 {
2706 cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] No job-sheets attribute.",
2707 job->id);
2708 if ((job->job_sheets =
2709 ippFindAttribute(job->attrs, "job-sheets", IPP_TAG_ZERO)) != NULL)
2710 cupsdLogMessage(CUPSD_LOG_DEBUG,
2711 "[Job %d] ... but someone added one without setting "
2712 "job_sheets!", job->id);
2713 }
2714 else if (job->job_sheets->num_values == 1)
2715 cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] job-sheets=%s", job->id,
2716 job->job_sheets->values[0].string.text);
2717 else
2718 cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] job-sheets=%s,%s", job->id,
2719 job->job_sheets->values[0].string.text,
2720 job->job_sheets->values[1].string.text);
2721
2722 if (printer->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT))
2723 banner_page = 0;
2724 else if (job->job_sheets == NULL)
2725 banner_page = 0;
2726 else if (strcasecmp(job->job_sheets->values[0].string.text, "none") != 0 &&
2727 job->current_file == 0)
2728 banner_page = 1;
2729 else if (job->job_sheets->num_values > 1 &&
2730 strcasecmp(job->job_sheets->values[1].string.text, "none") != 0 &&
2731 job->current_file == (job->num_files - 1))
2732 banner_page = 1;
2733 else
2734 banner_page = 0;
2735
2736 cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] banner_page = %d", job->id,
2737 banner_page);
2738
2739 /*
2740 * Building the options string is harder than it needs to be, but
2741 * for the moment we need to pass strings for command-line args and
2742 * not IPP attribute pointers... :)
2743 *
2744 * First allocate/reallocate the option buffer as needed...
2745 */
2746
2747 i = ipp_length(job->attrs);
2748
2749 if (i > optlength)
2750 {
2751 if (optlength == 0)
2752 optptr = malloc(i);
2753 else
2754 optptr = realloc(options, i);
2755
2756 if (optptr == NULL)
2757 {
2758 cupsdLogMessage(CUPSD_LOG_CRIT,
2759 "[Job %d] Unable to allocate %d bytes for option buffer!",
2760 job->id, i);
2761
2762 cupsArrayDelete(filters);
2763
2764 FilterLevel -= job->cost;
2765
2766 cupsdCancelJob(job, 0, IPP_JOB_ABORTED);
2767 return;
2768 }
2769
2770 options = optptr;
2771 optlength = i;
2772 }
2773
2774 /*
2775 * Now loop through the attributes and convert them to the textual
2776 * representation used by the filters...
2777 */
2778
2779 optptr = options;
2780 *optptr = '\0';
2781
2782 snprintf(title, sizeof(title), "%s-%d", printer->name, job->id);
2783 strcpy(copies, "1");
2784
2785 for (attr = job->attrs->attrs; attr != NULL; attr = attr->next)
2786 {
2787 if (!strcmp(attr->name, "copies") &&
2788 attr->value_tag == IPP_TAG_INTEGER)
2789 {
2790 /*
2791 * Don't use the # copies attribute if we are printing the job sheets...
2792 */
2793
2794 if (!banner_page)
2795 sprintf(copies, "%d", attr->values[0].integer);
2796 }
2797 else if (!strcmp(attr->name, "job-name") &&
2798 (attr->value_tag == IPP_TAG_NAME ||
2799 attr->value_tag == IPP_TAG_NAMELANG))
2800 strlcpy(title, attr->values[0].string.text, sizeof(title));
2801 else if (attr->group_tag == IPP_TAG_JOB)
2802 {
2803 /*
2804 * Filter out other unwanted attributes...
2805 */
2806
2807 if (attr->value_tag == IPP_TAG_MIMETYPE ||
2808 attr->value_tag == IPP_TAG_NAMELANG ||
2809 attr->value_tag == IPP_TAG_TEXTLANG ||
2810 (attr->value_tag == IPP_TAG_URI && strcmp(attr->name, "job-uuid")) ||
2811 attr->value_tag == IPP_TAG_URISCHEME ||
2812 attr->value_tag == IPP_TAG_BEGIN_COLLECTION) /* Not yet supported */
2813 continue;
2814
2815 if (!strncmp(attr->name, "time-", 5))
2816 continue;
2817
2818 if (!strncmp(attr->name, "job-", 4) && strcmp(attr->name, "job-uuid") &&
2819 !(printer->type & CUPS_PRINTER_REMOTE))
2820 continue;
2821
2822 if (!strncmp(attr->name, "job-", 4) &&
2823 strcmp(attr->name, "job-uuid") &&
2824 strcmp(attr->name, "job-billing") &&
2825 strcmp(attr->name, "job-sheets") &&
2826 strcmp(attr->name, "job-hold-until") &&
2827 strcmp(attr->name, "job-priority"))
2828 continue;
2829
2830 if ((!strcmp(attr->name, "page-label") ||
2831 !strcmp(attr->name, "page-border") ||
2832 !strncmp(attr->name, "number-up", 9) ||
2833 !strcmp(attr->name, "page-ranges") ||
2834 !strcmp(attr->name, "page-set") ||
2835 !strcasecmp(attr->name, "AP_FIRSTPAGE_InputSlot") ||
2836 !strcasecmp(attr->name, "AP_FIRSTPAGE_ManualFeed")) &&
2837 banner_page)
2838 continue;
2839
2840 /*
2841 * Otherwise add them to the list...
2842 */
2843
2844 if (optptr > options)
2845 strlcat(optptr, " ", optlength - (optptr - options));
2846
2847 if (attr->value_tag != IPP_TAG_BOOLEAN)
2848 {
2849 strlcat(optptr, attr->name, optlength - (optptr - options));
2850 strlcat(optptr, "=", optlength - (optptr - options));
2851 }
2852
2853 for (i = 0; i < attr->num_values; i ++)
2854 {
2855 if (i)
2856 strlcat(optptr, ",", optlength - (optptr - options));
2857
2858 optptr += strlen(optptr);
2859
2860 switch (attr->value_tag)
2861 {
2862 case IPP_TAG_INTEGER :
2863 case IPP_TAG_ENUM :
2864 snprintf(optptr, optlength - (optptr - options),
2865 "%d", attr->values[i].integer);
2866 break;
2867
2868 case IPP_TAG_BOOLEAN :
2869 if (!attr->values[i].boolean)
2870 strlcat(optptr, "no", optlength - (optptr - options));
2871
2872 case IPP_TAG_NOVALUE :
2873 strlcat(optptr, attr->name,
2874 optlength - (optptr - options));
2875 break;
2876
2877 case IPP_TAG_RANGE :
2878 if (attr->values[i].range.lower == attr->values[i].range.upper)
2879 snprintf(optptr, optlength - (optptr - options) - 1,
2880 "%d", attr->values[i].range.lower);
2881 else
2882 snprintf(optptr, optlength - (optptr - options) - 1,
2883 "%d-%d", attr->values[i].range.lower,
2884 attr->values[i].range.upper);
2885 break;
2886
2887 case IPP_TAG_RESOLUTION :
2888 snprintf(optptr, optlength - (optptr - options) - 1,
2889 "%dx%d%s", attr->values[i].resolution.xres,
2890 attr->values[i].resolution.yres,
2891 attr->values[i].resolution.units == IPP_RES_PER_INCH ?
2892 "dpi" : "dpc");
2893 break;
2894
2895 case IPP_TAG_STRING :
2896 case IPP_TAG_TEXT :
2897 case IPP_TAG_NAME :
2898 case IPP_TAG_KEYWORD :
2899 case IPP_TAG_CHARSET :
2900 case IPP_TAG_LANGUAGE :
2901 case IPP_TAG_URI :
2902 for (valptr = attr->values[i].string.text; *valptr;)
2903 {
2904 if (strchr(" \t\n\\\'\"", *valptr))
2905 *optptr++ = '\\';
2906 *optptr++ = *valptr++;
2907 }
2908
2909 *optptr = '\0';
2910 break;
2911
2912 default :
2913 break; /* anti-compiler-warning-code */
2914 }
2915 }
2916
2917 optptr += strlen(optptr);
2918 }
2919 }
2920
2921 /*
2922 * Build the command-line arguments for the filters. Each filter
2923 * has 6 or 7 arguments:
2924 *
2925 * argv[0] = printer
2926 * argv[1] = job ID
2927 * argv[2] = username
2928 * argv[3] = title
2929 * argv[4] = # copies
2930 * argv[5] = options
2931 * argv[6] = filename (optional; normally stdin)
2932 *
2933 * This allows legacy printer drivers that use the old System V
2934 * printing interface to be used by CUPS.
2935 *
2936 * For remote jobs, we send all of the files in the argument list.
2937 */
2938
2939 if (printer->remote && job->num_files > 1)
2940 argv = calloc(7 + job->num_files, sizeof(char *));
2941 else
2942 argv = calloc(8, sizeof(char *));
2943
2944 sprintf(jobid, "%d", job->id);
2945
2946 argv[0] = printer->name;
2947 argv[1] = jobid;
2948 argv[2] = job->username;
2949 argv[3] = title;
2950 argv[4] = copies;
2951 argv[5] = options;
2952
2953 if (printer->remote && job->num_files > 1)
2954 {
2955 for (i = 0; i < job->num_files; i ++)
2956 {
2957 snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot,
2958 job->id, i + 1);
2959 argv[6 + i] = strdup(filename);
2960 }
2961 }
2962 else
2963 {
2964 snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot,
2965 job->id, job->current_file + 1);
2966 argv[6] = filename;
2967 }
2968
2969 for (i = 0; argv[i]; i ++)
2970 cupsdLogMessage(CUPSD_LOG_DEBUG,
2971 "[Job %d] argv[%d]=\"%s\"", job->id, i, argv[i]);
2972
2973 /*
2974 * Create environment variable strings for the filters...
2975 */
2976
2977 attr = ippFindAttribute(job->attrs, "attributes-natural-language",
2978 IPP_TAG_LANGUAGE);
2979
2980 switch (strlen(attr->values[0].string.text))
2981 {
2982 default :
2983 /*
2984 * This is an unknown or badly formatted language code; use
2985 * the POSIX locale...
2986 */
2987
2988 strcpy(lang, "LANG=C");
2989 break;
2990
2991 case 2 :
2992 /*
2993 * Just the language code (ll)...
2994 */
2995
2996 snprintf(lang, sizeof(lang), "LANG=%s",
2997 attr->values[0].string.text);
2998 break;
2999
3000 case 5 :
3001 /*
3002 * Language and country code (ll-cc)...
3003 */
3004
3005 snprintf(lang, sizeof(lang), "LANG=%c%c_%c%c",
3006 attr->values[0].string.text[0],
3007 attr->values[0].string.text[1],
3008 toupper(attr->values[0].string.text[3] & 255),
3009 toupper(attr->values[0].string.text[4] & 255));
3010 break;
3011 }
3012
3013 attr = ippFindAttribute(job->attrs, "document-format",
3014 IPP_TAG_MIMETYPE);
3015 if (attr != NULL &&
3016 (optptr = strstr(attr->values[0].string.text, "charset=")) != NULL)
3017 snprintf(charset, sizeof(charset), "CHARSET=%s", optptr + 8);
3018 else
3019 {
3020 attr = ippFindAttribute(job->attrs, "attributes-charset",
3021 IPP_TAG_CHARSET);
3022 snprintf(charset, sizeof(charset), "CHARSET=%s",
3023 attr->values[0].string.text);
3024 }
3025
3026 snprintf(content_type, sizeof(content_type), "CONTENT_TYPE=%s/%s",
3027 job->filetypes[job->current_file]->super,
3028 job->filetypes[job->current_file]->type);
3029 snprintf(device_uri, sizeof(device_uri), "DEVICE_URI=%s",
3030 printer->device_uri);
3031 cupsdSanitizeURI(printer->device_uri, sani_uri, sizeof(sani_uri));
3032 snprintf(ppd, sizeof(ppd), "PPD=%s/ppd/%s.ppd", ServerRoot, printer->name);
3033 snprintf(printer_name, sizeof(printer_name), "PRINTER=%s", printer->name);
3034 snprintf(rip_max_cache, sizeof(rip_max_cache), "RIP_MAX_CACHE=%s", RIPCache);
3035
3036 envc = cupsdLoadEnv(envp, (int)(sizeof(envp) / sizeof(envp[0])));
3037
3038 envp[envc ++] = charset;
3039 envp[envc ++] = lang;
3040 envp[envc ++] = ppd;
3041 envp[envc ++] = rip_max_cache;
3042 envp[envc ++] = content_type;
3043 envp[envc ++] = device_uri;
3044 envp[envc ++] = printer_name;
3045
3046 if (!printer->remote && !printer->raw &&
3047 (filter = (mime_filter_t *)cupsArrayLast(filters)) != NULL &&
3048 filter->dst)
3049 {
3050 snprintf(final_content_type, sizeof(final_content_type),
3051 "FINAL_CONTENT_TYPE=%s/%s",
3052 filter->dst->super, filter->dst->type);
3053 envp[envc ++] = final_content_type;
3054 }
3055
3056 if (Classification && !banner_page)
3057 {
3058 if ((attr = ippFindAttribute(job->attrs, "job-sheets",
3059 IPP_TAG_NAME)) == NULL)
3060 snprintf(classification, sizeof(classification), "CLASSIFICATION=%s",
3061 Classification);
3062 else if (attr->num_values > 1 &&
3063 strcmp(attr->values[1].string.text, "none") != 0)
3064 snprintf(classification, sizeof(classification), "CLASSIFICATION=%s",
3065 attr->values[1].string.text);
3066 else
3067 snprintf(classification, sizeof(classification), "CLASSIFICATION=%s",
3068 attr->values[0].string.text);
3069
3070 envp[envc ++] = classification;
3071 }
3072
3073 if (job->dtype & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT))
3074 {
3075 snprintf(class_name, sizeof(class_name), "CLASS=%s", job->dest);
3076 envp[envc ++] = class_name;
3077 }
3078
3079 if (job->auth_username)
3080 envp[envc ++] = job->auth_username;
3081 if (job->auth_domain)
3082 envp[envc ++] = job->auth_domain;
3083 if (job->auth_password)
3084 envp[envc ++] = job->auth_password;
3085
3086 #ifdef HAVE_GSSAPI
3087 if (job->ccname)
3088 envp[envc ++] = job->ccname;
3089 #endif /* HAVE_GSSAPI */
3090
3091 envp[envc] = NULL;
3092
3093 for (i = 0; i < envc; i ++)
3094 if (!strncmp(envp[i], "AUTH_", 5))
3095 cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] envp[%d]=\"AUTH_%c****\"",
3096 job->id, i, envp[i][5]);
3097 else if (strncmp(envp[i], "DEVICE_URI=", 11))
3098 cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] envp[%d]=\"%s\"",
3099 job->id, i, envp[i]);
3100 else
3101 cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] envp[%d]=\"DEVICE_URI=%s\"",
3102 job->id, i, sani_uri);
3103
3104 if (printer->remote)
3105 job->current_file = job->num_files;
3106 else
3107 job->current_file ++;
3108
3109 /*
3110 * Now create processes for all of the filters...
3111 */
3112
3113 filterfds[0][0] = -1;
3114 filterfds[0][1] = -1;
3115 filterfds[1][0] = -1;
3116 filterfds[1][1] = -1;
3117
3118 if (!job->status_buffer)
3119 {
3120 if (cupsdOpenPipe(job->status_pipes))
3121 {
3122 cupsdLogMessage(CUPSD_LOG_ERROR,
3123 "[Job %d] Unable to create job status pipes - %s.",
3124 job->id, strerror(errno));
3125 snprintf(printer->state_message, sizeof(printer->state_message),
3126 "Unable to create status pipes - %s.", strerror(errno));
3127
3128 cupsdAddPrinterHistory(printer);
3129
3130 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
3131 "Job canceled because the server could not create the job "
3132 "status pipes.");
3133
3134 goto abort_job;
3135 }
3136
3137 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3138 "[Job %d] start_job: status_pipes = [ %d %d ]",
3139 job->id, job->status_pipes[0], job->status_pipes[1]);
3140
3141 job->status_buffer = cupsdStatBufNew(job->status_pipes[0], "[Job %d]",
3142 job->id);
3143 job->status_level = CUPSD_LOG_INFO;
3144 }
3145
3146 job->status = 0;
3147 memset(job->filters, 0, sizeof(job->filters));
3148
3149 for (i = 0, slot = 0, filter = (mime_filter_t *)cupsArrayFirst(filters);
3150 filter;
3151 i ++, filter = (mime_filter_t *)cupsArrayNext(filters))
3152 {
3153 if (filter->filter[0] != '/')
3154 snprintf(command, sizeof(command), "%s/filter/%s", ServerBin,
3155 filter->filter);
3156 else
3157 strlcpy(command, filter->filter, sizeof(command));
3158
3159 if (i < (cupsArrayCount(filters) - 1))
3160 {
3161 if (cupsdOpenPipe(filterfds[slot]))
3162 {
3163 cupsdLogMessage(CUPSD_LOG_ERROR,
3164 "[Job %d] Unable to create job filter pipes - %s.",
3165 job->id, strerror(errno));
3166 snprintf(printer->state_message, sizeof(printer->state_message),
3167 "Unable to create filter pipes - %s.", strerror(errno));
3168 cupsdAddPrinterHistory(printer);
3169
3170 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
3171 "Job canceled because the server could not create the "
3172 "filter pipes.");
3173
3174 goto abort_job;
3175 }
3176 }
3177 else
3178 {
3179 if (job->current_file == 1)
3180 {
3181 if (strncmp(printer->device_uri, "file:", 5) != 0)
3182 {
3183 if (cupsdOpenPipe(job->print_pipes))
3184 {
3185 cupsdLogMessage(CUPSD_LOG_ERROR,
3186 "[Job %d] Unable to create job backend pipes - %s.",
3187 job->id, strerror(errno));
3188 snprintf(printer->state_message, sizeof(printer->state_message),
3189 "Unable to create backend pipes - %s.", strerror(errno));
3190 cupsdAddPrinterHistory(printer);
3191
3192 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
3193 "Job canceled because the server could not create "
3194 "the backend pipes.");
3195
3196 goto abort_job;
3197 }
3198 }
3199 else
3200 {
3201 job->print_pipes[0] = -1;
3202 if (!strcmp(printer->device_uri, "file:/dev/null") ||
3203 !strcmp(printer->device_uri, "file:///dev/null"))
3204 job->print_pipes[1] = -1;
3205 else
3206 {
3207 if (!strncmp(printer->device_uri, "file:/dev/", 10))
3208 job->print_pipes[1] = open(printer->device_uri + 5,
3209 O_WRONLY | O_EXCL);
3210 else if (!strncmp(printer->device_uri, "file:///dev/", 12))
3211 job->print_pipes[1] = open(printer->device_uri + 7,
3212 O_WRONLY | O_EXCL);
3213 else if (!strncmp(printer->device_uri, "file:///", 8))
3214 job->print_pipes[1] = open(printer->device_uri + 7,
3215 O_WRONLY | O_CREAT | O_TRUNC, 0600);
3216 else
3217 job->print_pipes[1] = open(printer->device_uri + 5,
3218 O_WRONLY | O_CREAT | O_TRUNC, 0600);
3219
3220 if (job->print_pipes[1] < 0)
3221 {
3222 cupsdLogMessage(CUPSD_LOG_ERROR,
3223 "[Job %d] Unable to open output file \"%s\" - %s.",
3224 job->id, printer->device_uri, strerror(errno));
3225 snprintf(printer->state_message, sizeof(printer->state_message),
3226 "Unable to open output file \"%s\" - %s.",
3227 printer->device_uri, strerror(errno));
3228
3229 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
3230 "Job canceled because the server could not open the "
3231 "output file.");
3232
3233 goto abort_job;
3234 }
3235
3236 fcntl(job->print_pipes[1], F_SETFD,
3237 fcntl(job->print_pipes[1], F_GETFD) | FD_CLOEXEC);
3238 }
3239 }
3240
3241 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3242 "[Job %d] start_job: print_pipes = [ %d %d ]",
3243 job->id, job->print_pipes[0], job->print_pipes[1]);
3244 }
3245
3246 filterfds[slot][0] = job->print_pipes[0];
3247 filterfds[slot][1] = job->print_pipes[1];
3248 }
3249
3250 cupsdLogMessage(CUPSD_LOG_DEBUG2, "[Job %d] start_job: filter=\"%s\"",
3251 job->id, command);
3252 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3253 "[Job %d] start_job: filterfds[%d]=[ %d %d ]",
3254 job->id, slot, filterfds[slot][0], filterfds[slot][1]);
3255
3256 pid = cupsdStartProcess(command, argv, envp, filterfds[!slot][0],
3257 filterfds[slot][1], job->status_pipes[1],
3258 job->back_pipes[0], job->side_pipes[0], 0,
3259 job->filters + i);
3260
3261 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3262 "[Job %d] start_job: Closing filter pipes for slot %d "
3263 "[ %d %d ]...",
3264 job->id, !slot, filterfds[!slot][0], filterfds[!slot][1]);
3265
3266 cupsdClosePipe(filterfds[!slot]);
3267
3268 if (pid == 0)
3269 {
3270 cupsdLogMessage(CUPSD_LOG_ERROR,
3271 "[Job %d] Unable to start filter \"%s\" - %s.",
3272 job->id, filter->filter, strerror(errno));
3273 snprintf(printer->state_message, sizeof(printer->state_message),
3274 "Unable to start filter \"%s\" - %s.",
3275 filter->filter, strerror(errno));
3276
3277 cupsdAddPrinterHistory(printer);
3278
3279 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
3280 "Job canceled because the server could not execute a "
3281 "filter.");
3282
3283 goto abort_job;
3284 }
3285
3286 cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Started filter %s (PID %d)",
3287 job->id, command, pid);
3288
3289 argv[6] = NULL;
3290 slot = !slot;
3291 }
3292
3293 cupsArrayDelete(filters);
3294
3295 /*
3296 * Finally, pipe the final output into a backend process if needed...
3297 */
3298
3299 if (strncmp(printer->device_uri, "file:", 5) != 0)
3300 {
3301 if (job->current_file == 1)
3302 {
3303 sscanf(printer->device_uri, "%254[^:]", method);
3304 snprintf(command, sizeof(command), "%s/backend/%s", ServerBin, method);
3305
3306 /*
3307 * See if the backend needs to run as root...
3308 */
3309
3310 if (RunUser)
3311 backroot = 0;
3312 else if (stat(command, &backinfo))
3313 backroot = 0;
3314 else
3315 backroot = !(backinfo.st_mode & (S_IRWXG | S_IRWXO));
3316
3317 argv[0] = sani_uri;
3318
3319 filterfds[slot][0] = -1;
3320 filterfds[slot][1] = -1;
3321
3322 cupsdLogMessage(CUPSD_LOG_DEBUG2, "[Job %d] start_job: backend=\"%s\"",
3323 job->id, command);
3324 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3325 "[Job %d] start_job: filterfds[%d] = [ %d %d ]", job->id,
3326 slot, filterfds[slot][0], filterfds[slot][1]);
3327
3328 pid = cupsdStartProcess(command, argv, envp, filterfds[!slot][0],
3329 filterfds[slot][1], job->status_pipes[1],
3330 job->back_pipes[1], job->side_pipes[1],
3331 backroot, &(job->backend));
3332
3333 if (pid == 0)
3334 {
3335 cupsdLogMessage(CUPSD_LOG_ERROR,
3336 "[Job %d] Unable to start backend \"%s\" - %s.",
3337 job->id, method, strerror(errno));
3338 snprintf(printer->state_message, sizeof(printer->state_message),
3339 "Unable to start backend \"%s\" - %s.", method,
3340 strerror(errno));
3341
3342 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
3343 "Job canceled because the server could not execute "
3344 "the backend.");
3345
3346 goto abort_job;
3347 }
3348 else
3349 {
3350 cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Started backend %s (PID %d)",
3351 job->id, command, pid);
3352 }
3353 }
3354
3355 if (job->current_file == job->num_files)
3356 {
3357 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3358 "[Job %d] start_job: Closing print pipes [ %d %d ]...",
3359 job->id, job->print_pipes[0], job->print_pipes[1]);
3360
3361 cupsdClosePipe(job->print_pipes);
3362
3363 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3364 "[Job %d] start_job: Closing back pipes [ %d %d ]...",
3365 job->id, job->back_pipes[0], job->back_pipes[1]);
3366
3367 cupsdClosePipe(job->back_pipes);
3368
3369 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3370 "[Job %d] start_job: Closing side pipes [ %d %d ]...",
3371 job->id, job->side_pipes[0], job->side_pipes[1]);
3372
3373 cupsdClosePipe(job->side_pipes);
3374
3375 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3376 "[Job %d] start_job: Closing status output pipe %d...",
3377 job->id, job->status_pipes[1]);
3378
3379 close(job->status_pipes[1]);
3380 job->status_pipes[1] = -1;
3381 }
3382 }
3383 else
3384 {
3385 filterfds[slot][0] = -1;
3386 filterfds[slot][1] = -1;
3387
3388 if (job->current_file == job->num_files)
3389 {
3390 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3391 "[Job %d] start_job: Closing print pipes [ %d %d ]...",
3392 job->id, job->print_pipes[0], job->print_pipes[1]);
3393
3394 cupsdClosePipe(job->print_pipes);
3395
3396 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3397 "[Job %d] start_job: Closing status output pipe %d...",
3398 job->id, job->status_pipes[1]);
3399
3400 close(job->status_pipes[1]);
3401 job->status_pipes[1] = -1;
3402 }
3403 }
3404
3405 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3406 "[Job %d] start_job: Closing filter pipes for slot %d "
3407 "[ %d %d ]...",
3408 job->id, slot, filterfds[slot][0], filterfds[slot][1]);
3409 cupsdClosePipe(filterfds[slot]);
3410
3411 if (printer->remote && job->num_files > 1)
3412 {
3413 for (i = 0; i < job->num_files; i ++)
3414 free(argv[i + 6]);
3415 }
3416
3417 free(argv);
3418
3419 cupsdAddSelect(job->status_buffer->fd, (cupsd_selfunc_t)update_job, NULL,
3420 job);
3421
3422 cupsdAddEvent(CUPSD_EVENT_JOB_STATE, job->printer, job, "Job #%d started.",
3423 job->id);
3424
3425 return;
3426
3427
3428 /*
3429 * If we get here, we need to abort the current job and close out all
3430 * files and pipes...
3431 */
3432
3433 abort_job:
3434
3435 for (slot = 0; slot < 2; slot ++)
3436 {
3437 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3438 "[Job %d] start_job: Closing filter pipes for slot %d "
3439 "[ %d %d ]...",
3440 job->id, slot, filterfds[slot][0], filterfds[slot][1]);
3441 cupsdClosePipe(filterfds[slot]);
3442 }
3443
3444 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3445 "[Job %d] start_job: Closing status pipes [ %d %d ]...",
3446 job->id, job->status_pipes[0], job->status_pipes[1]);
3447 cupsdClosePipe(job->status_pipes);
3448 cupsdStatBufDelete(job->status_buffer);
3449
3450 job->status_buffer = NULL;
3451
3452 cupsArrayDelete(filters);
3453
3454 if (printer->remote && job->num_files > 1)
3455 {
3456 for (i = 0; i < job->num_files; i ++)
3457 free(argv[i + 6]);
3458 }
3459
3460 free(argv);
3461
3462 cupsdStopJob(job, 0);
3463 }
3464
3465
3466 /*
3467 * 'unload_job()' - Unload a job from memory.
3468 */
3469
3470 static void
3471 unload_job(cupsd_job_t *job) /* I - Job */
3472 {
3473 if (!job->attrs)
3474 return;
3475
3476 cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] Unloading...", job->id);
3477
3478 ippDelete(job->attrs);
3479
3480 job->attrs = NULL;
3481 job->state = NULL;
3482 job->sheets = NULL;
3483 job->job_sheets = NULL;
3484 job->printer_message = NULL;
3485 job->printer_reasons = NULL;
3486 }
3487
3488
3489 /*
3490 * 'update_job()' - Read a status update from a job's filters.
3491 */
3492
3493 void
3494 update_job(cupsd_job_t *job) /* I - Job to check */
3495 {
3496 int i; /* Looping var */
3497 int copies; /* Number of copies printed */
3498 char message[1024], /* Message text */
3499 *ptr; /* Pointer update... */
3500 int loglevel, /* Log level for message */
3501 event = 0; /* Events? */
3502
3503
3504 while ((ptr = cupsdStatBufUpdate(job->status_buffer, &loglevel,
3505 message, sizeof(message))) != NULL)
3506 {
3507 /*
3508 * Process page and printer state messages as needed...
3509 */
3510
3511 if (loglevel == CUPSD_LOG_PAGE)
3512 {
3513 /*
3514 * Page message; send the message to the page_log file and update the
3515 * job sheet count...
3516 */
3517
3518 if (job->sheets != NULL)
3519 {
3520 if (!strncasecmp(message, "total ", 6))
3521 {
3522 /*
3523 * Got a total count of pages from a backend or filter...
3524 */
3525
3526 copies = atoi(message + 6);
3527 copies -= job->sheets->values[0].integer; /* Just track the delta */
3528 }
3529 else if (!sscanf(message, "%*d%d", &copies))
3530 copies = 1;
3531
3532 job->sheets->values[0].integer += copies;
3533
3534 if (job->printer->page_limit)
3535 {
3536 cupsd_quota_t *q = cupsdUpdateQuota(job->printer, job->username,
3537 copies, 0);
3538
3539 #ifdef __APPLE__
3540 if (AppleQuotas && q->page_count == -3)
3541 {
3542 /*
3543 * Quota limit exceeded, cancel job in progress immediately...
3544 */
3545
3546 cupsdLogMessage(CUPSD_LOG_INFO,
3547 "[Job %d] Canceled because pages exceed user %s "
3548 "quota limit on printer %s (%s).",
3549 job->id, job->username, job->printer->name,
3550 job->printer->info);
3551
3552 cupsdCancelJob(job, 1, IPP_JOB_CANCELED);
3553 return;
3554 }
3555 #else
3556 (void)q;
3557 #endif /* __APPLE__ */
3558 }
3559 }
3560
3561 cupsdLogPage(job, message);
3562
3563 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS, job->printer, job,
3564 "Printed %d page(s).", job->sheets->values[0].integer);
3565 }
3566 else if (loglevel == CUPSD_LOG_STATE)
3567 {
3568 if (!strcmp(message, "paused"))
3569 cupsdStopPrinter(job->printer, 1);
3570 else
3571 {
3572 cupsdSetPrinterReasons(job->printer, message);
3573 cupsdAddPrinterHistory(job->printer);
3574 event |= CUPSD_EVENT_PRINTER_STATE_CHANGED;
3575 }
3576
3577 update_job_attrs(job);
3578 }
3579 else if (loglevel == CUPSD_LOG_ATTR)
3580 {
3581 /*
3582 * Set attribute(s)...
3583 */
3584
3585 int num_attrs; /* Number of attributes */
3586 cups_option_t *attrs; /* Attributes */
3587 const char *attr; /* Attribute */
3588
3589
3590 num_attrs = cupsParseOptions(message, 0, &attrs);
3591
3592 if ((attr = cupsGetOption("auth-info-required", num_attrs,
3593 attrs)) != NULL)
3594 cupsdSetAuthInfoRequired(job->printer, attr, NULL);
3595
3596 if ((attr = cupsGetOption("printer-alert", num_attrs, attrs)) != NULL)
3597 {
3598 cupsdSetString(&job->printer->alert, attr);
3599 event |= CUPSD_EVENT_PRINTER_STATE_CHANGED;
3600 }
3601
3602 if ((attr = cupsGetOption("printer-alert-description", num_attrs,
3603 attrs)) != NULL)
3604 {
3605 cupsdSetString(&job->printer->alert_description, attr);
3606 event |= CUPSD_EVENT_PRINTER_STATE_CHANGED;
3607 }
3608
3609 cupsFreeOptions(num_attrs, attrs);
3610 }
3611 #ifdef __APPLE__
3612 else if (!strncmp(message, "recoverable:", 12))
3613 {
3614 cupsdSetPrinterReasons(job->printer,
3615 "+com.apple.print.recoverable-warning");
3616
3617 ptr = message + 12;
3618 while (isspace(*ptr & 255))
3619 ptr ++;
3620
3621 cupsdSetString(&job->printer->recoverable, ptr);
3622 cupsdAddPrinterHistory(job->printer);
3623 event |= CUPSD_EVENT_PRINTER_STATE_CHANGED;
3624 }
3625 else if (!strncmp(message, "recovered:", 10))
3626 {
3627 cupsdSetPrinterReasons(job->printer,
3628 "-com.apple.print.recoverable-warning");
3629
3630 ptr = message + 10;
3631 while (isspace(*ptr & 255))
3632 ptr ++;
3633
3634 cupsdSetString(&job->printer->recoverable, ptr);
3635 cupsdAddPrinterHistory(job->printer);
3636 event |= CUPSD_EVENT_PRINTER_STATE_CHANGED;
3637 }
3638 #endif /* __APPLE__ */
3639 else if (loglevel <= job->status_level)
3640 {
3641 /*
3642 * Some message to show in the printer-state-message attribute...
3643 */
3644
3645 if (loglevel != CUPSD_LOG_NOTICE)
3646 job->status_level = loglevel;
3647
3648 strlcpy(job->printer->state_message, message,
3649 sizeof(job->printer->state_message));
3650 cupsdAddPrinterHistory(job->printer);
3651 event |= CUPSD_EVENT_PRINTER_STATE_CHANGED;
3652
3653 update_job_attrs(job);
3654 }
3655
3656 if (!strchr(job->status_buffer->buffer, '\n'))
3657 break;
3658 }
3659
3660 if ((event & CUPSD_EVENT_PRINTER_STATE_CHANGED))
3661 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE_CHANGED, job->printer, NULL,
3662 (job->printer->type & CUPS_PRINTER_CLASS) ?
3663 "Class \"%s\" state changed." :
3664 "Printer \"%s\" state changed.",
3665 job->printer->name);
3666
3667 if (ptr == NULL && !job->status_buffer->bufused)
3668 {
3669 /*
3670 * See if all of the filters and the backend have returned their
3671 * exit statuses.
3672 */
3673
3674 for (i = 0; job->filters[i] < 0; i ++);
3675
3676 if (job->filters[i])
3677 return;
3678
3679 if (job->current_file >= job->num_files && job->backend > 0)
3680 return;
3681
3682 /*
3683 * Handle the end of job stuff...
3684 */
3685
3686 cupsdFinishJob(job);
3687 }
3688 }
3689
3690
3691 /*
3692 * 'update_job_attrs()' - Update the job-printer-* attributes.
3693 */
3694
3695 void
3696 update_job_attrs(cupsd_job_t *job) /* I - Job to update */
3697 {
3698 int i; /* Looping var */
3699 int num_reasons; /* Actual number of reasons */
3700 const char * const *reasons; /* Reasons */
3701 static const char *none = "none", /* "none" */
3702 *paused = "paused";
3703 /* "paused" */
3704
3705
3706 /*
3707 * Get/create the job-printer-state-* attributes...
3708 */
3709
3710 if (!job->printer_message)
3711 {
3712 if ((job->printer_message = ippFindAttribute(job->attrs,
3713 "job-printer-state-message",
3714 IPP_TAG_TEXT)) == NULL)
3715 job->printer_message = ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_TEXT,
3716 "job-printer-state-message",
3717 NULL, "");
3718 }
3719
3720 if (!job->printer_reasons)
3721 job->printer_reasons = ippFindAttribute(job->attrs,
3722 "job-printer-state-reasons",
3723 IPP_TAG_KEYWORD);
3724
3725 /*
3726 * If the job isn't printing, return now...
3727 */
3728
3729 if (!job->printer)
3730 return;
3731
3732 /*
3733 * Otherwise copy the printer-state-message value...
3734 */
3735
3736 if (job->printer->state_message[0])
3737 cupsdSetString(&(job->printer_message->values[0].string.text),
3738 job->printer->state_message);
3739
3740 /*
3741 * ... and the printer-state-reasons value...
3742 */
3743
3744 if (job->printer->num_reasons == 0)
3745 {
3746 num_reasons = 1;
3747 reasons = job->printer->state == IPP_PRINTER_STOPPED ? &paused : &none;
3748 }
3749 else
3750 {
3751 num_reasons = job->printer->num_reasons;
3752 reasons = (const char * const *)job->printer->reasons;
3753 }
3754
3755 if (!job->printer_reasons || job->printer_reasons->num_values != num_reasons)
3756 {
3757 ippDeleteAttribute(job->attrs, job->printer_reasons);
3758
3759 job->printer_reasons = ippAddStrings(job->attrs,
3760 IPP_TAG_JOB, IPP_TAG_KEYWORD,
3761 "job-printer-state-reasons",
3762 num_reasons, NULL, NULL);
3763 }
3764
3765 for (i = 0; i < num_reasons; i ++)
3766 cupsdSetString(&(job->printer_reasons->values[i].string.text), reasons[i]);
3767 }
3768
3769
3770 /*
3771 * End of "$Id: job.c 6735 2007-07-26 18:38:29Z mike $".
3772 */