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