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