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