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