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