]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/job.c
Import CUPS 1.4svn-r7908.
[thirdparty/cups.git] / scheduler / job.c
1 /*
2 * "$Id: job.c 7682 2008-06-21 00:06:02Z 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 sani_uri[1024], /* Sanitized DEVICE_URI env var */
2481 filename[1024], /* Job filename */
2482 command[1024], /* Full path to command */
2483 jobid[255], /* Job ID string */
2484 title[IPP_MAX_NAME],
2485 /* Job title string */
2486 copies[255], /* # copies string */
2487 *envp[MAX_ENV + 16],
2488 /* Environment variables */
2489 charset[255], /* CHARSET env variable */
2490 class_name[255],/* CLASS env variable */
2491 classification[1024],
2492 /* CLASSIFICATION env variable */
2493 content_type[1024],
2494 /* CONTENT_TYPE env variable */
2495 device_uri[1024],
2496 /* DEVICE_URI env variable */
2497 final_content_type[1024],
2498 /* FINAL_CONTENT_TYPE env variable */
2499 lang[255], /* LANG env variable */
2500 #ifdef __APPLE__
2501 apple_language[255],
2502 /* APPLE_LANGUAGE env variable */
2503 #endif /* __APPLE__ */
2504 ppd[1024], /* PPD env variable */
2505 printer_name[255],
2506 /* PRINTER env variable */
2507 rip_max_cache[255];
2508 /* RIP_MAX_CACHE env variable */
2509 static char *options = NULL;/* Full list of options */
2510 static int optlength = 0; /* Length of option buffer */
2511
2512
2513 cupsdLogJob(job, CUPSD_LOG_DEBUG2, "start_job: file = %d/%d",
2514 job->current_file, job->num_files);
2515
2516 if (job->num_files == 0)
2517 {
2518 cupsdLogJob(job, CUPSD_LOG_ERROR, "No files, canceling job!");
2519 cupsdCancelJob(job, 0, IPP_JOB_ABORTED);
2520 return;
2521 }
2522
2523 /*
2524 * Figure out what filters are required to convert from
2525 * the source to the destination type...
2526 */
2527
2528 filters = NULL;
2529 job->cost = 0;
2530
2531 if (printer->raw)
2532 {
2533 /*
2534 * Remote jobs and raw queues go directly to the printer without
2535 * filtering...
2536 */
2537
2538 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Sending job to queue tagged as raw...");
2539
2540 filters = NULL;
2541 }
2542 else
2543 {
2544 /*
2545 * Local jobs get filtered...
2546 */
2547
2548 filters = mimeFilter(MimeDatabase, job->filetypes[job->current_file],
2549 printer->filetype, &(job->cost));
2550
2551 if (!filters)
2552 {
2553 cupsdLogJob(job, CUPSD_LOG_ERROR,
2554 "Unable to convert file %d to printable format!",
2555 job->current_file);
2556 cupsdLogMessage(CUPSD_LOG_INFO,
2557 "Hint: Do you have Ghostscript installed?");
2558
2559 if (LogLevel < CUPSD_LOG_DEBUG)
2560 cupsdLogMessage(CUPSD_LOG_INFO,
2561 "Hint: Try setting the LogLevel to \"debug\".");
2562
2563 job->current_file ++;
2564
2565 if (job->current_file == job->num_files)
2566 cupsdCancelJob(job, 0, IPP_JOB_ABORTED);
2567
2568 return;
2569 }
2570
2571 /*
2572 * Remove NULL ("-") filters...
2573 */
2574
2575 for (filter = (mime_filter_t *)cupsArrayFirst(filters);
2576 filter;
2577 filter = (mime_filter_t *)cupsArrayNext(filters))
2578 if (!strcmp(filter->filter, "-"))
2579 cupsArrayRemove(filters, filter);
2580
2581 if (cupsArrayCount(filters) == 0)
2582 {
2583 cupsArrayDelete(filters);
2584 filters = NULL;
2585 }
2586
2587 /*
2588 * If this printer has any pre-filters, insert the required pre-filter
2589 * in the filters array...
2590 */
2591
2592 if (printer->prefiltertype && filters)
2593 {
2594 prefilters = cupsArrayNew(NULL, NULL);
2595
2596 for (filter = (mime_filter_t *)cupsArrayFirst(filters);
2597 filter;
2598 filter = (mime_filter_t *)cupsArrayNext(filters))
2599 {
2600 if ((prefilter = mimeFilterLookup(MimeDatabase, filter->src,
2601 printer->prefiltertype)))
2602 {
2603 cupsArrayAdd(prefilters, prefilter);
2604 job->cost += prefilter->cost;
2605 }
2606
2607 cupsArrayAdd(prefilters, filter);
2608 }
2609
2610 cupsArrayDelete(filters);
2611 filters = prefilters;
2612 }
2613 }
2614
2615 /*
2616 * Set a minimum cost of 100 for all jobs so that FilterLimit
2617 * works with raw queues and other low-cost paths.
2618 */
2619
2620 if (job->cost < 100)
2621 job->cost = 100;
2622
2623 /*
2624 * See if the filter cost is too high...
2625 */
2626
2627 if ((FilterLevel + job->cost) > FilterLimit && FilterLevel > 0 &&
2628 FilterLimit > 0)
2629 {
2630 /*
2631 * Don't print this job quite yet...
2632 */
2633
2634 cupsArrayDelete(filters);
2635
2636 cupsdLogJob(job, CUPSD_LOG_INFO,
2637 "Holding because filter limit has been reached.");
2638 cupsdLogJob(job, CUPSD_LOG_DEBUG2,
2639 "start_job: file=%d, cost=%d, level=%d, limit=%d",
2640 job->current_file, job->cost, FilterLevel,
2641 FilterLimit);
2642 return;
2643 }
2644
2645 FilterLevel += job->cost;
2646
2647 /*
2648 * Add decompression/raw filter as needed...
2649 */
2650
2651 if ((!printer->raw && job->compressions[job->current_file]) ||
2652 (!filters && !printer->remote &&
2653 (job->num_files > 1 || !strncmp(printer->device_uri, "file:", 5))))
2654 {
2655 /*
2656 * Add gziptoany filter to the front of the list...
2657 */
2658
2659 if (!filters)
2660 filters = cupsArrayNew(NULL, NULL);
2661
2662 if (!cupsArrayInsert(filters, &gziptoany_filter))
2663 {
2664 cupsdLogJob(job, CUPSD_LOG_ERROR,
2665 "Unable to add decompression filter - %s", strerror(errno));
2666
2667 cupsArrayDelete(filters);
2668
2669 job->current_file ++;
2670
2671 if (job->current_file == job->num_files)
2672 cupsdCancelJob(job, 0, IPP_JOB_ABORTED);
2673
2674 return;
2675 }
2676 }
2677
2678 /*
2679 * Add port monitor, if any...
2680 */
2681
2682 if (printer->port_monitor)
2683 {
2684 /*
2685 * Add port monitor to the end of the list...
2686 */
2687
2688 if (!filters)
2689 filters = cupsArrayNew(NULL, NULL);
2690
2691 port_monitor.src = NULL;
2692 port_monitor.dst = NULL;
2693 port_monitor.cost = 0;
2694
2695 snprintf(port_monitor.filter, sizeof(port_monitor.filter),
2696 "%s/monitor/%s", ServerBin, printer->port_monitor);
2697
2698 if (!cupsArrayAdd(filters, &port_monitor))
2699 {
2700 cupsdLogJob(job, CUPSD_LOG_ERROR,
2701 "Unable to add port monitor - %s", strerror(errno));
2702
2703 cupsArrayDelete(filters);
2704
2705 job->current_file ++;
2706
2707 if (job->current_file == job->num_files)
2708 cupsdCancelJob(job, 0, IPP_JOB_ABORTED);
2709
2710 return;
2711 }
2712 }
2713
2714 /*
2715 * Make sure we don't go over the "MAX_FILTERS" limit...
2716 */
2717
2718 if (cupsArrayCount(filters) > MAX_FILTERS)
2719 {
2720 cupsdLogJob(job, CUPSD_LOG_ERROR,
2721 "Too many filters (%d > %d), unable to print!",
2722 cupsArrayCount(filters), MAX_FILTERS);
2723
2724 cupsArrayDelete(filters);
2725 cupsdCancelJob(job, 0, IPP_JOB_STOPPED);
2726
2727 return;
2728 }
2729
2730 /*
2731 * Update the printer and job state to "processing"...
2732 */
2733
2734 job->state->values[0].integer = IPP_JOB_PROCESSING;
2735 job->state_value = IPP_JOB_PROCESSING;
2736 job->progress = 0;
2737 job->status = 0;
2738 job->printer = printer;
2739 printer->job = job;
2740
2741 cupsdSetPrinterState(printer, IPP_PRINTER_PROCESSING, 0);
2742
2743 if (job->current_file == 0)
2744 {
2745 /*
2746 * Add to the printing list...
2747 */
2748
2749 cupsArrayAdd(PrintingJobs, job);
2750 cupsdSetBusyState();
2751
2752 /*
2753 * Set the processing time...
2754 */
2755
2756 set_time(job, "time-at-processing");
2757
2758 /*
2759 * Create the backchannel pipes and make them non-blocking...
2760 */
2761
2762 cupsdOpenPipe(job->back_pipes);
2763
2764 fcntl(job->back_pipes[0], F_SETFL,
2765 fcntl(job->back_pipes[0], F_GETFL) | O_NONBLOCK);
2766
2767 fcntl(job->back_pipes[1], F_SETFL,
2768 fcntl(job->back_pipes[1], F_GETFL) | O_NONBLOCK);
2769
2770 /*
2771 * Create the side-channel pipes and make them non-blocking...
2772 */
2773
2774 socketpair(AF_LOCAL, SOCK_STREAM, 0, job->side_pipes);
2775
2776 fcntl(job->side_pipes[0], F_SETFL,
2777 fcntl(job->side_pipes[0], F_GETFL) | O_NONBLOCK);
2778
2779 fcntl(job->side_pipes[1], F_SETFL,
2780 fcntl(job->side_pipes[1], F_GETFL) | O_NONBLOCK);
2781 }
2782
2783 /*
2784 * Determine if we are printing a banner page or not...
2785 */
2786
2787 if (job->job_sheets == NULL)
2788 {
2789 cupsdLogJob(job, CUPSD_LOG_DEBUG, "No job-sheets attribute.");
2790 if ((job->job_sheets =
2791 ippFindAttribute(job->attrs, "job-sheets", IPP_TAG_ZERO)) != NULL)
2792 cupsdLogJob(job, CUPSD_LOG_DEBUG,
2793 "... but someone added one without setting job_sheets!");
2794 }
2795 else if (job->job_sheets->num_values == 1)
2796 cupsdLogJob(job, CUPSD_LOG_DEBUG, "job-sheets=%s",
2797 job->job_sheets->values[0].string.text);
2798 else
2799 cupsdLogJob(job, CUPSD_LOG_DEBUG, "job-sheets=%s,%s",
2800 job->job_sheets->values[0].string.text,
2801 job->job_sheets->values[1].string.text);
2802
2803 if (printer->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT))
2804 banner_page = 0;
2805 else if (job->job_sheets == NULL)
2806 banner_page = 0;
2807 else if (strcasecmp(job->job_sheets->values[0].string.text, "none") != 0 &&
2808 job->current_file == 0)
2809 banner_page = 1;
2810 else if (job->job_sheets->num_values > 1 &&
2811 strcasecmp(job->job_sheets->values[1].string.text, "none") != 0 &&
2812 job->current_file == (job->num_files - 1))
2813 banner_page = 1;
2814 else
2815 banner_page = 0;
2816
2817 cupsdLogJob(job, CUPSD_LOG_DEBUG, "banner_page = %d", banner_page);
2818
2819 /*
2820 * Building the options string is harder than it needs to be, but
2821 * for the moment we need to pass strings for command-line args and
2822 * not IPP attribute pointers... :)
2823 *
2824 * First allocate/reallocate the option buffer as needed...
2825 */
2826
2827 i = ipp_length(job->attrs);
2828
2829 if (i > optlength || !options)
2830 {
2831 if (optlength == 0)
2832 optptr = malloc(i);
2833 else
2834 optptr = realloc(options, i);
2835
2836 if (optptr == NULL)
2837 {
2838 cupsdLogJob(job, CUPSD_LOG_CRIT,
2839 "Unable to allocate %d bytes for option buffer!", i);
2840
2841 cupsArrayDelete(filters);
2842
2843 FilterLevel -= job->cost;
2844
2845 cupsdCancelJob(job, 0, IPP_JOB_ABORTED);
2846 return;
2847 }
2848
2849 options = optptr;
2850 optlength = i;
2851 }
2852
2853 /*
2854 * Now loop through the attributes and convert them to the textual
2855 * representation used by the filters...
2856 */
2857
2858 optptr = options;
2859 *optptr = '\0';
2860
2861 snprintf(title, sizeof(title), "%s-%d", printer->name, job->id);
2862 strcpy(copies, "1");
2863
2864 for (attr = job->attrs->attrs; attr != NULL; attr = attr->next)
2865 {
2866 if (!strcmp(attr->name, "copies") &&
2867 attr->value_tag == IPP_TAG_INTEGER)
2868 {
2869 /*
2870 * Don't use the # copies attribute if we are printing the job sheets...
2871 */
2872
2873 if (!banner_page)
2874 sprintf(copies, "%d", attr->values[0].integer);
2875 }
2876 else if (!strcmp(attr->name, "job-name") &&
2877 (attr->value_tag == IPP_TAG_NAME ||
2878 attr->value_tag == IPP_TAG_NAMELANG))
2879 strlcpy(title, attr->values[0].string.text, sizeof(title));
2880 else if (attr->group_tag == IPP_TAG_JOB)
2881 {
2882 /*
2883 * Filter out other unwanted attributes...
2884 */
2885
2886 if (attr->value_tag == IPP_TAG_MIMETYPE ||
2887 attr->value_tag == IPP_TAG_NAMELANG ||
2888 attr->value_tag == IPP_TAG_TEXTLANG ||
2889 (attr->value_tag == IPP_TAG_URI && strcmp(attr->name, "job-uuid")) ||
2890 attr->value_tag == IPP_TAG_URISCHEME ||
2891 attr->value_tag == IPP_TAG_BEGIN_COLLECTION) /* Not yet supported */
2892 continue;
2893
2894 if (!strncmp(attr->name, "time-", 5))
2895 continue;
2896
2897 if (!strncmp(attr->name, "job-", 4) &&
2898 strcmp(attr->name, "job-uuid") &&
2899 strcmp(attr->name, "job-impressions") &&
2900 strcmp(attr->name, "job-originating-host-name") &&
2901 !(printer->type & CUPS_PRINTER_REMOTE))
2902 continue;
2903
2904 if ((!strcmp(attr->name, "job-impressions") ||
2905 !strcmp(attr->name, "page-label") ||
2906 !strcmp(attr->name, "page-border") ||
2907 !strncmp(attr->name, "number-up", 9) ||
2908 !strcmp(attr->name, "page-ranges") ||
2909 !strcmp(attr->name, "page-set") ||
2910 !strcasecmp(attr->name, "AP_FIRSTPAGE_InputSlot") ||
2911 !strcasecmp(attr->name, "AP_FIRSTPAGE_ManualFeed") ||
2912 !strcasecmp(attr->name, "com.apple.print.PrintSettings."
2913 "PMTotalSidesImaged..n.") ||
2914 !strcasecmp(attr->name, "com.apple.print.PrintSettings."
2915 "PMTotalBeginPages..n.")) &&
2916 banner_page)
2917 continue;
2918
2919 /*
2920 * Otherwise add them to the list...
2921 */
2922
2923 if (optptr > options)
2924 strlcat(optptr, " ", optlength - (optptr - options));
2925
2926 if (attr->value_tag != IPP_TAG_BOOLEAN)
2927 {
2928 strlcat(optptr, attr->name, optlength - (optptr - options));
2929 strlcat(optptr, "=", optlength - (optptr - options));
2930 }
2931
2932 for (i = 0; i < attr->num_values; i ++)
2933 {
2934 if (i)
2935 strlcat(optptr, ",", optlength - (optptr - options));
2936
2937 optptr += strlen(optptr);
2938
2939 switch (attr->value_tag)
2940 {
2941 case IPP_TAG_INTEGER :
2942 case IPP_TAG_ENUM :
2943 snprintf(optptr, optlength - (optptr - options),
2944 "%d", attr->values[i].integer);
2945 break;
2946
2947 case IPP_TAG_BOOLEAN :
2948 if (!attr->values[i].boolean)
2949 strlcat(optptr, "no", optlength - (optptr - options));
2950
2951 case IPP_TAG_NOVALUE :
2952 strlcat(optptr, attr->name,
2953 optlength - (optptr - options));
2954 break;
2955
2956 case IPP_TAG_RANGE :
2957 if (attr->values[i].range.lower == attr->values[i].range.upper)
2958 snprintf(optptr, optlength - (optptr - options) - 1,
2959 "%d", attr->values[i].range.lower);
2960 else
2961 snprintf(optptr, optlength - (optptr - options) - 1,
2962 "%d-%d", attr->values[i].range.lower,
2963 attr->values[i].range.upper);
2964 break;
2965
2966 case IPP_TAG_RESOLUTION :
2967 snprintf(optptr, optlength - (optptr - options) - 1,
2968 "%dx%d%s", attr->values[i].resolution.xres,
2969 attr->values[i].resolution.yres,
2970 attr->values[i].resolution.units == IPP_RES_PER_INCH ?
2971 "dpi" : "dpc");
2972 break;
2973
2974 case IPP_TAG_STRING :
2975 case IPP_TAG_TEXT :
2976 case IPP_TAG_NAME :
2977 case IPP_TAG_KEYWORD :
2978 case IPP_TAG_CHARSET :
2979 case IPP_TAG_LANGUAGE :
2980 case IPP_TAG_URI :
2981 for (valptr = attr->values[i].string.text; *valptr;)
2982 {
2983 if (strchr(" \t\n\\\'\"", *valptr))
2984 *optptr++ = '\\';
2985 *optptr++ = *valptr++;
2986 }
2987
2988 *optptr = '\0';
2989 break;
2990
2991 default :
2992 break; /* anti-compiler-warning-code */
2993 }
2994 }
2995
2996 optptr += strlen(optptr);
2997 }
2998 }
2999
3000 /*
3001 * Build the command-line arguments for the filters. Each filter
3002 * has 6 or 7 arguments:
3003 *
3004 * argv[0] = printer
3005 * argv[1] = job ID
3006 * argv[2] = username
3007 * argv[3] = title
3008 * argv[4] = # copies
3009 * argv[5] = options
3010 * argv[6] = filename (optional; normally stdin)
3011 *
3012 * This allows legacy printer drivers that use the old System V
3013 * printing interface to be used by CUPS.
3014 *
3015 * For remote jobs, we send all of the files in the argument list.
3016 */
3017
3018 if (printer->remote && job->num_files > 1)
3019 argv = calloc(7 + job->num_files, sizeof(char *));
3020 else
3021 argv = calloc(8, sizeof(char *));
3022
3023 if (!argv)
3024 {
3025 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to allocate argument array!");
3026 cupsArrayDelete(filters);
3027
3028 FilterLevel -= job->cost;
3029
3030 cupsdStopPrinter(printer, 0);
3031 return;
3032 }
3033
3034 sprintf(jobid, "%d", job->id);
3035
3036 argv[0] = printer->name;
3037 argv[1] = jobid;
3038 argv[2] = job->username;
3039 argv[3] = title;
3040 argv[4] = copies;
3041 argv[5] = options;
3042
3043 if (printer->remote && job->num_files > 1)
3044 {
3045 for (i = 0; i < job->num_files; i ++)
3046 {
3047 snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot,
3048 job->id, i + 1);
3049 argv[6 + i] = strdup(filename);
3050 }
3051 }
3052 else
3053 {
3054 snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot,
3055 job->id, job->current_file + 1);
3056 argv[6] = filename;
3057 }
3058
3059 for (i = 0; argv[i]; i ++)
3060 cupsdLogJob(job, CUPSD_LOG_DEBUG,
3061 "argv[%d]=\"%s\"", i, argv[i]);
3062
3063 /*
3064 * Create environment variable strings for the filters...
3065 */
3066
3067 attr = ippFindAttribute(job->attrs, "attributes-natural-language",
3068 IPP_TAG_LANGUAGE);
3069
3070 #ifdef __APPLE__
3071 strcpy(apple_language, "APPLE_LANGUAGE=");
3072 _cupsAppleLanguage(attr->values[0].string.text,
3073 apple_language + 15, sizeof(apple_language) - 15);
3074 #endif /* __APPLE__ */
3075
3076 switch (strlen(attr->values[0].string.text))
3077 {
3078 default :
3079 /*
3080 * This is an unknown or badly formatted language code; use
3081 * the POSIX locale...
3082 */
3083
3084 strcpy(lang, "LANG=C");
3085 break;
3086
3087 case 2 :
3088 /*
3089 * Just the language code (ll)...
3090 */
3091
3092 snprintf(lang, sizeof(lang), "LANG=%s.UTF8",
3093 attr->values[0].string.text);
3094 break;
3095
3096 case 5 :
3097 /*
3098 * Language and country code (ll-cc)...
3099 */
3100
3101 snprintf(lang, sizeof(lang), "LANG=%c%c_%c%c.UTF8",
3102 attr->values[0].string.text[0],
3103 attr->values[0].string.text[1],
3104 toupper(attr->values[0].string.text[3] & 255),
3105 toupper(attr->values[0].string.text[4] & 255));
3106 break;
3107 }
3108
3109 attr = ippFindAttribute(job->attrs, "document-format",
3110 IPP_TAG_MIMETYPE);
3111 if (attr != NULL &&
3112 (optptr = strstr(attr->values[0].string.text, "charset=")) != NULL)
3113 snprintf(charset, sizeof(charset), "CHARSET=%s", optptr + 8);
3114 else
3115 {
3116 attr = ippFindAttribute(job->attrs, "attributes-charset",
3117 IPP_TAG_CHARSET);
3118 snprintf(charset, sizeof(charset), "CHARSET=%s",
3119 attr->values[0].string.text);
3120 }
3121
3122 snprintf(content_type, sizeof(content_type), "CONTENT_TYPE=%s/%s",
3123 job->filetypes[job->current_file]->super,
3124 job->filetypes[job->current_file]->type);
3125 snprintf(device_uri, sizeof(device_uri), "DEVICE_URI=%s",
3126 printer->device_uri);
3127 cupsdSanitizeURI(printer->device_uri, sani_uri, sizeof(sani_uri));
3128 snprintf(ppd, sizeof(ppd), "PPD=%s/ppd/%s.ppd", ServerRoot, printer->name);
3129 snprintf(printer_name, sizeof(printer_name), "PRINTER=%s", printer->name);
3130 snprintf(rip_max_cache, sizeof(rip_max_cache), "RIP_MAX_CACHE=%s", RIPCache);
3131
3132 envc = cupsdLoadEnv(envp, (int)(sizeof(envp) / sizeof(envp[0])));
3133
3134 envp[envc ++] = charset;
3135 envp[envc ++] = lang;
3136 #ifdef __APPLE__
3137 envp[envc ++] = apple_language;
3138 #endif /* __APPLE__ */
3139 envp[envc ++] = ppd;
3140 envp[envc ++] = rip_max_cache;
3141 envp[envc ++] = content_type;
3142 envp[envc ++] = device_uri;
3143 envp[envc ++] = printer_name;
3144
3145 if (!printer->remote && !printer->raw)
3146 {
3147 filter = (mime_filter_t *)cupsArrayLast(filters);
3148
3149 if (printer->port_monitor)
3150 filter = (mime_filter_t *)cupsArrayPrev(filters);
3151
3152 if (filter && filter->dst)
3153 {
3154 snprintf(final_content_type, sizeof(final_content_type),
3155 "FINAL_CONTENT_TYPE=%s/%s",
3156 filter->dst->super, filter->dst->type);
3157 envp[envc ++] = final_content_type;
3158 }
3159 }
3160
3161 if (Classification && !banner_page)
3162 {
3163 if ((attr = ippFindAttribute(job->attrs, "job-sheets",
3164 IPP_TAG_NAME)) == NULL)
3165 snprintf(classification, sizeof(classification), "CLASSIFICATION=%s",
3166 Classification);
3167 else if (attr->num_values > 1 &&
3168 strcmp(attr->values[1].string.text, "none") != 0)
3169 snprintf(classification, sizeof(classification), "CLASSIFICATION=%s",
3170 attr->values[1].string.text);
3171 else
3172 snprintf(classification, sizeof(classification), "CLASSIFICATION=%s",
3173 attr->values[0].string.text);
3174
3175 envp[envc ++] = classification;
3176 }
3177
3178 if (job->dtype & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT))
3179 {
3180 snprintf(class_name, sizeof(class_name), "CLASS=%s", job->dest);
3181 envp[envc ++] = class_name;
3182 }
3183
3184 if (job->auth_username)
3185 envp[envc ++] = job->auth_username;
3186 if (job->auth_domain)
3187 envp[envc ++] = job->auth_domain;
3188 if (job->auth_password)
3189 envp[envc ++] = job->auth_password;
3190
3191 #ifdef HAVE_GSSAPI
3192 if (job->ccname)
3193 envp[envc ++] = job->ccname;
3194 #endif /* HAVE_GSSAPI */
3195
3196 envp[envc] = NULL;
3197
3198 for (i = 0; i < envc; i ++)
3199 if (!strncmp(envp[i], "AUTH_", 5))
3200 cupsdLogJob(job, CUPSD_LOG_DEBUG, "envp[%d]=\"AUTH_%c****\"", i,
3201 envp[i][5]);
3202 else if (strncmp(envp[i], "DEVICE_URI=", 11))
3203 cupsdLogJob(job, CUPSD_LOG_DEBUG, "envp[%d]=\"%s\"", i, envp[i]);
3204 else
3205 cupsdLogJob(job, CUPSD_LOG_DEBUG, "envp[%d]=\"DEVICE_URI=%s\"", i,
3206 sani_uri);
3207
3208 if (printer->remote)
3209 job->current_file = job->num_files;
3210 else
3211 job->current_file ++;
3212
3213 /*
3214 * Now create processes for all of the filters...
3215 */
3216
3217 filterfds[0][0] = -1;
3218 filterfds[0][1] = -1;
3219 filterfds[1][0] = -1;
3220 filterfds[1][1] = -1;
3221
3222 if (!job->status_buffer)
3223 {
3224 if (cupsdOpenPipe(job->status_pipes))
3225 {
3226 cupsdLogJob(job, CUPSD_LOG_ERROR,
3227 "Unable to create job status pipes - %s.", strerror(errno));
3228 snprintf(printer->state_message, sizeof(printer->state_message),
3229 "Unable to create status pipes - %s.", strerror(errno));
3230
3231 cupsdAddPrinterHistory(printer);
3232
3233 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
3234 "Job canceled because the server could not create the job "
3235 "status pipes.");
3236
3237 goto abort_job;
3238 }
3239
3240 cupsdLogJob(job, CUPSD_LOG_DEBUG2,
3241 "start_job: status_pipes = [ %d %d ]",
3242 job->status_pipes[0], job->status_pipes[1]);
3243
3244 job->status_buffer = cupsdStatBufNew(job->status_pipes[0], NULL);
3245 job->status_level = CUPSD_LOG_INFO;
3246 }
3247
3248 job->status = 0;
3249 memset(job->filters, 0, sizeof(job->filters));
3250
3251 if (!job->profile)
3252 job->profile = cupsdCreateProfile(job->id);
3253
3254 for (i = 0, slot = 0, filter = (mime_filter_t *)cupsArrayFirst(filters);
3255 filter;
3256 i ++, filter = (mime_filter_t *)cupsArrayNext(filters))
3257 {
3258 if (filter->filter[0] != '/')
3259 snprintf(command, sizeof(command), "%s/filter/%s", ServerBin,
3260 filter->filter);
3261 else
3262 strlcpy(command, filter->filter, sizeof(command));
3263
3264 if (i < (cupsArrayCount(filters) - 1))
3265 {
3266 if (cupsdOpenPipe(filterfds[slot]))
3267 {
3268 cupsdLogJob(job, CUPSD_LOG_ERROR,
3269 "Unable to create job filter pipes - %s.", strerror(errno));
3270 snprintf(printer->state_message, sizeof(printer->state_message),
3271 "Unable to create filter pipes - %s.", strerror(errno));
3272 cupsdAddPrinterHistory(printer);
3273
3274 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
3275 "Job canceled because the server could not create the "
3276 "filter pipes.");
3277
3278 goto abort_job;
3279 }
3280 }
3281 else
3282 {
3283 if (job->current_file == 1)
3284 {
3285 if (strncmp(printer->device_uri, "file:", 5) != 0)
3286 {
3287 if (cupsdOpenPipe(job->print_pipes))
3288 {
3289 cupsdLogJob(job, CUPSD_LOG_ERROR,
3290 "Unable to create job backend pipes - %s.",
3291 strerror(errno));
3292 snprintf(printer->state_message, sizeof(printer->state_message),
3293 "Unable to create backend pipes - %s.", strerror(errno));
3294 cupsdAddPrinterHistory(printer);
3295
3296 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
3297 "Job canceled because the server could not create "
3298 "the backend pipes.");
3299
3300 goto abort_job;
3301 }
3302 }
3303 else
3304 {
3305 job->print_pipes[0] = -1;
3306 if (!strcmp(printer->device_uri, "file:/dev/null") ||
3307 !strcmp(printer->device_uri, "file:///dev/null"))
3308 job->print_pipes[1] = -1;
3309 else
3310 {
3311 if (!strncmp(printer->device_uri, "file:/dev/", 10))
3312 job->print_pipes[1] = open(printer->device_uri + 5,
3313 O_WRONLY | O_EXCL);
3314 else if (!strncmp(printer->device_uri, "file:///dev/", 12))
3315 job->print_pipes[1] = open(printer->device_uri + 7,
3316 O_WRONLY | O_EXCL);
3317 else if (!strncmp(printer->device_uri, "file:///", 8))
3318 job->print_pipes[1] = open(printer->device_uri + 7,
3319 O_WRONLY | O_CREAT | O_TRUNC, 0600);
3320 else
3321 job->print_pipes[1] = open(printer->device_uri + 5,
3322 O_WRONLY | O_CREAT | O_TRUNC, 0600);
3323
3324 if (job->print_pipes[1] < 0)
3325 {
3326 cupsdLogJob(job, CUPSD_LOG_ERROR,
3327 "Unable to open output file \"%s\" - %s.",
3328 printer->device_uri, strerror(errno));
3329 snprintf(printer->state_message, sizeof(printer->state_message),
3330 "Unable to open output file \"%s\" - %s.",
3331 printer->device_uri, strerror(errno));
3332
3333 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
3334 "Job canceled because the server could not open the "
3335 "output file.");
3336
3337 goto abort_job;
3338 }
3339
3340 fcntl(job->print_pipes[1], F_SETFD,
3341 fcntl(job->print_pipes[1], F_GETFD) | FD_CLOEXEC);
3342 }
3343 }
3344
3345 cupsdLogJob(job, CUPSD_LOG_DEBUG2,
3346 "start_job: print_pipes = [ %d %d ]",
3347 job->print_pipes[0], job->print_pipes[1]);
3348 }
3349
3350 filterfds[slot][0] = job->print_pipes[0];
3351 filterfds[slot][1] = job->print_pipes[1];
3352 }
3353
3354 cupsdLogJob(job, CUPSD_LOG_DEBUG2, "start_job: filter=\"%s\"", command);
3355 cupsdLogJob(job, CUPSD_LOG_DEBUG2, "start_job: filterfds[%d]=[ %d %d ]",
3356 slot, filterfds[slot][0], filterfds[slot][1]);
3357
3358 pid = cupsdStartProcess(command, argv, envp, filterfds[!slot][0],
3359 filterfds[slot][1], job->status_pipes[1],
3360 job->back_pipes[0], job->side_pipes[0], 0,
3361 job->profile, job->filters + i);
3362
3363 cupsdLogJob(job, CUPSD_LOG_DEBUG2,
3364 "start_job: Closing filter pipes for slot %d [ %d %d ]...",
3365 !slot, filterfds[!slot][0], filterfds[!slot][1]);
3366
3367 cupsdClosePipe(filterfds[!slot]);
3368
3369 if (pid == 0)
3370 {
3371 cupsdLogJob(job, CUPSD_LOG_ERROR, "Unable to start filter \"%s\" - %s.",
3372 filter->filter, strerror(errno));
3373 snprintf(printer->state_message, sizeof(printer->state_message),
3374 "Unable to start filter \"%s\" - %s.",
3375 filter->filter, strerror(errno));
3376
3377 cupsdAddPrinterHistory(printer);
3378
3379 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
3380 "Job canceled because the server could not execute a "
3381 "filter.");
3382
3383 goto abort_job;
3384 }
3385
3386 cupsdLogJob(job, CUPSD_LOG_INFO, "Started filter %s (PID %d)", command,
3387 pid);
3388
3389 argv[6] = NULL;
3390 slot = !slot;
3391 }
3392
3393 cupsArrayDelete(filters);
3394 filters = NULL;
3395
3396 /*
3397 * Finally, pipe the final output into a backend process if needed...
3398 */
3399
3400 if (strncmp(printer->device_uri, "file:", 5) != 0)
3401 {
3402 if (job->current_file == 1 || printer->remote)
3403 {
3404 sscanf(printer->device_uri, "%254[^:]", method);
3405 snprintf(command, sizeof(command), "%s/backend/%s", ServerBin, method);
3406
3407 /*
3408 * See if the backend needs to run as root...
3409 */
3410
3411 if (RunUser)
3412 backroot = 0;
3413 else if (stat(command, &backinfo))
3414 backroot = 0;
3415 else
3416 backroot = !(backinfo.st_mode & (S_IRWXG | S_IRWXO));
3417
3418 argv[0] = sani_uri;
3419
3420 filterfds[slot][0] = -1;
3421 filterfds[slot][1] = -1;
3422
3423 cupsdLogJob(job, CUPSD_LOG_DEBUG2, "start_job: backend=\"%s\"", command);
3424 cupsdLogJob(job, CUPSD_LOG_DEBUG2, "start_job: filterfds[%d] = [ %d %d ]",
3425 slot, filterfds[slot][0], filterfds[slot][1]);
3426
3427 pid = cupsdStartProcess(command, argv, envp, filterfds[!slot][0],
3428 filterfds[slot][1], job->status_pipes[1],
3429 job->back_pipes[1], job->side_pipes[1],
3430 backroot, job->profile, &(job->backend));
3431
3432 if (pid == 0)
3433 {
3434 cupsdLogJob(job, CUPSD_LOG_ERROR, "Unable to start backend \"%s\" - %s.",
3435 method, strerror(errno));
3436 snprintf(printer->state_message, sizeof(printer->state_message),
3437 "Unable to start backend \"%s\" - %s.", method,
3438 strerror(errno));
3439
3440 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
3441 "Job canceled because the server could not execute "
3442 "the backend.");
3443
3444 goto abort_job;
3445 }
3446 else
3447 {
3448 cupsdLogJob(job, CUPSD_LOG_INFO, "Started backend %s (PID %d)",
3449 command, pid);
3450 }
3451 }
3452
3453 if (job->current_file == job->num_files)
3454 {
3455 cupsdLogJob(job, CUPSD_LOG_DEBUG2,
3456 "start_job: Closing print pipes [ %d %d ]...",
3457 job->print_pipes[0], job->print_pipes[1]);
3458
3459 cupsdClosePipe(job->print_pipes);
3460
3461 cupsdLogJob(job, CUPSD_LOG_DEBUG2,
3462 "start_job: Closing back pipes [ %d %d ]...",
3463 job->back_pipes[0], job->back_pipes[1]);
3464
3465 cupsdClosePipe(job->back_pipes);
3466
3467 cupsdLogJob(job, CUPSD_LOG_DEBUG2,
3468 "start_job: Closing side pipes [ %d %d ]...",
3469 job->side_pipes[0], job->side_pipes[1]);
3470
3471 cupsdClosePipe(job->side_pipes);
3472
3473 cupsdLogJob(job, CUPSD_LOG_DEBUG2,
3474 "start_job: Closing status output pipe %d...",
3475 job->status_pipes[1]);
3476
3477 close(job->status_pipes[1]);
3478 job->status_pipes[1] = -1;
3479 }
3480 }
3481 else
3482 {
3483 filterfds[slot][0] = -1;
3484 filterfds[slot][1] = -1;
3485
3486 if (job->current_file == job->num_files)
3487 {
3488 cupsdLogJob(job, CUPSD_LOG_DEBUG2,
3489 "start_job: Closing print pipes [ %d %d ]...",
3490 job->print_pipes[0], job->print_pipes[1]);
3491
3492 cupsdClosePipe(job->print_pipes);
3493
3494 cupsdLogJob(job, CUPSD_LOG_DEBUG2,
3495 "start_job: Closing status output pipe %d...",
3496 job->status_pipes[1]);
3497
3498 close(job->status_pipes[1]);
3499 job->status_pipes[1] = -1;
3500 }
3501 }
3502
3503 cupsdLogJob(job, CUPSD_LOG_DEBUG2,
3504 "start_job: Closing filter pipes for slot %d [ %d %d ]...",
3505 slot, filterfds[slot][0], filterfds[slot][1]);
3506 cupsdClosePipe(filterfds[slot]);
3507
3508 if (printer->remote && job->num_files > 1)
3509 {
3510 for (i = 0; i < job->num_files; i ++)
3511 free(argv[i + 6]);
3512 }
3513
3514 free(argv);
3515
3516 cupsdAddSelect(job->status_buffer->fd, (cupsd_selfunc_t)update_job, NULL,
3517 job);
3518
3519 cupsdAddEvent(CUPSD_EVENT_JOB_STATE, job->printer, job, "Job #%d started.",
3520 job->id);
3521
3522 return;
3523
3524
3525 /*
3526 * If we get here, we need to abort the current job and close out all
3527 * files and pipes...
3528 */
3529
3530 abort_job:
3531
3532 for (slot = 0; slot < 2; slot ++)
3533 {
3534 cupsdLogJob(job, CUPSD_LOG_DEBUG2,
3535 "start_job: Closing filter pipes for slot %d [ %d %d ]...",
3536 slot, filterfds[slot][0], filterfds[slot][1]);
3537 cupsdClosePipe(filterfds[slot]);
3538 }
3539
3540 cupsdLogJob(job, CUPSD_LOG_DEBUG2,
3541 "start_job: Closing status pipes [ %d %d ]...",
3542 job->status_pipes[0], job->status_pipes[1]);
3543 cupsdClosePipe(job->status_pipes);
3544 cupsdStatBufDelete(job->status_buffer);
3545
3546 job->status_buffer = NULL;
3547
3548 cupsArrayDelete(filters);
3549
3550 if (printer->remote && job->num_files > 1)
3551 {
3552 for (i = 0; i < job->num_files; i ++)
3553 free(argv[i + 6]);
3554 }
3555
3556 free(argv);
3557
3558 cupsdStopJob(job, 0);
3559 }
3560
3561
3562 /*
3563 * 'unload_job()' - Unload a job from memory.
3564 */
3565
3566 static void
3567 unload_job(cupsd_job_t *job) /* I - Job */
3568 {
3569 if (!job->attrs)
3570 return;
3571
3572 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Unloading...");
3573
3574 ippDelete(job->attrs);
3575
3576 job->attrs = NULL;
3577 job->state = NULL;
3578 job->sheets = NULL;
3579 job->job_sheets = NULL;
3580 job->printer_message = NULL;
3581 job->printer_reasons = NULL;
3582 }
3583
3584
3585 /*
3586 * 'update_job()' - Read a status update from a job's filters.
3587 */
3588
3589 void
3590 update_job(cupsd_job_t *job) /* I - Job to check */
3591 {
3592 int i; /* Looping var */
3593 int copies; /* Number of copies printed */
3594 char message[1024], /* Message text */
3595 *ptr; /* Pointer update... */
3596 int loglevel, /* Log level for message */
3597 event = 0; /* Events? */
3598
3599
3600 while ((ptr = cupsdStatBufUpdate(job->status_buffer, &loglevel,
3601 message, sizeof(message))) != NULL)
3602 {
3603 /*
3604 * Process page and printer state messages as needed...
3605 */
3606
3607 if (loglevel == CUPSD_LOG_PAGE)
3608 {
3609 /*
3610 * Page message; send the message to the page_log file and update the
3611 * job sheet count...
3612 */
3613
3614 cupsdLogJob(job, CUPSD_LOG_DEBUG, "PAGE: %s", message);
3615
3616 if (job->sheets)
3617 {
3618 if (!strncasecmp(message, "total ", 6))
3619 {
3620 /*
3621 * Got a total count of pages from a backend or filter...
3622 */
3623
3624 copies = atoi(message + 6);
3625 copies -= job->sheets->values[0].integer; /* Just track the delta */
3626 }
3627 else if (!sscanf(message, "%*d%d", &copies))
3628 copies = 1;
3629
3630 job->sheets->values[0].integer += copies;
3631
3632 if (job->printer->page_limit)
3633 {
3634 cupsd_quota_t *q = cupsdUpdateQuota(job->printer, job->username,
3635 copies, 0);
3636
3637 #ifdef __APPLE__
3638 if (AppleQuotas && q->page_count == -3)
3639 {
3640 /*
3641 * Quota limit exceeded, cancel job in progress immediately...
3642 */
3643
3644 cupsdLogJob(job, CUPSD_LOG_INFO,
3645 "Canceled because pages exceed user %s "
3646 "quota limit on printer %s (%s).",
3647 job->username, job->printer->name,
3648 job->printer->info);
3649
3650 cupsdCancelJob(job, 1, IPP_JOB_CANCELED);
3651 return;
3652 }
3653 #else
3654 (void)q;
3655 #endif /* __APPLE__ */
3656 }
3657 }
3658
3659 cupsdLogPage(job, message);
3660
3661 if (job->sheets)
3662 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS, job->printer, job,
3663 "Printed %d page(s).", job->sheets->values[0].integer);
3664 }
3665 else if (loglevel == CUPSD_LOG_STATE)
3666 {
3667 cupsdLogJob(job, CUPSD_LOG_DEBUG, "STATE: %s", message);
3668
3669 if (!strcmp(message, "paused"))
3670 {
3671 cupsdStopPrinter(job->printer, 1);
3672 return;
3673 }
3674 else
3675 {
3676 cupsdSetPrinterReasons(job->printer, message);
3677 cupsdAddPrinterHistory(job->printer);
3678 event |= CUPSD_EVENT_PRINTER_STATE;
3679 }
3680
3681 update_job_attrs(job, 0);
3682 }
3683 else if (loglevel == CUPSD_LOG_ATTR)
3684 {
3685 /*
3686 * Set attribute(s)...
3687 */
3688
3689 int num_attrs; /* Number of attributes */
3690 cups_option_t *attrs; /* Attributes */
3691 const char *attr; /* Attribute */
3692
3693
3694 cupsdLogJob(job, CUPSD_LOG_DEBUG, "ATTR: %s", message);
3695
3696 num_attrs = cupsParseOptions(message, 0, &attrs);
3697
3698 if ((attr = cupsGetOption("auth-info-required", num_attrs,
3699 attrs)) != NULL)
3700 {
3701 cupsdSetAuthInfoRequired(job->printer, attr, NULL);
3702 cupsdSetPrinterAttrs(job->printer);
3703
3704 if (job->printer->type & CUPS_PRINTER_DISCOVERED)
3705 cupsdMarkDirty(CUPSD_DIRTY_REMOTE);
3706 else
3707 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
3708 }
3709
3710 if ((attr = cupsGetOption("job-media-progress", num_attrs,
3711 attrs)) != NULL)
3712 {
3713 int progress = atoi(attr);
3714
3715
3716 if (progress >= 0 && progress <= 100)
3717 {
3718 job->progress = progress;
3719
3720 if (job->sheets)
3721 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS, job->printer, job,
3722 "Printing page %d, %d%%",
3723 job->sheets->values[0].integer, job->progress);
3724 }
3725 }
3726
3727 if ((attr = cupsGetOption("printer-alert", num_attrs, attrs)) != NULL)
3728 {
3729 cupsdSetString(&job->printer->alert, attr);
3730 event |= CUPSD_EVENT_PRINTER_STATE;
3731 }
3732
3733 if ((attr = cupsGetOption("printer-alert-description", num_attrs,
3734 attrs)) != NULL)
3735 {
3736 cupsdSetString(&job->printer->alert_description, attr);
3737 event |= CUPSD_EVENT_PRINTER_STATE;
3738 }
3739
3740 if ((attr = cupsGetOption("marker-colors", num_attrs, attrs)) != NULL)
3741 {
3742 cupsdSetPrinterAttr(job->printer, "marker-colors", (char *)attr);
3743 job->printer->marker_time = time(NULL);
3744 event |= CUPSD_EVENT_PRINTER_STATE;
3745 }
3746
3747 if ((attr = cupsGetOption("marker-levels", num_attrs, attrs)) != NULL)
3748 {
3749 cupsdSetPrinterAttr(job->printer, "marker-levels", (char *)attr);
3750 job->printer->marker_time = time(NULL);
3751 event |= CUPSD_EVENT_PRINTER_STATE;
3752 }
3753
3754 if ((attr = cupsGetOption("marker-message", num_attrs, attrs)) != NULL)
3755 {
3756 cupsdSetPrinterAttr(job->printer, "marker-message", (char *)attr);
3757 job->printer->marker_time = time(NULL);
3758 event |= CUPSD_EVENT_PRINTER_STATE;
3759 }
3760
3761 if ((attr = cupsGetOption("marker-names", num_attrs, attrs)) != NULL)
3762 {
3763 cupsdSetPrinterAttr(job->printer, "marker-names", (char *)attr);
3764 job->printer->marker_time = time(NULL);
3765 event |= CUPSD_EVENT_PRINTER_STATE;
3766 }
3767
3768 if ((attr = cupsGetOption("marker-types", num_attrs, attrs)) != NULL)
3769 {
3770 cupsdSetPrinterAttr(job->printer, "marker-types", (char *)attr);
3771 job->printer->marker_time = time(NULL);
3772 event |= CUPSD_EVENT_PRINTER_STATE;
3773 }
3774
3775 cupsFreeOptions(num_attrs, attrs);
3776 }
3777 else if (loglevel == CUPSD_LOG_PPD)
3778 {
3779 /*
3780 * Set attribute(s)...
3781 */
3782
3783 int num_keywords; /* Number of keywords */
3784 cups_option_t *keywords; /* Keywords */
3785
3786
3787 cupsdLogJob(job, CUPSD_LOG_DEBUG, "PPD: %s", message);
3788
3789 num_keywords = cupsParseOptions(message, 0, &keywords);
3790
3791 if (cupsdUpdatePrinterPPD(job->printer, num_keywords, keywords))
3792 cupsdSetPrinterAttrs(job->printer);
3793
3794 cupsFreeOptions(num_keywords, keywords);
3795 }
3796 #ifdef __APPLE__
3797 else if (!strncmp(message, "recoverable:", 12))
3798 {
3799 cupsdSetPrinterReasons(job->printer,
3800 "+com.apple.print.recoverable-warning");
3801
3802 ptr = message + 12;
3803 while (isspace(*ptr & 255))
3804 ptr ++;
3805
3806 cupsdSetString(&job->printer->recoverable, ptr);
3807 cupsdAddPrinterHistory(job->printer);
3808 event |= CUPSD_EVENT_PRINTER_STATE;
3809 }
3810 else if (!strncmp(message, "recovered:", 10))
3811 {
3812 cupsdSetPrinterReasons(job->printer,
3813 "-com.apple.print.recoverable-warning");
3814
3815 ptr = message + 10;
3816 while (isspace(*ptr & 255))
3817 ptr ++;
3818
3819 cupsdSetString(&job->printer->recoverable, ptr);
3820 cupsdAddPrinterHistory(job->printer);
3821 event |= CUPSD_EVENT_PRINTER_STATE;
3822 }
3823 #endif /* __APPLE__ */
3824 else
3825 {
3826 cupsdLogJob(job, loglevel, "%s", message);
3827
3828 if (loglevel < CUPSD_LOG_DEBUG)
3829 {
3830 strlcpy(job->printer->state_message, message,
3831 sizeof(job->printer->state_message));
3832 cupsdAddPrinterHistory(job->printer);
3833
3834 event |= CUPSD_EVENT_PRINTER_STATE;
3835
3836 if (loglevel <= job->status_level)
3837 {
3838 /*
3839 * Some messages show in the printer-state-message attribute...
3840 */
3841
3842 if (loglevel != CUPSD_LOG_NOTICE)
3843 job->status_level = loglevel;
3844
3845 update_job_attrs(job, 1);
3846 }
3847 }
3848 }
3849
3850 if (!strchr(job->status_buffer->buffer, '\n'))
3851 break;
3852 }
3853
3854 if (event & CUPSD_EVENT_PRINTER_STATE)
3855 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, job->printer, NULL,
3856 (job->printer->type & CUPS_PRINTER_CLASS) ?
3857 "Class \"%s\" state changed." :
3858 "Printer \"%s\" state changed.",
3859 job->printer->name);
3860
3861 if (ptr == NULL && !job->status_buffer->bufused)
3862 {
3863 /*
3864 * See if all of the filters and the backend have returned their
3865 * exit statuses.
3866 */
3867
3868 for (i = 0; job->filters[i] < 0; i ++);
3869
3870 if (job->filters[i])
3871 return;
3872
3873 if (job->current_file >= job->num_files && job->backend > 0)
3874 return;
3875
3876 /*
3877 * Handle the end of job stuff...
3878 */
3879
3880 cupsdFinishJob(job);
3881 }
3882 }
3883
3884
3885 /*
3886 * 'update_job_attrs()' - Update the job-printer-* attributes.
3887 */
3888
3889 void
3890 update_job_attrs(cupsd_job_t *job, /* I - Job to update */
3891 int do_message)/* I - 1 = update job-printer-state message */
3892 {
3893 int i; /* Looping var */
3894 int num_reasons; /* Actual number of reasons */
3895 const char * const *reasons; /* Reasons */
3896 static const char *none = "none", /* "none" */
3897 *paused = "paused";
3898 /* "paused" */
3899
3900
3901 /*
3902 * Get/create the job-printer-state-* attributes...
3903 */
3904
3905 if (!job->printer_message)
3906 {
3907 if ((job->printer_message = ippFindAttribute(job->attrs,
3908 "job-printer-state-message",
3909 IPP_TAG_TEXT)) == NULL)
3910 job->printer_message = ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_TEXT,
3911 "job-printer-state-message",
3912 NULL, "");
3913 }
3914
3915 if (!job->printer_reasons)
3916 job->printer_reasons = ippFindAttribute(job->attrs,
3917 "job-printer-state-reasons",
3918 IPP_TAG_KEYWORD);
3919
3920 /*
3921 * If the job isn't printing, return now...
3922 */
3923
3924 if (!job->printer)
3925 return;
3926
3927 /*
3928 * Otherwise copy the printer-state-message value...
3929 */
3930
3931 if (job->printer->state_message[0] &&
3932 (do_message || !job->printer_message->values[0].string.text[0]))
3933 cupsdSetString(&(job->printer_message->values[0].string.text),
3934 job->printer->state_message);
3935
3936 /*
3937 * ... and the printer-state-reasons value...
3938 */
3939
3940 if (job->printer->num_reasons == 0)
3941 {
3942 num_reasons = 1;
3943 reasons = job->printer->state == IPP_PRINTER_STOPPED ? &paused : &none;
3944 }
3945 else
3946 {
3947 num_reasons = job->printer->num_reasons;
3948 reasons = (const char * const *)job->printer->reasons;
3949 }
3950
3951 if (!job->printer_reasons || job->printer_reasons->num_values != num_reasons)
3952 {
3953 ippDeleteAttribute(job->attrs, job->printer_reasons);
3954
3955 job->printer_reasons = ippAddStrings(job->attrs,
3956 IPP_TAG_JOB, IPP_TAG_KEYWORD,
3957 "job-printer-state-reasons",
3958 num_reasons, NULL, NULL);
3959 }
3960
3961 for (i = 0; i < num_reasons; i ++)
3962 cupsdSetString(&(job->printer_reasons->values[i].string.text), reasons[i]);
3963 }
3964
3965
3966 /*
3967 * End of "$Id: job.c 7682 2008-06-21 00:06:02Z mike $".
3968 */