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