2 Unix SMB/Netbios implementation.
4 printing backend routines
5 Copyright (C) Andrew Tridgell 1992-2000
6 Copyright (C) Jeremy Allison 2002
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 /* Current printer interface */
26 static struct printif *current_printif = &generic_printif;
29 the printing backend revolves around a tdb database that stores the
30 SMB view of the print queue
32 The key for this database is a jobid - a internally generated number that
33 uniquely identifies a print job
35 reading the print queue involves two steps:
36 - possibly running lpq and updating the internal database from that
37 - reading entries from the database
39 jobids are assigned when a job starts spooling.
42 /***************************************************************************
43 Nightmare. LANMAN jobid's are 16 bit numbers..... We must map them to 32
44 bit RPC jobids.... JRA.
45 ***************************************************************************/
47 static TDB_CONTEXT *rap_tdb;
48 static uint16 next_rap_jobid;
50 uint16 pjobid_to_rap(int snum, uint32 jobid)
57 /* Create the in-memory tdb. */
58 rap_tdb = tdb_open_log(NULL, 0, TDB_INTERNAL, (O_RDWR|O_CREAT), 0644);
63 SIVAL(&jinfo,0,(int32)snum);
64 SIVAL(&jinfo,4,jobid);
66 key.dptr = (char *)&jinfo;
67 key.dsize = sizeof(jinfo);
68 data = tdb_fetch(rap_tdb, key);
69 if (data.dptr && data.dsize == sizeof(uint16)) {
70 memcpy(&rap_jobid, data.dptr, sizeof(uint16));
75 /* Not found - create and store mapping. */
76 rap_jobid = ++next_rap_jobid;
78 rap_jobid = ++next_rap_jobid;
79 data.dptr = (char *)&rap_jobid;
80 data.dsize = sizeof(rap_jobid);
81 tdb_store(rap_tdb, key, data, TDB_REPLACE);
82 tdb_store(rap_tdb, data, key, TDB_REPLACE);
86 BOOL rap_to_pjobid(uint16 rap_jobid, int *psnum, uint32 *pjobid)
94 key.dptr = (char *)&rap_jobid;
95 key.dsize = sizeof(rap_jobid);
96 data = tdb_fetch(rap_tdb, key);
97 if (data.dptr && data.dsize == sizeof(jinfo)) {
98 *psnum = IVAL(&jinfo,0);
99 *pjobid = IVAL(&jinfo,4);
100 SAFE_FREE(data.dptr);
103 SAFE_FREE(data.dptr);
107 static void rap_jobid_delete(int snum, uint32 jobid)
116 SIVAL(&jinfo,0,(int32)snum);
117 SIVAL(&jinfo,4,jobid);
119 key.dptr = (char *)&jinfo;
120 key.dsize = sizeof(jinfo);
121 data = tdb_fetch(rap_tdb, key);
122 if (!data.dptr || (data.dsize != sizeof(uint16))) {
123 SAFE_FREE(data.dptr);
127 memcpy(&rap_jobid, data.dptr, sizeof(uint16));
128 SAFE_FREE(data.dptr);
129 data.dptr = (char *)&rap_jobid;
130 data.dsize = sizeof(rap_jobid);
131 tdb_delete(rap_tdb, key);
132 tdb_delete(rap_tdb, data);
135 static pid_t local_pid;
137 static int get_queue_status(int, print_status_struct *);
139 /* There can be this many printing tdb's open, plus any locked ones. */
140 #define MAX_PRINT_DBS_OPEN 1
142 struct tdb_print_db {
143 struct tdb_print_db *next, *prev;
146 fstring printer_name;
149 static struct tdb_print_db *print_db_head;
151 /****************************************************************************
152 Function to find or create the printer specific job tdb given a printername.
153 Limits the number of tdb's open to MAX_PRINT_DBS_OPEN.
154 ****************************************************************************/
156 static struct tdb_print_db *get_print_db_byname(const char *printername)
158 struct tdb_print_db *p = NULL, *last_entry = NULL;
160 pstring printdb_path;
161 BOOL done_become_root = False;
163 for (p = print_db_head, last_entry = print_db_head; p; p = p->next) {
164 /* Ensure the list terminates... JRA. */
165 SMB_ASSERT(p->next != print_db_head);
167 if (p->tdb && strequal(p->printer_name, printername)) {
168 DLIST_PROMOTE(print_db_head, p);
177 if (num_open >= MAX_PRINT_DBS_OPEN) {
178 /* Try and recycle the last entry. */
179 DLIST_PROMOTE(print_db_head, last_entry);
181 for (p = print_db_head; p; p = p->next) {
185 if (tdb_close(print_db_head->tdb)) {
186 DEBUG(0,("get_print_db: Failed to close tdb for printer %s\n",
187 print_db_head->printer_name ));
193 memset(p->printer_name, '\0', sizeof(p->printer_name));
197 DLIST_PROMOTE(print_db_head, p);
204 p = (struct tdb_print_db *)malloc(sizeof(struct tdb_print_db));
206 DEBUG(0,("get_print_db: malloc fail !\n"));
210 DLIST_ADD(print_db_head, p);
213 pstrcpy(printdb_path, lock_path("printing/"));
214 pstrcat(printdb_path, printername);
215 pstrcat(printdb_path, ".tdb");
217 if (geteuid() != 0) {
219 done_become_root = True;
222 p->tdb = tdb_open_log(printdb_path, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
224 if (done_become_root)
228 DEBUG(0,("get_print_db: Failed to open printer backend database %s.\n",
230 DLIST_REMOVE(print_db_head, p);
234 fstrcpy(p->printer_name, printername);
239 /***************************************************************************
240 Remove a reference count.
241 ****************************************************************************/
243 static void release_print_db( struct tdb_print_db *pdb)
246 SMB_ASSERT(pdb->ref_count >= 0);
249 /***************************************************************************
250 Close all open print db entries.
251 ****************************************************************************/
253 static void close_all_print_db(void)
255 struct tdb_print_db *p = NULL, *next_p = NULL;
257 for (p = print_db_head; p; p = next_p) {
262 DLIST_REMOVE(print_db_head, p);
268 /****************************************************************************
269 Initialise the printing backend. Called once at startup before the fork().
270 ****************************************************************************/
272 BOOL print_backend_init(void)
274 char *sversion = "INFO/version";
275 pstring printing_path;
276 int services = lp_numservices();
279 if (local_pid == sys_getpid())
282 unlink(lock_path("printing.tdb"));
283 pstrcpy(printing_path,lock_path("printing"));
284 mkdir(printing_path,0755);
286 local_pid = sys_getpid();
288 /* handle a Samba upgrade */
290 for (snum = 0; snum < services; snum++) {
291 struct tdb_print_db *pdb;
292 if (!lp_print_ok(snum))
295 pdb = get_print_db_byname(lp_const_servicename(snum));
298 if (tdb_lock_bystring(pdb->tdb, sversion, 0) == -1) {
299 DEBUG(0,("print_backend_init: Failed to open printer %s database\n", lp_const_servicename(snum) ));
300 release_print_db(pdb);
303 if (tdb_fetch_int32(pdb->tdb, sversion) != PRINT_DATABASE_VERSION) {
304 tdb_traverse(pdb->tdb, tdb_traverse_delete_fn, NULL);
305 tdb_store_int32(pdb->tdb, sversion, PRINT_DATABASE_VERSION);
307 tdb_unlock_bystring(pdb->tdb, sversion);
308 release_print_db(pdb);
311 close_all_print_db(); /* Don't leave any open. */
313 /* select the appropriate printing interface... */
315 if (strcmp(lp_printcapname(), "cups") == 0)
316 current_printif = &cups_printif;
317 #endif /* HAVE_CUPS */
319 /* do NT print initialization... */
320 return nt_printing_init();
323 /****************************************************************************
324 Shut down printing backend. Called once at shutdown to close the tdb.
325 ****************************************************************************/
327 void printing_end(void)
329 close_all_print_db(); /* Don't leave any open. */
332 /****************************************************************************
333 Useful function to generate a tdb key.
334 ****************************************************************************/
336 static TDB_DATA print_key(uint32 jobid)
342 ret.dptr = (void *)&j;
343 ret.dsize = sizeof(j);
347 /***********************************************************************
348 unpack a pjob from a tdb buffer
349 ***********************************************************************/
351 int unpack_pjob( char* buf, int buflen, struct printjob *pjob )
359 len += tdb_unpack(buf+len, buflen-len, "dddddddddffff",
377 if ( (used = unpack_devicemode(&pjob->nt_devmode, buf+len, buflen-len)) == -1 )
386 /****************************************************************************
387 Useful function to find a print job in the database.
388 ****************************************************************************/
390 static struct printjob *print_job_find(int snum, uint32 jobid)
392 static struct printjob pjob;
394 struct tdb_print_db *pdb = get_print_db_byname(lp_const_servicename(snum));
400 ret = tdb_fetch(pdb->tdb, print_key(jobid));
401 release_print_db(pdb);
406 if ( pjob.nt_devmode )
407 free_nt_devicemode( &pjob.nt_devmode );
411 if ( unpack_pjob( ret.dptr, ret.dsize, &pjob ) == -1 ) {
420 /* Convert a unix jobid to a smb jobid */
422 static uint32 sysjob_to_jobid_value;
424 static int unixjob_traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA key,
425 TDB_DATA data, void *state)
427 struct printjob *pjob;
428 int *sysjob = (int *)state;
430 if (!data.dptr || data.dsize == 0)
433 pjob = (struct printjob *)data.dptr;
434 if (key.dsize != sizeof(uint32))
437 if (*sysjob == pjob->sysjob) {
438 uint32 *jobid = (uint32 *)key.dptr;
440 sysjob_to_jobid_value = *jobid;
447 /****************************************************************************
448 This is a *horribly expensive call as we have to iterate through all the
449 current printer tdb's. Don't do this often ! JRA.
450 ****************************************************************************/
452 uint32 sysjob_to_jobid(int unix_jobid)
454 int services = lp_numservices();
457 sysjob_to_jobid_value = (uint32)-1;
459 for (snum = 0; snum < services; snum++) {
460 struct tdb_print_db *pdb;
461 if (!lp_print_ok(snum))
463 pdb = get_print_db_byname(lp_const_servicename(snum));
465 tdb_traverse(pdb->tdb, unixjob_traverse_fn, &unix_jobid);
466 release_print_db(pdb);
467 if (sysjob_to_jobid_value != (uint32)-1)
468 return sysjob_to_jobid_value;
473 /****************************************************************************
474 Send notifications based on what has changed after a pjob_store.
475 ****************************************************************************/
479 uint32 spoolss_status;
480 } lpq_to_spoolss_status_map[] = {
481 { LPQ_QUEUED, JOB_STATUS_QUEUED },
482 { LPQ_PAUSED, JOB_STATUS_PAUSED },
483 { LPQ_SPOOLING, JOB_STATUS_SPOOLING },
484 { LPQ_PRINTING, JOB_STATUS_PRINTING },
485 { LPQ_DELETING, JOB_STATUS_DELETING },
486 { LPQ_OFFLINE, JOB_STATUS_OFFLINE },
487 { LPQ_PAPEROUT, JOB_STATUS_PAPEROUT },
488 { LPQ_PRINTED, JOB_STATUS_PRINTED },
489 { LPQ_DELETED, JOB_STATUS_DELETED },
490 { LPQ_BLOCKED, JOB_STATUS_BLOCKED },
491 { LPQ_USER_INTERVENTION, JOB_STATUS_USER_INTERVENTION },
495 /* Convert a lpq status value stored in printing.tdb into the
496 appropriate win32 API constant. */
498 static uint32 map_to_spoolss_status(uint32 lpq_status)
502 while (lpq_to_spoolss_status_map[i].lpq_status != -1) {
503 if (lpq_to_spoolss_status_map[i].lpq_status == lpq_status)
504 return lpq_to_spoolss_status_map[i].spoolss_status;
511 static void pjob_store_notify(int snum, uint32 jobid, struct printjob *old_data,
512 struct printjob *new_data)
514 BOOL new_job = False;
519 /* Notify the job name first */
521 if (new_job || !strequal(old_data->jobname, new_data->jobname))
522 notify_job_name(snum, jobid, new_data->jobname);
524 /* Job attributes that can't be changed. We only send
525 notification for these on a new job. */
528 notify_job_submitted(snum, jobid, new_data->starttime);
529 notify_job_username(snum, jobid, new_data->user);
532 /* Job attributes of a new job or attributes that can be
535 if (new_job || old_data->status != new_data->status)
536 notify_job_status(snum, jobid, map_to_spoolss_status(new_data->status));
538 if (new_job || old_data->size != new_data->size)
539 notify_job_total_bytes(snum, jobid, new_data->size);
541 if (new_job || old_data->page_count != new_data->page_count)
542 notify_job_total_pages(snum, jobid, new_data->page_count);
545 /****************************************************************************
546 Store a job structure back to the database.
547 ****************************************************************************/
549 static BOOL pjob_store(int snum, uint32 jobid, struct printjob *pjob)
551 TDB_DATA old_data, new_data;
553 struct tdb_print_db *pdb = get_print_db_byname(lp_const_servicename(snum));
555 int len, newlen, buflen;
563 old_data = tdb_fetch(pdb->tdb, print_key(jobid));
565 /* Doh! Now we have to pack/unpack data since the NT_DEVICEMODE was added */
572 len += tdb_pack(buf+len, buflen-len, "dddddddddffff",
587 len += pack_devicemode(pjob->nt_devmode, buf+len, buflen-len);
592 tb = (char *)Realloc(buf, len);
594 DEBUG(0,("pjob_store: failed to enlarge buffer!\n"));
601 } while ( buflen != len );
607 new_data.dsize = len;
608 ret = (tdb_store(pdb->tdb, print_key(jobid), new_data, TDB_REPLACE) == 0);
610 release_print_db(pdb);
612 /* Send notify updates for what has changed */
614 if ( ret && (old_data.dsize == 0 || old_data.dsize == sizeof(*pjob)) )
615 pjob_store_notify( snum, jobid, (struct printjob *)old_data.dptr, pjob );
618 SAFE_FREE( old_data.dptr );
624 /****************************************************************************
625 Remove a job structure from the database.
626 ****************************************************************************/
628 static void pjob_delete(int snum, uint32 jobid)
630 struct printjob *pjob = print_job_find(snum, jobid);
631 uint32 job_status = 0;
632 struct tdb_print_db *pdb = get_print_db_byname(lp_const_servicename(snum));
638 DEBUG(5, ("pjob_delete(): we were asked to delete nonexistent job %u\n",
639 (unsigned int)jobid));
640 release_print_db(pdb);
644 /* Send a notification that a job has been deleted */
646 job_status = map_to_spoolss_status(pjob->status);
648 /* We must cycle through JOB_STATUS_DELETING and
649 JOB_STATUS_DELETED for the port monitor to delete the job
652 job_status |= JOB_STATUS_DELETING;
653 notify_job_status(snum, jobid, job_status);
655 job_status |= JOB_STATUS_DELETED;
656 notify_job_status(snum, jobid, job_status);
658 /* Remove from printing.tdb */
660 tdb_delete(pdb->tdb, print_key(jobid));
661 release_print_db(pdb);
662 rap_jobid_delete(snum, jobid);
665 /****************************************************************************
666 Parse a file name from the system spooler to generate a jobid.
667 ****************************************************************************/
669 static uint32 print_parse_jobid(char *fname)
673 if (strncmp(fname,PRINT_SPOOL_PREFIX,strlen(PRINT_SPOOL_PREFIX)) != 0)
675 fname += strlen(PRINT_SPOOL_PREFIX);
681 return (uint32)jobid;
684 /****************************************************************************
685 List a unix job in the print database.
686 ****************************************************************************/
688 static void print_unix_job(int snum, print_queue_struct *q)
690 uint32 jobid = q->job + UNIX_JOB_START;
691 struct printjob pj, *old_pj;
693 /* Preserve the timestamp on an existing unix print job */
695 old_pj = print_job_find(snum, jobid);
702 pj.starttime = old_pj ? old_pj->starttime : q->time;
703 pj.status = q->status;
707 fstrcpy(pj.filename, "");
708 fstrcpy(pj.jobname, q->fs_file);
709 fstrcpy(pj.user, q->fs_user);
710 fstrcpy(pj.queuename, lp_const_servicename(snum));
712 pjob_store(snum, jobid, &pj);
716 struct traverse_struct {
717 print_queue_struct *queue;
718 int qcount, snum, maxcount, total_jobs;
721 /****************************************************************************
722 Utility fn to delete any jobs that are no longer active.
723 ****************************************************************************/
725 static int traverse_fn_delete(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
727 struct traverse_struct *ts = (struct traverse_struct *)state;
728 struct printjob pjob;
732 if ( key.dsize != sizeof(jobid) )
735 memcpy(&jobid, key.dptr, sizeof(jobid));
736 if ( unpack_pjob( data.dptr, data.dsize, &pjob ) == -1 )
738 free_nt_devicemode( &pjob.nt_devmode );
741 if (ts->snum != lp_servicenumber(pjob.queuename)) {
742 /* this isn't for the queue we are looking at - this cannot happen with the split tdb's. JRA */
747 /* remove a unix job if it isn't in the system queue any more */
749 for (i=0;i<ts->qcount;i++) {
750 uint32 u_jobid = (ts->queue[i].job + UNIX_JOB_START);
751 if (jobid == u_jobid)
755 pjob_delete(ts->snum, jobid);
761 /* maybe it hasn't been spooled yet */
763 /* if a job is not spooled and the process doesn't
764 exist then kill it. This cleans up after smbd
766 if (!process_exists(pjob.pid))
767 pjob_delete(ts->snum, jobid);
773 for (i=0;i<ts->qcount;i++) {
774 uint32 curr_jobid = print_parse_jobid(ts->queue[i].fs_file);
775 if (jobid == curr_jobid)
779 /* The job isn't in the system queue - we have to assume it has
780 completed, so delete the database entry. */
782 if (i == ts->qcount) {
783 time_t cur_t = time(NULL);
785 /* A race can occur between the time a job is spooled and
786 when it appears in the lpq output. This happens when
787 the job is added to printing.tdb when another smbd
788 running print_queue_update() has completed a lpq and
789 is currently traversing the printing tdb and deleting jobs.
790 A workaround is to not delete the job if it has been
791 submitted less than lp_lpqcachetime() seconds ago. */
793 if ((cur_t - pjob.starttime) > lp_lpqcachetime())
794 pjob_delete(ts->snum, jobid);
804 /****************************************************************************
805 Check if the print queue has been updated recently enough.
806 ****************************************************************************/
808 static void print_cache_flush(int snum)
811 const char *printername = lp_const_servicename(snum);
812 struct tdb_print_db *pdb = get_print_db_byname(printername);
816 slprintf(key, sizeof(key)-1, "CACHE/%s", printername);
817 tdb_store_int32(pdb->tdb, key, -1);
818 release_print_db(pdb);
821 /****************************************************************************
822 Check if someone already thinks they are doing the update.
823 ****************************************************************************/
825 static pid_t get_updating_pid(fstring printer_name)
830 struct tdb_print_db *pdb = get_print_db_byname(printer_name);
834 slprintf(keystr, sizeof(keystr)-1, "UPDATING/%s", printer_name);
836 key.dsize = strlen(keystr);
838 data = tdb_fetch(pdb->tdb, key);
839 release_print_db(pdb);
840 if (!data.dptr || data.dsize != sizeof(pid_t)) {
841 SAFE_FREE(data.dptr);
845 memcpy(&updating_pid, data.dptr, sizeof(pid_t));
846 SAFE_FREE(data.dptr);
848 if (process_exists(updating_pid))
854 /****************************************************************************
855 Set the fact that we're doing the update, or have finished doing the update
857 ****************************************************************************/
859 static void set_updating_pid(const fstring printer_name, BOOL delete)
864 pid_t updating_pid = sys_getpid();
865 struct tdb_print_db *pdb = get_print_db_byname(printer_name);
870 slprintf(keystr, sizeof(keystr)-1, "UPDATING/%s", printer_name);
872 key.dsize = strlen(keystr);
875 tdb_delete(pdb->tdb, key);
876 release_print_db(pdb);
880 data.dptr = (void *)&updating_pid;
881 data.dsize = sizeof(pid_t);
883 tdb_store(pdb->tdb, key, data, TDB_REPLACE);
884 release_print_db(pdb);
887 /****************************************************************************
888 Update the internal database from the system print queue for a queue.
889 ****************************************************************************/
891 static void print_queue_update(int snum)
894 print_queue_struct *queue = NULL;
895 print_status_struct status;
896 print_status_struct old_status;
897 struct printjob *pjob;
898 struct traverse_struct tstruct;
899 fstring keystr, printer_name, cachestr;
901 struct tdb_print_db *pdb;
903 fstrcpy(printer_name, lp_const_servicename(snum));
904 pdb = get_print_db_byname(printer_name);
909 * Check to see if someone else is doing this update.
910 * This is essentially a mutex on the update.
913 if (get_updating_pid(printer_name) != -1) {
914 release_print_db(pdb);
918 /* Lock the queue for the database update */
920 slprintf(keystr, sizeof(keystr) - 1, "LOCK/%s", printer_name);
921 /* Only wait 10 seconds for this. */
922 if (tdb_lock_bystring(pdb->tdb, keystr, 10) == -1) {
923 DEBUG(0,("print_queue_update: Failed to lock printer %s database\n", printer_name));
924 release_print_db(pdb);
929 * Ensure that no one else got in here.
930 * If the updating pid is still -1 then we are
934 if (get_updating_pid(printer_name) != -1) {
936 * Someone else is doing the update, exit.
938 tdb_unlock_bystring(pdb->tdb, keystr);
939 release_print_db(pdb);
944 * We're going to do the update ourselves.
947 /* Tell others we're doing the update. */
948 set_updating_pid(printer_name, False);
951 * Allow others to enter and notice we're doing
955 tdb_unlock_bystring(pdb->tdb, keystr);
958 * Update the cache time FIRST ! Stops others even
959 * attempting to get the lock and doing this
960 * if the lpq takes a long time.
963 slprintf(cachestr, sizeof(cachestr)-1, "CACHE/%s", printer_name);
964 tdb_store_int32(pdb->tdb, cachestr, (int)time(NULL));
966 /* get the current queue using the appropriate interface */
969 qcount = (*(current_printif->queue_get))(snum, &queue, &status);
971 DEBUG(3, ("%d job%s in queue for %s\n", qcount, (qcount != 1) ?
972 "s" : "", printer_name));
975 any job in the internal database that is marked as spooled
976 and doesn't exist in the system queue is considered finished
977 and removed from the database
979 any job in the system database but not in the internal database
980 is added as a unix job
982 fill in any system job numbers as we go
984 for (i=0; i<qcount; i++) {
985 uint32 jobid = print_parse_jobid(queue[i].fs_file);
987 if (jobid == (uint32)-1) {
988 /* assume its a unix print job */
989 print_unix_job(snum, &queue[i]);
993 /* we have an active SMB print job - update its status */
994 pjob = print_job_find(snum, jobid);
996 /* err, somethings wrong. Probably smbd was restarted
997 with jobs in the queue. All we can do is treat them
998 like unix jobs. Pity. */
999 print_unix_job(snum, &queue[i]);
1003 pjob->sysjob = queue[i].job;
1004 pjob->status = queue[i].status;
1006 pjob_store(snum, jobid, pjob);
1009 /* now delete any queued entries that don't appear in the
1011 tstruct.queue = queue;
1012 tstruct.qcount = qcount;
1013 tstruct.snum = snum;
1014 tstruct.total_jobs = 0;
1016 tdb_traverse(pdb->tdb, traverse_fn_delete, (void *)&tstruct);
1018 SAFE_FREE(tstruct.queue);
1020 tdb_store_int32(pdb->tdb, "INFO/total_jobs", tstruct.total_jobs);
1022 if( qcount != get_queue_status(snum, &old_status))
1023 DEBUG(10,("print_queue_update: queue status change %d jobs -> %d jobs for printer %s\n",
1024 old_status.qcount, qcount, printer_name ));
1026 /* store the new queue status structure */
1027 slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", printer_name);
1029 key.dsize = strlen(keystr);
1031 status.qcount = qcount;
1032 data.dptr = (void *)&status;
1033 data.dsize = sizeof(status);
1034 tdb_store(pdb->tdb, key, data, TDB_REPLACE);
1037 * Update the cache time again. We want to do this call
1038 * as little as possible...
1041 slprintf(keystr, sizeof(keystr)-1, "CACHE/%s", printer_name);
1042 tdb_store_int32(pdb->tdb, keystr, (int32)time(NULL));
1044 /* Delete our pid from the db. */
1045 set_updating_pid(printer_name, True);
1046 release_print_db(pdb);
1049 /****************************************************************************
1050 Fetch and clean the pid_t record list for all pids interested in notify
1051 messages. data needs freeing on exit.
1052 ****************************************************************************/
1054 #define NOTIFY_PID_LIST_KEY "NOTIFY_PID_LIST"
1055 #define PRINT_SERVER_ENTRY_NAME "___PRINT_SERVER_ENTRY___"
1057 static TDB_DATA get_printer_notify_pid_list(TDB_CONTEXT *tdb, const char *printer_name, BOOL cleanlist)
1064 data = tdb_fetch_by_string( tdb, NOTIFY_PID_LIST_KEY );
1071 if (data.dsize % 8) {
1072 DEBUG(0,("get_printer_notify_pid_list: Size of record for printer %s not a multiple of 8 !\n", printer_name ));
1073 tdb_delete_by_string(tdb, NOTIFY_PID_LIST_KEY );
1074 SAFE_FREE(data.dptr);
1083 * Weed out all dead entries.
1086 for( i = 0; i < data.dsize; i += 8) {
1087 pid_t pid = (pid_t)IVAL(data.dptr, i);
1089 if (pid == sys_getpid())
1092 /* Entry is dead if process doesn't exist or refcount is zero. */
1094 while ((i < data.dsize) && ((IVAL(data.dptr, i + 4) == 0) || !process_exists(pid))) {
1096 /* Refcount == zero is a logic error and should never happen. */
1097 if (IVAL(data.dptr, i + 4) == 0) {
1098 DEBUG(0,("get_printer_notify_pid_list: Refcount == 0 for pid = %u printer %s !\n",
1099 (unsigned int)pid, printer_name ));
1102 if (data.dsize - i > 8)
1103 memmove( &data.dptr[i], &data.dptr[i+8], data.dsize - i - 8);
1111 /****************************************************************************
1112 Return a malloced list of pid_t's that are interested in getting update
1113 messages on this print queue. Used in printing/notify to send the messages.
1114 ****************************************************************************/
1116 BOOL print_notify_pid_list(const char *printername, TALLOC_CTX *mem_ctx, size_t *p_num_pids, pid_t **pp_pid_list)
1118 struct tdb_print_db *pdb = NULL;
1119 TDB_CONTEXT *tdb = NULL;
1122 size_t i, num_pids, offset;
1126 *pp_pid_list = NULL;
1128 if (strequal(printername, PRINT_SERVER_ENTRY_NAME)) {
1130 tdb = conn_tdb_ctx();
1132 pdb = get_print_db_byname(printername);
1138 if (tdb_read_lock_bystring(tdb, NOTIFY_PID_LIST_KEY, 10) == -1) {
1139 DEBUG(0,("print_notify_pid_list: Failed to lock printer %s database\n",
1142 release_print_db(pdb);
1146 data = get_printer_notify_pid_list( tdb, printername, True );
1153 num_pids = data.dsize / 8;
1155 if ((pid_list = (pid_t *)talloc(mem_ctx, sizeof(pid_t) * num_pids)) == NULL) {
1160 for( i = 0, offset = 0; offset < data.dsize; offset += 8, i++)
1161 pid_list[i] = (pid_t)IVAL(data.dptr, offset);
1163 *pp_pid_list = pid_list;
1164 *p_num_pids = num_pids;
1170 tdb_read_unlock_bystring(tdb, NOTIFY_PID_LIST_KEY);
1172 release_print_db(pdb);
1173 SAFE_FREE(data.dptr);
1177 /****************************************************************************
1178 Create/Update an entry in the print tdb that will allow us to send notify
1179 updates only to interested smbd's.
1180 ****************************************************************************/
1182 BOOL print_notify_register_pid(int snum)
1185 struct tdb_print_db *pdb = NULL;
1186 TDB_CONTEXT *tdb = NULL;
1187 const char *printername;
1188 uint32 mypid = (uint32)sys_getpid();
1193 printername = lp_const_servicename(snum);
1194 pdb = get_print_db_byname(printername);
1199 printername = PRINT_SERVER_ENTRY_NAME;
1201 tdb = conn_tdb_ctx();
1204 if (tdb_lock_bystring(tdb, NOTIFY_PID_LIST_KEY, 10) == -1) {
1205 DEBUG(0,("print_notify_register_pid: Failed to lock printer %s\n",
1208 release_print_db(pdb);
1212 data = get_printer_notify_pid_list( tdb, printername, True );
1214 /* Add ourselves and increase the refcount. */
1216 for (i = 0; i < data.dsize; i += 8) {
1217 if (IVAL(data.dptr,i) == mypid) {
1218 uint32 new_refcount = IVAL(data.dptr, i+4) + 1;
1219 SIVAL(data.dptr, i+4, new_refcount);
1224 if (i == data.dsize) {
1225 /* We weren't in the list. Realloc. */
1226 data.dptr = Realloc(data.dptr, data.dsize + 8);
1228 DEBUG(0,("print_notify_register_pid: Relloc fail for printer %s\n",
1233 SIVAL(data.dptr,data.dsize - 8,mypid);
1234 SIVAL(data.dptr,data.dsize - 4,1); /* Refcount. */
1237 /* Store back the record. */
1238 if (tdb_store_by_string(tdb, NOTIFY_PID_LIST_KEY, data, TDB_REPLACE) == -1) {
1239 DEBUG(0,("print_notify_register_pid: Failed to update pid \
1240 list for printer %s\n", printername));
1248 tdb_unlock_bystring(tdb, NOTIFY_PID_LIST_KEY);
1250 release_print_db(pdb);
1251 SAFE_FREE(data.dptr);
1255 /****************************************************************************
1256 Update an entry in the print tdb that will allow us to send notify
1257 updates only to interested smbd's.
1258 ****************************************************************************/
1260 BOOL print_notify_deregister_pid(int snum)
1263 struct tdb_print_db *pdb = NULL;
1264 TDB_CONTEXT *tdb = NULL;
1265 const char *printername;
1266 uint32 mypid = (uint32)sys_getpid();
1271 printername = lp_const_servicename(snum);
1272 pdb = get_print_db_byname(printername);
1277 printername = PRINT_SERVER_ENTRY_NAME;
1279 tdb = conn_tdb_ctx();
1282 if (tdb_lock_bystring(tdb, NOTIFY_PID_LIST_KEY, 10) == -1) {
1283 DEBUG(0,("print_notify_register_pid: Failed to lock \
1284 printer %s database\n", printername));
1286 release_print_db(pdb);
1290 data = get_printer_notify_pid_list( tdb, printername, True );
1292 /* Reduce refcount. Remove ourselves if zero. */
1294 for (i = 0; i < data.dsize; ) {
1295 if (IVAL(data.dptr,i) == mypid) {
1296 uint32 refcount = IVAL(data.dptr, i+4);
1300 if (refcount == 0) {
1301 if (data.dsize - i > 8)
1302 memmove( &data.dptr[i], &data.dptr[i+8], data.dsize - i - 8);
1306 SIVAL(data.dptr, i+4, refcount);
1312 if (data.dsize == 0)
1313 SAFE_FREE(data.dptr);
1315 /* Store back the record. */
1316 if (tdb_store_by_string(tdb, NOTIFY_PID_LIST_KEY, data, TDB_REPLACE) == -1) {
1317 DEBUG(0,("print_notify_register_pid: Failed to update pid \
1318 list for printer %s\n", printername));
1326 tdb_unlock_bystring(tdb, NOTIFY_PID_LIST_KEY);
1328 release_print_db(pdb);
1329 SAFE_FREE(data.dptr);
1333 /****************************************************************************
1334 Check if a jobid is valid. It is valid if it exists in the database.
1335 ****************************************************************************/
1337 BOOL print_job_exists(int snum, uint32 jobid)
1339 struct tdb_print_db *pdb = get_print_db_byname(lp_const_servicename(snum));
1344 ret = tdb_exists(pdb->tdb, print_key(jobid));
1345 release_print_db(pdb);
1349 /****************************************************************************
1350 Give the fd used for a jobid.
1351 ****************************************************************************/
1353 int print_job_fd(int snum, uint32 jobid)
1355 struct printjob *pjob = print_job_find(snum, jobid);
1358 /* don't allow another process to get this info - it is meaningless */
1359 if (pjob->pid != local_pid)
1364 /****************************************************************************
1365 Give the filename used for a jobid.
1366 Only valid for the process doing the spooling and when the job
1367 has not been spooled.
1368 ****************************************************************************/
1370 char *print_job_fname(int snum, uint32 jobid)
1372 struct printjob *pjob = print_job_find(snum, jobid);
1373 if (!pjob || pjob->spooled || pjob->pid != local_pid)
1375 return pjob->filename;
1379 /****************************************************************************
1380 Give the filename used for a jobid.
1381 Only valid for the process doing the spooling and when the job
1382 has not been spooled.
1383 ****************************************************************************/
1385 NT_DEVICEMODE *print_job_devmode(int snum, uint32 jobid)
1387 struct printjob *pjob = print_job_find(snum, jobid);
1392 return pjob->nt_devmode;
1395 /****************************************************************************
1396 Set the place in the queue for a job.
1397 ****************************************************************************/
1399 BOOL print_job_set_place(int snum, uint32 jobid, int place)
1401 DEBUG(2,("print_job_set_place not implemented yet\n"));
1405 /****************************************************************************
1406 Set the name of a job. Only possible for owner.
1407 ****************************************************************************/
1409 BOOL print_job_set_name(int snum, uint32 jobid, char *name)
1411 struct printjob *pjob = print_job_find(snum, jobid);
1412 if (!pjob || pjob->pid != local_pid)
1415 fstrcpy(pjob->jobname, name);
1416 return pjob_store(snum, jobid, pjob);
1419 /****************************************************************************
1420 Delete a print job - don't update queue.
1421 ****************************************************************************/
1423 static BOOL print_job_delete1(int snum, uint32 jobid)
1425 struct printjob *pjob = print_job_find(snum, jobid);
1432 * If already deleting just return.
1435 if (pjob->status == LPQ_DELETING)
1438 /* Hrm - we need to be able to cope with deleting a job before it
1439 has reached the spooler. */
1441 if (pjob->sysjob == -1) {
1442 DEBUG(5, ("attempt to delete job %u not seen by lpr\n", (unsigned int)jobid));
1445 /* Set the tdb entry to be deleting. */
1447 pjob->status = LPQ_DELETING;
1448 pjob_store(snum, jobid, pjob);
1450 if (pjob->spooled && pjob->sysjob != -1)
1451 result = (*(current_printif->job_delete))(snum, pjob);
1453 /* Delete the tdb entry if the delete suceeded or the job hasn't
1457 pjob_delete(snum, jobid);
1459 return (result == 0);
1462 /****************************************************************************
1463 Return true if the current user owns the print job.
1464 ****************************************************************************/
1466 static BOOL is_owner(struct current_user *user, int snum, uint32 jobid)
1468 struct printjob *pjob = print_job_find(snum, jobid);
1474 if ((vuser = get_valid_user_struct(user->vuid)) != NULL) {
1475 return strequal(pjob->user, vuser->user.smb_name);
1477 return strequal(pjob->user, uidtoname(user->uid));
1481 /****************************************************************************
1483 ****************************************************************************/
1485 BOOL print_job_delete(struct current_user *user, int snum, uint32 jobid, WERROR *errcode)
1487 BOOL owner, deleted;
1492 owner = is_owner(user, snum, jobid);
1494 /* Check access against security descriptor or whether the user
1498 !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
1499 DEBUG(3, ("delete denied by security descriptor\n"));
1500 *errcode = WERR_ACCESS_DENIED;
1502 /* BEGIN_ADMIN_LOG */
1503 sys_adminlog( LOG_ERR, (char *)
1504 "Permission denied-- user not allowed to delete, \
1505 pause, or resume print job. User name: %s. Printer name: %s.",
1506 uidtoname(user->uid), PRINTERNAME(snum) );
1513 * get the spooled filename of the print job
1514 * if this works, then the file has not been spooled
1515 * to the underlying print system. Just delete the
1516 * spool file & return.
1519 if ( (fname = print_job_fname( snum, jobid )) != NULL )
1521 /* remove the spool file */
1522 DEBUG(10,("print_job_delete: Removing spool file [%s]\n", fname ));
1523 if ( unlink( fname ) == -1 ) {
1524 *errcode = map_werror_from_unix(errno);
1531 if (!print_job_delete1(snum, jobid)) {
1532 *errcode = WERR_ACCESS_DENIED;
1536 /* force update the database and say the delete failed if the
1539 print_queue_update(snum);
1541 deleted = !print_job_exists(snum, jobid);
1543 *errcode = WERR_ACCESS_DENIED;
1548 /****************************************************************************
1550 ****************************************************************************/
1552 BOOL print_job_pause(struct current_user *user, int snum, uint32 jobid, WERROR *errcode)
1554 struct printjob *pjob = print_job_find(snum, jobid);
1560 if (!pjob->spooled || pjob->sysjob == -1)
1563 if (!is_owner(user, snum, jobid) &&
1564 !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
1565 DEBUG(3, ("pause denied by security descriptor\n"));
1567 /* BEGIN_ADMIN_LOG */
1568 sys_adminlog( LOG_ERR, (char *)
1569 "Permission denied-- user not allowed to delete, \
1570 pause, or resume print job. User name: %s. Printer name: %s.",
1571 uidtoname(user->uid), PRINTERNAME(snum) );
1574 *errcode = WERR_ACCESS_DENIED;
1578 /* need to pause the spooled entry */
1579 ret = (*(current_printif->job_pause))(snum, pjob);
1582 *errcode = WERR_INVALID_PARAM;
1586 /* force update the database */
1587 print_cache_flush(snum);
1589 /* Send a printer notify message */
1591 notify_job_status(snum, jobid, JOB_STATUS_PAUSED);
1593 /* how do we tell if this succeeded? */
1598 /****************************************************************************
1600 ****************************************************************************/
1602 BOOL print_job_resume(struct current_user *user, int snum, uint32 jobid, WERROR *errcode)
1604 struct printjob *pjob = print_job_find(snum, jobid);
1610 if (!pjob->spooled || pjob->sysjob == -1)
1613 if (!is_owner(user, snum, jobid) &&
1614 !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
1615 DEBUG(3, ("resume denied by security descriptor\n"));
1616 *errcode = WERR_ACCESS_DENIED;
1618 /* BEGIN_ADMIN_LOG */
1619 sys_adminlog( LOG_ERR, (char *)
1620 "Permission denied-- user not allowed to delete, \
1621 pause, or resume print job. User name: %s. Printer name: %s.",
1622 uidtoname(user->uid), PRINTERNAME(snum) );
1627 ret = (*(current_printif->job_resume))(snum, pjob);
1630 *errcode = WERR_INVALID_PARAM;
1634 /* force update the database */
1635 print_cache_flush(snum);
1637 /* Send a printer notify message */
1639 notify_job_status(snum, jobid, JOB_STATUS_QUEUED);
1644 /****************************************************************************
1645 Write to a print file.
1646 ****************************************************************************/
1648 int print_job_write(int snum, uint32 jobid, const char *buf, int size)
1651 struct printjob *pjob = print_job_find(snum, jobid);
1655 /* don't allow another process to get this info - it is meaningless */
1656 if (pjob->pid != local_pid)
1659 return_code = write(pjob->fd, buf, size);
1660 if (return_code>0) {
1662 pjob_store(snum, jobid, pjob);
1667 /****************************************************************************
1668 Check if the print queue has been updated recently enough.
1669 ****************************************************************************/
1671 static BOOL print_cache_expired(int snum)
1674 time_t last_qscan_time, time_now = time(NULL);
1675 const char *printername = lp_const_servicename(snum);
1676 struct tdb_print_db *pdb = get_print_db_byname(printername);
1681 slprintf(key, sizeof(key), "CACHE/%s", printername);
1682 last_qscan_time = (time_t)tdb_fetch_int32(pdb->tdb, key);
1685 * Invalidate the queue for 3 reasons.
1686 * (1). last queue scan time == -1.
1687 * (2). Current time - last queue scan time > allowed cache time.
1688 * (3). last queue scan time > current time + MAX_CACHE_VALID_TIME (1 hour by default).
1689 * This last test picks up machines for which the clock has been moved
1690 * forward, an lpq scan done and then the clock moved back. Otherwise
1691 * that last lpq scan would stay around for a loooong loooong time... :-). JRA.
1694 if (last_qscan_time == ((time_t)-1) || (time_now - last_qscan_time) >= lp_lpqcachetime() ||
1695 last_qscan_time > (time_now + MAX_CACHE_VALID_TIME)) {
1696 DEBUG(3, ("print cache expired for queue %s \
1697 (last_qscan_time = %d, time now = %d, qcachetime = %d)\n", printername,
1698 (int)last_qscan_time, (int)time_now, (int)lp_lpqcachetime() ));
1699 release_print_db(pdb);
1702 release_print_db(pdb);
1706 /****************************************************************************
1707 Get the queue status - do not update if db is out of date.
1708 ****************************************************************************/
1710 static int get_queue_status(int snum, print_status_struct *status)
1714 const char *printername = lp_const_servicename(snum);
1715 struct tdb_print_db *pdb = get_print_db_byname(printername);
1719 ZERO_STRUCTP(status);
1720 slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", printername);
1722 key.dsize = strlen(keystr);
1723 data = tdb_fetch(pdb->tdb, key);
1724 release_print_db(pdb);
1726 if (data.dsize == sizeof(print_status_struct))
1727 memcpy(status, data.dptr, sizeof(print_status_struct));
1728 SAFE_FREE(data.dptr);
1730 return status->qcount;
1733 /****************************************************************************
1734 Determine the number of jobs in a queue.
1735 ****************************************************************************/
1737 int print_queue_length(int snum, print_status_struct *pstatus)
1739 print_status_struct status;
1742 /* make sure the database is up to date */
1743 if (print_cache_expired(snum))
1744 print_queue_update(snum);
1746 /* also fetch the queue status */
1747 memset(&status, 0, sizeof(status));
1748 len = get_queue_status(snum, &status);
1754 /***************************************************************************
1755 Start spooling a job - return the jobid.
1756 ***************************************************************************/
1758 uint32 print_job_start(struct current_user *user, int snum, char *jobname, NT_DEVICEMODE *nt_devmode )
1762 struct printjob pjob;
1766 const char *printername = lp_const_servicename(snum);
1767 struct tdb_print_db *pdb = get_print_db_byname(printername);
1768 BOOL pdb_locked = False;
1775 if (!print_access_check(user, snum, PRINTER_ACCESS_USE)) {
1776 DEBUG(3, ("print_job_start: job start denied by security descriptor\n"));
1777 release_print_db(pdb);
1781 if (!print_time_access_check(snum)) {
1782 DEBUG(3, ("print_job_start: job start denied by time check\n"));
1783 release_print_db(pdb);
1787 path = lp_pathname(snum);
1789 /* see if we have sufficient disk space */
1790 if (lp_minprintspace(snum)) {
1791 SMB_BIG_UINT dspace, dsize;
1792 if (sys_fsusage(path, &dspace, &dsize) == 0 &&
1793 dspace < 2*(SMB_BIG_UINT)lp_minprintspace(snum)) {
1794 DEBUG(3, ("print_job_start: disk space check failed.\n"));
1795 release_print_db(pdb);
1801 /* for autoloaded printers, check that the printcap entry still exists */
1802 if (lp_autoloaded(snum) && !pcap_printername_ok(lp_const_servicename(snum), NULL)) {
1803 DEBUG(3, ("print_job_start: printer name %s check failed.\n", lp_const_servicename(snum) ));
1804 release_print_db(pdb);
1809 /* Insure the maximum queue size is not violated */
1810 if ((njobs = print_queue_length(snum,NULL)) > lp_maxprintjobs(snum)) {
1811 DEBUG(3, ("print_job_start: number of jobs (%d) larger than max printjobs per queue (%d).\n",
1812 njobs, lp_maxprintjobs(snum) ));
1813 release_print_db(pdb);
1818 /* Lock the database - only wait 20 seconds. */
1819 if (tdb_lock_bystring(pdb->tdb, "INFO/nextjob", 20) == -1) {
1820 DEBUG(0,("print_job_start: failed to lock printing database %s\n", printername ));
1821 release_print_db(pdb);
1827 next_jobid = tdb_fetch_int32(pdb->tdb, "INFO/nextjob");
1828 if (next_jobid == -1)
1831 for (jobid = NEXT_JOBID(next_jobid); jobid != next_jobid; jobid = NEXT_JOBID(jobid)) {
1832 if (!print_job_exists(snum, jobid))
1836 if (jobid == next_jobid) {
1837 DEBUG(3, ("print_job_start: jobid (%d)==next_jobid(%d).\n",
1838 jobid, next_jobid ));
1843 /* Store a dummy placeholder. This must be quick as we have the lock. */
1848 if (tdb_store(pdb->tdb, print_key(jobid), dum, TDB_INSERT) == -1) {
1849 DEBUG(3, ("print_job_start: jobid (%d) failed to store placeholder.\n",
1856 if (tdb_store_int32(pdb->tdb, "INFO/nextjob", jobid)==-1) {
1857 DEBUG(3, ("print_job_start: failed to store INFO/nextjob.\n"));
1862 /* We've finished with the INFO/nextjob lock. */
1863 tdb_unlock_bystring(pdb->tdb, "INFO/nextjob");
1866 /* create the database entry */
1870 pjob.pid = local_pid;
1873 pjob.starttime = time(NULL);
1874 pjob.status = LPQ_SPOOLING;
1876 pjob.spooled = False;
1878 pjob.nt_devmode = nt_devmode;
1880 fstrcpy(pjob.jobname, jobname);
1882 if ((vuser = get_valid_user_struct(user->vuid)) != NULL) {
1883 fstrcpy(pjob.user, vuser->user.smb_name);
1885 fstrcpy(pjob.user, uidtoname(user->uid));
1888 fstrcpy(pjob.queuename, lp_const_servicename(snum));
1890 /* we have a job entry - now create the spool file */
1891 slprintf(pjob.filename, sizeof(pjob.filename)-1, "%s/%s%.8u.XXXXXX",
1892 path, PRINT_SPOOL_PREFIX, (unsigned int)jobid);
1893 pjob.fd = smb_mkstemp(pjob.filename);
1895 if (pjob.fd == -1) {
1896 if (errno == EACCES) {
1897 /* Common setup error, force a report. */
1898 DEBUG(0, ("print_job_start: insufficient permissions \
1899 to open spool file %s.\n", pjob.filename));
1901 /* Normal case, report at level 3 and above. */
1902 DEBUG(3, ("print_job_start: can't open spool file %s,\n", pjob.filename));
1903 DEBUGADD(3, ("errno = %d (%s).\n", errno, strerror(errno)));
1908 pjob_store(snum, jobid, &pjob);
1910 release_print_db(pdb);
1916 pjob_delete(snum, jobid);
1919 tdb_unlock_bystring(pdb->tdb, "INFO/nextjob");
1920 release_print_db(pdb);
1922 DEBUG(3, ("print_job_start: returning fail. Error = %s\n", strerror(errno) ));
1926 /****************************************************************************
1927 Update the number of pages spooled to jobid
1928 ****************************************************************************/
1930 void print_job_endpage(int snum, uint32 jobid)
1932 struct printjob *pjob = print_job_find(snum, jobid);
1935 /* don't allow another process to get this info - it is meaningless */
1936 if (pjob->pid != local_pid)
1940 pjob_store(snum, jobid, pjob);
1943 /****************************************************************************
1944 Print a file - called on closing the file. This spools the job.
1945 If normal close is false then we're tearing down the jobs - treat as an
1947 ****************************************************************************/
1949 BOOL print_job_end(int snum, uint32 jobid, BOOL normal_close)
1951 struct printjob *pjob = print_job_find(snum, jobid);
1953 SMB_STRUCT_STAT sbuf;
1958 if (pjob->spooled || pjob->pid != local_pid)
1961 if (normal_close && (sys_fstat(pjob->fd, &sbuf) == 0)) {
1962 pjob->size = sbuf.st_size;
1968 * Not a normal close or we couldn't stat the job file,
1969 * so something has gone wrong. Cleanup.
1973 DEBUG(3,("print_job_end: failed to stat file for jobid %d\n", jobid ));
1977 /* Technically, this is not quite right. If the printer has a separator
1978 * page turned on, the NT spooler prints the separator page even if the
1979 * print job is 0 bytes. 010215 JRR */
1980 if (pjob->size == 0 || pjob->status == LPQ_DELETING) {
1981 /* don't bother spooling empty files or something being deleted. */
1982 DEBUG(5,("print_job_end: canceling spool of %s (%s)\n",
1983 pjob->filename, pjob->size ? "deleted" : "zero length" ));
1984 unlink(pjob->filename);
1985 pjob_delete(snum, jobid);
1989 ret = (*(current_printif->job_submit))(snum, pjob);
1994 /* The print job has been sucessfully handed over to the back-end */
1996 pjob->spooled = True;
1997 pjob->status = LPQ_QUEUED;
1998 pjob_store(snum, jobid, pjob);
2000 /* make sure the database is up to date */
2001 if (print_cache_expired(snum))
2002 print_queue_update(snum);
2008 /* The print job was not succesfully started. Cleanup */
2009 /* Still need to add proper error return propagation! 010122:JRR */
2010 unlink(pjob->filename);
2011 pjob_delete(snum, jobid);
2015 /****************************************************************************
2016 Utility fn to enumerate the print queue.
2017 ****************************************************************************/
2019 static int traverse_fn_queue(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
2021 struct traverse_struct *ts = (struct traverse_struct *)state;
2022 struct printjob pjob;
2028 if ( key.dsize != sizeof(jobid) )
2031 memcpy(&jobid, key.dptr, sizeof(jobid));
2033 if ( unpack_pjob( data.dptr, data.dsize, &pjob ) == -1 )
2035 free_nt_devicemode( &pjob.nt_devmode );
2037 /* maybe it isn't for this queue */
2038 if (ts->snum != lp_servicenumber(pjob.queuename))
2041 if (ts->qcount >= ts->maxcount)
2046 ts->queue[i].job = jobid;
2047 ts->queue[i].size = pjob.size;
2048 ts->queue[i].page_count = pjob.page_count;
2049 ts->queue[i].status = pjob.status;
2050 ts->queue[i].priority = 1;
2051 ts->queue[i].time = pjob.starttime;
2052 fstrcpy(ts->queue[i].fs_user, pjob.user);
2053 fstrcpy(ts->queue[i].fs_file, pjob.jobname);
2060 struct traverse_count_struct {
2064 /****************************************************************************
2065 Utility fn to count the number of entries in the print queue.
2066 ****************************************************************************/
2068 static int traverse_count_fn_queue(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
2070 struct traverse_count_struct *ts = (struct traverse_count_struct *)state;
2071 struct printjob pjob;
2076 if ( key.dsize != sizeof(jobid) )
2079 memcpy(&jobid, key.dptr, sizeof(jobid));
2081 if ( unpack_pjob( data.dptr, data.dsize, &pjob ) == -1 )
2084 free_nt_devicemode( &pjob.nt_devmode );
2086 /* maybe it isn't for this queue - this cannot happen with the tdb/printer code. JRA */
2087 if (ts->snum != lp_servicenumber(pjob.queuename))
2095 /****************************************************************************
2096 Sort print jobs by submittal time.
2097 ****************************************************************************/
2099 static int printjob_comp(print_queue_struct *j1, print_queue_struct *j2)
2110 /* Sort on job start time */
2112 if (j1->time == j2->time)
2114 return (j1->time > j2->time) ? 1 : -1;
2117 /****************************************************************************
2118 Get a printer queue listing.
2119 ****************************************************************************/
2121 int print_queue_status(int snum,
2122 print_queue_struct **queue,
2123 print_status_struct *status)
2125 struct traverse_struct tstruct;
2126 struct traverse_count_struct tsc;
2129 const char *printername = lp_const_servicename(snum);
2130 struct tdb_print_db *pdb = get_print_db_byname(printername);
2137 /* make sure the database is up to date */
2138 if (print_cache_expired(snum))
2139 print_queue_update(snum);
2142 * Fetch the queue status. We must do this first, as there may
2143 * be no jobs in the queue.
2145 ZERO_STRUCTP(status);
2146 slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", printername);
2148 key.dsize = strlen(keystr);
2149 data = tdb_fetch(pdb->tdb, key);
2151 if (data.dsize == sizeof(*status)) {
2152 memcpy(status, data.dptr, sizeof(*status));
2154 SAFE_FREE(data.dptr);
2158 * Now, fetch the print queue information. We first count the number
2159 * of entries, and then only retrieve the queue if necessary.
2164 tdb_traverse(pdb->tdb, traverse_count_fn_queue, (void *)&tsc);
2166 if (tsc.count == 0) {
2167 release_print_db(pdb);
2171 /* Allocate the queue size. */
2172 if ((tstruct.queue = (print_queue_struct *)
2173 malloc(sizeof(print_queue_struct)*tsc.count)) == NULL) {
2174 release_print_db(pdb);
2179 * Fill in the queue.
2180 * We need maxcount as the queue size may have changed between
2181 * the two calls to tdb_traverse.
2184 tstruct.maxcount = tsc.count;
2185 tstruct.snum = snum;
2187 tdb_traverse(pdb->tdb, traverse_fn_queue, (void *)&tstruct);
2188 release_print_db(pdb);
2190 /* Sort the queue by submission time otherwise they are displayed
2193 qsort(tstruct.queue, tstruct.qcount, sizeof(print_queue_struct),
2194 QSORT_CAST(printjob_comp));
2196 *queue = tstruct.queue;
2197 return tstruct.qcount;
2200 /****************************************************************************
2201 Turn a queue name into a snum.
2202 ****************************************************************************/
2204 int print_queue_snum(const char *qname)
2206 int snum = lp_servicenumber(qname);
2207 if (snum == -1 || !lp_print_ok(snum))
2212 /****************************************************************************
2214 ****************************************************************************/
2216 BOOL print_queue_pause(struct current_user *user, int snum, WERROR *errcode)
2220 if (!print_access_check(user, snum, PRINTER_ACCESS_ADMINISTER)) {
2221 *errcode = WERR_ACCESS_DENIED;
2225 ret = (*(current_printif->queue_pause))(snum);
2228 *errcode = WERR_INVALID_PARAM;
2232 /* force update the database */
2233 print_cache_flush(snum);
2235 /* Send a printer notify message */
2237 notify_printer_status(snum, PRINTER_STATUS_PAUSED);
2242 /****************************************************************************
2244 ****************************************************************************/
2246 BOOL print_queue_resume(struct current_user *user, int snum, WERROR *errcode)
2250 if (!print_access_check(user, snum, PRINTER_ACCESS_ADMINISTER)) {
2251 *errcode = WERR_ACCESS_DENIED;
2255 ret = (*(current_printif->queue_resume))(snum);
2258 *errcode = WERR_INVALID_PARAM;
2262 /* make sure the database is up to date */
2263 if (print_cache_expired(snum))
2264 print_queue_update(snum);
2266 /* Send a printer notify message */
2268 notify_printer_status(snum, PRINTER_STATUS_OK);
2273 /****************************************************************************
2274 Purge a queue - implemented by deleting all jobs that we can delete.
2275 ****************************************************************************/
2277 BOOL print_queue_purge(struct current_user *user, int snum, WERROR *errcode)
2279 print_queue_struct *queue;
2280 print_status_struct status;
2284 /* Force and update so the count is accurate (i.e. not a cached count) */
2285 print_queue_update(snum);
2287 can_job_admin = print_access_check(user, snum, JOB_ACCESS_ADMINISTER);
2288 njobs = print_queue_status(snum, &queue, &status);
2290 for (i=0;i<njobs;i++) {
2291 BOOL owner = is_owner(user, snum, queue[i].job);
2293 if (owner || can_job_admin) {
2294 print_job_delete1(snum, queue[i].job);