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