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