]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/job.c
Merge changes from CUPS 1.4svn-r8162.
[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 ((job->state = ippFindAttribute(job->attrs, "job-state",
1116 IPP_TAG_ENUM)) == NULL)
1117 {
1118 cupsdLogMessage(CUPSD_LOG_ERROR,
1119 "[Job %d] Missing or bad job-state attribute in control "
1120 "file!", job->id);
1121 ippDelete(job->attrs);
1122 job->attrs = NULL;
1123 unlink(jobfile);
1124 return;
1125 }
1126
1127 job->state_value = (ipp_jstate_t)job->state->values[0].integer;
1128
1129 if (!job->dest)
1130 {
1131 if ((attr = ippFindAttribute(job->attrs, "job-printer-uri",
1132 IPP_TAG_URI)) == NULL)
1133 {
1134 cupsdLogMessage(CUPSD_LOG_ERROR,
1135 "[Job %d] No job-printer-uri attribute in control file!",
1136 job->id);
1137 ippDelete(job->attrs);
1138 job->attrs = NULL;
1139 unlink(jobfile);
1140 return;
1141 }
1142
1143 if ((dest = cupsdValidateDest(attr->values[0].string.text, &(job->dtype),
1144 &destptr)) == NULL)
1145 {
1146 cupsdLogMessage(CUPSD_LOG_ERROR,
1147 "[Job %d] Unable to queue job for destination \"%s\"!",
1148 job->id, attr->values[0].string.text);
1149 ippDelete(job->attrs);
1150 job->attrs = NULL;
1151 unlink(jobfile);
1152 return;
1153 }
1154
1155 cupsdSetString(&job->dest, dest);
1156 }
1157 else if ((destptr = cupsdFindDest(job->dest)) == NULL)
1158 {
1159 cupsdLogMessage(CUPSD_LOG_ERROR,
1160 "[Job %d] Unable to queue job for destination \"%s\"!",
1161 job->id, job->dest);
1162 ippDelete(job->attrs);
1163 job->attrs = NULL;
1164 unlink(jobfile);
1165 return;
1166 }
1167
1168 job->sheets = ippFindAttribute(job->attrs, "job-media-sheets-completed",
1169 IPP_TAG_INTEGER);
1170 job->job_sheets = ippFindAttribute(job->attrs, "job-sheets", IPP_TAG_NAME);
1171
1172 if (!job->priority)
1173 {
1174 if ((attr = ippFindAttribute(job->attrs, "job-priority",
1175 IPP_TAG_INTEGER)) == NULL)
1176 {
1177 cupsdLogMessage(CUPSD_LOG_ERROR,
1178 "[Job %d] Missing or bad job-priority attribute in "
1179 "control file!", job->id);
1180 ippDelete(job->attrs);
1181 job->attrs = NULL;
1182 unlink(jobfile);
1183 return;
1184 }
1185
1186 job->priority = attr->values[0].integer;
1187 }
1188
1189 if (!job->username)
1190 {
1191 if ((attr = ippFindAttribute(job->attrs, "job-originating-user-name",
1192 IPP_TAG_NAME)) == NULL)
1193 {
1194 cupsdLogMessage(CUPSD_LOG_ERROR,
1195 "[Job %d] Missing or bad job-originating-user-name "
1196 "attribute in control file!", job->id);
1197 ippDelete(job->attrs);
1198 job->attrs = NULL;
1199 unlink(jobfile);
1200 return;
1201 }
1202
1203 cupsdSetString(&job->username, attr->values[0].string.text);
1204 }
1205
1206 /*
1207 * Set the job hold-until time and state...
1208 */
1209
1210 if (job->state_value == IPP_JOB_HELD)
1211 {
1212 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
1213 IPP_TAG_KEYWORD)) == NULL)
1214 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
1215
1216 if (attr)
1217 cupsdSetJobHoldUntil(job, attr->values[0].string.text);
1218 else
1219 {
1220 job->state->values[0].integer = IPP_JOB_PENDING;
1221 job->state_value = IPP_JOB_PENDING;
1222 }
1223 }
1224 else if (job->state_value == IPP_JOB_PROCESSING)
1225 {
1226 job->state->values[0].integer = IPP_JOB_PENDING;
1227 job->state_value = IPP_JOB_PENDING;
1228 }
1229
1230 if (!job->num_files)
1231 {
1232 /*
1233 * Find all the d##### files...
1234 */
1235
1236 for (fileid = 1; fileid < 10000; fileid ++)
1237 {
1238 snprintf(jobfile, sizeof(jobfile), "%s/d%05d-%03d", RequestRoot,
1239 job->id, fileid);
1240
1241 if (access(jobfile, 0))
1242 break;
1243
1244 cupsdLogMessage(CUPSD_LOG_DEBUG,
1245 "[Job %d] Auto-typing document file \"%s\"...", job->id,
1246 jobfile);
1247
1248 if (fileid > job->num_files)
1249 {
1250 if (job->num_files == 0)
1251 {
1252 compressions = (int *)calloc(fileid, sizeof(int));
1253 filetypes = (mime_type_t **)calloc(fileid, sizeof(mime_type_t *));
1254 }
1255 else
1256 {
1257 compressions = (int *)realloc(job->compressions,
1258 sizeof(int) * fileid);
1259 filetypes = (mime_type_t **)realloc(job->filetypes,
1260 sizeof(mime_type_t *) *
1261 fileid);
1262 }
1263
1264 if (!compressions || !filetypes)
1265 {
1266 cupsdLogMessage(CUPSD_LOG_ERROR,
1267 "[Job %d] Ran out of memory for job file types!",
1268 job->id);
1269 return;
1270 }
1271
1272 job->compressions = compressions;
1273 job->filetypes = filetypes;
1274 job->num_files = fileid;
1275 }
1276
1277 job->filetypes[fileid - 1] = mimeFileType(MimeDatabase, jobfile, NULL,
1278 job->compressions + fileid - 1);
1279
1280 if (!job->filetypes[fileid - 1])
1281 job->filetypes[fileid - 1] = mimeType(MimeDatabase, "application",
1282 "vnd.cups-raw");
1283 }
1284 }
1285
1286 /*
1287 * Load authentication information as needed...
1288 */
1289
1290 if (job->state_value < IPP_JOB_STOPPED)
1291 {
1292 snprintf(jobfile, sizeof(jobfile), "%s/a%05d", RequestRoot, job->id);
1293
1294 cupsdClearString(&job->auth_username);
1295 cupsdClearString(&job->auth_domain);
1296 cupsdClearString(&job->auth_password);
1297
1298 if ((fp = cupsFileOpen(jobfile, "r")) != NULL)
1299 {
1300 int i, /* Looping var */
1301 bytes; /* Size of auth data */
1302 char line[255], /* Line from file */
1303 data[255]; /* Decoded data */
1304
1305
1306 for (i = 0;
1307 i < destptr->num_auth_info_required &&
1308 cupsFileGets(fp, line, sizeof(line));
1309 i ++)
1310 {
1311 bytes = sizeof(data);
1312 httpDecode64_2(data, &bytes, line);
1313
1314 if (!strcmp(destptr->auth_info_required[i], "username"))
1315 cupsdSetStringf(&job->auth_username, "AUTH_USERNAME=%s", data);
1316 else if (!strcmp(destptr->auth_info_required[i], "domain"))
1317 cupsdSetStringf(&job->auth_domain, "AUTH_DOMAIN=%s", data);
1318 else if (!strcmp(destptr->auth_info_required[i], "password"))
1319 cupsdSetStringf(&job->auth_password, "AUTH_PASSWORD=%s", data);
1320 }
1321
1322 cupsFileClose(fp);
1323 }
1324 }
1325
1326 job->access_time = time(NULL);
1327 }
1328
1329
1330 /*
1331 * 'cupsdMoveJob()' - Move the specified job to a different destination.
1332 */
1333
1334 void
1335 cupsdMoveJob(cupsd_job_t *job, /* I - Job */
1336 cupsd_printer_t *p) /* I - Destination printer or class */
1337 {
1338 ipp_attribute_t *attr; /* job-printer-uri attribute */
1339 const char *olddest; /* Old destination */
1340 cupsd_printer_t *oldp; /* Old pointer */
1341
1342
1343 /*
1344 * Don't move completed jobs...
1345 */
1346
1347 if (job->state_value > IPP_JOB_STOPPED)
1348 return;
1349
1350 /*
1351 * Get the old destination...
1352 */
1353
1354 olddest = job->dest;
1355
1356 if (job->printer)
1357 oldp = job->printer;
1358 else
1359 oldp = cupsdFindDest(olddest);
1360
1361 /*
1362 * Change the destination information...
1363 */
1364
1365 if (job->state_value == IPP_JOB_PROCESSING)
1366 cupsdStopJob(job, 0);
1367 else
1368 cupsdLoadJob(job);
1369
1370 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED, oldp, job,
1371 "Job #%d moved from %s to %s.", job->id, olddest,
1372 p->name);
1373
1374 cupsdSetString(&job->dest, p->name);
1375 job->dtype = p->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_REMOTE |
1376 CUPS_PRINTER_IMPLICIT);
1377
1378 if ((attr = ippFindAttribute(job->attrs, "job-printer-uri",
1379 IPP_TAG_URI)) != NULL)
1380 cupsdSetString(&(attr->values[0].string.text), p->uri);
1381
1382 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED, p, job,
1383 "Job #%d moved from %s to %s.", job->id, olddest,
1384 p->name);
1385
1386 job->dirty = 1;
1387 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
1388 }
1389
1390
1391 /*
1392 * 'cupsdReleaseJob()' - Release the specified job.
1393 */
1394
1395 void
1396 cupsdReleaseJob(cupsd_job_t *job) /* I - Job */
1397 {
1398 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdReleaseJob: id = %d", job->id);
1399
1400 if (job->state_value == IPP_JOB_HELD)
1401 {
1402 /*
1403 * Add trailing banner as needed...
1404 */
1405
1406 if (job->pending_timeout)
1407 cupsdTimeoutJob(job);
1408
1409 DEBUG_puts("cupsdReleaseJob: setting state to pending...");
1410
1411 job->state->values[0].integer = IPP_JOB_PENDING;
1412 job->state_value = IPP_JOB_PENDING;
1413 job->dirty = 1;
1414 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
1415 }
1416 }
1417
1418
1419 /*
1420 * 'cupsdRestartJob()' - Restart the specified job.
1421 */
1422
1423 void
1424 cupsdRestartJob(cupsd_job_t *job) /* I - Job */
1425 {
1426 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdRestartJob: id = %d", job->id);
1427
1428 if (job->state_value == IPP_JOB_STOPPED || job->num_files)
1429 {
1430 ipp_jstate_t old_state; /* Old job state */
1431
1432
1433 cupsdLoadJob(job);
1434
1435 old_state = job->state_value;
1436
1437 job->tries = 0;
1438 job->state->values[0].integer = IPP_JOB_PENDING;
1439 job->state_value = IPP_JOB_PENDING;
1440
1441 job->dirty = 1;
1442 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
1443
1444 if (old_state > IPP_JOB_STOPPED)
1445 cupsArrayAdd(ActiveJobs, job);
1446 }
1447 }
1448
1449
1450 /*
1451 * 'cupsdSaveAllJobs()' - Save a summary of all jobs to disk.
1452 */
1453
1454 void
1455 cupsdSaveAllJobs(void)
1456 {
1457 int i; /* Looping var */
1458 cups_file_t *fp; /* Job cache file */
1459 char temp[1024]; /* Temporary string */
1460 cupsd_job_t *job; /* Current job */
1461 time_t curtime; /* Current time */
1462 struct tm *curdate; /* Current date */
1463
1464
1465 snprintf(temp, sizeof(temp), "%s/job.cache", CacheDir);
1466 if ((fp = cupsFileOpen(temp, "w")) == NULL)
1467 {
1468 cupsdLogMessage(CUPSD_LOG_ERROR,
1469 "Unable to create job cache file \"%s\" - %s",
1470 temp, strerror(errno));
1471 return;
1472 }
1473
1474 cupsdLogMessage(CUPSD_LOG_INFO, "Saving job cache file \"%s\"...", temp);
1475
1476 /*
1477 * Restrict access to the file...
1478 */
1479
1480 fchown(cupsFileNumber(fp), getuid(), Group);
1481 fchmod(cupsFileNumber(fp), ConfigFilePerm);
1482
1483 /*
1484 * Write a small header to the file...
1485 */
1486
1487 curtime = time(NULL);
1488 curdate = localtime(&curtime);
1489 strftime(temp, sizeof(temp) - 1, "%Y-%m-%d %H:%M", curdate);
1490
1491 cupsFilePuts(fp, "# Job cache file for " CUPS_SVERSION "\n");
1492 cupsFilePrintf(fp, "# Written by cupsd on %s\n", temp);
1493 cupsFilePrintf(fp, "NextJobId %d\n", NextJobId);
1494
1495 /*
1496 * Write each job known to the system...
1497 */
1498
1499 for (job = (cupsd_job_t *)cupsArrayFirst(Jobs);
1500 job;
1501 job = (cupsd_job_t *)cupsArrayNext(Jobs))
1502 {
1503 cupsFilePrintf(fp, "<Job %d>\n", job->id);
1504 cupsFilePrintf(fp, "State %d\n", job->state_value);
1505 cupsFilePrintf(fp, "Priority %d\n", job->priority);
1506 cupsFilePrintf(fp, "Username %s\n", job->username);
1507 cupsFilePrintf(fp, "Destination %s\n", job->dest);
1508 cupsFilePrintf(fp, "DestType %d\n", job->dtype);
1509 cupsFilePrintf(fp, "NumFiles %d\n", job->num_files);
1510 for (i = 0; i < job->num_files; i ++)
1511 cupsFilePrintf(fp, "File %d %s/%s %d\n", i + 1, job->filetypes[i]->super,
1512 job->filetypes[i]->type, job->compressions[i]);
1513 cupsFilePuts(fp, "</Job>\n");
1514 }
1515
1516 cupsFileClose(fp);
1517 }
1518
1519
1520 /*
1521 * 'cupsdSaveJob()' - Save a job to disk.
1522 */
1523
1524 void
1525 cupsdSaveJob(cupsd_job_t *job) /* I - Job */
1526 {
1527 char filename[1024]; /* Job control filename */
1528 cups_file_t *fp; /* Job file */
1529
1530
1531 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdSaveJob(job=%p(%d)): job->attrs=%p",
1532 job, job->id, job->attrs);
1533
1534 snprintf(filename, sizeof(filename), "%s/c%05d", RequestRoot, job->id);
1535
1536 if ((fp = cupsFileOpen(filename, "w")) == NULL)
1537 {
1538 cupsdLogMessage(CUPSD_LOG_ERROR,
1539 "[Job %d] Unable to create job control file \"%s\" - %s.",
1540 job->id, filename, strerror(errno));
1541 return;
1542 }
1543
1544 fchmod(cupsFileNumber(fp), 0600);
1545 fchown(cupsFileNumber(fp), RunUser, Group);
1546
1547 job->attrs->state = IPP_IDLE;
1548
1549 if (ippWriteIO(fp, (ipp_iocb_t)cupsFileWrite, 1, NULL,
1550 job->attrs) != IPP_DATA)
1551 cupsdLogMessage(CUPSD_LOG_ERROR,
1552 "[Job %d] Unable to write job control file!", job->id);
1553
1554 cupsFileClose(fp);
1555
1556 job->dirty = 0;
1557 }
1558
1559
1560 /*
1561 * 'cupsdSetJobHoldUntil()' - Set the hold time for a job...
1562 */
1563
1564 void
1565 cupsdSetJobHoldUntil(cupsd_job_t *job, /* I - Job */
1566 const char *when) /* I - When to resume */
1567 {
1568 time_t curtime; /* Current time */
1569 struct tm *curdate; /* Current date */
1570 int hour; /* Hold hour */
1571 int minute; /* Hold minute */
1572 int second; /* Hold second */
1573
1574
1575 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdSetJobHoldUntil(%d, \"%s\")",
1576 job->id, when);
1577
1578 second = 0;
1579
1580 if (!strcmp(when, "indefinite") || !strcmp(when, "auth-info-required"))
1581 {
1582 /*
1583 * Hold indefinitely...
1584 */
1585
1586 job->hold_until = 0;
1587 }
1588 else if (!strcmp(when, "day-time"))
1589 {
1590 /*
1591 * Hold to 6am the next morning unless local time is < 6pm.
1592 */
1593
1594 curtime = time(NULL);
1595 curdate = localtime(&curtime);
1596
1597 if (curdate->tm_hour < 18)
1598 job->hold_until = curtime;
1599 else
1600 job->hold_until = curtime +
1601 ((29 - curdate->tm_hour) * 60 + 59 -
1602 curdate->tm_min) * 60 + 60 - curdate->tm_sec;
1603 }
1604 else if (!strcmp(when, "evening") || !strcmp(when, "night"))
1605 {
1606 /*
1607 * Hold to 6pm unless local time is > 6pm or < 6am.
1608 */
1609
1610 curtime = time(NULL);
1611 curdate = localtime(&curtime);
1612
1613 if (curdate->tm_hour < 6 || curdate->tm_hour >= 18)
1614 job->hold_until = curtime;
1615 else
1616 job->hold_until = curtime +
1617 ((17 - curdate->tm_hour) * 60 + 59 -
1618 curdate->tm_min) * 60 + 60 - curdate->tm_sec;
1619 }
1620 else if (!strcmp(when, "second-shift"))
1621 {
1622 /*
1623 * Hold to 4pm unless local time is > 4pm.
1624 */
1625
1626 curtime = time(NULL);
1627 curdate = localtime(&curtime);
1628
1629 if (curdate->tm_hour >= 16)
1630 job->hold_until = curtime;
1631 else
1632 job->hold_until = curtime +
1633 ((15 - curdate->tm_hour) * 60 + 59 -
1634 curdate->tm_min) * 60 + 60 - curdate->tm_sec;
1635 }
1636 else if (!strcmp(when, "third-shift"))
1637 {
1638 /*
1639 * Hold to 12am unless local time is < 8am.
1640 */
1641
1642 curtime = time(NULL);
1643 curdate = localtime(&curtime);
1644
1645 if (curdate->tm_hour < 8)
1646 job->hold_until = curtime;
1647 else
1648 job->hold_until = curtime +
1649 ((23 - curdate->tm_hour) * 60 + 59 -
1650 curdate->tm_min) * 60 + 60 - curdate->tm_sec;
1651 }
1652 else if (!strcmp(when, "weekend"))
1653 {
1654 /*
1655 * Hold to weekend unless we are in the weekend.
1656 */
1657
1658 curtime = time(NULL);
1659 curdate = localtime(&curtime);
1660
1661 if (curdate->tm_wday == 0 || curdate->tm_wday == 6)
1662 job->hold_until = curtime;
1663 else
1664 job->hold_until = curtime +
1665 (((5 - curdate->tm_wday) * 24 +
1666 (17 - curdate->tm_hour)) * 60 + 59 -
1667 curdate->tm_min) * 60 + 60 - curdate->tm_sec;
1668 }
1669 else if (sscanf(when, "%d:%d:%d", &hour, &minute, &second) >= 2)
1670 {
1671 /*
1672 * Hold to specified GMT time (HH:MM or HH:MM:SS)...
1673 */
1674
1675 curtime = time(NULL);
1676 curdate = gmtime(&curtime);
1677
1678 job->hold_until = curtime +
1679 ((hour - curdate->tm_hour) * 60 + minute -
1680 curdate->tm_min) * 60 + second - curdate->tm_sec;
1681
1682 /*
1683 * Hold until next day as needed...
1684 */
1685
1686 if (job->hold_until < curtime)
1687 job->hold_until += 24 * 60 * 60;
1688 }
1689
1690 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdSetJobHoldUntil: hold_until = %d",
1691 (int)job->hold_until);
1692 }
1693
1694
1695 /*
1696 * 'cupsdSetJobPriority()' - Set the priority of a job, moving it up/down in
1697 * the list as needed.
1698 */
1699
1700 void
1701 cupsdSetJobPriority(
1702 cupsd_job_t *job, /* I - Job ID */
1703 int priority) /* I - New priority (0 to 100) */
1704 {
1705 ipp_attribute_t *attr; /* Job attribute */
1706
1707
1708 /*
1709 * Don't change completed jobs...
1710 */
1711
1712 if (job->state_value >= IPP_JOB_PROCESSING)
1713 return;
1714
1715 /*
1716 * Set the new priority and re-add the job into the active list...
1717 */
1718
1719 cupsArrayRemove(ActiveJobs, job);
1720
1721 job->priority = priority;
1722
1723 if ((attr = ippFindAttribute(job->attrs, "job-priority",
1724 IPP_TAG_INTEGER)) != NULL)
1725 attr->values[0].integer = priority;
1726 else
1727 ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-priority",
1728 priority);
1729
1730 cupsArrayAdd(ActiveJobs, job);
1731
1732 job->dirty = 1;
1733 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
1734 }
1735
1736
1737 /*
1738 * 'cupsdStopAllJobs()' - Stop all print jobs.
1739 */
1740
1741 void
1742 cupsdStopAllJobs(int force) /* I - 1 = Force all filters to stop */
1743 {
1744 cupsd_job_t *job; /* Current job */
1745
1746
1747 DEBUG_puts("cupsdStopAllJobs()");
1748
1749 for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs);
1750 job;
1751 job = (cupsd_job_t *)cupsArrayNext(ActiveJobs))
1752 if (job->state_value == IPP_JOB_PROCESSING)
1753 {
1754 cupsdStopJob(job, force);
1755 job->state->values[0].integer = IPP_JOB_PENDING;
1756 job->state_value = IPP_JOB_PENDING;
1757 }
1758 }
1759
1760
1761 /*
1762 * 'cupsdStopJob()' - Stop a print job.
1763 */
1764
1765 void
1766 cupsdStopJob(cupsd_job_t *job, /* I - Job */
1767 int force) /* I - 1 = Force all filters to stop */
1768 {
1769 int i; /* Looping var */
1770
1771
1772 cupsdLogMessage(CUPSD_LOG_DEBUG2, "[Job %d] cupsdStopJob: force = %d",
1773 job->id, force);
1774
1775 if (job->state_value != IPP_JOB_PROCESSING)
1776 return;
1777
1778 FilterLevel -= job->cost;
1779
1780 if (job->printer->state == IPP_PRINTER_PROCESSING)
1781 cupsdSetPrinterState(job->printer, IPP_PRINTER_IDLE, 0);
1782
1783 job->state->values[0].integer = IPP_JOB_STOPPED;
1784 job->state_value = IPP_JOB_STOPPED;
1785 job->printer->job = NULL;
1786 job->printer = NULL;
1787
1788 job->current_file --;
1789
1790 for (i = 0; job->filters[i]; i ++)
1791 if (job->filters[i] > 0)
1792 {
1793 cupsdEndProcess(job->filters[i], force);
1794 job->filters[i] = 0;
1795 }
1796
1797 if (job->backend > 0)
1798 {
1799 cupsdEndProcess(job->backend, force);
1800 job->backend = 0;
1801 }
1802
1803 cupsdDestroyProfile(job->profile);
1804 job->profile = NULL;
1805
1806 cupsdLogMessage(CUPSD_LOG_DEBUG2, "[Job %d] Closing print pipes [ %d %d ]...",
1807 job->id, job->print_pipes[0], job->print_pipes[1]);
1808
1809 cupsdClosePipe(job->print_pipes);
1810
1811 cupsdLogMessage(CUPSD_LOG_DEBUG2, "[Job %d] Closing back pipes [ %d %d ]...",
1812 job->id, job->back_pipes[0], job->back_pipes[1]);
1813
1814 cupsdClosePipe(job->back_pipes);
1815
1816 cupsdLogMessage(CUPSD_LOG_DEBUG2, "[Job %d] Closing side pipes [ %d %d ]...",
1817 job->id, job->side_pipes[0], job->side_pipes[1]);
1818
1819 cupsdClosePipe(job->side_pipes);
1820
1821 if (job->status_buffer)
1822 {
1823 /*
1824 * Close the pipe and clear the input bit.
1825 */
1826
1827 cupsdRemoveSelect(job->status_buffer->fd);
1828
1829 cupsdLogMessage(CUPSD_LOG_DEBUG2,
1830 "[Job %d] Closing status pipes [ %d %d ]...",
1831 job->id, job->status_pipes[0], job->status_pipes[1]);
1832
1833 cupsdClosePipe(job->status_pipes);
1834 cupsdStatBufDelete(job->status_buffer);
1835
1836 job->status_buffer = NULL;
1837 }
1838 }
1839
1840
1841 /*
1842 * 'cupsdUnloadCompletedJobs()' - Flush completed job history from memory.
1843 */
1844
1845 void
1846 cupsdUnloadCompletedJobs(void)
1847 {
1848 cupsd_job_t *job; /* Current job */
1849 time_t expire; /* Expiration time */
1850
1851
1852 expire = time(NULL) - 60;
1853
1854 for (job = (cupsd_job_t *)cupsArrayFirst(Jobs);
1855 job;
1856 job = (cupsd_job_t *)cupsArrayNext(Jobs))
1857 if (job->attrs && job->state_value >= IPP_JOB_STOPPED &&
1858 job->access_time < expire)
1859 {
1860 if (job->dirty)
1861 cupsdSaveJob(job);
1862
1863 unload_job(job);
1864 }
1865 }
1866
1867
1868 /*
1869 * 'compare_active_jobs()' - Compare the job IDs and priorities of two jobs.
1870 */
1871
1872 static int /* O - Difference */
1873 compare_active_jobs(void *first, /* I - First job */
1874 void *second, /* I - Second job */
1875 void *data) /* I - App data (not used) */
1876 {
1877 int diff; /* Difference */
1878
1879
1880 if ((diff = ((cupsd_job_t *)second)->priority -
1881 ((cupsd_job_t *)first)->priority) != 0)
1882 return (diff);
1883 else
1884 return (((cupsd_job_t *)first)->id - ((cupsd_job_t *)second)->id);
1885 }
1886
1887
1888 /*
1889 * 'compare_jobs()' - Compare the job IDs of two jobs.
1890 */
1891
1892 static int /* O - Difference */
1893 compare_jobs(void *first, /* I - First job */
1894 void *second, /* I - Second job */
1895 void *data) /* I - App data (not used) */
1896 {
1897 return (((cupsd_job_t *)first)->id - ((cupsd_job_t *)second)->id);
1898 }
1899
1900
1901 /*
1902 * 'ipp_length()' - Compute the size of the buffer needed to hold
1903 * the textual IPP attributes.
1904 */
1905
1906 static int /* O - Size of attribute buffer */
1907 ipp_length(ipp_t *ipp) /* I - IPP request */
1908 {
1909 int bytes; /* Number of bytes */
1910 int i; /* Looping var */
1911 ipp_attribute_t *attr; /* Current attribute */
1912
1913
1914 /*
1915 * Loop through all attributes...
1916 */
1917
1918 bytes = 0;
1919
1920 for (attr = ipp->attrs; attr != NULL; attr = attr->next)
1921 {
1922 /*
1923 * Skip attributes that won't be sent to filters...
1924 */
1925
1926 if (attr->value_tag == IPP_TAG_MIMETYPE ||
1927 attr->value_tag == IPP_TAG_NAMELANG ||
1928 attr->value_tag == IPP_TAG_TEXTLANG ||
1929 attr->value_tag == IPP_TAG_URI ||
1930 attr->value_tag == IPP_TAG_URISCHEME)
1931 continue;
1932
1933 if (strncmp(attr->name, "time-", 5) == 0)
1934 continue;
1935
1936 /*
1937 * Add space for a leading space and commas between each value.
1938 * For the first attribute, the leading space isn't used, so the
1939 * extra byte can be used as the nul terminator...
1940 */
1941
1942 bytes ++; /* " " separator */
1943 bytes += attr->num_values; /* "," separators */
1944
1945 /*
1946 * Boolean attributes appear as "foo,nofoo,foo,nofoo", while
1947 * other attributes appear as "foo=value1,value2,...,valueN".
1948 */
1949
1950 if (attr->value_tag != IPP_TAG_BOOLEAN)
1951 bytes += strlen(attr->name);
1952 else
1953 bytes += attr->num_values * strlen(attr->name);
1954
1955 /*
1956 * Now add the size required for each value in the attribute...
1957 */
1958
1959 switch (attr->value_tag)
1960 {
1961 case IPP_TAG_INTEGER :
1962 case IPP_TAG_ENUM :
1963 /*
1964 * Minimum value of a signed integer is -2147483647, or 11 digits.
1965 */
1966
1967 bytes += attr->num_values * 11;
1968 break;
1969
1970 case IPP_TAG_BOOLEAN :
1971 /*
1972 * Add two bytes for each false ("no") value...
1973 */
1974
1975 for (i = 0; i < attr->num_values; i ++)
1976 if (!attr->values[i].boolean)
1977 bytes += 2;
1978 break;
1979
1980 case IPP_TAG_RANGE :
1981 /*
1982 * A range is two signed integers separated by a hyphen, or
1983 * 23 characters max.
1984 */
1985
1986 bytes += attr->num_values * 23;
1987 break;
1988
1989 case IPP_TAG_RESOLUTION :
1990 /*
1991 * A resolution is two signed integers separated by an "x" and
1992 * suffixed by the units, or 26 characters max.
1993 */
1994
1995 bytes += attr->num_values * 26;
1996 break;
1997
1998 case IPP_TAG_STRING :
1999 case IPP_TAG_TEXT :
2000 case IPP_TAG_NAME :
2001 case IPP_TAG_KEYWORD :
2002 case IPP_TAG_CHARSET :
2003 case IPP_TAG_LANGUAGE :
2004 case IPP_TAG_URI :
2005 /*
2006 * Strings can contain characters that need quoting. We need
2007 * at least 2 * len + 2 characters to cover the quotes and
2008 * any backslashes in the string.
2009 */
2010
2011 for (i = 0; i < attr->num_values; i ++)
2012 bytes += 2 * strlen(attr->values[i].string.text) + 2;
2013 break;
2014
2015 default :
2016 break; /* anti-compiler-warning-code */
2017 }
2018 }
2019
2020 return (bytes);
2021 }
2022
2023
2024 /*
2025 * 'load_job_cache()' - Load jobs from the job.cache file.
2026 */
2027
2028 static void
2029 load_job_cache(const char *filename) /* I - job.cache filename */
2030 {
2031 cups_file_t *fp; /* job.cache file */
2032 char line[1024], /* Line buffer */
2033 *value; /* Value on line */
2034 int linenum; /* Line number in file */
2035 cupsd_job_t *job; /* Current job */
2036 int jobid; /* Job ID */
2037 char jobfile[1024]; /* Job filename */
2038
2039
2040 /*
2041 * Open the job.cache file...
2042 */
2043
2044 if ((fp = cupsFileOpen(filename, "r")) == NULL)
2045 {
2046 if (errno != ENOENT)
2047 cupsdLogMessage(CUPSD_LOG_ERROR,
2048 "Unable to open job cache file \"%s\": %s",
2049 filename, strerror(errno));
2050
2051 load_request_root();
2052
2053 return;
2054 }
2055
2056 /*
2057 * Read entries from the job cache file and create jobs as needed.
2058 */
2059
2060 cupsdLogMessage(CUPSD_LOG_INFO, "Loading job cache file \"%s\"...",
2061 filename);
2062
2063 linenum = 0;
2064 job = NULL;
2065
2066 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
2067 {
2068 if (!strcasecmp(line, "NextJobId"))
2069 {
2070 if (value)
2071 NextJobId = atoi(value);
2072 }
2073 else if (!strcasecmp(line, "<Job"))
2074 {
2075 if (job)
2076 {
2077 cupsdLogMessage(CUPSD_LOG_ERROR, "Missing </Job> directive on line %d!",
2078 linenum);
2079 continue;
2080 }
2081
2082 if (!value)
2083 {
2084 cupsdLogMessage(CUPSD_LOG_ERROR, "Missing job ID on line %d!", linenum);
2085 continue;
2086 }
2087
2088 jobid = atoi(value);
2089
2090 if (jobid < 1)
2091 {
2092 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad job ID %d on line %d!", jobid,
2093 linenum);
2094 continue;
2095 }
2096
2097 snprintf(jobfile, sizeof(jobfile), "%s/c%05d", RequestRoot, jobid);
2098 if (access(jobfile, 0))
2099 {
2100 cupsdLogMessage(CUPSD_LOG_ERROR, "[Job %d] Files have gone away!",
2101 jobid);
2102 continue;
2103 }
2104
2105 job = calloc(1, sizeof(cupsd_job_t));
2106 if (!job)
2107 {
2108 cupsdLogMessage(CUPSD_LOG_EMERG,
2109 "[Job %d] Unable to allocate memory for job!", jobid);
2110 break;
2111 }
2112
2113 job->id = jobid;
2114 job->back_pipes[0] = -1;
2115 job->back_pipes[1] = -1;
2116 job->print_pipes[0] = -1;
2117 job->print_pipes[1] = -1;
2118 job->side_pipes[0] = -1;
2119 job->side_pipes[1] = -1;
2120 job->status_pipes[0] = -1;
2121 job->status_pipes[1] = -1;
2122
2123 cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] Loading from cache...",
2124 job->id);
2125 }
2126 else if (!job)
2127 {
2128 cupsdLogMessage(CUPSD_LOG_ERROR,
2129 "Missing <Job #> directive on line %d!", linenum);
2130 continue;
2131 }
2132 else if (!strcasecmp(line, "</Job>"))
2133 {
2134 cupsArrayAdd(Jobs, job);
2135
2136 if (job->state_value <= IPP_JOB_STOPPED)
2137 {
2138 cupsArrayAdd(ActiveJobs, job);
2139 cupsdLoadJob(job);
2140 }
2141
2142 job = NULL;
2143 }
2144 else if (!value)
2145 {
2146 cupsdLogMessage(CUPSD_LOG_ERROR, "Missing value on line %d!", linenum);
2147 continue;
2148 }
2149 else if (!strcasecmp(line, "State"))
2150 {
2151 job->state_value = (ipp_jstate_t)atoi(value);
2152
2153 if (job->state_value < IPP_JOB_PENDING)
2154 job->state_value = IPP_JOB_PENDING;
2155 else if (job->state_value > IPP_JOB_COMPLETED)
2156 job->state_value = IPP_JOB_COMPLETED;
2157 }
2158 else if (!strcasecmp(line, "Priority"))
2159 {
2160 job->priority = atoi(value);
2161 }
2162 else if (!strcasecmp(line, "Username"))
2163 {
2164 cupsdSetString(&job->username, value);
2165 }
2166 else if (!strcasecmp(line, "Destination"))
2167 {
2168 cupsdSetString(&job->dest, value);
2169 }
2170 else if (!strcasecmp(line, "DestType"))
2171 {
2172 job->dtype = (cups_ptype_t)atoi(value);
2173 }
2174 else if (!strcasecmp(line, "NumFiles"))
2175 {
2176 job->num_files = atoi(value);
2177
2178 if (job->num_files < 0)
2179 {
2180 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad NumFiles value %d on line %d!",
2181 job->num_files, linenum);
2182 job->num_files = 0;
2183 continue;
2184 }
2185
2186 if (job->num_files > 0)
2187 {
2188 snprintf(jobfile, sizeof(jobfile), "%s/d%05d-001", RequestRoot,
2189 job->id);
2190 if (access(jobfile, 0))
2191 {
2192 cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Data files have gone away!",
2193 job->id);
2194 job->num_files = 0;
2195 continue;
2196 }
2197
2198 job->filetypes = calloc(job->num_files, sizeof(mime_type_t *));
2199 job->compressions = calloc(job->num_files, sizeof(int));
2200
2201 if (!job->filetypes || !job->compressions)
2202 {
2203 cupsdLogMessage(CUPSD_LOG_EMERG,
2204 "[Job %d] Unable to allocate memory for %d files!",
2205 job->id, job->num_files);
2206 break;
2207 }
2208 }
2209 }
2210 else if (!strcasecmp(line, "File"))
2211 {
2212 int number, /* File number */
2213 compression; /* Compression value */
2214 char super[MIME_MAX_SUPER], /* MIME super type */
2215 type[MIME_MAX_TYPE]; /* MIME type */
2216
2217
2218 if (sscanf(value, "%d%*[ \t]%15[^/]/%255s%d", &number, super, type,
2219 &compression) != 4)
2220 {
2221 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad File on line %d!", linenum);
2222 continue;
2223 }
2224
2225 if (number < 1 || number > job->num_files)
2226 {
2227 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad File number %d on line %d!",
2228 number, linenum);
2229 continue;
2230 }
2231
2232 number --;
2233
2234 job->compressions[number] = compression;
2235 job->filetypes[number] = mimeType(MimeDatabase, super, type);
2236
2237 if (!job->filetypes[number])
2238 {
2239 /*
2240 * If the original MIME type is unknown, auto-type it!
2241 */
2242
2243 cupsdLogMessage(CUPSD_LOG_ERROR,
2244 "[Job %d] Unknown MIME type %s/%s for file %d!",
2245 job->id, super, type, number + 1);
2246
2247 snprintf(jobfile, sizeof(jobfile), "%s/d%05d-%03d", RequestRoot,
2248 job->id, number + 1);
2249 job->filetypes[number] = mimeFileType(MimeDatabase, jobfile, NULL,
2250 job->compressions + number);
2251
2252 /*
2253 * If that didn't work, assume it is raw...
2254 */
2255
2256 if (!job->filetypes[number])
2257 job->filetypes[number] = mimeType(MimeDatabase, "application",
2258 "vnd.cups-raw");
2259 }
2260 }
2261 else
2262 cupsdLogMessage(CUPSD_LOG_ERROR, "Unknown %s directive on line %d!",
2263 line, linenum);
2264 }
2265
2266 cupsFileClose(fp);
2267 }
2268
2269
2270 /*
2271 * 'load_next_job_id()' - Load the NextJobId value from the job.cache file.
2272 */
2273
2274 static void
2275 load_next_job_id(const char *filename) /* I - job.cache filename */
2276 {
2277 cups_file_t *fp; /* job.cache file */
2278 char line[1024], /* Line buffer */
2279 *value; /* Value on line */
2280 int linenum; /* Line number in file */
2281 int next_job_id; /* NextJobId value from line */
2282
2283
2284 /*
2285 * Read the NextJobId directive from the job.cache file and use
2286 * the value (if any).
2287 */
2288
2289 if ((fp = cupsFileOpen(filename, "r")) == NULL)
2290 {
2291 if (errno != ENOENT)
2292 cupsdLogMessage(CUPSD_LOG_ERROR,
2293 "Unable to open job cache file \"%s\": %s",
2294 filename, strerror(errno));
2295
2296 return;
2297 }
2298
2299 cupsdLogMessage(CUPSD_LOG_INFO,
2300 "Loading NextJobId from job cache file \"%s\"...", filename);
2301
2302 linenum = 0;
2303
2304 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
2305 {
2306 if (!strcasecmp(line, "NextJobId"))
2307 {
2308 if (value)
2309 {
2310 next_job_id = atoi(value);
2311
2312 if (next_job_id > NextJobId)
2313 NextJobId = next_job_id;
2314 }
2315 break;
2316 }
2317 }
2318
2319 cupsFileClose(fp);
2320 }
2321
2322
2323 /*
2324 * 'load_request_root()' - Load jobs from the RequestRoot directory.
2325 */
2326
2327 static void
2328 load_request_root(void)
2329 {
2330 cups_dir_t *dir; /* Directory */
2331 cups_dentry_t *dent; /* Directory entry */
2332 cupsd_job_t *job; /* New job */
2333
2334
2335 /*
2336 * Open the requests directory...
2337 */
2338
2339 cupsdLogMessage(CUPSD_LOG_DEBUG, "Scanning %s for jobs...", RequestRoot);
2340
2341 if ((dir = cupsDirOpen(RequestRoot)) == NULL)
2342 {
2343 cupsdLogMessage(CUPSD_LOG_ERROR,
2344 "Unable to open spool directory \"%s\": %s",
2345 RequestRoot, strerror(errno));
2346 return;
2347 }
2348
2349 /*
2350 * Read all the c##### files...
2351 */
2352
2353 while ((dent = cupsDirRead(dir)) != NULL)
2354 if (strlen(dent->filename) >= 6 && dent->filename[0] == 'c')
2355 {
2356 /*
2357 * Allocate memory for the job...
2358 */
2359
2360 if ((job = calloc(sizeof(cupsd_job_t), 1)) == NULL)
2361 {
2362 cupsdLogMessage(CUPSD_LOG_ERROR, "Ran out of memory for jobs!");
2363 cupsDirClose(dir);
2364 return;
2365 }
2366
2367 /*
2368 * Assign the job ID...
2369 */
2370
2371 job->id = atoi(dent->filename + 1);
2372 job->back_pipes[0] = -1;
2373 job->back_pipes[1] = -1;
2374 job->print_pipes[0] = -1;
2375 job->print_pipes[1] = -1;
2376 job->side_pipes[0] = -1;
2377 job->side_pipes[1] = -1;
2378 job->status_pipes[0] = -1;
2379 job->status_pipes[1] = -1;
2380
2381 if (job->id >= NextJobId)
2382 NextJobId = job->id + 1;
2383
2384 /*
2385 * Load the job...
2386 */
2387
2388 cupsdLoadJob(job);
2389
2390 /*
2391 * Insert the job into the array, sorting by job priority and ID...
2392 */
2393
2394 cupsArrayAdd(Jobs, job);
2395
2396 if (job->state_value <= IPP_JOB_STOPPED)
2397 cupsArrayAdd(ActiveJobs, job);
2398 else
2399 unload_job(job);
2400 }
2401
2402 cupsDirClose(dir);
2403 }
2404
2405
2406 /*
2407 * 'set_time()' - Set one of the "time-at-xyz" attributes...
2408 */
2409
2410 static void
2411 set_time(cupsd_job_t *job, /* I - Job to update */
2412 const char *name) /* I - Name of attribute */
2413 {
2414 ipp_attribute_t *attr; /* Time attribute */
2415
2416
2417 if ((attr = ippFindAttribute(job->attrs, name, IPP_TAG_ZERO)) != NULL)
2418 {
2419 attr->value_tag = IPP_TAG_INTEGER;
2420 attr->values[0].integer = time(NULL);
2421 }
2422 }
2423
2424
2425 /*
2426 * 'set_hold_until()' - Set the hold time and update job-hold-until attribute...
2427 */
2428
2429 static void
2430 set_hold_until(cupsd_job_t *job, /* I - Job to update */
2431 time_t holdtime) /* I - Hold until time */
2432 {
2433 ipp_attribute_t *attr; /* job-hold-until attribute */
2434 struct tm *holddate; /* Hold date */
2435 char holdstr[64]; /* Hold time */
2436
2437
2438 /*
2439 * Set the hold_until value and hold the job...
2440 */
2441
2442 cupsdLogMessage(CUPSD_LOG_DEBUG, "set_hold_until: hold_until = %d",
2443 (int)holdtime);
2444
2445 job->state->values[0].integer = IPP_JOB_HELD;
2446 job->state_value = IPP_JOB_HELD;
2447 job->hold_until = holdtime;
2448
2449 /*
2450 * Update the job-hold-until attribute with a string representing GMT
2451 * time (HH:MM:SS)...
2452 */
2453
2454 holddate = gmtime(&holdtime);
2455 snprintf(holdstr, sizeof(holdstr), "%d:%d:%d", holddate->tm_hour,
2456 holddate->tm_min, holddate->tm_sec);
2457
2458 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
2459 IPP_TAG_KEYWORD)) == NULL)
2460 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
2461
2462 /*
2463 * Either add the attribute or update the value of the existing one
2464 */
2465
2466 if (attr == NULL)
2467 ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_KEYWORD, "job-hold-until",
2468 NULL, holdstr);
2469 else
2470 cupsdSetString(&attr->values[0].string.text, holdstr);
2471
2472 job->dirty = 1;
2473 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
2474 }
2475
2476
2477 /*
2478 * 'start_job()' - Start a print job.
2479 */
2480
2481 static void
2482 start_job(cupsd_job_t *job, /* I - Job ID */
2483 cupsd_printer_t *printer) /* I - Printer to print job */
2484 {
2485 int i; /* Looping var */
2486 int slot; /* Pipe slot */
2487 cups_array_t *filters, /* Filters for job */
2488 *prefilters; /* Filters with prefilters */
2489 mime_filter_t *filter, /* Current filter */
2490 *prefilter, /* Prefilter */
2491 port_monitor; /* Port monitor filter */
2492 char method[255], /* Method for output */
2493 *optptr, /* Pointer to options */
2494 *valptr; /* Pointer in value string */
2495 ipp_attribute_t *attr; /* Current attribute */
2496 struct stat backinfo; /* Backend file information */
2497 int backroot; /* Run backend as root? */
2498 int pid; /* Process ID of new filter process */
2499 int banner_page; /* 1 if banner page, 0 otherwise */
2500 int filterfds[2][2];/* Pipes used between filters */
2501 int envc; /* Number of environment variables */
2502 char **argv, /* Filter command-line arguments */
2503 filename[1024], /* Job filename */
2504 command[1024], /* Full path to command */
2505 jobid[255], /* Job ID string */
2506 title[IPP_MAX_NAME],
2507 /* Job title string */
2508 copies[255], /* # copies string */
2509 *envp[MAX_ENV + 19],
2510 /* Environment variables */
2511 charset[255], /* CHARSET env variable */
2512 class_name[255],/* CLASS env variable */
2513 classification[1024],
2514 /* CLASSIFICATION env variable */
2515 content_type[1024],
2516 /* CONTENT_TYPE env variable */
2517 device_uri[1024],
2518 /* DEVICE_URI env variable */
2519 final_content_type[1024],
2520 /* FINAL_CONTENT_TYPE env variable */
2521 lang[255], /* LANG env variable */
2522 #ifdef __APPLE__
2523 apple_language[255],
2524 /* APPLE_LANGUAGE env variable */
2525 #endif /* __APPLE__ */
2526 ppd[1024], /* PPD env variable */
2527 printer_info[255],
2528 /* PRINTER_INFO env variable */
2529 printer_location[255],
2530 /* PRINTER_LOCATION env variable */
2531 printer_name[255],
2532 /* PRINTER env variable */
2533 rip_max_cache[255];
2534 /* RIP_MAX_CACHE env variable */
2535 static char *options = NULL;/* Full list of options */
2536 static int optlength = 0; /* Length of option buffer */
2537
2538
2539 cupsdLogJob(job, CUPSD_LOG_DEBUG2, "start_job: file = %d/%d",
2540 job->current_file, job->num_files);
2541
2542 if (job->num_files == 0)
2543 {
2544 cupsdLogJob(job, CUPSD_LOG_ERROR, "No files, canceling job!");
2545 cupsdCancelJob(job, 0, IPP_JOB_ABORTED);
2546 return;
2547 }
2548
2549 /*
2550 * Figure out what filters are required to convert from
2551 * the source to the destination type...
2552 */
2553
2554 filters = NULL;
2555 job->cost = 0;
2556
2557 if (printer->raw)
2558 {
2559 /*
2560 * Remote jobs and raw queues go directly to the printer without
2561 * filtering...
2562 */
2563
2564 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Sending job to queue tagged as raw...");
2565
2566 filters = NULL;
2567 }
2568 else
2569 {
2570 /*
2571 * Local jobs get filtered...
2572 */
2573
2574 filters = mimeFilter(MimeDatabase, job->filetypes[job->current_file],
2575 printer->filetype, &(job->cost));
2576
2577 if (!filters)
2578 {
2579 cupsdLogJob(job, CUPSD_LOG_ERROR,
2580 "Unable to convert file %d to printable format!",
2581 job->current_file);
2582 cupsdLogMessage(CUPSD_LOG_INFO,
2583 "Hint: Do you have Ghostscript installed?");
2584
2585 if (LogLevel < CUPSD_LOG_DEBUG)
2586 cupsdLogMessage(CUPSD_LOG_INFO,
2587 "Hint: Try setting the LogLevel to \"debug\".");
2588
2589 job->current_file ++;
2590
2591 if (job->current_file == job->num_files)
2592 cupsdCancelJob(job, 0, IPP_JOB_ABORTED);
2593
2594 return;
2595 }
2596
2597 /*
2598 * Remove NULL ("-") filters...
2599 */
2600
2601 for (filter = (mime_filter_t *)cupsArrayFirst(filters);
2602 filter;
2603 filter = (mime_filter_t *)cupsArrayNext(filters))
2604 if (!strcmp(filter->filter, "-"))
2605 cupsArrayRemove(filters, filter);
2606
2607 if (cupsArrayCount(filters) == 0)
2608 {
2609 cupsArrayDelete(filters);
2610 filters = NULL;
2611 }
2612
2613 /*
2614 * If this printer has any pre-filters, insert the required pre-filter
2615 * in the filters array...
2616 */
2617
2618 if (printer->prefiltertype && filters)
2619 {
2620 prefilters = cupsArrayNew(NULL, NULL);
2621
2622 for (filter = (mime_filter_t *)cupsArrayFirst(filters);
2623 filter;
2624 filter = (mime_filter_t *)cupsArrayNext(filters))
2625 {
2626 if ((prefilter = mimeFilterLookup(MimeDatabase, filter->src,
2627 printer->prefiltertype)))
2628 {
2629 cupsArrayAdd(prefilters, prefilter);
2630 job->cost += prefilter->cost;
2631 }
2632
2633 cupsArrayAdd(prefilters, filter);
2634 }
2635
2636 cupsArrayDelete(filters);
2637 filters = prefilters;
2638 }
2639 }
2640
2641 /*
2642 * Set a minimum cost of 100 for all jobs so that FilterLimit
2643 * works with raw queues and other low-cost paths.
2644 */
2645
2646 if (job->cost < 100)
2647 job->cost = 100;
2648
2649 /*
2650 * See if the filter cost is too high...
2651 */
2652
2653 if ((FilterLevel + job->cost) > FilterLimit && FilterLevel > 0 &&
2654 FilterLimit > 0)
2655 {
2656 /*
2657 * Don't print this job quite yet...
2658 */
2659
2660 cupsArrayDelete(filters);
2661
2662 cupsdLogJob(job, CUPSD_LOG_INFO,
2663 "Holding because filter limit has been reached.");
2664 cupsdLogJob(job, CUPSD_LOG_DEBUG2,
2665 "start_job: file=%d, cost=%d, level=%d, limit=%d",
2666 job->current_file, job->cost, FilterLevel,
2667 FilterLimit);
2668 return;
2669 }
2670
2671 FilterLevel += job->cost;
2672
2673 /*
2674 * Add decompression/raw filter as needed...
2675 */
2676
2677 if ((!printer->raw && job->compressions[job->current_file]) ||
2678 (!filters && !printer->remote &&
2679 (job->num_files > 1 || !strncmp(printer->device_uri, "file:", 5))))
2680 {
2681 /*
2682 * Add gziptoany filter to the front of the list...
2683 */
2684
2685 if (!filters)
2686 filters = cupsArrayNew(NULL, NULL);
2687
2688 if (!cupsArrayInsert(filters, &gziptoany_filter))
2689 {
2690 cupsdLogJob(job, CUPSD_LOG_ERROR,
2691 "Unable to add decompression filter - %s", strerror(errno));
2692
2693 cupsArrayDelete(filters);
2694
2695 job->current_file ++;
2696
2697 if (job->current_file == job->num_files)
2698 cupsdCancelJob(job, 0, IPP_JOB_ABORTED);
2699
2700 return;
2701 }
2702 }
2703
2704 /*
2705 * Add port monitor, if any...
2706 */
2707
2708 if (printer->port_monitor)
2709 {
2710 /*
2711 * Add port monitor to the end of the list...
2712 */
2713
2714 if (!filters)
2715 filters = cupsArrayNew(NULL, NULL);
2716
2717 port_monitor.src = NULL;
2718 port_monitor.dst = NULL;
2719 port_monitor.cost = 0;
2720
2721 snprintf(port_monitor.filter, sizeof(port_monitor.filter),
2722 "%s/monitor/%s", ServerBin, printer->port_monitor);
2723
2724 if (!cupsArrayAdd(filters, &port_monitor))
2725 {
2726 cupsdLogJob(job, CUPSD_LOG_ERROR,
2727 "Unable to add port monitor - %s", strerror(errno));
2728
2729 cupsArrayDelete(filters);
2730
2731 job->current_file ++;
2732
2733 if (job->current_file == job->num_files)
2734 cupsdCancelJob(job, 0, IPP_JOB_ABORTED);
2735
2736 return;
2737 }
2738 }
2739
2740 /*
2741 * Make sure we don't go over the "MAX_FILTERS" limit...
2742 */
2743
2744 if (cupsArrayCount(filters) > MAX_FILTERS)
2745 {
2746 cupsdLogJob(job, CUPSD_LOG_ERROR,
2747 "Too many filters (%d > %d), unable to print!",
2748 cupsArrayCount(filters), MAX_FILTERS);
2749
2750 cupsArrayDelete(filters);
2751 cupsdCancelJob(job, 0, IPP_JOB_STOPPED);
2752
2753 return;
2754 }
2755
2756 /*
2757 * Update the printer and job state to "processing"...
2758 */
2759
2760 job->state->values[0].integer = IPP_JOB_PROCESSING;
2761 job->state_value = IPP_JOB_PROCESSING;
2762 job->progress = 0;
2763 job->status = 0;
2764 job->printer = printer;
2765 printer->job = job;
2766
2767 cupsdSetPrinterState(printer, IPP_PRINTER_PROCESSING, 0);
2768
2769 if (job->current_file == 0)
2770 {
2771 /*
2772 * Add to the printing list...
2773 */
2774
2775 cupsArrayAdd(PrintingJobs, job);
2776 cupsdSetBusyState();
2777
2778 /*
2779 * Set the processing time...
2780 */
2781
2782 set_time(job, "time-at-processing");
2783
2784 /*
2785 * Create the backchannel pipes and make them non-blocking...
2786 */
2787
2788 cupsdOpenPipe(job->back_pipes);
2789
2790 fcntl(job->back_pipes[0], F_SETFL,
2791 fcntl(job->back_pipes[0], F_GETFL) | O_NONBLOCK);
2792
2793 fcntl(job->back_pipes[1], F_SETFL,
2794 fcntl(job->back_pipes[1], F_GETFL) | O_NONBLOCK);
2795
2796 /*
2797 * Create the side-channel pipes and make them non-blocking...
2798 */
2799
2800 socketpair(AF_LOCAL, SOCK_STREAM, 0, job->side_pipes);
2801
2802 fcntl(job->side_pipes[0], F_SETFL,
2803 fcntl(job->side_pipes[0], F_GETFL) | O_NONBLOCK);
2804
2805 fcntl(job->side_pipes[1], F_SETFL,
2806 fcntl(job->side_pipes[1], F_GETFL) | O_NONBLOCK);
2807 }
2808
2809 /*
2810 * Determine if we are printing a banner page or not...
2811 */
2812
2813 if (job->job_sheets == NULL)
2814 {
2815 cupsdLogJob(job, CUPSD_LOG_DEBUG, "No job-sheets attribute.");
2816 if ((job->job_sheets =
2817 ippFindAttribute(job->attrs, "job-sheets", IPP_TAG_ZERO)) != NULL)
2818 cupsdLogJob(job, CUPSD_LOG_DEBUG,
2819 "... but someone added one without setting job_sheets!");
2820 }
2821 else if (job->job_sheets->num_values == 1)
2822 cupsdLogJob(job, CUPSD_LOG_DEBUG, "job-sheets=%s",
2823 job->job_sheets->values[0].string.text);
2824 else
2825 cupsdLogJob(job, CUPSD_LOG_DEBUG, "job-sheets=%s,%s",
2826 job->job_sheets->values[0].string.text,
2827 job->job_sheets->values[1].string.text);
2828
2829 if (printer->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT))
2830 banner_page = 0;
2831 else if (job->job_sheets == NULL)
2832 banner_page = 0;
2833 else if (strcasecmp(job->job_sheets->values[0].string.text, "none") != 0 &&
2834 job->current_file == 0)
2835 banner_page = 1;
2836 else if (job->job_sheets->num_values > 1 &&
2837 strcasecmp(job->job_sheets->values[1].string.text, "none") != 0 &&
2838 job->current_file == (job->num_files - 1))
2839 banner_page = 1;
2840 else
2841 banner_page = 0;
2842
2843 cupsdLogJob(job, CUPSD_LOG_DEBUG, "banner_page = %d", banner_page);
2844
2845 /*
2846 * Building the options string is harder than it needs to be, but
2847 * for the moment we need to pass strings for command-line args and
2848 * not IPP attribute pointers... :)
2849 *
2850 * First allocate/reallocate the option buffer as needed...
2851 */
2852
2853 i = ipp_length(job->attrs);
2854
2855 if (i > optlength || !options)
2856 {
2857 if (optlength == 0)
2858 optptr = malloc(i);
2859 else
2860 optptr = realloc(options, i);
2861
2862 if (optptr == NULL)
2863 {
2864 cupsdLogJob(job, CUPSD_LOG_CRIT,
2865 "Unable to allocate %d bytes for option buffer!", i);
2866
2867 cupsArrayDelete(filters);
2868
2869 FilterLevel -= job->cost;
2870
2871 cupsdCancelJob(job, 0, IPP_JOB_ABORTED);
2872 return;
2873 }
2874
2875 options = optptr;
2876 optlength = i;
2877 }
2878
2879 /*
2880 * Now loop through the attributes and convert them to the textual
2881 * representation used by the filters...
2882 */
2883
2884 optptr = options;
2885 *optptr = '\0';
2886
2887 snprintf(title, sizeof(title), "%s-%d", printer->name, job->id);
2888 strcpy(copies, "1");
2889
2890 for (attr = job->attrs->attrs; attr != NULL; attr = attr->next)
2891 {
2892 if (!strcmp(attr->name, "copies") &&
2893 attr->value_tag == IPP_TAG_INTEGER)
2894 {
2895 /*
2896 * Don't use the # copies attribute if we are printing the job sheets...
2897 */
2898
2899 if (!banner_page)
2900 sprintf(copies, "%d", attr->values[0].integer);
2901 }
2902 else if (!strcmp(attr->name, "job-name") &&
2903 (attr->value_tag == IPP_TAG_NAME ||
2904 attr->value_tag == IPP_TAG_NAMELANG))
2905 strlcpy(title, attr->values[0].string.text, sizeof(title));
2906 else if (attr->group_tag == IPP_TAG_JOB)
2907 {
2908 /*
2909 * Filter out other unwanted attributes...
2910 */
2911
2912 if (attr->value_tag == IPP_TAG_MIMETYPE ||
2913 attr->value_tag == IPP_TAG_NAMELANG ||
2914 attr->value_tag == IPP_TAG_TEXTLANG ||
2915 (attr->value_tag == IPP_TAG_URI && strcmp(attr->name, "job-uuid")) ||
2916 attr->value_tag == IPP_TAG_URISCHEME ||
2917 attr->value_tag == IPP_TAG_BEGIN_COLLECTION) /* Not yet supported */
2918 continue;
2919
2920 if (!strncmp(attr->name, "time-", 5))
2921 continue;
2922
2923 if (!strncmp(attr->name, "job-", 4) &&
2924 strcmp(attr->name, "job-billing") &&
2925 strcmp(attr->name, "job-impressions") &&
2926 strcmp(attr->name, "job-originating-host-name") &&
2927 strcmp(attr->name, "job-uuid") &&
2928 !(printer->type & CUPS_PRINTER_REMOTE))
2929 continue;
2930
2931 if ((!strcmp(attr->name, "job-impressions") ||
2932 !strcmp(attr->name, "page-label") ||
2933 !strcmp(attr->name, "page-border") ||
2934 !strncmp(attr->name, "number-up", 9) ||
2935 !strcmp(attr->name, "page-ranges") ||
2936 !strcmp(attr->name, "page-set") ||
2937 !strcasecmp(attr->name, "AP_FIRSTPAGE_InputSlot") ||
2938 !strcasecmp(attr->name, "AP_FIRSTPAGE_ManualFeed") ||
2939 !strcasecmp(attr->name, "com.apple.print.PrintSettings."
2940 "PMTotalSidesImaged..n.") ||
2941 !strcasecmp(attr->name, "com.apple.print.PrintSettings."
2942 "PMTotalBeginPages..n.")) &&
2943 banner_page)
2944 continue;
2945
2946 /*
2947 * Otherwise add them to the list...
2948 */
2949
2950 if (optptr > options)
2951 strlcat(optptr, " ", optlength - (optptr - options));
2952
2953 if (attr->value_tag != IPP_TAG_BOOLEAN)
2954 {
2955 strlcat(optptr, attr->name, optlength - (optptr - options));
2956 strlcat(optptr, "=", optlength - (optptr - options));
2957 }
2958
2959 for (i = 0; i < attr->num_values; i ++)
2960 {
2961 if (i)
2962 strlcat(optptr, ",", optlength - (optptr - options));
2963
2964 optptr += strlen(optptr);
2965
2966 switch (attr->value_tag)
2967 {
2968 case IPP_TAG_INTEGER :
2969 case IPP_TAG_ENUM :
2970 snprintf(optptr, optlength - (optptr - options),
2971 "%d", attr->values[i].integer);
2972 break;
2973
2974 case IPP_TAG_BOOLEAN :
2975 if (!attr->values[i].boolean)
2976 strlcat(optptr, "no", optlength - (optptr - options));
2977
2978 case IPP_TAG_NOVALUE :
2979 strlcat(optptr, attr->name,
2980 optlength - (optptr - options));
2981 break;
2982
2983 case IPP_TAG_RANGE :
2984 if (attr->values[i].range.lower == attr->values[i].range.upper)
2985 snprintf(optptr, optlength - (optptr - options) - 1,
2986 "%d", attr->values[i].range.lower);
2987 else
2988 snprintf(optptr, optlength - (optptr - options) - 1,
2989 "%d-%d", attr->values[i].range.lower,
2990 attr->values[i].range.upper);
2991 break;
2992
2993 case IPP_TAG_RESOLUTION :
2994 snprintf(optptr, optlength - (optptr - options) - 1,
2995 "%dx%d%s", attr->values[i].resolution.xres,
2996 attr->values[i].resolution.yres,
2997 attr->values[i].resolution.units == IPP_RES_PER_INCH ?
2998 "dpi" : "dpc");
2999 break;
3000
3001 case IPP_TAG_STRING :
3002 case IPP_TAG_TEXT :
3003 case IPP_TAG_NAME :
3004 case IPP_TAG_KEYWORD :
3005 case IPP_TAG_CHARSET :
3006 case IPP_TAG_LANGUAGE :
3007 case IPP_TAG_URI :
3008 for (valptr = attr->values[i].string.text; *valptr;)
3009 {
3010 if (strchr(" \t\n\\\'\"", *valptr))
3011 *optptr++ = '\\';
3012 *optptr++ = *valptr++;
3013 }
3014
3015 *optptr = '\0';
3016 break;
3017
3018 default :
3019 break; /* anti-compiler-warning-code */
3020 }
3021 }
3022
3023 optptr += strlen(optptr);
3024 }
3025 }
3026
3027 /*
3028 * Build the command-line arguments for the filters. Each filter
3029 * has 6 or 7 arguments:
3030 *
3031 * argv[0] = printer
3032 * argv[1] = job ID
3033 * argv[2] = username
3034 * argv[3] = title
3035 * argv[4] = # copies
3036 * argv[5] = options
3037 * argv[6] = filename (optional; normally stdin)
3038 *
3039 * This allows legacy printer drivers that use the old System V
3040 * printing interface to be used by CUPS.
3041 *
3042 * For remote jobs, we send all of the files in the argument list.
3043 */
3044
3045 if (printer->remote && job->num_files > 1)
3046 argv = calloc(7 + job->num_files, sizeof(char *));
3047 else
3048 argv = calloc(8, sizeof(char *));
3049
3050 if (!argv)
3051 {
3052 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to allocate argument array!");
3053 cupsArrayDelete(filters);
3054
3055 FilterLevel -= job->cost;
3056
3057 cupsdStopPrinter(printer, 0);
3058 return;
3059 }
3060
3061 sprintf(jobid, "%d", job->id);
3062
3063 argv[0] = printer->name;
3064 argv[1] = jobid;
3065 argv[2] = job->username;
3066 argv[3] = title;
3067 argv[4] = copies;
3068 argv[5] = options;
3069
3070 if (printer->remote && job->num_files > 1)
3071 {
3072 for (i = 0; i < job->num_files; i ++)
3073 {
3074 snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot,
3075 job->id, i + 1);
3076 argv[6 + i] = strdup(filename);
3077 }
3078 }
3079 else
3080 {
3081 snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot,
3082 job->id, job->current_file + 1);
3083 argv[6] = filename;
3084 }
3085
3086 for (i = 0; argv[i]; i ++)
3087 cupsdLogJob(job, CUPSD_LOG_DEBUG,
3088 "argv[%d]=\"%s\"", i, argv[i]);
3089
3090 /*
3091 * Create environment variable strings for the filters...
3092 */
3093
3094 attr = ippFindAttribute(job->attrs, "attributes-natural-language",
3095 IPP_TAG_LANGUAGE);
3096
3097 #ifdef __APPLE__
3098 strcpy(apple_language, "APPLE_LANGUAGE=");
3099 _cupsAppleLanguage(attr->values[0].string.text,
3100 apple_language + 15, sizeof(apple_language) - 15);
3101 #endif /* __APPLE__ */
3102
3103 switch (strlen(attr->values[0].string.text))
3104 {
3105 default :
3106 /*
3107 * This is an unknown or badly formatted language code; use
3108 * the POSIX locale...
3109 */
3110
3111 strcpy(lang, "LANG=C");
3112 break;
3113
3114 case 2 :
3115 /*
3116 * Just the language code (ll)...
3117 */
3118
3119 snprintf(lang, sizeof(lang), "LANG=%s.UTF8",
3120 attr->values[0].string.text);
3121 break;
3122
3123 case 5 :
3124 /*
3125 * Language and country code (ll-cc)...
3126 */
3127
3128 snprintf(lang, sizeof(lang), "LANG=%c%c_%c%c.UTF8",
3129 attr->values[0].string.text[0],
3130 attr->values[0].string.text[1],
3131 toupper(attr->values[0].string.text[3] & 255),
3132 toupper(attr->values[0].string.text[4] & 255));
3133 break;
3134 }
3135
3136 attr = ippFindAttribute(job->attrs, "document-format",
3137 IPP_TAG_MIMETYPE);
3138 if (attr != NULL &&
3139 (optptr = strstr(attr->values[0].string.text, "charset=")) != NULL)
3140 snprintf(charset, sizeof(charset), "CHARSET=%s", optptr + 8);
3141 else
3142 {
3143 attr = ippFindAttribute(job->attrs, "attributes-charset",
3144 IPP_TAG_CHARSET);
3145 snprintf(charset, sizeof(charset), "CHARSET=%s",
3146 attr->values[0].string.text);
3147 }
3148
3149 snprintf(content_type, sizeof(content_type), "CONTENT_TYPE=%s/%s",
3150 job->filetypes[job->current_file]->super,
3151 job->filetypes[job->current_file]->type);
3152 snprintf(device_uri, sizeof(device_uri), "DEVICE_URI=%s",
3153 printer->device_uri);
3154 snprintf(ppd, sizeof(ppd), "PPD=%s/ppd/%s.ppd", ServerRoot, printer->name);
3155 snprintf(printer_info, sizeof(printer_name), "PRINTER_INFO=%s",
3156 printer->info ? printer->info : "");
3157 snprintf(printer_location, sizeof(printer_name), "PRINTER_LOCATION=%s",
3158 printer->location ? printer->location : "");
3159 snprintf(printer_name, sizeof(printer_name), "PRINTER=%s", printer->name);
3160 snprintf(rip_max_cache, sizeof(rip_max_cache), "RIP_MAX_CACHE=%s", RIPCache);
3161
3162 envc = cupsdLoadEnv(envp, (int)(sizeof(envp) / sizeof(envp[0])));
3163
3164 envp[envc ++] = charset;
3165 envp[envc ++] = lang;
3166 #ifdef __APPLE__
3167 envp[envc ++] = apple_language;
3168 #endif /* __APPLE__ */
3169 envp[envc ++] = ppd;
3170 envp[envc ++] = rip_max_cache;
3171 envp[envc ++] = content_type;
3172 envp[envc ++] = device_uri;
3173 envp[envc ++] = printer_info;
3174 envp[envc ++] = printer_location;
3175 envp[envc ++] = printer_name;
3176 envp[envc ++] = banner_page ? "CUPS_FILETYPE=job-sheet" :
3177 "CUPS_FILETYPE=document";
3178
3179 if (!printer->remote && !printer->raw)
3180 {
3181 filter = (mime_filter_t *)cupsArrayLast(filters);
3182
3183 if (printer->port_monitor)
3184 filter = (mime_filter_t *)cupsArrayPrev(filters);
3185
3186 if (filter && filter->dst)
3187 {
3188 snprintf(final_content_type, sizeof(final_content_type),
3189 "FINAL_CONTENT_TYPE=%s/%s",
3190 filter->dst->super, filter->dst->type);
3191 envp[envc ++] = final_content_type;
3192 }
3193 }
3194
3195 if (Classification && !banner_page)
3196 {
3197 if ((attr = ippFindAttribute(job->attrs, "job-sheets",
3198 IPP_TAG_NAME)) == NULL)
3199 snprintf(classification, sizeof(classification), "CLASSIFICATION=%s",
3200 Classification);
3201 else if (attr->num_values > 1 &&
3202 strcmp(attr->values[1].string.text, "none") != 0)
3203 snprintf(classification, sizeof(classification), "CLASSIFICATION=%s",
3204 attr->values[1].string.text);
3205 else
3206 snprintf(classification, sizeof(classification), "CLASSIFICATION=%s",
3207 attr->values[0].string.text);
3208
3209 envp[envc ++] = classification;
3210 }
3211
3212 if (job->dtype & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT))
3213 {
3214 snprintf(class_name, sizeof(class_name), "CLASS=%s", job->dest);
3215 envp[envc ++] = class_name;
3216 }
3217
3218 if (job->auth_username)
3219 envp[envc ++] = job->auth_username;
3220 if (job->auth_domain)
3221 envp[envc ++] = job->auth_domain;
3222 if (job->auth_password)
3223 envp[envc ++] = job->auth_password;
3224
3225 #ifdef HAVE_GSSAPI
3226 if (job->ccname)
3227 envp[envc ++] = job->ccname;
3228 #endif /* HAVE_GSSAPI */
3229
3230 envp[envc] = NULL;
3231
3232 for (i = 0; i < envc; i ++)
3233 if (!strncmp(envp[i], "AUTH_", 5))
3234 cupsdLogJob(job, CUPSD_LOG_DEBUG, "envp[%d]=\"AUTH_%c****\"", i,
3235 envp[i][5]);
3236 else if (strncmp(envp[i], "DEVICE_URI=", 11))
3237 cupsdLogJob(job, CUPSD_LOG_DEBUG, "envp[%d]=\"%s\"", i, envp[i]);
3238 else
3239 cupsdLogJob(job, CUPSD_LOG_DEBUG, "envp[%d]=\"DEVICE_URI=%s\"", i,
3240 printer->sanitized_device_uri);
3241
3242 if (printer->remote)
3243 job->current_file = job->num_files;
3244 else
3245 job->current_file ++;
3246
3247 /*
3248 * Now create processes for all of the filters...
3249 */
3250
3251 filterfds[0][0] = -1;
3252 filterfds[0][1] = -1;
3253 filterfds[1][0] = -1;
3254 filterfds[1][1] = -1;
3255
3256 if (!job->status_buffer)
3257 {
3258 if (cupsdOpenPipe(job->status_pipes))
3259 {
3260 cupsdLogJob(job, CUPSD_LOG_ERROR,
3261 "Unable to create job status pipes - %s.", strerror(errno));
3262 snprintf(printer->state_message, sizeof(printer->state_message),
3263 "Unable to create status pipes - %s.", strerror(errno));
3264
3265 cupsdAddPrinterHistory(printer);
3266
3267 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
3268 "Job canceled because the server could not create the job "
3269 "status pipes.");
3270
3271 goto abort_job;
3272 }
3273
3274 cupsdLogJob(job, CUPSD_LOG_DEBUG2,
3275 "start_job: status_pipes = [ %d %d ]",
3276 job->status_pipes[0], job->status_pipes[1]);
3277
3278 job->status_buffer = cupsdStatBufNew(job->status_pipes[0], NULL);
3279 job->status_level = CUPSD_LOG_INFO;
3280 }
3281
3282 job->status = 0;
3283 memset(job->filters, 0, sizeof(job->filters));
3284
3285 if (!job->profile)
3286 job->profile = cupsdCreateProfile(job->id);
3287
3288 for (i = 0, slot = 0, filter = (mime_filter_t *)cupsArrayFirst(filters);
3289 filter;
3290 i ++, filter = (mime_filter_t *)cupsArrayNext(filters))
3291 {
3292 if (filter->filter[0] != '/')
3293 snprintf(command, sizeof(command), "%s/filter/%s", ServerBin,
3294 filter->filter);
3295 else
3296 strlcpy(command, filter->filter, sizeof(command));
3297
3298 if (i < (cupsArrayCount(filters) - 1))
3299 {
3300 if (cupsdOpenPipe(filterfds[slot]))
3301 {
3302 cupsdLogJob(job, CUPSD_LOG_ERROR,
3303 "Unable to create job filter pipes - %s.", strerror(errno));
3304 snprintf(printer->state_message, sizeof(printer->state_message),
3305 "Unable to create filter pipes - %s.", strerror(errno));
3306 cupsdAddPrinterHistory(printer);
3307
3308 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
3309 "Job canceled because the server could not create the "
3310 "filter pipes.");
3311
3312 goto abort_job;
3313 }
3314 }
3315 else
3316 {
3317 if (job->current_file == 1)
3318 {
3319 if (strncmp(printer->device_uri, "file:", 5) != 0)
3320 {
3321 if (cupsdOpenPipe(job->print_pipes))
3322 {
3323 cupsdLogJob(job, CUPSD_LOG_ERROR,
3324 "Unable to create job backend pipes - %s.",
3325 strerror(errno));
3326 snprintf(printer->state_message, sizeof(printer->state_message),
3327 "Unable to create backend pipes - %s.", strerror(errno));
3328 cupsdAddPrinterHistory(printer);
3329
3330 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
3331 "Job canceled because the server could not create "
3332 "the backend pipes.");
3333
3334 goto abort_job;
3335 }
3336 }
3337 else
3338 {
3339 job->print_pipes[0] = -1;
3340 if (!strcmp(printer->device_uri, "file:/dev/null") ||
3341 !strcmp(printer->device_uri, "file:///dev/null"))
3342 job->print_pipes[1] = -1;
3343 else
3344 {
3345 if (!strncmp(printer->device_uri, "file:/dev/", 10))
3346 job->print_pipes[1] = open(printer->device_uri + 5,
3347 O_WRONLY | O_EXCL);
3348 else if (!strncmp(printer->device_uri, "file:///dev/", 12))
3349 job->print_pipes[1] = open(printer->device_uri + 7,
3350 O_WRONLY | O_EXCL);
3351 else if (!strncmp(printer->device_uri, "file:///", 8))
3352 job->print_pipes[1] = open(printer->device_uri + 7,
3353 O_WRONLY | O_CREAT | O_TRUNC, 0600);
3354 else
3355 job->print_pipes[1] = open(printer->device_uri + 5,
3356 O_WRONLY | O_CREAT | O_TRUNC, 0600);
3357
3358 if (job->print_pipes[1] < 0)
3359 {
3360 cupsdLogJob(job, CUPSD_LOG_ERROR,
3361 "Unable to open output file \"%s\" - %s.",
3362 printer->device_uri, strerror(errno));
3363 snprintf(printer->state_message, sizeof(printer->state_message),
3364 "Unable to open output file \"%s\" - %s.",
3365 printer->device_uri, strerror(errno));
3366
3367 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
3368 "Job canceled because the server could not open the "
3369 "output file.");
3370
3371 goto abort_job;
3372 }
3373
3374 fcntl(job->print_pipes[1], F_SETFD,
3375 fcntl(job->print_pipes[1], F_GETFD) | FD_CLOEXEC);
3376 }
3377 }
3378
3379 cupsdLogJob(job, CUPSD_LOG_DEBUG2,
3380 "start_job: print_pipes = [ %d %d ]",
3381 job->print_pipes[0], job->print_pipes[1]);
3382 }
3383
3384 filterfds[slot][0] = job->print_pipes[0];
3385 filterfds[slot][1] = job->print_pipes[1];
3386 }
3387
3388 cupsdLogJob(job, CUPSD_LOG_DEBUG2, "start_job: filter=\"%s\"", command);
3389 cupsdLogJob(job, CUPSD_LOG_DEBUG2, "start_job: filterfds[%d]=[ %d %d ]",
3390 slot, filterfds[slot][0], filterfds[slot][1]);
3391
3392 pid = cupsdStartProcess(command, argv, envp, filterfds[!slot][0],
3393 filterfds[slot][1], job->status_pipes[1],
3394 job->back_pipes[0], job->side_pipes[0], 0,
3395 job->profile, job->filters + i);
3396
3397 cupsdLogJob(job, CUPSD_LOG_DEBUG2,
3398 "start_job: Closing filter pipes for slot %d [ %d %d ]...",
3399 !slot, filterfds[!slot][0], filterfds[!slot][1]);
3400
3401 cupsdClosePipe(filterfds[!slot]);
3402
3403 if (pid == 0)
3404 {
3405 cupsdLogJob(job, CUPSD_LOG_ERROR, "Unable to start filter \"%s\" - %s.",
3406 filter->filter, strerror(errno));
3407 snprintf(printer->state_message, sizeof(printer->state_message),
3408 "Unable to start filter \"%s\" - %s.",
3409 filter->filter, strerror(errno));
3410
3411 cupsdAddPrinterHistory(printer);
3412
3413 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
3414 "Job canceled because the server could not execute a "
3415 "filter.");
3416
3417 goto abort_job;
3418 }
3419
3420 cupsdLogJob(job, CUPSD_LOG_INFO, "Started filter %s (PID %d)", command,
3421 pid);
3422
3423 argv[6] = NULL;
3424 slot = !slot;
3425 }
3426
3427 cupsArrayDelete(filters);
3428 filters = NULL;
3429
3430 /*
3431 * Finally, pipe the final output into a backend process if needed...
3432 */
3433
3434 if (strncmp(printer->device_uri, "file:", 5) != 0)
3435 {
3436 if (job->current_file == 1 || printer->remote)
3437 {
3438 sscanf(printer->device_uri, "%254[^:]", method);
3439 snprintf(command, sizeof(command), "%s/backend/%s", ServerBin, method);
3440
3441 /*
3442 * See if the backend needs to run as root...
3443 */
3444
3445 if (RunUser)
3446 backroot = 0;
3447 else if (stat(command, &backinfo))
3448 backroot = 0;
3449 else
3450 backroot = !(backinfo.st_mode & (S_IRWXG | S_IRWXO));
3451
3452 argv[0] = printer->sanitized_device_uri;
3453
3454 filterfds[slot][0] = -1;
3455 filterfds[slot][1] = -1;
3456
3457 cupsdLogJob(job, CUPSD_LOG_DEBUG2, "start_job: backend=\"%s\"", command);
3458 cupsdLogJob(job, CUPSD_LOG_DEBUG2, "start_job: filterfds[%d] = [ %d %d ]",
3459 slot, filterfds[slot][0], filterfds[slot][1]);
3460
3461 pid = cupsdStartProcess(command, argv, envp, filterfds[!slot][0],
3462 filterfds[slot][1], job->status_pipes[1],
3463 job->back_pipes[1], job->side_pipes[1],
3464 backroot, job->profile, &(job->backend));
3465
3466 if (pid == 0)
3467 {
3468 cupsdLogJob(job, CUPSD_LOG_ERROR, "Unable to start backend \"%s\" - %s.",
3469 method, strerror(errno));
3470 snprintf(printer->state_message, sizeof(printer->state_message),
3471 "Unable to start backend \"%s\" - %s.", method,
3472 strerror(errno));
3473
3474 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
3475 "Job canceled because the server could not execute "
3476 "the backend.");
3477
3478 goto abort_job;
3479 }
3480 else
3481 {
3482 cupsdLogJob(job, CUPSD_LOG_INFO, "Started backend %s (PID %d)",
3483 command, pid);
3484 }
3485 }
3486
3487 if (job->current_file == job->num_files)
3488 {
3489 cupsdLogJob(job, CUPSD_LOG_DEBUG2,
3490 "start_job: Closing print pipes [ %d %d ]...",
3491 job->print_pipes[0], job->print_pipes[1]);
3492
3493 cupsdClosePipe(job->print_pipes);
3494
3495 cupsdLogJob(job, CUPSD_LOG_DEBUG2,
3496 "start_job: Closing back pipes [ %d %d ]...",
3497 job->back_pipes[0], job->back_pipes[1]);
3498
3499 cupsdClosePipe(job->back_pipes);
3500
3501 cupsdLogJob(job, CUPSD_LOG_DEBUG2,
3502 "start_job: Closing side pipes [ %d %d ]...",
3503 job->side_pipes[0], job->side_pipes[1]);
3504
3505 cupsdClosePipe(job->side_pipes);
3506
3507 cupsdLogJob(job, CUPSD_LOG_DEBUG2,
3508 "start_job: Closing status output pipe %d...",
3509 job->status_pipes[1]);
3510
3511 close(job->status_pipes[1]);
3512 job->status_pipes[1] = -1;
3513 }
3514 }
3515 else
3516 {
3517 filterfds[slot][0] = -1;
3518 filterfds[slot][1] = -1;
3519
3520 if (job->current_file == job->num_files)
3521 {
3522 cupsdLogJob(job, CUPSD_LOG_DEBUG2,
3523 "start_job: Closing print pipes [ %d %d ]...",
3524 job->print_pipes[0], job->print_pipes[1]);
3525
3526 cupsdClosePipe(job->print_pipes);
3527
3528 cupsdLogJob(job, CUPSD_LOG_DEBUG2,
3529 "start_job: Closing status output pipe %d...",
3530 job->status_pipes[1]);
3531
3532 close(job->status_pipes[1]);
3533 job->status_pipes[1] = -1;
3534 }
3535 }
3536
3537 cupsdLogJob(job, CUPSD_LOG_DEBUG2,
3538 "start_job: Closing filter pipes for slot %d [ %d %d ]...",
3539 slot, filterfds[slot][0], filterfds[slot][1]);
3540 cupsdClosePipe(filterfds[slot]);
3541
3542 if (printer->remote && job->num_files > 1)
3543 {
3544 for (i = 0; i < job->num_files; i ++)
3545 free(argv[i + 6]);
3546 }
3547
3548 free(argv);
3549
3550 cupsdAddSelect(job->status_buffer->fd, (cupsd_selfunc_t)update_job, NULL,
3551 job);
3552
3553 cupsdAddEvent(CUPSD_EVENT_JOB_STATE, job->printer, job, "Job #%d started.",
3554 job->id);
3555
3556 return;
3557
3558
3559 /*
3560 * If we get here, we need to abort the current job and close out all
3561 * files and pipes...
3562 */
3563
3564 abort_job:
3565
3566 for (slot = 0; slot < 2; slot ++)
3567 {
3568 cupsdLogJob(job, CUPSD_LOG_DEBUG2,
3569 "start_job: Closing filter pipes for slot %d [ %d %d ]...",
3570 slot, filterfds[slot][0], filterfds[slot][1]);
3571 cupsdClosePipe(filterfds[slot]);
3572 }
3573
3574 cupsdLogJob(job, CUPSD_LOG_DEBUG2,
3575 "start_job: Closing status pipes [ %d %d ]...",
3576 job->status_pipes[0], job->status_pipes[1]);
3577 cupsdClosePipe(job->status_pipes);
3578 cupsdStatBufDelete(job->status_buffer);
3579
3580 job->status_buffer = NULL;
3581
3582 cupsArrayDelete(filters);
3583
3584 if (printer->remote && job->num_files > 1)
3585 {
3586 for (i = 0; i < job->num_files; i ++)
3587 free(argv[i + 6]);
3588 }
3589
3590 free(argv);
3591
3592 cupsdStopJob(job, 0);
3593 }
3594
3595
3596 /*
3597 * 'unload_job()' - Unload a job from memory.
3598 */
3599
3600 static void
3601 unload_job(cupsd_job_t *job) /* I - Job */
3602 {
3603 if (!job->attrs)
3604 return;
3605
3606 cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] Unloading...", job->id);
3607
3608 ippDelete(job->attrs);
3609
3610 job->attrs = NULL;
3611 job->state = NULL;
3612 job->sheets = NULL;
3613 job->job_sheets = NULL;
3614 job->printer_message = NULL;
3615 job->printer_reasons = NULL;
3616 }
3617
3618
3619 /*
3620 * 'update_job()' - Read a status update from a job's filters.
3621 */
3622
3623 void
3624 update_job(cupsd_job_t *job) /* I - Job to check */
3625 {
3626 int i; /* Looping var */
3627 int copies; /* Number of copies printed */
3628 char message[1024], /* Message text */
3629 *ptr; /* Pointer update... */
3630 int loglevel, /* Log level for message */
3631 event = 0; /* Events? */
3632
3633
3634 while ((ptr = cupsdStatBufUpdate(job->status_buffer, &loglevel,
3635 message, sizeof(message))) != NULL)
3636 {
3637 /*
3638 * Process page and printer state messages as needed...
3639 */
3640
3641 if (loglevel == CUPSD_LOG_PAGE)
3642 {
3643 /*
3644 * Page message; send the message to the page_log file and update the
3645 * job sheet count...
3646 */
3647
3648 cupsdLogJob(job, CUPSD_LOG_DEBUG, "PAGE: %s", message);
3649
3650 if (job->sheets)
3651 {
3652 if (!strncasecmp(message, "total ", 6))
3653 {
3654 /*
3655 * Got a total count of pages from a backend or filter...
3656 */
3657
3658 copies = atoi(message + 6);
3659 copies -= job->sheets->values[0].integer; /* Just track the delta */
3660 }
3661 else if (!sscanf(message, "%*d%d", &copies))
3662 copies = 1;
3663
3664 job->sheets->values[0].integer += copies;
3665
3666 if (job->printer->page_limit)
3667 {
3668 cupsd_quota_t *q = cupsdUpdateQuota(job->printer, job->username,
3669 copies, 0);
3670
3671 #ifdef __APPLE__
3672 if (AppleQuotas && q->page_count == -3)
3673 {
3674 /*
3675 * Quota limit exceeded, cancel job in progress immediately...
3676 */
3677
3678 cupsdLogJob(job, CUPSD_LOG_INFO,
3679 "Canceled because pages exceed user %s "
3680 "quota limit on printer %s (%s).",
3681 job->username, job->printer->name,
3682 job->printer->info);
3683
3684 cupsdCancelJob(job, 1, IPP_JOB_CANCELED);
3685 return;
3686 }
3687 #else
3688 (void)q;
3689 #endif /* __APPLE__ */
3690 }
3691 }
3692
3693 cupsdLogPage(job, message);
3694
3695 if (job->sheets)
3696 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS, job->printer, job,
3697 "Printed %d page(s).", job->sheets->values[0].integer);
3698 }
3699 else if (loglevel == CUPSD_LOG_STATE)
3700 {
3701 cupsdLogJob(job, CUPSD_LOG_DEBUG, "STATE: %s", message);
3702
3703 if (!strcmp(message, "paused"))
3704 {
3705 cupsdStopPrinter(job->printer, 1);
3706 return;
3707 }
3708 else
3709 {
3710 cupsdSetPrinterReasons(job->printer, message);
3711 cupsdAddPrinterHistory(job->printer);
3712 event |= CUPSD_EVENT_PRINTER_STATE;
3713 }
3714
3715 update_job_attrs(job, 0);
3716 }
3717 else if (loglevel == CUPSD_LOG_ATTR)
3718 {
3719 /*
3720 * Set attribute(s)...
3721 */
3722
3723 int num_attrs; /* Number of attributes */
3724 cups_option_t *attrs; /* Attributes */
3725 const char *attr; /* Attribute */
3726
3727
3728 cupsdLogJob(job, CUPSD_LOG_DEBUG, "ATTR: %s", message);
3729
3730 num_attrs = cupsParseOptions(message, 0, &attrs);
3731
3732 if ((attr = cupsGetOption("auth-info-required", num_attrs,
3733 attrs)) != NULL)
3734 {
3735 cupsdSetAuthInfoRequired(job->printer, attr, NULL);
3736 cupsdSetPrinterAttrs(job->printer);
3737
3738 if (job->printer->type & CUPS_PRINTER_DISCOVERED)
3739 cupsdMarkDirty(CUPSD_DIRTY_REMOTE);
3740 else
3741 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
3742 }
3743
3744 if ((attr = cupsGetOption("job-media-progress", num_attrs,
3745 attrs)) != NULL)
3746 {
3747 int progress = atoi(attr);
3748
3749
3750 if (progress >= 0 && progress <= 100)
3751 {
3752 job->progress = progress;
3753
3754 if (job->sheets)
3755 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS, job->printer, job,
3756 "Printing page %d, %d%%",
3757 job->sheets->values[0].integer, job->progress);
3758 }
3759 }
3760
3761 if ((attr = cupsGetOption("printer-alert", num_attrs, attrs)) != NULL)
3762 {
3763 cupsdSetString(&job->printer->alert, attr);
3764 event |= CUPSD_EVENT_PRINTER_STATE;
3765 }
3766
3767 if ((attr = cupsGetOption("printer-alert-description", num_attrs,
3768 attrs)) != NULL)
3769 {
3770 cupsdSetString(&job->printer->alert_description, attr);
3771 event |= CUPSD_EVENT_PRINTER_STATE;
3772 }
3773
3774 if ((attr = cupsGetOption("marker-colors", num_attrs, attrs)) != NULL)
3775 {
3776 cupsdSetPrinterAttr(job->printer, "marker-colors", (char *)attr);
3777 job->printer->marker_time = time(NULL);
3778 event |= CUPSD_EVENT_PRINTER_STATE;
3779 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
3780 }
3781
3782 if ((attr = cupsGetOption("marker-levels", num_attrs, attrs)) != NULL)
3783 {
3784 cupsdSetPrinterAttr(job->printer, "marker-levels", (char *)attr);
3785 job->printer->marker_time = time(NULL);
3786 event |= CUPSD_EVENT_PRINTER_STATE;
3787 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
3788 }
3789
3790 if ((attr = cupsGetOption("marker-low-levels", num_attrs, attrs)) != NULL)
3791 {
3792 cupsdSetPrinterAttr(job->printer, "marker-low-levels", (char *)attr);
3793 job->printer->marker_time = time(NULL);
3794 event |= CUPSD_EVENT_PRINTER_STATE;
3795 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
3796 }
3797
3798 if ((attr = cupsGetOption("marker-high-levels", num_attrs, attrs)) != NULL)
3799 {
3800 cupsdSetPrinterAttr(job->printer, "marker-high-levels", (char *)attr);
3801 job->printer->marker_time = time(NULL);
3802 event |= CUPSD_EVENT_PRINTER_STATE;
3803 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
3804 }
3805
3806 if ((attr = cupsGetOption("marker-message", num_attrs, attrs)) != NULL)
3807 {
3808 cupsdSetPrinterAttr(job->printer, "marker-message", (char *)attr);
3809 job->printer->marker_time = time(NULL);
3810 event |= CUPSD_EVENT_PRINTER_STATE;
3811 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
3812 }
3813
3814 if ((attr = cupsGetOption("marker-names", num_attrs, attrs)) != NULL)
3815 {
3816 cupsdSetPrinterAttr(job->printer, "marker-names", (char *)attr);
3817 job->printer->marker_time = time(NULL);
3818 event |= CUPSD_EVENT_PRINTER_STATE;
3819 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
3820 }
3821
3822 if ((attr = cupsGetOption("marker-types", num_attrs, attrs)) != NULL)
3823 {
3824 cupsdSetPrinterAttr(job->printer, "marker-types", (char *)attr);
3825 job->printer->marker_time = time(NULL);
3826 event |= CUPSD_EVENT_PRINTER_STATE;
3827 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
3828 }
3829
3830 cupsFreeOptions(num_attrs, attrs);
3831 }
3832 else if (loglevel == CUPSD_LOG_PPD)
3833 {
3834 /*
3835 * Set attribute(s)...
3836 */
3837
3838 int num_keywords; /* Number of keywords */
3839 cups_option_t *keywords; /* Keywords */
3840
3841
3842 cupsdLogJob(job, CUPSD_LOG_DEBUG, "PPD: %s", message);
3843
3844 num_keywords = cupsParseOptions(message, 0, &keywords);
3845
3846 if (cupsdUpdatePrinterPPD(job->printer, num_keywords, keywords))
3847 cupsdSetPrinterAttrs(job->printer);
3848
3849 cupsFreeOptions(num_keywords, keywords);
3850 }
3851 #ifdef __APPLE__
3852 else if (!strncmp(message, "recoverable:", 12))
3853 {
3854 cupsdSetPrinterReasons(job->printer,
3855 "+com.apple.print.recoverable-warning");
3856
3857 ptr = message + 12;
3858 while (isspace(*ptr & 255))
3859 ptr ++;
3860
3861 cupsdSetString(&job->printer->recoverable, ptr);
3862 cupsdAddPrinterHistory(job->printer);
3863 event |= CUPSD_EVENT_PRINTER_STATE;
3864 }
3865 else if (!strncmp(message, "recovered:", 10))
3866 {
3867 cupsdSetPrinterReasons(job->printer,
3868 "-com.apple.print.recoverable-warning");
3869
3870 ptr = message + 10;
3871 while (isspace(*ptr & 255))
3872 ptr ++;
3873
3874 cupsdSetString(&job->printer->recoverable, ptr);
3875 cupsdAddPrinterHistory(job->printer);
3876 event |= CUPSD_EVENT_PRINTER_STATE;
3877 }
3878 #endif /* __APPLE__ */
3879 else
3880 {
3881 cupsdLogJob(job, loglevel, "%s", message);
3882
3883 if (loglevel < CUPSD_LOG_DEBUG)
3884 {
3885 strlcpy(job->printer->state_message, message,
3886 sizeof(job->printer->state_message));
3887 cupsdAddPrinterHistory(job->printer);
3888
3889 event |= CUPSD_EVENT_PRINTER_STATE;
3890
3891 if (loglevel <= job->status_level)
3892 {
3893 /*
3894 * Some messages show in the printer-state-message attribute...
3895 */
3896
3897 if (loglevel != CUPSD_LOG_NOTICE)
3898 job->status_level = loglevel;
3899
3900 update_job_attrs(job, 1);
3901 }
3902 }
3903 }
3904
3905 if (!strchr(job->status_buffer->buffer, '\n'))
3906 break;
3907 }
3908
3909 if (event & CUPSD_EVENT_PRINTER_STATE)
3910 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, job->printer, NULL,
3911 (job->printer->type & CUPS_PRINTER_CLASS) ?
3912 "Class \"%s\" state changed." :
3913 "Printer \"%s\" state changed.",
3914 job->printer->name);
3915
3916 if (ptr == NULL && !job->status_buffer->bufused)
3917 {
3918 /*
3919 * See if all of the filters and the backend have returned their
3920 * exit statuses.
3921 */
3922
3923 for (i = 0; job->filters[i] < 0; i ++);
3924
3925 if (job->filters[i])
3926 return;
3927
3928 if (job->current_file >= job->num_files && job->backend > 0)
3929 return;
3930
3931 /*
3932 * Handle the end of job stuff...
3933 */
3934
3935 cupsdFinishJob(job);
3936 }
3937 }
3938
3939
3940 /*
3941 * 'update_job_attrs()' - Update the job-printer-* attributes.
3942 */
3943
3944 void
3945 update_job_attrs(cupsd_job_t *job, /* I - Job to update */
3946 int do_message)/* I - 1 = update job-printer-state message */
3947 {
3948 int i; /* Looping var */
3949 int num_reasons; /* Actual number of reasons */
3950 const char * const *reasons; /* Reasons */
3951 static const char *none = "none", /* "none" */
3952 *paused = "paused";
3953 /* "paused" */
3954
3955
3956 /*
3957 * Get/create the job-printer-state-* attributes...
3958 */
3959
3960 if (!job->printer_message)
3961 {
3962 if ((job->printer_message = ippFindAttribute(job->attrs,
3963 "job-printer-state-message",
3964 IPP_TAG_TEXT)) == NULL)
3965 job->printer_message = ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_TEXT,
3966 "job-printer-state-message",
3967 NULL, "");
3968 }
3969
3970 if (!job->printer_reasons)
3971 job->printer_reasons = ippFindAttribute(job->attrs,
3972 "job-printer-state-reasons",
3973 IPP_TAG_KEYWORD);
3974
3975 /*
3976 * If the job isn't printing, return now...
3977 */
3978
3979 if (!job->printer)
3980 return;
3981
3982 /*
3983 * Otherwise copy the printer-state-message value...
3984 */
3985
3986 if (job->printer->state_message[0] &&
3987 (do_message || !job->printer_message->values[0].string.text[0]))
3988 cupsdSetString(&(job->printer_message->values[0].string.text),
3989 job->printer->state_message);
3990
3991 /*
3992 * ... and the printer-state-reasons value...
3993 */
3994
3995 if (job->printer->num_reasons == 0)
3996 {
3997 num_reasons = 1;
3998 reasons = job->printer->state == IPP_PRINTER_STOPPED ? &paused : &none;
3999 }
4000 else
4001 {
4002 num_reasons = job->printer->num_reasons;
4003 reasons = (const char * const *)job->printer->reasons;
4004 }
4005
4006 if (!job->printer_reasons || job->printer_reasons->num_values != num_reasons)
4007 {
4008 ippDeleteAttribute(job->attrs, job->printer_reasons);
4009
4010 job->printer_reasons = ippAddStrings(job->attrs,
4011 IPP_TAG_JOB, IPP_TAG_KEYWORD,
4012 "job-printer-state-reasons",
4013 num_reasons, NULL, NULL);
4014 }
4015
4016 for (i = 0; i < num_reasons; i ++)
4017 cupsdSetString(&(job->printer_reasons->values[i].string.text), reasons[i]);
4018 }
4019
4020
4021 /*
4022 * End of "$Id: job.c 7902 2008-09-03 14:20:17Z mike $".
4023 */