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