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