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