]> git.ipfire.org Git - thirdparty/cups.git/blame - scheduler/job.c
Merge changes from CUPS 1.7svn-r10755.
[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
2472 job->state->values[0].integer = newstate;
2473 job->state_value = newstate;
2474
2475 switch (newstate)
2476 {
2477 case IPP_JOB_PENDING :
2478 /*
2479 * Update job-hold-until as needed...
2480 */
2481
2482 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
2483 IPP_TAG_KEYWORD)) == NULL)
2484 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
2485
2486 if (attr)
ef416fc2 2487 {
b9faaae1
MS
2488 attr->value_tag = IPP_TAG_KEYWORD;
2489 cupsdSetString(&(attr->values[0].string.text), "no-hold");
e1d6a774 2490 }
ef416fc2 2491
b9faaae1
MS
2492 default :
2493 break;
ef416fc2 2494
b9faaae1
MS
2495 case IPP_JOB_ABORTED :
2496 case IPP_JOB_CANCELED :
2497 case IPP_JOB_COMPLETED :
2498 set_time(job, "time-at-completed");
12f89d24 2499 ippSetString(job->attrs, &job->reasons, 0, "processing-to-stop-point");
b9faaae1
MS
2500 break;
2501 }
ef416fc2 2502
b9faaae1
MS
2503 /*
2504 * Log message as needed...
2505 */
ef416fc2 2506
b9faaae1
MS
2507 if (message)
2508 {
2509 char buffer[2048]; /* Message buffer */
2510 va_list ap; /* Pointer to additional arguments */
ef416fc2 2511
b9faaae1
MS
2512 va_start(ap, message);
2513 vsnprintf(buffer, sizeof(buffer), message, ap);
2514 va_end(ap);
bd7854cb 2515
b9faaae1
MS
2516 if (newstate > IPP_JOB_STOPPED)
2517 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job, "%s", buffer);
2518 else
2519 cupsdAddEvent(CUPSD_EVENT_JOB_STATE, job->printer, job, "%s", buffer);
2520
2521 if (newstate == IPP_JOB_STOPPED || newstate == IPP_JOB_ABORTED)
2522 cupsdLogJob(job, CUPSD_LOG_ERROR, "%s", buffer);
2523 else
2524 cupsdLogJob(job, CUPSD_LOG_INFO, "%s", buffer);
2525 }
2526
2527 /*
2528 * Handle post-state-change actions...
2529 */
2530
2531 switch (newstate)
2532 {
2533 case IPP_JOB_PROCESSING :
e1d6a774 2534 /*
b9faaae1 2535 * Add the job to the "printing" list...
e1d6a774 2536 */
bd7854cb 2537
b9faaae1
MS
2538 if (!cupsArrayFind(PrintingJobs, job))
2539 cupsArrayAdd(PrintingJobs, job);
ef416fc2 2540
b9faaae1
MS
2541 /*
2542 * Set the processing time...
2543 */
ef416fc2 2544
b9faaae1
MS
2545 set_time(job, "time-at-processing");
2546
2547 case IPP_JOB_PENDING :
2548 case IPP_JOB_HELD :
2549 case IPP_JOB_STOPPED :
e1d6a774 2550 /*
b9faaae1 2551 * Make sure the job is in the active list...
e1d6a774 2552 */
ef416fc2 2553
b9faaae1
MS
2554 if (!cupsArrayFind(ActiveJobs, job))
2555 cupsArrayAdd(ActiveJobs, job);
ef416fc2 2556
b9faaae1
MS
2557 /*
2558 * Save the job state to disk...
2559 */
ef416fc2 2560
b9faaae1
MS
2561 job->dirty = 1;
2562 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
2563 break;
ef416fc2 2564
b9faaae1
MS
2565 case IPP_JOB_ABORTED :
2566 case IPP_JOB_CANCELED :
2567 case IPP_JOB_COMPLETED :
7a0cbd5e
MS
2568 if (newstate == IPP_JOB_CANCELED)
2569 {
2570 /*
2571 * Remove the job from the active list if there are no processes still
2572 * running for it...
2573 */
ef416fc2 2574
7a0cbd5e
MS
2575 for (i = 0; job->filters[i] < 0; i++);
2576
2577 if (!job->filters[i] && job->backend <= 0)
2578 cupsArrayRemove(ActiveJobs, job);
2579 }
2580 else
2581 {
2582 /*
2583 * Otherwise just remove the job from the active list immediately...
2584 */
2585
2586 cupsArrayRemove(ActiveJobs, job);
2587 }
ef416fc2 2588
b9faaae1 2589 /*
7a0cbd5e 2590 * Expire job subscriptions since the job is now "completed"...
b9faaae1 2591 */
ef416fc2 2592
7a0cbd5e 2593 cupsdExpireSubscriptions(NULL, job);
ef416fc2 2594
b9faaae1
MS
2595#ifdef __APPLE__
2596 /*
2597 * If we are going to sleep and the PrintingJobs count is now 0, allow the
2598 * sleep to happen immediately...
2599 */
ef416fc2 2600
b9faaae1
MS
2601 if (Sleeping && cupsArrayCount(PrintingJobs) == 0)
2602 cupsdAllowSleep();
2603#endif /* __APPLE__ */
ef416fc2 2604
b9faaae1
MS
2605 /*
2606 * Remove any authentication data...
2607 */
bd7854cb 2608
b9faaae1
MS
2609 snprintf(filename, sizeof(filename), "%s/a%05d", RequestRoot, job->id);
2610 if (cupsdRemoveFile(filename) && errno != ENOENT)
2611 cupsdLogMessage(CUPSD_LOG_ERROR,
2612 "Unable to remove authentication cache: %s",
2613 strerror(errno));
bd7854cb 2614
88f9aafc
MS
2615 for (i = 0;
2616 i < (int)(sizeof(job->auth_env) / sizeof(job->auth_env[0]));
2617 i ++)
2618 cupsdClearString(job->auth_env + i);
2619
07ed0e9a 2620 cupsdClearString(&job->auth_uid);
ef416fc2 2621
b9faaae1
MS
2622 /*
2623 * Remove the print file for good if we aren't preserving jobs or
2624 * files...
2625 */
e1d6a774 2626
b9faaae1 2627 if (!JobHistory || !JobFiles || action == CUPSD_JOB_PURGE)
82cc1f9a 2628 remove_job_files(job);
ef416fc2 2629
b9faaae1
MS
2630 if (JobHistory && action != CUPSD_JOB_PURGE)
2631 {
2632 /*
2633 * Save job state info...
2634 */
e1d6a774 2635
b9faaae1
MS
2636 job->dirty = 1;
2637 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
2638 }
ba55dc12
MS
2639 else if (!job->printer)
2640 {
2641 /*
2642 * Delete the job immediately if not actively printing...
2643 */
2644
2645 cupsdDeleteJob(job, CUPSD_JOB_PURGE);
2646 job = NULL;
2647 }
b9faaae1 2648 break;
e1d6a774 2649 }
2650
e07d4801
MS
2651 /*
2652 * Finalize the job immediately if we forced things...
2653 */
2654
ba55dc12 2655 if (action >= CUPSD_JOB_FORCE && job && job->printer)
ef55b745 2656 finalize_job(job, 0);
e07d4801 2657
e1d6a774 2658 /*
b9faaae1 2659 * Update the server "busy" state...
e1d6a774 2660 */
ef416fc2 2661
b9faaae1
MS
2662 cupsdSetBusyState();
2663}
e00b005a 2664
ef416fc2 2665
b9faaae1
MS
2666/*
2667 * 'cupsdStopAllJobs()' - Stop all print jobs.
2668 */
ef416fc2 2669
b9faaae1
MS
2670void
2671cupsdStopAllJobs(
238c3832
MS
2672 cupsd_jobaction_t action, /* I - Action */
2673 int kill_delay) /* I - Number of seconds before we kill */
b9faaae1
MS
2674{
2675 cupsd_job_t *job; /* Current job */
ef416fc2 2676
ef416fc2 2677
b9faaae1 2678 DEBUG_puts("cupsdStopAllJobs()");
ef416fc2 2679
b9faaae1
MS
2680 for (job = (cupsd_job_t *)cupsArrayFirst(PrintingJobs);
2681 job;
2682 job = (cupsd_job_t *)cupsArrayNext(PrintingJobs))
238c3832
MS
2683 {
2684 if (kill_delay)
2685 job->kill_time = time(NULL) + kill_delay;
2686
b9faaae1 2687 cupsdSetJobState(job, IPP_JOB_PENDING, action, NULL);
238c3832 2688 }
b9faaae1 2689}
bd7854cb 2690
bd7854cb 2691
b9faaae1
MS
2692/*
2693 * 'cupsdUnloadCompletedJobs()' - Flush completed job history from memory.
2694 */
ef416fc2 2695
b9faaae1
MS
2696void
2697cupsdUnloadCompletedJobs(void)
2698{
2699 cupsd_job_t *job; /* Current job */
2700 time_t expire; /* Expiration time */
ef416fc2 2701
b9faaae1
MS
2702
2703 expire = time(NULL) - 60;
2704
2705 for (job = (cupsd_job_t *)cupsArrayFirst(Jobs);
2706 job;
2707 job = (cupsd_job_t *)cupsArrayNext(Jobs))
2708 if (job->attrs && job->state_value >= IPP_JOB_STOPPED && !job->printer &&
2709 job->access_time < expire)
2710 {
2711 if (job->dirty)
2712 cupsdSaveJob(job);
2713
2714 unload_job(job);
2715 }
e1d6a774 2716}
ef416fc2 2717
ef416fc2 2718
82cc1f9a
MS
2719/*
2720 * 'cupsdUpdateJobs()' - Update the history/file files for all jobs.
2721 */
2722
2723void
2724cupsdUpdateJobs(void)
2725{
2726 cupsd_job_t *job; /* Current job */
2727 time_t curtime; /* Current time */
2728 ipp_attribute_t *attr; /* time-at-completed attribute */
2729
2730
2731 curtime = time(NULL);
2732 JobHistoryUpdate = 0;
2733
2734 for (job = (cupsd_job_t *)cupsArrayFirst(Jobs);
2735 job;
2736 job = (cupsd_job_t *)cupsArrayNext(Jobs))
2737 {
2738 if (job->state_value >= IPP_JOB_CANCELED &&
2739 (attr = ippFindAttribute(job->attrs, "time-at-completed",
2740 IPP_TAG_INTEGER)) != NULL)
2741 {
2742 /*
2743 * Update history/file expiration times...
2744 */
2745
2746 if (JobHistory < INT_MAX)
2747 job->history_time = attr->values[0].integer + JobHistory;
2748 else
2749 job->history_time = INT_MAX;
2750
2751 if (job->history_time < curtime)
2752 {
2753 cupsdDeleteJob(job, CUPSD_JOB_PURGE);
2754 continue;
2755 }
2756
2757 if (job->history_time < JobHistoryUpdate || !JobHistoryUpdate)
2758 JobHistoryUpdate = job->history_time;
2759
2760 if (JobFiles < INT_MAX)
2761 job->file_time = attr->values[0].integer + JobFiles;
2762 else
2763 job->file_time = INT_MAX;
2764
2765 if (job->file_time < JobHistoryUpdate || !JobHistoryUpdate)
2766 JobHistoryUpdate = job->file_time;
2767 }
2768 }
2769
2770 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdUpdateAllJobs: JobHistoryUpdate=%ld",
2771 (long)JobHistoryUpdate);
2772}
2773
2774
e1d6a774 2775/*
b9faaae1 2776 * 'compare_active_jobs()' - Compare the job IDs and priorities of two jobs.
e1d6a774 2777 */
ef416fc2 2778
b9faaae1
MS
2779static int /* O - Difference */
2780compare_active_jobs(void *first, /* I - First job */
2781 void *second, /* I - Second job */
2782 void *data) /* I - App data (not used) */
e1d6a774 2783{
b9faaae1 2784 int diff; /* Difference */
ef416fc2 2785
ef416fc2 2786
321d8d57
MS
2787 (void)data;
2788
b9faaae1
MS
2789 if ((diff = ((cupsd_job_t *)second)->priority -
2790 ((cupsd_job_t *)first)->priority) != 0)
2791 return (diff);
2792 else
2793 return (((cupsd_job_t *)first)->id - ((cupsd_job_t *)second)->id);
e1d6a774 2794}
bd7854cb 2795
ef416fc2 2796
e1d6a774 2797/*
b9faaae1 2798 * 'compare_jobs()' - Compare the job IDs of two jobs.
e1d6a774 2799 */
ef416fc2 2800
b9faaae1
MS
2801static int /* O - Difference */
2802compare_jobs(void *first, /* I - First job */
2803 void *second, /* I - Second job */
2804 void *data) /* I - App data (not used) */
e1d6a774 2805{
321d8d57
MS
2806 (void)data;
2807
b9faaae1
MS
2808 return (((cupsd_job_t *)first)->id - ((cupsd_job_t *)second)->id);
2809}
ef416fc2 2810
ef416fc2 2811
178cb736
MS
2812/*
2813 * 'dump_job_history()' - Dump any debug messages for a job.
2814 */
2815
2816static void
2817dump_job_history(cupsd_job_t *job) /* I - Job */
2818{
2819 int i, /* Looping var */
2820 oldsize; /* Current MaxLogSize */
2821 struct tm *date; /* Date/time value */
2822 cupsd_joblog_t *message; /* Current message */
2823 char temp[2048], /* Log message */
2824 *ptr, /* Pointer into log message */
2825 start[256], /* Start time */
2826 end[256]; /* End time */
2827 cupsd_printer_t *printer; /* Printer for job */
2828
2829
2830 /*
2831 * See if we have anything to dump...
2832 */
2833
2834 if (!job->history)
2835 return;
2836
2837 /*
2838 * Disable log rotation temporarily...
2839 */
2840
2841 oldsize = MaxLogSize;
2842 MaxLogSize = 0;
2843
2844 /*
2845 * Copy the debug messages to the log...
2846 */
2847
2848 message = (cupsd_joblog_t *)cupsArrayFirst(job->history);
2849 date = localtime(&(message->time));
2850 strftime(start, sizeof(start), "%X", date);
2851
2852 message = (cupsd_joblog_t *)cupsArrayLast(job->history);
2853 date = localtime(&(message->time));
2854 strftime(end, sizeof(end), "%X", date);
2855
2856 snprintf(temp, sizeof(temp),
2857 "[Job %d] The following messages were recorded from %s to %s",
2858 job->id, start, end);
2859 cupsdWriteErrorLog(CUPSD_LOG_DEBUG, temp);
2860
2861 for (message = (cupsd_joblog_t *)cupsArrayFirst(job->history);
2862 message;
2863 message = (cupsd_joblog_t *)cupsArrayNext(job->history))
2864 cupsdWriteErrorLog(CUPSD_LOG_DEBUG, message->message);
2865
2866 snprintf(temp, sizeof(temp), "[Job %d] End of messages", job->id);
2867 cupsdWriteErrorLog(CUPSD_LOG_DEBUG, temp);
2868
2869 /*
2870 * Log the printer state values...
2871 */
2872
2873 if ((printer = job->printer) == NULL)
2874 printer = cupsdFindDest(job->dest);
2875
2876 if (printer)
2877 {
2878 snprintf(temp, sizeof(temp), "[Job %d] printer-state=%d(%s)", job->id,
2879 printer->state,
2880 printer->state == IPP_PRINTER_IDLE ? "idle" :
2881 printer->state == IPP_PRINTER_PROCESSING ? "processing" :
2882 "stopped");
2883 cupsdWriteErrorLog(CUPSD_LOG_DEBUG, temp);
2884
2885 snprintf(temp, sizeof(temp), "[Job %d] printer-state-message=\"%s\"",
2886 job->id, printer->state_message);
2887 cupsdWriteErrorLog(CUPSD_LOG_DEBUG, temp);
2888
2889 snprintf(temp, sizeof(temp), "[Job %d] printer-state-reasons=", job->id);
2890 ptr = temp + strlen(temp);
2891 if (printer->num_reasons == 0)
2892 strlcpy(ptr, "none", sizeof(temp) - (ptr - temp));
2893 else
2894 {
2895 for (i = 0;
2896 i < printer->num_reasons && ptr < (temp + sizeof(temp) - 2);
2897 i ++)
2898 {
2899 if (i)
2900 *ptr++ = ',';
2901
2902 strlcpy(ptr, printer->reasons[i], sizeof(temp) - (ptr - temp));
2903 ptr += strlen(ptr);
2904 }
2905 }
2906 cupsdWriteErrorLog(CUPSD_LOG_DEBUG, temp);
2907 }
2908
2909 /*
2910 * Restore log file rotation...
2911 */
2912
2913 MaxLogSize = oldsize;
2914
2915 /*
2916 * Free all messages...
2917 */
2918
2919 free_job_history(job);
2920}
2921
2922
2923/*
2924 * 'free_job_history()' - Free any log history.
2925 */
2926
2927static void
2928free_job_history(cupsd_job_t *job) /* I - Job */
2929{
2930 char *message; /* Current message */
2931
2932
2933 if (!job->history)
2934 return;
2935
2936 for (message = (char *)cupsArrayFirst(job->history);
2937 message;
2938 message = (char *)cupsArrayNext(job->history))
2939 free(message);
2940
2941 cupsArrayDelete(job->history);
2942 job->history = NULL;
2943}
2944
2945
b9faaae1
MS
2946/*
2947 * 'finalize_job()' - Cleanup after job filter processes and support data.
2948 */
ef416fc2 2949
b9faaae1 2950static void
ef55b745
MS
2951finalize_job(cupsd_job_t *job, /* I - Job */
2952 int set_job_state) /* I - 1 = set the job state */
b9faaae1
MS
2953{
2954 ipp_pstate_t printer_state; /* New printer state value */
2955 ipp_jstate_t job_state; /* New job state value */
2956 const char *message; /* Message for job state */
2957 char buffer[1024]; /* Buffer for formatted messages */
e00b005a 2958
b9faaae1
MS
2959
2960 cupsdLogMessage(CUPSD_LOG_DEBUG2, "finalize_job(job=%p(%d))", job, job->id);
ef416fc2 2961
e1d6a774 2962 /*
4d301e69 2963 * Clear the "connecting-to-device" reason, which is only valid when a printer
07ed0e9a 2964 * is processing, along with any remote printing job state...
e1d6a774 2965 */
ef416fc2 2966
07ed0e9a
MS
2967 cupsdSetPrinterReasons(job->printer, "-connecting-to-device,"
2968 "cups-remote-pending,"
2969 "cups-remote-pending-held,"
2970 "cups-remote-processing,"
2971 "cups-remote-stopped,"
2972 "cups-remote-canceled,"
2973 "cups-remote-aborted,"
2974 "cups-remote-completed");
ef416fc2 2975
e1d6a774 2976 /*
b9faaae1
MS
2977 * Similarly, clear the "offline-report" reason for non-USB devices since we
2978 * rarely have current information for network devices...
e1d6a774 2979 */
ef416fc2 2980
a469f8a5
MS
2981 if (strncmp(job->printer->device_uri, "usb:", 4) &&
2982 strncmp(job->printer->device_uri, "ippusb:", 7))
b9faaae1 2983 cupsdSetPrinterReasons(job->printer, "-offline-report");
ef416fc2 2984
b9faaae1
MS
2985 /*
2986 * Free the security profile...
2987 */
ef416fc2 2988
b9faaae1
MS
2989 cupsdDestroyProfile(job->profile);
2990 job->profile = NULL;
ef416fc2 2991
10d09e33 2992 /*
a469f8a5 2993 * Clear the unresponsive job watchdog timers...
10d09e33
MS
2994 */
2995
a469f8a5
MS
2996 job->cancel_time = 0;
2997 job->kill_time = 0;
10d09e33 2998
b9faaae1
MS
2999 /*
3000 * Close pipes and status buffer...
3001 */
ef416fc2 3002
b9faaae1
MS
3003 cupsdClosePipe(job->print_pipes);
3004 cupsdClosePipe(job->back_pipes);
3005 cupsdClosePipe(job->side_pipes);
ef416fc2 3006
e07d4801
MS
3007 cupsdRemoveSelect(job->status_pipes[0]);
3008 cupsdClosePipe(job->status_pipes);
b9faaae1
MS
3009 cupsdStatBufDelete(job->status_buffer);
3010 job->status_buffer = NULL;
ef416fc2 3011
e1d6a774 3012 /*
b9faaae1 3013 * Process the exit status...
e1d6a774 3014 */
ef416fc2 3015
1340db2d
MS
3016 if (job->printer->state == IPP_PRINTER_PROCESSING)
3017 printer_state = IPP_PRINTER_IDLE;
3018 else
3019 printer_state = job->printer->state;
3020
3021 switch (job_state = job->state_value)
3022 {
3023 case IPP_JOB_PENDING :
3024 message = "Job paused.";
3025 break;
3026
3027 case IPP_JOB_HELD :
3028 message = "Job held.";
3029 break;
3030
3031 default :
3032 case IPP_JOB_PROCESSING :
3033 case IPP_JOB_COMPLETED :
f11a948a
MS
3034 job_state = IPP_JOB_COMPLETED;
3035 message = "Job completed.";
12f89d24
MS
3036
3037 ippSetString(job->attrs, &job->reasons, 0,
3038 "job-completed-successfully");
1340db2d
MS
3039 break;
3040
3041 case IPP_JOB_STOPPED :
3042 message = "Job stopped.";
12f89d24
MS
3043
3044 ippSetString(job->attrs, &job->reasons, 0, "job-stopped");
1340db2d
MS
3045 break;
3046
3047 case IPP_JOB_CANCELED :
3048 message = "Job canceled.";
12f89d24
MS
3049
3050 ippSetString(job->attrs, &job->reasons, 0, "job-canceled-by-user");
1340db2d
MS
3051 break;
3052
3053 case IPP_JOB_ABORTED :
3054 message = "Job aborted.";
3055 break;
3056 }
ef416fc2 3057
b9faaae1 3058 if (job->status < 0)
e1d6a774 3059 {
3060 /*
b9faaae1 3061 * Backend had errors...
e1d6a774 3062 */
ef416fc2 3063
b9faaae1
MS
3064 int exit_code; /* Exit code from backend */
3065
e1d6a774 3066 /*
b9faaae1
MS
3067 * Convert the status to an exit code. Due to the way the W* macros are
3068 * implemented on MacOS X (bug?), we have to store the exit status in a
3069 * variable first and then convert...
e1d6a774 3070 */
ef416fc2 3071
b9faaae1
MS
3072 exit_code = -job->status;
3073 if (WIFEXITED(exit_code))
3074 exit_code = WEXITSTATUS(exit_code);
3075 else
12f89d24
MS
3076 {
3077 ippSetString(job->attrs, &job->reasons, 0, "cups-backend-crashed");
b9faaae1 3078 exit_code = job->status;
12f89d24 3079 }
ef416fc2 3080
b9faaae1
MS
3081 cupsdLogJob(job, CUPSD_LOG_INFO, "Backend returned status %d (%s)",
3082 exit_code,
3083 exit_code == CUPS_BACKEND_FAILED ? "failed" :
3084 exit_code == CUPS_BACKEND_AUTH_REQUIRED ?
3085 "authentication required" :
3086 exit_code == CUPS_BACKEND_HOLD ? "hold job" :
3087 exit_code == CUPS_BACKEND_STOP ? "stop printer" :
3088 exit_code == CUPS_BACKEND_CANCEL ? "cancel job" :
a469f8a5
MS
3089 exit_code == CUPS_BACKEND_RETRY ? "retry job later" :
3090 exit_code == CUPS_BACKEND_RETRY_CURRENT ? "retry job immediately" :
b9faaae1 3091 exit_code < 0 ? "crashed" : "unknown");
ef416fc2 3092
ef416fc2 3093 /*
b9faaae1 3094 * Do what needs to be done...
ef416fc2 3095 */
3096
b9faaae1 3097 switch (exit_code)
f7deaa1a 3098 {
b9faaae1
MS
3099 default :
3100 case CUPS_BACKEND_FAILED :
3101 /*
3102 * Backend failure, use the error-policy to determine how to
3103 * act...
3104 */
f7deaa1a 3105
a2326b5b 3106 if (job->dtype & CUPS_PRINTER_CLASS)
b9faaae1
MS
3107 {
3108 /*
3109 * Queued on a class - mark the job as pending and we'll retry on
3110 * another printer...
3111 */
f7deaa1a 3112
8b116e60
MS
3113 if (job_state == IPP_JOB_COMPLETED)
3114 {
3115 job_state = IPP_JOB_PENDING;
3116 message = "Retrying job on another printer.";
12f89d24
MS
3117
3118 ippSetString(job->attrs, &job->reasons, 0,
3119 "resources-are-not-ready");
8b116e60 3120 }
b9faaae1
MS
3121 }
3122 else if (!strcmp(job->printer->error_policy, "retry-current-job"))
3123 {
3124 /*
3125 * The error policy is "retry-current-job" - mark the job as pending
3126 * and we'll retry on the same printer...
3127 */
f7deaa1a 3128
8b116e60
MS
3129 if (job_state == IPP_JOB_COMPLETED)
3130 {
3131 job_state = IPP_JOB_PENDING;
3132 message = "Retrying job on same printer.";
12f89d24
MS
3133
3134 ippSetString(job->attrs, &job->reasons, 0, "none");
8b116e60 3135 }
b9faaae1
MS
3136 }
3137 else if ((job->printer->type & CUPS_PRINTER_FAX) ||
3138 !strcmp(job->printer->error_policy, "retry-job"))
3139 {
8b116e60 3140 if (job_state == IPP_JOB_COMPLETED)
b9faaae1
MS
3141 {
3142 /*
8b116e60
MS
3143 * The job was queued on a fax or the error policy is "retry-job" -
3144 * hold the job if the number of retries is less than the
3145 * JobRetryLimit, otherwise abort the job.
b9faaae1 3146 */
ef416fc2 3147
8b116e60
MS
3148 job->tries ++;
3149
7cf5915e 3150 if (job->tries > JobRetryLimit && JobRetryLimit > 0)
8b116e60
MS
3151 {
3152 /*
3153 * Too many tries...
3154 */
3155
3156 snprintf(buffer, sizeof(buffer),
3157 "Job aborted after %d unsuccessful attempts.",
3158 JobRetryLimit);
3159 job_state = IPP_JOB_ABORTED;
3160 message = buffer;
12f89d24
MS
3161
3162 ippSetString(job->attrs, &job->reasons, 0, "aborted-by-system");
8b116e60
MS
3163 }
3164 else
3165 {
3166 /*
3167 * Try again in N seconds...
3168 */
ef416fc2 3169
8b116e60
MS
3170 snprintf(buffer, sizeof(buffer),
3171 "Job held for %d seconds since it could not be sent.",
3172 JobRetryInterval);
0268488e
MS
3173
3174 job->hold_until = time(NULL) + JobRetryInterval;
3175 job_state = IPP_JOB_HELD;
3176 message = buffer;
12f89d24
MS
3177
3178 ippSetString(job->attrs, &job->reasons, 0,
3179 "resources-are-not-ready");
8b116e60
MS
3180 }
3181 }
b9faaae1 3182 }
8b116e60
MS
3183 else if (!strcmp(job->printer->error_policy, "abort-job") &&
3184 job_state == IPP_JOB_COMPLETED)
b9faaae1
MS
3185 {
3186 job_state = IPP_JOB_ABORTED;
3187 message = "Job aborted due to backend errors; please consult "
3188 "the error_log file for details.";
12f89d24
MS
3189
3190 ippSetString(job->attrs, &job->reasons, 0, "aborted-by-system");
b9faaae1 3191 }
f11a948a 3192 else if (job->state_value == IPP_JOB_PROCESSING)
b9faaae1 3193 {
f11a948a 3194 job_state = IPP_JOB_PENDING;
b9faaae1 3195 printer_state = IPP_PRINTER_STOPPED;
b9faaae1 3196 message = "Printer stopped due to backend errors; please "
8b116e60 3197 "consult the error_log file for details.";
12f89d24
MS
3198
3199 ippSetString(job->attrs, &job->reasons, 0, "none");
b9faaae1
MS
3200 }
3201 break;
bd7854cb 3202
b9faaae1
MS
3203 case CUPS_BACKEND_CANCEL :
3204 /*
f3c17241 3205 * Cancel the job...
b9faaae1 3206 */
bd7854cb 3207
8b116e60
MS
3208 if (job_state == IPP_JOB_COMPLETED)
3209 {
f3c17241
MS
3210 job_state = IPP_JOB_CANCELED;
3211 message = "Job canceled at printer.";
12f89d24 3212
f3c17241 3213 ippSetString(job->attrs, &job->reasons, 0, "canceled-at-device");
8b116e60 3214 }
b9faaae1 3215 break;
bd7854cb 3216
b9faaae1 3217 case CUPS_BACKEND_HOLD :
8b116e60
MS
3218 if (job_state == IPP_JOB_COMPLETED)
3219 {
3220 /*
3221 * Hold the job...
3222 */
bd7854cb 3223
8b116e60 3224 cupsdSetJobHoldUntil(job, "indefinite", 1);
12f89d24
MS
3225 ippSetString(job->attrs, &job->reasons, 0,
3226 "job-hold-until-specified");
bd7854cb 3227
8b116e60
MS
3228 job_state = IPP_JOB_HELD;
3229 message = "Job held indefinitely due to backend errors; please "
3230 "consult the error_log file for details.";
3231 }
b9faaae1 3232 break;
2abf387c 3233
b9faaae1
MS
3234 case CUPS_BACKEND_STOP :
3235 /*
3236 * Stop the printer...
3237 */
bd7854cb 3238
b9faaae1 3239 printer_state = IPP_PRINTER_STOPPED;
b9faaae1
MS
3240 message = "Printer stopped due to backend errors; please "
3241 "consult the error_log file for details.";
8b116e60
MS
3242
3243 if (job_state == IPP_JOB_COMPLETED)
12f89d24 3244 {
8b116e60 3245 job_state = IPP_JOB_PENDING;
12f89d24
MS
3246
3247 ippSetString(job->attrs, &job->reasons, 0,
3248 "resources-are-not-ready");
3249 }
b9faaae1 3250 break;
bd7854cb 3251
b9faaae1
MS
3252 case CUPS_BACKEND_AUTH_REQUIRED :
3253 /*
3254 * Hold the job for authentication...
3255 */
bd7854cb 3256
8b116e60
MS
3257 if (job_state == IPP_JOB_COMPLETED)
3258 {
3259 cupsdSetJobHoldUntil(job, "auth-info-required", 1);
ef416fc2 3260
8b116e60
MS
3261 job_state = IPP_JOB_HELD;
3262 message = "Job held for authentication.";
12f89d24 3263
a469f8a5
MS
3264 if (strncmp(job->reasons->values[0].string.text, "account-", 8))
3265 ippSetString(job->attrs, &job->reasons, 0,
3266 "cups-held-for-authentication");
8b116e60 3267 }
b9faaae1 3268 break;
22c9029b
MS
3269
3270 case CUPS_BACKEND_RETRY :
3271 if (job_state == IPP_JOB_COMPLETED)
3272 {
3273 /*
3274 * Hold the job if the number of retries is less than the
3275 * JobRetryLimit, otherwise abort the job.
3276 */
3277
3278 job->tries ++;
3279
3280 if (job->tries > JobRetryLimit && JobRetryLimit > 0)
3281 {
3282 /*
3283 * Too many tries...
3284 */
3285
3286 snprintf(buffer, sizeof(buffer),
3287 "Job aborted after %d unsuccessful attempts.",
3288 JobRetryLimit);
3289 job_state = IPP_JOB_ABORTED;
3290 message = buffer;
12f89d24
MS
3291
3292 ippSetString(job->attrs, &job->reasons, 0, "aborted-by-system");
22c9029b
MS
3293 }
3294 else
3295 {
3296 /*
3297 * Try again in N seconds...
3298 */
3299
3300 snprintf(buffer, sizeof(buffer),
3301 "Job held for %d seconds since it could not be sent.",
3302 JobRetryInterval);
3303
3304 job->hold_until = time(NULL) + JobRetryInterval;
3305 job_state = IPP_JOB_HELD;
3306 message = buffer;
12f89d24
MS
3307
3308 ippSetString(job->attrs, &job->reasons, 0,
3309 "resources-are-not-ready");
22c9029b
MS
3310 }
3311 }
3312 break;
3313
3314 case CUPS_BACKEND_RETRY_CURRENT :
3315 /*
3316 * Mark the job as pending and retry on the same printer...
3317 */
3318
3319 if (job_state == IPP_JOB_COMPLETED)
3320 {
3321 job_state = IPP_JOB_PENDING;
3322 message = "Retrying job on same printer.";
12f89d24
MS
3323
3324 ippSetString(job->attrs, &job->reasons, 0, "none");
22c9029b
MS
3325 }
3326 break;
e1d6a774 3327 }
3328 }
b9faaae1 3329 else if (job->status > 0)
ef416fc2 3330 {
3331 /*
b9faaae1 3332 * Filter had errors; stop job...
ef416fc2 3333 */
3334
8b116e60
MS
3335 if (job_state == IPP_JOB_COMPLETED)
3336 {
3337 job_state = IPP_JOB_STOPPED;
3338 message = "Job stopped due to filter errors; please consult the "
3339 "error_log file for details.";
12f89d24
MS
3340
3341 if (WIFSIGNALED(job->status))
3342 ippSetString(job->attrs, &job->reasons, 0, "cups-filter-crashed");
3343 else
3344 ippSetString(job->attrs, &job->reasons, 0, "job-completed-with-errors");
8b116e60 3345 }
e1d6a774 3346 }
e00b005a 3347
3dfe78b3 3348 /*
b9faaae1 3349 * Update the printer and job state.
3dfe78b3
MS
3350 */
3351
321d8d57 3352 if (set_job_state && job_state != job->state_value)
ef55b745
MS
3353 cupsdSetJobState(job, job_state, CUPSD_JOB_DEFAULT, "%s", message);
3354
b9faaae1
MS
3355 cupsdSetPrinterState(job->printer, printer_state,
3356 printer_state == IPP_PRINTER_STOPPED);
3357 update_job_attrs(job, 0);
3dfe78b3 3358
178cb736
MS
3359 if (job->history)
3360 {
a4845881
MS
3361 if (job->status &&
3362 (job->state_value == IPP_JOB_ABORTED ||
3363 job->state_value == IPP_JOB_STOPPED))
178cb736
MS
3364 dump_job_history(job);
3365 else
3366 free_job_history(job);
3367 }
3368
b9faaae1 3369 cupsArrayRemove(PrintingJobs, job);
3dfe78b3 3370
a29fd7dd
MS
3371 /*
3372 * Apply any PPD updates...
3373 */
3374
3375 if (job->num_keywords)
3376 {
3377 if (cupsdUpdatePrinterPPD(job->printer, job->num_keywords, job->keywords))
3378 cupsdSetPrinterAttrs(job->printer);
3379
3380 cupsFreeOptions(job->num_keywords, job->keywords);
3381
3382 job->num_keywords = 0;
3383 job->keywords = NULL;
3384 }
3385
e1d6a774 3386 /*
b9faaae1 3387 * Clear the printer <-> job association...
e1d6a774 3388 */
ef416fc2 3389
b9faaae1
MS
3390 job->printer->job = NULL;
3391 job->printer = NULL;
ef416fc2 3392
e1d6a774 3393 /*
b9faaae1 3394 * Try printing another job...
e1d6a774 3395 */
ef416fc2 3396
b9faaae1
MS
3397 if (printer_state != IPP_PRINTER_STOPPED)
3398 cupsdCheckJobs();
3399}
ef416fc2 3400
ef416fc2 3401
b9faaae1
MS
3402/*
3403 * 'get_options()' - Get a string containing the job options.
3404 */
3405
3406static char * /* O - Options string */
3407get_options(cupsd_job_t *job, /* I - Job */
3408 int banner_page, /* I - Printing a banner page? */
3409 char *copies, /* I - Copies buffer */
3410 size_t copies_size, /* I - Size of copies buffer */
3411 char *title, /* I - Title buffer */
3412 size_t title_size) /* I - Size of title buffer */
3413{
3414 int i; /* Looping var */
c7017ecc 3415 size_t newlength; /* New option buffer length */
b9faaae1
MS
3416 char *optptr, /* Pointer to options */
3417 *valptr; /* Pointer in value string */
3418 ipp_attribute_t *attr; /* Current attribute */
f14324a7 3419 _ppd_cache_t *pc; /* PPD cache and mapping data */
c7017ecc
MS
3420 int num_pwgppds; /* Number of PWG->PPD options */
3421 cups_option_t *pwgppds, /* PWG->PPD options */
3422 *pwgppd, /* Current PWG->PPD option */
3423 *preset; /* Current preset option */
f14324a7
MS
3424 int print_color_mode,
3425 /* Output mode (if any) */
c7017ecc
MS
3426 print_quality; /* Print quality (if any) */
3427 const char *ppd; /* PPD option choice */
3428 int exact; /* Did we get an exact match? */
b9faaae1 3429 static char *options = NULL;/* Full list of options */
c7017ecc 3430 static size_t optlength = 0; /* Length of option buffer */
b9faaae1 3431
ef416fc2 3432
e1d6a774 3433 /*
c7017ecc
MS
3434 * Building the options string is harder than it needs to be, but for the
3435 * moment we need to pass strings for command-line args and not IPP attribute
3436 * pointers... :)
e1d6a774 3437 *
c7017ecc 3438 * First build an options array for any PWG->PPD mapped option/choice pairs.
e1d6a774 3439 */
ef416fc2 3440
f14324a7 3441 pc = job->printer->pc;
c7017ecc
MS
3442 num_pwgppds = 0;
3443 pwgppds = NULL;
ef416fc2 3444
f14324a7 3445 if (pc &&
7cf5915e
MS
3446 !ippFindAttribute(job->attrs,
3447 "com.apple.print.DocumentTicket.PMSpoolFormat",
3448 IPP_TAG_ZERO) &&
0268488e 3449 !ippFindAttribute(job->attrs, "APPrinterPreset", IPP_TAG_ZERO) &&
5a9febac 3450 (ippFindAttribute(job->attrs, "print-color-mode", IPP_TAG_ZERO) ||
7cf5915e 3451 ippFindAttribute(job->attrs, "print-quality", IPP_TAG_ZERO)))
c7017ecc 3452 {
7cf5915e 3453 /*
5a9febac 3454 * Map print-color-mode and print-quality to a preset...
7cf5915e
MS
3455 */
3456
f14324a7 3457 if ((attr = ippFindAttribute(job->attrs, "print-color-mode",
5a9febac
MS
3458 IPP_TAG_KEYWORD)) != NULL &&
3459 !strcmp(attr->values[0].string.text, "monochrome"))
f14324a7 3460 print_color_mode = _PWG_PRINT_COLOR_MODE_MONOCHROME;
c7017ecc 3461 else
f14324a7 3462 print_color_mode = _PWG_PRINT_COLOR_MODE_COLOR;
c7017ecc
MS
3463
3464 if ((attr = ippFindAttribute(job->attrs, "print-quality",
10d09e33 3465 IPP_TAG_ENUM)) != NULL &&
c7017ecc
MS
3466 attr->values[0].integer >= IPP_QUALITY_DRAFT &&
3467 attr->values[0].integer <= IPP_QUALITY_HIGH)
3468 print_quality = attr->values[0].integer - IPP_QUALITY_DRAFT;
3469 else
030ae6a1 3470 print_quality = _PWG_PRINT_QUALITY_NORMAL;
c7017ecc 3471
f14324a7 3472 if (pc->num_presets[print_color_mode][print_quality] == 0)
c7017ecc
MS
3473 {
3474 /*
3475 * Try to find a preset that works so that we maximize the chances of us
3476 * getting a good print using IPP attributes.
3477 */
3478
f14324a7 3479 if (pc->num_presets[print_color_mode][_PWG_PRINT_QUALITY_NORMAL] > 0)
c7017ecc 3480 print_quality = _PWG_PRINT_QUALITY_NORMAL;
f14324a7
MS
3481 else if (pc->num_presets[_PWG_PRINT_COLOR_MODE_COLOR][print_quality] > 0)
3482 print_color_mode = _PWG_PRINT_COLOR_MODE_COLOR;
c7017ecc
MS
3483 else
3484 {
f14324a7
MS
3485 print_quality = _PWG_PRINT_QUALITY_NORMAL;
3486 print_color_mode = _PWG_PRINT_COLOR_MODE_COLOR;
c7017ecc
MS
3487 }
3488 }
3489
f14324a7 3490 if (pc->num_presets[print_color_mode][print_quality] > 0)
c7017ecc
MS
3491 {
3492 /*
3493 * Copy the preset options as long as the corresponding names are not
3494 * already defined in the IPP request...
3495 */
3496
f14324a7
MS
3497 for (i = pc->num_presets[print_color_mode][print_quality],
3498 preset = pc->presets[print_color_mode][print_quality];
c7017ecc
MS
3499 i > 0;
3500 i --, preset ++)
3501 {
3502 if (!ippFindAttribute(job->attrs, preset->name, IPP_TAG_ZERO))
3503 num_pwgppds = cupsAddOption(preset->name, preset->value, num_pwgppds,
3504 &pwgppds);
3505 }
3506 }
7cf5915e 3507 }
c7017ecc 3508
f14324a7 3509 if (pc)
7cf5915e 3510 {
4220952d
MS
3511 if (!ippFindAttribute(job->attrs, "InputSlot", IPP_TAG_ZERO) &&
3512 !ippFindAttribute(job->attrs, "HPPaperSource", IPP_TAG_ZERO))
3513 {
f14324a7
MS
3514 if ((ppd = _ppdCacheGetInputSlot(pc, job->attrs, NULL)) != NULL)
3515 num_pwgppds = cupsAddOption(pc->source_option, ppd, num_pwgppds,
4220952d 3516 &pwgppds);
4220952d 3517 }
4220952d 3518 if (!ippFindAttribute(job->attrs, "MediaType", IPP_TAG_ZERO) &&
f14324a7 3519 (ppd = _ppdCacheGetMediaType(pc, job->attrs, NULL)) != NULL)
4220952d 3520 num_pwgppds = cupsAddOption("MediaType", ppd, num_pwgppds, &pwgppds);
c7017ecc 3521
4220952d
MS
3522 if (!ippFindAttribute(job->attrs, "PageRegion", IPP_TAG_ZERO) &&
3523 !ippFindAttribute(job->attrs, "PageSize", IPP_TAG_ZERO) &&
f14324a7 3524 (ppd = _ppdCacheGetPageSize(pc, job->attrs, NULL, &exact)) != NULL)
030ae6a1 3525 {
4220952d 3526 num_pwgppds = cupsAddOption("PageSize", ppd, num_pwgppds, &pwgppds);
c7017ecc 3527
030ae6a1
MS
3528 if (!ippFindAttribute(job->attrs, "media", IPP_TAG_ZERO))
3529 num_pwgppds = cupsAddOption("media", ppd, num_pwgppds, &pwgppds);
3530 }
3531
4220952d
MS
3532 if (!ippFindAttribute(job->attrs, "OutputBin", IPP_TAG_ZERO) &&
3533 (attr = ippFindAttribute(job->attrs, "output-bin",
3534 IPP_TAG_ZERO)) != NULL &&
3535 (attr->value_tag == IPP_TAG_KEYWORD ||
3536 attr->value_tag == IPP_TAG_NAME) &&
f14324a7 3537 (ppd = _ppdCacheGetOutputBin(pc, attr->values[0].string.text)) != NULL)
0268488e
MS
3538 {
3539 /*
3540 * Map output-bin to OutputBin option...
3541 */
3542
4220952d 3543 num_pwgppds = cupsAddOption("OutputBin", ppd, num_pwgppds, &pwgppds);
0268488e
MS
3544 }
3545
f14324a7
MS
3546 if (pc->sides_option &&
3547 !ippFindAttribute(job->attrs, pc->sides_option, IPP_TAG_ZERO) &&
0268488e
MS
3548 (attr = ippFindAttribute(job->attrs, "sides", IPP_TAG_KEYWORD)) != NULL)
3549 {
3550 /*
3551 * Map sides to duplex option...
3552 */
3553
3554 if (!strcmp(attr->values[0].string.text, "one-sided"))
f14324a7 3555 num_pwgppds = cupsAddOption(pc->sides_option, pc->sides_1sided,
0268488e
MS
3556 num_pwgppds, &pwgppds);
3557 else if (!strcmp(attr->values[0].string.text, "two-sided-long-edge"))
f14324a7 3558 num_pwgppds = cupsAddOption(pc->sides_option, pc->sides_2sided_long,
0268488e
MS
3559 num_pwgppds, &pwgppds);
3560 else if (!strcmp(attr->values[0].string.text, "two-sided-short-edge"))
f14324a7 3561 num_pwgppds = cupsAddOption(pc->sides_option, pc->sides_2sided_short,
0268488e
MS
3562 num_pwgppds, &pwgppds);
3563 }
dcb445bc
MS
3564
3565 /*
3566 * Map finishings values...
3567 */
3568
3569 num_pwgppds = _ppdCacheGetFinishingOptions(pc, job->attrs,
3570 IPP_FINISHINGS_NONE, num_pwgppds,
3571 &pwgppds);
4220952d 3572 }
c7017ecc
MS
3573
3574 /*
3575 * Figure out how much room we need...
3576 */
3577
3578 newlength = ipp_length(job->attrs);
3579
3580 for (i = num_pwgppds, pwgppd = pwgppds; i > 0; i --, pwgppd ++)
3581 newlength += 1 + strlen(pwgppd->name) + 1 + strlen(pwgppd->value);
3582
3583 /*
3584 * Then allocate/reallocate the option buffer as needed...
3585 */
3586
85dda01c
MS
3587 if (newlength == 0) /* This can never happen, but Clang */
3588 newlength = 1; /* thinks it can... */
3589
c7017ecc 3590 if (newlength > optlength || !options)
e1d6a774 3591 {
b9faaae1 3592 if (!options)
c7017ecc 3593 optptr = malloc(newlength);
e1d6a774 3594 else
c7017ecc 3595 optptr = realloc(options, newlength);
ef416fc2 3596
b9faaae1 3597 if (!optptr)
e1d6a774 3598 {
75bd9771 3599 cupsdLogJob(job, CUPSD_LOG_CRIT,
12f89d24 3600 "Unable to allocate " CUPS_LLFMT " bytes for option buffer.",
c7017ecc 3601 CUPS_LLCAST newlength);
b9faaae1 3602 return (NULL);
e1d6a774 3603 }
ef416fc2 3604
e1d6a774 3605 options = optptr;
c7017ecc 3606 optlength = newlength;
e1d6a774 3607 }
ef416fc2 3608
e1d6a774 3609 /*
3610 * Now loop through the attributes and convert them to the textual
3611 * representation used by the filters...
3612 */
ef416fc2 3613
e1d6a774 3614 optptr = options;
3615 *optptr = '\0';
bd7854cb 3616
1340db2d
MS
3617 snprintf(title, title_size, "%s-%d", job->printer->name, job->id);
3618 strlcpy(copies, "1", copies_size);
bd7854cb 3619
e1d6a774 3620 for (attr = job->attrs->attrs; attr != NULL; attr = attr->next)
bd7854cb 3621 {
e1d6a774 3622 if (!strcmp(attr->name, "copies") &&
3623 attr->value_tag == IPP_TAG_INTEGER)
3624 {
3625 /*
3626 * Don't use the # copies attribute if we are printing the job sheets...
3627 */
bd7854cb 3628
e1d6a774 3629 if (!banner_page)
1340db2d 3630 snprintf(copies, copies_size, "%d", attr->values[0].integer);
e1d6a774 3631 }
3632 else if (!strcmp(attr->name, "job-name") &&
3633 (attr->value_tag == IPP_TAG_NAME ||
3634 attr->value_tag == IPP_TAG_NAMELANG))
1340db2d 3635 strlcpy(title, attr->values[0].string.text, title_size);
e1d6a774 3636 else if (attr->group_tag == IPP_TAG_JOB)
3637 {
3638 /*
3639 * Filter out other unwanted attributes...
3640 */
bd7854cb 3641
c7017ecc
MS
3642 if (attr->value_tag == IPP_TAG_NOVALUE ||
3643 attr->value_tag == IPP_TAG_MIMETYPE ||
e1d6a774 3644 attr->value_tag == IPP_TAG_NAMELANG ||
3645 attr->value_tag == IPP_TAG_TEXTLANG ||
3646 (attr->value_tag == IPP_TAG_URI && strcmp(attr->name, "job-uuid")) ||
3647 attr->value_tag == IPP_TAG_URISCHEME ||
3648 attr->value_tag == IPP_TAG_BEGIN_COLLECTION) /* Not yet supported */
3649 continue;
bd7854cb 3650
5a9febac
MS
3651 if (!strcmp(attr->name, "job-hold-until") ||
3652 !strcmp(attr->name, "job-id") ||
3653 !strcmp(attr->name, "job-k-octets") ||
3654 !strcmp(attr->name, "job-media-sheets") ||
3655 !strcmp(attr->name, "job-media-sheets-completed") ||
3656 !strcmp(attr->name, "job-state") ||
3657 !strcmp(attr->name, "job-state-reasons"))
e1d6a774 3658 continue;
bd7854cb 3659
e1d6a774 3660 if (!strncmp(attr->name, "job-", 4) &&
5a9febac
MS
3661 strcmp(attr->name, "job-account-id") &&
3662 strcmp(attr->name, "job-accounting-user-id") &&
a469f8a5 3663 strcmp(attr->name, "job-authorization-uri") &&
e4572d57 3664 strcmp(attr->name, "job-billing") &&
5d6412a9 3665 strcmp(attr->name, "job-impressions") &&
c5571a1d 3666 strcmp(attr->name, "job-originating-host-name") &&
5a9febac 3667 strcmp(attr->name, "job-password") &&
a469f8a5 3668 strcmp(attr->name, "job-password-encryption") &&
e4572d57 3669 strcmp(attr->name, "job-uuid") &&
b9faaae1 3670 !(job->printer->type & CUPS_PRINTER_REMOTE))
e1d6a774 3671 continue;
ef416fc2 3672
5d6412a9
MS
3673 if ((!strcmp(attr->name, "job-impressions") ||
3674 !strcmp(attr->name, "page-label") ||
e1d6a774 3675 !strcmp(attr->name, "page-border") ||
3676 !strncmp(attr->name, "number-up", 9) ||
b94498cf 3677 !strcmp(attr->name, "page-ranges") ||
e1d6a774 3678 !strcmp(attr->name, "page-set") ||
88f9aafc
MS
3679 !_cups_strcasecmp(attr->name, "AP_FIRSTPAGE_InputSlot") ||
3680 !_cups_strcasecmp(attr->name, "AP_FIRSTPAGE_ManualFeed") ||
3681 !_cups_strcasecmp(attr->name, "com.apple.print.PrintSettings."
db1f069b 3682 "PMTotalSidesImaged..n.") ||
88f9aafc 3683 !_cups_strcasecmp(attr->name, "com.apple.print.PrintSettings."
db1f069b 3684 "PMTotalBeginPages..n.")) &&
e1d6a774 3685 banner_page)
3686 continue;
ef416fc2 3687
e1d6a774 3688 /*
3689 * Otherwise add them to the list...
3690 */
ef416fc2 3691
e1d6a774 3692 if (optptr > options)
3693 strlcat(optptr, " ", optlength - (optptr - options));
ef416fc2 3694
e1d6a774 3695 if (attr->value_tag != IPP_TAG_BOOLEAN)
3696 {
3697 strlcat(optptr, attr->name, optlength - (optptr - options));
3698 strlcat(optptr, "=", optlength - (optptr - options));
3699 }
ef416fc2 3700
e1d6a774 3701 for (i = 0; i < attr->num_values; i ++)
3702 {
3703 if (i)
3704 strlcat(optptr, ",", optlength - (optptr - options));
ef416fc2 3705
e1d6a774 3706 optptr += strlen(optptr);
ef416fc2 3707
e1d6a774 3708 switch (attr->value_tag)
3709 {
3710 case IPP_TAG_INTEGER :
3711 case IPP_TAG_ENUM :
3712 snprintf(optptr, optlength - (optptr - options),
3713 "%d", attr->values[i].integer);
3714 break;
ef416fc2 3715
e1d6a774 3716 case IPP_TAG_BOOLEAN :
3717 if (!attr->values[i].boolean)
3718 strlcat(optptr, "no", optlength - (optptr - options));
ef416fc2 3719
4220952d
MS
3720 strlcat(optptr, attr->name,
3721 optlength - (optptr - options));
3722 break;
3723
e1d6a774 3724 case IPP_TAG_RANGE :
3725 if (attr->values[i].range.lower == attr->values[i].range.upper)
3726 snprintf(optptr, optlength - (optptr - options) - 1,
3727 "%d", attr->values[i].range.lower);
3728 else
3729 snprintf(optptr, optlength - (optptr - options) - 1,
3730 "%d-%d", attr->values[i].range.lower,
3731 attr->values[i].range.upper);
3732 break;
ef416fc2 3733
e1d6a774 3734 case IPP_TAG_RESOLUTION :
3735 snprintf(optptr, optlength - (optptr - options) - 1,
3736 "%dx%d%s", attr->values[i].resolution.xres,
3737 attr->values[i].resolution.yres,
3738 attr->values[i].resolution.units == IPP_RES_PER_INCH ?
3e7fe0ca 3739 "dpi" : "dpcm");
e1d6a774 3740 break;
ef416fc2 3741
e1d6a774 3742 case IPP_TAG_STRING :
3743 case IPP_TAG_TEXT :
3744 case IPP_TAG_NAME :
3745 case IPP_TAG_KEYWORD :
3746 case IPP_TAG_CHARSET :
3747 case IPP_TAG_LANGUAGE :
3748 case IPP_TAG_URI :
3749 for (valptr = attr->values[i].string.text; *valptr;)
3750 {
3751 if (strchr(" \t\n\\\'\"", *valptr))
3752 *optptr++ = '\\';
3753 *optptr++ = *valptr++;
3754 }
3755
3756 *optptr = '\0';
3757 break;
3758
3759 default :
3760 break; /* anti-compiler-warning-code */
3761 }
3762 }
3763
3764 optptr += strlen(optptr);
3765 }
3766 }
3767
c7017ecc
MS
3768 /*
3769 * Finally loop through the PWG->PPD mapped options and add them...
3770 */
3771
3772 for (i = num_pwgppds, pwgppd = pwgppds; i > 0; i --, pwgppd ++)
3773 {
3774 *optptr++ = ' ';
5a9febac 3775 strlcpy(optptr, pwgppd->name, optlength - (optptr - options));
c7017ecc
MS
3776 optptr += strlen(optptr);
3777 *optptr++ = '=';
5a9febac 3778 strlcpy(optptr, pwgppd->value, optlength - (optptr - options));
c7017ecc
MS
3779 optptr += strlen(optptr);
3780 }
3781
3782 cupsFreeOptions(num_pwgppds, pwgppds);
3783
3784 /*
3785 * Return the options string...
3786 */
b9faaae1
MS
3787
3788 return (options);
3789}
3790
3791
3792/*
3793 * 'ipp_length()' - Compute the size of the buffer needed to hold
3794 * the textual IPP attributes.
3795 */
3796
c7017ecc 3797static size_t /* O - Size of attribute buffer */
b9faaae1
MS
3798ipp_length(ipp_t *ipp) /* I - IPP request */
3799{
c7017ecc 3800 size_t bytes; /* Number of bytes */
b9faaae1
MS
3801 int i; /* Looping var */
3802 ipp_attribute_t *attr; /* Current attribute */
3803
3804
3805 /*
3806 * Loop through all attributes...
e1d6a774 3807 */
3808
b9faaae1 3809 bytes = 0;
e1d6a774 3810
b9faaae1 3811 for (attr = ipp->attrs; attr != NULL; attr = attr->next)
91c84a35 3812 {
b9faaae1
MS
3813 /*
3814 * Skip attributes that won't be sent to filters...
3815 */
91c84a35 3816
c7017ecc
MS
3817 if (attr->value_tag == IPP_TAG_NOVALUE ||
3818 attr->value_tag == IPP_TAG_MIMETYPE ||
b9faaae1
MS
3819 attr->value_tag == IPP_TAG_NAMELANG ||
3820 attr->value_tag == IPP_TAG_TEXTLANG ||
3821 attr->value_tag == IPP_TAG_URI ||
3822 attr->value_tag == IPP_TAG_URISCHEME)
3823 continue;
91c84a35 3824
b9faaae1
MS
3825 /*
3826 * Add space for a leading space and commas between each value.
3827 * For the first attribute, the leading space isn't used, so the
3828 * extra byte can be used as the nul terminator...
3829 */
e1d6a774 3830
b9faaae1
MS
3831 bytes ++; /* " " separator */
3832 bytes += attr->num_values; /* "," separators */
e1d6a774 3833
b9faaae1
MS
3834 /*
3835 * Boolean attributes appear as "foo,nofoo,foo,nofoo", while
3836 * other attributes appear as "foo=value1,value2,...,valueN".
3837 */
3838
3839 if (attr->value_tag != IPP_TAG_BOOLEAN)
3840 bytes += strlen(attr->name);
3841 else
3842 bytes += attr->num_values * strlen(attr->name);
3843
3844 /*
3845 * Now add the size required for each value in the attribute...
3846 */
3847
3848 switch (attr->value_tag)
ef416fc2 3849 {
b9faaae1
MS
3850 case IPP_TAG_INTEGER :
3851 case IPP_TAG_ENUM :
3852 /*
3853 * Minimum value of a signed integer is -2147483647, or 11 digits.
3854 */
3855
3856 bytes += attr->num_values * 11;
3857 break;
3858
3859 case IPP_TAG_BOOLEAN :
3860 /*
3861 * Add two bytes for each false ("no") value...
3862 */
3863
3864 for (i = 0; i < attr->num_values; i ++)
3865 if (!attr->values[i].boolean)
3866 bytes += 2;
3867 break;
3868
3869 case IPP_TAG_RANGE :
3870 /*
3871 * A range is two signed integers separated by a hyphen, or
3872 * 23 characters max.
3873 */
3874
3875 bytes += attr->num_values * 23;
3876 break;
3877
3878 case IPP_TAG_RESOLUTION :
3879 /*
3880 * A resolution is two signed integers separated by an "x" and
3881 * suffixed by the units, or 26 characters max.
3882 */
3883
3884 bytes += attr->num_values * 26;
3885 break;
3886
3887 case IPP_TAG_STRING :
3888 case IPP_TAG_TEXT :
3889 case IPP_TAG_NAME :
3890 case IPP_TAG_KEYWORD :
3891 case IPP_TAG_CHARSET :
3892 case IPP_TAG_LANGUAGE :
3893 case IPP_TAG_URI :
3894 /*
3895 * Strings can contain characters that need quoting. We need
3896 * at least 2 * len + 2 characters to cover the quotes and
3897 * any backslashes in the string.
3898 */
3899
3900 for (i = 0; i < attr->num_values; i ++)
3901 bytes += 2 * strlen(attr->values[i].string.text) + 2;
3902 break;
3903
3904 default :
3905 break; /* anti-compiler-warning-code */
e1d6a774 3906 }
3907 }
b9faaae1
MS
3908
3909 return (bytes);
3910}
3911
3912
3913/*
3914 * 'load_job_cache()' - Load jobs from the job.cache file.
3915 */
3916
3917static void
3918load_job_cache(const char *filename) /* I - job.cache filename */
3919{
3920 cups_file_t *fp; /* job.cache file */
3921 char line[1024], /* Line buffer */
3922 *value; /* Value on line */
3923 int linenum; /* Line number in file */
3924 cupsd_job_t *job; /* Current job */
3925 int jobid; /* Job ID */
3926 char jobfile[1024]; /* Job filename */
3927
3928
3929 /*
3930 * Open the job.cache file...
3931 */
3932
321d8d57 3933 if ((fp = cupsdOpenConfFile(filename)) == NULL)
e1d6a774 3934 {
b9faaae1 3935 load_request_root();
b9faaae1
MS
3936 return;
3937 }
ef416fc2 3938
e1d6a774 3939 /*
b9faaae1 3940 * Read entries from the job cache file and create jobs as needed.
e1d6a774 3941 */
ef416fc2 3942
b9faaae1
MS
3943 cupsdLogMessage(CUPSD_LOG_INFO, "Loading job cache file \"%s\"...",
3944 filename);
ef416fc2 3945
b9faaae1
MS
3946 linenum = 0;
3947 job = NULL;
3948
3949 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
3950 {
88f9aafc 3951 if (!_cups_strcasecmp(line, "NextJobId"))
b9faaae1
MS
3952 {
3953 if (value)
3954 NextJobId = atoi(value);
3955 }
88f9aafc 3956 else if (!_cups_strcasecmp(line, "<Job"))
b9faaae1
MS
3957 {
3958 if (job)
3959 {
12f89d24 3960 cupsdLogMessage(CUPSD_LOG_ERROR, "Missing </Job> directive on line %d.",
b9faaae1
MS
3961 linenum);
3962 continue;
3963 }
3964
3965 if (!value)
3966 {
12f89d24 3967 cupsdLogMessage(CUPSD_LOG_ERROR, "Missing job ID on line %d.", linenum);
b9faaae1
MS
3968 continue;
3969 }
3970
3971 jobid = atoi(value);
3972
3973 if (jobid < 1)
3974 {
12f89d24 3975 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad job ID %d on line %d.", jobid,
b9faaae1
MS
3976 linenum);
3977 continue;
3978 }
3979
3980 snprintf(jobfile, sizeof(jobfile), "%s/c%05d", RequestRoot, jobid);
3981 if (access(jobfile, 0))
3982 {
321d8d57
MS
3983 snprintf(jobfile, sizeof(jobfile), "%s/c%05d.N", RequestRoot, jobid);
3984 if (access(jobfile, 0))
3985 {
12f89d24 3986 cupsdLogMessage(CUPSD_LOG_ERROR, "[Job %d] Files have gone away.",
321d8d57
MS
3987 jobid);
3988 continue;
3989 }
b9faaae1
MS
3990 }
3991
3992 job = calloc(1, sizeof(cupsd_job_t));
3993 if (!job)
3994 {
3995 cupsdLogMessage(CUPSD_LOG_EMERG,
12f89d24 3996 "[Job %d] Unable to allocate memory for job.", jobid);
b9faaae1
MS
3997 break;
3998 }
3999
4000 job->id = jobid;
4001 job->back_pipes[0] = -1;
4002 job->back_pipes[1] = -1;
4003 job->print_pipes[0] = -1;
4004 job->print_pipes[1] = -1;
4005 job->side_pipes[0] = -1;
4006 job->side_pipes[1] = -1;
4007 job->status_pipes[0] = -1;
4008 job->status_pipes[1] = -1;
4009
4010 cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] Loading from cache...",
4011 job->id);
4012 }
4013 else if (!job)
4014 {
4015 cupsdLogMessage(CUPSD_LOG_ERROR,
12f89d24 4016 "Missing <Job #> directive on line %d.", linenum);
b9faaae1
MS
4017 continue;
4018 }
88f9aafc 4019 else if (!_cups_strcasecmp(line, "</Job>"))
b9faaae1
MS
4020 {
4021 cupsArrayAdd(Jobs, job);
4022
f701418f
MS
4023 if (job->state_value <= IPP_JOB_STOPPED && cupsdLoadJob(job))
4024 cupsArrayAdd(ActiveJobs, job);
b9faaae1
MS
4025
4026 job = NULL;
4027 }
4028 else if (!value)
4029 {
12f89d24 4030 cupsdLogMessage(CUPSD_LOG_ERROR, "Missing value on line %d.", linenum);
b9faaae1
MS
4031 continue;
4032 }
88f9aafc 4033 else if (!_cups_strcasecmp(line, "State"))
b9faaae1
MS
4034 {
4035 job->state_value = (ipp_jstate_t)atoi(value);
4036
4037 if (job->state_value < IPP_JOB_PENDING)
4038 job->state_value = IPP_JOB_PENDING;
4039 else if (job->state_value > IPP_JOB_COMPLETED)
4040 job->state_value = IPP_JOB_COMPLETED;
4041 }
88f9aafc 4042 else if (!_cups_strcasecmp(line, "HoldUntil"))
8b116e60
MS
4043 {
4044 job->hold_until = atoi(value);
4045 }
88f9aafc 4046 else if (!_cups_strcasecmp(line, "Priority"))
b9faaae1
MS
4047 {
4048 job->priority = atoi(value);
4049 }
88f9aafc 4050 else if (!_cups_strcasecmp(line, "Username"))
b9faaae1
MS
4051 {
4052 cupsdSetString(&job->username, value);
4053 }
88f9aafc 4054 else if (!_cups_strcasecmp(line, "Destination"))
b9faaae1
MS
4055 {
4056 cupsdSetString(&job->dest, value);
4057 }
88f9aafc 4058 else if (!_cups_strcasecmp(line, "DestType"))
b9faaae1
MS
4059 {
4060 job->dtype = (cups_ptype_t)atoi(value);
4061 }
88f9aafc 4062 else if (!_cups_strcasecmp(line, "NumFiles"))
b9faaae1
MS
4063 {
4064 job->num_files = atoi(value);
4065
4066 if (job->num_files < 0)
4067 {
12f89d24 4068 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad NumFiles value %d on line %d.",
b9faaae1
MS
4069 job->num_files, linenum);
4070 job->num_files = 0;
4071 continue;
4072 }
4073
4074 if (job->num_files > 0)
4075 {
4076 snprintf(jobfile, sizeof(jobfile), "%s/d%05d-001", RequestRoot,
4077 job->id);
4078 if (access(jobfile, 0))
4079 {
12f89d24 4080 cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Data files have gone away.",
b9faaae1
MS
4081 job->id);
4082 job->num_files = 0;
4083 continue;
4084 }
4085
4086 job->filetypes = calloc(job->num_files, sizeof(mime_type_t *));
4087 job->compressions = calloc(job->num_files, sizeof(int));
4088
4089 if (!job->filetypes || !job->compressions)
4090 {
4091 cupsdLogMessage(CUPSD_LOG_EMERG,
12f89d24 4092 "[Job %d] Unable to allocate memory for %d files.",
b9faaae1
MS
4093 job->id, job->num_files);
4094 break;
4095 }
4096 }
4097 }
88f9aafc 4098 else if (!_cups_strcasecmp(line, "File"))
b9faaae1
MS
4099 {
4100 int number, /* File number */
4101 compression; /* Compression value */
4102 char super[MIME_MAX_SUPER], /* MIME super type */
4103 type[MIME_MAX_TYPE]; /* MIME type */
4104
4105
4106 if (sscanf(value, "%d%*[ \t]%15[^/]/%255s%d", &number, super, type,
4107 &compression) != 4)
4108 {
12f89d24 4109 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad File on line %d.", linenum);
b9faaae1
MS
4110 continue;
4111 }
4112
4113 if (number < 1 || number > job->num_files)
4114 {
12f89d24 4115 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad File number %d on line %d.",
b9faaae1
MS
4116 number, linenum);
4117 continue;
4118 }
0a682745 4119
b9faaae1 4120 number --;
ef416fc2 4121
b9faaae1
MS
4122 job->compressions[number] = compression;
4123 job->filetypes[number] = mimeType(MimeDatabase, super, type);
ef416fc2 4124
b9faaae1
MS
4125 if (!job->filetypes[number])
4126 {
e1d6a774 4127 /*
b9faaae1 4128 * If the original MIME type is unknown, auto-type it!
e1d6a774 4129 */
ef416fc2 4130
b9faaae1 4131 cupsdLogMessage(CUPSD_LOG_ERROR,
12f89d24 4132 "[Job %d] Unknown MIME type %s/%s for file %d.",
b9faaae1
MS
4133 job->id, super, type, number + 1);
4134
4135 snprintf(jobfile, sizeof(jobfile), "%s/d%05d-%03d", RequestRoot,
4136 job->id, number + 1);
4137 job->filetypes[number] = mimeFileType(MimeDatabase, jobfile, NULL,
4138 job->compressions + number);
ef416fc2 4139
e1d6a774 4140 /*
b9faaae1 4141 * If that didn't work, assume it is raw...
e1d6a774 4142 */
ef416fc2 4143
b9faaae1
MS
4144 if (!job->filetypes[number])
4145 job->filetypes[number] = mimeType(MimeDatabase, "application",
4146 "vnd.cups-raw");
4147 }
4148 }
4149 else
12f89d24 4150 cupsdLogMessage(CUPSD_LOG_ERROR, "Unknown %s directive on line %d.",
b9faaae1 4151 line, linenum);
ef416fc2 4152 }
4153
b9faaae1
MS
4154 cupsFileClose(fp);
4155}
ef416fc2 4156
4157
b9faaae1
MS
4158/*
4159 * 'load_next_job_id()' - Load the NextJobId value from the job.cache file.
4160 */
bd7854cb 4161
b9faaae1
MS
4162static void
4163load_next_job_id(const char *filename) /* I - job.cache filename */
4164{
4165 cups_file_t *fp; /* job.cache file */
4166 char line[1024], /* Line buffer */
4167 *value; /* Value on line */
4168 int linenum; /* Line number in file */
4169 int next_job_id; /* NextJobId value from line */
db0bd74a 4170
db0bd74a 4171
b9faaae1
MS
4172 /*
4173 * Read the NextJobId directive from the job.cache file and use
4174 * the value (if any).
4175 */
bd7854cb 4176
b9faaae1 4177 if ((fp = cupsFileOpen(filename, "r")) == NULL)
e1d6a774 4178 {
b9faaae1
MS
4179 if (errno != ENOENT)
4180 cupsdLogMessage(CUPSD_LOG_ERROR,
4181 "Unable to open job cache file \"%s\": %s",
4182 filename, strerror(errno));
bd7854cb 4183
b9faaae1 4184 return;
e1d6a774 4185 }
bd7854cb 4186
b9faaae1
MS
4187 cupsdLogMessage(CUPSD_LOG_INFO,
4188 "Loading NextJobId from job cache file \"%s\"...", filename);
4189
4190 linenum = 0;
4191
4192 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
bd7854cb 4193 {
88f9aafc 4194 if (!_cups_strcasecmp(line, "NextJobId"))
b9faaae1
MS
4195 {
4196 if (value)
4197 {
4198 next_job_id = atoi(value);
4199
4200 if (next_job_id > NextJobId)
4201 NextJobId = next_job_id;
4202 }
4203 break;
4204 }
e1d6a774 4205 }
bd7854cb 4206
b9faaae1
MS
4207 cupsFileClose(fp);
4208}
09a101d6 4209
f7deaa1a 4210
b9faaae1
MS
4211/*
4212 * 'load_request_root()' - Load jobs from the RequestRoot directory.
4213 */
bd7854cb 4214
b9faaae1
MS
4215static void
4216load_request_root(void)
4217{
4218 cups_dir_t *dir; /* Directory */
4219 cups_dentry_t *dent; /* Directory entry */
4220 cupsd_job_t *job; /* New job */
e1d6a774 4221
bd7854cb 4222
4223 /*
b9faaae1 4224 * Open the requests directory...
bd7854cb 4225 */
4226
b9faaae1 4227 cupsdLogMessage(CUPSD_LOG_DEBUG, "Scanning %s for jobs...", RequestRoot);
bd7854cb 4228
b9faaae1 4229 if ((dir = cupsDirOpen(RequestRoot)) == NULL)
bd7854cb 4230 {
b9faaae1
MS
4231 cupsdLogMessage(CUPSD_LOG_ERROR,
4232 "Unable to open spool directory \"%s\": %s",
4233 RequestRoot, strerror(errno));
4234 return;
e1d6a774 4235 }
bd7854cb 4236
b9faaae1
MS
4237 /*
4238 * Read all the c##### files...
4239 */
bd7854cb 4240
b9faaae1
MS
4241 while ((dent = cupsDirRead(dir)) != NULL)
4242 if (strlen(dent->filename) >= 6 && dent->filename[0] == 'c')
bd7854cb 4243 {
b9faaae1
MS
4244 /*
4245 * Allocate memory for the job...
4246 */
bd7854cb 4247
b9faaae1 4248 if ((job = calloc(sizeof(cupsd_job_t), 1)) == NULL)
bd7854cb 4249 {
12f89d24 4250 cupsdLogMessage(CUPSD_LOG_ERROR, "Ran out of memory for jobs.");
b9faaae1
MS
4251 cupsDirClose(dir);
4252 return;
4253 }
bd7854cb 4254
b9faaae1
MS
4255 /*
4256 * Assign the job ID...
4257 */
bd7854cb 4258
b9faaae1
MS
4259 job->id = atoi(dent->filename + 1);
4260 job->back_pipes[0] = -1;
4261 job->back_pipes[1] = -1;
4262 job->print_pipes[0] = -1;
4263 job->print_pipes[1] = -1;
4264 job->side_pipes[0] = -1;
4265 job->side_pipes[1] = -1;
4266 job->status_pipes[0] = -1;
4267 job->status_pipes[1] = -1;
bd7854cb 4268
b9faaae1
MS
4269 if (job->id >= NextJobId)
4270 NextJobId = job->id + 1;
ed486911 4271
b9faaae1
MS
4272 /*
4273 * Load the job...
4274 */
ed486911 4275
f701418f
MS
4276 if (cupsdLoadJob(job))
4277 {
4278 /*
4279 * Insert the job into the array, sorting by job priority and ID...
4280 */
bd7854cb 4281
f701418f 4282 cupsArrayAdd(Jobs, job);
e1d6a774 4283
f701418f
MS
4284 if (job->state_value <= IPP_JOB_STOPPED)
4285 cupsArrayAdd(ActiveJobs, job);
4286 else
4287 unload_job(job);
4288 }
bd7854cb 4289 }
bd7854cb 4290
b9faaae1
MS
4291 cupsDirClose(dir);
4292}
bd7854cb 4293
4294
82cc1f9a
MS
4295/*
4296 * 'remove_job_files()' - Remove the document files for a job.
4297 */
4298
4299static void
4300remove_job_files(cupsd_job_t *job) /* I - Job */
4301{
4302 int i; /* Looping var */
4303 char filename[1024]; /* Document filename */
4304
4305
4306 if (job->num_files <= 0)
4307 return;
4308
4309 for (i = 1; i <= job->num_files; i ++)
4310 {
4311 snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot,
4312 job->id, i);
4313 if (Classification)
4314 cupsdRemoveFile(filename);
4315 else
4316 unlink(filename);
4317 }
4318
4319 free(job->filetypes);
4320 free(job->compressions);
4321
4322 job->file_time = 0;
4323 job->num_files = 0;
4324 job->filetypes = NULL;
4325 job->compressions = NULL;
4326
4327 LastEvent |= CUPSD_EVENT_PRINTER_STATE_CHANGED;
4328}
4329
4330
4331/*
4332 * 'remove_job_history()' - Remove the control file for a job.
4333 */
4334
4335static void
4336remove_job_history(cupsd_job_t *job) /* I - Job */
4337{
4338 char filename[1024]; /* Control filename */
4339
4340
4341 /*
4342 * Remove the job info file...
4343 */
4344
4345 snprintf(filename, sizeof(filename), "%s/c%05d", RequestRoot,
4346 job->id);
4347 if (Classification)
4348 cupsdRemoveFile(filename);
4349 else
4350 unlink(filename);
4351
4352 LastEvent |= CUPSD_EVENT_PRINTER_STATE_CHANGED;
4353}
4354
4355
b9faaae1
MS
4356/*
4357 * 'set_time()' - Set one of the "time-at-xyz" attributes.
4358 */
bd7854cb 4359
b9faaae1
MS
4360static void
4361set_time(cupsd_job_t *job, /* I - Job to update */
4362 const char *name) /* I - Name of attribute */
4363{
4364 ipp_attribute_t *attr; /* Time attribute */
82cc1f9a
MS
4365 time_t curtime; /* Current time */
4366
bd7854cb 4367
82cc1f9a
MS
4368 curtime = time(NULL);
4369
4370 cupsdLogJob(job, CUPSD_LOG_DEBUG, "%s=%ld", name, (long)curtime);
bd7854cb 4371
b9faaae1
MS
4372 if ((attr = ippFindAttribute(job->attrs, name, IPP_TAG_ZERO)) != NULL)
4373 {
4374 attr->value_tag = IPP_TAG_INTEGER;
82cc1f9a
MS
4375 attr->values[0].integer = curtime;
4376 }
4377
4378 if (!strcmp(name, "time-at-completed"))
4379 {
4380 if (JobHistory < INT_MAX)
4381 job->history_time = attr->values[0].integer + JobHistory;
4382 else
4383 job->history_time = INT_MAX;
4384
4385 if (job->history_time < JobHistoryUpdate || !JobHistoryUpdate)
4386 JobHistoryUpdate = job->history_time;
4387
4388 if (JobFiles < INT_MAX)
4389 job->file_time = attr->values[0].integer + JobFiles;
4390 else
4391 job->file_time = INT_MAX;
4392
4393 if (job->file_time < JobHistoryUpdate || !JobHistoryUpdate)
4394 JobHistoryUpdate = job->file_time;
4395
4396 cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_time: JobHistoryUpdate=%ld",
4397 (long)JobHistoryUpdate);
b9faaae1
MS
4398 }
4399}
bd7854cb 4400
e1d6a774 4401
b9faaae1
MS
4402/*
4403 * 'start_job()' - Start a print job.
4404 */
e1d6a774 4405
b9faaae1
MS
4406static void
4407start_job(cupsd_job_t *job, /* I - Job ID */
4408 cupsd_printer_t *printer) /* I - Printer to print job */
4409{
4410 cupsdLogMessage(CUPSD_LOG_DEBUG2, "start_job(job=%p(%d), printer=%p(%s))",
4411 job, job->id, printer, printer->name);
bd7854cb 4412
b9faaae1
MS
4413 /*
4414 * Make sure we have some files around before we try to print...
4415 */
bd7854cb 4416
b9faaae1
MS
4417 if (job->num_files == 0)
4418 {
12f89d24 4419 ippSetString(job->attrs, &job->reasons, 0, "aborted-by-system");
b9faaae1
MS
4420 cupsdSetJobState(job, IPP_JOB_ABORTED, CUPSD_JOB_DEFAULT,
4421 "Aborting job because it has no files.");
4422 return;
4423 }
bd7854cb 4424
b9faaae1
MS
4425 /*
4426 * Update the printer and job state to "processing"...
4427 */
bd7854cb 4428
b9faaae1
MS
4429 if (!cupsdLoadJob(job))
4430 return;
89d46774 4431
3e7fe0ca
MS
4432 if (!job->printer_message)
4433 job->printer_message = ippFindAttribute(job->attrs,
4434 "job-printer-state-message",
4435 IPP_TAG_TEXT);
88f9aafc
MS
4436 if (job->printer_message)
4437 cupsdSetString(&(job->printer_message->values[0].string.text), "");
4438
12f89d24 4439 ippSetString(job->attrs, &job->reasons, 0, "job-printing");
b9faaae1
MS
4440 cupsdSetJobState(job, IPP_JOB_PROCESSING, CUPSD_JOB_DEFAULT, NULL);
4441 cupsdSetPrinterState(printer, IPP_PRINTER_PROCESSING, 0);
07ed0e9a
MS
4442 cupsdSetPrinterReasons(printer, "-cups-remote-pending,"
4443 "cups-remote-pending-held,"
4444 "cups-remote-processing,"
4445 "cups-remote-stopped,"
4446 "cups-remote-canceled,"
4447 "cups-remote-aborted,"
4448 "cups-remote-completed");
f7deaa1a 4449
1340db2d
MS
4450 job->cost = 0;
4451 job->current_file = 0;
82cc1f9a
MS
4452 job->file_time = 0;
4453 job->history_time = 0;
1340db2d
MS
4454 job->progress = 0;
4455 job->printer = printer;
4456 printer->job = job;
f7deaa1a 4457
dcb445bc
MS
4458 if (MaxJobTime > 0)
4459 job->cancel_time = time(NULL) + MaxJobTime;
4460 else
4461 job->cancel_time = 0;
4462
b9faaae1
MS
4463 /*
4464 * Setup the last exit status and security profiles...
4465 */
89d46774 4466
b9faaae1
MS
4467 job->status = 0;
4468 job->profile = cupsdCreateProfile(job->id);
bd7854cb 4469
b9faaae1
MS
4470 /*
4471 * Create the status pipes and buffer...
4472 */
bd7854cb 4473
b9faaae1
MS
4474 if (cupsdOpenPipe(job->status_pipes))
4475 {
4476 cupsdLogJob(job, CUPSD_LOG_DEBUG,
4477 "Unable to create job status pipes - %s.", strerror(errno));
89d46774 4478
b9faaae1
MS
4479 cupsdSetJobState(job, IPP_JOB_STOPPED, CUPSD_JOB_DEFAULT,
4480 "Job stopped because the scheduler could not create the "
4481 "job status pipes.");
89d46774 4482
b9faaae1
MS
4483 cupsdDestroyProfile(job->profile);
4484 job->profile = NULL;
4485 return;
e1d6a774 4486 }
bd7854cb 4487
b9faaae1
MS
4488 job->status_buffer = cupsdStatBufNew(job->status_pipes[0], NULL);
4489 job->status_level = CUPSD_LOG_INFO;
4490
4491 /*
4492 * Create the backchannel pipes and make them non-blocking...
4493 */
bd7854cb 4494
b9faaae1 4495 if (cupsdOpenPipe(job->back_pipes))
e1d6a774 4496 {
b9faaae1
MS
4497 cupsdLogJob(job, CUPSD_LOG_DEBUG,
4498 "Unable to create back-channel pipes - %s.", strerror(errno));
4499
4500 cupsdSetJobState(job, IPP_JOB_STOPPED, CUPSD_JOB_DEFAULT,
4501 "Job stopped because the scheduler could not create the "
4502 "back-channel pipes.");
4503
4504 cupsdClosePipe(job->status_pipes);
4505 cupsdStatBufDelete(job->status_buffer);
4506 job->status_buffer = NULL;
4507
4508 cupsdDestroyProfile(job->profile);
4509 job->profile = NULL;
4510 return;
e1d6a774 4511 }
bd7854cb 4512
b9faaae1
MS
4513 fcntl(job->back_pipes[0], F_SETFL,
4514 fcntl(job->back_pipes[0], F_GETFL) | O_NONBLOCK);
4515 fcntl(job->back_pipes[1], F_SETFL,
4516 fcntl(job->back_pipes[1], F_GETFL) | O_NONBLOCK);
ef416fc2 4517
b9faaae1
MS
4518 /*
4519 * Create the side-channel pipes and make them non-blocking...
4520 */
ef416fc2 4521
b9faaae1
MS
4522 if (socketpair(AF_LOCAL, SOCK_STREAM, 0, job->side_pipes))
4523 {
4524 cupsdLogJob(job, CUPSD_LOG_DEBUG,
4525 "Unable to create side-channel pipes - %s.", strerror(errno));
ef416fc2 4526
b9faaae1
MS
4527 cupsdSetJobState(job, IPP_JOB_STOPPED, CUPSD_JOB_DEFAULT,
4528 "Job stopped because the scheduler could not create the "
4529 "side-channel pipes.");
ef416fc2 4530
b9faaae1
MS
4531 cupsdClosePipe(job->back_pipes);
4532
4533 cupsdClosePipe(job->status_pipes);
4534 cupsdStatBufDelete(job->status_buffer);
4535 job->status_buffer = NULL;
4536
4537 cupsdDestroyProfile(job->profile);
4538 job->profile = NULL;
4539 return;
4540 }
4541
4542 fcntl(job->side_pipes[0], F_SETFL,
4543 fcntl(job->side_pipes[0], F_GETFL) | O_NONBLOCK);
4544 fcntl(job->side_pipes[1], F_SETFL,
4545 fcntl(job->side_pipes[1], F_GETFL) | O_NONBLOCK);
ef416fc2 4546
7a0cbd5e
MS
4547 fcntl(job->side_pipes[0], F_SETFD,
4548 fcntl(job->side_pipes[0], F_GETFD) | FD_CLOEXEC);
4549 fcntl(job->side_pipes[1], F_SETFD,
4550 fcntl(job->side_pipes[1], F_GETFD) | FD_CLOEXEC);
4551
ef416fc2 4552 /*
b9faaae1 4553 * Now start the first file in the job...
ef416fc2 4554 */
4555
b9faaae1
MS
4556 cupsdContinueJob(job);
4557}
ef416fc2 4558
ef416fc2 4559
b9faaae1
MS
4560/*
4561 * 'stop_job()' - Stop a print job.
4562 */
ef416fc2 4563
b9faaae1
MS
4564static void
4565stop_job(cupsd_job_t *job, /* I - Job */
4566 cupsd_jobaction_t action) /* I - Action */
4567{
4568 int i; /* Looping var */
b94498cf 4569
ef416fc2 4570
b9faaae1
MS
4571 cupsdLogMessage(CUPSD_LOG_DEBUG2, "stop_job(job=%p(%d), action=%d)", job,
4572 job->id, action);
ef416fc2 4573
b9faaae1
MS
4574 FilterLevel -= job->cost;
4575 job->cost = 0;
ef416fc2 4576
238c3832
MS
4577 if (action == CUPSD_JOB_DEFAULT && !job->kill_time)
4578 job->kill_time = time(NULL) + JobKillDelay;
ef55b745 4579 else if (action >= CUPSD_JOB_FORCE)
238c3832
MS
4580 job->kill_time = 0;
4581
b9faaae1
MS
4582 for (i = 0; job->filters[i]; i ++)
4583 if (job->filters[i] > 0)
ef55b745
MS
4584 {
4585 cupsdEndProcess(job->filters[i], action >= CUPSD_JOB_FORCE);
4586
4587 if (action >= CUPSD_JOB_FORCE)
4588 job->filters[i] = -job->filters[i];
4589 }
b9faaae1
MS
4590
4591 if (job->backend > 0)
ef55b745
MS
4592 {
4593 cupsdEndProcess(job->backend, action >= CUPSD_JOB_FORCE);
4594
4595 if (action >= CUPSD_JOB_FORCE)
4596 job->backend = -job->backend;
4597 }
4598
4599 if (action >= CUPSD_JOB_FORCE)
4600 {
4601 /*
4602 * Clear job status...
4603 */
4604
4605 job->status = 0;
4606 }
e1d6a774 4607}
ef416fc2 4608
e1d6a774 4609
4610/*
4611 * 'unload_job()' - Unload a job from memory.
4612 */
4613
4614static void
4615unload_job(cupsd_job_t *job) /* I - Job */
4616{
4617 if (!job->attrs)
4618 return;
4619
ed6e7faf 4620 cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] Unloading...", job->id);
e1d6a774 4621
4622 ippDelete(job->attrs);
4623
cc0d019f
MS
4624 job->attrs = NULL;
4625 job->state = NULL;
12f89d24 4626 job->reasons = NULL;
cc0d019f
MS
4627 job->sheets = NULL;
4628 job->job_sheets = NULL;
4629 job->printer_message = NULL;
4630 job->printer_reasons = NULL;
ef416fc2 4631}
4632
4633
4634/*
f899b121 4635 * 'update_job()' - Read a status update from a job's filters.
4636 */
4637
4638void
09a101d6 4639update_job(cupsd_job_t *job) /* I - Job to check */
f899b121 4640{
4641 int i; /* Looping var */
4642 int copies; /* Number of copies printed */
178cb736
MS
4643 char message[CUPSD_SB_BUFFER_SIZE],
4644 /* Message text */
f899b121 4645 *ptr; /* Pointer update... */
4646 int loglevel, /* Log level for message */
4647 event = 0; /* Events? */
f0ab5bff
MS
4648 static const char * const levels[] = /* Log levels */
4649 {
4650 "NONE",
4651 "EMERG",
4652 "ALERT",
4653 "CRIT",
4654 "ERROR",
4655 "WARN",
4656 "NOTICE",
4657 "INFO",
4658 "DEBUG",
4659 "DEBUG2"
4660 };
f899b121 4661
4662
f0ab5bff
MS
4663 /*
4664 * Get the printer associated with this job; if the printer is stopped for
4665 * any reason then job->printer will be reset to NULL, so make sure we have
4666 * a valid pointer...
4667 */
4668
f899b121 4669 while ((ptr = cupsdStatBufUpdate(job->status_buffer, &loglevel,
4670 message, sizeof(message))) != NULL)
4671 {
4672 /*
4673 * Process page and printer state messages as needed...
4674 */
4675
4676 if (loglevel == CUPSD_LOG_PAGE)
4677 {
4678 /*
4679 * Page message; send the message to the page_log file and update the
4680 * job sheet count...
4681 */
4682
75bd9771 4683 cupsdLogJob(job, CUPSD_LOG_DEBUG, "PAGE: %s", message);
dd1abb6b 4684
91c84a35 4685 if (job->sheets)
f899b121 4686 {
88f9aafc 4687 if (!_cups_strncasecmp(message, "total ", 6))
f899b121 4688 {
4689 /*
4690 * Got a total count of pages from a backend or filter...
4691 */
4692
4693 copies = atoi(message + 6);
4694 copies -= job->sheets->values[0].integer; /* Just track the delta */
4695 }
4696 else if (!sscanf(message, "%*d%d", &copies))
4697 copies = 1;
4698
4699 job->sheets->values[0].integer += copies;
4700
b9faaae1 4701 if (job->printer->page_limit)
ba55dc12 4702 cupsdUpdateQuota(job->printer, job->username, copies, 0);
f899b121 4703 }
4704
4705 cupsdLogPage(job, message);
4706
91c84a35 4707 if (job->sheets)
b9faaae1 4708 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS, job->printer, job,
91c84a35 4709 "Printed %d page(s).", job->sheets->values[0].integer);
f899b121 4710 }
a469f8a5
MS
4711 else if (loglevel == CUPSD_LOG_JOBSTATE)
4712 {
4713 /*
4714 * Support "keyword" to set job-state-reasons to the specified keyword.
4715 * This is sufficient for the current paid printing stuff.
4716 */
4717
4718 cupsdLogJob(job, CUPSD_LOG_DEBUG, "JOBSTATE: %s", message);
4719
4720 ippSetString(job->attrs, &job->reasons, 0, message);
4721 }
f899b121 4722 else if (loglevel == CUPSD_LOG_STATE)
4723 {
75bd9771 4724 cupsdLogJob(job, CUPSD_LOG_DEBUG, "STATE: %s", message);
dd1abb6b 4725
09a101d6 4726 if (!strcmp(message, "paused"))
c24d2134 4727 {
b9faaae1 4728 cupsdStopPrinter(job->printer, 1);
c24d2134
MS
4729 return;
4730 }
acb056cb 4731 else if (cupsdSetPrinterReasons(job->printer, message))
dcb445bc 4732 {
d9bca400 4733 event |= CUPSD_EVENT_PRINTER_STATE;
bc44d920 4734
dcb445bc
MS
4735 if (MaxJobTime > 0 && strstr(message, "connecting-to-device") != NULL)
4736 {
4737 /*
4738 * Reset cancel time after connecting to the device...
4739 */
4740
4741 for (i = 0; i < job->printer->num_reasons; i ++)
4742 if (!strcmp(job->printer->reasons[i], "connecting-to-device"))
4743 break;
4744
4745 if (i >= job->printer->num_reasons)
4746 job->cancel_time = time(NULL) + MaxJobTime;
4747 }
4748 }
4749
4509bb49 4750 update_job_attrs(job, 0);
f899b121 4751 }
4752 else if (loglevel == CUPSD_LOG_ATTR)
4753 {
4754 /*
4755 * Set attribute(s)...
4756 */
4757
4758 int num_attrs; /* Number of attributes */
4759 cups_option_t *attrs; /* Attributes */
4760 const char *attr; /* Attribute */
4761
75bd9771 4762 cupsdLogJob(job, CUPSD_LOG_DEBUG, "ATTR: %s", message);
dd1abb6b 4763
f899b121 4764 num_attrs = cupsParseOptions(message, 0, &attrs);
4765
5a9febac
MS
4766 if ((attr = cupsGetOption("auth-info-default", num_attrs,
4767 attrs)) != NULL)
4768 {
4769 job->printer->num_options = cupsAddOption("auth-info", attr,
4770 job->printer->num_options,
4771 &(job->printer->options));
4772 cupsdSetPrinterAttrs(job->printer);
4773
4774 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
4775 }
4776
f899b121 4777 if ((attr = cupsGetOption("auth-info-required", num_attrs,
4778 attrs)) != NULL)
7ff4fea9 4779 {
b9faaae1
MS
4780 cupsdSetAuthInfoRequired(job->printer, attr, NULL);
4781 cupsdSetPrinterAttrs(job->printer);
3dfe78b3 4782
a2326b5b 4783 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
7ff4fea9 4784 }
f899b121 4785
9a4f8274
MS
4786 if ((attr = cupsGetOption("job-media-progress", num_attrs,
4787 attrs)) != NULL)
4788 {
4789 int progress = atoi(attr);
4790
4791
4792 if (progress >= 0 && progress <= 100)
4793 {
4794 job->progress = progress;
4795
4796 if (job->sheets)
b9faaae1 4797 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS, job->printer, job,
9a4f8274
MS
4798 "Printing page %d, %d%%",
4799 job->sheets->values[0].integer, job->progress);
4800 }
4801 }
4802
323c5de1 4803 if ((attr = cupsGetOption("printer-alert", num_attrs, attrs)) != NULL)
4804 {
b9faaae1 4805 cupsdSetString(&job->printer->alert, attr);
d9bca400 4806 event |= CUPSD_EVENT_PRINTER_STATE;
323c5de1 4807 }
4808
4809 if ((attr = cupsGetOption("printer-alert-description", num_attrs,
4810 attrs)) != NULL)
4811 {
b9faaae1 4812 cupsdSetString(&job->printer->alert_description, attr);
d9bca400 4813 event |= CUPSD_EVENT_PRINTER_STATE;
323c5de1 4814 }
4815
5a738aea
MS
4816 if ((attr = cupsGetOption("marker-colors", num_attrs, attrs)) != NULL)
4817 {
b9faaae1
MS
4818 cupsdSetPrinterAttr(job->printer, "marker-colors", (char *)attr);
4819 job->printer->marker_time = time(NULL);
5a738aea 4820 event |= CUPSD_EVENT_PRINTER_STATE;
52f6f666 4821 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
5a738aea
MS
4822 }
4823
4824 if ((attr = cupsGetOption("marker-levels", num_attrs, attrs)) != NULL)
4825 {
b9faaae1
MS
4826 cupsdSetPrinterAttr(job->printer, "marker-levels", (char *)attr);
4827 job->printer->marker_time = time(NULL);
5a738aea 4828 event |= CUPSD_EVENT_PRINTER_STATE;
52f6f666 4829 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
5a738aea
MS
4830 }
4831
ed6e7faf
MS
4832 if ((attr = cupsGetOption("marker-low-levels", num_attrs, attrs)) != NULL)
4833 {
b9faaae1
MS
4834 cupsdSetPrinterAttr(job->printer, "marker-low-levels", (char *)attr);
4835 job->printer->marker_time = time(NULL);
ed6e7faf
MS
4836 event |= CUPSD_EVENT_PRINTER_STATE;
4837 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
4838 }
4839
4840 if ((attr = cupsGetOption("marker-high-levels", num_attrs, attrs)) != NULL)
4841 {
b9faaae1
MS
4842 cupsdSetPrinterAttr(job->printer, "marker-high-levels", (char *)attr);
4843 job->printer->marker_time = time(NULL);
ed6e7faf
MS
4844 event |= CUPSD_EVENT_PRINTER_STATE;
4845 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
4846 }
4847
75bd9771
MS
4848 if ((attr = cupsGetOption("marker-message", num_attrs, attrs)) != NULL)
4849 {
b9faaae1
MS
4850 cupsdSetPrinterAttr(job->printer, "marker-message", (char *)attr);
4851 job->printer->marker_time = time(NULL);
75bd9771 4852 event |= CUPSD_EVENT_PRINTER_STATE;
52f6f666 4853 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
75bd9771
MS
4854 }
4855
5a738aea
MS
4856 if ((attr = cupsGetOption("marker-names", num_attrs, attrs)) != NULL)
4857 {
b9faaae1
MS
4858 cupsdSetPrinterAttr(job->printer, "marker-names", (char *)attr);
4859 job->printer->marker_time = time(NULL);
5a738aea 4860 event |= CUPSD_EVENT_PRINTER_STATE;
52f6f666 4861 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
5a738aea
MS
4862 }
4863
4864 if ((attr = cupsGetOption("marker-types", num_attrs, attrs)) != NULL)
4865 {
b9faaae1
MS
4866 cupsdSetPrinterAttr(job->printer, "marker-types", (char *)attr);
4867 job->printer->marker_time = time(NULL);
5a738aea 4868 event |= CUPSD_EVENT_PRINTER_STATE;
52f6f666 4869 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
5a738aea
MS
4870 }
4871
f899b121 4872 cupsFreeOptions(num_attrs, attrs);
4873 }
c9fc04c6
MS
4874 else if (loglevel == CUPSD_LOG_PPD)
4875 {
4876 /*
4877 * Set attribute(s)...
4878 */
4879
75bd9771 4880 cupsdLogJob(job, CUPSD_LOG_DEBUG, "PPD: %s", message);
dd1abb6b 4881
a29fd7dd
MS
4882 job->num_keywords = cupsParseOptions(message, job->num_keywords,
4883 &job->keywords);
c9fc04c6 4884 }
4d301e69 4885 else
f899b121 4886 {
4d301e69
MS
4887 /*
4888 * Strip legacy message prefix...
4889 */
f899b121 4890
4d301e69 4891 if (!strncmp(message, "recoverable:", 12))
d2354e63 4892 {
4d301e69
MS
4893 ptr = message + 12;
4894 while (isspace(*ptr & 255))
4895 ptr ++;
d2354e63 4896 }
4d301e69 4897 else if (!strncmp(message, "recovered:", 10))
acb056cb 4898 {
4d301e69
MS
4899 ptr = message + 10;
4900 while (isspace(*ptr & 255))
4901 ptr ++;
acb056cb 4902 }
4d301e69
MS
4903 else
4904 ptr = message;
4905
dcb445bc
MS
4906 if (*ptr)
4907 cupsdLogJob(job, loglevel, "%s", ptr);
f899b121 4908
5180a04c
MS
4909 if (loglevel < CUPSD_LOG_DEBUG &&
4910 strcmp(job->printer->state_message, ptr))
1f0275e3 4911 {
4d301e69 4912 strlcpy(job->printer->state_message, ptr,
b9faaae1 4913 sizeof(job->printer->state_message));
4509bb49 4914
8b116e60 4915 event |= CUPSD_EVENT_PRINTER_STATE | CUPSD_EVENT_JOB_PROGRESS;
4509bb49 4916
229681c1 4917 if (loglevel <= job->status_level && job->status_level > CUPSD_LOG_ERROR)
1f0275e3
MS
4918 {
4919 /*
b9faaae1 4920 * Some messages show in the job-printer-state-message attribute...
1f0275e3 4921 */
09a101d6 4922
1f0275e3
MS
4923 if (loglevel != CUPSD_LOG_NOTICE)
4924 job->status_level = loglevel;
01ce6322 4925
1f0275e3 4926 update_job_attrs(job, 1);
f0ab5bff
MS
4927
4928 cupsdLogJob(job, CUPSD_LOG_DEBUG,
4929 "Set job-printer-state-message to \"%s\", "
4930 "current level=%s",
4931 job->printer_message->values[0].string.text,
4932 levels[job->status_level]);
1f0275e3 4933 }
75bd9771 4934 }
f899b121 4935 }
4936
4937 if (!strchr(job->status_buffer->buffer, '\n'))
4938 break;
4939 }
4940
f8b3a85b
MS
4941 if (event & CUPSD_EVENT_JOB_PROGRESS)
4942 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS, job->printer, job,
4943 "%s", job->printer->state_message);
5180a04c 4944 if (event & CUPSD_EVENT_PRINTER_STATE)
b9faaae1
MS
4945 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, job->printer, NULL,
4946 (job->printer->type & CUPS_PRINTER_CLASS) ?
f899b121 4947 "Class \"%s\" state changed." :
4948 "Printer \"%s\" state changed.",
b9faaae1 4949 job->printer->name);
f899b121 4950
8b116e60 4951
f899b121 4952 if (ptr == NULL && !job->status_buffer->bufused)
4953 {
4954 /*
4955 * See if all of the filters and the backend have returned their
4956 * exit statuses.
4957 */
4958
4959 for (i = 0; job->filters[i] < 0; i ++);
4960
4961 if (job->filters[i])
d7871c8c
MS
4962 {
4963 /*
4964 * EOF but we haven't collected the exit status of all filters...
4965 */
4966
4967 cupsdCheckProcess();
f899b121 4968 return;
d7871c8c 4969 }
f899b121 4970
4971 if (job->current_file >= job->num_files && job->backend > 0)
d7871c8c
MS
4972 {
4973 /*
4974 * EOF but we haven't collected the exit status of the backend...
4975 */
4976
4977 cupsdCheckProcess();
f899b121 4978 return;
d7871c8c 4979 }
f899b121 4980
4981 /*
4982 * Handle the end of job stuff...
4983 */
4984
ef55b745 4985 finalize_job(job, 1);
b9faaae1
MS
4986
4987 /*
4988 * Check for new jobs...
4989 */
4990
4991 cupsdCheckJobs();
f899b121 4992 }
4993}
4994
4995
4996/*
bc44d920 4997 * 'update_job_attrs()' - Update the job-printer-* attributes.
4998 */
4999
5000void
4509bb49 5001update_job_attrs(cupsd_job_t *job, /* I - Job to update */
b9faaae1 5002 int do_message)/* I - 1 = copy job-printer-state message */
bc44d920 5003{
5004 int i; /* Looping var */
5005 int num_reasons; /* Actual number of reasons */
5006 const char * const *reasons; /* Reasons */
f0ab5bff 5007 static const char *none = "none"; /* "none" reason */
bc44d920 5008
5009
5010 /*
5011 * Get/create the job-printer-state-* attributes...
5012 */
5013
5014 if (!job->printer_message)
5015 {
5016 if ((job->printer_message = ippFindAttribute(job->attrs,
5017 "job-printer-state-message",
5018 IPP_TAG_TEXT)) == NULL)
5019 job->printer_message = ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_TEXT,
5020 "job-printer-state-message",
5021 NULL, "");
5022 }
5023
5024 if (!job->printer_reasons)
5025 job->printer_reasons = ippFindAttribute(job->attrs,
5026 "job-printer-state-reasons",
5027 IPP_TAG_KEYWORD);
5028
bc44d920 5029 /*
b9faaae1 5030 * Copy or clear the printer-state-message value as needed...
bc44d920 5031 */
5032
b9faaae1
MS
5033 if (job->state_value != IPP_JOB_PROCESSING &&
5034 job->status_level == CUPSD_LOG_INFO)
dcb445bc 5035 {
b9faaae1 5036 cupsdSetString(&(job->printer_message->values[0].string.text), "");
dcb445bc
MS
5037
5038 job->dirty = 1;
5039 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
5040 }
b9faaae1 5041 else if (job->printer->state_message[0] && do_message)
dcb445bc 5042 {
bc44d920 5043 cupsdSetString(&(job->printer_message->values[0].string.text),
b9faaae1 5044 job->printer->state_message);
ef55b745 5045
dcb445bc
MS
5046 job->dirty = 1;
5047 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
5048 }
5049
bc44d920 5050 /*
5051 * ... and the printer-state-reasons value...
5052 */
5053
b9faaae1 5054 if (job->printer->num_reasons == 0)
bc44d920 5055 {
5056 num_reasons = 1;
f0ab5bff 5057 reasons = &none;
bc44d920 5058 }
5059 else
5060 {
b9faaae1
MS
5061 num_reasons = job->printer->num_reasons;
5062 reasons = (const char * const *)job->printer->reasons;
bc44d920 5063 }
5064
5065 if (!job->printer_reasons || job->printer_reasons->num_values != num_reasons)
5066 {
b9faaae1
MS
5067 /*
5068 * Replace/create a job-printer-state-reasons attribute...
5069 */
5070
bc44d920 5071 ippDeleteAttribute(job->attrs, job->printer_reasons);
5072
5073 job->printer_reasons = ippAddStrings(job->attrs,
5074 IPP_TAG_JOB, IPP_TAG_KEYWORD,
5075 "job-printer-state-reasons",
5076 num_reasons, NULL, NULL);
5077 }
b9faaae1 5078 else
d2354e63 5079 {
b9faaae1
MS
5080 /*
5081 * Don't bother clearing the reason strings if they are the same...
5082 */
5083
5084 for (i = 0; i < num_reasons; i ++)
5085 if (strcmp(job->printer_reasons->values[i].string.text, reasons[i]))
5086 break;
5087
5088 if (i >= num_reasons)
5089 return;
5090
5091 /*
5092 * Not the same, so free the current strings...
5093 */
5094
5095 for (i = 0; i < num_reasons; i ++)
d2354e63
MS
5096 _cupsStrFree(job->printer_reasons->values[i].string.text);
5097 }
bc44d920 5098
b9faaae1
MS
5099 /*
5100 * Copy the reasons...
5101 */
5102
bc44d920 5103 for (i = 0; i < num_reasons; i ++)
d2354e63 5104 job->printer_reasons->values[i].string.text = _cupsStrAlloc(reasons[i]);
dcb445bc
MS
5105
5106 job->dirty = 1;
5107 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
bc44d920 5108}
5109
5110
5111/*
b19ccc9e 5112 * End of "$Id: job.c 7902 2008-09-03 14:20:17Z mike $".
ef416fc2 5113 */