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