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