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