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