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