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