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