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