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