]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - scheduler/job.c
Merge changes from CUPS 1.4svn-r7874.
[thirdparty/cups.git] / scheduler / job.c
index c4e5b20df9f1bc913a021a2a132b33f811f75dc2..e2d74c6329e22191daceb31b0327e65b19996248 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * "$Id: job.c 7005 2007-10-01 23:45:48Z mike $"
+ * "$Id: job.c 7682 2008-06-21 00:06:02Z mike $"
  *
  *   Job management routines for the Common UNIX Printing System (CUPS).
  *
@@ -21,6 +21,7 @@
  *   cupsdCheckJobs()           - Check the pending jobs and start any if
  *                                the destination is available.
  *   cupsdCleanJobs()           - Clean out old jobs.
+ *   cupsdDeleteJob()           - Free all memory used by a job.
  *   cupsdFinishJob()           - Finish a job.
  *   cupsdFreeAllJobs()         - Free all jobs from memory.
  *   cupsdFindJob()             - Find the specified job.
@@ -44,7 +45,6 @@
  *   compare_active_jobs()      - Compare the job IDs and priorities of two
  *                                jobs.
  *   compare_jobs()             - Compare the job IDs of two jobs.
- *   free_job()                 - Free all memory used by a job.
  *   ipp_length()               - Compute the size of the buffer needed to
  *                                hold the textual IPP attributes.
  *   load_job_cache()           - Load jobs from the job.cache file.
@@ -89,7 +89,6 @@ static mime_filter_t  gziptoany_filter =
 
 static int     compare_active_jobs(void *first, void *second, void *data);
 static int     compare_jobs(void *first, void *second, void *data);
-static void    free_job(cupsd_job_t *job);
 static int     ipp_length(ipp_t *ipp);
 static void    load_job_cache(const char *filename);
 static void    load_next_job_id(const char *filename);
@@ -99,7 +98,7 @@ static void   set_hold_until(cupsd_job_t *job, time_t holdtime);
 static void    start_job(cupsd_job_t *job, cupsd_printer_t *printer);
 static void    unload_job(cupsd_job_t *job);
 static void    update_job(cupsd_job_t *job);
-static void    update_job_attrs(cupsd_job_t *job);
+static void    update_job_attrs(cupsd_job_t *job, int do_message);
 
 
 /*
@@ -211,10 +210,11 @@ cupsdCancelJob(cupsd_job_t  *job, /* I - Job to cancel */
   cupsdExpireSubscriptions(NULL, job);
 
  /*
-  * Remove the job from the active list...
+  * Remove the job from the active and printing lists...
   */
 
   cupsArrayRemove(ActiveJobs, job);
+  cupsArrayRemove(PrintingJobs, job);
 
  /*
   * Remove any authentication data...
@@ -277,7 +277,8 @@ cupsdCancelJob(cupsd_job_t  *job,   /* I - Job to cancel */
     * Save job state info...
     */
 
-    cupsdSaveJob(job);
+    job->dirty = 1;
+    cupsdMarkDirty(CUPSD_DIRTY_JOBS);
   }
   else
   {
@@ -299,8 +300,10 @@ cupsdCancelJob(cupsd_job_t  *job,  /* I - Job to cancel */
     * Free all memory used...
     */
 
-    free_job(job);
+    cupsdDeleteJob(job);
   }
+
+  cupsdSetBusyState();
 }
 
 
@@ -352,6 +355,7 @@ cupsdCheckJobs(void)
   cupsd_job_t          *job;           /* Current job in queue */
   cupsd_printer_t      *printer,       /* Printer destination */
                        *pclass;        /* Printer class destination */
+  ipp_attribute_t      *attr;          /* Job attribute */
 
 
   DEBUG_puts("cupsdCheckJobs()");
@@ -369,8 +373,9 @@ cupsdCheckJobs(void)
     */
 
     cupsdLogMessage(CUPSD_LOG_DEBUG2,
-                    "cupsdCheckJobs: Job %d: state_value=%d, loaded=%s",
-                    job->id, job->state_value, job->attrs ? "yes" : "no");
+                    "cupsdCheckJobs: Job %d: dest=%s, dtype=%x, "
+                   "state_value=%d, loaded=%s", job->id, job->dest, job->dtype,
+                   job->state_value, job->attrs ? "yes" : "no");
 
     if (job->state_value == IPP_JOB_HELD &&
         job->hold_until &&
@@ -385,6 +390,18 @@ cupsdCheckJobs(void)
 
       job->state->values[0].integer = IPP_JOB_PENDING;
       job->state_value              = IPP_JOB_PENDING;
+
+      if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
+                                  IPP_TAG_KEYWORD)) == NULL)
+       attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
+
+      if (attr)
+      {
+       attr->value_tag = IPP_TAG_KEYWORD;
+       cupsdSetString(&(attr->values[0].string.text), "no-hold");
+       job->dirty = 1;
+       cupsdMarkDirty(CUPSD_DIRTY_JOBS);
+      }
     }
 
    /*
@@ -420,9 +437,9 @@ cupsdCheckJobs(void)
        * cancel the job...
        */
 
-        cupsdLogMessage(CUPSD_LOG_WARN,
-                       "[Job %d] Printer/class %s has gone away; canceling job!",
-                       job->id, job->dest);
+        cupsdLogJob(job, CUPSD_LOG_WARN,
+                   "Printer/class %s has gone away; canceling job!",
+                   job->dest);
 
        cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
                       "Job canceled because the destination printer/class has "
@@ -444,15 +461,15 @@ cupsdCheckJobs(void)
          * so that we know which printer actually printed the job...
          */
 
-          ipp_attribute_t      *attr;  /* job-actual-printer-uri attribute */
-
-
           if ((attr = ippFindAttribute(job->attrs, "job-actual-printer-uri",
                                       IPP_TAG_URI)) != NULL)
             cupsdSetString(&attr->values[0].string.text, printer->uri);
          else
            ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_URI,
                         "job-actual-printer-uri", NULL, printer->uri);
+
+          job->dirty = 1;
+          cupsdMarkDirty(CUPSD_DIRTY_JOBS);
        }
 
         if ((!(printer->type & CUPS_PRINTER_DISCOVERED) && /* Printer is local */
@@ -501,6 +518,45 @@ cupsdCleanJobs(void)
 }
 
 
+/*
+ * 'cupsdDeleteJob()' - Free all memory used by a job.
+ */
+
+void
+cupsdDeleteJob(cupsd_job_t *job)       /* I - Job */
+{
+  cupsdClearString(&job->username);
+  cupsdClearString(&job->dest);
+  cupsdClearString(&job->auth_username);
+  cupsdClearString(&job->auth_domain);
+  cupsdClearString(&job->auth_password);
+
+#ifdef HAVE_GSSAPI
+ /*
+  * Destroy the credential cache and clear the KRB5CCNAME env var string.
+  */
+
+  if (job->ccache)
+  {
+    krb5_cc_destroy(KerberosContext, job->ccache);
+    job->ccache = NULL;
+  }
+
+  cupsdClearString(&job->ccname);
+#endif /* HAVE_GSSAPI */
+
+  if (job->num_files > 0)
+  {
+    free(job->compressions);
+    free(job->filetypes);
+  }
+
+  ippDelete(job->attrs);
+
+  free(job);
+}
+
+
 /*
  * 'cupsdFinishJob()' - Finish a job.
  */
@@ -512,12 +568,12 @@ cupsdFinishJob(cupsd_job_t *job)  /* I - Job */
   ipp_attribute_t      *attr;          /* job-hold-until attribute */
 
 
-  cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] File %d is complete.",
-                  job->id, job->current_file - 1);
+  cupsdLogJob(job, CUPSD_LOG_DEBUG, "File %d is complete.",
+             job->current_file - 1);
 
-  cupsdLogMessage(CUPSD_LOG_DEBUG2,
-                  "[Job %d] cupsdFinishJob: job->status is %d",
-                  job->id, job->status);
+  cupsdLogJob(job, CUPSD_LOG_DEBUG2,
+             "cupsdFinishJob: job->status is %d",
+             job->status);
 
   if (job->status_buffer &&
       (job->status < 0 || job->current_file >= job->num_files))
@@ -528,9 +584,9 @@ cupsdFinishJob(cupsd_job_t *job)    /* I - Job */
 
     cupsdRemoveSelect(job->status_buffer->fd);
 
-    cupsdLogMessage(CUPSD_LOG_DEBUG2,
-                   "[Job %d] cupsdFinishJob: Closing status pipes [ %d %d ]...",
-                   job->id, job->status_pipes[0], job->status_pipes[1]);
+    cupsdLogJob(job, CUPSD_LOG_DEBUG2,
+               "cupsdFinishJob: Closing status pipes [ %d %d ]...",
+               job->status_pipes[0], job->status_pipes[1]);
 
     cupsdClosePipe(job->status_pipes);
     cupsdStatBufDelete(job->status_buffer);
