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