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