@@ -540,7 +596,7 @@ cupsdFinishJob(cupsd_job_t *job)    /* I - Job */
 
   printer = job->printer;
 
-  update_job_attrs(job);
+  update_job_attrs(job, 0);
 
   if (job->status < 0)
   {
@@ -563,15 +619,15 @@ cupsdFinishJob(cupsd_job_t *job)  /* I - Job */
     else
       exit_code = job->status;
 
-    cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Backend returned status %d (%s)",
-                    job->id, exit_code,
-                   exit_code == CUPS_BACKEND_FAILED ? "failed" :
-                       exit_code == CUPS_BACKEND_AUTH_REQUIRED ?
-                           "authentication required" :
-                       exit_code == CUPS_BACKEND_HOLD ? "hold job" :
-                       exit_code == CUPS_BACKEND_STOP ? "stop printer" :
-                       exit_code == CUPS_BACKEND_CANCEL ? "cancel job" :
-                       exit_code < 0 ? "crashed" : "unknown");
+    cupsdLogJob(job, CUPSD_LOG_INFO, "Backend returned status %d (%s)",
+               exit_code,
+               exit_code == CUPS_BACKEND_FAILED ? "failed" :
+                   exit_code == CUPS_BACKEND_AUTH_REQUIRED ?
+                       "authentication required" :
+                   exit_code == CUPS_BACKEND_HOLD ? "hold job" :
+                   exit_code == CUPS_BACKEND_STOP ? "stop printer" :
+                   exit_code == CUPS_BACKEND_CANCEL ? "cancel job" :
+                   exit_code < 0 ? "crashed" : "unknown");
 
    /*
     * Do what needs to be done...
@@ -599,14 +655,17 @@ cupsdFinishJob(cupsd_job_t *job)  /* I - Job */
            job->state_value              = IPP_JOB_PENDING;
           }
 
-         cupsdSaveJob(job);
+         job->dirty = 1;
+         cupsdMarkDirty(CUPSD_DIRTY_JOBS);
 
         /*
-         * If the job was queued to a class, try requeuing it...  For
-         * faxes and retry-job queues, hold the current job for 5 minutes.
+         * If the job was queued to a class or the error policy is
+         * "retry-current-job", try requeuing it...  For faxes and retry-job
+         * queues, hold the current job for 5 minutes.
          */
 
-         if (job->dtype & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT))
+         if ((job->dtype & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT)) ||
+             !strcmp(printer->error_policy, "retry-current-job"))
            cupsdCheckJobs();
          else if ((printer->type & CUPS_PRINTER_FAX) ||
                   !strcmp(printer->error_policy, "retry-job"))
@@ -624,10 +683,10 @@ cupsdFinishJob(cupsd_job_t *job)  /* I - Job */
              * Too many tries...
              */
 
-             cupsdLogMessage(CUPSD_LOG_ERROR,
-                             "[Job %d] Canceling job since it could not be "
-                             "sent after %d tries.",
-                             job->id, JobRetryLimit);
+             cupsdLogJob(job, CUPSD_LOG_ERROR,
+                         "Canceling job since it could not be "
+                         "sent after %d tries.",
+                         JobRetryLimit);
 
              cupsdCancelJob(job, 0, IPP_JOB_ABORTED);
            }
@@ -677,7 +736,8 @@ cupsdFinishJob(cupsd_job_t *job)    /* I - Job */
          job->state->values[0].integer = IPP_JOB_HELD;
          job->state_value              = IPP_JOB_HELD;
 
-         cupsdSaveJob(job);
+         job->dirty = 1;
+         cupsdMarkDirty(CUPSD_DIRTY_JOBS);
 
          cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED, printer, job,
                        "Job held due to backend errors; please consult "
@@ -694,7 +754,9 @@ cupsdFinishJob(cupsd_job_t *job)    /* I - Job */
          job->state->values[0].integer = IPP_JOB_PENDING;
          job->state_value              = IPP_JOB_PENDING;
 
-         cupsdSaveJob(job);
+         job->dirty = 1;
+         cupsdMarkDirty(CUPSD_DIRTY_JOBS);
+
          cupsdSetPrinterState(printer, IPP_PRINTER_STOPPED, 1);
 
          cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED, printer, job,
@@ -721,7 +783,8 @@ cupsdFinishJob(cupsd_job_t *job)    /* I - Job */
          job->state->values[0].integer = IPP_JOB_HELD;
          job->state_value              = IPP_JOB_HELD;
 
-         cupsdSaveJob(job);
+         job->dirty = 1;
+         cupsdMarkDirty(CUPSD_DIRTY_JOBS);
 
          cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED, printer, job,
                        "Authentication is required for job %d.", job->id);
@@ -740,10 +803,10 @@ cupsdFinishJob(cupsd_job_t *job)  /* I - Job */
     * Filter had errors; stop job...
     */
 
-    cupsdLogMessage(CUPSD_LOG_ERROR,
-                    "[Job %d] Job stopped due to filter errors.", job->id);
+    cupsdLogJob(job, CUPSD_LOG_ERROR, "Job stopped due to filter errors.");
     cupsdStopJob(job, 1);
-    cupsdSaveJob(job);
+    job->dirty = 1;
+    cupsdMarkDirty(CUPSD_DIRTY_JOBS);
     cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED, printer, job,
                   "Job stopped due to filter errors; please consult the "
                  "error_log file for details.");
@@ -770,8 +833,7 @@ cupsdFinishJob(cupsd_job_t *job)    /* I - Job */
       * Close out this job...
       */
 
-      cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Completed successfully.",
-                     job->id);
+      cupsdLogJob(job, CUPSD_LOG_INFO, "Completed successfully.");
       cupsdCancelJob(job, 0, IPP_JOB_COMPLETED);
       cupsdCheckJobs();
     }
@@ -803,8 +865,9 @@ cupsdFreeAllJobs(void)
   {
     cupsArrayRemove(Jobs, job);
     cupsArrayRemove(ActiveJobs, job);
+    cupsArrayRemove(PrintingJobs, job);
 
-    free_job(job);
+    cupsdDeleteJob(job);
   }
 
   cupsdReleaseSignals();
@@ -893,9 +956,8 @@ cupsdHoldJob(cupsd_job_t *job)              /* I - Job data */
   job->state_value              = IPP_JOB_HELD;
   job->current_file             = 0;
 
-  cupsdSaveJob(job);
-
-  cupsdCheckJobs();
+  job->dirty = 1;
+  cupsdMarkDirty(CUPSD_DIRTY_JOBS);
 }
 
 
@@ -922,6 +984,9 @@ cupsdLoadAllJobs(void)
   if (!ActiveJobs)
     ActiveJobs = cupsArrayNew(compare_active_jobs, NULL);
 
+  if (!PrintingJobs)
+    PrintingJobs = cupsArrayNew(compare_jobs, NULL);
+
  /*
   * See whether the job.cache file is older than the RequestRoot directory...
   */
@@ -997,8 +1062,7 @@ cupsdLoadJob(cupsd_job_t *job)             /* I - Job */
 
   if ((job->attrs = ippNew()) == NULL)
   {
-    cupsdLogMessage(CUPSD_LOG_ERROR,
-                    "[Job %d] Ran out of memory for job attributes!", job->id);
+    cupsdLogJob(job, CUPSD_LOG_ERROR, "Ran out of memory for job attributes!");
     return;
   }
 
@@ -1006,14 +1070,14 @@ cupsdLoadJob(cupsd_job_t *job)          /* I - Job */
   * Load job attributes...
   */
 
