If signing starts successfully, don't just turn it off automatically if
[jra/samba/.git] / source3 / printing / printing.c
index 2121fb20a370eb0c58eff730e87b7f7bdebfed7e..ad17213c2d65f0d0efeb83cb0d3cad1f446b03fd 100644 (file)
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
 
+#include "includes.h"
 #include "printing.h"
 
 /* Current printer interface */
 static struct printif *current_printif = &generic_printif;
+static BOOL remove_from_jobs_changed(int snum, uint32 jobid);
 
 /* 
    the printing backend revolves around a tdb database that stores the
@@ -53,6 +55,8 @@ uint16 pjobid_to_rap(int snum, uint32 jobid)
        TDB_DATA data, key;
        char jinfo[8];
 
+       DEBUG(10,("pjobid_to_rap: called.\n"));
+
        if (!rap_tdb) {
                /* Create the in-memory tdb. */
                rap_tdb = tdb_open_log(NULL, 0, TDB_INTERNAL, (O_RDWR|O_CREAT), 0644);
@@ -69,8 +73,12 @@ uint16 pjobid_to_rap(int snum, uint32 jobid)
        if (data.dptr && data.dsize == sizeof(uint16)) {
                memcpy(&rap_jobid, data.dptr, sizeof(uint16));
                SAFE_FREE(data.dptr);
+               DEBUG(10,("pjobid_to_rap: jobid %u maps to RAP jobid %u\n",
+                               (unsigned int)jobid,
+                               (unsigned int)rap_jobid));
                return rap_jobid;
        }
+       SAFE_FREE(data.dptr);
        /* Not found - create and store mapping. */
        rap_jobid = ++next_rap_jobid;
        if (rap_jobid == 0)
@@ -79,13 +87,18 @@ uint16 pjobid_to_rap(int snum, uint32 jobid)
        data.dsize = sizeof(rap_jobid);
        tdb_store(rap_tdb, key, data, TDB_REPLACE);
        tdb_store(rap_tdb, data, key, TDB_REPLACE);
