]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
bc44d920 | 2 | * "$Id: job.c 6671 2007-07-13 23:35:24Z mike $" |
ef416fc2 | 3 | * |
4 | * Job management routines for the Common UNIX Printing System (CUPS). | |
5 | * | |
bc44d920 | 6 | * Copyright 2007 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 | * | |
17 | * cupsdAddJob() - Add a new job to the job queue... | |
18 | * cupsdCancelJob() - Cancel the specified print job. | |
bd7854cb | 19 | * cupsdCancelJobs() - Cancel all jobs for the given |
20 | * destination/user... | |
21 | * cupsdCheckJobs() - Check the pending jobs and start any if | |
22 | * the destination is available. | |
ef416fc2 | 23 | * cupsdCleanJobs() - Clean out old jobs. |
bd7854cb | 24 | * cupsdFinishJob() - Finish a job. |
ef416fc2 | 25 | * cupsdFreeAllJobs() - Free all jobs from memory. |
26 | * cupsdFindJob() - Find the specified job. | |
27 | * cupsdGetPrinterJobCount() - Get the number of pending, processing, | |
ef416fc2 | 28 | * cupsdGetUserJobCount() - Get the number of pending, processing, |
ef416fc2 | 29 | * cupsdHoldJob() - Hold the specified job. |
30 | * cupsdLoadAllJobs() - Load all jobs from disk. | |
bd7854cb | 31 | * cupsdLoadJob() - Load a single job... |
ef416fc2 | 32 | * cupsdMoveJob() - Move the specified job to a different |
33 | * destination. | |
34 | * cupsdReleaseJob() - Release the specified job. | |
35 | * cupsdRestartJob() - Restart the specified job. | |
bd7854cb | 36 | * cupsdSaveAllJobs() - Save a summary of all jobs to disk. |
ef416fc2 | 37 | * cupsdSaveJob() - Save a job to disk. |
38 | * cupsdSetJobHoldUntil() - Set the hold time for a job... | |
bd7854cb | 39 | * cupsdSetJobPriority() - Set the priority of a job, moving it |
40 | * up/down in the list as needed. | |
ef416fc2 | 41 | * cupsdStopAllJobs() - Stop all print jobs. |
42 | * cupsdStopJob() - Stop a print job. | |
bd7854cb | 43 | * cupsdUnloadCompletedJobs() - Flush completed job history from memory. |
bd7854cb | 44 | * compare_active_jobs() - Compare the job IDs and priorities of two |
45 | * jobs. | |
ef416fc2 | 46 | * compare_jobs() - Compare the job IDs of two jobs. |
bd7854cb | 47 | * free_job() - Free all memory used by a job. |
48 | * ipp_length() - Compute the size of the buffer needed to | |
49 | * hold the textual IPP attributes. | |
50 | * load_job_cache() - Load jobs from the job.cache file. | |
51 | * load_next_job_id() - Load the NextJobId value from the | |
52 | * job.cache file. | |
53 | * load_request_root() - Load jobs from the RequestRoot directory. | |
54 | * set_time() - Set one of the "time-at-xyz" attributes... | |
55 | * set_hold_until() - Set the hold time and update job-hold-until | |
56 | * attribute... | |
e1d6a774 | 57 | * start_job() - Start a print job. |
58 | * unload_job() - Unload a job from memory. | |
f899b121 | 59 | * update_job() - Read a status update from a jobs filters. |
bc44d920 | 60 | * update_job_attrs() - Update the job-printer-* attributes. |
ef416fc2 | 61 | */ |
62 | ||
63 | /* | |
64 | * Include necessary headers... | |
65 | */ | |
66 | ||
67 | #include "cupsd.h" | |
68 | #include <grp.h> | |
69 | #include <cups/backend.h> | |
70 | #include <cups/dir.h> | |
71 | ||
72 | ||
73 | /* | |
74 | * Local globals... | |
75 | */ | |
76 | ||
77 | static mime_filter_t gziptoany_filter = | |
78 | { | |
79 | NULL, /* Source type */ | |
80 | NULL, /* Destination type */ | |
81 | 0, /* Cost */ | |
82 | "gziptoany" /* Filter program to run */ | |
83 | }; | |
84 | ||
85 | ||
86 | /* | |
87 | * Local functions... | |
88 | */ | |
89 | ||
90 | static int compare_active_jobs(void *first, void *second, void *data); | |
91 | static int compare_jobs(void *first, void *second, void *data); | |
bd7854cb | 92 | static void free_job(cupsd_job_t *job); |
ef416fc2 | 93 | static int ipp_length(ipp_t *ipp); |
bd7854cb | 94 | static void load_job_cache(const char *filename); |
95 | static void load_next_job_id(const char *filename); | |
96 | static void load_request_root(void); | |
ef416fc2 | 97 | static void set_time(cupsd_job_t *job, const char *name); |
98 | static void set_hold_until(cupsd_job_t *job, time_t holdtime); | |
e1d6a774 | 99 | static void start_job(cupsd_job_t *job, cupsd_printer_t *printer); |
100 | static void unload_job(cupsd_job_t *job); | |
f899b121 | 101 | static void update_job(cupsd_job_t *job); |
bc44d920 | 102 | static void update_job_attrs(cupsd_job_t *job); |
ef416fc2 | 103 | |
104 | ||
105 | /* | |
106 | * 'cupsdAddJob()' - Add a new job to the job queue... | |
107 | */ | |
108 | ||
109 | cupsd_job_t * /* O - New job record */ | |
110 | cupsdAddJob(int priority, /* I - Job priority */ | |
111 | const char *dest) /* I - Job destination */ | |
112 | { | |
113 | cupsd_job_t *job; /* New job record */ | |
114 | ||
115 | ||
116 | job = calloc(sizeof(cupsd_job_t), 1); | |
117 | ||
89d46774 | 118 | job->id = NextJobId ++; |
119 | job->priority = priority; | |
120 | job->back_pipes[0] = -1; | |
121 | job->back_pipes[1] = -1; | |
122 | job->print_pipes[0] = -1; | |
123 | job->print_pipes[1] = -1; | |
f7deaa1a | 124 | job->side_pipes[0] = -1; |
125 | job->side_pipes[1] = -1; | |
89d46774 | 126 | job->status_pipes[0] = -1; |
127 | job->status_pipes[1] = -1; | |
ef416fc2 | 128 | |
129 | cupsdSetString(&job->dest, dest); | |
130 | ||
131 | /* | |
132 | * Add the new job to the "all jobs" and "active jobs" lists... | |
133 | */ | |
134 | ||
135 | cupsArrayAdd(Jobs, job); | |
136 | cupsArrayAdd(ActiveJobs, job); | |
137 | ||
138 | return (job); | |
139 | } | |
140 | ||
141 | ||
142 | /* | |
143 | * 'cupsdCancelJob()' - Cancel the specified print job. | |
144 | */ | |
145 | ||
146 | void | |
07725fee | 147 | cupsdCancelJob(cupsd_job_t *job, /* I - Job to cancel */ |
148 | int purge, /* I - Purge jobs? */ | |
149 | ipp_jstate_t newstate) /* I - New job state */ | |
ef416fc2 | 150 | { |
151 | int i; /* Looping var */ | |
152 | char filename[1024]; /* Job filename */ | |
07725fee | 153 | cupsd_printer_t *printer; /* Printer used by job */ |
ef416fc2 | 154 | |
155 | ||
bd7854cb | 156 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdCancelJob: id = %d", job->id); |
ef416fc2 | 157 | |
158 | /* | |
159 | * Stop any processes that are working on the current job... | |
160 | */ | |
161 | ||
07725fee | 162 | printer = job->printer; |
163 | ||
bd7854cb | 164 | if (job->state_value == IPP_JOB_PROCESSING) |
ef416fc2 | 165 | cupsdStopJob(job, 0); |
166 | ||
bd7854cb | 167 | cupsdLoadJob(job); |
168 | ||
169 | if (job->attrs) | |
07725fee | 170 | job->state->values[0].integer = newstate; |
ef416fc2 | 171 | |
07725fee | 172 | job->state_value = newstate; |
ef416fc2 | 173 | |
174 | set_time(job, "time-at-completed"); | |
175 | ||
07725fee | 176 | /* |
177 | * Send any pending notifications and then expire them... | |
178 | */ | |
179 | ||
180 | switch (newstate) | |
181 | { | |
182 | default : | |
183 | break; | |
184 | ||
185 | case IPP_JOB_CANCELED : | |
186 | cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, printer, job, | |
187 | purge ? "Job purged." : "Job canceled."); | |
188 | break; | |
189 | ||
190 | case IPP_JOB_ABORTED : | |
191 | cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, printer, job, | |
192 | "Job aborted; please consult the error_log file " | |
193 | "for details."); | |
194 | break; | |
195 | ||
196 | case IPP_JOB_COMPLETED : | |
197 | /* | |
c0e1af83 | 198 | * Clear the printer's printer-state-message and move on... |
07725fee | 199 | */ |
200 | ||
201 | printer->state_message[0] = '\0'; | |
202 | ||
07725fee | 203 | cupsdSetPrinterState(printer, IPP_PRINTER_IDLE, 0); |
204 | ||
205 | cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, printer, job, | |
206 | "Job completed."); | |
207 | break; | |
208 | } | |
209 | ||
ef416fc2 | 210 | cupsdExpireSubscriptions(NULL, job); |
211 | ||
bd7854cb | 212 | /* |
213 | * Remove the job from the active list... | |
214 | */ | |
215 | ||
216 | cupsArrayRemove(ActiveJobs, job); | |
217 | ||
ef416fc2 | 218 | /* |
219 | * Remove any authentication data... | |
220 | */ | |
221 | ||
bd7854cb | 222 | snprintf(filename, sizeof(filename), "%s/a%05d", RequestRoot, job->id); |
ef416fc2 | 223 | unlink(filename); |
224 | ||
09a101d6 | 225 | cupsdClearString(&job->auth_username); |
226 | cupsdClearString(&job->auth_domain); | |
227 | cupsdClearString(&job->auth_password); | |
228 | ||
ef416fc2 | 229 | /* |
230 | * Remove the print file for good if we aren't preserving jobs or | |
231 | * files... | |
232 | */ | |
233 | ||
234 | job->current_file = 0; | |
235 | ||
bd7854cb | 236 | if (!JobHistory || !JobFiles || purge || (job->dtype & CUPS_PRINTER_REMOTE)) |
237 | { | |
ef416fc2 | 238 | for (i = 1; i <= job->num_files; i ++) |
239 | { | |
240 | snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, | |
241 | job->id, i); | |
242 | unlink(filename); | |
243 | } | |
244 | ||
bd7854cb | 245 | if (job->num_files > 0) |
246 | { | |
247 | free(job->filetypes); | |
248 | free(job->compressions); | |
249 | ||
250 | job->num_files = 0; | |
251 | job->filetypes = NULL; | |
252 | job->compressions = NULL; | |
253 | } | |
254 | } | |
255 | ||
ef416fc2 | 256 | if (JobHistory && !purge && !(job->dtype & CUPS_PRINTER_REMOTE)) |
257 | { | |
258 | /* | |
259 | * Save job state info... | |
260 | */ | |
261 | ||
262 | cupsdSaveJob(job); | |
263 | } | |
264 | else | |
265 | { | |
266 | /* | |
267 | * Remove the job info file... | |
268 | */ | |
269 | ||
270 | snprintf(filename, sizeof(filename), "%s/c%05d", RequestRoot, | |
271 | job->id); | |
272 | unlink(filename); | |
273 | ||
274 | /* | |
275 | * Remove the job from the "all jobs" list... | |
276 | */ | |
277 | ||
278 | cupsArrayRemove(Jobs, job); | |
279 | ||
280 | /* | |
281 | * Free all memory used... | |
282 | */ | |
283 | ||
bd7854cb | 284 | free_job(job); |
ef416fc2 | 285 | } |
286 | } | |
287 | ||
288 | ||
289 | /* | |
290 | * 'cupsdCancelJobs()' - Cancel all jobs for the given destination/user... | |
291 | */ | |
292 | ||
293 | void | |
294 | cupsdCancelJobs(const char *dest, /* I - Destination to cancel */ | |
295 | const char *username, /* I - Username or NULL */ | |
296 | int purge) /* I - Purge jobs? */ | |
297 | { | |
298 | cupsd_job_t *job; /* Current job */ | |
299 | ||
300 | ||
301 | for (job = (cupsd_job_t *)cupsArrayFirst(Jobs); | |
302 | job; | |
303 | job = (cupsd_job_t *)cupsArrayNext(Jobs)) | |
304 | if ((dest == NULL || !strcmp(job->dest, dest)) && | |
305 | (username == NULL || !strcmp(job->username, username))) | |
306 | { | |
307 | /* | |
308 | * Cancel all jobs matching this destination/user... | |
309 | */ | |
310 | ||
07725fee | 311 | cupsdCancelJob(job, purge, IPP_JOB_CANCELED); |
ef416fc2 | 312 | } |
313 | ||
314 | cupsdCheckJobs(); | |
315 | } | |
316 | ||
317 | ||
318 | /* | |
319 | * 'cupsdCheckJobs()' - Check the pending jobs and start any if the destination | |
320 | * is available. | |
321 | */ | |
322 | ||
323 | void | |
324 | cupsdCheckJobs(void) | |
325 | { | |
326 | cupsd_job_t *job; /* Current job in queue */ | |
327 | cupsd_printer_t *printer, /* Printer destination */ | |
328 | *pclass; /* Printer class destination */ | |
329 | ||
330 | ||
331 | DEBUG_puts("cupsdCheckJobs()"); | |
332 | ||
bd7854cb | 333 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
334 | "cupsdCheckJobs: %d active jobs, sleeping=%d, reload=%d", | |
335 | cupsArrayCount(ActiveJobs), Sleeping, NeedReload); | |
336 | ||
ef416fc2 | 337 | for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs); |
338 | job; | |
339 | job = (cupsd_job_t *)cupsArrayNext(ActiveJobs)) | |
340 | { | |
341 | /* | |
342 | * Start held jobs if they are ready... | |
343 | */ | |
344 | ||
bd7854cb | 345 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
346 | "cupsdCheckJobs: Job %d: state_value=%d, loaded=%s", | |
347 | job->id, job->state_value, job->attrs ? "yes" : "no"); | |
348 | ||
349 | if (job->state_value == IPP_JOB_HELD && | |
ef416fc2 | 350 | job->hold_until && |
351 | job->hold_until < time(NULL)) | |
bd7854cb | 352 | { |
09a101d6 | 353 | if (job->pending_timeout) |
354 | cupsdTimeoutJob(job); /* Add trailing banner as needed */ | |
355 | ||
ef416fc2 | 356 | job->state->values[0].integer = IPP_JOB_PENDING; |
bd7854cb | 357 | job->state_value = IPP_JOB_PENDING; |
358 | } | |
ef416fc2 | 359 | |
360 | /* | |
361 | * Start pending jobs if the destination is available... | |
362 | */ | |
363 | ||
bd7854cb | 364 | if (job->state_value == IPP_JOB_PENDING && !NeedReload && !Sleeping) |
ef416fc2 | 365 | { |
366 | printer = cupsdFindDest(job->dest); | |
367 | pclass = NULL; | |
368 | ||
369 | while (printer && | |
370 | (printer->type & (CUPS_PRINTER_IMPLICIT | CUPS_PRINTER_CLASS))) | |
371 | { | |
372 | /* | |
373 | * If the class is remote, just pass it to the remote server... | |
374 | */ | |
375 | ||
376 | pclass = printer; | |
377 | ||
c0e1af83 | 378 | if (pclass->state == IPP_PRINTER_STOPPED) |
b86bc4cf | 379 | printer = NULL; |
c0e1af83 | 380 | else if (pclass->type & CUPS_PRINTER_REMOTE) |
381 | break; | |
382 | else | |
383 | printer = cupsdFindAvailablePrinter(printer->name); | |
ef416fc2 | 384 | } |
385 | ||
386 | if (!printer && !pclass) | |
387 | { | |
388 | /* | |
389 | * Whoa, the printer and/or class for this destination went away; | |
390 | * cancel the job... | |
391 | */ | |
392 | ||
393 | cupsdLogMessage(CUPSD_LOG_WARN, | |
09a101d6 | 394 | "[Job %d] Printer/class %s has gone away; canceling job!", |
395 | job->id, job->dest); | |
ef416fc2 | 396 | |
397 | cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job, | |
bd7854cb | 398 | "Job canceled because the destination printer/class has " |
399 | "gone away."); | |
ef416fc2 | 400 | |
07725fee | 401 | cupsdCancelJob(job, 1, IPP_JOB_ABORTED); |
ef416fc2 | 402 | } |
403 | else if (printer) | |
404 | { | |
405 | /* | |
406 | * See if the printer is available or remote and not printing a job; | |
407 | * if so, start the job... | |
408 | */ | |
409 | ||
410 | if (pclass) | |
411 | { | |
412 | /* | |
413 | * Add/update a job-actual-printer-uri attribute for this job | |
414 | * so that we know which printer actually printed the job... | |
415 | */ | |
416 | ||
417 | ipp_attribute_t *attr; /* job-actual-printer-uri attribute */ | |
418 | ||
419 | ||
420 | if ((attr = ippFindAttribute(job->attrs, "job-actual-printer-uri", | |
421 | IPP_TAG_URI)) != NULL) | |
422 | cupsdSetString(&attr->values[0].string.text, printer->uri); | |
423 | else | |
424 | ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_URI, | |
425 | "job-actual-printer-uri", NULL, printer->uri); | |
426 | } | |
427 | ||
09a101d6 | 428 | if ((!(printer->type & CUPS_PRINTER_DISCOVERED) && /* Printer is local */ |
e53920b9 | 429 | printer->state == IPP_PRINTER_IDLE) || /* and idle */ |
09a101d6 | 430 | ((printer->type & CUPS_PRINTER_DISCOVERED) && /* Printer is remote */ |
bd7854cb | 431 | !printer->job)) /* and not printing */ |
bc44d920 | 432 | { |
433 | /* | |
434 | * Clear any message and reasons for the queue... | |
435 | */ | |
436 | ||
437 | printer->state_message[0] = '\0'; | |
438 | ||
439 | cupsdSetPrinterReasons(printer, "none"); | |
440 | ||
441 | /* | |
442 | * Start the job... | |
443 | */ | |
444 | ||
e1d6a774 | 445 | start_job(job, printer); |
bc44d920 | 446 | } |
ef416fc2 | 447 | } |
448 | } | |
449 | } | |
450 | } | |
451 | ||
452 | ||
453 | /* | |
454 | * 'cupsdCleanJobs()' - Clean out old jobs. | |
455 | */ | |
456 | ||
457 | void | |
458 | cupsdCleanJobs(void) | |
459 | { | |
460 | cupsd_job_t *job; /* Current job */ | |
461 | ||
462 | ||
bd7854cb | 463 | if (MaxJobs <= 0) |
ef416fc2 | 464 | return; |
465 | ||
466 | for (job = (cupsd_job_t *)cupsArrayFirst(Jobs); | |
467 | job && cupsArrayCount(Jobs) >= MaxJobs; | |
468 | job = (cupsd_job_t *)cupsArrayNext(Jobs)) | |
07725fee | 469 | if (job->state_value >= IPP_JOB_CANCELED) |
470 | cupsdCancelJob(job, 1, IPP_JOB_CANCELED); | |
ef416fc2 | 471 | } |
472 | ||
473 | ||
474 | /* | |
475 | * 'cupsdFinishJob()' - Finish a job. | |
476 | */ | |
477 | ||
478 | void | |
479 | cupsdFinishJob(cupsd_job_t *job) /* I - Job */ | |
480 | { | |
ef416fc2 | 481 | cupsd_printer_t *printer; /* Current printer */ |
09a101d6 | 482 | ipp_attribute_t *attr; /* job-hold-until attribute */ |
ef416fc2 | 483 | |
484 | ||
bd7854cb | 485 | cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] File %d is complete.", |
ef416fc2 | 486 | job->id, job->current_file - 1); |
487 | ||
09a101d6 | 488 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
489 | "[Job %d] cupsdFinishJob: job->status is %d", | |
490 | job->id, job->status); | |
ef416fc2 | 491 | |
07725fee | 492 | if (job->status_buffer && |
493 | (job->status < 0 || job->current_file >= job->num_files)) | |
ef416fc2 | 494 | { |
495 | /* | |
496 | * Close the pipe and clear the input bit. | |
497 | */ | |
498 | ||
f7deaa1a | 499 | cupsdRemoveSelect(job->status_buffer->fd); |
ef416fc2 | 500 | |
501 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
09a101d6 | 502 | "[Job %d] cupsdFinishJob: Closing status pipes [ %d %d ]...", |
503 | job->id, job->status_pipes[0], job->status_pipes[1]); | |
ef416fc2 | 504 | |
89d46774 | 505 | cupsdClosePipe(job->status_pipes); |
ef416fc2 | 506 | cupsdStatBufDelete(job->status_buffer); |
507 | ||
508 | job->status_buffer = NULL; | |
509 | } | |
510 | ||
e00b005a | 511 | printer = job->printer; |
512 | ||
bc44d920 | 513 | update_job_attrs(job); |
514 | ||
ef416fc2 | 515 | if (job->status < 0) |
516 | { | |
517 | /* | |
518 | * Backend had errors; stop it... | |
519 | */ | |
520 | ||
a74454a7 | 521 | int exit_code; /* Exit code from backend */ |
522 | ||
523 | ||
524 | /* | |
525 | * Convert the status to an exit code. Due to the way the W* macros are | |
526 | * implemented on MacOS X (bug?), we have to store the exit status in a | |
527 | * variable first and then convert... | |
528 | */ | |
529 | ||
530 | exit_code = -job->status; | |
531 | if (WIFEXITED(exit_code)) | |
532 | exit_code = WEXITSTATUS(exit_code); | |
533 | else | |
534 | exit_code = job->status; | |
535 | ||
536 | cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Backend returned status %d (%s)", | |
537 | job->id, exit_code, | |
538 | exit_code == CUPS_BACKEND_FAILED ? "failed" : | |
539 | exit_code == CUPS_BACKEND_AUTH_REQUIRED ? | |
540 | "authentication required" : | |
541 | exit_code == CUPS_BACKEND_HOLD ? "hold job" : | |
542 | exit_code == CUPS_BACKEND_STOP ? "stop printer" : | |
543 | exit_code == CUPS_BACKEND_CANCEL ? "cancel job" : | |
544 | exit_code < 0 ? "crashed" : "unknown"); | |
545 | ||
546 | /* | |
547 | * Do what needs to be done... | |
548 | */ | |
549 | ||
550 | switch (exit_code) | |
ef416fc2 | 551 | { |
552 | default : | |
553 | case CUPS_BACKEND_FAILED : | |
554 | /* | |
555 | * Backend failure, use the error-policy to determine how to | |
556 | * act... | |
557 | */ | |
558 | ||
559 | cupsdStopJob(job, 0); | |
ed486911 | 560 | |
07725fee | 561 | if (job->dtype & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT)) |
ed486911 | 562 | { |
563 | /* | |
564 | * Mark the job as pending again - we'll retry on another | |
565 | * printer... | |
566 | */ | |
567 | ||
568 | job->state->values[0].integer = IPP_JOB_PENDING; | |
569 | job->state_value = IPP_JOB_PENDING; | |
570 | } | |
571 | ||
ef416fc2 | 572 | cupsdSaveJob(job); |
573 | ||
574 | /* | |
575 | * If the job was queued to a class, try requeuing it... For | |
576 | * faxes and retry-job queues, hold the current job for 5 minutes. | |
577 | */ | |
578 | ||
579 | if (job->dtype & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT)) | |
580 | cupsdCheckJobs(); | |
581 | else if ((printer->type & CUPS_PRINTER_FAX) || | |
582 | !strcmp(printer->error_policy, "retry-job")) | |
583 | { | |
584 | /* | |
585 | * See how many times we've tried to send the job; if more than | |
586 | * the limit, cancel the job. | |
587 | */ | |
588 | ||
589 | job->tries ++; | |
590 | ||
591 | if (job->tries >= JobRetryLimit) | |
592 | { | |
593 | /* | |
594 | * Too many tries... | |
595 | */ | |
596 | ||
597 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
09a101d6 | 598 | "[Job %d] Canceling job since it could not be " |
599 | "sent after %d tries.", | |
ef416fc2 | 600 | job->id, JobRetryLimit); |
601 | ||
07725fee | 602 | cupsdCancelJob(job, 0, IPP_JOB_ABORTED); |
ef416fc2 | 603 | } |
604 | else | |
605 | { | |
606 | /* | |
607 | * Try again in N seconds... | |
608 | */ | |
609 | ||
610 | set_hold_until(job, time(NULL) + JobRetryInterval); | |
07725fee | 611 | |
612 | cupsdAddEvent(CUPSD_EVENT_JOB_STATE, job->printer, job, | |
613 | "Job held due to fax errors; please consult " | |
614 | "the error_log file for details."); | |
615 | cupsdSetPrinterState(printer, IPP_PRINTER_IDLE, 0); | |
ef416fc2 | 616 | } |
617 | } | |
618 | else if (!strcmp(printer->error_policy, "abort-job")) | |
07725fee | 619 | cupsdCancelJob(job, 0, IPP_JOB_ABORTED); |
620 | else | |
621 | { | |
622 | cupsdSetPrinterState(printer, IPP_PRINTER_STOPPED, 1); | |
623 | ||
624 | cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED, printer, job, | |
625 | "Job stopped due to backend errors; please consult " | |
626 | "the error_log file for details."); | |
627 | } | |
ef416fc2 | 628 | break; |
629 | ||
630 | case CUPS_BACKEND_CANCEL : | |
631 | /* | |
632 | * Cancel the job... | |
633 | */ | |
634 | ||
07725fee | 635 | cupsdCancelJob(job, 0, IPP_JOB_CANCELED); |
ef416fc2 | 636 | break; |
637 | ||
638 | case CUPS_BACKEND_HOLD : | |
639 | /* | |
640 | * Hold the job... | |
641 | */ | |
642 | ||
643 | cupsdStopJob(job, 0); | |
07725fee | 644 | |
ef416fc2 | 645 | cupsdSetJobHoldUntil(job, "indefinite"); |
07725fee | 646 | |
647 | job->state->values[0].integer = IPP_JOB_HELD; | |
648 | job->state_value = IPP_JOB_HELD; | |
649 | ||
ef416fc2 | 650 | cupsdSaveJob(job); |
07725fee | 651 | |
652 | cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED, printer, job, | |
653 | "Job held due to backend errors; please consult " | |
654 | "the error_log file for details."); | |
ef416fc2 | 655 | break; |
656 | ||
657 | case CUPS_BACKEND_STOP : | |
658 | /* | |
659 | * Stop the printer... | |
660 | */ | |
661 | ||
662 | cupsdStopJob(job, 0); | |
07725fee | 663 | |
664 | job->state->values[0].integer = IPP_JOB_PENDING; | |
665 | job->state_value = IPP_JOB_PENDING; | |
666 | ||
ef416fc2 | 667 | cupsdSaveJob(job); |
668 | cupsdSetPrinterState(printer, IPP_PRINTER_STOPPED, 1); | |
07725fee | 669 | |
670 | cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED, printer, job, | |
671 | "Job stopped due to backend errors; please consult " | |
672 | "the error_log file for details."); | |
ef416fc2 | 673 | break; |
674 | ||
675 | case CUPS_BACKEND_AUTH_REQUIRED : | |
676 | cupsdStopJob(job, 0); | |
07725fee | 677 | |
09a101d6 | 678 | cupsdSetJobHoldUntil(job, "auth-info-required"); |
679 | ||
680 | if ((attr = ippFindAttribute(job->attrs, "job-hold-until", | |
681 | IPP_TAG_KEYWORD)) == NULL) | |
682 | attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME); | |
683 | ||
684 | if (attr) | |
685 | { | |
686 | attr->value_tag = IPP_TAG_KEYWORD; | |
687 | cupsdSetString(&(attr->values[0].string.text), | |
688 | "auth-info-required"); | |
689 | } | |
07725fee | 690 | |
691 | job->state->values[0].integer = IPP_JOB_HELD; | |
692 | job->state_value = IPP_JOB_HELD; | |
693 | ||
ef416fc2 | 694 | cupsdSaveJob(job); |
695 | ||
696 | cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED, printer, job, | |
697 | "Authentication is required for job %d.", job->id); | |
698 | break; | |
699 | } | |
700 | ||
701 | /* | |
702 | * Try printing another job... | |
703 | */ | |
704 | ||
705 | cupsdCheckJobs(); | |
706 | } | |
707 | else if (job->status > 0) | |
708 | { | |
709 | /* | |
e00b005a | 710 | * Filter had errors; stop job... |
ef416fc2 | 711 | */ |
712 | ||
e00b005a | 713 | cupsdStopJob(job, 1); |
714 | cupsdSaveJob(job); | |
bd7854cb | 715 | cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED, printer, job, |
e00b005a | 716 | "Job stopped due to filter errors; please consult the " |
717 | "error_log file for details."); | |
718 | cupsdCheckJobs(); | |
ef416fc2 | 719 | } |
720 | else | |
721 | { | |
722 | /* | |
723 | * Job printed successfully; cancel it... | |
724 | */ | |
725 | ||
726 | if (job->current_file < job->num_files) | |
727 | { | |
e00b005a | 728 | /* |
729 | * Start the next file in the job... | |
730 | */ | |
731 | ||
ef416fc2 | 732 | FilterLevel -= job->cost; |
e1d6a774 | 733 | start_job(job, printer); |
ef416fc2 | 734 | } |
735 | else | |
736 | { | |
e00b005a | 737 | /* |
738 | * Close out this job... | |
739 | */ | |
740 | ||
07725fee | 741 | cupsdCancelJob(job, 0, IPP_JOB_COMPLETED); |
ef416fc2 | 742 | cupsdCheckJobs(); |
743 | } | |
744 | } | |
745 | } | |
746 | ||
747 | ||
748 | /* | |
749 | * 'cupsdFreeAllJobs()' - Free all jobs from memory. | |
750 | */ | |
751 | ||
752 | void | |
753 | cupsdFreeAllJobs(void) | |
754 | { | |
755 | cupsd_job_t *job; /* Current job */ | |
756 | ||
757 | ||
bd7854cb | 758 | if (!Jobs) |
759 | return; | |
760 | ||
ef416fc2 | 761 | cupsdHoldSignals(); |
762 | ||
d09495fa | 763 | cupsdStopAllJobs(1); |
bd7854cb | 764 | cupsdSaveAllJobs(); |
ef416fc2 | 765 | |
766 | for (job = (cupsd_job_t *)cupsArrayFirst(Jobs); | |
767 | job; | |
768 | job = (cupsd_job_t *)cupsArrayNext(Jobs)) | |
769 | { | |
770 | cupsArrayRemove(Jobs, job); | |
771 | cupsArrayRemove(ActiveJobs, job); | |
772 | ||
bd7854cb | 773 | free_job(job); |
ef416fc2 | 774 | } |
775 | ||
776 | cupsdReleaseSignals(); | |
777 | } | |
778 | ||
779 | ||
780 | /* | |
781 | * 'cupsdFindJob()' - Find the specified job. | |
782 | */ | |
783 | ||
784 | cupsd_job_t * /* O - Job data */ | |
785 | cupsdFindJob(int id) /* I - Job ID */ | |
786 | { | |
787 | cupsd_job_t key; /* Search key */ | |
788 | ||
789 | ||
790 | key.id = id; | |
791 | ||
792 | return ((cupsd_job_t *)cupsArrayFind(Jobs, &key)); | |
793 | } | |
794 | ||
795 | ||
796 | /* | |
797 | * 'cupsdGetPrinterJobCount()' - Get the number of pending, processing, | |
798 | * or held jobs in a printer or class. | |
799 | */ | |
800 | ||
801 | int /* O - Job count */ | |
802 | cupsdGetPrinterJobCount( | |
803 | const char *dest) /* I - Printer or class name */ | |
804 | { | |
805 | int count; /* Job count */ | |
806 | cupsd_job_t *job; /* Current job */ | |
807 | ||
808 | ||
809 | for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs), count = 0; | |
810 | job; | |
811 | job = (cupsd_job_t *)cupsArrayNext(ActiveJobs)) | |
bd7854cb | 812 | if (job->dest && !strcasecmp(job->dest, dest)) |
ef416fc2 | 813 | count ++; |
814 | ||
815 | return (count); | |
816 | } | |
817 | ||
818 | ||
819 | /* | |
820 | * 'cupsdGetUserJobCount()' - Get the number of pending, processing, | |
821 | * or held jobs for a user. | |
822 | */ | |
823 | ||
824 | int /* O - Job count */ | |
825 | cupsdGetUserJobCount( | |
826 | const char *username) /* I - Username */ | |
827 | { | |
828 | int count; /* Job count */ | |
829 | cupsd_job_t *job; /* Current job */ | |
830 | ||
831 | ||
832 | for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs), count = 0; | |
833 | job; | |
834 | job = (cupsd_job_t *)cupsArrayNext(ActiveJobs)) | |
835 | if (!strcasecmp(job->username, username)) | |
836 | count ++; | |
837 | ||
838 | return (count); | |
839 | } | |
840 | ||
841 | ||
842 | /* | |
843 | * 'cupsdHoldJob()' - Hold the specified job. | |
844 | */ | |
845 | ||
846 | void | |
847 | cupsdHoldJob(cupsd_job_t *job) /* I - Job data */ | |
848 | { | |
bd7854cb | 849 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdHoldJob: id = %d", job->id); |
ef416fc2 | 850 | |
bd7854cb | 851 | if (job->state_value == IPP_JOB_PROCESSING) |
ef416fc2 | 852 | cupsdStopJob(job, 0); |
bd7854cb | 853 | else |
854 | cupsdLoadJob(job); | |
ef416fc2 | 855 | |
856 | DEBUG_puts("cupsdHoldJob: setting state to held..."); | |
857 | ||
858 | job->state->values[0].integer = IPP_JOB_HELD; | |
bd7854cb | 859 | job->state_value = IPP_JOB_HELD; |
ef416fc2 | 860 | |
861 | cupsdSaveJob(job); | |
862 | ||
863 | cupsdCheckJobs(); | |
864 | } | |
865 | ||
866 | ||
867 | /* | |
868 | * 'cupsdLoadAllJobs()' - Load all jobs from disk. | |
869 | */ | |
870 | ||
871 | void | |
872 | cupsdLoadAllJobs(void) | |
873 | { | |
bd7854cb | 874 | char filename[1024]; /* Full filename of job.cache file */ |
875 | struct stat fileinfo, /* Information on job.cache file */ | |
876 | dirinfo; /* Information on RequestRoot dir */ | |
877 | ||
ef416fc2 | 878 | |
879 | ||
880 | /* | |
bd7854cb | 881 | * Create the job arrays as needed... |
ef416fc2 | 882 | */ |
883 | ||
884 | if (!Jobs) | |
885 | Jobs = cupsArrayNew(compare_jobs, NULL); | |
886 | ||
887 | if (!ActiveJobs) | |
888 | ActiveJobs = cupsArrayNew(compare_active_jobs, NULL); | |
889 | ||
890 | /* | |
bd7854cb | 891 | * See whether the job.cache file is older than the RequestRoot directory... |
ef416fc2 | 892 | */ |
893 | ||
bd7854cb | 894 | snprintf(filename, sizeof(filename), "%s/job.cache", CacheDir); |
ef416fc2 | 895 | |
bd7854cb | 896 | if (stat(filename, &fileinfo)) |
ef416fc2 | 897 | { |
bd7854cb | 898 | fileinfo.st_mtime = 0; |
899 | ||
900 | if (errno != ENOENT) | |
901 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
902 | "Unable to get file information for \"%s\" - %s", | |
903 | filename, strerror(errno)); | |
904 | } | |
905 | ||
906 | if (stat(RequestRoot, &dirinfo)) | |
907 | { | |
908 | dirinfo.st_mtime = 0; | |
909 | ||
910 | if (errno != ENOENT) | |
911 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
912 | "Unable to get directory information for \"%s\" - %s", | |
913 | RequestRoot, strerror(errno)); | |
ef416fc2 | 914 | } |
915 | ||
916 | /* | |
bd7854cb | 917 | * Load the most recent source for job data... |
ef416fc2 | 918 | */ |
919 | ||
bd7854cb | 920 | if (dirinfo.st_mtime > fileinfo.st_mtime) |
921 | { | |
922 | load_request_root(); | |
ef416fc2 | 923 | |
bd7854cb | 924 | load_next_job_id(filename); |
925 | } | |
926 | else | |
927 | load_job_cache(filename); | |
ef416fc2 | 928 | |
bd7854cb | 929 | /* |
930 | * Clean out old jobs as needed... | |
931 | */ | |
ef416fc2 | 932 | |
bd7854cb | 933 | if (MaxJobs > 0 && cupsArrayCount(Jobs) >= MaxJobs) |
934 | cupsdCleanJobs(); | |
935 | } | |
ef416fc2 | 936 | |
ef416fc2 | 937 | |
bd7854cb | 938 | /* |
939 | * 'cupsdLoadJob()' - Load a single job... | |
940 | */ | |
ef416fc2 | 941 | |
bd7854cb | 942 | void |
943 | cupsdLoadJob(cupsd_job_t *job) /* I - Job */ | |
944 | { | |
945 | char jobfile[1024]; /* Job filename */ | |
946 | cups_file_t *fp; /* Job file */ | |
947 | int fileid; /* Current file ID */ | |
948 | ipp_attribute_t *attr; /* Job attribute */ | |
09a101d6 | 949 | const char *dest; /* Destination name */ |
950 | cupsd_printer_t *destptr; /* Pointer to destination */ | |
bd7854cb | 951 | mime_type_t **filetypes; /* New filetypes array */ |
952 | int *compressions; /* New compressions array */ | |
ef416fc2 | 953 | |
ef416fc2 | 954 | |
bd7854cb | 955 | if (job->attrs) |
956 | { | |
f301802f | 957 | if (job->state_value > IPP_JOB_STOPPED) |
bd7854cb | 958 | job->access_time = time(NULL); |
ef416fc2 | 959 | |
bd7854cb | 960 | return; |
961 | } | |
ef416fc2 | 962 | |
bd7854cb | 963 | if ((job->attrs = ippNew()) == NULL) |
964 | { | |
09a101d6 | 965 | cupsdLogMessage(CUPSD_LOG_ERROR, |
966 | "[Job %d] Ran out of memory for job attributes!", job->id); | |
bd7854cb | 967 | return; |
968 | } | |
ef416fc2 | 969 | |
bd7854cb | 970 | /* |
971 | * Load job attributes... | |
972 | */ | |
ef416fc2 | 973 | |
09a101d6 | 974 | cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] Loading attributes...", job->id); |
ef416fc2 | 975 | |
bd7854cb | 976 | snprintf(jobfile, sizeof(jobfile), "%s/c%05d", RequestRoot, job->id); |
977 | if ((fp = cupsFileOpen(jobfile, "r")) == NULL) | |
978 | { | |
979 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
09a101d6 | 980 | "[Job %d] Unable to open job control file \"%s\" - %s!", |
981 | job->id, jobfile, strerror(errno)); | |
bd7854cb | 982 | ippDelete(job->attrs); |
983 | job->attrs = NULL; | |
984 | return; | |
985 | } | |
ef416fc2 | 986 | |
bd7854cb | 987 | if (ippReadIO(fp, (ipp_iocb_t)cupsFileRead, 1, NULL, job->attrs) != IPP_DATA) |
988 | { | |
09a101d6 | 989 | cupsdLogMessage(CUPSD_LOG_ERROR, |
990 | "[Job %d] Unable to read job control file \"%s\"!", | |
991 | job->id, jobfile); | |
bd7854cb | 992 | cupsFileClose(fp); |
993 | ippDelete(job->attrs); | |
994 | job->attrs = NULL; | |
995 | unlink(jobfile); | |
996 | return; | |
997 | } | |
ef416fc2 | 998 | |
bd7854cb | 999 | cupsFileClose(fp); |
ef416fc2 | 1000 | |
bd7854cb | 1001 | /* |
1002 | * Copy attribute data to the job object... | |
1003 | */ | |
ef416fc2 | 1004 | |
bd7854cb | 1005 | if ((job->state = ippFindAttribute(job->attrs, "job-state", |
1006 | IPP_TAG_ENUM)) == NULL) | |
1007 | { | |
1008 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
09a101d6 | 1009 | "[Job %d] Missing or bad job-state attribute in " |
1010 | "control file!", | |
1011 | job->id); | |
bd7854cb | 1012 | ippDelete(job->attrs); |
1013 | job->attrs = NULL; | |
1014 | unlink(jobfile); | |
1015 | return; | |
1016 | } | |
ef416fc2 | 1017 | |
bd7854cb | 1018 | job->state_value = (ipp_jstate_t)job->state->values[0].integer; |
ef416fc2 | 1019 | |
bd7854cb | 1020 | if (!job->dest) |
1021 | { | |
1022 | if ((attr = ippFindAttribute(job->attrs, "job-printer-uri", | |
1023 | IPP_TAG_URI)) == NULL) | |
1024 | { | |
1025 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
09a101d6 | 1026 | "[Job %d] No job-printer-uri attribute in control file!", |
1027 | job->id); | |
bd7854cb | 1028 | ippDelete(job->attrs); |
1029 | job->attrs = NULL; | |
1030 | unlink(jobfile); | |
1031 | return; | |
1032 | } | |
ef416fc2 | 1033 | |
f7deaa1a | 1034 | if ((dest = cupsdValidateDest(attr->values[0].string.text, &(job->dtype), |
09a101d6 | 1035 | &destptr)) == NULL) |
bd7854cb | 1036 | { |
1037 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
09a101d6 | 1038 | "[Job %d] Unable to queue job for destination \"%s\"!", |
1039 | job->id, attr->values[0].string.text); | |
bd7854cb | 1040 | ippDelete(job->attrs); |
1041 | job->attrs = NULL; | |
1042 | unlink(jobfile); | |
1043 | return; | |
1044 | } | |
ef416fc2 | 1045 | |
bd7854cb | 1046 | cupsdSetString(&job->dest, dest); |
1047 | } | |
bc44d920 | 1048 | else if ((destptr = cupsdFindDest(job->dest)) == NULL) |
1049 | { | |
1050 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1051 | "[Job %d] Unable to queue job for destination \"%s\"!", | |
1052 | job->id, job->dest); | |
1053 | ippDelete(job->attrs); | |
1054 | job->attrs = NULL; | |
1055 | unlink(jobfile); | |
1056 | return; | |
1057 | } | |
bd7854cb | 1058 | |
1059 | job->sheets = ippFindAttribute(job->attrs, "job-media-sheets-completed", | |
1060 | IPP_TAG_INTEGER); | |
1061 | job->job_sheets = ippFindAttribute(job->attrs, "job-sheets", IPP_TAG_NAME); | |
1062 | ||
1063 | if (!job->priority) | |
1064 | { | |
1065 | if ((attr = ippFindAttribute(job->attrs, "job-priority", | |
1066 | IPP_TAG_INTEGER)) == NULL) | |
1067 | { | |
1068 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
09a101d6 | 1069 | "[Job %d] Missing or bad job-priority attribute in " |
1070 | "control file!", job->id); | |
bd7854cb | 1071 | ippDelete(job->attrs); |
1072 | job->attrs = NULL; | |
1073 | unlink(jobfile); | |
1074 | return; | |
1075 | } | |
1076 | ||
1077 | job->priority = attr->values[0].integer; | |
1078 | } | |
1079 | ||
1080 | if (!job->username) | |
1081 | { | |
1082 | if ((attr = ippFindAttribute(job->attrs, "job-originating-user-name", | |
1083 | IPP_TAG_NAME)) == NULL) | |
1084 | { | |
1085 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
09a101d6 | 1086 | "[Job %d] Missing or bad job-originating-user-name " |
1087 | "attribute in control file!", job->id); | |
bd7854cb | 1088 | ippDelete(job->attrs); |
1089 | job->attrs = NULL; | |
1090 | unlink(jobfile); | |
1091 | return; | |
ef416fc2 | 1092 | } |
1093 | ||
bd7854cb | 1094 | cupsdSetString(&job->username, attr->values[0].string.text); |
1095 | } | |
1096 | ||
ef416fc2 | 1097 | /* |
bd7854cb | 1098 | * Set the job hold-until time and state... |
ef416fc2 | 1099 | */ |
1100 | ||
bd7854cb | 1101 | if (job->state_value == IPP_JOB_HELD) |
1102 | { | |
1103 | if ((attr = ippFindAttribute(job->attrs, "job-hold-until", | |
1104 | IPP_TAG_KEYWORD)) == NULL) | |
1105 | attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME); | |
ef416fc2 | 1106 | |
bd7854cb | 1107 | if (attr) |
1108 | cupsdSetJobHoldUntil(job, attr->values[0].string.text); | |
1109 | else | |
ef416fc2 | 1110 | { |
bd7854cb | 1111 | job->state->values[0].integer = IPP_JOB_PENDING; |
1112 | job->state_value = IPP_JOB_PENDING; | |
1113 | } | |
1114 | } | |
1115 | else if (job->state_value == IPP_JOB_PROCESSING) | |
1116 | { | |
1117 | job->state->values[0].integer = IPP_JOB_PENDING; | |
1118 | job->state_value = IPP_JOB_PENDING; | |
1119 | } | |
ef416fc2 | 1120 | |
bd7854cb | 1121 | if (!job->num_files) |
1122 | { | |
1123 | /* | |
1124 | * Find all the d##### files... | |
1125 | */ | |
ef416fc2 | 1126 | |
bd7854cb | 1127 | for (fileid = 1; fileid < 10000; fileid ++) |
1128 | { | |
1129 | snprintf(jobfile, sizeof(jobfile), "%s/d%05d-%03d", RequestRoot, | |
1130 | job->id, fileid); | |
ef416fc2 | 1131 | |
bd7854cb | 1132 | if (access(jobfile, 0)) |
1133 | break; | |
ef416fc2 | 1134 | |
09a101d6 | 1135 | cupsdLogMessage(CUPSD_LOG_DEBUG, |
1136 | "[Job %d] Auto-typing document file \"%s\"...", | |
1137 | job->id, jobfile); | |
ef416fc2 | 1138 | |
1139 | if (fileid > job->num_files) | |
1140 | { | |
1141 | if (job->num_files == 0) | |
1142 | { | |
1143 | compressions = (int *)calloc(fileid, sizeof(int)); | |
1144 | filetypes = (mime_type_t **)calloc(fileid, sizeof(mime_type_t *)); | |
1145 | } | |
1146 | else | |
1147 | { | |
1148 | compressions = (int *)realloc(job->compressions, | |
1149 | sizeof(int) * fileid); | |
1150 | filetypes = (mime_type_t **)realloc(job->filetypes, | |
bd7854cb | 1151 | sizeof(mime_type_t *) * |
1152 | fileid); | |
ef416fc2 | 1153 | } |
1154 | ||
bd7854cb | 1155 | if (!compressions || !filetypes) |
ef416fc2 | 1156 | { |
fa73b229 | 1157 | cupsdLogMessage(CUPSD_LOG_ERROR, |
09a101d6 | 1158 | "[Job %d] Ran out of memory for job file types!", |
1159 | job->id); | |
bd7854cb | 1160 | return; |
ef416fc2 | 1161 | } |
1162 | ||
1163 | job->compressions = compressions; | |
1164 | job->filetypes = filetypes; | |
1165 | job->num_files = fileid; | |
1166 | } | |
1167 | ||
bd7854cb | 1168 | job->filetypes[fileid - 1] = mimeFileType(MimeDatabase, jobfile, NULL, |
ef416fc2 | 1169 | job->compressions + fileid - 1); |
1170 | ||
bd7854cb | 1171 | if (!job->filetypes[fileid - 1]) |
ef416fc2 | 1172 | job->filetypes[fileid - 1] = mimeType(MimeDatabase, "application", |
1173 | "vnd.cups-raw"); | |
1174 | } | |
bd7854cb | 1175 | } |
ef416fc2 | 1176 | |
09a101d6 | 1177 | /* |
1178 | * Load authentication information as needed... | |
1179 | */ | |
1180 | ||
1181 | if (job->state_value < IPP_JOB_STOPPED) | |
1182 | { | |
1183 | snprintf(jobfile, sizeof(jobfile), "%s/a%05d", RequestRoot, job->id); | |
1184 | ||
1185 | cupsdClearString(&job->auth_username); | |
1186 | cupsdClearString(&job->auth_domain); | |
1187 | cupsdClearString(&job->auth_password); | |
1188 | ||
1189 | if ((fp = cupsFileOpen(jobfile, "r")) != NULL) | |
1190 | { | |
1191 | int i, /* Looping var */ | |
1192 | bytes; /* Size of auth data */ | |
1193 | char line[255], /* Line from file */ | |
1194 | data[255]; /* Decoded data */ | |
1195 | ||
1196 | ||
1197 | for (i = 0; | |
1198 | i < destptr->num_auth_info_required && | |
1199 | cupsFileGets(fp, line, sizeof(line)); | |
1200 | i ++) | |
1201 | { | |
1202 | bytes = sizeof(data); | |
1203 | httpDecode64_2(data, &bytes, line); | |
1204 | ||
1205 | if (!strcmp(destptr->auth_info_required[i], "username")) | |
1206 | cupsdSetStringf(&job->auth_username, "AUTH_USERNAME=%s", data); | |
1207 | else if (!strcmp(destptr->auth_info_required[i], "domain")) | |
1208 | cupsdSetStringf(&job->auth_domain, "AUTH_DOMAIN=%s", data); | |
1209 | else if (!strcmp(destptr->auth_info_required[i], "password")) | |
1210 | cupsdSetStringf(&job->auth_password, "AUTH_PASSWORD=%s", data); | |
1211 | } | |
1212 | ||
1213 | cupsFileClose(fp); | |
1214 | } | |
1215 | } | |
bd7854cb | 1216 | job->access_time = time(NULL); |
ef416fc2 | 1217 | } |
1218 | ||
1219 | ||
1220 | /* | |
1221 | * 'cupsdMoveJob()' - Move the specified job to a different destination. | |
1222 | */ | |
1223 | ||
1224 | void | |
e53920b9 | 1225 | cupsdMoveJob(cupsd_job_t *job, /* I - Job */ |
1226 | cupsd_printer_t *p) /* I - Destination printer or class */ | |
ef416fc2 | 1227 | { |
1228 | ipp_attribute_t *attr; /* job-printer-uri attribute */ | |
e53920b9 | 1229 | const char *olddest; /* Old destination */ |
1230 | cupsd_printer_t *oldp; /* Old pointer */ | |
ef416fc2 | 1231 | |
1232 | ||
1233 | /* | |
e53920b9 | 1234 | * Don't move completed jobs... |
ef416fc2 | 1235 | */ |
1236 | ||
e53920b9 | 1237 | if (job->state_value > IPP_JOB_STOPPED) |
ef416fc2 | 1238 | return; |
1239 | ||
1240 | /* | |
e53920b9 | 1241 | * Get the old destination... |
ef416fc2 | 1242 | */ |
1243 | ||
e53920b9 | 1244 | olddest = job->dest; |
1245 | ||
1246 | if (job->printer) | |
1247 | oldp = job->printer; | |
1248 | else | |
1249 | oldp = cupsdFindDest(olddest); | |
ef416fc2 | 1250 | |
1251 | /* | |
1252 | * Change the destination information... | |
1253 | */ | |
1254 | ||
bd7854cb | 1255 | cupsdLoadJob(job); |
1256 | ||
e53920b9 | 1257 | cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED, oldp, job, |
1258 | "Job #%d moved from %s to %s.", job->id, olddest, | |
1259 | p->name); | |
1260 | ||
1261 | cupsdSetString(&job->dest, p->name); | |
ef416fc2 | 1262 | job->dtype = p->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_REMOTE | |
1263 | CUPS_PRINTER_IMPLICIT); | |
1264 | ||
bd7854cb | 1265 | if ((attr = ippFindAttribute(job->attrs, "job-printer-uri", |
1266 | IPP_TAG_URI)) != NULL) | |
ef416fc2 | 1267 | cupsdSetString(&(attr->values[0].string.text), p->uri); |
1268 | ||
e53920b9 | 1269 | cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED, p, job, |
1270 | "Job #%d moved from %s to %s.", job->id, olddest, | |
1271 | p->name); | |
1272 | ||
ef416fc2 | 1273 | cupsdSaveJob(job); |
1274 | } | |
1275 | ||
1276 | ||
1277 | /* | |
1278 | * 'cupsdReleaseJob()' - Release the specified job. | |
1279 | */ | |
1280 | ||
1281 | void | |
1282 | cupsdReleaseJob(cupsd_job_t *job) /* I - Job */ | |
1283 | { | |
bd7854cb | 1284 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdReleaseJob: id = %d", job->id); |
ef416fc2 | 1285 | |
bd7854cb | 1286 | if (job->state_value == IPP_JOB_HELD) |
ef416fc2 | 1287 | { |
1288 | DEBUG_puts("cupsdReleaseJob: setting state to pending..."); | |
1289 | ||
1290 | job->state->values[0].integer = IPP_JOB_PENDING; | |
bd7854cb | 1291 | job->state_value = IPP_JOB_PENDING; |
ef416fc2 | 1292 | cupsdSaveJob(job); |
1293 | cupsdCheckJobs(); | |
1294 | } | |
1295 | } | |
1296 | ||
1297 | ||
1298 | /* | |
1299 | * 'cupsdRestartJob()' - Restart the specified job. | |
1300 | */ | |
1301 | ||
1302 | void | |
1303 | cupsdRestartJob(cupsd_job_t *job) /* I - Job */ | |
1304 | { | |
bd7854cb | 1305 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdRestartJob: id = %d", job->id); |
ef416fc2 | 1306 | |
bd7854cb | 1307 | if (job->state_value == IPP_JOB_STOPPED || job->num_files) |
ef416fc2 | 1308 | { |
07725fee | 1309 | ipp_jstate_t old_state; /* Old job state */ |
1310 | ||
1311 | ||
bd7854cb | 1312 | cupsdLoadJob(job); |
1313 | ||
07725fee | 1314 | old_state = job->state_value; |
1315 | ||
ef416fc2 | 1316 | job->tries = 0; |
1317 | job->state->values[0].integer = IPP_JOB_PENDING; | |
bd7854cb | 1318 | job->state_value = IPP_JOB_PENDING; |
07725fee | 1319 | |
ef416fc2 | 1320 | cupsdSaveJob(job); |
07725fee | 1321 | |
1322 | if (old_state > IPP_JOB_STOPPED) | |
1323 | cupsArrayAdd(ActiveJobs, job); | |
1324 | ||
ef416fc2 | 1325 | cupsdCheckJobs(); |
1326 | } | |
1327 | } | |
1328 | ||
1329 | ||
1330 | /* | |
bd7854cb | 1331 | * 'cupsdSaveAllJobs()' - Save a summary of all jobs to disk. |
ef416fc2 | 1332 | */ |
1333 | ||
1334 | void | |
bd7854cb | 1335 | cupsdSaveAllJobs(void) |
1336 | { | |
1337 | int i; /* Looping var */ | |
1338 | cups_file_t *fp; /* Job cache file */ | |
1339 | char temp[1024]; /* Temporary string */ | |
1340 | cupsd_job_t *job; /* Current job */ | |
1341 | time_t curtime; /* Current time */ | |
1342 | struct tm *curdate; /* Current date */ | |
1343 | ||
1344 | ||
1345 | snprintf(temp, sizeof(temp), "%s/job.cache", CacheDir); | |
1346 | if ((fp = cupsFileOpen(temp, "w")) == NULL) | |
1347 | { | |
1348 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1349 | "Unable to create job cache file \"%s\" - %s", | |
1350 | temp, strerror(errno)); | |
1351 | return; | |
1352 | } | |
1353 | ||
1354 | cupsdLogMessage(CUPSD_LOG_INFO, "Saving job cache file \"%s\"...", temp); | |
1355 | ||
1356 | /* | |
1357 | * Restrict access to the file... | |
1358 | */ | |
1359 | ||
1360 | fchown(cupsFileNumber(fp), getuid(), Group); | |
1361 | fchmod(cupsFileNumber(fp), ConfigFilePerm); | |
1362 | ||
1363 | /* | |
1364 | * Write a small header to the file... | |
1365 | */ | |
1366 | ||
1367 | curtime = time(NULL); | |
1368 | curdate = localtime(&curtime); | |
1369 | strftime(temp, sizeof(temp) - 1, "%Y-%m-%d %H:%M", curdate); | |
1370 | ||
1371 | cupsFilePuts(fp, "# Job cache file for " CUPS_SVERSION "\n"); | |
1372 | cupsFilePrintf(fp, "# Written by cupsd on %s\n", temp); | |
1373 | cupsFilePrintf(fp, "NextJobId %d\n", NextJobId); | |
1374 | ||
1375 | /* | |
1376 | * Write each job known to the system... | |
1377 | */ | |
1378 | ||
1379 | for (job = (cupsd_job_t *)cupsArrayFirst(Jobs); | |
1380 | job; | |
1381 | job = (cupsd_job_t *)cupsArrayNext(Jobs)) | |
1382 | { | |
1383 | cupsFilePrintf(fp, "<Job %d>\n", job->id); | |
1384 | cupsFilePrintf(fp, "State %d\n", job->state_value); | |
1385 | cupsFilePrintf(fp, "Priority %d\n", job->priority); | |
1386 | cupsFilePrintf(fp, "Username %s\n", job->username); | |
1387 | cupsFilePrintf(fp, "Destination %s\n", job->dest); | |
1388 | cupsFilePrintf(fp, "DestType %d\n", job->dtype); | |
1389 | cupsFilePrintf(fp, "NumFiles %d\n", job->num_files); | |
1390 | for (i = 0; i < job->num_files; i ++) | |
1391 | cupsFilePrintf(fp, "File %d %s/%s %d\n", i + 1, job->filetypes[i]->super, | |
1392 | job->filetypes[i]->type, job->compressions[i]); | |
1393 | cupsFilePuts(fp, "</Job>\n"); | |
1394 | } | |
1395 | ||
1396 | cupsFileClose(fp); | |
1397 | } | |
1398 | ||
1399 | ||
1400 | /* | |
1401 | * 'cupsdSaveJob()' - Save a job to disk. | |
1402 | */ | |
1403 | ||
1404 | void | |
1405 | cupsdSaveJob(cupsd_job_t *job) /* I - Job */ | |
ef416fc2 | 1406 | { |
1407 | char filename[1024]; /* Job control filename */ | |
fa73b229 | 1408 | cups_file_t *fp; /* Job file */ |
ef416fc2 | 1409 | |
1410 | ||
bd7854cb | 1411 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdSaveJob(job=%p(%d)): job->attrs=%p", |
1412 | job, job->id, job->attrs); | |
1413 | ||
ef416fc2 | 1414 | snprintf(filename, sizeof(filename), "%s/c%05d", RequestRoot, job->id); |
1415 | ||
fa73b229 | 1416 | if ((fp = cupsFileOpen(filename, "w")) == NULL) |
ef416fc2 | 1417 | { |
1418 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
09a101d6 | 1419 | "[Job %d] Unable to create job control file \"%s\" - %s.", |
1420 | job->id, filename, strerror(errno)); | |
ef416fc2 | 1421 | return; |
1422 | } | |
1423 | ||
fa73b229 | 1424 | fchmod(cupsFileNumber(fp), 0600); |
1425 | fchown(cupsFileNumber(fp), RunUser, Group); | |
ef416fc2 | 1426 | |
bd7854cb | 1427 | job->attrs->state = IPP_IDLE; |
1428 | ||
1429 | if (ippWriteIO(fp, (ipp_iocb_t)cupsFileWrite, 1, NULL, | |
1430 | job->attrs) != IPP_DATA) | |
09a101d6 | 1431 | cupsdLogMessage(CUPSD_LOG_ERROR, |
1432 | "[Job %d] Unable to write job control file!", job->id); | |
ef416fc2 | 1433 | |
fa73b229 | 1434 | cupsFileClose(fp); |
ef416fc2 | 1435 | } |
1436 | ||
1437 | ||
1438 | /* | |
1439 | * 'cupsdSetJobHoldUntil()' - Set the hold time for a job... | |
1440 | */ | |
1441 | ||
1442 | void | |
1443 | cupsdSetJobHoldUntil(cupsd_job_t *job, /* I - Job */ | |
1444 | const char *when) /* I - When to resume */ | |
1445 | { | |
1446 | time_t curtime; /* Current time */ | |
1447 | struct tm *curdate; /* Current date */ | |
1448 | int hour; /* Hold hour */ | |
1449 | int minute; /* Hold minute */ | |
1450 | int second; /* Hold second */ | |
1451 | ||
1452 | ||
bd7854cb | 1453 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdSetJobHoldUntil(%d, \"%s\")", |
ef416fc2 | 1454 | job->id, when); |
1455 | ||
1456 | second = 0; | |
1457 | ||
09a101d6 | 1458 | if (!strcmp(when, "indefinite") || !strcmp(when, "auth-info-required")) |
ef416fc2 | 1459 | { |
1460 | /* | |
1461 | * Hold indefinitely... | |
1462 | */ | |
1463 | ||
1464 | job->hold_until = 0; | |
1465 | } | |
1466 | else if (!strcmp(when, "day-time")) | |
1467 | { | |
1468 | /* | |
1469 | * Hold to 6am the next morning unless local time is < 6pm. | |
1470 | */ | |
1471 | ||
1472 | curtime = time(NULL); | |
1473 | curdate = localtime(&curtime); | |
1474 | ||
1475 | if (curdate->tm_hour < 18) | |
1476 | job->hold_until = curtime; | |
1477 | else | |
1478 | job->hold_until = curtime + | |
1479 | ((29 - curdate->tm_hour) * 60 + 59 - | |
1480 | curdate->tm_min) * 60 + 60 - curdate->tm_sec; | |
1481 | } | |
89d46774 | 1482 | else if (!strcmp(when, "evening") || !strcmp(when, "night")) |
ef416fc2 | 1483 | { |
1484 | /* | |
1485 | * Hold to 6pm unless local time is > 6pm or < 6am. | |
1486 | */ | |
1487 | ||
1488 | curtime = time(NULL); | |
1489 | curdate = localtime(&curtime); | |
1490 | ||
1491 | if (curdate->tm_hour < 6 || curdate->tm_hour >= 18) | |
1492 | job->hold_until = curtime; | |
1493 | else | |
1494 | job->hold_until = curtime + | |
1495 | ((17 - curdate->tm_hour) * 60 + 59 - | |
1496 | curdate->tm_min) * 60 + 60 - curdate->tm_sec; | |
f7deaa1a | 1497 | } |
ef416fc2 | 1498 | else if (!strcmp(when, "second-shift")) |
1499 | { | |
1500 | /* | |
1501 | * Hold to 4pm unless local time is > 4pm. | |
1502 | */ | |
1503 | ||
1504 | curtime = time(NULL); | |
1505 | curdate = localtime(&curtime); | |
1506 | ||
1507 | if (curdate->tm_hour >= 16) | |
1508 | job->hold_until = curtime; | |
1509 | else | |
1510 | job->hold_until = curtime + | |
1511 | ((15 - curdate->tm_hour) * 60 + 59 - | |
1512 | curdate->tm_min) * 60 + 60 - curdate->tm_sec; | |
f7deaa1a | 1513 | } |
ef416fc2 | 1514 | else if (!strcmp(when, "third-shift")) |
1515 | { | |
1516 | /* | |
1517 | * Hold to 12am unless local time is < 8am. | |
1518 | */ | |
1519 | ||
1520 | curtime = time(NULL); | |
1521 | curdate = localtime(&curtime); | |
1522 | ||
1523 | if (curdate->tm_hour < 8) | |
1524 | job->hold_until = curtime; | |
1525 | else | |
1526 | job->hold_until = curtime + | |
1527 | ((23 - curdate->tm_hour) * 60 + 59 - | |
1528 | curdate->tm_min) * 60 + 60 - curdate->tm_sec; | |
f7deaa1a | 1529 | } |
ef416fc2 | 1530 | else if (!strcmp(when, "weekend")) |
1531 | { | |
1532 | /* | |
1533 | * Hold to weekend unless we are in the weekend. | |
1534 | */ | |
1535 | ||
1536 | curtime = time(NULL); | |
1537 | curdate = localtime(&curtime); | |
1538 | ||
1539 | if (curdate->tm_wday || curdate->tm_wday == 6) | |
1540 | job->hold_until = curtime; | |
1541 | else | |
1542 | job->hold_until = curtime + | |
1543 | (((5 - curdate->tm_wday) * 24 + | |
1544 | (17 - curdate->tm_hour)) * 60 + 59 - | |
1545 | curdate->tm_min) * 60 + 60 - curdate->tm_sec; | |
1546 | } | |
1547 | else if (sscanf(when, "%d:%d:%d", &hour, &minute, &second) >= 2) | |
1548 | { | |
1549 | /* | |
1550 | * Hold to specified GMT time (HH:MM or HH:MM:SS)... | |
1551 | */ | |
1552 | ||
1553 | curtime = time(NULL); | |
1554 | curdate = gmtime(&curtime); | |
1555 | ||
1556 | job->hold_until = curtime + | |
1557 | ((hour - curdate->tm_hour) * 60 + minute - | |
1558 | curdate->tm_min) * 60 + second - curdate->tm_sec; | |
1559 | ||
1560 | /* | |
1561 | * Hold until next day as needed... | |
1562 | */ | |
1563 | ||
1564 | if (job->hold_until < curtime) | |
b86bc4cf | 1565 | job->hold_until += 24 * 60 * 60; |
ef416fc2 | 1566 | } |
1567 | ||
bd7854cb | 1568 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdSetJobHoldUntil: hold_until = %d", |
ef416fc2 | 1569 | (int)job->hold_until); |
1570 | } | |
1571 | ||
1572 | ||
1573 | /* | |
1574 | * 'cupsdSetJobPriority()' - Set the priority of a job, moving it up/down in | |
1575 | * the list as needed. | |
1576 | */ | |
1577 | ||
1578 | void | |
1579 | cupsdSetJobPriority( | |
1580 | cupsd_job_t *job, /* I - Job ID */ | |
1581 | int priority) /* I - New priority (0 to 100) */ | |
1582 | { | |
1583 | ipp_attribute_t *attr; /* Job attribute */ | |
1584 | ||
1585 | ||
1586 | /* | |
1587 | * Don't change completed jobs... | |
1588 | */ | |
1589 | ||
bd7854cb | 1590 | if (job->state_value >= IPP_JOB_PROCESSING) |
ef416fc2 | 1591 | return; |
1592 | ||
1593 | /* | |
1594 | * Set the new priority and re-add the job into the active list... | |
1595 | */ | |
1596 | ||
1597 | cupsArrayRemove(ActiveJobs, job); | |
1598 | ||
1599 | job->priority = priority; | |
1600 | ||
bd7854cb | 1601 | if ((attr = ippFindAttribute(job->attrs, "job-priority", |
1602 | IPP_TAG_INTEGER)) != NULL) | |
ef416fc2 | 1603 | attr->values[0].integer = priority; |
1604 | else | |
1605 | ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-priority", | |
1606 | priority); | |
1607 | ||
1608 | cupsArrayAdd(ActiveJobs, job); | |
1609 | ||
1610 | cupsdSaveJob(job); | |
1611 | } | |
1612 | ||
1613 | ||
1614 | /* | |
e1d6a774 | 1615 | * 'cupsdStopAllJobs()' - Stop all print jobs. |
ef416fc2 | 1616 | */ |
1617 | ||
1618 | void | |
d09495fa | 1619 | cupsdStopAllJobs(int force) /* I - 1 = Force all filters to stop */ |
ef416fc2 | 1620 | { |
e1d6a774 | 1621 | cupsd_job_t *job; /* Current job */ |
ef416fc2 | 1622 | |
ef416fc2 | 1623 | |
e1d6a774 | 1624 | DEBUG_puts("cupsdStopAllJobs()"); |
ef416fc2 | 1625 | |
e1d6a774 | 1626 | for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs); |
1627 | job; | |
1628 | job = (cupsd_job_t *)cupsArrayNext(ActiveJobs)) | |
1629 | if (job->state_value == IPP_JOB_PROCESSING) | |
1630 | { | |
d09495fa | 1631 | cupsdStopJob(job, force); |
e1d6a774 | 1632 | job->state->values[0].integer = IPP_JOB_PENDING; |
1633 | job->state_value = IPP_JOB_PENDING; | |
1634 | } | |
1635 | } | |
ef416fc2 | 1636 | |
ef416fc2 | 1637 | |
e1d6a774 | 1638 | /* |
1639 | * 'cupsdStopJob()' - Stop a print job. | |
1640 | */ | |
ef416fc2 | 1641 | |
e1d6a774 | 1642 | void |
1643 | cupsdStopJob(cupsd_job_t *job, /* I - Job */ | |
1644 | int force) /* I - 1 = Force all filters to stop */ | |
1645 | { | |
1646 | int i; /* Looping var */ | |
ef416fc2 | 1647 | |
ef416fc2 | 1648 | |
09a101d6 | 1649 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
1650 | "[Job %d] cupsdStopJob: force = %d", job->id, force); | |
ef416fc2 | 1651 | |
e1d6a774 | 1652 | if (job->state_value != IPP_JOB_PROCESSING) |
1653 | return; | |
ef416fc2 | 1654 | |
e1d6a774 | 1655 | FilterLevel -= job->cost; |
ef416fc2 | 1656 | |
07725fee | 1657 | if (job->printer->state == IPP_PRINTER_PROCESSING) |
e1d6a774 | 1658 | cupsdSetPrinterState(job->printer, IPP_PRINTER_IDLE, 0); |
ef416fc2 | 1659 | |
e1d6a774 | 1660 | job->state->values[0].integer = IPP_JOB_STOPPED; |
1661 | job->state_value = IPP_JOB_STOPPED; | |
1662 | job->printer->job = NULL; | |
1663 | job->printer = NULL; | |
ef416fc2 | 1664 | |
e1d6a774 | 1665 | job->current_file --; |
ef416fc2 | 1666 | |
e1d6a774 | 1667 | for (i = 0; job->filters[i]; i ++) |
1668 | if (job->filters[i] > 0) | |
ef416fc2 | 1669 | { |
e1d6a774 | 1670 | cupsdEndProcess(job->filters[i], force); |
1671 | job->filters[i] = 0; | |
ef416fc2 | 1672 | } |
ef416fc2 | 1673 | |
e1d6a774 | 1674 | if (job->backend > 0) |
ef416fc2 | 1675 | { |
e1d6a774 | 1676 | cupsdEndProcess(job->backend, force); |
1677 | job->backend = 0; | |
ef416fc2 | 1678 | } |
1679 | ||
09a101d6 | 1680 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "[Job %d] Closing print pipes [ %d %d ]...", |
1681 | job->id, job->print_pipes[0], job->print_pipes[1]); | |
ef416fc2 | 1682 | |
e1d6a774 | 1683 | cupsdClosePipe(job->print_pipes); |
bd7854cb | 1684 | |
09a101d6 | 1685 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "[Job %d] Closing back pipes [ %d %d ]...", |
1686 | job->id, job->back_pipes[0], job->back_pipes[1]); | |
bd7854cb | 1687 | |
e1d6a774 | 1688 | cupsdClosePipe(job->back_pipes); |
ef416fc2 | 1689 | |
09a101d6 | 1690 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "[Job %d] Closing side pipes [ %d %d ]...", |
1691 | job->id, job->side_pipes[0], job->side_pipes[1]); | |
f7deaa1a | 1692 | |
1693 | cupsdClosePipe(job->side_pipes); | |
1694 | ||
e1d6a774 | 1695 | if (job->status_buffer) |
ef416fc2 | 1696 | { |
1697 | /* | |
e1d6a774 | 1698 | * Close the pipe and clear the input bit. |
ef416fc2 | 1699 | */ |
1700 | ||
f7deaa1a | 1701 | cupsdRemoveSelect(job->status_buffer->fd); |
ef416fc2 | 1702 | |
e1d6a774 | 1703 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
09a101d6 | 1704 | "[Job %d] Closing status pipes [ %d %d ]...", job->id, |
89d46774 | 1705 | job->status_pipes[0], job->status_pipes[1]); |
ef416fc2 | 1706 | |
89d46774 | 1707 | cupsdClosePipe(job->status_pipes); |
e1d6a774 | 1708 | cupsdStatBufDelete(job->status_buffer); |
ef416fc2 | 1709 | |
e1d6a774 | 1710 | job->status_buffer = NULL; |
ef416fc2 | 1711 | } |
e1d6a774 | 1712 | } |
ef416fc2 | 1713 | |
ef416fc2 | 1714 | |
e1d6a774 | 1715 | /* |
1716 | * 'cupsdUnloadCompletedJobs()' - Flush completed job history from memory. | |
1717 | */ | |
ef416fc2 | 1718 | |
e1d6a774 | 1719 | void |
1720 | cupsdUnloadCompletedJobs(void) | |
1721 | { | |
1722 | cupsd_job_t *job; /* Current job */ | |
1723 | time_t expire; /* Expiration time */ | |
ef416fc2 | 1724 | |
ef416fc2 | 1725 | |
e1d6a774 | 1726 | expire = time(NULL) - 60; |
ef416fc2 | 1727 | |
e1d6a774 | 1728 | for (job = (cupsd_job_t *)cupsArrayFirst(Jobs); |
1729 | job; | |
1730 | job = (cupsd_job_t *)cupsArrayNext(Jobs)) | |
1731 | if (job->attrs && job->state_value >= IPP_JOB_STOPPED && | |
1732 | job->access_time < expire) | |
1733 | unload_job(job); | |
1734 | } | |
ef416fc2 | 1735 | |
ef416fc2 | 1736 | |
e1d6a774 | 1737 | /* |
1738 | * 'compare_active_jobs()' - Compare the job IDs and priorities of two jobs. | |
1739 | */ | |
ef416fc2 | 1740 | |
e1d6a774 | 1741 | static int /* O - Difference */ |
1742 | compare_active_jobs(void *first, /* I - First job */ | |
1743 | void *second, /* I - Second job */ | |
1744 | void *data) /* I - App data (not used) */ | |
1745 | { | |
1746 | int diff; /* Difference */ | |
ef416fc2 | 1747 | |
ef416fc2 | 1748 | |
8ca02f3c | 1749 | if ((diff = ((cupsd_job_t *)second)->priority - |
1750 | ((cupsd_job_t *)first)->priority) != 0) | |
e1d6a774 | 1751 | return (diff); |
1752 | else | |
1753 | return (((cupsd_job_t *)first)->id - ((cupsd_job_t *)second)->id); | |
1754 | } | |
ef416fc2 | 1755 | |
ef416fc2 | 1756 | |
e1d6a774 | 1757 | /* |
1758 | * 'compare_jobs()' - Compare the job IDs of two jobs. | |
1759 | */ | |
ef416fc2 | 1760 | |
e1d6a774 | 1761 | static int /* O - Difference */ |
1762 | compare_jobs(void *first, /* I - First job */ | |
1763 | void *second, /* I - Second job */ | |
1764 | void *data) /* I - App data (not used) */ | |
1765 | { | |
1766 | return (((cupsd_job_t *)first)->id - ((cupsd_job_t *)second)->id); | |
1767 | } | |
ef416fc2 | 1768 | |
ef416fc2 | 1769 | |
e1d6a774 | 1770 | /* |
1771 | * 'free_job()' - Free all memory used by a job. | |
1772 | */ | |
ef416fc2 | 1773 | |
e1d6a774 | 1774 | static void |
1775 | free_job(cupsd_job_t *job) /* I - Job */ | |
1776 | { | |
1777 | cupsdClearString(&job->username); | |
1778 | cupsdClearString(&job->dest); | |
09a101d6 | 1779 | cupsdClearString(&job->auth_username); |
1780 | cupsdClearString(&job->auth_domain); | |
1781 | cupsdClearString(&job->auth_password); | |
f7deaa1a | 1782 | #ifdef HAVE_GSSAPI |
1783 | cupsdClearString(&job->ccname); | |
1784 | #endif /* HAVE_GSSAPI */ | |
ef416fc2 | 1785 | |
e1d6a774 | 1786 | if (job->num_files > 0) |
1787 | { | |
1788 | free(job->compressions); | |
1789 | free(job->filetypes); | |
ef416fc2 | 1790 | } |
1791 | ||
e1d6a774 | 1792 | ippDelete(job->attrs); |
ef416fc2 | 1793 | |
e1d6a774 | 1794 | free(job); |
1795 | } | |
bd7854cb | 1796 | |
ef416fc2 | 1797 | |
e1d6a774 | 1798 | /* |
f7deaa1a | 1799 | * 'ipp_length()' - Compute the size of the buffer needed to hold |
e1d6a774 | 1800 | * the textual IPP attributes. |
1801 | */ | |
ef416fc2 | 1802 | |
e1d6a774 | 1803 | static int /* O - Size of attribute buffer */ |
1804 | ipp_length(ipp_t *ipp) /* I - IPP request */ | |
1805 | { | |
1806 | int bytes; /* Number of bytes */ | |
1807 | int i; /* Looping var */ | |
1808 | ipp_attribute_t *attr; /* Current attribute */ | |
bd7854cb | 1809 | |
ef416fc2 | 1810 | |
1811 | /* | |
e1d6a774 | 1812 | * Loop through all attributes... |
ef416fc2 | 1813 | */ |
1814 | ||
e1d6a774 | 1815 | bytes = 0; |
ef416fc2 | 1816 | |
e1d6a774 | 1817 | for (attr = ipp->attrs; attr != NULL; attr = attr->next) |
ef416fc2 | 1818 | { |
e1d6a774 | 1819 | /* |
1820 | * Skip attributes that won't be sent to filters... | |
1821 | */ | |
ef416fc2 | 1822 | |
e1d6a774 | 1823 | if (attr->value_tag == IPP_TAG_MIMETYPE || |
1824 | attr->value_tag == IPP_TAG_NAMELANG || | |
1825 | attr->value_tag == IPP_TAG_TEXTLANG || | |
1826 | attr->value_tag == IPP_TAG_URI || | |
1827 | attr->value_tag == IPP_TAG_URISCHEME) | |
1828 | continue; | |
ef416fc2 | 1829 | |
e1d6a774 | 1830 | if (strncmp(attr->name, "time-", 5) == 0) |
1831 | continue; | |
ef416fc2 | 1832 | |
e1d6a774 | 1833 | /* |
1834 | * Add space for a leading space and commas between each value. | |
1835 | * For the first attribute, the leading space isn't used, so the | |
1836 | * extra byte can be used as the nul terminator... | |
1837 | */ | |
ef416fc2 | 1838 | |
e1d6a774 | 1839 | bytes ++; /* " " separator */ |
1840 | bytes += attr->num_values; /* "," separators */ | |
ef416fc2 | 1841 | |
e1d6a774 | 1842 | /* |
1843 | * Boolean attributes appear as "foo,nofoo,foo,nofoo", while | |
1844 | * other attributes appear as "foo=value1,value2,...,valueN". | |
1845 | */ | |
ef416fc2 | 1846 | |
e1d6a774 | 1847 | if (attr->value_tag != IPP_TAG_BOOLEAN) |
1848 | bytes += strlen(attr->name); | |
1849 | else | |
1850 | bytes += attr->num_values * strlen(attr->name); | |
ef416fc2 | 1851 | |
e1d6a774 | 1852 | /* |
1853 | * Now add the size required for each value in the attribute... | |
1854 | */ | |
ef416fc2 | 1855 | |
e1d6a774 | 1856 | switch (attr->value_tag) |
1857 | { | |
1858 | case IPP_TAG_INTEGER : | |
1859 | case IPP_TAG_ENUM : | |
1860 | /* | |
1861 | * Minimum value of a signed integer is -2147483647, or 11 digits. | |
1862 | */ | |
ef416fc2 | 1863 | |
e1d6a774 | 1864 | bytes += attr->num_values * 11; |
1865 | break; | |
ef416fc2 | 1866 | |
e1d6a774 | 1867 | case IPP_TAG_BOOLEAN : |
1868 | /* | |
1869 | * Add two bytes for each false ("no") value... | |
1870 | */ | |
ef416fc2 | 1871 | |
e1d6a774 | 1872 | for (i = 0; i < attr->num_values; i ++) |
1873 | if (!attr->values[i].boolean) | |
1874 | bytes += 2; | |
1875 | break; | |
ef416fc2 | 1876 | |
e1d6a774 | 1877 | case IPP_TAG_RANGE : |
1878 | /* | |
1879 | * A range is two signed integers separated by a hyphen, or | |
1880 | * 23 characters max. | |
1881 | */ | |
ef416fc2 | 1882 | |
e1d6a774 | 1883 | bytes += attr->num_values * 23; |
1884 | break; | |
ef416fc2 | 1885 | |
e1d6a774 | 1886 | case IPP_TAG_RESOLUTION : |
1887 | /* | |
1888 | * A resolution is two signed integers separated by an "x" and | |
1889 | * suffixed by the units, or 26 characters max. | |
1890 | */ | |
ef416fc2 | 1891 | |
e1d6a774 | 1892 | bytes += attr->num_values * 26; |
1893 | break; | |
ef416fc2 | 1894 | |
e1d6a774 | 1895 | case IPP_TAG_STRING : |
1896 | case IPP_TAG_TEXT : | |
1897 | case IPP_TAG_NAME : | |
1898 | case IPP_TAG_KEYWORD : | |
1899 | case IPP_TAG_CHARSET : | |
1900 | case IPP_TAG_LANGUAGE : | |
1901 | case IPP_TAG_URI : | |
1902 | /* | |
1903 | * Strings can contain characters that need quoting. We need | |
1904 | * at least 2 * len + 2 characters to cover the quotes and | |
1905 | * any backslashes in the string. | |
1906 | */ | |
ef416fc2 | 1907 | |
e1d6a774 | 1908 | for (i = 0; i < attr->num_values; i ++) |
1909 | bytes += 2 * strlen(attr->values[i].string.text) + 2; | |
1910 | break; | |
bd7854cb | 1911 | |
e1d6a774 | 1912 | default : |
1913 | break; /* anti-compiler-warning-code */ | |
bd7854cb | 1914 | } |
ef416fc2 | 1915 | } |
1916 | ||
e1d6a774 | 1917 | return (bytes); |
1918 | } | |
ef416fc2 | 1919 | |
ef416fc2 | 1920 | |
e1d6a774 | 1921 | /* |
1922 | * 'load_job_cache()' - Load jobs from the job.cache file. | |
1923 | */ | |
ef416fc2 | 1924 | |
e1d6a774 | 1925 | static void |
1926 | load_job_cache(const char *filename) /* I - job.cache filename */ | |
1927 | { | |
1928 | cups_file_t *fp; /* job.cache file */ | |
1929 | char line[1024], /* Line buffer */ | |
1930 | *value; /* Value on line */ | |
1931 | int linenum; /* Line number in file */ | |
1932 | cupsd_job_t *job; /* Current job */ | |
1933 | int jobid; /* Job ID */ | |
1934 | char jobfile[1024]; /* Job filename */ | |
ef416fc2 | 1935 | |
ef416fc2 | 1936 | |
e1d6a774 | 1937 | /* |
1938 | * Open the job.cache file... | |
1939 | */ | |
bd7854cb | 1940 | |
e1d6a774 | 1941 | if ((fp = cupsFileOpen(filename, "r")) == NULL) |
1942 | { | |
1943 | if (errno != ENOENT) | |
1944 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1945 | "Unable to open job cache file \"%s\": %s", | |
1946 | filename, strerror(errno)); | |
bd7854cb | 1947 | |
e1d6a774 | 1948 | load_request_root(); |
bd7854cb | 1949 | |
ef416fc2 | 1950 | return; |
1951 | } | |
1952 | ||
e1d6a774 | 1953 | /* |
1954 | * Read entries from the job cache file and create jobs as needed. | |
1955 | */ | |
ef416fc2 | 1956 | |
e1d6a774 | 1957 | cupsdLogMessage(CUPSD_LOG_INFO, "Loading job cache file \"%s\"...", |
1958 | filename); | |
ef416fc2 | 1959 | |
e1d6a774 | 1960 | linenum = 0; |
1961 | job = NULL; | |
ef416fc2 | 1962 | |
e1d6a774 | 1963 | while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum)) |
1964 | { | |
1965 | if (!strcasecmp(line, "NextJobId")) | |
ef416fc2 | 1966 | { |
e1d6a774 | 1967 | if (value) |
1968 | NextJobId = atoi(value); | |
1969 | } | |
1970 | else if (!strcasecmp(line, "<Job")) | |
1971 | { | |
1972 | if (job) | |
ef416fc2 | 1973 | { |
e1d6a774 | 1974 | cupsdLogMessage(CUPSD_LOG_ERROR, "Missing </Job> directive on line %d!", |
1975 | linenum); | |
1976 | continue; | |
1977 | } | |
ef416fc2 | 1978 | |
e1d6a774 | 1979 | if (!value) |
1980 | { | |
1981 | cupsdLogMessage(CUPSD_LOG_ERROR, "Missing job ID on line %d!", linenum); | |
1982 | continue; | |
1983 | } | |
bd7854cb | 1984 | |
e1d6a774 | 1985 | jobid = atoi(value); |
bd7854cb | 1986 | |
e1d6a774 | 1987 | if (jobid < 1) |
1988 | { | |
1989 | cupsdLogMessage(CUPSD_LOG_ERROR, "Bad job ID %d on line %d!", jobid, | |
1990 | linenum); | |
1991 | continue; | |
1992 | } | |
ef416fc2 | 1993 | |
e1d6a774 | 1994 | snprintf(jobfile, sizeof(jobfile), "%s/c%05d", RequestRoot, jobid); |
1995 | if (access(jobfile, 0)) | |
1996 | { | |
09a101d6 | 1997 | cupsdLogMessage(CUPSD_LOG_ERROR, "[Job %d] Files have gone away!", |
1998 | jobid); | |
e1d6a774 | 1999 | continue; |
ef416fc2 | 2000 | } |
e1d6a774 | 2001 | |
2002 | job = calloc(1, sizeof(cupsd_job_t)); | |
2003 | if (!job) | |
ef416fc2 | 2004 | { |
e1d6a774 | 2005 | cupsdLogMessage(CUPSD_LOG_EMERG, |
09a101d6 | 2006 | "[Job %d] Unable to allocate memory for job!", jobid); |
e1d6a774 | 2007 | break; |
2008 | } | |
ef416fc2 | 2009 | |
89d46774 | 2010 | job->id = jobid; |
2011 | job->back_pipes[0] = -1; | |
2012 | job->back_pipes[1] = -1; | |
2013 | job->print_pipes[0] = -1; | |
2014 | job->print_pipes[1] = -1; | |
f7deaa1a | 2015 | job->side_pipes[0] = -1; |
2016 | job->side_pipes[1] = -1; | |
89d46774 | 2017 | job->status_pipes[0] = -1; |
2018 | job->status_pipes[1] = -1; | |
ef416fc2 | 2019 | |
09a101d6 | 2020 | cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] Loading from cache...", job->id); |
e1d6a774 | 2021 | } |
2022 | else if (!job) | |
2023 | { | |
2024 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
2025 | "Missing <Job #> directive on line %d!", linenum); | |
2026 | continue; | |
2027 | } | |
2028 | else if (!strcasecmp(line, "</Job>")) | |
2029 | { | |
2030 | cupsArrayAdd(Jobs, job); | |
ef416fc2 | 2031 | |
f301802f | 2032 | if (job->state_value <= IPP_JOB_STOPPED) |
e1d6a774 | 2033 | { |
2034 | cupsArrayAdd(ActiveJobs, job); | |
2035 | cupsdLoadJob(job); | |
2036 | } | |
bd7854cb | 2037 | |
e1d6a774 | 2038 | job = NULL; |
2039 | } | |
2040 | else if (!value) | |
2041 | { | |
2042 | cupsdLogMessage(CUPSD_LOG_ERROR, "Missing value on line %d!", linenum); | |
2043 | continue; | |
2044 | } | |
2045 | else if (!strcasecmp(line, "State")) | |
2046 | { | |
d09495fa | 2047 | job->state_value = (ipp_jstate_t)atoi(value); |
e1d6a774 | 2048 | |
2049 | if (job->state_value < IPP_JOB_PENDING) | |
2050 | job->state_value = IPP_JOB_PENDING; | |
2051 | else if (job->state_value > IPP_JOB_COMPLETED) | |
2052 | job->state_value = IPP_JOB_COMPLETED; | |
2053 | } | |
2054 | else if (!strcasecmp(line, "Priority")) | |
2055 | { | |
2056 | job->priority = atoi(value); | |
2057 | } | |
2058 | else if (!strcasecmp(line, "Username")) | |
2059 | { | |
2060 | cupsdSetString(&job->username, value); | |
2061 | } | |
2062 | else if (!strcasecmp(line, "Destination")) | |
2063 | { | |
2064 | cupsdSetString(&job->dest, value); | |
2065 | } | |
2066 | else if (!strcasecmp(line, "DestType")) | |
2067 | { | |
2068 | job->dtype = (cups_ptype_t)atoi(value); | |
2069 | } | |
2070 | else if (!strcasecmp(line, "NumFiles")) | |
2071 | { | |
2072 | job->num_files = atoi(value); | |
bd7854cb | 2073 | |
e1d6a774 | 2074 | if (job->num_files < 0) |
2075 | { | |
2076 | cupsdLogMessage(CUPSD_LOG_ERROR, "Bad NumFiles value %d on line %d!", | |
2077 | job->num_files, linenum); | |
2078 | job->num_files = 0; | |
2079 | continue; | |
2080 | } | |
ef416fc2 | 2081 | |
e1d6a774 | 2082 | if (job->num_files > 0) |
2083 | { | |
2084 | snprintf(jobfile, sizeof(jobfile), "%s/d%05d-001", RequestRoot, | |
2085 | job->id); | |
2086 | if (access(jobfile, 0)) | |
2087 | { | |
2088 | cupsdLogMessage(CUPSD_LOG_INFO, | |
09a101d6 | 2089 | "[Job %d] Data files have gone away!", job->id); |
e1d6a774 | 2090 | job->num_files = 0; |
2091 | continue; | |
ef416fc2 | 2092 | } |
e1d6a774 | 2093 | |
2094 | job->filetypes = calloc(job->num_files, sizeof(mime_type_t *)); | |
2095 | job->compressions = calloc(job->num_files, sizeof(int)); | |
2096 | ||
2097 | if (!job->filetypes || !job->compressions) | |
ef416fc2 | 2098 | { |
e1d6a774 | 2099 | cupsdLogMessage(CUPSD_LOG_EMERG, |
09a101d6 | 2100 | "[Job %d] Unable to allocate memory for %d files!", |
2101 | job->id, job->num_files); | |
e1d6a774 | 2102 | break; |
2103 | } | |
2104 | } | |
2105 | } | |
2106 | else if (!strcasecmp(line, "File")) | |
2107 | { | |
2108 | int number, /* File number */ | |
2109 | compression; /* Compression value */ | |
2110 | char super[MIME_MAX_SUPER], /* MIME super type */ | |
2111 | type[MIME_MAX_TYPE]; /* MIME type */ | |
ef416fc2 | 2112 | |
ef416fc2 | 2113 | |
e1d6a774 | 2114 | if (sscanf(value, "%d%*[ \t]%15[^/]/%255s%d", &number, super, type, |
2115 | &compression) != 4) | |
2116 | { | |
2117 | cupsdLogMessage(CUPSD_LOG_ERROR, "Bad File on line %d!", linenum); | |
2118 | continue; | |
2119 | } | |
ef416fc2 | 2120 | |
e1d6a774 | 2121 | if (number < 1 || number > job->num_files) |
2122 | { | |
2123 | cupsdLogMessage(CUPSD_LOG_ERROR, "Bad File number %d on line %d!", | |
2124 | number, linenum); | |
2125 | continue; | |
2126 | } | |
ef416fc2 | 2127 | |
e1d6a774 | 2128 | number --; |
ef416fc2 | 2129 | |
e1d6a774 | 2130 | job->compressions[number] = compression; |
2131 | job->filetypes[number] = mimeType(MimeDatabase, super, type); | |
bd7854cb | 2132 | |
e1d6a774 | 2133 | if (!job->filetypes[number]) |
2134 | { | |
2135 | /* | |
2136 | * If the original MIME type is unknown, auto-type it! | |
2137 | */ | |
bd7854cb | 2138 | |
e1d6a774 | 2139 | cupsdLogMessage(CUPSD_LOG_ERROR, |
09a101d6 | 2140 | "[Job %d] Unknown MIME type %s/%s for file %d!", |
2141 | job->id, super, type, number + 1); | |
ef416fc2 | 2142 | |
e1d6a774 | 2143 | snprintf(jobfile, sizeof(jobfile), "%s/d%05d-%03d", RequestRoot, |
2144 | job->id, number + 1); | |
2145 | job->filetypes[number] = mimeFileType(MimeDatabase, jobfile, NULL, | |
2146 | job->compressions + number); | |
ef416fc2 | 2147 | |
e1d6a774 | 2148 | /* |
2149 | * If that didn't work, assume it is raw... | |
2150 | */ | |
ef416fc2 | 2151 | |
e1d6a774 | 2152 | if (!job->filetypes[number]) |
2153 | job->filetypes[number] = mimeType(MimeDatabase, "application", | |
2154 | "vnd.cups-raw"); | |
ef416fc2 | 2155 | } |
ef416fc2 | 2156 | } |
e1d6a774 | 2157 | else |
2158 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unknown %s directive on line %d!", | |
2159 | line, linenum); | |
2160 | } | |
ef416fc2 | 2161 | |
e1d6a774 | 2162 | cupsFileClose(fp); |
2163 | } | |
ef416fc2 | 2164 | |
ef416fc2 | 2165 | |
e1d6a774 | 2166 | /* |
2167 | * 'load_next_job_id()' - Load the NextJobId value from the job.cache file. | |
2168 | */ | |
ef416fc2 | 2169 | |
e1d6a774 | 2170 | static void |
2171 | load_next_job_id(const char *filename) /* I - job.cache filename */ | |
2172 | { | |
2173 | cups_file_t *fp; /* job.cache file */ | |
2174 | char line[1024], /* Line buffer */ | |
2175 | *value; /* Value on line */ | |
2176 | int linenum; /* Line number in file */ | |
89d46774 | 2177 | int next_job_id; /* NextJobId value from line */ |
ef416fc2 | 2178 | |
ef416fc2 | 2179 | |
e1d6a774 | 2180 | /* |
2181 | * Read the NextJobId directive from the job.cache file and use | |
2182 | * the value (if any). | |
2183 | */ | |
ef416fc2 | 2184 | |
e1d6a774 | 2185 | if ((fp = cupsFileOpen(filename, "r")) == NULL) |
2186 | { | |
2187 | if (errno != ENOENT) | |
2188 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
2189 | "Unable to open job cache file \"%s\": %s", | |
2190 | filename, strerror(errno)); | |
ef416fc2 | 2191 | |
e1d6a774 | 2192 | return; |
2193 | } | |
ef416fc2 | 2194 | |
e1d6a774 | 2195 | cupsdLogMessage(CUPSD_LOG_INFO, |
2196 | "Loading NextJobId from job cache file \"%s\"...", filename); | |
bd7854cb | 2197 | |
e1d6a774 | 2198 | linenum = 0; |
bd7854cb | 2199 | |
e1d6a774 | 2200 | while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum)) |
2201 | { | |
2202 | if (!strcasecmp(line, "NextJobId")) | |
2203 | { | |
2204 | if (value) | |
89d46774 | 2205 | { |
2206 | next_job_id = atoi(value); | |
ef416fc2 | 2207 | |
89d46774 | 2208 | if (next_job_id > NextJobId) |
2209 | NextJobId = next_job_id; | |
2210 | } | |
e1d6a774 | 2211 | break; |
ef416fc2 | 2212 | } |
e1d6a774 | 2213 | } |
ef416fc2 | 2214 | |
e1d6a774 | 2215 | cupsFileClose(fp); |
2216 | } | |
ef416fc2 | 2217 | |
ef416fc2 | 2218 | |
e1d6a774 | 2219 | /* |
2220 | * 'load_request_root()' - Load jobs from the RequestRoot directory. | |
2221 | */ | |
2222 | ||
2223 | static void | |
2224 | load_request_root(void) | |
2225 | { | |
2226 | cups_dir_t *dir; /* Directory */ | |
2227 | cups_dentry_t *dent; /* Directory entry */ | |
2228 | cupsd_job_t *job; /* New job */ | |
2229 | ||
ef416fc2 | 2230 | |
2231 | /* | |
e1d6a774 | 2232 | * Open the requests directory... |
ef416fc2 | 2233 | */ |
2234 | ||
e1d6a774 | 2235 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Scanning %s for jobs...", RequestRoot); |
2236 | ||
2237 | if ((dir = cupsDirOpen(RequestRoot)) == NULL) | |
ef416fc2 | 2238 | { |
e1d6a774 | 2239 | cupsdLogMessage(CUPSD_LOG_ERROR, |
2240 | "Unable to open spool directory \"%s\": %s", | |
2241 | RequestRoot, strerror(errno)); | |
2242 | return; | |
2243 | } | |
2244 | ||
2245 | /* | |
2246 | * Read all the c##### files... | |
2247 | */ | |
ef416fc2 | 2248 | |
e1d6a774 | 2249 | while ((dent = cupsDirRead(dir)) != NULL) |
2250 | if (strlen(dent->filename) >= 6 && dent->filename[0] == 'c') | |
2251 | { | |
e00b005a | 2252 | /* |
e1d6a774 | 2253 | * Allocate memory for the job... |
e00b005a | 2254 | */ |
2255 | ||
e1d6a774 | 2256 | if ((job = calloc(sizeof(cupsd_job_t), 1)) == NULL) |
ef416fc2 | 2257 | { |
e1d6a774 | 2258 | cupsdLogMessage(CUPSD_LOG_ERROR, "Ran out of memory for jobs!"); |
2259 | cupsDirClose(dir); | |
ef416fc2 | 2260 | return; |
2261 | } | |
2262 | ||
e1d6a774 | 2263 | /* |
2264 | * Assign the job ID... | |
2265 | */ | |
ef416fc2 | 2266 | |
89d46774 | 2267 | job->id = atoi(dent->filename + 1); |
2268 | job->back_pipes[0] = -1; | |
2269 | job->back_pipes[1] = -1; | |
2270 | job->print_pipes[0] = -1; | |
2271 | job->print_pipes[1] = -1; | |
f7deaa1a | 2272 | job->side_pipes[0] = -1; |
2273 | job->side_pipes[1] = -1; | |
89d46774 | 2274 | job->status_pipes[0] = -1; |
2275 | job->status_pipes[1] = -1; | |
ef416fc2 | 2276 | |
e1d6a774 | 2277 | if (job->id >= NextJobId) |
2278 | NextJobId = job->id + 1; | |
ef416fc2 | 2279 | |
e1d6a774 | 2280 | /* |
2281 | * Load the job... | |
2282 | */ | |
ef416fc2 | 2283 | |
e1d6a774 | 2284 | cupsdLoadJob(job); |
bd7854cb | 2285 | |
e1d6a774 | 2286 | /* |
2287 | * Insert the job into the array, sorting by job priority and ID... | |
2288 | */ | |
bd7854cb | 2289 | |
e1d6a774 | 2290 | cupsArrayAdd(Jobs, job); |
ef416fc2 | 2291 | |
f301802f | 2292 | if (job->state_value <= IPP_JOB_STOPPED) |
07725fee | 2293 | cupsArrayAdd(ActiveJobs, job); |
ef416fc2 | 2294 | else |
e1d6a774 | 2295 | unload_job(job); |
ef416fc2 | 2296 | } |
2297 | ||
e1d6a774 | 2298 | cupsDirClose(dir); |
2299 | } | |
ef416fc2 | 2300 | |
ef416fc2 | 2301 | |
e1d6a774 | 2302 | /* |
2303 | * 'set_time()' - Set one of the "time-at-xyz" attributes... | |
2304 | */ | |
ef416fc2 | 2305 | |
e1d6a774 | 2306 | static void |
2307 | set_time(cupsd_job_t *job, /* I - Job to update */ | |
2308 | const char *name) /* I - Name of attribute */ | |
2309 | { | |
2310 | ipp_attribute_t *attr; /* Time attribute */ | |
ef416fc2 | 2311 | |
ef416fc2 | 2312 | |
e1d6a774 | 2313 | if ((attr = ippFindAttribute(job->attrs, name, IPP_TAG_ZERO)) != NULL) |
bd7854cb | 2314 | { |
e1d6a774 | 2315 | attr->value_tag = IPP_TAG_INTEGER; |
2316 | attr->values[0].integer = time(NULL); | |
bd7854cb | 2317 | } |
e1d6a774 | 2318 | } |
bd7854cb | 2319 | |
ef416fc2 | 2320 | |
e1d6a774 | 2321 | /* |
2322 | * 'set_hold_until()' - Set the hold time and update job-hold-until attribute... | |
2323 | */ | |
ef416fc2 | 2324 | |
f7deaa1a | 2325 | static void |
e1d6a774 | 2326 | set_hold_until(cupsd_job_t *job, /* I - Job to update */ |
2327 | time_t holdtime) /* I - Hold until time */ | |
2328 | { | |
2329 | ipp_attribute_t *attr; /* job-hold-until attribute */ | |
2330 | struct tm *holddate; /* Hold date */ | |
2331 | char holdstr[64]; /* Hold time */ | |
ef416fc2 | 2332 | |
ef416fc2 | 2333 | |
e1d6a774 | 2334 | /* |
2335 | * Set the hold_until value and hold the job... | |
2336 | */ | |
ef416fc2 | 2337 | |
e1d6a774 | 2338 | cupsdLogMessage(CUPSD_LOG_DEBUG, "set_hold_until: hold_until = %d", |
2339 | (int)holdtime); | |
e00b005a | 2340 | |
e1d6a774 | 2341 | job->state->values[0].integer = IPP_JOB_HELD; |
2342 | job->state_value = IPP_JOB_HELD; | |
2343 | job->hold_until = holdtime; | |
ef416fc2 | 2344 | |
e1d6a774 | 2345 | /* |
2346 | * Update the job-hold-until attribute with a string representing GMT | |
2347 | * time (HH:MM:SS)... | |
2348 | */ | |
ef416fc2 | 2349 | |
e1d6a774 | 2350 | holddate = gmtime(&holdtime); |
f7deaa1a | 2351 | snprintf(holdstr, sizeof(holdstr), "%d:%d:%d", holddate->tm_hour, |
e1d6a774 | 2352 | holddate->tm_min, holddate->tm_sec); |
ef416fc2 | 2353 | |
e1d6a774 | 2354 | if ((attr = ippFindAttribute(job->attrs, "job-hold-until", |
2355 | IPP_TAG_KEYWORD)) == NULL) | |
2356 | attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME); | |
ef416fc2 | 2357 | |
e1d6a774 | 2358 | /* |
2359 | * Either add the attribute or update the value of the existing one | |
2360 | */ | |
ef416fc2 | 2361 | |
e1d6a774 | 2362 | if (attr == NULL) |
2363 | attr = ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_KEYWORD, | |
2364 | "job-hold-until", NULL, holdstr); | |
2365 | else | |
2366 | cupsdSetString(&attr->values[0].string.text, holdstr); | |
ef416fc2 | 2367 | |
e1d6a774 | 2368 | cupsdSaveJob(job); |
ef416fc2 | 2369 | } |
2370 | ||
2371 | ||
2372 | /* | |
e1d6a774 | 2373 | * 'start_job()' - Start a print job. |
ef416fc2 | 2374 | */ |
2375 | ||
e1d6a774 | 2376 | static void |
2377 | start_job(cupsd_job_t *job, /* I - Job ID */ | |
2378 | cupsd_printer_t *printer) /* I - Printer to print job */ | |
ef416fc2 | 2379 | { |
e1d6a774 | 2380 | int i; /* Looping var */ |
2381 | int slot; /* Pipe slot */ | |
f7deaa1a | 2382 | cups_array_t *filters, /* Filters for job */ |
2383 | *prefilters; /* Filters with prefilters */ | |
e1d6a774 | 2384 | mime_filter_t *filter, /* Current filter */ |
f7deaa1a | 2385 | *prefilter, /* Prefilter */ |
e1d6a774 | 2386 | port_monitor; /* Port monitor filter */ |
2387 | char method[255], /* Method for output */ | |
2388 | *optptr, /* Pointer to options */ | |
2389 | *valptr; /* Pointer in value string */ | |
2390 | ipp_attribute_t *attr; /* Current attribute */ | |
2391 | struct stat backinfo; /* Backend file information */ | |
2392 | int backroot; /* Run backend as root? */ | |
2393 | int pid; /* Process ID of new filter process */ | |
2394 | int banner_page; /* 1 if banner page, 0 otherwise */ | |
89d46774 | 2395 | int filterfds[2][2];/* Pipes used between filters */ |
e1d6a774 | 2396 | int envc; /* Number of environment variables */ |
2397 | char **argv, /* Filter command-line arguments */ | |
2398 | sani_uri[1024], /* Sanitized DEVICE_URI env var */ | |
2399 | filename[1024], /* Job filename */ | |
2400 | command[1024], /* Full path to command */ | |
2401 | jobid[255], /* Job ID string */ | |
2402 | title[IPP_MAX_NAME], | |
2403 | /* Job title string */ | |
2404 | copies[255], /* # copies string */ | |
09a101d6 | 2405 | *envp[MAX_ENV + 15], |
e1d6a774 | 2406 | /* Environment variables */ |
2407 | charset[255], /* CHARSET env variable */ | |
2408 | class_name[255],/* CLASS env variable */ | |
2409 | classification[1024], | |
2410 | /* CLASSIFICATION env variable */ | |
2411 | content_type[1024], | |
2412 | /* CONTENT_TYPE env variable */ | |
2413 | device_uri[1024], | |
2414 | /* DEVICE_URI env variable */ | |
2415 | final_content_type[1024], | |
2416 | /* FINAL_CONTENT_TYPE env variable */ | |
2417 | lang[255], /* LANG env variable */ | |
2418 | ppd[1024], /* PPD env variable */ | |
2419 | printer_name[255], | |
2420 | /* PRINTER env variable */ | |
2421 | rip_max_cache[255]; | |
2422 | /* RIP_MAX_CACHE env variable */ | |
e1d6a774 | 2423 | static char *options = NULL;/* Full list of options */ |
2424 | static int optlength = 0; /* Length of option buffer */ | |
ef416fc2 | 2425 | |
2426 | ||
09a101d6 | 2427 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "[Job %d] start_job: file = %d/%d", |
e1d6a774 | 2428 | job->id, job->current_file, job->num_files); |
ef416fc2 | 2429 | |
e1d6a774 | 2430 | if (job->num_files == 0) |
2431 | { | |
09a101d6 | 2432 | cupsdLogMessage(CUPSD_LOG_ERROR, "[Job %d] No files, canceling job!", |
e1d6a774 | 2433 | job->id); |
2434 | ||
07725fee | 2435 | cupsdCancelJob(job, 0, IPP_JOB_ABORTED); |
ef416fc2 | 2436 | return; |
e1d6a774 | 2437 | } |
ef416fc2 | 2438 | |
e1d6a774 | 2439 | /* |
2440 | * Figure out what filters are required to convert from | |
2441 | * the source to the destination type... | |
2442 | */ | |
ef416fc2 | 2443 | |
e1d6a774 | 2444 | filters = NULL; |
2445 | job->cost = 0; | |
ef416fc2 | 2446 | |
e1d6a774 | 2447 | if (printer->raw) |
2448 | { | |
2449 | /* | |
2450 | * Remote jobs and raw queues go directly to the printer without | |
2451 | * filtering... | |
2452 | */ | |
ef416fc2 | 2453 | |
e1d6a774 | 2454 | cupsdLogMessage(CUPSD_LOG_DEBUG, |
2455 | "[Job %d] Sending job to queue tagged as raw...", job->id); | |
ef416fc2 | 2456 | |
e1d6a774 | 2457 | filters = NULL; |
2458 | } | |
2459 | else | |
2460 | { | |
2461 | /* | |
2462 | * Local jobs get filtered... | |
2463 | */ | |
ef416fc2 | 2464 | |
e1d6a774 | 2465 | filters = mimeFilter(MimeDatabase, job->filetypes[job->current_file], |
2466 | printer->filetype, &(job->cost)); | |
2467 | ||
2468 | if (!filters) | |
ef416fc2 | 2469 | { |
e1d6a774 | 2470 | cupsdLogMessage(CUPSD_LOG_ERROR, |
09a101d6 | 2471 | "[Job %d] Unable to convert file %d to printable format!", |
e1d6a774 | 2472 | job->current_file, job->id); |
2473 | cupsdLogMessage(CUPSD_LOG_INFO, | |
2474 | "Hint: Do you have ESP Ghostscript installed?"); | |
ef416fc2 | 2475 | |
e1d6a774 | 2476 | if (LogLevel < CUPSD_LOG_DEBUG) |
2477 | cupsdLogMessage(CUPSD_LOG_INFO, | |
2478 | "Hint: Try setting the LogLevel to \"debug\"."); | |
ef416fc2 | 2479 | |
e1d6a774 | 2480 | job->current_file ++; |
ef416fc2 | 2481 | |
e1d6a774 | 2482 | if (job->current_file == job->num_files) |
07725fee | 2483 | cupsdCancelJob(job, 0, IPP_JOB_ABORTED); |
ef416fc2 | 2484 | |
e1d6a774 | 2485 | return; |
2486 | } | |
ef416fc2 | 2487 | |
ef416fc2 | 2488 | /* |
e1d6a774 | 2489 | * Remove NULL ("-") filters... |
ef416fc2 | 2490 | */ |
2491 | ||
e1d6a774 | 2492 | for (filter = (mime_filter_t *)cupsArrayFirst(filters); |
2493 | filter; | |
2494 | filter = (mime_filter_t *)cupsArrayNext(filters)) | |
2495 | if (!strcmp(filter->filter, "-")) | |
2496 | cupsArrayRemove(filters, filter); | |
ef416fc2 | 2497 | |
e1d6a774 | 2498 | if (cupsArrayCount(filters) == 0) |
2499 | { | |
2500 | cupsArrayDelete(filters); | |
2501 | filters = NULL; | |
2502 | } | |
f7deaa1a | 2503 | |
2504 | /* | |
2505 | * If this printer has any pre-filters, insert the required pre-filter | |
2506 | * in the filters array... | |
2507 | */ | |
2508 | ||
2509 | if (printer->prefiltertype && filters) | |
2510 | { | |
2511 | prefilters = cupsArrayNew(NULL, NULL); | |
2512 | ||
2513 | for (filter = (mime_filter_t *)cupsArrayFirst(filters); | |
2514 | filter; | |
2515 | filter = (mime_filter_t *)cupsArrayNext(filters)) | |
2516 | { | |
2517 | if ((prefilter = mimeFilterLookup(MimeDatabase, filter->src, | |
2518 | printer->prefiltertype))) | |
2519 | { | |
2520 | cupsArrayAdd(prefilters, prefilter); | |
2521 | job->cost += prefilter->cost; | |
2522 | } | |
2523 | ||
2524 | cupsArrayAdd(prefilters, filter); | |
2525 | } | |
2526 | ||
2527 | cupsArrayDelete(filters); | |
2528 | filters = prefilters; | |
2529 | } | |
e1d6a774 | 2530 | } |
ef416fc2 | 2531 | |
e1d6a774 | 2532 | /* |
2533 | * Set a minimum cost of 100 for all jobs so that FilterLimit | |
2534 | * works with raw queues and other low-cost paths. | |
2535 | */ | |
ef416fc2 | 2536 | |
e1d6a774 | 2537 | if (job->cost < 100) |
2538 | job->cost = 100; | |
ef416fc2 | 2539 | |
e1d6a774 | 2540 | /* |
2541 | * See if the filter cost is too high... | |
2542 | */ | |
ef416fc2 | 2543 | |
e1d6a774 | 2544 | if ((FilterLevel + job->cost) > FilterLimit && FilterLevel > 0 && |
2545 | FilterLimit > 0) | |
2546 | { | |
2547 | /* | |
2548 | * Don't print this job quite yet... | |
2549 | */ | |
bd7854cb | 2550 | |
e1d6a774 | 2551 | cupsArrayDelete(filters); |
bd7854cb | 2552 | |
e1d6a774 | 2553 | cupsdLogMessage(CUPSD_LOG_INFO, |
09a101d6 | 2554 | "[Job %d] Holding because filter limit has been reached.", |
e1d6a774 | 2555 | job->id); |
2556 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
09a101d6 | 2557 | "[Job %d] start_job: file=%d, cost=%d, level=%d, limit=%d", |
e1d6a774 | 2558 | job->id, job->current_file, job->cost, FilterLevel, |
2559 | FilterLimit); | |
2560 | return; | |
2561 | } | |
bd7854cb | 2562 | |
e1d6a774 | 2563 | FilterLevel += job->cost; |
bd7854cb | 2564 | |
e1d6a774 | 2565 | /* |
f899b121 | 2566 | * Add decompression/raw filter as needed... |
e1d6a774 | 2567 | */ |
bd7854cb | 2568 | |
f899b121 | 2569 | if ((!printer->raw && job->compressions[job->current_file]) || |
2570 | (!filters && !printer->remote && | |
2571 | (job->num_files > 1 || !strncmp(printer->device_uri, "file:", 5)))) | |
e1d6a774 | 2572 | { |
2573 | /* | |
2574 | * Add gziptoany filter to the front of the list... | |
2575 | */ | |
bd7854cb | 2576 | |
2abf387c | 2577 | if (!filters) |
2578 | filters = cupsArrayNew(NULL, NULL); | |
2579 | ||
e1d6a774 | 2580 | if (!cupsArrayInsert(filters, &gziptoany_filter)) |
2581 | { | |
2582 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
09a101d6 | 2583 | "[Job %d] Unable to add decompression filter - %s", |
2584 | job->id, strerror(errno)); | |
bd7854cb | 2585 | |
e1d6a774 | 2586 | cupsArrayDelete(filters); |
bd7854cb | 2587 | |
e1d6a774 | 2588 | job->current_file ++; |
bd7854cb | 2589 | |
e1d6a774 | 2590 | if (job->current_file == job->num_files) |
07725fee | 2591 | cupsdCancelJob(job, 0, IPP_JOB_ABORTED); |
ef416fc2 | 2592 | |
e1d6a774 | 2593 | return; |
2594 | } | |
2595 | } | |
ef416fc2 | 2596 | |
e1d6a774 | 2597 | /* |
2598 | * Add port monitor, if any... | |
2599 | */ | |
ef416fc2 | 2600 | |
e1d6a774 | 2601 | if (printer->port_monitor) |
ef416fc2 | 2602 | { |
2603 | /* | |
e1d6a774 | 2604 | * Add port monitor to the end of the list... |
ef416fc2 | 2605 | */ |
2606 | ||
2abf387c | 2607 | if (!filters) |
2608 | filters = cupsArrayNew(NULL, NULL); | |
2609 | ||
e1d6a774 | 2610 | if (!cupsArrayAdd(filters, &port_monitor)) |
ef416fc2 | 2611 | { |
09a101d6 | 2612 | cupsdLogMessage(CUPSD_LOG_ERROR, |
2613 | "[Job %d] Unable to add port monitor - %s", | |
2614 | job->id, strerror(errno)); | |
ef416fc2 | 2615 | |
e1d6a774 | 2616 | cupsArrayDelete(filters); |
ef416fc2 | 2617 | |
e1d6a774 | 2618 | job->current_file ++; |
ef416fc2 | 2619 | |
e1d6a774 | 2620 | if (job->current_file == job->num_files) |
07725fee | 2621 | cupsdCancelJob(job, 0, IPP_JOB_ABORTED); |
ef416fc2 | 2622 | |
e1d6a774 | 2623 | return; |
e00b005a | 2624 | } |
ef416fc2 | 2625 | |
e1d6a774 | 2626 | snprintf(port_monitor.filter, sizeof(port_monitor.filter), |
2627 | "%s/monitor/%s", ServerBin, printer->port_monitor); | |
2628 | } | |
e00b005a | 2629 | |
e1d6a774 | 2630 | /* |
2631 | * Update the printer and job state to "processing"... | |
2632 | */ | |
ef416fc2 | 2633 | |
e1d6a774 | 2634 | job->state->values[0].integer = IPP_JOB_PROCESSING; |
2635 | job->state_value = IPP_JOB_PROCESSING; | |
f899b121 | 2636 | |
e1d6a774 | 2637 | job->status = 0; |
2638 | job->printer = printer; | |
2639 | printer->job = job; | |
f899b121 | 2640 | |
e1d6a774 | 2641 | cupsdSetPrinterState(printer, IPP_PRINTER_PROCESSING, 0); |
ef416fc2 | 2642 | |
e1d6a774 | 2643 | if (job->current_file == 0) |
ef416fc2 | 2644 | { |
f301802f | 2645 | /* |
2646 | * Set the processing time... | |
2647 | */ | |
2648 | ||
e1d6a774 | 2649 | set_time(job, "time-at-processing"); |
f301802f | 2650 | |
2651 | /* | |
2652 | * Create the backchannel pipes and make them non-blocking... | |
2653 | */ | |
2654 | ||
e1d6a774 | 2655 | cupsdOpenPipe(job->back_pipes); |
f301802f | 2656 | |
2657 | fcntl(job->back_pipes[0], F_SETFL, | |
2658 | fcntl(job->back_pipes[0], F_GETFL) | O_NONBLOCK); | |
2659 | ||
2660 | fcntl(job->back_pipes[1], F_SETFL, | |
2661 | fcntl(job->back_pipes[1], F_GETFL) | O_NONBLOCK); | |
f7deaa1a | 2662 | |
2663 | /* | |
2664 | * Create the side-channel pipes and make them non-blocking... | |
2665 | */ | |
2666 | ||
2667 | socketpair(AF_LOCAL, SOCK_STREAM, 0, job->side_pipes); | |
2668 | ||
2669 | fcntl(job->side_pipes[0], F_SETFL, | |
2670 | fcntl(job->side_pipes[0], F_GETFL) | O_NONBLOCK); | |
2671 | ||
2672 | fcntl(job->side_pipes[1], F_SETFL, | |
2673 | fcntl(job->side_pipes[1], F_GETFL) | O_NONBLOCK); | |
e1d6a774 | 2674 | } |
ef416fc2 | 2675 | |
e1d6a774 | 2676 | /* |
2677 | * Determine if we are printing a banner page or not... | |
2678 | */ | |
ef416fc2 | 2679 | |
e1d6a774 | 2680 | if (job->job_sheets == NULL) |
2681 | { | |
09a101d6 | 2682 | cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] No job-sheets attribute.", |
2683 | job->id); | |
e1d6a774 | 2684 | if ((job->job_sheets = |
2685 | ippFindAttribute(job->attrs, "job-sheets", IPP_TAG_ZERO)) != NULL) | |
2686 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
09a101d6 | 2687 | "[Job %d] ... but someone added one without setting " |
2688 | "job_sheets!", job->id); | |
e1d6a774 | 2689 | } |
2690 | else if (job->job_sheets->num_values == 1) | |
09a101d6 | 2691 | cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] job-sheets=%s", job->id, |
2692 | job->job_sheets->values[0].string.text); | |
e1d6a774 | 2693 | else |
09a101d6 | 2694 | cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] job-sheets=%s,%s", job->id, |
e1d6a774 | 2695 | job->job_sheets->values[0].string.text, |
2696 | job->job_sheets->values[1].string.text); | |
ef416fc2 | 2697 | |
e1d6a774 | 2698 | if (printer->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)) |
2699 | banner_page = 0; | |
2700 | else if (job->job_sheets == NULL) | |
2701 | banner_page = 0; | |
2702 | else if (strcasecmp(job->job_sheets->values[0].string.text, "none") != 0 && | |
2703 | job->current_file == 0) | |
2704 | banner_page = 1; | |
2705 | else if (job->job_sheets->num_values > 1 && | |
2706 | strcasecmp(job->job_sheets->values[1].string.text, "none") != 0 && | |
2707 | job->current_file == (job->num_files - 1)) | |
2708 | banner_page = 1; | |
2709 | else | |
2710 | banner_page = 0; | |
ef416fc2 | 2711 | |
09a101d6 | 2712 | cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] banner_page = %d", job->id, |
2713 | banner_page); | |
ef416fc2 | 2714 | |
e1d6a774 | 2715 | /* |
2716 | * Building the options string is harder than it needs to be, but | |
2717 | * for the moment we need to pass strings for command-line args and | |
2718 | * not IPP attribute pointers... :) | |
2719 | * | |
2720 | * First allocate/reallocate the option buffer as needed... | |
2721 | */ | |
ef416fc2 | 2722 | |
e1d6a774 | 2723 | i = ipp_length(job->attrs); |
ef416fc2 | 2724 | |
e1d6a774 | 2725 | if (i > optlength) |
2726 | { | |
2727 | if (optlength == 0) | |
2728 | optptr = malloc(i); | |
2729 | else | |
2730 | optptr = realloc(options, i); | |
ef416fc2 | 2731 | |
e1d6a774 | 2732 | if (optptr == NULL) |
2733 | { | |
2734 | cupsdLogMessage(CUPSD_LOG_CRIT, | |
09a101d6 | 2735 | "[Job %d] Unable to allocate %d bytes for option buffer!", |
2736 | job->id, i); | |
ef416fc2 | 2737 | |
e1d6a774 | 2738 | cupsArrayDelete(filters); |
ef416fc2 | 2739 | |
e1d6a774 | 2740 | FilterLevel -= job->cost; |
ef416fc2 | 2741 | |
07725fee | 2742 | cupsdCancelJob(job, 0, IPP_JOB_ABORTED); |
e1d6a774 | 2743 | return; |
2744 | } | |
ef416fc2 | 2745 | |
e1d6a774 | 2746 | options = optptr; |
2747 | optlength = i; | |
2748 | } | |
ef416fc2 | 2749 | |
e1d6a774 | 2750 | /* |
2751 | * Now loop through the attributes and convert them to the textual | |
2752 | * representation used by the filters... | |
2753 | */ | |
ef416fc2 | 2754 | |
e1d6a774 | 2755 | optptr = options; |
2756 | *optptr = '\0'; | |
bd7854cb | 2757 | |
e1d6a774 | 2758 | snprintf(title, sizeof(title), "%s-%d", printer->name, job->id); |
2759 | strcpy(copies, "1"); | |
bd7854cb | 2760 | |
e1d6a774 | 2761 | for (attr = job->attrs->attrs; attr != NULL; attr = attr->next) |
bd7854cb | 2762 | { |
e1d6a774 | 2763 | if (!strcmp(attr->name, "copies") && |
2764 | attr->value_tag == IPP_TAG_INTEGER) | |
2765 | { | |
2766 | /* | |
2767 | * Don't use the # copies attribute if we are printing the job sheets... | |
2768 | */ | |
bd7854cb | 2769 | |
e1d6a774 | 2770 | if (!banner_page) |
2771 | sprintf(copies, "%d", attr->values[0].integer); | |
2772 | } | |
2773 | else if (!strcmp(attr->name, "job-name") && | |
2774 | (attr->value_tag == IPP_TAG_NAME || | |
2775 | attr->value_tag == IPP_TAG_NAMELANG)) | |
2776 | strlcpy(title, attr->values[0].string.text, sizeof(title)); | |
2777 | else if (attr->group_tag == IPP_TAG_JOB) | |
2778 | { | |
2779 | /* | |
2780 | * Filter out other unwanted attributes... | |
2781 | */ | |
bd7854cb | 2782 | |
e1d6a774 | 2783 | if (attr->value_tag == IPP_TAG_MIMETYPE || |
2784 | attr->value_tag == IPP_TAG_NAMELANG || | |
2785 | attr->value_tag == IPP_TAG_TEXTLANG || | |
2786 | (attr->value_tag == IPP_TAG_URI && strcmp(attr->name, "job-uuid")) || | |
2787 | attr->value_tag == IPP_TAG_URISCHEME || | |
2788 | attr->value_tag == IPP_TAG_BEGIN_COLLECTION) /* Not yet supported */ | |
2789 | continue; | |
bd7854cb | 2790 | |
e1d6a774 | 2791 | if (!strncmp(attr->name, "time-", 5)) |
2792 | continue; | |
bd7854cb | 2793 | |
e1d6a774 | 2794 | if (!strncmp(attr->name, "job-", 4) && strcmp(attr->name, "job-uuid") && |
2795 | !(printer->type & CUPS_PRINTER_REMOTE)) | |
2796 | continue; | |
ef416fc2 | 2797 | |
e1d6a774 | 2798 | if (!strncmp(attr->name, "job-", 4) && |
2799 | strcmp(attr->name, "job-uuid") && | |
2800 | strcmp(attr->name, "job-billing") && | |
2801 | strcmp(attr->name, "job-sheets") && | |
2802 | strcmp(attr->name, "job-hold-until") && | |
2803 | strcmp(attr->name, "job-priority")) | |
2804 | continue; | |
ef416fc2 | 2805 | |
e1d6a774 | 2806 | if ((!strcmp(attr->name, "page-label") || |
2807 | !strcmp(attr->name, "page-border") || | |
2808 | !strncmp(attr->name, "number-up", 9) || | |
b94498cf | 2809 | !strcmp(attr->name, "page-ranges") || |
e1d6a774 | 2810 | !strcmp(attr->name, "page-set") || |
2811 | !strcasecmp(attr->name, "AP_FIRSTPAGE_InputSlot") || | |
2812 | !strcasecmp(attr->name, "AP_FIRSTPAGE_ManualFeed")) && | |
2813 | banner_page) | |
2814 | continue; | |
ef416fc2 | 2815 | |
e1d6a774 | 2816 | /* |
2817 | * Otherwise add them to the list... | |
2818 | */ | |
ef416fc2 | 2819 | |
e1d6a774 | 2820 | if (optptr > options) |
2821 | strlcat(optptr, " ", optlength - (optptr - options)); | |
ef416fc2 | 2822 | |
e1d6a774 | 2823 | if (attr->value_tag != IPP_TAG_BOOLEAN) |
2824 | { | |
2825 | strlcat(optptr, attr->name, optlength - (optptr - options)); | |
2826 | strlcat(optptr, "=", optlength - (optptr - options)); | |
2827 | } | |
ef416fc2 | 2828 | |
e1d6a774 | 2829 | for (i = 0; i < attr->num_values; i ++) |
2830 | { | |
2831 | if (i) | |
2832 | strlcat(optptr, ",", optlength - (optptr - options)); | |
ef416fc2 | 2833 | |
e1d6a774 | 2834 | optptr += strlen(optptr); |
ef416fc2 | 2835 | |
e1d6a774 | 2836 | switch (attr->value_tag) |
2837 | { | |
2838 | case IPP_TAG_INTEGER : | |
2839 | case IPP_TAG_ENUM : | |
2840 | snprintf(optptr, optlength - (optptr - options), | |
2841 | "%d", attr->values[i].integer); | |
2842 | break; | |
ef416fc2 | 2843 | |
e1d6a774 | 2844 | case IPP_TAG_BOOLEAN : |
2845 | if (!attr->values[i].boolean) | |
2846 | strlcat(optptr, "no", optlength - (optptr - options)); | |
ef416fc2 | 2847 | |
e1d6a774 | 2848 | case IPP_TAG_NOVALUE : |
2849 | strlcat(optptr, attr->name, | |
2850 | optlength - (optptr - options)); | |
2851 | break; | |
ef416fc2 | 2852 | |
e1d6a774 | 2853 | case IPP_TAG_RANGE : |
2854 | if (attr->values[i].range.lower == attr->values[i].range.upper) | |
2855 | snprintf(optptr, optlength - (optptr - options) - 1, | |
2856 | "%d", attr->values[i].range.lower); | |
2857 | else | |
2858 | snprintf(optptr, optlength - (optptr - options) - 1, | |
2859 | "%d-%d", attr->values[i].range.lower, | |
2860 | attr->values[i].range.upper); | |
2861 | break; | |
ef416fc2 | 2862 | |
e1d6a774 | 2863 | case IPP_TAG_RESOLUTION : |
2864 | snprintf(optptr, optlength - (optptr - options) - 1, | |
2865 | "%dx%d%s", attr->values[i].resolution.xres, | |
2866 | attr->values[i].resolution.yres, | |
2867 | attr->values[i].resolution.units == IPP_RES_PER_INCH ? | |
2868 | "dpi" : "dpc"); | |
2869 | break; | |
ef416fc2 | 2870 | |
e1d6a774 | 2871 | case IPP_TAG_STRING : |
2872 | case IPP_TAG_TEXT : | |
2873 | case IPP_TAG_NAME : | |
2874 | case IPP_TAG_KEYWORD : | |
2875 | case IPP_TAG_CHARSET : | |
2876 | case IPP_TAG_LANGUAGE : | |
2877 | case IPP_TAG_URI : | |
2878 | for (valptr = attr->values[i].string.text; *valptr;) | |
2879 | { | |
2880 | if (strchr(" \t\n\\\'\"", *valptr)) | |
2881 | *optptr++ = '\\'; | |
2882 | *optptr++ = *valptr++; | |
2883 | } | |
2884 | ||
2885 | *optptr = '\0'; | |
2886 | break; | |
2887 | ||
2888 | default : | |
2889 | break; /* anti-compiler-warning-code */ | |
2890 | } | |
2891 | } | |
2892 | ||
2893 | optptr += strlen(optptr); | |
2894 | } | |
2895 | } | |
2896 | ||
2897 | /* | |
2898 | * Build the command-line arguments for the filters. Each filter | |
2899 | * has 6 or 7 arguments: | |
2900 | * | |
2901 | * argv[0] = printer | |
2902 | * argv[1] = job ID | |
2903 | * argv[2] = username | |
2904 | * argv[3] = title | |
2905 | * argv[4] = # copies | |
2906 | * argv[5] = options | |
2907 | * argv[6] = filename (optional; normally stdin) | |
2908 | * | |
2909 | * This allows legacy printer drivers that use the old System V | |
2910 | * printing interface to be used by CUPS. | |
2911 | * | |
2912 | * For remote jobs, we send all of the files in the argument list. | |
2913 | */ | |
2914 | ||
d09495fa | 2915 | if (printer->remote && job->num_files > 1) |
e1d6a774 | 2916 | argv = calloc(7 + job->num_files, sizeof(char *)); |
2917 | else | |
2918 | argv = calloc(8, sizeof(char *)); | |
2919 | ||
2920 | sprintf(jobid, "%d", job->id); | |
2921 | ||
2922 | argv[0] = printer->name; | |
2923 | argv[1] = jobid; | |
2924 | argv[2] = job->username; | |
2925 | argv[3] = title; | |
2926 | argv[4] = copies; | |
2927 | argv[5] = options; | |
2928 | ||
d09495fa | 2929 | if (printer->remote && job->num_files > 1) |
e1d6a774 | 2930 | { |
2931 | for (i = 0; i < job->num_files; i ++) | |
ef416fc2 | 2932 | { |
e1d6a774 | 2933 | snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, |
2934 | job->id, i + 1); | |
2935 | argv[6 + i] = strdup(filename); | |
2936 | } | |
2937 | } | |
2938 | else | |
2939 | { | |
2940 | snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, | |
2941 | job->id, job->current_file + 1); | |
2942 | argv[6] = filename; | |
2943 | } | |
ef416fc2 | 2944 | |
e1d6a774 | 2945 | for (i = 0; argv[i]; i ++) |
2946 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
2947 | "[Job %d] argv[%d]=\"%s\"", job->id, i, argv[i]); | |
ef416fc2 | 2948 | |
e1d6a774 | 2949 | /* |
2950 | * Create environment variable strings for the filters... | |
2951 | */ | |
ef416fc2 | 2952 | |
e1d6a774 | 2953 | attr = ippFindAttribute(job->attrs, "attributes-natural-language", |
2954 | IPP_TAG_LANGUAGE); | |
ef416fc2 | 2955 | |
e1d6a774 | 2956 | switch (strlen(attr->values[0].string.text)) |
2957 | { | |
2958 | default : | |
2959 | /* | |
2960 | * This is an unknown or badly formatted language code; use | |
2961 | * the POSIX locale... | |
2962 | */ | |
ef416fc2 | 2963 | |
e1d6a774 | 2964 | strcpy(lang, "LANG=C"); |
2965 | break; | |
ef416fc2 | 2966 | |
e1d6a774 | 2967 | case 2 : |
2968 | /* | |
2969 | * Just the language code (ll)... | |
2970 | */ | |
ef416fc2 | 2971 | |
e1d6a774 | 2972 | snprintf(lang, sizeof(lang), "LANG=%s", |
2973 | attr->values[0].string.text); | |
2974 | break; | |
ef416fc2 | 2975 | |
e1d6a774 | 2976 | case 5 : |
2977 | /* | |
2978 | * Language and country code (ll-cc)... | |
2979 | */ | |
ef416fc2 | 2980 | |
e1d6a774 | 2981 | snprintf(lang, sizeof(lang), "LANG=%c%c_%c%c", |
2982 | attr->values[0].string.text[0], | |
2983 | attr->values[0].string.text[1], | |
2984 | toupper(attr->values[0].string.text[3] & 255), | |
2985 | toupper(attr->values[0].string.text[4] & 255)); | |
2986 | break; | |
2987 | } | |
ef416fc2 | 2988 | |
e1d6a774 | 2989 | attr = ippFindAttribute(job->attrs, "document-format", |
2990 | IPP_TAG_MIMETYPE); | |
2991 | if (attr != NULL && | |
2992 | (optptr = strstr(attr->values[0].string.text, "charset=")) != NULL) | |
2993 | snprintf(charset, sizeof(charset), "CHARSET=%s", optptr + 8); | |
2994 | else | |
2995 | { | |
2996 | attr = ippFindAttribute(job->attrs, "attributes-charset", | |
2997 | IPP_TAG_CHARSET); | |
2998 | snprintf(charset, sizeof(charset), "CHARSET=%s", | |
2999 | attr->values[0].string.text); | |
ef416fc2 | 3000 | } |
3001 | ||
e1d6a774 | 3002 | snprintf(content_type, sizeof(content_type), "CONTENT_TYPE=%s/%s", |
3003 | job->filetypes[job->current_file]->super, | |
3004 | job->filetypes[job->current_file]->type); | |
3005 | snprintf(device_uri, sizeof(device_uri), "DEVICE_URI=%s", | |
3006 | printer->device_uri); | |
3007 | cupsdSanitizeURI(printer->device_uri, sani_uri, sizeof(sani_uri)); | |
3008 | snprintf(ppd, sizeof(ppd), "PPD=%s/ppd/%s.ppd", ServerRoot, printer->name); | |
3009 | snprintf(printer_name, sizeof(printer_name), "PRINTER=%s", printer->name); | |
3010 | snprintf(rip_max_cache, sizeof(rip_max_cache), "RIP_MAX_CACHE=%s", RIPCache); | |
ef416fc2 | 3011 | |
e1d6a774 | 3012 | envc = cupsdLoadEnv(envp, (int)(sizeof(envp) / sizeof(envp[0]))); |
ef416fc2 | 3013 | |
e1d6a774 | 3014 | envp[envc ++] = charset; |
3015 | envp[envc ++] = lang; | |
3016 | envp[envc ++] = ppd; | |
3017 | envp[envc ++] = rip_max_cache; | |
3018 | envp[envc ++] = content_type; | |
3019 | envp[envc ++] = device_uri; | |
3020 | envp[envc ++] = printer_name; | |
bd7854cb | 3021 | |
f899b121 | 3022 | if (!printer->remote && !printer->raw && |
f42414bf | 3023 | (filter = (mime_filter_t *)cupsArrayLast(filters)) != NULL && |
3024 | filter->dst) | |
e1d6a774 | 3025 | { |
3026 | snprintf(final_content_type, sizeof(final_content_type), | |
3027 | "FINAL_CONTENT_TYPE=%s/%s", | |
d09495fa | 3028 | filter->dst->super, filter->dst->type); |
e1d6a774 | 3029 | envp[envc ++] = final_content_type; |
3030 | } | |
bd7854cb | 3031 | |
e1d6a774 | 3032 | if (Classification && !banner_page) |
3033 | { | |
3034 | if ((attr = ippFindAttribute(job->attrs, "job-sheets", | |
3035 | IPP_TAG_NAME)) == NULL) | |
3036 | snprintf(classification, sizeof(classification), "CLASSIFICATION=%s", | |
3037 | Classification); | |
3038 | else if (attr->num_values > 1 && | |
3039 | strcmp(attr->values[1].string.text, "none") != 0) | |
3040 | snprintf(classification, sizeof(classification), "CLASSIFICATION=%s", | |
3041 | attr->values[1].string.text); | |
3042 | else | |
3043 | snprintf(classification, sizeof(classification), "CLASSIFICATION=%s", | |
3044 | attr->values[0].string.text); | |
bd7854cb | 3045 | |
e1d6a774 | 3046 | envp[envc ++] = classification; |
3047 | } | |
bd7854cb | 3048 | |
e1d6a774 | 3049 | if (job->dtype & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT)) |
bd7854cb | 3050 | { |
e1d6a774 | 3051 | snprintf(class_name, sizeof(class_name), "CLASS=%s", job->dest); |
3052 | envp[envc ++] = class_name; | |
3053 | } | |
bd7854cb | 3054 | |
09a101d6 | 3055 | if (job->auth_username) |
3056 | envp[envc ++] = job->auth_username; | |
3057 | if (job->auth_domain) | |
3058 | envp[envc ++] = job->auth_domain; | |
3059 | if (job->auth_password) | |
3060 | envp[envc ++] = job->auth_password; | |
3061 | ||
f7deaa1a | 3062 | #ifdef HAVE_GSSAPI |
3063 | if (job->ccname) | |
3064 | envp[envc ++] = job->ccname; | |
3065 | #endif /* HAVE_GSSAPI */ | |
3066 | ||
e1d6a774 | 3067 | envp[envc] = NULL; |
bd7854cb | 3068 | |
e1d6a774 | 3069 | for (i = 0; i < envc; i ++) |
09a101d6 | 3070 | if (!strncmp(envp[i], "AUTH_", 5)) |
3071 | cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] envp[%d]=\"AUTH_%c****\"", | |
3072 | job->id, i, envp[i][5]); | |
3073 | else if (strncmp(envp[i], "DEVICE_URI=", 11)) | |
e1d6a774 | 3074 | cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] envp[%d]=\"%s\"", |
3075 | job->id, i, envp[i]); | |
3076 | else | |
3077 | cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] envp[%d]=\"DEVICE_URI=%s\"", | |
3078 | job->id, i, sani_uri); | |
3079 | ||
d09495fa | 3080 | if (printer->remote) |
e1d6a774 | 3081 | job->current_file = job->num_files; |
3082 | else | |
3083 | job->current_file ++; | |
bd7854cb | 3084 | |
3085 | /* | |
e1d6a774 | 3086 | * Now create processes for all of the filters... |
bd7854cb | 3087 | */ |
3088 | ||
e1d6a774 | 3089 | filterfds[0][0] = -1; |
3090 | filterfds[0][1] = -1; | |
3091 | filterfds[1][0] = -1; | |
3092 | filterfds[1][1] = -1; | |
bd7854cb | 3093 | |
89d46774 | 3094 | if (!job->status_buffer) |
bd7854cb | 3095 | { |
89d46774 | 3096 | if (cupsdOpenPipe(job->status_pipes)) |
3097 | { | |
09a101d6 | 3098 | cupsdLogMessage(CUPSD_LOG_ERROR, |
3099 | "[Job %d] Unable to create job status pipes - %s.", | |
3100 | job->id, strerror(errno)); | |
89d46774 | 3101 | snprintf(printer->state_message, sizeof(printer->state_message), |
3102 | "Unable to create status pipes - %s.", strerror(errno)); | |
f7deaa1a | 3103 | |
89d46774 | 3104 | cupsdAddPrinterHistory(printer); |
f7deaa1a | 3105 | |
89d46774 | 3106 | cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job, |
3107 | "Job canceled because the server could not create the job " | |
3108 | "status pipes."); | |
f7deaa1a | 3109 | |
89d46774 | 3110 | goto abort_job; |
3111 | } | |
f7deaa1a | 3112 | |
09a101d6 | 3113 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
3114 | "[Job %d] start_job: status_pipes = [ %d %d ]", | |
3115 | job->id, job->status_pipes[0], job->status_pipes[1]); | |
f7deaa1a | 3116 | |
89d46774 | 3117 | job->status_buffer = cupsdStatBufNew(job->status_pipes[0], "[Job %d]", |
3118 | job->id); | |
09a101d6 | 3119 | job->status_level = CUPSD_LOG_INFO; |
e1d6a774 | 3120 | } |
bd7854cb | 3121 | |
89d46774 | 3122 | job->status = 0; |
e1d6a774 | 3123 | memset(job->filters, 0, sizeof(job->filters)); |
bd7854cb | 3124 | |
e1d6a774 | 3125 | for (i = 0, slot = 0, filter = (mime_filter_t *)cupsArrayFirst(filters); |
3126 | filter; | |
3127 | i ++, filter = (mime_filter_t *)cupsArrayNext(filters)) | |
3128 | { | |
3129 | if (filter->filter[0] != '/') | |
3130 | snprintf(command, sizeof(command), "%s/filter/%s", ServerBin, | |
3131 | filter->filter); | |
3132 | else | |
3133 | strlcpy(command, filter->filter, sizeof(command)); | |
bd7854cb | 3134 | |
e1d6a774 | 3135 | if (i < (cupsArrayCount(filters) - 1)) |
bd7854cb | 3136 | { |
e1d6a774 | 3137 | if (cupsdOpenPipe(filterfds[slot])) |
3138 | { | |
3139 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
09a101d6 | 3140 | "[Job %d] Unable to create job filter pipes - %s.", |
3141 | job->id, strerror(errno)); | |
e1d6a774 | 3142 | snprintf(printer->state_message, sizeof(printer->state_message), |
3143 | "Unable to create filter pipes - %s.", strerror(errno)); | |
3144 | cupsdAddPrinterHistory(printer); | |
bd7854cb | 3145 | |
e1d6a774 | 3146 | cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job, |
3147 | "Job canceled because the server could not create the " | |
3148 | "filter pipes."); | |
bd7854cb | 3149 | |
e1d6a774 | 3150 | goto abort_job; |
bd7854cb | 3151 | } |
e1d6a774 | 3152 | } |
3153 | else | |
3154 | { | |
3155 | if (job->current_file == 1) | |
bd7854cb | 3156 | { |
e1d6a774 | 3157 | if (strncmp(printer->device_uri, "file:", 5) != 0) |
3158 | { | |
3159 | if (cupsdOpenPipe(job->print_pipes)) | |
3160 | { | |
3161 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
09a101d6 | 3162 | "[Job %d] Unable to create job backend pipes - %s.", |
3163 | job->id, strerror(errno)); | |
e1d6a774 | 3164 | snprintf(printer->state_message, sizeof(printer->state_message), |
3165 | "Unable to create backend pipes - %s.", strerror(errno)); | |
3166 | cupsdAddPrinterHistory(printer); | |
bd7854cb | 3167 | |
e1d6a774 | 3168 | cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job, |
3169 | "Job canceled because the server could not create " | |
3170 | "the backend pipes."); | |
bd7854cb | 3171 | |
e1d6a774 | 3172 | goto abort_job; |
3173 | } | |
3174 | } | |
3175 | else | |
3176 | { | |
3177 | job->print_pipes[0] = -1; | |
ed486911 | 3178 | if (!strcmp(printer->device_uri, "file:/dev/null") || |
3179 | !strcmp(printer->device_uri, "file:///dev/null")) | |
3180 | job->print_pipes[1] = -1; | |
e1d6a774 | 3181 | else |
e1d6a774 | 3182 | { |
ed486911 | 3183 | if (!strncmp(printer->device_uri, "file:/dev/", 10)) |
3184 | job->print_pipes[1] = open(printer->device_uri + 5, | |
3185 | O_WRONLY | O_EXCL); | |
3186 | else if (!strncmp(printer->device_uri, "file:///dev/", 12)) | |
3187 | job->print_pipes[1] = open(printer->device_uri + 7, | |
3188 | O_WRONLY | O_EXCL); | |
3189 | else if (!strncmp(printer->device_uri, "file:///", 8)) | |
3190 | job->print_pipes[1] = open(printer->device_uri + 7, | |
3191 | O_WRONLY | O_CREAT | O_TRUNC, 0600); | |
3192 | else | |
3193 | job->print_pipes[1] = open(printer->device_uri + 5, | |
3194 | O_WRONLY | O_CREAT | O_TRUNC, 0600); | |
bd7854cb | 3195 | |
ed486911 | 3196 | if (job->print_pipes[1] < 0) |
3197 | { | |
3198 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
09a101d6 | 3199 | "[Job %d] Unable to open output file \"%s\" - %s.", |
3200 | job->id, printer->device_uri, strerror(errno)); | |
ed486911 | 3201 | snprintf(printer->state_message, sizeof(printer->state_message), |
3202 | "Unable to open output file \"%s\" - %s.", | |
3203 | printer->device_uri, strerror(errno)); | |
3204 | ||
3205 | cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job, | |
3206 | "Job canceled because the server could not open the " | |
3207 | "output file."); | |
3208 | ||
3209 | goto abort_job; | |
3210 | } | |
bd7854cb | 3211 | |
ed486911 | 3212 | fcntl(job->print_pipes[1], F_SETFD, |
3213 | fcntl(job->print_pipes[1], F_GETFD) | FD_CLOEXEC); | |
3214 | } | |
e1d6a774 | 3215 | } |
bd7854cb | 3216 | |
e1d6a774 | 3217 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
09a101d6 | 3218 | "[Job %d] start_job: print_pipes = [ %d %d ]", |
3219 | job->id, job->print_pipes[0], job->print_pipes[1]); | |
bd7854cb | 3220 | } |
e1d6a774 | 3221 | |
3222 | filterfds[slot][0] = job->print_pipes[0]; | |
3223 | filterfds[slot][1] = job->print_pipes[1]; | |
bd7854cb | 3224 | } |
bd7854cb | 3225 | |
09a101d6 | 3226 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "[Job %d] start_job: filter=\"%s\"", |
3227 | job->id, command); | |
e1d6a774 | 3228 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
09a101d6 | 3229 | "[Job %d] start_job: filterfds[%d]=[ %d %d ]", |
3230 | job->id, slot, filterfds[slot][0], filterfds[slot][1]); | |
bd7854cb | 3231 | |
e1d6a774 | 3232 | pid = cupsdStartProcess(command, argv, envp, filterfds[!slot][0], |
89d46774 | 3233 | filterfds[slot][1], job->status_pipes[1], |
f7deaa1a | 3234 | job->back_pipes[0], job->side_pipes[0], 0, |
3235 | job->filters + i); | |
bd7854cb | 3236 | |
e1d6a774 | 3237 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
09a101d6 | 3238 | "[Job %d] start_job: Closing filter pipes for slot %d " |
e1d6a774 | 3239 | "[ %d %d ]...", |
09a101d6 | 3240 | job->id, !slot, filterfds[!slot][0], filterfds[!slot][1]); |
bd7854cb | 3241 | |
e1d6a774 | 3242 | cupsdClosePipe(filterfds[!slot]); |
bd7854cb | 3243 | |
e1d6a774 | 3244 | if (pid == 0) |
3245 | { | |
09a101d6 | 3246 | cupsdLogMessage(CUPSD_LOG_ERROR, |
3247 | "[Job %d] Unable to start filter \"%s\" - %s.", | |
3248 | job->id, filter->filter, strerror(errno)); | |
e1d6a774 | 3249 | snprintf(printer->state_message, sizeof(printer->state_message), |
3250 | "Unable to start filter \"%s\" - %s.", | |
3251 | filter->filter, strerror(errno)); | |
bd7854cb | 3252 | |
e1d6a774 | 3253 | cupsdAddPrinterHistory(printer); |
bd7854cb | 3254 | |
e1d6a774 | 3255 | cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job, |
3256 | "Job canceled because the server could not execute a " | |
3257 | "filter."); | |
bd7854cb | 3258 | |
e1d6a774 | 3259 | goto abort_job; |
3260 | } | |
3261 | ||
09a101d6 | 3262 | cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Started filter %s (PID %d)", |
3263 | job->id, command, pid); | |
e1d6a774 | 3264 | |
3265 | argv[6] = NULL; | |
3266 | slot = !slot; | |
bd7854cb | 3267 | } |
3268 | ||
e1d6a774 | 3269 | cupsArrayDelete(filters); |
bd7854cb | 3270 | |
e1d6a774 | 3271 | /* |
3272 | * Finally, pipe the final output into a backend process if needed... | |
3273 | */ | |
bd7854cb | 3274 | |
e1d6a774 | 3275 | if (strncmp(printer->device_uri, "file:", 5) != 0) |
bd7854cb | 3276 | { |
e1d6a774 | 3277 | if (job->current_file == 1) |
bd7854cb | 3278 | { |
e1d6a774 | 3279 | sscanf(printer->device_uri, "%254[^:]", method); |
3280 | snprintf(command, sizeof(command), "%s/backend/%s", ServerBin, method); | |
bd7854cb | 3281 | |
e1d6a774 | 3282 | /* |
3283 | * See if the backend needs to run as root... | |
3284 | */ | |
bd7854cb | 3285 | |
e1d6a774 | 3286 | if (RunUser) |
3287 | backroot = 0; | |
3288 | else if (stat(command, &backinfo)) | |
3289 | backroot = 0; | |
3290 | else | |
3291 | backroot = !(backinfo.st_mode & (S_IRWXG | S_IRWXO)); | |
bd7854cb | 3292 | |
e1d6a774 | 3293 | argv[0] = sani_uri; |
bd7854cb | 3294 | |
e1d6a774 | 3295 | filterfds[slot][0] = -1; |
ed486911 | 3296 | filterfds[slot][1] = -1; |
bd7854cb | 3297 | |
09a101d6 | 3298 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "[Job %d] start_job: backend=\"%s\"", |
3299 | job->id, command); | |
e1d6a774 | 3300 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
09a101d6 | 3301 | "[Job %d] start_job: filterfds[%d] = [ %d %d ]", job->id, |
e1d6a774 | 3302 | slot, filterfds[slot][0], filterfds[slot][1]); |
bd7854cb | 3303 | |
e1d6a774 | 3304 | pid = cupsdStartProcess(command, argv, envp, filterfds[!slot][0], |
89d46774 | 3305 | filterfds[slot][1], job->status_pipes[1], |
f7deaa1a | 3306 | job->back_pipes[1], job->side_pipes[1], |
3307 | backroot, &(job->backend)); | |
bd7854cb | 3308 | |
e1d6a774 | 3309 | if (pid == 0) |
bd7854cb | 3310 | { |
09a101d6 | 3311 | cupsdLogMessage(CUPSD_LOG_ERROR, |
3312 | "[Job %d] Unable to start backend \"%s\" - %s.", | |
3313 | job->id, method, strerror(errno)); | |
e1d6a774 | 3314 | snprintf(printer->state_message, sizeof(printer->state_message), |
3315 | "Unable to start backend \"%s\" - %s.", method, | |
3316 | strerror(errno)); | |
3317 | ||
3318 | cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job, | |
3319 | "Job canceled because the server could not execute " | |
3320 | "the backend."); | |
3321 | ||
3322 | goto abort_job; | |
3323 | } | |
3324 | else | |
3325 | { | |
09a101d6 | 3326 | cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Started backend %s (PID %d)", |
3327 | job->id, command, pid); | |
bd7854cb | 3328 | } |
e1d6a774 | 3329 | } |
bd7854cb | 3330 | |
e1d6a774 | 3331 | if (job->current_file == job->num_files) |
3332 | { | |
3333 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
09a101d6 | 3334 | "[Job %d] start_job: Closing print pipes [ %d %d ]...", |
3335 | job->id, job->print_pipes[0], job->print_pipes[1]); | |
bd7854cb | 3336 | |
e1d6a774 | 3337 | cupsdClosePipe(job->print_pipes); |
bd7854cb | 3338 | |
e1d6a774 | 3339 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
09a101d6 | 3340 | "[Job %d] start_job: Closing back pipes [ %d %d ]...", |
3341 | job->id, job->back_pipes[0], job->back_pipes[1]); | |
bd7854cb | 3342 | |
e1d6a774 | 3343 | cupsdClosePipe(job->back_pipes); |
89d46774 | 3344 | |
f7deaa1a | 3345 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
09a101d6 | 3346 | "[Job %d] start_job: Closing side pipes [ %d %d ]...", |
3347 | job->id, job->side_pipes[0], job->side_pipes[1]); | |
f7deaa1a | 3348 | |
3349 | cupsdClosePipe(job->side_pipes); | |
3350 | ||
89d46774 | 3351 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
09a101d6 | 3352 | "[Job %d] start_job: Closing status output pipe %d...", |
3353 | job->id, job->status_pipes[1]); | |
89d46774 | 3354 | |
3355 | close(job->status_pipes[1]); | |
3356 | job->status_pipes[1] = -1; | |
e1d6a774 | 3357 | } |
3358 | } | |
3359 | else | |
3360 | { | |
3361 | filterfds[slot][0] = -1; | |
3362 | filterfds[slot][1] = -1; | |
bd7854cb | 3363 | |
e1d6a774 | 3364 | if (job->current_file == job->num_files) |
3365 | { | |
3366 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
09a101d6 | 3367 | "[Job %d] start_job: Closing print pipes [ %d %d ]...", |
3368 | job->id, job->print_pipes[0], job->print_pipes[1]); | |
bd7854cb | 3369 | |
e1d6a774 | 3370 | cupsdClosePipe(job->print_pipes); |
89d46774 | 3371 | |
3372 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
09a101d6 | 3373 | "[Job %d] start_job: Closing status output pipe %d...", |
3374 | job->id, job->status_pipes[1]); | |
89d46774 | 3375 | |
3376 | close(job->status_pipes[1]); | |
3377 | job->status_pipes[1] = -1; | |
bd7854cb | 3378 | } |
e1d6a774 | 3379 | } |
bd7854cb | 3380 | |
89d46774 | 3381 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
09a101d6 | 3382 | "[Job %d] start_job: Closing filter pipes for slot %d " |
89d46774 | 3383 | "[ %d %d ]...", |
09a101d6 | 3384 | job->id, slot, filterfds[slot][0], filterfds[slot][1]); |
89d46774 | 3385 | cupsdClosePipe(filterfds[slot]); |
bd7854cb | 3386 | |
d09495fa | 3387 | if (printer->remote && job->num_files > 1) |
e1d6a774 | 3388 | { |
3389 | for (i = 0; i < job->num_files; i ++) | |
3390 | free(argv[i + 6]); | |
3391 | } | |
bd7854cb | 3392 | |
e1d6a774 | 3393 | free(argv); |
ef416fc2 | 3394 | |
f899b121 | 3395 | cupsdAddSelect(job->status_buffer->fd, (cupsd_selfunc_t)update_job, NULL, |
f7deaa1a | 3396 | job); |
ef416fc2 | 3397 | |
e1d6a774 | 3398 | cupsdAddEvent(CUPSD_EVENT_JOB_STATE, job->printer, job, "Job #%d started.", |
3399 | job->id); | |
ef416fc2 | 3400 | |
e1d6a774 | 3401 | return; |
ef416fc2 | 3402 | |
3403 | ||
3404 | /* | |
e1d6a774 | 3405 | * If we get here, we need to abort the current job and close out all |
3406 | * files and pipes... | |
ef416fc2 | 3407 | */ |
3408 | ||
e1d6a774 | 3409 | abort_job: |
ef416fc2 | 3410 | |
e1d6a774 | 3411 | for (slot = 0; slot < 2; slot ++) |
3412 | { | |
3413 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
09a101d6 | 3414 | "[Job %d] start_job: Closing filter pipes for slot %d " |
e1d6a774 | 3415 | "[ %d %d ]...", |
09a101d6 | 3416 | job->id, slot, filterfds[slot][0], filterfds[slot][1]); |
e1d6a774 | 3417 | cupsdClosePipe(filterfds[slot]); |
3418 | } | |
ef416fc2 | 3419 | |
e1d6a774 | 3420 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
09a101d6 | 3421 | "[Job %d] start_job: Closing status pipes [ %d %d ]...", |
3422 | job->id, job->status_pipes[0], job->status_pipes[1]); | |
89d46774 | 3423 | cupsdClosePipe(job->status_pipes); |
3424 | cupsdStatBufDelete(job->status_buffer); | |
ef416fc2 | 3425 | |
b94498cf | 3426 | job->status_buffer = NULL; |
3427 | ||
e1d6a774 | 3428 | cupsArrayDelete(filters); |
ef416fc2 | 3429 | |
d09495fa | 3430 | if (printer->remote && job->num_files > 1) |
e1d6a774 | 3431 | { |
3432 | for (i = 0; i < job->num_files; i ++) | |
3433 | free(argv[i + 6]); | |
3434 | } | |
ef416fc2 | 3435 | |
e1d6a774 | 3436 | free(argv); |
ef416fc2 | 3437 | |
e1d6a774 | 3438 | cupsdStopJob(job, 0); |
3439 | } | |
ef416fc2 | 3440 | |
e1d6a774 | 3441 | |
3442 | /* | |
3443 | * 'unload_job()' - Unload a job from memory. | |
3444 | */ | |
3445 | ||
3446 | static void | |
3447 | unload_job(cupsd_job_t *job) /* I - Job */ | |
3448 | { | |
3449 | if (!job->attrs) | |
3450 | return; | |
3451 | ||
09a101d6 | 3452 | cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] Unloading...", job->id); |
e1d6a774 | 3453 | |
3454 | ippDelete(job->attrs); | |
3455 | ||
3456 | job->attrs = NULL; | |
3457 | job->state = NULL; | |
3458 | job->sheets = NULL; | |
3459 | job->job_sheets = NULL; | |
ef416fc2 | 3460 | } |
3461 | ||
3462 | ||
3463 | /* | |
f899b121 | 3464 | * 'update_job()' - Read a status update from a job's filters. |
3465 | */ | |
3466 | ||
3467 | void | |
09a101d6 | 3468 | update_job(cupsd_job_t *job) /* I - Job to check */ |
f899b121 | 3469 | { |
3470 | int i; /* Looping var */ | |
3471 | int copies; /* Number of copies printed */ | |
3472 | char message[1024], /* Message text */ | |
3473 | *ptr; /* Pointer update... */ | |
3474 | int loglevel, /* Log level for message */ | |
3475 | event = 0; /* Events? */ | |
3476 | ||
3477 | ||
3478 | while ((ptr = cupsdStatBufUpdate(job->status_buffer, &loglevel, | |
3479 | message, sizeof(message))) != NULL) | |
3480 | { | |
3481 | /* | |
3482 | * Process page and printer state messages as needed... | |
3483 | */ | |
3484 | ||
3485 | if (loglevel == CUPSD_LOG_PAGE) | |
3486 | { | |
3487 | /* | |
3488 | * Page message; send the message to the page_log file and update the | |
3489 | * job sheet count... | |
3490 | */ | |
3491 | ||
3492 | if (job->sheets != NULL) | |
3493 | { | |
3494 | if (!strncasecmp(message, "total ", 6)) | |
3495 | { | |
3496 | /* | |
3497 | * Got a total count of pages from a backend or filter... | |
3498 | */ | |
3499 | ||
3500 | copies = atoi(message + 6); | |
3501 | copies -= job->sheets->values[0].integer; /* Just track the delta */ | |
3502 | } | |
3503 | else if (!sscanf(message, "%*d%d", &copies)) | |
3504 | copies = 1; | |
3505 | ||
3506 | job->sheets->values[0].integer += copies; | |
3507 | ||
3508 | if (job->printer->page_limit) | |
3509 | { | |
3510 | cupsd_quota_t *q = cupsdUpdateQuota(job->printer, job->username, | |
3511 | copies, 0); | |
3512 | ||
3513 | #ifdef __APPLE__ | |
3514 | if (AppleQuotas && q->page_count == -3) | |
3515 | { | |
3516 | /* | |
3517 | * Quota limit exceeded, cancel job in progress immediately... | |
3518 | */ | |
3519 | ||
3520 | cupsdLogMessage(CUPSD_LOG_INFO, | |
09a101d6 | 3521 | "[Job %d] Canceled because pages exceed user %s " |
3522 | "quota limit on printer %s (%s).", | |
f899b121 | 3523 | job->id, job->username, job->printer->name, |
3524 | job->printer->info); | |
3525 | ||
3526 | cupsdCancelJob(job, 1, IPP_JOB_CANCELED); | |
3527 | return; | |
3528 | } | |
3529 | #else | |
3530 | (void)q; | |
3531 | #endif /* __APPLE__ */ | |
3532 | } | |
3533 | } | |
3534 | ||
3535 | cupsdLogPage(job, message); | |
3536 | ||
3537 | cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS, job->printer, job, | |
3538 | "Printed %d page(s).", job->sheets->values[0].integer); | |
3539 | } | |
3540 | else if (loglevel == CUPSD_LOG_STATE) | |
3541 | { | |
09a101d6 | 3542 | if (!strcmp(message, "paused")) |
3543 | cupsdStopPrinter(job->printer, 1); | |
3544 | else | |
3545 | { | |
3546 | cupsdSetPrinterReasons(job->printer, message); | |
3547 | cupsdAddPrinterHistory(job->printer); | |
3548 | event |= CUPSD_EVENT_PRINTER_STATE_CHANGED; | |
3549 | } | |
bc44d920 | 3550 | |
3551 | update_job_attrs(job); | |
f899b121 | 3552 | } |
3553 | else if (loglevel == CUPSD_LOG_ATTR) | |
3554 | { | |
3555 | /* | |
3556 | * Set attribute(s)... | |
3557 | */ | |
3558 | ||
3559 | int num_attrs; /* Number of attributes */ | |
3560 | cups_option_t *attrs; /* Attributes */ | |
3561 | const char *attr; /* Attribute */ | |
3562 | ||
3563 | ||
3564 | num_attrs = cupsParseOptions(message, 0, &attrs); | |
3565 | ||
3566 | if ((attr = cupsGetOption("auth-info-required", num_attrs, | |
3567 | attrs)) != NULL) | |
3568 | cupsdSetAuthInfoRequired(job->printer, attr, NULL); | |
3569 | ||
323c5de1 | 3570 | if ((attr = cupsGetOption("printer-alert", num_attrs, attrs)) != NULL) |
3571 | { | |
3572 | cupsdSetString(&job->printer->alert, attr); | |
3573 | event |= CUPSD_EVENT_PRINTER_STATE_CHANGED; | |
3574 | } | |
3575 | ||
3576 | if ((attr = cupsGetOption("printer-alert-description", num_attrs, | |
3577 | attrs)) != NULL) | |
3578 | { | |
3579 | cupsdSetString(&job->printer->alert_description, attr); | |
3580 | event |= CUPSD_EVENT_PRINTER_STATE_CHANGED; | |
3581 | } | |
3582 | ||
f899b121 | 3583 | cupsFreeOptions(num_attrs, attrs); |
3584 | } | |
3585 | #ifdef __APPLE__ | |
3586 | else if (!strncmp(message, "recoverable:", 12)) | |
3587 | { | |
3588 | cupsdSetPrinterReasons(job->printer, | |
3589 | "+com.apple.print.recoverable-warning"); | |
3590 | ||
3591 | ptr = message + 12; | |
3592 | while (isspace(*ptr & 255)) | |
3593 | ptr ++; | |
3594 | ||
3595 | cupsdSetString(&job->printer->recoverable, ptr); | |
3596 | cupsdAddPrinterHistory(job->printer); | |
3597 | event |= CUPSD_EVENT_PRINTER_STATE_CHANGED; | |
3598 | } | |
3599 | else if (!strncmp(message, "recovered:", 10)) | |
3600 | { | |
3601 | cupsdSetPrinterReasons(job->printer, | |
3602 | "-com.apple.print.recoverable-warning"); | |
3603 | ||
3604 | ptr = message + 10; | |
3605 | while (isspace(*ptr & 255)) | |
3606 | ptr ++; | |
3607 | ||
3608 | cupsdSetString(&job->printer->recoverable, ptr); | |
3609 | cupsdAddPrinterHistory(job->printer); | |
3610 | event |= CUPSD_EVENT_PRINTER_STATE_CHANGED; | |
3611 | } | |
3612 | #endif /* __APPLE__ */ | |
09a101d6 | 3613 | else if (loglevel <= job->status_level) |
f899b121 | 3614 | { |
3615 | /* | |
3616 | * Some message to show in the printer-state-message attribute... | |
3617 | */ | |
3618 | ||
09a101d6 | 3619 | job->status_level = loglevel; |
3620 | ||
f899b121 | 3621 | strlcpy(job->printer->state_message, message, |
3622 | sizeof(job->printer->state_message)); | |
3623 | cupsdAddPrinterHistory(job->printer); | |
3624 | event |= CUPSD_EVENT_PRINTER_STATE_CHANGED; | |
bc44d920 | 3625 | |
3626 | update_job_attrs(job); | |
f899b121 | 3627 | } |
3628 | ||
3629 | if (!strchr(job->status_buffer->buffer, '\n')) | |
3630 | break; | |
3631 | } | |
3632 | ||
3633 | if ((event & CUPSD_EVENT_PRINTER_STATE_CHANGED)) | |
3634 | cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE_CHANGED, job->printer, NULL, | |
3635 | (job->printer->type & CUPS_PRINTER_CLASS) ? | |
3636 | "Class \"%s\" state changed." : | |
3637 | "Printer \"%s\" state changed.", | |
3638 | job->printer->name); | |
3639 | ||
3640 | if (ptr == NULL && !job->status_buffer->bufused) | |
3641 | { | |
3642 | /* | |
3643 | * See if all of the filters and the backend have returned their | |
3644 | * exit statuses. | |
3645 | */ | |
3646 | ||
3647 | for (i = 0; job->filters[i] < 0; i ++); | |
3648 | ||
3649 | if (job->filters[i]) | |
3650 | return; | |
3651 | ||
3652 | if (job->current_file >= job->num_files && job->backend > 0) | |
3653 | return; | |
3654 | ||
3655 | /* | |
3656 | * Handle the end of job stuff... | |
3657 | */ | |
3658 | ||
3659 | cupsdFinishJob(job); | |
3660 | } | |
3661 | } | |
3662 | ||
3663 | ||
3664 | /* | |
bc44d920 | 3665 | * 'update_job_attrs()' - Update the job-printer-* attributes. |
3666 | */ | |
3667 | ||
3668 | void | |
3669 | update_job_attrs(cupsd_job_t *job) /* I - Job to update */ | |
3670 | { | |
3671 | int i; /* Looping var */ | |
3672 | int num_reasons; /* Actual number of reasons */ | |
3673 | const char * const *reasons; /* Reasons */ | |
3674 | static const char *none = "none", /* "none" */ | |
3675 | *paused = "paused"; | |
3676 | /* "paused" */ | |
3677 | ||
3678 | ||
3679 | /* | |
3680 | * Get/create the job-printer-state-* attributes... | |
3681 | */ | |
3682 | ||
3683 | if (!job->printer_message) | |
3684 | { | |
3685 | if ((job->printer_message = ippFindAttribute(job->attrs, | |
3686 | "job-printer-state-message", | |
3687 | IPP_TAG_TEXT)) == NULL) | |
3688 | job->printer_message = ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_TEXT, | |
3689 | "job-printer-state-message", | |
3690 | NULL, ""); | |
3691 | } | |
3692 | ||
3693 | if (!job->printer_reasons) | |
3694 | job->printer_reasons = ippFindAttribute(job->attrs, | |
3695 | "job-printer-state-reasons", | |
3696 | IPP_TAG_KEYWORD); | |
3697 | ||
3698 | /* | |
3699 | * If the job isn't printing, return now... | |
3700 | */ | |
3701 | ||
3702 | if (!job->printer) | |
3703 | return; | |
3704 | ||
3705 | /* | |
3706 | * Otherwise copy the printer-state-message value... | |
3707 | */ | |
3708 | ||
3709 | if (job->printer->state_message[0]) | |
3710 | cupsdSetString(&(job->printer_message->values[0].string.text), | |
3711 | job->printer->state_message); | |
3712 | ||
3713 | /* | |
3714 | * ... and the printer-state-reasons value... | |
3715 | */ | |
3716 | ||
3717 | if (job->printer->num_reasons == 0) | |
3718 | { | |
3719 | num_reasons = 1; | |
3720 | reasons = job->printer->state == IPP_PRINTER_STOPPED ? &paused : &none; | |
3721 | } | |
3722 | else | |
3723 | { | |
3724 | num_reasons = job->printer->num_reasons; | |
3725 | reasons = (const char * const *)job->printer->reasons; | |
3726 | } | |
3727 | ||
3728 | if (!job->printer_reasons || job->printer_reasons->num_values != num_reasons) | |
3729 | { | |
3730 | ippDeleteAttribute(job->attrs, job->printer_reasons); | |
3731 | ||
3732 | job->printer_reasons = ippAddStrings(job->attrs, | |
3733 | IPP_TAG_JOB, IPP_TAG_KEYWORD, | |
3734 | "job-printer-state-reasons", | |
3735 | num_reasons, NULL, NULL); | |
3736 | } | |
3737 | ||
3738 | for (i = 0; i < num_reasons; i ++) | |
3739 | cupsdSetString(&(job->printer_reasons->values[i].string.text), reasons[i]); | |
3740 | } | |
3741 | ||
3742 | ||
3743 | /* | |
3744 | * End of "$Id: job.c 6671 2007-07-13 23:35:24Z mike $". | |
ef416fc2 | 3745 | */ |