-  cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] Loading attributes...", job->id);
+  cupsdLogJob(job, CUPSD_LOG_DEBUG, "Loading attributes...");
 
   snprintf(jobfile, sizeof(jobfile), "%s/c%05d", RequestRoot, job->id);
   if ((fp = cupsFileOpen(jobfile, "r")) == NULL)
   {
-    cupsdLogMessage(CUPSD_LOG_ERROR,
-                   "[Job %d] Unable to open job control file \"%s\" - %s!",
-                   job->id, jobfile, strerror(errno));
+    cupsdLogJob(job, CUPSD_LOG_ERROR,
+               "Unable to open job control file \"%s\" - %s!",
+               jobfile, strerror(errno));
     ippDelete(job->attrs);
     job->attrs = NULL;
     return;
@@ -1021,9 +1085,9 @@ cupsdLoadJob(cupsd_job_t *job)            /* I - Job */
 
   if (ippReadIO(fp, (ipp_iocb_t)cupsFileRead, 1, NULL, job->attrs) != IPP_DATA)
   {
-    cupsdLogMessage(CUPSD_LOG_ERROR,
-                    "[Job %d] Unable to read job control file \"%s\"!",
-                   job->id, jobfile);
+    cupsdLogJob(job, CUPSD_LOG_ERROR,
+               "Unable to read job control file \"%s\"!",
+               jobfile);
     cupsFileClose(fp);
     ippDelete(job->attrs);
     job->attrs = NULL;
@@ -1040,10 +1104,8 @@ cupsdLoadJob(cupsd_job_t *job)           /* I - Job */
   if ((job->state = ippFindAttribute(job->attrs, "job-state",
                                      IPP_TAG_ENUM)) == NULL)
   {
-    cupsdLogMessage(CUPSD_LOG_ERROR,
-                   "[Job %d] Missing or bad job-state attribute in "
-                   "control file!",
-                   job->id);
+    cupsdLogJob(job, CUPSD_LOG_ERROR,
+               "Missing or bad job-state attribute in control file!");
     ippDelete(job->attrs);
     job->attrs = NULL;
     unlink(jobfile);
@@ -1057,9 +1119,8 @@ cupsdLoadJob(cupsd_job_t *job)            /* I - Job */
     if ((attr = ippFindAttribute(job->attrs, "job-printer-uri",
                                  IPP_TAG_URI)) == NULL)
     {
-      cupsdLogMessage(CUPSD_LOG_ERROR,
-                     "[Job %d] No job-printer-uri attribute in control file!",
-                     job->id);
+      cupsdLogJob(job, CUPSD_LOG_ERROR,
+                 "No job-printer-uri attribute in control file!");
       ippDelete(job->attrs);
       job->attrs = NULL;
       unlink(jobfile);
@@ -1069,9 +1130,9 @@ cupsdLoadJob(cupsd_job_t *job)            /* I - Job */
     if ((dest = cupsdValidateDest(attr->values[0].string.text, &(job->dtype),
                                   &destptr)) == NULL)
     {
-      cupsdLogMessage(CUPSD_LOG_ERROR,
-                     "[Job %d] Unable to queue job for destination \"%s\"!",
-                     job->id, attr->values[0].string.text);
+      cupsdLogJob(job, CUPSD_LOG_ERROR,
+                 "Unable to queue job for destination \"%s\"!",
+                 attr->values[0].string.text);
       ippDelete(job->attrs);
       job->attrs = NULL;
       unlink(jobfile);
@@ -1082,9 +1143,8 @@ cupsdLoadJob(cupsd_job_t *job)            /* I - Job */
   }
   else if ((destptr = cupsdFindDest(job->dest)) == NULL)
   {
-    cupsdLogMessage(CUPSD_LOG_ERROR,
-                   "[Job %d] Unable to queue job for destination \"%s\"!",
-                   job->id, job->dest);
+    cupsdLogJob(job, CUPSD_LOG_ERROR,
+               "Unable to queue job for destination \"%s\"!", job->dest);
     ippDelete(job->attrs);
     job->attrs = NULL;
     unlink(jobfile);
@@ -1100,9 +1160,8 @@ cupsdLoadJob(cupsd_job_t *job)            /* I - Job */
     if ((attr = ippFindAttribute(job->attrs, "job-priority",
                                 IPP_TAG_INTEGER)) == NULL)
     {
-      cupsdLogMessage(CUPSD_LOG_ERROR,
-                     "[Job %d] Missing or bad job-priority attribute in "
-                     "control file!", job->id);
+      cupsdLogJob(job, CUPSD_LOG_ERROR,
+                 "Missing or bad job-priority attribute in control file!");
       ippDelete(job->attrs);
       job->attrs = NULL;
       unlink(jobfile);
@@ -1117,9 +1176,9 @@ cupsdLoadJob(cupsd_job_t *job)            /* I - Job */
     if ((attr = ippFindAttribute(job->attrs, "job-originating-user-name",
                                 IPP_TAG_NAME)) == NULL)
     {
-      cupsdLogMessage(CUPSD_LOG_ERROR,
-                     "[Job %d] Missing or bad job-originating-user-name "
-                     "attribute in control file!", job->id);
+      cupsdLogJob(job, CUPSD_LOG_ERROR,
+                 "Missing or bad job-originating-user-name attribute in "
+                 "control file!");
       ippDelete(job->attrs);
       job->attrs = NULL;
       unlink(jobfile);
@@ -1167,9 +1226,8 @@ cupsdLoadJob(cupsd_job_t *job)            /* I - Job */
       if (access(jobfile, 0))
         break;
 
-      cupsdLogMessage(CUPSD_LOG_DEBUG,
-                      "[Job %d] Auto-typing document file \"%s\"...",
-                      job->id, jobfile);
+      cupsdLogJob(job, CUPSD_LOG_DEBUG,
+                 "Auto-typing document file \"%s\"...", jobfile);
 
       if (fileid > job->num_files)
       {
@@ -1189,9 +1247,8 @@ cupsdLoadJob(cupsd_job_t *job)            /* I - Job */
 
         if (!compressions || !filetypes)
        {
-          cupsdLogMessage(CUPSD_LOG_ERROR,
-                         "[Job %d] Ran out of memory for job file types!",
-                         job->id);
+          cupsdLogJob(job, CUPSD_LOG_ERROR,
+                     "Ran out of memory for job file types!");
          return;
        }
 
@@ -1308,7 +1365,8 @@ cupsdMoveJob(cupsd_job_t     *job,        /* I - Job */
                 "Job #%d moved from %s to %s.", job->id, olddest,
                p->name);
 
-  cupsdSaveJob(job);
+  job->dirty = 1;
+  cupsdMarkDirty(CUPSD_DIRTY_JOBS);
 }
 
 
@@ -1323,12 +1381,19 @@ cupsdReleaseJob(cupsd_job_t *job)       /* I - Job */
 
   if (job->state_value == IPP_JOB_HELD)
   {
+   /*
+    * Add trailing banner as needed...
+    */
+
+    if (job->pending_timeout)
+      cupsdTimeoutJob(job);
+
     DEBUG_puts("cupsdReleaseJob: setting state to pending...");
 
     job->state->values[0].integer = IPP_JOB_PENDING;
     job->state_value              = IPP_JOB_PENDING;
-    cupsdSaveJob(job);
-    cupsdCheckJobs();
+    job->dirty = 1;
+    cupsdMarkDirty(CUPSD_DIRTY_JOBS);
   }
 }
 
@@ -1355,12 +1420,11 @@ cupsdRestartJob(cupsd_job_t *job)       /* I - Job */
     job->state->values[0].integer = IPP_JOB_PENDING;
     job->state_value              = IPP_JOB_PENDING;
 
-    cupsdSaveJob(job);
+    job->dirty = 1;
+    cupsdMarkDirty(CUPSD_DIRTY_JOBS);
 
     if (old_state > IPP_JOB_STOPPED)
       cupsArrayAdd(ActiveJobs, job);
-
-    cupsdCheckJobs();
   }
 }
 
@@ -1453,9 +1517,9 @@ cupsdSaveJob(cupsd_job_t *job)            /* I - Job */
 
   if ((fp = cupsFileOpen(filename, "w")) == NULL)
   {
-    cupsdLogMessage(CUPSD_LOG_ERROR,
-                    "[Job %d] Unable to create job control file \"%s\" - %s.",
-                    job->id, filename, strerror(errno));
+    cupsdLogJob(job, CUPSD_LOG_ERROR,
+               "Unable to create job control file \"%s\" - %s.",
+               filename, strerror(errno));
     return;
   }
 
@@ -1466,10 +1530,11 @@ cupsdSaveJob(cupsd_job_t *job)          /* I - Job */
 
   if (ippWriteIO(fp, (ipp_iocb_t)cupsFileWrite, 1, NULL,
                  job->attrs) != IPP_DATA)
-    cupsdLogMessage(CUPSD_LOG_ERROR,
-                    "[Job %d] Unable to write job control file!", job->id);
+    cupsdLogJob(job, CUPSD_LOG_ERROR, "Unable to write job control file!");
 
   cupsFileClose(fp);
+
+  job->dirty = 0;
 }
 
 
@@ -1645,7 +1710,8 @@ cupsdSetJobPriority(
 
   cupsArrayAdd(ActiveJobs, job);
 
-  cupsdSaveJob(job);
+  job->dirty = 1;
+  cupsdMarkDirty(CUPSD_DIRTY_JOBS);
 }
 
 
@@ -1684,8 +1750,7 @@ cupsdStopJob(cupsd_job_t *job,            /* I - Job */
   int  i;                              /* Looping var */
 
 
-  cupsdLogMessage(CUPSD_LOG_DEBUG2,
-                  "[Job %d] cupsdStopJob: force = %d", job->id, force);
+  cupsdLogJob(job, CUPSD_LOG_DEBUG2, "cupsdStopJob: force = %d", force);
 
   if (job->state_value != IPP_JOB_PROCESSING)
     return;
@@ -1718,18 +1783,18 @@ cupsdStopJob(cupsd_job_t *job,          /* I - Job */
   cupsdDestroyProfile(job->profile);
   job->profile = NULL;
 
-  cupsdLogMessage(CUPSD_LOG_DEBUG2, "[Job %d] Closing print pipes [ %d %d ]...",
-                  job->id, job->print_pipes[0], job->print_pipes[1]);
+  cupsdLogJob(job, CUPSD_LOG_DEBUG2, "Closing print pipes [ %d %d ]...",
+             job->print_pipes[0], job->print_pipes[1]);
 
   cupsdClosePipe(job->print_pipes);
 
-  cupsdLogMessage(CUPSD_LOG_DEBUG2, "[Job %d] Closing back pipes [ %d %d ]...",
-                  job->id, job->back_pipes[0], job->back_pipes[1]);
+  cupsdLogJob(job, CUPSD_LOG_DEBUG2, "Closing back pipes [ %d %d ]...",
+             job->back_pipes[0], job->back_pipes[1]);
 
   cupsdClosePipe(job->back_pipes);
 
-  cupsdLogMessage(CUPSD_LOG_DEBUG2, "[Job %d] Closing side pipes [ %d %d ]...",
-                  job->id, job->side_pipes[0], job->side_pipes[1]);
+  cupsdLogJob(job, CUPSD_LOG_DEBUG2, "Closing side pipes [ %d %d ]...",
+             job->side_pipes[0], job->side_pipes[1]);
 
   cupsdClosePipe(job->side_pipes);
 
@@ -1741,9 +1806,8 @@ cupsdStopJob(cupsd_job_t *job,            /* I - Job */
 
     cupsdRemoveSelect(job->status_buffer->fd);
 
-    cupsdLogMessage(CUPSD_LOG_DEBUG2,
-                    "[Job %d] Closing status pipes [ %d %d ]...", job->id,
-                    job->status_pipes[0], job->status_pipes[1]);
+    cupsdLogJob(job, CUPSD_LOG_DEBUG2, "Closing status pipes [ %d %d ]...",
+               job->status_pipes[0], job->status_pipes[1]);
 
     cupsdClosePipe(job->status_pipes);
     cupsdStatBufDelete(job->status_buffer);
@@ -1771,7 +1835,12 @@ cupsdUnloadCompletedJobs(void)
        job = (cupsd_job_t *)cupsArrayNext(Jobs))
     if (job->attrs && job->state_value >= IPP_JOB_STOPPED &&
         job->access_time < expire)
+    {
+      if (job->dirty)
+        cupsdSaveJob(job);
+
       unload_job(job);
+    }
 }
 
 
@@ -1808,45 +1877,6 @@ compare_jobs(void *first,                /* I - First job */
 }
 
 
-/*
- * 'free_job()' - Free all memory used by a job.
- */
-
-static void
-free_job(cupsd_job_t *job)             /* I - Job */
-{
-  cupsdClearString(&job->username);
-  cupsdClearString(&job->dest);
-  cupsdClearString(&job->auth_username);
-  cupsdClearString(&job->auth_domain);
-  cupsdClearString(&job->auth_password);
-
-#ifdef HAVE_GSSAPI
- /*
-  * Destroy the credential cache and clear the KRB5CCNAME env var string.
-  */
-
-  if (job->ccache)
-  {
-    krb5_cc_destroy(KerberosContext, job->ccache);
-    job->ccache = NULL;
-  }
-
-  cupsdClearString(&job->ccname);
-#endif /* HAVE_GSSAPI */
-
-  if (job->num_files > 0)
-  {
-    free(job->compressions);
-    free(job->filetypes);
-  }
-
-  ippDelete(job->attrs);
-
-  free(job);
-}
-
-
 /*
  * 'ipp_length()' - Compute the size of the buffer needed to hold
  *                 the textual IPP attributes.
@@ -2055,7 +2085,7 @@ load_job_cache(const char *filename)      /* I - job.cache filename */
       if (!job)
       {
         cupsdLogMessage(CUPSD_LOG_EMERG,
-                       "[Job %d] Unable to allocate memory for job!", jobid);
+                       "[Job %d] Unable to allocate memory for job!", jobid);
         break;
       }
 
@@ -2069,7 +2099,7 @@ load_job_cache(const char *filename)      /* I - job.cache filename */
       job->status_pipes[0] = -1;
       job->status_pipes[1] = -1;
 
-      cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] Loading from cache...", job->id);
+      cupsdLogJob(job, CUPSD_LOG_DEBUG, "Loading from cache...");
     }
     else if (!job)
     {
@@ -2137,8 +2167,7 @@ load_job_cache(const char *filename)      /* I - job.cache filename */
                 job->id);
         if (access(jobfile, 0))
        {
-         cupsdLogMessage(CUPSD_LOG_INFO,
-                         "[Job %d] Data files have gone away!", job->id);
+         cupsdLogJob(job, CUPSD_LOG_INFO, "Data files have gone away!");
           job->num_files = 0;
          continue;
        }
@@ -2148,9 +2177,9 @@ load_job_cache(const char *filename)      /* I - job.cache filename */
 
         if (!job->filetypes || !job->compressions)
        {
-         cupsdLogMessage(CUPSD_LOG_EMERG,
-                         "[Job %d] Unable to allocate memory for %d files!",
-                         job->id, job->num_files);
+         cupsdLogJob(job, CUPSD_LOG_EMERG,
+                     "Unable to allocate memory for %d files!",
+                     job->num_files);
           break;
        }
       }
