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