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