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