]> git.ipfire.org Git - thirdparty/cups.git/blame - scheduler/job.c
To prepare to load cups into easysw/current, perform 4 renames.
[thirdparty/cups.git] / scheduler / job.c
CommitLineData
ef416fc2 1/*
a4d04587 2 * "$Id: job.c 5023 2006-01-29 14:39:44Z mike $"
ef416fc2 3 *
4 * Job management routines for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1997-2005 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 destination/user...
29 * cupsdCheckJobs() - Check the pending jobs and start any if the
30 * destination is available.
31 * cupsdCleanJobs() - Clean out old jobs.
32 * cupsdFreeAllJobs() - Free all jobs from memory.
33 * cupsdFindJob() - Find the specified job.
34 * cupsdGetPrinterJobCount() - Get the number of pending, processing,
35 * or held jobs in a printer or class.
36 * cupsdGetUserJobCount() - Get the number of pending, processing,
37 * or held jobs for a user.
38 * cupsdHoldJob() - Hold the specified job.
39 * cupsdLoadAllJobs() - Load all jobs from disk.
40 * cupsdMoveJob() - Move the specified job to a different
41 * destination.
42 * cupsdReleaseJob() - Release the specified job.
43 * cupsdRestartJob() - Restart the specified job.
44 * cupsdSaveJob() - Save a job to disk.
45 * cupsdSetJobHoldUntil() - Set the hold time for a job...
46 * cupsdSetJobPriority() - Set the priority of a job, moving it up/down
47 * in the list as needed.
48 * cupsdStartJob() - Start a print job.
49 * cupsdStopAllJobs() - Stop all print jobs.
50 * cupsdStopJob() - Stop a print job.
51 * cupsdUpdateJob() - Read a status update from a job's filters.
52 * compare_active_jobs() - Compare the job IDs and priorities of two jobs.
53 * compare_jobs() - Compare the job IDs of two jobs.
54 * ipp_length() - Compute the size of the buffer needed to hold
55 * the textual IPP attributes.
56 * set_hold_until() - Set the hold time and update job-hold-until attribute.
57 */
58
59/*
60 * Include necessary headers...
61 */
62
63#include "cupsd.h"
64#include <grp.h>
65#include <cups/backend.h>
66#include <cups/dir.h>
67
68
69/*
70 * Local globals...
71 */
72
73static mime_filter_t gziptoany_filter =
74 {
75 NULL, /* Source type */
76 NULL, /* Destination type */
77 0, /* Cost */
78 "gziptoany" /* Filter program to run */
79 };
80
81
82/*
83 * Local functions...
84 */
85
86static int compare_active_jobs(void *first, void *second, void *data);
87static int compare_jobs(void *first, void *second, void *data);
88static int ipp_length(ipp_t *ipp);
89static void set_time(cupsd_job_t *job, const char *name);
90static void set_hold_until(cupsd_job_t *job, time_t holdtime);
91
92
93/*
94 * 'cupsdAddJob()' - Add a new job to the job queue...
95 */
96
97cupsd_job_t * /* O - New job record */
98cupsdAddJob(int priority, /* I - Job priority */
99 const char *dest) /* I - Job destination */
100{
101 cupsd_job_t *job; /* New job record */
102
103
104 job = calloc(sizeof(cupsd_job_t), 1);
105
106 job->id = NextJobId ++;
107 job->priority = priority;
108 job->back_pipes[0] = -1;
109 job->back_pipes[1] = -1;
110 job->print_pipes[0] = -1;
111 job->print_pipes[1] = -1;
112
113 cupsdSetString(&job->dest, dest);
114
115 /*
116 * Add the new job to the "all jobs" and "active jobs" lists...
117 */
118
119 cupsArrayAdd(Jobs, job);
120 cupsArrayAdd(ActiveJobs, job);
121
122 return (job);
123}
124
125
126/*
127 * 'cupsdCancelJob()' - Cancel the specified print job.
128 */
129
130void
131cupsdCancelJob(cupsd_job_t *job, /* I - Job to cancel */
132 int purge) /* I - Purge jobs? */
133{
134 int i; /* Looping var */
135 char filename[1024]; /* Job filename */
136
137
138 cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdCancelJob: id = %d", job->id);
139
140 /*
141 * Remove the job from the active list...
142 */
143
144 cupsArrayRemove(ActiveJobs, job);
145
146 /*
147 * Stop any processes that are working on the current job...
148 */
149
150 if (job->state->values[0].integer == IPP_JOB_PROCESSING)
151 cupsdStopJob(job, 0);
152
153 cupsArrayRemove(ActiveJobs, job);
154
155 job->state->values[0].integer = IPP_JOB_CANCELLED;
156
157 set_time(job, "time-at-completed");
158
159 cupsdExpireSubscriptions(NULL, job);
160
161 /*
162 * Remove any authentication data...
163 */
164
165 snprintf(filename, sizeof(filename), "%s/a%05d", RequestRoot,
166 job->id);
167 unlink(filename);
168
169 /*
170 * Remove the print file for good if we aren't preserving jobs or
171 * files...
172 */
173
174 job->current_file = 0;
175
176 if (!JobHistory || !JobFiles || purge ||
177 (job->dtype & CUPS_PRINTER_REMOTE))
178 for (i = 1; i <= job->num_files; i ++)
179 {
180 snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot,
181 job->id, i);
182 unlink(filename);
183 }
184
185 if (JobHistory && !purge && !(job->dtype & CUPS_PRINTER_REMOTE))
186 {
187 /*
188 * Save job state info...
189 */
190
191 cupsdSaveJob(job);
192 }
193 else
194 {
195 /*
196 * Remove the job info file...
197 */
198
199 snprintf(filename, sizeof(filename), "%s/c%05d", RequestRoot,
200 job->id);
201 unlink(filename);
202
203 /*
204 * Remove the job from the "all jobs" list...
205 */
206
207 cupsArrayRemove(Jobs, job);
208
209 /*
210 * Free all memory used...
211 */
212
213 if (job->attrs != NULL)
214 ippDelete(job->attrs);
215
216 if (job->num_files > 0)
217 {
218 free(job->compressions);
219 free(job->filetypes);
220 }
221
222 cupsdClearString(&job->username);
223 cupsdClearString(&job->dest);
224
225 free(job);
226 }
227}
228
229
230/*
231 * 'cupsdCancelJobs()' - Cancel all jobs for the given destination/user...
232 */
233
234void
235cupsdCancelJobs(const char *dest, /* I - Destination to cancel */
236 const char *username, /* I - Username or NULL */
237 int purge) /* I - Purge jobs? */
238{
239 cupsd_job_t *job; /* Current job */
240
241
242 for (job = (cupsd_job_t *)cupsArrayFirst(Jobs);
243 job;
244 job = (cupsd_job_t *)cupsArrayNext(Jobs))
245 if ((dest == NULL || !strcmp(job->dest, dest)) &&
246 (username == NULL || !strcmp(job->username, username)))
247 {
248 /*
249 * Cancel all jobs matching this destination/user...
250 */
251
252 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
253 purge ? "Job purged." : "Job canceled.");
254
255 cupsdCancelJob(job, purge);
256 }
257
258 cupsdCheckJobs();
259}
260
261
262/*
263 * 'cupsdCheckJobs()' - Check the pending jobs and start any if the destination
264 * is available.
265 */
266
267void
268cupsdCheckJobs(void)
269{
270 cupsd_job_t *job; /* Current job in queue */
271 cupsd_printer_t *printer, /* Printer destination */
272 *pclass; /* Printer class destination */
273
274
275 DEBUG_puts("cupsdCheckJobs()");
276
277 for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs);
278 job;
279 job = (cupsd_job_t *)cupsArrayNext(ActiveJobs))
280 {
281 /*
282 * Start held jobs if they are ready...
283 */
284
285 if (job->state->values[0].integer == IPP_JOB_HELD &&
286 job->hold_until &&
287 job->hold_until < time(NULL))
288 job->state->values[0].integer = IPP_JOB_PENDING;
289
290 /*
291 * Start pending jobs if the destination is available...
292 */
293
09ec0018 294 if (job->state->values[0].integer == IPP_JOB_PENDING && !NeedReload &&
295 !Sleeping)
ef416fc2 296 {
297 printer = cupsdFindDest(job->dest);
298 pclass = NULL;
299
300 while (printer &&
301 (printer->type & (CUPS_PRINTER_IMPLICIT | CUPS_PRINTER_CLASS)))
302 {
303 /*
304 * If the class is remote, just pass it to the remote server...
305 */
306
307 pclass = printer;
308
309 if (!(pclass->type & CUPS_PRINTER_REMOTE))
310 {
311 if (pclass->state != IPP_PRINTER_STOPPED)
312 printer = cupsdFindAvailablePrinter(job->dest);
313 else
314 printer = NULL;
315 }
316 }
317
318 if (!printer && !pclass)
319 {
320 /*
321 * Whoa, the printer and/or class for this destination went away;
322 * cancel the job...
323 */
324
325 cupsdLogMessage(CUPSD_LOG_WARN,
326 "Printer/class %s has gone away; cancelling job %d!",
327 job->dest, job->id);
328
329 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
330 "Job canceled because the destination printer/class has gone away.");
331
332 cupsdCancelJob(job, 1);
333 }
334 else if (printer)
335 {
336 /*
337 * See if the printer is available or remote and not printing a job;
338 * if so, start the job...
339 */
340
341 if (pclass)
342 {
343 /*
344 * Add/update a job-actual-printer-uri attribute for this job
345 * so that we know which printer actually printed the job...
346 */
347
348 ipp_attribute_t *attr; /* job-actual-printer-uri attribute */
349
350
351 if ((attr = ippFindAttribute(job->attrs, "job-actual-printer-uri",
352 IPP_TAG_URI)) != NULL)
353 cupsdSetString(&attr->values[0].string.text, printer->uri);
354 else
355 ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_URI,
356 "job-actual-printer-uri", NULL, printer->uri);
357 }
358
359 if (printer->state == IPP_PRINTER_IDLE || /* Printer is idle */
360 ((printer->type & CUPS_PRINTER_REMOTE) && /* Printer is remote */
361 !printer->job)) /* and not printing a job */
362 cupsdStartJob(job, printer);
363 }
364 }
365 }
366}
367
368
369/*
370 * 'cupsdCleanJobs()' - Clean out old jobs.
371 */
372
373void
374cupsdCleanJobs(void)
375{
376 cupsd_job_t *job; /* Current job */
377
378
379 if (!MaxJobs)
380 return;
381
382 for (job = (cupsd_job_t *)cupsArrayFirst(Jobs);
383 job && cupsArrayCount(Jobs) >= MaxJobs;
384 job = (cupsd_job_t *)cupsArrayNext(Jobs))
385 if (job->state->values[0].integer >= IPP_JOB_CANCELLED)
386 cupsdCancelJob(job, 1);
387}
388
389
390/*
391 * 'cupsdFinishJob()' - Finish a job.
392 */
393
394void
395cupsdFinishJob(cupsd_job_t *job) /* I - Job */
396{
397 int job_history; /* Did cupsdCancelJob() keep the job? */
398 cupsd_printer_t *printer; /* Current printer */
399
400
401 cupsdLogMessage(CUPSD_LOG_DEBUG,
402 "cupsdFinishJob: job %d, file %d is complete.",
403 job->id, job->current_file - 1);
404
405 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdFinishJob: job->status is %d",
406 job->status);
407
408 if (job->status_buffer && job->current_file >= job->num_files)
409 {
410 /*
411 * Close the pipe and clear the input bit.
412 */
413
414 cupsdLogMessage(CUPSD_LOG_DEBUG2,
415 "cupsdFinishJob: Removing fd %d from InputSet...",
416 job->status_buffer->fd);
417
418 FD_CLR(job->status_buffer->fd, InputSet);
419
420 cupsdLogMessage(CUPSD_LOG_DEBUG2,
421 "cupsdFinishJob: Closing status input pipe %d...",
422 job->status_buffer->fd);
423
424 cupsdStatBufDelete(job->status_buffer);
425
426 job->status_buffer = NULL;
427 }
428
429 if (job->status < 0)
430 {
431 /*
432 * Backend had errors; stop it...
433 */
434
435 printer = job->printer;
436
437 switch (-job->status)
438 {
439 default :
440 case CUPS_BACKEND_FAILED :
441 /*
442 * Backend failure, use the error-policy to determine how to
443 * act...
444 */
445
446 cupsdStopJob(job, 0);
447 job->state->values[0].integer = IPP_JOB_PENDING;
448 cupsdSaveJob(job);
449
450 /*
451 * If the job was queued to a class, try requeuing it... For
452 * faxes and retry-job queues, hold the current job for 5 minutes.
453 */
454
455 if (job->dtype & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT))
456 cupsdCheckJobs();
457 else if ((printer->type & CUPS_PRINTER_FAX) ||
458 !strcmp(printer->error_policy, "retry-job"))
459 {
460 /*
461 * See how many times we've tried to send the job; if more than
462 * the limit, cancel the job.
463 */
464
465 job->tries ++;
466
467 if (job->tries >= JobRetryLimit)
468 {
469 /*
470 * Too many tries...
471 */
472
473 cupsdLogMessage(CUPSD_LOG_ERROR,
474 "Canceling job %d since it could not be sent after %d tries.",
475 job->id, JobRetryLimit);
476
477 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
478 "Job canceled since it could not be sent after %d tries.",
479 JobRetryLimit);
480
481 cupsdCancelJob(job, 0);
482 }
483 else
484 {
485 /*
486 * Try again in N seconds...
487 */
488
489 set_hold_until(job, time(NULL) + JobRetryInterval);
490 }
491 }
492 else if (!strcmp(printer->error_policy, "abort-job"))
493 cupsdCancelJob(job, 0);
494 break;
495
496 case CUPS_BACKEND_CANCEL :
497 /*
498 * Cancel the job...
499 */
500
501 cupsdCancelJob(job, 0);
502 break;
503
504 case CUPS_BACKEND_HOLD :
505 /*
506 * Hold the job...
507 */
508
509 cupsdStopJob(job, 0);
510 cupsdSetJobHoldUntil(job, "indefinite");
511 cupsdSaveJob(job);
512 break;
513
514 case CUPS_BACKEND_STOP :
515 /*
516 * Stop the printer...
517 */
518
519 cupsdStopJob(job, 0);
520 cupsdSaveJob(job);
521 cupsdSetPrinterState(printer, IPP_PRINTER_STOPPED, 1);
522 break;
523
524 case CUPS_BACKEND_AUTH_REQUIRED :
525 cupsdStopJob(job, 0);
526 cupsdSetJobHoldUntil(job, "authenticated");
527 cupsdSaveJob(job);
528
529 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED, printer, job,
530 "Authentication is required for job %d.", job->id);
531 break;
532 }
533
534 /*
535 * Try printing another job...
536 */
537
538 cupsdCheckJobs();
539 }
540 else if (job->status > 0)
541 {
542 /*
543 * Filter had errors; cancel it...
544 */
545
546 if (job->current_file < job->num_files)
547 cupsdStartJob(job, job->printer);
548 else
549 {
550 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
551 "Job aborted due to filter errors; please consult the "
552 "error_log file for details.");
553
554 job_history = JobHistory && !(job->dtype & CUPS_PRINTER_REMOTE);
555
556 cupsdCancelJob(job, 0);
557
558 if (job_history)
559 {
560 job->state->values[0].integer = IPP_JOB_ABORTED;
561 cupsdSaveJob(job);
562 }
563
564 cupsdCheckJobs();
565 }
566 }
567 else
568 {
569 /*
570 * Job printed successfully; cancel it...
571 */
572
573 if (job->current_file < job->num_files)
574 {
575 FilterLevel -= job->cost;
576 cupsdStartJob(job, job->printer);
577 }
578 else
579 {
580 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
581 "Job completed successfully.");
582
583 job_history = JobHistory && !(job->dtype & CUPS_PRINTER_REMOTE);
584
585 cupsdCancelJob(job, 0);
586
587 if (job_history)
588 {
589 job->state->values[0].integer = IPP_JOB_COMPLETED;
590 cupsdSaveJob(job);
591 }
592
593 cupsdCheckJobs();
594 }
595 }
596}
597
598
599/*
600 * 'cupsdFreeAllJobs()' - Free all jobs from memory.
601 */
602
603void
604cupsdFreeAllJobs(void)
605{
606 cupsd_job_t *job; /* Current job */
607
608
609 cupsdHoldSignals();
610
611 cupsdStopAllJobs();
612
613 for (job = (cupsd_job_t *)cupsArrayFirst(Jobs);
614 job;
615 job = (cupsd_job_t *)cupsArrayNext(Jobs))
616 {
617 cupsArrayRemove(Jobs, job);
618 cupsArrayRemove(ActiveJobs, job);
619
620 ippDelete(job->attrs);
621
622 if (job->num_files > 0)
623 {
624 free(job->compressions);
625 free(job->filetypes);
626 }
627
628 free(job);
629 }
630
631 cupsdReleaseSignals();
632}
633
634
635/*
636 * 'cupsdFindJob()' - Find the specified job.
637 */
638
639cupsd_job_t * /* O - Job data */
640cupsdFindJob(int id) /* I - Job ID */
641{
642 cupsd_job_t key; /* Search key */
643
644
645 key.id = id;
646
647 return ((cupsd_job_t *)cupsArrayFind(Jobs, &key));
648}
649
650
651/*
652 * 'cupsdGetPrinterJobCount()' - Get the number of pending, processing,
653 * or held jobs in a printer or class.
654 */
655
656int /* O - Job count */
657cupsdGetPrinterJobCount(
658 const char *dest) /* I - Printer or class name */
659{
660 int count; /* Job count */
661 cupsd_job_t *job; /* Current job */
662
663
664 for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs), count = 0;
665 job;
666 job = (cupsd_job_t *)cupsArrayNext(ActiveJobs))
667 if (!strcasecmp(job->dest, dest))
668 count ++;
669
670 return (count);
671}
672
673
674/*
675 * 'cupsdGetUserJobCount()' - Get the number of pending, processing,
676 * or held jobs for a user.
677 */
678
679int /* O - Job count */
680cupsdGetUserJobCount(
681 const char *username) /* I - Username */
682{
683 int count; /* Job count */
684 cupsd_job_t *job; /* Current job */
685
686
687 for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs), count = 0;
688 job;
689 job = (cupsd_job_t *)cupsArrayNext(ActiveJobs))
690 if (!strcasecmp(job->username, username))
691 count ++;
692
693 return (count);
694}
695
696
697/*
698 * 'cupsdHoldJob()' - Hold the specified job.
699 */
700
701void
702cupsdHoldJob(cupsd_job_t *job) /* I - Job data */
703{
704 cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdHoldJob: id = %d", job->id);
705
706 if (job->state->values[0].integer == IPP_JOB_PROCESSING)
707 cupsdStopJob(job, 0);
708
709 DEBUG_puts("cupsdHoldJob: setting state to held...");
710
711 job->state->values[0].integer = IPP_JOB_HELD;
712
713 cupsdSaveJob(job);
714
715 cupsdCheckJobs();
716}
717
718
719/*
720 * 'cupsdLoadAllJobs()' - Load all jobs from disk.
721 */
722
723void
724cupsdLoadAllJobs(void)
725{
726 cups_dir_t *dir; /* Directory */
727 cups_dentry_t *dent; /* Directory entry */
728 char filename[1024]; /* Full filename of job file */
fa73b229 729 cups_file_t *fp; /* Job file */
ef416fc2 730 cupsd_job_t *job; /* New job */
731 int jobid, /* Current job ID */
732 fileid; /* Current file ID */
733 ipp_attribute_t *attr; /* Job attribute */
734 char method[HTTP_MAX_URI],
735 /* Method portion of URI */
736 username[HTTP_MAX_URI],
737 /* Username portion of URI */
738 host[HTTP_MAX_URI],
739 /* Host portion of URI */
740 resource[HTTP_MAX_URI];
741 /* Resource portion of URI */
742 int port; /* Port portion of URI */
743 const char *dest; /* Destination */
744 mime_type_t **filetypes; /* New filetypes array */
745 int *compressions; /* New compressions array */
746
747
748 /*
749 * First create the job lists...
750 */
751
752 if (!Jobs)
753 Jobs = cupsArrayNew(compare_jobs, NULL);
754
755 if (!ActiveJobs)
756 ActiveJobs = cupsArrayNew(compare_active_jobs, NULL);
757
758 /*
759 * Then open the requests directory...
760 */
761
762 cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdLoadAllJobs: Scanning %s...",
763 RequestRoot);
764
765 if ((dir = cupsDirOpen(RequestRoot)) == NULL)
766 {
767 cupsdLogMessage(CUPSD_LOG_ERROR,
768 "cupsdLoadAllJobs: Unable to open spool directory %s: %s",
769 RequestRoot, strerror(errno));
770 return;
771 }
772
773 /*
774 * Read all the c##### files...
775 */
776
777 while ((dent = cupsDirRead(dir)) != NULL)
778 if (strlen(dent->filename) >= 6 && dent->filename[0] == 'c')
779 {
780 /*
781 * Allocate memory for the job...
782 */
783
784 if ((job = calloc(sizeof(cupsd_job_t), 1)) == NULL)
785 {
786 cupsdLogMessage(CUPSD_LOG_ERROR,
787 "cupsdLoadAllJobs: Ran out of memory for jobs!");
788 cupsDirClose(dir);
789 return;
790 }
791
792 if ((job->attrs = ippNew()) == NULL)
793 {
794 free(job);
795 cupsdLogMessage(CUPSD_LOG_ERROR,
796 "cupsdLoadAllJobs: Ran out of memory for job attributes!");
797 cupsDirClose(dir);
798 return;
799 }
800
801 /*
802 * Assign the job ID...
803 */
804
805 job->id = atoi(dent->filename + 1);
806 job->back_pipes[0] = -1;
807 job->back_pipes[1] = -1;
808 job->print_pipes[0] = -1;
809 job->print_pipes[1] = -1;
810
811 cupsdLogMessage(CUPSD_LOG_DEBUG,
812 "cupsdLoadAllJobs: Loading attributes for job %d...",
813 job->id);
814
815 if (job->id >= NextJobId)
816 NextJobId = job->id + 1;
817
818 /*
819 * Load the job control file...
820 */
821
822 snprintf(filename, sizeof(filename), "%s/%s", RequestRoot, dent->filename);
fa73b229 823 if ((fp = cupsFileOpen(filename, "r")) == NULL)
ef416fc2 824 {
825 cupsdLogMessage(CUPSD_LOG_ERROR,
fa73b229 826 "cupsdLoadAllJobs: Unable to open job control file "
827 "\"%s\" - %s!",
ef416fc2 828 filename, strerror(errno));
829 ippDelete(job->attrs);
830 free(job);
831 unlink(filename);
832 continue;
833 }
834 else
835 {
fa73b229 836 if (ippReadIO(fp, (ipp_iocb_t)cupsFileRead, 1, NULL,
837 job->attrs) != IPP_DATA)
ef416fc2 838 {
839 cupsdLogMessage(CUPSD_LOG_ERROR,
fa73b229 840 "cupsdLoadAllJobs: Unable to read job control file "
841 "\"%s\"!",
ef416fc2 842 filename);
fa73b229 843 cupsFileClose(fp);
ef416fc2 844 ippDelete(job->attrs);
845 free(job);
846 unlink(filename);
847 continue;
848 }
849
fa73b229 850 cupsFileClose(fp);
ef416fc2 851 }
852
853 if ((job->state = ippFindAttribute(job->attrs, "job-state", IPP_TAG_ENUM)) == NULL)
854 {
855 cupsdLogMessage(CUPSD_LOG_ERROR,
fa73b229 856 "cupsdLoadAllJobs: Missing or bad job-state attribute "
857 "in control file \"%s\"!",
ef416fc2 858 filename);
859 ippDelete(job->attrs);
860 free(job);
861 unlink(filename);
862 continue;
863 }
864
865 if ((attr = ippFindAttribute(job->attrs, "job-printer-uri", IPP_TAG_URI)) == NULL)
866 {
867 cupsdLogMessage(CUPSD_LOG_ERROR,
fa73b229 868 "cupsdLoadAllJobs: No job-printer-uri attribute in "
869 "control file \"%s\"!",
ef416fc2 870 filename);
871 ippDelete(job->attrs);
872 free(job);
873 unlink(filename);
874 continue;
875 }
876
a4d04587 877 httpSeparateURI(HTTP_URI_CODING_ALL, attr->values[0].string.text, method,
878 sizeof(method), username, sizeof(username), host,
879 sizeof(host), &port, resource, sizeof(resource));
ef416fc2 880
881 if ((dest = cupsdValidateDest(host, resource, &(job->dtype),
882 NULL)) == NULL)
883 {
884 cupsdLogMessage(CUPSD_LOG_ERROR,
fa73b229 885 "cupsdLoadAllJobs: Unable to queue job for destination "
886 "\"%s\"!",
ef416fc2 887 attr->values[0].string.text);
888 ippDelete(job->attrs);
889 free(job);
890 unlink(filename);
891 continue;
892 }
893
894 cupsdSetString(&job->dest, dest);
895
896 job->sheets = ippFindAttribute(job->attrs, "job-media-sheets-completed",
897 IPP_TAG_INTEGER);
898 job->job_sheets = ippFindAttribute(job->attrs, "job-sheets", IPP_TAG_NAME);
899
fa73b229 900 if ((attr = ippFindAttribute(job->attrs, "job-priority",
901 IPP_TAG_INTEGER)) == NULL)
ef416fc2 902 {
903 cupsdLogMessage(CUPSD_LOG_ERROR,
fa73b229 904 "cupsdLoadAllJobs: Missing or bad job-priority "
905 "attribute in control file \"%s\"!",
ef416fc2 906 filename);
907 ippDelete(job->attrs);
908 free(job);
909 unlink(filename);
910 continue;
911 }
912 job->priority = attr->values[0].integer;
913
fa73b229 914 if ((attr = ippFindAttribute(job->attrs, "job-originating-user-name",
915 IPP_TAG_NAME)) == NULL)
ef416fc2 916 {
917 cupsdLogMessage(CUPSD_LOG_ERROR,
fa73b229 918 "cupsdLoadAllJobs: Missing or bad "
919 "job-originating-user-name attribute in control file "
920 "\"%s\"!",
ef416fc2 921 filename);
922 ippDelete(job->attrs);
923 free(job);
924 unlink(filename);
925 continue;
926 }
927 cupsdSetString(&job->username, attr->values[0].string.text);
928
929 /*
930 * Insert the job into the array, sorting by job priority and ID...
931 */
932
933 cupsArrayAdd(Jobs, job);
934 if (job->state->values[0].integer < IPP_JOB_STOPPED)
935 cupsArrayAdd(ActiveJobs,job);
936
937 /*
938 * Set the job hold-until time and state...
939 */
940
941 if (job->state->values[0].integer == IPP_JOB_HELD)
942 {
fa73b229 943 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
944 IPP_TAG_KEYWORD)) == NULL)
ef416fc2 945 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
946
947 if (attr == NULL)
948 job->state->values[0].integer = IPP_JOB_PENDING;
949 else
950 cupsdSetJobHoldUntil(job, attr->values[0].string.text);
951 }
952 else if (job->state->values[0].integer == IPP_JOB_PROCESSING)
953 job->state->values[0].integer = IPP_JOB_PENDING;
954 }
955
956 /*
957 * Read all the d##### files...
958 */
959
960 cupsDirRewind(dir);
961
962 while ((dent = cupsDirRead(dir)) != NULL)
963 if (strlen(dent->filename) > 7 && dent->filename[0] == 'd' &&
964 strchr(dent->filename, '-'))
965 {
966 /*
967 * Find the job...
968 */
969
970 jobid = atoi(dent->filename + 1);
971 fileid = atoi(strchr(dent->filename, '-') + 1);
972
973 cupsdLogMessage(CUPSD_LOG_DEBUG,
974 "cupsdLoadAllJobs: Auto-typing document file %s...",
975 dent->filename);
976
977 snprintf(filename, sizeof(filename), "%s/%s", RequestRoot, dent->filename);
978
979 if ((job = cupsdFindJob(jobid)) == NULL)
980 {
981 cupsdLogMessage(CUPSD_LOG_ERROR,
982 "cupsdLoadAllJobs: Orphaned print file \"%s\"!",
983 filename);
984 unlink(filename);
985 continue;
986 }
987
988 if (fileid > job->num_files)
989 {
990 if (job->num_files == 0)
991 {
992 compressions = (int *)calloc(fileid, sizeof(int));
993 filetypes = (mime_type_t **)calloc(fileid, sizeof(mime_type_t *));
994 }
995 else
996 {
997 compressions = (int *)realloc(job->compressions,
998 sizeof(int) * fileid);
999 filetypes = (mime_type_t **)realloc(job->filetypes,
1000 sizeof(mime_type_t *) * fileid);
1001 }
1002
1003 if (compressions == NULL || filetypes == NULL)
1004 {
fa73b229 1005 cupsdLogMessage(CUPSD_LOG_ERROR,
1006 "cupsdLoadAllJobs: Ran out of memory for job file "
1007 "types!");
ef416fc2 1008 continue;
1009 }
1010
1011 job->compressions = compressions;
1012 job->filetypes = filetypes;
1013 job->num_files = fileid;
1014 }
1015
1016 job->filetypes[fileid - 1] = mimeFileType(MimeDatabase, filename,
1017 job->compressions + fileid - 1);
1018
1019 if (job->filetypes[fileid - 1] == NULL)
1020 job->filetypes[fileid - 1] = mimeType(MimeDatabase, "application",
1021 "vnd.cups-raw");
1022 }
1023
1024 cupsDirClose(dir);
1025
1026 /*
1027 * Clean out old jobs as needed...
1028 */
1029
1030 cupsdCleanJobs();
1031}
1032
1033
1034/*
1035 * 'cupsdMoveJob()' - Move the specified job to a different destination.
1036 */
1037
1038void
1039cupsdMoveJob(cupsd_job_t *job, /* I - Job */
1040 const char *dest) /* I - Destination */
1041{
1042 ipp_attribute_t *attr; /* job-printer-uri attribute */
1043 cupsd_printer_t *p; /* Destination printer or class */
1044
1045
1046 /*
1047 * Find the printer...
1048 */
1049
1050 if ((p = cupsdFindDest(dest)) == NULL)
1051 return;
1052
1053 /*
1054 * Don't move completed jobs...
1055 */
1056
1057 if (job->state->values[0].integer >= IPP_JOB_PROCESSING)
1058 return;
1059
1060 /*
1061 * Change the destination information...
1062 */
1063
1064 cupsdSetString(&job->dest, dest);
1065 job->dtype = p->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_REMOTE |
1066 CUPS_PRINTER_IMPLICIT);
1067
1068 if ((attr = ippFindAttribute(job->attrs, "job-printer-uri", IPP_TAG_URI)) != NULL)
1069 cupsdSetString(&(attr->values[0].string.text), p->uri);
1070
1071 cupsdSaveJob(job);
1072}
1073
1074
1075/*
1076 * 'cupsdReleaseJob()' - Release the specified job.
1077 */
1078
1079void
1080cupsdReleaseJob(cupsd_job_t *job) /* I - Job */
1081{
1082 cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdReleaseJob: id = %d", job->id);
1083
1084 if (job->state->values[0].integer == IPP_JOB_HELD)
1085 {
1086 DEBUG_puts("cupsdReleaseJob: setting state to pending...");
1087
1088 job->state->values[0].integer = IPP_JOB_PENDING;
1089 cupsdSaveJob(job);
1090 cupsdCheckJobs();
1091 }
1092}
1093
1094
1095/*
1096 * 'cupsdRestartJob()' - Restart the specified job.
1097 */
1098
1099void
1100cupsdRestartJob(cupsd_job_t *job) /* I - Job */
1101{
1102 cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdRestartJob: id = %d", job->id);
1103
1104 if (job->state->values[0].integer == IPP_JOB_STOPPED || JobFiles)
1105 {
1106 job->tries = 0;
1107 job->state->values[0].integer = IPP_JOB_PENDING;
1108 cupsdSaveJob(job);
1109 cupsdCheckJobs();
1110 }
1111}
1112
1113
1114/*
1115 * 'cupsdSaveJob()' - Save a job to disk.
1116 */
1117
1118void
1119cupsdSaveJob(cupsd_job_t *job) /* I - Job */
1120{
1121 char filename[1024]; /* Job control filename */
fa73b229 1122 cups_file_t *fp; /* Job file */
ef416fc2 1123
1124
1125 snprintf(filename, sizeof(filename), "%s/c%05d", RequestRoot, job->id);
1126
fa73b229 1127 if ((fp = cupsFileOpen(filename, "w")) == NULL)
ef416fc2 1128 {
1129 cupsdLogMessage(CUPSD_LOG_ERROR,
fa73b229 1130 "cupsdSaveJob: Unable to create job control file "
1131 "\"%s\" - %s.",
ef416fc2 1132 filename, strerror(errno));
1133 return;
1134 }
1135
fa73b229 1136 fchmod(cupsFileNumber(fp), 0600);
1137 fchown(cupsFileNumber(fp), RunUser, Group);
ef416fc2 1138
fa73b229 1139 ippWriteIO(fp, (ipp_iocb_t)cupsFileWrite, 1, NULL, job->attrs);
ef416fc2 1140
fa73b229 1141 cupsFileClose(fp);
ef416fc2 1142}
1143
1144
1145/*
1146 * 'cupsdSetJobHoldUntil()' - Set the hold time for a job...
1147 */
1148
1149void
1150cupsdSetJobHoldUntil(cupsd_job_t *job, /* I - Job */
1151 const char *when) /* I - When to resume */
1152{
1153 time_t curtime; /* Current time */
1154 struct tm *curdate; /* Current date */
1155 int hour; /* Hold hour */
1156 int minute; /* Hold minute */
1157 int second; /* Hold second */
1158
1159
1160 cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdSetJobHoldUntil(%d, \"%s\")",
1161 job->id, when);
1162
1163 second = 0;
1164
1165 if (!strcmp(when, "indefinite") || !strcmp(when, "authenticated"))
1166 {
1167 /*
1168 * Hold indefinitely...
1169 */
1170
1171 job->hold_until = 0;
1172 }
1173 else if (!strcmp(when, "day-time"))
1174 {
1175 /*
1176 * Hold to 6am the next morning unless local time is < 6pm.
1177 */
1178
1179 curtime = time(NULL);
1180 curdate = localtime(&curtime);
1181
1182 if (curdate->tm_hour < 18)
1183 job->hold_until = curtime;
1184 else
1185 job->hold_until = curtime +
1186 ((29 - curdate->tm_hour) * 60 + 59 -
1187 curdate->tm_min) * 60 + 60 - curdate->tm_sec;
1188 }
1189 else if (!strcmp(when, "evening") || strcmp(when, "night"))
1190 {
1191 /*
1192 * Hold to 6pm unless local time is > 6pm or < 6am.
1193 */
1194
1195 curtime = time(NULL);
1196 curdate = localtime(&curtime);
1197
1198 if (curdate->tm_hour < 6 || curdate->tm_hour >= 18)
1199 job->hold_until = curtime;
1200 else
1201 job->hold_until = curtime +
1202 ((17 - curdate->tm_hour) * 60 + 59 -
1203 curdate->tm_min) * 60 + 60 - curdate->tm_sec;
1204 }
1205 else if (!strcmp(when, "second-shift"))
1206 {
1207 /*
1208 * Hold to 4pm unless local time is > 4pm.
1209 */
1210
1211 curtime = time(NULL);
1212 curdate = localtime(&curtime);
1213
1214 if (curdate->tm_hour >= 16)
1215 job->hold_until = curtime;
1216 else
1217 job->hold_until = curtime +
1218 ((15 - curdate->tm_hour) * 60 + 59 -
1219 curdate->tm_min) * 60 + 60 - curdate->tm_sec;
1220 }
1221 else if (!strcmp(when, "third-shift"))
1222 {
1223 /*
1224 * Hold to 12am unless local time is < 8am.
1225 */
1226
1227 curtime = time(NULL);
1228 curdate = localtime(&curtime);
1229
1230 if (curdate->tm_hour < 8)
1231 job->hold_until = curtime;
1232 else
1233 job->hold_until = curtime +
1234 ((23 - curdate->tm_hour) * 60 + 59 -
1235 curdate->tm_min) * 60 + 60 - curdate->tm_sec;
1236 }
1237 else if (!strcmp(when, "weekend"))
1238 {
1239 /*
1240 * Hold to weekend unless we are in the weekend.
1241 */
1242
1243 curtime = time(NULL);
1244 curdate = localtime(&curtime);
1245
1246 if (curdate->tm_wday || curdate->tm_wday == 6)
1247 job->hold_until = curtime;
1248 else
1249 job->hold_until = curtime +
1250 (((5 - curdate->tm_wday) * 24 +
1251 (17 - curdate->tm_hour)) * 60 + 59 -
1252 curdate->tm_min) * 60 + 60 - curdate->tm_sec;
1253 }
1254 else if (sscanf(when, "%d:%d:%d", &hour, &minute, &second) >= 2)
1255 {
1256 /*
1257 * Hold to specified GMT time (HH:MM or HH:MM:SS)...
1258 */
1259
1260 curtime = time(NULL);
1261 curdate = gmtime(&curtime);
1262
1263 job->hold_until = curtime +
1264 ((hour - curdate->tm_hour) * 60 + minute -
1265 curdate->tm_min) * 60 + second - curdate->tm_sec;
1266
1267 /*
1268 * Hold until next day as needed...
1269 */
1270
1271 if (job->hold_until < curtime)
1272 job->hold_until += 24 * 60 * 60 * 60;
1273 }
1274
1275 cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdSetJobHoldUntil: hold_until = %d",
1276 (int)job->hold_until);
1277}
1278
1279
1280/*
1281 * 'cupsdSetJobPriority()' - Set the priority of a job, moving it up/down in
1282 * the list as needed.
1283 */
1284
1285void
1286cupsdSetJobPriority(
1287 cupsd_job_t *job, /* I - Job ID */
1288 int priority) /* I - New priority (0 to 100) */
1289{
1290 ipp_attribute_t *attr; /* Job attribute */
1291
1292
1293 /*
1294 * Don't change completed jobs...
1295 */
1296
1297 if (job->state->values[0].integer >= IPP_JOB_PROCESSING)
1298 return;
1299
1300 /*
1301 * Set the new priority and re-add the job into the active list...
1302 */
1303
1304 cupsArrayRemove(ActiveJobs, job);
1305
1306 job->priority = priority;
1307
1308 if ((attr = ippFindAttribute(job->attrs, "job-priority", IPP_TAG_INTEGER)) != NULL)
1309 attr->values[0].integer = priority;
1310 else
1311 ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-priority",
1312 priority);
1313
1314 cupsArrayAdd(ActiveJobs, job);
1315
1316 cupsdSaveJob(job);
1317}
1318
1319
1320/*
1321 * 'cupsdStartJob()' - Start a print job.
1322 */
1323
1324void
1325cupsdStartJob(cupsd_job_t *job, /* I - Job ID */
1326 cupsd_printer_t *printer) /* I - Printer to print job */
1327{
1328 int i; /* Looping var */
1329 int slot; /* Pipe slot */
fa73b229 1330 cups_array_t *filters; /* Filters for job */
1331 mime_filter_t *filter, /* Current filter */
1332 port_monitor; /* Port monitor filter */
ef416fc2 1333 char method[255], /* Method for output */
1334 *optptr, /* Pointer to options */
1335 *valptr; /* Pointer in value string */
1336 ipp_attribute_t *attr; /* Current attribute */
1337 int pid; /* Process ID of new filter process */
1338 int banner_page; /* 1 if banner page, 0 otherwise */
1339 int statusfds[2], /* Pipes used between the filters and scheduler */
1340 filterfds[2][2];/* Pipes used between the filters */
1341 int envc; /* Number of environment variables */
1342 char *argv[8], /* Filter command-line arguments */
1343 sani_uri[1024], /* Sanitized DEVICE_URI env var */
1344 filename[1024], /* Job filename */
1345 command[1024], /* Full path to filter/backend command */
1346 jobid[255], /* Job ID string */
1347 title[IPP_MAX_NAME],
1348 /* Job title string */
1349 copies[255], /* # copies string */
1350 *envp[100], /* Environment variables */
1351 charset[255], /* CHARSET environment variable */
1352 class_name[255],/* CLASS environment variable */
1353 classification[1024],
1354 /* CLASSIFICATION environment variable */
1355 content_type[1024],
1356 /* CONTENT_TYPE environment variable */
1357 device_uri[1024],
1358 /* DEVICE_URI environment variable */
1359 lang[255], /* LANG environment variable */
1360 ppd[1024], /* PPD environment variable */
1361 printer_name[255],
1362 /* PRINTER environment variable */
1363 rip_max_cache[255];
1364 /* RIP_MAX_CACHE environment variable */
1365 static char *options = NULL;/* Full list of options */
1366 static int optlength = 0; /* Length of option buffer */
1367
1368
1369 cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdStartJob() id = %d, file = %d/%d",
1370 job->id, job->current_file, job->num_files);
1371
1372 if (job->num_files == 0)
1373 {
1374 cupsdLogMessage(CUPSD_LOG_ERROR, "Job ID %d has no files! Cancelling it!",
1375 job->id);
1376
1377 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
1378 "Job canceled because it has no files.");
1379
1380 cupsdCancelJob(job, 0);
1381 return;
1382 }
1383
1384 /*
1385 * Figure out what filters are required to convert from
1386 * the source to the destination type...
1387 */
1388
fa73b229 1389 filters = NULL;
ef416fc2 1390 job->cost = 0;
1391
1392 if (printer->raw)
1393 {
1394 /*
1395 * Remote jobs and raw queues go directly to the printer without
1396 * filtering...
1397 */
1398
1399 cupsdLogMessage(CUPSD_LOG_DEBUG,
1400 "cupsdStartJob: Sending job to queue tagged as raw...");
1401
1402 filters = NULL;
1403 }
1404 else
1405 {
1406 /*
1407 * Local jobs get filtered...
1408 */
1409
1410 filters = mimeFilter(MimeDatabase, job->filetypes[job->current_file],
fa73b229 1411 printer->filetype, &(job->cost), MAX_FILTERS - 1);
ef416fc2 1412
fa73b229 1413 if (!filters)
ef416fc2 1414 {
1415 cupsdLogMessage(CUPSD_LOG_ERROR,
1416 "Unable to convert file %d to printable format for job %d!",
1417 job->current_file, job->id);
1418 cupsdLogMessage(CUPSD_LOG_INFO,
1419 "Hint: Do you have ESP Ghostscript installed?");
1420
1421 if (LogLevel < CUPSD_LOG_DEBUG)
1422 cupsdLogMessage(CUPSD_LOG_INFO,
1423 "Hint: Try setting the LogLevel to \"debug\".");
1424
1425 job->current_file ++;
1426
1427 if (job->current_file == job->num_files)
1428 {
1429 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
1430 "Job canceled because it has no files that can be printed.");
1431
1432 cupsdCancelJob(job, 0);
1433 }
1434
1435 return;
1436 }
1437
1438 /*
1439 * Remove NULL ("-") filters...
1440 */
1441
fa73b229 1442 for (filter = (mime_filter_t *)cupsArrayFirst(filters);
1443 filter;
1444 filter = (mime_filter_t *)cupsArrayNext(filters))
1445 if (!strcmp(filter->filter, "-"))
1446 cupsArrayRemove(filters, filter);
ef416fc2 1447
fa73b229 1448 if (cupsArrayCount(filters) == 0)
ef416fc2 1449 {
fa73b229 1450 cupsArrayDelete(filters);
ef416fc2 1451 filters = NULL;
1452 }
ef416fc2 1453 }
1454
1455 /*
1456 * See if the filter cost is too high...
1457 */
1458
1459 if ((FilterLevel + job->cost) > FilterLimit && FilterLevel > 0 &&
1460 FilterLimit > 0)
1461 {
1462 /*
1463 * Don't print this job quite yet...
1464 */
1465
fa73b229 1466 cupsArrayDelete(filters);
ef416fc2 1467
1468 cupsdLogMessage(CUPSD_LOG_INFO,
1469 "Holding job %d because filter limit has been reached.",
1470 job->id);
1471 cupsdLogMessage(CUPSD_LOG_DEBUG,
1472 "cupsdStartJob: id=%d, file=%d, cost=%d, level=%d, limit=%d",
1473 job->id, job->current_file, job->cost, FilterLevel,
1474 FilterLimit);
1475 return;
1476 }
1477
1478 FilterLevel += job->cost;
1479
1480 /*
1481 * Add decompression filters, if any...
1482 */
1483
1484 if (job->compressions[job->current_file])
1485 {
1486 /*
1487 * Add gziptoany filter to the front of the list...
1488 */
1489
fa73b229 1490 if (!cupsArrayInsert(filters, &gziptoany_filter))
ef416fc2 1491 {
1492 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to add decompression filter - %s",
1493 strerror(errno));
1494
1495 if (filters != NULL)
1496 free(filters);
1497
1498 job->current_file ++;
1499
1500 if (job->current_file == job->num_files)
1501 {
1502 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
fa73b229 1503 "Job canceled because the print file could not be "
1504 "decompressed.");
ef416fc2 1505
1506 cupsdCancelJob(job, 0);
1507 }
1508
1509 return;
1510 }
ef416fc2 1511 }
1512
1513 /*
1514 * Add port monitor, if any...
1515 */
1516
1517 if (printer->port_monitor)
1518 {
1519 /*
1520 * Add port monitor to the end of the list...
1521 */
1522
fa73b229 1523 if (!cupsArrayAdd(filters, &port_monitor))
ef416fc2 1524 {
1525 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to add port monitor - %s",
1526 strerror(errno));
1527
1528 if (filters != NULL)
1529 free(filters);
1530
1531 job->current_file ++;
1532
1533 if (job->current_file == job->num_files)
1534 {
1535 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
fa73b229 1536 "Job canceled because the port monitor could not be "
1537 "added.");
ef416fc2 1538
1539 cupsdCancelJob(job, 0);
1540 }
1541
1542 return;
1543 }
1544
fa73b229 1545 snprintf(port_monitor.filter, sizeof(port_monitor.filter),
ef416fc2 1546 "%s/monitor/%s", ServerBin, printer->port_monitor);
ef416fc2 1547 }
1548
1549 /*
1550 * Update the printer and job state to "processing"...
1551 */
1552
1553 job->state->values[0].integer = IPP_JOB_PROCESSING;
1554 job->status = 0;
1555 job->printer = printer;
1556 printer->job = job;
1557 cupsdSetPrinterState(printer, IPP_PRINTER_PROCESSING, 0);
1558
1559 if (job->current_file == 0)
1560 {
1561 set_time(job, "time-at-processing");
1562 cupsdOpenPipe(job->back_pipes);
1563 }
1564
1565 /*
1566 * Determine if we are printing a banner page or not...
1567 */
1568
1569 if (job->job_sheets == NULL)
1570 {
1571 cupsdLogMessage(CUPSD_LOG_DEBUG, "No job-sheets attribute.");
1572 if ((job->job_sheets =
1573 ippFindAttribute(job->attrs, "job-sheets", IPP_TAG_ZERO)) != NULL)
1574 cupsdLogMessage(CUPSD_LOG_DEBUG,
1575 "... but someone added one without setting job_sheets!");
1576 }
1577 else if (job->job_sheets->num_values == 1)
1578 cupsdLogMessage(CUPSD_LOG_DEBUG, "job-sheets=%s",
1579 job->job_sheets->values[0].string.text);
1580 else
1581 cupsdLogMessage(CUPSD_LOG_DEBUG, "job-sheets=%s,%s",
1582 job->job_sheets->values[0].string.text,
1583 job->job_sheets->values[1].string.text);
1584
1585 if (printer->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT))
1586 banner_page = 0;
1587 else if (job->job_sheets == NULL)
1588 banner_page = 0;
1589 else if (strcasecmp(job->job_sheets->values[0].string.text, "none") != 0 &&
1590 job->current_file == 0)
1591 banner_page = 1;
1592 else if (job->job_sheets->num_values > 1 &&
1593 strcasecmp(job->job_sheets->values[1].string.text, "none") != 0 &&
1594 job->current_file == (job->num_files - 1))
1595 banner_page = 1;
1596 else
1597 banner_page = 0;
1598
1599 cupsdLogMessage(CUPSD_LOG_DEBUG, "banner_page = %d", banner_page);
1600
1601 /*
1602 * Building the options string is harder than it needs to be, but
1603 * for the moment we need to pass strings for command-line args and
1604 * not IPP attribute pointers... :)
1605 *
1606 * First allocate/reallocate the option buffer as needed...
1607 */
1608
1609 i = ipp_length(job->attrs);
1610
1611 if (i > optlength)
1612 {
1613 if (optlength == 0)
1614 optptr = malloc(i);
1615 else
1616 optptr = realloc(options, i);
1617
1618 if (optptr == NULL)
1619 {
1620 cupsdLogMessage(CUPSD_LOG_CRIT,
1621 "cupsdStartJob: Unable to allocate %d bytes for option buffer for job %d!",
1622 i, job->id);
1623
1624 if (filters != NULL)
1625 free(filters);
1626
1627 FilterLevel -= job->cost;
1628
1629 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
1630 "Job canceled because the server ran out of memory.");
1631
1632 cupsdCancelJob(job, 0);
1633 return;
1634 }
1635
1636 options = optptr;
1637 optlength = i;
1638 }
1639
1640 /*
1641 * Now loop through the attributes and convert them to the textual
1642 * representation used by the filters...
1643 */
1644
1645 optptr = options;
1646 *optptr = '\0';
1647
1648 snprintf(title, sizeof(title), "%s-%d", printer->name, job->id);
1649 strcpy(copies, "1");
1650
1651 for (attr = job->attrs->attrs; attr != NULL; attr = attr->next)
1652 {
1653 if (strcmp(attr->name, "copies") == 0 &&
1654 attr->value_tag == IPP_TAG_INTEGER)
1655 {
1656 /*
1657 * Don't use the # copies attribute if we are printing the job sheets...
1658 */
1659
1660 if (!banner_page)
1661 sprintf(copies, "%d", attr->values[0].integer);
1662 }
1663 else if (strcmp(attr->name, "job-name") == 0 &&
1664 (attr->value_tag == IPP_TAG_NAME ||
1665 attr->value_tag == IPP_TAG_NAMELANG))
1666 strlcpy(title, attr->values[0].string.text, sizeof(title));
1667 else if (attr->group_tag == IPP_TAG_JOB)
1668 {
1669 /*
1670 * Filter out other unwanted attributes...
1671 */
1672
1673 if (attr->value_tag == IPP_TAG_MIMETYPE ||
1674 attr->value_tag == IPP_TAG_NAMELANG ||
1675 attr->value_tag == IPP_TAG_TEXTLANG ||
1676 attr->value_tag == IPP_TAG_URI ||
1677 attr->value_tag == IPP_TAG_URISCHEME ||
1678 attr->value_tag == IPP_TAG_BEGIN_COLLECTION) /* Not yet supported */
1679 continue;
1680
1681 if (strncmp(attr->name, "time-", 5) == 0)
1682 continue;
1683
1684 if (strncmp(attr->name, "job-", 4) == 0 &&
1685 !(printer->type & CUPS_PRINTER_REMOTE))
1686 continue;
1687
1688 if (strncmp(attr->name, "job-", 4) == 0 &&
1689 strcmp(attr->name, "job-billing") != 0 &&
1690 strcmp(attr->name, "job-sheets") != 0 &&
1691 strcmp(attr->name, "job-hold-until") != 0 &&
1692 strcmp(attr->name, "job-priority") != 0)
1693 continue;
1694
1695 if ((strcmp(attr->name, "page-label") == 0 ||
1696 strcmp(attr->name, "page-border") == 0 ||
1697 strncmp(attr->name, "number-up", 9) == 0 ||
1698 strcmp(attr->name, "page-set") == 0) &&
1699 banner_page)
1700 continue;
1701
1702 /*
1703 * Otherwise add them to the list...
1704 */
1705
1706 if (optptr > options)
1707 strlcat(optptr, " ", optlength - (optptr - options));
1708
1709 if (attr->value_tag != IPP_TAG_BOOLEAN)
1710 {
1711 strlcat(optptr, attr->name, optlength - (optptr - options));
1712 strlcat(optptr, "=", optlength - (optptr - options));
1713 }
1714
1715 for (i = 0; i < attr->num_values; i ++)
1716 {
1717 if (i)
1718 strlcat(optptr, ",", optlength - (optptr - options));
1719
1720 optptr += strlen(optptr);
1721
1722 switch (attr->value_tag)
1723 {
1724 case IPP_TAG_INTEGER :
1725 case IPP_TAG_ENUM :
1726 snprintf(optptr, optlength - (optptr - options),
1727 "%d", attr->values[i].integer);
1728 break;
1729
1730 case IPP_TAG_BOOLEAN :
1731 if (!attr->values[i].boolean)
1732 strlcat(optptr, "no", optlength - (optptr - options));
1733
1734 case IPP_TAG_NOVALUE :
1735 strlcat(optptr, attr->name,
1736 optlength - (optptr - options));
1737 break;
1738
1739 case IPP_TAG_RANGE :
1740 if (attr->values[i].range.lower == attr->values[i].range.upper)
1741 snprintf(optptr, optlength - (optptr - options) - 1,
1742 "%d", attr->values[i].range.lower);
1743 else
1744 snprintf(optptr, optlength - (optptr - options) - 1,
1745 "%d-%d", attr->values[i].range.lower,
1746 attr->values[i].range.upper);
1747 break;
1748
1749 case IPP_TAG_RESOLUTION :
1750 snprintf(optptr, optlength - (optptr - options) - 1,
1751 "%dx%d%s", attr->values[i].resolution.xres,
1752 attr->values[i].resolution.yres,
1753 attr->values[i].resolution.units == IPP_RES_PER_INCH ?
1754 "dpi" : "dpc");
1755 break;
1756
1757 case IPP_TAG_STRING :
1758 case IPP_TAG_TEXT :
1759 case IPP_TAG_NAME :
1760 case IPP_TAG_KEYWORD :
1761 case IPP_TAG_CHARSET :
1762 case IPP_TAG_LANGUAGE :
1763 for (valptr = attr->values[i].string.text; *valptr;)
1764 {
1765 if (strchr(" \t\n\\\'\"", *valptr))
1766 *optptr++ = '\\';
1767 *optptr++ = *valptr++;
1768 }
1769
1770 *optptr = '\0';
1771 break;
1772
1773 default :
1774 break; /* anti-compiler-warning-code */
1775 }
1776 }
1777
1778 optptr += strlen(optptr);
1779 }
1780 }
1781
1782 /*
1783 * Build the command-line arguments for the filters. Each filter
1784 * has 6 or 7 arguments:
1785 *
1786 * argv[0] = printer
1787 * argv[1] = job ID
1788 * argv[2] = username
1789 * argv[3] = title
1790 * argv[4] = # copies
1791 * argv[5] = options
1792 * argv[6] = filename (optional; normally stdin)
1793 *
1794 * This allows legacy printer drivers that use the old System V
1795 * printing interface to be used by CUPS.
1796 */
1797
1798 sprintf(jobid, "%d", job->id);
1799 snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot,
1800 job->id, job->current_file + 1);
1801
1802 argv[0] = printer->name;
1803 argv[1] = jobid;
1804 argv[2] = job->username;
1805 argv[3] = title;
1806 argv[4] = copies;
1807 argv[5] = options;
1808 argv[6] = filename;
1809 argv[7] = NULL;
1810
1811 cupsdLogMessage(CUPSD_LOG_DEBUG,
1812 "cupsdStartJob: argv = \"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\"",
1813 argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6]);
1814
1815 /*
1816 * Create environment variable strings for the filters...
1817 */
1818
1819 attr = ippFindAttribute(job->attrs, "attributes-natural-language",
1820 IPP_TAG_LANGUAGE);
1821
1822 switch (strlen(attr->values[0].string.text))
1823 {
1824 default :
1825 /*
1826 * This is an unknown or badly formatted language code; use
1827 * the POSIX locale...
1828 */
1829
1830 strcpy(lang, "LANG=C");
1831 break;
1832
1833 case 2 :
1834 /*
1835 * Just the language code (ll)...
1836 */
1837
1838 snprintf(lang, sizeof(lang), "LANG=%s",
1839 attr->values[0].string.text);
1840 break;
1841
1842 case 5 :
1843 /*
1844 * Language and country code (ll-cc)...
1845 */
1846
1847 snprintf(lang, sizeof(lang), "LANG=%c%c_%c%c",
1848 attr->values[0].string.text[0],
1849 attr->values[0].string.text[1],
1850 toupper(attr->values[0].string.text[3] & 255),
1851 toupper(attr->values[0].string.text[4] & 255));
1852 break;
1853 }
1854
1855 attr = ippFindAttribute(job->attrs, "document-format",
1856 IPP_TAG_MIMETYPE);
1857 if (attr != NULL &&
1858 (optptr = strstr(attr->values[0].string.text, "charset=")) != NULL)
1859 snprintf(charset, sizeof(charset), "CHARSET=%s", optptr + 8);
1860 else
1861 {
1862 attr = ippFindAttribute(job->attrs, "attributes-charset",
1863 IPP_TAG_CHARSET);
1864 snprintf(charset, sizeof(charset), "CHARSET=%s",
1865 attr->values[0].string.text);
1866 }
1867
1868 snprintf(content_type, sizeof(content_type), "CONTENT_TYPE=%s/%s",
1869 job->filetypes[job->current_file]->super,
1870 job->filetypes[job->current_file]->type);
1871 snprintf(device_uri, sizeof(device_uri), "DEVICE_URI=%s", printer->device_uri);
1872 cupsdSanitizeURI(printer->device_uri, sani_uri, sizeof(sani_uri));
1873 snprintf(ppd, sizeof(ppd), "PPD=%s/ppd/%s.ppd", ServerRoot, printer->name);
1874 snprintf(printer_name, sizeof(printer_name), "PRINTER=%s", printer->name);
1875 snprintf(rip_max_cache, sizeof(rip_max_cache), "RIP_MAX_CACHE=%s", RIPCache);
1876
1877 envc = cupsdLoadEnv(envp, (int)(sizeof(envp) / sizeof(envp[0])));
1878
1879 envp[envc ++] = charset;
1880 envp[envc ++] = lang;
1881 envp[envc ++] = ppd;
1882 envp[envc ++] = rip_max_cache;
1883 envp[envc ++] = content_type;
1884 envp[envc ++] = device_uri;
1885 envp[envc ++] = printer_name;
1886
1887 if (Classification && !banner_page)
1888 {
1889 if ((attr = ippFindAttribute(job->attrs, "job-sheets",
1890 IPP_TAG_NAME)) == NULL)
1891 snprintf(classification, sizeof(classification), "CLASSIFICATION=%s",
1892 Classification);
1893 else if (attr->num_values > 1 &&
1894 strcmp(attr->values[1].string.text, "none") != 0)
1895 snprintf(classification, sizeof(classification), "CLASSIFICATION=%s",
1896 attr->values[1].string.text);
1897 else
1898 snprintf(classification, sizeof(classification), "CLASSIFICATION=%s",
1899 attr->values[0].string.text);
1900
1901 envp[envc ++] = classification;
1902 }
1903
1904 if (job->dtype & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT))
1905 {
1906 snprintf(class_name, sizeof(class_name), "CLASS=%s", job->dest);
1907 envp[envc ++] = class_name;
1908 }
1909
1910 envp[envc] = NULL;
1911
1912 for (i = 0; i < envc; i ++)
1913 if (strncmp(envp[i], "DEVICE_URI=", 11))
1914 cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdStartJob: envp[%d]=\"%s\"",
1915 i, envp[i]);
1916 else
1917 cupsdLogMessage(CUPSD_LOG_DEBUG,
1918 "cupsdStartJob: envp[%d]=\"DEVICE_URI=%s\"", i, sani_uri);
1919
1920 job->current_file ++;
1921
1922 /*
1923 * Now create processes for all of the filters...
1924 */
1925
1926 if (cupsdOpenPipe(statusfds))
1927 {
1928 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to create job status pipes - %s.",
1929 strerror(errno));
1930 snprintf(printer->state_message, sizeof(printer->state_message),
1931 "Unable to create status pipes - %s.", strerror(errno));
1932
1933 cupsdAddPrinterHistory(printer);
1934
1935 if (filters != NULL)
1936 free(filters);
1937
1938 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
1939 "Job canceled because the server could not create the job status pipes.");
1940
1941 cupsdCancelJob(job, 0);
1942 return;
1943 }
1944
1945 cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdStartJob: statusfds = [ %d %d ]",
1946 statusfds[0], statusfds[1]);
1947
1948#ifdef FD_CLOEXEC
1949 fcntl(statusfds[0], F_SETFD, FD_CLOEXEC);
1950 fcntl(statusfds[1], F_SETFD, FD_CLOEXEC);
1951#endif /* FD_CLOEXEC */
1952
1953 job->status_buffer = cupsdStatBufNew(statusfds[0], "[Job %d]",
1954 job->id);
1955 job->status = 0;
1956 memset(job->filters, 0, sizeof(job->filters));
1957
1958 filterfds[1][0] = open("/dev/null", O_RDONLY);
1959 filterfds[1][1] = -1;
1960
1961 if (filterfds[1][0] < 0)
1962 {
1963 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to open \"/dev/null\" - %s.",
1964 strerror(errno));
1965 snprintf(printer->state_message, sizeof(printer->state_message),
1966 "Unable to open \"/dev/null\" - %s.", strerror(errno));
1967
1968 cupsdAddPrinterHistory(printer);
1969
1970 if (filters != NULL)
1971 free(filters);
1972
1973 cupsdClosePipe(statusfds);
1974 cupsdCancelJob(job, 0);
1975 return;
1976 }
1977
1978 fcntl(filterfds[1][0], F_SETFD, fcntl(filterfds[1][0], F_GETFD) | FD_CLOEXEC);
1979
1980 cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdStartJob: filterfds[%d] = [ %d %d ]",
1981 1, filterfds[1][0], filterfds[1][1]);
1982
fa73b229 1983 for (i = 0, slot = 0, filter = (mime_filter_t *)cupsArrayFirst(filters);
1984 filter;
1985 i ++, filter = (mime_filter_t *)cupsArrayNext(filters))
ef416fc2 1986 {
fa73b229 1987 if (filter->filter[0] != '/')
ef416fc2 1988 snprintf(command, sizeof(command), "%s/filter/%s", ServerBin,
fa73b229 1989 filter->filter);
ef416fc2 1990 else
fa73b229 1991 strlcpy(command, filter->filter, sizeof(command));
ef416fc2 1992
fa73b229 1993 if (i < (cupsArrayCount(filters) - 1))
ef416fc2 1994 {
1995 if (cupsdOpenPipe(filterfds[slot]))
1996 {
1997 cupsdLogMessage(CUPSD_LOG_ERROR,
1998 "Unable to create job filter pipes - %s.",
1999 strerror(errno));
2000 snprintf(printer->state_message, sizeof(printer->state_message),
2001 "Unable to create filter pipes - %s.", strerror(errno));
2002 cupsdAddPrinterHistory(printer);
2003
fa73b229 2004 cupsArrayDelete(filters);
ef416fc2 2005
2006 cupsdClosePipe(statusfds);
2007 cupsdClosePipe(filterfds[!slot]);
2008
2009 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
2010 "Job canceled because the server could not create the filter pipes.");
2011
2012 cupsdCancelJob(job, 0);
2013 return;
2014 }
2015 }
2016 else
2017 {
2018 if (job->current_file == 1)
2019 {
2020 if (strncmp(printer->device_uri, "file:", 5) != 0)
2021 {
2022 if (cupsdOpenPipe(job->print_pipes))
2023 {
2024 cupsdLogMessage(CUPSD_LOG_ERROR,
2025 "Unable to create job backend pipes - %s.",
2026 strerror(errno));
2027 snprintf(printer->state_message, sizeof(printer->state_message),
2028 "Unable to create backend pipes - %s.", strerror(errno));
2029 cupsdAddPrinterHistory(printer);
2030
fa73b229 2031 cupsArrayDelete(filters);
ef416fc2 2032
2033 cupsdClosePipe(statusfds);
2034 cupsdClosePipe(filterfds[!slot]);
2035
2036 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
2037 "Job canceled because the server could not create the backend pipes.");
2038
2039 cupsdCancelJob(job, 0);
2040 return;
2041 }
2042 }
2043 else
2044 {
2045 job->print_pipes[0] = -1;
2046 if (!strncmp(printer->device_uri, "file:/dev/", 10) &&
2047 strcmp(printer->device_uri, "file:/dev/null"))
2048 job->print_pipes[1] = open(printer->device_uri + 5,
2049 O_WRONLY | O_EXCL);
2050 else if (!strncmp(printer->device_uri, "file:///dev/", 12) &&
2051 strcmp(printer->device_uri, "file:///dev/null"))
2052 job->print_pipes[1] = open(printer->device_uri + 7,
2053 O_WRONLY | O_EXCL);
2054 else
2055 job->print_pipes[1] = open(printer->device_uri + 5,
2056 O_WRONLY | O_CREAT | O_TRUNC, 0600);
2057
2058 if (job->print_pipes[1] < 0)
2059 {
2060 cupsdLogMessage(CUPSD_LOG_ERROR,
2061 "Unable to open output file \"%s\" - %s.",
2062 printer->device_uri, strerror(errno));
2063 snprintf(printer->state_message, sizeof(printer->state_message),
2064 "Unable to open output file \"%s\" - %s.",
2065 printer->device_uri, strerror(errno));
2066
2067 cupsdAddPrinterHistory(printer);
2068
fa73b229 2069 cupsArrayDelete(filters);
ef416fc2 2070
2071 cupsdClosePipe(statusfds);
2072 cupsdClosePipe(filterfds[!slot]);
2073
2074 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
2075 "Job canceled because the server could not open the output file.");
2076
2077 cupsdCancelJob(job, 0);
2078 return;
2079 }
2080
2081 fcntl(job->print_pipes[1], F_SETFD,
2082 fcntl(job->print_pipes[1], F_GETFD) | FD_CLOEXEC);
2083 }
2084
2085 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdStartJob: print_pipes = [ %d %d ]",
2086 job->print_pipes[0], job->print_pipes[1]);
2087 }
2088
2089 filterfds[slot][0] = job->print_pipes[0];
2090 filterfds[slot][1] = job->print_pipes[1];
2091 }
2092
2093 cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdStartJob: filter = \"%s\"", command);
2094 cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdStartJob: filterfds[%d] = [ %d %d ]",
2095 slot, filterfds[slot][0], filterfds[slot][1]);
2096
2097 pid = cupsdStartProcess(command, argv, envp, filterfds[!slot][0],
2098 filterfds[slot][1], statusfds[1],
2099 job->back_pipes[0], 0, job->filters + i);
2100
2101 cupsdLogMessage(CUPSD_LOG_DEBUG2,
2102 "cupsdStartJob: Closing filter pipes for slot %d [ %d %d ]...",
2103 !slot, filterfds[!slot][0], filterfds[!slot][1]);
2104
2105 cupsdClosePipe(filterfds[!slot]);
2106
2107 if (pid == 0)
2108 {
2109 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to start filter \"%s\" - %s.",
fa73b229 2110 filter->filter, strerror(errno));
ef416fc2 2111 snprintf(printer->state_message, sizeof(printer->state_message),
2112 "Unable to start filter \"%s\" - %s.",
fa73b229 2113 filter->filter, strerror(errno));
ef416fc2 2114
2115 cupsdAddPrinterHistory(printer);
2116
fa73b229 2117 cupsArrayDelete(filters);
ef416fc2 2118
2119 cupsdAddPrinterHistory(printer);
2120
2121 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
2122 "Job canceled because the server could not execute a filter.");
2123
2124 cupsdCancelJob(job, 0);
2125 return;
2126 }
2127
2128 cupsdLogMessage(CUPSD_LOG_INFO, "Started filter %s (PID %d) for job %d.",
2129 command, pid, job->id);
2130
2131 argv[6] = NULL;
2132 slot = !slot;
2133 }
2134
fa73b229 2135 cupsArrayDelete(filters);
ef416fc2 2136
2137 /*
2138 * Finally, pipe the final output into a backend process if needed...
2139 */
2140
2141 if (strncmp(printer->device_uri, "file:", 5) != 0)
2142 {
2143 if (job->current_file == 1)
2144 {
2145 sscanf(printer->device_uri, "%254[^:]", method);
2146 snprintf(command, sizeof(command), "%s/backend/%s", ServerBin, method);
2147
2148 argv[0] = sani_uri;
2149
2150 filterfds[slot][0] = -1;
2151 filterfds[slot][1] = open("/dev/null", O_WRONLY);
2152
2153 if (filterfds[slot][1] < 0)
2154 {
2155 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to open \"/dev/null\" - %s.",
2156 strerror(errno));
2157 snprintf(printer->state_message, sizeof(printer->state_message),
2158 "Unable to open \"/dev/null\" - %s.", strerror(errno));
2159
2160 cupsdAddPrinterHistory(printer);
2161
ef416fc2 2162 cupsdClosePipe(statusfds);
2163
2164 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
2165 "Job canceled because the server could not open a file.");
2166
2167 cupsdCancelJob(job, 0);
2168 return;
2169 }
2170
2171 fcntl(filterfds[slot][1], F_SETFD,
2172 fcntl(filterfds[slot][1], F_GETFD) | FD_CLOEXEC);
2173
2174 cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdStartJob: backend = \"%s\"",
2175 command);
2176 cupsdLogMessage(CUPSD_LOG_DEBUG,
2177 "cupsdStartJob: filterfds[%d] = [ %d %d ]",
2178 slot, filterfds[slot][0], filterfds[slot][1]);
2179
2180 pid = cupsdStartProcess(command, argv, envp, filterfds[!slot][0],
2181 filterfds[slot][1], statusfds[1],
2182 job->back_pipes[1], 1,
2183 &(job->backend));
2184
2185 if (pid == 0)
2186 {
2187 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to start backend \"%s\" - %s.",
2188 method, strerror(errno));
2189 snprintf(printer->state_message, sizeof(printer->state_message),
2190 "Unable to start backend \"%s\" - %s.", method, strerror(errno));
2191
2192 cupsdLogMessage(CUPSD_LOG_DEBUG2,
2193 "cupsdStartJob: Closing print pipes [ %d %d ]...",
2194 job->print_pipes[0], job->print_pipes[1]);
2195
2196 cupsdClosePipe(job->print_pipes);
2197
2198 cupsdLogMessage(CUPSD_LOG_DEBUG2,
2199 "cupsdStartJob: Closing back pipes [ %d %d ]...",
2200 job->back_pipes[0], job->back_pipes[1]);
2201
2202 cupsdClosePipe(job->back_pipes);
2203
2204 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
2205 "Job canceled because the server could not execute the backend.");
2206
2207 cupsdCancelJob(job, 0);
2208 return;
2209 }
2210 else
2211 {
2212 cupsdLogMessage(CUPSD_LOG_INFO,
2213 "Started backend %s (PID %d) for job %d.",
2214 command, pid, job->id);
2215 }
2216 }
2217
2218 if (job->current_file == job->num_files)
2219 {
2220 cupsdLogMessage(CUPSD_LOG_DEBUG2,
2221 "cupsdStartJob: Closing print pipes [ %d %d ]...",
2222 job->print_pipes[0], job->print_pipes[1]);
2223
2224 cupsdClosePipe(job->print_pipes);
2225
2226 cupsdLogMessage(CUPSD_LOG_DEBUG2,
2227 "cupsdStartJob: Closing back pipes [ %d %d ]...",
2228 job->back_pipes[0], job->back_pipes[1]);
2229
2230 cupsdClosePipe(job->back_pipes);
2231 }
2232 }
2233 else
2234 {
2235 filterfds[slot][0] = -1;
2236 filterfds[slot][1] = -1;
2237
2238 if (job->current_file == job->num_files)
2239 {
2240 cupsdLogMessage(CUPSD_LOG_DEBUG2,
2241 "cupsdStartJob: Closing print pipes [ %d %d ]...",
2242 job->print_pipes[0], job->print_pipes[1]);
2243
2244 cupsdClosePipe(job->print_pipes);
2245 }
2246 }
2247
2248 cupsdLogMessage(CUPSD_LOG_DEBUG2,
2249 "cupsdStartJob: Closing filter pipes for slot %d [ %d %d ]...",
2250 slot, filterfds[slot][0], filterfds[slot][1]);
2251
2252 cupsdClosePipe(filterfds[slot]);
2253
2254 cupsdLogMessage(CUPSD_LOG_DEBUG2,
2255 "cupsdStartJob: Closing status output pipe %d...",
2256 statusfds[1]);
2257
2258 close(statusfds[1]);
2259
2260 cupsdLogMessage(CUPSD_LOG_DEBUG2,
2261 "cupsdStartJob: Adding fd %d to InputSet...",
2262 job->status_buffer->fd);
2263
2264 FD_SET(job->status_buffer->fd, InputSet);
2265}
2266
2267
2268/*
2269 * 'cupsdStopAllJobs()' - Stop all print jobs.
2270 */
2271
2272void
2273cupsdStopAllJobs(void)
2274{
2275 cupsd_job_t *job; /* Current job */
2276
2277
2278 DEBUG_puts("cupsdStopAllJobs()");
2279
2280 for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs);
2281 job;
2282 job = (cupsd_job_t *)cupsArrayNext(ActiveJobs))
2283 if (job->state->values[0].integer == IPP_JOB_PROCESSING)
2284 {
2285 cupsdStopJob(job, 1);
2286 job->state->values[0].integer = IPP_JOB_PENDING;
2287 }
2288}
2289
2290
2291/*
2292 * 'cupsdStopJob()' - Stop a print job.
2293 */
2294
2295void
2296cupsdStopJob(cupsd_job_t *job, /* I - Job */
2297 int force) /* I - 1 = Force all filters to stop */
2298{
2299 int i; /* Looping var */
2300
2301
2302 cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdStopJob: id = %d, force = %d",
2303 job->id, force);
2304
2305 if (job->state->values[0].integer != IPP_JOB_PROCESSING)
2306 return;
2307
2308 FilterLevel -= job->cost;
2309
2310 if (job->status < 0 &&
2311 !(job->dtype & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT)) &&
2312 !(job->printer->type & CUPS_PRINTER_FAX) &&
2313 !strcmp(job->printer->error_policy, "stop-printer"))
2314 cupsdSetPrinterState(job->printer, IPP_PRINTER_STOPPED, 1);
2315 else if (job->printer->state != IPP_PRINTER_STOPPED)
2316 cupsdSetPrinterState(job->printer, IPP_PRINTER_IDLE, 0);
2317
2318 cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdStopJob: printer state is %d",
2319 job->printer->state);
2320
2321 job->state->values[0].integer = IPP_JOB_STOPPED;
2322 job->printer->job = NULL;
2323 job->printer = NULL;
2324
2325 job->current_file --;
2326
2327 for (i = 0; job->filters[i]; i ++)
2328 if (job->filters[i] > 0)
2329 {
2330 cupsdEndProcess(job->filters[i], force);
2331 job->filters[i] = 0;
2332 }
2333
2334 if (job->backend > 0)
2335 {
2336 cupsdEndProcess(job->backend, force);
2337 job->backend = 0;
2338 }
2339
2340 cupsdLogMessage(CUPSD_LOG_DEBUG2,
2341 "cupsdStopJob: Closing print pipes [ %d %d ]...",
2342 job->print_pipes[0], job->print_pipes[1]);
2343
2344 cupsdClosePipe(job->print_pipes);
2345
2346 cupsdLogMessage(CUPSD_LOG_DEBUG2,
2347 "cupsdStopJob: Closing back pipes [ %d %d ]...",
2348 job->back_pipes[0], job->back_pipes[1]);
2349
2350 cupsdClosePipe(job->back_pipes);
2351
2352 if (job->status_buffer)
2353 {
2354 /*
2355 * Close the pipe and clear the input bit.
2356 */
2357
2358 cupsdLogMessage(CUPSD_LOG_DEBUG2,
2359 "cupsdStopJob: Removing fd %d from InputSet...",
2360 job->status_buffer->fd);
2361
2362 FD_CLR(job->status_buffer->fd, InputSet);
2363
2364 cupsdLogMessage(CUPSD_LOG_DEBUG2,
2365 "cupsdStopJob: Closing status input pipe %d...",
2366 job->status_buffer->fd);
2367
2368 cupsdStatBufDelete(job->status_buffer);
2369
2370 job->status_buffer = NULL;
2371 }
2372}
2373
2374
2375/*
2376 * 'cupsdUpdateJob()' - Read a status update from a job's filters.
2377 */
2378
2379void
2380cupsdUpdateJob(cupsd_job_t *job) /* I - Job to check */
2381{
2382 int i; /* Looping var */
2383 int copies; /* Number of copies printed */
2384 char message[1024], /* Message text */
2385 *ptr; /* Pointer update... */
2386 int loglevel; /* Log level for message */
2387
2388
2389 while ((ptr = cupsdStatBufUpdate(job->status_buffer, &loglevel,
2390 message, sizeof(message))) != NULL)
2391 {
2392 /*
2393 * Process page and printer state messages as needed...
2394 */
2395
2396 if (loglevel == CUPSD_LOG_PAGE)
2397 {
2398 /*
2399 * Page message; send the message to the page_log file and update the
2400 * job sheet count...
2401 */
2402
2403 if (job->sheets != NULL)
2404 {
2405 if (!strncasecmp(message, "total ", 6))
2406 {
2407 /*
2408 * Got a total count of pages from a backend or filter...
2409 */
2410
2411 copies = atoi(message + 6);
2412 copies -= job->sheets->values[0].integer; /* Just track the delta */
2413 }
2414 else if (!sscanf(message, "%*d%d", &copies))
2415 copies = 1;
2416
2417 job->sheets->values[0].integer += copies;
2418
2419 if (job->printer->page_limit)
2420 cupsdUpdateQuota(job->printer, job->username, copies, 0);
2421 }
2422
2423 cupsdLogPage(job, message);
2424
2425 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS, job->printer, job,
2426 "Printed %d page(s).", job->sheets->values[0].integer);
2427 }
2428 else if (loglevel == CUPSD_LOG_STATE)
2429 cupsdSetPrinterReasons(job->printer, message);
2430 else if (loglevel == CUPSD_LOG_ATTR)
2431 {
2432 /*
2433 * Set attribute(s)...
2434 */
2435
2436 /**** TODO ****/
2437 }
2438
2439 if (!strchr(job->status_buffer->buffer, '\n'))
2440 break;
2441 }
2442
2443 if (ptr == NULL)
2444 {
2445 /*
2446 * See if all of the filters and the backend have returned their
2447 * exit statuses.
2448 */
2449
2450 for (i = 0; job->filters[i] < 0; i ++);
2451
2452 if (job->filters[i])
2453 return;
2454
2455 if (job->current_file >= job->num_files && job->backend > 0)
2456 return;
2457
2458 /*
2459 * Handle the end of job stuff...
2460 */
2461
2462 cupsdFinishJob(job);
2463 }
2464}
2465
2466
2467/*
2468 * 'compare_active_jobs()' - Compare the job IDs and priorities of two jobs.
2469 */
2470
2471static int /* O - Difference */
2472compare_active_jobs(void *first, /* I - First job */
2473 void *second, /* I - Second job */
2474 void *data) /* I - App data (not used) */
2475{
2476 int diff; /* Difference */
2477
2478
2479 if ((diff = ((cupsd_job_t *)first)->priority - ((cupsd_job_t *)second)->priority) != 0)
2480 return (diff);
2481 else
2482 return (((cupsd_job_t *)first)->id - ((cupsd_job_t *)second)->id);
2483}
2484
2485
2486/*
2487 * 'compare_jobs()' - Compare the job IDs of two jobs.
2488 */
2489
2490static int /* O - Difference */
2491compare_jobs(void *first, /* I - First job */
2492 void *second, /* I - Second job */
2493 void *data) /* I - App data (not used) */
2494{
2495 return (((cupsd_job_t *)first)->id - ((cupsd_job_t *)second)->id);
2496}
2497
2498
2499/*
2500 * 'ipp_length()' - Compute the size of the buffer needed to hold
2501 * the textual IPP attributes.
2502 */
2503
2504int /* O - Size of buffer to hold IPP attributes */
2505ipp_length(ipp_t *ipp) /* I - IPP request */
2506{
2507 int bytes; /* Number of bytes */
2508 int i; /* Looping var */
2509 ipp_attribute_t *attr; /* Current attribute */
2510
2511
2512 /*
2513 * Loop through all attributes...
2514 */
2515
2516 bytes = 0;
2517
2518 for (attr = ipp->attrs; attr != NULL; attr = attr->next)
2519 {
2520 /*
2521 * Skip attributes that won't be sent to filters...
2522 */
2523
2524 if (attr->value_tag == IPP_TAG_MIMETYPE ||
2525 attr->value_tag == IPP_TAG_NAMELANG ||
2526 attr->value_tag == IPP_TAG_TEXTLANG ||
2527 attr->value_tag == IPP_TAG_URI ||
2528 attr->value_tag == IPP_TAG_URISCHEME)
2529 continue;
2530
2531 if (strncmp(attr->name, "time-", 5) == 0)
2532 continue;
2533
2534 /*
2535 * Add space for a leading space and commas between each value.
2536 * For the first attribute, the leading space isn't used, so the
2537 * extra byte can be used as the nul terminator...
2538 */
2539
2540 bytes ++; /* " " separator */
2541 bytes += attr->num_values; /* "," separators */
2542
2543 /*
2544 * Boolean attributes appear as "foo,nofoo,foo,nofoo", while
2545 * other attributes appear as "foo=value1,value2,...,valueN".
2546 */
2547
2548 if (attr->value_tag != IPP_TAG_BOOLEAN)
2549 bytes += strlen(attr->name);
2550 else
2551 bytes += attr->num_values * strlen(attr->name);
2552
2553 /*
2554 * Now add the size required for each value in the attribute...
2555 */
2556
2557 switch (attr->value_tag)
2558 {
2559 case IPP_TAG_INTEGER :
2560 case IPP_TAG_ENUM :
2561 /*
2562 * Minimum value of a signed integer is -2147483647, or 11 digits.
2563 */
2564
2565 bytes += attr->num_values * 11;
2566 break;
2567
2568 case IPP_TAG_BOOLEAN :
2569 /*
2570 * Add two bytes for each false ("no") value...
2571 */
2572
2573 for (i = 0; i < attr->num_values; i ++)
2574 if (!attr->values[i].boolean)
2575 bytes += 2;
2576 break;
2577
2578 case IPP_TAG_RANGE :
2579 /*
2580 * A range is two signed integers separated by a hyphen, or
2581 * 23 characters max.
2582 */
2583
2584 bytes += attr->num_values * 23;
2585 break;
2586
2587 case IPP_TAG_RESOLUTION :
2588 /*
2589 * A resolution is two signed integers separated by an "x" and
2590 * suffixed by the units, or 26 characters max.
2591 */
2592
2593 bytes += attr->num_values * 26;
2594 break;
2595
2596 case IPP_TAG_STRING :
2597 case IPP_TAG_TEXT :
2598 case IPP_TAG_NAME :
2599 case IPP_TAG_KEYWORD :
2600 case IPP_TAG_CHARSET :
2601 case IPP_TAG_LANGUAGE :
2602 /*
2603 * Strings can contain characters that need quoting. We need
2604 * at least 2 * len + 2 characters to cover the quotes and
2605 * any backslashes in the string.
2606 */
2607
2608 for (i = 0; i < attr->num_values; i ++)
2609 bytes += 2 * strlen(attr->values[i].string.text) + 2;
2610 break;
2611
2612 default :
2613 break; /* anti-compiler-warning-code */
2614 }
2615 }
2616
2617 return (bytes);
2618}
2619
2620
2621/*
2622 * 'set_time()' - Set one of the "time-at-xyz" attributes...
2623 */
2624
2625static void
2626set_time(cupsd_job_t *job, /* I - Job to update */
2627 const char *name) /* I - Name of attribute */
2628{
2629 ipp_attribute_t *attr; /* Time attribute */
2630
2631
2632 if ((attr = ippFindAttribute(job->attrs, name, IPP_TAG_ZERO)) != NULL)
2633 {
2634 attr->value_tag = IPP_TAG_INTEGER;
2635 attr->values[0].integer = time(NULL);
2636 }
2637}
2638
2639
2640/*
2641 * 'set_hold_until()' - Set the hold time and update job-hold-until attribute...
2642 */
2643
2644static void
2645set_hold_until(cupsd_job_t *job, /* I - Job to update */
2646 time_t holdtime) /* I - Hold until time */
2647{
2648 ipp_attribute_t *attr; /* job-hold-until attribute */
2649 struct tm *holddate; /* Hold date */
2650 char holdstr[64]; /* Hold time */
2651
2652
2653 /*
2654 * Set the hold_until value and hold the job...
2655 */
2656
2657 cupsdLogMessage(CUPSD_LOG_DEBUG, "set_hold_until: hold_until = %d", (int)holdtime);
2658
2659 job->state->values[0].integer = IPP_JOB_HELD;
2660 job->hold_until = holdtime;
2661
2662 /*
2663 * Update the job-hold-until attribute with a string representing GMT
2664 * time (HH:MM:SS)...
2665 */
2666
2667 holddate = gmtime(&holdtime);
2668 snprintf(holdstr, sizeof(holdstr), "%d:%d:%d", holddate->tm_hour,
2669 holddate->tm_min, holddate->tm_sec);
2670
2671 if ((attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_KEYWORD)) == NULL)
2672 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
2673
2674 /*
2675 * Either add the attribute or update the value of the existing one
2676 */
2677
2678 if (attr == NULL)
2679 attr = ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_KEYWORD,
2680 "job-hold-until", NULL, holdstr);
2681 else
2682 cupsdSetString(&attr->values[0].string.text, holdstr);
2683
2684 cupsdSaveJob(job);
2685}
2686
2687
2688/*
a4d04587 2689 * End of "$Id: job.c 5023 2006-01-29 14:39:44Z mike $".
ef416fc2 2690 */