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