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