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