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