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