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