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