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