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