]> git.ipfire.org Git - thirdparty/cups.git/blame - scheduler/job.c
Merge changes from CUPS 1.4svn-r7874.
[thirdparty/cups.git] / scheduler / job.c
CommitLineData
ef416fc2 1/*
75bd9771 2 * "$Id: job.c 7682 2008-06-21 00:06:02Z 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 */
2480 sani_uri[1024], /* Sanitized DEVICE_URI env var */
2481 filename[1024], /* Job filename */
2482 command[1024], /* Full path to command */
2483 jobid[255], /* Job ID string */
2484 title[IPP_MAX_NAME],
2485 /* Job title string */
2486 copies[255], /* # copies string */
0a682745 2487 *envp[MAX_ENV + 16],
e1d6a774 2488 /* Environment variables */
2489 charset[255], /* CHARSET env variable */
2490 class_name[255],/* CLASS env variable */
2491 classification[1024],
2492 /* CLASSIFICATION env variable */
2493 content_type[1024],
2494 /* CONTENT_TYPE env variable */
2495 device_uri[1024],
2496 /* DEVICE_URI env variable */
2497 final_content_type[1024],
2498 /* FINAL_CONTENT_TYPE env variable */
2499 lang[255], /* LANG env variable */
0a682745
MS
2500#ifdef __APPLE__
2501 apple_language[255],
2502 /* APPLE_LANGUAGE env variable */
2503#endif /* __APPLE__ */
e1d6a774 2504 ppd[1024], /* PPD env variable */
2505 printer_name[255],
2506 /* PRINTER env variable */
2507 rip_max_cache[255];
2508 /* RIP_MAX_CACHE env variable */
e1d6a774 2509 static char *options = NULL;/* Full list of options */
2510 static int optlength = 0; /* Length of option buffer */
ef416fc2 2511
2512
75bd9771
MS
2513 cupsdLogJob(job, CUPSD_LOG_DEBUG2, "start_job: file = %d/%d",
2514 job->current_file, job->num_files);
ef416fc2 2515
e1d6a774 2516 if (job->num_files == 0)
2517 {
75bd9771 2518 cupsdLogJob(job, CUPSD_LOG_ERROR, "No files, canceling job!");
07725fee 2519 cupsdCancelJob(job, 0, IPP_JOB_ABORTED);
ef416fc2 2520 return;
e1d6a774 2521 }
ef416fc2 2522
e1d6a774 2523 /*
2524 * Figure out what filters are required to convert from
2525 * the source to the destination type...
2526 */
ef416fc2 2527
e1d6a774 2528 filters = NULL;
2529 job->cost = 0;
ef416fc2 2530
e1d6a774 2531 if (printer->raw)
2532 {
2533 /*
2534 * Remote jobs and raw queues go directly to the printer without
2535 * filtering...
2536 */
ef416fc2 2537
75bd9771 2538 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Sending job to queue tagged as raw...");
ef416fc2 2539
e1d6a774 2540 filters = NULL;
2541 }
2542 else
2543 {
2544 /*
2545 * Local jobs get filtered...
2546 */
ef416fc2 2547
e1d6a774 2548 filters = mimeFilter(MimeDatabase, job->filetypes[job->current_file],
2549 printer->filetype, &(job->cost));
2550
2551 if (!filters)
ef416fc2 2552 {
75bd9771
MS
2553 cupsdLogJob(job, CUPSD_LOG_ERROR,
2554 "Unable to convert file %d to printable format!",
2555 job->current_file);
e1d6a774 2556 cupsdLogMessage(CUPSD_LOG_INFO,
d9bca400 2557 "Hint: Do you have Ghostscript installed?");
ef416fc2 2558
e1d6a774 2559 if (LogLevel < CUPSD_LOG_DEBUG)
2560 cupsdLogMessage(CUPSD_LOG_INFO,
2561 "Hint: Try setting the LogLevel to \"debug\".");
ef416fc2 2562
e1d6a774 2563 job->current_file ++;
ef416fc2 2564
e1d6a774 2565 if (job->current_file == job->num_files)
07725fee 2566 cupsdCancelJob(job, 0, IPP_JOB_ABORTED);
ef416fc2 2567
e1d6a774 2568 return;
2569 }
ef416fc2 2570
ef416fc2 2571 /*
e1d6a774 2572 * Remove NULL ("-") filters...
ef416fc2 2573 */
2574
e1d6a774 2575 for (filter = (mime_filter_t *)cupsArrayFirst(filters);
2576 filter;
2577 filter = (mime_filter_t *)cupsArrayNext(filters))
2578 if (!strcmp(filter->filter, "-"))
2579 cupsArrayRemove(filters, filter);
ef416fc2 2580
e1d6a774 2581 if (cupsArrayCount(filters) == 0)
2582 {
2583 cupsArrayDelete(filters);
2584 filters = NULL;
2585 }
f7deaa1a 2586
2587 /*
2588 * If this printer has any pre-filters, insert the required pre-filter
2589 * in the filters array...
2590 */
2591
2592 if (printer->prefiltertype && filters)
2593 {
2594 prefilters = cupsArrayNew(NULL, NULL);
2595
2596 for (filter = (mime_filter_t *)cupsArrayFirst(filters);
2597 filter;
2598 filter = (mime_filter_t *)cupsArrayNext(filters))
2599 {
2600 if ((prefilter = mimeFilterLookup(MimeDatabase, filter->src,
2601 printer->prefiltertype)))
2602 {
2603 cupsArrayAdd(prefilters, prefilter);
2604 job->cost += prefilter->cost;
2605 }
2606
2607 cupsArrayAdd(prefilters, filter);
2608 }
2609
2610 cupsArrayDelete(filters);
2611 filters = prefilters;
2612 }
e1d6a774 2613 }
ef416fc2 2614
e1d6a774 2615 /*
2616 * Set a minimum cost of 100 for all jobs so that FilterLimit
2617 * works with raw queues and other low-cost paths.
2618 */
ef416fc2 2619
e1d6a774 2620 if (job->cost < 100)
2621 job->cost = 100;
ef416fc2 2622
e1d6a774 2623 /*
2624 * See if the filter cost is too high...
2625 */
ef416fc2 2626
e1d6a774 2627 if ((FilterLevel + job->cost) > FilterLimit && FilterLevel > 0 &&
2628 FilterLimit > 0)
2629 {
2630 /*
2631 * Don't print this job quite yet...
2632 */
bd7854cb 2633
e1d6a774 2634 cupsArrayDelete(filters);
bd7854cb 2635
75bd9771
MS
2636 cupsdLogJob(job, CUPSD_LOG_INFO,
2637 "Holding because filter limit has been reached.");
2638 cupsdLogJob(job, CUPSD_LOG_DEBUG2,
2639 "start_job: file=%d, cost=%d, level=%d, limit=%d",
2640 job->current_file, job->cost, FilterLevel,
2641 FilterLimit);
e1d6a774 2642 return;
2643 }
bd7854cb 2644
e1d6a774 2645 FilterLevel += job->cost;
bd7854cb 2646
e1d6a774 2647 /*
f899b121 2648 * Add decompression/raw filter as needed...
e1d6a774 2649 */
bd7854cb 2650
f899b121 2651 if ((!printer->raw && job->compressions[job->current_file]) ||
2652 (!filters && !printer->remote &&
2653 (job->num_files > 1 || !strncmp(printer->device_uri, "file:", 5))))
e1d6a774 2654 {
2655 /*
2656 * Add gziptoany filter to the front of the list...
2657 */
bd7854cb 2658
2abf387c 2659 if (!filters)
2660 filters = cupsArrayNew(NULL, NULL);
2661
e1d6a774 2662 if (!cupsArrayInsert(filters, &gziptoany_filter))
2663 {
75bd9771
MS
2664 cupsdLogJob(job, CUPSD_LOG_ERROR,
2665 "Unable to add decompression filter - %s", strerror(errno));
bd7854cb 2666
e1d6a774 2667 cupsArrayDelete(filters);
bd7854cb 2668
e1d6a774 2669 job->current_file ++;
bd7854cb 2670
e1d6a774 2671 if (job->current_file == job->num_files)
07725fee 2672 cupsdCancelJob(job, 0, IPP_JOB_ABORTED);
ef416fc2 2673
e1d6a774 2674 return;
2675 }
2676 }
ef416fc2 2677
e1d6a774 2678 /*
2679 * Add port monitor, if any...
2680 */
ef416fc2 2681
e1d6a774 2682 if (printer->port_monitor)
ef416fc2 2683 {
2684 /*
e1d6a774 2685 * Add port monitor to the end of the list...
ef416fc2 2686 */
2687
2abf387c 2688 if (!filters)
2689 filters = cupsArrayNew(NULL, NULL);
2690
db0bd74a
MS
2691 port_monitor.src = NULL;
2692 port_monitor.dst = NULL;
2693 port_monitor.cost = 0;
2694
2695 snprintf(port_monitor.filter, sizeof(port_monitor.filter),
2696 "%s/monitor/%s", ServerBin, printer->port_monitor);
2697
e1d6a774 2698 if (!cupsArrayAdd(filters, &port_monitor))
ef416fc2 2699 {
75bd9771
MS
2700 cupsdLogJob(job, CUPSD_LOG_ERROR,
2701 "Unable to add port monitor - %s", strerror(errno));
ef416fc2 2702
e1d6a774 2703 cupsArrayDelete(filters);
ef416fc2 2704
e1d6a774 2705 job->current_file ++;
ef416fc2 2706
e1d6a774 2707 if (job->current_file == job->num_files)
07725fee 2708 cupsdCancelJob(job, 0, IPP_JOB_ABORTED);
ef416fc2 2709
e1d6a774 2710 return;
e00b005a 2711 }
e1d6a774 2712 }
e00b005a 2713
3dfe78b3
MS
2714 /*
2715 * Make sure we don't go over the "MAX_FILTERS" limit...
2716 */
2717
2718 if (cupsArrayCount(filters) > MAX_FILTERS)
2719 {
75bd9771
MS
2720 cupsdLogJob(job, CUPSD_LOG_ERROR,
2721 "Too many filters (%d > %d), unable to print!",
2722 cupsArrayCount(filters), MAX_FILTERS);
3dfe78b3
MS
2723
2724 cupsArrayDelete(filters);
2725 cupsdCancelJob(job, 0, IPP_JOB_STOPPED);
2726
2727 return;
2728 }
2729
e1d6a774 2730 /*
2731 * Update the printer and job state to "processing"...
2732 */
ef416fc2 2733
e1d6a774 2734 job->state->values[0].integer = IPP_JOB_PROCESSING;
2735 job->state_value = IPP_JOB_PROCESSING;
f899b121 2736
e1d6a774 2737 job->status = 0;
2738 job->printer = printer;
2739 printer->job = job;
f899b121 2740
e1d6a774 2741 cupsdSetPrinterState(printer, IPP_PRINTER_PROCESSING, 0);
ef416fc2 2742
e1d6a774 2743 if (job->current_file == 0)
ef416fc2 2744 {
3dfe78b3
MS
2745 /*
2746 * Add to the printing list...
2747 */
2748
2749 cupsArrayAdd(PrintingJobs, job);
2750 cupsdSetBusyState();
2751
f301802f 2752 /*
2753 * Set the processing time...
2754 */
2755
e1d6a774 2756 set_time(job, "time-at-processing");
f301802f 2757
2758 /*
2759 * Create the backchannel pipes and make them non-blocking...
2760 */
2761
e1d6a774 2762 cupsdOpenPipe(job->back_pipes);
f301802f 2763
2764 fcntl(job->back_pipes[0], F_SETFL,
2765 fcntl(job->back_pipes[0], F_GETFL) | O_NONBLOCK);
2766
2767 fcntl(job->back_pipes[1], F_SETFL,
2768 fcntl(job->back_pipes[1], F_GETFL) | O_NONBLOCK);
f7deaa1a 2769
2770 /*
2771 * Create the side-channel pipes and make them non-blocking...
2772 */
2773
2774 socketpair(AF_LOCAL, SOCK_STREAM, 0, job->side_pipes);
2775
2776 fcntl(job->side_pipes[0], F_SETFL,
2777 fcntl(job->side_pipes[0], F_GETFL) | O_NONBLOCK);
2778
2779 fcntl(job->side_pipes[1], F_SETFL,
2780 fcntl(job->side_pipes[1], F_GETFL) | O_NONBLOCK);
e1d6a774 2781 }
ef416fc2 2782
e1d6a774 2783 /*
2784 * Determine if we are printing a banner page or not...
2785 */
ef416fc2 2786
e1d6a774 2787 if (job->job_sheets == NULL)
2788 {
75bd9771 2789 cupsdLogJob(job, CUPSD_LOG_DEBUG, "No job-sheets attribute.");
e1d6a774 2790 if ((job->job_sheets =
2791 ippFindAttribute(job->attrs, "job-sheets", IPP_TAG_ZERO)) != NULL)
75bd9771
MS
2792 cupsdLogJob(job, CUPSD_LOG_DEBUG,
2793 "... but someone added one without setting job_sheets!");
e1d6a774 2794 }
2795 else if (job->job_sheets->num_values == 1)
75bd9771
MS
2796 cupsdLogJob(job, CUPSD_LOG_DEBUG, "job-sheets=%s",
2797 job->job_sheets->values[0].string.text);
e1d6a774 2798 else
75bd9771
MS
2799 cupsdLogJob(job, CUPSD_LOG_DEBUG, "job-sheets=%s,%s",
2800 job->job_sheets->values[0].string.text,
2801 job->job_sheets->values[1].string.text);
ef416fc2 2802
e1d6a774 2803 if (printer->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT))
2804 banner_page = 0;
2805 else if (job->job_sheets == NULL)
2806 banner_page = 0;
2807 else if (strcasecmp(job->job_sheets->values[0].string.text, "none") != 0 &&
2808 job->current_file == 0)
2809 banner_page = 1;
2810 else if (job->job_sheets->num_values > 1 &&
2811 strcasecmp(job->job_sheets->values[1].string.text, "none") != 0 &&
2812 job->current_file == (job->num_files - 1))
2813 banner_page = 1;
2814 else
2815 banner_page = 0;
ef416fc2 2816
75bd9771 2817 cupsdLogJob(job, CUPSD_LOG_DEBUG, "banner_page = %d", banner_page);
ef416fc2 2818
e1d6a774 2819 /*
2820 * Building the options string is harder than it needs to be, but
2821 * for the moment we need to pass strings for command-line args and
2822 * not IPP attribute pointers... :)
2823 *
2824 * First allocate/reallocate the option buffer as needed...
2825 */
ef416fc2 2826
e1d6a774 2827 i = ipp_length(job->attrs);
ef416fc2 2828
1f0275e3 2829 if (i > optlength || !options)
e1d6a774 2830 {
2831 if (optlength == 0)
2832 optptr = malloc(i);
2833 else
2834 optptr = realloc(options, i);
ef416fc2 2835
e1d6a774 2836 if (optptr == NULL)
2837 {
75bd9771
MS
2838 cupsdLogJob(job, CUPSD_LOG_CRIT,
2839 "Unable to allocate %d bytes for option buffer!", i);
ef416fc2 2840
e1d6a774 2841 cupsArrayDelete(filters);
ef416fc2 2842
e1d6a774 2843 FilterLevel -= job->cost;
ef416fc2 2844
07725fee 2845 cupsdCancelJob(job, 0, IPP_JOB_ABORTED);
e1d6a774 2846 return;
2847 }
ef416fc2 2848
e1d6a774 2849 options = optptr;
2850 optlength = i;
2851 }
ef416fc2 2852
e1d6a774 2853 /*
2854 * Now loop through the attributes and convert them to the textual
2855 * representation used by the filters...
2856 */
ef416fc2 2857
e1d6a774 2858 optptr = options;
2859 *optptr = '\0';
bd7854cb 2860
e1d6a774 2861 snprintf(title, sizeof(title), "%s-%d", printer->name, job->id);
2862 strcpy(copies, "1");
bd7854cb 2863
e1d6a774 2864 for (attr = job->attrs->attrs; attr != NULL; attr = attr->next)
bd7854cb 2865 {
e1d6a774 2866 if (!strcmp(attr->name, "copies") &&
2867 attr->value_tag == IPP_TAG_INTEGER)
2868 {
2869 /*
2870 * Don't use the # copies attribute if we are printing the job sheets...
2871 */
bd7854cb 2872
e1d6a774 2873 if (!banner_page)
2874 sprintf(copies, "%d", attr->values[0].integer);
2875 }
2876 else if (!strcmp(attr->name, "job-name") &&
2877 (attr->value_tag == IPP_TAG_NAME ||
2878 attr->value_tag == IPP_TAG_NAMELANG))
2879 strlcpy(title, attr->values[0].string.text, sizeof(title));
2880 else if (attr->group_tag == IPP_TAG_JOB)
2881 {
2882 /*
2883 * Filter out other unwanted attributes...
2884 */
bd7854cb 2885
e1d6a774 2886 if (attr->value_tag == IPP_TAG_MIMETYPE ||
2887 attr->value_tag == IPP_TAG_NAMELANG ||
2888 attr->value_tag == IPP_TAG_TEXTLANG ||
2889 (attr->value_tag == IPP_TAG_URI && strcmp(attr->name, "job-uuid")) ||
2890 attr->value_tag == IPP_TAG_URISCHEME ||
2891 attr->value_tag == IPP_TAG_BEGIN_COLLECTION) /* Not yet supported */
2892 continue;
bd7854cb 2893
e1d6a774 2894 if (!strncmp(attr->name, "time-", 5))
2895 continue;
bd7854cb 2896
e1d6a774 2897 if (!strncmp(attr->name, "job-", 4) && strcmp(attr->name, "job-uuid") &&
5d6412a9
MS
2898 strcmp(attr->name, "job-impressions") &&
2899 strcmp(attr->name, "job-originating-host-name") &&
e1d6a774 2900 !(printer->type & CUPS_PRINTER_REMOTE))
2901 continue;
ef416fc2 2902
e1d6a774 2903 if (!strncmp(attr->name, "job-", 4) &&
2904 strcmp(attr->name, "job-uuid") &&
2905 strcmp(attr->name, "job-billing") &&
5d6412a9 2906 strcmp(attr->name, "job-impressions") &&
e1d6a774 2907 strcmp(attr->name, "job-sheets") &&
2908 strcmp(attr->name, "job-hold-until") &&
2909 strcmp(attr->name, "job-priority"))
2910 continue;
ef416fc2 2911
5d6412a9
MS
2912 if ((!strcmp(attr->name, "job-impressions") ||
2913 !strcmp(attr->name, "page-label") ||
e1d6a774 2914 !strcmp(attr->name, "page-border") ||
2915 !strncmp(attr->name, "number-up", 9) ||
b94498cf 2916 !strcmp(attr->name, "page-ranges") ||
e1d6a774 2917 !strcmp(attr->name, "page-set") ||
2918 !strcasecmp(attr->name, "AP_FIRSTPAGE_InputSlot") ||
db1f069b
MS
2919 !strcasecmp(attr->name, "AP_FIRSTPAGE_ManualFeed") ||
2920 !strcasecmp(attr->name, "com.apple.print.PrintSettings."
2921 "PMTotalSidesImaged..n.") ||
2922 !strcasecmp(attr->name, "com.apple.print.PrintSettings."
2923 "PMTotalBeginPages..n.")) &&
e1d6a774 2924 banner_page)
2925 continue;
ef416fc2 2926
e1d6a774 2927 /*
2928 * Otherwise add them to the list...
2929 */
ef416fc2 2930
e1d6a774 2931 if (optptr > options)
2932 strlcat(optptr, " ", optlength - (optptr - options));
ef416fc2 2933
e1d6a774 2934 if (attr->value_tag != IPP_TAG_BOOLEAN)
2935 {
2936 strlcat(optptr, attr->name, optlength - (optptr - options));
2937 strlcat(optptr, "=", optlength - (optptr - options));
2938 }
ef416fc2 2939
e1d6a774 2940 for (i = 0; i < attr->num_values; i ++)
2941 {
2942 if (i)
2943 strlcat(optptr, ",", optlength - (optptr - options));
ef416fc2 2944
e1d6a774 2945 optptr += strlen(optptr);
ef416fc2 2946
e1d6a774 2947 switch (attr->value_tag)
2948 {
2949 case IPP_TAG_INTEGER :
2950 case IPP_TAG_ENUM :
2951 snprintf(optptr, optlength - (optptr - options),
2952 "%d", attr->values[i].integer);
2953 break;
ef416fc2 2954
e1d6a774 2955 case IPP_TAG_BOOLEAN :
2956 if (!attr->values[i].boolean)
2957 strlcat(optptr, "no", optlength - (optptr - options));
ef416fc2 2958
e1d6a774 2959 case IPP_TAG_NOVALUE :
2960 strlcat(optptr, attr->name,
2961 optlength - (optptr - options));
2962 break;
ef416fc2 2963
e1d6a774 2964 case IPP_TAG_RANGE :
2965 if (attr->values[i].range.lower == attr->values[i].range.upper)
2966 snprintf(optptr, optlength - (optptr - options) - 1,
2967 "%d", attr->values[i].range.lower);
2968 else
2969 snprintf(optptr, optlength - (optptr - options) - 1,
2970 "%d-%d", attr->values[i].range.lower,
2971 attr->values[i].range.upper);
2972 break;
ef416fc2 2973
e1d6a774 2974 case IPP_TAG_RESOLUTION :
2975 snprintf(optptr, optlength - (optptr - options) - 1,
2976 "%dx%d%s", attr->values[i].resolution.xres,
2977 attr->values[i].resolution.yres,
2978 attr->values[i].resolution.units == IPP_RES_PER_INCH ?
2979 "dpi" : "dpc");
2980 break;
ef416fc2 2981
e1d6a774 2982 case IPP_TAG_STRING :
2983 case IPP_TAG_TEXT :
2984 case IPP_TAG_NAME :
2985 case IPP_TAG_KEYWORD :
2986 case IPP_TAG_CHARSET :
2987 case IPP_TAG_LANGUAGE :
2988 case IPP_TAG_URI :
2989 for (valptr = attr->values[i].string.text; *valptr;)
2990 {
2991 if (strchr(" \t\n\\\'\"", *valptr))
2992 *optptr++ = '\\';
2993 *optptr++ = *valptr++;
2994 }
2995
2996 *optptr = '\0';
2997 break;
2998
2999 default :
3000 break; /* anti-compiler-warning-code */
3001 }
3002 }
3003
3004 optptr += strlen(optptr);
3005 }
3006 }
3007
3008 /*
3009 * Build the command-line arguments for the filters. Each filter
3010 * has 6 or 7 arguments:
3011 *
3012 * argv[0] = printer
3013 * argv[1] = job ID
3014 * argv[2] = username
3015 * argv[3] = title
3016 * argv[4] = # copies
3017 * argv[5] = options
3018 * argv[6] = filename (optional; normally stdin)
3019 *
3020 * This allows legacy printer drivers that use the old System V
3021 * printing interface to be used by CUPS.
3022 *
3023 * For remote jobs, we send all of the files in the argument list.
3024 */
3025
d09495fa 3026 if (printer->remote && job->num_files > 1)
e1d6a774 3027 argv = calloc(7 + job->num_files, sizeof(char *));
3028 else
3029 argv = calloc(8, sizeof(char *));
3030
91c84a35
MS
3031 if (!argv)
3032 {
3033 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to allocate argument array!");
3034 cupsArrayDelete(filters);
3035
3036 FilterLevel -= job->cost;
3037
3038 cupsdStopPrinter(printer, 0);
3039 return;
3040 }
3041
e1d6a774 3042 sprintf(jobid, "%d", job->id);
3043
3044 argv[0] = printer->name;
3045 argv[1] = jobid;
3046 argv[2] = job->username;
3047 argv[3] = title;
3048 argv[4] = copies;
3049 argv[5] = options;
3050
d09495fa 3051 if (printer->remote && job->num_files > 1)
e1d6a774 3052 {
3053 for (i = 0; i < job->num_files; i ++)
ef416fc2 3054 {
e1d6a774 3055 snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot,
3056 job->id, i + 1);
3057 argv[6 + i] = strdup(filename);
3058 }
3059 }
3060 else
3061 {
3062 snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot,
3063 job->id, job->current_file + 1);
3064 argv[6] = filename;
3065 }
ef416fc2 3066
e1d6a774 3067 for (i = 0; argv[i]; i ++)
75bd9771
MS
3068 cupsdLogJob(job, CUPSD_LOG_DEBUG,
3069 "argv[%d]=\"%s\"", i, argv[i]);
ef416fc2 3070
e1d6a774 3071 /*
3072 * Create environment variable strings for the filters...
3073 */
ef416fc2 3074
e1d6a774 3075 attr = ippFindAttribute(job->attrs, "attributes-natural-language",
3076 IPP_TAG_LANGUAGE);
ef416fc2 3077
0a682745 3078#ifdef __APPLE__
a4924f6c 3079 strcpy(apple_language, "APPLE_LANGUAGE=");
0a682745
MS
3080 _cupsAppleLanguage(attr->values[0].string.text,
3081 apple_language + 15, sizeof(apple_language) - 15);
3082#endif /* __APPLE__ */
3083
e1d6a774 3084 switch (strlen(attr->values[0].string.text))
3085 {
3086 default :
3087 /*
3088 * This is an unknown or badly formatted language code; use
3089 * the POSIX locale...
3090 */
ef416fc2 3091
e1d6a774 3092 strcpy(lang, "LANG=C");
3093 break;
ef416fc2 3094
e1d6a774 3095 case 2 :
3096 /*
3097 * Just the language code (ll)...
3098 */
ef416fc2 3099
7dfedb92 3100 snprintf(lang, sizeof(lang), "LANG=%s.UTF8",
e1d6a774 3101 attr->values[0].string.text);
3102 break;
ef416fc2 3103
e1d6a774 3104 case 5 :
3105 /*
3106 * Language and country code (ll-cc)...
3107 */
ef416fc2 3108
7dfedb92 3109 snprintf(lang, sizeof(lang), "LANG=%c%c_%c%c.UTF8",
e1d6a774 3110 attr->values[0].string.text[0],
3111 attr->values[0].string.text[1],
3112 toupper(attr->values[0].string.text[3] & 255),
3113 toupper(attr->values[0].string.text[4] & 255));
3114 break;
3115 }
ef416fc2 3116
e1d6a774 3117 attr = ippFindAttribute(job->attrs, "document-format",
3118 IPP_TAG_MIMETYPE);
3119 if (attr != NULL &&
3120 (optptr = strstr(attr->values[0].string.text, "charset=")) != NULL)
3121 snprintf(charset, sizeof(charset), "CHARSET=%s", optptr + 8);
3122 else
3123 {
3124 attr = ippFindAttribute(job->attrs, "attributes-charset",
3125 IPP_TAG_CHARSET);
3126 snprintf(charset, sizeof(charset), "CHARSET=%s",
3127 attr->values[0].string.text);
ef416fc2 3128 }
3129
e1d6a774 3130 snprintf(content_type, sizeof(content_type), "CONTENT_TYPE=%s/%s",
3131 job->filetypes[job->current_file]->super,
3132 job->filetypes[job->current_file]->type);
3133 snprintf(device_uri, sizeof(device_uri), "DEVICE_URI=%s",
3134 printer->device_uri);
3135 cupsdSanitizeURI(printer->device_uri, sani_uri, sizeof(sani_uri));
3136 snprintf(ppd, sizeof(ppd), "PPD=%s/ppd/%s.ppd", ServerRoot, printer->name);
3137 snprintf(printer_name, sizeof(printer_name), "PRINTER=%s", printer->name);
3138 snprintf(rip_max_cache, sizeof(rip_max_cache), "RIP_MAX_CACHE=%s", RIPCache);
ef416fc2 3139
e1d6a774 3140 envc = cupsdLoadEnv(envp, (int)(sizeof(envp) / sizeof(envp[0])));
ef416fc2 3141
e1d6a774 3142 envp[envc ++] = charset;
3143 envp[envc ++] = lang;
0a682745
MS
3144#ifdef __APPLE__
3145 envp[envc ++] = apple_language;
3146#endif /* __APPLE__ */
e1d6a774 3147 envp[envc ++] = ppd;
3148 envp[envc ++] = rip_max_cache;
3149 envp[envc ++] = content_type;
3150 envp[envc ++] = device_uri;
3151 envp[envc ++] = printer_name;
bd7854cb 3152
db0bd74a 3153 if (!printer->remote && !printer->raw)
e1d6a774 3154 {
db0bd74a
MS
3155 filter = (mime_filter_t *)cupsArrayLast(filters);
3156
3157 if (printer->port_monitor)
3158 filter = (mime_filter_t *)cupsArrayPrev(filters);
3159
3160 if (filter && filter->dst)
3161 {
3162 snprintf(final_content_type, sizeof(final_content_type),
3163 "FINAL_CONTENT_TYPE=%s/%s",
3164 filter->dst->super, filter->dst->type);
3165 envp[envc ++] = final_content_type;
3166 }
e1d6a774 3167 }
bd7854cb 3168
e1d6a774 3169 if (Classification && !banner_page)
3170 {
3171 if ((attr = ippFindAttribute(job->attrs, "job-sheets",
3172 IPP_TAG_NAME)) == NULL)
3173 snprintf(classification, sizeof(classification), "CLASSIFICATION=%s",
3174 Classification);
3175 else if (attr->num_values > 1 &&
3176 strcmp(attr->values[1].string.text, "none") != 0)
3177 snprintf(classification, sizeof(classification), "CLASSIFICATION=%s",
3178 attr->values[1].string.text);
3179 else
3180 snprintf(classification, sizeof(classification), "CLASSIFICATION=%s",
3181 attr->values[0].string.text);
bd7854cb 3182
e1d6a774 3183 envp[envc ++] = classification;
3184 }
bd7854cb 3185
e1d6a774 3186 if (job->dtype & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT))
bd7854cb 3187 {
e1d6a774 3188 snprintf(class_name, sizeof(class_name), "CLASS=%s", job->dest);
3189 envp[envc ++] = class_name;
3190 }
bd7854cb 3191
09a101d6 3192 if (job->auth_username)
3193 envp[envc ++] = job->auth_username;
3194 if (job->auth_domain)
3195 envp[envc ++] = job->auth_domain;
3196 if (job->auth_password)
3197 envp[envc ++] = job->auth_password;
3198
f7deaa1a 3199#ifdef HAVE_GSSAPI
3200 if (job->ccname)
3201 envp[envc ++] = job->ccname;
3202#endif /* HAVE_GSSAPI */
3203
e1d6a774 3204 envp[envc] = NULL;
bd7854cb 3205
e1d6a774 3206 for (i = 0; i < envc; i ++)
db0bd74a 3207 if (!strncmp(envp[i], "AUTH_", 5))
75bd9771
MS
3208 cupsdLogJob(job, CUPSD_LOG_DEBUG, "envp[%d]=\"AUTH_%c****\"", i,
3209 envp[i][5]);
09a101d6 3210 else if (strncmp(envp[i], "DEVICE_URI=", 11))
75bd9771 3211 cupsdLogJob(job, CUPSD_LOG_DEBUG, "envp[%d]=\"%s\"", i, envp[i]);
e1d6a774 3212 else
75bd9771
MS
3213 cupsdLogJob(job, CUPSD_LOG_DEBUG, "envp[%d]=\"DEVICE_URI=%s\"", i,
3214 sani_uri);
e1d6a774 3215
d09495fa 3216 if (printer->remote)
e1d6a774 3217 job->current_file = job->num_files;
3218 else
3219 job->current_file ++;
bd7854cb 3220
3221 /*
e1d6a774 3222 * Now create processes for all of the filters...
bd7854cb 3223 */
3224
e1d6a774 3225 filterfds[0][0] = -1;
3226 filterfds[0][1] = -1;
3227 filterfds[1][0] = -1;
3228 filterfds[1][1] = -1;
bd7854cb 3229
89d46774 3230 if (!job->status_buffer)
bd7854cb 3231 {
89d46774 3232 if (cupsdOpenPipe(job->status_pipes))
3233 {
75bd9771
MS
3234 cupsdLogJob(job, CUPSD_LOG_ERROR,
3235 "Unable to create job status pipes - %s.", strerror(errno));
89d46774 3236 snprintf(printer->state_message, sizeof(printer->state_message),
3237 "Unable to create status pipes - %s.", strerror(errno));
f7deaa1a 3238
89d46774 3239 cupsdAddPrinterHistory(printer);
f7deaa1a 3240
89d46774 3241 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
3242 "Job canceled because the server could not create the job "
3243 "status pipes.");
f7deaa1a 3244
89d46774 3245 goto abort_job;
3246 }
f7deaa1a 3247
75bd9771
MS
3248 cupsdLogJob(job, CUPSD_LOG_DEBUG2,
3249 "start_job: status_pipes = [ %d %d ]",
3250 job->status_pipes[0], job->status_pipes[1]);
f7deaa1a 3251
75bd9771 3252 job->status_buffer = cupsdStatBufNew(job->status_pipes[0], NULL);
09a101d6 3253 job->status_level = CUPSD_LOG_INFO;
e1d6a774 3254 }
bd7854cb 3255
89d46774 3256 job->status = 0;
e1d6a774 3257 memset(job->filters, 0, sizeof(job->filters));
bd7854cb 3258
a4924f6c
MS
3259 if (!job->profile)
3260 job->profile = cupsdCreateProfile(job->id);
3261
e1d6a774 3262 for (i = 0, slot = 0, filter = (mime_filter_t *)cupsArrayFirst(filters);
3263 filter;
3264 i ++, filter = (mime_filter_t *)cupsArrayNext(filters))
3265 {
3266 if (filter->filter[0] != '/')
3267 snprintf(command, sizeof(command), "%s/filter/%s", ServerBin,
3268 filter->filter);
3269 else
3270 strlcpy(command, filter->filter, sizeof(command));
bd7854cb 3271
e1d6a774 3272 if (i < (cupsArrayCount(filters) - 1))
bd7854cb 3273 {
e1d6a774 3274 if (cupsdOpenPipe(filterfds[slot]))
3275 {
75bd9771
MS
3276 cupsdLogJob(job, CUPSD_LOG_ERROR,
3277 "Unable to create job filter pipes - %s.", strerror(errno));
e1d6a774 3278 snprintf(printer->state_message, sizeof(printer->state_message),
3279 "Unable to create filter pipes - %s.", strerror(errno));
3280 cupsdAddPrinterHistory(printer);
bd7854cb 3281
e1d6a774 3282 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
3283 "Job canceled because the server could not create the "
3284 "filter pipes.");
bd7854cb 3285
e1d6a774 3286 goto abort_job;
bd7854cb 3287 }
e1d6a774 3288 }
3289 else
3290 {
3291 if (job->current_file == 1)
bd7854cb 3292 {
e1d6a774 3293 if (strncmp(printer->device_uri, "file:", 5) != 0)
3294 {
3295 if (cupsdOpenPipe(job->print_pipes))
3296 {
75bd9771
MS
3297 cupsdLogJob(job, CUPSD_LOG_ERROR,
3298 "Unable to create job backend pipes - %s.",
3299 strerror(errno));
e1d6a774 3300 snprintf(printer->state_message, sizeof(printer->state_message),
3301 "Unable to create backend pipes - %s.", strerror(errno));
3302 cupsdAddPrinterHistory(printer);
bd7854cb 3303
e1d6a774 3304 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
3305 "Job canceled because the server could not create "
3306 "the backend pipes.");
bd7854cb 3307
e1d6a774 3308 goto abort_job;
3309 }
3310 }
3311 else
3312 {
3313 job->print_pipes[0] = -1;
ed486911 3314 if (!strcmp(printer->device_uri, "file:/dev/null") ||
3315 !strcmp(printer->device_uri, "file:///dev/null"))
3316 job->print_pipes[1] = -1;
e1d6a774 3317 else
e1d6a774 3318 {
ed486911 3319 if (!strncmp(printer->device_uri, "file:/dev/", 10))
3320 job->print_pipes[1] = open(printer->device_uri + 5,
3321 O_WRONLY | O_EXCL);
3322 else if (!strncmp(printer->device_uri, "file:///dev/", 12))
3323 job->print_pipes[1] = open(printer->device_uri + 7,
3324 O_WRONLY | O_EXCL);
3325 else if (!strncmp(printer->device_uri, "file:///", 8))
3326 job->print_pipes[1] = open(printer->device_uri + 7,
3327 O_WRONLY | O_CREAT | O_TRUNC, 0600);
3328 else
3329 job->print_pipes[1] = open(printer->device_uri + 5,
3330 O_WRONLY | O_CREAT | O_TRUNC, 0600);
bd7854cb 3331
ed486911 3332 if (job->print_pipes[1] < 0)
3333 {
75bd9771
MS
3334 cupsdLogJob(job, CUPSD_LOG_ERROR,
3335 "Unable to open output file \"%s\" - %s.",
3336 printer->device_uri, strerror(errno));
ed486911 3337 snprintf(printer->state_message, sizeof(printer->state_message),
3338 "Unable to open output file \"%s\" - %s.",
3339 printer->device_uri, strerror(errno));
3340
3341 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
3342 "Job canceled because the server could not open the "
3343 "output file.");
3344
3345 goto abort_job;
3346 }
bd7854cb 3347
ed486911 3348 fcntl(job->print_pipes[1], F_SETFD,
3349 fcntl(job->print_pipes[1], F_GETFD) | FD_CLOEXEC);
3350 }
e1d6a774 3351 }
bd7854cb 3352
75bd9771
MS
3353 cupsdLogJob(job, CUPSD_LOG_DEBUG2,
3354 "start_job: print_pipes = [ %d %d ]",
3355 job->print_pipes[0], job->print_pipes[1]);
bd7854cb 3356 }
e1d6a774 3357
3358 filterfds[slot][0] = job->print_pipes[0];
3359 filterfds[slot][1] = job->print_pipes[1];
bd7854cb 3360 }
bd7854cb 3361
75bd9771
MS
3362 cupsdLogJob(job, CUPSD_LOG_DEBUG2, "start_job: filter=\"%s\"", command);
3363 cupsdLogJob(job, CUPSD_LOG_DEBUG2, "start_job: filterfds[%d]=[ %d %d ]",
3364 slot, filterfds[slot][0], filterfds[slot][1]);
bd7854cb 3365
e1d6a774 3366 pid = cupsdStartProcess(command, argv, envp, filterfds[!slot][0],
89d46774 3367 filterfds[slot][1], job->status_pipes[1],
f7deaa1a 3368 job->back_pipes[0], job->side_pipes[0], 0,
a4924f6c 3369 job->profile, job->filters + i);
bd7854cb 3370
75bd9771
MS
3371 cupsdLogJob(job, CUPSD_LOG_DEBUG2,
3372 "start_job: Closing filter pipes for slot %d [ %d %d ]...",
3373 !slot, filterfds[!slot][0], filterfds[!slot][1]);
bd7854cb 3374
e1d6a774 3375 cupsdClosePipe(filterfds[!slot]);
bd7854cb 3376
e1d6a774 3377 if (pid == 0)
3378 {
75bd9771
MS
3379 cupsdLogJob(job, CUPSD_LOG_ERROR, "Unable to start filter \"%s\" - %s.",
3380 filter->filter, strerror(errno));
e1d6a774 3381 snprintf(printer->state_message, sizeof(printer->state_message),
3382 "Unable to start filter \"%s\" - %s.",
3383 filter->filter, strerror(errno));
bd7854cb 3384
e1d6a774 3385 cupsdAddPrinterHistory(printer);
bd7854cb 3386
e1d6a774 3387 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
3388 "Job canceled because the server could not execute a "
3389 "filter.");
bd7854cb 3390
e1d6a774 3391 goto abort_job;
3392 }
3393
75bd9771
MS
3394 cupsdLogJob(job, CUPSD_LOG_INFO, "Started filter %s (PID %d)", command,
3395 pid);
e1d6a774 3396
3397 argv[6] = NULL;
3398 slot = !slot;
bd7854cb 3399 }
3400
e1d6a774 3401 cupsArrayDelete(filters);
75bd9771 3402 filters = NULL;
bd7854cb 3403
e1d6a774 3404 /*
3405 * Finally, pipe the final output into a backend process if needed...
3406 */
bd7854cb 3407
e1d6a774 3408 if (strncmp(printer->device_uri, "file:", 5) != 0)
bd7854cb 3409 {
d9bca400 3410 if (job->current_file == 1 || printer->remote)
bd7854cb 3411 {
e1d6a774 3412 sscanf(printer->device_uri, "%254[^:]", method);
3413 snprintf(command, sizeof(command), "%s/backend/%s", ServerBin, method);
bd7854cb 3414
e1d6a774 3415 /*
3416 * See if the backend needs to run as root...
3417 */
bd7854cb 3418
e1d6a774 3419 if (RunUser)
3420 backroot = 0;
3421 else if (stat(command, &backinfo))
3422 backroot = 0;
3423 else
3424 backroot = !(backinfo.st_mode & (S_IRWXG | S_IRWXO));
bd7854cb 3425
e1d6a774 3426 argv[0] = sani_uri;
bd7854cb 3427
e1d6a774 3428 filterfds[slot][0] = -1;
ed486911 3429 filterfds[slot][1] = -1;
bd7854cb 3430
75bd9771
MS
3431 cupsdLogJob(job, CUPSD_LOG_DEBUG2, "start_job: backend=\"%s\"", command);
3432 cupsdLogJob(job, CUPSD_LOG_DEBUG2, "start_job: filterfds[%d] = [ %d %d ]",
3433 slot, filterfds[slot][0], filterfds[slot][1]);
bd7854cb 3434
e1d6a774 3435 pid = cupsdStartProcess(command, argv, envp, filterfds[!slot][0],
89d46774 3436 filterfds[slot][1], job->status_pipes[1],
f7deaa1a 3437 job->back_pipes[1], job->side_pipes[1],
a4924f6c 3438 backroot, job->profile, &(job->backend));
bd7854cb 3439
e1d6a774 3440 if (pid == 0)
bd7854cb 3441 {
75bd9771
MS
3442 cupsdLogJob(job, CUPSD_LOG_ERROR, "Unable to start backend \"%s\" - %s.",
3443 method, strerror(errno));
e1d6a774 3444 snprintf(printer->state_message, sizeof(printer->state_message),
3445 "Unable to start backend \"%s\" - %s.", method,
3446 strerror(errno));
3447
3448 cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
3449 "Job canceled because the server could not execute "
3450 "the backend.");
3451
3452 goto abort_job;
3453 }
3454 else
3455 {
75bd9771
MS
3456 cupsdLogJob(job, CUPSD_LOG_INFO, "Started backend %s (PID %d)",
3457 command, pid);
bd7854cb 3458 }
e1d6a774 3459 }
bd7854cb 3460
e1d6a774 3461 if (job->current_file == job->num_files)
3462 {
75bd9771
MS
3463 cupsdLogJob(job, CUPSD_LOG_DEBUG2,
3464 "start_job: Closing print pipes [ %d %d ]...",
3465 job->print_pipes[0], job->print_pipes[1]);
bd7854cb 3466
e1d6a774 3467 cupsdClosePipe(job->print_pipes);
bd7854cb 3468
75bd9771
MS
3469 cupsdLogJob(job, CUPSD_LOG_DEBUG2,
3470 "start_job: Closing back pipes [ %d %d ]...",
3471 job->back_pipes[0], job->back_pipes[1]);
bd7854cb 3472
e1d6a774 3473 cupsdClosePipe(job->back_pipes);
89d46774 3474
75bd9771
MS
3475 cupsdLogJob(job, CUPSD_LOG_DEBUG2,
3476 "start_job: Closing side pipes [ %d %d ]...",
3477 job->side_pipes[0], job->side_pipes[1]);
f7deaa1a 3478
3479 cupsdClosePipe(job->side_pipes);
3480
75bd9771
MS
3481 cupsdLogJob(job, CUPSD_LOG_DEBUG2,
3482 "start_job: Closing status output pipe %d...",
3483 job->status_pipes[1]);
89d46774 3484
3485 close(job->status_pipes[1]);
3486 job->status_pipes[1] = -1;
e1d6a774 3487 }
3488 }
3489 else
3490 {
3491 filterfds[slot][0] = -1;
3492 filterfds[slot][1] = -1;
bd7854cb 3493
e1d6a774 3494 if (job->current_file == job->num_files)
3495 {
75bd9771
MS
3496 cupsdLogJob(job, CUPSD_LOG_DEBUG2,
3497 "start_job: Closing print pipes [ %d %d ]...",
3498 job->print_pipes[0], job->print_pipes[1]);
bd7854cb 3499
e1d6a774 3500 cupsdClosePipe(job->print_pipes);
89d46774 3501
75bd9771
MS
3502 cupsdLogJob(job, CUPSD_LOG_DEBUG2,
3503 "start_job: Closing status output pipe %d...",
3504 job->status_pipes[1]);
89d46774 3505
3506 close(job->status_pipes[1]);
3507 job->status_pipes[1] = -1;
bd7854cb 3508 }
e1d6a774 3509 }
bd7854cb 3510
75bd9771
MS
3511 cupsdLogJob(job, CUPSD_LOG_DEBUG2,
3512 "start_job: Closing filter pipes for slot %d [ %d %d ]...",
3513 slot, filterfds[slot][0], filterfds[slot][1]);
89d46774 3514 cupsdClosePipe(filterfds[slot]);
bd7854cb 3515
d09495fa 3516 if (printer->remote && job->num_files > 1)
e1d6a774 3517 {
3518 for (i = 0; i < job->num_files; i ++)
3519 free(argv[i + 6]);
3520 }
bd7854cb 3521
e1d6a774 3522 free(argv);
ef416fc2 3523
f899b121 3524 cupsdAddSelect(job->status_buffer->fd, (cupsd_selfunc_t)update_job, NULL,
f7deaa1a 3525 job);
ef416fc2 3526
e1d6a774 3527 cupsdAddEvent(CUPSD_EVENT_JOB_STATE, job->printer, job, "Job #%d started.",
3528 job->id);
ef416fc2 3529
e1d6a774 3530 return;
ef416fc2 3531
3532
3533 /*
e1d6a774 3534 * If we get here, we need to abort the current job and close out all
3535 * files and pipes...
ef416fc2 3536 */
3537
e1d6a774 3538 abort_job:
ef416fc2 3539
e1d6a774 3540 for (slot = 0; slot < 2; slot ++)
3541 {
75bd9771
MS
3542 cupsdLogJob(job, CUPSD_LOG_DEBUG2,
3543 "start_job: Closing filter pipes for slot %d [ %d %d ]...",
3544 slot, filterfds[slot][0], filterfds[slot][1]);
e1d6a774 3545 cupsdClosePipe(filterfds[slot]);
3546 }
ef416fc2 3547
75bd9771
MS
3548 cupsdLogJob(job, CUPSD_LOG_DEBUG2,
3549 "start_job: Closing status pipes [ %d %d ]...",
3550 job->status_pipes[0], job->status_pipes[1]);
89d46774 3551 cupsdClosePipe(job->status_pipes);
3552 cupsdStatBufDelete(job->status_buffer);
ef416fc2 3553
b94498cf 3554 job->status_buffer = NULL;
3555
e1d6a774 3556 cupsArrayDelete(filters);
ef416fc2 3557
d09495fa 3558 if (printer->remote && job->num_files > 1)
e1d6a774 3559 {
3560 for (i = 0; i < job->num_files; i ++)
3561 free(argv[i + 6]);
3562 }
ef416fc2 3563
e1d6a774 3564 free(argv);
ef416fc2 3565
e1d6a774 3566 cupsdStopJob(job, 0);
3567}
ef416fc2 3568
e1d6a774 3569
3570/*
3571 * 'unload_job()' - Unload a job from memory.
3572 */
3573
3574static void
3575unload_job(cupsd_job_t *job) /* I - Job */
3576{
3577 if (!job->attrs)
3578 return;
3579
75bd9771 3580 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Unloading...");
e1d6a774 3581
3582 ippDelete(job->attrs);
3583
cc0d019f
MS
3584 job->attrs = NULL;
3585 job->state = NULL;
3586 job->sheets = NULL;
3587 job->job_sheets = NULL;
3588 job->printer_message = NULL;
3589 job->printer_reasons = NULL;
ef416fc2 3590}
3591
3592
3593/*
f899b121 3594 * 'update_job()' - Read a status update from a job's filters.
3595 */
3596
3597void
09a101d6 3598update_job(cupsd_job_t *job) /* I - Job to check */
f899b121 3599{
3600 int i; /* Looping var */
3601 int copies; /* Number of copies printed */
3602 char message[1024], /* Message text */
3603 *ptr; /* Pointer update... */
3604 int loglevel, /* Log level for message */
3605 event = 0; /* Events? */
3606
3607
3608 while ((ptr = cupsdStatBufUpdate(job->status_buffer, &loglevel,
3609 message, sizeof(message))) != NULL)
3610 {
3611 /*
3612 * Process page and printer state messages as needed...
3613 */
3614
3615 if (loglevel == CUPSD_LOG_PAGE)
3616 {
3617 /*
3618 * Page message; send the message to the page_log file and update the
3619 * job sheet count...
3620 */
3621
75bd9771 3622 cupsdLogJob(job, CUPSD_LOG_DEBUG, "PAGE: %s", message);
dd1abb6b 3623
91c84a35 3624 if (job->sheets)
f899b121 3625 {
3626 if (!strncasecmp(message, "total ", 6))
3627 {
3628 /*
3629 * Got a total count of pages from a backend or filter...
3630 */
3631
3632 copies = atoi(message + 6);
3633 copies -= job->sheets->values[0].integer; /* Just track the delta */
3634 }
3635 else if (!sscanf(message, "%*d%d", &copies))
3636 copies = 1;
3637
3638 job->sheets->values[0].integer += copies;
3639
3640 if (job->printer->page_limit)
3641 {
3642 cupsd_quota_t *q = cupsdUpdateQuota(job->printer, job->username,
3643 copies, 0);
3644
3645#ifdef __APPLE__
3646 if (AppleQuotas && q->page_count == -3)
3647 {
3648 /*
3649 * Quota limit exceeded, cancel job in progress immediately...
3650 */
3651
75bd9771
MS
3652 cupsdLogJob(job, CUPSD_LOG_INFO,
3653 "Canceled because pages exceed user %s "
3654 "quota limit on printer %s (%s).",
3655 job->username, job->printer->name,
3656 job->printer->info);
f899b121 3657
3658 cupsdCancelJob(job, 1, IPP_JOB_CANCELED);
3659 return;
3660 }
3661#else
3662 (void)q;
3663#endif /* __APPLE__ */
3664 }
3665 }
3666
3667 cupsdLogPage(job, message);
3668
91c84a35
MS
3669 if (job->sheets)
3670 cupsdAddEvent(CUPSD_EVENT_JOB_PROGRESS, job->printer, job,
3671 "Printed %d page(s).", job->sheets->values[0].integer);
f899b121 3672 }
3673 else if (loglevel == CUPSD_LOG_STATE)
3674 {
75bd9771 3675 cupsdLogJob(job, CUPSD_LOG_DEBUG, "STATE: %s", message);
dd1abb6b 3676
09a101d6 3677 if (!strcmp(message, "paused"))
c24d2134 3678 {
09a101d6 3679 cupsdStopPrinter(job->printer, 1);
c24d2134
MS
3680 return;
3681 }
09a101d6 3682 else
3683 {
3684 cupsdSetPrinterReasons(job->printer, message);
3685 cupsdAddPrinterHistory(job->printer);
d9bca400 3686 event |= CUPSD_EVENT_PRINTER_STATE;
09a101d6 3687 }
bc44d920 3688
4509bb49 3689 update_job_attrs(job, 0);
f899b121 3690 }
3691 else if (loglevel == CUPSD_LOG_ATTR)
3692 {
3693 /*
3694 * Set attribute(s)...
3695 */
3696
3697 int num_attrs; /* Number of attributes */
3698 cups_option_t *attrs; /* Attributes */
3699 const char *attr; /* Attribute */
3700
3701
75bd9771 3702 cupsdLogJob(job, CUPSD_LOG_DEBUG, "ATTR: %s", message);
dd1abb6b 3703
f899b121 3704 num_attrs = cupsParseOptions(message, 0, &attrs);
3705
3706 if ((attr = cupsGetOption("auth-info-required", num_attrs,
3707 attrs)) != NULL)
7ff4fea9 3708 {
f899b121 3709 cupsdSetAuthInfoRequired(job->printer, attr, NULL);
7dfedb92 3710 cupsdSetPrinterAttrs(job->printer);
3dfe78b3
MS
3711
3712 if (job->printer->type & CUPS_PRINTER_DISCOVERED)
3713 cupsdMarkDirty(CUPSD_DIRTY_REMOTE);
3714 else
3715 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
7ff4fea9 3716 }
f899b121 3717
323c5de1 3718 if ((attr = cupsGetOption("printer-alert", num_attrs, attrs)) != NULL)
3719 {
3720 cupsdSetString(&job->printer->alert, attr);
d9bca400 3721 event |= CUPSD_EVENT_PRINTER_STATE;
323c5de1 3722 }
3723
3724 if ((attr = cupsGetOption("printer-alert-description", num_attrs,
3725 attrs)) != NULL)
3726 {
3727 cupsdSetString(&job->printer->alert_description, attr);
d9bca400 3728 event |= CUPSD_EVENT_PRINTER_STATE;
323c5de1 3729 }
3730
5a738aea
MS
3731 if ((attr = cupsGetOption("marker-colors", num_attrs, attrs)) != NULL)
3732 {
3733 cupsdSetPrinterAttr(job->printer, "marker-colors", (char *)attr);
3734 job->printer->marker_time = time(NULL);
3735 event |= CUPSD_EVENT_PRINTER_STATE;
3736 }
3737
3738 if ((attr = cupsGetOption("marker-levels", num_attrs, attrs)) != NULL)
3739 {
3740 cupsdSetPrinterAttr(job->printer, "marker-levels", (char *)attr);
3741 job->printer->marker_time = time(NULL);
3742 event |= CUPSD_EVENT_PRINTER_STATE;
3743 }
3744
75bd9771
MS
3745 if ((attr = cupsGetOption("marker-message", num_attrs, attrs)) != NULL)
3746 {
3747 cupsdSetPrinterAttr(job->printer, "marker-message", (char *)attr);
3748 job->printer->marker_time = time(NULL);
3749 event |= CUPSD_EVENT_PRINTER_STATE;
3750 }
3751
5a738aea
MS
3752 if ((attr = cupsGetOption("marker-names", num_attrs, attrs)) != NULL)
3753 {
3754 cupsdSetPrinterAttr(job->printer, "marker-names", (char *)attr);
3755 job->printer->marker_time = time(NULL);
3756 event |= CUPSD_EVENT_PRINTER_STATE;
3757 }
3758
3759 if ((attr = cupsGetOption("marker-types", num_attrs, attrs)) != NULL)
3760 {
3761 cupsdSetPrinterAttr(job->printer, "marker-types", (char *)attr);
3762 job->printer->marker_time = time(NULL);
3763 event |= CUPSD_EVENT_PRINTER_STATE;
3764 }
3765
f899b121 3766 cupsFreeOptions(num_attrs, attrs);
3767 }
c9fc04c6
MS
3768 else if (loglevel == CUPSD_LOG_PPD)
3769 {
3770 /*
3771 * Set attribute(s)...
3772 */
3773
3774 int num_keywords; /* Number of keywords */
3775 cups_option_t *keywords; /* Keywords */
3776
3777
75bd9771 3778 cupsdLogJob(job, CUPSD_LOG_DEBUG, "PPD: %s", message);
dd1abb6b 3779
c9fc04c6
MS
3780 num_keywords = cupsParseOptions(message, 0, &keywords);
3781
3782 if (cupsdUpdatePrinterPPD(job->printer, num_keywords, keywords))
3783 cupsdSetPrinterAttrs(job->printer);
3784
3785 cupsFreeOptions(num_keywords, keywords);
3786 }
f899b121 3787#ifdef __APPLE__
3788 else if (!strncmp(message, "recoverable:", 12))
3789 {
3790 cupsdSetPrinterReasons(job->printer,
3791 "+com.apple.print.recoverable-warning");
3792
3793 ptr = message + 12;
3794 while (isspace(*ptr & 255))
3795 ptr ++;
3796
3797 cupsdSetString(&job->printer->recoverable, ptr);
3798 cupsdAddPrinterHistory(job->printer);
d9bca400 3799 event |= CUPSD_EVENT_PRINTER_STATE;
f899b121 3800 }
3801 else if (!strncmp(message, "recovered:", 10))
3802 {
3803 cupsdSetPrinterReasons(job->printer,
3804 "-com.apple.print.recoverable-warning");
3805
3806 ptr = message + 10;
3807 while (isspace(*ptr & 255))
3808 ptr ++;
3809
3810 cupsdSetString(&job->printer->recoverable, ptr);
3811 cupsdAddPrinterHistory(job->printer);
d9bca400 3812 event |= CUPSD_EVENT_PRINTER_STATE;
f899b121 3813 }
3814#endif /* __APPLE__ */
75bd9771 3815 else
f899b121 3816 {
75bd9771 3817 cupsdLogJob(job, loglevel, "%s", message);
f899b121 3818
1f0275e3
MS
3819 if (loglevel < CUPSD_LOG_DEBUG)
3820 {
3821 strlcpy(job->printer->state_message, message,
3822 sizeof(job->printer->state_message));
3823 cupsdAddPrinterHistory(job->printer);
4509bb49 3824
4509bb49
MS
3825 event |= CUPSD_EVENT_PRINTER_STATE;
3826
1f0275e3
MS
3827 if (loglevel <= job->status_level)
3828 {
3829 /*
3830 * Some messages show in the printer-state-message attribute...
3831 */
09a101d6 3832
1f0275e3
MS
3833 if (loglevel != CUPSD_LOG_NOTICE)
3834 job->status_level = loglevel;
01ce6322 3835
1f0275e3
MS
3836 update_job_attrs(job, 1);
3837 }
75bd9771 3838 }
f899b121 3839 }
3840
3841 if (!strchr(job->status_buffer->buffer, '\n'))
3842 break;
3843 }
3844
d9bca400
MS
3845 if (event & CUPSD_EVENT_PRINTER_STATE)
3846 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, job->printer, NULL,
f899b121 3847 (job->printer->type & CUPS_PRINTER_CLASS) ?
3848 "Class \"%s\" state changed." :
3849 "Printer \"%s\" state changed.",
3850 job->printer->name);
3851
3852 if (ptr == NULL && !job->status_buffer->bufused)
3853 {
3854 /*
3855 * See if all of the filters and the backend have returned their
3856 * exit statuses.
3857 */
3858
3859 for (i = 0; job->filters[i] < 0; i ++);
3860
3861 if (job->filters[i])
3862 return;
3863
3864 if (job->current_file >= job->num_files && job->backend > 0)
3865 return;
3866
3867 /*
3868 * Handle the end of job stuff...
3869 */
3870
3871 cupsdFinishJob(job);
3872 }
3873}
3874
3875
3876/*
bc44d920 3877 * 'update_job_attrs()' - Update the job-printer-* attributes.
3878 */
3879
3880void
4509bb49
MS
3881update_job_attrs(cupsd_job_t *job, /* I - Job to update */
3882 int do_message)/* I - 1 = update job-printer-state message */
bc44d920 3883{
3884 int i; /* Looping var */
3885 int num_reasons; /* Actual number of reasons */
3886 const char * const *reasons; /* Reasons */
3887 static const char *none = "none", /* "none" */
3888 *paused = "paused";
3889 /* "paused" */
3890
3891
3892 /*
3893 * Get/create the job-printer-state-* attributes...
3894 */
3895
3896 if (!job->printer_message)
3897 {
3898 if ((job->printer_message = ippFindAttribute(job->attrs,
3899 "job-printer-state-message",
3900 IPP_TAG_TEXT)) == NULL)
3901 job->printer_message = ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_TEXT,
3902 "job-printer-state-message",
3903 NULL, "");
3904 }
3905
3906 if (!job->printer_reasons)
3907 job->printer_reasons = ippFindAttribute(job->attrs,
3908 "job-printer-state-reasons",
3909 IPP_TAG_KEYWORD);
3910
3911 /*
3912 * If the job isn't printing, return now...
3913 */
3914
3915 if (!job->printer)
3916 return;
3917
3918 /*
3919 * Otherwise copy the printer-state-message value...
3920 */
3921
4509bb49
MS
3922 if (job->printer->state_message[0] &&
3923 (do_message || !job->printer_message->values[0].string.text[0]))
bc44d920 3924 cupsdSetString(&(job->printer_message->values[0].string.text),
3925 job->printer->state_message);
3926
3927 /*
3928 * ... and the printer-state-reasons value...
3929 */
3930
3931 if (job->printer->num_reasons == 0)
3932 {
3933 num_reasons = 1;
3934 reasons = job->printer->state == IPP_PRINTER_STOPPED ? &paused : &none;
3935 }
3936 else
3937 {
3938 num_reasons = job->printer->num_reasons;
3939 reasons = (const char * const *)job->printer->reasons;
3940 }
3941
3942 if (!job->printer_reasons || job->printer_reasons->num_values != num_reasons)
3943 {
3944 ippDeleteAttribute(job->attrs, job->printer_reasons);
3945
3946 job->printer_reasons = ippAddStrings(job->attrs,
3947 IPP_TAG_JOB, IPP_TAG_KEYWORD,
3948 "job-printer-state-reasons",
3949 num_reasons, NULL, NULL);
3950 }
3951
3952 for (i = 0; i < num_reasons; i ++)
3953 cupsdSetString(&(job->printer_reasons->values[i].string.text), reasons[i]);
3954}
3955
3956
3957/*
75bd9771 3958 * End of "$Id: job.c 7682 2008-06-21 00:06:02Z mike $".
ef416fc2 3959 */