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