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, uint32 jobid)
584 struct printjob pj, *old_pj;
586 if (jobid == (uint32)-1)
587 jobid = q->job + UNIX_JOB_START;
589 /* Preserve the timestamp on an existing unix print job */
591 old_pj = print_job_find(snum, jobid);
598 pj.starttime = old_pj ? old_pj->starttime : q->time;
599 pj.status = q->status;
602 pj.smbjob = (old_pj != NULL ? True : False);
603 fstrcpy(pj.filename, old_pj ? old_pj->filename : "");
604 if (jobid < UNIX_JOB_START)
605 fstrcpy(pj.jobname, old_pj ? old_pj->jobname : "Remote Downlevel Document");
607 fstrcpy(pj.jobname, old_pj ? old_pj->jobname : q->fs_file);
608 fstrcpy(pj.user, old_pj ? old_pj->user : q->fs_user);
609 fstrcpy(pj.queuename, old_pj ? old_pj->queuename : lp_const_servicename(snum));
611 pjob_store(snum, jobid, &pj);
615 struct traverse_struct {
616 print_queue_struct *queue;
617 int qcount, snum, maxcount, total_jobs;
621 /****************************************************************************
622 Utility fn to delete any jobs that are no longer active.
623 ****************************************************************************/
625 static int traverse_fn_delete(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
627 struct traverse_struct *ts = (struct traverse_struct *)state;
628 struct printjob pjob;
632 if ( key.dsize != sizeof(jobid) )
635 memcpy(&jobid, key.dptr, sizeof(jobid));
636 if ( unpack_pjob( data.dptr, data.dsize, &pjob ) == -1 )
638 free_nt_devicemode( &pjob.nt_devmode );
641 if (ts->snum != lp_servicenumber(pjob.queuename)) {
642 /* this isn't for the queue we are looking at - this cannot happen with the split tdb's. JRA */
647 /* remove a unix job if it isn't in the system queue any more */
649 for (i=0;i<ts->qcount;i++) {
650 uint32 u_jobid = (ts->queue[i].job + UNIX_JOB_START);
651 if (jobid == u_jobid)
655 pjob_delete(ts->snum, jobid);
661 /* maybe it hasn't been spooled yet */
663 /* if a job is not spooled and the process doesn't
664 exist then kill it. This cleans up after smbd
666 if (!process_exists(pjob.pid))
667 pjob_delete(ts->snum, jobid);
673 for (i=0;i<ts->qcount;i++) {
674 uint32 curr_jobid = print_parse_jobid(ts->queue[i].fs_file);
675 if (jobid == curr_jobid)
679 /* The job isn't in the system queue - we have to assume it has
680 completed, so delete the database entry. */
682 if (i == ts->qcount) {
684 /* A race can occur between the time a job is spooled and
685 when it appears in the lpq output. This happens when
686 the job is added to printing.tdb when another smbd
687 running print_queue_update() has completed a lpq and
688 is currently traversing the printing tdb and deleting jobs.
689 Don't delete the job if it was submitted after the lpq_time. */
691 if (pjob.starttime < ts->lpq_time)
692 pjob_delete(ts->snum, jobid);
702 /****************************************************************************
703 Check if the print queue has been updated recently enough.
704 ****************************************************************************/
706 static void print_cache_flush(int snum)
709 const char *printername = lp_const_servicename(snum);
710 struct tdb_print_db *pdb = get_print_db_byname(printername);
714 slprintf(key, sizeof(key)-1, "CACHE/%s", printername);
715 tdb_store_int32(pdb->tdb, key, -1);
716 release_print_db(pdb);
719 /****************************************************************************
720 Check if someone already thinks they are doing the update.
721 ****************************************************************************/
723 static pid_t get_updating_pid(fstring printer_name)
728 struct tdb_print_db *pdb = get_print_db_byname(printer_name);
732 slprintf(keystr, sizeof(keystr)-1, "UPDATING/%s", printer_name);
734 key.dsize = strlen(keystr);
736 data = tdb_fetch(pdb->tdb, key);
737 release_print_db(pdb);
738 if (!data.dptr || data.dsize != sizeof(pid_t)) {
739 SAFE_FREE(data.dptr);
743 memcpy(&updating_pid, data.dptr, sizeof(pid_t));
744 SAFE_FREE(data.dptr);
746 if (process_exists(updating_pid))
752 /****************************************************************************
753 Set the fact that we're doing the update, or have finished doing the update
755 ****************************************************************************/
757 static void set_updating_pid(const fstring printer_name, BOOL delete)
762 pid_t updating_pid = sys_getpid();
763 struct tdb_print_db *pdb = get_print_db_byname(printer_name);
768 slprintf(keystr, sizeof(keystr)-1, "UPDATING/%s", printer_name);
770 key.dsize = strlen(keystr);
773 tdb_delete(pdb->tdb, key);
774 release_print_db(pdb);
778 data.dptr = (void *)&updating_pid;
779 data.dsize = sizeof(pid_t);
781 tdb_store(pdb->tdb, key, data, TDB_REPLACE);
782 release_print_db(pdb);
785 /****************************************************************************
786 Update the internal database from the system print queue for a queue.
787 ****************************************************************************/
789 static void print_queue_update(int snum)
792 print_queue_struct *queue = NULL;
793 print_status_struct status;
794 print_status_struct old_status;
795 struct printjob *pjob;
796 struct traverse_struct tstruct;
797 fstring keystr, printer_name, cachestr;
799 struct tdb_print_db *pdb;
801 fstrcpy(printer_name, lp_const_servicename(snum));
802 pdb = get_print_db_byname(printer_name);
807 * Check to see if someone else is doing this update.
808 * This is essentially a mutex on the update.
811 if (get_updating_pid(printer_name) != -1) {
812 release_print_db(pdb);
816 /* Lock the queue for the database update */
818 slprintf(keystr, sizeof(keystr) - 1, "LOCK/%s", printer_name);
819 /* Only wait 10 seconds for this. */
820 if (tdb_lock_bystring(pdb->tdb, keystr, 10) == -1) {
821 DEBUG(0,("print_queue_update: Failed to lock printer %s database\n", printer_name));
822 release_print_db(pdb);
827 * Ensure that no one else got in here.
828 * If the updating pid is still -1 then we are
832 if (get_updating_pid(printer_name) != -1) {
834 * Someone else is doing the update, exit.
836 tdb_unlock_bystring(pdb->tdb, keystr);
837 release_print_db(pdb);
842 * We're going to do the update ourselves.
845 /* Tell others we're doing the update. */
846 set_updating_pid(printer_name, False);
849 * Allow others to enter and notice we're doing
853 tdb_unlock_bystring(pdb->tdb, keystr);
856 * Update the cache time FIRST ! Stops others even
857 * attempting to get the lock and doing this
858 * if the lpq takes a long time.
861 slprintf(cachestr, sizeof(cachestr)-1, "CACHE/%s", printer_name);
862 tdb_store_int32(pdb->tdb, cachestr, (int)time(NULL));
864 /* get the current queue using the appropriate interface */
867 qcount = (*(current_printif->queue_get))(snum, &queue, &status);
869 DEBUG(3, ("%d job%s in queue for %s\n", qcount, (qcount != 1) ?
870 "s" : "", printer_name));
873 any job in the internal database that is marked as spooled
874 and doesn't exist in the system queue is considered finished
875 and removed from the database
877 any job in the system database but not in the internal database
878 is added as a unix job
880 fill in any system job numbers as we go
882 for (i=0; i<qcount; i++) {
883 uint32 jobid = print_parse_jobid(queue[i].fs_file);
885 if (jobid == (uint32)-1) {
886 /* assume its a unix print job */
887 print_unix_job(snum, &queue[i], jobid);
891 /* we have an active SMB print job - update its status */
892 pjob = print_job_find(snum, jobid);
894 /* err, somethings wrong. Probably smbd was restarted
895 with jobs in the queue. All we can do is treat them
896 like unix jobs. Pity. */
897 print_unix_job(snum, &queue[i], jobid);
901 pjob->sysjob = queue[i].job;
902 pjob->status = queue[i].status;
904 pjob_store(snum, jobid, pjob);
907 /* now delete any queued entries that don't appear in the
909 tstruct.queue = queue;
910 tstruct.qcount = qcount;
912 tstruct.total_jobs = 0;
913 tstruct.lpq_time = time(NULL);
915 tdb_traverse(pdb->tdb, traverse_fn_delete, (void *)&tstruct);
917 SAFE_FREE(tstruct.queue);
919 DEBUG(10,("print_queue_update: printer %s INFO/total_jobs = %d\n",
920 printer_name, tstruct.total_jobs ));
922 tdb_store_int32(pdb->tdb, "INFO/total_jobs", tstruct.total_jobs);
924 get_queue_status(snum, &old_status);
925 if (old_status.qcount != qcount)
926 DEBUG(10,("print_queue_update: queue status change %d jobs -> %d jobs for printer %s\n",
927 old_status.qcount, qcount, printer_name ));
929 /* store the new queue status structure */
930 slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", printer_name);
932 key.dsize = strlen(keystr);
934 status.qcount = qcount;
935 data.dptr = (void *)&status;
936 data.dsize = sizeof(status);
937 tdb_store(pdb->tdb, key, data, TDB_REPLACE);
940 * Update the cache time again. We want to do this call
941 * as little as possible...
944 slprintf(keystr, sizeof(keystr)-1, "CACHE/%s", printer_name);
945 tdb_store_int32(pdb->tdb, keystr, (int32)time(NULL));
947 /* Delete our pid from the db. */
948 set_updating_pid(printer_name, True);
949 release_print_db(pdb);
952 /****************************************************************************
953 Create/Update an entry in the print tdb that will allow us to send notify
954 updates only to interested smbd's.
955 ****************************************************************************/
957 BOOL print_notify_register_pid(int snum)
960 struct tdb_print_db *pdb = NULL;
961 TDB_CONTEXT *tdb = NULL;
962 const char *printername;
963 uint32 mypid = (uint32)sys_getpid();
967 /* if (snum == -1), then the change notify request was
968 on a print server handle and we need to register on
973 int num_services = lp_numservices();
976 for ( idx=0; idx<num_services; idx++ ) {
977 if (lp_snum_ok(idx) && lp_print_ok(idx) )
978 print_notify_register_pid(idx);
983 else /* register for a specific printer */
985 printername = lp_const_servicename(snum);
986 pdb = get_print_db_byname(printername);
992 if (tdb_lock_bystring(tdb, NOTIFY_PID_LIST_KEY, 10) == -1) {
993 DEBUG(0,("print_notify_register_pid: Failed to lock printer %s\n",
996 release_print_db(pdb);
1000 data = get_printer_notify_pid_list( tdb, printername, True );
1002 /* Add ourselves and increase the refcount. */
1004 for (i = 0; i < data.dsize; i += 8) {
1005 if (IVAL(data.dptr,i) == mypid) {
1006 uint32 new_refcount = IVAL(data.dptr, i+4) + 1;
1007 SIVAL(data.dptr, i+4, new_refcount);
1012 if (i == data.dsize) {
1013 /* We weren't in the list. Realloc. */
1014 data.dptr = Realloc(data.dptr, data.dsize + 8);
1016 DEBUG(0,("print_notify_register_pid: Relloc fail for printer %s\n",
1021 SIVAL(data.dptr,data.dsize - 8,mypid);
1022 SIVAL(data.dptr,data.dsize - 4,1); /* Refcount. */
1025 /* Store back the record. */
1026 if (tdb_store_by_string(tdb, NOTIFY_PID_LIST_KEY, data, TDB_REPLACE) == -1) {
1027 DEBUG(0,("print_notify_register_pid: Failed to update pid \
1028 list for printer %s\n", printername));
1036 tdb_unlock_bystring(tdb, NOTIFY_PID_LIST_KEY);
1038 release_print_db(pdb);
1039 SAFE_FREE(data.dptr);
1043 /****************************************************************************
1044 Update an entry in the print tdb that will allow us to send notify
1045 updates only to interested smbd's.
1046 ****************************************************************************/
1048 BOOL print_notify_deregister_pid(int snum)
1051 struct tdb_print_db *pdb = NULL;
1052 TDB_CONTEXT *tdb = NULL;
1053 const char *printername;
1054 uint32 mypid = (uint32)sys_getpid();
1058 /* if ( snum == -1 ), we are deregister a print server handle
1059 which means to deregister on all print queues */
1063 int num_services = lp_numservices();
1066 for ( idx=0; idx<num_services; idx++ ) {
1067 if ( lp_snum_ok(idx) && lp_print_ok(idx) )
1068 print_notify_deregister_pid(idx);
1073 else /* deregister a specific printer */
1075 printername = lp_const_servicename(snum);
1076 pdb = get_print_db_byname(printername);
1082 if (tdb_lock_bystring(tdb, NOTIFY_PID_LIST_KEY, 10) == -1) {
1083 DEBUG(0,("print_notify_register_pid: Failed to lock \
1084 printer %s database\n", printername));
1086 release_print_db(pdb);
1090 data = get_printer_notify_pid_list( tdb, printername, True );
1092 /* Reduce refcount. Remove ourselves if zero. */
1094 for (i = 0; i < data.dsize; ) {
1095 if (IVAL(data.dptr,i) == mypid) {
1096 uint32 refcount = IVAL(data.dptr, i+4);
1100 if (refcount == 0) {
1101 if (data.dsize - i > 8)
1102 memmove( &data.dptr[i], &data.dptr[i+8], data.dsize - i - 8);
1106 SIVAL(data.dptr, i+4, refcount);
1112 if (data.dsize == 0)
1113 SAFE_FREE(data.dptr);
1115 /* Store back the record. */
1116 if (tdb_store_by_string(tdb, NOTIFY_PID_LIST_KEY, data, TDB_REPLACE) == -1) {
1117 DEBUG(0,("print_notify_register_pid: Failed to update pid \
1118 list for printer %s\n", printername));
1126 tdb_unlock_bystring(tdb, NOTIFY_PID_LIST_KEY);
1128 release_print_db(pdb);
1129 SAFE_FREE(data.dptr);
1133 /****************************************************************************
1134 Check if a jobid is valid. It is valid if it exists in the database.
1135 ****************************************************************************/
1137 BOOL print_job_exists(int snum, uint32 jobid)
1139 struct tdb_print_db *pdb = get_print_db_byname(lp_const_servicename(snum));
1144 ret = tdb_exists(pdb->tdb, print_key(jobid));
1145 release_print_db(pdb);
1149 /****************************************************************************
1150 Give the fd used for a jobid.
1151 ****************************************************************************/
1153 int print_job_fd(int snum, uint32 jobid)
1155 struct printjob *pjob = print_job_find(snum, jobid);
1158 /* don't allow another process to get this info - it is meaningless */
1159 if (pjob->pid != local_pid)
1164 /****************************************************************************
1165 Give the filename used for a jobid.
1166 Only valid for the process doing the spooling and when the job
1167 has not been spooled.
1168 ****************************************************************************/
1170 char *print_job_fname(int snum, uint32 jobid)
1172 struct printjob *pjob = print_job_find(snum, jobid);
1173 if (!pjob || pjob->spooled || pjob->pid != local_pid)
1175 return pjob->filename;
1179 /****************************************************************************
1180 Give the filename used for a jobid.
1181 Only valid for the process doing the spooling and when the job
1182 has not been spooled.
1183 ****************************************************************************/
1185 NT_DEVICEMODE *print_job_devmode(int snum, uint32 jobid)
1187 struct printjob *pjob = print_job_find(snum, jobid);
1192 return pjob->nt_devmode;
1195 /****************************************************************************
1196 Set the place in the queue for a job.
1197 ****************************************************************************/
1199 BOOL print_job_set_place(int snum, uint32 jobid, int place)
1201 DEBUG(2,("print_job_set_place not implemented yet\n"));
1205 /****************************************************************************
1206 Set the name of a job. Only possible for owner.
1207 ****************************************************************************/
1209 BOOL print_job_set_name(int snum, uint32 jobid, char *name)
1211 struct printjob *pjob = print_job_find(snum, jobid);
1212 if (!pjob || pjob->pid != local_pid)
1215 fstrcpy(pjob->jobname, name);
1216 return pjob_store(snum, jobid, pjob);
1219 /****************************************************************************
1220 Delete a print job - don't update queue.
1221 ****************************************************************************/
1223 static BOOL print_job_delete1(int snum, uint32 jobid)
1225 struct printjob *pjob = print_job_find(snum, jobid);
1232 * If already deleting just return.
1235 if (pjob->status == LPQ_DELETING)
1238 /* Hrm - we need to be able to cope with deleting a job before it
1239 has reached the spooler. */
1241 if (pjob->sysjob == -1) {
1242 DEBUG(5, ("attempt to delete job %u not seen by lpr\n", (unsigned int)jobid));
1245 /* Set the tdb entry to be deleting. */
1247 pjob->status = LPQ_DELETING;
1248 pjob_store(snum, jobid, pjob);
1250 if (pjob->spooled && pjob->sysjob != -1)
1251 result = (*(current_printif->job_delete))(snum, pjob);
1253 /* Delete the tdb entry if the delete suceeded or the job hasn't
1257 pjob_delete(snum, jobid);
1259 return (result == 0);
1262 /****************************************************************************
1263 Return true if the current user owns the print job.
1264 ****************************************************************************/
1266 static BOOL is_owner(struct current_user *user, int snum, uint32 jobid)
1268 struct printjob *pjob = print_job_find(snum, jobid);
1274 if ((vuser = get_valid_user_struct(user->vuid)) != NULL) {
1275 return strequal(pjob->user, vuser->user.smb_name);
1277 return strequal(pjob->user, uidtoname(user->uid));
1281 /****************************************************************************
1283 ****************************************************************************/
1285 BOOL print_job_delete(struct current_user *user, int snum, uint32 jobid, WERROR *errcode)
1287 BOOL owner, deleted;
1292 owner = is_owner(user, snum, jobid);
1294 /* Check access against security descriptor or whether the user
1298 !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
1299 DEBUG(3, ("delete denied by security descriptor\n"));
1300 *errcode = WERR_ACCESS_DENIED;
1302 /* BEGIN_ADMIN_LOG */
1303 sys_adminlog( LOG_ERR,
1304 "Permission denied-- user not allowed to delete, \
1305 pause, or resume print job. User name: %s. Printer name: %s.",
1306 uidtoname(user->uid), PRINTERNAME(snum) );
1313 * get the spooled filename of the print job
1314 * if this works, then the file has not been spooled
1315 * to the underlying print system. Just delete the
1316 * spool file & return.
1319 if ( (fname = print_job_fname( snum, jobid )) != NULL )
1321 /* remove the spool file */
1322 DEBUG(10,("print_job_delete: Removing spool file [%s]\n", fname ));
1323 if ( unlink( fname ) == -1 ) {
1324 *errcode = map_werror_from_unix(errno);
1331 if (!print_job_delete1(snum, jobid)) {
1332 *errcode = WERR_ACCESS_DENIED;
1336 /* force update the database and say the delete failed if the
1339 print_queue_update(snum);
1341 deleted = !print_job_exists(snum, jobid);
1343 *errcode = WERR_ACCESS_DENIED;
1348 /****************************************************************************
1350 ****************************************************************************/
1352 BOOL print_job_pause(struct current_user *user, int snum, uint32 jobid, WERROR *errcode)
1354 struct printjob *pjob = print_job_find(snum, jobid);
1360 if (!pjob->spooled || pjob->sysjob == -1)
1363 if (!is_owner(user, snum, jobid) &&
1364 !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
1365 DEBUG(3, ("pause denied by security descriptor\n"));
1367 /* BEGIN_ADMIN_LOG */
1368 sys_adminlog( LOG_ERR,
1369 "Permission denied-- user not allowed to delete, \
1370 pause, or resume print job. User name: %s. Printer name: %s.",
1371 uidtoname(user->uid), PRINTERNAME(snum) );
1374 *errcode = WERR_ACCESS_DENIED;
1378 /* need to pause the spooled entry */
1379 ret = (*(current_printif->job_pause))(snum, pjob);
1382 *errcode = WERR_INVALID_PARAM;
1386 /* force update the database */
1387 print_cache_flush(snum);
1389 /* Send a printer notify message */
1391 notify_job_status(snum, jobid, JOB_STATUS_PAUSED);
1393 /* how do we tell if this succeeded? */
1398 /****************************************************************************
1400 ****************************************************************************/
1402 BOOL print_job_resume(struct current_user *user, int snum, uint32 jobid, WERROR *errcode)
1404 struct printjob *pjob = print_job_find(snum, jobid);
1410 if (!pjob->spooled || pjob->sysjob == -1)
1413 if (!is_owner(user, snum, jobid) &&
1414 !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
1415 DEBUG(3, ("resume denied by security descriptor\n"));
1416 *errcode = WERR_ACCESS_DENIED;
1418 /* BEGIN_ADMIN_LOG */
1419 sys_adminlog( LOG_ERR,
1420 "Permission denied-- user not allowed to delete, \
1421 pause, or resume print job. User name: %s. Printer name: %s.",
1422 uidtoname(user->uid), PRINTERNAME(snum) );
1427 ret = (*(current_printif->job_resume))(snum, pjob);
1430 *errcode = WERR_INVALID_PARAM;
1434 /* force update the database */
1435 print_cache_flush(snum);
1437 /* Send a printer notify message */
1439 notify_job_status(snum, jobid, JOB_STATUS_QUEUED);
1444 /****************************************************************************
1445 Write to a print file.
1446 ****************************************************************************/
1448 int print_job_write(int snum, uint32 jobid, const char *buf, int size)
1451 struct printjob *pjob = print_job_find(snum, jobid);
1455 /* don't allow another process to get this info - it is meaningless */
1456 if (pjob->pid != local_pid)
1459 return_code = write(pjob->fd, buf, size);
1460 if (return_code>0) {
1462 pjob_store(snum, jobid, pjob);
1467 /****************************************************************************
1468 Check if the print queue has been updated recently enough.
1469 ****************************************************************************/
1471 static BOOL print_cache_expired(int snum)
1474 time_t last_qscan_time, time_now = time(NULL);
1475 const char *printername = lp_const_servicename(snum);
1476 struct tdb_print_db *pdb = get_print_db_byname(printername);
1481 slprintf(key, sizeof(key), "CACHE/%s", printername);
1482 last_qscan_time = (time_t)tdb_fetch_int32(pdb->tdb, key);
1485 * Invalidate the queue for 3 reasons.
1486 * (1). last queue scan time == -1.
1487 * (2). Current time - last queue scan time > allowed cache time.
1488 * (3). last queue scan time > current time + MAX_CACHE_VALID_TIME (1 hour by default).
1489 * This last test picks up machines for which the clock has been moved
1490 * forward, an lpq scan done and then the clock moved back. Otherwise
1491 * that last lpq scan would stay around for a loooong loooong time... :-). JRA.
1494 if (last_qscan_time == ((time_t)-1) || (time_now - last_qscan_time) >= lp_lpqcachetime() ||
1495 last_qscan_time > (time_now + MAX_CACHE_VALID_TIME)) {
1496 DEBUG(3, ("print cache expired for queue %s \
1497 (last_qscan_time = %d, time now = %d, qcachetime = %d)\n", printername,
1498 (int)last_qscan_time, (int)time_now, (int)lp_lpqcachetime() ));
1499 release_print_db(pdb);
1502 release_print_db(pdb);
1506 /****************************************************************************
1507 Get the queue status - do not update if db is out of date.
1508 ****************************************************************************/
1510 static int get_queue_status(int snum, print_status_struct *status)
1514 const char *printername = lp_const_servicename(snum);
1515 struct tdb_print_db *pdb = get_print_db_byname(printername);
1522 ZERO_STRUCTP(status);
1523 slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", printername);
1525 key.dsize = strlen(keystr);
1526 data = tdb_fetch(pdb->tdb, key);
1528 if (data.dsize == sizeof(print_status_struct))
1529 memcpy(status, data.dptr, sizeof(print_status_struct));
1530 SAFE_FREE(data.dptr);
1533 len = tdb_fetch_int32(pdb->tdb, "INFO/total_jobs");
1534 release_print_db(pdb);
1535 return (len == -1 ? 0 : len);
1538 /****************************************************************************
1539 Determine the number of jobs in a queue.
1540 ****************************************************************************/
1542 int print_queue_length(int snum, print_status_struct *pstatus)
1544 print_status_struct status;
1547 /* make sure the database is up to date */
1548 if (print_cache_expired(snum))
1549 print_queue_update(snum);
1551 /* also fetch the queue status */
1552 memset(&status, 0, sizeof(status));
1553 len = get_queue_status(snum, &status);
1561 /***************************************************************************
1562 Allocate a jobid. Hold the lock for as short a time as possible.
1563 ***************************************************************************/
1565 static BOOL allocate_print_jobid(struct tdb_print_db *pdb, int snum, const char *printername, uint32 *pjobid)
1570 *pjobid = (uint32)-1;
1572 for (i = 0; i < 3; i++) {
1573 /* Lock the database - only wait 20 seconds. */
1574 if (tdb_lock_bystring(pdb->tdb, "INFO/nextjob", 20) == -1) {
1575 DEBUG(0,("allocate_print_jobid: failed to lock printing database %s\n", printername ));
1579 if (!tdb_fetch_uint32(pdb->tdb, "INFO/nextjob", &jobid)) {
1580 if (tdb_error(pdb->tdb) != TDB_ERR_NOEXIST) {
1581 DEBUG(0, ("allocate_print_jobid: failed to fetch INFO/nextjob for print queue %s\n",
1588 jobid = NEXT_JOBID(jobid);
1590 if (tdb_store_int32(pdb->tdb, "INFO/nextjob", jobid)==-1) {
1591 DEBUG(3, ("allocate_print_jobid: failed to store INFO/nextjob.\n"));
1592 tdb_unlock_bystring(pdb->tdb, "INFO/nextjob");
1596 /* We've finished with the INFO/nextjob lock. */
1597 tdb_unlock_bystring(pdb->tdb, "INFO/nextjob");
1599 if (!print_job_exists(snum, jobid))
1604 DEBUG(0, ("allocate_print_jobid: failed to allocate a print job for queue %s\n",
1606 /* Probably full... */
1611 /* Store a dummy placeholder. */
1616 if (tdb_store(pdb->tdb, print_key(jobid), dum, TDB_INSERT) == -1) {
1617 DEBUG(3, ("allocate_print_jobid: jobid (%d) failed to store placeholder.\n",
1627 /***************************************************************************
1628 Start spooling a job - return the jobid.
1629 ***************************************************************************/
1631 uint32 print_job_start(struct current_user *user, int snum, char *jobname, NT_DEVICEMODE *nt_devmode )
1635 struct printjob pjob;
1637 const char *printername = lp_const_servicename(snum);
1638 struct tdb_print_db *pdb = get_print_db_byname(printername);
1646 if (!print_access_check(user, snum, PRINTER_ACCESS_USE)) {
1647 DEBUG(3, ("print_job_start: job start denied by security descriptor\n"));
1648 release_print_db(pdb);
1652 if (!print_time_access_check(snum)) {
1653 DEBUG(3, ("print_job_start: job start denied by time check\n"));
1654 release_print_db(pdb);
1658 path = lp_pathname(snum);
1660 /* see if we have sufficient disk space */
1661 if (lp_minprintspace(snum)) {
1662 SMB_BIG_UINT dspace, dsize;
1663 if (sys_fsusage(path, &dspace, &dsize) == 0 &&
1664 dspace < 2*(SMB_BIG_UINT)lp_minprintspace(snum)) {
1665 DEBUG(3, ("print_job_start: disk space check failed.\n"));
1666 release_print_db(pdb);
1672 /* for autoloaded printers, check that the printcap entry still exists */
1673 if (lp_autoloaded(snum) && !pcap_printername_ok(lp_const_servicename(snum), NULL)) {
1674 DEBUG(3, ("print_job_start: printer name %s check failed.\n", lp_const_servicename(snum) ));
1675 release_print_db(pdb);
1680 /* Insure the maximum queue size is not violated */
1681 if ((njobs = print_queue_length(snum,NULL)) > lp_maxprintjobs(snum)) {
1682 DEBUG(3, ("print_job_start: Queue %s number of jobs (%d) larger than max printjobs per queue (%d).\n",
1683 printername, njobs, lp_maxprintjobs(snum) ));
1684 release_print_db(pdb);
1689 DEBUG(10,("print_job_start: Queue %s number of jobs (%d), max printjobs = %d\n",
1690 printername, njobs, lp_maxprintjobs(snum) ));
1692 if (!allocate_print_jobid(pdb, snum, printername, &jobid))
1695 /* create the database entry */
1699 pjob.pid = local_pid;
1702 pjob.starttime = time(NULL);
1703 pjob.status = LPQ_SPOOLING;
1705 pjob.spooled = False;
1707 pjob.nt_devmode = nt_devmode;
1709 fstrcpy(pjob.jobname, jobname);
1711 if ((vuser = get_valid_user_struct(user->vuid)) != NULL) {
1712 fstrcpy(pjob.user, vuser->user.smb_name);
1714 fstrcpy(pjob.user, uidtoname(user->uid));
1717 fstrcpy(pjob.queuename, lp_const_servicename(snum));
1719 /* we have a job entry - now create the spool file */
1720 slprintf(pjob.filename, sizeof(pjob.filename)-1, "%s/%s%.8u.XXXXXX",
1721 path, PRINT_SPOOL_PREFIX, (unsigned int)jobid);
1722 pjob.fd = smb_mkstemp(pjob.filename);
1724 if (pjob.fd == -1) {
1725 if (errno == EACCES) {
1726 /* Common setup error, force a report. */
1727 DEBUG(0, ("print_job_start: insufficient permissions \
1728 to open spool file %s.\n", pjob.filename));
1730 /* Normal case, report at level 3 and above. */
1731 DEBUG(3, ("print_job_start: can't open spool file %s,\n", pjob.filename));
1732 DEBUGADD(3, ("errno = %d (%s).\n", errno, strerror(errno)));
1737 pjob_store(snum, jobid, &pjob);
1739 /* Ensure we keep a rough count of the number of total jobs... */
1740 tdb_change_int32_atomic(pdb->tdb, "INFO/total_jobs", &njobs, 1);
1742 release_print_db(pdb);
1748 pjob_delete(snum, jobid);
1750 release_print_db(pdb);
1752 DEBUG(3, ("print_job_start: returning fail. Error = %s\n", strerror(errno) ));
1756 /****************************************************************************
1757 Update the number of pages spooled to jobid
1758 ****************************************************************************/
1760 void print_job_endpage(int snum, uint32 jobid)
1762 struct printjob *pjob = print_job_find(snum, jobid);
1765 /* don't allow another process to get this info - it is meaningless */
1766 if (pjob->pid != local_pid)
1770 pjob_store(snum, jobid, pjob);
1773 /****************************************************************************
1774 Print a file - called on closing the file. This spools the job.
1775 If normal close is false then we're tearing down the jobs - treat as an
1777 ****************************************************************************/
1779 BOOL print_job_end(int snum, uint32 jobid, BOOL normal_close)
1781 struct printjob *pjob = print_job_find(snum, jobid);
1783 SMB_STRUCT_STAT sbuf;
1788 if (pjob->spooled || pjob->pid != local_pid)
1791 if (normal_close && (sys_fstat(pjob->fd, &sbuf) == 0)) {
1792 pjob->size = sbuf.st_size;
1798 * Not a normal close or we couldn't stat the job file,
1799 * so something has gone wrong. Cleanup.
1803 DEBUG(3,("print_job_end: failed to stat file for jobid %d\n", jobid ));
1807 /* Technically, this is not quite right. If the printer has a separator
1808 * page turned on, the NT spooler prints the separator page even if the
1809 * print job is 0 bytes. 010215 JRR */
1810 if (pjob->size == 0 || pjob->status == LPQ_DELETING) {
1811 /* don't bother spooling empty files or something being deleted. */
1812 DEBUG(5,("print_job_end: canceling spool of %s (%s)\n",
1813 pjob->filename, pjob->size ? "deleted" : "zero length" ));
1814 unlink(pjob->filename);
1815 pjob_delete(snum, jobid);
1819 ret = (*(current_printif->job_submit))(snum, pjob);
1824 /* The print job has been sucessfully handed over to the back-end */
1826 pjob->spooled = True;
1827 pjob->status = LPQ_QUEUED;
1828 pjob_store(snum, jobid, pjob);
1830 /* make sure the database is up to date */
1831 if (print_cache_expired(snum))
1832 print_queue_update(snum);
1838 /* The print job was not succesfully started. Cleanup */
1839 /* Still need to add proper error return propagation! 010122:JRR */
1840 unlink(pjob->filename);
1841 pjob_delete(snum, jobid);
1845 /****************************************************************************
1846 Utility fn to enumerate the print queue.
1847 ****************************************************************************/
1849 static int traverse_fn_queue(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
1851 struct traverse_struct *ts = (struct traverse_struct *)state;
1852 struct printjob pjob;
1858 if ( key.dsize != sizeof(jobid) )
1861 memcpy(&jobid, key.dptr, sizeof(jobid));
1863 if ( unpack_pjob( data.dptr, data.dsize, &pjob ) == -1 )
1865 free_nt_devicemode( &pjob.nt_devmode );
1867 /* maybe it isn't for this queue */
1868 if (ts->snum != lp_servicenumber(pjob.queuename))
1871 if (ts->qcount >= ts->maxcount)
1876 ts->queue[i].job = jobid;
1877 ts->queue[i].size = pjob.size;
1878 ts->queue[i].page_count = pjob.page_count;
1879 ts->queue[i].status = pjob.status;
1880 ts->queue[i].priority = 1;
1881 ts->queue[i].time = pjob.starttime;
1882 fstrcpy(ts->queue[i].fs_user, pjob.user);
1883 fstrcpy(ts->queue[i].fs_file, pjob.jobname);
1890 struct traverse_count_struct {
1894 /****************************************************************************
1895 Utility fn to count the number of entries in the print queue.
1896 ****************************************************************************/
1898 static int traverse_count_fn_queue(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
1900 struct traverse_count_struct *ts = (struct traverse_count_struct *)state;
1901 struct printjob pjob;
1906 if ( key.dsize != sizeof(jobid) )
1909 memcpy(&jobid, key.dptr, sizeof(jobid));
1911 if ( unpack_pjob( data.dptr, data.dsize, &pjob ) == -1 )
1914 free_nt_devicemode( &pjob.nt_devmode );
1916 /* maybe it isn't for this queue - this cannot happen with the tdb/printer code. JRA */
1917 if (ts->snum != lp_servicenumber(pjob.queuename))
1925 /****************************************************************************
1926 Sort print jobs by submittal time.
1927 ****************************************************************************/
1929 static int printjob_comp(print_queue_struct *j1, print_queue_struct *j2)
1940 /* Sort on job start time */
1942 if (j1->time == j2->time)
1944 return (j1->time > j2->time) ? 1 : -1;
1947 /****************************************************************************
1948 Get a printer queue listing.
1949 set queue = NULL and status = NULL if you just want to update the cache
1950 ****************************************************************************/
1952 int print_queue_status(int snum,
1953 print_queue_struct **queue,
1954 print_status_struct *status)
1956 struct traverse_struct tstruct;
1957 struct traverse_count_struct tsc;
1960 const char *printername;
1961 struct tdb_print_db *pdb;
1963 /* make sure the database is up to date */
1965 if (print_cache_expired(snum))
1966 print_queue_update(snum);
1968 /* return if we are done */
1970 if ( !queue || !status )
1974 printername = lp_const_servicename(snum);
1975 pdb = get_print_db_byname(printername);
1981 * Fetch the queue status. We must do this first, as there may
1982 * be no jobs in the queue.
1984 ZERO_STRUCTP(status);
1985 slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", printername);
1987 key.dsize = strlen(keystr);
1988 data = tdb_fetch(pdb->tdb, key);
1990 if (data.dsize == sizeof(*status)) {
1991 memcpy(status, data.dptr, sizeof(*status));
1993 SAFE_FREE(data.dptr);
1997 * Now, fetch the print queue information. We first count the number
1998 * of entries, and then only retrieve the queue if necessary.
2003 tdb_traverse(pdb->tdb, traverse_count_fn_queue, (void *)&tsc);
2005 if (tsc.count == 0) {
2006 release_print_db(pdb);
2010 /* Allocate the queue size. */
2011 if ((tstruct.queue = (print_queue_struct *)
2012 malloc(sizeof(print_queue_struct)*tsc.count)) == NULL) {
2013 release_print_db(pdb);
2018 * Fill in the queue.
2019 * We need maxcount as the queue size may have changed between
2020 * the two calls to tdb_traverse.
2023 tstruct.maxcount = tsc.count;
2024 tstruct.snum = snum;
2026 tdb_traverse(pdb->tdb, traverse_fn_queue, (void *)&tstruct);
2027 release_print_db(pdb);
2029 /* Sort the queue by submission time otherwise they are displayed
2032 qsort(tstruct.queue, tstruct.qcount, sizeof(print_queue_struct),
2033 QSORT_CAST(printjob_comp));
2035 *queue = tstruct.queue;
2036 return tstruct.qcount;
2039 /****************************************************************************
2041 ****************************************************************************/
2043 BOOL print_queue_pause(struct current_user *user, int snum, WERROR *errcode)
2047 if (!print_access_check(user, snum, PRINTER_ACCESS_ADMINISTER)) {
2048 *errcode = WERR_ACCESS_DENIED;
2052 ret = (*(current_printif->queue_pause))(snum);
2055 *errcode = WERR_INVALID_PARAM;
2059 /* force update the database */
2060 print_cache_flush(snum);
2062 /* Send a printer notify message */
2064 notify_printer_status(snum, PRINTER_STATUS_PAUSED);
2069 /****************************************************************************
2071 ****************************************************************************/
2073 BOOL print_queue_resume(struct current_user *user, int snum, WERROR *errcode)
2077 if (!print_access_check(user, snum, PRINTER_ACCESS_ADMINISTER)) {
2078 *errcode = WERR_ACCESS_DENIED;
2082 ret = (*(current_printif->queue_resume))(snum);
2085 *errcode = WERR_INVALID_PARAM;
2089 /* make sure the database is up to date */
2090 if (print_cache_expired(snum))
2091 print_queue_update(snum);
2093 /* Send a printer notify message */
2095 notify_printer_status(snum, PRINTER_STATUS_OK);
2100 /****************************************************************************
2101 Purge a queue - implemented by deleting all jobs that we can delete.
2102 ****************************************************************************/
2104 BOOL print_queue_purge(struct current_user *user, int snum, WERROR *errcode)
2106 print_queue_struct *queue;
2107 print_status_struct status;
2111 /* Force and update so the count is accurate (i.e. not a cached count) */
2112 print_queue_update(snum);
2114 can_job_admin = print_access_check(user, snum, JOB_ACCESS_ADMINISTER);
2115 njobs = print_queue_status(snum, &queue, &status);
2117 for (i=0;i<njobs;i++) {
2118 BOOL owner = is_owner(user, snum, queue[i].job);
2120 if (owner || can_job_admin) {
2121 print_job_delete1(snum, queue[i].job);