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