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