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