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