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