]> git.ipfire.org Git - thirdparty/cups.git/blame - scheduler/job.c
Merge changes from CUPS 1.4svn-r8606.
[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 */
f11a948a 461 ipp_jstate_t abort_state = IPP_JOB_STOPPED;
e07d4801 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 547 abort_message = "Aborting job because it cannot be printed.";
f11a948a
MS
548 abort_state = IPP_JOB_ABORTED;
549
e07d4801 550 goto abort_job;
b9faaae1 551 }
ef416fc2 552
ef416fc2 553 /*
b9faaae1 554 * Remove NULL ("-") filters...
ef416fc2 555 */
556
b9faaae1
MS
557 for (filter = (mime_filter_t *)cupsArrayFirst(filters);
558 filter;
559 filter = (mime_filter_t *)cupsArrayNext(filters))
560 if (!strcmp(filter->filter, "-"))
561 cupsArrayRemove(filters, filter);
ef416fc2 562
b9faaae1
MS
563 if (cupsArrayCount(filters) == 0)
564 {
565 cupsArrayDelete(filters);
566 filters = NULL;
567 }
ef416fc2 568
b9faaae1
MS
569 /*
570 * If this printer has any pre-filters, insert the required pre-filter
571 * in the filters array...
572 */
ef416fc2 573
b9faaae1
MS
574 if (job->printer->prefiltertype && filters)
575 {
576 prefilters = cupsArrayNew(NULL, NULL);
577
578 for (filter = (mime_filter_t *)cupsArrayFirst(filters);
579 filter;
580 filter = (mime_filter_t *)cupsArrayNext(filters))
581 {
582 if ((prefilter = mimeFilterLookup(MimeDatabase, filter->src,
583 job->printer->prefiltertype)))
584 {
585 cupsArrayAdd(prefilters, prefilter);
586 job->cost += prefilter->cost;
587 }
ef416fc2 588
b9faaae1
MS
589 cupsArrayAdd(prefilters, filter);
590 }
e00b005a 591
b9faaae1
MS
592 cupsArrayDelete(filters);
593 filters = prefilters;
594 }
595 }
bc44d920 596
c168a833 597 /*
b9faaae1
MS
598 * Set a minimum cost of 100 for all jobs so that FilterLimit
599 * works with raw queues and other low-cost paths.
c168a833
MS
600 */
601
b9faaae1
MS
602 if (job->cost < 100)
603 job->cost = 100;
c168a833 604
745129be 605 /*
b9faaae1 606 * See if the filter cost is too high...
745129be
MS
607 */
608
b9faaae1
MS
609 if ((FilterLevel + job->cost) > FilterLimit && FilterLevel > 0 &&
610 FilterLimit > 0)
ef416fc2 611 {
612 /*
b9faaae1 613 * Don't print this job quite yet...
ef416fc2 614 */
615
b9faaae1 616 cupsArrayDelete(filters);
a74454a7 617
b9faaae1
MS
618 cupsdLogJob(job, CUPSD_LOG_INFO,
619 "Holding because filter limit has been reached.");
620 cupsdLogJob(job, CUPSD_LOG_DEBUG2,
621 "cupsdContinueJob: file=%d, cost=%d, level=%d, limit=%d",
622 job->current_file, job->cost, FilterLevel,
623 FilterLimit);
e07d4801
MS
624
625 job->pending_cost = job->cost;
626 job->cost = 0;
b9faaae1
MS
627 return;
628 }
a74454a7 629
b9faaae1 630 FilterLevel += job->cost;
a74454a7 631
b9faaae1
MS
632 /*
633 * Add decompression/raw filter as needed...
634 */
a74454a7 635
b9faaae1
MS
636 if ((!job->printer->raw && job->compressions[job->current_file]) ||
637 (!filters && !job->printer->remote &&
638 (job->num_files > 1 || !strncmp(job->printer->device_uri, "file:", 5))))
639 {
a74454a7 640 /*
b9faaae1 641 * Add gziptoany filter to the front of the list...
a74454a7 642 */
643
b9faaae1
MS
644 if (!filters)
645 filters = cupsArrayNew(NULL, NULL);
ef416fc2 646
b9faaae1
MS
647 if (!cupsArrayInsert(filters, &gziptoany_filter))
648 {
649 cupsdLogJob(job, CUPSD_LOG_DEBUG,
650 "Unable to add decompression filter - %s", strerror(errno));
ed486911 651
b9faaae1 652 cupsArrayDelete(filters);
ed486911 653
e07d4801 654 abort_message = "Stopping job because the scheduler ran out of memory.";
ed486911 655
e07d4801 656 goto abort_job;
b9faaae1
MS
657 }
658 }
ef416fc2 659
b9faaae1
MS
660 /*
661 * Add port monitor, if any...
662 */
ef416fc2 663
b9faaae1
MS
664 if (job->printer->port_monitor)
665 {
666 /*
667 * Add port monitor to the end of the list...
668 */
ef416fc2 669
b9faaae1
MS
670 if (!filters)
671 filters = cupsArrayNew(NULL, NULL);
ef416fc2 672
b9faaae1
MS
673 port_monitor.src = NULL;
674 port_monitor.dst = NULL;
675 port_monitor.cost = 0;
ef416fc2 676
b9faaae1
MS
677 snprintf(port_monitor.filter, sizeof(port_monitor.filter),
678 "%s/monitor/%s", ServerBin, job->printer->port_monitor);
ef416fc2 679
b9faaae1
MS
680 if (!cupsArrayAdd(filters, &port_monitor))
681 {
682 cupsdLogJob(job, CUPSD_LOG_DEBUG,
683 "Unable to add port monitor - %s", strerror(errno));
ef416fc2 684
e07d4801 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 701 abort_message = "Aborting job because it needs too many filters to print.";
f11a948a
MS
702 abort_state = IPP_JOB_ABORTED;
703
e07d4801 704 goto abort_job;
b9faaae1 705 }
07725fee 706
b9faaae1
MS
707 /*
708 * Determine if we are printing a banner page or not...
709 */
07725fee 710
b9faaae1
MS
711 if (job->job_sheets == NULL)
712 {
713 cupsdLogJob(job, CUPSD_LOG_DEBUG, "No job-sheets attribute.");
714 if ((job->job_sheets =
715 ippFindAttribute(job->attrs, "job-sheets", IPP_TAG_ZERO)) != NULL)
716 cupsdLogJob(job, CUPSD_LOG_DEBUG,
717 "... but someone added one without setting job_sheets!");
718 }
719 else if (job->job_sheets->num_values == 1)
720 cupsdLogJob(job, CUPSD_LOG_DEBUG, "job-sheets=%s",
721 job->job_sheets->values[0].string.text);
722 else
723 cupsdLogJob(job, CUPSD_LOG_DEBUG, "job-sheets=%s,%s",
724 job->job_sheets->values[0].string.text,
725 job->job_sheets->values[1].string.text);
07725fee 726
b9faaae1
MS
727 if (job->printer->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT))
728 banner_page = 0;
729 else if (job->job_sheets == NULL)
730 banner_page = 0;
731 else if (strcasecmp(job->job_sheets->values[0].string.text, "none") != 0 &&
732 job->current_file == 0)
733 banner_page = 1;
734 else if (job->job_sheets->num_values > 1 &&
735 strcasecmp(job->job_sheets->values[1].string.text, "none") != 0 &&
736 job->current_file == (job->num_files - 1))
737 banner_page = 1;
738 else
739 banner_page = 0;
07725fee 740
b9faaae1
MS
741 if ((options = get_options(job, banner_page, copies, sizeof(copies), title,
742 sizeof(title))) == NULL)
743 {
e07d4801 744 abort_message = "Stopping job because the scheduler ran out of memory.";
ef416fc2 745
e07d4801 746 goto abort_job;
b9faaae1 747 }
ef416fc2 748
b9faaae1
MS
749 /*
750 * Build the command-line arguments for the filters. Each filter
751 * has 6 or 7 arguments:
752 *
753 * argv[0] = printer
754 * argv[1] = job ID
755 * argv[2] = username
756 * argv[3] = title
757 * argv[4] = # copies
758 * argv[5] = options
759 * argv[6] = filename (optional; normally stdin)
760 *
761 * This allows legacy printer drivers that use the old System V
762 * printing interface to be used by CUPS.
763 *
764 * For remote jobs, we send all of the files in the argument list.
765 */
07725fee 766
b9faaae1
MS
767 if (job->printer->remote)
768 argv = calloc(7 + job->num_files, sizeof(char *));
769 else
770 argv = calloc(8, sizeof(char *));
07725fee 771
b9faaae1
MS
772 if (!argv)
773 {
774 cupsdLogMessage(CUPSD_LOG_DEBUG, "Unable to allocate argument array - %s",
775 strerror(errno));
07725fee 776
e07d4801
MS
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
f11a948a 1051 abort_message = "Stopping job because the scheduler could not execute a "
b9faaae1 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 :
f11a948a
MS
2552 job_state = IPP_JOB_COMPLETED;
2553 message = "Job completed.";
1340db2d
MS
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 }
f11a948a 2687 else if (job->state_value == IPP_JOB_PROCESSING)
b9faaae1 2688 {
f11a948a 2689 job_state = IPP_JOB_PENDING;
b9faaae1 2690 printer_state = IPP_PRINTER_STOPPED;
b9faaae1 2691 message = "Printer stopped due to backend errors; please "
8b116e60 2692 "consult the error_log file for details.";
b9faaae1
MS
2693 }
2694 break;
bd7854cb 2695
b9faaae1
MS
2696 case CUPS_BACKEND_CANCEL :
2697 /*
2698 * Abort the job...
2699 */
bd7854cb 2700
8b116e60
MS
2701 if (job_state == IPP_JOB_COMPLETED)
2702 {
2703 job_state = IPP_JOB_ABORTED;
2704 message = "Job aborted due to backend errors; please consult "
2705 "the error_log file for details.";
2706 }
b9faaae1 2707 break;
bd7854cb 2708
b9faaae1 2709 case CUPS_BACKEND_HOLD :
8b116e60
MS
2710 if (job_state == IPP_JOB_COMPLETED)
2711 {
2712 /*
2713 * Hold the job...
2714 */
bd7854cb 2715
8b116e60 2716 cupsdSetJobHoldUntil(job, "indefinite", 1);
bd7854cb 2717
8b116e60
MS
2718 job_state = IPP_JOB_HELD;
2719 message = "Job held indefinitely due to backend errors; please "
2720 "consult the error_log file for details.";
2721 }
b9faaae1 2722 break;
2abf387c 2723
b9faaae1
MS
2724 case CUPS_BACKEND_STOP :
2725 /*
2726 * Stop the printer...
2727 */
bd7854cb 2728
b9faaae1 2729 printer_state = IPP_PRINTER_STOPPED;
b9faaae1
MS
2730 message = "Printer stopped due to backend errors; please "
2731 "consult the error_log file for details.";
8b116e60
MS
2732
2733 if (job_state == IPP_JOB_COMPLETED)
2734 job_state = IPP_JOB_PENDING;
b9faaae1 2735 break;
bd7854cb 2736
b9faaae1
MS
2737 case CUPS_BACKEND_AUTH_REQUIRED :
2738 /*
2739 * Hold the job for authentication...
2740 */
bd7854cb 2741
8b116e60
MS
2742 if (job_state == IPP_JOB_COMPLETED)
2743 {
2744 cupsdSetJobHoldUntil(job, "auth-info-required", 1);
ef416fc2 2745
8b116e60
MS
2746 job_state = IPP_JOB_HELD;
2747 message = "Job held for authentication.";
2748 }
b9faaae1 2749 break;
e1d6a774 2750 }
2751 }
b9faaae1 2752 else if (job->status > 0)
ef416fc2 2753 {
2754 /*
b9faaae1 2755 * Filter had errors; stop job...
ef416fc2 2756 */
2757
8b116e60
MS
2758 if (job_state == IPP_JOB_COMPLETED)
2759 {
2760 job_state = IPP_JOB_STOPPED;
2761 message = "Job stopped due to filter errors; please consult the "
2762 "error_log file for details.";
2763 }
e1d6a774 2764 }
e00b005a 2765
3dfe78b3 2766 /*
b9faaae1 2767 * Update the printer and job state.
3dfe78b3
MS
2768 */
2769
b9faaae1
MS
2770 cupsdSetJobState(job, job_state, CUPSD_JOB_DEFAULT, "%s", message);
2771 cupsdSetPrinterState(job->printer, printer_state,
2772 printer_state == IPP_PRINTER_STOPPED);
2773 update_job_attrs(job, 0);
3dfe78b3 2774
b9faaae1 2775 cupsArrayRemove(PrintingJobs, job);
3dfe78b3 2776
e1d6a774 2777 /*
b9faaae1 2778 * Clear the printer <-> job association...
e1d6a774 2779 */
ef416fc2 2780
b9faaae1
MS
2781 job->printer->job = NULL;
2782 job->printer = NULL;
ef416fc2 2783
e1d6a774 2784 /*
b9faaae1 2785 * Try printing another job...
e1d6a774 2786 */
ef416fc2 2787
b9faaae1
MS
2788 if (printer_state != IPP_PRINTER_STOPPED)
2789 cupsdCheckJobs();
2790}
ef416fc2 2791
ef416fc2 2792
b9faaae1
MS
2793/*
2794 * 'get_options()' - Get a string containing the job options.
2795 */
2796
2797static char * /* O - Options string */
2798get_options(cupsd_job_t *job, /* I - Job */
2799 int banner_page, /* I - Printing a banner page? */
2800 char *copies, /* I - Copies buffer */
2801 size_t copies_size, /* I - Size of copies buffer */
2802 char *title, /* I - Title buffer */
2803 size_t title_size) /* I - Size of title buffer */
2804{
2805 int i; /* Looping var */
2806 char *optptr, /* Pointer to options */
2807 *valptr; /* Pointer in value string */
2808 ipp_attribute_t *attr; /* Current attribute */
2809 static char *options = NULL;/* Full list of options */
2810 static int optlength = 0; /* Length of option buffer */
2811
ef416fc2 2812
e1d6a774 2813 /*
2814 * Building the options string is harder than it needs to be, but
2815 * for the moment we need to pass strings for command-line args and
2816 * not IPP attribute pointers... :)
2817 *
2818 * First allocate/reallocate the option buffer as needed...
2819 */
ef416fc2 2820
e1d6a774 2821 i = ipp_length(job->attrs);
ef416fc2 2822
1f0275e3 2823 if (i > optlength || !options)
e1d6a774 2824 {
b9faaae1 2825 if (!options)
e1d6a774 2826 optptr = malloc(i);
2827 else
2828 optptr = realloc(options, i);
ef416fc2 2829
b9faaae1 2830 if (!optptr)
e1d6a774 2831 {
75bd9771
MS
2832 cupsdLogJob(job, CUPSD_LOG_CRIT,
2833 "Unable to allocate %d bytes for option buffer!", i);
b9faaae1 2834 return (NULL);
e1d6a774 2835 }
ef416fc2 2836
e1d6a774 2837 options = optptr;
2838 optlength = i;
2839 }
ef416fc2 2840
e1d6a774 2841 /*
2842 * Now loop through the attributes and convert them to the textual
2843 * representation used by the filters...
2844 */
ef416fc2 2845
e1d6a774 2846 optptr = options;
2847 *optptr = '\0';
bd7854cb 2848
1340db2d
MS
2849 snprintf(title, title_size, "%s-%d", job->printer->name, job->id);
2850 strlcpy(copies, "1", copies_size);
bd7854cb 2851
e1d6a774 2852 for (attr = job->attrs->attrs; attr != NULL; attr = attr->next)
bd7854cb 2853 {
e1d6a774 2854 if (!strcmp(attr->name, "copies") &&
2855 attr->value_tag == IPP_TAG_INTEGER)
2856 {
2857 /*
2858 * Don't use the # copies attribute if we are printing the job sheets...
2859 */
bd7854cb 2860
e1d6a774 2861 if (!banner_page)
1340db2d 2862 snprintf(copies, copies_size, "%d", attr->values[0].integer);
e1d6a774 2863 }
2864 else if (!strcmp(attr->name, "job-name") &&
2865 (attr->value_tag == IPP_TAG_NAME ||
2866 attr->value_tag == IPP_TAG_NAMELANG))
1340db2d 2867 strlcpy(title, attr->values[0].string.text, title_size);
e1d6a774 2868 else if (attr->group_tag == IPP_TAG_JOB)
2869 {
2870 /*
2871 * Filter out other unwanted attributes...
2872 */
bd7854cb 2873
e1d6a774 2874 if (attr->value_tag == IPP_TAG_MIMETYPE ||
2875 attr->value_tag == IPP_TAG_NAMELANG ||
2876 attr->value_tag == IPP_TAG_TEXTLANG ||
2877 (attr->value_tag == IPP_TAG_URI && strcmp(attr->name, "job-uuid")) ||
2878 attr->value_tag == IPP_TAG_URISCHEME ||
2879 attr->value_tag == IPP_TAG_BEGIN_COLLECTION) /* Not yet supported */
2880 continue;
bd7854cb 2881
e1d6a774 2882 if (!strncmp(attr->name, "time-", 5))
2883 continue;
bd7854cb 2884
e1d6a774 2885 if (!strncmp(attr->name, "job-", 4) &&
e4572d57 2886 strcmp(attr->name, "job-billing") &&
5d6412a9 2887 strcmp(attr->name, "job-impressions") &&
c5571a1d 2888 strcmp(attr->name, "job-originating-host-name") &&
e4572d57 2889 strcmp(attr->name, "job-uuid") &&
b9faaae1 2890 !(job->printer->type & CUPS_PRINTER_REMOTE))
e1d6a774 2891 continue;
ef416fc2 2892
5d6412a9
MS
2893 if ((!strcmp(attr->name, "job-impressions") ||
2894 !strcmp(attr->name, "page-label") ||
e1d6a774 2895 !strcmp(attr->name, "page-border") ||
2896 !strncmp(attr->name, "number-up", 9) ||
b94498cf 2897 !strcmp(attr->name, "page-ranges") ||
e1d6a774 2898 !strcmp(attr->name, "page-set") ||
2899 !strcasecmp(attr->name, "AP_FIRSTPAGE_InputSlot") ||
db1f069b
MS
2900 !strcasecmp(attr->name, "AP_FIRSTPAGE_ManualFeed") ||
2901 !strcasecmp(attr->name, "com.apple.print.PrintSettings."
2902 "PMTotalSidesImaged..n.") ||
2903 !strcasecmp(attr->name, "com.apple.print.PrintSettings."
2904 "PMTotalBeginPages..n.")) &&
e1d6a774 2905 banner_page)
2906 continue;
ef416fc2 2907
e1d6a774 2908 /*
2909 * Otherwise add them to the list...
2910 */
ef416fc2 2911
e1d6a774 2912 if (optptr > options)
2913 strlcat(optptr, " ", optlength - (optptr - options));
ef416fc2 2914
e1d6a774 2915 if (attr->value_tag != IPP_TAG_BOOLEAN)
2916 {
2917 strlcat(optptr, attr->name, optlength - (optptr - options));
2918 strlcat(optptr, "=", optlength - (optptr - options));
2919 }
ef416fc2 2920
e1d6a774 2921 for (i = 0; i < attr->num_values; i ++)
2922 {
2923 if (i)
2924 strlcat(optptr, ",", optlength - (optptr - options));
ef416fc2 2925
e1d6a774 2926 optptr += strlen(optptr);
ef416fc2 2927
e1d6a774 2928 switch (attr->value_tag)
2929 {
2930 case IPP_TAG_INTEGER :
2931 case IPP_TAG_ENUM :
2932 snprintf(optptr, optlength - (optptr - options),
2933 "%d", attr->values[i].integer);
2934 break;
ef416fc2 2935
e1d6a774 2936 case IPP_TAG_BOOLEAN :
2937 if (!attr->values[i].boolean)
2938 strlcat(optptr, "no", optlength - (optptr - options));
ef416fc2 2939
e1d6a774 2940 case IPP_TAG_NOVALUE :
2941 strlcat(optptr, attr->name,
2942 optlength - (optptr - options));
2943 break;
ef416fc2 2944
e1d6a774 2945 case IPP_TAG_RANGE :
2946 if (attr->values[i].range.lower == attr->values[i].range.upper)
2947 snprintf(optptr, optlength - (optptr - options) - 1,
2948 "%d", attr->values[i].range.lower);
2949 else
2950 snprintf(optptr, optlength - (optptr - options) - 1,
2951 "%d-%d", attr->values[i].range.lower,
2952 attr->values[i].range.upper);
2953 break;
ef416fc2 2954
e1d6a774 2955 case IPP_TAG_RESOLUTION :
2956 snprintf(optptr, optlength - (optptr - options) - 1,
2957 "%dx%d%s", attr->values[i].resolution.xres,
2958 attr->values[i].resolution.yres,
2959 attr->values[i].resolution.units == IPP_RES_PER_INCH ?
2960 "dpi" : "dpc");
2961 break;
ef416fc2 2962
e1d6a774 2963 case IPP_TAG_STRING :
2964 case IPP_TAG_TEXT :
2965 case IPP_TAG_NAME :
2966 case IPP_TAG_KEYWORD :
2967 case IPP_TAG_CHARSET :
2968 case IPP_TAG_LANGUAGE :
2969 case IPP_TAG_URI :
2970 for (valptr = attr->values[i].string.text; *valptr;)
2971 {
2972 if (strchr(" \t\n\\\'\"", *valptr))
2973 *optptr++ = '\\';
2974 *optptr++ = *valptr++;
2975 }
2976
2977 *optptr = '\0';
2978 break;
2979
2980 default :
2981 break; /* anti-compiler-warning-code */
2982 }
2983 }
2984
2985 optptr += strlen(optptr);
2986 }
2987 }
2988
b9faaae1
MS
2989
2990 return (options);
2991}
2992
2993
2994/*
2995 * 'ipp_length()' - Compute the size of the buffer needed to hold
2996 * the textual IPP attributes.
2997 */
2998
2999static int /* O - Size of attribute buffer */
3000ipp_length(ipp_t *ipp) /* I - IPP request */
3001{
3002 int bytes; /* Number of bytes */
3003 int i; /* Looping var */
3004 ipp_attribute_t *attr; /* Current attribute */
3005
3006
3007 /*
3008 * Loop through all attributes...
e1d6a774 3009 */
3010
b9faaae1 3011 bytes = 0;
e1d6a774 3012
b9faaae1 3013 for (attr = ipp->attrs; attr != NULL; attr = attr->next)
91c84a35 3014 {
b9faaae1
MS
3015 /*
3016 * Skip attributes that won't be sent to filters...
3017 */
91c84a35 3018
b9faaae1
MS
3019 if (attr->value_tag == IPP_TAG_MIMETYPE ||
3020 attr->value_tag == IPP_TAG_NAMELANG ||
3021 attr->value_tag == IPP_TAG_TEXTLANG ||
3022 attr->value_tag == IPP_TAG_URI ||
3023 attr->value_tag == IPP_TAG_URISCHEME)
3024 continue;
91c84a35 3025
b9faaae1
MS
3026 if (strncmp(attr->name, "time-", 5) == 0)
3027 continue;
91c84a35 3028
b9faaae1
MS
3029 /*
3030 * Add space for a leading space and commas between each value.
3031 * For the first attribute, the leading space isn't used, so the
3032 * extra byte can be used as the nul terminator...
3033 */
e1d6a774 3034
b9faaae1
MS
3035 bytes ++; /* " " separator */
3036 bytes += attr->num_values; /* "," separators */
e1d6a774 3037
b9faaae1
MS
3038 /*
3039 * Boolean attributes appear as "foo,nofoo,foo,nofoo", while
3040 * other attributes appear as "foo=value1,value2,...,valueN".
3041 */
3042
3043 if (attr->value_tag != IPP_TAG_BOOLEAN)
3044 bytes += strlen(attr->name);
3045 else
3046 bytes += attr->num_values * strlen(attr->name);
3047
3048 /*
3049 * Now add the size required for each value in the attribute...
3050 */
3051
3052 switch (attr->value_tag)
ef416fc2 3053 {
b9faaae1
MS
3054 case IPP_TAG_INTEGER :
3055 case IPP_TAG_ENUM :
3056 /*
3057 * Minimum value of a signed integer is -2147483647, or 11 digits.
3058 */
3059
3060 bytes += attr->num_values * 11;
3061 break;
3062
3063 case IPP_TAG_BOOLEAN :
3064 /*
3065 * Add two bytes for each false ("no") value...
3066 */
3067
3068 for (i = 0; i < attr->num_values; i ++)
3069 if (!attr->values[i].boolean)
3070 bytes += 2;
3071 break;
3072
3073 case IPP_TAG_RANGE :
3074 /*
3075 * A range is two signed integers separated by a hyphen, or
3076 * 23 characters max.
3077 */
3078
3079 bytes += attr->num_values * 23;
3080 break;
3081
3082 case IPP_TAG_RESOLUTION :
3083 /*
3084 * A resolution is two signed integers separated by an "x" and
3085 * suffixed by the units, or 26 characters max.
3086 */
3087
3088 bytes += attr->num_values * 26;
3089 break;
3090
3091 case IPP_TAG_STRING :
3092 case IPP_TAG_TEXT :
3093 case IPP_TAG_NAME :
3094 case IPP_TAG_KEYWORD :
3095 case IPP_TAG_CHARSET :
3096 case IPP_TAG_LANGUAGE :
3097 case IPP_TAG_URI :
3098 /*
3099 * Strings can contain characters that need quoting. We need
3100 * at least 2 * len + 2 characters to cover the quotes and
3101 * any backslashes in the string.
3102 */
3103
3104 for (i = 0; i < attr->num_values; i ++)
3105 bytes += 2 * strlen(attr->values[i].string.text) + 2;
3106 break;
3107
3108 default :
3109 break; /* anti-compiler-warning-code */
e1d6a774 3110 }
3111 }
b9faaae1
MS
3112
3113 return (bytes);
3114}
3115
3116
3117/*
3118 * 'load_job_cache()' - Load jobs from the job.cache file.
3119 */
3120
3121static void
3122load_job_cache(const char *filename) /* I - job.cache filename */
3123{
3124 cups_file_t *fp; /* job.cache file */
3125 char line[1024], /* Line buffer */
3126 *value; /* Value on line */
3127 int linenum; /* Line number in file */
3128 cupsd_job_t *job; /* Current job */
3129 int jobid; /* Job ID */
3130 char jobfile[1024]; /* Job filename */
3131
3132
3133 /*
3134 * Open the job.cache file...
3135 */
3136
3137 if ((fp = cupsFileOpen(filename, "r")) == NULL)
e1d6a774 3138 {
b9faaae1
MS
3139 if (errno != ENOENT)
3140 cupsdLogMessage(CUPSD_LOG_ERROR,
3141 "Unable to open job cache file \"%s\": %s",
3142 filename, strerror(errno));
ef416fc2 3143
b9faaae1
MS
3144 load_request_root();
3145
3146 return;
3147 }
ef416fc2 3148
e1d6a774 3149 /*
b9faaae1 3150 * Read entries from the job cache file and create jobs as needed.
e1d6a774 3151 */
ef416fc2 3152
b9faaae1
MS
3153 cupsdLogMessage(CUPSD_LOG_INFO, "Loading job cache file \"%s\"...",
3154 filename);
ef416fc2 3155
b9faaae1
MS
3156 linenum = 0;
3157 job = NULL;
3158
3159 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
3160 {
3161 if (!strcasecmp(line, "NextJobId"))
3162 {
3163 if (value)
3164 NextJobId = atoi(value);
3165 }
3166 else if (!strcasecmp(line, "<Job"))
3167 {
3168 if (job)
3169 {
3170 cupsdLogMessage(CUPSD_LOG_ERROR, "Missing </Job> directive on line %d!",
3171 linenum);
3172 continue;
3173 }
3174
3175 if (!value)
3176 {
3177 cupsdLogMessage(CUPSD_LOG_ERROR, "Missing job ID on line %d!", linenum);
3178 continue;
3179 }
3180
3181 jobid = atoi(value);
3182
3183 if (jobid < 1)
3184 {
3185 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad job ID %d on line %d!", jobid,
3186 linenum);
3187 continue;
3188 }
3189
3190 snprintf(jobfile, sizeof(jobfile), "%s/c%05d", RequestRoot, jobid);
3191 if (access(jobfile, 0))
3192 {
3193 cupsdLogMessage(CUPSD_LOG_ERROR, "[Job %d] Files have gone away!",
3194 jobid);
3195 continue;
3196 }
3197
3198 job = calloc(1, sizeof(cupsd_job_t));
3199 if (!job)
3200 {
3201 cupsdLogMessage(CUPSD_LOG_EMERG,
3202 "[Job %d] Unable to allocate memory for job!", jobid);
3203 break;
3204 }
3205
3206 job->id = jobid;
3207 job->back_pipes[0] = -1;
3208 job->back_pipes[1] = -1;
3209 job->print_pipes[0] = -1;
3210 job->print_pipes[1] = -1;
3211 job->side_pipes[0] = -1;
3212 job->side_pipes[1] = -1;
3213 job->status_pipes[0] = -1;
3214 job->status_pipes[1] = -1;
3215
3216 cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] Loading from cache...",
3217 job->id);
3218 }
3219 else if (!job)
3220 {
3221 cupsdLogMessage(CUPSD_LOG_ERROR,
3222 "Missing <Job #> directive on line %d!", linenum);
3223 continue;
3224 }
3225 else if (!strcasecmp(line, "</Job>"))
3226 {
3227 cupsArrayAdd(Jobs, job);
3228
3229 if (job->state_value <= IPP_JOB_STOPPED)
3230 {
3231 cupsArrayAdd(ActiveJobs, job);
3232 cupsdLoadJob(job);
3233 }
3234
3235 job = NULL;
3236 }
3237 else if (!value)
3238 {
3239 cupsdLogMessage(CUPSD_LOG_ERROR, "Missing value on line %d!", linenum);
3240 continue;
3241 }
3242 else if (!strcasecmp(line, "State"))
3243 {
3244 job->state_value = (ipp_jstate_t)atoi(value);
3245
3246 if (job->state_value < IPP_JOB_PENDING)
3247 job->state_value = IPP_JOB_PENDING;
3248 else if (job->state_value > IPP_JOB_COMPLETED)
3249 job->state_value = IPP_JOB_COMPLETED;
3250 }
8b116e60
MS
3251 else if (!strcasecmp(line, "HoldUntil"))
3252 {
3253 job->hold_until = atoi(value);
3254 }
b9faaae1
MS
3255 else if (!strcasecmp(line, "Priority"))
3256 {
3257 job->priority = atoi(value);
3258 }
3259 else if (!strcasecmp(line, "Username"))
3260 {
3261 cupsdSetString(&job->username, value);
3262 }
3263 else if (!strcasecmp(line, "Destination"))
3264 {
3265 cupsdSetString(&job->dest, value);
3266 }
3267 else if (!strcasecmp(line, "DestType"))
3268 {
3269 job->dtype = (cups_ptype_t)atoi(value);
3270 }
3271 else if (!strcasecmp(line, "NumFiles"))
3272 {
3273 job->num_files = atoi(value);
3274
3275 if (job->num_files < 0)
3276 {
3277 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad NumFiles value %d on line %d!",
3278 job->num_files, linenum);
3279 job->num_files = 0;
3280 continue;
3281 }
3282
3283 if (job->num_files > 0)
3284 {
3285 snprintf(jobfile, sizeof(jobfile), "%s/d%05d-001", RequestRoot,
3286 job->id);
3287 if (access(jobfile, 0))
3288 {
3289 cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Data files have gone away!",
3290 job->id);
3291 job->num_files = 0;
3292 continue;
3293 }
3294
3295 job->filetypes = calloc(job->num_files, sizeof(mime_type_t *));
3296 job->compressions = calloc(job->num_files, sizeof(int));
3297
3298 if (!job->filetypes || !job->compressions)
3299 {
3300 cupsdLogMessage(CUPSD_LOG_EMERG,
3301 "[Job %d] Unable to allocate memory for %d files!",
3302 job->id, job->num_files);
3303 break;
3304 }
3305 }
3306 }
3307 else if (!strcasecmp(line, "File"))
3308 {
3309 int number, /* File number */
3310 compression; /* Compression value */
3311 char super[MIME_MAX_SUPER], /* MIME super type */
3312 type[MIME_MAX_TYPE]; /* MIME type */
3313
3314
3315 if (sscanf(value, "%d%*[ \t]%15[^/]/%255s%d", &number, super, type,
3316 &compression) != 4)
3317 {
3318 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad File on line %d!", linenum);
3319 continue;
3320 }
3321
3322 if (number < 1 || number > job->num_files)
3323 {
3324 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad File number %d on line %d!",
3325 number, linenum);
3326 continue;
3327 }
0a682745 3328
b9faaae1 3329 number --;
ef416fc2 3330
b9faaae1
MS
3331 job->compressions[number] = compression;
3332 job->filetypes[number] = mimeType(MimeDatabase, super, type);
ef416fc2 3333
b9faaae1
MS
3334 if (!job->filetypes[number])
3335 {
e1d6a774 3336 /*
b9faaae1 3337 * If the original MIME type is unknown, auto-type it!
e1d6a774 3338 */
ef416fc2 3339
b9faaae1
MS
3340 cupsdLogMessage(CUPSD_LOG_ERROR,
3341 "[Job %d] Unknown MIME type %s/%s for file %d!",
3342 job->id, super, type, number + 1);
3343
3344 snprintf(jobfile, sizeof(jobfile), "%s/d%05d-%03d", RequestRoot,
3345 job->id, number + 1);
3346 job->filetypes[number] = mimeFileType(MimeDatabase, jobfile, NULL,
3347 job->compressions + number);
ef416fc2 3348
e1d6a774 3349 /*
b9faaae1 3350 * If that didn't work, assume it is raw...
e1d6a774 3351 */
ef416fc2 3352
b9faaae1
MS
3353 if (!job->filetypes[number])
3354 job->filetypes[number] = mimeType(MimeDatabase, "application",
3355 "vnd.cups-raw");
3356 }
3357 }
3358 else
3359 cupsdLogMessage(CUPSD_LOG_ERROR, "Unknown %s directive on line %d!",
3360 line, linenum);
ef416fc2 3361 }
3362
b9faaae1
MS
3363 cupsFileClose(fp);
3364}
ef416fc2 3365
3366
b9faaae1
MS
3367/*
3368 * 'load_next_job_id()' - Load the NextJobId value from the job.cache file.
3369 */
bd7854cb 3370
b9faaae1
MS
3371static void
3372load_next_job_id(const char *filename) /* I - job.cache filename */
3373{
3374 cups_file_t *fp; /* job.cache file */
3375 char line[1024], /* Line buffer */
3376 *value; /* Value on line */
3377 int linenum; /* Line number in file */
3378 int next_job_id; /* NextJobId value from line */
db0bd74a 3379
db0bd74a 3380
b9faaae1
MS
3381 /*
3382 * Read the NextJobId directive from the job.cache file and use
3383 * the value (if any).
3384 */
bd7854cb 3385
b9faaae1 3386 if ((fp = cupsFileOpen(filename, "r")) == NULL)
e1d6a774 3387 {
b9faaae1
MS
3388 if (errno != ENOENT)
3389 cupsdLogMessage(CUPSD_LOG_ERROR,
3390 "Unable to open job cache file \"%s\": %s",
3391 filename, strerror(errno));
bd7854cb 3392
b9faaae1 3393 return;
e1d6a774 3394 }
bd7854cb 3395
b9faaae1
MS
3396 cupsdLogMessage(CUPSD_LOG_INFO,
3397 "Loading NextJobId from job cache file \"%s\"...", filename);
3398
3399 linenum = 0;
3400
3401 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
bd7854cb 3402 {
b9faaae1
MS
3403 if (!strcasecmp(line, "NextJobId"))
3404 {
3405 if (value)
3406 {
3407 next_job_id = atoi(value);
3408
3409 if (next_job_id > NextJobId)
3410 NextJobId = next_job_id;
3411 }
3412 break;
3413 }
e1d6a774 3414 }
bd7854cb 3415
b9faaae1
MS
3416 cupsFileClose(fp);
3417}
09a101d6 3418
f7deaa1a 3419
b9faaae1
MS
3420/*
3421 * 'load_request_root()' - Load jobs from the RequestRoot directory.
3422 */
bd7854cb 3423
b9faaae1
MS
3424static void
3425load_request_root(void)
3426{
3427 cups_dir_t *dir; /* Directory */
3428 cups_dentry_t *dent; /* Directory entry */
3429 cupsd_job_t *job; /* New job */
e1d6a774 3430
bd7854cb 3431
3432 /*
b9faaae1 3433 * Open the requests directory...
bd7854cb 3434 */
3435
b9faaae1 3436 cupsdLogMessage(CUPSD_LOG_DEBUG, "Scanning %s for jobs...", RequestRoot);
bd7854cb 3437
b9faaae1 3438 if ((dir = cupsDirOpen(RequestRoot)) == NULL)
bd7854cb 3439 {
b9faaae1
MS
3440 cupsdLogMessage(CUPSD_LOG_ERROR,
3441 "Unable to open spool directory \"%s\": %s",
3442 RequestRoot, strerror(errno));
3443 return;
e1d6a774 3444 }
bd7854cb 3445
b9faaae1
MS
3446 /*
3447 * Read all the c##### files...
3448 */
bd7854cb 3449
b9faaae1
MS
3450 while ((dent = cupsDirRead(dir)) != NULL)
3451 if (strlen(dent->filename) >= 6 && dent->filename[0] == 'c')
bd7854cb 3452 {
b9faaae1
MS
3453 /*
3454 * Allocate memory for the job...
3455 */
bd7854cb 3456
b9faaae1 3457 if ((job = calloc(sizeof(cupsd_job_t), 1)) == NULL)
bd7854cb 3458 {
b9faaae1
MS
3459 cupsdLogMessage(CUPSD_LOG_ERROR, "Ran out of memory for jobs!");
3460 cupsDirClose(dir);
3461 return;
3462 }
bd7854cb 3463
b9faaae1
MS
3464 /*
3465 * Assign the job ID...
3466 */
bd7854cb 3467
b9faaae1
MS
3468 job->id = atoi(dent->filename + 1);
3469 job->back_pipes[0] = -1;
3470 job->back_pipes[1] = -1;
3471 job->print_pipes[0] = -1;
3472 job->print_pipes[1] = -1;
3473 job->side_pipes[0] = -1;
3474 job->side_pipes[1] = -1;
3475 job->status_pipes[0] = -1;
3476 job->status_pipes[1] = -1;
bd7854cb 3477
b9faaae1
MS
3478 if (job->id >= NextJobId)
3479 NextJobId = job->id + 1;
ed486911 3480
b9faaae1
MS
3481 /*
3482 * Load the job...
3483 */
ed486911 3484
b9faaae1 3485 cupsdLoadJob(job);
bd7854cb 3486
b9faaae1
MS
3487 /*
3488 * Insert the job into the array, sorting by job priority and ID...
3489 */
bd7854cb 3490
b9faaae1 3491 cupsArrayAdd(Jobs, job);
e1d6a774 3492
b9faaae1
MS
3493 if (job->state_value <= IPP_JOB_STOPPED)
3494 cupsArrayAdd(ActiveJobs, job);
3495 else
3496 unload_job(job);
bd7854cb 3497 }
bd7854cb 3498
b9faaae1
MS
3499 cupsDirClose(dir);
3500}
bd7854cb 3501
3502
b9faaae1
MS
3503/*
3504 * 'set_hold_until()' - Set the hold time and update job-hold-until attribute.
3505 */
bd7854cb 3506
b9faaae1
MS
3507static void
3508set_hold_until(cupsd_job_t *job, /* I - Job to update */
3509 time_t holdtime) /* I - Hold until time */
3510{
3511 ipp_attribute_t *attr; /* job-hold-until attribute */
3512 struct tm *holddate; /* Hold date */
3513 char holdstr[64]; /* Hold time */
bd7854cb 3514
3515
b9faaae1
MS
3516 /*
3517 * Set the hold_until value and hold the job...
3518 */
bd7854cb 3519
b9faaae1
MS
3520 cupsdLogMessage(CUPSD_LOG_DEBUG, "set_hold_until: hold_until = %d",
3521 (int)holdtime);
bd7854cb 3522
b9faaae1
MS
3523 job->state->values[0].integer = IPP_JOB_HELD;
3524 job->state_value = IPP_JOB_HELD;
3525 job->hold_until = holdtime;
e1d6a774 3526
b9faaae1
MS
3527 /*
3528 * Update the job-hold-until attribute with a string representing GMT
3529 * time (HH:MM:SS)...
3530 */
e1d6a774 3531
b9faaae1
MS
3532 holddate = gmtime(&holdtime);
3533 snprintf(holdstr, sizeof(holdstr), "%d:%d:%d", holddate->tm_hour,
3534 holddate->tm_min, holddate->tm_sec);
bd7854cb 3535
b9faaae1
MS
3536 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
3537 IPP_TAG_KEYWORD)) == NULL)
3538 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
bd7854cb 3539
e1d6a774 3540 /*
b9faaae1 3541 * Either add the attribute or update the value of the existing one
e1d6a774 3542 */
bd7854cb 3543
b9faaae1
MS
3544 if (attr == NULL)
3545 ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_KEYWORD, "job-hold-until",
3546 NULL, holdstr);
3547 else
3548 cupsdSetString(&attr->values[0].string.text, holdstr);
bd7854cb 3549
b9faaae1
MS
3550 job->dirty = 1;
3551 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
3552}
bd7854cb 3553
bd7854cb 3554
b9faaae1
MS
3555/*
3556 * 'set_time()' - Set one of the "time-at-xyz" attributes.
3557 */
bd7854cb 3558
b9faaae1
MS
3559static void
3560set_time(cupsd_job_t *job, /* I - Job to update */
3561 const char *name) /* I - Name of attribute */
3562{
3563 ipp_attribute_t *attr; /* Time attribute */
bd7854cb 3564
bd7854cb 3565
b9faaae1
MS
3566 if ((attr = ippFindAttribute(job->attrs, name, IPP_TAG_ZERO)) != NULL)
3567 {
3568 attr->value_tag = IPP_TAG_INTEGER;
3569 attr->values[0].integer = time(NULL);
3570 }
3571}
bd7854cb 3572
e1d6a774 3573
b9faaae1
MS
3574/*
3575 * 'start_job()' - Start a print job.
3576 */
e1d6a774 3577
b9faaae1
MS
3578static void
3579start_job(cupsd_job_t *job, /* I - Job ID */
3580 cupsd_printer_t *printer) /* I - Printer to print job */
3581{
3582 cupsdLogMessage(CUPSD_LOG_DEBUG2, "start_job(job=%p(%d), printer=%p(%s))",
3583 job, job->id, printer, printer->name);
bd7854cb 3584
b9faaae1
MS
3585 /*
3586 * Make sure we have some files around before we try to print...
3587 */
bd7854cb 3588
b9faaae1
MS
3589 if (job->num_files == 0)
3590 {
3591 cupsdSetJobState(job, IPP_JOB_ABORTED, CUPSD_JOB_DEFAULT,
3592 "Aborting job because it has no files.");
3593 return;
3594 }
bd7854cb 3595
b9faaae1
MS
3596 /*
3597 * Update the printer and job state to "processing"...
3598 */
bd7854cb 3599
b9faaae1
MS
3600 if (!cupsdLoadJob(job))
3601 return;
89d46774 3602
b9faaae1
MS
3603 cupsdSetJobState(job, IPP_JOB_PROCESSING, CUPSD_JOB_DEFAULT, NULL);
3604 cupsdSetPrinterState(printer, IPP_PRINTER_PROCESSING, 0);
f7deaa1a 3605
1340db2d
MS
3606 job->cost = 0;
3607 job->current_file = 0;
3608 job->progress = 0;
3609 job->printer = printer;
3610 printer->job = job;
f7deaa1a 3611
b9faaae1
MS
3612 /*
3613 * Setup the last exit status and security profiles...
3614 */
89d46774 3615
b9faaae1
MS
3616 job->status = 0;
3617 job->profile = cupsdCreateProfile(job->id);
bd7854cb 3618
b9faaae1
MS
3619 /*
3620 * Create the status pipes and buffer...
3621 */
bd7854cb 3622
b9faaae1
MS
3623 if (cupsdOpenPipe(job->status_pipes))
3624 {
3625 cupsdLogJob(job, CUPSD_LOG_DEBUG,
3626 "Unable to create job status pipes - %s.", strerror(errno));
89d46774 3627
b9faaae1
MS
3628 cupsdSetJobState(job, IPP_JOB_STOPPED, CUPSD_JOB_DEFAULT,
3629 "Job stopped because the scheduler could not create the "
3630 "job status pipes.");
89d46774 3631
b9faaae1
MS
3632 cupsdDestroyProfile(job->profile);
3633 job->profile = NULL;
3634 return;
e1d6a774 3635 }
bd7854cb 3636
b9faaae1
MS
3637 job->status_buffer = cupsdStatBufNew(job->status_pipes[0], NULL);
3638 job->status_level = CUPSD_LOG_INFO;
3639
38e73f87
MS
3640 if (job->printer_message)
3641 cupsdSetString(&(job->printer_message->values[0].string.text), "");
3642
b9faaae1
MS
3643 /*
3644 * Create the backchannel pipes and make them non-blocking...
3645 */
bd7854cb 3646
b9faaae1 3647 if (cupsdOpenPipe(job->back_pipes))
e1d6a774 3648 {
b9faaae1
MS
3649 cupsdLogJob(job, CUPSD_LOG_DEBUG,
3650 "Unable to create back-channel pipes - %s.", strerror(errno));
3651
3652 cupsdSetJobState(job, IPP_JOB_STOPPED, CUPSD_JOB_DEFAULT,
3653 "Job stopped because the scheduler could not create the "
3654 "back-channel pipes.");
3655
3656 cupsdClosePipe(job->status_pipes);
3657 cupsdStatBufDelete(job->status_buffer);
3658 job->status_buffer = NULL;
3659
3660 cupsdDestroyProfile(job->profile);
3661 job->profile = NULL;
3662 return;
e1d6a774 3663 }
bd7854cb 3664
b9faaae1
MS
3665 fcntl(job->back_pipes[0], F_SETFL,
3666 fcntl(job->back_pipes[0], F_GETFL) | O_NONBLOCK);
3667 fcntl(job->back_pipes[1], F_SETFL,
3668 fcntl(job->back_pipes[1], F_GETFL) | O_NONBLOCK);
ef416fc2 3669
b9faaae1
MS
3670 /*
3671 * Create the side-channel pipes and make them non-blocking...
3672 */
ef416fc2 3673
b9faaae1
MS
3674 if (socketpair(AF_LOCAL, SOCK_STREAM, 0, job->side_pipes))
3675 {
3676 cupsdLogJob(job, CUPSD_LOG_DEBUG,
3677 "Unable to create side-channel pipes - %s.", strerror(errno));
ef416fc2 3678
b9faaae1
MS
3679 cupsdSetJobState(job, IPP_JOB_STOPPED, CUPSD_JOB_DEFAULT,
3680 "Job stopped because the scheduler could not create the "
3681 "side-channel pipes.");
ef416fc2 3682
b9faaae1
MS
3683 cupsdClosePipe(job->back_pipes);
3684
3685 cupsdClosePipe(job->status_pipes);
3686 cupsdStatBufDelete(job->status_buffer);
3687 job->status_buffer = NULL;
3688
3689 cupsdDestroyProfile(job->profile);
3690 job->profile = NULL;
3691 return;
3692 }
3693
3694 fcntl(job->side_pipes[0], F_SETFL,
3695 fcntl(job->side_pipes[0], F_GETFL) | O_NONBLOCK);
3696 fcntl(job->side_pipes[1], F_SETFL,
3697 fcntl(job->side_pipes[1], F_GETFL) | O_NONBLOCK);
ef416fc2 3698
3699 /*
b9faaae1 3700 * Now start the first file in the job...
ef416fc2 3701 */
3702
b9faaae1
MS
3703 cupsdContinueJob(job);
3704}
ef416fc2 3705
ef416fc2 3706
b9faaae1
MS
3707/*
3708 * 'stop_job()' - Stop a print job.
3709 */
ef416fc2 3710
b9faaae1
MS
3711static void
3712stop_job(cupsd_job_t *job, /* I - Job */
3713 cupsd_jobaction_t action) /* I - Action */
3714{
3715 int i; /* Looping var */
b94498cf 3716
ef416fc2 3717
b9faaae1
MS
3718 cupsdLogMessage(CUPSD_LOG_DEBUG2, "stop_job(job=%p(%d), action=%d)", job,
3719 job->id, action);
ef416fc2 3720
b9faaae1
MS
3721 FilterLevel -= job->cost;
3722 job->cost = 0;
ef416fc2 3723
238c3832
MS
3724 if (action == CUPSD_JOB_DEFAULT && !job->kill_time)
3725 job->kill_time = time(NULL) + JobKillDelay;
3726 else if (action == CUPSD_JOB_FORCE)
3727 job->kill_time = 0;
3728
b9faaae1
MS
3729 for (i = 0; job->filters[i]; i ++)
3730 if (job->filters[i] > 0)
3731 cupsdEndProcess(job->filters[i], action == CUPSD_JOB_FORCE);
3732
3733 if (job->backend > 0)
3734 cupsdEndProcess(job->backend, action == CUPSD_JOB_FORCE);
e1d6a774 3735}
ef416fc2 3736
e1d6a774 3737
3738/*
3739 * 'unload_job()' - Unload a job from memory.
3740 */
3741
3742static void
3743unload_job(cupsd_job_t *job) /* I - Job */
3744{
3745 if (!job->attrs)
3746 return;
3747
ed6e7faf 3748 cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] Unloading...", job->id);
e1d6a774 3749
3750 ippDelete(job->attrs);
3751
cc0d019f
MS
3752 job->attrs = NULL;
3753 job->state = NULL;
3754 job->sheets = NULL;
3755 job->job_sheets = NULL;
3756 job->printer_message = NULL;
3757 job->printer_reasons = NULL;
ef416fc2 3758}
3759
3760
3761/*
f899b121 3762 * 'update_job()' - Read a status update from a job's filters.
3763 */
3764
3765void
09a101d6 3766update_job(cupsd_job_t *job) /* I - Job to check */
f899b121 3767{
3768 int i; /* Looping var */
3769 int copies; /* Number of copies printed */
3770 char message[1024], /* Message text */
3771 *ptr; /* Pointer update... */
3772 int loglevel, /* Log level for message */
3773 event = 0; /* Events? */
f0ab5bff
MS
3774 static const char * const levels[] = /* Log levels */
3775 {
3776 "NONE",
3777 "EMERG",
3778 "ALERT",
3779 "CRIT",
3780 "ERROR",
3781 "WARN",
3782 "NOTICE",
3783 "INFO",
3784 "DEBUG",
3785 "DEBUG2"
3786 };
f899b121 3787
3788
f0ab5bff
MS
3789 /*
3790 * Get the printer associated with this job; if the printer is stopped for
3791 * any reason then job->printer will be reset to NULL, so make sure we have
3792 * a valid pointer...
3793 */
3794
f899b121 3795 while ((ptr = cupsdStatBufUpdate(job->status_buffer, &loglevel,
3796 message, sizeof(message))) != NULL)
3797 {
3798 /*
3799 * Process page and printer state messages as needed...
3800 */
3801
3802 if (loglevel == CUPSD_LOG_PAGE)
3803 {
3804 /*
3805 * Page message; send the message to the page_log file and update the
3806 * job sheet count...
3807 */
3808
75bd9771 3809 cupsdLogJob(job, CUPSD_LOG_DEBUG, "PAGE: %s", message);
dd1abb6b 3810
91c84a35 3811 if (job->sheets)
f899b121 3812 {
3813 if (!strncasecmp(message, "total ", 6))
3814 {
3815 /*
3816 * Got a total count of pages from a backend or filter...
3817 */
3818
3819 copies = atoi(message + 6);
3820 copies -= job->sheets->values[0].integer; /* Just track the delta */
3821 }
3822 else if (!sscanf(message, "%*d%d", &copies))
3823 copies = 1;
3824
3825 job->sheets->values[0].integer += copies;
3826
b9faaae1 3827 if (job->printer->page_limit)
f899b121 3828 {
b9faaae1 3829 cupsd_quota_t *q = cupsdUpdateQuota(job->printer, job->username,
f899b121 3830 copies, 0);
3831
3832#ifdef __APPLE__
3833 if (AppleQuotas && q->page_count == -3)
3834 {
3835 /*
3836 * Quota limit exceeded, cancel job in progress immediately...
3837 */
3838
b9faaae1
MS
3839 cupsdSetJobState(job, IPP_JOB_CANCELED, CUPSD_JOB_DEFAULT,
3840 "Canceled job because pages exceed user %s "
3841 "quota limit on printer %s (%s).",
3842 job->username, job->printer->name,
3843 job->printer->info);
f899b121 3844 return;
3845 }
3846#else
3847 (void)q;
3848#endif /* __APPLE__ */
3849 }
3850 }
3851
3852 cupsdLogPage(job, message);
3853
91c84a35 3854 if (job->sheets)
b9faaae1 3855 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS, job->printer, job,
91c84a35 3856 "Printed %d page(s).", job->sheets->values[0].integer);
f899b121 3857 }
3858 else if (loglevel == CUPSD_LOG_STATE)
3859 {
75bd9771 3860 cupsdLogJob(job, CUPSD_LOG_DEBUG, "STATE: %s", message);
dd1abb6b 3861
09a101d6 3862 if (!strcmp(message, "paused"))
c24d2134 3863 {
b9faaae1 3864 cupsdStopPrinter(job->printer, 1);
c24d2134
MS
3865 return;
3866 }
09a101d6 3867 else
3868 {
b9faaae1
MS
3869 cupsdSetPrinterReasons(job->printer, message);
3870 cupsdAddPrinterHistory(job->printer);
d9bca400 3871 event |= CUPSD_EVENT_PRINTER_STATE;
09a101d6 3872 }
bc44d920 3873
4509bb49 3874 update_job_attrs(job, 0);
f899b121 3875 }
3876 else if (loglevel == CUPSD_LOG_ATTR)
3877 {
3878 /*
3879 * Set attribute(s)...
3880 */
3881
3882 int num_attrs; /* Number of attributes */
3883 cups_option_t *attrs; /* Attributes */
3884 const char *attr; /* Attribute */
3885
3886
75bd9771 3887 cupsdLogJob(job, CUPSD_LOG_DEBUG, "ATTR: %s", message);
dd1abb6b 3888
f899b121 3889 num_attrs = cupsParseOptions(message, 0, &attrs);
3890
3891 if ((attr = cupsGetOption("auth-info-required", num_attrs,
3892 attrs)) != NULL)
7ff4fea9 3893 {
b9faaae1
MS
3894 cupsdSetAuthInfoRequired(job->printer, attr, NULL);
3895 cupsdSetPrinterAttrs(job->printer);
3dfe78b3 3896
b9faaae1 3897 if (job->printer->type & CUPS_PRINTER_DISCOVERED)
3dfe78b3
MS
3898 cupsdMarkDirty(CUPSD_DIRTY_REMOTE);
3899 else
3900 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
7ff4fea9 3901 }
f899b121 3902
9a4f8274
MS
3903 if ((attr = cupsGetOption("job-media-progress", num_attrs,
3904 attrs)) != NULL)
3905 {
3906 int progress = atoi(attr);
3907
3908
3909 if (progress >= 0 && progress <= 100)
3910 {
3911 job->progress = progress;
3912
3913 if (job->sheets)
b9faaae1 3914 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS, job->printer, job,
9a4f8274
MS
3915 "Printing page %d, %d%%",
3916 job->sheets->values[0].integer, job->progress);
3917 }
3918 }
3919
323c5de1 3920 if ((attr = cupsGetOption("printer-alert", num_attrs, attrs)) != NULL)
3921 {
b9faaae1 3922 cupsdSetString(&job->printer->alert, attr);
d9bca400 3923 event |= CUPSD_EVENT_PRINTER_STATE;
323c5de1 3924 }
3925
3926 if ((attr = cupsGetOption("printer-alert-description", num_attrs,
3927 attrs)) != NULL)
3928 {
b9faaae1 3929 cupsdSetString(&job->printer->alert_description, attr);
d9bca400 3930 event |= CUPSD_EVENT_PRINTER_STATE;
323c5de1 3931 }
3932
5a738aea
MS
3933 if ((attr = cupsGetOption("marker-colors", num_attrs, attrs)) != NULL)
3934 {
b9faaae1
MS
3935 cupsdSetPrinterAttr(job->printer, "marker-colors", (char *)attr);
3936 job->printer->marker_time = time(NULL);
5a738aea 3937 event |= CUPSD_EVENT_PRINTER_STATE;
52f6f666 3938 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
5a738aea
MS
3939 }
3940
3941 if ((attr = cupsGetOption("marker-levels", num_attrs, attrs)) != NULL)
3942 {
b9faaae1
MS
3943 cupsdSetPrinterAttr(job->printer, "marker-levels", (char *)attr);
3944 job->printer->marker_time = time(NULL);
5a738aea 3945 event |= CUPSD_EVENT_PRINTER_STATE;
52f6f666 3946 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
5a738aea
MS
3947 }
3948
ed6e7faf
MS
3949 if ((attr = cupsGetOption("marker-low-levels", num_attrs, attrs)) != NULL)
3950 {
b9faaae1
MS
3951 cupsdSetPrinterAttr(job->printer, "marker-low-levels", (char *)attr);
3952 job->printer->marker_time = time(NULL);
ed6e7faf
MS
3953 event |= CUPSD_EVENT_PRINTER_STATE;
3954 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
3955 }
3956
3957 if ((attr = cupsGetOption("marker-high-levels", num_attrs, attrs)) != NULL)
3958 {
b9faaae1
MS
3959 cupsdSetPrinterAttr(job->printer, "marker-high-levels", (char *)attr);
3960 job->printer->marker_time = time(NULL);
ed6e7faf
MS
3961 event |= CUPSD_EVENT_PRINTER_STATE;
3962 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
3963 }
3964
75bd9771
MS
3965 if ((attr = cupsGetOption("marker-message", num_attrs, attrs)) != NULL)
3966 {
b9faaae1
MS
3967 cupsdSetPrinterAttr(job->printer, "marker-message", (char *)attr);
3968 job->printer->marker_time = time(NULL);
75bd9771 3969 event |= CUPSD_EVENT_PRINTER_STATE;
52f6f666 3970 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
75bd9771
MS
3971 }
3972
5a738aea
MS
3973 if ((attr = cupsGetOption("marker-names", num_attrs, attrs)) != NULL)
3974 {
b9faaae1
MS
3975 cupsdSetPrinterAttr(job->printer, "marker-names", (char *)attr);
3976 job->printer->marker_time = time(NULL);
5a738aea 3977 event |= CUPSD_EVENT_PRINTER_STATE;
52f6f666 3978 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
5a738aea
MS
3979 }
3980
3981 if ((attr = cupsGetOption("marker-types", num_attrs, attrs)) != NULL)
3982 {
b9faaae1
MS
3983 cupsdSetPrinterAttr(job->printer, "marker-types", (char *)attr);
3984 job->printer->marker_time = time(NULL);
5a738aea 3985 event |= CUPSD_EVENT_PRINTER_STATE;
52f6f666 3986 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
5a738aea
MS
3987 }
3988
f899b121 3989 cupsFreeOptions(num_attrs, attrs);
3990 }
c9fc04c6
MS
3991 else if (loglevel == CUPSD_LOG_PPD)
3992 {
3993 /*
3994 * Set attribute(s)...
3995 */
3996
3997 int num_keywords; /* Number of keywords */
3998 cups_option_t *keywords; /* Keywords */
3999
4000
75bd9771 4001 cupsdLogJob(job, CUPSD_LOG_DEBUG, "PPD: %s", message);
dd1abb6b 4002
c9fc04c6
MS
4003 num_keywords = cupsParseOptions(message, 0, &keywords);
4004
b9faaae1
MS
4005 if (cupsdUpdatePrinterPPD(job->printer, num_keywords, keywords))
4006 cupsdSetPrinterAttrs(job->printer);
c9fc04c6
MS
4007
4008 cupsFreeOptions(num_keywords, keywords);
4009 }
f899b121 4010#ifdef __APPLE__
4011 else if (!strncmp(message, "recoverable:", 12))
4012 {
f899b121 4013 ptr = message + 12;
4014 while (isspace(*ptr & 255))
4015 ptr ++;
4016
d2354e63
MS
4017 if (*ptr)
4018 {
b9faaae1
MS
4019 cupsdSetPrinterReasons(job->printer,
4020 "+com.apple.print.recoverable-warning");
4021 cupsdSetString(&(job->printer->recoverable), ptr);
4022 cupsdAddPrinterHistory(job->printer);
d2354e63
MS
4023 event |= CUPSD_EVENT_PRINTER_STATE;
4024 }
f899b121 4025 }
4026 else if (!strncmp(message, "recovered:", 10))
4027 {
b9faaae1
MS
4028 cupsdSetPrinterReasons(job->printer,
4029 "-com.apple.print.recoverable-warning");
f899b121 4030
4031 ptr = message + 10;
4032 while (isspace(*ptr & 255))
4033 ptr ++;
4034
b9faaae1
MS
4035 cupsdSetString(&(job->printer->recoverable), ptr);
4036 cupsdAddPrinterHistory(job->printer);
d9bca400 4037 event |= CUPSD_EVENT_PRINTER_STATE;
f899b121 4038 }
4039#endif /* __APPLE__ */
75bd9771 4040 else
f899b121 4041 {
94da7e34 4042 cupsdLogJob(job, loglevel, "%s", message);
f899b121 4043
1f0275e3
MS
4044 if (loglevel < CUPSD_LOG_DEBUG)
4045 {
b9faaae1
MS
4046 strlcpy(job->printer->state_message, message,
4047 sizeof(job->printer->state_message));
4048 cupsdAddPrinterHistory(job->printer);
4509bb49 4049
8b116e60 4050 event |= CUPSD_EVENT_PRINTER_STATE | CUPSD_EVENT_JOB_PROGRESS;
4509bb49 4051
94da7e34 4052 if (loglevel < job->status_level)
1f0275e3
MS
4053 {
4054 /*
b9faaae1 4055 * Some messages show in the job-printer-state-message attribute...
1f0275e3 4056 */
09a101d6 4057
1f0275e3
MS
4058 if (loglevel != CUPSD_LOG_NOTICE)
4059 job->status_level = loglevel;
01ce6322 4060
1f0275e3 4061 update_job_attrs(job, 1);
f0ab5bff
MS
4062
4063 cupsdLogJob(job, CUPSD_LOG_DEBUG,
4064 "Set job-printer-state-message to \"%s\", "
4065 "current level=%s",
4066 job->printer_message->values[0].string.text,
4067 levels[job->status_level]);
1f0275e3 4068 }
75bd9771 4069 }
f899b121 4070 }
4071
4072 if (!strchr(job->status_buffer->buffer, '\n'))
4073 break;
4074 }
4075
d9bca400 4076 if (event & CUPSD_EVENT_PRINTER_STATE)
b9faaae1
MS
4077 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, job->printer, NULL,
4078 (job->printer->type & CUPS_PRINTER_CLASS) ?
f899b121 4079 "Class \"%s\" state changed." :
4080 "Printer \"%s\" state changed.",
b9faaae1 4081 job->printer->name);
f899b121 4082
8b116e60
MS
4083 if (event & CUPSD_EVENT_JOB_PROGRESS)
4084 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS, job->printer, job,
4085 "%s", job->printer->state_message);
4086
f899b121 4087 if (ptr == NULL && !job->status_buffer->bufused)
4088 {
4089 /*
4090 * See if all of the filters and the backend have returned their
4091 * exit statuses.
4092 */
4093
4094 for (i = 0; job->filters[i] < 0; i ++);
4095
4096 if (job->filters[i])
d7871c8c
MS
4097 {
4098 /*
4099 * EOF but we haven't collected the exit status of all filters...
4100 */
4101
4102 cupsdCheckProcess();
f899b121 4103 return;
d7871c8c 4104 }
f899b121 4105
4106 if (job->current_file >= job->num_files && job->backend > 0)
d7871c8c
MS
4107 {
4108 /*
4109 * EOF but we haven't collected the exit status of the backend...
4110 */
4111
4112 cupsdCheckProcess();
f899b121 4113 return;
d7871c8c 4114 }
f899b121 4115
4116 /*
4117 * Handle the end of job stuff...
4118 */
4119
b9faaae1
MS
4120 finalize_job(job);
4121
4122 /*
4123 * Check for new jobs...
4124 */
4125
4126 cupsdCheckJobs();
f899b121 4127 }
4128}
4129
4130
4131/*
bc44d920 4132 * 'update_job_attrs()' - Update the job-printer-* attributes.
4133 */
4134
4135void
4509bb49 4136update_job_attrs(cupsd_job_t *job, /* I - Job to update */
b9faaae1 4137 int do_message)/* I - 1 = copy job-printer-state message */
bc44d920 4138{
4139 int i; /* Looping var */
4140 int num_reasons; /* Actual number of reasons */
4141 const char * const *reasons; /* Reasons */
f0ab5bff 4142 static const char *none = "none"; /* "none" reason */
bc44d920 4143
4144
4145 /*
4146 * Get/create the job-printer-state-* attributes...
4147 */
4148
4149 if (!job->printer_message)
4150 {
4151 if ((job->printer_message = ippFindAttribute(job->attrs,
4152 "job-printer-state-message",
4153 IPP_TAG_TEXT)) == NULL)
4154 job->printer_message = ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_TEXT,
4155 "job-printer-state-message",
4156 NULL, "");
4157 }
4158
4159 if (!job->printer_reasons)
4160 job->printer_reasons = ippFindAttribute(job->attrs,
4161 "job-printer-state-reasons",
4162 IPP_TAG_KEYWORD);
4163
bc44d920 4164 /*
b9faaae1 4165 * Copy or clear the printer-state-message value as needed...
bc44d920 4166 */
4167
b9faaae1
MS
4168 if (job->state_value != IPP_JOB_PROCESSING &&
4169 job->status_level == CUPSD_LOG_INFO)
4170 cupsdSetString(&(job->printer_message->values[0].string.text), "");
4171 else if (job->printer->state_message[0] && do_message)
bc44d920 4172 cupsdSetString(&(job->printer_message->values[0].string.text),
b9faaae1
MS
4173 job->printer->state_message);
4174
bc44d920 4175 /*
4176 * ... and the printer-state-reasons value...
4177 */
4178
b9faaae1 4179 if (job->printer->num_reasons == 0)
bc44d920 4180 {
4181 num_reasons = 1;
f0ab5bff 4182 reasons = &none;
bc44d920 4183 }
4184 else
4185 {
b9faaae1
MS
4186 num_reasons = job->printer->num_reasons;
4187 reasons = (const char * const *)job->printer->reasons;
bc44d920 4188 }
4189
4190 if (!job->printer_reasons || job->printer_reasons->num_values != num_reasons)
4191 {
b9faaae1
MS
4192 /*
4193 * Replace/create a job-printer-state-reasons attribute...
4194 */
4195
bc44d920 4196 ippDeleteAttribute(job->attrs, job->printer_reasons);
4197
4198 job->printer_reasons = ippAddStrings(job->attrs,
4199 IPP_TAG_JOB, IPP_TAG_KEYWORD,
4200 "job-printer-state-reasons",
4201 num_reasons, NULL, NULL);
4202 }
b9faaae1 4203 else
d2354e63 4204 {
b9faaae1
MS
4205 /*
4206 * Don't bother clearing the reason strings if they are the same...
4207 */
4208
4209 for (i = 0; i < num_reasons; i ++)
4210 if (strcmp(job->printer_reasons->values[i].string.text, reasons[i]))
4211 break;
4212
4213 if (i >= num_reasons)
4214 return;
4215
4216 /*
4217 * Not the same, so free the current strings...
4218 */
4219
4220 for (i = 0; i < num_reasons; i ++)
d2354e63
MS
4221 _cupsStrFree(job->printer_reasons->values[i].string.text);
4222 }
bc44d920 4223
b9faaae1
MS
4224 /*
4225 * Copy the reasons...
4226 */
4227
bc44d920 4228 for (i = 0; i < num_reasons; i ++)
d2354e63 4229 job->printer_reasons->values[i].string.text = _cupsStrAlloc(reasons[i]);
bc44d920 4230}
4231
4232
4233/*
b19ccc9e 4234 * End of "$Id: job.c 7902 2008-09-03 14:20:17Z mike $".
ef416fc2 4235 */