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