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