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