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