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