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