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