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