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