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