@@ -2188,9 +2217,9 @@ load_job_cache(const char *filename)      /* I - job.cache filename */
         * If the original MIME type is unknown, auto-type it!
        */
 
-        cupsdLogMessage(CUPSD_LOG_ERROR,
-                       "[Job %d] Unknown MIME type %s/%s for file %d!",
-                       job->id, super, type, number + 1);
+        cupsdLogJob(job, CUPSD_LOG_ERROR,
+                   "Unknown MIME type %s/%s for file %d!",
+                   super, type, number + 1);
 
         snprintf(jobfile, sizeof(jobfile), "%s/d%05d-%03d", RequestRoot,
                 job->id, number + 1);
@@ -2412,12 +2441,13 @@ set_hold_until(cupsd_job_t *job,        /* I - Job to update */
   */
 
   if (attr == NULL)
-    attr = ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_KEYWORD,
-                        "job-hold-until", NULL, holdstr);
+    ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_KEYWORD, "job-hold-until",
+                 NULL, holdstr);
   else
     cupsdSetString(&attr->values[0].string.text, holdstr);
 
-  cupsdSaveJob(job);
+  job->dirty = 1;
+  cupsdMarkDirty(CUPSD_DIRTY_JOBS);
 }
 
 
@@ -2480,14 +2510,12 @@ start_job(cupsd_job_t     *job,         /* I - Job ID */
   static int           optlength = 0;  /* Length of option buffer */
 
 
-  cupsdLogMessage(CUPSD_LOG_DEBUG2, "[Job %d] start_job: file = %d/%d",
-                  job->id, job->current_file, job->num_files);
+  cupsdLogJob(job, CUPSD_LOG_DEBUG2, "start_job: file = %d/%d",
+             job->current_file, job->num_files);
 
   if (job->num_files == 0)
   {
-    cupsdLogMessage(CUPSD_LOG_ERROR, "[Job %d] No files, canceling job!",
-                    job->id);
-
+    cupsdLogJob(job, CUPSD_LOG_ERROR, "No files, canceling job!");
     cupsdCancelJob(job, 0, IPP_JOB_ABORTED);
     return;
   }
@@ -2507,8 +2535,7 @@ start_job(cupsd_job_t     *job,           /* I - Job ID */
     * filtering...
     */
 
-    cupsdLogMessage(CUPSD_LOG_DEBUG,
-                    "[Job %d] Sending job to queue tagged as raw...", job->id);
+    cupsdLogJob(job, CUPSD_LOG_DEBUG, "Sending job to queue tagged as raw...");
 
     filters = NULL;
   }
@@ -2523,11 +2550,11 @@ start_job(cupsd_job_t     *job,         /* I - Job ID */
 
     if (!filters)
     {
-      cupsdLogMessage(CUPSD_LOG_ERROR,
-                      "[Job %d] Unable to convert file %d to printable format!",
-                     job->current_file, job->id);
+      cupsdLogJob(job, CUPSD_LOG_ERROR,
+                 "Unable to convert file %d to printable format!",
+                 job->current_file);
       cupsdLogMessage(CUPSD_LOG_INFO,
-                      "Hint: Do you have ESP Ghostscript installed?");
+                      "Hint: Do you have Ghostscript installed?");
 
       if (LogLevel < CUPSD_LOG_DEBUG)
         cupsdLogMessage(CUPSD_LOG_INFO,
@@ -2606,13 +2633,12 @@ start_job(cupsd_job_t     *job,         /* I - Job ID */
 
     cupsArrayDelete(filters);
 
-    cupsdLogMessage(CUPSD_LOG_INFO,
-                    "[Job %d] Holding because filter limit has been reached.",
-                    job->id);
-    cupsdLogMessage(CUPSD_LOG_DEBUG2,
-                    "[Job %d] start_job: file=%d, cost=%d, level=%d, limit=%d",
-                    job->id, job->current_file, job->cost, FilterLevel,
-                   FilterLimit);
+    cupsdLogJob(job, CUPSD_LOG_INFO,
+               "Holding because filter limit has been reached.");
+    cupsdLogJob(job, CUPSD_LOG_DEBUG2,
+               "start_job: file=%d, cost=%d, level=%d, limit=%d",
+               job->current_file, job->cost, FilterLevel,
+               FilterLimit);
     return;
   }
 
@@ -2635,9 +2661,8 @@ start_job(cupsd_job_t     *job,           /* I - Job ID */
 
     if (!cupsArrayInsert(filters, &gziptoany_filter))
     {
-      cupsdLogMessage(CUPSD_LOG_ERROR,
-                      "[Job %d] Unable to add decompression filter - %s",
-                     job->id, strerror(errno));
+      cupsdLogJob(job, CUPSD_LOG_ERROR,
+                 "Unable to add decompression filter - %s", strerror(errno));
 
       cupsArrayDelete(filters);
 
@@ -2663,11 +2688,17 @@ start_job(cupsd_job_t     *job,         /* I - Job ID */
     if (!filters)
       filters = cupsArrayNew(NULL, NULL);
 
+    port_monitor.src  = NULL;
+    port_monitor.dst  = NULL;
+    port_monitor.cost = 0;
+
+    snprintf(port_monitor.filter, sizeof(port_monitor.filter),
+             "%s/monitor/%s", ServerBin, printer->port_monitor);
+
     if (!cupsArrayAdd(filters, &port_monitor))
     {
-      cupsdLogMessage(CUPSD_LOG_ERROR,
-                      "[Job %d] Unable to add port monitor - %s",
-                      job->id, strerror(errno));
+      cupsdLogJob(job, CUPSD_LOG_ERROR,
+                 "Unable to add port monitor - %s", strerror(errno));
 
       cupsArrayDelete(filters);
 
@@ -2678,9 +2709,22 @@ start_job(cupsd_job_t     *job,          /* I - Job ID */
 
       return;
     }
+  }
 
-    snprintf(port_monitor.filter, sizeof(port_monitor.filter),
-             "%s/monitor/%s", ServerBin, printer->port_monitor);
+ /*
+  * Make sure we don't go over the "MAX_FILTERS" limit...
+  */
+
+  if (cupsArrayCount(filters) > MAX_FILTERS)
+  {
+    cupsdLogJob(job, CUPSD_LOG_ERROR,
+               "Too many filters (%d > %d), unable to print!",
+               cupsArrayCount(filters), MAX_FILTERS);
+
+    cupsArrayDelete(filters);
+    cupsdCancelJob(job, 0, IPP_JOB_STOPPED);
+
+    return;
   }
 
  /*
@@ -2698,6 +2742,13 @@ start_job(cupsd_job_t     *job,          /* I - Job ID */
 
   if (job->current_file == 0)
   {
+   /*
+    * Add to the printing list...
+    */
+
+    cupsArrayAdd(PrintingJobs, job);
+    cupsdSetBusyState();
+
    /*
     * Set the processing time...
     */
@@ -2735,21 +2786,19 @@ start_job(cupsd_job_t     *job,         /* I - Job ID */
 
   if (job->job_sheets == NULL)
   {
-    cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] No job-sheets attribute.",
-                    job->id);
+    cupsdLogJob(job, CUPSD_LOG_DEBUG, "No job-sheets attribute.");
     if ((job->job_sheets =
          ippFindAttribute(job->attrs, "job-sheets", IPP_TAG_ZERO)) != NULL)
-      cupsdLogMessage(CUPSD_LOG_DEBUG,
-                      "[Job %d] ... but someone added one without setting "
-                     "job_sheets!", job->id);
+      cupsdLogJob(job, CUPSD_LOG_DEBUG,
+                 "... but someone added one without setting job_sheets!");
   }
   else if (job->job_sheets->num_values == 1)
-    cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] job-sheets=%s", job->id,
-                    job->job_sheets->values[0].string.text);
+    cupsdLogJob(job, CUPSD_LOG_DEBUG, "job-sheets=%s",
+               job->job_sheets->values[0].string.text);
   else
-    cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] job-sheets=%s,%s", job->id,
-               job->job_sheets->values[0].string.text,
-               job->job_sheets->values[1].string.text);
+    cupsdLogJob(job, CUPSD_LOG_DEBUG, "job-sheets=%s,%s",
+                job->job_sheets->values[0].string.text,
+                job->job_sheets->values[1].string.text);
 
   if (printer->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT))
     banner_page = 0;
@@ -2765,8 +2814,7 @@ start_job(cupsd_job_t     *job,           /* I - Job ID */
   else
     banner_page = 0;
 
-  cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] banner_page = %d", job->id,
-                  banner_page);
+  cupsdLogJob(job, CUPSD_LOG_DEBUG, "banner_page = %d", banner_page);
 
  /*
   * Building the options string is harder than it needs to be, but
@@ -2778,7 +2826,7 @@ start_job(cupsd_job_t     *job,           /* I - Job ID */
 
   i = ipp_length(job->attrs);
 