+
+       DEBUG(10,("pjobid_to_rap: created jobid %u maps to RAP jobid %u\n",
+                               (unsigned int)jobid,
+                               (unsigned int)rap_jobid));
        return rap_jobid;
 }
 
 BOOL rap_to_pjobid(uint16 rap_jobid, int *psnum, uint32 *pjobid)
 {
        TDB_DATA data, key;
-       char jinfo[8];
+
+       DEBUG(10,("rap_to_pjobid called.\n"));
 
        if (!rap_tdb)
                return False;
@@ -93,12 +106,19 @@ BOOL rap_to_pjobid(uint16 rap_jobid, int *psnum, uint32 *pjobid)
        key.dptr = (char *)&rap_jobid;
        key.dsize = sizeof(rap_jobid);
        data = tdb_fetch(rap_tdb, key);
-       if (data.dptr && data.dsize == sizeof(jinfo)) {
-               *psnum = IVAL(&jinfo,0);
-               *pjobid = IVAL(&jinfo,4);
+       if (data.dptr && data.dsize == 8) {
+               *psnum = IVAL(data.dptr,0);
+               *pjobid = IVAL(data.dptr,4);
+               DEBUG(10,("rap_to_pjobid: jobid %u maps to RAP jobid %u\n",
+                               (unsigned int)*pjobid,
+                               (unsigned int)rap_jobid));
                SAFE_FREE(data.dptr);
                return True;
        }
+
+       DEBUG(10,("rap_to_pjobid: Failed to lookup RAP jobid %u\n",
+                               (unsigned int)rap_jobid));
+       SAFE_FREE(data.dptr);
        return False;
 }
 
@@ -108,6 +128,8 @@ static void rap_jobid_delete(int snum, uint32 jobid)
        uint16 rap_jobid;
        char jinfo[8];
 
+       DEBUG(10,("rap_jobid_delete: called.\n"));
+
        if (!rap_tdb)
                return;
 
@@ -117,8 +139,15 @@ static void rap_jobid_delete(int snum, uint32 jobid)
        key.dptr = (char *)&jinfo;
        key.dsize = sizeof(jinfo);
        data = tdb_fetch(rap_tdb, key);
-       if (!data.dptr || (data.dsize != sizeof(uint16)))
+       if (!data.dptr || (data.dsize != sizeof(uint16))) {
+               DEBUG(10,("rap_jobid_delete: cannot find jobid %u\n",
+                                       (unsigned int)jobid ));
+               SAFE_FREE(data.dptr);
                return;
+       }
+
+       DEBUG(10,("rap_jobid_delete: deleting jobid %u\n",
+                               (unsigned int)jobid ));
 
        memcpy(&rap_jobid, data.dptr, sizeof(uint16));
        SAFE_FREE(data.dptr);
@@ -132,142 +161,13 @@ static pid_t local_pid;
 
 static int get_queue_status(int, print_status_struct *);
 
-/* There can be this many printing tdb's open, plus any locked ones. */
-#define MAX_PRINT_DBS_OPEN 1
-
-struct tdb_print_db {
-       struct tdb_print_db *next, *prev;
-       TDB_CONTEXT *tdb;
-       int ref_count;
-       fstring printer_name;
-};
-
-static struct tdb_print_db *print_db_head;
-
-/****************************************************************************
-  Function to find or create the printer specific job tdb given a printername.
-  Limits the number of tdb's open to MAX_PRINT_DBS_OPEN.
-****************************************************************************/
-
-static struct tdb_print_db *get_print_db_byname(const char *printername)
-{
-       struct tdb_print_db *p = NULL, *last_entry = NULL;
-       int num_open = 0;
-       pstring printdb_path;
-       BOOL done_become_root = False;
-
-       for (p = print_db_head, last_entry = print_db_head; p; p = p->next) {
-               /* Ensure the list terminates... JRA. */
-               SMB_ASSERT(p->next != print_db_head);
-
-               if (p->tdb && strequal(p->printer_name, printername)) {
-                       DLIST_PROMOTE(print_db_head, p);
-                       p->ref_count++;
-                       return p;
-               }
-               num_open++;
-               last_entry = p;
-       }
-
-       /* Not found. */
-       if (num_open >= MAX_PRINT_DBS_OPEN) {
-               /* Try and recycle the last entry. */
-               DLIST_PROMOTE(print_db_head, last_entry);
-
-               for (p = print_db_head; p; p = p->next) {
-                       if (p->ref_count)
-                               continue;
-                       if (p->tdb) {
-                               if (tdb_close(print_db_head->tdb)) {
-                                       DEBUG(0,("get_print_db: Failed to close tdb for printer %s\n",
-                                                               print_db_head->printer_name ));
-                                       return NULL;
-                               }
-                       }
-                       p->tdb = NULL;
-                       p->ref_count = 0;
-                       memset(p->printer_name, '\0', sizeof(p->printer_name));
-                       break;
-               }
-               if (p) {
-                       DLIST_PROMOTE(print_db_head, p);
-                       p = print_db_head;
-               }
-       }
-       
-       if (!p) {
-               /* Create one. */
-               p = (struct tdb_print_db *)malloc(sizeof(struct tdb_print_db));
-               if (!p) {
-                       DEBUG(0,("get_print_db: malloc fail !\n"));
-                       return NULL;
-               }
-               ZERO_STRUCTP(p);
-               DLIST_ADD(print_db_head, p);
-       }
-
-       pstrcpy(printdb_path, lock_path("printing/"));
-       pstrcat(printdb_path, printername);
-       pstrcat(printdb_path, ".tdb");
-
-       if (geteuid() != 0) {
-               become_root();
-               done_become_root = True;
-       }
-
-       p->tdb = tdb_open_log(printdb_path, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
-
-       if (done_become_root)
-               unbecome_root();
-
-       if (!p->tdb) {
-               DEBUG(0,("get_print_db: Failed to open printer backend database %s.\n",
-                                       printdb_path ));
-               DLIST_REMOVE(print_db_head, p);
-               SAFE_FREE(p);
-               return NULL;
-       }
-       fstrcpy(p->printer_name, printername);
-       p->ref_count++;
-       return p;
-}
-
-/***************************************************************************
- Remove a reference count.
-****************************************************************************/
-
-static void release_print_db( struct tdb_print_db *pdb)
-{
-       pdb->ref_count--;
-       SMB_ASSERT(pdb->ref_count >= 0);
-}
-
-/***************************************************************************
- Close all open print db entries.
-****************************************************************************/
-
-static void close_all_print_db(void)
-{
-       struct tdb_print_db *p = NULL, *next_p = NULL;
-
-       for (p = print_db_head; p; p = next_p) {
-               next_p = p->next;
-
-               if (p->tdb)
-                       tdb_close(p->tdb);
-               DLIST_REMOVE(print_db_head, p);
-               ZERO_STRUCTP(p);
-               SAFE_FREE(p);
-       }
-}
-
 /****************************************************************************
  Initialise the printing backend. Called once at startup before the fork().
 ****************************************************************************/
 
 BOOL print_backend_init(void)
 {
-       char *sversion = "INFO/version";
+       const char *sversion = "INFO/version";
        pstring printing_path;
        int services = lp_numservices();
        int snum;
@@ -348,20 +248,22 @@ int unpack_pjob( char* buf, int buflen, struct printjob *pjob )
 {
        int     len = 0;
        int     used;
-       
+       uint32 pjpid, pjsysjob, pjfd, pjstarttime, pjstatus;
+       uint32 pjsize, pjpage_count, pjspooled, pjsmbjob;
+
        if ( !buf || !pjob )
                return -1;
                
        len += tdb_unpack(buf+len, buflen-len, "dddddddddffff",
-                               &pjob->pid,
-                               &pjob->sysjob,
-                               &pjob->fd,
-                               &pjob->starttime,
-                               &pjob->status,
-                               &pjob->size,
-                               &pjob->page_count,
-                               &pjob->spooled,
-                               &pjob->smbjob,
+                               &pjpid,
+                               &pjsysjob,
+                               &pjfd,
+                               &pjstarttime,
+                               &pjstatus,
+                               &pjsize,
+                               &pjpage_count,
+                               &pjspooled,
+                               &pjsmbjob,
                                pjob->filename,
                                pjob->jobname,
                                pjob->user,
@@ -374,6 +276,16 @@ int unpack_pjob( char* buf, int buflen, struct printjob *pjob )
                return -1;
        
        len += used;
+
+       pjob->pid = pjpid;
+       pjob->sysjob = pjsysjob;
+       pjob->fd = pjfd;
+       pjob->starttime = pjstarttime;
+       pjob->status = pjstatus;
+       pjob->size = pjsize;
+       pjob->page_count = pjpage_count;
+       pjob->spooled = pjspooled;
+       pjob->smbjob = pjsmbjob;
        
        return len;
 
@@ -404,8 +316,10 @@ static struct printjob *print_job_find(int snum, uint32 jobid)
                
        ZERO_STRUCT( pjob );
        
-       if ( unpack_pjob( ret.dptr, ret.dsize, &pjob ) == -1 )
+       if ( unpack_pjob( ret.dptr, ret.dsize, &pjob ) == -1 ) {
+               SAFE_FREE(ret.dptr);
                return NULL;
+       }
        
        SAFE_FREE(ret.dptr);    
        return &pjob;
@@ -564,15 +478,15 @@ static BOOL pjob_store(int snum, uint32 jobid, struct printjob *pjob)
                len = 0;
                buflen = newlen;
                len += tdb_pack(buf+len, buflen-len, "dddddddddffff",
-                               pjob->pid,
-                               pjob->sysjob,
-                               pjob->fd,
-                               pjob->starttime,
-                               pjob->status,
-                               pjob->size,
-                               pjob->page_count,
-                               pjob->spooled,
-                               pjob->smbjob,
+                               (uint32)pjob->pid,
+                               (uint32)pjob->sysjob,
+                               (uint32)pjob->fd,
+                               (uint32)pjob->starttime,
+                               (uint32)pjob->status,
+                               (uint32)pjob->size,
+                               (uint32)pjob->page_count,
+                               (uint32)pjob->spooled,
+                               (uint32)pjob->smbjob,
                                pjob->filename,
                                pjob->jobname,
                                pjob->user,
@@ -580,8 +494,7 @@ static BOOL pjob_store(int snum, uint32 jobid, struct printjob *pjob)
 
                len += pack_devicemode(pjob->nt_devmode, buf+len, buflen-len);
        
-               if (buflen != len) 
-               {
+               if (buflen != len) {
                        char *tb;
 
                        tb = (char *)Realloc(buf, len);
@@ -593,8 +506,7 @@ static BOOL pjob_store(int snum, uint32 jobid, struct printjob *pjob)
                                buf = tb;
                        newlen = len;
                }
-       }
-       while ( buflen != len );
+       } while ( buflen != len );
                
        
        /* Store new data */
@@ -621,7 +533,7 @@ done:
  Remove a job structure from the database.
 ****************************************************************************/
 
-static void pjob_delete(int snum, uint32 jobid)
+void pjob_delete(int snum, uint32 jobid)
 {
        struct printjob *pjob = print_job_find(snum, jobid);
        uint32 job_status = 0;
@@ -681,11 +593,13 @@ static uint32 print_parse_jobid(char *fname)
  List a unix job in the print database.
 ****************************************************************************/
 
-static void print_unix_job(int snum, print_queue_struct *q)
+static void print_unix_job(int snum, print_queue_struct *q, uint32 jobid)
 {
-       uint32 jobid = q->job + UNIX_JOB_START;
        struct printjob pj, *old_pj;
 
+       if (jobid == (uint32)-1)
+               jobid = q->job + UNIX_JOB_START;
+
        /* Preserve the timestamp on an existing unix print job */
 
        old_pj = print_job_find(snum, jobid);
@@ -699,11 +613,14 @@ static void print_unix_job(int snum, print_queue_struct *q)
        pj.status = q->status;
        pj.size = q->size;
        pj.spooled = True;
-       pj.smbjob = False;
-       fstrcpy(pj.filename, "");
-       fstrcpy(pj.jobname, q->fs_file);
-       fstrcpy(pj.user, q->fs_user);
-       fstrcpy(pj.queuename, lp_const_servicename(snum));
+       pj.smbjob = (old_pj != NULL ? True : False);
+       fstrcpy(pj.filename, old_pj ? old_pj->filename : "");
+       if (jobid < UNIX_JOB_START)
+               fstrcpy(pj.jobname, old_pj ? old_pj->jobname : "Remote Downlevel Document");
+       else
+               fstrcpy(pj.jobname, old_pj ? old_pj->jobname : q->fs_file);
+       fstrcpy(pj.user, old_pj ? old_pj->user : q->fs_user);
+       fstrcpy(pj.queuename, old_pj ? old_pj->queuename : lp_const_servicename(snum));
 
        pjob_store(snum, jobid, &pj);
 }
@@ -712,6 +629,7 @@ static void print_unix_job(int snum, print_queue_struct *q)
 struct traverse_struct {
        print_queue_struct *queue;
        int qcount, snum, maxcount, total_jobs;
+       time_t lpq_time;
 };
 
 /****************************************************************************
@@ -747,9 +665,11 @@ static int traverse_fn_delete(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void
                        if (jobid == u_jobid)
                                break;
                }
-               if (i == ts->qcount)
+               if (i == ts->qcount) {
+                       DEBUG(10,("traverse_fn_delete: pjob %u deleted due to !smbjob\n",
+                                               (unsigned int)jobid ));
                        pjob_delete(ts->snum, jobid);
-               else
+               else
                        ts->total_jobs++;
                return 0;
        }
@@ -759,9 +679,11 @@ static int traverse_fn_delete(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void
                /* if a job is not spooled and the process doesn't
                    exist then kill it. This cleans up after smbd
                    deaths */
-               if (!process_exists(pjob.pid))
+               if (!process_exists(pjob.pid)) {
+                       DEBUG(10,("traverse_fn_delete: pjob %u deleted due to !process_exists (%u)\n",
+                                               (unsigned int)jobid, (unsigned int)pjob.pid ));
                        pjob_delete(ts->snum, jobid);
-               else
+               else
                        ts->total_jobs++;
                return 0;
        }
@@ -776,23 +698,36 @@ static int traverse_fn_delete(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void
           completed, so delete the database entry. */
 
        if (i == ts->qcount) {
-               time_t cur_t = time(NULL);
 
                /* A race can occur between the time a job is spooled and
                   when it appears in the lpq output.  This happens when
                   the job is added to printing.tdb when another smbd
                   running print_queue_update() has completed a lpq and
                   is currently traversing the printing tdb and deleting jobs.
-                  A workaround is to not delete the job if it has been 
-                  submitted less than lp_lpqcachetime() seconds ago. */
+                  Don't delete the job if it was submitted after the lpq_time. */
 
-               if ((cur_t - pjob.starttime) > lp_lpqcachetime())
+               if (pjob.starttime < ts->lpq_time) {
+                       DEBUG(10,("traverse_fn_delete: pjob %u deleted due to pjob.starttime (%u) < ts->lpq_time (%u)\n",
+                                               (unsigned int)jobid,
+                                               (unsigned int)pjob.starttime,
+                                               (unsigned int)ts->lpq_time ));
                        pjob_delete(ts->snum, jobid);
-               else
+               else
                        ts->total_jobs++;
+               return 0;
        }
-       else
-               ts->total_jobs++;
+
+       /* Save the pjob attributes we will store. */
+       ts->queue[i].job = jobid;
+       ts->queue[i].size = pjob.size;
+       ts->queue[i].page_count = pjob.page_count;
+       ts->queue[i].status = pjob.status;
+       ts->queue[i].priority = 1;
+       ts->queue[i].time = pjob.starttime;
+       fstrcpy(ts->queue[i].fs_user, pjob.user);
+       fstrcpy(ts->queue[i].fs_file, pjob.jobname);
+
+       ts->total_jobs++;
 
        return 0;
 }
@@ -833,8 +768,10 @@ static pid_t get_updating_pid(fstring printer_name)
 
        data = tdb_fetch(pdb->tdb, key);
        release_print_db(pdb);
-       if (!data.dptr || data.dsize != sizeof(pid_t))
+       if (!data.dptr || data.dsize != sizeof(pid_t)) {
+               SAFE_FREE(data.dptr);
                return (pid_t)-1;
+       }
 
        memcpy(&updating_pid, data.dptr, sizeof(pid_t));
        SAFE_FREE(data.dptr);
@@ -878,6 +815,116 @@ static void set_updating_pid(const fstring printer_name, BOOL delete)
        release_print_db(pdb);
 }
 
+/****************************************************************************
+ Sort print jobs by submittal time.
+****************************************************************************/
+
+static int printjob_comp(print_queue_struct *j1, print_queue_struct *j2)
+{
+       /* Silly cases */
+
+       if (!j1 && !j2)
+               return 0;
+       if (!j1)
+               return -1;
+       if (!j2)
+               return 1;
+
+       /* Sort on job start time */
+
+       if (j1->time == j2->time)
+               return 0;
+       return (j1->time > j2->time) ? 1 : -1;
+}
+
+/****************************************************************************
+ Store the sorted queue representation for later portmon retrieval.
+****************************************************************************/
+
+static void store_queue_struct(struct tdb_print_db *pdb, struct traverse_struct *pts)
+{
+       TDB_DATA data, key;
+       int max_reported_jobs = lp_max_reported_jobs(pts->snum);
+       print_queue_struct *queue = pts->queue;
+       size_t len;
+       size_t i;
+       uint qcount;
+
+       if (max_reported_jobs && (max_reported_jobs < pts->qcount))
+               pts->qcount = max_reported_jobs;
+       qcount = pts->qcount;
+
+       /* Work out the size. */
+       data.dsize = 0;
+       data.dsize += tdb_pack(NULL, 0, "d", qcount);
+
+       for (i = 0; i < pts->qcount; i++) {
+               data.dsize += tdb_pack(NULL, 0, "ddddddff",
+                               (uint32)queue[i].job,
+                               (uint32)queue[i].size,
+                               (uint32)queue[i].page_count,
+                               (uint32)queue[i].status,
+                               (uint32)queue[i].priority,
+                               (uint32)queue[i].time,
+                               queue[i].fs_user,
+                               queue[i].fs_file);
+       }
+
+       if ((data.dptr = malloc(data.dsize)) == NULL)
+               return;
+
+        len = 0;
+       len += tdb_pack(data.dptr + len, data.dsize - len, "d", qcount);
+       for (i = 0; i < pts->qcount; i++) {
+               len += tdb_pack(data.dptr + len, data.dsize - len, "ddddddff",
+                               (uint32)queue[i].job,
+                               (uint32)queue[i].size,
+                               (uint32)queue[i].page_count,
+                               (uint32)queue[i].status,
+                               (uint32)queue[i].priority,
+                               (uint32)queue[i].time,
+                               queue[i].fs_user,
+                               queue[i].fs_file);
+       }
+
+       key.dptr = "INFO/linear_queue_array";
+       key.dsize = strlen(key.dptr);
+       tdb_store(pdb->tdb, key, data, TDB_REPLACE);
+       SAFE_FREE(data.dptr);
+       return;
+}
+
+static TDB_DATA get_jobs_changed_data(struct tdb_print_db *pdb)
+{
+       TDB_DATA data, key;
+
+       key.dptr = "INFO/jobs_changed";
+       key.dsize = strlen(key.dptr);
+       ZERO_STRUCT(data);
+
+       data = tdb_fetch(pdb->tdb, key);
+       if (data.dptr == NULL || data.dsize == 0 || (data.dsize % 4 != 0)) {
+               SAFE_FREE(data.dptr);
+               ZERO_STRUCT(data);
+       }
+
+       return data;
+}
+
+static void check_job_changed(int snum, TDB_DATA data, uint32 jobid)
+{
+       unsigned int i;
+       unsigned int job_count = data.dsize / 4;
+
+       for (i = 0; i < job_count; i++) {
+               uint32 ch_jobid;
+
+               memcpy(&ch_jobid, data.dptr + (i*4), 4);
+               if (ch_jobid == jobid)
+                       remove_from_jobs_changed(snum, jobid);
+       }
+}
+
 /****************************************************************************
  Update the internal database from the system print queue for a queue.
 ****************************************************************************/
@@ -892,6 +939,7 @@ static void print_queue_update(int snum)
        struct traverse_struct tstruct;
        fstring keystr, printer_name, cachestr;
        TDB_DATA data, key;
+       TDB_DATA jcdata;
        struct tdb_print_db *pdb;
 
        fstrcpy(printer_name, lp_const_servicename(snum));
@@ -965,6 +1013,12 @@ static void print_queue_update(int snum)
        DEBUG(3, ("%d job%s in queue for %s\n", qcount, (qcount != 1) ?
                "s" : "", printer_name));
 
+       /* Sort the queue by submission time otherwise they are displayed
+          in hash order. */
+
+       qsort(queue, qcount, sizeof(print_queue_struct),
+             QSORT_CAST(printjob_comp));
+
        /*
          any job in the internal database that is marked as spooled
          and doesn't exist in the system queue is considered finished
@@ -975,12 +1029,15 @@ static void print_queue_update(int snum)
 
          fill in any system job numbers as we go
        */
+
+       jcdata = get_jobs_changed_data(pdb);
+
        for (i=0; i<qcount; i++) {
                uint32 jobid = print_parse_jobid(queue[i].fs_file);
 
                if (jobid == (uint32)-1) {
                        /* assume its a unix print job */
-                       print_unix_job(snum, &queue[i]);
+                       print_unix_job(snum, &queue[i], jobid);
                        continue;
                }
 
@@ -990,30 +1047,40 @@ static void print_queue_update(int snum)
                        /* err, somethings wrong. Probably smbd was restarted
                           with jobs in the queue. All we can do is treat them
                           like unix jobs. Pity. */
-                       print_unix_job(snum, &queue[i]);
+                       print_unix_job(snum, &queue[i], jobid);
                        continue;
                }
 
                pjob->sysjob = queue[i].job;
                pjob->status = queue[i].status;
-
                pjob_store(snum, jobid, pjob);
+               check_job_changed(snum, jcdata, jobid);
        }
 
+       SAFE_FREE(jcdata.dptr);
+
        /* now delete any queued entries that don't appear in the
            system queue */
        tstruct.queue = queue;
        tstruct.qcount = qcount;
        tstruct.snum = snum;
        tstruct.total_jobs = 0;
+       tstruct.lpq_time = time(NULL);
 
        tdb_traverse(pdb->tdb, traverse_fn_delete, (void *)&tstruct);
 
+       /* Store the linearised queue, max jobs only. */
+       store_queue_struct(pdb, &tstruct);
+
        SAFE_FREE(tstruct.queue);
 
+       DEBUG(10,("print_queue_update: printer %s INFO/total_jobs = %d\n",
+                               printer_name, tstruct.total_jobs ));
+
        tdb_store_int32(pdb->tdb, "INFO/total_jobs", tstruct.total_jobs);
 
-       if( qcount != get_queue_status(snum, &old_status))
+       get_queue_status(snum, &old_status);
+       if (old_status.qcount != qcount)
                DEBUG(10,("print_queue_update: queue status change %d jobs -> %d jobs for printer %s\n",
                                        old_status.qcount, qcount, printer_name ));
 
@@ -1040,6 +1107,187 @@ static void print_queue_update(int snum)
        release_print_db(pdb);
 }
 
+/****************************************************************************
+ Create/Update an entry in the print tdb that will allow us to send notify
+ updates only to interested smbd's. 
+****************************************************************************/
+
+BOOL print_notify_register_pid(int snum)
+{
+       TDB_DATA data;
+       struct tdb_print_db *pdb = NULL;
+       TDB_CONTEXT *tdb = NULL;
+       const char *printername;
+       uint32 mypid = (uint32)sys_getpid();
+       BOOL ret = False;
+       size_t i;
+
+       /* if (snum == -1), then the change notify request was
+          on a print server handle and we need to register on
+          all print queus */
+          
+       if (snum == -1) 
+       {
+               int num_services = lp_numservices();
+               int idx;
+               
+               for ( idx=0; idx<num_services; idx++ ) {
+                       if (lp_snum_ok(idx) && lp_print_ok(idx) )
+                               print_notify_register_pid(idx);
+               }
+               
+               return True;
+       }
+       else /* register for a specific printer */
+       {
+               printername = lp_const_servicename(snum);
+               pdb = get_print_db_byname(printername);
+               if (!pdb)
+                       return False;
+               tdb = pdb->tdb;
+       }
+
+       if (tdb_lock_bystring(tdb, NOTIFY_PID_LIST_KEY, 10) == -1) {
+               DEBUG(0,("print_notify_register_pid: Failed to lock printer %s\n",
+                                       printername));
+               if (pdb)
+                       release_print_db(pdb);
+               return False;
+       }
+
+       data = get_printer_notify_pid_list( tdb, printername, True );
+
+       /* Add ourselves and increase the refcount. */
+
+       for (i = 0; i < data.dsize; i += 8) {
+               if (IVAL(data.dptr,i) == mypid) {
+                       uint32 new_refcount = IVAL(data.dptr, i+4) + 1;
+                       SIVAL(data.dptr, i+4, new_refcount);
+                       break;
+               }
+       }
+
+       if (i == data.dsize) {
+               /* We weren't in the list. Realloc. */
+               data.dptr = Realloc(data.dptr, data.dsize + 8);
+               if (!data.dptr) {
+                       DEBUG(0,("print_notify_register_pid: Relloc fail for printer %s\n",
+                                               printername));
+                       goto done;
+               }
+               data.dsize += 8;
+               SIVAL(data.dptr,data.dsize - 8,mypid);
+               SIVAL(data.dptr,data.dsize - 4,1); /* Refcount. */
+       }
+
+       /* Store back the record. */
+       if (tdb_store_bystring(tdb, NOTIFY_PID_LIST_KEY, data, TDB_REPLACE) == -1) {
+               DEBUG(0,("print_notify_register_pid: Failed to update pid \
+list for printer %s\n", printername));
+               goto done;
+       }
+
+       ret = True;
+
+ done:
+
+       tdb_unlock_bystring(tdb, NOTIFY_PID_LIST_KEY);
+       if (pdb)
+               release_print_db(pdb);
+       SAFE_FREE(data.dptr);
+       return ret;
+}
+
+/****************************************************************************
+ Update an entry in the print tdb that will allow us to send notify
+ updates only to interested smbd's. 
+****************************************************************************/
+
+BOOL print_notify_deregister_pid(int snum)
+{
+       TDB_DATA data;
+       struct tdb_print_db *pdb = NULL;
+       TDB_CONTEXT *tdb = NULL;
+       const char *printername;
+       uint32 mypid = (uint32)sys_getpid();
+       size_t i;
+       BOOL ret = False;
+
+       /* if ( snum == -1 ), we are deregister a print server handle
+          which means to deregister on all print queues */
+          
+       if (snum == -1) 
+       {
+               int num_services = lp_numservices();
+               int idx;
+               
+               for ( idx=0; idx<num_services; idx++ ) {
+                       if ( lp_snum_ok(idx) && lp_print_ok(idx) )
+                               print_notify_deregister_pid(idx);
+               }
+               
+               return True;
+       }
+       else /* deregister a specific printer */
+       {
+               printername = lp_const_servicename(snum);
+               pdb = get_print_db_byname(printername);
+               if (!pdb)
+                       return False;
+               tdb = pdb->tdb;
+       }
+
+       if (tdb_lock_bystring(tdb, NOTIFY_PID_LIST_KEY, 10) == -1) {
+               DEBUG(0,("print_notify_register_pid: Failed to lock \
+printer %s database\n", printername));
+               if (pdb)
+                       release_print_db(pdb);
+               return False;
+       }
+
+       data = get_printer_notify_pid_list( tdb, printername, True );
+
+       /* Reduce refcount. Remove ourselves if zero. */
+
+       for (i = 0; i < data.dsize; ) {
+               if (IVAL(data.dptr,i) == mypid) {
+                       uint32 refcount = IVAL(data.dptr, i+4);
+
+                       refcount--;
+
+                       if (refcount == 0) {
+                               if (data.dsize - i > 8)
+                                       memmove( &data.dptr[i], &data.dptr[i+8], data.dsize - i - 8);
+                               data.dsize -= 8;
+                               continue;
+                       }
+                       SIVAL(data.dptr, i+4, refcount);
+               }
+
+               i += 8;
+       }
+
+       if (data.dsize == 0)
+               SAFE_FREE(data.dptr);
+
+       /* Store back the record. */
+       if (tdb_store_bystring(tdb, NOTIFY_PID_LIST_KEY, data, TDB_REPLACE) == -1) {
+               DEBUG(0,("print_notify_register_pid: Failed to update pid \
+list for printer %s\n", printername));
+               goto done;
+       }
+
+       ret = True;
+
+  done:
+
+       tdb_unlock_bystring(tdb, NOTIFY_PID_LIST_KEY);
+       if (pdb)
+               release_print_db(pdb);
+       SAFE_FREE(data.dptr);
+       return ret;
+}
+
 /****************************************************************************
  Check if a jobid is valid. It is valid if it exists in the database.
 ****************************************************************************/
@@ -1126,6 +1374,62 @@ BOOL print_job_set_name(int snum, uint32 jobid, char *name)
        return pjob_store(snum, jobid, pjob);
 }
 
+/***************************************************************************
+ Remove a jobid from the 'jobs changed' list.
+***************************************************************************/
+
+static BOOL remove_from_jobs_changed(int snum, uint32 jobid)
+{
+       const char *printername = lp_const_servicename(snum);
+       struct tdb_print_db *pdb = get_print_db_byname(printername);
+       TDB_DATA data, key;
+       size_t job_count, i;
+       BOOL ret = False;
+       BOOL gotlock = False;
+
+       key.dptr = "INFO/jobs_changed";
+       key.dsize = strlen(key.dptr);
+       ZERO_STRUCT(data);
+
+       if (tdb_chainlock_with_timeout(pdb->tdb, key, 5) == -1)
+               goto out;
+
+       gotlock = True;
+
+       data = tdb_fetch(pdb->tdb, key);
+
+       if (data.dptr == NULL || data.dsize == 0 || (data.dsize % 4 != 0))
+               goto out;
+
+       job_count = data.dsize / 4;
+       for (i = 0; i < job_count; i++) {
+               uint32 ch_jobid;
+
+               memcpy(&ch_jobid, data.dptr + (i*4), 4);
+               if (ch_jobid == jobid) {
+                       if (i < job_count -1 )
+                               memmove(data.dptr + (i*4), data.dptr + (i*4) + 4, (job_count - i - 1)*4 );
+                       data.dsize -= 4;
+                       if (tdb_store(pdb->tdb, key, data, TDB_REPLACE) == -1)
+                               goto out;
+                       break;
+               }
+       }
+
+       ret = True;
+  out:
+
+       if (gotlock)
+               tdb_chainunlock(pdb->tdb, key);
+       SAFE_FREE(data.dptr);
+       release_print_db(pdb);
+       if (ret)
+               DEBUG(10,("remove_from_jobs_changed: removed jobid %u\n", (unsigned int)jobid ));
+       else
+               DEBUG(10,("remove_from_jobs_changed: Failed to remove jobid %u\n", (unsigned int)jobid ));
+       return ret;
+}
+
 /****************************************************************************
  Delete a print job - don't update queue.
 ****************************************************************************/
@@ -1159,12 +1463,24 @@ static BOOL print_job_delete1(int snum, uint32 jobid)
 
        if (pjob->spooled && pjob->sysjob != -1)
                result = (*(current_printif->job_delete))(snum, pjob);
+       else
+               remove_from_jobs_changed(snum, jobid);
 
-       /* Delete the tdb entry if the delete suceeded or the job hasn't
+       /* Delete the tdb entry if the delete succeeded or the job hasn't
           been spooled. */
 
-       if (result == 0)
+       if (result == 0) {
+               const char *printername = lp_const_servicename(snum);
+               struct tdb_print_db *pdb = get_print_db_byname(printername);
+               int njobs = 1;
+
+               if (!pdb)
+                       return False;
                pjob_delete(snum, jobid);
+               /* Ensure we keep a rough count of the number of total jobs... */
+               tdb_change_int32_atomic(pdb->tdb, "INFO/total_jobs", &njobs, -1);
+               release_print_db(pdb);
+       }
 
        return (result == 0);
 }
@@ -1208,6 +1524,14 @@ BOOL print_job_delete(struct current_user *user, int snum, uint32 jobid, WERROR
            !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
                DEBUG(3, ("delete denied by security descriptor\n"));
                *errcode = WERR_ACCESS_DENIED;
+
+               /* BEGIN_ADMIN_LOG */
+               sys_adminlog( LOG_ERR, 
+                             "Permission denied-- user not allowed to delete, \
+pause, or resume print job. User name: %s. Printer name: %s.",
+                             uidtoname(user->uid), PRINTERNAME(snum) );
+               /* END_ADMIN_LOG */
+
                return False;
        }
 
@@ -1265,6 +1589,14 @@ BOOL print_job_pause(struct current_user *user, int snum, uint32 jobid, WERROR *
        if (!is_owner(user, snum, jobid) &&
            !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
                DEBUG(3, ("pause denied by security descriptor\n"));
+
+               /* BEGIN_ADMIN_LOG */
+               sys_adminlog( LOG_ERR, 
+                       "Permission denied-- user not allowed to delete, \
+pause, or resume print job. User name: %s. Printer name: %s.",
+                               uidtoname(user->uid), PRINTERNAME(snum) );
+               /* END_ADMIN_LOG */
+
                *errcode = WERR_ACCESS_DENIED;
                return False;
        }
@@ -1308,6 +1640,13 @@ BOOL print_job_resume(struct current_user *user, int snum, uint32 jobid, WERROR
            !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
                DEBUG(3, ("resume denied by security descriptor\n"));
                *errcode = WERR_ACCESS_DENIED;
+
+               /* BEGIN_ADMIN_LOG */
+               sys_adminlog( LOG_ERR, 
+                        "Permission denied-- user not allowed to delete, \
+pause, or resume print job. User name: %s. Printer name: %s.",
+                       uidtoname(user->uid), PRINTERNAME(snum) );
+               /* END_ADMIN_LOG */
                return False;
        }
 
@@ -1400,21 +1739,26 @@ static int get_queue_status(int snum, print_status_struct *status)
        TDB_DATA data, key;
        const char *printername = lp_const_servicename(snum);
        struct tdb_print_db *pdb = get_print_db_byname(printername);
+       int len;
+
        if (!pdb)
                return 0;
 
-       ZERO_STRUCTP(status);
-       slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", printername);
-       key.dptr = keystr;
-       key.dsize = strlen(keystr);
-       data = tdb_fetch(pdb->tdb, key);
-       release_print_db(pdb);
-       if (data.dptr) {
-               if (data.dsize == sizeof(print_status_struct))
-                       memcpy(status, data.dptr, sizeof(print_status_struct));
-               SAFE_FREE(data.dptr);
+       if (status) {
+               ZERO_STRUCTP(status);
+               slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", printername);
+               key.dptr = keystr;
+               key.dsize = strlen(keystr);
+               data = tdb_fetch(pdb->tdb, key);
+               if (data.dptr) {
+                       if (data.dsize == sizeof(print_status_struct))
+                               memcpy(status, data.dptr, sizeof(print_status_struct));
+                       SAFE_FREE(data.dptr);
+               }
        }
-       return status->qcount;
+       len = tdb_fetch_int32(pdb->tdb, "INFO/total_jobs");
+       release_print_db(pdb);
+       return (len == -1 ? 0 : len);
 }
 
 /****************************************************************************
@@ -1433,11 +1777,97 @@ int print_queue_length(int snum, print_status_struct *pstatus)
        /* also fetch the queue status */
        memset(&status, 0, sizeof(status));
        len = get_queue_status(snum, &status);
+
        if (pstatus)
                *pstatus = status;
+
        return len;
 }
 
+/***************************************************************************
+ Allocate a jobid. Hold the lock for as short a time as possible.
+***************************************************************************/
+
+static BOOL allocate_print_jobid(struct tdb_print_db *pdb, int snum, const char *printername, uint32 *pjobid)
+{
+       int i;
+       uint32 jobid;
+
+       *pjobid = (uint32)-1;
+
+       for (i = 0; i < 3; i++) {
+               /* Lock the database - only wait 20 seconds. */
+               if (tdb_lock_bystring(pdb->tdb, "INFO/nextjob", 20) == -1) {
+                       DEBUG(0,("allocate_print_jobid: failed to lock printing database %s\n", printername ));
+                       return False;
+               }
+
+               if (!tdb_fetch_uint32(pdb->tdb, "INFO/nextjob", &jobid)) {
+                       if (tdb_error(pdb->tdb) != TDB_ERR_NOEXIST) {
+                               DEBUG(0, ("allocate_print_jobid: failed to fetch INFO/nextjob for print queue %s\n",
+                                               printername ));
+                               return False;
+                       }
+                       jobid = 0;
+               }
+
+               jobid = NEXT_JOBID(jobid);
+
+               if (tdb_store_int32(pdb->tdb, "INFO/nextjob", jobid)==-1) {
+                       DEBUG(3, ("allocate_print_jobid: failed to store INFO/nextjob.\n"));
+                       tdb_unlock_bystring(pdb->tdb, "INFO/nextjob");
+                       return False;
+               }
+
+               /* We've finished with the INFO/nextjob lock. */
+               tdb_unlock_bystring(pdb->tdb, "INFO/nextjob");
+                               
+               if (!print_job_exists(snum, jobid))
+                       break;
+       }
+
+       if (i > 2) {
+               DEBUG(0, ("allocate_print_jobid: failed to allocate a print job for queue %s\n",
+                               printername ));
+               /* Probably full... */
+               errno = ENOSPC;
+               return False;
+       }
+
+       /* Store a dummy placeholder. */
+       {
+               TDB_DATA dum;
+               dum.dptr = NULL;
+               dum.dsize = 0;
+               if (tdb_store(pdb->tdb, print_key(jobid), dum, TDB_INSERT) == -1) {
+                       DEBUG(3, ("allocate_print_jobid: jobid (%d) failed to store placeholder.\n",
+                               jobid ));
+                       return False;
+               }
+       }
+
+       *pjobid = jobid;
+       return True;
+}
+
+/***************************************************************************
+ Append a jobid to the 'jobs changed' list.
+***************************************************************************/
+
+static BOOL add_to_jobs_changed(struct tdb_print_db *pdb, uint32 jobid)
+{
+       TDB_DATA data, key;
+
+       key.dptr = "INFO/jobs_changed";
+       key.dsize = strlen(key.dptr);
+       data.dptr = (char *)&jobid;
+       data.dsize = 4;
+
+       DEBUG(10,("add_to_jobs_changed: Added jobid %u\n", (unsigned int)jobid ));
+
+       return (tdb_append(pdb->tdb, key, data) == 0);
+}
+
 /***************************************************************************
  Start spooling a job - return the jobid.
 ***************************************************************************/
@@ -1447,12 +1877,10 @@ uint32 print_job_start(struct current_user *user, int snum, char *jobname, NT_DE
        uint32 jobid;
        char *path;
        struct printjob pjob;
-       int next_jobid;
        user_struct *vuser;
-       int njobs = 0;
        const char *printername = lp_const_servicename(snum);
        struct tdb_print_db *pdb = get_print_db_byname(printername);
-       BOOL pdb_locked = False;
+       int njobs;
 
        errno = 0;
 
@@ -1495,60 +1923,18 @@ uint32 print_job_start(struct current_user *user, int snum, char *jobname, NT_DE
 
        /* Insure the maximum queue size is not violated */
        if ((njobs = print_queue_length(snum,NULL)) > lp_maxprintjobs(snum)) {
-               DEBUG(3, ("print_job_start: number of jobs (%d) larger than max printjobs per queue (%d).\n",
-                       njobs, lp_maxprintjobs(snum) ));
+               DEBUG(3, ("print_job_start: Queue %s number of jobs (%d) larger than max printjobs per queue (%d).\n",
+                       printername, njobs, lp_maxprintjobs(snum) ));
                release_print_db(pdb);
                errno = ENOSPC;
                return (uint32)-1;
        }
 
-       /* Lock the database - only wait 20 seconds. */
-       if (tdb_lock_bystring(pdb->tdb, "INFO/nextjob", 20) == -1) {
-               DEBUG(0,("print_job_start: failed to lock printing database %s\n", printername ));
-               release_print_db(pdb);
-               return (uint32)-1;
-       }
-
-       pdb_locked = True;
+       DEBUG(10,("print_job_start: Queue %s number of jobs (%d), max printjobs = %d\n",
+                       printername, njobs, lp_maxprintjobs(snum) ));
 
-       next_jobid = tdb_fetch_int32(pdb->tdb, "INFO/nextjob");
-       if (next_jobid == -1)
-               next_jobid = 1;
-
-       for (jobid = NEXT_JOBID(next_jobid); jobid != next_jobid; jobid = NEXT_JOBID(jobid)) {
-               if (!print_job_exists(snum, jobid))
-                       break;
-       }
-                               
-       if (jobid == next_jobid) {
-               DEBUG(3, ("print_job_start: jobid (%d)==next_jobid(%d).\n",
-                               jobid, next_jobid ));
-               jobid = -1;
+       if (!allocate_print_jobid(pdb, snum, printername, &jobid))
                goto fail;
-       }
-
-       /* Store a dummy placeholder. This must be quick as we have the lock. */
-       {
-               TDB_DATA dum;
-               dum.dptr = NULL;
-               dum.dsize = 0;
-               if (tdb_store(pdb->tdb, print_key(jobid), dum, TDB_INSERT) == -1) {
-                       DEBUG(3, ("print_job_start: jobid (%d) failed to store placeholder.\n",
-                               jobid ));
-                       jobid = -1;
-                       goto fail;
-               }
-       }
-
-       if (tdb_store_int32(pdb->tdb, "INFO/nextjob", jobid)==-1) {
-               DEBUG(3, ("print_job_start: failed to store INFO/nextjob.\n"));
-               jobid = -1;
-               goto fail;
-       }
-
-       /* We've finished with the INFO/nextjob lock. */
-       tdb_unlock_bystring(pdb->tdb, "INFO/nextjob");
-       pdb_locked = False;
 
        /* create the database entry */
        
@@ -1594,6 +1980,12 @@ to open spool file %s.\n", pjob.filename));
 
        pjob_store(snum, jobid, &pjob);
 
+       /* Update the 'jobs changed' entry used by print_queue_status. */
+       add_to_jobs_changed(pdb, jobid);
+
+       /* Ensure we keep a rough count of the number of total jobs... */
+       tdb_change_int32_atomic(pdb->tdb, "INFO/total_jobs", &njobs, 1);
+
        release_print_db(pdb);
 
        return jobid;
@@ -1602,12 +1994,10 @@ to open spool file %s.\n", pjob.filename));
        if (jobid != -1)
                pjob_delete(snum, jobid);
 
-       if (pdb_locked)
-               tdb_unlock_bystring(pdb->tdb, "INFO/nextjob");
        release_print_db(pdb);
 
        DEBUG(3, ("print_job_start: returning fail. Error = %s\n", strerror(errno) ));
-       return -1;
+       return (uint32)-1;
 }
 
 /****************************************************************************
@@ -1696,139 +2086,169 @@ fail:
        /* Still need to add proper error return propagation! 010122:JRR */
        unlink(pjob->filename);
        pjob_delete(snum, jobid);
+       remove_from_jobs_changed(snum, jobid);
        return False;
 }
 
 /****************************************************************************
Utility fn to enumerate the print queue.
Get a snapshot of jobs in the system without traversing.
 ****************************************************************************/
 
-static int traverse_fn_queue(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
+static BOOL get_stored_queue_info(struct tdb_print_db *pdb, int snum, int *pcount, print_queue_struct **ppqueue)
 {
-       struct traverse_struct *ts = (struct traverse_struct *)state;
-       struct printjob pjob;
-       int i;
-       uint32 jobid;
+       TDB_DATA data, key, cgdata;
+       print_queue_struct *queue = NULL;
+       uint32 qcount = 0;
+       uint32 extra_count = 0;
+       int total_count = 0;
+       size_t len = 0;
+       uint32 i;
+       int max_reported_jobs = lp_max_reported_jobs(snum);
+       BOOL ret = False;
 
-       /* sanity checks */
-       
-       if ( key.dsize != sizeof(jobid) )
-               return 0;
-               
-       memcpy(&jobid, key.dptr, sizeof(jobid));
-       
-       if ( unpack_pjob( data.dptr, data.dsize, &pjob ) == -1 )
-               return 0;
-       free_nt_devicemode( &pjob.nt_devmode );
+       /* make sure the database is up to date */
+       if (print_cache_expired(snum))
+               print_queue_update(snum);
+       *pcount = 0;
+       *ppqueue = NULL;
 
-       /* maybe it isn't for this queue */
-       if (ts->snum != lp_servicenumber(pjob.queuename))
-               return 0;
+       ZERO_STRUCT(data);
+       ZERO_STRUCT(cgdata);
+       key.dptr = "INFO/linear_queue_array";
+       key.dsize = strlen(key.dptr);
 
-       if (ts->qcount >= ts->maxcount)
-               return 0;
+       /* Get the stored queue data. */
+       data = tdb_fetch(pdb->tdb, key);
 
-       i = ts->qcount;
+       if (data.dptr == NULL || data.dsize < 4)
+               qcount = 0;
+       else
+               memcpy(&qcount, data.dptr, 4);
 
-       ts->queue[i].job = jobid;
-       ts->queue[i].size = pjob.size;
-       ts->queue[i].page_count = pjob.page_count;
-       ts->queue[i].status = pjob.status;
-       ts->queue[i].priority = 1;
-       ts->queue[i].time = pjob.starttime;
-       fstrcpy(ts->queue[i].fs_user, pjob.user);
-       fstrcpy(ts->queue[i].fs_file, pjob.jobname);
+       /* Get the changed jobs list. */
+       key.dptr = "INFO/jobs_changed";
+       key.dsize = strlen(key.dptr);
 
-       ts->qcount++;
+       cgdata = tdb_fetch(pdb->tdb, key);
+       if (cgdata.dptr != NULL && (cgdata.dsize % 4 == 0))
+               extra_count = cgdata.dsize/4;
 
-       return 0;
-}
+       DEBUG(5,("get_stored_queue_info: qcount = %u, extra_count = %u\n", (unsigned int)qcount, (unsigned int)extra_count));
 
-struct traverse_count_struct {
-       int snum, count;
-};
+       /* Allocate the queue size. */
+       if (qcount == 0 && extra_count == 0)
+               goto out;
+
+       if ((queue = (print_queue_struct *)malloc(sizeof(print_queue_struct)*(qcount + extra_count))) == NULL)
+               goto out;
+
+       /* Retrieve the linearised queue data. */
+       len = 0;
+       for( i  = 0; i < qcount; i++) {
+               uint32 qjob, qsize, qpage_count, qstatus, qpriority, qtime;
+               len += tdb_unpack(data.dptr + 4 + len, data.dsize - len, "ddddddff",
+                               &qjob,
+                               &qsize,
+                               &qpage_count,
+                               &qstatus,
+                               &qpriority,
+                               &qtime,
+                               queue[i].fs_user,
+                               queue[i].fs_file);
+               queue[i].job = qjob;
+               queue[i].size = qsize;
+               queue[i].page_count = qpage_count;
+               queue[i].status = qstatus;
+               queue[i].priority = qpriority;
+               queue[i].time = qtime;
+       }
 
-/****************************************************************************
- Utility fn to count the number of entries in the print queue.
-****************************************************************************/
+       total_count = qcount;
 
-static int traverse_count_fn_queue(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
-{
-       struct traverse_count_struct *ts = (struct traverse_count_struct *)state;
-       struct printjob pjob;
-       uint32 jobid;
+       /* Add in the changed jobids. */
+       for( i  = 0; i < extra_count; i++) {
+               uint32 jobid;
+               struct printjob *pjob;
 
-       /* sanity checks */
-       
-       if (  key.dsize != sizeof(jobid) )
-               return 0;
-               
-       memcpy(&jobid, key.dptr, sizeof(jobid));
-       
-       if ( unpack_pjob( data.dptr, data.dsize, &pjob ) == -1 )
-               return 0;
-               
-       free_nt_devicemode( &pjob.nt_devmode );
+               memcpy(&jobid, &cgdata.dptr[i*4], 4);
+               DEBUG(5,("get_stored_queue_info: changed job = %u\n", (unsigned int)jobid));
+               pjob = print_job_find(snum, jobid);
+               if (!pjob) {
+                       DEBUG(5,("get_stored_queue_info: failed to find changed job = %u\n", (unsigned int)jobid));
+                       remove_from_jobs_changed(snum, jobid);
+                       continue;
+               }
 
-       /* maybe it isn't for this queue - this cannot happen with the tdb/printer code. JRA */
-       if (ts->snum != lp_servicenumber(pjob.queuename))
-               return 0;
+               queue[total_count].job = jobid;
+               queue[total_count].size = pjob->size;
+               queue[total_count].page_count = pjob->page_count;
+               queue[total_count].status = pjob->status;
+               queue[total_count].priority = 1;
+               queue[total_count].time = pjob->starttime;
+               fstrcpy(queue[total_count].fs_user, pjob->user);
+               fstrcpy(queue[total_count].fs_file, pjob->jobname);
+               total_count++;
+       }
 
-       ts->count++;
+       /* Sort the queue by submission time otherwise they are displayed
+          in hash order. */
 
-       return 0;
-}
+       qsort(queue, total_count, sizeof(print_queue_struct), QSORT_CAST(printjob_comp));
 
-/****************************************************************************
- Sort print jobs by submittal time.
-****************************************************************************/
+       DEBUG(5,("get_stored_queue_info: total_count = %u\n", (unsigned int)total_count));
 
-static int printjob_comp(print_queue_struct *j1, print_queue_struct *j2)
-{
-       /* Silly cases */
+       if (max_reported_jobs && total_count > max_reported_jobs)
+               total_count = max_reported_jobs;
 
-       if (!j1 && !j2)
-               return 0;
-       if (!j1)
-               return -1;
-       if (!j2)
-               return 1;
+       *ppqueue = queue;
+       *pcount = total_count;
 
-       /* Sort on job start time */
+       ret = True;
 
-       if (j1->time == j2->time)
-               return 0;
-       return (j1->time > j2->time) ? 1 : -1;
+  out:
+
+       SAFE_FREE(data.dptr);
+       SAFE_FREE(cgdata.dptr);
+       return ret;
 }
 
 /****************************************************************************
  Get a printer queue listing.
+ set queue = NULL and status = NULL if you just want to update the cache
 ****************************************************************************/
 
 int print_queue_status(int snum, 
-                      print_queue_struct **queue,
+                      print_queue_struct **ppqueue,
                       print_status_struct *status)
 {
-       struct traverse_struct tstruct;
-       struct traverse_count_struct tsc;
        fstring keystr;
        TDB_DATA data, key;
-       const char *printername = lp_const_servicename(snum);
-       struct tdb_print_db *pdb = get_print_db_byname(printername);
-
-       *queue = NULL;
-       
-       if (!pdb)
-               return 0;
+       const char *printername;
+       struct tdb_print_db *pdb;
+       int count = 0;
 
        /* make sure the database is up to date */
+
        if (print_cache_expired(snum))
                print_queue_update(snum);
 
+       /* return if we are done */
+       if ( !ppqueue || !status )
+               return 0;
+
+       *ppqueue = NULL;
+       printername = lp_const_servicename(snum);
+       pdb = get_print_db_byname(printername);
+
+       if (!pdb)
+               return 0;
+
        /*
         * Fetch the queue status.  We must do this first, as there may
         * be no jobs in the queue.
         */
+
        ZERO_STRUCTP(status);
        slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", printername);
        key.dptr = keystr;
@@ -1845,55 +2265,14 @@ int print_queue_status(int snum,
         * Now, fetch the print queue information.  We first count the number
         * of entries, and then only retrieve the queue if necessary.
         */
-       tsc.count = 0;
-       tsc.snum = snum;
-       
-       tdb_traverse(pdb->tdb, traverse_count_fn_queue, (void *)&tsc);
 
-       if (tsc.count == 0) {
+       if (!get_stored_queue_info(pdb, snum, &count, ppqueue)) {
                release_print_db(pdb);
                return 0;
        }
 
-       /* Allocate the queue size. */
-       if ((tstruct.queue = (print_queue_struct *)
-            malloc(sizeof(print_queue_struct)*tsc.count)) == NULL) {
-               release_print_db(pdb);
-               return 0;
-       }
-
-       /*
-        * Fill in the queue.
-        * We need maxcount as the queue size may have changed between
-        * the two calls to tdb_traverse.
-        */
-       tstruct.qcount = 0;
-       tstruct.maxcount = tsc.count;
-       tstruct.snum = snum;
-
-       tdb_traverse(pdb->tdb, traverse_fn_queue, (void *)&tstruct);
        release_print_db(pdb);
-
-       /* Sort the queue by submission time otherwise they are displayed
-          in hash order. */
-
-       qsort(tstruct.queue, tstruct.qcount, sizeof(print_queue_struct),
-             QSORT_CAST(printjob_comp));
-
-       *queue = tstruct.queue;
-       return tstruct.qcount;
-}
-
-/****************************************************************************
- Turn a queue name into a snum.
-****************************************************************************/
-
-int print_queue_snum(const char *qname)
-{
-       int snum = lp_servicenumber(qname);
-       if (snum == -1 || !lp_print_ok(snum))
-               return -1;
-       return snum;
+       return count;
 }
 
 /****************************************************************************