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