-  if (i > optlength)
+  if (i > optlength || !options)
   {
     if (optlength == 0)
       optptr = malloc(i);
@@ -2787,9 +2835,8 @@ start_job(cupsd_job_t     *job,           /* I - Job ID */
 
     if (optptr == NULL)
     {
-      cupsdLogMessage(CUPSD_LOG_CRIT,
-                      "[Job %d] Unable to allocate %d bytes for option buffer!",
-                     job->id, i);
+      cupsdLogJob(job, CUPSD_LOG_CRIT,
+                 "Unable to allocate %d bytes for option buffer!", i);
 
       cupsArrayDelete(filters);
 
@@ -2848,18 +2895,22 @@ start_job(cupsd_job_t     *job,         /* I - Job ID */
        continue;
 
       if (!strncmp(attr->name, "job-", 4) && strcmp(attr->name, "job-uuid") &&
+          strcmp(attr->name, "job-impressions") &&
+          strcmp(attr->name, "job-originating-host-name") &&
           !(printer->type & CUPS_PRINTER_REMOTE))
        continue;
 
       if (!strncmp(attr->name, "job-", 4) &&
           strcmp(attr->name, "job-uuid") &&
           strcmp(attr->name, "job-billing") &&
+          strcmp(attr->name, "job-impressions") &&
           strcmp(attr->name, "job-sheets") &&
           strcmp(attr->name, "job-hold-until") &&
          strcmp(attr->name, "job-priority"))
        continue;
 
-      if ((!strcmp(attr->name, "page-label") ||
+      if ((!strcmp(attr->name, "job-impressions") ||
+           !strcmp(attr->name, "page-label") ||
            !strcmp(attr->name, "page-border") ||
            !strncmp(attr->name, "number-up", 9) ||
           !strcmp(attr->name, "page-ranges") ||
@@ -3014,8 +3065,8 @@ start_job(cupsd_job_t     *job,           /* I - Job ID */
   }
 
   for (i = 0; argv[i]; i ++)
-    cupsdLogMessage(CUPSD_LOG_DEBUG,
-                    "[Job %d] argv[%d]=\"%s\"", job->id, i, argv[i]);
+    cupsdLogJob(job, CUPSD_LOG_DEBUG,
+               "argv[%d]=\"%s\"", i, argv[i]);
 
  /*
   * Create environment variable strings for the filters...
@@ -3099,14 +3150,20 @@ start_job(cupsd_job_t     *job,         /* I - Job ID */
   envp[envc ++] = device_uri;
   envp[envc ++] = printer_name;
 
-  if (!printer->remote && !printer->raw &&
-      (filter = (mime_filter_t *)cupsArrayLast(filters)) != NULL &&
-      filter->dst)
+  if (!printer->remote && !printer->raw)
   {
-    snprintf(final_content_type, sizeof(final_content_type),
-             "FINAL_CONTENT_TYPE=%s/%s",
-            filter->dst->super, filter->dst->type);
-    envp[envc ++] = final_content_type;
+    filter = (mime_filter_t *)cupsArrayLast(filters);
+
+    if (printer->port_monitor)
+      filter = (mime_filter_t *)cupsArrayPrev(filters);
+
+    if (filter && filter->dst)
+    {
+      snprintf(final_content_type, sizeof(final_content_type),
+              "FINAL_CONTENT_TYPE=%s/%s",
+              filter->dst->super, filter->dst->type);
+      envp[envc ++] = final_content_type;
+    }
   }
 
   if (Classification && !banner_page)
@@ -3147,15 +3204,14 @@ start_job(cupsd_job_t     *job,         /* I - Job ID */
   envp[envc] = NULL;
 
   for (i = 0; i < envc; i ++)
-    if (!strncmp(envp[i], "CUPSD_AUTH_", 5))
-      cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] envp[%d]=\"CUPSD_AUTH_%c****\"",
-                      job->id, i, envp[i][5]);
+    if (!strncmp(envp[i], "AUTH_", 5))
+      cupsdLogJob(job, CUPSD_LOG_DEBUG, "envp[%d]=\"AUTH_%c****\"", i,
+                  envp[i][5]);
     else if (strncmp(envp[i], "DEVICE_URI=", 11))
-      cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] envp[%d]=\"%s\"",
-                      job->id, i, envp[i]);
+      cupsdLogJob(job, CUPSD_LOG_DEBUG, "envp[%d]=\"%s\"", i, envp[i]);
     else
-      cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] envp[%d]=\"DEVICE_URI=%s\"",
-                      job->id, i, sani_uri);
+      cupsdLogJob(job, CUPSD_LOG_DEBUG, "envp[%d]=\"DEVICE_URI=%s\"", i,
+                  sani_uri);
 
   if (printer->remote)
     job->current_file = job->num_files;
@@ -3175,9 +3231,8 @@ start_job(cupsd_job_t     *job,           /* I - Job ID */
   {
     if (cupsdOpenPipe(job->status_pipes))
     {
-      cupsdLogMessage(CUPSD_LOG_ERROR,
-                      "[Job %d] Unable to create job status pipes - %s.",
-                     job->id, strerror(errno));
+      cupsdLogJob(job, CUPSD_LOG_ERROR,
+                 "Unable to create job status pipes - %s.", strerror(errno));
       snprintf(printer->state_message, sizeof(printer->state_message),
               "Unable to create status pipes - %s.", strerror(errno));
 
@@ -3190,12 +3245,11 @@ start_job(cupsd_job_t     *job,         /* I - Job ID */
       goto abort_job;
     }
 
-    cupsdLogMessage(CUPSD_LOG_DEBUG2,
-                    "[Job %d] start_job: status_pipes = [ %d %d ]",
-                   job->id, job->status_pipes[0], job->status_pipes[1]);
+    cupsdLogJob(job, CUPSD_LOG_DEBUG2,
+               "start_job: status_pipes = [ %d %d ]",
+               job->status_pipes[0], job->status_pipes[1]);
 
-    job->status_buffer = cupsdStatBufNew(job->status_pipes[0], "[Job %d]",
-                                        job->id);
+    job->status_buffer = cupsdStatBufNew(job->status_pipes[0], NULL);
     job->status_level  = CUPSD_LOG_INFO;
   }
 
@@ -3219,9 +3273,8 @@ start_job(cupsd_job_t     *job,           /* I - Job ID */
     {
       if (cupsdOpenPipe(filterfds[slot]))
       {
-       cupsdLogMessage(CUPSD_LOG_ERROR,
-                       "[Job %d] Unable to create job filter pipes - %s.",
-                       job->id, strerror(errno));
+       cupsdLogJob(job, CUPSD_LOG_ERROR,
+                   "Unable to create job filter pipes - %s.", strerror(errno));
        snprintf(printer->state_message, sizeof(printer->state_message),
                "Unable to create filter pipes - %s.", strerror(errno));
        cupsdAddPrinterHistory(printer);
@@ -3241,9 +3294,9 @@ start_job(cupsd_job_t     *job,           /* I - Job ID */
        {
          if (cupsdOpenPipe(job->print_pipes))
          {
-           cupsdLogMessage(CUPSD_LOG_ERROR,
-                           "[Job %d] Unable to create job backend pipes - %s.",
-                           job->id, strerror(errno));
+           cupsdLogJob(job, CUPSD_LOG_ERROR,
+                       "Unable to create job backend pipes - %s.",
+                       strerror(errno));
            snprintf(printer->state_message, sizeof(printer->state_message),
                    "Unable to create backend pipes - %s.", strerror(errno));
            cupsdAddPrinterHistory(printer);
@@ -3278,9 +3331,9 @@ start_job(cupsd_job_t     *job,           /* I - Job ID */
 
            if (job->print_pipes[1] < 0)
            {
-              cupsdLogMessage(CUPSD_LOG_ERROR,
-                             "[Job %d] Unable to open output file \"%s\" - %s.",
-                             job->id, printer->device_uri, strerror(errno));
+              cupsdLogJob(job, CUPSD_LOG_ERROR,
+                         "Unable to open output file \"%s\" - %s.",
+                         printer->device_uri, strerror(errno));
               snprintf(printer->state_message, sizeof(printer->state_message),
                       "Unable to open output file \"%s\" - %s.",
                       printer->device_uri, strerror(errno));
@@ -3297,38 +3350,34 @@ start_job(cupsd_job_t     *job,         /* I - Job ID */
           }
        }
 
-       cupsdLogMessage(CUPSD_LOG_DEBUG2,
-                       "[Job %d] start_job: print_pipes = [ %d %d ]",
-                        job->id, job->print_pipes[0], job->print_pipes[1]);
+       cupsdLogJob(job, CUPSD_LOG_DEBUG2,
+                   "start_job: print_pipes = [ %d %d ]",
+                   job->print_pipes[0], job->print_pipes[1]);
       }
 
       filterfds[slot][0] = job->print_pipes[0];
       filterfds[slot][1] = job->print_pipes[1];
     }
 
-    cupsdLogMessage(CUPSD_LOG_DEBUG2, "[Job %d] start_job: filter=\"%s\"",
-                    job->id, command);
-    cupsdLogMessage(CUPSD_LOG_DEBUG2,
-                    "[Job %d] start_job: filterfds[%d]=[ %d %d ]",
-                    job->id, slot, filterfds[slot][0], filterfds[slot][1]);
+    cupsdLogJob(job, CUPSD_LOG_DEBUG2, "start_job: filter=\"%s\"", command);
+    cupsdLogJob(job, CUPSD_LOG_DEBUG2, "start_job: filterfds[%d]=[ %d %d ]",
+               slot, filterfds[slot][0], filterfds[slot][1]);
 
     pid = cupsdStartProcess(command, argv, envp, filterfds[!slot][0],
                             filterfds[slot][1], job->status_pipes[1],
                            job->back_pipes[0], job->side_pipes[0], 0,
                            job->profile, job->filters + i);
 
-    cupsdLogMessage(CUPSD_LOG_DEBUG2,
-                    "[Job %d] start_job: Closing filter pipes for slot %d "
-                   "[ %d %d ]...",
-                    job->id, !slot, filterfds[!slot][0], filterfds[!slot][1]);
+    cupsdLogJob(job, CUPSD_LOG_DEBUG2,
+               "start_job: Closing filter pipes for slot %d [ %d %d ]...",
+               !slot, filterfds[!slot][0], filterfds[!slot][1]);
 
     cupsdClosePipe(filterfds[!slot]);
 
     if (pid == 0)
     {
-      cupsdLogMessage(CUPSD_LOG_ERROR,
-                      "[Job %d] Unable to start filter \"%s\" - %s.",
-                      job->id, filter->filter, strerror(errno));
+      cupsdLogJob(job, CUPSD_LOG_ERROR, "Unable to start filter \"%s\" - %s.",
+                 filter->filter, strerror(errno));
       snprintf(printer->state_message, sizeof(printer->state_message),
                "Unable to start filter \"%s\" - %s.",
                filter->filter, strerror(errno));
@@ -3342,14 +3391,15 @@ start_job(cupsd_job_t     *job,         /* I - Job ID */
       goto abort_job;
     }
 
-    cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Started filter %s (PID %d)",
-                    job->id, command, pid);
+    cupsdLogJob(job, CUPSD_LOG_INFO, "Started filter %s (PID %d)", command,
+                pid);
 
     argv[6] = NULL;
     slot    = !slot;
   }
 
   cupsArrayDelete(filters);
+  filters = NULL;
 
  /*
   * Finally, pipe the final output into a backend process if needed...
@@ -3357,7 +3407,7 @@ start_job(cupsd_job_t     *job,           /* I - Job ID */
 
   if (strncmp(printer->device_uri, "file:", 5) != 0)
   {
-    if (job->current_file == 1)
+    if (job->current_file == 1 || printer->remote)
     {
       sscanf(printer->device_uri, "%254[^:]", method);
       snprintf(command, sizeof(command), "%s/backend/%s", ServerBin, method);
@@ -3378,11 +3428,9 @@ start_job(cupsd_job_t     *job,          /* I - Job ID */
       filterfds[slot][0] = -1;
       filterfds[slot][1] = -1;
 
-      cupsdLogMessage(CUPSD_LOG_DEBUG2, "[Job %d] start_job: backend=\"%s\"",
-                      job->id, command);
-      cupsdLogMessage(CUPSD_LOG_DEBUG2,
-                      "[Job %d] start_job: filterfds[%d] = [ %d %d ]", job->id,
-                     slot, filterfds[slot][0], filterfds[slot][1]);
+      cupsdLogJob(job, CUPSD_LOG_DEBUG2, "start_job: backend=\"%s\"", command);
+      cupsdLogJob(job, CUPSD_LOG_DEBUG2, "start_job: filterfds[%d] = [ %d %d ]",
+                 slot, filterfds[slot][0], filterfds[slot][1]);
 
       pid = cupsdStartProcess(command, argv, envp, filterfds[!slot][0],
                              filterfds[slot][1], job->status_pipes[1],
@@ -3391,9 +3439,8 @@ start_job(cupsd_job_t     *job,           /* I - Job ID */
 
       if (pid == 0)
       {
-       cupsdLogMessage(CUPSD_LOG_ERROR,
-                       "[Job %d] Unable to start backend \"%s\" - %s.",
-                       job->id, method, strerror(errno));
+       cupsdLogJob(job, CUPSD_LOG_ERROR, "Unable to start backend \"%s\" - %s.",
+                   method, strerror(errno));
        snprintf(printer->state_message, sizeof(printer->state_message),
                 "Unable to start backend \"%s\" - %s.", method,
                 strerror(errno));
@@ -3406,34 +3453,34 @@ start_job(cupsd_job_t     *job,         /* I - Job ID */
       }
       else
       {
-       cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Started backend %s (PID %d)",
-                       job->id, command, pid);
+       cupsdLogJob(job, CUPSD_LOG_INFO, "Started backend %s (PID %d)",
+                   command, pid);
       }
     }
 
     if (job->current_file == job->num_files)
     {
-      cupsdLogMessage(CUPSD_LOG_DEBUG2,
-                      "[Job %d] start_job: Closing print pipes [ %d %d ]...",
-                     job->id, job->print_pipes[0], job->print_pipes[1]);
+      cupsdLogJob(job, CUPSD_LOG_DEBUG2,
+                 "start_job: Closing print pipes [ %d %d ]...",
+                 job->print_pipes[0], job->print_pipes[1]);
 
       cupsdClosePipe(job->print_pipes);
 
-      cupsdLogMessage(CUPSD_LOG_DEBUG2,
-                      "[Job %d] start_job: Closing back pipes [ %d %d ]...",
-                     job->id, job->back_pipes[0], job->back_pipes[1]);
+      cupsdLogJob(job, CUPSD_LOG_DEBUG2,
+                 "start_job: Closing back pipes [ %d %d ]...",
+                 job->back_pipes[0], job->back_pipes[1]);
 
       cupsdClosePipe(job->back_pipes);
 
-      cupsdLogMessage(CUPSD_LOG_DEBUG2,
-                      "[Job %d] start_job: Closing side pipes [ %d %d ]...",
-                     job->id, job->side_pipes[0], job->side_pipes[1]);
+      cupsdLogJob(job, CUPSD_LOG_DEBUG2,
+                 "start_job: Closing side pipes [ %d %d ]...",
+                 job->side_pipes[0], job->side_pipes[1]);
 
       cupsdClosePipe(job->side_pipes);
 
-      cupsdLogMessage(CUPSD_LOG_DEBUG2,
-                     "[Job %d] start_job: Closing status output pipe %d...",
-                     job->id, job->status_pipes[1]);
+      cupsdLogJob(job, CUPSD_LOG_DEBUG2,
+                 "start_job: Closing status output pipe %d...",
+                 job->status_pipes[1]);
 
       close(job->status_pipes[1]);
       job->status_pipes[1] = -1;
@@ -3446,25 +3493,24 @@ start_job(cupsd_job_t     *job,         /* I - Job ID */
 
     if (job->current_file == job->num_files)
     {
-      cupsdLogMessage(CUPSD_LOG_DEBUG2,
-                      "[Job %d] start_job: Closing print pipes [ %d %d ]...",
-                     job->id, job->print_pipes[0], job->print_pipes[1]);
+      cupsdLogJob(job, CUPSD_LOG_DEBUG2,
+                 "start_job: Closing print pipes [ %d %d ]...",
+                 job->print_pipes[0], job->print_pipes[1]);
 
       cupsdClosePipe(job->print_pipes);
 
-      cupsdLogMessage(CUPSD_LOG_DEBUG2,
-                     "[Job %d] start_job: Closing status output pipe %d...",
-                     job->id, job->status_pipes[1]);
+      cupsdLogJob(job, CUPSD_LOG_DEBUG2,
+                 "start_job: Closing status output pipe %d...",
+                 job->status_pipes[1]);
 
       close(job->status_pipes[1]);
       job->status_pipes[1] = -1;
     }
   }
 
-  cupsdLogMessage(CUPSD_LOG_DEBUG2,
-                 "[Job %d] start_job: Closing filter pipes for slot %d "
-                 "[ %d %d ]...",
-                 job->id, slot, filterfds[slot][0], filterfds[slot][1]);
+  cupsdLogJob(job, CUPSD_LOG_DEBUG2,
+             "start_job: Closing filter pipes for slot %d [ %d %d ]...",
+             slot, filterfds[slot][0], filterfds[slot][1]);
   cupsdClosePipe(filterfds[slot]);
 
   if (printer->remote && job->num_files > 1)
@@ -3493,16 +3539,15 @@ start_job(cupsd_job_t     *job,         /* I - Job ID */
 
   for (slot = 0; slot < 2; slot ++)
   {
-    cupsdLogMessage(CUPSD_LOG_DEBUG2,
-                    "[Job %d] start_job: Closing filter pipes for slot %d "
-                   "[ %d %d ]...",
-                    job->id, slot, filterfds[slot][0], filterfds[slot][1]);
+    cupsdLogJob(job, CUPSD_LOG_DEBUG2,
+               "start_job: Closing filter pipes for slot %d [ %d %d ]...",
+               slot, filterfds[slot][0], filterfds[slot][1]);
     cupsdClosePipe(filterfds[slot]);
   }
 
-  cupsdLogMessage(CUPSD_LOG_DEBUG2,
-                  "[Job %d] start_job: Closing status pipes [ %d %d ]...",
-                  job->id, job->status_pipes[0], job->status_pipes[1]);
+  cupsdLogJob(job, CUPSD_LOG_DEBUG2,
+             "start_job: Closing status pipes [ %d %d ]...",
+             job->status_pipes[0], job->status_pipes[1]);
   cupsdClosePipe(job->status_pipes);
   cupsdStatBufDelete(job->status_buffer);
 
@@ -3532,7 +3577,7 @@ unload_job(cupsd_job_t *job)              /* I - Job */
   if (!job->attrs)
     return;
 
-  cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] Unloading...", job->id);
+  cupsdLogJob(job, CUPSD_LOG_DEBUG, "Unloading...");
 
   ippDelete(job->attrs);
 
@@ -3574,6 +3619,8 @@ update_job(cupsd_job_t *job)              /* I - Job to check */
       * job sheet count...
       */
 
+      cupsdLogJob(job, CUPSD_LOG_DEBUG, "PAGE: %s", message);
+
       if (job->sheets)
       {
         if (!strncasecmp(message, "total ", 6))
@@ -3602,11 +3649,11 @@ update_job(cupsd_job_t *job)            /* I - Job to check */
            * Quota limit exceeded, cancel job in progress immediately...
            */
 
-           cupsdLogMessage(CUPSD_LOG_INFO,
-                           "[Job %d] Canceled because pages exceed user %s "
-                           "quota limit on printer %s (%s).",
-                           job->id, job->username, job->printer->name,
-                           job->printer->info);
+           cupsdLogJob(job, CUPSD_LOG_INFO,
+                       "Canceled because pages exceed user %s "
+                       "quota limit on printer %s (%s).",
+                       job->username, job->printer->name,
+                       job->printer->info);
 
            cupsdCancelJob(job, 1, IPP_JOB_CANCELED);
            return;
@@ -3625,6 +3672,8 @@ update_job(cupsd_job_t *job)              /* I - Job to check */
     }
     else if (loglevel == CUPSD_LOG_STATE)
     {
+      cupsdLogJob(job, CUPSD_LOG_DEBUG, "STATE: %s", message);
+
       if (!strcmp(message, "paused"))
       {
         cupsdStopPrinter(job->printer, 1);
@@ -3634,10 +3683,10 @@ update_job(cupsd_job_t *job)            /* I - Job to check */
       {
        cupsdSetPrinterReasons(job->printer, message);
        cupsdAddPrinterHistory(job->printer);
-       event |= CUPSD_EVENT_PRINTER_STATE_CHANGED;
+       event |= CUPSD_EVENT_PRINTER_STATE;
       }
 
-      update_job_attrs(job);
+      update_job_attrs(job, 0);
     }
     else if (loglevel == CUPSD_LOG_ATTR)
     {
@@ -3650,6 +3699,8 @@ update_job(cupsd_job_t *job)              /* I - Job to check */
       const char       *attr;          /* Attribute */
 
 
+      cupsdLogJob(job, CUPSD_LOG_DEBUG, "ATTR: %s", message);
+
       num_attrs = cupsParseOptions(message, 0, &attrs);
 
       if ((attr = cupsGetOption("auth-info-required", num_attrs,
@@ -3657,24 +3708,82 @@ update_job(cupsd_job_t *job)            /* I - Job to check */
       {
         cupsdSetAuthInfoRequired(job->printer, attr, NULL);
        cupsdSetPrinterAttrs(job->printer);
-       cupsdSaveAllPrinters();
+
+       if (job->printer->type & CUPS_PRINTER_DISCOVERED)
+         cupsdMarkDirty(CUPSD_DIRTY_REMOTE);
+       else
+         cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
       }
 
       if ((attr = cupsGetOption("printer-alert", num_attrs, attrs)) != NULL)
       {
         cupsdSetString(&job->printer->alert, attr);
-       event |= CUPSD_EVENT_PRINTER_STATE_CHANGED;
+       event |= CUPSD_EVENT_PRINTER_STATE;
       }
 
       if ((attr = cupsGetOption("printer-alert-description", num_attrs,
                                 attrs)) != NULL)
       {
         cupsdSetString(&job->printer->alert_description, attr);
-       event |= CUPSD_EVENT_PRINTER_STATE_CHANGED;
+       event |= CUPSD_EVENT_PRINTER_STATE;
+      }
+
+      if ((attr = cupsGetOption("marker-colors", num_attrs, attrs)) != NULL)
+      {
+        cupsdSetPrinterAttr(job->printer, "marker-colors", (char *)attr);
+       job->printer->marker_time = time(NULL);
+       event |= CUPSD_EVENT_PRINTER_STATE;
+      }
+
+      if ((attr = cupsGetOption("marker-levels", num_attrs, attrs)) != NULL)
+      {
+        cupsdSetPrinterAttr(job->printer, "marker-levels", (char *)attr);
+       job->printer->marker_time = time(NULL);
+       event |= CUPSD_EVENT_PRINTER_STATE;
+      }
+
+      if ((attr = cupsGetOption("marker-message", num_attrs, attrs)) != NULL)
+      {
+        cupsdSetPrinterAttr(job->printer, "marker-message", (char *)attr);
+       job->printer->marker_time = time(NULL);
+       event |= CUPSD_EVENT_PRINTER_STATE;
+      }
+
+      if ((attr = cupsGetOption("marker-names", num_attrs, attrs)) != NULL)
+      {
+        cupsdSetPrinterAttr(job->printer, "marker-names", (char *)attr);
+       job->printer->marker_time = time(NULL);
+       event |= CUPSD_EVENT_PRINTER_STATE;
+      }
+
+      if ((attr = cupsGetOption("marker-types", num_attrs, attrs)) != NULL)
+      {
+        cupsdSetPrinterAttr(job->printer, "marker-types", (char *)attr);
+       job->printer->marker_time = time(NULL);
+       event |= CUPSD_EVENT_PRINTER_STATE;
       }
 
       cupsFreeOptions(num_attrs, attrs);
     }
+    else if (loglevel == CUPSD_LOG_PPD)
+    {
+     /*
+      * Set attribute(s)...
+      */
+
+      int              num_keywords;   /* Number of keywords */
+      cups_option_t    *keywords;      /* Keywords */
+
+
+      cupsdLogJob(job, CUPSD_LOG_DEBUG, "PPD: %s", message);
+
+      num_keywords = cupsParseOptions(message, 0, &keywords);
+
+      if (cupsdUpdatePrinterPPD(job->printer, num_keywords, keywords))
+        cupsdSetPrinterAttrs(job->printer);
+
+      cupsFreeOptions(num_keywords, keywords);
+    }
 #ifdef __APPLE__
     else if (!strncmp(message, "recoverable:", 12))
     {
@@ -3687,7 +3796,7 @@ update_job(cupsd_job_t *job)              /* I - Job to check */
 
       cupsdSetString(&job->printer->recoverable, ptr);
       cupsdAddPrinterHistory(job->printer);
-      event |= CUPSD_EVENT_PRINTER_STATE_CHANGED;
+      event |= CUPSD_EVENT_PRINTER_STATE;
     }
     else if (!strncmp(message, "recovered:", 10))
     {
@@ -3700,32 +3809,41 @@ update_job(cupsd_job_t *job)            /* I - Job to check */
 
       cupsdSetString(&job->printer->recoverable, ptr);
       cupsdAddPrinterHistory(job->printer);
-      event |= CUPSD_EVENT_PRINTER_STATE_CHANGED;
+      event |= CUPSD_EVENT_PRINTER_STATE;
     }
 #endif /* __APPLE__ */
-    else if (loglevel <= job->status_level)
+    else
     {
-     /*
-      * Some message to show in the printer-state-message attribute...
-      */
+      cupsdLogJob(job, loglevel, "%s", message);
+
+      if (loglevel < CUPSD_LOG_DEBUG)
+      {
+       strlcpy(job->printer->state_message, message,
+               sizeof(job->printer->state_message));
+       cupsdAddPrinterHistory(job->printer);
 
-      if (loglevel != CUPSD_LOG_NOTICE)
-        job->status_level = loglevel;
+       event |= CUPSD_EVENT_PRINTER_STATE;
 
-      strlcpy(job->printer->state_message, message,
-              sizeof(job->printer->state_message));
-      cupsdAddPrinterHistory(job->printer);
-      event |= CUPSD_EVENT_PRINTER_STATE_CHANGED;
+       if (loglevel <= job->status_level)
+       {
+        /*
+         * Some messages show in the printer-state-message attribute...
+         */
 
-      update_job_attrs(job);
+         if (loglevel != CUPSD_LOG_NOTICE)
+           job->status_level = loglevel;
+
+         update_job_attrs(job, 1);
+       }
+      }
     }
 
     if (!strchr(job->status_buffer->buffer, '\n'))
       break;
   }
 
-  if ((event & CUPSD_EVENT_PRINTER_STATE_CHANGED))
-    cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE_CHANGED, job->printer, NULL,
+  if (event & CUPSD_EVENT_PRINTER_STATE)
+    cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, job->printer, NULL,
                  (job->printer->type & CUPS_PRINTER_CLASS) ?
                      "Class \"%s\" state changed." :
                      "Printer \"%s\" state changed.",
@@ -3760,7 +3878,8 @@ update_job(cupsd_job_t *job)              /* I - Job to check */
  */
 
 void
-update_job_attrs(cupsd_job_t *job)     /* I - Job to update */
+update_job_attrs(cupsd_job_t *job,     /* I - Job to update */
+                 int         do_message)/* I - 1 = update job-printer-state message */
 {
   int                  i;              /* Looping var */
   int                  num_reasons;    /* Actual number of reasons */
@@ -3800,7 +3919,8 @@ update_job_attrs(cupsd_job_t *job)        /* I - Job to update */
   * Otherwise copy the printer-state-message value...
   */
 
-  if (job->printer->state_message[0])
+  if (job->printer->state_message[0] &&
+      (do_message || !job->printer_message->values[0].string.text[0]))
     cupsdSetString(&(job->printer_message->values[0].string.text),
                   job->printer->state_message);
 
@@ -3835,5 +3955,5 @@ update_job_attrs(cupsd_job_t *job)        /* I - Job to update */
 
 
 /*
- * End of "$Id: job.c 7005 2007-10-01 23:45:48Z mike $".
+ * End of "$Id: job.c 7682 2008-06-21 00:06:02Z mike $".
  */