]> git.ipfire.org Git - thirdparty/cups.git/blame - scheduler/job.c
The scheduler now returns completed jobs in the correct newest-to-oldest order
[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
1678 if (job->state_value >= IPP_JOB_CANCELED &&
1679 (attr = ippFindAttribute(job->attrs, "time-at-completed",
1680 IPP_TAG_INTEGER)) != NULL)
1681 {
eec1fbc3
MS
1682 job->completed_time = attr->values[0].integer;
1683
82cc1f9a
MS
1684 if (JobHistory < INT_MAX)
1685 job->history_time = attr->values[0].integer + JobHistory;
1686 else
1687 job->history_time = INT_MAX;
1688
1689 if (job->history_time < time(NULL))
1690 goto error; /* Expired, remove from history */
1691
1692 if (job->history_time < JobHistoryUpdate || !JobHistoryUpdate)
1693 JobHistoryUpdate = job->history_time;
1694
1695 if (JobFiles < INT_MAX)
1696 job->file_time = attr->values[0].integer + JobFiles;
1697 else
1698 job->file_time = INT_MAX;
1699
1700 if (job->file_time < JobHistoryUpdate || !JobHistoryUpdate)
1701 JobHistoryUpdate = job->file_time;
1702
1703 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdLoadJob: JobHistoryUpdate=%ld",
1704 (long)JobHistoryUpdate);
1705 }
ef416fc2 1706
b9faaae1 1707 if (!job->dest)
ef416fc2 1708 {
b9faaae1
MS
1709 if ((attr = ippFindAttribute(job->attrs, "job-printer-uri",
1710 IPP_TAG_URI)) == NULL)
1711 {
72f50170
MS
1712 cupsdLogJob(job, CUPSD_LOG_ERROR,
1713 "No job-printer-uri attribute in control file.");
b9faaae1
MS
1714 goto error;
1715 }
ef416fc2 1716
b9faaae1
MS
1717 if ((dest = cupsdValidateDest(attr->values[0].string.text, &(job->dtype),
1718 &destptr)) == NULL)
1719 {
72f50170
MS
1720 cupsdLogJob(job, CUPSD_LOG_ERROR,
1721 "Unable to queue job for destination \"%s\".",
1722 attr->values[0].string.text);
b9faaae1
MS
1723 goto error;
1724 }
ef416fc2 1725
b9faaae1 1726 cupsdSetString(&job->dest, dest);
f7deaa1a 1727 }
b9faaae1 1728 else if ((destptr = cupsdFindDest(job->dest)) == NULL)
ef416fc2 1729 {
72f50170
MS
1730 cupsdLogJob(job, CUPSD_LOG_ERROR,
1731 "Unable to queue job for destination \"%s\".",
1732 job->dest);
b9faaae1
MS
1733 goto error;
1734 }
ef416fc2 1735
12f89d24
MS
1736 if ((job->reasons = ippFindAttribute(job->attrs, "job-state-reasons",
1737 IPP_TAG_KEYWORD)) == NULL)
1738 {
1739 const char *reason; /* job-state-reason keyword */
1740
72f50170
MS
1741 cupsdLogJob(job, CUPSD_LOG_DEBUG,
1742 "Adding missing job-state-reasons attribute to control file.");
12f89d24
MS
1743
1744 switch (job->state_value)
1745 {
82cc1f9a 1746 default :
12f89d24
MS
1747 case IPP_JOB_PENDING :
1748 if (destptr->state == IPP_PRINTER_STOPPED)
1749 reason = "printer-stopped";
1750 else
1751 reason = "none";
1752 break;
1753
1754 case IPP_JOB_HELD :
1755 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
1756 IPP_TAG_ZERO)) != NULL &&
1757 (attr->value_tag == IPP_TAG_NAME ||
1758 attr->value_tag == IPP_TAG_NAMELANG ||
1759 attr->value_tag == IPP_TAG_KEYWORD) &&
1760 strcmp(attr->values[0].string.text, "no-hold"))
1761 reason = "job-hold-until-specified";
1762 else
1763 reason = "job-incoming";
1764 break;
1765
1766 case IPP_JOB_PROCESSING :
1767 reason = "job-printing";
1768 break;
1769
1770 case IPP_JOB_STOPPED :
1771 reason = "job-stopped";
1772 break;
1773
1774 case IPP_JOB_CANCELED :
1775 reason = "job-canceled-by-user";
1776 break;
1777
1778 case IPP_JOB_ABORTED :
1779 reason = "aborted-by-system";
1780 break;
1781
1782 case IPP_JOB_COMPLETED :
1783 reason = "job-completed-successfully";
1784 break;
1785 }
1786
1787 job->reasons = ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_KEYWORD,
1788 "job-state-reasons", NULL, reason);
1789 }
1790 else if (job->state_value == IPP_JOB_PENDING)
1791 {
1792 if (destptr->state == IPP_PRINTER_STOPPED)
1793 ippSetString(job->attrs, &job->reasons, 0, "printer-stopped");
1794 else
1795 ippSetString(job->attrs, &job->reasons, 0, "none");
1796 }
1797
b9faaae1
MS
1798 job->sheets = ippFindAttribute(job->attrs, "job-media-sheets-completed",
1799 IPP_TAG_INTEGER);
1800 job->job_sheets = ippFindAttribute(job->attrs, "job-sheets", IPP_TAG_NAME);
ef416fc2 1801
b9faaae1
MS
1802 if (!job->priority)
1803 {
1804 if ((attr = ippFindAttribute(job->attrs, "job-priority",
1805 IPP_TAG_INTEGER)) == NULL)
1806 {
72f50170
MS
1807 cupsdLogJob(job, CUPSD_LOG_ERROR,
1808 "Missing or bad job-priority attribute in control file.");
b9faaae1
MS
1809 goto error;
1810 }
1811
1812 job->priority = attr->values[0].integer;
f7deaa1a 1813 }
b9faaae1
MS
1814
1815 if (!job->username)
ef416fc2 1816 {
b9faaae1
MS
1817 if ((attr = ippFindAttribute(job->attrs, "job-originating-user-name",
1818 IPP_TAG_NAME)) == NULL)
1819 {
72f50170
MS
1820 cupsdLogJob(job, CUPSD_LOG_ERROR,
1821 "Missing or bad job-originating-user-name "
1822 "attribute in control file.");
b9faaae1
MS
1823 goto error;
1824 }
ef416fc2 1825
b9faaae1
MS
1826 cupsdSetString(&job->username, attr->values[0].string.text);
1827 }
ef416fc2 1828
b9faaae1
MS
1829 /*
1830 * Set the job hold-until time and state...
1831 */
1832
1833 if (job->state_value == IPP_JOB_HELD)
1834 {
1835 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
1836 IPP_TAG_KEYWORD)) == NULL)
1837 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
1838
1839 if (attr)
1840 cupsdSetJobHoldUntil(job, attr->values[0].string.text, CUPSD_JOB_DEFAULT);
ef416fc2 1841 else
b9faaae1
MS
1842 {
1843 job->state->values[0].integer = IPP_JOB_PENDING;
1844 job->state_value = IPP_JOB_PENDING;
1845 }
f7deaa1a 1846 }
b9faaae1
MS
1847 else if (job->state_value == IPP_JOB_PROCESSING)
1848 {
1849 job->state->values[0].integer = IPP_JOB_PENDING;
1850 job->state_value = IPP_JOB_PENDING;
1851 }
1852
1853 if (!job->num_files)
ef416fc2 1854 {
1855 /*
b9faaae1 1856 * Find all the d##### files...
ef416fc2 1857 */
1858
b9faaae1
MS
1859 for (fileid = 1; fileid < 10000; fileid ++)
1860 {
1861 snprintf(jobfile, sizeof(jobfile), "%s/d%05d-%03d", RequestRoot,
1862 job->id, fileid);
ef416fc2 1863
b9faaae1
MS
1864 if (access(jobfile, 0))
1865 break;
1866
72f50170
MS
1867 cupsdLogJob(job, CUPSD_LOG_DEBUG,
1868 "Auto-typing document file \"%s\"...", jobfile);
b9faaae1
MS
1869
1870 if (fileid > job->num_files)
1871 {
1872 if (job->num_files == 0)
1873 {
7e86f2f6
MS
1874 compressions = (int *)calloc((size_t)fileid, sizeof(int));
1875 filetypes = (mime_type_t **)calloc((size_t)fileid, sizeof(mime_type_t *));
b9faaae1
MS
1876 }
1877 else
1878 {
7e86f2f6
MS
1879 compressions = (int *)realloc(job->compressions, sizeof(int) * (size_t)fileid);
1880 filetypes = (mime_type_t **)realloc(job->filetypes, sizeof(mime_type_t *) * (size_t)fileid);
b9faaae1
MS
1881 }
1882
cb7f98ee
MS
1883 if (compressions)
1884 job->compressions = compressions;
1885
1886 if (filetypes)
1887 job->filetypes = filetypes;
1888
b9faaae1
MS
1889 if (!compressions || !filetypes)
1890 {
72f50170
MS
1891 cupsdLogJob(job, CUPSD_LOG_ERROR,
1892 "Ran out of memory for job file types.");
ef55b745
MS
1893
1894 ippDelete(job->attrs);
1895 job->attrs = NULL;
1896
ef55b745
MS
1897 if (job->compressions)
1898 {
1899 free(job->compressions);
1900 job->compressions = NULL;
1901 }
1902
1903 if (job->filetypes)
1904 {
1905 free(job->filetypes);
1906 job->filetypes = NULL;
1907 }
1908
1909 job->num_files = 0;
1910 return (0);
b9faaae1
MS
1911 }
1912
cb7f98ee 1913 job->num_files = fileid;
b9faaae1
MS
1914 }
1915
1916 job->filetypes[fileid - 1] = mimeFileType(MimeDatabase, jobfile, NULL,
1917 job->compressions + fileid - 1);
1918
1919 if (!job->filetypes[fileid - 1])
1920 job->filetypes[fileid - 1] = mimeType(MimeDatabase, "application",
1921 "vnd.cups-raw");
1922 }
ef416fc2 1923 }
b9faaae1
MS
1924
1925 /*
1926 * Load authentication information as needed...
1927 */
1928
1929 if (job->state_value < IPP_JOB_STOPPED)
ef416fc2 1930 {
b9faaae1 1931 snprintf(jobfile, sizeof(jobfile), "%s/a%05d", RequestRoot, job->id);
ef416fc2 1932
88f9aafc
MS
1933 for (i = 0;
1934 i < (int)(sizeof(job->auth_env) / sizeof(job->auth_env[0]));
1935 i ++)
1936 cupsdClearString(job->auth_env + i);
07ed0e9a 1937 cupsdClearString(&job->auth_uid);
ef416fc2 1938
b9faaae1
MS
1939 if ((fp = cupsFileOpen(jobfile, "r")) != NULL)
1940 {
dcb445bc
MS
1941 int bytes, /* Size of auth data */
1942 linenum = 1; /* Current line number */
85dda01c 1943 char line[65536], /* Line from file */
dcb445bc 1944 *value, /* Value from line */
85dda01c 1945 data[65536]; /* Decoded data */
ef416fc2 1946
ef416fc2 1947
dcb445bc 1948 if (cupsFileGets(fp, line, sizeof(line)) &&
94436c5a 1949 !strcmp(line, "CUPSD-AUTH-V3"))
b9faaae1 1950 {
dcb445bc
MS
1951 i = 0;
1952 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
1953 {
1954 /*
1955 * Decode value...
1956 */
1957
94436c5a
MS
1958 if (strcmp(line, "negotiate") && strcmp(line, "uid"))
1959 {
1960 bytes = sizeof(data);
1961 httpDecode64_2(data, &bytes, value);
1962 }
b9faaae1 1963
dcb445bc
MS
1964 /*
1965 * Assign environment variables...
1966 */
1967
1968 if (!strcmp(line, "uid"))
1969 {
1970 cupsdSetStringf(&job->auth_uid, "AUTH_UID=%s", value);
1971 continue;
1972 }
1973 else if (i >= (int)(sizeof(job->auth_env) / sizeof(job->auth_env[0])))
1974 break;
1975
1976 if (!strcmp(line, "username"))
1977 cupsdSetStringf(job->auth_env + i, "AUTH_USERNAME=%s", data);
1978 else if (!strcmp(line, "domain"))
1979 cupsdSetStringf(job->auth_env + i, "AUTH_DOMAIN=%s", data);
1980 else if (!strcmp(line, "password"))
1981 cupsdSetStringf(job->auth_env + i, "AUTH_PASSWORD=%s", data);
1982 else if (!strcmp(line, "negotiate"))
94436c5a 1983 cupsdSetStringf(job->auth_env + i, "AUTH_NEGOTIATE=%s", value);
dcb445bc
MS
1984 else
1985 continue;
1986
1987 i ++;
1988 }
1989 }
07ed0e9a 1990
b9faaae1
MS
1991 cupsFileClose(fp);
1992 }
ef416fc2 1993 }
1994
b9faaae1
MS
1995 job->access_time = time(NULL);
1996 return (1);
1997
1998 /*
1999 * If we get here then something bad happened...
2000 */
2001
2002 error:
2003
2004 ippDelete(job->attrs);
2005 job->attrs = NULL;
ef55b745 2006
82cc1f9a
MS
2007 remove_job_history(job);
2008 remove_job_files(job);
b9faaae1
MS
2009
2010 return (0);
ef416fc2 2011}
2012
2013
2014/*
b9faaae1 2015 * 'cupsdMoveJob()' - Move the specified job to a different destination.
ef416fc2 2016 */
2017
2018void
b9faaae1
MS
2019cupsdMoveJob(cupsd_job_t *job, /* I - Job */
2020 cupsd_printer_t *p) /* I - Destination printer or class */
ef416fc2 2021{
b9faaae1
MS
2022 ipp_attribute_t *attr; /* job-printer-uri attribute */
2023 const char *olddest; /* Old destination */
2024 cupsd_printer_t *oldp; /* Old pointer */
ef416fc2 2025
2026
2027 /*
b9faaae1 2028 * Don't move completed jobs...
ef416fc2 2029 */
2030
b9faaae1 2031 if (job->state_value > IPP_JOB_STOPPED)
ef416fc2 2032 return;
2033
2034 /*
b9faaae1 2035 * Get the old destination...
ef416fc2 2036 */
2037
b9faaae1 2038 olddest = job->dest;
ef416fc2 2039
b9faaae1
MS
2040 if (job->printer)
2041 oldp = job->printer;
ef416fc2 2042 else
b9faaae1 2043 oldp = cupsdFindDest(olddest);
ef416fc2 2044
b9faaae1
MS
2045 /*
2046 * Change the destination information...
2047 */
2048
eac3a0a0
MS
2049 if (job->state_value > IPP_JOB_HELD)
2050 cupsdSetJobState(job, IPP_JOB_PENDING, CUPSD_JOB_DEFAULT,
2051 "Stopping job prior to move.");
b9faaae1
MS
2052
2053 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED, oldp, job,
2054 "Job #%d moved from %s to %s.", job->id, olddest,
2055 p->name);
2056
2057 cupsdSetString(&job->dest, p->name);
a2326b5b 2058 job->dtype = p->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_REMOTE);
b9faaae1
MS
2059
2060 if ((attr = ippFindAttribute(job->attrs, "job-printer-uri",
2061 IPP_TAG_URI)) != NULL)
2062 cupsdSetString(&(attr->values[0].string.text), p->uri);
2063
2064 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED, p, job,
2065 "Job #%d moved from %s to %s.", job->id, olddest,
2066 p->name);
ef416fc2 2067
3dfe78b3
MS
2068 job->dirty = 1;
2069 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
ef416fc2 2070}
2071
2072
2073/*
b9faaae1 2074 * 'cupsdReleaseJob()' - Release the specified job.
ef416fc2 2075 */
2076
2077void
b9faaae1 2078cupsdReleaseJob(cupsd_job_t *job) /* I - Job */
ef416fc2 2079{
b9faaae1
MS
2080 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdReleaseJob(job=%p(%d))", job,
2081 job->id);
ef416fc2 2082
b9faaae1
MS
2083 if (job->state_value == IPP_JOB_HELD)
2084 {
2085 /*
2086 * Add trailing banner as needed...
2087 */
ef416fc2 2088
b9faaae1
MS
2089 if (job->pending_timeout)
2090 cupsdTimeoutJob(job);
ef416fc2 2091
b9faaae1
MS
2092 cupsdSetJobState(job, IPP_JOB_PENDING, CUPSD_JOB_DEFAULT,
2093 "Job released by user.");
2094 }
e1d6a774 2095}
ef416fc2 2096
ef416fc2 2097
e1d6a774 2098/*
b9faaae1 2099 * 'cupsdRestartJob()' - Restart the specified job.
e1d6a774 2100 */
ef416fc2 2101
e1d6a774 2102void
b9faaae1 2103cupsdRestartJob(cupsd_job_t *job) /* I - Job */
e1d6a774 2104{
b9faaae1
MS
2105 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdRestartJob(job=%p(%d))", job,
2106 job->id);
ef416fc2 2107
b9faaae1
MS
2108 if (job->state_value == IPP_JOB_STOPPED || job->num_files)
2109 cupsdSetJobState(job, IPP_JOB_PENDING, CUPSD_JOB_DEFAULT,
2110 "Job restarted by user.");
2111}
ef416fc2 2112
ef416fc2 2113
b9faaae1
MS
2114/*
2115 * 'cupsdSaveAllJobs()' - Save a summary of all jobs to disk.
2116 */
ef416fc2 2117
b9faaae1
MS
2118void
2119cupsdSaveAllJobs(void)
2120{
2121 int i; /* Looping var */
321d8d57
MS
2122 cups_file_t *fp; /* job.cache file */
2123 char filename[1024], /* job.cache filename */
2124 temp[1024]; /* Temporary string */
b9faaae1
MS
2125 cupsd_job_t *job; /* Current job */
2126 time_t curtime; /* Current time */
2127 struct tm *curdate; /* Current date */
ef416fc2 2128
ef416fc2 2129
321d8d57
MS
2130 snprintf(filename, sizeof(filename), "%s/job.cache", CacheDir);
2131 if ((fp = cupsdCreateConfFile(filename, ConfigFilePerm)) == NULL)
c168a833 2132 return;
ef416fc2 2133
321d8d57 2134 cupsdLogMessage(CUPSD_LOG_INFO, "Saving job.cache...");
bd7854cb 2135
b9faaae1
MS
2136 /*
2137 * Write a small header to the file...
2138 */
bd7854cb 2139
b9faaae1
MS
2140 curtime = time(NULL);
2141 curdate = localtime(&curtime);
2142 strftime(temp, sizeof(temp) - 1, "%Y-%m-%d %H:%M", curdate);
ef416fc2 2143
b9faaae1
MS
2144 cupsFilePuts(fp, "# Job cache file for " CUPS_SVERSION "\n");
2145 cupsFilePrintf(fp, "# Written by cupsd on %s\n", temp);
2146 cupsFilePrintf(fp, "NextJobId %d\n", NextJobId);
f7deaa1a 2147
b9faaae1
MS
2148 /*
2149 * Write each job known to the system...
2150 */
f7deaa1a 2151
b9faaae1
MS
2152 for (job = (cupsd_job_t *)cupsArrayFirst(Jobs);
2153 job;
2154 job = (cupsd_job_t *)cupsArrayNext(Jobs))
ef416fc2 2155 {
b9faaae1
MS
2156 cupsFilePrintf(fp, "<Job %d>\n", job->id);
2157 cupsFilePrintf(fp, "State %d\n", job->state_value);
eec1fbc3
MS
2158 if (job->completed_time)
2159 cupsFilePrintf(fp, "Completed %ld\n", (long)job->completed_time);
b9faaae1 2160 cupsFilePrintf(fp, "Priority %d\n", job->priority);
eec1fbc3
MS
2161 if (job->hold_until)
2162 cupsFilePrintf(fp, "HoldUntil %ld\n", (long)job->hold_until);
b9faaae1
MS
2163 cupsFilePrintf(fp, "Username %s\n", job->username);
2164 cupsFilePrintf(fp, "Destination %s\n", job->dest);
2165 cupsFilePrintf(fp, "DestType %d\n", job->dtype);
2166 cupsFilePrintf(fp, "NumFiles %d\n", job->num_files);
2167 for (i = 0; i < job->num_files; i ++)
2168 cupsFilePrintf(fp, "File %d %s/%s %d\n", i + 1, job->filetypes[i]->super,
2169 job->filetypes[i]->type, job->compressions[i]);
2170 cupsFilePuts(fp, "</Job>\n");
ef416fc2 2171 }
b9faaae1 2172
321d8d57 2173 cupsdCloseCreatedConfFile(fp, filename);
e1d6a774 2174}
ef416fc2 2175
ef416fc2 2176
e1d6a774 2177/*
b9faaae1 2178 * 'cupsdSaveJob()' - Save a job to disk.
e1d6a774 2179 */
ef416fc2 2180
e1d6a774 2181void
b9faaae1 2182cupsdSaveJob(cupsd_job_t *job) /* I - Job */
e1d6a774 2183{
82cc1f9a 2184 char filename[1024]; /* Job control filename */
b9faaae1 2185 cups_file_t *fp; /* Job file */
3dfe78b3 2186
ef416fc2 2187
b9faaae1
MS
2188 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdSaveJob(job=%p(%d)): job->attrs=%p",
2189 job, job->id, job->attrs);
ef416fc2 2190
b9faaae1 2191 snprintf(filename, sizeof(filename), "%s/c%05d", RequestRoot, job->id);
ef416fc2 2192
82cc1f9a 2193 if ((fp = cupsdCreateConfFile(filename, ConfigFilePerm & 0600)) == NULL)
b9faaae1 2194 return;
ef416fc2 2195
b9faaae1 2196 fchown(cupsFileNumber(fp), RunUser, Group);
ef416fc2 2197
b9faaae1 2198 job->attrs->state = IPP_IDLE;
ef416fc2 2199
b9faaae1
MS
2200 if (ippWriteIO(fp, (ipp_iocb_t)cupsFileWrite, 1, NULL,
2201 job->attrs) != IPP_DATA)
321d8d57 2202 {
72f50170 2203 cupsdLogJob(job, CUPSD_LOG_ERROR, "Unable to write job control file.");
321d8d57 2204 cupsFileClose(fp);
321d8d57
MS
2205 return;
2206 }
ef416fc2 2207
82cc1f9a 2208 if (!cupsdCloseCreatedConfFile(fp, filename))
cb7f98ee
MS
2209 {
2210 /*
2211 * Remove backup file and mark this job as clean...
2212 */
2213
2214 strlcat(filename, ".O", sizeof(filename));
2215 unlink(filename);
2216
82cc1f9a 2217 job->dirty = 0;
cb7f98ee 2218 }
e1d6a774 2219}
ef416fc2 2220
ef416fc2 2221
e1d6a774 2222/*
b9faaae1 2223 * 'cupsdSetJobHoldUntil()' - Set the hold time for a job.
e1d6a774 2224 */
ef416fc2 2225
b9faaae1
MS
2226void
2227cupsdSetJobHoldUntil(cupsd_job_t *job, /* I - Job */
2228 const char *when, /* I - When to resume */
2229 int update)/* I - Update job-hold-until attr? */
e1d6a774 2230{
b9faaae1
MS
2231 time_t curtime; /* Current time */
2232 struct tm *curdate; /* Current date */
2233 int hour; /* Hold hour */
2234 int minute; /* Hold minute */
2235 int second = 0; /* Hold second */
ef416fc2 2236
ef416fc2 2237
b9faaae1
MS
2238 cupsdLogMessage(CUPSD_LOG_DEBUG2,
2239 "cupsdSetJobHoldUntil(job=%p(%d), when=\"%s\", update=%d)",
2240 job, job->id, when, update);
ef416fc2 2241
b9faaae1 2242 if (update)
ef416fc2 2243 {
e1d6a774 2244 /*
b9faaae1 2245 * Update the job-hold-until attribute...
e1d6a774 2246 */
ef416fc2 2247
b9faaae1 2248 ipp_attribute_t *attr; /* job-hold-until attribute */
ef416fc2 2249
b9faaae1
MS
2250 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
2251 IPP_TAG_KEYWORD)) == NULL)
2252 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
ef416fc2 2253
b9faaae1
MS
2254 if (attr)
2255 cupsdSetString(&(attr->values[0].string.text), when);
2256 else
2257 attr = ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_KEYWORD,
2258 "job-hold-until", NULL, when);
ef416fc2 2259
b9faaae1
MS
2260 if (attr)
2261 {
2262 if (isdigit(when[0] & 255))
2263 attr->value_tag = IPP_TAG_NAME;
2264 else
2265 attr->value_tag = IPP_TAG_KEYWORD;
2266
2267 job->dirty = 1;
2268 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
2269 }
3e7fe0ca 2270
b9faaae1 2271 }
ef416fc2 2272
67dde2cc
MS
2273 ippSetString(job->attrs, &job->reasons, 0, "job-hold-until-specified");
2274
b9faaae1
MS
2275 /*
2276 * Update the hold time...
2277 */
2278
3e7fe0ca
MS
2279 job->cancel_time = 0;
2280
b9faaae1
MS
2281 if (!strcmp(when, "indefinite") || !strcmp(when, "auth-info-required"))
2282 {
e1d6a774 2283 /*
b9faaae1 2284 * Hold indefinitely...
e1d6a774 2285 */
ef416fc2 2286
b9faaae1 2287 job->hold_until = 0;
3e7fe0ca
MS
2288
2289 if (MaxHoldTime > 0)
2290 job->cancel_time = time(NULL) + MaxHoldTime;
b9faaae1
MS
2291 }
2292 else if (!strcmp(when, "day-time"))
2293 {
e1d6a774 2294 /*
b9faaae1 2295 * Hold to 6am the next morning unless local time is < 6pm.
e1d6a774 2296 */
ef416fc2 2297
b9faaae1
MS
2298 curtime = time(NULL);
2299 curdate = localtime(&curtime);
ef416fc2 2300
b9faaae1
MS
2301 if (curdate->tm_hour < 18)
2302 job->hold_until = curtime;
2303 else
2304 job->hold_until = curtime +
2305 ((29 - curdate->tm_hour) * 60 + 59 -
2306 curdate->tm_min) * 60 + 60 - curdate->tm_sec;
2307 }
2308 else if (!strcmp(when, "evening") || !strcmp(when, "night"))
2309 {
2310 /*
2311 * Hold to 6pm unless local time is > 6pm or < 6am.
2312 */
ef416fc2 2313
b9faaae1
MS
2314 curtime = time(NULL);
2315 curdate = localtime(&curtime);
ef416fc2 2316
b9faaae1
MS
2317 if (curdate->tm_hour < 6 || curdate->tm_hour >= 18)
2318 job->hold_until = curtime;
2319 else
2320 job->hold_until = curtime +
2321 ((17 - curdate->tm_hour) * 60 + 59 -
2322 curdate->tm_min) * 60 + 60 - curdate->tm_sec;
2323 }
2324 else if (!strcmp(when, "second-shift"))
2325 {
2326 /*
2327 * Hold to 4pm unless local time is > 4pm.
2328 */
ef416fc2 2329
b9faaae1
MS
2330 curtime = time(NULL);
2331 curdate = localtime(&curtime);
ef416fc2 2332
b9faaae1
MS
2333 if (curdate->tm_hour >= 16)
2334 job->hold_until = curtime;
2335 else
2336 job->hold_until = curtime +
2337 ((15 - curdate->tm_hour) * 60 + 59 -
2338 curdate->tm_min) * 60 + 60 - curdate->tm_sec;
2339 }
2340 else if (!strcmp(when, "third-shift"))
2341 {
2342 /*
2343 * Hold to 12am unless local time is < 8am.
2344 */
ef416fc2 2345
b9faaae1
MS
2346 curtime = time(NULL);
2347 curdate = localtime(&curtime);
ef416fc2 2348
b9faaae1
MS
2349 if (curdate->tm_hour < 8)
2350 job->hold_until = curtime;
2351 else
2352 job->hold_until = curtime +
2353 ((23 - curdate->tm_hour) * 60 + 59 -
2354 curdate->tm_min) * 60 + 60 - curdate->tm_sec;
2355 }
2356 else if (!strcmp(when, "weekend"))
2357 {
2358 /*
2359 * Hold to weekend unless we are in the weekend.
2360 */
ef416fc2 2361
b9faaae1
MS
2362 curtime = time(NULL);
2363 curdate = localtime(&curtime);
ef416fc2 2364
b9faaae1
MS
2365 if (curdate->tm_wday == 0 || curdate->tm_wday == 6)
2366 job->hold_until = curtime;
2367 else
2368 job->hold_until = curtime +
2369 (((5 - curdate->tm_wday) * 24 +
2370 (17 - curdate->tm_hour)) * 60 + 59 -
2371 curdate->tm_min) * 60 + 60 - curdate->tm_sec;
2372 }
2373 else if (sscanf(when, "%d:%d:%d", &hour, &minute, &second) >= 2)
2374 {
2375 /*
2376 * Hold to specified GMT time (HH:MM or HH:MM:SS)...
2377 */
bd7854cb 2378
b9faaae1
MS
2379 curtime = time(NULL);
2380 curdate = gmtime(&curtime);
2381
2382 job->hold_until = curtime +
2383 ((hour - curdate->tm_hour) * 60 + minute -
2384 curdate->tm_min) * 60 + second - curdate->tm_sec;
2385
2386 /*
2387 * Hold until next day as needed...
2388 */
2389
2390 if (job->hold_until < curtime)
2391 job->hold_until += 24 * 60 * 60;
ef416fc2 2392 }
2393
b9faaae1
MS
2394 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdSetJobHoldUntil: hold_until=%d",
2395 (int)job->hold_until);
e1d6a774 2396}
ef416fc2 2397
ef416fc2 2398
e1d6a774 2399/*
b9faaae1
MS
2400 * 'cupsdSetJobPriority()' - Set the priority of a job, moving it up/down in
2401 * the list as needed.
e1d6a774 2402 */
ef416fc2 2403
b9faaae1
MS
2404void
2405cupsdSetJobPriority(
2406 cupsd_job_t *job, /* I - Job ID */
2407 int priority) /* I - New priority (0 to 100) */
e1d6a774 2408{
b9faaae1 2409 ipp_attribute_t *attr; /* Job attribute */
ef416fc2 2410
ef416fc2 2411
e1d6a774 2412 /*
b9faaae1 2413 * Don't change completed jobs...
e1d6a774 2414 */
bd7854cb 2415
b9faaae1 2416 if (job->state_value >= IPP_JOB_PROCESSING)
ef416fc2 2417 return;
ef416fc2 2418
e1d6a774 2419 /*
b9faaae1 2420 * Set the new priority and re-add the job into the active list...
e1d6a774 2421 */
ef416fc2 2422
b9faaae1 2423 cupsArrayRemove(ActiveJobs, job);
ef416fc2 2424
b9faaae1 2425 job->priority = priority;
ef416fc2 2426
b9faaae1
MS
2427 if ((attr = ippFindAttribute(job->attrs, "job-priority",
2428 IPP_TAG_INTEGER)) != NULL)
2429 attr->values[0].integer = priority;
2430 else
2431 ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-priority",
2432 priority);
ef416fc2 2433
b9faaae1 2434 cupsArrayAdd(ActiveJobs, job);
bd7854cb 2435
b9faaae1
MS
2436 job->dirty = 1;
2437 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
2438}
bd7854cb 2439
ef416fc2 2440
b9faaae1
MS
2441/*
2442 * 'cupsdSetJobState()' - Set the state of the specified print job.
2443 */
e1d6a774 2444
b9faaae1
MS
2445void
2446cupsdSetJobState(
2447 cupsd_job_t *job, /* I - Job to cancel */
2448 ipp_jstate_t newstate, /* I - New job state */
2449 cupsd_jobaction_t action, /* I - Action to take */
2450 const char *message, /* I - Message to log */
2451 ...) /* I - Additional arguments as needed */
2452{
2453 int i; /* Looping var */
2454 ipp_jstate_t oldstate; /* Old state */
2455 char filename[1024]; /* Job filename */
2456 ipp_attribute_t *attr; /* Job attribute */
ef416fc2 2457
ef416fc2 2458
b9faaae1
MS
2459 cupsdLogMessage(CUPSD_LOG_DEBUG2,
2460 "cupsdSetJobState(job=%p(%d), state=%d, newstate=%d, "
2461 "action=%d, message=\"%s\")", job, job->id, job->state_value,
2462 newstate, action, message ? message : "(null)");
ef416fc2 2463
bd7854cb 2464
b9faaae1
MS
2465 /*
2466 * Make sure we have the job attributes...
2467 */
e1d6a774 2468
b9faaae1
MS
2469 if (!cupsdLoadJob(job))
2470 return;
bd7854cb 2471
f99f3698
MS
2472 /*
2473 * Don't do anything if the state is unchanged and we aren't purging the
2474 * job...
2475 */
2476
2477 oldstate = job->state_value;
2478 if (newstate == oldstate && action != CUPSD_JOB_PURGE)
2479 return;
e1d6a774 2480
b9faaae1
MS
2481 /*
2482 * Stop any processes that are working on the current job...
2483 */
e1d6a774 2484
b9faaae1 2485 if (oldstate == IPP_JOB_PROCESSING)
ef55b745 2486 stop_job(job, action);
b9faaae1
MS
2487
2488 /*
2489 * Set the new job state...
2490 */
2491
0fa6c7fa
MS
2492 job->state_value = newstate;
2493
2494 if (job->state)
2495 job->state->values[0].integer = newstate;
b9faaae1
MS
2496
2497 switch (newstate)
2498 {
2499 case IPP_JOB_PENDING :
2500 /*
2501 * Update job-hold-until as needed...
2502 */
2503
2504 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
2505 IPP_TAG_KEYWORD)) == NULL)
2506 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
2507
2508 if (attr)
ef416fc2 2509 {
b9faaae1
MS
2510 attr->value_tag = IPP_TAG_KEYWORD;
2511 cupsdSetString(&(attr->values[0].string.text), "no-hold");
e1d6a774 2512 }
ef416fc2 2513
b9faaae1
MS
2514 default :
2515 break;
ef416fc2 2516
b9faaae1
MS
2517 case IPP_JOB_ABORTED :
2518 case IPP_JOB_CANCELED :
2519 case IPP_JOB_COMPLETED :
2520 set_time(job, "time-at-completed");
12f89d24 2521 ippSetString(job->attrs, &job->reasons, 0, "processing-to-stop-point");
b9faaae1
MS
2522 break;
2523 }
ef416fc2 2524
b9faaae1
MS
2525 /*
2526 * Log message as needed...
2527 */
ef416fc2 2528
b9faaae1
MS
2529 if (message)
2530 {
2531 char buffer[2048]; /* Message buffer */
2532 va_list ap; /* Pointer to additional arguments */
ef416fc2 2533
b9faaae1
MS
2534 va_start(ap, message);
2535 vsnprintf(buffer, sizeof(buffer), message, ap);
2536 va_end(ap);
bd7854cb 2537
b9faaae1
MS
2538 if (newstate > IPP_JOB_STOPPED)
2539 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job, "%s", buffer);
2540 else
2541 cupsdAddEvent(CUPSD_EVENT_JOB_STATE, job->printer, job, "%s", buffer);
2542
2543 if (newstate == IPP_JOB_STOPPED || newstate == IPP_JOB_ABORTED)
2544 cupsdLogJob(job, CUPSD_LOG_ERROR, "%s", buffer);
2545 else
2546 cupsdLogJob(job, CUPSD_LOG_INFO, "%s", buffer);
2547 }
2548
2549 /*
2550 * Handle post-state-change actions...
2551 */
2552
2553 switch (newstate)
2554 {
2555 case IPP_JOB_PROCESSING :
e1d6a774 2556 /*
b9faaae1 2557 * Add the job to the "printing" list...
e1d6a774 2558 */
bd7854cb 2559
b9faaae1
MS
2560 if (!cupsArrayFind(PrintingJobs, job))
2561 cupsArrayAdd(PrintingJobs, job);
ef416fc2 2562
b9faaae1
MS
2563 /*
2564 * Set the processing time...
2565 */
ef416fc2 2566
b9faaae1
MS
2567 set_time(job, "time-at-processing");
2568
2569 case IPP_JOB_PENDING :
2570 case IPP_JOB_HELD :
2571 case IPP_JOB_STOPPED :
e1d6a774 2572 /*
b9faaae1 2573 * Make sure the job is in the active list...
e1d6a774 2574 */
ef416fc2 2575
b9faaae1
MS
2576 if (!cupsArrayFind(ActiveJobs, job))
2577 cupsArrayAdd(ActiveJobs, job);
ef416fc2 2578
b9faaae1
MS
2579 /*
2580 * Save the job state to disk...
2581 */
ef416fc2 2582
b9faaae1
MS
2583 job->dirty = 1;
2584 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
2585 break;
ef416fc2 2586
b9faaae1
MS
2587 case IPP_JOB_ABORTED :
2588 case IPP_JOB_CANCELED :
2589 case IPP_JOB_COMPLETED :
7a0cbd5e
MS
2590 if (newstate == IPP_JOB_CANCELED)
2591 {
2592 /*
2593 * Remove the job from the active list if there are no processes still
2594 * running for it...
2595 */
ef416fc2 2596
7a0cbd5e
MS
2597 for (i = 0; job->filters[i] < 0; i++);
2598
2599 if (!job->filters[i] && job->backend <= 0)
2600 cupsArrayRemove(ActiveJobs, job);
2601 }
2602 else
2603 {
2604 /*
2605 * Otherwise just remove the job from the active list immediately...
2606 */
2607
2608 cupsArrayRemove(ActiveJobs, job);
2609 }
ef416fc2 2610
b9faaae1 2611 /*
7a0cbd5e 2612 * Expire job subscriptions since the job is now "completed"...
b9faaae1 2613 */
ef416fc2 2614
7a0cbd5e 2615 cupsdExpireSubscriptions(NULL, job);
ef416fc2 2616
b9faaae1
MS
2617#ifdef __APPLE__
2618 /*
2619 * If we are going to sleep and the PrintingJobs count is now 0, allow the
2620 * sleep to happen immediately...
2621 */
ef416fc2 2622
b9faaae1
MS
2623 if (Sleeping && cupsArrayCount(PrintingJobs) == 0)
2624 cupsdAllowSleep();
2625#endif /* __APPLE__ */
ef416fc2 2626
b9faaae1
MS
2627 /*
2628 * Remove any authentication data...
2629 */
bd7854cb 2630
b9faaae1
MS
2631 snprintf(filename, sizeof(filename), "%s/a%05d", RequestRoot, job->id);
2632 if (cupsdRemoveFile(filename) && errno != ENOENT)
2633 cupsdLogMessage(CUPSD_LOG_ERROR,
2634 "Unable to remove authentication cache: %s",
2635 strerror(errno));
bd7854cb 2636
88f9aafc
MS
2637 for (i = 0;
2638 i < (int)(sizeof(job->auth_env) / sizeof(job->auth_env[0]));
2639 i ++)
2640 cupsdClearString(job->auth_env + i);
2641
07ed0e9a 2642 cupsdClearString(&job->auth_uid);
ef416fc2 2643
b9faaae1
MS
2644 /*
2645 * Remove the print file for good if we aren't preserving jobs or
2646 * files...
2647 */
e1d6a774 2648
b9faaae1 2649 if (!JobHistory || !JobFiles || action == CUPSD_JOB_PURGE)
82cc1f9a 2650 remove_job_files(job);
ef416fc2 2651
b9faaae1
MS
2652 if (JobHistory && action != CUPSD_JOB_PURGE)
2653 {
2654 /*
2655 * Save job state info...
2656 */
e1d6a774 2657
b9faaae1
MS
2658 job->dirty = 1;
2659 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
2660 }
ba55dc12
MS
2661 else if (!job->printer)
2662 {
2663 /*
2664 * Delete the job immediately if not actively printing...
2665 */
2666
2667 cupsdDeleteJob(job, CUPSD_JOB_PURGE);
2668 job = NULL;
2669 }
b9faaae1 2670 break;
e1d6a774 2671 }
2672
e07d4801
MS
2673 /*
2674 * Finalize the job immediately if we forced things...
2675 */
2676
ba55dc12 2677 if (action >= CUPSD_JOB_FORCE && job && job->printer)
ef55b745 2678 finalize_job(job, 0);
e07d4801 2679
e1d6a774 2680 /*
b9faaae1 2681 * Update the server "busy" state...
e1d6a774 2682 */
ef416fc2 2683
b9faaae1
MS
2684 cupsdSetBusyState();
2685}
e00b005a 2686
ef416fc2 2687
b9faaae1
MS
2688/*
2689 * 'cupsdStopAllJobs()' - Stop all print jobs.
2690 */
ef416fc2 2691
b9faaae1
MS
2692void
2693cupsdStopAllJobs(
238c3832
MS
2694 cupsd_jobaction_t action, /* I - Action */
2695 int kill_delay) /* I - Number of seconds before we kill */
b9faaae1
MS
2696{
2697 cupsd_job_t *job; /* Current job */
ef416fc2 2698
ef416fc2 2699
b9faaae1 2700 DEBUG_puts("cupsdStopAllJobs()");
ef416fc2 2701
b9faaae1
MS
2702 for (job = (cupsd_job_t *)cupsArrayFirst(PrintingJobs);
2703 job;
2704 job = (cupsd_job_t *)cupsArrayNext(PrintingJobs))
238c3832 2705 {
581dae2d
MS
2706 if (job->completed)
2707 {
2708 cupsdSetJobState(job, IPP_JOB_COMPLETED, CUPSD_JOB_FORCE, NULL);
2709 }
2710 else
2711 {
2712 if (kill_delay)
2713 job->kill_time = time(NULL) + kill_delay;
238c3832 2714
581dae2d
MS
2715 cupsdSetJobState(job, IPP_JOB_PENDING, action, NULL);
2716 }
238c3832 2717 }
b9faaae1 2718}
bd7854cb 2719
bd7854cb 2720
b9faaae1
MS
2721/*
2722 * 'cupsdUnloadCompletedJobs()' - Flush completed job history from memory.
2723 */
ef416fc2 2724
b9faaae1
MS
2725void
2726cupsdUnloadCompletedJobs(void)
2727{
2728 cupsd_job_t *job; /* Current job */
2729 time_t expire; /* Expiration time */
ef416fc2 2730
b9faaae1
MS
2731
2732 expire = time(NULL) - 60;
2733
2734 for (job = (cupsd_job_t *)cupsArrayFirst(Jobs);
2735 job;
2736 job = (cupsd_job_t *)cupsArrayNext(Jobs))
2737 if (job->attrs && job->state_value >= IPP_JOB_STOPPED && !job->printer &&
2738 job->access_time < expire)
2739 {
2740 if (job->dirty)
2741 cupsdSaveJob(job);
2742
2743 unload_job(job);
2744 }
e1d6a774 2745}
ef416fc2 2746
ef416fc2 2747
82cc1f9a
MS
2748/*
2749 * 'cupsdUpdateJobs()' - Update the history/file files for all jobs.
2750 */
2751
2752void
2753cupsdUpdateJobs(void)
2754{
2755 cupsd_job_t *job; /* Current job */
2756 time_t curtime; /* Current time */
2757 ipp_attribute_t *attr; /* time-at-completed attribute */
2758
2759
2760 curtime = time(NULL);
2761 JobHistoryUpdate = 0;
2762
2763 for (job = (cupsd_job_t *)cupsArrayFirst(Jobs);
2764 job;
2765 job = (cupsd_job_t *)cupsArrayNext(Jobs))
2766 {
2767 if (job->state_value >= IPP_JOB_CANCELED &&
2768 (attr = ippFindAttribute(job->attrs, "time-at-completed",
2769 IPP_TAG_INTEGER)) != NULL)
2770 {
2771 /*
2772 * Update history/file expiration times...
2773 */
2774
2775 if (JobHistory < INT_MAX)
2776 job->history_time = attr->values[0].integer + JobHistory;
2777 else
2778 job->history_time = INT_MAX;
2779
2780 if (job->history_time < curtime)
2781 {
2782 cupsdDeleteJob(job, CUPSD_JOB_PURGE);
2783 continue;
2784 }
2785
2786 if (job->history_time < JobHistoryUpdate || !JobHistoryUpdate)
2787 JobHistoryUpdate = job->history_time;
2788
2789 if (JobFiles < INT_MAX)
2790 job->file_time = attr->values[0].integer + JobFiles;
2791 else
2792 job->file_time = INT_MAX;
2793
2794 if (job->file_time < JobHistoryUpdate || !JobHistoryUpdate)
2795 JobHistoryUpdate = job->file_time;
2796 }
2797 }
2798
2799 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdUpdateAllJobs: JobHistoryUpdate=%ld",
2800 (long)JobHistoryUpdate);
2801}
2802
2803
e1d6a774 2804/*
b9faaae1 2805 * 'compare_active_jobs()' - Compare the job IDs and priorities of two jobs.
e1d6a774 2806 */
ef416fc2 2807
b9faaae1
MS
2808static int /* O - Difference */
2809compare_active_jobs(void *first, /* I - First job */
2810 void *second, /* I - Second job */
2811 void *data) /* I - App data (not used) */
e1d6a774 2812{
b9faaae1 2813 int diff; /* Difference */
ef416fc2 2814
ef416fc2 2815
321d8d57
MS
2816 (void)data;
2817
b9faaae1
MS
2818 if ((diff = ((cupsd_job_t *)second)->priority -
2819 ((cupsd_job_t *)first)->priority) != 0)
2820 return (diff);
2821 else
2822 return (((cupsd_job_t *)first)->id - ((cupsd_job_t *)second)->id);
e1d6a774 2823}
bd7854cb 2824
ef416fc2 2825
eec1fbc3
MS
2826/*
2827 * 'compare_completed_jobs()' - Compare the job IDs and completion times of two jobs.
2828 */
2829
2830static int /* O - Difference */
2831compare_completed_jobs(void *first, /* I - First job */
2832 void *second, /* I - Second job */
2833 void *data) /* I - App data (not used) */
2834{
2835 int diff; /* Difference */
2836
2837
2838 (void)data;
2839
2840 if ((diff = ((cupsd_job_t *)second)->completed_time -
2841 ((cupsd_job_t *)first)->completed_time) != 0)
2842 return (diff);
2843 else
2844 return (((cupsd_job_t *)first)->id - ((cupsd_job_t *)second)->id);
2845}
2846
2847
e1d6a774 2848/*
b9faaae1 2849 * 'compare_jobs()' - Compare the job IDs of two jobs.
e1d6a774 2850 */
ef416fc2 2851
b9faaae1
MS
2852static int /* O - Difference */
2853compare_jobs(void *first, /* I - First job */
2854 void *second, /* I - Second job */
2855 void *data) /* I - App data (not used) */
e1d6a774 2856{
321d8d57
MS
2857 (void)data;
2858
b9faaae1
MS
2859 return (((cupsd_job_t *)first)->id - ((cupsd_job_t *)second)->id);
2860}
ef416fc2 2861
ef416fc2 2862
178cb736
MS
2863/*
2864 * 'dump_job_history()' - Dump any debug messages for a job.
2865 */
2866
2867static void
2868dump_job_history(cupsd_job_t *job) /* I - Job */
2869{
2870 int i, /* Looping var */
2871 oldsize; /* Current MaxLogSize */
2872 struct tm *date; /* Date/time value */
2873 cupsd_joblog_t *message; /* Current message */
2874 char temp[2048], /* Log message */
2875 *ptr, /* Pointer into log message */
2876 start[256], /* Start time */
2877 end[256]; /* End time */
2878 cupsd_printer_t *printer; /* Printer for job */
2879
2880
2881 /*
2882 * See if we have anything to dump...
2883 */
2884
2885 if (!job->history)
2886 return;
2887
2888 /*
2889 * Disable log rotation temporarily...
2890 */
2891
2892 oldsize = MaxLogSize;
2893 MaxLogSize = 0;
2894
2895 /*
2896 * Copy the debug messages to the log...
2897 */
2898
2899 message = (cupsd_joblog_t *)cupsArrayFirst(job->history);
2900 date = localtime(&(message->time));
2901 strftime(start, sizeof(start), "%X", date);
2902
2903 message = (cupsd_joblog_t *)cupsArrayLast(job->history);
2904 date = localtime(&(message->time));
2905 strftime(end, sizeof(end), "%X", date);
2906
2907 snprintf(temp, sizeof(temp),
2908 "[Job %d] The following messages were recorded from %s to %s",
2909 job->id, start, end);
2910 cupsdWriteErrorLog(CUPSD_LOG_DEBUG, temp);
2911
2912 for (message = (cupsd_joblog_t *)cupsArrayFirst(job->history);
2913 message;
2914 message = (cupsd_joblog_t *)cupsArrayNext(job->history))
2915 cupsdWriteErrorLog(CUPSD_LOG_DEBUG, message->message);
2916
2917 snprintf(temp, sizeof(temp), "[Job %d] End of messages", job->id);
2918 cupsdWriteErrorLog(CUPSD_LOG_DEBUG, temp);
2919
2920 /*
2921 * Log the printer state values...
2922 */
2923
2924 if ((printer = job->printer) == NULL)
2925 printer = cupsdFindDest(job->dest);
2926
2927 if (printer)
2928 {
2929 snprintf(temp, sizeof(temp), "[Job %d] printer-state=%d(%s)", job->id,
2930 printer->state,
2931 printer->state == IPP_PRINTER_IDLE ? "idle" :
2932 printer->state == IPP_PRINTER_PROCESSING ? "processing" :
2933 "stopped");
2934 cupsdWriteErrorLog(CUPSD_LOG_DEBUG, temp);
2935
2936 snprintf(temp, sizeof(temp), "[Job %d] printer-state-message=\"%s\"",
2937 job->id, printer->state_message);
2938 cupsdWriteErrorLog(CUPSD_LOG_DEBUG, temp);
2939
2940 snprintf(temp, sizeof(temp), "[Job %d] printer-state-reasons=", job->id);
2941 ptr = temp + strlen(temp);
2942 if (printer->num_reasons == 0)
7e86f2f6 2943 strlcpy(ptr, "none", sizeof(temp) - (size_t)(ptr - temp));
178cb736
MS
2944 else
2945 {
2946 for (i = 0;
2947 i < printer->num_reasons && ptr < (temp + sizeof(temp) - 2);
2948 i ++)
2949 {
2950 if (i)
2951 *ptr++ = ',';
2952
7e86f2f6 2953 strlcpy(ptr, printer->reasons[i], sizeof(temp) - (size_t)(ptr - temp));
178cb736
MS
2954 ptr += strlen(ptr);
2955 }
2956 }
2957 cupsdWriteErrorLog(CUPSD_LOG_DEBUG, temp);
2958 }
2959
2960 /*
2961 * Restore log file rotation...
2962 */
2963
2964 MaxLogSize = oldsize;
2965
2966 /*
2967 * Free all messages...
2968 */
2969
2970 free_job_history(job);
2971}
2972
2973
2974/*
2975 * 'free_job_history()' - Free any log history.
2976 */
2977
2978static void
2979free_job_history(cupsd_job_t *job) /* I - Job */
2980{
2981 char *message; /* Current message */
2982
2983
2984 if (!job->history)
2985 return;
2986
2987 for (message = (char *)cupsArrayFirst(job->history);
2988 message;
2989 message = (char *)cupsArrayNext(job->history))
2990 free(message);
2991
2992 cupsArrayDelete(job->history);
2993 job->history = NULL;
2994}
2995
2996
b9faaae1
MS
2997/*
2998 * 'finalize_job()' - Cleanup after job filter processes and support data.
2999 */
ef416fc2 3000
b9faaae1 3001static void
ef55b745
MS
3002finalize_job(cupsd_job_t *job, /* I - Job */
3003 int set_job_state) /* I - 1 = set the job state */
b9faaae1
MS
3004{
3005 ipp_pstate_t printer_state; /* New printer state value */
3006 ipp_jstate_t job_state; /* New job state value */
3007 const char *message; /* Message for job state */
3008 char buffer[1024]; /* Buffer for formatted messages */
e00b005a 3009
b9faaae1
MS
3010
3011 cupsdLogMessage(CUPSD_LOG_DEBUG2, "finalize_job(job=%p(%d))", job, job->id);
ef416fc2 3012
e1d6a774 3013 /*
1e3e80bb
MS
3014 * Clear the "connecting-to-device" and "cups-waiting-for-job-completed"
3015 * reasons, which are only valid when a printer is processing, along with any
3016 * remote printing job state...
e1d6a774 3017 */
ef416fc2 3018
07ed0e9a 3019 cupsdSetPrinterReasons(job->printer, "-connecting-to-device,"
1e3e80bb 3020 "cups-waiting-for-job-completed,"
07ed0e9a
MS
3021 "cups-remote-pending,"
3022 "cups-remote-pending-held,"
3023 "cups-remote-processing,"
3024 "cups-remote-stopped,"
3025 "cups-remote-canceled,"
3026 "cups-remote-aborted,"
3027 "cups-remote-completed");
ef416fc2 3028
e1d6a774 3029 /*
b9faaae1
MS
3030 * Similarly, clear the "offline-report" reason for non-USB devices since we
3031 * rarely have current information for network devices...
e1d6a774 3032 */
ef416fc2 3033
a469f8a5
MS
3034 if (strncmp(job->printer->device_uri, "usb:", 4) &&
3035 strncmp(job->printer->device_uri, "ippusb:", 7))
b9faaae1 3036 cupsdSetPrinterReasons(job->printer, "-offline-report");
ef416fc2 3037
b9faaae1
MS
3038 /*
3039 * Free the security profile...
3040 */
ef416fc2 3041
b9faaae1
MS
3042 cupsdDestroyProfile(job->profile);
3043 job->profile = NULL;
8fe0183a
MS
3044 cupsdDestroyProfile(job->bprofile);
3045 job->bprofile = NULL;
ef416fc2 3046
10d09e33 3047 /*
a469f8a5 3048 * Clear the unresponsive job watchdog timers...
10d09e33
MS
3049 */
3050
a469f8a5
MS
3051 job->cancel_time = 0;
3052 job->kill_time = 0;
10d09e33 3053
b9faaae1
MS
3054 /*
3055 * Close pipes and status buffer...
3056 */
ef416fc2 3057
b9faaae1
MS
3058 cupsdClosePipe(job->print_pipes);
3059 cupsdClosePipe(job->back_pipes);
3060 cupsdClosePipe(job->side_pipes);
ef416fc2 3061
e07d4801
MS
3062 cupsdRemoveSelect(job->status_pipes[0]);
3063 cupsdClosePipe(job->status_pipes);
b9faaae1
MS
3064 cupsdStatBufDelete(job->status_buffer);
3065 job->status_buffer = NULL;
ef416fc2 3066
e1d6a774 3067 /*
b9faaae1 3068 * Process the exit status...
e1d6a774 3069 */
ef416fc2 3070
1340db2d
MS
3071 if (job->printer->state == IPP_PRINTER_PROCESSING)
3072 printer_state = IPP_PRINTER_IDLE;
3073 else
3074 printer_state = job->printer->state;
3075
3076 switch (job_state = job->state_value)
3077 {
3078 case IPP_JOB_PENDING :
3079 message = "Job paused.";
3080 break;
3081
3082 case IPP_JOB_HELD :
3083 message = "Job held.";
3084 break;
3085
3086 default :
3087 case IPP_JOB_PROCESSING :
3088 case IPP_JOB_COMPLETED :
f11a948a
MS
3089 job_state = IPP_JOB_COMPLETED;
3090 message = "Job completed.";
12f89d24 3091
6961465f
MS
3092 if (!job->status)
3093 ippSetString(job->attrs, &job->reasons, 0,
3094 "job-completed-successfully");
1340db2d
MS
3095 break;
3096
3097 case IPP_JOB_STOPPED :
3098 message = "Job stopped.";
12f89d24
MS
3099
3100 ippSetString(job->attrs, &job->reasons, 0, "job-stopped");
1340db2d
MS
3101 break;
3102
3103 case IPP_JOB_CANCELED :
3104 message = "Job canceled.";
12f89d24
MS
3105
3106 ippSetString(job->attrs, &job->reasons, 0, "job-canceled-by-user");
1340db2d
MS
3107 break;
3108
3109 case IPP_JOB_ABORTED :
3110 message = "Job aborted.";
3111 break;
3112 }
ef416fc2 3113
b9faaae1 3114 if (job->status < 0)
e1d6a774 3115 {
3116 /*
b9faaae1 3117 * Backend had errors...
e1d6a774 3118 */
ef416fc2 3119
b9faaae1
MS
3120 int exit_code; /* Exit code from backend */
3121
e1d6a774 3122 /*
b9faaae1
MS
3123 * Convert the status to an exit code. Due to the way the W* macros are
3124 * implemented on MacOS X (bug?), we have to store the exit status in a
3125 * variable first and then convert...
e1d6a774 3126 */
ef416fc2 3127
b9faaae1
MS
3128 exit_code = -job->status;
3129 if (WIFEXITED(exit_code))
3130 exit_code = WEXITSTATUS(exit_code);
3131 else
12f89d24
MS
3132 {
3133 ippSetString(job->attrs, &job->reasons, 0, "cups-backend-crashed");
b9faaae1 3134 exit_code = job->status;
12f89d24 3135 }
ef416fc2 3136
b9faaae1
MS
3137 cupsdLogJob(job, CUPSD_LOG_INFO, "Backend returned status %d (%s)",
3138 exit_code,
3139 exit_code == CUPS_BACKEND_FAILED ? "failed" :
3140 exit_code == CUPS_BACKEND_AUTH_REQUIRED ?
3141 "authentication required" :
3142 exit_code == CUPS_BACKEND_HOLD ? "hold job" :
3143 exit_code == CUPS_BACKEND_STOP ? "stop printer" :
3144 exit_code == CUPS_BACKEND_CANCEL ? "cancel job" :
a469f8a5
MS
3145 exit_code == CUPS_BACKEND_RETRY ? "retry job later" :
3146 exit_code == CUPS_BACKEND_RETRY_CURRENT ? "retry job immediately" :
b9faaae1 3147 exit_code < 0 ? "crashed" : "unknown");
ef416fc2 3148
ef416fc2 3149 /*
b9faaae1 3150 * Do what needs to be done...
ef416fc2 3151 */
3152
b9faaae1 3153 switch (exit_code)
f7deaa1a 3154 {
b9faaae1
MS
3155 default :
3156 case CUPS_BACKEND_FAILED :
3157 /*
3158 * Backend failure, use the error-policy to determine how to
3159 * act...
3160 */
f7deaa1a 3161
a2326b5b 3162 if (job->dtype & CUPS_PRINTER_CLASS)
b9faaae1
MS
3163 {
3164 /*
3165 * Queued on a class - mark the job as pending and we'll retry on
3166 * another printer...
3167 */
f7deaa1a 3168
8b116e60
MS
3169 if (job_state == IPP_JOB_COMPLETED)
3170 {
3171 job_state = IPP_JOB_PENDING;
3172 message = "Retrying job on another printer.";
12f89d24
MS
3173
3174 ippSetString(job->attrs, &job->reasons, 0,
3175 "resources-are-not-ready");
8b116e60 3176 }
b9faaae1
MS
3177 }
3178 else if (!strcmp(job->printer->error_policy, "retry-current-job"))
3179 {
3180 /*
3181 * The error policy is "retry-current-job" - mark the job as pending
3182 * and we'll retry on the same printer...
3183 */
f7deaa1a 3184
8b116e60
MS
3185 if (job_state == IPP_JOB_COMPLETED)
3186 {
3187 job_state = IPP_JOB_PENDING;
3188 message = "Retrying job on same printer.";
12f89d24
MS
3189
3190 ippSetString(job->attrs, &job->reasons, 0, "none");
8b116e60 3191 }
b9faaae1
MS
3192 }
3193 else if ((job->printer->type & CUPS_PRINTER_FAX) ||
3194 !strcmp(job->printer->error_policy, "retry-job"))
3195 {
8b116e60 3196 if (job_state == IPP_JOB_COMPLETED)
b9faaae1
MS
3197 {
3198 /*
8b116e60
MS
3199 * The job was queued on a fax or the error policy is "retry-job" -
3200 * hold the job if the number of retries is less than the
3201 * JobRetryLimit, otherwise abort the job.
b9faaae1 3202 */
ef416fc2 3203
8b116e60
MS
3204 job->tries ++;
3205
7cf5915e 3206 if (job->tries > JobRetryLimit && JobRetryLimit > 0)
8b116e60
MS
3207 {
3208 /*
3209 * Too many tries...
3210 */
3211
3212 snprintf(buffer, sizeof(buffer),
3213 "Job aborted after %d unsuccessful attempts.",
3214 JobRetryLimit);
3215 job_state = IPP_JOB_ABORTED;
3216 message = buffer;
12f89d24
MS
3217
3218 ippSetString(job->attrs, &job->reasons, 0, "aborted-by-system");
8b116e60
MS
3219 }
3220 else
3221 {
3222 /*
3223 * Try again in N seconds...
3224 */
ef416fc2 3225
8b116e60
MS
3226 snprintf(buffer, sizeof(buffer),
3227 "Job held for %d seconds since it could not be sent.",
3228 JobRetryInterval);
0268488e
MS
3229
3230 job->hold_until = time(NULL) + JobRetryInterval;
3231 job_state = IPP_JOB_HELD;
3232 message = buffer;
12f89d24
MS
3233
3234 ippSetString(job->attrs, &job->reasons, 0,
3235 "resources-are-not-ready");
8b116e60
MS
3236 }
3237 }
b9faaae1 3238 }
8b116e60
MS
3239 else if (!strcmp(job->printer->error_policy, "abort-job") &&
3240 job_state == IPP_JOB_COMPLETED)
b9faaae1
MS
3241 {
3242 job_state = IPP_JOB_ABORTED;
3243 message = "Job aborted due to backend errors; please consult "
3244 "the error_log file for details.";
12f89d24
MS
3245
3246 ippSetString(job->attrs, &job->reasons, 0, "aborted-by-system");
b9faaae1 3247 }
f11a948a 3248 else if (job->state_value == IPP_JOB_PROCESSING)
b9faaae1 3249 {
f11a948a 3250 job_state = IPP_JOB_PENDING;
b9faaae1 3251 printer_state = IPP_PRINTER_STOPPED;
b9faaae1 3252 message = "Printer stopped due to backend errors; please "
8b116e60 3253 "consult the error_log file for details.";
12f89d24
MS
3254
3255 ippSetString(job->attrs, &job->reasons, 0, "none");
b9faaae1
MS
3256 }
3257 break;
bd7854cb 3258
b9faaae1
MS
3259 case CUPS_BACKEND_CANCEL :
3260 /*
f3c17241 3261 * Cancel the job...
b9faaae1 3262 */
bd7854cb 3263
8b116e60
MS
3264 if (job_state == IPP_JOB_COMPLETED)
3265 {
f3c17241
MS
3266 job_state = IPP_JOB_CANCELED;
3267 message = "Job canceled at printer.";
12f89d24 3268
f3c17241 3269 ippSetString(job->attrs, &job->reasons, 0, "canceled-at-device");
8b116e60 3270 }
b9faaae1 3271 break;
bd7854cb 3272
b9faaae1 3273 case CUPS_BACKEND_HOLD :
8b116e60
MS
3274 if (job_state == IPP_JOB_COMPLETED)
3275 {
3276 /*
3277 * Hold the job...
3278 */
bd7854cb 3279
6961465f 3280 const char *reason = ippGetString(job->reasons, 0, NULL);
bd7854cb 3281
6961465f
MS
3282 cupsdLogJob(job, CUPSD_LOG_DEBUG, "job-state-reasons=\"%s\"",
3283 reason);
3284
3285 if (!reason || strncmp(reason, "account-", 8))
3286 {
3287 cupsdSetJobHoldUntil(job, "indefinite", 1);
3288
3289 ippSetString(job->attrs, &job->reasons, 0,
3290 "job-hold-until-specified");
3291 message = "Job held indefinitely due to backend errors; please "
8b116e60 3292 "consult the error_log file for details.";
6961465f
MS
3293 }
3294 else if (!strcmp(reason, "account-info-needed"))
3295 {
3296 cupsdSetJobHoldUntil(job, "indefinite", 0);
3297
3298 message = "Job held indefinitely - account information is "
3299 "required.";
3300 }
3301 else if (!strcmp(reason, "account-closed"))
3302 {
3303 cupsdSetJobHoldUntil(job, "indefinite", 0);
3304
3305 message = "Job held indefinitely - account has been closed.";
3306 }
3307 else if (!strcmp(reason, "account-limit-reached"))
3308 {
3309 cupsdSetJobHoldUntil(job, "indefinite", 0);
3310
3311 message = "Job held indefinitely - account limit has been "
3312 "reached.";
3313 }
3314 else
3315 {
3316 cupsdSetJobHoldUntil(job, "indefinite", 0);
3317
3318 message = "Job held indefinitely - account authorization failed.";
3319 }
3320
3321 job_state = IPP_JOB_HELD;
8b116e60 3322 }
b9faaae1 3323 break;
2abf387c 3324
b9faaae1
MS
3325 case CUPS_BACKEND_STOP :
3326 /*
3327 * Stop the printer...
3328 */
bd7854cb 3329
b9faaae1 3330 printer_state = IPP_PRINTER_STOPPED;
b9faaae1
MS
3331 message = "Printer stopped due to backend errors; please "
3332 "consult the error_log file for details.";
8b116e60
MS
3333
3334 if (job_state == IPP_JOB_COMPLETED)
12f89d24 3335 {
8b116e60 3336 job_state = IPP_JOB_PENDING;
12f89d24
MS
3337
3338 ippSetString(job->attrs, &job->reasons, 0,
3339 "resources-are-not-ready");
3340 }
b9faaae1 3341 break;
bd7854cb 3342
b9faaae1
MS
3343 case CUPS_BACKEND_AUTH_REQUIRED :
3344 /*
3345 * Hold the job for authentication...
3346 */
bd7854cb 3347
8b116e60
MS
3348 if (job_state == IPP_JOB_COMPLETED)
3349 {
3350 cupsdSetJobHoldUntil(job, "auth-info-required", 1);
ef416fc2 3351
8b116e60
MS
3352 job_state = IPP_JOB_HELD;
3353 message = "Job held for authentication.";
12f89d24 3354
a469f8a5
MS
3355 if (strncmp(job->reasons->values[0].string.text, "account-", 8))
3356 ippSetString(job->attrs, &job->reasons, 0,
3357 "cups-held-for-authentication");
8b116e60 3358 }
b9faaae1 3359 break;
22c9029b
MS
3360
3361 case CUPS_BACKEND_RETRY :
3362 if (job_state == IPP_JOB_COMPLETED)
3363 {
3364 /*
3365 * Hold the job if the number of retries is less than the
3366 * JobRetryLimit, otherwise abort the job.
3367 */
3368
3369 job->tries ++;
3370
3371 if (job->tries > JobRetryLimit && JobRetryLimit > 0)
3372 {
3373 /*
3374 * Too many tries...
3375 */
3376
3377 snprintf(buffer, sizeof(buffer),
3378 "Job aborted after %d unsuccessful attempts.",
3379 JobRetryLimit);
3380 job_state = IPP_JOB_ABORTED;
3381 message = buffer;
12f89d24
MS
3382
3383 ippSetString(job->attrs, &job->reasons, 0, "aborted-by-system");
22c9029b
MS
3384 }
3385 else
3386 {
3387 /*
3388 * Try again in N seconds...
3389 */
3390
3391 snprintf(buffer, sizeof(buffer),
3392 "Job held for %d seconds since it could not be sent.",
3393 JobRetryInterval);
3394
3395 job->hold_until = time(NULL) + JobRetryInterval;
3396 job_state = IPP_JOB_HELD;
3397 message = buffer;
12f89d24
MS
3398
3399 ippSetString(job->attrs, &job->reasons, 0,
3400 "resources-are-not-ready");
22c9029b
MS
3401 }
3402 }
3403 break;
3404
3405 case CUPS_BACKEND_RETRY_CURRENT :
3406 /*
3407 * Mark the job as pending and retry on the same printer...
3408 */
3409
3410 if (job_state == IPP_JOB_COMPLETED)
3411 {
3412 job_state = IPP_JOB_PENDING;
3413 message = "Retrying job on same printer.";
12f89d24
MS
3414
3415 ippSetString(job->attrs, &job->reasons, 0, "none");
22c9029b
MS
3416 }
3417 break;
e1d6a774 3418 }
3419 }
b9faaae1 3420 else if (job->status > 0)
ef416fc2 3421 {
3422 /*
b9faaae1 3423 * Filter had errors; stop job...
ef416fc2 3424 */
3425
8b116e60
MS
3426 if (job_state == IPP_JOB_COMPLETED)
3427 {
3428 job_state = IPP_JOB_STOPPED;
3429 message = "Job stopped due to filter errors; please consult the "
3430 "error_log file for details.";
12f89d24
MS
3431
3432 if (WIFSIGNALED(job->status))
3433 ippSetString(job->attrs, &job->reasons, 0, "cups-filter-crashed");
3434 else
3435 ippSetString(job->attrs, &job->reasons, 0, "job-completed-with-errors");
8b116e60 3436 }
e1d6a774 3437 }
e00b005a 3438
3dfe78b3 3439 /*
b9faaae1 3440 * Update the printer and job state.
3dfe78b3
MS
3441 */
3442
321d8d57 3443 if (set_job_state && job_state != job->state_value)
ef55b745
MS
3444 cupsdSetJobState(job, job_state, CUPSD_JOB_DEFAULT, "%s", message);
3445
b9faaae1
MS
3446 cupsdSetPrinterState(job->printer, printer_state,
3447 printer_state == IPP_PRINTER_STOPPED);
3448 update_job_attrs(job, 0);
3dfe78b3 3449
178cb736
MS
3450 if (job->history)
3451 {
a4845881
MS
3452 if (job->status &&
3453 (job->state_value == IPP_JOB_ABORTED ||
3454 job->state_value == IPP_JOB_STOPPED))
178cb736
MS
3455 dump_job_history(job);
3456 else
3457 free_job_history(job);
3458 }
3459
b9faaae1 3460 cupsArrayRemove(PrintingJobs, job);
3dfe78b3 3461
a29fd7dd
MS
3462 /*
3463 * Apply any PPD updates...
3464 */
3465
3466 if (job->num_keywords)
3467 {
3468 if (cupsdUpdatePrinterPPD(job->printer, job->num_keywords, job->keywords))
3469 cupsdSetPrinterAttrs(job->printer);
3470
3471 cupsFreeOptions(job->num_keywords, job->keywords);
3472
3473 job->num_keywords = 0;
3474 job->keywords = NULL;
3475 }
3476
e1d6a774 3477 /*
b9faaae1 3478 * Clear the printer <-> job association...
e1d6a774 3479 */
ef416fc2 3480
b9faaae1
MS
3481 job->printer->job = NULL;
3482 job->printer = NULL;
ef416fc2 3483
e1d6a774 3484 /*
b9faaae1 3485 * Try printing another job...
e1d6a774 3486 */
ef416fc2 3487
b9faaae1
MS
3488 if (printer_state != IPP_PRINTER_STOPPED)
3489 cupsdCheckJobs();
3490}
ef416fc2 3491
ef416fc2 3492
b9faaae1
MS
3493/*
3494 * 'get_options()' - Get a string containing the job options.
3495 */
3496
3497static char * /* O - Options string */
3498get_options(cupsd_job_t *job, /* I - Job */
3499 int banner_page, /* I - Printing a banner page? */
3500 char *copies, /* I - Copies buffer */
3501 size_t copies_size, /* I - Size of copies buffer */
3502 char *title, /* I - Title buffer */
3503 size_t title_size) /* I - Size of title buffer */
3504{
3505 int i; /* Looping var */
c7017ecc 3506 size_t newlength; /* New option buffer length */
b9faaae1
MS
3507 char *optptr, /* Pointer to options */
3508 *valptr; /* Pointer in value string */
3509 ipp_attribute_t *attr; /* Current attribute */
f14324a7 3510 _ppd_cache_t *pc; /* PPD cache and mapping data */
c7017ecc
MS
3511 int num_pwgppds; /* Number of PWG->PPD options */
3512 cups_option_t *pwgppds, /* PWG->PPD options */
3513 *pwgppd, /* Current PWG->PPD option */
3514 *preset; /* Current preset option */
f14324a7
MS
3515 int print_color_mode,
3516 /* Output mode (if any) */
c7017ecc
MS
3517 print_quality; /* Print quality (if any) */
3518 const char *ppd; /* PPD option choice */
3519 int exact; /* Did we get an exact match? */
b9faaae1 3520 static char *options = NULL;/* Full list of options */
c7017ecc 3521 static size_t optlength = 0; /* Length of option buffer */
b9faaae1 3522
ef416fc2 3523
e1d6a774 3524 /*
c7017ecc
MS
3525 * Building the options string is harder than it needs to be, but for the
3526 * moment we need to pass strings for command-line args and not IPP attribute
3527 * pointers... :)
e1d6a774 3528 *
c7017ecc 3529 * First build an options array for any PWG->PPD mapped option/choice pairs.
e1d6a774 3530 */
ef416fc2 3531
f14324a7 3532 pc = job->printer->pc;
c7017ecc
MS
3533 num_pwgppds = 0;
3534 pwgppds = NULL;
ef416fc2 3535
f14324a7 3536 if (pc &&
7cf5915e
MS
3537 !ippFindAttribute(job->attrs,
3538 "com.apple.print.DocumentTicket.PMSpoolFormat",
3539 IPP_TAG_ZERO) &&
0268488e 3540 !ippFindAttribute(job->attrs, "APPrinterPreset", IPP_TAG_ZERO) &&
5a9febac 3541 (ippFindAttribute(job->attrs, "print-color-mode", IPP_TAG_ZERO) ||
7cf5915e 3542 ippFindAttribute(job->attrs, "print-quality", IPP_TAG_ZERO)))
c7017ecc 3543 {
7cf5915e 3544 /*
5a9febac 3545 * Map print-color-mode and print-quality to a preset...
7cf5915e
MS
3546 */
3547
f14324a7 3548 if ((attr = ippFindAttribute(job->attrs, "print-color-mode",
5a9febac
MS
3549 IPP_TAG_KEYWORD)) != NULL &&
3550 !strcmp(attr->values[0].string.text, "monochrome"))
f14324a7 3551 print_color_mode = _PWG_PRINT_COLOR_MODE_MONOCHROME;
c7017ecc 3552 else
f14324a7 3553 print_color_mode = _PWG_PRINT_COLOR_MODE_COLOR;
c7017ecc
MS
3554
3555 if ((attr = ippFindAttribute(job->attrs, "print-quality",
10d09e33 3556 IPP_TAG_ENUM)) != NULL &&
c7017ecc
MS
3557 attr->values[0].integer >= IPP_QUALITY_DRAFT &&
3558 attr->values[0].integer <= IPP_QUALITY_HIGH)
3559 print_quality = attr->values[0].integer - IPP_QUALITY_DRAFT;
3560 else
030ae6a1 3561 print_quality = _PWG_PRINT_QUALITY_NORMAL;
c7017ecc 3562
f14324a7 3563 if (pc->num_presets[print_color_mode][print_quality] == 0)
c7017ecc
MS
3564 {
3565 /*
3566 * Try to find a preset that works so that we maximize the chances of us
3567 * getting a good print using IPP attributes.
3568 */
3569
f14324a7 3570 if (pc->num_presets[print_color_mode][_PWG_PRINT_QUALITY_NORMAL] > 0)
c7017ecc 3571 print_quality = _PWG_PRINT_QUALITY_NORMAL;
f14324a7
MS
3572 else if (pc->num_presets[_PWG_PRINT_COLOR_MODE_COLOR][print_quality] > 0)
3573 print_color_mode = _PWG_PRINT_COLOR_MODE_COLOR;
c7017ecc
MS
3574 else
3575 {
f14324a7
MS
3576 print_quality = _PWG_PRINT_QUALITY_NORMAL;
3577 print_color_mode = _PWG_PRINT_COLOR_MODE_COLOR;
c7017ecc
MS
3578 }
3579 }
3580
f14324a7 3581 if (pc->num_presets[print_color_mode][print_quality] > 0)
c7017ecc
MS
3582 {
3583 /*
3584 * Copy the preset options as long as the corresponding names are not
3585 * already defined in the IPP request...
3586 */
3587
f14324a7
MS
3588 for (i = pc->num_presets[print_color_mode][print_quality],
3589 preset = pc->presets[print_color_mode][print_quality];
c7017ecc
MS
3590 i > 0;
3591 i --, preset ++)
3592 {
3593 if (!ippFindAttribute(job->attrs, preset->name, IPP_TAG_ZERO))
3594 num_pwgppds = cupsAddOption(preset->name, preset->value, num_pwgppds,
3595 &pwgppds);
3596 }
3597 }
7cf5915e 3598 }
c7017ecc 3599
f14324a7 3600 if (pc)
7cf5915e 3601 {
4220952d
MS
3602 if (!ippFindAttribute(job->attrs, "InputSlot", IPP_TAG_ZERO) &&
3603 !ippFindAttribute(job->attrs, "HPPaperSource", IPP_TAG_ZERO))
3604 {
f14324a7
MS
3605 if ((ppd = _ppdCacheGetInputSlot(pc, job->attrs, NULL)) != NULL)
3606 num_pwgppds = cupsAddOption(pc->source_option, ppd, num_pwgppds,
4220952d 3607 &pwgppds);
4220952d 3608 }
4220952d 3609 if (!ippFindAttribute(job->attrs, "MediaType", IPP_TAG_ZERO) &&
f14324a7 3610 (ppd = _ppdCacheGetMediaType(pc, job->attrs, NULL)) != NULL)
4220952d 3611 num_pwgppds = cupsAddOption("MediaType", ppd, num_pwgppds, &pwgppds);
c7017ecc 3612
4220952d
MS
3613 if (!ippFindAttribute(job->attrs, "PageRegion", IPP_TAG_ZERO) &&
3614 !ippFindAttribute(job->attrs, "PageSize", IPP_TAG_ZERO) &&
f14324a7 3615 (ppd = _ppdCacheGetPageSize(pc, job->attrs, NULL, &exact)) != NULL)
030ae6a1 3616 {
4220952d 3617 num_pwgppds = cupsAddOption("PageSize", ppd, num_pwgppds, &pwgppds);
c7017ecc 3618
030ae6a1
MS
3619 if (!ippFindAttribute(job->attrs, "media", IPP_TAG_ZERO))
3620 num_pwgppds = cupsAddOption("media", ppd, num_pwgppds, &pwgppds);
3621 }
3622
4220952d
MS
3623 if (!ippFindAttribute(job->attrs, "OutputBin", IPP_TAG_ZERO) &&
3624 (attr = ippFindAttribute(job->attrs, "output-bin",
3625 IPP_TAG_ZERO)) != NULL &&
3626 (attr->value_tag == IPP_TAG_KEYWORD ||
3627 attr->value_tag == IPP_TAG_NAME) &&
f14324a7 3628 (ppd = _ppdCacheGetOutputBin(pc, attr->values[0].string.text)) != NULL)
0268488e
MS
3629 {
3630 /*
3631 * Map output-bin to OutputBin option...
3632 */
3633
4220952d 3634 num_pwgppds = cupsAddOption("OutputBin", ppd, num_pwgppds, &pwgppds);
0268488e
MS
3635 }
3636
f14324a7
MS
3637 if (pc->sides_option &&
3638 !ippFindAttribute(job->attrs, pc->sides_option, IPP_TAG_ZERO) &&
0268488e
MS
3639 (attr = ippFindAttribute(job->attrs, "sides", IPP_TAG_KEYWORD)) != NULL)
3640 {
3641 /*
3642 * Map sides to duplex option...
3643 */
3644
3645 if (!strcmp(attr->values[0].string.text, "one-sided"))
f14324a7 3646 num_pwgppds = cupsAddOption(pc->sides_option, pc->sides_1sided,
0268488e
MS
3647 num_pwgppds, &pwgppds);
3648 else if (!strcmp(attr->values[0].string.text, "two-sided-long-edge"))
f14324a7 3649 num_pwgppds = cupsAddOption(pc->sides_option, pc->sides_2sided_long,
0268488e
MS
3650 num_pwgppds, &pwgppds);
3651 else if (!strcmp(attr->values[0].string.text, "two-sided-short-edge"))
f14324a7 3652 num_pwgppds = cupsAddOption(pc->sides_option, pc->sides_2sided_short,
0268488e
MS
3653 num_pwgppds, &pwgppds);
3654 }
dcb445bc
MS
3655
3656 /*
3657 * Map finishings values...
3658 */
3659
3660 num_pwgppds = _ppdCacheGetFinishingOptions(pc, job->attrs,
3661 IPP_FINISHINGS_NONE, num_pwgppds,
3662 &pwgppds);
4220952d 3663 }
c7017ecc
MS
3664
3665 /*
3666 * Figure out how much room we need...
3667 */
3668
3669 newlength = ipp_length(job->attrs);
3670
3671 for (i = num_pwgppds, pwgppd = pwgppds; i > 0; i --, pwgppd ++)
3672 newlength += 1 + strlen(pwgppd->name) + 1 + strlen(pwgppd->value);
3673
3674 /*
3675 * Then allocate/reallocate the option buffer as needed...
3676 */
3677
85dda01c
MS
3678 if (newlength == 0) /* This can never happen, but Clang */
3679 newlength = 1; /* thinks it can... */
3680
c7017ecc 3681 if (newlength > optlength || !options)
e1d6a774 3682 {
b9faaae1 3683 if (!options)
c7017ecc 3684 optptr = malloc(newlength);
e1d6a774 3685 else
c7017ecc 3686 optptr = realloc(options, newlength);
ef416fc2 3687
b9faaae1 3688 if (!optptr)
e1d6a774 3689 {
75bd9771 3690 cupsdLogJob(job, CUPSD_LOG_CRIT,
12f89d24 3691 "Unable to allocate " CUPS_LLFMT " bytes for option buffer.",
c7017ecc 3692 CUPS_LLCAST newlength);
b9faaae1 3693 return (NULL);
e1d6a774 3694 }
ef416fc2 3695
e1d6a774 3696 options = optptr;
c7017ecc 3697 optlength = newlength;
e1d6a774 3698 }
ef416fc2 3699
e1d6a774 3700 /*
3701 * Now loop through the attributes and convert them to the textual
3702 * representation used by the filters...
3703 */
ef416fc2 3704
e1d6a774 3705 optptr = options;
3706 *optptr = '\0';
bd7854cb 3707
1340db2d
MS
3708 snprintf(title, title_size, "%s-%d", job->printer->name, job->id);
3709 strlcpy(copies, "1", copies_size);
bd7854cb 3710
e1d6a774 3711 for (attr = job->attrs->attrs; attr != NULL; attr = attr->next)
bd7854cb 3712 {
e1d6a774 3713 if (!strcmp(attr->name, "copies") &&
3714 attr->value_tag == IPP_TAG_INTEGER)
3715 {
3716 /*
3717 * Don't use the # copies attribute if we are printing the job sheets...
3718 */
bd7854cb 3719
e1d6a774 3720 if (!banner_page)
1340db2d 3721 snprintf(copies, copies_size, "%d", attr->values[0].integer);
e1d6a774 3722 }
3723 else if (!strcmp(attr->name, "job-name") &&
3724 (attr->value_tag == IPP_TAG_NAME ||
3725 attr->value_tag == IPP_TAG_NAMELANG))
1340db2d 3726 strlcpy(title, attr->values[0].string.text, title_size);
e1d6a774 3727 else if (attr->group_tag == IPP_TAG_JOB)
3728 {
3729 /*
3730 * Filter out other unwanted attributes...
3731 */
bd7854cb 3732
c7017ecc
MS
3733 if (attr->value_tag == IPP_TAG_NOVALUE ||
3734 attr->value_tag == IPP_TAG_MIMETYPE ||
e1d6a774 3735 attr->value_tag == IPP_TAG_NAMELANG ||
3736 attr->value_tag == IPP_TAG_TEXTLANG ||
db8b865d
MS
3737 (attr->value_tag == IPP_TAG_URI && strcmp(attr->name, "job-uuid") &&
3738 strcmp(attr->name, "job-authorization-uri")) ||
e1d6a774 3739 attr->value_tag == IPP_TAG_URISCHEME ||
3740 attr->value_tag == IPP_TAG_BEGIN_COLLECTION) /* Not yet supported */
3741 continue;
bd7854cb 3742
5a9febac
MS
3743 if (!strcmp(attr->name, "job-hold-until") ||
3744 !strcmp(attr->name, "job-id") ||
3745 !strcmp(attr->name, "job-k-octets") ||
3746 !strcmp(attr->name, "job-media-sheets") ||
3747 !strcmp(attr->name, "job-media-sheets-completed") ||
3748 !strcmp(attr->name, "job-state") ||
3749 !strcmp(attr->name, "job-state-reasons"))
e1d6a774 3750 continue;
bd7854cb 3751
e1d6a774 3752 if (!strncmp(attr->name, "job-", 4) &&
5a9febac
MS
3753 strcmp(attr->name, "job-account-id") &&
3754 strcmp(attr->name, "job-accounting-user-id") &&
a469f8a5 3755 strcmp(attr->name, "job-authorization-uri") &&
e4572d57 3756 strcmp(attr->name, "job-billing") &&
5d6412a9 3757 strcmp(attr->name, "job-impressions") &&
c5571a1d 3758 strcmp(attr->name, "job-originating-host-name") &&
5a9febac 3759 strcmp(attr->name, "job-password") &&
a469f8a5 3760 strcmp(attr->name, "job-password-encryption") &&
e4572d57 3761 strcmp(attr->name, "job-uuid") &&
b9faaae1 3762 !(job->printer->type & CUPS_PRINTER_REMOTE))
e1d6a774 3763 continue;
ef416fc2 3764
5d6412a9
MS
3765 if ((!strcmp(attr->name, "job-impressions") ||
3766 !strcmp(attr->name, "page-label") ||
e1d6a774 3767 !strcmp(attr->name, "page-border") ||
3768 !strncmp(attr->name, "number-up", 9) ||
b94498cf 3769 !strcmp(attr->name, "page-ranges") ||
e1d6a774 3770 !strcmp(attr->name, "page-set") ||
88f9aafc
MS
3771 !_cups_strcasecmp(attr->name, "AP_FIRSTPAGE_InputSlot") ||
3772 !_cups_strcasecmp(attr->name, "AP_FIRSTPAGE_ManualFeed") ||
3773 !_cups_strcasecmp(attr->name, "com.apple.print.PrintSettings."
db1f069b 3774 "PMTotalSidesImaged..n.") ||
88f9aafc 3775 !_cups_strcasecmp(attr->name, "com.apple.print.PrintSettings."
db1f069b 3776 "PMTotalBeginPages..n.")) &&
e1d6a774 3777 banner_page)
3778 continue;
ef416fc2 3779
e1d6a774 3780 /*
3781 * Otherwise add them to the list...
3782 */
ef416fc2 3783
e1d6a774 3784 if (optptr > options)
7e86f2f6 3785 strlcat(optptr, " ", optlength - (size_t)(optptr - options));
ef416fc2 3786
e1d6a774 3787 if (attr->value_tag != IPP_TAG_BOOLEAN)
3788 {
7e86f2f6
MS
3789 strlcat(optptr, attr->name, optlength - (size_t)(optptr - options));
3790 strlcat(optptr, "=", optlength - (size_t)(optptr - options));
e1d6a774 3791 }
ef416fc2 3792
e1d6a774 3793 for (i = 0; i < attr->num_values; i ++)
3794 {
3795 if (i)
7e86f2f6 3796 strlcat(optptr, ",", optlength - (size_t)(optptr - options));
ef416fc2 3797
e1d6a774 3798 optptr += strlen(optptr);
ef416fc2 3799
e1d6a774 3800 switch (attr->value_tag)
3801 {
3802 case IPP_TAG_INTEGER :
3803 case IPP_TAG_ENUM :
7e86f2f6 3804 snprintf(optptr, optlength - (size_t)(optptr - options),
e1d6a774 3805 "%d", attr->values[i].integer);
3806 break;
ef416fc2 3807
e1d6a774 3808 case IPP_TAG_BOOLEAN :
3809 if (!attr->values[i].boolean)
7e86f2f6 3810 strlcat(optptr, "no", optlength - (size_t)(optptr - options));
ef416fc2 3811
7e86f2f6 3812 strlcat(optptr, attr->name, optlength - (size_t)(optptr - options));
4220952d
MS
3813 break;
3814
e1d6a774 3815 case IPP_TAG_RANGE :
3816 if (attr->values[i].range.lower == attr->values[i].range.upper)
7e86f2f6 3817 snprintf(optptr, optlength - (size_t)(optptr - options) - 1,
e1d6a774 3818 "%d", attr->values[i].range.lower);
3819 else
7e86f2f6 3820 snprintf(optptr, optlength - (size_t)(optptr - options) - 1,
e1d6a774 3821 "%d-%d", attr->values[i].range.lower,
3822 attr->values[i].range.upper);
3823 break;
ef416fc2 3824
e1d6a774 3825 case IPP_TAG_RESOLUTION :
7e86f2f6 3826 snprintf(optptr, optlength - (size_t)(optptr - options) - 1,
e1d6a774 3827 "%dx%d%s", attr->values[i].resolution.xres,
3828 attr->values[i].resolution.yres,
3829 attr->values[i].resolution.units == IPP_RES_PER_INCH ?
3e7fe0ca 3830 "dpi" : "dpcm");
e1d6a774 3831 break;
ef416fc2 3832
e1d6a774 3833 case IPP_TAG_STRING :
3834 case IPP_TAG_TEXT :
3835 case IPP_TAG_NAME :
3836 case IPP_TAG_KEYWORD :
3837 case IPP_TAG_CHARSET :
3838 case IPP_TAG_LANGUAGE :
3839 case IPP_TAG_URI :
3840 for (valptr = attr->values[i].string.text; *valptr;)
3841 {
3842 if (strchr(" \t\n\\\'\"", *valptr))
3843 *optptr++ = '\\';
3844 *optptr++ = *valptr++;
3845 }
3846
3847 *optptr = '\0';
3848 break;
3849
3850 default :
3851 break; /* anti-compiler-warning-code */
3852 }
3853 }
3854
3855 optptr += strlen(optptr);
3856 }
3857 }
3858
c7017ecc
MS
3859 /*
3860 * Finally loop through the PWG->PPD mapped options and add them...
3861 */
3862
3863 for (i = num_pwgppds, pwgppd = pwgppds; i > 0; i --, pwgppd ++)
3864 {
3865 *optptr++ = ' ';
7e86f2f6 3866 strlcpy(optptr, pwgppd->name, optlength - (size_t)(optptr - options));
c7017ecc
MS
3867 optptr += strlen(optptr);
3868 *optptr++ = '=';
7e86f2f6 3869 strlcpy(optptr, pwgppd->value, optlength - (size_t)(optptr - options));
c7017ecc
MS
3870 optptr += strlen(optptr);
3871 }
3872
3873 cupsFreeOptions(num_pwgppds, pwgppds);
3874
3875 /*
3876 * Return the options string...
3877 */
b9faaae1
MS
3878
3879 return (options);
3880}
3881
3882
3883/*
3884 * 'ipp_length()' - Compute the size of the buffer needed to hold
3885 * the textual IPP attributes.
3886 */
3887
c7017ecc 3888static size_t /* O - Size of attribute buffer */
b9faaae1
MS
3889ipp_length(ipp_t *ipp) /* I - IPP request */
3890{
c7017ecc 3891 size_t bytes; /* Number of bytes */
b9faaae1
MS
3892 int i; /* Looping var */
3893 ipp_attribute_t *attr; /* Current attribute */
3894
3895
3896 /*
3897 * Loop through all attributes...
e1d6a774 3898 */
3899
b9faaae1 3900 bytes = 0;
e1d6a774 3901
b9faaae1 3902 for (attr = ipp->attrs; attr != NULL; attr = attr->next)
91c84a35 3903 {
b9faaae1
MS
3904 /*
3905 * Skip attributes that won't be sent to filters...
3906 */
91c84a35 3907
c7017ecc
MS
3908 if (attr->value_tag == IPP_TAG_NOVALUE ||
3909 attr->value_tag == IPP_TAG_MIMETYPE ||
b9faaae1
MS
3910 attr->value_tag == IPP_TAG_NAMELANG ||
3911 attr->value_tag == IPP_TAG_TEXTLANG ||
3912 attr->value_tag == IPP_TAG_URI ||
3913 attr->value_tag == IPP_TAG_URISCHEME)
3914 continue;
91c84a35 3915
b9faaae1
MS
3916 /*
3917 * Add space for a leading space and commas between each value.
3918 * For the first attribute, the leading space isn't used, so the
3919 * extra byte can be used as the nul terminator...
3920 */
e1d6a774 3921
b9faaae1 3922 bytes ++; /* " " separator */
7e86f2f6 3923 bytes += (size_t)attr->num_values; /* "," separators */
e1d6a774 3924
b9faaae1
MS
3925 /*
3926 * Boolean attributes appear as "foo,nofoo,foo,nofoo", while
3927 * other attributes appear as "foo=value1,value2,...,valueN".
3928 */
3929
3930 if (attr->value_tag != IPP_TAG_BOOLEAN)
3931 bytes += strlen(attr->name);
3932 else
7e86f2f6 3933 bytes += (size_t)attr->num_values * strlen(attr->name);
b9faaae1
MS
3934
3935 /*
3936 * Now add the size required for each value in the attribute...
3937 */
3938
3939 switch (attr->value_tag)
ef416fc2 3940 {
b9faaae1
MS
3941 case IPP_TAG_INTEGER :
3942 case IPP_TAG_ENUM :
3943 /*
3944 * Minimum value of a signed integer is -2147483647, or 11 digits.
3945 */
3946
7e86f2f6 3947 bytes += (size_t)attr->num_values * 11;
b9faaae1
MS
3948 break;
3949
3950 case IPP_TAG_BOOLEAN :
3951 /*
3952 * Add two bytes for each false ("no") value...
3953 */
3954
3955 for (i = 0; i < attr->num_values; i ++)
3956 if (!attr->values[i].boolean)
3957 bytes += 2;
3958 break;
3959
3960 case IPP_TAG_RANGE :
3961 /*
3962 * A range is two signed integers separated by a hyphen, or
3963 * 23 characters max.
3964 */
3965
7e86f2f6 3966 bytes += (size_t)attr->num_values * 23;
b9faaae1
MS
3967 break;
3968
3969 case IPP_TAG_RESOLUTION :
3970 /*
3971 * A resolution is two signed integers separated by an "x" and
3972 * suffixed by the units, or 26 characters max.
3973 */
3974
7e86f2f6 3975 bytes += (size_t)attr->num_values * 26;
b9faaae1
MS
3976 break;
3977
3978 case IPP_TAG_STRING :
3979 case IPP_TAG_TEXT :
3980 case IPP_TAG_NAME :
3981 case IPP_TAG_KEYWORD :
3982 case IPP_TAG_CHARSET :
3983 case IPP_TAG_LANGUAGE :
3984 case IPP_TAG_URI :
3985 /*
3986 * Strings can contain characters that need quoting. We need
3987 * at least 2 * len + 2 characters to cover the quotes and
3988 * any backslashes in the string.
3989 */
3990
3991 for (i = 0; i < attr->num_values; i ++)
3992 bytes += 2 * strlen(attr->values[i].string.text) + 2;
3993 break;
3994
3995 default :
3996 break; /* anti-compiler-warning-code */
e1d6a774 3997 }
3998 }
b9faaae1
MS
3999
4000 return (bytes);
4001}
4002
4003
4004/*
4005 * 'load_job_cache()' - Load jobs from the job.cache file.
4006 */
4007
4008static void
4009load_job_cache(const char *filename) /* I - job.cache filename */
4010{
4011 cups_file_t *fp; /* job.cache file */
4012 char line[1024], /* Line buffer */
4013 *value; /* Value on line */
4014 int linenum; /* Line number in file */
4015 cupsd_job_t *job; /* Current job */
4016 int jobid; /* Job ID */
4017 char jobfile[1024]; /* Job filename */
4018
4019
4020 /*
4021 * Open the job.cache file...
4022 */
4023
321d8d57 4024 if ((fp = cupsdOpenConfFile(filename)) == NULL)
e1d6a774 4025 {
b9faaae1 4026 load_request_root();
b9faaae1
MS
4027 return;
4028 }
ef416fc2 4029
e1d6a774 4030 /*
b9faaae1 4031 * Read entries from the job cache file and create jobs as needed.
e1d6a774 4032 */
ef416fc2 4033
b9faaae1
MS
4034 cupsdLogMessage(CUPSD_LOG_INFO, "Loading job cache file \"%s\"...",
4035 filename);
ef416fc2 4036
b9faaae1
MS
4037 linenum = 0;
4038 job = NULL;
4039
4040 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
4041 {
88f9aafc 4042 if (!_cups_strcasecmp(line, "NextJobId"))
b9faaae1
MS
4043 {
4044 if (value)
4045 NextJobId = atoi(value);
4046 }
88f9aafc 4047 else if (!_cups_strcasecmp(line, "<Job"))
b9faaae1
MS
4048 {
4049 if (job)
4050 {
12f89d24 4051 cupsdLogMessage(CUPSD_LOG_ERROR, "Missing </Job> directive on line %d.",
b9faaae1
MS
4052 linenum);
4053 continue;
4054 }
4055
4056 if (!value)
4057 {
12f89d24 4058 cupsdLogMessage(CUPSD_LOG_ERROR, "Missing job ID on line %d.", linenum);
b9faaae1
MS
4059 continue;
4060 }
4061
4062 jobid = atoi(value);
4063
4064 if (jobid < 1)
4065 {
12f89d24 4066 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad job ID %d on line %d.", jobid,
b9faaae1
MS
4067 linenum);
4068 continue;
4069 }
4070
4071 snprintf(jobfile, sizeof(jobfile), "%s/c%05d", RequestRoot, jobid);
4072 if (access(jobfile, 0))
4073 {
321d8d57
MS
4074 snprintf(jobfile, sizeof(jobfile), "%s/c%05d.N", RequestRoot, jobid);
4075 if (access(jobfile, 0))
4076 {
12f89d24 4077 cupsdLogMessage(CUPSD_LOG_ERROR, "[Job %d] Files have gone away.",
321d8d57
MS
4078 jobid);
4079 continue;
4080 }
b9faaae1
MS
4081 }
4082
4083 job = calloc(1, sizeof(cupsd_job_t));
4084 if (!job)
4085 {
4086 cupsdLogMessage(CUPSD_LOG_EMERG,
12f89d24 4087 "[Job %d] Unable to allocate memory for job.", jobid);
b9faaae1
MS
4088 break;
4089 }
4090
4091 job->id = jobid;
4092 job->back_pipes[0] = -1;
4093 job->back_pipes[1] = -1;
4094 job->print_pipes[0] = -1;
4095 job->print_pipes[1] = -1;
4096 job->side_pipes[0] = -1;
4097 job->side_pipes[1] = -1;
4098 job->status_pipes[0] = -1;
4099 job->status_pipes[1] = -1;
4100
72f50170 4101 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Loading from cache...");
b9faaae1
MS
4102 }
4103 else if (!job)
4104 {
4105 cupsdLogMessage(CUPSD_LOG_ERROR,
12f89d24 4106 "Missing <Job #> directive on line %d.", linenum);
b9faaae1
MS
4107 continue;
4108 }
88f9aafc 4109 else if (!_cups_strcasecmp(line, "</Job>"))
b9faaae1
MS
4110 {
4111 cupsArrayAdd(Jobs, job);
4112
f701418f
MS
4113 if (job->state_value <= IPP_JOB_STOPPED && cupsdLoadJob(job))
4114 cupsArrayAdd(ActiveJobs, job);
eec1fbc3
MS
4115 else if (job->state_value > IPP_JOB_STOPPED)
4116 {
4117 if (!job->completed_time)
4118 {
4119 cupsdLoadJob(job);
4120 unload_job(job);
4121 }
4122 }
b9faaae1
MS
4123
4124 job = NULL;
4125 }
4126 else if (!value)
4127 {
12f89d24 4128 cupsdLogMessage(CUPSD_LOG_ERROR, "Missing value on line %d.", linenum);
b9faaae1
MS
4129 continue;
4130 }
88f9aafc 4131 else if (!_cups_strcasecmp(line, "State"))
b9faaae1
MS
4132 {
4133 job->state_value = (ipp_jstate_t)atoi(value);
4134
4135 if (job->state_value < IPP_JOB_PENDING)
4136 job->state_value = IPP_JOB_PENDING;
4137 else if (job->state_value > IPP_JOB_COMPLETED)
4138 job->state_value = IPP_JOB_COMPLETED;
4139 }
eec1fbc3
MS
4140 else if (!_cups_strcasecmp(line, "Completed"))
4141 {
4142 job->completed_time = strtol(value, NULL, 10);
4143 }
88f9aafc 4144 else if (!_cups_strcasecmp(line, "HoldUntil"))
8b116e60 4145 {
eec1fbc3 4146 job->hold_until = strtol(value, NULL, 10);
8b116e60 4147 }
88f9aafc 4148 else if (!_cups_strcasecmp(line, "Priority"))
b9faaae1
MS
4149 {
4150 job->priority = atoi(value);
4151 }
88f9aafc 4152 else if (!_cups_strcasecmp(line, "Username"))
b9faaae1
MS
4153 {
4154 cupsdSetString(&job->username, value);
4155 }
88f9aafc 4156 else if (!_cups_strcasecmp(line, "Destination"))
b9faaae1
MS
4157 {
4158 cupsdSetString(&job->dest, value);
4159 }
88f9aafc 4160 else if (!_cups_strcasecmp(line, "DestType"))
b9faaae1
MS
4161 {
4162 job->dtype = (cups_ptype_t)atoi(value);
4163 }
88f9aafc 4164 else if (!_cups_strcasecmp(line, "NumFiles"))
b9faaae1
MS
4165 {
4166 job->num_files = atoi(value);
4167
4168 if (job->num_files < 0)
4169 {
12f89d24 4170 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad NumFiles value %d on line %d.",
b9faaae1
MS
4171 job->num_files, linenum);
4172 job->num_files = 0;
4173 continue;
4174 }
4175
4176 if (job->num_files > 0)
4177 {
4178 snprintf(jobfile, sizeof(jobfile), "%s/d%05d-001", RequestRoot,
4179 job->id);
4180 if (access(jobfile, 0))
4181 {
72f50170 4182 cupsdLogJob(job, CUPSD_LOG_INFO, "Data files have gone away.");
b9faaae1
MS
4183 job->num_files = 0;
4184 continue;
4185 }
4186
7e86f2f6
MS
4187 job->filetypes = calloc((size_t)job->num_files, sizeof(mime_type_t *));
4188 job->compressions = calloc((size_t)job->num_files, sizeof(int));
b9faaae1
MS
4189
4190 if (!job->filetypes || !job->compressions)
4191 {
72f50170
MS
4192 cupsdLogJob(job, CUPSD_LOG_EMERG,
4193 "Unable to allocate memory for %d files.",
4194 job->num_files);
b9faaae1
MS
4195 break;
4196 }
4197 }
4198 }
88f9aafc 4199 else if (!_cups_strcasecmp(line, "File"))
b9faaae1
MS
4200 {
4201 int number, /* File number */
4202 compression; /* Compression value */
4203 char super[MIME_MAX_SUPER], /* MIME super type */
4204 type[MIME_MAX_TYPE]; /* MIME type */
4205
4206
4207 if (sscanf(value, "%d%*[ \t]%15[^/]/%255s%d", &number, super, type,
4208 &compression) != 4)
4209 {
12f89d24 4210 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad File on line %d.", linenum);
b9faaae1
MS
4211 continue;
4212 }
4213
4214 if (number < 1 || number > job->num_files)
4215 {
12f89d24 4216 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad File number %d on line %d.",
b9faaae1
MS
4217 number, linenum);
4218 continue;
4219 }
0a682745 4220
b9faaae1 4221 number --;
ef416fc2 4222
b9faaae1
MS
4223 job->compressions[number] = compression;
4224 job->filetypes[number] = mimeType(MimeDatabase, super, type);
ef416fc2 4225
b9faaae1
MS
4226 if (!job->filetypes[number])
4227 {
e1d6a774 4228 /*
b9faaae1 4229 * If the original MIME type is unknown, auto-type it!
e1d6a774 4230 */
ef416fc2 4231
72f50170
MS
4232 cupsdLogJob(job, CUPSD_LOG_ERROR,
4233 "Unknown MIME type %s/%s for file %d.",
4234 super, type, number + 1);
b9faaae1
MS
4235
4236 snprintf(jobfile, sizeof(jobfile), "%s/d%05d-%03d", RequestRoot,
4237 job->id, number + 1);
4238 job->filetypes[number] = mimeFileType(MimeDatabase, jobfile, NULL,
4239 job->compressions + number);
ef416fc2 4240
e1d6a774 4241 /*
b9faaae1 4242 * If that didn't work, assume it is raw...
e1d6a774 4243 */
ef416fc2 4244
b9faaae1
MS
4245 if (!job->filetypes[number])
4246 job->filetypes[number] = mimeType(MimeDatabase, "application",
4247 "vnd.cups-raw");
4248 }
4249 }
4250 else
12f89d24 4251 cupsdLogMessage(CUPSD_LOG_ERROR, "Unknown %s directive on line %d.",
b9faaae1 4252 line, linenum);
ef416fc2 4253 }
4254
cb7f98ee
MS
4255 if (job)
4256 {
4257 cupsdLogMessage(CUPSD_LOG_ERROR,
4258 "Missing </Job> directive on line %d.", linenum);
4259 cupsdDeleteJob(job, CUPSD_JOB_PURGE);
4260 }
4261
b9faaae1
MS
4262 cupsFileClose(fp);
4263}
ef416fc2 4264
4265
b9faaae1
MS
4266/*
4267 * 'load_next_job_id()' - Load the NextJobId value from the job.cache file.
4268 */
bd7854cb 4269
b9faaae1
MS
4270static void
4271load_next_job_id(const char *filename) /* I - job.cache filename */
4272{
4273 cups_file_t *fp; /* job.cache file */
4274 char line[1024], /* Line buffer */
4275 *value; /* Value on line */
4276 int linenum; /* Line number in file */
4277 int next_job_id; /* NextJobId value from line */
db0bd74a 4278
db0bd74a 4279
b9faaae1
MS
4280 /*
4281 * Read the NextJobId directive from the job.cache file and use
4282 * the value (if any).
4283 */
bd7854cb 4284
b9faaae1 4285 if ((fp = cupsFileOpen(filename, "r")) == NULL)
e1d6a774 4286 {
b9faaae1
MS
4287 if (errno != ENOENT)
4288 cupsdLogMessage(CUPSD_LOG_ERROR,
4289 "Unable to open job cache file \"%s\": %s",
4290 filename, strerror(errno));
bd7854cb 4291
b9faaae1 4292 return;
e1d6a774 4293 }
bd7854cb 4294
b9faaae1
MS
4295 cupsdLogMessage(CUPSD_LOG_INFO,
4296 "Loading NextJobId from job cache file \"%s\"...", filename);
4297
4298 linenum = 0;
4299
4300 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
bd7854cb 4301 {
88f9aafc 4302 if (!_cups_strcasecmp(line, "NextJobId"))
b9faaae1
MS
4303 {
4304 if (value)
4305 {
4306 next_job_id = atoi(value);
4307
4308 if (next_job_id > NextJobId)
4309 NextJobId = next_job_id;
4310 }
4311 break;
4312 }
e1d6a774 4313 }
bd7854cb 4314
b9faaae1
MS
4315 cupsFileClose(fp);
4316}
09a101d6 4317
f7deaa1a 4318
b9faaae1
MS
4319/*
4320 * 'load_request_root()' - Load jobs from the RequestRoot directory.
4321 */
bd7854cb 4322
b9faaae1
MS
4323static void
4324load_request_root(void)
4325{
4326 cups_dir_t *dir; /* Directory */
4327 cups_dentry_t *dent; /* Directory entry */
4328 cupsd_job_t *job; /* New job */
e1d6a774 4329
bd7854cb 4330
4331 /*
b9faaae1 4332 * Open the requests directory...
bd7854cb 4333 */
4334
b9faaae1 4335 cupsdLogMessage(CUPSD_LOG_DEBUG, "Scanning %s for jobs...", RequestRoot);
bd7854cb 4336
b9faaae1 4337 if ((dir = cupsDirOpen(RequestRoot)) == NULL)
bd7854cb 4338 {
b9faaae1
MS
4339 cupsdLogMessage(CUPSD_LOG_ERROR,
4340 "Unable to open spool directory \"%s\": %s",
4341 RequestRoot, strerror(errno));
4342 return;
e1d6a774 4343 }
bd7854cb 4344
b9faaae1
MS
4345 /*
4346 * Read all the c##### files...
4347 */
bd7854cb 4348
b9faaae1
MS
4349 while ((dent = cupsDirRead(dir)) != NULL)
4350 if (strlen(dent->filename) >= 6 && dent->filename[0] == 'c')
bd7854cb 4351 {
b9faaae1
MS
4352 /*
4353 * Allocate memory for the job...
4354 */
bd7854cb 4355
b9faaae1 4356 if ((job = calloc(sizeof(cupsd_job_t), 1)) == NULL)
bd7854cb 4357 {
12f89d24 4358 cupsdLogMessage(CUPSD_LOG_ERROR, "Ran out of memory for jobs.");
b9faaae1
MS
4359 cupsDirClose(dir);
4360 return;
4361 }
bd7854cb 4362
b9faaae1
MS
4363 /*
4364 * Assign the job ID...
4365 */
bd7854cb 4366
b9faaae1
MS
4367 job->id = atoi(dent->filename + 1);
4368 job->back_pipes[0] = -1;
4369 job->back_pipes[1] = -1;
4370 job->print_pipes[0] = -1;
4371 job->print_pipes[1] = -1;
4372 job->side_pipes[0] = -1;
4373 job->side_pipes[1] = -1;
4374 job->status_pipes[0] = -1;
4375 job->status_pipes[1] = -1;
bd7854cb 4376
b9faaae1
MS
4377 if (job->id >= NextJobId)
4378 NextJobId = job->id + 1;
ed486911 4379
b9faaae1
MS
4380 /*
4381 * Load the job...
4382 */
ed486911 4383
f701418f
MS
4384 if (cupsdLoadJob(job))
4385 {
4386 /*
4387 * Insert the job into the array, sorting by job priority and ID...
4388 */
bd7854cb 4389
f701418f 4390 cupsArrayAdd(Jobs, job);
e1d6a774 4391
f701418f
MS
4392 if (job->state_value <= IPP_JOB_STOPPED)
4393 cupsArrayAdd(ActiveJobs, job);
4394 else
4395 unload_job(job);
4396 }
0fa6c7fa
MS
4397 else
4398 free(job);
bd7854cb 4399 }
bd7854cb 4400
b9faaae1
MS
4401 cupsDirClose(dir);
4402}
bd7854cb 4403
4404
82cc1f9a
MS
4405/*
4406 * 'remove_job_files()' - Remove the document files for a job.
4407 */
4408
4409static void
4410remove_job_files(cupsd_job_t *job) /* I - Job */
4411{
4412 int i; /* Looping var */
4413 char filename[1024]; /* Document filename */
4414
4415
4416 if (job->num_files <= 0)
4417 return;
4418
4419 for (i = 1; i <= job->num_files; i ++)
4420 {
4421 snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot,
4422 job->id, i);
cb7f98ee 4423 cupsdUnlinkOrRemoveFile(filename);
82cc1f9a
MS
4424 }
4425
4426 free(job->filetypes);
4427 free(job->compressions);
4428
4429 job->file_time = 0;
4430 job->num_files = 0;
4431 job->filetypes = NULL;
4432 job->compressions = NULL;
4433
4434 LastEvent |= CUPSD_EVENT_PRINTER_STATE_CHANGED;
4435}
4436
4437
4438/*
4439 * 'remove_job_history()' - Remove the control file for a job.
4440 */
4441
4442static void
4443remove_job_history(cupsd_job_t *job) /* I - Job */
4444{
4445 char filename[1024]; /* Control filename */
4446
4447
4448 /*
4449 * Remove the job info file...
4450 */
4451
4452 snprintf(filename, sizeof(filename), "%s/c%05d", RequestRoot,
4453 job->id);
cb7f98ee 4454 cupsdUnlinkOrRemoveFile(filename);
82cc1f9a
MS
4455
4456 LastEvent |= CUPSD_EVENT_PRINTER_STATE_CHANGED;
4457}
4458
4459
b9faaae1
MS
4460/*
4461 * 'set_time()' - Set one of the "time-at-xyz" attributes.
4462 */
bd7854cb 4463
b9faaae1
MS
4464static void
4465set_time(cupsd_job_t *job, /* I - Job to update */
4466 const char *name) /* I - Name of attribute */
4467{
4468 ipp_attribute_t *attr; /* Time attribute */
82cc1f9a
MS
4469 time_t curtime; /* Current time */
4470
bd7854cb 4471
82cc1f9a
MS
4472 curtime = time(NULL);
4473
4474 cupsdLogJob(job, CUPSD_LOG_DEBUG, "%s=%ld", name, (long)curtime);
bd7854cb 4475
b9faaae1
MS
4476 if ((attr = ippFindAttribute(job->attrs, name, IPP_TAG_ZERO)) != NULL)
4477 {
4478 attr->value_tag = IPP_TAG_INTEGER;
82cc1f9a
MS
4479 attr->values[0].integer = curtime;
4480 }
4481
4482 if (!strcmp(name, "time-at-completed"))
4483 {
eec1fbc3
MS
4484 job->completed_time = curtime;
4485
0fa6c7fa 4486 if (JobHistory < INT_MAX && attr)
82cc1f9a
MS
4487 job->history_time = attr->values[0].integer + JobHistory;
4488 else
4489 job->history_time = INT_MAX;
4490
4491 if (job->history_time < JobHistoryUpdate || !JobHistoryUpdate)
4492 JobHistoryUpdate = job->history_time;
4493
0fa6c7fa 4494 if (JobFiles < INT_MAX && attr)
82cc1f9a
MS
4495 job->file_time = attr->values[0].integer + JobFiles;
4496 else
4497 job->file_time = INT_MAX;
4498
4499 if (job->file_time < JobHistoryUpdate || !JobHistoryUpdate)
4500 JobHistoryUpdate = job->file_time;
4501
4502 cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_time: JobHistoryUpdate=%ld",
4503 (long)JobHistoryUpdate);
b9faaae1
MS
4504 }
4505}
bd7854cb 4506
e1d6a774 4507
b9faaae1
MS
4508/*
4509 * 'start_job()' - Start a print job.
4510 */
e1d6a774 4511
b9faaae1
MS
4512static void
4513start_job(cupsd_job_t *job, /* I - Job ID */
4514 cupsd_printer_t *printer) /* I - Printer to print job */
4515{
cb7f98ee 4516 const char *filename; /* Support filename */
982069db
MS
4517 ipp_attribute_t *cancel_after = ippFindAttribute(job->attrs,
4518 "job-cancel-after",
4519 IPP_TAG_INTEGER);
4520 /* job-cancel-after attribute */
cb7f98ee
MS
4521
4522
b9faaae1
MS
4523 cupsdLogMessage(CUPSD_LOG_DEBUG2, "start_job(job=%p(%d), printer=%p(%s))",
4524 job, job->id, printer, printer->name);
bd7854cb 4525
b9faaae1
MS
4526 /*
4527 * Make sure we have some files around before we try to print...
4528 */
bd7854cb 4529
b9faaae1
MS
4530 if (job->num_files == 0)
4531 {
12f89d24 4532 ippSetString(job->attrs, &job->reasons, 0, "aborted-by-system");
b9faaae1
MS
4533 cupsdSetJobState(job, IPP_JOB_ABORTED, CUPSD_JOB_DEFAULT,
4534 "Aborting job because it has no files.");
4535 return;
4536 }
bd7854cb 4537
b9faaae1
MS
4538 /*
4539 * Update the printer and job state to "processing"...
4540 */
bd7854cb 4541
b9faaae1
MS
4542 if (!cupsdLoadJob(job))
4543 return;
89d46774 4544
3e7fe0ca
MS
4545 if (!job->printer_message)
4546 job->printer_message = ippFindAttribute(job->attrs,
4547 "job-printer-state-message",
4548 IPP_TAG_TEXT);
88f9aafc
MS
4549 if (job->printer_message)
4550 cupsdSetString(&(job->printer_message->values[0].string.text), "");
4551
12f89d24 4552 ippSetString(job->attrs, &job->reasons, 0, "job-printing");
b9faaae1
MS
4553 cupsdSetJobState(job, IPP_JOB_PROCESSING, CUPSD_JOB_DEFAULT, NULL);
4554 cupsdSetPrinterState(printer, IPP_PRINTER_PROCESSING, 0);
07ed0e9a
MS
4555 cupsdSetPrinterReasons(printer, "-cups-remote-pending,"
4556 "cups-remote-pending-held,"
4557 "cups-remote-processing,"
4558 "cups-remote-stopped,"
4559 "cups-remote-canceled,"
4560 "cups-remote-aborted,"
4561 "cups-remote-completed");
f7deaa1a 4562
1340db2d
MS
4563 job->cost = 0;
4564 job->current_file = 0;
82cc1f9a
MS
4565 job->file_time = 0;
4566 job->history_time = 0;
1340db2d
MS
4567 job->progress = 0;
4568 job->printer = printer;
4569 printer->job = job;
f7deaa1a 4570
982069db
MS
4571 if (cancel_after)
4572 job->cancel_time = time(NULL) + ippGetInteger(cancel_after, 0);
4573 else if (MaxJobTime > 0)
dcb445bc
MS
4574 job->cancel_time = time(NULL) + MaxJobTime;
4575 else
4576 job->cancel_time = 0;
4577
cb7f98ee
MS
4578 /*
4579 * Check for support files...
4580 */
4581
4582 cupsdSetPrinterReasons(job->printer, "-cups-missing-filter-warning,"
4583 "cups-insecure-filter-warning");
4584
4585 if (printer->pc)
4586 {
4587 for (filename = (const char *)cupsArrayFirst(printer->pc->support_files);
4588 filename;
4589 filename = (const char *)cupsArrayNext(printer->pc->support_files))
4590 {
4591 if (_cupsFileCheck(filename, _CUPS_FILE_CHECK_FILE, !RunUser,
4592 cupsdLogFCMessage, printer))
4593 break;
4594 }
4595 }
4596
b9faaae1
MS
4597 /*
4598 * Setup the last exit status and security profiles...
4599 */
89d46774 4600
8fe0183a
MS
4601 job->status = 0;
4602 job->profile = cupsdCreateProfile(job->id, 0);
4603 job->bprofile = cupsdCreateProfile(job->id, 1);
bd7854cb 4604
b9faaae1
MS
4605 /*
4606 * Create the status pipes and buffer...
4607 */
bd7854cb 4608
b9faaae1
MS
4609 if (cupsdOpenPipe(job->status_pipes))
4610 {
4611 cupsdLogJob(job, CUPSD_LOG_DEBUG,
4612 "Unable to create job status pipes - %s.", strerror(errno));
89d46774 4613
b9faaae1
MS
4614 cupsdSetJobState(job, IPP_JOB_STOPPED, CUPSD_JOB_DEFAULT,
4615 "Job stopped because the scheduler could not create the "
4616 "job status pipes.");
89d46774 4617
b9faaae1
MS
4618 cupsdDestroyProfile(job->profile);
4619 job->profile = NULL;
8fe0183a
MS
4620 cupsdDestroyProfile(job->bprofile);
4621 job->bprofile = NULL;
b9faaae1 4622 return;
e1d6a774 4623 }
bd7854cb 4624
b9faaae1
MS
4625 job->status_buffer = cupsdStatBufNew(job->status_pipes[0], NULL);
4626 job->status_level = CUPSD_LOG_INFO;
4627
4628 /*
4629 * Create the backchannel pipes and make them non-blocking...
4630 */
bd7854cb 4631
b9faaae1 4632 if (cupsdOpenPipe(job->back_pipes))
e1d6a774 4633 {
b9faaae1
MS
4634 cupsdLogJob(job, CUPSD_LOG_DEBUG,
4635 "Unable to create back-channel pipes - %s.", strerror(errno));
4636
4637 cupsdSetJobState(job, IPP_JOB_STOPPED, CUPSD_JOB_DEFAULT,
4638 "Job stopped because the scheduler could not create the "
4639 "back-channel pipes.");
4640
4641 cupsdClosePipe(job->status_pipes);
4642 cupsdStatBufDelete(job->status_buffer);
4643 job->status_buffer = NULL;
4644
4645 cupsdDestroyProfile(job->profile);
4646 job->profile = NULL;
8fe0183a
MS
4647 cupsdDestroyProfile(job->bprofile);
4648 job->bprofile = NULL;
b9faaae1 4649 return;
e1d6a774 4650 }
bd7854cb 4651
b9faaae1
MS
4652 fcntl(job->back_pipes[0], F_SETFL,
4653 fcntl(job->back_pipes[0], F_GETFL) | O_NONBLOCK);
4654 fcntl(job->back_pipes[1], F_SETFL,
4655 fcntl(job->back_pipes[1], F_GETFL) | O_NONBLOCK);
ef416fc2 4656
b9faaae1
MS
4657 /*
4658 * Create the side-channel pipes and make them non-blocking...
4659 */
ef416fc2 4660
b9faaae1
MS
4661 if (socketpair(AF_LOCAL, SOCK_STREAM, 0, job->side_pipes))
4662 {
4663 cupsdLogJob(job, CUPSD_LOG_DEBUG,
4664 "Unable to create side-channel pipes - %s.", strerror(errno));
ef416fc2 4665
b9faaae1
MS
4666 cupsdSetJobState(job, IPP_JOB_STOPPED, CUPSD_JOB_DEFAULT,
4667 "Job stopped because the scheduler could not create the "
4668 "side-channel pipes.");
ef416fc2 4669
b9faaae1
MS
4670 cupsdClosePipe(job->back_pipes);
4671
4672 cupsdClosePipe(job->status_pipes);
4673 cupsdStatBufDelete(job->status_buffer);
4674 job->status_buffer = NULL;
4675
4676 cupsdDestroyProfile(job->profile);
4677 job->profile = NULL;
8fe0183a
MS
4678 cupsdDestroyProfile(job->bprofile);
4679 job->bprofile = NULL;
b9faaae1
MS
4680 return;
4681 }
4682
4683 fcntl(job->side_pipes[0], F_SETFL,
4684 fcntl(job->side_pipes[0], F_GETFL) | O_NONBLOCK);
4685 fcntl(job->side_pipes[1], F_SETFL,
4686 fcntl(job->side_pipes[1], F_GETFL) | O_NONBLOCK);
ef416fc2 4687
7a0cbd5e
MS
4688 fcntl(job->side_pipes[0], F_SETFD,
4689 fcntl(job->side_pipes[0], F_GETFD) | FD_CLOEXEC);
4690 fcntl(job->side_pipes[1], F_SETFD,
4691 fcntl(job->side_pipes[1], F_GETFD) | FD_CLOEXEC);
4692
ef416fc2 4693 /*
b9faaae1 4694 * Now start the first file in the job...
ef416fc2 4695 */
4696
b9faaae1
MS
4697 cupsdContinueJob(job);
4698}
ef416fc2 4699
ef416fc2 4700
b9faaae1
MS
4701/*
4702 * 'stop_job()' - Stop a print job.
4703 */
ef416fc2 4704
b9faaae1
MS
4705static void
4706stop_job(cupsd_job_t *job, /* I - Job */
4707 cupsd_jobaction_t action) /* I - Action */
4708{
4709 int i; /* Looping var */
b94498cf 4710
ef416fc2 4711
b9faaae1
MS
4712 cupsdLogMessage(CUPSD_LOG_DEBUG2, "stop_job(job=%p(%d), action=%d)", job,
4713 job->id, action);
ef416fc2 4714
b9faaae1
MS
4715 FilterLevel -= job->cost;
4716 job->cost = 0;
ef416fc2 4717
238c3832
MS
4718 if (action == CUPSD_JOB_DEFAULT && !job->kill_time)
4719 job->kill_time = time(NULL) + JobKillDelay;
ef55b745 4720 else if (action >= CUPSD_JOB_FORCE)
238c3832
MS
4721 job->kill_time = 0;
4722
b9faaae1
MS
4723 for (i = 0; job->filters[i]; i ++)
4724 if (job->filters[i] > 0)
ef55b745
MS
4725 {
4726 cupsdEndProcess(job->filters[i], action >= CUPSD_JOB_FORCE);
4727
4728 if (action >= CUPSD_JOB_FORCE)
4729 job->filters[i] = -job->filters[i];
4730 }
b9faaae1
MS
4731
4732 if (job->backend > 0)
ef55b745
MS
4733 {
4734 cupsdEndProcess(job->backend, action >= CUPSD_JOB_FORCE);
4735
4736 if (action >= CUPSD_JOB_FORCE)
4737 job->backend = -job->backend;
4738 }
4739
4740 if (action >= CUPSD_JOB_FORCE)
4741 {
4742 /*
4743 * Clear job status...
4744 */
4745
4746 job->status = 0;
4747 }
e1d6a774 4748}
ef416fc2 4749
e1d6a774 4750
4751/*
4752 * 'unload_job()' - Unload a job from memory.
4753 */
4754
4755static void
4756unload_job(cupsd_job_t *job) /* I - Job */
4757{
4758 if (!job->attrs)
4759 return;
4760
72f50170 4761 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Unloading...");
e1d6a774 4762
4763 ippDelete(job->attrs);
4764
cc0d019f
MS
4765 job->attrs = NULL;
4766 job->state = NULL;
12f89d24 4767 job->reasons = NULL;
cc0d019f
MS
4768 job->sheets = NULL;
4769 job->job_sheets = NULL;
4770 job->printer_message = NULL;
4771 job->printer_reasons = NULL;
ef416fc2 4772}
4773
4774
4775/*
f899b121 4776 * 'update_job()' - Read a status update from a job's filters.
4777 */
4778
4779void
09a101d6 4780update_job(cupsd_job_t *job) /* I - Job to check */
f899b121 4781{
4782 int i; /* Looping var */
4783 int copies; /* Number of copies printed */
178cb736
MS
4784 char message[CUPSD_SB_BUFFER_SIZE],
4785 /* Message text */
f899b121 4786 *ptr; /* Pointer update... */
4787 int loglevel, /* Log level for message */
4788 event = 0; /* Events? */
f0ab5bff
MS
4789 static const char * const levels[] = /* Log levels */
4790 {
4791 "NONE",
4792 "EMERG",
4793 "ALERT",
4794 "CRIT",
4795 "ERROR",
4796 "WARN",
4797 "NOTICE",
4798 "INFO",
4799 "DEBUG",
4800 "DEBUG2"
4801 };
f899b121 4802
4803
f0ab5bff
MS
4804 /*
4805 * Get the printer associated with this job; if the printer is stopped for
4806 * any reason then job->printer will be reset to NULL, so make sure we have
4807 * a valid pointer...
4808 */
4809
f899b121 4810 while ((ptr = cupsdStatBufUpdate(job->status_buffer, &loglevel,
4811 message, sizeof(message))) != NULL)
4812 {
4813 /*
4814 * Process page and printer state messages as needed...
4815 */
4816
4817 if (loglevel == CUPSD_LOG_PAGE)
4818 {
4819 /*
4820 * Page message; send the message to the page_log file and update the
4821 * job sheet count...
4822 */
4823
75bd9771 4824 cupsdLogJob(job, CUPSD_LOG_DEBUG, "PAGE: %s", message);
dd1abb6b 4825
91c84a35 4826 if (job->sheets)
f899b121 4827 {
88f9aafc 4828 if (!_cups_strncasecmp(message, "total ", 6))
f899b121 4829 {
4830 /*
4831 * Got a total count of pages from a backend or filter...
4832 */
4833
4834 copies = atoi(message + 6);
4835 copies -= job->sheets->values[0].integer; /* Just track the delta */
4836 }
4837 else if (!sscanf(message, "%*d%d", &copies))
4838 copies = 1;
4839
4840 job->sheets->values[0].integer += copies;
4841
b9faaae1 4842 if (job->printer->page_limit)
ba55dc12 4843 cupsdUpdateQuota(job->printer, job->username, copies, 0);
f899b121 4844 }
4845
4846 cupsdLogPage(job, message);
4847
91c84a35 4848 if (job->sheets)
b9faaae1 4849 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS, job->printer, job,
91c84a35 4850 "Printed %d page(s).", job->sheets->values[0].integer);
f899b121 4851 }
a469f8a5
MS
4852 else if (loglevel == CUPSD_LOG_JOBSTATE)
4853 {
4854 /*
4855 * Support "keyword" to set job-state-reasons to the specified keyword.
4856 * This is sufficient for the current paid printing stuff.
4857 */
4858
4859 cupsdLogJob(job, CUPSD_LOG_DEBUG, "JOBSTATE: %s", message);
4860
8e67b75b
MS
4861 if (!strcmp(message, "cups-retry-as-raster"))
4862 job->retry_as_raster = 1;
4863 else
4864 ippSetString(job->attrs, &job->reasons, 0, message);
a469f8a5 4865 }
f899b121 4866 else if (loglevel == CUPSD_LOG_STATE)
4867 {
75bd9771 4868 cupsdLogJob(job, CUPSD_LOG_DEBUG, "STATE: %s", message);
dd1abb6b 4869
09a101d6 4870 if (!strcmp(message, "paused"))
c24d2134 4871 {
b9faaae1 4872 cupsdStopPrinter(job->printer, 1);
c24d2134
MS
4873 return;
4874 }
0fa6c7fa 4875 else if (message[0] && cupsdSetPrinterReasons(job->printer, message))
dcb445bc 4876 {
d9bca400 4877 event |= CUPSD_EVENT_PRINTER_STATE;
bc44d920 4878
982069db 4879 if (MaxJobTime > 0)
dcb445bc
MS
4880 {
4881 /*
4882 * Reset cancel time after connecting to the device...
4883 */
4884
4885 for (i = 0; i < job->printer->num_reasons; i ++)
4886 if (!strcmp(job->printer->reasons[i], "connecting-to-device"))
4887 break;
4888
4889 if (i >= job->printer->num_reasons)
982069db 4890 {
581dae2d
MS
4891 ipp_attribute_t *cancel_after = ippFindAttribute(job->attrs,
4892 "job-cancel-after",
4893 IPP_TAG_INTEGER);
4894 /* job-cancel-after attribute */
4895
982069db
MS
4896 if (cancel_after)
4897 job->cancel_time = time(NULL) + ippGetInteger(cancel_after, 0);
4898 else
4899 job->cancel_time = time(NULL) + MaxJobTime;
4900 }
dcb445bc
MS
4901 }
4902 }
4903
4509bb49 4904 update_job_attrs(job, 0);
f899b121 4905 }
4906 else if (loglevel == CUPSD_LOG_ATTR)
4907 {
4908 /*
4909 * Set attribute(s)...
4910 */
4911
4912 int num_attrs; /* Number of attributes */
4913 cups_option_t *attrs; /* Attributes */
4914 const char *attr; /* Attribute */
4915
75bd9771 4916 cupsdLogJob(job, CUPSD_LOG_DEBUG, "ATTR: %s", message);
dd1abb6b 4917
f899b121 4918 num_attrs = cupsParseOptions(message, 0, &attrs);
4919
5a9febac
MS
4920 if ((attr = cupsGetOption("auth-info-default", num_attrs,
4921 attrs)) != NULL)
4922 {
4923 job->printer->num_options = cupsAddOption("auth-info", attr,
4924 job->printer->num_options,
4925 &(job->printer->options));
4926 cupsdSetPrinterAttrs(job->printer);
4927
4928 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
4929 }
4930
f899b121 4931 if ((attr = cupsGetOption("auth-info-required", num_attrs,
4932 attrs)) != NULL)
7ff4fea9 4933 {
b9faaae1
MS
4934 cupsdSetAuthInfoRequired(job->printer, attr, NULL);
4935 cupsdSetPrinterAttrs(job->printer);
3dfe78b3 4936
a2326b5b 4937 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
7ff4fea9 4938 }
f899b121 4939
9a4f8274
MS
4940 if ((attr = cupsGetOption("job-media-progress", num_attrs,
4941 attrs)) != NULL)
4942 {
4943 int progress = atoi(attr);
4944
4945
4946 if (progress >= 0 && progress <= 100)
4947 {
4948 job->progress = progress;
4949
4950 if (job->sheets)
b9faaae1 4951 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS, job->printer, job,
9a4f8274
MS
4952 "Printing page %d, %d%%",
4953 job->sheets->values[0].integer, job->progress);
4954 }
4955 }
4956
323c5de1 4957 if ((attr = cupsGetOption("printer-alert", num_attrs, attrs)) != NULL)
4958 {
b9faaae1 4959 cupsdSetString(&job->printer->alert, attr);
d9bca400 4960 event |= CUPSD_EVENT_PRINTER_STATE;
323c5de1 4961 }
4962
4963 if ((attr = cupsGetOption("printer-alert-description", num_attrs,
4964 attrs)) != NULL)
4965 {
b9faaae1 4966 cupsdSetString(&job->printer->alert_description, attr);
d9bca400 4967 event |= CUPSD_EVENT_PRINTER_STATE;
323c5de1 4968 }
4969
5a738aea
MS
4970 if ((attr = cupsGetOption("marker-colors", num_attrs, attrs)) != NULL)
4971 {
b9faaae1
MS
4972 cupsdSetPrinterAttr(job->printer, "marker-colors", (char *)attr);
4973 job->printer->marker_time = time(NULL);
5a738aea 4974 event |= CUPSD_EVENT_PRINTER_STATE;
52f6f666 4975 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
5a738aea
MS
4976 }
4977
4978 if ((attr = cupsGetOption("marker-levels", num_attrs, attrs)) != NULL)
4979 {
b9faaae1
MS
4980 cupsdSetPrinterAttr(job->printer, "marker-levels", (char *)attr);
4981 job->printer->marker_time = time(NULL);
5a738aea 4982 event |= CUPSD_EVENT_PRINTER_STATE;
52f6f666 4983 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
5a738aea
MS
4984 }
4985
ed6e7faf
MS
4986 if ((attr = cupsGetOption("marker-low-levels", num_attrs, attrs)) != NULL)
4987 {
b9faaae1
MS
4988 cupsdSetPrinterAttr(job->printer, "marker-low-levels", (char *)attr);
4989 job->printer->marker_time = time(NULL);
ed6e7faf
MS
4990 event |= CUPSD_EVENT_PRINTER_STATE;
4991 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
4992 }
4993
4994 if ((attr = cupsGetOption("marker-high-levels", num_attrs, attrs)) != NULL)
4995 {
b9faaae1
MS
4996 cupsdSetPrinterAttr(job->printer, "marker-high-levels", (char *)attr);
4997 job->printer->marker_time = time(NULL);
ed6e7faf
MS
4998 event |= CUPSD_EVENT_PRINTER_STATE;
4999 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
5000 }
5001
75bd9771
MS
5002 if ((attr = cupsGetOption("marker-message", num_attrs, attrs)) != NULL)
5003 {
b9faaae1
MS
5004 cupsdSetPrinterAttr(job->printer, "marker-message", (char *)attr);
5005 job->printer->marker_time = time(NULL);
75bd9771 5006 event |= CUPSD_EVENT_PRINTER_STATE;
52f6f666 5007 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
75bd9771
MS
5008 }
5009
5a738aea
MS
5010 if ((attr = cupsGetOption("marker-names", num_attrs, attrs)) != NULL)
5011 {
b9faaae1
MS
5012 cupsdSetPrinterAttr(job->printer, "marker-names", (char *)attr);
5013 job->printer->marker_time = time(NULL);
5a738aea 5014 event |= CUPSD_EVENT_PRINTER_STATE;
52f6f666 5015 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
5a738aea
MS
5016 }
5017
5018 if ((attr = cupsGetOption("marker-types", num_attrs, attrs)) != NULL)
5019 {
b9faaae1
MS
5020 cupsdSetPrinterAttr(job->printer, "marker-types", (char *)attr);
5021 job->printer->marker_time = time(NULL);
5a738aea 5022 event |= CUPSD_EVENT_PRINTER_STATE;
52f6f666 5023 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
5a738aea
MS
5024 }
5025
f899b121 5026 cupsFreeOptions(num_attrs, attrs);
5027 }
c9fc04c6
MS
5028 else if (loglevel == CUPSD_LOG_PPD)
5029 {
5030 /*
5031 * Set attribute(s)...
5032 */
5033
75bd9771 5034 cupsdLogJob(job, CUPSD_LOG_DEBUG, "PPD: %s", message);
dd1abb6b 5035
a29fd7dd
MS
5036 job->num_keywords = cupsParseOptions(message, job->num_keywords,
5037 &job->keywords);
c9fc04c6 5038 }
4d301e69 5039 else
f899b121 5040 {
4d301e69
MS
5041 /*
5042 * Strip legacy message prefix...
5043 */
f899b121 5044
4d301e69 5045 if (!strncmp(message, "recoverable:", 12))
d2354e63 5046 {
4d301e69
MS
5047 ptr = message + 12;
5048 while (isspace(*ptr & 255))
5049 ptr ++;
d2354e63 5050 }
4d301e69 5051 else if (!strncmp(message, "recovered:", 10))
acb056cb 5052 {
4d301e69
MS
5053 ptr = message + 10;
5054 while (isspace(*ptr & 255))
5055 ptr ++;
acb056cb 5056 }
4d301e69
MS
5057 else
5058 ptr = message;
5059
dcb445bc
MS
5060 if (*ptr)
5061 cupsdLogJob(job, loglevel, "%s", ptr);
f899b121 5062
5180a04c
MS
5063 if (loglevel < CUPSD_LOG_DEBUG &&
5064 strcmp(job->printer->state_message, ptr))
1f0275e3 5065 {
4d301e69 5066 strlcpy(job->printer->state_message, ptr,
b9faaae1 5067 sizeof(job->printer->state_message));
4509bb49 5068
8b116e60 5069 event |= CUPSD_EVENT_PRINTER_STATE | CUPSD_EVENT_JOB_PROGRESS;
4509bb49 5070
229681c1 5071 if (loglevel <= job->status_level && job->status_level > CUPSD_LOG_ERROR)
1f0275e3
MS
5072 {
5073 /*
b9faaae1 5074 * Some messages show in the job-printer-state-message attribute...
1f0275e3 5075 */
09a101d6 5076
1f0275e3
MS
5077 if (loglevel != CUPSD_LOG_NOTICE)
5078 job->status_level = loglevel;
01ce6322 5079
1f0275e3 5080 update_job_attrs(job, 1);
f0ab5bff
MS
5081
5082 cupsdLogJob(job, CUPSD_LOG_DEBUG,
5083 "Set job-printer-state-message to \"%s\", "
5084 "current level=%s",
5085 job->printer_message->values[0].string.text,
5086 levels[job->status_level]);
1f0275e3 5087 }
75bd9771 5088 }
f899b121 5089 }
5090
5091 if (!strchr(job->status_buffer->buffer, '\n'))
5092 break;
5093 }
5094
f8b3a85b
MS
5095 if (event & CUPSD_EVENT_JOB_PROGRESS)
5096 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS, job->printer, job,
5097 "%s", job->printer->state_message);
5180a04c 5098 if (event & CUPSD_EVENT_PRINTER_STATE)
b9faaae1
MS
5099 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, job->printer, NULL,
5100 (job->printer->type & CUPS_PRINTER_CLASS) ?
f899b121 5101 "Class \"%s\" state changed." :
5102 "Printer \"%s\" state changed.",
b9faaae1 5103 job->printer->name);
f899b121 5104
8b116e60 5105
f899b121 5106 if (ptr == NULL && !job->status_buffer->bufused)
5107 {
5108 /*
5109 * See if all of the filters and the backend have returned their
5110 * exit statuses.
5111 */
5112
5113 for (i = 0; job->filters[i] < 0; i ++);
5114
5115 if (job->filters[i])
d7871c8c
MS
5116 {
5117 /*
5118 * EOF but we haven't collected the exit status of all filters...
5119 */
5120
5121 cupsdCheckProcess();
f899b121 5122 return;
d7871c8c 5123 }
f899b121 5124
5125 if (job->current_file >= job->num_files && job->backend > 0)
d7871c8c
MS
5126 {
5127 /*
5128 * EOF but we haven't collected the exit status of the backend...
5129 */
5130
5131 cupsdCheckProcess();
f899b121 5132 return;
d7871c8c 5133 }
f899b121 5134
5135 /*
5136 * Handle the end of job stuff...
5137 */
5138
ef55b745 5139 finalize_job(job, 1);
b9faaae1
MS
5140
5141 /*
5142 * Check for new jobs...
5143 */
5144
5145 cupsdCheckJobs();
f899b121 5146 }
5147}
5148
5149
5150/*
bc44d920 5151 * 'update_job_attrs()' - Update the job-printer-* attributes.
5152 */
5153
5154void
4509bb49 5155update_job_attrs(cupsd_job_t *job, /* I - Job to update */
b9faaae1 5156 int do_message)/* I - 1 = copy job-printer-state message */
bc44d920 5157{
5158 int i; /* Looping var */
5159 int num_reasons; /* Actual number of reasons */
5160 const char * const *reasons; /* Reasons */
f0ab5bff 5161 static const char *none = "none"; /* "none" reason */
bc44d920 5162
5163
5164 /*
5165 * Get/create the job-printer-state-* attributes...
5166 */
5167
5168 if (!job->printer_message)
5169 {
5170 if ((job->printer_message = ippFindAttribute(job->attrs,
5171 "job-printer-state-message",
5172 IPP_TAG_TEXT)) == NULL)
5173 job->printer_message = ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_TEXT,
5174 "job-printer-state-message",
5175 NULL, "");
5176 }
5177
5178 if (!job->printer_reasons)
5179 job->printer_reasons = ippFindAttribute(job->attrs,
5180 "job-printer-state-reasons",
5181 IPP_TAG_KEYWORD);
5182
bc44d920 5183 /*
b9faaae1 5184 * Copy or clear the printer-state-message value as needed...
bc44d920 5185 */
5186
b9faaae1
MS
5187 if (job->state_value != IPP_JOB_PROCESSING &&
5188 job->status_level == CUPSD_LOG_INFO)
dcb445bc 5189 {
b9faaae1 5190 cupsdSetString(&(job->printer_message->values[0].string.text), "");
dcb445bc
MS
5191
5192 job->dirty = 1;
5193 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
5194 }
b9faaae1 5195 else if (job->printer->state_message[0] && do_message)
dcb445bc 5196 {
bc44d920 5197 cupsdSetString(&(job->printer_message->values[0].string.text),
b9faaae1 5198 job->printer->state_message);
ef55b745 5199
dcb445bc
MS
5200 job->dirty = 1;
5201 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
5202 }
5203
bc44d920 5204 /*
5205 * ... and the printer-state-reasons value...
5206 */
5207
b9faaae1 5208 if (job->printer->num_reasons == 0)
bc44d920 5209 {
5210 num_reasons = 1;
f0ab5bff 5211 reasons = &none;
bc44d920 5212 }
5213 else
5214 {
b9faaae1
MS
5215 num_reasons = job->printer->num_reasons;
5216 reasons = (const char * const *)job->printer->reasons;
bc44d920 5217 }
5218
5219 if (!job->printer_reasons || job->printer_reasons->num_values != num_reasons)
5220 {
b9faaae1
MS
5221 /*
5222 * Replace/create a job-printer-state-reasons attribute...
5223 */
5224
bc44d920 5225 ippDeleteAttribute(job->attrs, job->printer_reasons);
5226
5227 job->printer_reasons = ippAddStrings(job->attrs,
5228 IPP_TAG_JOB, IPP_TAG_KEYWORD,
5229 "job-printer-state-reasons",
5230 num_reasons, NULL, NULL);
5231 }
b9faaae1 5232 else
d2354e63 5233 {
b9faaae1
MS
5234 /*
5235 * Don't bother clearing the reason strings if they are the same...
5236 */
5237
5238 for (i = 0; i < num_reasons; i ++)
5239 if (strcmp(job->printer_reasons->values[i].string.text, reasons[i]))
5240 break;
5241
5242 if (i >= num_reasons)
5243 return;
5244
5245 /*
5246 * Not the same, so free the current strings...
5247 */
5248
5249 for (i = 0; i < num_reasons; i ++)
d2354e63
MS
5250 _cupsStrFree(job->printer_reasons->values[i].string.text);
5251 }
bc44d920 5252
b9faaae1
MS
5253 /*
5254 * Copy the reasons...
5255 */
5256
bc44d920 5257 for (i = 0; i < num_reasons; i ++)
d2354e63 5258 job->printer_reasons->values[i].string.text = _cupsStrAlloc(reasons[i]);
dcb445bc
MS
5259
5260 job->dirty = 1;
5261 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
bc44d920 5262}
5263
5264
5265/*
f2d18633 5266 * End of "$Id$".
ef416fc2 5267 */