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