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