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)
56 DEBUG(10,("pjobid_to_rap: called.\n"));
59 /* Create the in-memory tdb. */
60 rap_tdb = tdb_open_log(NULL, 0, TDB_INTERNAL, (O_RDWR|O_CREAT), 0644);
65 SIVAL(&jinfo,0,(int32)snum);
66 SIVAL(&jinfo,4,jobid);
68 key.dptr = (char *)&jinfo;
69 key.dsize = sizeof(jinfo);
70 data = tdb_fetch(rap_tdb, key);
71 if (data.dptr && data.dsize == sizeof(uint16)) {
72 memcpy(&rap_jobid, data.dptr, sizeof(uint16));
74 DEBUG(10,("pjobid_to_rap: jobid %u maps to RAP jobid %u\n",
76 (unsigned int)rap_jobid));
80 /* Not found - create and store mapping. */
81 rap_jobid = ++next_rap_jobid;
83 rap_jobid = ++next_rap_jobid;
84 data.dptr = (char *)&rap_jobid;
85 data.dsize = sizeof(rap_jobid);
86 tdb_store(rap_tdb, key, data, TDB_REPLACE);
87 tdb_store(rap_tdb, data, key, TDB_REPLACE);
89 DEBUG(10,("pjobid_to_rap: created jobid %u maps to RAP jobid %u\n",
91 (unsigned int)rap_jobid));
95 BOOL rap_to_pjobid(uint16 rap_jobid, int *psnum, uint32 *pjobid)
99 DEBUG(10,("rap_to_pjobid called.\n"));
104 key.dptr = (char *)&rap_jobid;
105 key.dsize = sizeof(rap_jobid);
106 data = tdb_fetch(rap_tdb, key);
107 if (data.dptr && data.dsize == 8) {
108 *psnum = IVAL(data.dptr,0);
109 *pjobid = IVAL(data.dptr,4);
110 DEBUG(10,("rap_to_pjobid: jobid %u maps to RAP jobid %u\n",
111 (unsigned int)*pjobid,
112 (unsigned int)rap_jobid));
113 SAFE_FREE(data.dptr);
117 DEBUG(10,("rap_to_pjobid: Failed to lookup RAP jobid %u\n",
118 (unsigned int)rap_jobid));
119 SAFE_FREE(data.dptr);
123 static void rap_jobid_delete(int snum, uint32 jobid)
129 DEBUG(10,("rap_jobid_delete: called.\n"));
134 SIVAL(&jinfo,0,(int32)snum);
135 SIVAL(&jinfo,4,jobid);
137 key.dptr = (char *)&jinfo;
138 key.dsize = sizeof(jinfo);
139 data = tdb_fetch(rap_tdb, key);
140 if (!data.dptr || (data.dsize != sizeof(uint16))) {
141 DEBUG(10,("rap_jobid_delete: cannot find jobid %u\n",
142 (unsigned int)jobid ));
143 SAFE_FREE(data.dptr);
147 DEBUG(10,("rap_jobid_delete: deleting jobid %u\n",
148 (unsigned int)jobid ));
150 memcpy(&rap_jobid, data.dptr, sizeof(uint16));
151 SAFE_FREE(data.dptr);
152 data.dptr = (char *)&rap_jobid;
153 data.dsize = sizeof(rap_jobid);
154 tdb_delete(rap_tdb, key);
155 tdb_delete(rap_tdb, data);
158 static pid_t local_pid;
160 static int get_queue_status(int, print_status_struct *);
162 /****************************************************************************
163 Initialise the printing backend. Called once at startup before the fork().
164 ****************************************************************************/
166 BOOL print_backend_init(void)
168 const char *sversion = "INFO/version";
169 pstring printing_path;
170 int services = lp_numservices();
173 if (local_pid == sys_getpid())
176 unlink(lock_path("printing.tdb"));
177 pstrcpy(printing_path,lock_path("printing"));
178 mkdir(printing_path,0755);
180 local_pid = sys_getpid();
182 /* handle a Samba upgrade */
184 for (snum = 0; snum < services; snum++) {
185 struct tdb_print_db *pdb;
186 if (!lp_print_ok(snum))
189 pdb = get_print_db_byname(lp_const_servicename(snum));
192 if (tdb_lock_bystring(pdb->tdb, sversion, 0) == -1) {
193 DEBUG(0,("print_backend_init: Failed to open printer %s database\n", lp_const_servicename(snum) ));
194 release_print_db(pdb);
197 if (tdb_fetch_int32(pdb->tdb, sversion) != PRINT_DATABASE_VERSION) {
198 tdb_traverse(pdb->tdb, tdb_traverse_delete_fn, NULL);
199 tdb_store_int32(pdb->tdb, sversion, PRINT_DATABASE_VERSION);
201 tdb_unlock_bystring(pdb->tdb, sversion);
202 release_print_db(pdb);
205 close_all_print_db(); /* Don't leave any open. */
207 /* select the appropriate printing interface... */
209 if (strcmp(lp_printcapname(), "cups") == 0)
210 current_printif = &cups_printif;
211 #endif /* HAVE_CUPS */
213 /* do NT print initialization... */
214 return nt_printing_init();
217 /****************************************************************************
218 Shut down printing backend. Called once at shutdown to close the tdb.
219 ****************************************************************************/
221 void printing_end(void)
223 close_all_print_db(); /* Don't leave any open. */
226 /****************************************************************************
227 Useful function to generate a tdb key.
228 ****************************************************************************/
230 static TDB_DATA print_key(uint32 jobid)
236 ret.dptr = (void *)&j;
237 ret.dsize = sizeof(j);
241 /***********************************************************************
242 unpack a pjob from a tdb buffer
243 ***********************************************************************/
245 int unpack_pjob( char* buf, int buflen, struct printjob *pjob )
253 len += tdb_unpack(buf+len, buflen-len, "dddddddddffff",
271 if ( (used = unpack_devicemode(&pjob->nt_devmode, buf+len, buflen-len)) == -1 )
280 /****************************************************************************
281 Useful function to find a print job in the database.
282 ****************************************************************************/
284 static struct printjob *print_job_find(int snum, uint32 jobid)
286 static struct printjob pjob;
288 struct tdb_print_db *pdb = get_print_db_byname(lp_const_servicename(snum));
294 ret = tdb_fetch(pdb->tdb, print_key(jobid));
295 release_print_db(pdb);
300 if ( pjob.nt_devmode )
301 free_nt_devicemode( &pjob.nt_devmode );
305 if ( unpack_pjob( ret.dptr, ret.dsize, &pjob ) == -1 ) {
314 /* Convert a unix jobid to a smb jobid */
316 static uint32 sysjob_to_jobid_value;
318 static int unixjob_traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA key,
319 TDB_DATA data, void *state)
321 struct printjob *pjob;
322 int *sysjob = (int *)state;
324 if (!data.dptr || data.dsize == 0)
327 pjob = (struct printjob *)data.dptr;
328 if (key.dsize != sizeof(uint32))
331 if (*sysjob == pjob->sysjob) {
332 uint32 *jobid = (uint32 *)key.dptr;
334 sysjob_to_jobid_value = *jobid;
341 /****************************************************************************
342 This is a *horribly expensive call as we have to iterate through all the
343 current printer tdb's. Don't do this often ! JRA.
344 ****************************************************************************/
346 uint32 sysjob_to_jobid(int unix_jobid)
348 int services = lp_numservices();
351 sysjob_to_jobid_value = (uint32)-1;
353 for (snum = 0; snum < services; snum++) {
354 struct tdb_print_db *pdb;
355 if (!lp_print_ok(snum))
357 pdb = get_print_db_byname(lp_const_servicename(snum));
359 tdb_traverse(pdb->tdb, unixjob_traverse_fn, &unix_jobid);
360 release_print_db(pdb);
361 if (sysjob_to_jobid_value != (uint32)-1)
362 return sysjob_to_jobid_value;
367 /****************************************************************************
368 Send notifications based on what has changed after a pjob_store.
369 ****************************************************************************/
373 uint32 spoolss_status;
374 } lpq_to_spoolss_status_map[] = {
375 { LPQ_QUEUED, JOB_STATUS_QUEUED },
376 { LPQ_PAUSED, JOB_STATUS_PAUSED },
377 { LPQ_SPOOLING, JOB_STATUS_SPOOLING },
378 { LPQ_PRINTING, JOB_STATUS_PRINTING },
379 { LPQ_DELETING, JOB_STATUS_DELETING },
380 { LPQ_OFFLINE, JOB_STATUS_OFFLINE },
381 { LPQ_PAPEROUT, JOB_STATUS_PAPEROUT },
382 { LPQ_PRINTED, JOB_STATUS_PRINTED },
383 { LPQ_DELETED, JOB_STATUS_DELETED },
384 { LPQ_BLOCKED, JOB_STATUS_BLOCKED },
385 { LPQ_USER_INTERVENTION, JOB_STATUS_USER_INTERVENTION },
389 /* Convert a lpq status value stored in printing.tdb into the
390 appropriate win32 API constant. */
392 static uint32 map_to_spoolss_status(uint32 lpq_status)
396 while (lpq_to_spoolss_status_map[i].lpq_status != -1) {
397 if (lpq_to_spoolss_status_map[i].lpq_status == lpq_status)
398 return lpq_to_spoolss_status_map[i].spoolss_status;
405 static void pjob_store_notify(int snum, uint32 jobid, struct printjob *old_data,
406 struct printjob *new_data)
408 BOOL new_job = False;
413 /* Notify the job name first */
415 if (new_job || !strequal(old_data->jobname, new_data->jobname))
416 notify_job_name(snum, jobid, new_data->jobname);
418 /* Job attributes that can't be changed. We only send
419 notification for these on a new job. */
422 notify_job_submitted(snum, jobid, new_data->starttime);
423 notify_job_username(snum, jobid, new_data->user);
426 /* Job attributes of a new job or attributes that can be
429 if (new_job || old_data->status != new_data->status)
430 notify_job_status(snum, jobid, map_to_spoolss_status(new_data->status));
432 if (new_job || old_data->size != new_data->size)
433 notify_job_total_bytes(snum, jobid, new_data->size);
435 if (new_job || old_data->page_count != new_data->page_count)
436 notify_job_total_pages(snum, jobid, new_data->page_count);
439 /****************************************************************************
440 Store a job structure back to the database.
441 ****************************************************************************/
443 static BOOL pjob_store(int snum, uint32 jobid, struct printjob *pjob)
445 TDB_DATA old_data, new_data;
447 struct tdb_print_db *pdb = get_print_db_byname(lp_const_servicename(snum));
449 int len, newlen, buflen;
457 old_data = tdb_fetch(pdb->tdb, print_key(jobid));
459 /* Doh! Now we have to pack/unpack data since the NT_DEVICEMODE was added */
466 len += tdb_pack(buf+len, buflen-len, "dddddddddffff",
481 len += pack_devicemode(pjob->nt_devmode, buf+len, buflen-len);
486 tb = (char *)Realloc(buf, len);
488 DEBUG(0,("pjob_store: failed to enlarge buffer!\n"));
495 } while ( buflen != len );
501 new_data.dsize = len;
502 ret = (tdb_store(pdb->tdb, print_key(jobid), new_data, TDB_REPLACE) == 0);
504 release_print_db(pdb);
506 /* Send notify updates for what has changed */
508 if ( ret && (old_data.dsize == 0 || old_data.dsize == sizeof(*pjob)) )
509 pjob_store_notify( snum, jobid, (struct printjob *)old_data.dptr, pjob );
512 SAFE_FREE( old_data.dptr );
518 /****************************************************************************
519 Remove a job structure from the database.
520 ****************************************************************************/
522 void pjob_delete(int snum, uint32 jobid)
524 struct printjob *pjob = print_job_find(snum, jobid);
525 uint32 job_status = 0;
526 struct tdb_print_db *pdb = get_print_db_byname(lp_const_servicename(snum));
532 DEBUG(5, ("pjob_delete(): we were asked to delete nonexistent job %u\n",
533 (unsigned int)jobid));
534 release_print_db(pdb);
538 /* Send a notification that a job has been deleted */
540 job_status = map_to_spoolss_status(pjob->status);
542 /* We must cycle through JOB_STATUS_DELETING and
543 JOB_STATUS_DELETED for the port monitor to delete the job
546 job_status |= JOB_STATUS_DELETING;
547 notify_job_status(snum, jobid, job_status);
549 job_status |= JOB_STATUS_DELETED;
550 notify_job_status(snum, jobid, job_status);
552 /* Remove from printing.tdb */
554 tdb_delete(pdb->tdb, print_key(jobid));
555 release_print_db(pdb);
556 rap_jobid_delete(snum, jobid);
559 /****************************************************************************
560 Parse a file name from the system spooler to generate a jobid.
561 ****************************************************************************/
563 static uint32 print_parse_jobid(char *fname)
567 if (strncmp(fname,PRINT_SPOOL_PREFIX,strlen(PRINT_SPOOL_PREFIX)) != 0)
569 fname += strlen(PRINT_SPOOL_PREFIX);
575 return (uint32)jobid;
578 /****************************************************************************
579 List a unix job in the print database.
580 ****************************************************************************/
582 static void print_unix_job(int snum, print_queue_struct *q)
584 uint32 jobid = q->job + UNIX_JOB_START;
585 struct printjob pj, *old_pj;
587 /* Preserve the timestamp on an existing unix print job */
589 old_pj = print_job_find(snum, jobid);
596 pj.starttime = old_pj ? old_pj->starttime : q->time;
597 pj.status = q->status;
601 fstrcpy(pj.filename, "");
602 fstrcpy(pj.jobname, q->fs_file);
603 fstrcpy(pj.user, q->fs_user);
604 fstrcpy(pj.queuename, lp_const_servicename(snum));
606 pjob_store(snum, jobid, &pj);
610 struct traverse_struct {
611 print_queue_struct *queue;
612 int qcount, snum, maxcount, total_jobs;
616 /****************************************************************************
617 Utility fn to delete any jobs that are no longer active.
618 ****************************************************************************/
620 static int traverse_fn_delete(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
622 struct traverse_struct *ts = (struct traverse_struct *)state;
623 struct printjob pjob;
627 if ( key.dsize != sizeof(jobid) )
630 memcpy(&jobid, key.dptr, sizeof(jobid));
631 if ( unpack_pjob( data.dptr, data.dsize, &pjob ) == -1 )
633 free_nt_devicemode( &pjob.nt_devmode );
636 if (ts->snum != lp_servicenumber(pjob.queuename)) {
637 /* this isn't for the queue we are looking at - this cannot happen with the split tdb's. JRA */
642 /* remove a unix job if it isn't in the system queue any more */
644 for (i=0;i<ts->qcount;i++) {
645 uint32 u_jobid = (ts->queue[i].job + UNIX_JOB_START);
646 if (jobid == u_jobid)
650 pjob_delete(ts->snum, jobid);
656 /* maybe it hasn't been spooled yet */
658 /* if a job is not spooled and the process doesn't
659 exist then kill it. This cleans up after smbd
661 if (!process_exists(pjob.pid))
662 pjob_delete(ts->snum, jobid);
668 for (i=0;i<ts->qcount;i++) {
669 uint32 curr_jobid = print_parse_jobid(ts->queue[i].fs_file);
670 if (jobid == curr_jobid)
674 /* The job isn't in the system queue - we have to assume it has
675 completed, so delete the database entry. */
677 if (i == ts->qcount) {
679 /* A race can occur between the time a job is spooled and
680 when it appears in the lpq output. This happens when
681 the job is added to printing.tdb when another smbd
682 running print_queue_update() has completed a lpq and
683 is currently traversing the printing tdb and deleting jobs.
684 Don't delete the job if it was submitted after the lpq_time. */
686 if (pjob.starttime < ts->lpq_time)
687 pjob_delete(ts->snum, jobid);
697 /****************************************************************************
698 Check if the print queue has been updated recently enough.
699 ****************************************************************************/
701 static void print_cache_flush(int snum)
704 const char *printername = lp_const_servicename(snum);
705 struct tdb_print_db *pdb = get_print_db_byname(printername);
709 slprintf(key, sizeof(key)-1, "CACHE/%s", printername);
710 tdb_store_int32(pdb->tdb, key, -1);
711 release_print_db(pdb);
714 /****************************************************************************
715 Check if someone already thinks they are doing the update.
716 ****************************************************************************/
718 static pid_t get_updating_pid(fstring printer_name)
723 struct tdb_print_db *pdb = get_print_db_byname(printer_name);
727 slprintf(keystr, sizeof(keystr)-1, "UPDATING/%s", printer_name);
729 key.dsize = strlen(keystr);
731 data = tdb_fetch(pdb->tdb, key);
732 release_print_db(pdb);
733 if (!data.dptr || data.dsize != sizeof(pid_t)) {
734 SAFE_FREE(data.dptr);
738 memcpy(&updating_pid, data.dptr, sizeof(pid_t));
739 SAFE_FREE(data.dptr);
741 if (process_exists(updating_pid))
747 /****************************************************************************
748 Set the fact that we're doing the update, or have finished doing the update
750 ****************************************************************************/
752 static void set_updating_pid(const fstring printer_name, BOOL delete)
757 pid_t updating_pid = sys_getpid();
758 struct tdb_print_db *pdb = get_print_db_byname(printer_name);
763 slprintf(keystr, sizeof(keystr)-1, "UPDATING/%s", printer_name);
765 key.dsize = strlen(keystr);
768 tdb_delete(pdb->tdb, key);
769 release_print_db(pdb);
773 data.dptr = (void *)&updating_pid;
774 data.dsize = sizeof(pid_t);
776 tdb_store(pdb->tdb, key, data, TDB_REPLACE);
777 release_print_db(pdb);
780 /****************************************************************************
781 Update the internal database from the system print queue for a queue.
782 ****************************************************************************/
784 static void print_queue_update(int snum)
787 print_queue_struct *queue = NULL;
788 print_status_struct status;
789 print_status_struct old_status;
790 struct printjob *pjob;
791 struct traverse_struct tstruct;
792 fstring keystr, printer_name, cachestr;
794 struct tdb_print_db *pdb;
796 fstrcpy(printer_name, lp_const_servicename(snum));
797 pdb = get_print_db_byname(printer_name);
802 * Check to see if someone else is doing this update.
803 * This is essentially a mutex on the update.
806 if (get_updating_pid(printer_name) != -1) {
807 release_print_db(pdb);
811 /* Lock the queue for the database update */
813 slprintf(keystr, sizeof(keystr) - 1, "LOCK/%s", printer_name);
814 /* Only wait 10 seconds for this. */
815 if (tdb_lock_bystring(pdb->tdb, keystr, 10) == -1) {
816 DEBUG(0,("print_queue_update: Failed to lock printer %s database\n", printer_name));
817 release_print_db(pdb);
822 * Ensure that no one else got in here.
823 * If the updating pid is still -1 then we are
827 if (get_updating_pid(printer_name) != -1) {
829 * Someone else is doing the update, exit.
831 tdb_unlock_bystring(pdb->tdb, keystr);
832 release_print_db(pdb);
837 * We're going to do the update ourselves.
840 /* Tell others we're doing the update. */
841 set_updating_pid(printer_name, False);
844 * Allow others to enter and notice we're doing
848 tdb_unlock_bystring(pdb->tdb, keystr);
851 * Update the cache time FIRST ! Stops others even
852 * attempting to get the lock and doing this
853 * if the lpq takes a long time.
856 slprintf(cachestr, sizeof(cachestr)-1, "CACHE/%s", printer_name);
857 tdb_store_int32(pdb->tdb, cachestr, (int)time(NULL));
859 /* get the current queue using the appropriate interface */
862 qcount = (*(current_printif->queue_get))(snum, &queue, &status);
864 DEBUG(3, ("%d job%s in queue for %s\n", qcount, (qcount != 1) ?
865 "s" : "", printer_name));
868 any job in the internal database that is marked as spooled
869 and doesn't exist in the system queue is considered finished
870 and removed from the database
872 any job in the system database but not in the internal database
873 is added as a unix job
875 fill in any system job numbers as we go
877 for (i=0; i<qcount; i++) {
878 uint32 jobid = print_parse_jobid(queue[i].fs_file);
880 if (jobid == (uint32)-1) {
881 /* assume its a unix print job */
882 print_unix_job(snum, &queue[i]);
886 /* we have an active SMB print job - update its status */
887 pjob = print_job_find(snum, jobid);
889 /* err, somethings wrong. Probably smbd was restarted
890 with jobs in the queue. All we can do is treat them
891 like unix jobs. Pity. */
892 print_unix_job(snum, &queue[i]);
896 pjob->sysjob = queue[i].job;
897 pjob->status = queue[i].status;
899 pjob_store(snum, jobid, pjob);
902 /* now delete any queued entries that don't appear in the
904 tstruct.queue = queue;
905 tstruct.qcount = qcount;
907 tstruct.total_jobs = 0;
908 tstruct.lpq_time = time(NULL);
910 tdb_traverse(pdb->tdb, traverse_fn_delete, (void *)&tstruct);
912 SAFE_FREE(tstruct.queue);
914 tdb_store_int32(pdb->tdb, "INFO/total_jobs", tstruct.total_jobs);
916 if( qcount != get_queue_status(snum, &old_status))
917 DEBUG(10,("print_queue_update: queue status change %d jobs -> %d jobs for printer %s\n",
918 old_status.qcount, qcount, printer_name ));
920 /* store the new queue status structure */
921 slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", printer_name);
923 key.dsize = strlen(keystr);
925 status.qcount = qcount;
926 data.dptr = (void *)&status;
927 data.dsize = sizeof(status);
928 tdb_store(pdb->tdb, key, data, TDB_REPLACE);
931 * Update the cache time again. We want to do this call
932 * as little as possible...
935 slprintf(keystr, sizeof(keystr)-1, "CACHE/%s", printer_name);
936 tdb_store_int32(pdb->tdb, keystr, (int32)time(NULL));
938 /* Delete our pid from the db. */
939 set_updating_pid(printer_name, True);
940 release_print_db(pdb);
943 /****************************************************************************
944 Create/Update an entry in the print tdb that will allow us to send notify
945 updates only to interested smbd's.
946 ****************************************************************************/
948 BOOL print_notify_register_pid(int snum)
951 struct tdb_print_db *pdb = NULL;
952 TDB_CONTEXT *tdb = NULL;
953 const char *printername;
954 uint32 mypid = (uint32)sys_getpid();
958 /* if (snum == -1), then the change notify request was
959 on a print server handle and we need to register on
964 int num_services = lp_numservices();
967 for ( idx=0; idx<num_services; idx++ ) {
968 if (lp_snum_ok(idx) && lp_print_ok(idx) )
969 print_notify_register_pid(idx);
974 else /* register for a specific printer */
976 printername = lp_const_servicename(snum);
977 pdb = get_print_db_byname(printername);
983 if (tdb_lock_bystring(tdb, NOTIFY_PID_LIST_KEY, 10) == -1) {
984 DEBUG(0,("print_notify_register_pid: Failed to lock printer %s\n",
987 release_print_db(pdb);
991 data = get_printer_notify_pid_list( tdb, printername, True );
993 /* Add ourselves and increase the refcount. */
995 for (i = 0; i < data.dsize; i += 8) {
996 if (IVAL(data.dptr,i) == mypid) {
997 uint32 new_refcount = IVAL(data.dptr, i+4) + 1;
998 SIVAL(data.dptr, i+4, new_refcount);
1003 if (i == data.dsize) {
1004 /* We weren't in the list. Realloc. */
1005 data.dptr = Realloc(data.dptr, data.dsize + 8);
1007 DEBUG(0,("print_notify_register_pid: Relloc fail for printer %s\n",
1012 SIVAL(data.dptr,data.dsize - 8,mypid);
1013 SIVAL(data.dptr,data.dsize - 4,1); /* Refcount. */
1016 /* Store back the record. */
1017 if (tdb_store_by_string(tdb, NOTIFY_PID_LIST_KEY, data, TDB_REPLACE) == -1) {
1018 DEBUG(0,("print_notify_register_pid: Failed to update pid \
1019 list for printer %s\n", printername));
1027 tdb_unlock_bystring(tdb, NOTIFY_PID_LIST_KEY);
1029 release_print_db(pdb);
1030 SAFE_FREE(data.dptr);
1034 /****************************************************************************
1035 Update an entry in the print tdb that will allow us to send notify
1036 updates only to interested smbd's.
1037 ****************************************************************************/
1039 BOOL print_notify_deregister_pid(int snum)
1042 struct tdb_print_db *pdb = NULL;
1043 TDB_CONTEXT *tdb = NULL;
1044 const char *printername;
1045 uint32 mypid = (uint32)sys_getpid();
1049 /* if ( snum == -1 ), we are deregister a print server handle
1050 which means to deregister on all print queues */
1054 int num_services = lp_numservices();
1057 for ( idx=0; idx<num_services; idx++ ) {
1058 if ( lp_snum_ok(idx) && lp_print_ok(idx) )
1059 print_notify_deregister_pid(idx);
1064 else /* deregister a specific printer */
1066 printername = lp_const_servicename(snum);
1067 pdb = get_print_db_byname(printername);
1073 if (tdb_lock_bystring(tdb, NOTIFY_PID_LIST_KEY, 10) == -1) {
1074 DEBUG(0,("print_notify_register_pid: Failed to lock \
1075 printer %s database\n", printername));
1077 release_print_db(pdb);
1081 data = get_printer_notify_pid_list( tdb, printername, True );
1083 /* Reduce refcount. Remove ourselves if zero. */
1085 for (i = 0; i < data.dsize; ) {
1086 if (IVAL(data.dptr,i) == mypid) {
1087 uint32 refcount = IVAL(data.dptr, i+4);
1091 if (refcount == 0) {
1092 if (data.dsize - i > 8)
1093 memmove( &data.dptr[i], &data.dptr[i+8], data.dsize - i - 8);
1097 SIVAL(data.dptr, i+4, refcount);
1103 if (data.dsize == 0)
1104 SAFE_FREE(data.dptr);
1106 /* Store back the record. */
1107 if (tdb_store_by_string(tdb, NOTIFY_PID_LIST_KEY, data, TDB_REPLACE) == -1) {
1108 DEBUG(0,("print_notify_register_pid: Failed to update pid \
1109 list for printer %s\n", printername));
1117 tdb_unlock_bystring(tdb, NOTIFY_PID_LIST_KEY);
1119 release_print_db(pdb);
1120 SAFE_FREE(data.dptr);
1124 /****************************************************************************
1125 Check if a jobid is valid. It is valid if it exists in the database.
1126 ****************************************************************************/
1128 BOOL print_job_exists(int snum, uint32 jobid)
1130 struct tdb_print_db *pdb = get_print_db_byname(lp_const_servicename(snum));
1135 ret = tdb_exists(pdb->tdb, print_key(jobid));
1136 release_print_db(pdb);
1140 /****************************************************************************
1141 Give the fd used for a jobid.
1142 ****************************************************************************/
1144 int print_job_fd(int snum, uint32 jobid)
1146 struct printjob *pjob = print_job_find(snum, jobid);
1149 /* don't allow another process to get this info - it is meaningless */
1150 if (pjob->pid != local_pid)
1155 /****************************************************************************
1156 Give the filename used for a jobid.
1157 Only valid for the process doing the spooling and when the job
1158 has not been spooled.
1159 ****************************************************************************/
1161 char *print_job_fname(int snum, uint32 jobid)
1163 struct printjob *pjob = print_job_find(snum, jobid);
1164 if (!pjob || pjob->spooled || pjob->pid != local_pid)
1166 return pjob->filename;
1170 /****************************************************************************
1171 Give the filename used for a jobid.
1172 Only valid for the process doing the spooling and when the job
1173 has not been spooled.
1174 ****************************************************************************/
1176 NT_DEVICEMODE *print_job_devmode(int snum, uint32 jobid)
1178 struct printjob *pjob = print_job_find(snum, jobid);
1183 return pjob->nt_devmode;
1186 /****************************************************************************
1187 Set the place in the queue for a job.
1188 ****************************************************************************/
1190 BOOL print_job_set_place(int snum, uint32 jobid, int place)
1192 DEBUG(2,("print_job_set_place not implemented yet\n"));
1196 /****************************************************************************
1197 Set the name of a job. Only possible for owner.
1198 ****************************************************************************/
1200 BOOL print_job_set_name(int snum, uint32 jobid, char *name)
1202 struct printjob *pjob = print_job_find(snum, jobid);
1203 if (!pjob || pjob->pid != local_pid)
1206 fstrcpy(pjob->jobname, name);
1207 return pjob_store(snum, jobid, pjob);
1210 /****************************************************************************
1211 Delete a print job - don't update queue.
1212 ****************************************************************************/
1214 static BOOL print_job_delete1(int snum, uint32 jobid)
1216 struct printjob *pjob = print_job_find(snum, jobid);
1223 * If already deleting just return.
1226 if (pjob->status == LPQ_DELETING)
1229 /* Hrm - we need to be able to cope with deleting a job before it
1230 has reached the spooler. */
1232 if (pjob->sysjob == -1) {
1233 DEBUG(5, ("attempt to delete job %u not seen by lpr\n", (unsigned int)jobid));
1236 /* Set the tdb entry to be deleting. */
1238 pjob->status = LPQ_DELETING;
1239 pjob_store(snum, jobid, pjob);
1241 if (pjob->spooled && pjob->sysjob != -1)
1242 result = (*(current_printif->job_delete))(snum, pjob);
1244 /* Delete the tdb entry if the delete suceeded or the job hasn't
1248 pjob_delete(snum, jobid);
1250 return (result == 0);
1253 /****************************************************************************
1254 Return true if the current user owns the print job.
1255 ****************************************************************************/
1257 static BOOL is_owner(struct current_user *user, int snum, uint32 jobid)
1259 struct printjob *pjob = print_job_find(snum, jobid);
1265 if ((vuser = get_valid_user_struct(user->vuid)) != NULL) {
1266 return strequal(pjob->user, vuser->user.smb_name);
1268 return strequal(pjob->user, uidtoname(user->uid));
1272 /****************************************************************************
1274 ****************************************************************************/
1276 BOOL print_job_delete(struct current_user *user, int snum, uint32 jobid, WERROR *errcode)
1278 BOOL owner, deleted;
1283 owner = is_owner(user, snum, jobid);
1285 /* Check access against security descriptor or whether the user
1289 !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
1290 DEBUG(3, ("delete denied by security descriptor\n"));
1291 *errcode = WERR_ACCESS_DENIED;
1293 /* BEGIN_ADMIN_LOG */
1294 sys_adminlog( LOG_ERR,
1295 "Permission denied-- user not allowed to delete, \
1296 pause, or resume print job. User name: %s. Printer name: %s.",
1297 uidtoname(user->uid), PRINTERNAME(snum) );
1304 * get the spooled filename of the print job
1305 * if this works, then the file has not been spooled
1306 * to the underlying print system. Just delete the
1307 * spool file & return.
1310 if ( (fname = print_job_fname( snum, jobid )) != NULL )
1312 /* remove the spool file */
1313 DEBUG(10,("print_job_delete: Removing spool file [%s]\n", fname ));
1314 if ( unlink( fname ) == -1 ) {
1315 *errcode = map_werror_from_unix(errno);
1322 if (!print_job_delete1(snum, jobid)) {
1323 *errcode = WERR_ACCESS_DENIED;
1327 /* force update the database and say the delete failed if the
1330 print_queue_update(snum);
1332 deleted = !print_job_exists(snum, jobid);
1334 *errcode = WERR_ACCESS_DENIED;
1339 /****************************************************************************
1341 ****************************************************************************/
1343 BOOL print_job_pause(struct current_user *user, int snum, uint32 jobid, WERROR *errcode)
1345 struct printjob *pjob = print_job_find(snum, jobid);
1351 if (!pjob->spooled || pjob->sysjob == -1)
1354 if (!is_owner(user, snum, jobid) &&
1355 !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
1356 DEBUG(3, ("pause denied by security descriptor\n"));
1358 /* BEGIN_ADMIN_LOG */
1359 sys_adminlog( LOG_ERR,
1360 "Permission denied-- user not allowed to delete, \
1361 pause, or resume print job. User name: %s. Printer name: %s.",
1362 uidtoname(user->uid), PRINTERNAME(snum) );
1365 *errcode = WERR_ACCESS_DENIED;
1369 /* need to pause the spooled entry */
1370 ret = (*(current_printif->job_pause))(snum, pjob);
1373 *errcode = WERR_INVALID_PARAM;
1377 /* force update the database */
1378 print_cache_flush(snum);
1380 /* Send a printer notify message */
1382 notify_job_status(snum, jobid, JOB_STATUS_PAUSED);
1384 /* how do we tell if this succeeded? */
1389 /****************************************************************************
1391 ****************************************************************************/
1393 BOOL print_job_resume(struct current_user *user, int snum, uint32 jobid, WERROR *errcode)
1395 struct printjob *pjob = print_job_find(snum, jobid);
1401 if (!pjob->spooled || pjob->sysjob == -1)
1404 if (!is_owner(user, snum, jobid) &&
1405 !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
1406 DEBUG(3, ("resume denied by security descriptor\n"));
1407 *errcode = WERR_ACCESS_DENIED;
1409 /* BEGIN_ADMIN_LOG */
1410 sys_adminlog( LOG_ERR,
1411 "Permission denied-- user not allowed to delete, \
1412 pause, or resume print job. User name: %s. Printer name: %s.",
1413 uidtoname(user->uid), PRINTERNAME(snum) );
1418 ret = (*(current_printif->job_resume))(snum, pjob);
1421 *errcode = WERR_INVALID_PARAM;
1425 /* force update the database */
1426 print_cache_flush(snum);
1428 /* Send a printer notify message */
1430 notify_job_status(snum, jobid, JOB_STATUS_QUEUED);
1435 /****************************************************************************
1436 Write to a print file.
1437 ****************************************************************************/
1439 int print_job_write(int snum, uint32 jobid, const char *buf, int size)
1442 struct printjob *pjob = print_job_find(snum, jobid);
1446 /* don't allow another process to get this info - it is meaningless */
1447 if (pjob->pid != local_pid)
1450 return_code = write(pjob->fd, buf, size);
1451 if (return_code>0) {
1453 pjob_store(snum, jobid, pjob);
1458 /****************************************************************************
1459 Check if the print queue has been updated recently enough.
1460 ****************************************************************************/
1462 static BOOL print_cache_expired(int snum)
1465 time_t last_qscan_time, time_now = time(NULL);
1466 const char *printername = lp_const_servicename(snum);
1467 struct tdb_print_db *pdb = get_print_db_byname(printername);
1472 slprintf(key, sizeof(key), "CACHE/%s", printername);
1473 last_qscan_time = (time_t)tdb_fetch_int32(pdb->tdb, key);
1476 * Invalidate the queue for 3 reasons.
1477 * (1). last queue scan time == -1.
1478 * (2). Current time - last queue scan time > allowed cache time.
1479 * (3). last queue scan time > current time + MAX_CACHE_VALID_TIME (1 hour by default).
1480 * This last test picks up machines for which the clock has been moved
1481 * forward, an lpq scan done and then the clock moved back. Otherwise
1482 * that last lpq scan would stay around for a loooong loooong time... :-). JRA.
1485 if (last_qscan_time == ((time_t)-1) || (time_now - last_qscan_time) >= lp_lpqcachetime() ||
1486 last_qscan_time > (time_now + MAX_CACHE_VALID_TIME)) {
1487 DEBUG(3, ("print cache expired for queue %s \
1488 (last_qscan_time = %d, time now = %d, qcachetime = %d)\n", printername,
1489 (int)last_qscan_time, (int)time_now, (int)lp_lpqcachetime() ));
1490 release_print_db(pdb);
1493 release_print_db(pdb);
1497 /****************************************************************************
1498 Get the queue status - do not update if db is out of date.
1499 ****************************************************************************/
1501 static int get_queue_status(int snum, print_status_struct *status)
1505 const char *printername = lp_const_servicename(snum);
1506 struct tdb_print_db *pdb = get_print_db_byname(printername);
1510 ZERO_STRUCTP(status);
1511 slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", printername);
1513 key.dsize = strlen(keystr);
1514 data = tdb_fetch(pdb->tdb, key);
1515 release_print_db(pdb);
1517 if (data.dsize == sizeof(print_status_struct))
1518 memcpy(status, data.dptr, sizeof(print_status_struct));
1519 SAFE_FREE(data.dptr);
1521 return status->qcount;
1524 /****************************************************************************
1525 Determine the number of jobs in a queue.
1526 ****************************************************************************/
1528 int print_queue_length(int snum, print_status_struct *pstatus)
1530 print_status_struct status;
1533 /* make sure the database is up to date */
1534 if (print_cache_expired(snum))
1535 print_queue_update(snum);
1537 /* also fetch the queue status */
1538 memset(&status, 0, sizeof(status));
1539 len = get_queue_status(snum, &status);
1545 /***************************************************************************
1546 Start spooling a job - return the jobid.
1547 ***************************************************************************/
1549 uint32 print_job_start(struct current_user *user, int snum, char *jobname, NT_DEVICEMODE *nt_devmode )
1553 struct printjob pjob;
1557 const char *printername = lp_const_servicename(snum);
1558 struct tdb_print_db *pdb = get_print_db_byname(printername);
1559 BOOL pdb_locked = False;
1566 if (!print_access_check(user, snum, PRINTER_ACCESS_USE)) {
1567 DEBUG(3, ("print_job_start: job start denied by security descriptor\n"));
1568 release_print_db(pdb);
1572 if (!print_time_access_check(snum)) {
1573 DEBUG(3, ("print_job_start: job start denied by time check\n"));
1574 release_print_db(pdb);
1578 path = lp_pathname(snum);
1580 /* see if we have sufficient disk space */
1581 if (lp_minprintspace(snum)) {
1582 SMB_BIG_UINT dspace, dsize;
1583 if (sys_fsusage(path, &dspace, &dsize) == 0 &&
1584 dspace < 2*(SMB_BIG_UINT)lp_minprintspace(snum)) {
1585 DEBUG(3, ("print_job_start: disk space check failed.\n"));
1586 release_print_db(pdb);
1592 /* for autoloaded printers, check that the printcap entry still exists */
1593 if (lp_autoloaded(snum) && !pcap_printername_ok(lp_const_servicename(snum), NULL)) {
1594 DEBUG(3, ("print_job_start: printer name %s check failed.\n", lp_const_servicename(snum) ));
1595 release_print_db(pdb);
1600 /* Insure the maximum queue size is not violated */
1601 if ((njobs = print_queue_length(snum,NULL)) > lp_maxprintjobs(snum)) {
1602 DEBUG(3, ("print_job_start: number of jobs (%d) larger than max printjobs per queue (%d).\n",
1603 njobs, lp_maxprintjobs(snum) ));
1604 release_print_db(pdb);
1609 /* Lock the database - only wait 20 seconds. */
1610 if (tdb_lock_bystring(pdb->tdb, "INFO/nextjob", 20) == -1) {
1611 DEBUG(0,("print_job_start: failed to lock printing database %s\n", printername ));
1612 release_print_db(pdb);
1618 next_jobid = tdb_fetch_int32(pdb->tdb, "INFO/nextjob");
1619 if (next_jobid == -1)
1622 for (jobid = NEXT_JOBID(next_jobid); jobid != next_jobid; jobid = NEXT_JOBID(jobid)) {
1623 if (!print_job_exists(snum, jobid))
1627 if (jobid == next_jobid) {
1628 DEBUG(3, ("print_job_start: jobid (%d)==next_jobid(%d).\n",
1629 jobid, next_jobid ));
1634 /* Store a dummy placeholder. This must be quick as we have the lock. */
1639 if (tdb_store(pdb->tdb, print_key(jobid), dum, TDB_INSERT) == -1) {
1640 DEBUG(3, ("print_job_start: jobid (%d) failed to store placeholder.\n",
1647 if (tdb_store_int32(pdb->tdb, "INFO/nextjob", jobid)==-1) {
1648 DEBUG(3, ("print_job_start: failed to store INFO/nextjob.\n"));
1653 /* We've finished with the INFO/nextjob lock. */
1654 tdb_unlock_bystring(pdb->tdb, "INFO/nextjob");
1657 /* create the database entry */
1661 pjob.pid = local_pid;
1664 pjob.starttime = time(NULL);
1665 pjob.status = LPQ_SPOOLING;
1667 pjob.spooled = False;
1669 pjob.nt_devmode = nt_devmode;
1671 fstrcpy(pjob.jobname, jobname);
1673 if ((vuser = get_valid_user_struct(user->vuid)) != NULL) {
1674 fstrcpy(pjob.user, vuser->user.smb_name);
1676 fstrcpy(pjob.user, uidtoname(user->uid));
1679 fstrcpy(pjob.queuename, lp_const_servicename(snum));
1681 /* we have a job entry - now create the spool file */
1682 slprintf(pjob.filename, sizeof(pjob.filename)-1, "%s/%s%.8u.XXXXXX",
1683 path, PRINT_SPOOL_PREFIX, (unsigned int)jobid);
1684 pjob.fd = smb_mkstemp(pjob.filename);
1686 if (pjob.fd == -1) {
1687 if (errno == EACCES) {
1688 /* Common setup error, force a report. */
1689 DEBUG(0, ("print_job_start: insufficient permissions \
1690 to open spool file %s.\n", pjob.filename));
1692 /* Normal case, report at level 3 and above. */
1693 DEBUG(3, ("print_job_start: can't open spool file %s,\n", pjob.filename));
1694 DEBUGADD(3, ("errno = %d (%s).\n", errno, strerror(errno)));
1699 pjob_store(snum, jobid, &pjob);
1701 release_print_db(pdb);
1707 pjob_delete(snum, jobid);
1710 tdb_unlock_bystring(pdb->tdb, "INFO/nextjob");
1711 release_print_db(pdb);
1713 DEBUG(3, ("print_job_start: returning fail. Error = %s\n", strerror(errno) ));
1717 /****************************************************************************
1718 Update the number of pages spooled to jobid
1719 ****************************************************************************/
1721 void print_job_endpage(int snum, uint32 jobid)
1723 struct printjob *pjob = print_job_find(snum, jobid);
1726 /* don't allow another process to get this info - it is meaningless */
1727 if (pjob->pid != local_pid)
1731 pjob_store(snum, jobid, pjob);
1734 /****************************************************************************
1735 Print a file - called on closing the file. This spools the job.
1736 If normal close is false then we're tearing down the jobs - treat as an
1738 ****************************************************************************/
1740 BOOL print_job_end(int snum, uint32 jobid, BOOL normal_close)
1742 struct printjob *pjob = print_job_find(snum, jobid);
1744 SMB_STRUCT_STAT sbuf;
1749 if (pjob->spooled || pjob->pid != local_pid)
1752 if (normal_close && (sys_fstat(pjob->fd, &sbuf) == 0)) {
1753 pjob->size = sbuf.st_size;
1759 * Not a normal close or we couldn't stat the job file,
1760 * so something has gone wrong. Cleanup.
1764 DEBUG(3,("print_job_end: failed to stat file for jobid %d\n", jobid ));
1768 /* Technically, this is not quite right. If the printer has a separator
1769 * page turned on, the NT spooler prints the separator page even if the
1770 * print job is 0 bytes. 010215 JRR */
1771 if (pjob->size == 0 || pjob->status == LPQ_DELETING) {
1772 /* don't bother spooling empty files or something being deleted. */
1773 DEBUG(5,("print_job_end: canceling spool of %s (%s)\n",
1774 pjob->filename, pjob->size ? "deleted" : "zero length" ));
1775 unlink(pjob->filename);
1776 pjob_delete(snum, jobid);
1780 ret = (*(current_printif->job_submit))(snum, pjob);
1785 /* The print job has been sucessfully handed over to the back-end */
1787 pjob->spooled = True;
1788 pjob->status = LPQ_QUEUED;
1789 pjob_store(snum, jobid, pjob);
1791 /* make sure the database is up to date */
1792 if (print_cache_expired(snum))
1793 print_queue_update(snum);
1799 /* The print job was not succesfully started. Cleanup */
1800 /* Still need to add proper error return propagation! 010122:JRR */
1801 unlink(pjob->filename);
1802 pjob_delete(snum, jobid);
1806 /****************************************************************************
1807 Utility fn to enumerate the print queue.
1808 ****************************************************************************/
1810 static int traverse_fn_queue(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
1812 struct traverse_struct *ts = (struct traverse_struct *)state;
1813 struct printjob pjob;
1819 if ( key.dsize != sizeof(jobid) )
1822 memcpy(&jobid, key.dptr, sizeof(jobid));
1824 if ( unpack_pjob( data.dptr, data.dsize, &pjob ) == -1 )
1826 free_nt_devicemode( &pjob.nt_devmode );
1828 /* maybe it isn't for this queue */
1829 if (ts->snum != lp_servicenumber(pjob.queuename))
1832 if (ts->qcount >= ts->maxcount)
1837 ts->queue[i].job = jobid;
1838 ts->queue[i].size = pjob.size;
1839 ts->queue[i].page_count = pjob.page_count;
1840 ts->queue[i].status = pjob.status;
1841 ts->queue[i].priority = 1;
1842 ts->queue[i].time = pjob.starttime;
1843 fstrcpy(ts->queue[i].fs_user, pjob.user);
1844 fstrcpy(ts->queue[i].fs_file, pjob.jobname);
1851 struct traverse_count_struct {
1855 /****************************************************************************
1856 Utility fn to count the number of entries in the print queue.
1857 ****************************************************************************/
1859 static int traverse_count_fn_queue(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
1861 struct traverse_count_struct *ts = (struct traverse_count_struct *)state;
1862 struct printjob pjob;
1867 if ( key.dsize != sizeof(jobid) )
1870 memcpy(&jobid, key.dptr, sizeof(jobid));
1872 if ( unpack_pjob( data.dptr, data.dsize, &pjob ) == -1 )
1875 free_nt_devicemode( &pjob.nt_devmode );
1877 /* maybe it isn't for this queue - this cannot happen with the tdb/printer code. JRA */
1878 if (ts->snum != lp_servicenumber(pjob.queuename))
1886 /****************************************************************************
1887 Sort print jobs by submittal time.
1888 ****************************************************************************/
1890 static int printjob_comp(print_queue_struct *j1, print_queue_struct *j2)
1901 /* Sort on job start time */
1903 if (j1->time == j2->time)
1905 return (j1->time > j2->time) ? 1 : -1;
1908 /****************************************************************************
1909 Get a printer queue listing.
1910 set queue = NULL and status = NULL if you just want to update the cache
1911 ****************************************************************************/
1913 int print_queue_status(int snum,
1914 print_queue_struct **queue,
1915 print_status_struct *status)
1917 struct traverse_struct tstruct;
1918 struct traverse_count_struct tsc;
1921 const char *printername;
1922 struct tdb_print_db *pdb;
1924 /* make sure the database is up to date */
1926 if (print_cache_expired(snum))
1927 print_queue_update(snum);
1929 /* return if we are done */
1931 if ( !queue || !status )
1935 printername = lp_const_servicename(snum);
1936 pdb = get_print_db_byname(printername);
1942 * Fetch the queue status. We must do this first, as there may
1943 * be no jobs in the queue.
1945 ZERO_STRUCTP(status);
1946 slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", printername);
1948 key.dsize = strlen(keystr);
1949 data = tdb_fetch(pdb->tdb, key);
1951 if (data.dsize == sizeof(*status)) {
1952 memcpy(status, data.dptr, sizeof(*status));
1954 SAFE_FREE(data.dptr);
1958 * Now, fetch the print queue information. We first count the number
1959 * of entries, and then only retrieve the queue if necessary.
1964 tdb_traverse(pdb->tdb, traverse_count_fn_queue, (void *)&tsc);
1966 if (tsc.count == 0) {
1967 release_print_db(pdb);
1971 /* Allocate the queue size. */
1972 if ((tstruct.queue = (print_queue_struct *)
1973 malloc(sizeof(print_queue_struct)*tsc.count)) == NULL) {
1974 release_print_db(pdb);
1979 * Fill in the queue.
1980 * We need maxcount as the queue size may have changed between
1981 * the two calls to tdb_traverse.
1984 tstruct.maxcount = tsc.count;
1985 tstruct.snum = snum;
1987 tdb_traverse(pdb->tdb, traverse_fn_queue, (void *)&tstruct);
1988 release_print_db(pdb);
1990 /* Sort the queue by submission time otherwise they are displayed
1993 qsort(tstruct.queue, tstruct.qcount, sizeof(print_queue_struct),
1994 QSORT_CAST(printjob_comp));
1996 *queue = tstruct.queue;
1997 return tstruct.qcount;
2000 /****************************************************************************
2002 ****************************************************************************/
2004 BOOL print_queue_pause(struct current_user *user, int snum, WERROR *errcode)
2008 if (!print_access_check(user, snum, PRINTER_ACCESS_ADMINISTER)) {
2009 *errcode = WERR_ACCESS_DENIED;
2013 ret = (*(current_printif->queue_pause))(snum);
2016 *errcode = WERR_INVALID_PARAM;
2020 /* force update the database */
2021 print_cache_flush(snum);
2023 /* Send a printer notify message */
2025 notify_printer_status(snum, PRINTER_STATUS_PAUSED);
2030 /****************************************************************************
2032 ****************************************************************************/
2034 BOOL print_queue_resume(struct current_user *user, int snum, WERROR *errcode)
2038 if (!print_access_check(user, snum, PRINTER_ACCESS_ADMINISTER)) {
2039 *errcode = WERR_ACCESS_DENIED;
2043 ret = (*(current_printif->queue_resume))(snum);
2046 *errcode = WERR_INVALID_PARAM;
2050 /* make sure the database is up to date */
2051 if (print_cache_expired(snum))
2052 print_queue_update(snum);
2054 /* Send a printer notify message */
2056 notify_printer_status(snum, PRINTER_STATUS_OK);
2061 /****************************************************************************
2062 Purge a queue - implemented by deleting all jobs that we can delete.
2063 ****************************************************************************/
2065 BOOL print_queue_purge(struct current_user *user, int snum, WERROR *errcode)
2067 print_queue_struct *queue;
2068 print_status_struct status;
2072 /* Force and update so the count is accurate (i.e. not a cached count) */
2073 print_queue_update(snum);
2075 can_job_admin = print_access_check(user, snum, JOB_ACCESS_ADMINISTER);
2076 njobs = print_queue_status(snum, &queue, &status);
2078 for (i=0;i<njobs;i++) {
2079 BOOL owner = is_owner(user, snum, queue[i].job);
2081 if (owner || can_job_admin) {
2082 print_job_delete1(snum, queue[i].job);