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