]> 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/*
2abf387c 2 * "$Id: job.c 6032 2006-10-12 19:19:47Z 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);
ef416fc2 1778 }
ef416fc2 1779
e1d6a774 1780 if (!strchr(job->status_buffer->buffer, '\n'))
1781 break;
1782 }
ef416fc2 1783
07725fee 1784 if ((event & CUPSD_EVENT_PRINTER_STATE_CHANGED))
1785 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE_CHANGED, job->printer, NULL,
1786 (job->printer->type & CUPS_PRINTER_CLASS) ?
1787 "Class \"%s\" state changed." :
1788 "Printer \"%s\" state changed.",
1789 job->printer->name);
1790
d09495fa 1791 if (ptr == NULL && !job->status_buffer->bufused)
e1d6a774 1792 {
1793 /*
1794 * See if all of the filters and the backend have returned their
1795 * exit statuses.
1796 */
ef416fc2 1797
e1d6a774 1798 for (i = 0; job->filters[i] < 0; i ++);
ef416fc2 1799
e1d6a774 1800 if (job->filters[i])
1801 return;
ef416fc2 1802
e1d6a774 1803 if (job->current_file >= job->num_files && job->backend > 0)
1804 return;
ef416fc2 1805
e1d6a774 1806 /*
1807 * Handle the end of job stuff...
1808 */
ef416fc2 1809
e1d6a774 1810 cupsdFinishJob(job);
1811 }
1812}
ef416fc2 1813
ef416fc2 1814
e1d6a774 1815/*
1816 * 'compare_active_jobs()' - Compare the job IDs and priorities of two jobs.
1817 */
ef416fc2 1818
e1d6a774 1819static int /* O - Difference */
1820compare_active_jobs(void *first, /* I - First job */
1821 void *second, /* I - Second job */
1822 void *data) /* I - App data (not used) */
1823{
1824 int diff; /* Difference */
ef416fc2 1825
ef416fc2 1826
8ca02f3c 1827 if ((diff = ((cupsd_job_t *)second)->priority -
1828 ((cupsd_job_t *)first)->priority) != 0)
e1d6a774 1829 return (diff);
1830 else
1831 return (((cupsd_job_t *)first)->id - ((cupsd_job_t *)second)->id);
1832}
ef416fc2 1833
ef416fc2 1834
e1d6a774 1835/*
1836 * 'compare_jobs()' - Compare the job IDs of two jobs.
1837 */
ef416fc2 1838
e1d6a774 1839static int /* O - Difference */
1840compare_jobs(void *first, /* I - First job */
1841 void *second, /* I - Second job */
1842 void *data) /* I - App data (not used) */
1843{
1844 return (((cupsd_job_t *)first)->id - ((cupsd_job_t *)second)->id);
1845}
ef416fc2 1846
ef416fc2 1847
e1d6a774 1848/*
1849 * 'free_job()' - Free all memory used by a job.
1850 */
ef416fc2 1851
e1d6a774 1852static void
1853free_job(cupsd_job_t *job) /* I - Job */
1854{
1855 cupsdClearString(&job->username);
1856 cupsdClearString(&job->dest);
ef416fc2 1857
e1d6a774 1858 if (job->num_files > 0)
1859 {
1860 free(job->compressions);
1861 free(job->filetypes);
ef416fc2 1862 }
1863
e1d6a774 1864 ippDelete(job->attrs);
ef416fc2 1865
e1d6a774 1866 free(job);
1867}
bd7854cb 1868
ef416fc2 1869
e1d6a774 1870/*
1871 * 'ipp_length()' - Compute the size of the buffer needed to hold
1872 * the textual IPP attributes.
1873 */
ef416fc2 1874
e1d6a774 1875static int /* O - Size of attribute buffer */
1876ipp_length(ipp_t *ipp) /* I - IPP request */
1877{
1878 int bytes; /* Number of bytes */
1879 int i; /* Looping var */
1880 ipp_attribute_t *attr; /* Current attribute */
bd7854cb 1881
ef416fc2 1882
1883 /*
e1d6a774 1884 * Loop through all attributes...
ef416fc2 1885 */
1886
e1d6a774 1887 bytes = 0;
ef416fc2 1888
e1d6a774 1889 for (attr = ipp->attrs; attr != NULL; attr = attr->next)
ef416fc2 1890 {
e1d6a774 1891 /*
1892 * Skip attributes that won't be sent to filters...
1893 */
ef416fc2 1894
e1d6a774 1895 if (attr->value_tag == IPP_TAG_MIMETYPE ||
1896 attr->value_tag == IPP_TAG_NAMELANG ||
1897 attr->value_tag == IPP_TAG_TEXTLANG ||
1898 attr->value_tag == IPP_TAG_URI ||
1899 attr->value_tag == IPP_TAG_URISCHEME)
1900 continue;
ef416fc2 1901
e1d6a774 1902 if (strncmp(attr->name, "time-", 5) == 0)
1903 continue;
ef416fc2 1904
e1d6a774 1905 /*
1906 * Add space for a leading space and commas between each value.
1907 * For the first attribute, the leading space isn't used, so the
1908 * extra byte can be used as the nul terminator...
1909 */
ef416fc2 1910
e1d6a774 1911 bytes ++; /* " " separator */
1912 bytes += attr->num_values; /* "," separators */
ef416fc2 1913
e1d6a774 1914 /*
1915 * Boolean attributes appear as "foo,nofoo,foo,nofoo", while
1916 * other attributes appear as "foo=value1,value2,...,valueN".
1917 */
ef416fc2 1918
e1d6a774 1919 if (attr->value_tag != IPP_TAG_BOOLEAN)
1920 bytes += strlen(attr->name);
1921 else
1922 bytes += attr->num_values * strlen(attr->name);
ef416fc2 1923
e1d6a774 1924 /*
1925 * Now add the size required for each value in the attribute...
1926 */
ef416fc2 1927
e1d6a774 1928 switch (attr->value_tag)
1929 {
1930 case IPP_TAG_INTEGER :
1931 case IPP_TAG_ENUM :
1932 /*
1933 * Minimum value of a signed integer is -2147483647, or 11 digits.
1934 */
ef416fc2 1935
e1d6a774 1936 bytes += attr->num_values * 11;
1937 break;
ef416fc2 1938
e1d6a774 1939 case IPP_TAG_BOOLEAN :
1940 /*
1941 * Add two bytes for each false ("no") value...
1942 */
ef416fc2 1943
e1d6a774 1944 for (i = 0; i < attr->num_values; i ++)
1945 if (!attr->values[i].boolean)
1946 bytes += 2;
1947 break;
ef416fc2 1948
e1d6a774 1949 case IPP_TAG_RANGE :
1950 /*
1951 * A range is two signed integers separated by a hyphen, or
1952 * 23 characters max.
1953 */
ef416fc2 1954
e1d6a774 1955 bytes += attr->num_values * 23;
1956 break;
ef416fc2 1957
e1d6a774 1958 case IPP_TAG_RESOLUTION :
1959 /*
1960 * A resolution is two signed integers separated by an "x" and
1961 * suffixed by the units, or 26 characters max.
1962 */
ef416fc2 1963
e1d6a774 1964 bytes += attr->num_values * 26;
1965 break;
ef416fc2 1966
e1d6a774 1967 case IPP_TAG_STRING :
1968 case IPP_TAG_TEXT :
1969 case IPP_TAG_NAME :
1970 case IPP_TAG_KEYWORD :
1971 case IPP_TAG_CHARSET :
1972 case IPP_TAG_LANGUAGE :
1973 case IPP_TAG_URI :
1974 /*
1975 * Strings can contain characters that need quoting. We need
1976 * at least 2 * len + 2 characters to cover the quotes and
1977 * any backslashes in the string.
1978 */
ef416fc2 1979
e1d6a774 1980 for (i = 0; i < attr->num_values; i ++)
1981 bytes += 2 * strlen(attr->values[i].string.text) + 2;
1982 break;
bd7854cb 1983
e1d6a774 1984 default :
1985 break; /* anti-compiler-warning-code */
bd7854cb 1986 }
ef416fc2 1987 }
1988
e1d6a774 1989 return (bytes);
1990}
ef416fc2 1991
ef416fc2 1992
e1d6a774 1993/*
1994 * 'load_job_cache()' - Load jobs from the job.cache file.
1995 */
ef416fc2 1996
e1d6a774 1997static void
1998load_job_cache(const char *filename) /* I - job.cache filename */
1999{
2000 cups_file_t *fp; /* job.cache file */
2001 char line[1024], /* Line buffer */
2002 *value; /* Value on line */
2003 int linenum; /* Line number in file */
2004 cupsd_job_t *job; /* Current job */
2005 int jobid; /* Job ID */
2006 char jobfile[1024]; /* Job filename */
ef416fc2 2007
ef416fc2 2008
e1d6a774 2009 /*
2010 * Open the job.cache file...
2011 */
bd7854cb 2012
e1d6a774 2013 if ((fp = cupsFileOpen(filename, "r")) == NULL)
2014 {
2015 if (errno != ENOENT)
2016 cupsdLogMessage(CUPSD_LOG_ERROR,
2017 "Unable to open job cache file \"%s\": %s",
2018 filename, strerror(errno));
bd7854cb 2019
e1d6a774 2020 load_request_root();
bd7854cb 2021
ef416fc2 2022 return;
2023 }
2024
e1d6a774 2025 /*
2026 * Read entries from the job cache file and create jobs as needed.
2027 */
ef416fc2 2028
e1d6a774 2029 cupsdLogMessage(CUPSD_LOG_INFO, "Loading job cache file \"%s\"...",
2030 filename);
ef416fc2 2031
e1d6a774 2032 linenum = 0;
2033 job = NULL;
ef416fc2 2034
e1d6a774 2035 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
2036 {
2037 if (!strcasecmp(line, "NextJobId"))
ef416fc2 2038 {
e1d6a774 2039 if (value)
2040 NextJobId = atoi(value);
2041 }
2042 else if (!strcasecmp(line, "<Job"))
2043 {
2044 if (job)
ef416fc2 2045 {
e1d6a774 2046 cupsdLogMessage(CUPSD_LOG_ERROR, "Missing </Job> directive on line %d!",
2047 linenum);
2048 continue;
2049 }
ef416fc2 2050
e1d6a774 2051 if (!value)
2052 {
2053 cupsdLogMessage(CUPSD_LOG_ERROR, "Missing job ID on line %d!", linenum);
2054 continue;
2055 }
bd7854cb 2056
e1d6a774 2057 jobid = atoi(value);
bd7854cb 2058
e1d6a774 2059 if (jobid < 1)
2060 {
2061 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad job ID %d on line %d!", jobid,
2062 linenum);
2063 continue;
2064 }
ef416fc2 2065
e1d6a774 2066 snprintf(jobfile, sizeof(jobfile), "%s/c%05d", RequestRoot, jobid);
2067 if (access(jobfile, 0))
2068 {
2069 cupsdLogMessage(CUPSD_LOG_ERROR, "Job %d files have gone away!", jobid);
2070 continue;
ef416fc2 2071 }
e1d6a774 2072
2073 job = calloc(1, sizeof(cupsd_job_t));
2074 if (!job)
ef416fc2 2075 {
e1d6a774 2076 cupsdLogMessage(CUPSD_LOG_EMERG,
2077 "Unable to allocate memory for job %d!", jobid);
2078 break;
2079 }
ef416fc2 2080
89d46774 2081 job->id = jobid;
2082 job->back_pipes[0] = -1;
2083 job->back_pipes[1] = -1;
2084 job->print_pipes[0] = -1;
2085 job->print_pipes[1] = -1;
2086 job->status_pipes[0] = -1;
2087 job->status_pipes[1] = -1;
ef416fc2 2088
e1d6a774 2089 cupsdLogMessage(CUPSD_LOG_DEBUG, "Loading job %d from cache...", job->id);
2090 }
2091 else if (!job)
2092 {
2093 cupsdLogMessage(CUPSD_LOG_ERROR,
2094 "Missing <Job #> directive on line %d!", linenum);
2095 continue;
2096 }
2097 else if (!strcasecmp(line, "</Job>"))
2098 {
2099 cupsArrayAdd(Jobs, job);
ef416fc2 2100
f301802f 2101 if (job->state_value <= IPP_JOB_STOPPED)
e1d6a774 2102 {
2103 cupsArrayAdd(ActiveJobs, job);
2104 cupsdLoadJob(job);
2105 }
bd7854cb 2106
e1d6a774 2107 job = NULL;
2108 }
2109 else if (!value)
2110 {
2111 cupsdLogMessage(CUPSD_LOG_ERROR, "Missing value on line %d!", linenum);
2112 continue;
2113 }
2114 else if (!strcasecmp(line, "State"))
2115 {
d09495fa 2116 job->state_value = (ipp_jstate_t)atoi(value);
e1d6a774 2117
2118 if (job->state_value < IPP_JOB_PENDING)
2119 job->state_value = IPP_JOB_PENDING;
2120 else if (job->state_value > IPP_JOB_COMPLETED)
2121 job->state_value = IPP_JOB_COMPLETED;
2122 }
2123 else if (!strcasecmp(line, "Priority"))
2124 {
2125 job->priority = atoi(value);
2126 }
2127 else if (!strcasecmp(line, "Username"))
2128 {
2129 cupsdSetString(&job->username, value);
2130 }
2131 else if (!strcasecmp(line, "Destination"))
2132 {
2133 cupsdSetString(&job->dest, value);
2134 }
2135 else if (!strcasecmp(line, "DestType"))
2136 {
2137 job->dtype = (cups_ptype_t)atoi(value);
2138 }
2139 else if (!strcasecmp(line, "NumFiles"))
2140 {
2141 job->num_files = atoi(value);
bd7854cb 2142
e1d6a774 2143 if (job->num_files < 0)
2144 {
2145 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad NumFiles value %d on line %d!",
2146 job->num_files, linenum);
2147 job->num_files = 0;
2148 continue;
2149 }
ef416fc2 2150
e1d6a774 2151 if (job->num_files > 0)
2152 {
2153 snprintf(jobfile, sizeof(jobfile), "%s/d%05d-001", RequestRoot,
2154 job->id);
2155 if (access(jobfile, 0))
2156 {
2157 cupsdLogMessage(CUPSD_LOG_INFO,
2158 "Data files for job %d have gone away!", job->id);
2159 job->num_files = 0;
2160 continue;
ef416fc2 2161 }
e1d6a774 2162
2163 job->filetypes = calloc(job->num_files, sizeof(mime_type_t *));
2164 job->compressions = calloc(job->num_files, sizeof(int));
2165
2166 if (!job->filetypes || !job->compressions)
ef416fc2 2167 {
e1d6a774 2168 cupsdLogMessage(CUPSD_LOG_EMERG,
2169 "Unable to allocate memory for %d files!",
2170 job->num_files);
2171 break;
2172 }
2173 }
2174 }
2175 else if (!strcasecmp(line, "File"))
2176 {
2177 int number, /* File number */
2178 compression; /* Compression value */
2179 char super[MIME_MAX_SUPER], /* MIME super type */
2180 type[MIME_MAX_TYPE]; /* MIME type */
ef416fc2 2181
ef416fc2 2182
e1d6a774 2183 if (sscanf(value, "%d%*[ \t]%15[^/]/%255s%d", &number, super, type,
2184 &compression) != 4)
2185 {
2186 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad File on line %d!", linenum);
2187 continue;
2188 }
ef416fc2 2189
e1d6a774 2190 if (number < 1 || number > job->num_files)
2191 {
2192 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad File number %d on line %d!",
2193 number, linenum);
2194 continue;
2195 }
ef416fc2 2196
e1d6a774 2197 number --;
ef416fc2 2198
e1d6a774 2199 job->compressions[number] = compression;
2200 job->filetypes[number] = mimeType(MimeDatabase, super, type);
bd7854cb 2201
e1d6a774 2202 if (!job->filetypes[number])
2203 {
2204 /*
2205 * If the original MIME type is unknown, auto-type it!
2206 */
bd7854cb 2207
e1d6a774 2208 cupsdLogMessage(CUPSD_LOG_ERROR,
2209 "Unknown MIME type %s/%s for file %d of job %d!",
2210 super, type, number + 1, job->id);
ef416fc2 2211
e1d6a774 2212 snprintf(jobfile, sizeof(jobfile), "%s/d%05d-%03d", RequestRoot,
2213 job->id, number + 1);
2214 job->filetypes[number] = mimeFileType(MimeDatabase, jobfile, NULL,
2215 job->compressions + number);
ef416fc2 2216
e1d6a774 2217 /*
2218 * If that didn't work, assume it is raw...
2219 */
ef416fc2 2220
e1d6a774 2221 if (!job->filetypes[number])
2222 job->filetypes[number] = mimeType(MimeDatabase, "application",
2223 "vnd.cups-raw");
ef416fc2 2224 }
ef416fc2 2225 }
e1d6a774 2226 else
2227 cupsdLogMessage(CUPSD_LOG_ERROR, "Unknown %s directive on line %d!",
2228 line, linenum);
2229 }
ef416fc2 2230
e1d6a774 2231 cupsFileClose(fp);
2232}
ef416fc2 2233
ef416fc2 2234
e1d6a774 2235/*
2236 * 'load_next_job_id()' - Load the NextJobId value from the job.cache file.
2237 */
ef416fc2 2238
e1d6a774 2239static void
2240load_next_job_id(const char *filename) /* I - job.cache filename */
2241{
2242 cups_file_t *fp; /* job.cache file */
2243 char line[1024], /* Line buffer */
2244 *value; /* Value on line */
2245 int linenum; /* Line number in file */
89d46774 2246 int next_job_id; /* NextJobId value from line */
ef416fc2 2247
ef416fc2 2248
e1d6a774 2249 /*
2250 * Read the NextJobId directive from the job.cache file and use
2251 * the value (if any).
2252 */
ef416fc2 2253
e1d6a774 2254 if ((fp = cupsFileOpen(filename, "r")) == NULL)
2255 {
2256 if (errno != ENOENT)
2257 cupsdLogMessage(CUPSD_LOG_ERROR,
2258 "Unable to open job cache file \"%s\": %s",
2259 filename, strerror(errno));
ef416fc2 2260
e1d6a774 2261 return;
2262 }
ef416fc2 2263
e1d6a774 2264 cupsdLogMessage(CUPSD_LOG_INFO,
2265 "Loading NextJobId from job cache file \"%s\"...", filename);
bd7854cb 2266
e1d6a774 2267 linenum = 0;
bd7854cb 2268
e1d6a774 2269 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
2270 {
2271 if (!strcasecmp(line, "NextJobId"))
2272 {
2273 if (value)
89d46774 2274 {
2275 next_job_id = atoi(value);
ef416fc2 2276
89d46774 2277 if (next_job_id > NextJobId)
2278 NextJobId = next_job_id;
2279 }
e1d6a774 2280 break;
ef416fc2 2281 }
e1d6a774 2282 }
ef416fc2 2283
e1d6a774 2284 cupsFileClose(fp);
2285}
ef416fc2 2286
ef416fc2 2287
e1d6a774 2288/*
2289 * 'load_request_root()' - Load jobs from the RequestRoot directory.
2290 */
2291
2292static void
2293load_request_root(void)
2294{
2295 cups_dir_t *dir; /* Directory */
2296 cups_dentry_t *dent; /* Directory entry */
2297 cupsd_job_t *job; /* New job */
2298
ef416fc2 2299
2300 /*
e1d6a774 2301 * Open the requests directory...
ef416fc2 2302 */
2303
e1d6a774 2304 cupsdLogMessage(CUPSD_LOG_DEBUG, "Scanning %s for jobs...", RequestRoot);
2305
2306 if ((dir = cupsDirOpen(RequestRoot)) == NULL)
ef416fc2 2307 {
e1d6a774 2308 cupsdLogMessage(CUPSD_LOG_ERROR,
2309 "Unable to open spool directory \"%s\": %s",
2310 RequestRoot, strerror(errno));
2311 return;
2312 }
2313
2314 /*
2315 * Read all the c##### files...
2316 */
ef416fc2 2317
e1d6a774 2318 while ((dent = cupsDirRead(dir)) != NULL)
2319 if (strlen(dent->filename) >= 6 && dent->filename[0] == 'c')
2320 {
e00b005a 2321 /*
e1d6a774 2322 * Allocate memory for the job...
e00b005a 2323 */
2324
e1d6a774 2325 if ((job = calloc(sizeof(cupsd_job_t), 1)) == NULL)
ef416fc2 2326 {
e1d6a774 2327 cupsdLogMessage(CUPSD_LOG_ERROR, "Ran out of memory for jobs!");
2328 cupsDirClose(dir);
ef416fc2 2329 return;
2330 }
2331
e1d6a774 2332 /*
2333 * Assign the job ID...
2334 */
ef416fc2 2335
89d46774 2336 job->id = atoi(dent->filename + 1);
2337 job->back_pipes[0] = -1;
2338 job->back_pipes[1] = -1;
2339 job->print_pipes[0] = -1;
2340 job->print_pipes[1] = -1;
2341 job->status_pipes[0] = -1;
2342 job->status_pipes[1] = -1;
ef416fc2 2343
e1d6a774 2344 if (job->id >= NextJobId)
2345 NextJobId = job->id + 1;
ef416fc2 2346
e1d6a774 2347 /*
2348 * Load the job...
2349 */
ef416fc2 2350
e1d6a774 2351 cupsdLoadJob(job);
bd7854cb 2352
e1d6a774 2353 /*
2354 * Insert the job into the array, sorting by job priority and ID...
2355 */
bd7854cb 2356
e1d6a774 2357 cupsArrayAdd(Jobs, job);
ef416fc2 2358
f301802f 2359 if (job->state_value <= IPP_JOB_STOPPED)
07725fee 2360 cupsArrayAdd(ActiveJobs, job);
ef416fc2 2361 else
e1d6a774 2362 unload_job(job);
ef416fc2 2363 }
2364
e1d6a774 2365 cupsDirClose(dir);
2366}
ef416fc2 2367
ef416fc2 2368
e1d6a774 2369/*
2370 * 'set_time()' - Set one of the "time-at-xyz" attributes...
2371 */
ef416fc2 2372
e1d6a774 2373static void
2374set_time(cupsd_job_t *job, /* I - Job to update */
2375 const char *name) /* I - Name of attribute */
2376{
2377 ipp_attribute_t *attr; /* Time attribute */
ef416fc2 2378
ef416fc2 2379
e1d6a774 2380 if ((attr = ippFindAttribute(job->attrs, name, IPP_TAG_ZERO)) != NULL)
bd7854cb 2381 {
e1d6a774 2382 attr->value_tag = IPP_TAG_INTEGER;
2383 attr->values[0].integer = time(NULL);
bd7854cb 2384 }
e1d6a774 2385}
bd7854cb 2386
ef416fc2 2387
e1d6a774 2388/*
2389 * 'set_hold_until()' - Set the hold time and update job-hold-until attribute...
2390 */
ef416fc2 2391
e1d6a774 2392static void
2393set_hold_until(cupsd_job_t *job, /* I - Job to update */
2394 time_t holdtime) /* I - Hold until time */
2395{
2396 ipp_attribute_t *attr; /* job-hold-until attribute */
2397 struct tm *holddate; /* Hold date */
2398 char holdstr[64]; /* Hold time */
ef416fc2 2399
ef416fc2 2400
e1d6a774 2401 /*
2402 * Set the hold_until value and hold the job...
2403 */
ef416fc2 2404
e1d6a774 2405 cupsdLogMessage(CUPSD_LOG_DEBUG, "set_hold_until: hold_until = %d",
2406 (int)holdtime);
e00b005a 2407
e1d6a774 2408 job->state->values[0].integer = IPP_JOB_HELD;
2409 job->state_value = IPP_JOB_HELD;
2410 job->hold_until = holdtime;
ef416fc2 2411
e1d6a774 2412 /*
2413 * Update the job-hold-until attribute with a string representing GMT
2414 * time (HH:MM:SS)...
2415 */
ef416fc2 2416
e1d6a774 2417 holddate = gmtime(&holdtime);
2418 snprintf(holdstr, sizeof(holdstr), "%d:%d:%d", holddate->tm_hour,
2419 holddate->tm_min, holddate->tm_sec);
ef416fc2 2420
e1d6a774 2421 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
2422 IPP_TAG_KEYWORD)) == NULL)
2423 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
ef416fc2 2424
e1d6a774 2425 /*
2426 * Either add the attribute or update the value of the existing one
2427 */
ef416fc2 2428
e1d6a774 2429 if (attr == NULL)
2430 attr = ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_KEYWORD,
2431 "job-hold-until", NULL, holdstr);
2432 else
2433 cupsdSetString(&attr->values[0].string.text, holdstr);
ef416fc2 2434
e1d6a774 2435 cupsdSaveJob(job);
ef416fc2 2436}
2437
2438
2439/*
e1d6a774 2440 * 'start_job()' - Start a print job.
ef416fc2 2441 */
2442
e1d6a774 2443static void
2444start_job(cupsd_job_t *job, /* I - Job ID */
2445 cupsd_printer_t *printer) /* I - Printer to print job */
ef416fc2 2446{
e1d6a774 2447 int i; /* Looping var */
2448 int slot; /* Pipe slot */
2449 cups_array_t *filters; /* Filters for job */
2450 mime_filter_t *filter, /* Current filter */
2451 port_monitor; /* Port monitor filter */
2452 char method[255], /* Method for output */
2453 *optptr, /* Pointer to options */
2454 *valptr; /* Pointer in value string */
2455 ipp_attribute_t *attr; /* Current attribute */
2456 struct stat backinfo; /* Backend file information */
2457 int backroot; /* Run backend as root? */
2458 int pid; /* Process ID of new filter process */
2459 int banner_page; /* 1 if banner page, 0 otherwise */
89d46774 2460 int filterfds[2][2];/* Pipes used between filters */
e1d6a774 2461 int envc; /* Number of environment variables */
2462 char **argv, /* Filter command-line arguments */
2463 sani_uri[1024], /* Sanitized DEVICE_URI env var */
2464 filename[1024], /* Job filename */
2465 command[1024], /* Full path to command */
2466 jobid[255], /* Job ID string */
2467 title[IPP_MAX_NAME],
2468 /* Job title string */
2469 copies[255], /* # copies string */
2470 *envp[MAX_ENV + 11],
2471 /* Environment variables */
2472 charset[255], /* CHARSET env variable */
2473 class_name[255],/* CLASS env variable */
2474 classification[1024],
2475 /* CLASSIFICATION env variable */
2476 content_type[1024],
2477 /* CONTENT_TYPE env variable */
2478 device_uri[1024],
2479 /* DEVICE_URI env variable */
2480 final_content_type[1024],
2481 /* FINAL_CONTENT_TYPE env variable */
2482 lang[255], /* LANG env variable */
2483 ppd[1024], /* PPD env variable */
2484 printer_name[255],
2485 /* PRINTER env variable */
2486 rip_max_cache[255];
2487 /* RIP_MAX_CACHE env variable */
e1d6a774 2488 static char *options = NULL;/* Full list of options */
2489 static int optlength = 0; /* Length of option buffer */
ef416fc2 2490
2491
e1d6a774 2492 cupsdLogMessage(CUPSD_LOG_DEBUG2, "start_job: id = %d, file = %d/%d",
2493 job->id, job->current_file, job->num_files);
ef416fc2 2494
e1d6a774 2495 if (job->num_files == 0)
2496 {
d09495fa 2497 cupsdLogMessage(CUPSD_LOG_ERROR, "Job ID %d has no files! Canceling it!",
e1d6a774 2498 job->id);
2499
07725fee 2500 cupsdCancelJob(job, 0, IPP_JOB_ABORTED);
ef416fc2 2501 return;
e1d6a774 2502 }
ef416fc2 2503
89d46774 2504 if (printer->raw && !strncmp(printer->device_uri, "file:", 5))
2505 {
2506 cupsdLogMessage(CUPSD_LOG_ERROR,
2507 "Job ID %d cannot be printed to raw queue pointing to "
2508 "a file!",
2509 job->id);
2510
2511 strlcpy(printer->state_message, "Raw printers cannot use file: devices!",
2512 sizeof(printer->state_message));
2513 cupsdStopPrinter(printer, 1);
2514 cupsdAddPrinterHistory(printer);
2515 return;
2516 }
2517
e1d6a774 2518 /*
2519 * Figure out what filters are required to convert from
2520 * the source to the destination type...
2521 */
ef416fc2 2522
e1d6a774 2523 filters = NULL;
2524 job->cost = 0;
ef416fc2 2525
e1d6a774 2526 if (printer->raw)
2527 {
2528 /*
2529 * Remote jobs and raw queues go directly to the printer without
2530 * filtering...
2531 */
ef416fc2 2532
e1d6a774 2533 cupsdLogMessage(CUPSD_LOG_DEBUG,
2534 "[Job %d] Sending job to queue tagged as raw...", job->id);
ef416fc2 2535
e1d6a774 2536 filters = NULL;
2537 }
2538 else
2539 {
2540 /*
2541 * Local jobs get filtered...
2542 */
ef416fc2 2543
e1d6a774 2544 filters = mimeFilter(MimeDatabase, job->filetypes[job->current_file],
2545 printer->filetype, &(job->cost));
2546
2547 if (!filters)
ef416fc2 2548 {
e1d6a774 2549 cupsdLogMessage(CUPSD_LOG_ERROR,
2550 "Unable to convert file %d to printable format for "
2551 "job %d!",
2552 job->current_file, job->id);
2553 cupsdLogMessage(CUPSD_LOG_INFO,
2554 "Hint: Do you have ESP Ghostscript installed?");
ef416fc2 2555
e1d6a774 2556 if (LogLevel < CUPSD_LOG_DEBUG)
2557 cupsdLogMessage(CUPSD_LOG_INFO,
2558 "Hint: Try setting the LogLevel to \"debug\".");
ef416fc2 2559
e1d6a774 2560 job->current_file ++;
ef416fc2 2561
e1d6a774 2562 if (job->current_file == job->num_files)
07725fee 2563 cupsdCancelJob(job, 0, IPP_JOB_ABORTED);
ef416fc2 2564
e1d6a774 2565 return;
2566 }
ef416fc2 2567
ef416fc2 2568 /*
e1d6a774 2569 * Remove NULL ("-") filters...
ef416fc2 2570 */
2571
e1d6a774 2572 for (filter = (mime_filter_t *)cupsArrayFirst(filters);
2573 filter;
2574 filter = (mime_filter_t *)cupsArrayNext(filters))
2575 if (!strcmp(filter->filter, "-"))
2576 cupsArrayRemove(filters, filter);
ef416fc2 2577
e1d6a774 2578 if (cupsArrayCount(filters) == 0)
2579 {
2580 cupsArrayDelete(filters);
2581 filters = NULL;
2582 }
2583 }
ef416fc2 2584
e1d6a774 2585 /*
2586 * Set a minimum cost of 100 for all jobs so that FilterLimit
2587 * works with raw queues and other low-cost paths.
2588 */
ef416fc2 2589
e1d6a774 2590 if (job->cost < 100)
2591 job->cost = 100;
ef416fc2 2592
e1d6a774 2593 /*
2594 * See if the filter cost is too high...
2595 */
ef416fc2 2596
e1d6a774 2597 if ((FilterLevel + job->cost) > FilterLimit && FilterLevel > 0 &&
2598 FilterLimit > 0)
2599 {
2600 /*
2601 * Don't print this job quite yet...
2602 */
bd7854cb 2603
e1d6a774 2604 cupsArrayDelete(filters);
bd7854cb 2605
e1d6a774 2606 cupsdLogMessage(CUPSD_LOG_INFO,
2607 "Holding job %d because filter limit has been reached.",
2608 job->id);
2609 cupsdLogMessage(CUPSD_LOG_DEBUG2,
2610 "start_job: id=%d, file=%d, cost=%d, level=%d, "
2611 "limit=%d",
2612 job->id, job->current_file, job->cost, FilterLevel,
2613 FilterLimit);
2614 return;
2615 }
bd7854cb 2616
e1d6a774 2617 FilterLevel += job->cost;
bd7854cb 2618
e1d6a774 2619 /*
2620 * Add decompression filters, if any...
2621 */
bd7854cb 2622
d09495fa 2623 if (!printer->raw && job->compressions[job->current_file])
e1d6a774 2624 {
2625 /*
2626 * Add gziptoany filter to the front of the list...
2627 */
bd7854cb 2628
2abf387c 2629 if (!filters)
2630 filters = cupsArrayNew(NULL, NULL);
2631
e1d6a774 2632 if (!cupsArrayInsert(filters, &gziptoany_filter))
2633 {
2634 cupsdLogMessage(CUPSD_LOG_ERROR,
2635 "Unable to add decompression filter - %s",
2636 strerror(errno));
bd7854cb 2637
e1d6a774 2638 cupsArrayDelete(filters);
bd7854cb 2639
e1d6a774 2640 job->current_file ++;
bd7854cb 2641
e1d6a774 2642 if (job->current_file == job->num_files)
07725fee 2643 cupsdCancelJob(job, 0, IPP_JOB_ABORTED);
ef416fc2 2644
e1d6a774 2645 return;
2646 }
2647 }
ef416fc2 2648
e1d6a774 2649 /*
2650 * Add port monitor, if any...
2651 */
ef416fc2 2652
e1d6a774 2653 if (printer->port_monitor)
ef416fc2 2654 {
2655 /*
e1d6a774 2656 * Add port monitor to the end of the list...
ef416fc2 2657 */
2658
2abf387c 2659 if (!filters)
2660 filters = cupsArrayNew(NULL, NULL);
2661
e1d6a774 2662 if (!cupsArrayAdd(filters, &port_monitor))
ef416fc2 2663 {
e1d6a774 2664 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to add port monitor - %s",
2665 strerror(errno));
ef416fc2 2666
e1d6a774 2667 cupsArrayDelete(filters);
ef416fc2 2668
e1d6a774 2669 job->current_file ++;
ef416fc2 2670
e1d6a774 2671 if (job->current_file == job->num_files)
07725fee 2672 cupsdCancelJob(job, 0, IPP_JOB_ABORTED);
ef416fc2 2673
e1d6a774 2674 return;
e00b005a 2675 }
ef416fc2 2676
e1d6a774 2677 snprintf(port_monitor.filter, sizeof(port_monitor.filter),
2678 "%s/monitor/%s", ServerBin, printer->port_monitor);
2679 }
e00b005a 2680
e1d6a774 2681 /*
2682 * Update the printer and job state to "processing"...
2683 */
ef416fc2 2684
e1d6a774 2685 job->state->values[0].integer = IPP_JOB_PROCESSING;
2686 job->state_value = IPP_JOB_PROCESSING;
2687 job->status = 0;
2688 job->printer = printer;
2689 printer->job = job;
2690 cupsdSetPrinterState(printer, IPP_PRINTER_PROCESSING, 0);
ef416fc2 2691
e1d6a774 2692 if (job->current_file == 0)
ef416fc2 2693 {
f301802f 2694 /*
2695 * Set the processing time...
2696 */
2697
e1d6a774 2698 set_time(job, "time-at-processing");
f301802f 2699
2700 /*
2701 * Create the backchannel pipes and make them non-blocking...
2702 */
2703
e1d6a774 2704 cupsdOpenPipe(job->back_pipes);
f301802f 2705
2706 fcntl(job->back_pipes[0], F_SETFL,
2707 fcntl(job->back_pipes[0], F_GETFL) | O_NONBLOCK);
2708
2709 fcntl(job->back_pipes[1], F_SETFL,
2710 fcntl(job->back_pipes[1], F_GETFL) | O_NONBLOCK);
e1d6a774 2711 }
ef416fc2 2712
e1d6a774 2713 /*
2714 * Determine if we are printing a banner page or not...
2715 */
ef416fc2 2716
e1d6a774 2717 if (job->job_sheets == NULL)
2718 {
2719 cupsdLogMessage(CUPSD_LOG_DEBUG, "No job-sheets attribute.");
2720 if ((job->job_sheets =
2721 ippFindAttribute(job->attrs, "job-sheets", IPP_TAG_ZERO)) != NULL)
2722 cupsdLogMessage(CUPSD_LOG_DEBUG,
2723 "... but someone added one without setting job_sheets!");
2724 }
2725 else if (job->job_sheets->num_values == 1)
2726 cupsdLogMessage(CUPSD_LOG_DEBUG, "job-sheets=%s",
2727 job->job_sheets->values[0].string.text);
2728 else
2729 cupsdLogMessage(CUPSD_LOG_DEBUG, "job-sheets=%s,%s",
2730 job->job_sheets->values[0].string.text,
2731 job->job_sheets->values[1].string.text);
ef416fc2 2732
e1d6a774 2733 if (printer->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT))
2734 banner_page = 0;
2735 else if (job->job_sheets == NULL)
2736 banner_page = 0;
2737 else if (strcasecmp(job->job_sheets->values[0].string.text, "none") != 0 &&
2738 job->current_file == 0)
2739 banner_page = 1;
2740 else if (job->job_sheets->num_values > 1 &&
2741 strcasecmp(job->job_sheets->values[1].string.text, "none") != 0 &&
2742 job->current_file == (job->num_files - 1))
2743 banner_page = 1;
2744 else
2745 banner_page = 0;
ef416fc2 2746
e1d6a774 2747 cupsdLogMessage(CUPSD_LOG_DEBUG, "banner_page = %d", banner_page);
ef416fc2 2748
e1d6a774 2749 /*
2750 * Building the options string is harder than it needs to be, but
2751 * for the moment we need to pass strings for command-line args and
2752 * not IPP attribute pointers... :)
2753 *
2754 * First allocate/reallocate the option buffer as needed...
2755 */
ef416fc2 2756
e1d6a774 2757 i = ipp_length(job->attrs);
ef416fc2 2758
e1d6a774 2759 if (i > optlength)
2760 {
2761 if (optlength == 0)
2762 optptr = malloc(i);
2763 else
2764 optptr = realloc(options, i);
ef416fc2 2765
e1d6a774 2766 if (optptr == NULL)
2767 {
2768 cupsdLogMessage(CUPSD_LOG_CRIT,
2769 "Unable to allocate %d bytes for option buffer for "
2770 "job %d!", i, job->id);
ef416fc2 2771
e1d6a774 2772 cupsArrayDelete(filters);
ef416fc2 2773
e1d6a774 2774 FilterLevel -= job->cost;
ef416fc2 2775
07725fee 2776 cupsdCancelJob(job, 0, IPP_JOB_ABORTED);
e1d6a774 2777 return;
2778 }
ef416fc2 2779
e1d6a774 2780 options = optptr;
2781 optlength = i;
2782 }
ef416fc2 2783
e1d6a774 2784 /*
2785 * Now loop through the attributes and convert them to the textual
2786 * representation used by the filters...
2787 */
ef416fc2 2788
e1d6a774 2789 optptr = options;
2790 *optptr = '\0';
bd7854cb 2791
e1d6a774 2792 snprintf(title, sizeof(title), "%s-%d", printer->name, job->id);
2793 strcpy(copies, "1");
bd7854cb 2794
e1d6a774 2795 for (attr = job->attrs->attrs; attr != NULL; attr = attr->next)
bd7854cb 2796 {
e1d6a774 2797 if (!strcmp(attr->name, "copies") &&
2798 attr->value_tag == IPP_TAG_INTEGER)
2799 {
2800 /*
2801 * Don't use the # copies attribute if we are printing the job sheets...
2802 */
bd7854cb 2803
e1d6a774 2804 if (!banner_page)
2805 sprintf(copies, "%d", attr->values[0].integer);
2806 }
2807 else if (!strcmp(attr->name, "job-name") &&
2808 (attr->value_tag == IPP_TAG_NAME ||
2809 attr->value_tag == IPP_TAG_NAMELANG))
2810 strlcpy(title, attr->values[0].string.text, sizeof(title));
2811 else if (attr->group_tag == IPP_TAG_JOB)
2812 {
2813 /*
2814 * Filter out other unwanted attributes...
2815 */
bd7854cb 2816
e1d6a774 2817 if (attr->value_tag == IPP_TAG_MIMETYPE ||
2818 attr->value_tag == IPP_TAG_NAMELANG ||
2819 attr->value_tag == IPP_TAG_TEXTLANG ||
2820 (attr->value_tag == IPP_TAG_URI && strcmp(attr->name, "job-uuid")) ||
2821 attr->value_tag == IPP_TAG_URISCHEME ||
2822 attr->value_tag == IPP_TAG_BEGIN_COLLECTION) /* Not yet supported */
2823 continue;
bd7854cb 2824
e1d6a774 2825 if (!strncmp(attr->name, "time-", 5))
2826 continue;
bd7854cb 2827
e1d6a774 2828 if (!strncmp(attr->name, "job-", 4) && strcmp(attr->name, "job-uuid") &&
2829 !(printer->type & CUPS_PRINTER_REMOTE))
2830 continue;
ef416fc2 2831
e1d6a774 2832 if (!strncmp(attr->name, "job-", 4) &&
2833 strcmp(attr->name, "job-uuid") &&
2834 strcmp(attr->name, "job-billing") &&
2835 strcmp(attr->name, "job-sheets") &&
2836 strcmp(attr->name, "job-hold-until") &&
2837 strcmp(attr->name, "job-priority"))
2838 continue;
ef416fc2 2839
e1d6a774 2840 if ((!strcmp(attr->name, "page-label") ||
2841 !strcmp(attr->name, "page-border") ||
2842 !strncmp(attr->name, "number-up", 9) ||
2843 !strcmp(attr->name, "page-set") ||
2844 !strcasecmp(attr->name, "AP_FIRSTPAGE_InputSlot") ||
2845 !strcasecmp(attr->name, "AP_FIRSTPAGE_ManualFeed")) &&
2846 banner_page)
2847 continue;
ef416fc2 2848
e1d6a774 2849 /*
2850 * Otherwise add them to the list...
2851 */
ef416fc2 2852
e1d6a774 2853 if (optptr > options)
2854 strlcat(optptr, " ", optlength - (optptr - options));
ef416fc2 2855
e1d6a774 2856 if (attr->value_tag != IPP_TAG_BOOLEAN)
2857 {
2858 strlcat(optptr, attr->name, optlength - (optptr - options));
2859 strlcat(optptr, "=", optlength - (optptr - options));
2860 }
ef416fc2 2861
e1d6a774 2862 for (i = 0; i < attr->num_values; i ++)
2863 {
2864 if (i)
2865 strlcat(optptr, ",", optlength - (optptr - options));
ef416fc2 2866
e1d6a774 2867 optptr += strlen(optptr);
ef416fc2 2868
e1d6a774 2869 switch (attr->value_tag)
2870 {
2871 case IPP_TAG_INTEGER :
2872 case IPP_TAG_ENUM :
2873 snprintf(optptr, optlength - (optptr - options),
2874 "%d", attr->values[i].integer);
2875 break;
ef416fc2 2876
e1d6a774 2877 case IPP_TAG_BOOLEAN :
2878 if (!attr->values[i].boolean)
2879 strlcat(optptr, "no", optlength - (optptr - options));
ef416fc2 2880
e1d6a774 2881 case IPP_TAG_NOVALUE :
2882 strlcat(optptr, attr->name,
2883 optlength - (optptr - options));
2884 break;
ef416fc2 2885
e1d6a774 2886 case IPP_TAG_RANGE :
2887 if (attr->values[i].range.lower == attr->values[i].range.upper)
2888 snprintf(optptr, optlength - (optptr - options) - 1,
2889 "%d", attr->values[i].range.lower);
2890 else
2891 snprintf(optptr, optlength - (optptr - options) - 1,
2892 "%d-%d", attr->values[i].range.lower,
2893 attr->values[i].range.upper);
2894 break;
ef416fc2 2895
e1d6a774 2896 case IPP_TAG_RESOLUTION :
2897 snprintf(optptr, optlength - (optptr - options) - 1,
2898 "%dx%d%s", attr->values[i].resolution.xres,
2899 attr->values[i].resolution.yres,
2900 attr->values[i].resolution.units == IPP_RES_PER_INCH ?
2901 "dpi" : "dpc");
2902 break;
ef416fc2 2903
e1d6a774 2904 case IPP_TAG_STRING :
2905 case IPP_TAG_TEXT :
2906 case IPP_TAG_NAME :
2907 case IPP_TAG_KEYWORD :
2908 case IPP_TAG_CHARSET :
2909 case IPP_TAG_LANGUAGE :
2910 case IPP_TAG_URI :
2911 for (valptr = attr->values[i].string.text; *valptr;)
2912 {
2913 if (strchr(" \t\n\\\'\"", *valptr))
2914 *optptr++ = '\\';
2915 *optptr++ = *valptr++;
2916 }
2917
2918 *optptr = '\0';
2919 break;
2920
2921 default :
2922 break; /* anti-compiler-warning-code */
2923 }
2924 }
2925
2926 optptr += strlen(optptr);
2927 }
2928 }
2929
2930 /*
2931 * Build the command-line arguments for the filters. Each filter
2932 * has 6 or 7 arguments:
2933 *
2934 * argv[0] = printer
2935 * argv[1] = job ID
2936 * argv[2] = username
2937 * argv[3] = title
2938 * argv[4] = # copies
2939 * argv[5] = options
2940 * argv[6] = filename (optional; normally stdin)
2941 *
2942 * This allows legacy printer drivers that use the old System V
2943 * printing interface to be used by CUPS.
2944 *
2945 * For remote jobs, we send all of the files in the argument list.
2946 */
2947
d09495fa 2948 if (printer->remote && job->num_files > 1)
e1d6a774 2949 argv = calloc(7 + job->num_files, sizeof(char *));
2950 else
2951 argv = calloc(8, sizeof(char *));
2952
2953 sprintf(jobid, "%d", job->id);
2954
2955 argv[0] = printer->name;
2956 argv[1] = jobid;
2957 argv[2] = job->username;
2958 argv[3] = title;
2959 argv[4] = copies;
2960 argv[5] = options;
2961
d09495fa 2962 if (printer->remote && job->num_files > 1)
e1d6a774 2963 {
2964 for (i = 0; i < job->num_files; i ++)
ef416fc2 2965 {
e1d6a774 2966 snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot,
2967 job->id, i + 1);
2968 argv[6 + i] = strdup(filename);
2969 }
2970 }
2971 else
2972 {
2973 snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot,
2974 job->id, job->current_file + 1);
2975 argv[6] = filename;
2976 }
ef416fc2 2977
e1d6a774 2978 for (i = 0; argv[i]; i ++)
2979 cupsdLogMessage(CUPSD_LOG_DEBUG,
2980 "[Job %d] argv[%d]=\"%s\"", job->id, i, argv[i]);
ef416fc2 2981
e1d6a774 2982 /*
2983 * Create environment variable strings for the filters...
2984 */
ef416fc2 2985
e1d6a774 2986 attr = ippFindAttribute(job->attrs, "attributes-natural-language",
2987 IPP_TAG_LANGUAGE);
ef416fc2 2988
e1d6a774 2989 switch (strlen(attr->values[0].string.text))
2990 {
2991 default :
2992 /*
2993 * This is an unknown or badly formatted language code; use
2994 * the POSIX locale...
2995 */
ef416fc2 2996
e1d6a774 2997 strcpy(lang, "LANG=C");
2998 break;
ef416fc2 2999
e1d6a774 3000 case 2 :
3001 /*
3002 * Just the language code (ll)...
3003 */
ef416fc2 3004
e1d6a774 3005 snprintf(lang, sizeof(lang), "LANG=%s",
3006 attr->values[0].string.text);
3007 break;
ef416fc2 3008
e1d6a774 3009 case 5 :
3010 /*
3011 * Language and country code (ll-cc)...
3012 */
ef416fc2 3013
e1d6a774 3014 snprintf(lang, sizeof(lang), "LANG=%c%c_%c%c",
3015 attr->values[0].string.text[0],
3016 attr->values[0].string.text[1],
3017 toupper(attr->values[0].string.text[3] & 255),
3018 toupper(attr->values[0].string.text[4] & 255));
3019 break;
3020 }
ef416fc2 3021
e1d6a774 3022 attr = ippFindAttribute(job->attrs, "document-format",
3023 IPP_TAG_MIMETYPE);
3024 if (attr != NULL &&
3025 (optptr = strstr(attr->values[0].string.text, "charset=")) != NULL)
3026 snprintf(charset, sizeof(charset), "CHARSET=%s", optptr + 8);
3027 else
3028 {
3029 attr = ippFindAttribute(job->attrs, "attributes-charset",
3030 IPP_TAG_CHARSET);
3031 snprintf(charset, sizeof(charset), "CHARSET=%s",
3032 attr->values[0].string.text);
ef416fc2 3033 }
3034
e1d6a774 3035 snprintf(content_type, sizeof(content_type), "CONTENT_TYPE=%s/%s",
3036 job->filetypes[job->current_file]->super,
3037 job->filetypes[job->current_file]->type);
3038 snprintf(device_uri, sizeof(device_uri), "DEVICE_URI=%s",
3039 printer->device_uri);
3040 cupsdSanitizeURI(printer->device_uri, sani_uri, sizeof(sani_uri));
3041 snprintf(ppd, sizeof(ppd), "PPD=%s/ppd/%s.ppd", ServerRoot, printer->name);
3042 snprintf(printer_name, sizeof(printer_name), "PRINTER=%s", printer->name);
3043 snprintf(rip_max_cache, sizeof(rip_max_cache), "RIP_MAX_CACHE=%s", RIPCache);
ef416fc2 3044
e1d6a774 3045 envc = cupsdLoadEnv(envp, (int)(sizeof(envp) / sizeof(envp[0])));
ef416fc2 3046
e1d6a774 3047 envp[envc ++] = charset;
3048 envp[envc ++] = lang;
3049 envp[envc ++] = ppd;
3050 envp[envc ++] = rip_max_cache;
3051 envp[envc ++] = content_type;
3052 envp[envc ++] = device_uri;
3053 envp[envc ++] = printer_name;
bd7854cb 3054
d09495fa 3055 if (!printer->remote &&
3056 (filter = (mime_filter_t *)cupsArrayLast(filters)) != NULL)
e1d6a774 3057 {
3058 snprintf(final_content_type, sizeof(final_content_type),
3059 "FINAL_CONTENT_TYPE=%s/%s",
d09495fa 3060 filter->dst->super, filter->dst->type);
e1d6a774 3061 envp[envc ++] = final_content_type;
3062 }
bd7854cb 3063
e1d6a774 3064 if (Classification && !banner_page)
3065 {
3066 if ((attr = ippFindAttribute(job->attrs, "job-sheets",
3067 IPP_TAG_NAME)) == NULL)
3068 snprintf(classification, sizeof(classification), "CLASSIFICATION=%s",
3069 Classification);
3070 else if (attr->num_values > 1 &&
3071 strcmp(attr->values[1].string.text, "none") != 0)
3072 snprintf(classification, sizeof(classification), "CLASSIFICATION=%s",
3073 attr->values[1].string.text);
3074 else
3075 snprintf(classification, sizeof(classification), "CLASSIFICATION=%s",
3076 attr->values[0].string.text);
bd7854cb 3077
e1d6a774 3078 envp[envc ++] = classification;
3079 }
bd7854cb 3080
e1d6a774 3081 if (job->dtype & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT))
bd7854cb 3082 {
e1d6a774 3083 snprintf(class_name, sizeof(class_name), "CLASS=%s", job->dest);
3084 envp[envc ++] = class_name;
3085 }
bd7854cb 3086
e1d6a774 3087 envp[envc] = NULL;
bd7854cb 3088
e1d6a774 3089 for (i = 0; i < envc; i ++)
3090 if (strncmp(envp[i], "DEVICE_URI=", 11))
3091 cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] envp[%d]=\"%s\"",
3092 job->id, i, envp[i]);
3093 else
3094 cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] envp[%d]=\"DEVICE_URI=%s\"",
3095 job->id, i, sani_uri);
3096
d09495fa 3097 if (printer->remote)
e1d6a774 3098 job->current_file = job->num_files;
3099 else
3100 job->current_file ++;
bd7854cb 3101
3102 /*
e1d6a774 3103 * Now create processes for all of the filters...
bd7854cb 3104 */
3105
e1d6a774 3106 filterfds[0][0] = -1;
3107 filterfds[0][1] = -1;
3108 filterfds[1][0] = -1;
3109 filterfds[1][1] = -1;
bd7854cb 3110
89d46774 3111 if (!job->status_buffer)
bd7854cb 3112 {
89d46774 3113 if (cupsdOpenPipe(job->status_pipes))
3114 {
3115 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to create job status pipes - %s.",
3116 strerror(errno));
3117 snprintf(printer->state_message, sizeof(printer->state_message),
3118 "Unable to create status pipes - %s.", strerror(errno));
3119
3120 cupsdAddPrinterHistory(printer);
3121
3122 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
3123 "Job canceled because the server could not create the job "
3124 "status pipes.");
3125
3126 goto abort_job;
3127 }
3128
3129 cupsdLogMessage(CUPSD_LOG_DEBUG2, "start_job: status_pipes = [ %d %d ]",
3130 job->status_pipes[0], job->status_pipes[1]);
3131
3132 job->status_buffer = cupsdStatBufNew(job->status_pipes[0], "[Job %d]",
3133 job->id);
e1d6a774 3134 }
bd7854cb 3135
89d46774 3136 job->status = 0;
e1d6a774 3137 memset(job->filters, 0, sizeof(job->filters));
bd7854cb 3138
e1d6a774 3139 for (i = 0, slot = 0, filter = (mime_filter_t *)cupsArrayFirst(filters);
3140 filter;
3141 i ++, filter = (mime_filter_t *)cupsArrayNext(filters))
3142 {
3143 if (filter->filter[0] != '/')
3144 snprintf(command, sizeof(command), "%s/filter/%s", ServerBin,
3145 filter->filter);
3146 else
3147 strlcpy(command, filter->filter, sizeof(command));
bd7854cb 3148
e1d6a774 3149 if (i < (cupsArrayCount(filters) - 1))
bd7854cb 3150 {
e1d6a774 3151 if (cupsdOpenPipe(filterfds[slot]))
3152 {
3153 cupsdLogMessage(CUPSD_LOG_ERROR,
3154 "Unable to create job filter pipes - %s.",
3155 strerror(errno));
3156 snprintf(printer->state_message, sizeof(printer->state_message),
3157 "Unable to create filter pipes - %s.", strerror(errno));
3158 cupsdAddPrinterHistory(printer);
bd7854cb 3159
e1d6a774 3160 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
3161 "Job canceled because the server could not create the "
3162 "filter pipes.");
bd7854cb 3163
e1d6a774 3164 goto abort_job;
bd7854cb 3165 }
e1d6a774 3166 }
3167 else
3168 {
3169 if (job->current_file == 1)
bd7854cb 3170 {
e1d6a774 3171 if (strncmp(printer->device_uri, "file:", 5) != 0)
3172 {
3173 if (cupsdOpenPipe(job->print_pipes))
3174 {
3175 cupsdLogMessage(CUPSD_LOG_ERROR,
3176 "Unable to create job backend pipes - %s.",
3177 strerror(errno));
3178 snprintf(printer->state_message, sizeof(printer->state_message),
3179 "Unable to create backend pipes - %s.", strerror(errno));
3180 cupsdAddPrinterHistory(printer);
bd7854cb 3181
e1d6a774 3182 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
3183 "Job canceled because the server could not create "
3184 "the backend pipes.");
bd7854cb 3185
e1d6a774 3186 goto abort_job;
3187 }
3188 }
3189 else
3190 {
3191 job->print_pipes[0] = -1;
ed486911 3192 if (!strcmp(printer->device_uri, "file:/dev/null") ||
3193 !strcmp(printer->device_uri, "file:///dev/null"))
3194 job->print_pipes[1] = -1;
e1d6a774 3195 else
e1d6a774 3196 {
ed486911 3197 if (!strncmp(printer->device_uri, "file:/dev/", 10))
3198 job->print_pipes[1] = open(printer->device_uri + 5,
3199 O_WRONLY | O_EXCL);
3200 else if (!strncmp(printer->device_uri, "file:///dev/", 12))
3201 job->print_pipes[1] = open(printer->device_uri + 7,
3202 O_WRONLY | O_EXCL);
3203 else if (!strncmp(printer->device_uri, "file:///", 8))
3204 job->print_pipes[1] = open(printer->device_uri + 7,
3205 O_WRONLY | O_CREAT | O_TRUNC, 0600);
3206 else
3207 job->print_pipes[1] = open(printer->device_uri + 5,
3208 O_WRONLY | O_CREAT | O_TRUNC, 0600);
bd7854cb 3209
ed486911 3210 if (job->print_pipes[1] < 0)
3211 {
3212 cupsdLogMessage(CUPSD_LOG_ERROR,
3213 "Unable to open output file \"%s\" - %s.",
3214 printer->device_uri, strerror(errno));
3215 snprintf(printer->state_message, sizeof(printer->state_message),
3216 "Unable to open output file \"%s\" - %s.",
3217 printer->device_uri, strerror(errno));
3218
3219 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
3220 "Job canceled because the server could not open the "
3221 "output file.");
3222
3223 goto abort_job;
3224 }
bd7854cb 3225
ed486911 3226 fcntl(job->print_pipes[1], F_SETFD,
3227 fcntl(job->print_pipes[1], F_GETFD) | FD_CLOEXEC);
3228 }
e1d6a774 3229 }
bd7854cb 3230
e1d6a774 3231 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3232 "start_job: print_pipes = [ %d %d ]",
3233 job->print_pipes[0], job->print_pipes[1]);
bd7854cb 3234 }
e1d6a774 3235
3236 filterfds[slot][0] = job->print_pipes[0];
3237 filterfds[slot][1] = job->print_pipes[1];
bd7854cb 3238 }
bd7854cb 3239
e1d6a774 3240 cupsdLogMessage(CUPSD_LOG_DEBUG2, "start_job: filter=\"%s\"",
3241 command);
3242 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3243 "start_job: filterfds[%d]=[ %d %d ]",
3244 slot, filterfds[slot][0], filterfds[slot][1]);
bd7854cb 3245
e1d6a774 3246 pid = cupsdStartProcess(command, argv, envp, filterfds[!slot][0],
89d46774 3247 filterfds[slot][1], job->status_pipes[1],
e1d6a774 3248 job->back_pipes[0], 0, job->filters + i);
bd7854cb 3249
e1d6a774 3250 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3251 "start_job: Closing filter pipes for slot %d "
3252 "[ %d %d ]...",
3253 !slot, filterfds[!slot][0], filterfds[!slot][1]);
bd7854cb 3254
e1d6a774 3255 cupsdClosePipe(filterfds[!slot]);
bd7854cb 3256
e1d6a774 3257 if (pid == 0)
3258 {
3259 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to start filter \"%s\" - %s.",
3260 filter->filter, strerror(errno));
3261 snprintf(printer->state_message, sizeof(printer->state_message),
3262 "Unable to start filter \"%s\" - %s.",
3263 filter->filter, strerror(errno));
bd7854cb 3264
e1d6a774 3265 cupsdAddPrinterHistory(printer);
bd7854cb 3266
e1d6a774 3267 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
3268 "Job canceled because the server could not execute a "
3269 "filter.");
bd7854cb 3270
e1d6a774 3271 goto abort_job;
3272 }
3273
3274 cupsdLogMessage(CUPSD_LOG_INFO, "Started filter %s (PID %d) for job %d.",
3275 command, pid, job->id);
3276
3277 argv[6] = NULL;
3278 slot = !slot;
bd7854cb 3279 }
3280
e1d6a774 3281 cupsArrayDelete(filters);
bd7854cb 3282
e1d6a774 3283 /*
3284 * Finally, pipe the final output into a backend process if needed...
3285 */
bd7854cb 3286
e1d6a774 3287 if (strncmp(printer->device_uri, "file:", 5) != 0)
bd7854cb 3288 {
e1d6a774 3289 if (job->current_file == 1)
bd7854cb 3290 {
e1d6a774 3291 sscanf(printer->device_uri, "%254[^:]", method);
3292 snprintf(command, sizeof(command), "%s/backend/%s", ServerBin, method);
bd7854cb 3293
e1d6a774 3294 /*
3295 * See if the backend needs to run as root...
3296 */
bd7854cb 3297
e1d6a774 3298 if (RunUser)
3299 backroot = 0;
3300 else if (stat(command, &backinfo))
3301 backroot = 0;
3302 else
3303 backroot = !(backinfo.st_mode & (S_IRWXG | S_IRWXO));
bd7854cb 3304
e1d6a774 3305 argv[0] = sani_uri;
bd7854cb 3306
e1d6a774 3307 filterfds[slot][0] = -1;
ed486911 3308 filterfds[slot][1] = -1;
bd7854cb 3309
e1d6a774 3310 cupsdLogMessage(CUPSD_LOG_DEBUG2, "start_job: backend=\"%s\"",
3311 command);
3312 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3313 "start_job: filterfds[%d] = [ %d %d ]",
3314 slot, filterfds[slot][0], filterfds[slot][1]);
bd7854cb 3315
e1d6a774 3316 pid = cupsdStartProcess(command, argv, envp, filterfds[!slot][0],
89d46774 3317 filterfds[slot][1], job->status_pipes[1],
e1d6a774 3318 job->back_pipes[1], backroot,
3319 &(job->backend));
bd7854cb 3320
e1d6a774 3321 if (pid == 0)
bd7854cb 3322 {
e1d6a774 3323 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to start backend \"%s\" - %s.",
3324 method, strerror(errno));
3325 snprintf(printer->state_message, sizeof(printer->state_message),
3326 "Unable to start backend \"%s\" - %s.", method,
3327 strerror(errno));
3328
3329 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
3330 "Job canceled because the server could not execute "
3331 "the backend.");
3332
3333 goto abort_job;
3334 }
3335 else
3336 {
3337 cupsdLogMessage(CUPSD_LOG_INFO,
3338 "Started backend %s (PID %d) for job %d.",
3339 command, pid, job->id);
bd7854cb 3340 }
e1d6a774 3341 }
bd7854cb 3342
e1d6a774 3343 if (job->current_file == job->num_files)
3344 {
3345 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3346 "start_job: Closing print pipes [ %d %d ]...",
3347 job->print_pipes[0], job->print_pipes[1]);
bd7854cb 3348
e1d6a774 3349 cupsdClosePipe(job->print_pipes);
bd7854cb 3350
e1d6a774 3351 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3352 "start_job: Closing back pipes [ %d %d ]...",
3353 job->back_pipes[0], job->back_pipes[1]);
bd7854cb 3354
e1d6a774 3355 cupsdClosePipe(job->back_pipes);
89d46774 3356
3357 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3358 "start_job: Closing status output pipe %d...",
3359 job->status_pipes[1]);
3360
3361 close(job->status_pipes[1]);
3362 job->status_pipes[1] = -1;
e1d6a774 3363 }
3364 }
3365 else
3366 {
3367 filterfds[slot][0] = -1;
3368 filterfds[slot][1] = -1;
bd7854cb 3369
e1d6a774 3370 if (job->current_file == job->num_files)
3371 {
3372 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3373 "start_job: Closing print pipes [ %d %d ]...",
3374 job->print_pipes[0], job->print_pipes[1]);
bd7854cb 3375
e1d6a774 3376 cupsdClosePipe(job->print_pipes);
89d46774 3377
3378 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3379 "start_job: Closing status output pipe %d...",
3380 job->status_pipes[1]);
3381
3382 close(job->status_pipes[1]);
3383 job->status_pipes[1] = -1;
bd7854cb 3384 }
e1d6a774 3385 }
bd7854cb 3386
89d46774 3387 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3388 "start_job: Closing filter pipes for slot %d "
3389 "[ %d %d ]...",
3390 slot, filterfds[slot][0], filterfds[slot][1]);
3391 cupsdClosePipe(filterfds[slot]);
bd7854cb 3392
d09495fa 3393 if (printer->remote && job->num_files > 1)
e1d6a774 3394 {
3395 for (i = 0; i < job->num_files; i ++)
3396 free(argv[i + 6]);
3397 }
bd7854cb 3398
e1d6a774 3399 free(argv);
ef416fc2 3400
e1d6a774 3401 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3402 "start_job: Adding fd %d to InputSet...",
3403 job->status_buffer->fd);
ef416fc2 3404
e1d6a774 3405 FD_SET(job->status_buffer->fd, InputSet);
ef416fc2 3406
e1d6a774 3407 cupsdAddEvent(CUPSD_EVENT_JOB_STATE, job->printer, job, "Job #%d started.",
3408 job->id);
ef416fc2 3409
e1d6a774 3410 return;
ef416fc2 3411
3412
3413 /*
e1d6a774 3414 * If we get here, we need to abort the current job and close out all
3415 * files and pipes...
ef416fc2 3416 */
3417
e1d6a774 3418 abort_job:
ef416fc2 3419
e1d6a774 3420 for (slot = 0; slot < 2; slot ++)
3421 {
3422 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3423 "start_job: Closing filter pipes for slot %d "
3424 "[ %d %d ]...",
3425 slot, filterfds[slot][0], filterfds[slot][1]);
3426 cupsdClosePipe(filterfds[slot]);
3427 }
ef416fc2 3428
e1d6a774 3429 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3430 "start_job: Closing status pipes [ %d %d ]...",
89d46774 3431 job->status_pipes[0], job->status_pipes[1]);
3432 cupsdClosePipe(job->status_pipes);
3433 cupsdStatBufDelete(job->status_buffer);
ef416fc2 3434
e1d6a774 3435 cupsArrayDelete(filters);
ef416fc2 3436
d09495fa 3437 if (printer->remote && job->num_files > 1)
e1d6a774 3438 {
3439 for (i = 0; i < job->num_files; i ++)
3440 free(argv[i + 6]);
3441 }
ef416fc2 3442
e1d6a774 3443 free(argv);
ef416fc2 3444
e1d6a774 3445 cupsdStopJob(job, 0);
3446}
ef416fc2 3447
e1d6a774 3448
3449/*
3450 * 'unload_job()' - Unload a job from memory.
3451 */
3452
3453static void
3454unload_job(cupsd_job_t *job) /* I - Job */
3455{
3456 if (!job->attrs)
3457 return;
3458
3459 cupsdLogMessage(CUPSD_LOG_DEBUG, "Unloading job %d...", job->id);
3460
3461 ippDelete(job->attrs);
3462
3463 job->attrs = NULL;
3464 job->state = NULL;
3465 job->sheets = NULL;
3466 job->job_sheets = NULL;
ef416fc2 3467}
3468
3469
3470/*
2abf387c 3471 * End of "$Id: job.c 6032 2006-10-12 19:19:47Z mike $".
ef416fc2 3472 */