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));
74 /* Not found - create and store mapping. */
75 rap_jobid = ++next_rap_jobid;
77 rap_jobid = ++next_rap_jobid;
78 data.dptr = (char *)&rap_jobid;
79 data.dsize = sizeof(rap_jobid);
80 tdb_store(rap_tdb, key, data, TDB_REPLACE);
81 tdb_store(rap_tdb, data, key, TDB_REPLACE);
85 BOOL rap_to_pjobid(uint16 rap_jobid, int *psnum, uint32 *pjobid)
93 key.dptr = (char *)&rap_jobid;
94 key.dsize = sizeof(rap_jobid);
95 data = tdb_fetch(rap_tdb, key);
96 if (data.dptr && data.dsize == sizeof(jinfo)) {
97 *psnum = IVAL(&jinfo,0);
98 *pjobid = IVAL(&jinfo,4);
105 static void rap_jobid_delete(int snum, uint32 jobid)
114 SIVAL(&jinfo,0,(int32)snum);
115 SIVAL(&jinfo,4,jobid);
117 key.dptr = (char *)&jinfo;
118 key.dsize = sizeof(jinfo);
119 data = tdb_fetch(rap_tdb, key);
120 if (!data.dptr || (data.dsize != sizeof(uint16)))
123 memcpy(&rap_jobid, data.dptr, sizeof(uint16));
124 SAFE_FREE(data.dptr);
125 data.dptr = (char *)&rap_jobid;
126 data.dsize = sizeof(rap_jobid);
127 tdb_delete(rap_tdb, key);
128 tdb_delete(rap_tdb, data);
131 static pid_t local_pid;
133 static int get_queue_status(int, print_status_struct *);
135 /* There can be this many printing tdb's open, plus any locked ones. */
136 #define MAX_PRINT_DBS_OPEN 1
138 struct tdb_print_db {
139 struct tdb_print_db *next, *prev;
142 fstring printer_name;
145 static struct tdb_print_db *print_db_head;
147 /****************************************************************************
148 Function to find or create the printer specific job tdb given a printername.
149 Limits the number of tdb's open to MAX_PRINT_DBS_OPEN.
150 ****************************************************************************/
152 static struct tdb_print_db *get_print_db_byname(const char *printername)
154 struct tdb_print_db *p = NULL, *last_entry = NULL;
156 pstring printdb_path;
157 BOOL done_become_root = False;
159 for (p = print_db_head, last_entry = print_db_head; p; p = p->next) {
160 /* Ensure the list terminates... JRA. */
161 SMB_ASSERT(p->next != print_db_head);
163 if (p->tdb && strequal(p->printer_name, printername)) {
164 DLIST_PROMOTE(print_db_head, p);
173 if (num_open >= MAX_PRINT_DBS_OPEN) {
174 /* Try and recycle the last entry. */
175 DLIST_PROMOTE(print_db_head, last_entry);
177 for (p = print_db_head; p; p = p->next) {
181 if (tdb_close(print_db_head->tdb)) {
182 DEBUG(0,("get_print_db: Failed to close tdb for printer %s\n",
183 print_db_head->printer_name ));
189 memset(p->printer_name, '\0', sizeof(p->printer_name));
193 DLIST_PROMOTE(print_db_head, p);
200 p = (struct tdb_print_db *)malloc(sizeof(struct tdb_print_db));
202 DEBUG(0,("get_print_db: malloc fail !\n"));
206 DLIST_ADD(print_db_head, p);
209 pstrcpy(printdb_path, lock_path("printing/"));
210 pstrcat(printdb_path, printername);
211 pstrcat(printdb_path, ".tdb");
213 if (geteuid() != 0) {
215 done_become_root = True;
218 p->tdb = tdb_open_log(printdb_path, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
220 if (done_become_root)
224 DEBUG(0,("get_print_db: Failed to open printer backend database %s.\n",
226 DLIST_REMOVE(print_db_head, p);
230 fstrcpy(p->printer_name, printername);
235 /***************************************************************************
236 Remove a reference count.
237 ****************************************************************************/
239 static void release_print_db( struct tdb_print_db *pdb)
242 SMB_ASSERT(pdb->ref_count >= 0);
245 /***************************************************************************
246 Close all open print db entries.
247 ****************************************************************************/
249 static void close_all_print_db(void)
251 struct tdb_print_db *p = NULL, *next_p = NULL;
253 for (p = print_db_head; p; p = next_p) {
258 DLIST_REMOVE(print_db_head, p);
264 /****************************************************************************
265 Initialise the printing backend. Called once at startup before the fork().
266 ****************************************************************************/
268 BOOL print_backend_init(void)
270 char *sversion = "INFO/version";
271 pstring printing_path;
272 int services = lp_numservices();
275 if (local_pid == sys_getpid())
278 unlink(lock_path("printing.tdb"));
279 pstrcpy(printing_path,lock_path("printing"));
280 mkdir(printing_path,0755);
282 local_pid = sys_getpid();
284 /* handle a Samba upgrade */
286 for (snum = 0; snum < services; snum++) {
287 struct tdb_print_db *pdb;
288 if (!lp_print_ok(snum))
291 pdb = get_print_db_byname(lp_const_servicename(snum));
294 if (tdb_lock_bystring(pdb->tdb, sversion, 0) == -1) {
295 DEBUG(0,("print_backend_init: Failed to open printer %s database\n", lp_const_servicename(snum) ));
296 release_print_db(pdb);
299 if (tdb_fetch_int32(pdb->tdb, sversion) != PRINT_DATABASE_VERSION) {
300 tdb_traverse(pdb->tdb, tdb_traverse_delete_fn, NULL);
301 tdb_store_int32(pdb->tdb, sversion, PRINT_DATABASE_VERSION);
303 tdb_unlock_bystring(pdb->tdb, sversion);
304 release_print_db(pdb);
307 close_all_print_db(); /* Don't leave any open. */
309 /* select the appropriate printing interface... */
311 if (strcmp(lp_printcapname(), "cups") == 0)
312 current_printif = &cups_printif;
313 #endif /* HAVE_CUPS */
315 /* do NT print initialization... */
316 return nt_printing_init();
319 /****************************************************************************
320 Shut down printing backend. Called once at shutdown to close the tdb.
321 ****************************************************************************/
323 void printing_end(void)
325 close_all_print_db(); /* Don't leave any open. */
328 /****************************************************************************
329 Useful function to generate a tdb key.
330 ****************************************************************************/
332 static TDB_DATA print_key(uint32 jobid)
338 ret.dptr = (void *)&j;
339 ret.dsize = sizeof(j);
343 /***********************************************************************
344 unpack a pjob from a tdb buffer
345 ***********************************************************************/
347 int unpack_pjob( char* buf, int buflen, struct printjob *pjob )
355 len += tdb_unpack(buf+len, buflen-len, "dddddddddffff",
373 if ( (used = unpack_devicemode(&pjob->nt_devmode, buf+len, buflen-len)) == -1 )
382 /****************************************************************************
383 Useful function to find a print job in the database.
384 ****************************************************************************/
386 static struct printjob *print_job_find(int snum, uint32 jobid)
388 static struct printjob pjob;
390 struct tdb_print_db *pdb = get_print_db_byname(lp_const_servicename(snum));
396 ret = tdb_fetch(pdb->tdb, print_key(jobid));
397 release_print_db(pdb);
402 if ( pjob.nt_devmode )
403 free_nt_devicemode( &pjob.nt_devmode );
407 if ( unpack_pjob( ret.dptr, ret.dsize, &pjob ) == -1 )
414 /* Convert a unix jobid to a smb jobid */
416 static uint32 sysjob_to_jobid_value;
418 static int unixjob_traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA key,
419 TDB_DATA data, void *state)
421 struct printjob *pjob;
422 int *sysjob = (int *)state;
424 if (!data.dptr || data.dsize == 0)
427 pjob = (struct printjob *)data.dptr;
428 if (key.dsize != sizeof(uint32))
431 if (*sysjob == pjob->sysjob) {
432 uint32 *jobid = (uint32 *)key.dptr;
434 sysjob_to_jobid_value = *jobid;
441 /****************************************************************************
442 This is a *horribly expensive call as we have to iterate through all the
443 current printer tdb's. Don't do this often ! JRA.
444 ****************************************************************************/
446 uint32 sysjob_to_jobid(int unix_jobid)
448 int services = lp_numservices();
451 sysjob_to_jobid_value = (uint32)-1;
453 for (snum = 0; snum < services; snum++) {
454 struct tdb_print_db *pdb;
455 if (!lp_print_ok(snum))
457 pdb = get_print_db_byname(lp_const_servicename(snum));
459 tdb_traverse(pdb->tdb, unixjob_traverse_fn, &unix_jobid);
460 release_print_db(pdb);
461 if (sysjob_to_jobid_value != (uint32)-1)
462 return sysjob_to_jobid_value;
467 /****************************************************************************
468 Send notifications based on what has changed after a pjob_store.
469 ****************************************************************************/
473 uint32 spoolss_status;
474 } lpq_to_spoolss_status_map[] = {
475 { LPQ_QUEUED, JOB_STATUS_QUEUED },
476 { LPQ_PAUSED, JOB_STATUS_PAUSED },
477 { LPQ_SPOOLING, JOB_STATUS_SPOOLING },
478 { LPQ_PRINTING, JOB_STATUS_PRINTING },
479 { LPQ_DELETING, JOB_STATUS_DELETING },
480 { LPQ_OFFLINE, JOB_STATUS_OFFLINE },
481 { LPQ_PAPEROUT, JOB_STATUS_PAPEROUT },
482 { LPQ_PRINTED, JOB_STATUS_PRINTED },
483 { LPQ_DELETED, JOB_STATUS_DELETED },
484 { LPQ_BLOCKED, JOB_STATUS_BLOCKED },
485 { LPQ_USER_INTERVENTION, JOB_STATUS_USER_INTERVENTION },
489 /* Convert a lpq status value stored in printing.tdb into the
490 appropriate win32 API constant. */
492 static uint32 map_to_spoolss_status(uint32 lpq_status)
496 while (lpq_to_spoolss_status_map[i].lpq_status != -1) {
497 if (lpq_to_spoolss_status_map[i].lpq_status == lpq_status)
498 return lpq_to_spoolss_status_map[i].spoolss_status;
505 static void pjob_store_notify(int snum, uint32 jobid, struct printjob *old_data,
506 struct printjob *new_data)
508 BOOL new_job = False;
513 /* Notify the job name first */
515 if (new_job || !strequal(old_data->jobname, new_data->jobname))
516 notify_job_name(snum, jobid, new_data->jobname);
518 /* Job attributes that can't be changed. We only send
519 notification for these on a new job. */
522 notify_job_submitted(snum, jobid, new_data->starttime);
523 notify_job_username(snum, jobid, new_data->user);
526 /* Job attributes of a new job or attributes that can be
529 if (new_job || old_data->status != new_data->status)
530 notify_job_status(snum, jobid, map_to_spoolss_status(new_data->status));
532 if (new_job || old_data->size != new_data->size)
533 notify_job_total_bytes(snum, jobid, new_data->size);
535 if (new_job || old_data->page_count != new_data->page_count)
536 notify_job_total_pages(snum, jobid, new_data->page_count);
539 /****************************************************************************
540 Store a job structure back to the database.
541 ****************************************************************************/
543 static BOOL pjob_store(int snum, uint32 jobid, struct printjob *pjob)
545 TDB_DATA old_data, new_data;
547 struct tdb_print_db *pdb = get_print_db_byname(lp_const_servicename(snum));
549 int len, newlen, buflen;
557 old_data = tdb_fetch(pdb->tdb, print_key(jobid));
559 /* Doh! Now we have to pack/unpack data since the NT_DEVICEMODE was added */
566 len += tdb_pack(buf+len, buflen-len, "dddddddddffff",
581 len += pack_devicemode(pjob->nt_devmode, buf+len, buflen-len);
587 tb = (char *)Realloc(buf, len);
589 DEBUG(0,("pjob_store: failed to enlarge buffer!\n"));
597 while ( buflen != len );
603 new_data.dsize = len;
604 ret = (tdb_store(pdb->tdb, print_key(jobid), new_data, TDB_REPLACE) == 0);
606 release_print_db(pdb);
608 /* Send notify updates for what has changed */
610 if ( ret && (old_data.dsize == 0 || old_data.dsize == sizeof(*pjob)) )
611 pjob_store_notify( snum, jobid, (struct printjob *)old_data.dptr, pjob );
614 SAFE_FREE( old_data.dptr );
620 /****************************************************************************
621 Remove a job structure from the database.
622 ****************************************************************************/
624 static void pjob_delete(int snum, uint32 jobid)
626 struct printjob *pjob = print_job_find(snum, jobid);
627 uint32 job_status = 0;
628 struct tdb_print_db *pdb = get_print_db_byname(lp_const_servicename(snum));
634 DEBUG(5, ("pjob_delete(): we were asked to delete nonexistent job %u\n",
635 (unsigned int)jobid));
636 release_print_db(pdb);
640 /* Send a notification that a job has been deleted */
642 job_status = map_to_spoolss_status(pjob->status);
644 /* We must cycle through JOB_STATUS_DELETING and
645 JOB_STATUS_DELETED for the port monitor to delete the job
648 job_status |= JOB_STATUS_DELETING;
649 notify_job_status(snum, jobid, job_status);
651 job_status |= JOB_STATUS_DELETED;
652 notify_job_status(snum, jobid, job_status);
654 /* Remove from printing.tdb */
656 tdb_delete(pdb->tdb, print_key(jobid));
657 release_print_db(pdb);
658 rap_jobid_delete(snum, jobid);
661 /****************************************************************************
662 Parse a file name from the system spooler to generate a jobid.
663 ****************************************************************************/
665 static uint32 print_parse_jobid(char *fname)
669 if (strncmp(fname,PRINT_SPOOL_PREFIX,strlen(PRINT_SPOOL_PREFIX)) != 0)
671 fname += strlen(PRINT_SPOOL_PREFIX);
677 return (uint32)jobid;
680 /****************************************************************************
681 List a unix job in the print database.
682 ****************************************************************************/
684 static void print_unix_job(int snum, print_queue_struct *q)
686 uint32 jobid = q->job + UNIX_JOB_START;
687 struct printjob pj, *old_pj;
689 /* Preserve the timestamp on an existing unix print job */
691 old_pj = print_job_find(snum, jobid);
698 pj.starttime = old_pj ? old_pj->starttime : q->time;
699 pj.status = q->status;
703 fstrcpy(pj.filename, "");
704 fstrcpy(pj.jobname, q->fs_file);
705 fstrcpy(pj.user, q->fs_user);
706 fstrcpy(pj.queuename, lp_const_servicename(snum));
708 pjob_store(snum, jobid, &pj);
712 struct traverse_struct {
713 print_queue_struct *queue;
714 int qcount, snum, maxcount, total_jobs;
717 /****************************************************************************
718 Utility fn to delete any jobs that are no longer active.
719 ****************************************************************************/
721 static int traverse_fn_delete(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
723 struct traverse_struct *ts = (struct traverse_struct *)state;
724 struct printjob pjob;
728 if ( key.dsize != sizeof(jobid) )
731 memcpy(&jobid, key.dptr, sizeof(jobid));
732 if ( unpack_pjob( data.dptr, data.dsize, &pjob ) == -1 )
734 free_nt_devicemode( &pjob.nt_devmode );
737 if (ts->snum != lp_servicenumber(pjob.queuename)) {
738 /* this isn't for the queue we are looking at - this cannot happen with the split tdb's. JRA */
743 /* remove a unix job if it isn't in the system queue any more */
745 for (i=0;i<ts->qcount;i++) {
746 uint32 u_jobid = (ts->queue[i].job + UNIX_JOB_START);
747 if (jobid == u_jobid)
751 pjob_delete(ts->snum, jobid);
757 /* maybe it hasn't been spooled yet */
759 /* if a job is not spooled and the process doesn't
760 exist then kill it. This cleans up after smbd
762 if (!process_exists(pjob.pid))
763 pjob_delete(ts->snum, jobid);
769 for (i=0;i<ts->qcount;i++) {
770 uint32 curr_jobid = print_parse_jobid(ts->queue[i].fs_file);
771 if (jobid == curr_jobid)
775 /* The job isn't in the system queue - we have to assume it has
776 completed, so delete the database entry. */
778 if (i == ts->qcount) {
779 time_t cur_t = time(NULL);
781 /* A race can occur between the time a job is spooled and
782 when it appears in the lpq output. This happens when
783 the job is added to printing.tdb when another smbd
784 running print_queue_update() has completed a lpq and
785 is currently traversing the printing tdb and deleting jobs.
786 A workaround is to not delete the job if it has been
787 submitted less than lp_lpqcachetime() seconds ago. */
789 if ((cur_t - pjob.starttime) > lp_lpqcachetime())
790 pjob_delete(ts->snum, jobid);
800 /****************************************************************************
801 Check if the print queue has been updated recently enough.
802 ****************************************************************************/
804 static void print_cache_flush(int snum)
807 const char *printername = lp_const_servicename(snum);
808 struct tdb_print_db *pdb = get_print_db_byname(printername);
812 slprintf(key, sizeof(key)-1, "CACHE/%s", printername);
813 tdb_store_int32(pdb->tdb, key, -1);
814 release_print_db(pdb);
817 /****************************************************************************
818 Check if someone already thinks they are doing the update.
819 ****************************************************************************/
821 static pid_t get_updating_pid(fstring printer_name)
826 struct tdb_print_db *pdb = get_print_db_byname(printer_name);
830 slprintf(keystr, sizeof(keystr)-1, "UPDATING/%s", printer_name);
832 key.dsize = strlen(keystr);
834 data = tdb_fetch(pdb->tdb, key);
835 release_print_db(pdb);
836 if (!data.dptr || data.dsize != sizeof(pid_t))
839 memcpy(&updating_pid, data.dptr, sizeof(pid_t));
840 SAFE_FREE(data.dptr);
842 if (process_exists(updating_pid))
848 /****************************************************************************
849 Set the fact that we're doing the update, or have finished doing the update
851 ****************************************************************************/
853 static void set_updating_pid(const fstring printer_name, BOOL delete)
858 pid_t updating_pid = sys_getpid();
859 struct tdb_print_db *pdb = get_print_db_byname(printer_name);
864 slprintf(keystr, sizeof(keystr)-1, "UPDATING/%s", printer_name);
866 key.dsize = strlen(keystr);
869 tdb_delete(pdb->tdb, key);
870 release_print_db(pdb);
874 data.dptr = (void *)&updating_pid;
875 data.dsize = sizeof(pid_t);
877 tdb_store(pdb->tdb, key, data, TDB_REPLACE);
878 release_print_db(pdb);
881 /****************************************************************************
882 Update the internal database from the system print queue for a queue.
883 ****************************************************************************/
885 static void print_queue_update(int snum)
888 print_queue_struct *queue = NULL;
889 print_status_struct status;
890 print_status_struct old_status;
891 struct printjob *pjob;
892 struct traverse_struct tstruct;
893 fstring keystr, printer_name, cachestr;
895 struct tdb_print_db *pdb;
897 fstrcpy(printer_name, lp_const_servicename(snum));
898 pdb = get_print_db_byname(printer_name);
903 * Check to see if someone else is doing this update.
904 * This is essentially a mutex on the update.
907 if (get_updating_pid(printer_name) != -1) {
908 release_print_db(pdb);
912 /* Lock the queue for the database update */
914 slprintf(keystr, sizeof(keystr) - 1, "LOCK/%s", printer_name);
915 /* Only wait 10 seconds for this. */
916 if (tdb_lock_bystring(pdb->tdb, keystr, 10) == -1) {
917 DEBUG(0,("print_queue_update: Failed to lock printer %s database\n", printer_name));
918 release_print_db(pdb);
923 * Ensure that no one else got in here.
924 * If the updating pid is still -1 then we are
928 if (get_updating_pid(printer_name) != -1) {
930 * Someone else is doing the update, exit.
932 tdb_unlock_bystring(pdb->tdb, keystr);
933 release_print_db(pdb);
938 * We're going to do the update ourselves.
941 /* Tell others we're doing the update. */
942 set_updating_pid(printer_name, False);
945 * Allow others to enter and notice we're doing
949 tdb_unlock_bystring(pdb->tdb, keystr);
952 * Update the cache time FIRST ! Stops others even
953 * attempting to get the lock and doing this
954 * if the lpq takes a long time.
957 slprintf(cachestr, sizeof(cachestr)-1, "CACHE/%s", printer_name);
958 tdb_store_int32(pdb->tdb, cachestr, (int)time(NULL));
960 /* get the current queue using the appropriate interface */
963 qcount = (*(current_printif->queue_get))(snum, &queue, &status);
965 DEBUG(3, ("%d job%s in queue for %s\n", qcount, (qcount != 1) ?
966 "s" : "", printer_name));
969 any job in the internal database that is marked as spooled
970 and doesn't exist in the system queue is considered finished
971 and removed from the database
973 any job in the system database but not in the internal database
974 is added as a unix job
976 fill in any system job numbers as we go
978 for (i=0; i<qcount; i++) {
979 uint32 jobid = print_parse_jobid(queue[i].fs_file);
981 if (jobid == (uint32)-1) {
982 /* assume its a unix print job */
983 print_unix_job(snum, &queue[i]);
987 /* we have an active SMB print job - update its status */
988 pjob = print_job_find(snum, jobid);
990 /* err, somethings wrong. Probably smbd was restarted
991 with jobs in the queue. All we can do is treat them
992 like unix jobs. Pity. */
993 print_unix_job(snum, &queue[i]);
997 pjob->sysjob = queue[i].job;
998 pjob->status = queue[i].status;
1000 pjob_store(snum, jobid, pjob);
1003 /* now delete any queued entries that don't appear in the
1005 tstruct.queue = queue;
1006 tstruct.qcount = qcount;
1007 tstruct.snum = snum;
1008 tstruct.total_jobs = 0;
1010 tdb_traverse(pdb->tdb, traverse_fn_delete, (void *)&tstruct);
1012 SAFE_FREE(tstruct.queue);
1014 tdb_store_int32(pdb->tdb, "INFO/total_jobs", tstruct.total_jobs);
1016 if( qcount != get_queue_status(snum, &old_status))
1017 DEBUG(10,("print_queue_update: queue status change %d jobs -> %d jobs for printer %s\n",
1018 old_status.qcount, qcount, printer_name ));
1020 /* store the new queue status structure */
1021 slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", printer_name);
1023 key.dsize = strlen(keystr);
1025 status.qcount = qcount;
1026 data.dptr = (void *)&status;
1027 data.dsize = sizeof(status);
1028 tdb_store(pdb->tdb, key, data, TDB_REPLACE);
1031 * Update the cache time again. We want to do this call
1032 * as little as possible...
1035 slprintf(keystr, sizeof(keystr)-1, "CACHE/%s", printer_name);
1036 tdb_store_int32(pdb->tdb, keystr, (int32)time(NULL));
1038 /* Delete our pid from the db. */
1039 set_updating_pid(printer_name, True);
1040 release_print_db(pdb);
1043 /****************************************************************************
1044 Fetch and clean the pid_t record list for all pids interested in notify
1045 messages. data needs freeing on exit.
1046 ****************************************************************************/
1048 #define NOTIFY_PID_LIST_KEY "NOTIFY_PID_LIST"
1050 static TDB_DATA get_printer_notify_pid_list(struct tdb_print_db *pdb)
1057 data = tdb_fetch_by_string( pdb->tdb, NOTIFY_PID_LIST_KEY );
1064 if (data.dsize % 8) {
1065 DEBUG(0,("get_pid_list: Size of record for printer %s not a multiple of 8 !\n",
1066 pdb->printer_name ));
1067 tdb_delete_by_string(pdb->tdb, NOTIFY_PID_LIST_KEY );
1073 * Weed out all dead entries.
1076 for( i = 0; i < data.dsize; ) {
1077 pid_t pid = (pid_t)IVAL(data.dptr, i);
1079 if (pid == sys_getpid())
1082 /* Entry is dead if process doesn't exist or refcount is zero. */
1084 if ((IVAL(data.dptr, i + 4) == 0) || !process_exists(pid)) {
1086 /* Refcount == zero is a logic error and should never happen. */
1087 if (IVAL(data.dptr, i + 4) == 0) {
1088 DEBUG(0,("get_pid_list: Refcount == 0 for pid = %u printer %s !\n",
1089 (unsigned int)pid, pdb->printer_name ));
1092 if (data.dsize - i > 8)
1093 memmove( &data.dptr[i], &data.dptr[i+8], data.dsize - i - 8);
1104 /****************************************************************************
1105 Return a malloced list of pid_t's that are interested in getting update
1106 messages on this print queue. Used in printing/notify to send the messages.
1107 ****************************************************************************/
1109 BOOL print_notify_pid_list(const char *printername, TALLOC_CTX *mem_ctx, size_t *p_num_pids, pid_t **pp_pid_list)
1111 struct tdb_print_db *pdb;
1118 *pp_pid_list = NULL;
1120 pdb = get_print_db_byname(printername);
1124 if (tdb_lock_bystring(pdb->tdb, NOTIFY_PID_LIST_KEY, 10) == -1) {
1125 DEBUG(0,("print_notify_pid_list: Failed to lock printer %s database\n", printername));
1126 release_print_db(pdb);
1130 data = get_printer_notify_pid_list( pdb );
1137 num_pids = data.dsize / 8;
1139 if ((pid_list = (pid_t *)talloc(mem_ctx, sizeof(pid_t) * num_pids)) == NULL) {
1144 for( i = 0; i < data.dsize; i += 8) {
1145 pid_t pid = (pid_t)IVAL(data.dptr, i);
1149 *pp_pid_list = pid_list;
1150 *p_num_pids = num_pids;
1156 tdb_unlock_bystring(pdb->tdb, NOTIFY_PID_LIST_KEY);
1157 release_print_db(pdb);
1158 SAFE_FREE(data.dptr);
1162 /****************************************************************************
1163 Create/Update an entry in the print tdb that will allow us to send notify
1164 updates only to interested smbd's.
1165 ****************************************************************************/
1167 BOOL print_notify_register_pid(int snum)
1170 struct tdb_print_db *pdb;
1171 const char *printername = lp_const_servicename(snum);
1172 uint32 mypid = (uint32)sys_getpid();
1176 pdb = get_print_db_byname(printername);
1180 if (tdb_lock_bystring(pdb->tdb, NOTIFY_PID_LIST_KEY, 10) == -1) {
1181 DEBUG(0,("print_notify_register_pid: Failed to lock printer %s\n", printername));
1182 release_print_db(pdb);
1186 data = get_printer_notify_pid_list( pdb );
1188 /* Add ourselves and increase the refcount. */
1190 for (i = 0; i < data.dsize; i += 8) {
1191 if (IVAL(data.dptr,i) == mypid) {
1192 uint32 new_refcount = IVAL(data.dptr, i+4) + 1;
1193 SIVAL(data.dptr, i+4, new_refcount);
1198 if (i == data.dsize) {
1199 /* We weren't in the list. Realloc. */
1200 data.dptr = Realloc(data.dptr, data.dsize + 8);
1202 DEBUG(0,("print_notify_register_pid: Relloc fail for printer %s\n", printername));
1206 SIVAL(data.dptr,data.dsize - 8,mypid);
1207 SIVAL(data.dptr,data.dsize - 4,1); /* Refcount. */
1210 /* Store back the record. */
1211 if (tdb_store_by_string(pdb->tdb, NOTIFY_PID_LIST_KEY, data, TDB_REPLACE) == -1) {
1212 DEBUG(0,("print_notify_register_pid: Failed to update pid list for printer %s\n", printername));
1220 tdb_unlock_bystring(pdb->tdb, NOTIFY_PID_LIST_KEY);
1221 release_print_db(pdb);
1222 SAFE_FREE(data.dptr);
1226 /****************************************************************************
1227 Update an entry in the print tdb that will allow us to send notify
1228 updates only to interested smbd's.
1229 ****************************************************************************/
1231 BOOL print_notify_deregister_pid(int snum)
1234 struct tdb_print_db *pdb;
1235 const char *printername = lp_const_servicename(snum);
1236 uint32 mypid = (uint32)sys_getpid();
1240 pdb = get_print_db_byname(printername);
1244 if (tdb_lock_bystring(pdb->tdb, NOTIFY_PID_LIST_KEY, 10) == -1) {
1245 DEBUG(0,("print_notify_register_pid: Failed to lock printer %s database\n", printername));
1246 release_print_db(pdb);
1250 data = get_printer_notify_pid_list( pdb );
1252 /* Reduce refcount. Remove ourselves if zero. */
1254 for (i = 0; i < data.dsize; ) {
1255 if (IVAL(data.dptr,i) == mypid) {
1256 uint32 refcount = IVAL(data.dptr, i+4);
1260 if (refcount == 0) {
1261 if (data.dsize - i > 8)
1262 memmove( &data.dptr[i], &data.dptr[i+8], data.dsize - i - 8);
1266 SIVAL(data.dptr, i+4, refcount);
1272 if (data.dsize == 0)
1273 SAFE_FREE(data.dptr);
1275 /* Store back the record. */
1276 if (tdb_store_by_string(pdb->tdb, NOTIFY_PID_LIST_KEY, data, TDB_REPLACE) == -1) {
1277 DEBUG(0,("print_notify_register_pid: Failed to update pid list for printer %s\n", printername));
1285 tdb_unlock_bystring(pdb->tdb, NOTIFY_PID_LIST_KEY);
1286 release_print_db(pdb);
1287 SAFE_FREE(data.dptr);
1291 /****************************************************************************
1292 Check if a jobid is valid. It is valid if it exists in the database.
1293 ****************************************************************************/
1295 BOOL print_job_exists(int snum, uint32 jobid)
1297 struct tdb_print_db *pdb = get_print_db_byname(lp_const_servicename(snum));
1302 ret = tdb_exists(pdb->tdb, print_key(jobid));
1303 release_print_db(pdb);
1307 /****************************************************************************
1308 Give the fd used for a jobid.
1309 ****************************************************************************/
1311 int print_job_fd(int snum, uint32 jobid)
1313 struct printjob *pjob = print_job_find(snum, jobid);
1316 /* don't allow another process to get this info - it is meaningless */
1317 if (pjob->pid != local_pid)
1322 /****************************************************************************
1323 Give the filename used for a jobid.
1324 Only valid for the process doing the spooling and when the job
1325 has not been spooled.
1326 ****************************************************************************/
1328 char *print_job_fname(int snum, uint32 jobid)
1330 struct printjob *pjob = print_job_find(snum, jobid);
1331 if (!pjob || pjob->spooled || pjob->pid != local_pid)
1333 return pjob->filename;
1337 /****************************************************************************
1338 Give the filename used for a jobid.
1339 Only valid for the process doing the spooling and when the job
1340 has not been spooled.
1341 ****************************************************************************/
1343 NT_DEVICEMODE *print_job_devmode(int snum, uint32 jobid)
1345 struct printjob *pjob = print_job_find(snum, jobid);
1350 return pjob->nt_devmode;
1353 /****************************************************************************
1354 Set the place in the queue for a job.
1355 ****************************************************************************/
1357 BOOL print_job_set_place(int snum, uint32 jobid, int place)
1359 DEBUG(2,("print_job_set_place not implemented yet\n"));
1363 /****************************************************************************
1364 Set the name of a job. Only possible for owner.
1365 ****************************************************************************/
1367 BOOL print_job_set_name(int snum, uint32 jobid, char *name)
1369 struct printjob *pjob = print_job_find(snum, jobid);
1370 if (!pjob || pjob->pid != local_pid)
1373 fstrcpy(pjob->jobname, name);
1374 return pjob_store(snum, jobid, pjob);
1377 /****************************************************************************
1378 Delete a print job - don't update queue.
1379 ****************************************************************************/
1381 static BOOL print_job_delete1(int snum, uint32 jobid)
1383 struct printjob *pjob = print_job_find(snum, jobid);
1390 * If already deleting just return.
1393 if (pjob->status == LPQ_DELETING)
1396 /* Hrm - we need to be able to cope with deleting a job before it
1397 has reached the spooler. */
1399 if (pjob->sysjob == -1) {
1400 DEBUG(5, ("attempt to delete job %u not seen by lpr\n", (unsigned int)jobid));
1403 /* Set the tdb entry to be deleting. */
1405 pjob->status = LPQ_DELETING;
1406 pjob_store(snum, jobid, pjob);
1408 if (pjob->spooled && pjob->sysjob != -1)
1409 result = (*(current_printif->job_delete))(snum, pjob);
1411 /* Delete the tdb entry if the delete suceeded or the job hasn't
1415 pjob_delete(snum, jobid);
1417 return (result == 0);
1420 /****************************************************************************
1421 Return true if the current user owns the print job.
1422 ****************************************************************************/
1424 static BOOL is_owner(struct current_user *user, int snum, uint32 jobid)
1426 struct printjob *pjob = print_job_find(snum, jobid);
1432 if ((vuser = get_valid_user_struct(user->vuid)) != NULL) {
1433 return strequal(pjob->user, vuser->user.smb_name);
1435 return strequal(pjob->user, uidtoname(user->uid));
1439 /****************************************************************************
1441 ****************************************************************************/
1443 BOOL print_job_delete(struct current_user *user, int snum, uint32 jobid, WERROR *errcode)
1445 BOOL owner, deleted;
1450 owner = is_owner(user, snum, jobid);
1452 /* Check access against security descriptor or whether the user
1456 !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
1457 DEBUG(3, ("delete denied by security descriptor\n"));
1458 *errcode = WERR_ACCESS_DENIED;
1463 * get the spooled filename of the print job
1464 * if this works, then the file has not been spooled
1465 * to the underlying print system. Just delete the
1466 * spool file & return.
1469 if ( (fname = print_job_fname( snum, jobid )) != NULL )
1471 /* remove the spool file */
1472 DEBUG(10,("print_job_delete: Removing spool file [%s]\n", fname ));
1473 if ( unlink( fname ) == -1 ) {
1474 *errcode = map_werror_from_unix(errno);
1481 if (!print_job_delete1(snum, jobid)) {
1482 *errcode = WERR_ACCESS_DENIED;
1486 /* force update the database and say the delete failed if the
1489 print_queue_update(snum);
1491 deleted = !print_job_exists(snum, jobid);
1493 *errcode = WERR_ACCESS_DENIED;
1498 /****************************************************************************
1500 ****************************************************************************/
1502 BOOL print_job_pause(struct current_user *user, int snum, uint32 jobid, WERROR *errcode)
1504 struct printjob *pjob = print_job_find(snum, jobid);
1510 if (!pjob->spooled || pjob->sysjob == -1)
1513 if (!is_owner(user, snum, jobid) &&
1514 !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
1515 DEBUG(3, ("pause denied by security descriptor\n"));
1516 *errcode = WERR_ACCESS_DENIED;
1520 /* need to pause the spooled entry */
1521 ret = (*(current_printif->job_pause))(snum, pjob);
1524 *errcode = WERR_INVALID_PARAM;
1528 /* force update the database */
1529 print_cache_flush(snum);
1531 /* Send a printer notify message */
1533 notify_job_status(snum, jobid, JOB_STATUS_PAUSED);
1535 /* how do we tell if this succeeded? */
1540 /****************************************************************************
1542 ****************************************************************************/
1544 BOOL print_job_resume(struct current_user *user, int snum, uint32 jobid, WERROR *errcode)
1546 struct printjob *pjob = print_job_find(snum, jobid);
1552 if (!pjob->spooled || pjob->sysjob == -1)
1555 if (!is_owner(user, snum, jobid) &&
1556 !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
1557 DEBUG(3, ("resume denied by security descriptor\n"));
1558 *errcode = WERR_ACCESS_DENIED;
1562 ret = (*(current_printif->job_resume))(snum, pjob);
1565 *errcode = WERR_INVALID_PARAM;
1569 /* force update the database */
1570 print_cache_flush(snum);
1572 /* Send a printer notify message */
1574 notify_job_status(snum, jobid, JOB_STATUS_QUEUED);
1579 /****************************************************************************
1580 Write to a print file.
1581 ****************************************************************************/
1583 int print_job_write(int snum, uint32 jobid, const char *buf, int size)
1586 struct printjob *pjob = print_job_find(snum, jobid);
1590 /* don't allow another process to get this info - it is meaningless */
1591 if (pjob->pid != local_pid)
1594 return_code = write(pjob->fd, buf, size);
1595 if (return_code>0) {
1597 pjob_store(snum, jobid, pjob);
1602 /****************************************************************************
1603 Check if the print queue has been updated recently enough.
1604 ****************************************************************************/
1606 static BOOL print_cache_expired(int snum)
1609 time_t last_qscan_time, time_now = time(NULL);
1610 const char *printername = lp_const_servicename(snum);
1611 struct tdb_print_db *pdb = get_print_db_byname(printername);
1616 slprintf(key, sizeof(key), "CACHE/%s", printername);
1617 last_qscan_time = (time_t)tdb_fetch_int32(pdb->tdb, key);
1620 * Invalidate the queue for 3 reasons.
1621 * (1). last queue scan time == -1.
1622 * (2). Current time - last queue scan time > allowed cache time.
1623 * (3). last queue scan time > current time + MAX_CACHE_VALID_TIME (1 hour by default).
1624 * This last test picks up machines for which the clock has been moved
1625 * forward, an lpq scan done and then the clock moved back. Otherwise
1626 * that last lpq scan would stay around for a loooong loooong time... :-). JRA.
1629 if (last_qscan_time == ((time_t)-1) || (time_now - last_qscan_time) >= lp_lpqcachetime() ||
1630 last_qscan_time > (time_now + MAX_CACHE_VALID_TIME)) {
1631 DEBUG(3, ("print cache expired for queue %s \
1632 (last_qscan_time = %d, time now = %d, qcachetime = %d)\n", printername,
1633 (int)last_qscan_time, (int)time_now, (int)lp_lpqcachetime() ));
1634 release_print_db(pdb);
1637 release_print_db(pdb);
1641 /****************************************************************************
1642 Get the queue status - do not update if db is out of date.
1643 ****************************************************************************/
1645 static int get_queue_status(int snum, print_status_struct *status)
1649 const char *printername = lp_const_servicename(snum);
1650 struct tdb_print_db *pdb = get_print_db_byname(printername);
1654 ZERO_STRUCTP(status);
1655 slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", printername);
1657 key.dsize = strlen(keystr);
1658 data = tdb_fetch(pdb->tdb, key);
1659 release_print_db(pdb);
1661 if (data.dsize == sizeof(print_status_struct))
1662 memcpy(status, data.dptr, sizeof(print_status_struct));
1663 SAFE_FREE(data.dptr);
1665 return status->qcount;
1668 /****************************************************************************
1669 Determine the number of jobs in a queue.
1670 ****************************************************************************/
1672 int print_queue_length(int snum, print_status_struct *pstatus)
1674 print_status_struct status;
1677 /* make sure the database is up to date */
1678 if (print_cache_expired(snum))
1679 print_queue_update(snum);
1681 /* also fetch the queue status */
1682 memset(&status, 0, sizeof(status));
1683 len = get_queue_status(snum, &status);
1689 /***************************************************************************
1690 Start spooling a job - return the jobid.
1691 ***************************************************************************/
1693 uint32 print_job_start(struct current_user *user, int snum, char *jobname, NT_DEVICEMODE *nt_devmode )
1697 struct printjob pjob;
1701 const char *printername = lp_const_servicename(snum);
1702 struct tdb_print_db *pdb = get_print_db_byname(printername);
1703 BOOL pdb_locked = False;
1710 if (!print_access_check(user, snum, PRINTER_ACCESS_USE)) {
1711 DEBUG(3, ("print_job_start: job start denied by security descriptor\n"));
1712 release_print_db(pdb);
1716 if (!print_time_access_check(snum)) {
1717 DEBUG(3, ("print_job_start: job start denied by time check\n"));
1718 release_print_db(pdb);
1722 path = lp_pathname(snum);
1724 /* see if we have sufficient disk space */
1725 if (lp_minprintspace(snum)) {
1726 SMB_BIG_UINT dspace, dsize;
1727 if (sys_fsusage(path, &dspace, &dsize) == 0 &&
1728 dspace < 2*(SMB_BIG_UINT)lp_minprintspace(snum)) {
1729 DEBUG(3, ("print_job_start: disk space check failed.\n"));
1730 release_print_db(pdb);
1736 /* for autoloaded printers, check that the printcap entry still exists */
1737 if (lp_autoloaded(snum) && !pcap_printername_ok(lp_const_servicename(snum), NULL)) {
1738 DEBUG(3, ("print_job_start: printer name %s check failed.\n", lp_const_servicename(snum) ));
1739 release_print_db(pdb);
1744 /* Insure the maximum queue size is not violated */
1745 if ((njobs = print_queue_length(snum,NULL)) > lp_maxprintjobs(snum)) {
1746 DEBUG(3, ("print_job_start: number of jobs (%d) larger than max printjobs per queue (%d).\n",
1747 njobs, lp_maxprintjobs(snum) ));
1748 release_print_db(pdb);
1753 /* Lock the database - only wait 20 seconds. */
1754 if (tdb_lock_bystring(pdb->tdb, "INFO/nextjob", 20) == -1) {
1755 DEBUG(0,("print_job_start: failed to lock printing database %s\n", printername ));
1756 release_print_db(pdb);
1762 next_jobid = tdb_fetch_int32(pdb->tdb, "INFO/nextjob");
1763 if (next_jobid == -1)
1766 for (jobid = NEXT_JOBID(next_jobid); jobid != next_jobid; jobid = NEXT_JOBID(jobid)) {
1767 if (!print_job_exists(snum, jobid))
1771 if (jobid == next_jobid) {
1772 DEBUG(3, ("print_job_start: jobid (%d)==next_jobid(%d).\n",
1773 jobid, next_jobid ));
1778 /* Store a dummy placeholder. This must be quick as we have the lock. */
1783 if (tdb_store(pdb->tdb, print_key(jobid), dum, TDB_INSERT) == -1) {
1784 DEBUG(3, ("print_job_start: jobid (%d) failed to store placeholder.\n",
1791 if (tdb_store_int32(pdb->tdb, "INFO/nextjob", jobid)==-1) {
1792 DEBUG(3, ("print_job_start: failed to store INFO/nextjob.\n"));
1797 /* We've finished with the INFO/nextjob lock. */
1798 tdb_unlock_bystring(pdb->tdb, "INFO/nextjob");
1801 /* create the database entry */
1805 pjob.pid = local_pid;
1808 pjob.starttime = time(NULL);
1809 pjob.status = LPQ_SPOOLING;
1811 pjob.spooled = False;
1813 pjob.nt_devmode = nt_devmode;
1815 fstrcpy(pjob.jobname, jobname);
1817 if ((vuser = get_valid_user_struct(user->vuid)) != NULL) {
1818 fstrcpy(pjob.user, vuser->user.smb_name);
1820 fstrcpy(pjob.user, uidtoname(user->uid));
1823 fstrcpy(pjob.queuename, lp_const_servicename(snum));
1825 /* we have a job entry - now create the spool file */
1826 slprintf(pjob.filename, sizeof(pjob.filename)-1, "%s/%s%.8u.XXXXXX",
1827 path, PRINT_SPOOL_PREFIX, (unsigned int)jobid);
1828 pjob.fd = smb_mkstemp(pjob.filename);
1830 if (pjob.fd == -1) {
1831 if (errno == EACCES) {
1832 /* Common setup error, force a report. */
1833 DEBUG(0, ("print_job_start: insufficient permissions \
1834 to open spool file %s.\n", pjob.filename));
1836 /* Normal case, report at level 3 and above. */
1837 DEBUG(3, ("print_job_start: can't open spool file %s,\n", pjob.filename));
1838 DEBUGADD(3, ("errno = %d (%s).\n", errno, strerror(errno)));
1843 pjob_store(snum, jobid, &pjob);
1845 release_print_db(pdb);
1851 pjob_delete(snum, jobid);
1854 tdb_unlock_bystring(pdb->tdb, "INFO/nextjob");
1855 release_print_db(pdb);
1857 DEBUG(3, ("print_job_start: returning fail. Error = %s\n", strerror(errno) ));
1861 /****************************************************************************
1862 Update the number of pages spooled to jobid
1863 ****************************************************************************/
1865 void print_job_endpage(int snum, uint32 jobid)
1867 struct printjob *pjob = print_job_find(snum, jobid);
1870 /* don't allow another process to get this info - it is meaningless */
1871 if (pjob->pid != local_pid)
1875 pjob_store(snum, jobid, pjob);
1878 /****************************************************************************
1879 Print a file - called on closing the file. This spools the job.
1880 If normal close is false then we're tearing down the jobs - treat as an
1882 ****************************************************************************/
1884 BOOL print_job_end(int snum, uint32 jobid, BOOL normal_close)
1886 struct printjob *pjob = print_job_find(snum, jobid);
1888 SMB_STRUCT_STAT sbuf;
1893 if (pjob->spooled || pjob->pid != local_pid)
1896 if (normal_close && (sys_fstat(pjob->fd, &sbuf) == 0)) {
1897 pjob->size = sbuf.st_size;
1903 * Not a normal close or we couldn't stat the job file,
1904 * so something has gone wrong. Cleanup.
1908 DEBUG(3,("print_job_end: failed to stat file for jobid %d\n", jobid ));
1912 /* Technically, this is not quite right. If the printer has a separator
1913 * page turned on, the NT spooler prints the separator page even if the
1914 * print job is 0 bytes. 010215 JRR */
1915 if (pjob->size == 0 || pjob->status == LPQ_DELETING) {
1916 /* don't bother spooling empty files or something being deleted. */
1917 DEBUG(5,("print_job_end: canceling spool of %s (%s)\n",
1918 pjob->filename, pjob->size ? "deleted" : "zero length" ));
1919 unlink(pjob->filename);
1920 pjob_delete(snum, jobid);
1924 ret = (*(current_printif->job_submit))(snum, pjob);
1929 /* The print job has been sucessfully handed over to the back-end */
1931 pjob->spooled = True;
1932 pjob->status = LPQ_QUEUED;
1933 pjob_store(snum, jobid, pjob);
1935 /* make sure the database is up to date */
1936 if (print_cache_expired(snum))
1937 print_queue_update(snum);
1943 /* The print job was not succesfully started. Cleanup */
1944 /* Still need to add proper error return propagation! 010122:JRR */
1945 unlink(pjob->filename);
1946 pjob_delete(snum, jobid);
1950 /****************************************************************************
1951 Utility fn to enumerate the print queue.
1952 ****************************************************************************/
1954 static int traverse_fn_queue(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
1956 struct traverse_struct *ts = (struct traverse_struct *)state;
1957 struct printjob pjob;
1963 if ( key.dsize != sizeof(jobid) )
1966 memcpy(&jobid, key.dptr, sizeof(jobid));
1968 if ( unpack_pjob( data.dptr, data.dsize, &pjob ) == -1 )
1970 free_nt_devicemode( &pjob.nt_devmode );
1972 /* maybe it isn't for this queue */
1973 if (ts->snum != lp_servicenumber(pjob.queuename))
1976 if (ts->qcount >= ts->maxcount)
1981 ts->queue[i].job = jobid;
1982 ts->queue[i].size = pjob.size;
1983 ts->queue[i].page_count = pjob.page_count;
1984 ts->queue[i].status = pjob.status;
1985 ts->queue[i].priority = 1;
1986 ts->queue[i].time = pjob.starttime;
1987 fstrcpy(ts->queue[i].fs_user, pjob.user);
1988 fstrcpy(ts->queue[i].fs_file, pjob.jobname);
1995 struct traverse_count_struct {
1999 /****************************************************************************
2000 Utility fn to count the number of entries in the print queue.
2001 ****************************************************************************/
2003 static int traverse_count_fn_queue(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
2005 struct traverse_count_struct *ts = (struct traverse_count_struct *)state;
2006 struct printjob pjob;
2011 if ( key.dsize != sizeof(jobid) )
2014 memcpy(&jobid, key.dptr, sizeof(jobid));
2016 if ( unpack_pjob( data.dptr, data.dsize, &pjob ) == -1 )
2019 free_nt_devicemode( &pjob.nt_devmode );
2021 /* maybe it isn't for this queue - this cannot happen with the tdb/printer code. JRA */
2022 if (ts->snum != lp_servicenumber(pjob.queuename))
2030 /****************************************************************************
2031 Sort print jobs by submittal time.
2032 ****************************************************************************/
2034 static int printjob_comp(print_queue_struct *j1, print_queue_struct *j2)
2045 /* Sort on job start time */
2047 if (j1->time == j2->time)
2049 return (j1->time > j2->time) ? 1 : -1;
2052 /****************************************************************************
2053 Get a printer queue listing.
2054 ****************************************************************************/
2056 int print_queue_status(int snum,
2057 print_queue_struct **queue,
2058 print_status_struct *status)
2060 struct traverse_struct tstruct;
2061 struct traverse_count_struct tsc;
2064 const char *printername = lp_const_servicename(snum);
2065 struct tdb_print_db *pdb = get_print_db_byname(printername);
2072 /* make sure the database is up to date */
2073 if (print_cache_expired(snum))
2074 print_queue_update(snum);
2077 * Fetch the queue status. We must do this first, as there may
2078 * be no jobs in the queue.
2080 ZERO_STRUCTP(status);
2081 slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", printername);
2083 key.dsize = strlen(keystr);
2084 data = tdb_fetch(pdb->tdb, key);
2086 if (data.dsize == sizeof(*status)) {
2087 memcpy(status, data.dptr, sizeof(*status));
2089 SAFE_FREE(data.dptr);
2093 * Now, fetch the print queue information. We first count the number
2094 * of entries, and then only retrieve the queue if necessary.
2099 tdb_traverse(pdb->tdb, traverse_count_fn_queue, (void *)&tsc);
2101 if (tsc.count == 0) {
2102 release_print_db(pdb);
2106 /* Allocate the queue size. */
2107 if ((tstruct.queue = (print_queue_struct *)
2108 malloc(sizeof(print_queue_struct)*tsc.count)) == NULL) {
2109 release_print_db(pdb);
2114 * Fill in the queue.
2115 * We need maxcount as the queue size may have changed between
2116 * the two calls to tdb_traverse.
2119 tstruct.maxcount = tsc.count;
2120 tstruct.snum = snum;
2122 tdb_traverse(pdb->tdb, traverse_fn_queue, (void *)&tstruct);
2123 release_print_db(pdb);
2125 /* Sort the queue by submission time otherwise they are displayed
2128 qsort(tstruct.queue, tstruct.qcount, sizeof(print_queue_struct),
2129 QSORT_CAST(printjob_comp));
2131 *queue = tstruct.queue;
2132 return tstruct.qcount;
2135 /****************************************************************************
2136 Turn a queue name into a snum.
2137 ****************************************************************************/
2139 int print_queue_snum(const char *qname)
2141 int snum = lp_servicenumber(qname);
2142 if (snum == -1 || !lp_print_ok(snum))
2147 /****************************************************************************
2149 ****************************************************************************/
2151 BOOL print_queue_pause(struct current_user *user, int snum, WERROR *errcode)
2155 if (!print_access_check(user, snum, PRINTER_ACCESS_ADMINISTER)) {
2156 *errcode = WERR_ACCESS_DENIED;
2160 ret = (*(current_printif->queue_pause))(snum);
2163 *errcode = WERR_INVALID_PARAM;
2167 /* force update the database */
2168 print_cache_flush(snum);
2170 /* Send a printer notify message */
2172 notify_printer_status(snum, PRINTER_STATUS_PAUSED);
2177 /****************************************************************************
2179 ****************************************************************************/
2181 BOOL print_queue_resume(struct current_user *user, int snum, WERROR *errcode)
2185 if (!print_access_check(user, snum, PRINTER_ACCESS_ADMINISTER)) {
2186 *errcode = WERR_ACCESS_DENIED;
2190 ret = (*(current_printif->queue_resume))(snum);
2193 *errcode = WERR_INVALID_PARAM;
2197 /* make sure the database is up to date */
2198 if (print_cache_expired(snum))
2199 print_queue_update(snum);
2201 /* Send a printer notify message */
2203 notify_printer_status(snum, PRINTER_STATUS_OK);
2208 /****************************************************************************
2209 Purge a queue - implemented by deleting all jobs that we can delete.
2210 ****************************************************************************/
2212 BOOL print_queue_purge(struct current_user *user, int snum, WERROR *errcode)
2214 print_queue_struct *queue;
2215 print_status_struct status;
2219 /* Force and update so the count is accurate (i.e. not a cached count) */
2220 print_queue_update(snum);
2222 can_job_admin = print_access_check(user, snum, JOB_ACCESS_ADMINISTER);
2223 njobs = print_queue_status(snum, &queue, &status);
2225 for (i=0;i<njobs;i++) {
2226 BOOL owner = is_owner(user, snum, queue[i].job);
2228 if (owner || can_job_admin) {
2229 print_job_delete1(snum, queue[i].job);