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