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