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