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