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