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