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