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