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