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