]> git.ipfire.org Git - thirdparty/cups.git/blame - scheduler/job.c
Save work
[thirdparty/cups.git] / scheduler / job.c
CommitLineData
ef416fc2 1/*
f2d18633 2 * "$Id$"
ef416fc2 3 *
462b6e74 4 * Job management routines for the CUPS scheduler.
ef416fc2 5 *
7e86f2f6 6 * Copyright 2007-2014 by Apple Inc.
462b6e74 7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
ef416fc2 8 *
462b6e74
MS
9 * These coded instructions, statements, and computer programs are the
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
16/*
17 * Include necessary headers...
18 */
19
20#include "cupsd.h"
21#include <grp.h>
22#include <cups/backend.h>
23#include <cups/dir.h>
88f9aafc
MS
24#ifdef __APPLE__
25# include <IOKit/pwr_mgt/IOPMLib.h>
26# ifdef HAVE_IOKIT_PWR_MGT_IOPMLIBPRIVATE_H
27# include <IOKit/pwr_mgt/IOPMLibPrivate.h>
28# endif /* HAVE_IOKIT_PWR_MGT_IOPMLIBPRIVATE_H */
29#endif /* __APPLE__ */
ef416fc2 30
31
b9faaae1
MS
32/*
33 * Design Notes for Job Management
34 * -------------------------------
35 *
36 * STATE CHANGES
37 *
38 * pending Do nothing/check jobs
39 * pending-held Send SIGTERM to filters and backend
40 * processing Do nothing/start job
41 * stopped Send SIGKILL to filters and backend
42 * canceled Send SIGTERM to filters and backend
43 * aborted Finalize
44 * completed Finalize
45 *
46 * Finalize clears the printer <-> job association, deletes the status
47 * buffer, closes all of the pipes, etc. and doesn't get run until all of
48 * the print processes are finished.
49 *
50 * UNLOADING OF JOBS (cupsdUnloadCompletedJobs)
51 *
52 * We unload the job attributes when they are not needed to reduce overall
53 * memory consumption. We don't unload jobs where job->state_value <
54 * IPP_JOB_STOPPED, job->printer != NULL, or job->access_time is recent.
55 *
56 * STARTING OF JOBS (start_job)
57 *
58 * When a job is started, a status buffer, several pipes, a security
59 * profile, and a backend process are created for the life of that job.
60 * These are shared for every file in a job. For remote print jobs, the
61 * IPP backend is provided with every file in the job and no filters are
62 * run.
63 *
64 * The job->printer member tracks which printer is printing a job, which
65 * can be different than the destination in job->dest for classes. The
66 * printer object also has a job pointer to track which job is being
67 * printed.
68 *
69 * PRINTING OF JOB FILES (cupsdContinueJob)
70 *
71 * Each file in a job is filtered by 0 or more programs. After getting the
72 * list of filters needed and the total cost, the job is either passed or
73 * put back to the processing state until the current FilterLevel comes down
74 * enough to allow printing.
75 *
76 * If we can print, we build a string for the print options and run each of
77 * the filters, piping the output from one into the next.
78 *
79 * JOB STATUS UPDATES (update_job)
80 *
81 * The update_job function gets called whenever there are pending messages
82 * on the status pipe. These generally are updates to the marker-*,
83 * printer-state-message, or printer-state-reasons attributes. On EOF,
84 * finalize_job is called to clean up.
85 *
86 * FINALIZING JOBS (finalize_job)
87 *
88 * When all filters and the backend are done, we set the job state to
89 * completed (no errors), aborted (filter errors or abort-job policy),
90 * pending-held (auth required or retry-job policy), or pending
91 * (retry-current-job or stop-printer policies) as appropriate.
92 *
93 * Then we close the pipes and free the status buffers and profiles.
94 *
95 * JOB FILE COMPLETION (process_children in main.c)
96 *
97 * For multiple-file jobs, process_children (in main.c) sees that all
98 * filters have exited and calls in to print the next file if there are
99 * more files in the job, otherwise it waits for the backend to exit and
100 * update_job to do the cleanup.
101 */
102
103
ef416fc2 104/*
105 * Local globals...
106 */
107
108static mime_filter_t gziptoany_filter =
109 {
110 NULL, /* Source type */
111 NULL, /* Destination type */
112 0, /* Cost */
113 "gziptoany" /* Filter program to run */
114 };
115
116
117/*
118 * Local functions...
119 */
120
121static int compare_active_jobs(void *first, void *second, void *data);
eec1fbc3 122static int compare_completed_jobs(void *first, void *second, void *data);
ef416fc2 123static int compare_jobs(void *first, void *second, void *data);
178cb736 124static void dump_job_history(cupsd_job_t *job);
ef55b745 125static void finalize_job(cupsd_job_t *job, int set_job_state);
178cb736 126static void free_job_history(cupsd_job_t *job);
b9faaae1
MS
127static char *get_options(cupsd_job_t *job, int banner_page, char *copies,
128 size_t copies_size, char *title,
129 size_t title_size);
c7017ecc 130static size_t ipp_length(ipp_t *ipp);
bd7854cb 131static void load_job_cache(const char *filename);
132static void load_next_job_id(const char *filename);
133static void load_request_root(void);
82cc1f9a
MS
134static void remove_job_files(cupsd_job_t *job);
135static void remove_job_history(cupsd_job_t *job);
b9faaae1 136static void set_time(cupsd_job_t *job, const char *name);
e1d6a774 137static void start_job(cupsd_job_t *job, cupsd_printer_t *printer);
b9faaae1 138static void stop_job(cupsd_job_t *job, cupsd_jobaction_t action);
e1d6a774 139static void unload_job(cupsd_job_t *job);
f899b121 140static void update_job(cupsd_job_t *job);
4509bb49 141static void update_job_attrs(cupsd_job_t *job, int do_message);
ef416fc2 142
143
144/*
b9faaae1 145 * 'cupsdAddJob()' - Add a new job to the job queue.
ef416fc2 146 */
147
148cupsd_job_t * /* O - New job record */
149cupsdAddJob(int priority, /* I - Job priority */
150 const char *dest) /* I - Job destination */
151{
152 cupsd_job_t *job; /* New job record */
153
154
91c84a35
MS
155 if ((job = calloc(sizeof(cupsd_job_t), 1)) == NULL)
156 return (NULL);
ef416fc2 157
89d46774 158 job->id = NextJobId ++;
159 job->priority = priority;
160 job->back_pipes[0] = -1;
161 job->back_pipes[1] = -1;
162 job->print_pipes[0] = -1;
163 job->print_pipes[1] = -1;
f7deaa1a 164 job->side_pipes[0] = -1;
165 job->side_pipes[1] = -1;
89d46774 166 job->status_pipes[0] = -1;
167 job->status_pipes[1] = -1;
ef416fc2 168
169 cupsdSetString(&job->dest, dest);
170
171 /*
172 * Add the new job to the "all jobs" and "active jobs" lists...
173 */
174
175 cupsArrayAdd(Jobs, job);
176 cupsArrayAdd(ActiveJobs, job);
177
178 return (job);
179}
180
181
182/*
b9faaae1 183 * 'cupsdCancelJobs()' - Cancel all jobs for the given destination/user.
ef416fc2 184 */
185
186void
187cupsdCancelJobs(const char *dest, /* I - Destination to cancel */
188 const char *username, /* I - Username or NULL */
189 int purge) /* I - Purge jobs? */
190{
191 cupsd_job_t *job; /* Current job */
192
193
194 for (job = (cupsd_job_t *)cupsArrayFirst(Jobs);
195 job;
196 job = (cupsd_job_t *)cupsArrayNext(Jobs))
0a682745 197 {
b9faaae1 198 if ((!job->dest || !job->username) && !cupsdLoadJob(job))
0a682745
MS
199 continue;
200
b9faaae1
MS
201 if ((!dest || !strcmp(job->dest, dest)) &&
202 (!username || !strcmp(job->username, username)))
ef416fc2 203 {
204 /*
205 * Cancel all jobs matching this destination/user...
206 */
207
b9faaae1
MS
208 if (purge)
209 cupsdSetJobState(job, IPP_JOB_CANCELED, CUPSD_JOB_PURGE,
210 "Job purged by user.");
41681883 211 else if (job->state_value < IPP_JOB_CANCELED)
b9faaae1
MS
212 cupsdSetJobState(job, IPP_JOB_CANCELED, CUPSD_JOB_DEFAULT,
213 "Job canceled by user.");
ef416fc2 214 }
0a682745 215 }
ef416fc2 216
217 cupsdCheckJobs();
218}
219
220
221/*
222 * 'cupsdCheckJobs()' - Check the pending jobs and start any if the destination
223 * is available.
224 */
225
226void
227cupsdCheckJobs(void)
228{
229 cupsd_job_t *job; /* Current job in queue */
230 cupsd_printer_t *printer, /* Printer destination */
231 *pclass; /* Printer class destination */
ac884b6a 232 ipp_attribute_t *attr; /* Job attribute */
238c3832 233 time_t curtime; /* Current time */
ef416fc2 234
235
238c3832
MS
236 curtime = time(NULL);
237
a29fd7dd
MS
238 cupsdLogMessage(CUPSD_LOG_DEBUG2,
239 "cupsdCheckJobs: %d active jobs, sleeping=%d, reload=%d, "
240 "curtime=%ld", cupsArrayCount(ActiveJobs), Sleeping,
241 NeedReload, (long)curtime);
242
ef416fc2 243 for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs);
244 job;
245 job = (cupsd_job_t *)cupsArrayNext(ActiveJobs))
246 {
a29fd7dd
MS
247 cupsdLogMessage(CUPSD_LOG_DEBUG2,
248 "cupsdCheckJobs: Job %d - dest=\"%s\", printer=%p, "
249 "state=%d, cancel_time=%ld, hold_until=%ld, kill_time=%ld, "
250 "pending_cost=%d, pending_timeout=%ld", job->id, job->dest,
251 job->printer, job->state_value, (long)job->cancel_time,
252 (long)job->hold_until, (long)job->kill_time,
253 job->pending_cost, (long)job->pending_timeout);
254
238c3832
MS
255 /*
256 * Kill jobs if they are unresponsive...
257 */
258
259 if (job->kill_time && job->kill_time <= curtime)
260 {
581dae2d
MS
261 if (!job->completed)
262 cupsdLogJob(job, CUPSD_LOG_ERROR, "Stopping unresponsive job.");
10d09e33 263
238c3832
MS
264 stop_job(job, CUPSD_JOB_FORCE);
265 continue;
266 }
267
dcb445bc
MS
268 /*
269 * Cancel stuck jobs...
270 */
271
272 if (job->cancel_time && job->cancel_time <= curtime)
273 {
3aec6cf0
MS
274 int cancel_after; /* job-cancel-after value */
275
276 attr = ippFindAttribute(job->attrs, "job-cancel-after", IPP_TAG_INTEGER);
277 cancel_after = attr ? ippGetInteger(attr, 0) : MaxJobTime;
e3dfbe13 278
581dae2d 279 if (job->completed)
e3dfbe13 280 cupsdSetJobState(job, IPP_JOB_CANCELED, CUPSD_JOB_FORCE, "Marking stuck job as completed after %d seconds.", cancel_after);
581dae2d 281 else
e3dfbe13 282 cupsdSetJobState(job, IPP_JOB_CANCELED, CUPSD_JOB_DEFAULT, "Canceling stuck job after %d seconds.", cancel_after);
dcb445bc
MS
283 continue;
284 }
285
ef416fc2 286 /*
287 * Start held jobs if they are ready...
288 */
289
bd7854cb 290 if (job->state_value == IPP_JOB_HELD &&
ef416fc2 291 job->hold_until &&
238c3832 292 job->hold_until < curtime)
bd7854cb 293 {
09a101d6 294 if (job->pending_timeout)
91c84a35 295 {
dfd5680b
MS
296 /*
297 * This job is pending; check that we don't have an active Send-Document
298 * operation in progress on any of the client connections, then timeout
299 * the job so we can start printing...
300 */
301
302 cupsd_client_t *con; /* Current client connection */
303
dfd5680b
MS
304 for (con = (cupsd_client_t *)cupsArrayFirst(Clients);
305 con;
306 con = (cupsd_client_t *)cupsArrayNext(Clients))
307 if (con->request &&
308 con->request->request.op.operation_id == IPP_SEND_DOCUMENT)
309 break;
310
311 if (con)
312 continue;
313
91c84a35
MS
314 if (cupsdTimeoutJob(job))
315 continue;
316 }
09a101d6 317
b9faaae1
MS
318 cupsdSetJobState(job, IPP_JOB_PENDING, CUPSD_JOB_DEFAULT,
319 "Job submission timed out.");
bd7854cb 320 }
ef416fc2 321
e07d4801
MS
322 /*
323 * Continue jobs that are waiting on the FilterLimit...
324 */
325
326 if (job->pending_cost > 0 &&
327 ((FilterLevel + job->pending_cost) < FilterLimit || FilterLevel == 0))
328 cupsdContinueJob(job);
329
ef416fc2 330 /*
331 * Start pending jobs if the destination is available...
332 */
333
88f9aafc 334 if (job->state_value == IPP_JOB_PENDING && !NeedReload &&
581dae2d 335 !Sleeping && !DoingShutdown && !job->printer)
ef416fc2 336 {
337 printer = cupsdFindDest(job->dest);
338 pclass = NULL;
339
a2326b5b 340 while (printer && (printer->type & CUPS_PRINTER_CLASS))
ef416fc2 341 {
342 /*
343 * If the class is remote, just pass it to the remote server...
344 */
345
346 pclass = printer;
347
c0e1af83 348 if (pclass->state == IPP_PRINTER_STOPPED)
b86bc4cf 349 printer = NULL;
c0e1af83 350 else if (pclass->type & CUPS_PRINTER_REMOTE)
351 break;
352 else
353 printer = cupsdFindAvailablePrinter(printer->name);
ef416fc2 354 }
355
356 if (!printer && !pclass)
357 {
358 /*
359 * Whoa, the printer and/or class for this destination went away;
360 * cancel the job...
361 */
362
b9faaae1
MS
363 cupsdSetJobState(job, IPP_JOB_ABORTED, CUPSD_JOB_PURGE,
364 "Job aborted because the destination printer/class "
365 "has gone away.");
ef416fc2 366 }
61cf44e2 367 else if (printer && !printer->holding_new_jobs)
ef416fc2 368 {
369 /*
370 * See if the printer is available or remote and not printing a job;
371 * if so, start the job...
372 */
373
374 if (pclass)
375 {
376 /*
377 * Add/update a job-actual-printer-uri attribute for this job
378 * so that we know which printer actually printed the job...
379 */
380
ef416fc2 381 if ((attr = ippFindAttribute(job->attrs, "job-actual-printer-uri",
382 IPP_TAG_URI)) != NULL)
383 cupsdSetString(&attr->values[0].string.text, printer->uri);
384 else
385 ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_URI,
386 "job-actual-printer-uri", NULL, printer->uri);
3dfe78b3
MS
387
388 job->dirty = 1;
389 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
ef416fc2 390 }
391
a29fd7dd 392 if (!printer->job && printer->state == IPP_PRINTER_IDLE)
bc44d920 393 {
bc44d920 394 /*
395 * Start the job...
396 */
397
e1d6a774 398 start_job(job, printer);
bc44d920 399 }
ef416fc2 400 }
401 }
402 }
403}
404
405
406/*
407 * 'cupsdCleanJobs()' - Clean out old jobs.
408 */
409
410void
411cupsdCleanJobs(void)
412{
413 cupsd_job_t *job; /* Current job */
82cc1f9a
MS
414 time_t curtime; /* Current time */
415
ef416fc2 416
82cc1f9a
MS
417 cupsdLogMessage(CUPSD_LOG_DEBUG2,
418 "cupsdCleanJobs: MaxJobs=%d, JobHistory=%d, JobFiles=%d",
419 MaxJobs, JobHistory, JobFiles);
ef416fc2 420
82cc1f9a 421 if (MaxJobs <= 0 && JobHistory == INT_MAX && JobFiles == INT_MAX)
ef416fc2 422 return;
423
82cc1f9a
MS
424 curtime = time(NULL);
425 JobHistoryUpdate = 0;
426
ef416fc2 427 for (job = (cupsd_job_t *)cupsArrayFirst(Jobs);
82cc1f9a 428 job;
ef416fc2 429 job = (cupsd_job_t *)cupsArrayNext(Jobs))
82cc1f9a 430 {
b9faaae1 431 if (job->state_value >= IPP_JOB_CANCELED && !job->printer)
82cc1f9a
MS
432 {
433 /*
434 * Expire old jobs (or job files)...
435 */
436
437 if ((MaxJobs > 0 && cupsArrayCount(Jobs) >= MaxJobs) ||
438 (job->history_time && job->history_time <= curtime))
439 {
440 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Removing from history.");
441 cupsdDeleteJob(job, CUPSD_JOB_PURGE);
442 }
443 else if (job->file_time && job->file_time <= curtime)
444 {
445 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Removing document files.");
446 remove_job_files(job);
447
448 if (job->history_time < JobHistoryUpdate || !JobHistoryUpdate)
449 JobHistoryUpdate = job->history_time;
450 }
451 else
452 {
453 if (job->history_time < JobHistoryUpdate || !JobHistoryUpdate)
454 JobHistoryUpdate = job->history_time;
455
456 if (job->file_time < JobHistoryUpdate || !JobHistoryUpdate)
457 JobHistoryUpdate = job->file_time;
458 }
459 }
460 }
461
462 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdCleanJobs: JobHistoryUpdate=%ld",
463 (long)JobHistoryUpdate);
ef416fc2 464}
465
466
3dfe78b3 467/*
b9faaae1 468 * 'cupsdContinueJob()' - Continue printing with the next file in a job.
3dfe78b3
MS
469 */
470
471void
b9faaae1 472cupsdContinueJob(cupsd_job_t *job) /* I - Job */
3dfe78b3 473{
b9faaae1
MS
474 int i; /* Looping var */
475 int slot; /* Pipe slot */
e07d4801 476 cups_array_t *filters = NULL,/* Filters for job */
b9faaae1
MS
477 *prefilters; /* Filters with prefilters */
478 mime_filter_t *filter, /* Current filter */
479 *prefilter, /* Prefilter */
480 port_monitor; /* Port monitor filter */
481 char scheme[255]; /* Device URI scheme */
482 ipp_attribute_t *attr; /* Current attribute */
483 const char *ptr, /* Pointer into value */
484 *abort_message; /* Abort message */
f11a948a 485 ipp_jstate_t abort_state = IPP_JOB_STOPPED;
e07d4801 486 /* New job state on abort */
b9faaae1
MS
487 struct stat backinfo; /* Backend file information */
488 int backroot; /* Run backend as root? */
489 int pid; /* Process ID of new filter process */
490 int banner_page; /* 1 if banner page, 0 otherwise */
e07d4801
MS
491 int filterfds[2][2] = { { -1, -1 }, { -1, -1 } };
492 /* Pipes used between filters */
b9faaae1 493 int envc; /* Number of environment variables */
771bd8cb 494 struct stat fileinfo; /* Job file information */
59ac438c 495 int argc; /* Number of arguments */
e07d4801 496 char **argv = NULL, /* Filter command-line arguments */
b9faaae1
MS
497 filename[1024], /* Job filename */
498 command[1024], /* Full path to command */
499 jobid[255], /* Job ID string */
500 title[IPP_MAX_NAME],
501 /* Job title string */
502 copies[255], /* # copies string */
503 *options, /* Options string */
eac3a0a0 504 *envp[MAX_ENV + 21],
b9faaae1
MS
505 /* Environment variables */
506 charset[255], /* CHARSET env variable */
507 class_name[255],/* CLASS env variable */
508 classification[1024],
509 /* CLASSIFICATION env variable */
510 content_type[1024],
511 /* CONTENT_TYPE env variable */
512 device_uri[1024],
513 /* DEVICE_URI env variable */
6961465f 514 final_content_type[1024] = "",
b9faaae1
MS
515 /* FINAL_CONTENT_TYPE env variable */
516 lang[255], /* LANG env variable */
517#ifdef __APPLE__
518 apple_language[255],
519 /* APPLE_LANGUAGE env variable */
520#endif /* __APPLE__ */
22c9029b
MS
521 auth_info_required[255],
522 /* AUTH_INFO_REQUIRED env variable */
b9faaae1
MS
523 ppd[1024], /* PPD env variable */
524 printer_info[255],
525 /* PRINTER_INFO env variable */
526 printer_location[255],
527 /* PRINTER_LOCATION env variable */
528 printer_name[255],
529 /* PRINTER env variable */
eac3a0a0
MS
530 *printer_state_reasons = NULL,
531 /* PRINTER_STATE_REASONS env var */
b9faaae1
MS
532 rip_max_cache[255];
533 /* RIP_MAX_CACHE env variable */
534
535
536 cupsdLogMessage(CUPSD_LOG_DEBUG2,
537 "cupsdContinueJob(job=%p(%d)): current_file=%d, num_files=%d",
538 job, job->id, job->current_file, job->num_files);
3dfe78b3 539
3dfe78b3 540 /*
b9faaae1
MS
541 * Figure out what filters are required to convert from
542 * the source to the destination type...
3dfe78b3
MS
543 */
544
b9faaae1 545 FilterLevel -= job->cost;
3dfe78b3 546
e07d4801
MS
547 job->cost = 0;
548 job->pending_cost = 0;
549
550 memset(job->filters, 0, sizeof(job->filters));
551
b9faaae1 552 if (job->printer->raw)
3dfe78b3 553 {
b9faaae1
MS
554 /*
555 * Remote jobs and raw queues go directly to the printer without
556 * filtering...
557 */
3dfe78b3 558
b9faaae1 559 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Sending job to queue tagged as raw...");
b9faaae1
MS
560 }
561 else
562 {
563 /*
564 * Local jobs get filtered...
565 */
3dfe78b3 566
8e67b75b
MS
567 mime_type_t *dst = job->printer->filetype;
568 /* Destination file type */
569
771bd8cb
MS
570 snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot,
571 job->id, job->current_file + 1);
572 if (stat(filename, &fileinfo))
573 fileinfo.st_size = 0;
574
8e67b75b
MS
575 if (job->retry_as_raster)
576 {
577 /*
578 * Need to figure out whether the printer supports image/pwg-raster or
579 * image/urf, and use the corresponding type...
580 */
581
582 char type[MIME_MAX_TYPE]; /* MIME media type for printer */
583
584 snprintf(type, sizeof(type), "%s/image/urf", job->printer->name);
585 if ((dst = mimeType(MimeDatabase, "printer", type)) == NULL)
586 {
587 snprintf(type, sizeof(type), "%s/image/pwg-raster", job->printer->name);
588 dst = mimeType(MimeDatabase, "printer", type);
589 }
590
591 if (dst)
592 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Retrying job as \"%s\".", strchr(dst->type, '/') + 1);
593 else
594 cupsdLogJob(job, CUPSD_LOG_ERROR, "Unable to retry job using a supported raster format.");
595 }
596
597 filters = mimeFilter2(MimeDatabase, job->filetypes[job->current_file], (size_t)fileinfo.st_size, dst, &(job->cost));
ef416fc2 598
b9faaae1
MS
599 if (!filters)
600 {
601 cupsdLogJob(job, CUPSD_LOG_ERROR,
12f89d24 602 "Unable to convert file %d to printable format.",
b9faaae1 603 job->current_file);
ef416fc2 604
e07d4801 605 abort_message = "Aborting job because it cannot be printed.";
f11a948a
MS
606 abort_state = IPP_JOB_ABORTED;
607
12f89d24 608 ippSetString(job->attrs, &job->reasons, 0, "document-unprintable-error");
e07d4801 609 goto abort_job;
b9faaae1 610 }
ef416fc2 611
6961465f
MS
612 /*
613 * Figure out the final content type...
614 */
615
616 cupsdLogJob(job, CUPSD_LOG_DEBUG, "%d filters for job:",
617 cupsArrayCount(filters));
618 for (filter = (mime_filter_t *)cupsArrayFirst(filters);
619 filter;
620 filter = (mime_filter_t *)cupsArrayNext(filters))
621 cupsdLogJob(job, CUPSD_LOG_DEBUG, "%s (%s/%s to %s/%s, cost %d)",
622 filter->filter,
623 filter->src ? filter->src->super : "???",
624 filter->src ? filter->src->type : "???",
625 filter->dst ? filter->dst->super : "???",
626 filter->dst ? filter->dst->type : "???",
627 filter->cost);
628
629 if (!job->printer->remote)
630 {
631 for (filter = (mime_filter_t *)cupsArrayLast(filters);
632 filter && filter->dst;
633 filter = (mime_filter_t *)cupsArrayPrev(filters))
634 if (strcmp(filter->dst->super, "printer") ||
635 strcmp(filter->dst->type, job->printer->name))
636 break;
637
638 if (filter && filter->dst)
639 {
640 if ((ptr = strchr(filter->dst->type, '/')) != NULL)
641 snprintf(final_content_type, sizeof(final_content_type),
642 "FINAL_CONTENT_TYPE=%s", ptr + 1);
643 else
644 snprintf(final_content_type, sizeof(final_content_type),
645 "FINAL_CONTENT_TYPE=%s/%s", filter->dst->super,
646 filter->dst->type);
647 }
54f63cdf
MS
648 else
649 snprintf(final_content_type, sizeof(final_content_type),
650 "FINAL_CONTENT_TYPE=printer/%s", job->printer->name);
6961465f
MS
651 }
652
ef416fc2 653 /*
b9faaae1 654 * Remove NULL ("-") filters...
ef416fc2 655 */
656
b9faaae1
MS
657 for (filter = (mime_filter_t *)cupsArrayFirst(filters);
658 filter;
659 filter = (mime_filter_t *)cupsArrayNext(filters))
660 if (!strcmp(filter->filter, "-"))
661 cupsArrayRemove(filters, filter);
ef416fc2 662
b9faaae1
MS
663 if (cupsArrayCount(filters) == 0)
664 {
665 cupsArrayDelete(filters);
666 filters = NULL;
667 }
ef416fc2 668
b9faaae1
MS
669 /*
670 * If this printer has any pre-filters, insert the required pre-filter
671 * in the filters array...
672 */
ef416fc2 673
b9faaae1
MS
674 if (job->printer->prefiltertype && filters)
675 {
676 prefilters = cupsArrayNew(NULL, NULL);
677
678 for (filter = (mime_filter_t *)cupsArrayFirst(filters);
679 filter;
680 filter = (mime_filter_t *)cupsArrayNext(filters))
681 {
682 if ((prefilter = mimeFilterLookup(MimeDatabase, filter->src,
683 job->printer->prefiltertype)))
684 {
685 cupsArrayAdd(prefilters, prefilter);
686 job->cost += prefilter->cost;
687 }
ef416fc2 688
b9faaae1
MS
689 cupsArrayAdd(prefilters, filter);
690 }
e00b005a 691
b9faaae1
MS
692 cupsArrayDelete(filters);
693 filters = prefilters;
694 }
695 }
bc44d920 696
c168a833 697 /*
b9faaae1
MS
698 * Set a minimum cost of 100 for all jobs so that FilterLimit
699 * works with raw queues and other low-cost paths.
c168a833
MS
700 */
701
b9faaae1
MS
702 if (job->cost < 100)
703 job->cost = 100;
c168a833 704
745129be 705 /*
b9faaae1 706 * See if the filter cost is too high...
745129be
MS
707 */
708
b9faaae1
MS
709 if ((FilterLevel + job->cost) > FilterLimit && FilterLevel > 0 &&
710 FilterLimit > 0)
ef416fc2 711 {
712 /*
b9faaae1 713 * Don't print this job quite yet...
ef416fc2 714 */
715
b9faaae1 716 cupsArrayDelete(filters);
a74454a7 717
b9faaae1
MS
718 cupsdLogJob(job, CUPSD_LOG_INFO,
719 "Holding because filter limit has been reached.");
720 cupsdLogJob(job, CUPSD_LOG_DEBUG2,
721 "cupsdContinueJob: file=%d, cost=%d, level=%d, limit=%d",
722 job->current_file, job->cost, FilterLevel,
723 FilterLimit);
e07d4801
MS
724
725 job->pending_cost = job->cost;
726 job->cost = 0;
b9faaae1
MS
727 return;
728 }
a74454a7 729
b9faaae1 730 FilterLevel += job->cost;
a74454a7 731
b9faaae1
MS
732 /*
733 * Add decompression/raw filter as needed...
734 */
a74454a7 735
462b6e74
MS
736 if (job->compressions[job->current_file] &&
737 (!job->printer->remote || job->num_files == 1))
b9faaae1 738 {
a74454a7 739 /*
b9faaae1 740 * Add gziptoany filter to the front of the list...
a74454a7 741 */
742
b9faaae1
MS
743 if (!filters)
744 filters = cupsArrayNew(NULL, NULL);
ef416fc2 745
b9faaae1
MS
746 if (!cupsArrayInsert(filters, &gziptoany_filter))
747 {
748 cupsdLogJob(job, CUPSD_LOG_DEBUG,
749 "Unable to add decompression filter - %s", strerror(errno));
ed486911 750
b9faaae1 751 cupsArrayDelete(filters);
ed486911 752
e07d4801 753 abort_message = "Stopping job because the scheduler ran out of memory.";
ed486911 754
e07d4801 755 goto abort_job;
b9faaae1
MS
756 }
757 }
ef416fc2 758
b9faaae1
MS
759 /*
760 * Add port monitor, if any...
761 */
ef416fc2 762
b9faaae1
MS
763 if (job->printer->port_monitor)
764 {
765 /*
766 * Add port monitor to the end of the list...
767 */
ef416fc2 768
b9faaae1
MS
769 if (!filters)
770 filters = cupsArrayNew(NULL, NULL);
ef416fc2 771
b9faaae1
MS
772 port_monitor.src = NULL;
773 port_monitor.dst = NULL;
774 port_monitor.cost = 0;
ef416fc2 775
b9faaae1
MS
776 snprintf(port_monitor.filter, sizeof(port_monitor.filter),
777 "%s/monitor/%s", ServerBin, job->printer->port_monitor);
ef416fc2 778
b9faaae1
MS
779 if (!cupsArrayAdd(filters, &port_monitor))
780 {
781 cupsdLogJob(job, CUPSD_LOG_DEBUG,
782 "Unable to add port monitor - %s", strerror(errno));
ef416fc2 783
e07d4801 784 abort_message = "Stopping job because the scheduler ran out of memory.";
07725fee 785
e07d4801 786 goto abort_job;
b9faaae1
MS
787 }
788 }
ef416fc2 789
b9faaae1
MS
790 /*
791 * Make sure we don't go over the "MAX_FILTERS" limit...
792 */
ef416fc2 793
b9faaae1
MS
794 if (cupsArrayCount(filters) > MAX_FILTERS)
795 {
796 cupsdLogJob(job, CUPSD_LOG_DEBUG,
12f89d24 797 "Too many filters (%d > %d), unable to print.",
b9faaae1 798 cupsArrayCount(filters), MAX_FILTERS);
ef416fc2 799
e07d4801 800 abort_message = "Aborting job because it needs too many filters to print.";
f11a948a
MS
801 abort_state = IPP_JOB_ABORTED;
802
12f89d24
MS
803 ippSetString(job->attrs, &job->reasons, 0, "document-unprintable-error");
804
e07d4801 805 goto abort_job;
b9faaae1 806 }
07725fee 807
b9faaae1
MS
808 /*
809 * Determine if we are printing a banner page or not...
810 */
07725fee 811
b9faaae1
MS
812 if (job->job_sheets == NULL)
813 {
814 cupsdLogJob(job, CUPSD_LOG_DEBUG, "No job-sheets attribute.");
815 if ((job->job_sheets =
816 ippFindAttribute(job->attrs, "job-sheets", IPP_TAG_ZERO)) != NULL)
817 cupsdLogJob(job, CUPSD_LOG_DEBUG,
12f89d24 818 "... but someone added one without setting job_sheets.");
b9faaae1
MS
819 }
820 else if (job->job_sheets->num_values == 1)
821 cupsdLogJob(job, CUPSD_LOG_DEBUG, "job-sheets=%s",
822 job->job_sheets->values[0].string.text);
823 else
824 cupsdLogJob(job, CUPSD_LOG_DEBUG, "job-sheets=%s,%s",
825 job->job_sheets->values[0].string.text,
826 job->job_sheets->values[1].string.text);
07725fee 827
a2326b5b 828 if (job->printer->type & CUPS_PRINTER_REMOTE)
b9faaae1
MS
829 banner_page = 0;
830 else if (job->job_sheets == NULL)
831 banner_page = 0;
88f9aafc 832 else if (_cups_strcasecmp(job->job_sheets->values[0].string.text, "none") != 0 &&
b9faaae1
MS
833 job->current_file == 0)
834 banner_page = 1;
835 else if (job->job_sheets->num_values > 1 &&
88f9aafc 836 _cups_strcasecmp(job->job_sheets->values[1].string.text, "none") != 0 &&
b9faaae1
MS
837 job->current_file == (job->num_files - 1))
838 banner_page = 1;
839 else
840 banner_page = 0;
07725fee 841
b9faaae1
MS
842 if ((options = get_options(job, banner_page, copies, sizeof(copies), title,
843 sizeof(title))) == NULL)
844 {
e07d4801 845 abort_message = "Stopping job because the scheduler ran out of memory.";
ef416fc2 846
e07d4801 847 goto abort_job;
b9faaae1 848 }
ef416fc2 849
b9faaae1
MS
850 /*
851 * Build the command-line arguments for the filters. Each filter
852 * has 6 or 7 arguments:
853 *
854 * argv[0] = printer
855 * argv[1] = job ID
856 * argv[2] = username
857 * argv[3] = title
858 * argv[4] = # copies
859 * argv[5] = options
860 * argv[6] = filename (optional; normally stdin)
861 *
862 * This allows legacy printer drivers that use the old System V
863 * printing interface to be used by CUPS.
864 *
865 * For remote jobs, we send all of the files in the argument list.
866 */
07725fee 867
b9faaae1 868 if (job->printer->remote)
59ac438c 869 argc = 6 + job->num_files;
b9faaae1 870 else
59ac438c 871 argc = 7;
07725fee 872
7e86f2f6 873 if ((argv = calloc((size_t)argc + 1, sizeof(char *))) == NULL)
b9faaae1
MS
874 {
875 cupsdLogMessage(CUPSD_LOG_DEBUG, "Unable to allocate argument array - %s",
876 strerror(errno));
07725fee 877
e07d4801
MS
878 abort_message = "Stopping job because the scheduler ran out of memory.";
879
880 goto abort_job;
b9faaae1 881 }
ef416fc2 882
b9faaae1 883 sprintf(jobid, "%d", job->id);
ef416fc2 884
b9faaae1
MS
885 argv[0] = job->printer->name;
886 argv[1] = jobid;
887 argv[2] = job->username;
888 argv[3] = title;
889 argv[4] = copies;
890 argv[5] = options;
ef416fc2 891
b9faaae1 892 if (job->printer->remote && job->num_files > 1)
ef416fc2 893 {
b9faaae1
MS
894 for (i = 0; i < job->num_files; i ++)
895 {
896 snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot,
897 job->id, i + 1);
898 argv[6 + i] = strdup(filename);
899 }
ef416fc2 900 }
901 else
902 {
b9faaae1
MS
903 snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot,
904 job->id, job->current_file + 1);
462b6e74 905 argv[6] = strdup(filename);
ef416fc2 906 }
ef416fc2 907
b9faaae1
MS
908 for (i = 0; argv[i]; i ++)
909 cupsdLogJob(job, CUPSD_LOG_DEBUG, "argv[%d]=\"%s\"", i, argv[i]);
ef416fc2 910
b9faaae1
MS
911 /*
912 * Create environment variable strings for the filters...
913 */
bd7854cb 914
b9faaae1
MS
915 attr = ippFindAttribute(job->attrs, "attributes-natural-language",
916 IPP_TAG_LANGUAGE);
ef416fc2 917
b9faaae1 918#ifdef __APPLE__
5a9febac 919 strlcpy(apple_language, "APPLE_LANGUAGE=", sizeof(apple_language));
b9faaae1
MS
920 _cupsAppleLanguage(attr->values[0].string.text,
921 apple_language + 15, sizeof(apple_language) - 15);
922#endif /* __APPLE__ */
ef416fc2 923
b9faaae1 924 switch (strlen(attr->values[0].string.text))
ef416fc2 925 {
b9faaae1
MS
926 default :
927 /*
928 * This is an unknown or badly formatted language code; use
929 * the POSIX locale...
930 */
ef416fc2 931
5a9febac 932 strlcpy(lang, "LANG=C", sizeof(lang));
b9faaae1 933 break;
ef416fc2 934
b9faaae1
MS
935 case 2 :
936 /*
937 * Just the language code (ll)...
938 */
ef416fc2 939
1340db2d 940 snprintf(lang, sizeof(lang), "LANG=%s.UTF-8",
b9faaae1
MS
941 attr->values[0].string.text);
942 break;
ef416fc2 943
b9faaae1
MS
944 case 5 :
945 /*
946 * Language and country code (ll-cc)...
947 */
ef416fc2 948
1340db2d 949 snprintf(lang, sizeof(lang), "LANG=%c%c_%c%c.UTF-8",
b9faaae1
MS
950 attr->values[0].string.text[0],
951 attr->values[0].string.text[1],
952 toupper(attr->values[0].string.text[3] & 255),
953 toupper(attr->values[0].string.text[4] & 255));
954 break;
955 }
ef416fc2 956
b9faaae1
MS
957 if ((attr = ippFindAttribute(job->attrs, "document-format",
958 IPP_TAG_MIMETYPE)) != NULL &&
959 (ptr = strstr(attr->values[0].string.text, "charset=")) != NULL)
960 snprintf(charset, sizeof(charset), "CHARSET=%s", ptr + 8);
961 else
962 strlcpy(charset, "CHARSET=utf-8", sizeof(charset));
ef416fc2 963
b9faaae1
MS
964 snprintf(content_type, sizeof(content_type), "CONTENT_TYPE=%s/%s",
965 job->filetypes[job->current_file]->super,
966 job->filetypes[job->current_file]->type);
967 snprintf(device_uri, sizeof(device_uri), "DEVICE_URI=%s",
968 job->printer->device_uri);
969 snprintf(ppd, sizeof(ppd), "PPD=%s/ppd/%s.ppd", ServerRoot,
970 job->printer->name);
971 snprintf(printer_info, sizeof(printer_name), "PRINTER_INFO=%s",
972 job->printer->info ? job->printer->info : "");
973 snprintf(printer_location, sizeof(printer_name), "PRINTER_LOCATION=%s",
974 job->printer->location ? job->printer->location : "");
975 snprintf(printer_name, sizeof(printer_name), "PRINTER=%s", job->printer->name);
eac3a0a0
MS
976 if (job->printer->num_reasons > 0)
977 {
978 char *psrptr; /* Pointer into PRINTER_STATE_REASONS */
979 size_t psrlen; /* Size of PRINTER_STATE_REASONS */
980
981 for (psrlen = 22, i = 0; i < job->printer->num_reasons; i ++)
982 psrlen += strlen(job->printer->reasons[i]) + 1;
983
984 if ((printer_state_reasons = malloc(psrlen)) != NULL)
985 {
986 /*
987 * All of these strcpy's are safe because we allocated the psr string...
988 */
989
5a9febac 990 strlcpy(printer_state_reasons, "PRINTER_STATE_REASONS=", psrlen);
eac3a0a0
MS
991 for (psrptr = printer_state_reasons + 22, i = 0;
992 i < job->printer->num_reasons;
993 i ++)
994 {
995 if (i)
996 *psrptr++ = ',';
7e86f2f6 997 strlcpy(psrptr, job->printer->reasons[i], psrlen - (size_t)(psrptr - printer_state_reasons));
eac3a0a0
MS
998 psrptr += strlen(psrptr);
999 }
1000 }
1001 }
b9faaae1 1002 snprintf(rip_max_cache, sizeof(rip_max_cache), "RIP_MAX_CACHE=%s", RIPCache);
ef416fc2 1003
22c9029b
MS
1004 if (job->printer->num_auth_info_required == 1)
1005 snprintf(auth_info_required, sizeof(auth_info_required),
1006 "AUTH_INFO_REQUIRED=%s",
1007 job->printer->auth_info_required[0]);
1008 else if (job->printer->num_auth_info_required == 2)
1009 snprintf(auth_info_required, sizeof(auth_info_required),
1010 "AUTH_INFO_REQUIRED=%s,%s",
1011 job->printer->auth_info_required[0],
1012 job->printer->auth_info_required[1]);
1013 else if (job->printer->num_auth_info_required == 3)
1014 snprintf(auth_info_required, sizeof(auth_info_required),
1015 "AUTH_INFO_REQUIRED=%s,%s,%s",
1016 job->printer->auth_info_required[0],
1017 job->printer->auth_info_required[1],
1018 job->printer->auth_info_required[2]);
1019 else if (job->printer->num_auth_info_required == 4)
1020 snprintf(auth_info_required, sizeof(auth_info_required),
1021 "AUTH_INFO_REQUIRED=%s,%s,%s,%s",
1022 job->printer->auth_info_required[0],
1023 job->printer->auth_info_required[1],
1024 job->printer->auth_info_required[2],
1025 job->printer->auth_info_required[3]);
1026 else
1027 strlcpy(auth_info_required, "AUTH_INFO_REQUIRED=none",
1028 sizeof(auth_info_required));
1029
b9faaae1 1030 envc = cupsdLoadEnv(envp, (int)(sizeof(envp) / sizeof(envp[0])));
ef416fc2 1031
b9faaae1
MS
1032 envp[envc ++] = charset;
1033 envp[envc ++] = lang;
1034#ifdef __APPLE__
1035 envp[envc ++] = apple_language;
1036#endif /* __APPLE__ */
1037 envp[envc ++] = ppd;
1038 envp[envc ++] = rip_max_cache;
1039 envp[envc ++] = content_type;
1040 envp[envc ++] = device_uri;
1041 envp[envc ++] = printer_info;
1042 envp[envc ++] = printer_location;
1043 envp[envc ++] = printer_name;
eac3a0a0
MS
1044 envp[envc ++] = printer_state_reasons ? printer_state_reasons :
1045 "PRINTER_STATE_REASONS=none";
b9faaae1
MS
1046 envp[envc ++] = banner_page ? "CUPS_FILETYPE=job-sheet" :
1047 "CUPS_FILETYPE=document";
ef416fc2 1048
6961465f
MS
1049 if (final_content_type[0])
1050 envp[envc ++] = final_content_type;
ef416fc2 1051
b9faaae1
MS
1052 if (Classification && !banner_page)
1053 {
1054 if ((attr = ippFindAttribute(job->attrs, "job-sheets",
1055 IPP_TAG_NAME)) == NULL)
1056 snprintf(classification, sizeof(classification), "CLASSIFICATION=%s",
1057 Classification);
1058 else if (attr->num_values > 1 &&
1059 strcmp(attr->values[1].string.text, "none") != 0)
1060 snprintf(classification, sizeof(classification), "CLASSIFICATION=%s",
1061 attr->values[1].string.text);
1062 else
1063 snprintf(classification, sizeof(classification), "CLASSIFICATION=%s",
1064 attr->values[0].string.text);
ef416fc2 1065
b9faaae1
MS
1066 envp[envc ++] = classification;
1067 }
ef416fc2 1068
a2326b5b 1069 if (job->dtype & CUPS_PRINTER_CLASS)
b9faaae1
MS
1070 {
1071 snprintf(class_name, sizeof(class_name), "CLASS=%s", job->dest);
1072 envp[envc ++] = class_name;
1073 }
ef416fc2 1074
22c9029b 1075 envp[envc ++] = auth_info_required;
88f9aafc
MS
1076
1077 for (i = 0;
1078 i < (int)(sizeof(job->auth_env) / sizeof(job->auth_env[0]));
1079 i ++)
1080 if (job->auth_env[i])
1081 envp[envc ++] = job->auth_env[i];
1082 else
1083 break;
1084
07ed0e9a
MS
1085 if (job->auth_uid)
1086 envp[envc ++] = job->auth_uid;
ef416fc2 1087
b9faaae1 1088 envp[envc] = NULL;
ef416fc2 1089
b9faaae1
MS
1090 for (i = 0; i < envc; i ++)
1091 if (!strncmp(envp[i], "AUTH_", 5))
1092 cupsdLogJob(job, CUPSD_LOG_DEBUG, "envp[%d]=\"AUTH_%c****\"", i,
1093 envp[i][5]);
1094 else if (strncmp(envp[i], "DEVICE_URI=", 11))
1095 cupsdLogJob(job, CUPSD_LOG_DEBUG, "envp[%d]=\"%s\"", i, envp[i]);
1096 else
1097 cupsdLogJob(job, CUPSD_LOG_DEBUG, "envp[%d]=\"DEVICE_URI=%s\"", i,
1098 job->printer->sanitized_device_uri);
ef416fc2 1099
b9faaae1
MS
1100 if (job->printer->remote)
1101 job->current_file = job->num_files;
1102 else
1103 job->current_file ++;
ef416fc2 1104
b9faaae1
MS
1105 /*
1106 * Now create processes for all of the filters...
1107 */
ef416fc2 1108
b9faaae1
MS
1109 for (i = 0, slot = 0, filter = (mime_filter_t *)cupsArrayFirst(filters);
1110 filter;
1111 i ++, filter = (mime_filter_t *)cupsArrayNext(filters))
1112 {
1113 if (filter->filter[0] != '/')
1114 snprintf(command, sizeof(command), "%s/filter/%s", ServerBin,
1115 filter->filter);
1116 else
1117 strlcpy(command, filter->filter, sizeof(command));
ef416fc2 1118
b9faaae1
MS
1119 if (i < (cupsArrayCount(filters) - 1))
1120 {
1121 if (cupsdOpenPipe(filterfds[slot]))
1122 {
1123 abort_message = "Stopping job because the scheduler could not create "
1124 "the filter pipes.";
ef416fc2 1125
b9faaae1
MS
1126 goto abort_job;
1127 }
1128 }
1129 else
1130 {
82f97232
MS
1131 if (job->current_file == 1 ||
1132 (job->printer->pc && job->printer->pc->single_file))
b9faaae1
MS
1133 {
1134 if (strncmp(job->printer->device_uri, "file:", 5) != 0)
1135 {
1136 if (cupsdOpenPipe(job->print_pipes))
1137 {
1138 abort_message = "Stopping job because the scheduler could not "
1139 "create the backend pipes.";
ef416fc2 1140
b9faaae1
MS
1141 goto abort_job;
1142 }
1143 }
1144 else
1145 {
1146 job->print_pipes[0] = -1;
1147 if (!strcmp(job->printer->device_uri, "file:/dev/null") ||
1148 !strcmp(job->printer->device_uri, "file:///dev/null"))
1149 job->print_pipes[1] = -1;
1150 else
1151 {
1152 if (!strncmp(job->printer->device_uri, "file:/dev/", 10))
1153 job->print_pipes[1] = open(job->printer->device_uri + 5,
1154 O_WRONLY | O_EXCL);
1155 else if (!strncmp(job->printer->device_uri, "file:///dev/", 12))
1156 job->print_pipes[1] = open(job->printer->device_uri + 7,
1157 O_WRONLY | O_EXCL);
1158 else if (!strncmp(job->printer->device_uri, "file:///", 8))
1159 job->print_pipes[1] = open(job->printer->device_uri + 7,
1160 O_WRONLY | O_CREAT | O_TRUNC, 0600);
1161 else
1162 job->print_pipes[1] = open(job->printer->device_uri + 5,
1163 O_WRONLY | O_CREAT | O_TRUNC, 0600);
ef416fc2 1164
b9faaae1
MS
1165 if (job->print_pipes[1] < 0)
1166 {
1167 abort_message = "Stopping job because the scheduler could not "
1168 "open the output file.";
bd7854cb 1169
b9faaae1
MS
1170 goto abort_job;
1171 }
ef416fc2 1172
b9faaae1
MS
1173 fcntl(job->print_pipes[1], F_SETFD,
1174 fcntl(job->print_pipes[1], F_GETFD) | FD_CLOEXEC);
1175 }
1176 }
1177 }
ef416fc2 1178
b9faaae1
MS
1179 filterfds[slot][0] = job->print_pipes[0];
1180 filterfds[slot][1] = job->print_pipes[1];
1181 }
ef416fc2 1182
b9faaae1
MS
1183 pid = cupsdStartProcess(command, argv, envp, filterfds[!slot][0],
1184 filterfds[slot][1], job->status_pipes[1],
1185 job->back_pipes[0], job->side_pipes[0], 0,
38e73f87 1186 job->profile, job, job->filters + i);
ef416fc2 1187
b9faaae1 1188 cupsdClosePipe(filterfds[!slot]);
ef416fc2 1189
b9faaae1
MS
1190 if (pid == 0)
1191 {
1192 cupsdLogJob(job, CUPSD_LOG_ERROR, "Unable to start filter \"%s\" - %s.",
1193 filter->filter, strerror(errno));
3dfe78b3 1194
f11a948a 1195 abort_message = "Stopping job because the scheduler could not execute a "
b9faaae1 1196 "filter.";
ef416fc2 1197
b9faaae1
MS
1198 goto abort_job;
1199 }
ef416fc2 1200
b9faaae1
MS
1201 cupsdLogJob(job, CUPSD_LOG_INFO, "Started filter %s (PID %d)", command,
1202 pid);
bd7854cb 1203
59ac438c
MS
1204 if (argv[6])
1205 {
1206 free(argv[6]);
1207 argv[6] = NULL;
1208 }
1209
1210 slot = !slot;
bd7854cb 1211 }
1212
b9faaae1
MS
1213 cupsArrayDelete(filters);
1214 filters = NULL;
ef416fc2 1215
1216 /*
b9faaae1 1217 * Finally, pipe the final output into a backend process if needed...
ef416fc2 1218 */
1219
b9faaae1 1220 if (strncmp(job->printer->device_uri, "file:", 5) != 0)
bd7854cb 1221 {
82f97232
MS
1222 if (job->current_file == 1 || job->printer->remote ||
1223 (job->printer->pc && job->printer->pc->single_file))
b9faaae1
MS
1224 {
1225 sscanf(job->printer->device_uri, "%254[^:]", scheme);
1226 snprintf(command, sizeof(command), "%s/backend/%s", ServerBin, scheme);
ef416fc2 1227
b9faaae1
MS
1228 /*
1229 * See if the backend needs to run as root...
1230 */
ef416fc2 1231
b9faaae1
MS
1232 if (RunUser)
1233 backroot = 0;
1234 else if (stat(command, &backinfo))
1235 backroot = 0;
1236 else
0a393e6a 1237 backroot = !(backinfo.st_mode & (S_IWGRP | S_IRWXO));
ef416fc2 1238
b9faaae1 1239 argv[0] = job->printer->sanitized_device_uri;
ef416fc2 1240
b9faaae1
MS
1241 filterfds[slot][0] = -1;
1242 filterfds[slot][1] = -1;
ef416fc2 1243
b9faaae1
MS
1244 pid = cupsdStartProcess(command, argv, envp, filterfds[!slot][0],
1245 filterfds[slot][1], job->status_pipes[1],
1246 job->back_pipes[1], job->side_pipes[1],
8fe0183a 1247 backroot, job->bprofile, job, &(job->backend));
ef416fc2 1248
b9faaae1
MS
1249 if (pid == 0)
1250 {
1251 abort_message = "Stopping job because the sheduler could not execute "
1252 "the backend.";
1253
1254 goto abort_job;
1255 }
1256 else
1257 {
1258 cupsdLogJob(job, CUPSD_LOG_INFO, "Started backend %s (PID %d)",
1259 command, pid);
1260 }
1261 }
ef416fc2 1262
82f97232
MS
1263 if (job->current_file == job->num_files ||
1264 (job->printer->pc && job->printer->pc->single_file))
1265 cupsdClosePipe(job->print_pipes);
1266
b9faaae1
MS
1267 if (job->current_file == job->num_files)
1268 {
b9faaae1
MS
1269 cupsdClosePipe(job->back_pipes);
1270 cupsdClosePipe(job->side_pipes);
ef416fc2 1271
b9faaae1
MS
1272 close(job->status_pipes[1]);
1273 job->status_pipes[1] = -1;
1274 }
1275 }
1276 else
bd7854cb 1277 {
b9faaae1
MS
1278 filterfds[slot][0] = -1;
1279 filterfds[slot][1] = -1;
ef416fc2 1280
82f97232
MS
1281 if (job->current_file == job->num_files ||
1282 (job->printer->pc && job->printer->pc->single_file))
b9faaae1
MS
1283 cupsdClosePipe(job->print_pipes);
1284
82f97232
MS
1285 if (job->current_file == job->num_files)
1286 {
b9faaae1
MS
1287 close(job->status_pipes[1]);
1288 job->status_pipes[1] = -1;
1289 }
bd7854cb 1290 }
ef416fc2 1291
b9faaae1
MS
1292 cupsdClosePipe(filterfds[slot]);
1293
59ac438c
MS
1294 for (i = 6; i < argc; i ++)
1295 if (argv[i])
1296 free(argv[i]);
ef416fc2 1297
b9faaae1 1298 free(argv);
462b6e74 1299
eac3a0a0
MS
1300 if (printer_state_reasons)
1301 free(printer_state_reasons);
ef416fc2 1302
b9faaae1
MS
1303 cupsdAddSelect(job->status_buffer->fd, (cupsd_selfunc_t)update_job, NULL,
1304 job);
ef416fc2 1305
b9faaae1
MS
1306 cupsdAddEvent(CUPSD_EVENT_JOB_STATE, job->printer, job, "Job #%d started.",
1307 job->id);
ef416fc2 1308
b9faaae1 1309 return;
ef416fc2 1310
ef416fc2 1311
bd7854cb 1312 /*
b9faaae1
MS
1313 * If we get here, we need to abort the current job and close out all
1314 * files and pipes...
bd7854cb 1315 */
ef416fc2 1316
b9faaae1 1317 abort_job:
8b450588 1318
e07d4801
MS
1319 FilterLevel -= job->cost;
1320 job->cost = 0;
1321
b9faaae1
MS
1322 for (slot = 0; slot < 2; slot ++)
1323 cupsdClosePipe(filterfds[slot]);
1324
e07d4801
MS
1325 cupsArrayDelete(filters);
1326
1327 if (argv)
1328 {
59ac438c
MS
1329 for (i = 6; i < argc; i ++)
1330 if (argv[i])
1331 free(argv[i]);
e07d4801
MS
1332 }
1333
eac3a0a0
MS
1334 if (printer_state_reasons)
1335 free(printer_state_reasons);
1336
e07d4801
MS
1337 cupsdClosePipe(job->print_pipes);
1338 cupsdClosePipe(job->back_pipes);
1339 cupsdClosePipe(job->side_pipes);
1340
1341 cupsdRemoveSelect(job->status_pipes[0]);
b9faaae1
MS
1342 cupsdClosePipe(job->status_pipes);
1343 cupsdStatBufDelete(job->status_buffer);
1344 job->status_buffer = NULL;
1345
e07d4801
MS
1346 /*
1347 * Update the printer and job state.
1348 */
b9faaae1 1349
e07d4801
MS
1350 cupsdSetJobState(job, abort_state, CUPSD_JOB_DEFAULT, "%s", abort_message);
1351 cupsdSetPrinterState(job->printer, IPP_PRINTER_IDLE, 0);
1352 update_job_attrs(job, 0);
ef416fc2 1353
178cb736
MS
1354 if (job->history)
1355 free_job_history(job);
1356
e07d4801 1357 cupsArrayRemove(PrintingJobs, job);
ef416fc2 1358
e07d4801
MS
1359 /*
1360 * Clear the printer <-> job association...
1361 */
1362
1363 job->printer->job = NULL;
1364 job->printer = NULL;
b9faaae1 1365}
bd7854cb 1366
ef416fc2 1367
b9faaae1
MS
1368/*
1369 * 'cupsdDeleteJob()' - Free all memory used by a job.
1370 */
ef416fc2 1371
b9faaae1
MS
1372void
1373cupsdDeleteJob(cupsd_job_t *job, /* I - Job */
1374 cupsd_jobaction_t action)/* I - Action */
1375{
88f9aafc 1376 int i; /* Looping var */
22c9029b
MS
1377
1378
b9faaae1 1379 if (job->printer)
ef55b745 1380 finalize_job(job, 1);
ef416fc2 1381
b9faaae1 1382 if (action == CUPSD_JOB_PURGE)
82cc1f9a 1383 remove_job_history(job);
ef416fc2 1384
b9faaae1
MS
1385 cupsdClearString(&job->username);
1386 cupsdClearString(&job->dest);
88f9aafc
MS
1387 for (i = 0;
1388 i < (int)(sizeof(job->auth_env) / sizeof(job->auth_env[0]));
1389 i ++)
1390 cupsdClearString(job->auth_env + i);
07ed0e9a 1391 cupsdClearString(&job->auth_uid);
09a101d6 1392
82cc1f9a
MS
1393 if (action == CUPSD_JOB_PURGE)
1394 remove_job_files(job);
1395 else if (job->num_files > 0)
b9faaae1
MS
1396 {
1397 free(job->compressions);
1398 free(job->filetypes);
09a101d6 1399
82cc1f9a 1400 job->num_files = 0;
09a101d6 1401 }
ed6e7faf 1402
178cb736
MS
1403 if (job->history)
1404 free_job_history(job);
1405
b9faaae1 1406 unload_job(job);
dfd5680b 1407
b9faaae1
MS
1408 cupsArrayRemove(Jobs, job);
1409 cupsArrayRemove(ActiveJobs, job);
1410 cupsArrayRemove(PrintingJobs, job);
dfd5680b 1411
b9faaae1 1412 free(job);
ef416fc2 1413}
1414
1415
1416/*
b9faaae1 1417 * 'cupsdFreeAllJobs()' - Free all jobs from memory.
ef416fc2 1418 */
1419
1420void
b9faaae1 1421cupsdFreeAllJobs(void)
ef416fc2 1422{
b9faaae1 1423 cupsd_job_t *job; /* Current job */
ef416fc2 1424
ef416fc2 1425
b9faaae1 1426 if (!Jobs)
ef416fc2 1427 return;
1428
b9faaae1 1429 cupsdHoldSignals();
ef416fc2 1430
238c3832 1431 cupsdStopAllJobs(CUPSD_JOB_FORCE, 0);
b9faaae1 1432 cupsdSaveAllJobs();
e53920b9 1433
b9faaae1
MS
1434 for (job = (cupsd_job_t *)cupsArrayFirst(Jobs);
1435 job;
1436 job = (cupsd_job_t *)cupsArrayNext(Jobs))
1437 cupsdDeleteJob(job, CUPSD_JOB_DEFAULT);
ef416fc2 1438
b9faaae1
MS
1439 cupsdReleaseSignals();
1440}
ef416fc2 1441
bd7854cb 1442
b9faaae1
MS
1443/*
1444 * 'cupsdFindJob()' - Find the specified job.
1445 */
e53920b9 1446
b9faaae1
MS
1447cupsd_job_t * /* O - Job data */
1448cupsdFindJob(int id) /* I - Job ID */
1449{
1450 cupsd_job_t key; /* Search key */
ef416fc2 1451
ef416fc2 1452
b9faaae1 1453 key.id = id;
e53920b9 1454
b9faaae1 1455 return ((cupsd_job_t *)cupsArrayFind(Jobs, &key));
ef416fc2 1456}
1457
1458
eec1fbc3
MS
1459/*
1460 * 'cupsdGetCompletedJobs()'- Generate a completed jobs list.
1461 */
1462
1463cups_array_t * /* O - Array of jobs */
1464cupsdGetCompletedJobs(
1465 cupsd_printer_t *p) /* I - Printer */
1466{
1467 cups_array_t *list; /* Array of jobs */
1468 cupsd_job_t *job; /* Current job */
1469
1470
1471 list = cupsArrayNew(compare_completed_jobs, NULL);
1472
1473 for (job = (cupsd_job_t *)cupsArrayFirst(Jobs);
1474 job;
1475 job = (cupsd_job_t *)cupsArrayNext(Jobs))
1476 if ((!p || !_cups_strcasecmp(p->name, job->dest)) && job->state_value >= IPP_JOB_STOPPED && job->completed_time)
1477 cupsArrayAdd(list, job);
1478
1479 return (list);
1480}
1481
1482
ef416fc2 1483/*
b9faaae1
MS
1484 * 'cupsdGetPrinterJobCount()' - Get the number of pending, processing,
1485 * or held jobs in a printer or class.
ef416fc2 1486 */
1487
b9faaae1
MS
1488int /* O - Job count */
1489cupsdGetPrinterJobCount(
1490 const char *dest) /* I - Printer or class name */
ef416fc2 1491{
b9faaae1
MS
1492 int count; /* Job count */
1493 cupsd_job_t *job; /* Current job */
005dd1eb 1494
005dd1eb 1495
b9faaae1
MS
1496 for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs), count = 0;
1497 job;
1498 job = (cupsd_job_t *)cupsArrayNext(ActiveJobs))
88f9aafc 1499 if (job->dest && !_cups_strcasecmp(job->dest, dest))
b9faaae1 1500 count ++;
ef416fc2 1501
b9faaae1 1502 return (count);
ef416fc2 1503}
1504
1505
1506/*
b9faaae1
MS
1507 * 'cupsdGetUserJobCount()' - Get the number of pending, processing,
1508 * or held jobs for a user.
ef416fc2 1509 */
1510
b9faaae1
MS
1511int /* O - Job count */
1512cupsdGetUserJobCount(
1513 const char *username) /* I - Username */
ef416fc2 1514{
b9faaae1
MS
1515 int count; /* Job count */
1516 cupsd_job_t *job; /* Current job */
07725fee 1517
07725fee 1518
b9faaae1
MS
1519 for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs), count = 0;
1520 job;
1521 job = (cupsd_job_t *)cupsArrayNext(ActiveJobs))
88f9aafc 1522 if (!_cups_strcasecmp(job->username, username))
b9faaae1 1523 count ++;
07725fee 1524
b9faaae1 1525 return (count);
ef416fc2 1526}
1527
1528
1529/*
b9faaae1 1530 * 'cupsdLoadAllJobs()' - Load all jobs from disk.
ef416fc2 1531 */
1532
1533void
b9faaae1 1534cupsdLoadAllJobs(void)
bd7854cb 1535{
b9faaae1
MS
1536 char filename[1024]; /* Full filename of job.cache file */
1537 struct stat fileinfo, /* Information on job.cache file */
1538 dirinfo; /* Information on RequestRoot dir */
bd7854cb 1539
bd7854cb 1540
bd7854cb 1541
1542 /*
b9faaae1 1543 * Create the job arrays as needed...
bd7854cb 1544 */
1545
b9faaae1
MS
1546 if (!Jobs)
1547 Jobs = cupsArrayNew(compare_jobs, NULL);
1548
1549 if (!ActiveJobs)
1550 ActiveJobs = cupsArrayNew(compare_active_jobs, NULL);
1551
1552 if (!PrintingJobs)
1553 PrintingJobs = cupsArrayNew(compare_jobs, NULL);
bd7854cb 1554
1555 /*
b9faaae1 1556 * See whether the job.cache file is older than the RequestRoot directory...
bd7854cb 1557 */
1558
b9faaae1 1559 snprintf(filename, sizeof(filename), "%s/job.cache", CacheDir);
bd7854cb 1560
b9faaae1
MS
1561 if (stat(filename, &fileinfo))
1562 {
1563 fileinfo.st_mtime = 0;
1564
1565 if (errno != ENOENT)
1566 cupsdLogMessage(CUPSD_LOG_ERROR,
1567 "Unable to get file information for \"%s\" - %s",
1568 filename, strerror(errno));
1569 }
1570
1571 if (stat(RequestRoot, &dirinfo))
1572 {
1573 dirinfo.st_mtime = 0;
1574
1575 if (errno != ENOENT)
1576 cupsdLogMessage(CUPSD_LOG_ERROR,
1577 "Unable to get directory information for \"%s\" - %s",
1578 RequestRoot, strerror(errno));
1579 }
bd7854cb 1580
1581 /*
b9faaae1 1582 * Load the most recent source for job data...
bd7854cb 1583 */
1584
b9faaae1 1585 if (dirinfo.st_mtime > fileinfo.st_mtime)
bd7854cb 1586 {
b9faaae1
MS
1587 load_request_root();
1588
1589 load_next_job_id(filename);
bd7854cb 1590 }
b9faaae1
MS
1591 else
1592 load_job_cache(filename);
bd7854cb 1593
b9faaae1
MS
1594 /*
1595 * Clean out old jobs as needed...
1596 */
1597
1598 if (MaxJobs > 0 && cupsArrayCount(Jobs) >= MaxJobs)
1599 cupsdCleanJobs();
bd7854cb 1600}
1601
1602
1603/*
b9faaae1 1604 * 'cupsdLoadJob()' - Load a single job.
bd7854cb 1605 */
1606
b9faaae1
MS
1607int /* O - 1 on success, 0 on failure */
1608cupsdLoadJob(cupsd_job_t *job) /* I - Job */
ef416fc2 1609{
88f9aafc 1610 int i; /* Looping var */
b9faaae1
MS
1611 char jobfile[1024]; /* Job filename */
1612 cups_file_t *fp; /* Job file */
1613 int fileid; /* Current file ID */
1614 ipp_attribute_t *attr; /* Job attribute */
1615 const char *dest; /* Destination name */
1616 cupsd_printer_t *destptr; /* Pointer to destination */
1617 mime_type_t **filetypes; /* New filetypes array */
1618 int *compressions; /* New compressions array */
ef416fc2 1619
1620
b9faaae1
MS
1621 if (job->attrs)
1622 {
1623 if (job->state_value > IPP_JOB_STOPPED)
1624 job->access_time = time(NULL);
bd7854cb 1625
b9faaae1
MS
1626 return (1);
1627 }
ef416fc2 1628
b9faaae1 1629 if ((job->attrs = ippNew()) == NULL)
ef416fc2 1630 {
12f89d24 1631 cupsdLogJob(job, CUPSD_LOG_ERROR, "Ran out of memory for job attributes.");
b9faaae1 1632 return (0);
ef416fc2 1633 }
1634
b9faaae1
MS
1635 /*
1636 * Load job attributes...
1637 */
ef416fc2 1638
72f50170 1639 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Loading attributes...");
bd7854cb 1640
b9faaae1 1641 snprintf(jobfile, sizeof(jobfile), "%s/c%05d", RequestRoot, job->id);
82cc1f9a
MS
1642 if ((fp = cupsdOpenConfFile(jobfile)) == NULL)
1643 goto error;
ef416fc2 1644
b9faaae1
MS
1645 if (ippReadIO(fp, (ipp_iocb_t)cupsFileRead, 1, NULL, job->attrs) != IPP_DATA)
1646 {
72f50170
MS
1647 cupsdLogJob(job, CUPSD_LOG_ERROR,
1648 "Unable to read job control file \"%s\".", jobfile);
b9faaae1
MS
1649 cupsFileClose(fp);
1650 goto error;
1651 }
ef416fc2 1652
b9faaae1 1653 cupsFileClose(fp);
ef416fc2 1654
b9faaae1
MS
1655 /*
1656 * Copy attribute data to the job object...
1657 */
ef416fc2 1658
b9faaae1 1659 if (!ippFindAttribute(job->attrs, "time-at-creation", IPP_TAG_INTEGER))
ef416fc2 1660 {
72f50170
MS
1661 cupsdLogJob(job, CUPSD_LOG_ERROR,
1662 "Missing or bad time-at-creation attribute in control file.");
b9faaae1 1663 goto error;
ef416fc2 1664 }
b9faaae1
MS
1665
1666 if ((job->state = ippFindAttribute(job->attrs, "job-state",
1667 IPP_TAG_ENUM)) == NULL)
ef416fc2 1668 {
72f50170
MS
1669 cupsdLogJob(job, CUPSD_LOG_ERROR,
1670 "Missing or bad job-state attribute in control file.");
b9faaae1
MS
1671 goto error;
1672 }
ef416fc2 1673
82cc1f9a
MS
1674 job->state_value = (ipp_jstate_t)job->state->values[0].integer;
1675 job->file_time = 0;
1676 job->history_time = 0;
1677
f16ea703
MS
1678 if ((attr = ippFindAttribute(job->attrs, "time-at-creation", IPP_TAG_INTEGER)) != NULL)
1679 job->creation_time = attr->values[0].integer;
1680
1681 if (job->state_value >= IPP_JOB_CANCELED && (attr = ippFindAttribute(job->attrs, "time-at-completed", IPP_TAG_INTEGER)) != NULL)
82cc1f9a 1682 {
eec1fbc3
MS
1683 job->completed_time = attr->values[0].integer;
1684
82cc1f9a
MS
1685 if (JobHistory < INT_MAX)
1686 job->history_time = attr->values[0].integer + JobHistory;
1687 else
1688 job->history_time = INT_MAX;
1689
1690 if (job->history_time < time(NULL))
1691 goto error; /* Expired, remove from history */
1692
1693 if (job->history_time < JobHistoryUpdate || !JobHistoryUpdate)
1694 JobHistoryUpdate = job->history_time;
1695
1696 if (JobFiles < INT_MAX)
1697 job->file_time = attr->values[0].integer + JobFiles;
1698 else
1699 job->file_time = INT_MAX;
1700
1701 if (job->file_time < JobHistoryUpdate || !JobHistoryUpdate)
1702 JobHistoryUpdate = job->file_time;
1703
1704 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdLoadJob: JobHistoryUpdate=%ld",
1705 (long)JobHistoryUpdate);
1706 }
ef416fc2 1707
b9faaae1 1708 if (!job->dest)
ef416fc2 1709 {
b9faaae1
MS
1710 if ((attr = ippFindAttribute(job->attrs, "job-printer-uri",
1711 IPP_TAG_URI)) == NULL)
1712 {
72f50170
MS
1713 cupsdLogJob(job, CUPSD_LOG_ERROR,
1714 "No job-printer-uri attribute in control file.");
b9faaae1
MS
1715 goto error;
1716 }
ef416fc2 1717
b9faaae1
MS
1718 if ((dest = cupsdValidateDest(attr->values[0].string.text, &(job->dtype),
1719 &destptr)) == NULL)
1720 {
72f50170
MS
1721 cupsdLogJob(job, CUPSD_LOG_ERROR,
1722 "Unable to queue job for destination \"%s\".",
1723 attr->values[0].string.text);
b9faaae1
MS
1724 goto error;
1725 }
ef416fc2 1726
b9faaae1 1727 cupsdSetString(&job->dest, dest);
f7deaa1a 1728 }
b9faaae1 1729 else if ((destptr = cupsdFindDest(job->dest)) == NULL)
ef416fc2 1730 {
72f50170
MS
1731 cupsdLogJob(job, CUPSD_LOG_ERROR,
1732 "Unable to queue job for destination \"%s\".",
1733 job->dest);
b9faaae1
MS
1734 goto error;
1735 }
ef416fc2 1736
12f89d24
MS
1737 if ((job->reasons = ippFindAttribute(job->attrs, "job-state-reasons",
1738 IPP_TAG_KEYWORD)) == NULL)
1739 {
1740 const char *reason; /* job-state-reason keyword */
1741
72f50170
MS
1742 cupsdLogJob(job, CUPSD_LOG_DEBUG,
1743 "Adding missing job-state-reasons attribute to control file.");
12f89d24
MS
1744
1745 switch (job->state_value)
1746 {
82cc1f9a 1747 default :
12f89d24
MS
1748 case IPP_JOB_PENDING :
1749 if (destptr->state == IPP_PRINTER_STOPPED)
1750 reason = "printer-stopped";
1751 else
1752 reason = "none";
1753 break;
1754
1755 case IPP_JOB_HELD :
1756 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
1757 IPP_TAG_ZERO)) != NULL &&
1758 (attr->value_tag == IPP_TAG_NAME ||
1759 attr->value_tag == IPP_TAG_NAMELANG ||
1760 attr->value_tag == IPP_TAG_KEYWORD) &&
1761 strcmp(attr->values[0].string.text, "no-hold"))
1762 reason = "job-hold-until-specified";
1763 else
1764 reason = "job-incoming";
1765 break;
1766
1767 case IPP_JOB_PROCESSING :
1768 reason = "job-printing";
1769 break;
1770
1771 case IPP_JOB_STOPPED :
1772 reason = "job-stopped";
1773 break;
1774
1775 case IPP_JOB_CANCELED :
1776 reason = "job-canceled-by-user";
1777 break;
1778
1779 case IPP_JOB_ABORTED :
1780 reason = "aborted-by-system";
1781 break;
1782
1783 case IPP_JOB_COMPLETED :
1784 reason = "job-completed-successfully";
1785 break;
1786 }
1787
1788 job->reasons = ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_KEYWORD,
1789 "job-state-reasons", NULL, reason);
1790 }
1791 else if (job->state_value == IPP_JOB_PENDING)
1792 {
1793 if (destptr->state == IPP_PRINTER_STOPPED)
1794 ippSetString(job->attrs, &job->reasons, 0, "printer-stopped");
1795 else
1796 ippSetString(job->attrs, &job->reasons, 0, "none");
1797 }
1798
b9faaae1
MS
1799 job->sheets = ippFindAttribute(job->attrs, "job-media-sheets-completed",
1800 IPP_TAG_INTEGER);
1801 job->job_sheets = ippFindAttribute(job->attrs, "job-sheets", IPP_TAG_NAME);
ef416fc2 1802
b9faaae1
MS
1803 if (!job->priority)
1804 {
1805 if ((attr = ippFindAttribute(job->attrs, "job-priority",
1806 IPP_TAG_INTEGER)) == NULL)
1807 {
72f50170
MS
1808 cupsdLogJob(job, CUPSD_LOG_ERROR,
1809 "Missing or bad job-priority attribute in control file.");
b9faaae1
MS
1810 goto error;
1811 }
1812
1813 job->priority = attr->values[0].integer;
f7deaa1a 1814 }
b9faaae1
MS
1815
1816 if (!job->username)
ef416fc2 1817 {
b9faaae1
MS
1818 if ((attr = ippFindAttribute(job->attrs, "job-originating-user-name",
1819 IPP_TAG_NAME)) == NULL)
1820 {
72f50170
MS
1821 cupsdLogJob(job, CUPSD_LOG_ERROR,
1822 "Missing or bad job-originating-user-name "
1823 "attribute in control file.");
b9faaae1
MS
1824 goto error;
1825 }
ef416fc2 1826
b9faaae1
MS
1827 cupsdSetString(&job->username, attr->values[0].string.text);
1828 }
ef416fc2 1829
f16ea703
MS
1830 if (!job->name)
1831 {
1832 if ((attr = ippFindAttribute(job->attrs, "job-name", IPP_TAG_NAME)) != NULL)
1833 cupsdSetString(&job->name, attr->values[0].string.text);
1834 }
1835
b9faaae1
MS
1836 /*
1837 * Set the job hold-until time and state...
1838 */
1839
1840 if (job->state_value == IPP_JOB_HELD)
1841 {
1842 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
1843 IPP_TAG_KEYWORD)) == NULL)
1844 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
1845
1846 if (attr)
1847 cupsdSetJobHoldUntil(job, attr->values[0].string.text, CUPSD_JOB_DEFAULT);
ef416fc2 1848 else
b9faaae1
MS
1849 {
1850 job->state->values[0].integer = IPP_JOB_PENDING;
1851 job->state_value = IPP_JOB_PENDING;
1852 }
f7deaa1a 1853 }
b9faaae1
MS
1854 else if (job->state_value == IPP_JOB_PROCESSING)
1855 {
1856 job->state->values[0].integer = IPP_JOB_PENDING;
1857 job->state_value = IPP_JOB_PENDING;
1858 }
1859
f16ea703
MS
1860 if ((attr = ippFindAttribute(job->attrs, "job-k-octets", IPP_TAG_INTEGER)) != NULL)
1861 job->koctets = attr->values[0].integer;
1862
b9faaae1 1863 if (!job->num_files)
ef416fc2 1864 {
1865 /*
b9faaae1 1866 * Find all the d##### files...
ef416fc2 1867 */
1868
b9faaae1
MS
1869 for (fileid = 1; fileid < 10000; fileid ++)
1870 {
1871 snprintf(jobfile, sizeof(jobfile), "%s/d%05d-%03d", RequestRoot,
1872 job->id, fileid);
ef416fc2 1873
b9faaae1
MS
1874 if (access(jobfile, 0))
1875 break;
1876
72f50170
MS
1877 cupsdLogJob(job, CUPSD_LOG_DEBUG,
1878 "Auto-typing document file \"%s\"...", jobfile);
b9faaae1
MS
1879
1880 if (fileid > job->num_files)
1881 {
1882 if (job->num_files == 0)
1883 {
7e86f2f6
MS
1884 compressions = (int *)calloc((size_t)fileid, sizeof(int));
1885 filetypes = (mime_type_t **)calloc((size_t)fileid, sizeof(mime_type_t *));
b9faaae1
MS
1886 }
1887 else
1888 {
7e86f2f6
MS
1889 compressions = (int *)realloc(job->compressions, sizeof(int) * (size_t)fileid);
1890 filetypes = (mime_type_t **)realloc(job->filetypes, sizeof(mime_type_t *) * (size_t)fileid);
b9faaae1
MS
1891 }
1892
cb7f98ee
MS
1893 if (compressions)
1894 job->compressions = compressions;
1895
1896 if (filetypes)
1897 job->filetypes = filetypes;
1898
b9faaae1
MS
1899 if (!compressions || !filetypes)
1900 {
72f50170
MS
1901 cupsdLogJob(job, CUPSD_LOG_ERROR,
1902 "Ran out of memory for job file types.");
ef55b745
MS
1903
1904 ippDelete(job->attrs);
1905 job->attrs = NULL;
1906
ef55b745
MS
1907 if (job->compressions)
1908 {
1909 free(job->compressions);
1910 job->compressions = NULL;
1911 }
1912
1913 if (job->filetypes)
1914 {
1915 free(job->filetypes);
1916 job->filetypes = NULL;
1917 }
1918
1919 job->num_files = 0;
1920 return (0);
b9faaae1
MS
1921 }
1922
cb7f98ee 1923 job->num_files = fileid;
b9faaae1
MS
1924 }
1925
1926 job->filetypes[fileid - 1] = mimeFileType(MimeDatabase, jobfile, NULL,
1927 job->compressions + fileid - 1);
1928
1929 if (!job->filetypes[fileid - 1])
1930 job->filetypes[fileid - 1] = mimeType(MimeDatabase, "application",
1931 "vnd.cups-raw");
1932 }
ef416fc2 1933 }
b9faaae1
MS
1934
1935 /*
1936 * Load authentication information as needed...
1937 */
1938
1939 if (job->state_value < IPP_JOB_STOPPED)
ef416fc2 1940 {
b9faaae1 1941 snprintf(jobfile, sizeof(jobfile), "%s/a%05d", RequestRoot, job->id);
ef416fc2 1942
88f9aafc
MS
1943 for (i = 0;
1944 i < (int)(sizeof(job->auth_env) / sizeof(job->auth_env[0]));
1945 i ++)
1946 cupsdClearString(job->auth_env + i);
07ed0e9a 1947 cupsdClearString(&job->auth_uid);
ef416fc2 1948
b9faaae1
MS
1949 if ((fp = cupsFileOpen(jobfile, "r")) != NULL)
1950 {
dcb445bc
MS
1951 int bytes, /* Size of auth data */
1952 linenum = 1; /* Current line number */
85dda01c 1953 char line[65536], /* Line from file */
dcb445bc 1954 *value, /* Value from line */
85dda01c 1955 data[65536]; /* Decoded data */
ef416fc2 1956
ef416fc2 1957
dcb445bc 1958 if (cupsFileGets(fp, line, sizeof(line)) &&
94436c5a 1959 !strcmp(line, "CUPSD-AUTH-V3"))
b9faaae1 1960 {
dcb445bc
MS
1961 i = 0;
1962 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
1963 {
1964 /*
1965 * Decode value...
1966 */
1967
94436c5a
MS
1968 if (strcmp(line, "negotiate") && strcmp(line, "uid"))
1969 {
1970 bytes = sizeof(data);
1971 httpDecode64_2(data, &bytes, value);
1972 }
b9faaae1 1973
dcb445bc
MS
1974 /*
1975 * Assign environment variables...
1976 */
1977
1978 if (!strcmp(line, "uid"))
1979 {
1980 cupsdSetStringf(&job->auth_uid, "AUTH_UID=%s", value);
1981 continue;
1982 }
1983 else if (i >= (int)(sizeof(job->auth_env) / sizeof(job->auth_env[0])))
1984 break;
1985
1986 if (!strcmp(line, "username"))
1987 cupsdSetStringf(job->auth_env + i, "AUTH_USERNAME=%s", data);
1988 else if (!strcmp(line, "domain"))
1989 cupsdSetStringf(job->auth_env + i, "AUTH_DOMAIN=%s", data);
1990 else if (!strcmp(line, "password"))
1991 cupsdSetStringf(job->auth_env + i, "AUTH_PASSWORD=%s", data);
1992 else if (!strcmp(line, "negotiate"))
94436c5a 1993 cupsdSetStringf(job->auth_env + i, "AUTH_NEGOTIATE=%s", value);
dcb445bc
MS
1994 else
1995 continue;
1996
1997 i ++;
1998 }
1999 }
07ed0e9a 2000
b9faaae1
MS
2001 cupsFileClose(fp);
2002 }
ef416fc2 2003 }
2004
b9faaae1
MS
2005 job->access_time = time(NULL);
2006 return (1);
2007
2008 /*
2009 * If we get here then something bad happened...
2010 */
2011
2012 error:
2013
2014 ippDelete(job->attrs);
2015 job->attrs = NULL;
ef55b745 2016
82cc1f9a
MS
2017 remove_job_history(job);
2018 remove_job_files(job);
b9faaae1
MS
2019
2020 return (0);
ef416fc2 2021}
2022
2023
2024/*
b9faaae1 2025 * 'cupsdMoveJob()' - Move the specified job to a different destination.
ef416fc2 2026 */
2027
2028void
b9faaae1
MS
2029cupsdMoveJob(cupsd_job_t *job, /* I - Job */
2030 cupsd_printer_t *p) /* I - Destination printer or class */
ef416fc2 2031{
b9faaae1
MS
2032 ipp_attribute_t *attr; /* job-printer-uri attribute */
2033 const char *olddest; /* Old destination */
2034 cupsd_printer_t *oldp; /* Old pointer */
ef416fc2 2035
2036
2037 /*
b9faaae1 2038 * Don't move completed jobs...
ef416fc2 2039 */
2040
b9faaae1 2041 if (job->state_value > IPP_JOB_STOPPED)
ef416fc2 2042 return;
2043
2044 /*
b9faaae1 2045 * Get the old destination...
ef416fc2 2046 */
2047
b9faaae1 2048 olddest = job->dest;
ef416fc2 2049
b9faaae1
MS
2050 if (job->printer)
2051 oldp = job->printer;
ef416fc2 2052 else
b9faaae1 2053 oldp = cupsdFindDest(olddest);
ef416fc2 2054
b9faaae1
MS
2055 /*
2056 * Change the destination information...
2057 */
2058
eac3a0a0
MS
2059 if (job->state_value > IPP_JOB_HELD)
2060 cupsdSetJobState(job, IPP_JOB_PENDING, CUPSD_JOB_DEFAULT,
2061 "Stopping job prior to move.");
b9faaae1
MS
2062
2063 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED, oldp, job,
2064 "Job #%d moved from %s to %s.", job->id, olddest,
2065 p->name);
2066
2067 cupsdSetString(&job->dest, p->name);
a2326b5b 2068 job->dtype = p->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_REMOTE);
b9faaae1
MS
2069
2070 if ((attr = ippFindAttribute(job->attrs, "job-printer-uri",
2071 IPP_TAG_URI)) != NULL)
2072 cupsdSetString(&(attr->values[0].string.text), p->uri);
2073
2074 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED, p, job,
2075 "Job #%d moved from %s to %s.", job->id, olddest,
2076 p->name);
ef416fc2 2077
3dfe78b3
MS
2078 job->dirty = 1;
2079 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
ef416fc2 2080}
2081
2082
2083/*
b9faaae1 2084 * 'cupsdReleaseJob()' - Release the specified job.
ef416fc2 2085 */
2086
2087void
b9faaae1 2088cupsdReleaseJob(cupsd_job_t *job) /* I - Job */
ef416fc2 2089{
b9faaae1
MS
2090 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdReleaseJob(job=%p(%d))", job,
2091 job->id);
ef416fc2 2092
b9faaae1
MS
2093 if (job->state_value == IPP_JOB_HELD)
2094 {
2095 /*
2096 * Add trailing banner as needed...
2097 */
ef416fc2 2098
b9faaae1
MS
2099 if (job->pending_timeout)
2100 cupsdTimeoutJob(job);
ef416fc2 2101
b9faaae1
MS
2102 cupsdSetJobState(job, IPP_JOB_PENDING, CUPSD_JOB_DEFAULT,
2103 "Job released by user.");
2104 }
e1d6a774 2105}
ef416fc2 2106
ef416fc2 2107
e1d6a774 2108/*
b9faaae1 2109 * 'cupsdRestartJob()' - Restart the specified job.
e1d6a774 2110 */
ef416fc2 2111
e1d6a774 2112void
b9faaae1 2113cupsdRestartJob(cupsd_job_t *job) /* I - Job */
e1d6a774 2114{
b9faaae1
MS
2115 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdRestartJob(job=%p(%d))", job,
2116 job->id);
ef416fc2 2117
b9faaae1
MS
2118 if (job->state_value == IPP_JOB_STOPPED || job->num_files)
2119 cupsdSetJobState(job, IPP_JOB_PENDING, CUPSD_JOB_DEFAULT,
2120 "Job restarted by user.");
2121}
ef416fc2 2122
ef416fc2 2123
b9faaae1
MS
2124/*
2125 * 'cupsdSaveAllJobs()' - Save a summary of all jobs to disk.
2126 */
ef416fc2 2127
b9faaae1
MS
2128void
2129cupsdSaveAllJobs(void)
2130{
2131 int i; /* Looping var */
321d8d57
MS
2132 cups_file_t *fp; /* job.cache file */
2133 char filename[1024], /* job.cache filename */
2134 temp[1024]; /* Temporary string */
b9faaae1
MS
2135 cupsd_job_t *job; /* Current job */
2136 time_t curtime; /* Current time */
2137 struct tm *curdate; /* Current date */
ef416fc2 2138
ef416fc2 2139
321d8d57
MS
2140 snprintf(filename, sizeof(filename), "%s/job.cache", CacheDir);
2141 if ((fp = cupsdCreateConfFile(filename, ConfigFilePerm)) == NULL)
c168a833 2142 return;
ef416fc2 2143
321d8d57 2144 cupsdLogMessage(CUPSD_LOG_INFO, "Saving job.cache...");
bd7854cb 2145
b9faaae1
MS
2146 /*
2147 * Write a small header to the file...
2148 */
bd7854cb 2149
b9faaae1
MS
2150 curtime = time(NULL);
2151 curdate = localtime(&curtime);
2152 strftime(temp, sizeof(temp) - 1, "%Y-%m-%d %H:%M", curdate);
ef416fc2 2153
b9faaae1
MS
2154 cupsFilePuts(fp, "# Job cache file for " CUPS_SVERSION "\n");
2155 cupsFilePrintf(fp, "# Written by cupsd on %s\n", temp);
2156 cupsFilePrintf(fp, "NextJobId %d\n", NextJobId);
f7deaa1a 2157
b9faaae1
MS
2158 /*
2159 * Write each job known to the system...
2160 */
f7deaa1a 2161
b9faaae1
MS
2162 for (job = (cupsd_job_t *)cupsArrayFirst(Jobs);
2163 job;
2164 job = (cupsd_job_t *)cupsArrayNext(Jobs))
ef416fc2 2165 {
b9faaae1
MS
2166 cupsFilePrintf(fp, "<Job %d>\n", job->id);
2167 cupsFilePrintf(fp, "State %d\n", job->state_value);
f16ea703 2168 cupsFilePrintf(fp, "Created %ld\n", (long)job->creation_time);
eec1fbc3
MS
2169 if (job->completed_time)
2170 cupsFilePrintf(fp, "Completed %ld\n", (long)job->completed_time);
b9faaae1 2171 cupsFilePrintf(fp, "Priority %d\n", job->priority);
eec1fbc3
MS
2172 if (job->hold_until)
2173 cupsFilePrintf(fp, "HoldUntil %ld\n", (long)job->hold_until);
b9faaae1 2174 cupsFilePrintf(fp, "Username %s\n", job->username);
f16ea703
MS
2175 if (job->name)
2176 cupsFilePutConf(fp, "Name", job->name);
b9faaae1
MS
2177 cupsFilePrintf(fp, "Destination %s\n", job->dest);
2178 cupsFilePrintf(fp, "DestType %d\n", job->dtype);
f16ea703 2179 cupsFilePrintf(fp, "KOctets %d\n", job->koctets);
b9faaae1
MS
2180 cupsFilePrintf(fp, "NumFiles %d\n", job->num_files);
2181 for (i = 0; i < job->num_files; i ++)
2182 cupsFilePrintf(fp, "File %d %s/%s %d\n", i + 1, job->filetypes[i]->super,
2183 job->filetypes[i]->type, job->compressions[i]);
2184 cupsFilePuts(fp, "</Job>\n");
ef416fc2 2185 }
b9faaae1 2186
321d8d57 2187 cupsdCloseCreatedConfFile(fp, filename);
e1d6a774 2188}
ef416fc2 2189
ef416fc2 2190
e1d6a774 2191/*
b9faaae1 2192 * 'cupsdSaveJob()' - Save a job to disk.
e1d6a774 2193 */
ef416fc2 2194
e1d6a774 2195void
b9faaae1 2196cupsdSaveJob(cupsd_job_t *job) /* I - Job */
e1d6a774 2197{
82cc1f9a 2198 char filename[1024]; /* Job control filename */
b9faaae1 2199 cups_file_t *fp; /* Job file */
3dfe78b3 2200
ef416fc2 2201
b9faaae1
MS
2202 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdSaveJob(job=%p(%d)): job->attrs=%p",
2203 job, job->id, job->attrs);
ef416fc2 2204
b9faaae1 2205 snprintf(filename, sizeof(filename), "%s/c%05d", RequestRoot, job->id);
ef416fc2 2206
82cc1f9a 2207 if ((fp = cupsdCreateConfFile(filename, ConfigFilePerm & 0600)) == NULL)
b9faaae1 2208 return;
ef416fc2 2209
b9faaae1 2210 fchown(cupsFileNumber(fp), RunUser, Group);
ef416fc2 2211
b9faaae1 2212 job->attrs->state = IPP_IDLE;
ef416fc2 2213
b9faaae1
MS
2214 if (ippWriteIO(fp, (ipp_iocb_t)cupsFileWrite, 1, NULL,
2215 job->attrs) != IPP_DATA)
321d8d57 2216 {
72f50170 2217 cupsdLogJob(job, CUPSD_LOG_ERROR, "Unable to write job control file.");
321d8d57 2218 cupsFileClose(fp);
321d8d57
MS
2219 return;
2220 }
ef416fc2 2221
82cc1f9a 2222 if (!cupsdCloseCreatedConfFile(fp, filename))
cb7f98ee
MS
2223 {
2224 /*
2225 * Remove backup file and mark this job as clean...
2226 */
2227
2228 strlcat(filename, ".O", sizeof(filename));
2229 unlink(filename);
2230
82cc1f9a 2231 job->dirty = 0;
cb7f98ee 2232 }
e1d6a774 2233}
ef416fc2 2234
ef416fc2 2235
e1d6a774 2236/*
b9faaae1 2237 * 'cupsdSetJobHoldUntil()' - Set the hold time for a job.
e1d6a774 2238 */
ef416fc2 2239
b9faaae1
MS
2240void
2241cupsdSetJobHoldUntil(cupsd_job_t *job, /* I - Job */
2242 const char *when, /* I - When to resume */
2243 int update)/* I - Update job-hold-until attr? */
e1d6a774 2244{
b9faaae1
MS
2245 time_t curtime; /* Current time */
2246 struct tm *curdate; /* Current date */
2247 int hour; /* Hold hour */
2248 int minute; /* Hold minute */
2249 int second = 0; /* Hold second */
ef416fc2 2250
ef416fc2 2251
b9faaae1
MS
2252 cupsdLogMessage(CUPSD_LOG_DEBUG2,
2253 "cupsdSetJobHoldUntil(job=%p(%d), when=\"%s\", update=%d)",
2254 job, job->id, when, update);
ef416fc2 2255
b9faaae1 2256 if (update)
ef416fc2 2257 {
e1d6a774 2258 /*
b9faaae1 2259 * Update the job-hold-until attribute...
e1d6a774 2260 */
ef416fc2 2261
b9faaae1 2262 ipp_attribute_t *attr; /* job-hold-until attribute */
ef416fc2 2263
b9faaae1
MS
2264 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
2265 IPP_TAG_KEYWORD)) == NULL)
2266 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
ef416fc2 2267
b9faaae1
MS
2268 if (attr)
2269 cupsdSetString(&(attr->values[0].string.text), when);
2270 else
2271 attr = ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_KEYWORD,
2272 "job-hold-until", NULL, when);
ef416fc2 2273
b9faaae1
MS
2274 if (attr)
2275 {
2276 if (isdigit(when[0] & 255))
2277 attr->value_tag = IPP_TAG_NAME;
2278 else
2279 attr->value_tag = IPP_TAG_KEYWORD;
2280
2281 job->dirty = 1;
2282 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
2283 }
3e7fe0ca 2284
b9faaae1 2285 }
ef416fc2 2286
67dde2cc
MS
2287 ippSetString(job->attrs, &job->reasons, 0, "job-hold-until-specified");
2288
b9faaae1
MS
2289 /*
2290 * Update the hold time...
2291 */
2292
3e7fe0ca
MS
2293 job->cancel_time = 0;
2294
b9faaae1
MS
2295 if (!strcmp(when, "indefinite") || !strcmp(when, "auth-info-required"))
2296 {
e1d6a774 2297 /*
b9faaae1 2298 * Hold indefinitely...
e1d6a774 2299 */
ef416fc2 2300
b9faaae1 2301 job->hold_until = 0;
3e7fe0ca
MS
2302
2303 if (MaxHoldTime > 0)
2304 job->cancel_time = time(NULL) + MaxHoldTime;
b9faaae1
MS
2305 }
2306 else if (!strcmp(when, "day-time"))
2307 {
e1d6a774 2308 /*
b9faaae1 2309 * Hold to 6am the next morning unless local time is < 6pm.
e1d6a774 2310 */
ef416fc2 2311
b9faaae1
MS
2312 curtime = time(NULL);
2313 curdate = localtime(&curtime);
ef416fc2 2314
b9faaae1
MS
2315 if (curdate->tm_hour < 18)
2316 job->hold_until = curtime;
2317 else
2318 job->hold_until = curtime +
2319 ((29 - curdate->tm_hour) * 60 + 59 -
2320 curdate->tm_min) * 60 + 60 - curdate->tm_sec;
2321 }
2322 else if (!strcmp(when, "evening") || !strcmp(when, "night"))
2323 {
2324 /*
2325 * Hold to 6pm unless local time is > 6pm or < 6am.
2326 */
ef416fc2 2327
b9faaae1
MS
2328 curtime = time(NULL);
2329 curdate = localtime(&curtime);
ef416fc2 2330
b9faaae1
MS
2331 if (curdate->tm_hour < 6 || curdate->tm_hour >= 18)
2332 job->hold_until = curtime;
2333 else
2334 job->hold_until = curtime +
2335 ((17 - curdate->tm_hour) * 60 + 59 -
2336 curdate->tm_min) * 60 + 60 - curdate->tm_sec;
2337 }
2338 else if (!strcmp(when, "second-shift"))
2339 {
2340 /*
2341 * Hold to 4pm unless local time is > 4pm.
2342 */
ef416fc2 2343
b9faaae1
MS
2344 curtime = time(NULL);
2345 curdate = localtime(&curtime);
ef416fc2 2346
b9faaae1
MS
2347 if (curdate->tm_hour >= 16)
2348 job->hold_until = curtime;
2349 else
2350 job->hold_until = curtime +
2351 ((15 - curdate->tm_hour) * 60 + 59 -
2352 curdate->tm_min) * 60 + 60 - curdate->tm_sec;
2353 }
2354 else if (!strcmp(when, "third-shift"))
2355 {
2356 /*
2357 * Hold to 12am unless local time is < 8am.
2358 */
ef416fc2 2359
b9faaae1
MS
2360 curtime = time(NULL);
2361 curdate = localtime(&curtime);
ef416fc2 2362
b9faaae1
MS
2363 if (curdate->tm_hour < 8)
2364 job->hold_until = curtime;
2365 else
2366 job->hold_until = curtime +
2367 ((23 - curdate->tm_hour) * 60 + 59 -
2368 curdate->tm_min) * 60 + 60 - curdate->tm_sec;
2369 }
2370 else if (!strcmp(when, "weekend"))
2371 {
2372 /*
2373 * Hold to weekend unless we are in the weekend.
2374 */
ef416fc2 2375
b9faaae1
MS
2376 curtime = time(NULL);
2377 curdate = localtime(&curtime);
ef416fc2 2378
b9faaae1
MS
2379 if (curdate->tm_wday == 0 || curdate->tm_wday == 6)
2380 job->hold_until = curtime;
2381 else
2382 job->hold_until = curtime +
2383 (((5 - curdate->tm_wday) * 24 +
2384 (17 - curdate->tm_hour)) * 60 + 59 -
2385 curdate->tm_min) * 60 + 60 - curdate->tm_sec;
2386 }
2387 else if (sscanf(when, "%d:%d:%d", &hour, &minute, &second) >= 2)
2388 {
2389 /*
2390 * Hold to specified GMT time (HH:MM or HH:MM:SS)...
2391 */
bd7854cb 2392
b9faaae1
MS
2393 curtime = time(NULL);
2394 curdate = gmtime(&curtime);
2395
2396 job->hold_until = curtime +
2397 ((hour - curdate->tm_hour) * 60 + minute -
2398 curdate->tm_min) * 60 + second - curdate->tm_sec;
2399
2400 /*
2401 * Hold until next day as needed...
2402 */
2403
2404 if (job->hold_until < curtime)
2405 job->hold_until += 24 * 60 * 60;
ef416fc2 2406 }
2407
b9faaae1
MS
2408 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdSetJobHoldUntil: hold_until=%d",
2409 (int)job->hold_until);
e1d6a774 2410}
ef416fc2 2411
ef416fc2 2412
e1d6a774 2413/*
b9faaae1
MS
2414 * 'cupsdSetJobPriority()' - Set the priority of a job, moving it up/down in
2415 * the list as needed.
e1d6a774 2416 */
ef416fc2 2417
b9faaae1
MS
2418void
2419cupsdSetJobPriority(
2420 cupsd_job_t *job, /* I - Job ID */
2421 int priority) /* I - New priority (0 to 100) */
e1d6a774 2422{
b9faaae1 2423 ipp_attribute_t *attr; /* Job attribute */
ef416fc2 2424
ef416fc2 2425
e1d6a774 2426 /*
b9faaae1 2427 * Don't change completed jobs...
e1d6a774 2428 */
bd7854cb 2429
b9faaae1 2430 if (job->state_value >= IPP_JOB_PROCESSING)
ef416fc2 2431 return;
ef416fc2 2432
e1d6a774 2433 /*
b9faaae1 2434 * Set the new priority and re-add the job into the active list...
e1d6a774 2435 */
ef416fc2 2436
b9faaae1 2437 cupsArrayRemove(ActiveJobs, job);
ef416fc2 2438
b9faaae1 2439 job->priority = priority;
ef416fc2 2440
b9faaae1
MS
2441 if ((attr = ippFindAttribute(job->attrs, "job-priority",
2442 IPP_TAG_INTEGER)) != NULL)
2443 attr->values[0].integer = priority;
2444 else
2445 ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-priority",
2446 priority);
ef416fc2 2447
b9faaae1 2448 cupsArrayAdd(ActiveJobs, job);
bd7854cb 2449
b9faaae1
MS
2450 job->dirty = 1;
2451 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
2452}
bd7854cb 2453
ef416fc2 2454
b9faaae1
MS
2455/*
2456 * 'cupsdSetJobState()' - Set the state of the specified print job.
2457 */
e1d6a774 2458
b9faaae1
MS
2459void
2460cupsdSetJobState(
2461 cupsd_job_t *job, /* I - Job to cancel */
2462 ipp_jstate_t newstate, /* I - New job state */
2463 cupsd_jobaction_t action, /* I - Action to take */
2464 const char *message, /* I - Message to log */
2465 ...) /* I - Additional arguments as needed */
2466{
2467 int i; /* Looping var */
2468 ipp_jstate_t oldstate; /* Old state */
2469 char filename[1024]; /* Job filename */
2470 ipp_attribute_t *attr; /* Job attribute */
ef416fc2 2471
ef416fc2 2472
b9faaae1
MS
2473 cupsdLogMessage(CUPSD_LOG_DEBUG2,
2474 "cupsdSetJobState(job=%p(%d), state=%d, newstate=%d, "
2475 "action=%d, message=\"%s\")", job, job->id, job->state_value,
2476 newstate, action, message ? message : "(null)");
ef416fc2 2477
bd7854cb 2478
b9faaae1
MS
2479 /*
2480 * Make sure we have the job attributes...
2481 */
e1d6a774 2482
b9faaae1
MS
2483 if (!cupsdLoadJob(job))
2484 return;
bd7854cb 2485
f99f3698
MS
2486 /*
2487 * Don't do anything if the state is unchanged and we aren't purging the
2488 * job...
2489 */
2490
2491 oldstate = job->state_value;
2492 if (newstate == oldstate && action != CUPSD_JOB_PURGE)
2493 return;
e1d6a774 2494
b9faaae1
MS
2495 /*
2496 * Stop any processes that are working on the current job...
2497 */
e1d6a774 2498
b9faaae1 2499 if (oldstate == IPP_JOB_PROCESSING)
ef55b745 2500 stop_job(job, action);
b9faaae1
MS
2501
2502 /*
2503 * Set the new job state...
2504 */
2505
0fa6c7fa
MS
2506 job->state_value = newstate;
2507
2508 if (job->state)
2509 job->state->values[0].integer = newstate;
b9faaae1
MS
2510
2511 switch (newstate)
2512 {
2513 case IPP_JOB_PENDING :
2514 /*
2515 * Update job-hold-until as needed...
2516 */
2517
2518 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
2519 IPP_TAG_KEYWORD)) == NULL)
2520 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
2521
2522 if (attr)
ef416fc2 2523 {
b9faaae1
MS
2524 attr->value_tag = IPP_TAG_KEYWORD;
2525 cupsdSetString(&(attr->values[0].string.text), "no-hold");
e1d6a774 2526 }
ef416fc2 2527
b9faaae1
MS
2528 default :
2529 break;
ef416fc2 2530
b9faaae1
MS
2531 case IPP_JOB_ABORTED :
2532 case IPP_JOB_CANCELED :
2533 case IPP_JOB_COMPLETED :
2534 set_time(job, "time-at-completed");
12f89d24 2535 ippSetString(job->attrs, &job->reasons, 0, "processing-to-stop-point");
b9faaae1
MS
2536 break;
2537 }
ef416fc2 2538
b9faaae1
MS
2539 /*
2540 * Log message as needed...
2541 */
ef416fc2 2542
b9faaae1
MS
2543 if (message)
2544 {
2545 char buffer[2048]; /* Message buffer */
2546 va_list ap; /* Pointer to additional arguments */
ef416fc2 2547
b9faaae1
MS
2548 va_start(ap, message);
2549 vsnprintf(buffer, sizeof(buffer), message, ap);
2550 va_end(ap);
bd7854cb 2551
b9faaae1
MS
2552 if (newstate > IPP_JOB_STOPPED)
2553 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job, "%s", buffer);
2554 else
2555 cupsdAddEvent(CUPSD_EVENT_JOB_STATE, job->printer, job, "%s", buffer);
2556
2557 if (newstate == IPP_JOB_STOPPED || newstate == IPP_JOB_ABORTED)
2558 cupsdLogJob(job, CUPSD_LOG_ERROR, "%s", buffer);
2559 else
2560 cupsdLogJob(job, CUPSD_LOG_INFO, "%s", buffer);
2561 }
2562
2563 /*
2564 * Handle post-state-change actions...
2565 */
2566
2567 switch (newstate)
2568 {
2569 case IPP_JOB_PROCESSING :
e1d6a774 2570 /*
b9faaae1 2571 * Add the job to the "printing" list...
e1d6a774 2572 */
bd7854cb 2573
b9faaae1
MS
2574 if (!cupsArrayFind(PrintingJobs, job))
2575 cupsArrayAdd(PrintingJobs, job);
ef416fc2 2576
b9faaae1
MS
2577 /*
2578 * Set the processing time...
2579 */
ef416fc2 2580
b9faaae1
MS
2581 set_time(job, "time-at-processing");
2582
2583 case IPP_JOB_PENDING :
2584 case IPP_JOB_HELD :
2585 case IPP_JOB_STOPPED :
e1d6a774 2586 /*
b9faaae1 2587 * Make sure the job is in the active list...
e1d6a774 2588 */
ef416fc2 2589
b9faaae1
MS
2590 if (!cupsArrayFind(ActiveJobs, job))
2591 cupsArrayAdd(ActiveJobs, job);
ef416fc2 2592
b9faaae1
MS
2593 /*
2594 * Save the job state to disk...
2595 */
ef416fc2 2596
b9faaae1
MS
2597 job->dirty = 1;
2598 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
2599 break;
ef416fc2 2600
b9faaae1
MS
2601 case IPP_JOB_ABORTED :
2602 case IPP_JOB_CANCELED :
2603 case IPP_JOB_COMPLETED :
7a0cbd5e
MS
2604 if (newstate == IPP_JOB_CANCELED)
2605 {
2606 /*
2607 * Remove the job from the active list if there are no processes still
2608 * running for it...
2609 */
ef416fc2 2610
7a0cbd5e
MS
2611 for (i = 0; job->filters[i] < 0; i++);
2612
2613 if (!job->filters[i] && job->backend <= 0)
2614 cupsArrayRemove(ActiveJobs, job);
2615 }
2616 else
2617 {
2618 /*
2619 * Otherwise just remove the job from the active list immediately...
2620 */
2621
2622 cupsArrayRemove(ActiveJobs, job);
2623 }
ef416fc2 2624
b9faaae1 2625 /*
7a0cbd5e 2626 * Expire job subscriptions since the job is now "completed"...
b9faaae1 2627 */
ef416fc2 2628
7a0cbd5e 2629 cupsdExpireSubscriptions(NULL, job);
ef416fc2 2630
b9faaae1
MS
2631#ifdef __APPLE__
2632 /*
2633 * If we are going to sleep and the PrintingJobs count is now 0, allow the
2634 * sleep to happen immediately...
2635 */
ef416fc2 2636
b9faaae1
MS
2637 if (Sleeping && cupsArrayCount(PrintingJobs) == 0)
2638 cupsdAllowSleep();
2639#endif /* __APPLE__ */
ef416fc2 2640
b9faaae1
MS
2641 /*
2642 * Remove any authentication data...
2643 */
bd7854cb 2644
b9faaae1
MS
2645 snprintf(filename, sizeof(filename), "%s/a%05d", RequestRoot, job->id);
2646 if (cupsdRemoveFile(filename) && errno != ENOENT)
2647 cupsdLogMessage(CUPSD_LOG_ERROR,
2648 "Unable to remove authentication cache: %s",
2649 strerror(errno));
bd7854cb 2650
88f9aafc
MS
2651 for (i = 0;
2652 i < (int)(sizeof(job->auth_env) / sizeof(job->auth_env[0]));
2653 i ++)
2654 cupsdClearString(job->auth_env + i);
2655
07ed0e9a 2656 cupsdClearString(&job->auth_uid);
ef416fc2 2657
b9faaae1
MS
2658 /*
2659 * Remove the print file for good if we aren't preserving jobs or
2660 * files...
2661 */
e1d6a774 2662
b9faaae1 2663 if (!JobHistory || !JobFiles || action == CUPSD_JOB_PURGE)
82cc1f9a 2664 remove_job_files(job);
ef416fc2 2665
b9faaae1
MS
2666 if (JobHistory && action != CUPSD_JOB_PURGE)
2667 {
2668 /*
2669 * Save job state info...
2670 */
e1d6a774 2671
b9faaae1
MS
2672 job->dirty = 1;
2673 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
2674 }
ba55dc12
MS
2675 else if (!job->printer)
2676 {
2677 /*
2678 * Delete the job immediately if not actively printing...
2679 */
2680
2681 cupsdDeleteJob(job, CUPSD_JOB_PURGE);
2682 job = NULL;
2683 }
b9faaae1 2684 break;
e1d6a774 2685 }
2686
e07d4801
MS
2687 /*
2688 * Finalize the job immediately if we forced things...
2689 */
2690
ba55dc12 2691 if (action >= CUPSD_JOB_FORCE && job && job->printer)
ef55b745 2692 finalize_job(job, 0);
e07d4801 2693
e1d6a774 2694 /*
b9faaae1 2695 * Update the server "busy" state...
e1d6a774 2696 */
ef416fc2 2697
b9faaae1
MS
2698 cupsdSetBusyState();
2699}
e00b005a 2700
ef416fc2 2701
b9faaae1
MS
2702/*
2703 * 'cupsdStopAllJobs()' - Stop all print jobs.
2704 */
ef416fc2 2705
b9faaae1
MS
2706void
2707cupsdStopAllJobs(
238c3832
MS
2708 cupsd_jobaction_t action, /* I - Action */
2709 int kill_delay) /* I - Number of seconds before we kill */
b9faaae1
MS
2710{
2711 cupsd_job_t *job; /* Current job */
ef416fc2 2712
ef416fc2 2713
b9faaae1 2714 DEBUG_puts("cupsdStopAllJobs()");
ef416fc2 2715
b9faaae1
MS
2716 for (job = (cupsd_job_t *)cupsArrayFirst(PrintingJobs);
2717 job;
2718 job = (cupsd_job_t *)cupsArrayNext(PrintingJobs))
238c3832 2719 {
581dae2d
MS
2720 if (job->completed)
2721 {
2722 cupsdSetJobState(job, IPP_JOB_COMPLETED, CUPSD_JOB_FORCE, NULL);
2723 }
2724 else
2725 {
2726 if (kill_delay)
2727 job->kill_time = time(NULL) + kill_delay;
238c3832 2728
581dae2d
MS
2729 cupsdSetJobState(job, IPP_JOB_PENDING, action, NULL);
2730 }
238c3832 2731 }
b9faaae1 2732}
bd7854cb 2733
bd7854cb 2734
b9faaae1
MS
2735/*
2736 * 'cupsdUnloadCompletedJobs()' - Flush completed job history from memory.
2737 */
ef416fc2 2738
b9faaae1
MS
2739void
2740cupsdUnloadCompletedJobs(void)
2741{
2742 cupsd_job_t *job; /* Current job */
2743 time_t expire; /* Expiration time */
ef416fc2 2744
b9faaae1
MS
2745
2746 expire = time(NULL) - 60;
2747
2748 for (job = (cupsd_job_t *)cupsArrayFirst(Jobs);
2749 job;
2750 job = (cupsd_job_t *)cupsArrayNext(Jobs))
2751 if (job->attrs && job->state_value >= IPP_JOB_STOPPED && !job->printer &&
2752 job->access_time < expire)
2753 {
2754 if (job->dirty)
2755 cupsdSaveJob(job);
2756
2757 unload_job(job);
2758 }
e1d6a774 2759}
ef416fc2 2760
ef416fc2 2761
82cc1f9a
MS
2762/*
2763 * 'cupsdUpdateJobs()' - Update the history/file files for all jobs.
2764 */
2765
2766void
2767cupsdUpdateJobs(void)
2768{
2769 cupsd_job_t *job; /* Current job */
2770 time_t curtime; /* Current time */
2771 ipp_attribute_t *attr; /* time-at-completed attribute */
2772
2773
2774 curtime = time(NULL);
2775 JobHistoryUpdate = 0;
2776
2777 for (job = (cupsd_job_t *)cupsArrayFirst(Jobs);
2778 job;
2779 job = (cupsd_job_t *)cupsArrayNext(Jobs))
2780 {
2781 if (job->state_value >= IPP_JOB_CANCELED &&
2782 (attr = ippFindAttribute(job->attrs, "time-at-completed",
2783 IPP_TAG_INTEGER)) != NULL)
2784 {
2785 /*
2786 * Update history/file expiration times...
2787 */
2788
2789 if (JobHistory < INT_MAX)
2790 job->history_time = attr->values[0].integer + JobHistory;
2791 else
2792 job->history_time = INT_MAX;
2793
2794 if (job->history_time < curtime)
2795 {
2796 cupsdDeleteJob(job, CUPSD_JOB_PURGE);
2797 continue;
2798 }
2799
2800 if (job->history_time < JobHistoryUpdate || !JobHistoryUpdate)
2801 JobHistoryUpdate = job->history_time;
2802
2803 if (JobFiles < INT_MAX)
2804 job->file_time = attr->values[0].integer + JobFiles;
2805 else
2806 job->file_time = INT_MAX;
2807
2808 if (job->file_time < JobHistoryUpdate || !JobHistoryUpdate)
2809 JobHistoryUpdate = job->file_time;
2810 }
2811 }
2812
2813 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdUpdateAllJobs: JobHistoryUpdate=%ld",
2814 (long)JobHistoryUpdate);
2815}
2816
2817
e1d6a774 2818/*
b9faaae1 2819 * 'compare_active_jobs()' - Compare the job IDs and priorities of two jobs.
e1d6a774 2820 */
ef416fc2 2821
b9faaae1
MS
2822static int /* O - Difference */
2823compare_active_jobs(void *first, /* I - First job */
2824 void *second, /* I - Second job */
2825 void *data) /* I - App data (not used) */
e1d6a774 2826{
b9faaae1 2827 int diff; /* Difference */
ef416fc2 2828
ef416fc2 2829
321d8d57
MS
2830 (void)data;
2831
b9faaae1
MS
2832 if ((diff = ((cupsd_job_t *)second)->priority -
2833 ((cupsd_job_t *)first)->priority) != 0)
2834 return (diff);
2835 else
2836 return (((cupsd_job_t *)first)->id - ((cupsd_job_t *)second)->id);
e1d6a774 2837}
bd7854cb 2838
ef416fc2 2839
eec1fbc3
MS
2840/*
2841 * 'compare_completed_jobs()' - Compare the job IDs and completion times of two jobs.
2842 */
2843
2844static int /* O - Difference */
2845compare_completed_jobs(void *first, /* I - First job */
2846 void *second, /* I - Second job */
2847 void *data) /* I - App data (not used) */
2848{
2849 int diff; /* Difference */
2850
2851
2852 (void)data;
2853
2854 if ((diff = ((cupsd_job_t *)second)->completed_time -
2855 ((cupsd_job_t *)first)->completed_time) != 0)
2856 return (diff);
2857 else
2858 return (((cupsd_job_t *)first)->id - ((cupsd_job_t *)second)->id);
2859}
2860
2861
e1d6a774 2862/*
b9faaae1 2863 * 'compare_jobs()' - Compare the job IDs of two jobs.
e1d6a774 2864 */
ef416fc2 2865
b9faaae1
MS
2866static int /* O - Difference */
2867compare_jobs(void *first, /* I - First job */
2868 void *second, /* I - Second job */
2869 void *data) /* I - App data (not used) */
e1d6a774 2870{
321d8d57
MS
2871 (void)data;
2872
b9faaae1
MS
2873 return (((cupsd_job_t *)first)->id - ((cupsd_job_t *)second)->id);
2874}
ef416fc2 2875
ef416fc2 2876
178cb736
MS
2877/*
2878 * 'dump_job_history()' - Dump any debug messages for a job.
2879 */
2880
2881static void
2882dump_job_history(cupsd_job_t *job) /* I - Job */
2883{
2884 int i, /* Looping var */
2885 oldsize; /* Current MaxLogSize */
2886 struct tm *date; /* Date/time value */
2887 cupsd_joblog_t *message; /* Current message */
2888 char temp[2048], /* Log message */
2889 *ptr, /* Pointer into log message */
2890 start[256], /* Start time */
2891 end[256]; /* End time */
2892 cupsd_printer_t *printer; /* Printer for job */
2893
2894
2895 /*
2896 * See if we have anything to dump...
2897 */
2898
2899 if (!job->history)
2900 return;
2901
2902 /*
2903 * Disable log rotation temporarily...
2904 */
2905
2906 oldsize = MaxLogSize;
2907 MaxLogSize = 0;
2908
2909 /*
2910 * Copy the debug messages to the log...
2911 */
2912
2913 message = (cupsd_joblog_t *)cupsArrayFirst(job->history);
2914 date = localtime(&(message->time));
2915 strftime(start, sizeof(start), "%X", date);
2916
2917 message = (cupsd_joblog_t *)cupsArrayLast(job->history);
2918 date = localtime(&(message->time));
2919 strftime(end, sizeof(end), "%X", date);
2920
2921 snprintf(temp, sizeof(temp),
2922 "[Job %d] The following messages were recorded from %s to %s",
2923 job->id, start, end);
2924 cupsdWriteErrorLog(CUPSD_LOG_DEBUG, temp);
2925
2926 for (message = (cupsd_joblog_t *)cupsArrayFirst(job->history);
2927 message;
2928 message = (cupsd_joblog_t *)cupsArrayNext(job->history))
2929 cupsdWriteErrorLog(CUPSD_LOG_DEBUG, message->message);
2930
2931 snprintf(temp, sizeof(temp), "[Job %d] End of messages", job->id);
2932 cupsdWriteErrorLog(CUPSD_LOG_DEBUG, temp);
2933
2934 /*
2935 * Log the printer state values...
2936 */
2937
2938 if ((printer = job->printer) == NULL)
2939 printer = cupsdFindDest(job->dest);
2940
2941 if (printer)
2942 {
2943 snprintf(temp, sizeof(temp), "[Job %d] printer-state=%d(%s)", job->id,
2944 printer->state,
2945 printer->state == IPP_PRINTER_IDLE ? "idle" :
2946 printer->state == IPP_PRINTER_PROCESSING ? "processing" :
2947 "stopped");
2948 cupsdWriteErrorLog(CUPSD_LOG_DEBUG, temp);
2949
2950 snprintf(temp, sizeof(temp), "[Job %d] printer-state-message=\"%s\"",
2951 job->id, printer->state_message);
2952 cupsdWriteErrorLog(CUPSD_LOG_DEBUG, temp);
2953
2954 snprintf(temp, sizeof(temp), "[Job %d] printer-state-reasons=", job->id);
2955 ptr = temp + strlen(temp);
2956 if (printer->num_reasons == 0)
7e86f2f6 2957 strlcpy(ptr, "none", sizeof(temp) - (size_t)(ptr - temp));
178cb736
MS
2958 else
2959 {
2960 for (i = 0;
2961 i < printer->num_reasons && ptr < (temp + sizeof(temp) - 2);
2962 i ++)
2963 {
2964 if (i)
2965 *ptr++ = ',';
2966
7e86f2f6 2967 strlcpy(ptr, printer->reasons[i], sizeof(temp) - (size_t)(ptr - temp));
178cb736
MS
2968 ptr += strlen(ptr);
2969 }
2970 }
2971 cupsdWriteErrorLog(CUPSD_LOG_DEBUG, temp);
2972 }
2973
2974 /*
2975 * Restore log file rotation...
2976 */
2977
2978 MaxLogSize = oldsize;
2979
2980 /*
2981 * Free all messages...
2982 */
2983
2984 free_job_history(job);
2985}
2986
2987
2988/*
2989 * 'free_job_history()' - Free any log history.
2990 */
2991
2992static void
2993free_job_history(cupsd_job_t *job) /* I - Job */
2994{
2995 char *message; /* Current message */
2996
2997
2998 if (!job->history)
2999 return;
3000
3001 for (message = (char *)cupsArrayFirst(job->history);
3002 message;
3003 message = (char *)cupsArrayNext(job->history))
3004 free(message);
3005
3006 cupsArrayDelete(job->history);
3007 job->history = NULL;
3008}
3009
3010
b9faaae1
MS
3011/*
3012 * 'finalize_job()' - Cleanup after job filter processes and support data.
3013 */
ef416fc2 3014
b9faaae1 3015static void
ef55b745
MS
3016finalize_job(cupsd_job_t *job, /* I - Job */
3017 int set_job_state) /* I - 1 = set the job state */
b9faaae1
MS
3018{
3019 ipp_pstate_t printer_state; /* New printer state value */
3020 ipp_jstate_t job_state; /* New job state value */
3021 const char *message; /* Message for job state */
3022 char buffer[1024]; /* Buffer for formatted messages */
e00b005a 3023
b9faaae1
MS
3024
3025 cupsdLogMessage(CUPSD_LOG_DEBUG2, "finalize_job(job=%p(%d))", job, job->id);
ef416fc2 3026
e1d6a774 3027 /*
1e3e80bb
MS
3028 * Clear the "connecting-to-device" and "cups-waiting-for-job-completed"
3029 * reasons, which are only valid when a printer is processing, along with any
3030 * remote printing job state...
e1d6a774 3031 */
ef416fc2 3032
07ed0e9a 3033 cupsdSetPrinterReasons(job->printer, "-connecting-to-device,"
1e3e80bb 3034 "cups-waiting-for-job-completed,"
07ed0e9a
MS
3035 "cups-remote-pending,"
3036 "cups-remote-pending-held,"
3037 "cups-remote-processing,"
3038 "cups-remote-stopped,"
3039 "cups-remote-canceled,"
3040 "cups-remote-aborted,"
3041 "cups-remote-completed");
ef416fc2 3042
e1d6a774 3043 /*
b9faaae1
MS
3044 * Similarly, clear the "offline-report" reason for non-USB devices since we
3045 * rarely have current information for network devices...
e1d6a774 3046 */
ef416fc2 3047
a469f8a5
MS
3048 if (strncmp(job->printer->device_uri, "usb:", 4) &&
3049 strncmp(job->printer->device_uri, "ippusb:", 7))
b9faaae1 3050 cupsdSetPrinterReasons(job->printer, "-offline-report");
ef416fc2 3051
b9faaae1
MS
3052 /*
3053 * Free the security profile...
3054 */
ef416fc2 3055
b9faaae1
MS
3056 cupsdDestroyProfile(job->profile);
3057 job->profile = NULL;
8fe0183a
MS
3058 cupsdDestroyProfile(job->bprofile);
3059 job->bprofile = NULL;
ef416fc2 3060
10d09e33 3061 /*
a469f8a5 3062 * Clear the unresponsive job watchdog timers...
10d09e33
MS
3063 */
3064
a469f8a5
MS
3065 job->cancel_time = 0;
3066 job->kill_time = 0;
10d09e33 3067
b9faaae1
MS
3068 /*
3069 * Close pipes and status buffer...
3070 */
ef416fc2 3071
b9faaae1
MS
3072 cupsdClosePipe(job->print_pipes);
3073 cupsdClosePipe(job->back_pipes);
3074 cupsdClosePipe(job->side_pipes);
ef416fc2 3075
e07d4801
MS
3076 cupsdRemoveSelect(job->status_pipes[0]);
3077 cupsdClosePipe(job->status_pipes);
b9faaae1
MS
3078 cupsdStatBufDelete(job->status_buffer);
3079 job->status_buffer = NULL;
ef416fc2 3080
e1d6a774 3081 /*
b9faaae1 3082 * Process the exit status...
e1d6a774 3083 */
ef416fc2 3084
1340db2d
MS
3085 if (job->printer->state == IPP_PRINTER_PROCESSING)
3086 printer_state = IPP_PRINTER_IDLE;
3087 else
3088 printer_state = job->printer->state;
3089
3090 switch (job_state = job->state_value)
3091 {
3092 case IPP_JOB_PENDING :
3093 message = "Job paused.";
3094 break;
3095
3096 case IPP_JOB_HELD :
3097 message = "Job held.";
3098 break;
3099
3100 default :
3101 case IPP_JOB_PROCESSING :
3102 case IPP_JOB_COMPLETED :
f11a948a
MS
3103 job_state = IPP_JOB_COMPLETED;
3104 message = "Job completed.";
12f89d24 3105
6961465f
MS
3106 if (!job->status)
3107 ippSetString(job->attrs, &job->reasons, 0,
3108 "job-completed-successfully");
1340db2d
MS
3109 break;
3110
3111 case IPP_JOB_STOPPED :
3112 message = "Job stopped.";
12f89d24
MS
3113
3114 ippSetString(job->attrs, &job->reasons, 0, "job-stopped");
1340db2d
MS
3115 break;
3116
3117 case IPP_JOB_CANCELED :
3118 message = "Job canceled.";
12f89d24
MS
3119
3120 ippSetString(job->attrs, &job->reasons, 0, "job-canceled-by-user");
1340db2d
MS
3121 break;
3122
3123 case IPP_JOB_ABORTED :
3124 message = "Job aborted.";
3125 break;
3126 }
ef416fc2 3127
b9faaae1 3128 if (job->status < 0)
e1d6a774 3129 {
3130 /*
b9faaae1 3131 * Backend had errors...
e1d6a774 3132 */
ef416fc2 3133
b9faaae1
MS
3134 int exit_code; /* Exit code from backend */
3135
e1d6a774 3136 /*
b9faaae1
MS
3137 * Convert the status to an exit code. Due to the way the W* macros are
3138 * implemented on MacOS X (bug?), we have to store the exit status in a
3139 * variable first and then convert...
e1d6a774 3140 */
ef416fc2 3141
b9faaae1
MS
3142 exit_code = -job->status;
3143 if (WIFEXITED(exit_code))
3144 exit_code = WEXITSTATUS(exit_code);
3145 else
12f89d24
MS
3146 {
3147 ippSetString(job->attrs, &job->reasons, 0, "cups-backend-crashed");
b9faaae1 3148 exit_code = job->status;
12f89d24 3149 }
ef416fc2 3150
b9faaae1
MS
3151 cupsdLogJob(job, CUPSD_LOG_INFO, "Backend returned status %d (%s)",
3152 exit_code,
3153 exit_code == CUPS_BACKEND_FAILED ? "failed" :
3154 exit_code == CUPS_BACKEND_AUTH_REQUIRED ?
3155 "authentication required" :
3156 exit_code == CUPS_BACKEND_HOLD ? "hold job" :
3157 exit_code == CUPS_BACKEND_STOP ? "stop printer" :
3158 exit_code == CUPS_BACKEND_CANCEL ? "cancel job" :
a469f8a5
MS
3159 exit_code == CUPS_BACKEND_RETRY ? "retry job later" :
3160 exit_code == CUPS_BACKEND_RETRY_CURRENT ? "retry job immediately" :
b9faaae1 3161 exit_code < 0 ? "crashed" : "unknown");
ef416fc2 3162
ef416fc2 3163 /*
b9faaae1 3164 * Do what needs to be done...
ef416fc2 3165 */
3166
b9faaae1 3167 switch (exit_code)
f7deaa1a 3168 {
b9faaae1
MS
3169 default :
3170 case CUPS_BACKEND_FAILED :
3171 /*
3172 * Backend failure, use the error-policy to determine how to
3173 * act...
3174 */
f7deaa1a 3175
a2326b5b 3176 if (job->dtype & CUPS_PRINTER_CLASS)
b9faaae1
MS
3177 {
3178 /*
3179 * Queued on a class - mark the job as pending and we'll retry on
3180 * another printer...
3181 */
f7deaa1a 3182
8b116e60
MS
3183 if (job_state == IPP_JOB_COMPLETED)
3184 {
3185 job_state = IPP_JOB_PENDING;
3186 message = "Retrying job on another printer.";
12f89d24
MS
3187
3188 ippSetString(job->attrs, &job->reasons, 0,
3189 "resources-are-not-ready");
8b116e60 3190 }
b9faaae1
MS
3191 }
3192 else if (!strcmp(job->printer->error_policy, "retry-current-job"))
3193 {
3194 /*
3195 * The error policy is "retry-current-job" - mark the job as pending
3196 * and we'll retry on the same printer...
3197 */
f7deaa1a 3198
8b116e60
MS
3199 if (job_state == IPP_JOB_COMPLETED)
3200 {
3201 job_state = IPP_JOB_PENDING;
3202 message = "Retrying job on same printer.";
12f89d24
MS
3203
3204 ippSetString(job->attrs, &job->reasons, 0, "none");
8b116e60 3205 }
b9faaae1
MS
3206 }
3207 else if ((job->printer->type & CUPS_PRINTER_FAX) ||
3208 !strcmp(job->printer->error_policy, "retry-job"))
3209 {
8b116e60 3210 if (job_state == IPP_JOB_COMPLETED)
b9faaae1
MS
3211 {
3212 /*
8b116e60
MS
3213 * The job was queued on a fax or the error policy is "retry-job" -
3214 * hold the job if the number of retries is less than the
3215 * JobRetryLimit, otherwise abort the job.
b9faaae1 3216 */
ef416fc2 3217
8b116e60
MS
3218 job->tries ++;
3219
7cf5915e 3220 if (job->tries > JobRetryLimit && JobRetryLimit > 0)
8b116e60
MS
3221 {
3222 /*
3223 * Too many tries...
3224 */
3225
3226 snprintf(buffer, sizeof(buffer),
3227 "Job aborted after %d unsuccessful attempts.",
3228 JobRetryLimit);
3229 job_state = IPP_JOB_ABORTED;
3230 message = buffer;
12f89d24
MS
3231
3232 ippSetString(job->attrs, &job->reasons, 0, "aborted-by-system");
8b116e60
MS
3233 }
3234 else
3235 {
3236 /*
3237 * Try again in N seconds...
3238 */
ef416fc2 3239
8b116e60
MS
3240 snprintf(buffer, sizeof(buffer),
3241 "Job held for %d seconds since it could not be sent.",
3242 JobRetryInterval);
0268488e
MS
3243
3244 job->hold_until = time(NULL) + JobRetryInterval;
3245 job_state = IPP_JOB_HELD;
3246 message = buffer;
12f89d24
MS
3247
3248 ippSetString(job->attrs, &job->reasons, 0,
3249 "resources-are-not-ready");
8b116e60
MS
3250 }
3251 }
b9faaae1 3252 }
8b116e60
MS
3253 else if (!strcmp(job->printer->error_policy, "abort-job") &&
3254 job_state == IPP_JOB_COMPLETED)
b9faaae1
MS
3255 {
3256 job_state = IPP_JOB_ABORTED;
3257 message = "Job aborted due to backend errors; please consult "
3258 "the error_log file for details.";
12f89d24
MS
3259
3260 ippSetString(job->attrs, &job->reasons, 0, "aborted-by-system");
b9faaae1 3261 }
f11a948a 3262 else if (job->state_value == IPP_JOB_PROCESSING)
b9faaae1 3263 {
f11a948a 3264 job_state = IPP_JOB_PENDING;
b9faaae1 3265 printer_state = IPP_PRINTER_STOPPED;
b9faaae1 3266 message = "Printer stopped due to backend errors; please "
8b116e60 3267 "consult the error_log file for details.";
12f89d24
MS
3268
3269 ippSetString(job->attrs, &job->reasons, 0, "none");
b9faaae1
MS
3270 }
3271 break;
bd7854cb 3272
b9faaae1
MS
3273 case CUPS_BACKEND_CANCEL :
3274 /*
f3c17241 3275 * Cancel the job...
b9faaae1 3276 */
bd7854cb 3277
8b116e60
MS
3278 if (job_state == IPP_JOB_COMPLETED)
3279 {
f3c17241
MS
3280 job_state = IPP_JOB_CANCELED;
3281 message = "Job canceled at printer.";
12f89d24 3282
f3c17241 3283 ippSetString(job->attrs, &job->reasons, 0, "canceled-at-device");
8b116e60 3284 }
b9faaae1 3285 break;
bd7854cb 3286
b9faaae1 3287 case CUPS_BACKEND_HOLD :
8b116e60
MS
3288 if (job_state == IPP_JOB_COMPLETED)
3289 {
3290 /*
3291 * Hold the job...
3292 */
bd7854cb 3293
6961465f 3294 const char *reason = ippGetString(job->reasons, 0, NULL);
bd7854cb 3295
6961465f
MS
3296 cupsdLogJob(job, CUPSD_LOG_DEBUG, "job-state-reasons=\"%s\"",
3297 reason);
3298
3299 if (!reason || strncmp(reason, "account-", 8))
3300 {
3301 cupsdSetJobHoldUntil(job, "indefinite", 1);
3302
3303 ippSetString(job->attrs, &job->reasons, 0,
3304 "job-hold-until-specified");
3305 message = "Job held indefinitely due to backend errors; please "
8b116e60 3306 "consult the error_log file for details.";
6961465f
MS
3307 }
3308 else if (!strcmp(reason, "account-info-needed"))
3309 {
3310 cupsdSetJobHoldUntil(job, "indefinite", 0);
3311
3312 message = "Job held indefinitely - account information is "
3313 "required.";
3314 }
3315 else if (!strcmp(reason, "account-closed"))
3316 {
3317 cupsdSetJobHoldUntil(job, "indefinite", 0);
3318
3319 message = "Job held indefinitely - account has been closed.";
3320 }
3321 else if (!strcmp(reason, "account-limit-reached"))
3322 {
3323 cupsdSetJobHoldUntil(job, "indefinite", 0);
3324
3325 message = "Job held indefinitely - account limit has been "
3326 "reached.";
3327 }
3328 else
3329 {
3330 cupsdSetJobHoldUntil(job, "indefinite", 0);
3331
3332 message = "Job held indefinitely - account authorization failed.";
3333 }
3334
3335 job_state = IPP_JOB_HELD;
8b116e60 3336 }
b9faaae1 3337 break;
2abf387c 3338
b9faaae1
MS
3339 case CUPS_BACKEND_STOP :
3340 /*
3341 * Stop the printer...
3342 */
bd7854cb 3343
b9faaae1 3344 printer_state = IPP_PRINTER_STOPPED;
b9faaae1
MS
3345 message = "Printer stopped due to backend errors; please "
3346 "consult the error_log file for details.";
8b116e60
MS
3347
3348 if (job_state == IPP_JOB_COMPLETED)
12f89d24 3349 {
8b116e60 3350 job_state = IPP_JOB_PENDING;
12f89d24
MS
3351
3352 ippSetString(job->attrs, &job->reasons, 0,
3353 "resources-are-not-ready");
3354 }
b9faaae1 3355 break;
bd7854cb 3356
b9faaae1
MS
3357 case CUPS_BACKEND_AUTH_REQUIRED :
3358 /*
3359 * Hold the job for authentication...
3360 */
bd7854cb 3361
8b116e60
MS
3362 if (job_state == IPP_JOB_COMPLETED)
3363 {
3364 cupsdSetJobHoldUntil(job, "auth-info-required", 1);
ef416fc2 3365
8b116e60
MS
3366 job_state = IPP_JOB_HELD;
3367 message = "Job held for authentication.";
12f89d24 3368
a469f8a5
MS
3369 if (strncmp(job->reasons->values[0].string.text, "account-", 8))
3370 ippSetString(job->attrs, &job->reasons, 0,
3371 "cups-held-for-authentication");
8b116e60 3372 }
b9faaae1 3373 break;
22c9029b
MS
3374
3375 case CUPS_BACKEND_RETRY :
3376 if (job_state == IPP_JOB_COMPLETED)
3377 {
3378 /*
3379 * Hold the job if the number of retries is less than the
3380 * JobRetryLimit, otherwise abort the job.
3381 */
3382
3383 job->tries ++;
3384
3385 if (job->tries > JobRetryLimit && JobRetryLimit > 0)
3386 {
3387 /*
3388 * Too many tries...
3389 */
3390
3391 snprintf(buffer, sizeof(buffer),
3392 "Job aborted after %d unsuccessful attempts.",
3393 JobRetryLimit);
3394 job_state = IPP_JOB_ABORTED;
3395 message = buffer;
12f89d24
MS
3396
3397 ippSetString(job->attrs, &job->reasons, 0, "aborted-by-system");
22c9029b
MS
3398 }
3399 else
3400 {
3401 /*
3402 * Try again in N seconds...
3403 */
3404
3405 snprintf(buffer, sizeof(buffer),
3406 "Job held for %d seconds since it could not be sent.",
3407 JobRetryInterval);
3408
3409 job->hold_until = time(NULL) + JobRetryInterval;
3410 job_state = IPP_JOB_HELD;
3411 message = buffer;
12f89d24
MS
3412
3413 ippSetString(job->attrs, &job->reasons, 0,
3414 "resources-are-not-ready");
22c9029b
MS
3415 }
3416 }
3417 break;
3418
3419 case CUPS_BACKEND_RETRY_CURRENT :
3420 /*
3421 * Mark the job as pending and retry on the same printer...
3422 */
3423
3424 if (job_state == IPP_JOB_COMPLETED)
3425 {
3426 job_state = IPP_JOB_PENDING;
3427 message = "Retrying job on same printer.";
12f89d24
MS
3428
3429 ippSetString(job->attrs, &job->reasons, 0, "none");
22c9029b
MS
3430 }
3431 break;
e1d6a774 3432 }
3433 }
b9faaae1 3434 else if (job->status > 0)
ef416fc2 3435 {
3436 /*
b9faaae1 3437 * Filter had errors; stop job...
ef416fc2 3438 */
3439
8b116e60
MS
3440 if (job_state == IPP_JOB_COMPLETED)
3441 {
3442 job_state = IPP_JOB_STOPPED;
3443 message = "Job stopped due to filter errors; please consult the "
3444 "error_log file for details.";
12f89d24
MS
3445
3446 if (WIFSIGNALED(job->status))
3447 ippSetString(job->attrs, &job->reasons, 0, "cups-filter-crashed");
3448 else
3449 ippSetString(job->attrs, &job->reasons, 0, "job-completed-with-errors");
8b116e60 3450 }
e1d6a774 3451 }
e00b005a 3452
3dfe78b3 3453 /*
b9faaae1 3454 * Update the printer and job state.
3dfe78b3
MS
3455 */
3456
321d8d57 3457 if (set_job_state && job_state != job->state_value)
ef55b745
MS
3458 cupsdSetJobState(job, job_state, CUPSD_JOB_DEFAULT, "%s", message);
3459
b9faaae1
MS
3460 cupsdSetPrinterState(job->printer, printer_state,
3461 printer_state == IPP_PRINTER_STOPPED);
3462 update_job_attrs(job, 0);
3dfe78b3 3463
178cb736
MS
3464 if (job->history)
3465 {
a4845881
MS
3466 if (job->status &&
3467 (job->state_value == IPP_JOB_ABORTED ||
3468 job->state_value == IPP_JOB_STOPPED))
178cb736
MS
3469 dump_job_history(job);
3470 else
3471 free_job_history(job);
3472 }
3473
b9faaae1 3474 cupsArrayRemove(PrintingJobs, job);
3dfe78b3 3475
a29fd7dd
MS
3476 /*
3477 * Apply any PPD updates...
3478 */
3479
3480 if (job->num_keywords)
3481 {
3482 if (cupsdUpdatePrinterPPD(job->printer, job->num_keywords, job->keywords))
3483 cupsdSetPrinterAttrs(job->printer);
3484
3485 cupsFreeOptions(job->num_keywords, job->keywords);
3486
3487 job->num_keywords = 0;
3488 job->keywords = NULL;
3489 }
3490
e1d6a774 3491 /*
b9faaae1 3492 * Clear the printer <-> job association...
e1d6a774 3493 */
ef416fc2 3494
b9faaae1
MS
3495 job->printer->job = NULL;
3496 job->printer = NULL;
ef416fc2 3497
e1d6a774 3498 /*
b9faaae1 3499 * Try printing another job...
e1d6a774 3500 */
ef416fc2 3501
b9faaae1
MS
3502 if (printer_state != IPP_PRINTER_STOPPED)
3503 cupsdCheckJobs();
3504}
ef416fc2 3505
ef416fc2 3506
b9faaae1
MS
3507/*
3508 * 'get_options()' - Get a string containing the job options.
3509 */
3510
3511static char * /* O - Options string */
3512get_options(cupsd_job_t *job, /* I - Job */
3513 int banner_page, /* I - Printing a banner page? */
3514 char *copies, /* I - Copies buffer */
3515 size_t copies_size, /* I - Size of copies buffer */
3516 char *title, /* I - Title buffer */
3517 size_t title_size) /* I - Size of title buffer */
3518{
3519 int i; /* Looping var */
c7017ecc 3520 size_t newlength; /* New option buffer length */
b9faaae1
MS
3521 char *optptr, /* Pointer to options */
3522 *valptr; /* Pointer in value string */
3523 ipp_attribute_t *attr; /* Current attribute */
f14324a7 3524 _ppd_cache_t *pc; /* PPD cache and mapping data */
c7017ecc
MS
3525 int num_pwgppds; /* Number of PWG->PPD options */
3526 cups_option_t *pwgppds, /* PWG->PPD options */
3527 *pwgppd, /* Current PWG->PPD option */
3528 *preset; /* Current preset option */
f14324a7
MS
3529 int print_color_mode,
3530 /* Output mode (if any) */
c7017ecc
MS
3531 print_quality; /* Print quality (if any) */
3532 const char *ppd; /* PPD option choice */
3533 int exact; /* Did we get an exact match? */
b9faaae1 3534 static char *options = NULL;/* Full list of options */
c7017ecc 3535 static size_t optlength = 0; /* Length of option buffer */
b9faaae1 3536
ef416fc2 3537
e1d6a774 3538 /*
c7017ecc
MS
3539 * Building the options string is harder than it needs to be, but for the
3540 * moment we need to pass strings for command-line args and not IPP attribute
3541 * pointers... :)
e1d6a774 3542 *
c7017ecc 3543 * First build an options array for any PWG->PPD mapped option/choice pairs.
e1d6a774 3544 */
ef416fc2 3545
f14324a7 3546 pc = job->printer->pc;
c7017ecc
MS
3547 num_pwgppds = 0;
3548 pwgppds = NULL;
ef416fc2 3549
f14324a7 3550 if (pc &&
7cf5915e
MS
3551 !ippFindAttribute(job->attrs,
3552 "com.apple.print.DocumentTicket.PMSpoolFormat",
3553 IPP_TAG_ZERO) &&
0268488e 3554 !ippFindAttribute(job->attrs, "APPrinterPreset", IPP_TAG_ZERO) &&
5a9febac 3555 (ippFindAttribute(job->attrs, "print-color-mode", IPP_TAG_ZERO) ||
7cf5915e 3556 ippFindAttribute(job->attrs, "print-quality", IPP_TAG_ZERO)))
c7017ecc 3557 {
7cf5915e 3558 /*
5a9febac 3559 * Map print-color-mode and print-quality to a preset...
7cf5915e
MS
3560 */
3561
f14324a7 3562 if ((attr = ippFindAttribute(job->attrs, "print-color-mode",
5a9febac
MS
3563 IPP_TAG_KEYWORD)) != NULL &&
3564 !strcmp(attr->values[0].string.text, "monochrome"))
f14324a7 3565 print_color_mode = _PWG_PRINT_COLOR_MODE_MONOCHROME;
c7017ecc 3566 else
f14324a7 3567 print_color_mode = _PWG_PRINT_COLOR_MODE_COLOR;
c7017ecc
MS
3568
3569 if ((attr = ippFindAttribute(job->attrs, "print-quality",
10d09e33 3570 IPP_TAG_ENUM)) != NULL &&
c7017ecc
MS
3571 attr->values[0].integer >= IPP_QUALITY_DRAFT &&
3572 attr->values[0].integer <= IPP_QUALITY_HIGH)
3573 print_quality = attr->values[0].integer - IPP_QUALITY_DRAFT;
3574 else
030ae6a1 3575 print_quality = _PWG_PRINT_QUALITY_NORMAL;
c7017ecc 3576
f14324a7 3577 if (pc->num_presets[print_color_mode][print_quality] == 0)
c7017ecc
MS
3578 {
3579 /*
3580 * Try to find a preset that works so that we maximize the chances of us
3581 * getting a good print using IPP attributes.
3582 */
3583
f14324a7 3584 if (pc->num_presets[print_color_mode][_PWG_PRINT_QUALITY_NORMAL] > 0)
c7017ecc 3585 print_quality = _PWG_PRINT_QUALITY_NORMAL;
f14324a7
MS
3586 else if (pc->num_presets[_PWG_PRINT_COLOR_MODE_COLOR][print_quality] > 0)
3587 print_color_mode = _PWG_PRINT_COLOR_MODE_COLOR;
c7017ecc
MS
3588 else
3589 {
f14324a7
MS
3590 print_quality = _PWG_PRINT_QUALITY_NORMAL;
3591 print_color_mode = _PWG_PRINT_COLOR_MODE_COLOR;
c7017ecc
MS
3592 }
3593 }
3594
f14324a7 3595 if (pc->num_presets[print_color_mode][print_quality] > 0)
c7017ecc
MS
3596 {
3597 /*
3598 * Copy the preset options as long as the corresponding names are not
3599 * already defined in the IPP request...
3600 */
3601
f14324a7
MS
3602 for (i = pc->num_presets[print_color_mode][print_quality],
3603 preset = pc->presets[print_color_mode][print_quality];
c7017ecc
MS
3604 i > 0;
3605 i --, preset ++)
3606 {
3607 if (!ippFindAttribute(job->attrs, preset->name, IPP_TAG_ZERO))
3608 num_pwgppds = cupsAddOption(preset->name, preset->value, num_pwgppds,
3609 &pwgppds);
3610 }
3611 }
7cf5915e 3612 }
c7017ecc 3613
f14324a7 3614 if (pc)
7cf5915e 3615 {
4220952d
MS
3616 if (!ippFindAttribute(job->attrs, "InputSlot", IPP_TAG_ZERO) &&
3617 !ippFindAttribute(job->attrs, "HPPaperSource", IPP_TAG_ZERO))
3618 {
f14324a7
MS
3619 if ((ppd = _ppdCacheGetInputSlot(pc, job->attrs, NULL)) != NULL)
3620 num_pwgppds = cupsAddOption(pc->source_option, ppd, num_pwgppds,
4220952d 3621 &pwgppds);
4220952d 3622 }
4220952d 3623 if (!ippFindAttribute(job->attrs, "MediaType", IPP_TAG_ZERO) &&
f14324a7 3624 (ppd = _ppdCacheGetMediaType(pc, job->attrs, NULL)) != NULL)
4220952d 3625 num_pwgppds = cupsAddOption("MediaType", ppd, num_pwgppds, &pwgppds);
c7017ecc 3626
4220952d
MS
3627 if (!ippFindAttribute(job->attrs, "PageRegion", IPP_TAG_ZERO) &&
3628 !ippFindAttribute(job->attrs, "PageSize", IPP_TAG_ZERO) &&
f14324a7 3629 (ppd = _ppdCacheGetPageSize(pc, job->attrs, NULL, &exact)) != NULL)
030ae6a1 3630 {
4220952d 3631 num_pwgppds = cupsAddOption("PageSize", ppd, num_pwgppds, &pwgppds);
c7017ecc 3632
030ae6a1
MS
3633 if (!ippFindAttribute(job->attrs, "media", IPP_TAG_ZERO))
3634 num_pwgppds = cupsAddOption("media", ppd, num_pwgppds, &pwgppds);
3635 }
3636
4220952d
MS
3637 if (!ippFindAttribute(job->attrs, "OutputBin", IPP_TAG_ZERO) &&
3638 (attr = ippFindAttribute(job->attrs, "output-bin",
3639 IPP_TAG_ZERO)) != NULL &&
3640 (attr->value_tag == IPP_TAG_KEYWORD ||
3641 attr->value_tag == IPP_TAG_NAME) &&
f14324a7 3642 (ppd = _ppdCacheGetOutputBin(pc, attr->values[0].string.text)) != NULL)
0268488e
MS
3643 {
3644 /*
3645 * Map output-bin to OutputBin option...
3646 */
3647
4220952d 3648 num_pwgppds = cupsAddOption("OutputBin", ppd, num_pwgppds, &pwgppds);
0268488e
MS
3649 }
3650
f14324a7
MS
3651 if (pc->sides_option &&
3652 !ippFindAttribute(job->attrs, pc->sides_option, IPP_TAG_ZERO) &&
0268488e
MS
3653 (attr = ippFindAttribute(job->attrs, "sides", IPP_TAG_KEYWORD)) != NULL)
3654 {
3655 /*
3656 * Map sides to duplex option...
3657 */
3658
3659 if (!strcmp(attr->values[0].string.text, "one-sided"))
f14324a7 3660 num_pwgppds = cupsAddOption(pc->sides_option, pc->sides_1sided,
0268488e
MS
3661 num_pwgppds, &pwgppds);
3662 else if (!strcmp(attr->values[0].string.text, "two-sided-long-edge"))
f14324a7 3663 num_pwgppds = cupsAddOption(pc->sides_option, pc->sides_2sided_long,
0268488e
MS
3664 num_pwgppds, &pwgppds);
3665 else if (!strcmp(attr->values[0].string.text, "two-sided-short-edge"))
f14324a7 3666 num_pwgppds = cupsAddOption(pc->sides_option, pc->sides_2sided_short,
0268488e
MS
3667 num_pwgppds, &pwgppds);
3668 }
dcb445bc
MS
3669
3670 /*
3671 * Map finishings values...
3672 */
3673
3674 num_pwgppds = _ppdCacheGetFinishingOptions(pc, job->attrs,
3675 IPP_FINISHINGS_NONE, num_pwgppds,
3676 &pwgppds);
4220952d 3677 }
c7017ecc
MS
3678
3679 /*
3680 * Figure out how much room we need...
3681 */
3682
3683 newlength = ipp_length(job->attrs);
3684
3685 for (i = num_pwgppds, pwgppd = pwgppds; i > 0; i --, pwgppd ++)
3686 newlength += 1 + strlen(pwgppd->name) + 1 + strlen(pwgppd->value);
3687
3688 /*
3689 * Then allocate/reallocate the option buffer as needed...
3690 */
3691
85dda01c
MS
3692 if (newlength == 0) /* This can never happen, but Clang */
3693 newlength = 1; /* thinks it can... */
3694
c7017ecc 3695 if (newlength > optlength || !options)
e1d6a774 3696 {
b9faaae1 3697 if (!options)
c7017ecc 3698 optptr = malloc(newlength);
e1d6a774 3699 else
c7017ecc 3700 optptr = realloc(options, newlength);
ef416fc2 3701
b9faaae1 3702 if (!optptr)
e1d6a774 3703 {
75bd9771 3704 cupsdLogJob(job, CUPSD_LOG_CRIT,
12f89d24 3705 "Unable to allocate " CUPS_LLFMT " bytes for option buffer.",
c7017ecc 3706 CUPS_LLCAST newlength);
b9faaae1 3707 return (NULL);
e1d6a774 3708 }
ef416fc2 3709
e1d6a774 3710 options = optptr;
c7017ecc 3711 optlength = newlength;
e1d6a774 3712 }
ef416fc2 3713
e1d6a774 3714 /*
3715 * Now loop through the attributes and convert them to the textual
3716 * representation used by the filters...
3717 */
ef416fc2 3718
e1d6a774 3719 optptr = options;
3720 *optptr = '\0';
bd7854cb 3721
1340db2d
MS
3722 snprintf(title, title_size, "%s-%d", job->printer->name, job->id);
3723 strlcpy(copies, "1", copies_size);
bd7854cb 3724
e1d6a774 3725 for (attr = job->attrs->attrs; attr != NULL; attr = attr->next)
bd7854cb 3726 {
e1d6a774 3727 if (!strcmp(attr->name, "copies") &&
3728 attr->value_tag == IPP_TAG_INTEGER)
3729 {
3730 /*
3731 * Don't use the # copies attribute if we are printing the job sheets...
3732 */
bd7854cb 3733
e1d6a774 3734 if (!banner_page)
1340db2d 3735 snprintf(copies, copies_size, "%d", attr->values[0].integer);
e1d6a774 3736 }
3737 else if (!strcmp(attr->name, "job-name") &&
3738 (attr->value_tag == IPP_TAG_NAME ||
3739 attr->value_tag == IPP_TAG_NAMELANG))
1340db2d 3740 strlcpy(title, attr->values[0].string.text, title_size);
e1d6a774 3741 else if (attr->group_tag == IPP_TAG_JOB)
3742 {
3743 /*
3744 * Filter out other unwanted attributes...
3745 */
bd7854cb 3746
c7017ecc
MS
3747 if (attr->value_tag == IPP_TAG_NOVALUE ||
3748 attr->value_tag == IPP_TAG_MIMETYPE ||
e1d6a774 3749 attr->value_tag == IPP_TAG_NAMELANG ||
3750 attr->value_tag == IPP_TAG_TEXTLANG ||
db8b865d
MS
3751 (attr->value_tag == IPP_TAG_URI && strcmp(attr->name, "job-uuid") &&
3752 strcmp(attr->name, "job-authorization-uri")) ||
e1d6a774 3753 attr->value_tag == IPP_TAG_URISCHEME ||
3754 attr->value_tag == IPP_TAG_BEGIN_COLLECTION) /* Not yet supported */
3755 continue;
bd7854cb 3756
5a9febac
MS
3757 if (!strcmp(attr->name, "job-hold-until") ||
3758 !strcmp(attr->name, "job-id") ||
3759 !strcmp(attr->name, "job-k-octets") ||
3760 !strcmp(attr->name, "job-media-sheets") ||
3761 !strcmp(attr->name, "job-media-sheets-completed") ||
3762 !strcmp(attr->name, "job-state") ||
3763 !strcmp(attr->name, "job-state-reasons"))
e1d6a774 3764 continue;
bd7854cb 3765
e1d6a774 3766 if (!strncmp(attr->name, "job-", 4) &&
5a9febac
MS
3767 strcmp(attr->name, "job-account-id") &&
3768 strcmp(attr->name, "job-accounting-user-id") &&
a469f8a5 3769 strcmp(attr->name, "job-authorization-uri") &&
e4572d57 3770 strcmp(attr->name, "job-billing") &&
5d6412a9 3771 strcmp(attr->name, "job-impressions") &&
c5571a1d 3772 strcmp(attr->name, "job-originating-host-name") &&
5a9febac 3773 strcmp(attr->name, "job-password") &&
a469f8a5 3774 strcmp(attr->name, "job-password-encryption") &&
e4572d57 3775 strcmp(attr->name, "job-uuid") &&
b9faaae1 3776 !(job->printer->type & CUPS_PRINTER_REMOTE))
e1d6a774 3777 continue;
ef416fc2 3778
5d6412a9
MS
3779 if ((!strcmp(attr->name, "job-impressions") ||
3780 !strcmp(attr->name, "page-label") ||
e1d6a774 3781 !strcmp(attr->name, "page-border") ||
3782 !strncmp(attr->name, "number-up", 9) ||
b94498cf 3783 !strcmp(attr->name, "page-ranges") ||
e1d6a774 3784 !strcmp(attr->name, "page-set") ||
88f9aafc
MS
3785 !_cups_strcasecmp(attr->name, "AP_FIRSTPAGE_InputSlot") ||
3786 !_cups_strcasecmp(attr->name, "AP_FIRSTPAGE_ManualFeed") ||
3787 !_cups_strcasecmp(attr->name, "com.apple.print.PrintSettings."
db1f069b 3788 "PMTotalSidesImaged..n.") ||
88f9aafc 3789 !_cups_strcasecmp(attr->name, "com.apple.print.PrintSettings."
db1f069b 3790 "PMTotalBeginPages..n.")) &&
e1d6a774 3791 banner_page)
3792 continue;
ef416fc2 3793
e1d6a774 3794 /*
3795 * Otherwise add them to the list...
3796 */
ef416fc2 3797
e1d6a774 3798 if (optptr > options)
7e86f2f6 3799 strlcat(optptr, " ", optlength - (size_t)(optptr - options));
ef416fc2 3800
e1d6a774 3801 if (attr->value_tag != IPP_TAG_BOOLEAN)
3802 {
7e86f2f6
MS
3803 strlcat(optptr, attr->name, optlength - (size_t)(optptr - options));
3804 strlcat(optptr, "=", optlength - (size_t)(optptr - options));
e1d6a774 3805 }
ef416fc2 3806
e1d6a774 3807 for (i = 0; i < attr->num_values; i ++)
3808 {
3809 if (i)
7e86f2f6 3810 strlcat(optptr, ",", optlength - (size_t)(optptr - options));
ef416fc2 3811
e1d6a774 3812 optptr += strlen(optptr);
ef416fc2 3813
e1d6a774 3814 switch (attr->value_tag)
3815 {
3816 case IPP_TAG_INTEGER :
3817 case IPP_TAG_ENUM :
7e86f2f6 3818 snprintf(optptr, optlength - (size_t)(optptr - options),
e1d6a774 3819 "%d", attr->values[i].integer);
3820 break;
ef416fc2 3821
e1d6a774 3822 case IPP_TAG_BOOLEAN :
3823 if (!attr->values[i].boolean)
7e86f2f6 3824 strlcat(optptr, "no", optlength - (size_t)(optptr - options));
ef416fc2 3825
7e86f2f6 3826 strlcat(optptr, attr->name, optlength - (size_t)(optptr - options));
4220952d
MS
3827 break;
3828
e1d6a774 3829 case IPP_TAG_RANGE :
3830 if (attr->values[i].range.lower == attr->values[i].range.upper)
7e86f2f6 3831 snprintf(optptr, optlength - (size_t)(optptr - options) - 1,
e1d6a774 3832 "%d", attr->values[i].range.lower);
3833 else
7e86f2f6 3834 snprintf(optptr, optlength - (size_t)(optptr - options) - 1,
e1d6a774 3835 "%d-%d", attr->values[i].range.lower,
3836 attr->values[i].range.upper);
3837 break;
ef416fc2 3838
e1d6a774 3839 case IPP_TAG_RESOLUTION :
7e86f2f6 3840 snprintf(optptr, optlength - (size_t)(optptr - options) - 1,
e1d6a774 3841 "%dx%d%s", attr->values[i].resolution.xres,
3842 attr->values[i].resolution.yres,
3843 attr->values[i].resolution.units == IPP_RES_PER_INCH ?
3e7fe0ca 3844 "dpi" : "dpcm");
e1d6a774 3845 break;
ef416fc2 3846
e1d6a774 3847 case IPP_TAG_STRING :
3848 case IPP_TAG_TEXT :
3849 case IPP_TAG_NAME :
3850 case IPP_TAG_KEYWORD :
3851 case IPP_TAG_CHARSET :
3852 case IPP_TAG_LANGUAGE :
3853 case IPP_TAG_URI :
3854 for (valptr = attr->values[i].string.text; *valptr;)
3855 {
3856 if (strchr(" \t\n\\\'\"", *valptr))
3857 *optptr++ = '\\';
3858 *optptr++ = *valptr++;
3859 }
3860
3861 *optptr = '\0';
3862 break;
3863
3864 default :
3865 break; /* anti-compiler-warning-code */
3866 }
3867 }
3868
3869 optptr += strlen(optptr);
3870 }
3871 }
3872
c7017ecc
MS
3873 /*
3874 * Finally loop through the PWG->PPD mapped options and add them...
3875 */
3876
3877 for (i = num_pwgppds, pwgppd = pwgppds; i > 0; i --, pwgppd ++)
3878 {
3879 *optptr++ = ' ';
7e86f2f6 3880 strlcpy(optptr, pwgppd->name, optlength - (size_t)(optptr - options));
c7017ecc
MS
3881 optptr += strlen(optptr);
3882 *optptr++ = '=';
7e86f2f6 3883 strlcpy(optptr, pwgppd->value, optlength - (size_t)(optptr - options));
c7017ecc
MS
3884 optptr += strlen(optptr);
3885 }
3886
3887 cupsFreeOptions(num_pwgppds, pwgppds);
3888
3889 /*
3890 * Return the options string...
3891 */
b9faaae1
MS
3892
3893 return (options);
3894}
3895
3896
3897/*
3898 * 'ipp_length()' - Compute the size of the buffer needed to hold
3899 * the textual IPP attributes.
3900 */
3901
c7017ecc 3902static size_t /* O - Size of attribute buffer */
b9faaae1
MS
3903ipp_length(ipp_t *ipp) /* I - IPP request */
3904{
c7017ecc 3905 size_t bytes; /* Number of bytes */
b9faaae1
MS
3906 int i; /* Looping var */
3907 ipp_attribute_t *attr; /* Current attribute */
3908
3909
3910 /*
3911 * Loop through all attributes...
e1d6a774 3912 */
3913
b9faaae1 3914 bytes = 0;
e1d6a774 3915
b9faaae1 3916 for (attr = ipp->attrs; attr != NULL; attr = attr->next)
91c84a35 3917 {
b9faaae1
MS
3918 /*
3919 * Skip attributes that won't be sent to filters...
3920 */
91c84a35 3921
c7017ecc
MS
3922 if (attr->value_tag == IPP_TAG_NOVALUE ||
3923 attr->value_tag == IPP_TAG_MIMETYPE ||
b9faaae1
MS
3924 attr->value_tag == IPP_TAG_NAMELANG ||
3925 attr->value_tag == IPP_TAG_TEXTLANG ||
3926 attr->value_tag == IPP_TAG_URI ||
3927 attr->value_tag == IPP_TAG_URISCHEME)
3928 continue;
91c84a35 3929
b9faaae1
MS
3930 /*
3931 * Add space for a leading space and commas between each value.
3932 * For the first attribute, the leading space isn't used, so the
3933 * extra byte can be used as the nul terminator...
3934 */
e1d6a774 3935
b9faaae1 3936 bytes ++; /* " " separator */
7e86f2f6 3937 bytes += (size_t)attr->num_values; /* "," separators */
e1d6a774 3938
b9faaae1
MS
3939 /*
3940 * Boolean attributes appear as "foo,nofoo,foo,nofoo", while
3941 * other attributes appear as "foo=value1,value2,...,valueN".
3942 */
3943
3944 if (attr->value_tag != IPP_TAG_BOOLEAN)
3945 bytes += strlen(attr->name);
3946 else
7e86f2f6 3947 bytes += (size_t)attr->num_values * strlen(attr->name);
b9faaae1
MS
3948
3949 /*
3950 * Now add the size required for each value in the attribute...
3951 */
3952
3953 switch (attr->value_tag)
ef416fc2 3954 {
b9faaae1
MS
3955 case IPP_TAG_INTEGER :
3956 case IPP_TAG_ENUM :
3957 /*
3958 * Minimum value of a signed integer is -2147483647, or 11 digits.
3959 */
3960
7e86f2f6 3961 bytes += (size_t)attr->num_values * 11;
b9faaae1
MS
3962 break;
3963
3964 case IPP_TAG_BOOLEAN :
3965 /*
3966 * Add two bytes for each false ("no") value...
3967 */
3968
3969 for (i = 0; i < attr->num_values; i ++)
3970 if (!attr->values[i].boolean)
3971 bytes += 2;
3972 break;
3973
3974 case IPP_TAG_RANGE :
3975 /*
3976 * A range is two signed integers separated by a hyphen, or
3977 * 23 characters max.
3978 */
3979
7e86f2f6 3980 bytes += (size_t)attr->num_values * 23;
b9faaae1
MS
3981 break;
3982
3983 case IPP_TAG_RESOLUTION :
3984 /*
3985 * A resolution is two signed integers separated by an "x" and
3986 * suffixed by the units, or 26 characters max.
3987 */
3988
7e86f2f6 3989 bytes += (size_t)attr->num_values * 26;
b9faaae1
MS
3990 break;
3991
3992 case IPP_TAG_STRING :
3993 case IPP_TAG_TEXT :
3994 case IPP_TAG_NAME :
3995 case IPP_TAG_KEYWORD :
3996 case IPP_TAG_CHARSET :
3997 case IPP_TAG_LANGUAGE :
3998 case IPP_TAG_URI :
3999 /*
4000 * Strings can contain characters that need quoting. We need
4001 * at least 2 * len + 2 characters to cover the quotes and
4002 * any backslashes in the string.
4003 */
4004
4005 for (i = 0; i < attr->num_values; i ++)
4006 bytes += 2 * strlen(attr->values[i].string.text) + 2;
4007 break;
4008
4009 default :
4010 break; /* anti-compiler-warning-code */
e1d6a774 4011 }
4012 }
b9faaae1
MS
4013
4014 return (bytes);
4015}
4016
4017
4018/*
4019 * 'load_job_cache()' - Load jobs from the job.cache file.
4020 */
4021
4022static void
4023load_job_cache(const char *filename) /* I - job.cache filename */
4024{
4025 cups_file_t *fp; /* job.cache file */
4026 char line[1024], /* Line buffer */
4027 *value; /* Value on line */
4028 int linenum; /* Line number in file */
4029 cupsd_job_t *job; /* Current job */
4030 int jobid; /* Job ID */
4031 char jobfile[1024]; /* Job filename */
4032
4033
4034 /*
4035 * Open the job.cache file...
4036 */
4037
321d8d57 4038 if ((fp = cupsdOpenConfFile(filename)) == NULL)
e1d6a774 4039 {
b9faaae1 4040 load_request_root();
b9faaae1
MS
4041 return;
4042 }
ef416fc2 4043
e1d6a774 4044 /*
b9faaae1 4045 * Read entries from the job cache file and create jobs as needed.
e1d6a774 4046 */
ef416fc2 4047
b9faaae1
MS
4048 cupsdLogMessage(CUPSD_LOG_INFO, "Loading job cache file \"%s\"...",
4049 filename);
ef416fc2 4050
b9faaae1
MS
4051 linenum = 0;
4052 job = NULL;
4053
4054 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
4055 {
88f9aafc 4056 if (!_cups_strcasecmp(line, "NextJobId"))
b9faaae1
MS
4057 {
4058 if (value)
4059 NextJobId = atoi(value);
4060 }
88f9aafc 4061 else if (!_cups_strcasecmp(line, "<Job"))
b9faaae1
MS
4062 {
4063 if (job)
4064 {
12f89d24 4065 cupsdLogMessage(CUPSD_LOG_ERROR, "Missing </Job> directive on line %d.",
b9faaae1
MS
4066 linenum);
4067 continue;
4068 }
4069
4070 if (!value)
4071 {
12f89d24 4072 cupsdLogMessage(CUPSD_LOG_ERROR, "Missing job ID on line %d.", linenum);
b9faaae1
MS
4073 continue;
4074 }
4075
4076 jobid = atoi(value);
4077
4078 if (jobid < 1)
4079 {
12f89d24 4080 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad job ID %d on line %d.", jobid,
b9faaae1
MS
4081 linenum);
4082 continue;
4083 }
4084
4085 snprintf(jobfile, sizeof(jobfile), "%s/c%05d", RequestRoot, jobid);
4086 if (access(jobfile, 0))
4087 {
321d8d57
MS
4088 snprintf(jobfile, sizeof(jobfile), "%s/c%05d.N", RequestRoot, jobid);
4089 if (access(jobfile, 0))
4090 {
12f89d24 4091 cupsdLogMessage(CUPSD_LOG_ERROR, "[Job %d] Files have gone away.",
321d8d57
MS
4092 jobid);
4093 continue;
4094 }
b9faaae1
MS
4095 }
4096
4097 job = calloc(1, sizeof(cupsd_job_t));
4098 if (!job)
4099 {
4100 cupsdLogMessage(CUPSD_LOG_EMERG,
12f89d24 4101 "[Job %d] Unable to allocate memory for job.", jobid);
b9faaae1
MS
4102 break;
4103 }
4104
4105 job->id = jobid;
4106 job->back_pipes[0] = -1;
4107 job->back_pipes[1] = -1;
4108 job->print_pipes[0] = -1;
4109 job->print_pipes[1] = -1;
4110 job->side_pipes[0] = -1;
4111 job->side_pipes[1] = -1;
4112 job->status_pipes[0] = -1;
4113 job->status_pipes[1] = -1;
4114
72f50170 4115 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Loading from cache...");
b9faaae1
MS
4116 }
4117 else if (!job)
4118 {
4119 cupsdLogMessage(CUPSD_LOG_ERROR,
12f89d24 4120 "Missing <Job #> directive on line %d.", linenum);
b9faaae1
MS
4121 continue;
4122 }
88f9aafc 4123 else if (!_cups_strcasecmp(line, "</Job>"))
b9faaae1
MS
4124 {
4125 cupsArrayAdd(Jobs, job);
4126
f701418f
MS
4127 if (job->state_value <= IPP_JOB_STOPPED && cupsdLoadJob(job))
4128 cupsArrayAdd(ActiveJobs, job);
eec1fbc3
MS
4129 else if (job->state_value > IPP_JOB_STOPPED)
4130 {
f16ea703 4131 if (!job->completed_time || !job->creation_time || !job->name || !job->koctets)
eec1fbc3
MS
4132 {
4133 cupsdLoadJob(job);
4134 unload_job(job);
4135 }
4136 }
b9faaae1
MS
4137
4138 job = NULL;
4139 }
4140 else if (!value)
4141 {
12f89d24 4142 cupsdLogMessage(CUPSD_LOG_ERROR, "Missing value on line %d.", linenum);
b9faaae1
MS
4143 continue;
4144 }
88f9aafc 4145 else if (!_cups_strcasecmp(line, "State"))
b9faaae1
MS
4146 {
4147 job->state_value = (ipp_jstate_t)atoi(value);
4148
4149 if (job->state_value < IPP_JOB_PENDING)
4150 job->state_value = IPP_JOB_PENDING;
4151 else if (job->state_value > IPP_JOB_COMPLETED)
4152 job->state_value = IPP_JOB_COMPLETED;
4153 }
f16ea703
MS
4154 else if (!_cups_strcasecmp(line, "Name"))
4155 {
4156 cupsdSetString(&(job->name), value);
4157 }
4158 else if (!_cups_strcasecmp(line, "Created"))
4159 {
4160 job->creation_time = strtol(value, NULL, 10);
4161 }
eec1fbc3
MS
4162 else if (!_cups_strcasecmp(line, "Completed"))
4163 {
4164 job->completed_time = strtol(value, NULL, 10);
4165 }
88f9aafc 4166 else if (!_cups_strcasecmp(line, "HoldUntil"))
8b116e60 4167 {
eec1fbc3 4168 job->hold_until = strtol(value, NULL, 10);
8b116e60 4169 }
88f9aafc 4170 else if (!_cups_strcasecmp(line, "Priority"))
b9faaae1
MS
4171 {
4172 job->priority = atoi(value);
4173 }
88f9aafc 4174 else if (!_cups_strcasecmp(line, "Username"))
b9faaae1
MS
4175 {
4176 cupsdSetString(&job->username, value);
4177 }
88f9aafc 4178 else if (!_cups_strcasecmp(line, "Destination"))
b9faaae1
MS
4179 {
4180 cupsdSetString(&job->dest, value);
4181 }
88f9aafc 4182 else if (!_cups_strcasecmp(line, "DestType"))
b9faaae1
MS
4183 {
4184 job->dtype = (cups_ptype_t)atoi(value);
4185 }
f16ea703
MS
4186 else if (!_cups_strcasecmp(line, "KOctets"))
4187 {
4188 job->koctets = atoi(value);
4189 }
88f9aafc 4190 else if (!_cups_strcasecmp(line, "NumFiles"))
b9faaae1
MS
4191 {
4192 job->num_files = atoi(value);
4193
4194 if (job->num_files < 0)
4195 {
12f89d24 4196 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad NumFiles value %d on line %d.",
b9faaae1
MS
4197 job->num_files, linenum);
4198 job->num_files = 0;
4199 continue;
4200 }
4201
4202 if (job->num_files > 0)
4203 {
4204 snprintf(jobfile, sizeof(jobfile), "%s/d%05d-001", RequestRoot,
4205 job->id);
4206 if (access(jobfile, 0))
4207 {
72f50170 4208 cupsdLogJob(job, CUPSD_LOG_INFO, "Data files have gone away.");
b9faaae1
MS
4209 job->num_files = 0;
4210 continue;
4211 }
4212
7e86f2f6
MS
4213 job->filetypes = calloc((size_t)job->num_files, sizeof(mime_type_t *));
4214 job->compressions = calloc((size_t)job->num_files, sizeof(int));
b9faaae1
MS
4215
4216 if (!job->filetypes || !job->compressions)
4217 {
72f50170
MS
4218 cupsdLogJob(job, CUPSD_LOG_EMERG,
4219 "Unable to allocate memory for %d files.",
4220 job->num_files);
b9faaae1
MS
4221 break;
4222 }
4223 }
4224 }
88f9aafc 4225 else if (!_cups_strcasecmp(line, "File"))
b9faaae1
MS
4226 {
4227 int number, /* File number */
4228 compression; /* Compression value */
4229 char super[MIME_MAX_SUPER], /* MIME super type */
4230 type[MIME_MAX_TYPE]; /* MIME type */
4231
4232
4233 if (sscanf(value, "%d%*[ \t]%15[^/]/%255s%d", &number, super, type,
4234 &compression) != 4)
4235 {
12f89d24 4236 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad File on line %d.", linenum);
b9faaae1
MS
4237 continue;
4238 }
4239
4240 if (number < 1 || number > job->num_files)
4241 {
12f89d24 4242 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad File number %d on line %d.",
b9faaae1
MS
4243 number, linenum);
4244 continue;
4245 }
0a682745 4246
b9faaae1 4247 number --;
ef416fc2 4248
b9faaae1
MS
4249 job->compressions[number] = compression;
4250 job->filetypes[number] = mimeType(MimeDatabase, super, type);
ef416fc2 4251
b9faaae1
MS
4252 if (!job->filetypes[number])
4253 {
e1d6a774 4254 /*
b9faaae1 4255 * If the original MIME type is unknown, auto-type it!
e1d6a774 4256 */
ef416fc2 4257
72f50170
MS
4258 cupsdLogJob(job, CUPSD_LOG_ERROR,
4259 "Unknown MIME type %s/%s for file %d.",
4260 super, type, number + 1);
b9faaae1
MS
4261
4262 snprintf(jobfile, sizeof(jobfile), "%s/d%05d-%03d", RequestRoot,
4263 job->id, number + 1);
4264 job->filetypes[number] = mimeFileType(MimeDatabase, jobfile, NULL,
4265 job->compressions + number);
ef416fc2 4266
e1d6a774 4267 /*
b9faaae1 4268 * If that didn't work, assume it is raw...
e1d6a774 4269 */
ef416fc2 4270
b9faaae1
MS
4271 if (!job->filetypes[number])
4272 job->filetypes[number] = mimeType(MimeDatabase, "application",
4273 "vnd.cups-raw");
4274 }
4275 }
4276 else
12f89d24 4277 cupsdLogMessage(CUPSD_LOG_ERROR, "Unknown %s directive on line %d.",
b9faaae1 4278 line, linenum);
ef416fc2 4279 }
4280
cb7f98ee
MS
4281 if (job)
4282 {
4283 cupsdLogMessage(CUPSD_LOG_ERROR,
4284 "Missing </Job> directive on line %d.", linenum);
4285 cupsdDeleteJob(job, CUPSD_JOB_PURGE);
4286 }
4287
b9faaae1
MS
4288 cupsFileClose(fp);
4289}
ef416fc2 4290
4291
b9faaae1
MS
4292/*
4293 * 'load_next_job_id()' - Load the NextJobId value from the job.cache file.
4294 */
bd7854cb 4295
b9faaae1
MS
4296static void
4297load_next_job_id(const char *filename) /* I - job.cache filename */
4298{
4299 cups_file_t *fp; /* job.cache file */
4300 char line[1024], /* Line buffer */
4301 *value; /* Value on line */
4302 int linenum; /* Line number in file */
4303 int next_job_id; /* NextJobId value from line */
db0bd74a 4304
db0bd74a 4305
b9faaae1
MS
4306 /*
4307 * Read the NextJobId directive from the job.cache file and use
4308 * the value (if any).
4309 */
bd7854cb 4310
b9faaae1 4311 if ((fp = cupsFileOpen(filename, "r")) == NULL)
e1d6a774 4312 {
b9faaae1
MS
4313 if (errno != ENOENT)
4314 cupsdLogMessage(CUPSD_LOG_ERROR,
4315 "Unable to open job cache file \"%s\": %s",
4316 filename, strerror(errno));
bd7854cb 4317
b9faaae1 4318 return;
e1d6a774 4319 }
bd7854cb 4320
b9faaae1
MS
4321 cupsdLogMessage(CUPSD_LOG_INFO,
4322 "Loading NextJobId from job cache file \"%s\"...", filename);
4323
4324 linenum = 0;
4325
4326 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
bd7854cb 4327 {
88f9aafc 4328 if (!_cups_strcasecmp(line, "NextJobId"))
b9faaae1
MS
4329 {
4330 if (value)
4331 {
4332 next_job_id = atoi(value);
4333
4334 if (next_job_id > NextJobId)
4335 NextJobId = next_job_id;
4336 }
4337 break;
4338 }
e1d6a774 4339 }
bd7854cb 4340
b9faaae1
MS
4341 cupsFileClose(fp);
4342}
09a101d6 4343
f7deaa1a 4344
b9faaae1
MS
4345/*
4346 * 'load_request_root()' - Load jobs from the RequestRoot directory.
4347 */
bd7854cb 4348
b9faaae1
MS
4349static void
4350load_request_root(void)
4351{
4352 cups_dir_t *dir; /* Directory */
4353 cups_dentry_t *dent; /* Directory entry */
4354 cupsd_job_t *job; /* New job */
e1d6a774 4355
bd7854cb 4356
4357 /*
b9faaae1 4358 * Open the requests directory...
bd7854cb 4359 */
4360
b9faaae1 4361 cupsdLogMessage(CUPSD_LOG_DEBUG, "Scanning %s for jobs...", RequestRoot);
bd7854cb 4362
b9faaae1 4363 if ((dir = cupsDirOpen(RequestRoot)) == NULL)
bd7854cb 4364 {
b9faaae1
MS
4365 cupsdLogMessage(CUPSD_LOG_ERROR,
4366 "Unable to open spool directory \"%s\": %s",
4367 RequestRoot, strerror(errno));
4368 return;
e1d6a774 4369 }
bd7854cb 4370
b9faaae1
MS
4371 /*
4372 * Read all the c##### files...
4373 */
bd7854cb 4374
b9faaae1
MS
4375 while ((dent = cupsDirRead(dir)) != NULL)
4376 if (strlen(dent->filename) >= 6 && dent->filename[0] == 'c')
bd7854cb 4377 {
b9faaae1
MS
4378 /*
4379 * Allocate memory for the job...
4380 */
bd7854cb 4381
b9faaae1 4382 if ((job = calloc(sizeof(cupsd_job_t), 1)) == NULL)
bd7854cb 4383 {
12f89d24 4384 cupsdLogMessage(CUPSD_LOG_ERROR, "Ran out of memory for jobs.");
b9faaae1
MS
4385 cupsDirClose(dir);
4386 return;
4387 }
bd7854cb 4388
b9faaae1
MS
4389 /*
4390 * Assign the job ID...
4391 */
bd7854cb 4392
b9faaae1
MS
4393 job->id = atoi(dent->filename + 1);
4394 job->back_pipes[0] = -1;
4395 job->back_pipes[1] = -1;
4396 job->print_pipes[0] = -1;
4397 job->print_pipes[1] = -1;
4398 job->side_pipes[0] = -1;
4399 job->side_pipes[1] = -1;
4400 job->status_pipes[0] = -1;
4401 job->status_pipes[1] = -1;
bd7854cb 4402
b9faaae1
MS
4403 if (job->id >= NextJobId)
4404 NextJobId = job->id + 1;
ed486911 4405
b9faaae1
MS
4406 /*
4407 * Load the job...
4408 */
ed486911 4409
f701418f
MS
4410 if (cupsdLoadJob(job))
4411 {
4412 /*
4413 * Insert the job into the array, sorting by job priority and ID...
4414 */
bd7854cb 4415
f701418f 4416 cupsArrayAdd(Jobs, job);
e1d6a774 4417
f701418f
MS
4418 if (job->state_value <= IPP_JOB_STOPPED)
4419 cupsArrayAdd(ActiveJobs, job);
4420 else
4421 unload_job(job);
4422 }
0fa6c7fa
MS
4423 else
4424 free(job);
bd7854cb 4425 }
bd7854cb 4426
b9faaae1
MS
4427 cupsDirClose(dir);
4428}
bd7854cb 4429
4430
82cc1f9a
MS
4431/*
4432 * 'remove_job_files()' - Remove the document files for a job.
4433 */
4434
4435static void
4436remove_job_files(cupsd_job_t *job) /* I - Job */
4437{
4438 int i; /* Looping var */
4439 char filename[1024]; /* Document filename */
4440
4441
4442 if (job->num_files <= 0)
4443 return;
4444
4445 for (i = 1; i <= job->num_files; i ++)
4446 {
4447 snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot,
4448 job->id, i);
cb7f98ee 4449 cupsdUnlinkOrRemoveFile(filename);
82cc1f9a
MS
4450 }
4451
4452 free(job->filetypes);
4453 free(job->compressions);
4454
4455 job->file_time = 0;
4456 job->num_files = 0;
4457 job->filetypes = NULL;
4458 job->compressions = NULL;
4459
4460 LastEvent |= CUPSD_EVENT_PRINTER_STATE_CHANGED;
4461}
4462
4463
4464/*
4465 * 'remove_job_history()' - Remove the control file for a job.
4466 */
4467
4468static void
4469remove_job_history(cupsd_job_t *job) /* I - Job */
4470{
4471 char filename[1024]; /* Control filename */
4472
4473
4474 /*
4475 * Remove the job info file...
4476 */
4477
4478 snprintf(filename, sizeof(filename), "%s/c%05d", RequestRoot,
4479 job->id);
cb7f98ee 4480 cupsdUnlinkOrRemoveFile(filename);
82cc1f9a
MS
4481
4482 LastEvent |= CUPSD_EVENT_PRINTER_STATE_CHANGED;
4483}
4484
4485
b9faaae1
MS
4486/*
4487 * 'set_time()' - Set one of the "time-at-xyz" attributes.
4488 */
bd7854cb 4489
b9faaae1
MS
4490static void
4491set_time(cupsd_job_t *job, /* I - Job to update */
4492 const char *name) /* I - Name of attribute */
4493{
4494 ipp_attribute_t *attr; /* Time attribute */
82cc1f9a
MS
4495 time_t curtime; /* Current time */
4496
bd7854cb 4497
82cc1f9a
MS
4498 curtime = time(NULL);
4499
4500 cupsdLogJob(job, CUPSD_LOG_DEBUG, "%s=%ld", name, (long)curtime);
bd7854cb 4501
b9faaae1
MS
4502 if ((attr = ippFindAttribute(job->attrs, name, IPP_TAG_ZERO)) != NULL)
4503 {
4504 attr->value_tag = IPP_TAG_INTEGER;
82cc1f9a
MS
4505 attr->values[0].integer = curtime;
4506 }
4507
4508 if (!strcmp(name, "time-at-completed"))
4509 {
eec1fbc3
MS
4510 job->completed_time = curtime;
4511
0fa6c7fa 4512 if (JobHistory < INT_MAX && attr)
82cc1f9a
MS
4513 job->history_time = attr->values[0].integer + JobHistory;
4514 else
4515 job->history_time = INT_MAX;
4516
4517 if (job->history_time < JobHistoryUpdate || !JobHistoryUpdate)
4518 JobHistoryUpdate = job->history_time;
4519
0fa6c7fa 4520 if (JobFiles < INT_MAX && attr)
82cc1f9a
MS
4521 job->file_time = attr->values[0].integer + JobFiles;
4522 else
4523 job->file_time = INT_MAX;
4524
4525 if (job->file_time < JobHistoryUpdate || !JobHistoryUpdate)
4526 JobHistoryUpdate = job->file_time;
4527
4528 cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_time: JobHistoryUpdate=%ld",
4529 (long)JobHistoryUpdate);
b9faaae1
MS
4530 }
4531}
bd7854cb 4532
e1d6a774 4533
b9faaae1
MS
4534/*
4535 * 'start_job()' - Start a print job.
4536 */
e1d6a774 4537
b9faaae1
MS
4538static void
4539start_job(cupsd_job_t *job, /* I - Job ID */
4540 cupsd_printer_t *printer) /* I - Printer to print job */
4541{
cb7f98ee 4542 const char *filename; /* Support filename */
982069db
MS
4543 ipp_attribute_t *cancel_after = ippFindAttribute(job->attrs,
4544 "job-cancel-after",
4545 IPP_TAG_INTEGER);
4546 /* job-cancel-after attribute */
cb7f98ee
MS
4547
4548
b9faaae1
MS
4549 cupsdLogMessage(CUPSD_LOG_DEBUG2, "start_job(job=%p(%d), printer=%p(%s))",
4550 job, job->id, printer, printer->name);
bd7854cb 4551
b9faaae1
MS
4552 /*
4553 * Make sure we have some files around before we try to print...
4554 */
bd7854cb 4555
b9faaae1
MS
4556 if (job->num_files == 0)
4557 {
12f89d24 4558 ippSetString(job->attrs, &job->reasons, 0, "aborted-by-system");
b9faaae1
MS
4559 cupsdSetJobState(job, IPP_JOB_ABORTED, CUPSD_JOB_DEFAULT,
4560 "Aborting job because it has no files.");
4561 return;
4562 }
bd7854cb 4563
b9faaae1
MS
4564 /*
4565 * Update the printer and job state to "processing"...
4566 */
bd7854cb 4567
b9faaae1
MS
4568 if (!cupsdLoadJob(job))
4569 return;
89d46774 4570
3e7fe0ca
MS
4571 if (!job->printer_message)
4572 job->printer_message = ippFindAttribute(job->attrs,
4573 "job-printer-state-message",
4574 IPP_TAG_TEXT);
88f9aafc
MS
4575 if (job->printer_message)
4576 cupsdSetString(&(job->printer_message->values[0].string.text), "");
4577
12f89d24 4578 ippSetString(job->attrs, &job->reasons, 0, "job-printing");
b9faaae1
MS
4579 cupsdSetJobState(job, IPP_JOB_PROCESSING, CUPSD_JOB_DEFAULT, NULL);
4580 cupsdSetPrinterState(printer, IPP_PRINTER_PROCESSING, 0);
07ed0e9a
MS
4581 cupsdSetPrinterReasons(printer, "-cups-remote-pending,"
4582 "cups-remote-pending-held,"
4583 "cups-remote-processing,"
4584 "cups-remote-stopped,"
4585 "cups-remote-canceled,"
4586 "cups-remote-aborted,"
4587 "cups-remote-completed");
f7deaa1a 4588
1340db2d
MS
4589 job->cost = 0;
4590 job->current_file = 0;
82cc1f9a
MS
4591 job->file_time = 0;
4592 job->history_time = 0;
1340db2d
MS
4593 job->progress = 0;
4594 job->printer = printer;
4595 printer->job = job;
f7deaa1a 4596
982069db
MS
4597 if (cancel_after)
4598 job->cancel_time = time(NULL) + ippGetInteger(cancel_after, 0);
4599 else if (MaxJobTime > 0)
dcb445bc
MS
4600 job->cancel_time = time(NULL) + MaxJobTime;
4601 else
4602 job->cancel_time = 0;
4603
cb7f98ee
MS
4604 /*
4605 * Check for support files...
4606 */
4607
4608 cupsdSetPrinterReasons(job->printer, "-cups-missing-filter-warning,"
4609 "cups-insecure-filter-warning");
4610
4611 if (printer->pc)
4612 {
4613 for (filename = (const char *)cupsArrayFirst(printer->pc->support_files);
4614 filename;
4615 filename = (const char *)cupsArrayNext(printer->pc->support_files))
4616 {
4617 if (_cupsFileCheck(filename, _CUPS_FILE_CHECK_FILE, !RunUser,
4618 cupsdLogFCMessage, printer))
4619 break;
4620 }
4621 }
4622
b9faaae1
MS
4623 /*
4624 * Setup the last exit status and security profiles...
4625 */
89d46774 4626
8fe0183a
MS
4627 job->status = 0;
4628 job->profile = cupsdCreateProfile(job->id, 0);
4629 job->bprofile = cupsdCreateProfile(job->id, 1);
bd7854cb 4630
b9faaae1
MS
4631 /*
4632 * Create the status pipes and buffer...
4633 */
bd7854cb 4634
b9faaae1
MS
4635 if (cupsdOpenPipe(job->status_pipes))
4636 {
4637 cupsdLogJob(job, CUPSD_LOG_DEBUG,
4638 "Unable to create job status pipes - %s.", strerror(errno));
89d46774 4639
b9faaae1
MS
4640 cupsdSetJobState(job, IPP_JOB_STOPPED, CUPSD_JOB_DEFAULT,
4641 "Job stopped because the scheduler could not create the "
4642 "job status pipes.");
89d46774 4643
b9faaae1
MS
4644 cupsdDestroyProfile(job->profile);
4645 job->profile = NULL;
8fe0183a
MS
4646 cupsdDestroyProfile(job->bprofile);
4647 job->bprofile = NULL;
b9faaae1 4648 return;
e1d6a774 4649 }
bd7854cb 4650
b9faaae1
MS
4651 job->status_buffer = cupsdStatBufNew(job->status_pipes[0], NULL);
4652 job->status_level = CUPSD_LOG_INFO;
4653
4654 /*
4655 * Create the backchannel pipes and make them non-blocking...
4656 */
bd7854cb 4657
b9faaae1 4658 if (cupsdOpenPipe(job->back_pipes))
e1d6a774 4659 {
b9faaae1
MS
4660 cupsdLogJob(job, CUPSD_LOG_DEBUG,
4661 "Unable to create back-channel pipes - %s.", strerror(errno));
4662
4663 cupsdSetJobState(job, IPP_JOB_STOPPED, CUPSD_JOB_DEFAULT,
4664 "Job stopped because the scheduler could not create the "
4665 "back-channel pipes.");
4666
4667 cupsdClosePipe(job->status_pipes);
4668 cupsdStatBufDelete(job->status_buffer);
4669 job->status_buffer = NULL;
4670
4671 cupsdDestroyProfile(job->profile);
4672 job->profile = NULL;
8fe0183a
MS
4673 cupsdDestroyProfile(job->bprofile);
4674 job->bprofile = NULL;
b9faaae1 4675 return;
e1d6a774 4676 }
bd7854cb 4677
b9faaae1
MS
4678 fcntl(job->back_pipes[0], F_SETFL,
4679 fcntl(job->back_pipes[0], F_GETFL) | O_NONBLOCK);
4680 fcntl(job->back_pipes[1], F_SETFL,
4681 fcntl(job->back_pipes[1], F_GETFL) | O_NONBLOCK);
ef416fc2 4682
b9faaae1
MS
4683 /*
4684 * Create the side-channel pipes and make them non-blocking...
4685 */
ef416fc2 4686
b9faaae1
MS
4687 if (socketpair(AF_LOCAL, SOCK_STREAM, 0, job->side_pipes))
4688 {
4689 cupsdLogJob(job, CUPSD_LOG_DEBUG,
4690 "Unable to create side-channel pipes - %s.", strerror(errno));
ef416fc2 4691
b9faaae1
MS
4692 cupsdSetJobState(job, IPP_JOB_STOPPED, CUPSD_JOB_DEFAULT,
4693 "Job stopped because the scheduler could not create the "
4694 "side-channel pipes.");
ef416fc2 4695
b9faaae1
MS
4696 cupsdClosePipe(job->back_pipes);
4697
4698 cupsdClosePipe(job->status_pipes);
4699 cupsdStatBufDelete(job->status_buffer);
4700 job->status_buffer = NULL;
4701
4702 cupsdDestroyProfile(job->profile);
4703 job->profile = NULL;
8fe0183a
MS
4704 cupsdDestroyProfile(job->bprofile);
4705 job->bprofile = NULL;
b9faaae1
MS
4706 return;
4707 }
4708
4709 fcntl(job->side_pipes[0], F_SETFL,
4710 fcntl(job->side_pipes[0], F_GETFL) | O_NONBLOCK);
4711 fcntl(job->side_pipes[1], F_SETFL,
4712 fcntl(job->side_pipes[1], F_GETFL) | O_NONBLOCK);
ef416fc2 4713
7a0cbd5e
MS
4714 fcntl(job->side_pipes[0], F_SETFD,
4715 fcntl(job->side_pipes[0], F_GETFD) | FD_CLOEXEC);
4716 fcntl(job->side_pipes[1], F_SETFD,
4717 fcntl(job->side_pipes[1], F_GETFD) | FD_CLOEXEC);
4718
ef416fc2 4719 /*
b9faaae1 4720 * Now start the first file in the job...
ef416fc2 4721 */
4722
b9faaae1
MS
4723 cupsdContinueJob(job);
4724}
ef416fc2 4725
ef416fc2 4726
b9faaae1
MS
4727/*
4728 * 'stop_job()' - Stop a print job.
4729 */
ef416fc2 4730
b9faaae1
MS
4731static void
4732stop_job(cupsd_job_t *job, /* I - Job */
4733 cupsd_jobaction_t action) /* I - Action */
4734{
4735 int i; /* Looping var */
b94498cf 4736
ef416fc2 4737
b9faaae1
MS
4738 cupsdLogMessage(CUPSD_LOG_DEBUG2, "stop_job(job=%p(%d), action=%d)", job,
4739 job->id, action);
ef416fc2 4740
b9faaae1
MS
4741 FilterLevel -= job->cost;
4742 job->cost = 0;
ef416fc2 4743
238c3832
MS
4744 if (action == CUPSD_JOB_DEFAULT && !job->kill_time)
4745 job->kill_time = time(NULL) + JobKillDelay;
ef55b745 4746 else if (action >= CUPSD_JOB_FORCE)
238c3832
MS
4747 job->kill_time = 0;
4748
b9faaae1
MS
4749 for (i = 0; job->filters[i]; i ++)
4750 if (job->filters[i] > 0)
ef55b745
MS
4751 {
4752 cupsdEndProcess(job->filters[i], action >= CUPSD_JOB_FORCE);
4753
4754 if (action >= CUPSD_JOB_FORCE)
4755 job->filters[i] = -job->filters[i];
4756 }
b9faaae1
MS
4757
4758 if (job->backend > 0)
ef55b745
MS
4759 {
4760 cupsdEndProcess(job->backend, action >= CUPSD_JOB_FORCE);
4761
4762 if (action >= CUPSD_JOB_FORCE)
4763 job->backend = -job->backend;
4764 }
4765
4766 if (action >= CUPSD_JOB_FORCE)
4767 {
4768 /*
4769 * Clear job status...
4770 */
4771
4772 job->status = 0;
4773 }
e1d6a774 4774}
ef416fc2 4775
e1d6a774 4776
4777/*
4778 * 'unload_job()' - Unload a job from memory.
4779 */
4780
4781static void
4782unload_job(cupsd_job_t *job) /* I - Job */
4783{
4784 if (!job->attrs)
4785 return;
4786
72f50170 4787 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Unloading...");
e1d6a774 4788
4789 ippDelete(job->attrs);
4790
cc0d019f
MS
4791 job->attrs = NULL;
4792 job->state = NULL;
12f89d24 4793 job->reasons = NULL;
cc0d019f
MS
4794 job->sheets = NULL;
4795 job->job_sheets = NULL;
4796 job->printer_message = NULL;
4797 job->printer_reasons = NULL;
ef416fc2 4798}
4799
4800
4801/*
f899b121 4802 * 'update_job()' - Read a status update from a job's filters.
4803 */
4804
4805void
09a101d6 4806update_job(cupsd_job_t *job) /* I - Job to check */
f899b121 4807{
4808 int i; /* Looping var */
4809 int copies; /* Number of copies printed */
178cb736
MS
4810 char message[CUPSD_SB_BUFFER_SIZE],
4811 /* Message text */
f899b121 4812 *ptr; /* Pointer update... */
4813 int loglevel, /* Log level for message */
4814 event = 0; /* Events? */
f0ab5bff
MS
4815 static const char * const levels[] = /* Log levels */
4816 {
4817 "NONE",
4818 "EMERG",
4819 "ALERT",
4820 "CRIT",
4821 "ERROR",
4822 "WARN",
4823 "NOTICE",
4824 "INFO",
4825 "DEBUG",
4826 "DEBUG2"
4827 };
f899b121 4828
4829
f0ab5bff
MS
4830 /*
4831 * Get the printer associated with this job; if the printer is stopped for
4832 * any reason then job->printer will be reset to NULL, so make sure we have
4833 * a valid pointer...
4834 */
4835
f899b121 4836 while ((ptr = cupsdStatBufUpdate(job->status_buffer, &loglevel,
4837 message, sizeof(message))) != NULL)
4838 {
4839 /*
4840 * Process page and printer state messages as needed...
4841 */
4842
4843 if (loglevel == CUPSD_LOG_PAGE)
4844 {
4845 /*
4846 * Page message; send the message to the page_log file and update the
4847 * job sheet count...
4848 */
4849
75bd9771 4850 cupsdLogJob(job, CUPSD_LOG_DEBUG, "PAGE: %s", message);
dd1abb6b 4851
91c84a35 4852 if (job->sheets)
f899b121 4853 {
88f9aafc 4854 if (!_cups_strncasecmp(message, "total ", 6))
f899b121 4855 {
4856 /*
4857 * Got a total count of pages from a backend or filter...
4858 */
4859
4860 copies = atoi(message + 6);
4861 copies -= job->sheets->values[0].integer; /* Just track the delta */
4862 }
4863 else if (!sscanf(message, "%*d%d", &copies))
4864 copies = 1;
4865
4866 job->sheets->values[0].integer += copies;
4867
b9faaae1 4868 if (job->printer->page_limit)
ba55dc12 4869 cupsdUpdateQuota(job->printer, job->username, copies, 0);
f899b121 4870 }
4871
4872 cupsdLogPage(job, message);
4873
91c84a35 4874 if (job->sheets)
b9faaae1 4875 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS, job->printer, job,
91c84a35 4876 "Printed %d page(s).", job->sheets->values[0].integer);
f899b121 4877 }
a469f8a5
MS
4878 else if (loglevel == CUPSD_LOG_JOBSTATE)
4879 {
4880 /*
4881 * Support "keyword" to set job-state-reasons to the specified keyword.
4882 * This is sufficient for the current paid printing stuff.
4883 */
4884
4885 cupsdLogJob(job, CUPSD_LOG_DEBUG, "JOBSTATE: %s", message);
4886
8e67b75b
MS
4887 if (!strcmp(message, "cups-retry-as-raster"))
4888 job->retry_as_raster = 1;
4889 else
4890 ippSetString(job->attrs, &job->reasons, 0, message);
a469f8a5 4891 }
f899b121 4892 else if (loglevel == CUPSD_LOG_STATE)
4893 {
75bd9771 4894 cupsdLogJob(job, CUPSD_LOG_DEBUG, "STATE: %s", message);
dd1abb6b 4895
09a101d6 4896 if (!strcmp(message, "paused"))
c24d2134 4897 {
b9faaae1 4898 cupsdStopPrinter(job->printer, 1);
c24d2134
MS
4899 return;
4900 }
0fa6c7fa 4901 else if (message[0] && cupsdSetPrinterReasons(job->printer, message))
dcb445bc 4902 {
d9bca400 4903 event |= CUPSD_EVENT_PRINTER_STATE;
bc44d920 4904
982069db 4905 if (MaxJobTime > 0)
dcb445bc
MS
4906 {
4907 /*
4908 * Reset cancel time after connecting to the device...
4909 */
4910
4911 for (i = 0; i < job->printer->num_reasons; i ++)
4912 if (!strcmp(job->printer->reasons[i], "connecting-to-device"))
4913 break;
4914
4915 if (i >= job->printer->num_reasons)
982069db 4916 {
581dae2d
MS
4917 ipp_attribute_t *cancel_after = ippFindAttribute(job->attrs,
4918 "job-cancel-after",
4919 IPP_TAG_INTEGER);
4920 /* job-cancel-after attribute */
4921
982069db
MS
4922 if (cancel_after)
4923 job->cancel_time = time(NULL) + ippGetInteger(cancel_after, 0);
4924 else
4925 job->cancel_time = time(NULL) + MaxJobTime;
4926 }
dcb445bc
MS
4927 }
4928 }
4929
4509bb49 4930 update_job_attrs(job, 0);
f899b121 4931 }
4932 else if (loglevel == CUPSD_LOG_ATTR)
4933 {
4934 /*
4935 * Set attribute(s)...
4936 */
4937
4938 int num_attrs; /* Number of attributes */
4939 cups_option_t *attrs; /* Attributes */
4940 const char *attr; /* Attribute */
4941
75bd9771 4942 cupsdLogJob(job, CUPSD_LOG_DEBUG, "ATTR: %s", message);
dd1abb6b 4943
f899b121 4944 num_attrs = cupsParseOptions(message, 0, &attrs);
4945
5a9febac
MS
4946 if ((attr = cupsGetOption("auth-info-default", num_attrs,
4947 attrs)) != NULL)
4948 {
4949 job->printer->num_options = cupsAddOption("auth-info", attr,
4950 job->printer->num_options,
4951 &(job->printer->options));
4952 cupsdSetPrinterAttrs(job->printer);
4953
4954 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
4955 }
4956
f899b121 4957 if ((attr = cupsGetOption("auth-info-required", num_attrs,
4958 attrs)) != NULL)
7ff4fea9 4959 {
b9faaae1
MS
4960 cupsdSetAuthInfoRequired(job->printer, attr, NULL);
4961 cupsdSetPrinterAttrs(job->printer);
3dfe78b3 4962
a2326b5b 4963 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
7ff4fea9 4964 }
f899b121 4965
9a4f8274
MS
4966 if ((attr = cupsGetOption("job-media-progress", num_attrs,
4967 attrs)) != NULL)
4968 {
4969 int progress = atoi(attr);
4970
4971
4972 if (progress >= 0 && progress <= 100)
4973 {
4974 job->progress = progress;
4975
4976 if (job->sheets)
b9faaae1 4977 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS, job->printer, job,
9a4f8274
MS
4978 "Printing page %d, %d%%",
4979 job->sheets->values[0].integer, job->progress);
4980 }
4981 }
4982
323c5de1 4983 if ((attr = cupsGetOption("printer-alert", num_attrs, attrs)) != NULL)
4984 {
b9faaae1 4985 cupsdSetString(&job->printer->alert, attr);
d9bca400 4986 event |= CUPSD_EVENT_PRINTER_STATE;
323c5de1 4987 }
4988
4989 if ((attr = cupsGetOption("printer-alert-description", num_attrs,
4990 attrs)) != NULL)
4991 {
b9faaae1 4992 cupsdSetString(&job->printer->alert_description, attr);
d9bca400 4993 event |= CUPSD_EVENT_PRINTER_STATE;
323c5de1 4994 }
4995
5a738aea
MS
4996 if ((attr = cupsGetOption("marker-colors", num_attrs, attrs)) != NULL)
4997 {
b9faaae1
MS
4998 cupsdSetPrinterAttr(job->printer, "marker-colors", (char *)attr);
4999 job->printer->marker_time = time(NULL);
5a738aea 5000 event |= CUPSD_EVENT_PRINTER_STATE;
52f6f666 5001 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
5a738aea
MS
5002 }
5003
5004 if ((attr = cupsGetOption("marker-levels", num_attrs, attrs)) != NULL)
5005 {
b9faaae1
MS
5006 cupsdSetPrinterAttr(job->printer, "marker-levels", (char *)attr);
5007 job->printer->marker_time = time(NULL);
5a738aea 5008 event |= CUPSD_EVENT_PRINTER_STATE;
52f6f666 5009 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
5a738aea
MS
5010 }
5011
ed6e7faf
MS
5012 if ((attr = cupsGetOption("marker-low-levels", num_attrs, attrs)) != NULL)
5013 {
b9faaae1
MS
5014 cupsdSetPrinterAttr(job->printer, "marker-low-levels", (char *)attr);
5015 job->printer->marker_time = time(NULL);
ed6e7faf
MS
5016 event |= CUPSD_EVENT_PRINTER_STATE;
5017 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
5018 }
5019
5020 if ((attr = cupsGetOption("marker-high-levels", num_attrs, attrs)) != NULL)
5021 {
b9faaae1
MS
5022 cupsdSetPrinterAttr(job->printer, "marker-high-levels", (char *)attr);
5023 job->printer->marker_time = time(NULL);
ed6e7faf
MS
5024 event |= CUPSD_EVENT_PRINTER_STATE;
5025 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
5026 }
5027
75bd9771
MS
5028 if ((attr = cupsGetOption("marker-message", num_attrs, attrs)) != NULL)
5029 {
b9faaae1
MS
5030 cupsdSetPrinterAttr(job->printer, "marker-message", (char *)attr);
5031 job->printer->marker_time = time(NULL);
75bd9771 5032 event |= CUPSD_EVENT_PRINTER_STATE;
52f6f666 5033 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
75bd9771
MS
5034 }
5035
5a738aea
MS
5036 if ((attr = cupsGetOption("marker-names", num_attrs, attrs)) != NULL)
5037 {
b9faaae1
MS
5038 cupsdSetPrinterAttr(job->printer, "marker-names", (char *)attr);
5039 job->printer->marker_time = time(NULL);
5a738aea 5040 event |= CUPSD_EVENT_PRINTER_STATE;
52f6f666 5041 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
5a738aea
MS
5042 }
5043
5044 if ((attr = cupsGetOption("marker-types", num_attrs, attrs)) != NULL)
5045 {
b9faaae1
MS
5046 cupsdSetPrinterAttr(job->printer, "marker-types", (char *)attr);
5047 job->printer->marker_time = time(NULL);
5a738aea 5048 event |= CUPSD_EVENT_PRINTER_STATE;
52f6f666 5049 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
5a738aea
MS
5050 }
5051
f899b121 5052 cupsFreeOptions(num_attrs, attrs);
5053 }
c9fc04c6
MS
5054 else if (loglevel == CUPSD_LOG_PPD)
5055 {
5056 /*
5057 * Set attribute(s)...
5058 */
5059
75bd9771 5060 cupsdLogJob(job, CUPSD_LOG_DEBUG, "PPD: %s", message);
dd1abb6b 5061
a29fd7dd
MS
5062 job->num_keywords = cupsParseOptions(message, job->num_keywords,
5063 &job->keywords);
c9fc04c6 5064 }
4d301e69 5065 else
f899b121 5066 {
4d301e69
MS
5067 /*
5068 * Strip legacy message prefix...
5069 */
f899b121 5070
4d301e69 5071 if (!strncmp(message, "recoverable:", 12))
d2354e63 5072 {
4d301e69
MS
5073 ptr = message + 12;
5074 while (isspace(*ptr & 255))
5075 ptr ++;
d2354e63 5076 }
4d301e69 5077 else if (!strncmp(message, "recovered:", 10))
acb056cb 5078 {
4d301e69
MS
5079 ptr = message + 10;
5080 while (isspace(*ptr & 255))
5081 ptr ++;
acb056cb 5082 }
4d301e69
MS
5083 else
5084 ptr = message;
5085
dcb445bc
MS
5086 if (*ptr)
5087 cupsdLogJob(job, loglevel, "%s", ptr);
f899b121 5088
5180a04c
MS
5089 if (loglevel < CUPSD_LOG_DEBUG &&
5090 strcmp(job->printer->state_message, ptr))
1f0275e3 5091 {
4d301e69 5092 strlcpy(job->printer->state_message, ptr,
b9faaae1 5093 sizeof(job->printer->state_message));
4509bb49 5094
8b116e60 5095 event |= CUPSD_EVENT_PRINTER_STATE | CUPSD_EVENT_JOB_PROGRESS;
4509bb49 5096
229681c1 5097 if (loglevel <= job->status_level && job->status_level > CUPSD_LOG_ERROR)
1f0275e3
MS
5098 {
5099 /*
b9faaae1 5100 * Some messages show in the job-printer-state-message attribute...
1f0275e3 5101 */
09a101d6 5102
1f0275e3
MS
5103 if (loglevel != CUPSD_LOG_NOTICE)
5104 job->status_level = loglevel;
01ce6322 5105
1f0275e3 5106 update_job_attrs(job, 1);
f0ab5bff
MS
5107
5108 cupsdLogJob(job, CUPSD_LOG_DEBUG,
5109 "Set job-printer-state-message to \"%s\", "
5110 "current level=%s",
5111 job->printer_message->values[0].string.text,
5112 levels[job->status_level]);
1f0275e3 5113 }
75bd9771 5114 }
f899b121 5115 }
5116
5117 if (!strchr(job->status_buffer->buffer, '\n'))
5118 break;
5119 }
5120
f8b3a85b
MS
5121 if (event & CUPSD_EVENT_JOB_PROGRESS)
5122 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS, job->printer, job,
5123 "%s", job->printer->state_message);
5180a04c 5124 if (event & CUPSD_EVENT_PRINTER_STATE)
b9faaae1
MS
5125 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, job->printer, NULL,
5126 (job->printer->type & CUPS_PRINTER_CLASS) ?
f899b121 5127 "Class \"%s\" state changed." :
5128 "Printer \"%s\" state changed.",
b9faaae1 5129 job->printer->name);
f899b121 5130
8b116e60 5131
f899b121 5132 if (ptr == NULL && !job->status_buffer->bufused)
5133 {
5134 /*
5135 * See if all of the filters and the backend have returned their
5136 * exit statuses.
5137 */
5138
5139 for (i = 0; job->filters[i] < 0; i ++);
5140
5141 if (job->filters[i])
d7871c8c
MS
5142 {
5143 /*
5144 * EOF but we haven't collected the exit status of all filters...
5145 */
5146
5147 cupsdCheckProcess();
f899b121 5148 return;
d7871c8c 5149 }
f899b121 5150
5151 if (job->current_file >= job->num_files && job->backend > 0)
d7871c8c
MS
5152 {
5153 /*
5154 * EOF but we haven't collected the exit status of the backend...
5155 */
5156
5157 cupsdCheckProcess();
f899b121 5158 return;
d7871c8c 5159 }
f899b121 5160
5161 /*
5162 * Handle the end of job stuff...
5163 */
5164
ef55b745 5165 finalize_job(job, 1);
b9faaae1
MS
5166
5167 /*
5168 * Check for new jobs...
5169 */
5170
5171 cupsdCheckJobs();
f899b121 5172 }
5173}
5174
5175
5176/*
bc44d920 5177 * 'update_job_attrs()' - Update the job-printer-* attributes.
5178 */
5179
5180void
4509bb49 5181update_job_attrs(cupsd_job_t *job, /* I - Job to update */
b9faaae1 5182 int do_message)/* I - 1 = copy job-printer-state message */
bc44d920 5183{
5184 int i; /* Looping var */
5185 int num_reasons; /* Actual number of reasons */
5186 const char * const *reasons; /* Reasons */
f0ab5bff 5187 static const char *none = "none"; /* "none" reason */
bc44d920 5188
5189
5190 /*
5191 * Get/create the job-printer-state-* attributes...
5192 */
5193
5194 if (!job->printer_message)
5195 {
5196 if ((job->printer_message = ippFindAttribute(job->attrs,
5197 "job-printer-state-message",
5198 IPP_TAG_TEXT)) == NULL)
5199 job->printer_message = ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_TEXT,
5200 "job-printer-state-message",
5201 NULL, "");
5202 }
5203
5204 if (!job->printer_reasons)
5205 job->printer_reasons = ippFindAttribute(job->attrs,
5206 "job-printer-state-reasons",
5207 IPP_TAG_KEYWORD);
5208
bc44d920 5209 /*
b9faaae1 5210 * Copy or clear the printer-state-message value as needed...
bc44d920 5211 */
5212
b9faaae1
MS
5213 if (job->state_value != IPP_JOB_PROCESSING &&
5214 job->status_level == CUPSD_LOG_INFO)
dcb445bc 5215 {
b9faaae1 5216 cupsdSetString(&(job->printer_message->values[0].string.text), "");
dcb445bc
MS
5217
5218 job->dirty = 1;
5219 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
5220 }
b9faaae1 5221 else if (job->printer->state_message[0] && do_message)
dcb445bc 5222 {
bc44d920 5223 cupsdSetString(&(job->printer_message->values[0].string.text),
b9faaae1 5224 job->printer->state_message);
ef55b745 5225
dcb445bc
MS
5226 job->dirty = 1;
5227 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
5228 }
5229
bc44d920 5230 /*
5231 * ... and the printer-state-reasons value...
5232 */
5233
b9faaae1 5234 if (job->printer->num_reasons == 0)
bc44d920 5235 {
5236 num_reasons = 1;
f0ab5bff 5237 reasons = &none;
bc44d920 5238 }
5239 else
5240 {
b9faaae1
MS
5241 num_reasons = job->printer->num_reasons;
5242 reasons = (const char * const *)job->printer->reasons;
bc44d920 5243 }
5244
5245 if (!job->printer_reasons || job->printer_reasons->num_values != num_reasons)
5246 {
b9faaae1
MS
5247 /*
5248 * Replace/create a job-printer-state-reasons attribute...
5249 */
5250
bc44d920 5251 ippDeleteAttribute(job->attrs, job->printer_reasons);
5252
5253 job->printer_reasons = ippAddStrings(job->attrs,
5254 IPP_TAG_JOB, IPP_TAG_KEYWORD,
5255 "job-printer-state-reasons",
5256 num_reasons, NULL, NULL);
5257 }
b9faaae1 5258 else
d2354e63 5259 {
b9faaae1
MS
5260 /*
5261 * Don't bother clearing the reason strings if they are the same...
5262 */
5263
5264 for (i = 0; i < num_reasons; i ++)
5265 if (strcmp(job->printer_reasons->values[i].string.text, reasons[i]))
5266 break;
5267
5268 if (i >= num_reasons)
5269 return;
5270
5271 /*
5272 * Not the same, so free the current strings...
5273 */
5274
5275 for (i = 0; i < num_reasons; i ++)
d2354e63
MS
5276 _cupsStrFree(job->printer_reasons->values[i].string.text);
5277 }
bc44d920 5278
b9faaae1
MS
5279 /*
5280 * Copy the reasons...
5281 */
5282
bc44d920 5283 for (i = 0; i < num_reasons; i ++)
d2354e63 5284 job->printer_reasons->values[i].string.text = _cupsStrAlloc(reasons[i]);
dcb445bc
MS
5285
5286 job->dirty = 1;
5287 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
bc44d920 5288}
5289
5290
5291/*
f2d18633 5292 * End of "$Id$".
ef416fc2 5293 */