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