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.
26 extern SIG_ATOMIC_T got_sig_term;
27 extern SIG_ATOMIC_T reload_after_sighup;
29 /* Current printer interface */
30 static BOOL remove_from_jobs_changed(const char* sharename, uint32 jobid);
33 the printing backend revolves around a tdb database that stores the
34 SMB view of the print queue
36 The key for this database is a jobid - a internally generated number that
37 uniquely identifies a print job
39 reading the print queue involves two steps:
40 - possibly running lpq and updating the internal database from that
41 - reading entries from the database
43 jobids are assigned when a job starts spooling.
46 static TDB_CONTEXT *rap_tdb;
47 static uint16 next_rap_jobid;
48 struct rap_jobid_key {
53 /***************************************************************************
54 Nightmare. LANMAN jobid's are 16 bit numbers..... We must map them to 32
55 bit RPC jobids.... JRA.
56 ***************************************************************************/
58 uint16 pjobid_to_rap(const char* sharename, uint32 jobid)
62 struct rap_jobid_key jinfo;
65 DEBUG(10,("pjobid_to_rap: called.\n"));
68 /* Create the in-memory tdb. */
69 rap_tdb = tdb_open_log(NULL, 0, TDB_INTERNAL, (O_RDWR|O_CREAT), 0644);
75 fstrcpy( jinfo.sharename, sharename );
77 key.dptr = (char*)&jinfo;
78 key.dsize = sizeof(jinfo);
80 data = tdb_fetch(rap_tdb, key);
81 if (data.dptr && data.dsize == sizeof(uint16)) {
82 rap_jobid = SVAL(data.dptr, 0);
84 DEBUG(10,("pjobid_to_rap: jobid %u maps to RAP jobid %u\n",
85 (unsigned int)jobid, (unsigned int)rap_jobid));
89 /* Not found - create and store mapping. */
90 rap_jobid = ++next_rap_jobid;
92 rap_jobid = ++next_rap_jobid;
93 SSVAL(buf,0,rap_jobid);
94 data.dptr = (char*)buf;
95 data.dsize = sizeof(rap_jobid);
96 tdb_store(rap_tdb, key, data, TDB_REPLACE);
97 tdb_store(rap_tdb, data, key, TDB_REPLACE);
99 DEBUG(10,("pjobid_to_rap: created jobid %u maps to RAP jobid %u\n",
100 (unsigned int)jobid, (unsigned int)rap_jobid));
104 BOOL rap_to_pjobid(uint16 rap_jobid, fstring sharename, uint32 *pjobid)
109 DEBUG(10,("rap_to_pjobid called.\n"));
114 SSVAL(buf,0,rap_jobid);
115 key.dptr = (char*)buf;
116 key.dsize = sizeof(rap_jobid);
117 data = tdb_fetch(rap_tdb, key);
118 if ( data.dptr && data.dsize == sizeof(struct rap_jobid_key) )
120 struct rap_jobid_key *jinfo = (struct rap_jobid_key*)data.dptr;
121 fstrcpy( sharename, jinfo->sharename );
122 *pjobid = jinfo->jobid;
123 DEBUG(10,("rap_to_pjobid: jobid %u maps to RAP jobid %u\n",
124 (unsigned int)*pjobid, (unsigned int)rap_jobid));
125 SAFE_FREE(data.dptr);
129 DEBUG(10,("rap_to_pjobid: Failed to lookup RAP jobid %u\n",
130 (unsigned int)rap_jobid));
131 SAFE_FREE(data.dptr);
135 static void rap_jobid_delete(const char* sharename, uint32 jobid)
139 struct rap_jobid_key jinfo;
142 DEBUG(10,("rap_jobid_delete: called.\n"));
147 ZERO_STRUCT( jinfo );
148 fstrcpy( jinfo.sharename, sharename );
150 key.dptr = (char*)&jinfo;
151 key.dsize = sizeof(jinfo);
153 data = tdb_fetch(rap_tdb, key);
154 if (!data.dptr || (data.dsize != sizeof(uint16))) {
155 DEBUG(10,("rap_jobid_delete: cannot find jobid %u\n",
156 (unsigned int)jobid ));
157 SAFE_FREE(data.dptr);
161 DEBUG(10,("rap_jobid_delete: deleting jobid %u\n",
162 (unsigned int)jobid ));
164 rap_jobid = SVAL(data.dptr, 0);
165 SAFE_FREE(data.dptr);
166 SSVAL(buf,0,rap_jobid);
167 data.dptr = (char*)buf;
168 data.dsize = sizeof(rap_jobid);
169 tdb_delete(rap_tdb, key);
170 tdb_delete(rap_tdb, data);
173 static int get_queue_status(const char* sharename, print_status_struct *);
175 /****************************************************************************
176 Initialise the printing backend. Called once at startup before the fork().
177 ****************************************************************************/
179 BOOL print_backend_init(void)
181 const char *sversion = "INFO/version";
182 pstring printing_path;
183 int services = lp_numservices();
186 unlink(lock_path("printing.tdb"));
187 pstrcpy(printing_path,lock_path("printing"));
188 mkdir(printing_path,0755);
190 /* handle a Samba upgrade */
192 for (snum = 0; snum < services; snum++) {
193 struct tdb_print_db *pdb;
194 if (!lp_print_ok(snum))
197 pdb = get_print_db_byname(lp_const_servicename(snum));
200 if (tdb_lock_bystring(pdb->tdb, sversion) == -1) {
201 DEBUG(0,("print_backend_init: Failed to open printer %s database\n", lp_const_servicename(snum) ));
202 release_print_db(pdb);
205 if (tdb_fetch_int32(pdb->tdb, sversion) != PRINT_DATABASE_VERSION) {
206 tdb_traverse(pdb->tdb, tdb_traverse_delete_fn, NULL);
207 tdb_store_int32(pdb->tdb, sversion, PRINT_DATABASE_VERSION);
209 tdb_unlock_bystring(pdb->tdb, sversion);
210 release_print_db(pdb);
213 close_all_print_db(); /* Don't leave any open. */
215 /* do NT print initialization... */
216 return nt_printing_init();
219 /****************************************************************************
220 Shut down printing backend. Called once at shutdown to close the tdb.
221 ****************************************************************************/
223 void printing_end(void)
225 close_all_print_db(); /* Don't leave any open. */
228 /****************************************************************************
229 Retrieve the set of printing functions for a given service. This allows
230 us to set the printer function table based on the value of the 'printing'
233 Use the generic interface as the default and only use cups interface only
234 when asked for (and only when supported)
235 ****************************************************************************/
237 static struct printif *get_printer_fns_from_type( enum printing_types type )
239 struct printif *printer_fns = &generic_printif;
242 if ( type == PRINT_CUPS ) {
243 printer_fns = &cups_printif;
245 #endif /* HAVE_CUPS */
248 if ( type == PRINT_IPRINT ) {
249 printer_fns = &iprint_printif;
251 #endif /* HAVE_IPRINT */
253 printer_fns->type = type;
258 static struct printif *get_printer_fns( int snum )
260 return get_printer_fns_from_type( (enum printing_types)lp_printing(snum) );
264 /****************************************************************************
265 Useful function to generate a tdb key.
266 ****************************************************************************/
268 static TDB_DATA print_key(uint32 jobid)
274 ret.dptr = (void *)&j;
275 ret.dsize = sizeof(j);
279 /***********************************************************************
280 unpack a pjob from a tdb buffer
281 ***********************************************************************/
283 int unpack_pjob( char* buf, int buflen, struct printjob *pjob )
287 uint32 pjpid, pjsysjob, pjfd, pjstarttime, pjstatus;
288 uint32 pjsize, pjpage_count, pjspooled, pjsmbjob;
293 len += tdb_unpack(buf+len, buflen-len, "dddddddddffff",
311 if ( (used = unpack_devicemode(&pjob->nt_devmode, buf+len, buflen-len)) == -1 )
317 pjob->sysjob = pjsysjob;
319 pjob->starttime = pjstarttime;
320 pjob->status = pjstatus;
322 pjob->page_count = pjpage_count;
323 pjob->spooled = pjspooled;
324 pjob->smbjob = pjsmbjob;
330 /****************************************************************************
331 Useful function to find a print job in the database.
332 ****************************************************************************/
334 static struct printjob *print_job_find(const char *sharename, uint32 jobid)
336 static struct printjob pjob;
338 struct tdb_print_db *pdb = get_print_db_byname(sharename);
340 DEBUG(10,("print_job_find: looking up job %u for share %s\n",
341 (unsigned int)jobid, sharename ));
347 ret = tdb_fetch(pdb->tdb, print_key(jobid));
348 release_print_db(pdb);
351 DEBUG(10,("print_job_find: failed to find jobid %u.\n", (unsigned int)jobid ));
355 if ( pjob.nt_devmode ) {
356 free_nt_devicemode( &pjob.nt_devmode );
361 if ( unpack_pjob( ret.dptr, ret.dsize, &pjob ) == -1 ) {
362 DEBUG(10,("print_job_find: failed to unpack jobid %u.\n", (unsigned int)jobid ));
369 DEBUG(10,("print_job_find: returning system job %d for jobid %u.\n",
370 (int)pjob.sysjob, (unsigned int)jobid ));
375 /* Convert a unix jobid to a smb jobid */
377 static uint32 sysjob_to_jobid_value;
379 static int unixjob_traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA key,
380 TDB_DATA data, void *state)
382 struct printjob *pjob;
383 int *sysjob = (int *)state;
385 if (!data.dptr || data.dsize == 0)
388 pjob = (struct printjob *)data.dptr;
389 if (key.dsize != sizeof(uint32))
392 if (*sysjob == pjob->sysjob) {
393 uint32 jobid = IVAL(key.dptr,0);
395 sysjob_to_jobid_value = jobid;
402 /****************************************************************************
403 This is a *horribly expensive call as we have to iterate through all the
404 current printer tdb's. Don't do this often ! JRA.
405 ****************************************************************************/
407 uint32 sysjob_to_jobid(int unix_jobid)
409 int services = lp_numservices();
412 sysjob_to_jobid_value = (uint32)-1;
414 for (snum = 0; snum < services; snum++) {
415 struct tdb_print_db *pdb;
416 if (!lp_print_ok(snum))
418 pdb = get_print_db_byname(lp_const_servicename(snum));
422 tdb_traverse(pdb->tdb, unixjob_traverse_fn, &unix_jobid);
423 release_print_db(pdb);
424 if (sysjob_to_jobid_value != (uint32)-1)
425 return sysjob_to_jobid_value;
430 /****************************************************************************
431 Send notifications based on what has changed after a pjob_store.
432 ****************************************************************************/
436 uint32 spoolss_status;
437 } lpq_to_spoolss_status_map[] = {
438 { LPQ_QUEUED, JOB_STATUS_QUEUED },
439 { LPQ_PAUSED, JOB_STATUS_PAUSED },
440 { LPQ_SPOOLING, JOB_STATUS_SPOOLING },
441 { LPQ_PRINTING, JOB_STATUS_PRINTING },
442 { LPQ_DELETING, JOB_STATUS_DELETING },
443 { LPQ_OFFLINE, JOB_STATUS_OFFLINE },
444 { LPQ_PAPEROUT, JOB_STATUS_PAPEROUT },
445 { LPQ_PRINTED, JOB_STATUS_PRINTED },
446 { LPQ_DELETED, JOB_STATUS_DELETED },
447 { LPQ_BLOCKED, JOB_STATUS_BLOCKED },
448 { LPQ_USER_INTERVENTION, JOB_STATUS_USER_INTERVENTION },
452 /* Convert a lpq status value stored in printing.tdb into the
453 appropriate win32 API constant. */
455 static uint32 map_to_spoolss_status(uint32 lpq_status)
459 while (lpq_to_spoolss_status_map[i].lpq_status != -1) {
460 if (lpq_to_spoolss_status_map[i].lpq_status == lpq_status)
461 return lpq_to_spoolss_status_map[i].spoolss_status;
468 static void pjob_store_notify(const char* sharename, uint32 jobid, struct printjob *old_data,
469 struct printjob *new_data)
471 BOOL new_job = False;
476 /* Job attributes that can't be changed. We only send
477 notification for these on a new job. */
479 /* ACHTUNG! Due to a bug in Samba's spoolss parsing of the
480 NOTIFY_INFO_DATA buffer, we *have* to send the job submission
481 time first or else we'll end up with potential alignment
482 errors. I don't think the systemtime should be spooled as
483 a string, but this gets us around that error.
484 --jerry (i'll feel dirty for this) */
487 notify_job_submitted(sharename, jobid, new_data->starttime);
488 notify_job_username(sharename, jobid, new_data->user);
491 if (new_job || !strequal(old_data->jobname, new_data->jobname))
492 notify_job_name(sharename, jobid, new_data->jobname);
494 /* Job attributes of a new job or attributes that can be
497 if (new_job || !strequal(old_data->jobname, new_data->jobname))
498 notify_job_name(sharename, jobid, new_data->jobname);
500 if (new_job || old_data->status != new_data->status)
501 notify_job_status(sharename, jobid, map_to_spoolss_status(new_data->status));
503 if (new_job || old_data->size != new_data->size)
504 notify_job_total_bytes(sharename, jobid, new_data->size);
506 if (new_job || old_data->page_count != new_data->page_count)
507 notify_job_total_pages(sharename, jobid, new_data->page_count);
510 /****************************************************************************
511 Store a job structure back to the database.
512 ****************************************************************************/
514 static BOOL pjob_store(const char* sharename, uint32 jobid, struct printjob *pjob)
516 TDB_DATA old_data, new_data;
518 struct tdb_print_db *pdb = get_print_db_byname(sharename);
520 int len, newlen, buflen;
528 old_data = tdb_fetch(pdb->tdb, print_key(jobid));
530 /* Doh! Now we have to pack/unpack data since the NT_DEVICEMODE was added */
537 len += tdb_pack(buf+len, buflen-len, "dddddddddffff",
539 (uint32)pjob->sysjob,
541 (uint32)pjob->starttime,
542 (uint32)pjob->status,
544 (uint32)pjob->page_count,
545 (uint32)pjob->spooled,
546 (uint32)pjob->smbjob,
552 len += pack_devicemode(pjob->nt_devmode, buf+len, buflen-len);
555 buf = (char *)SMB_REALLOC(buf, len);
557 DEBUG(0,("pjob_store: failed to enlarge buffer!\n"));
562 } while ( buflen != len );
568 new_data.dsize = len;
569 ret = (tdb_store(pdb->tdb, print_key(jobid), new_data, TDB_REPLACE) == 0);
571 release_print_db(pdb);
573 /* Send notify updates for what has changed */
576 struct printjob old_pjob;
578 if ( old_data.dsize )
580 if ( unpack_pjob( old_data.dptr, old_data.dsize, &old_pjob ) != -1 )
582 pjob_store_notify( sharename, jobid, &old_pjob , pjob );
583 free_nt_devicemode( &old_pjob.nt_devmode );
588 pjob_store_notify( sharename, jobid, NULL, pjob );
593 SAFE_FREE( old_data.dptr );
599 /****************************************************************************
600 Remove a job structure from the database.
601 ****************************************************************************/
603 void pjob_delete(const char* sharename, uint32 jobid)
605 struct printjob *pjob;
606 uint32 job_status = 0;
607 struct tdb_print_db *pdb;
609 pdb = get_print_db_byname( sharename );
614 pjob = print_job_find( sharename, jobid );
617 DEBUG(5, ("pjob_delete: we were asked to delete nonexistent job %u\n",
618 (unsigned int)jobid));
619 release_print_db(pdb);
623 /* We must cycle through JOB_STATUS_DELETING and
624 JOB_STATUS_DELETED for the port monitor to delete the job
627 job_status = JOB_STATUS_DELETING|JOB_STATUS_DELETED;
628 notify_job_status(sharename, jobid, job_status);
630 /* Remove from printing.tdb */
632 tdb_delete(pdb->tdb, print_key(jobid));
633 remove_from_jobs_changed(sharename, jobid);
634 release_print_db( pdb );
635 rap_jobid_delete(sharename, jobid);
638 /****************************************************************************
639 Parse a file name from the system spooler to generate a jobid.
640 ****************************************************************************/
642 static uint32 print_parse_jobid(char *fname)
646 if (strncmp(fname,PRINT_SPOOL_PREFIX,strlen(PRINT_SPOOL_PREFIX)) != 0)
648 fname += strlen(PRINT_SPOOL_PREFIX);
654 return (uint32)jobid;
657 /****************************************************************************
658 List a unix job in the print database.
659 ****************************************************************************/
661 static void print_unix_job(const char *sharename, print_queue_struct *q, uint32 jobid)
663 struct printjob pj, *old_pj;
665 if (jobid == (uint32)-1)
666 jobid = q->job + UNIX_JOB_START;
668 /* Preserve the timestamp on an existing unix print job */
670 old_pj = print_job_find(sharename, jobid);
677 pj.starttime = old_pj ? old_pj->starttime : q->time;
678 pj.status = q->status;
681 fstrcpy(pj.filename, old_pj ? old_pj->filename : "");
682 if (jobid < UNIX_JOB_START) {
684 fstrcpy(pj.jobname, old_pj ? old_pj->jobname : "Remote Downlevel Document");
687 fstrcpy(pj.jobname, old_pj ? old_pj->jobname : q->fs_file);
689 fstrcpy(pj.user, old_pj ? old_pj->user : q->fs_user);
690 fstrcpy(pj.queuename, old_pj ? old_pj->queuename : sharename );
692 pjob_store(sharename, jobid, &pj);
696 struct traverse_struct {
697 print_queue_struct *queue;
698 int qcount, snum, maxcount, total_jobs;
699 const char *sharename;
701 const char *lprm_command;
702 struct printif *print_if;
705 /****************************************************************************
706 Utility fn to delete any jobs that are no longer active.
707 ****************************************************************************/
709 static int traverse_fn_delete(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
711 struct traverse_struct *ts = (struct traverse_struct *)state;
712 struct printjob pjob;
716 if ( key.dsize != sizeof(jobid) )
719 jobid = IVAL(key.dptr, 0);
720 if ( unpack_pjob( data.dptr, data.dsize, &pjob ) == -1 )
722 free_nt_devicemode( &pjob.nt_devmode );
726 /* remove a unix job if it isn't in the system queue any more */
728 for (i=0;i<ts->qcount;i++) {
729 uint32 u_jobid = (ts->queue[i].job + UNIX_JOB_START);
730 if (jobid == u_jobid)
733 if (i == ts->qcount) {
734 DEBUG(10,("traverse_fn_delete: pjob %u deleted due to !smbjob\n",
735 (unsigned int)jobid ));
736 pjob_delete(ts->sharename, jobid);
740 /* need to continue the the bottom of the function to
741 save the correct attributes */
744 /* maybe it hasn't been spooled yet */
746 /* if a job is not spooled and the process doesn't
747 exist then kill it. This cleans up after smbd
749 if (!process_exists_by_pid(pjob.pid)) {
750 DEBUG(10,("traverse_fn_delete: pjob %u deleted due to !process_exists (%u)\n",
751 (unsigned int)jobid, (unsigned int)pjob.pid ));
752 pjob_delete(ts->sharename, jobid);
758 /* this check only makes sense for jobs submitted from Windows clients */
761 for (i=0;i<ts->qcount;i++) {
764 if ( pjob.status == LPQ_DELETED )
767 curr_jobid = print_parse_jobid(ts->queue[i].fs_file);
769 if (jobid == curr_jobid) {
771 /* try to clean up any jobs that need to be deleted */
773 if ( pjob.status == LPQ_DELETING ) {
776 result = (*(ts->print_if->job_delete))(
777 ts->sharename, ts->lprm_command, &pjob );
780 /* if we can't delete, then reset the job status */
781 pjob.status = LPQ_QUEUED;
782 pjob_store(ts->sharename, jobid, &pjob);
785 /* if we deleted the job, the remove the tdb record */
786 pjob_delete(ts->sharename, jobid);
787 pjob.status = LPQ_DELETED;
797 /* The job isn't in the system queue - we have to assume it has
798 completed, so delete the database entry. */
800 if (i == ts->qcount) {
802 /* A race can occur between the time a job is spooled and
803 when it appears in the lpq output. This happens when
804 the job is added to printing.tdb when another smbd
805 running print_queue_update() has completed a lpq and
806 is currently traversing the printing tdb and deleting jobs.
807 Don't delete the job if it was submitted after the lpq_time. */
809 if (pjob.starttime < ts->lpq_time) {
810 DEBUG(10,("traverse_fn_delete: pjob %u deleted due to pjob.starttime (%u) < ts->lpq_time (%u)\n",
812 (unsigned int)pjob.starttime,
813 (unsigned int)ts->lpq_time ));
814 pjob_delete(ts->sharename, jobid);
820 /* Save the pjob attributes we will store.
821 FIXME!!! This is the only place where queue->job
822 represents the SMB jobid --jerry */
824 ts->queue[i].job = jobid;
825 ts->queue[i].size = pjob.size;
826 ts->queue[i].page_count = pjob.page_count;
827 ts->queue[i].status = pjob.status;
828 ts->queue[i].priority = 1;
829 ts->queue[i].time = pjob.starttime;
830 fstrcpy(ts->queue[i].fs_user, pjob.user);
831 fstrcpy(ts->queue[i].fs_file, pjob.jobname);
838 /****************************************************************************
839 Check if the print queue has been updated recently enough.
840 ****************************************************************************/
842 static void print_cache_flush(const char *sharename)
845 struct tdb_print_db *pdb = get_print_db_byname(sharename);
849 slprintf(key, sizeof(key)-1, "CACHE/%s", sharename);
850 tdb_store_int32(pdb->tdb, key, -1);
851 release_print_db(pdb);
854 /****************************************************************************
855 Check if someone already thinks they are doing the update.
856 ****************************************************************************/
858 static pid_t get_updating_pid(const char *sharename)
863 struct tdb_print_db *pdb = get_print_db_byname(sharename);
867 slprintf(keystr, sizeof(keystr)-1, "UPDATING/%s", sharename);
869 key.dsize = strlen(keystr);
871 data = tdb_fetch(pdb->tdb, key);
872 release_print_db(pdb);
873 if (!data.dptr || data.dsize != sizeof(pid_t)) {
874 SAFE_FREE(data.dptr);
878 updating_pid = IVAL(data.dptr, 0);
879 SAFE_FREE(data.dptr);
881 if (process_exists_by_pid(updating_pid))
887 /****************************************************************************
888 Set the fact that we're doing the update, or have finished doing the update
890 ****************************************************************************/
892 static void set_updating_pid(const fstring sharename, BOOL updating)
897 pid_t updating_pid = sys_getpid();
900 struct tdb_print_db *pdb = get_print_db_byname(sharename);
905 slprintf(keystr, sizeof(keystr)-1, "UPDATING/%s", sharename);
907 key.dsize = strlen(keystr);
909 DEBUG(5, ("set_updating_pid: %s updating lpq cache for print share %s\n",
910 updating ? "" : "not ",
914 tdb_delete(pdb->tdb, key);
915 release_print_db(pdb);
919 SIVAL( buffer, 0, updating_pid);
920 data.dptr = (void *)buffer;
921 data.dsize = 4; /* we always assume this is a 4 byte value */
923 tdb_store(pdb->tdb, key, data, TDB_REPLACE);
924 release_print_db(pdb);
927 /****************************************************************************
928 Sort print jobs by submittal time.
929 ****************************************************************************/
931 static int printjob_comp(print_queue_struct *j1, print_queue_struct *j2)
942 /* Sort on job start time */
944 if (j1->time == j2->time)
946 return (j1->time > j2->time) ? 1 : -1;
949 /****************************************************************************
950 Store the sorted queue representation for later portmon retrieval.
952 ****************************************************************************/
954 static void store_queue_struct(struct tdb_print_db *pdb, struct traverse_struct *pts)
957 int max_reported_jobs = lp_max_reported_jobs(pts->snum);
958 print_queue_struct *queue = pts->queue;
963 if (max_reported_jobs && (max_reported_jobs < pts->qcount))
964 pts->qcount = max_reported_jobs;
967 /* Work out the size. */
969 data.dsize += tdb_pack(NULL, 0, "d", qcount);
971 for (i = 0; i < pts->qcount; i++) {
972 if ( queue[i].status == LPQ_DELETED )
976 data.dsize += tdb_pack(NULL, 0, "ddddddff",
977 (uint32)queue[i].job,
978 (uint32)queue[i].size,
979 (uint32)queue[i].page_count,
980 (uint32)queue[i].status,
981 (uint32)queue[i].priority,
982 (uint32)queue[i].time,
987 if ((data.dptr = SMB_MALLOC(data.dsize)) == NULL)
991 len += tdb_pack(data.dptr + len, data.dsize - len, "d", qcount);
992 for (i = 0; i < pts->qcount; i++) {
993 if ( queue[i].status == LPQ_DELETED )
996 len += tdb_pack(data.dptr + len, data.dsize - len, "ddddddff",
997 (uint32)queue[i].job,
998 (uint32)queue[i].size,
999 (uint32)queue[i].page_count,
1000 (uint32)queue[i].status,
1001 (uint32)queue[i].priority,
1002 (uint32)queue[i].time,
1007 tdb_store(pdb->tdb, string_tdb_data("INFO/linear_queue_array"), data,
1009 SAFE_FREE(data.dptr);
1013 static TDB_DATA get_jobs_changed_data(struct tdb_print_db *pdb)
1019 data = tdb_fetch(pdb->tdb, string_tdb_data("INFO/jobs_changed"));
1020 if (data.dptr == NULL || data.dsize == 0 || (data.dsize % 4 != 0)) {
1021 SAFE_FREE(data.dptr);
1028 static void check_job_changed(const char *sharename, TDB_DATA data, uint32 jobid)
1031 unsigned int job_count = data.dsize / 4;
1033 for (i = 0; i < job_count; i++) {
1036 ch_jobid = IVAL(data.dptr, i*4);
1037 if (ch_jobid == jobid)
1038 remove_from_jobs_changed(sharename, jobid);
1042 /****************************************************************************
1043 Check if the print queue has been updated recently enough.
1044 ****************************************************************************/
1046 static BOOL print_cache_expired(const char *sharename, BOOL check_pending)
1049 time_t last_qscan_time, time_now = time(NULL);
1050 struct tdb_print_db *pdb = get_print_db_byname(sharename);
1051 BOOL result = False;
1056 snprintf(key, sizeof(key), "CACHE/%s", sharename);
1057 last_qscan_time = (time_t)tdb_fetch_int32(pdb->tdb, key);
1060 * Invalidate the queue for 3 reasons.
1061 * (1). last queue scan time == -1.
1062 * (2). Current time - last queue scan time > allowed cache time.
1063 * (3). last queue scan time > current time + MAX_CACHE_VALID_TIME (1 hour by default).
1064 * This last test picks up machines for which the clock has been moved
1065 * forward, an lpq scan done and then the clock moved back. Otherwise
1066 * that last lpq scan would stay around for a loooong loooong time... :-). JRA.
1069 if (last_qscan_time == ((time_t)-1)
1070 || (time_now - last_qscan_time) >= lp_lpqcachetime()
1071 || last_qscan_time > (time_now + MAX_CACHE_VALID_TIME))
1074 time_t msg_pending_time;
1076 DEBUG(4, ("print_cache_expired: cache expired for queue %s "
1077 "(last_qscan_time = %d, time now = %d, qcachetime = %d)\n",
1078 sharename, (int)last_qscan_time, (int)time_now,
1079 (int)lp_lpqcachetime() ));
1081 /* check if another smbd has already sent a message to update the
1082 queue. Give the pending message one minute to clear and
1083 then send another message anyways. Make sure to check for
1084 clocks that have been run forward and then back again. */
1086 snprintf(key, sizeof(key), "MSG_PENDING/%s", sharename);
1089 && tdb_fetch_uint32( pdb->tdb, key, &u )
1090 && (msg_pending_time=u) > 0
1091 && msg_pending_time <= time_now
1092 && (time_now - msg_pending_time) < 60 )
1094 DEBUG(4,("print_cache_expired: message already pending for %s. Accepting cache\n",
1103 release_print_db(pdb);
1107 /****************************************************************************
1108 main work for updating the lpq cahe for a printer queue
1109 ****************************************************************************/
1111 static void print_queue_update_internal( const char *sharename,
1112 struct printif *current_printif,
1113 char *lpq_command, char *lprm_command )
1116 print_queue_struct *queue = NULL;
1117 print_status_struct status;
1118 print_status_struct old_status;
1119 struct printjob *pjob;
1120 struct traverse_struct tstruct;
1123 fstring keystr, cachestr;
1124 struct tdb_print_db *pdb = get_print_db_byname(sharename);
1130 DEBUG(5,("print_queue_update_internal: printer = %s, type = %d, lpq command = [%s]\n",
1131 sharename, current_printif->type, lpq_command));
1134 * Update the cache time FIRST ! Stops others even
1135 * attempting to get the lock and doing this
1136 * if the lpq takes a long time.
1139 slprintf(cachestr, sizeof(cachestr)-1, "CACHE/%s", sharename);
1140 tdb_store_int32(pdb->tdb, cachestr, (int)time(NULL));
1142 /* get the current queue using the appropriate interface */
1143 ZERO_STRUCT(status);
1145 qcount = (*(current_printif->queue_get))(sharename,
1146 current_printif->type,
1147 lpq_command, &queue, &status);
1149 DEBUG(3, ("print_queue_update_internal: %d job%s in queue for %s\n",
1150 qcount, (qcount != 1) ? "s" : "", sharename));
1152 /* Sort the queue by submission time otherwise they are displayed
1155 qsort(queue, qcount, sizeof(print_queue_struct),
1156 QSORT_CAST(printjob_comp));
1159 any job in the internal database that is marked as spooled
1160 and doesn't exist in the system queue is considered finished
1161 and removed from the database
1163 any job in the system database but not in the internal database
1164 is added as a unix job
1166 fill in any system job numbers as we go
1169 jcdata = get_jobs_changed_data(pdb);
1171 for (i=0; i<qcount; i++) {
1172 uint32 jobid = print_parse_jobid(queue[i].fs_file);
1174 if (jobid == (uint32)-1) {
1175 /* assume its a unix print job */
1176 print_unix_job(sharename, &queue[i], jobid);
1180 /* we have an active SMB print job - update its status */
1181 pjob = print_job_find(sharename, jobid);
1183 /* err, somethings wrong. Probably smbd was restarted
1184 with jobs in the queue. All we can do is treat them
1185 like unix jobs. Pity. */
1186 print_unix_job(sharename, &queue[i], jobid);
1190 pjob->sysjob = queue[i].job;
1192 /* don't reset the status on jobs to be deleted */
1194 if ( pjob->status != LPQ_DELETING )
1195 pjob->status = queue[i].status;
1197 pjob_store(sharename, jobid, pjob);
1199 check_job_changed(sharename, jcdata, jobid);
1202 SAFE_FREE(jcdata.dptr);
1204 /* now delete any queued entries that don't appear in the
1206 tstruct.queue = queue;
1207 tstruct.qcount = qcount;
1209 tstruct.total_jobs = 0;
1210 tstruct.lpq_time = time(NULL);
1211 tstruct.sharename = sharename;
1212 tstruct.lprm_command = lprm_command;
1213 tstruct.print_if = current_printif;
1215 tdb_traverse(pdb->tdb, traverse_fn_delete, (void *)&tstruct);
1217 /* Store the linearised queue, max jobs only. */
1218 store_queue_struct(pdb, &tstruct);
1220 SAFE_FREE(tstruct.queue);
1222 DEBUG(10,("print_queue_update_internal: printer %s INFO/total_jobs = %d\n",
1223 sharename, tstruct.total_jobs ));
1225 tdb_store_int32(pdb->tdb, "INFO/total_jobs", tstruct.total_jobs);
1227 get_queue_status(sharename, &old_status);
1228 if (old_status.qcount != qcount)
1229 DEBUG(10,("print_queue_update_internal: queue status change %d jobs -> %d jobs for printer %s\n",
1230 old_status.qcount, qcount, sharename));
1232 /* store the new queue status structure */
1233 slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", sharename);
1235 key.dsize = strlen(keystr);
1237 status.qcount = qcount;
1238 data.dptr = (void *)&status;
1239 data.dsize = sizeof(status);
1240 tdb_store(pdb->tdb, key, data, TDB_REPLACE);
1243 * Update the cache time again. We want to do this call
1244 * as little as possible...
1247 slprintf(keystr, sizeof(keystr)-1, "CACHE/%s", sharename);
1248 tdb_store_int32(pdb->tdb, keystr, (int32)time(NULL));
1250 /* clear the msg pending record for this queue */
1252 snprintf(keystr, sizeof(keystr), "MSG_PENDING/%s", sharename);
1254 if ( !tdb_store_uint32( pdb->tdb, keystr, 0 ) ) {
1255 /* log a message but continue on */
1257 DEBUG(0,("print_queue_update: failed to store MSG_PENDING flag for [%s]!\n",
1261 release_print_db( pdb );
1266 /****************************************************************************
1267 Update the internal database from the system print queue for a queue.
1268 obtain a lock on the print queue before proceeding (needed when mutiple
1269 smbd processes maytry to update the lpq cache concurrently).
1270 ****************************************************************************/
1272 static void print_queue_update_with_lock( const char *sharename,
1273 struct printif *current_printif,
1274 char *lpq_command, char *lprm_command )
1277 struct tdb_print_db *pdb;
1279 DEBUG(5,("print_queue_update_with_lock: printer share = %s\n", sharename));
1280 pdb = get_print_db_byname(sharename);
1284 if ( !print_cache_expired(sharename, False) ) {
1285 DEBUG(5,("print_queue_update_with_lock: print cache for %s is still ok\n", sharename));
1286 release_print_db(pdb);
1291 * Check to see if someone else is doing this update.
1292 * This is essentially a mutex on the update.
1295 if (get_updating_pid(sharename) != -1) {
1296 release_print_db(pdb);
1300 /* Lock the queue for the database update */
1302 slprintf(keystr, sizeof(keystr) - 1, "LOCK/%s", sharename);
1303 /* Only wait 10 seconds for this. */
1304 if (tdb_lock_bystring_with_timeout(pdb->tdb, keystr, 10) == -1) {
1305 DEBUG(0,("print_queue_update_with_lock: Failed to lock printer %s database\n", sharename));
1306 release_print_db(pdb);
1311 * Ensure that no one else got in here.
1312 * If the updating pid is still -1 then we are
1316 if (get_updating_pid(sharename) != -1) {
1318 * Someone else is doing the update, exit.
1320 tdb_unlock_bystring(pdb->tdb, keystr);
1321 release_print_db(pdb);
1326 * We're going to do the update ourselves.
1329 /* Tell others we're doing the update. */
1330 set_updating_pid(sharename, True);
1333 * Allow others to enter and notice we're doing
1337 tdb_unlock_bystring(pdb->tdb, keystr);
1339 /* do the main work now */
1341 print_queue_update_internal( sharename, current_printif,
1342 lpq_command, lprm_command );
1344 /* Delete our pid from the db. */
1345 set_updating_pid(sharename, False);
1346 release_print_db(pdb);
1349 /****************************************************************************
1350 this is the receive function of the background lpq updater
1351 ****************************************************************************/
1352 static void print_queue_receive(int msg_type, struct process_id src,
1353 void *buf, size_t msglen)
1356 pstring lpqcommand, lprmcommand;
1360 len = tdb_unpack( buf, msglen, "fdPP",
1367 DEBUG(0,("print_queue_receive: Got invalid print queue update message\n"));
1371 print_queue_update_with_lock(sharename,
1372 get_printer_fns_from_type((enum printing_types)printing_type),
1373 lpqcommand, lprmcommand );
1378 static pid_t background_lpq_updater_pid = -1;
1380 /****************************************************************************
1381 main thread of the background lpq updater
1382 ****************************************************************************/
1383 void start_background_queue(void)
1385 DEBUG(3,("start_background_queue: Starting background LPQ thread\n"));
1386 background_lpq_updater_pid = sys_fork();
1388 if (background_lpq_updater_pid == -1) {
1389 DEBUG(5,("start_background_queue: background LPQ thread failed to start. %s\n", strerror(errno) ));
1393 if(background_lpq_updater_pid == 0) {
1395 DEBUG(5,("start_background_queue: background LPQ thread started\n"));
1397 claim_connection( NULL, "smbd lpq backend", 0, False,
1398 FLAG_MSG_GENERAL|FLAG_MSG_SMBD|FLAG_MSG_PRINT_GENERAL);
1400 if (!locking_init(0)) {
1404 message_register(MSG_PRINTER_UPDATE, print_queue_receive);
1406 DEBUG(5,("start_background_queue: background LPQ thread waiting for messages\n"));
1410 /* check for some essential signals first */
1413 exit_server_cleanly(NULL);
1416 if (reload_after_sighup) {
1417 change_to_root_user();
1418 DEBUG(1,("Reloading services after SIGHUP\n"));
1419 reload_services(False);
1420 reload_after_sighup = 0;
1423 /* now check for messages */
1425 DEBUG(10,("start_background_queue: background LPQ thread got a message\n"));
1428 /* process any pending print change notify messages */
1430 print_notify_send_messages(0);
1435 /****************************************************************************
1436 update the internal database from the system print queue for a queue
1437 ****************************************************************************/
1439 static void print_queue_update(int snum, BOOL force)
1441 extern struct current_user current_user;
1442 extern userdom_struct current_user_info;
1445 pstring lpqcommand, lprmcommand;
1446 char *buffer = NULL;
1449 struct tdb_print_db *pdb;
1451 struct printif *current_printif;
1453 fstrcpy( sharename, lp_const_servicename(snum));
1455 /* don't strip out characters like '$' from the printername */
1457 pstrcpy( lpqcommand, lp_lpqcommand(snum));
1458 string_sub2( lpqcommand, "%p", PRINTERNAME(snum), sizeof(lpqcommand),
1459 False, False, False );
1460 standard_sub_advanced(lp_servicename(snum),
1461 current_user_info.unix_name, "",
1462 current_user.ut.gid,
1463 get_current_username(),
1464 current_user_info.domain,
1465 lpqcommand, sizeof(lpqcommand) );
1467 pstrcpy( lprmcommand, lp_lprmcommand(snum));
1468 string_sub2( lprmcommand, "%p", PRINTERNAME(snum), sizeof(lprmcommand),
1469 False, False, False );
1470 standard_sub_advanced(lp_servicename(snum),
1471 current_user_info.unix_name, "",
1472 current_user.ut.gid,
1473 get_current_username(),
1474 current_user_info.domain,
1475 lprmcommand, sizeof(lprmcommand) );
1478 * Make sure that the background queue process exists.
1479 * Otherwise just do the update ourselves
1482 if ( force || background_lpq_updater_pid == -1 ) {
1483 DEBUG(4,("print_queue_update: updating queue [%s] myself\n", sharename));
1484 current_printif = get_printer_fns( snum );
1485 print_queue_update_with_lock( sharename, current_printif, lpqcommand, lprmcommand );
1490 type = lp_printing(snum);
1492 /* get the length */
1494 len = tdb_pack( NULL, 0, "fdPP",
1500 buffer = SMB_XMALLOC_ARRAY( char, len );
1502 /* now pack the buffer */
1503 newlen = tdb_pack( buffer, len, "fdPP",
1509 SMB_ASSERT( newlen == len );
1511 DEBUG(10,("print_queue_update: Sending message -> printer = %s, "
1512 "type = %d, lpq command = [%s] lprm command = [%s]\n",
1513 sharename, type, lpqcommand, lprmcommand ));
1515 /* here we set a msg pending record for other smbd processes
1516 to throttle the number of duplicate print_queue_update msgs
1519 pdb = get_print_db_byname(sharename);
1525 snprintf(key, sizeof(key), "MSG_PENDING/%s", sharename);
1527 if ( !tdb_store_uint32( pdb->tdb, key, time(NULL) ) ) {
1528 /* log a message but continue on */
1530 DEBUG(0,("print_queue_update: failed to store MSG_PENDING flag for [%s]!\n",
1534 release_print_db( pdb );
1536 /* finally send the message */
1539 message_send_pid(pid_to_procid(background_lpq_updater_pid),
1540 MSG_PRINTER_UPDATE, buffer, len, False);
1543 SAFE_FREE( buffer );
1548 /****************************************************************************
1549 Create/Update an entry in the print tdb that will allow us to send notify
1550 updates only to interested smbd's.
1551 ****************************************************************************/
1553 BOOL print_notify_register_pid(int snum)
1556 struct tdb_print_db *pdb = NULL;
1557 TDB_CONTEXT *tdb = NULL;
1558 const char *printername;
1559 uint32 mypid = (uint32)sys_getpid();
1563 /* if (snum == -1), then the change notify request was
1564 on a print server handle and we need to register on
1569 int num_services = lp_numservices();
1572 for ( idx=0; idx<num_services; idx++ ) {
1573 if (lp_snum_ok(idx) && lp_print_ok(idx) )
1574 print_notify_register_pid(idx);
1579 else /* register for a specific printer */
1581 printername = lp_const_servicename(snum);
1582 pdb = get_print_db_byname(printername);
1588 if (tdb_lock_bystring_with_timeout(tdb, NOTIFY_PID_LIST_KEY, 10) == -1) {
1589 DEBUG(0,("print_notify_register_pid: Failed to lock printer %s\n",
1592 release_print_db(pdb);
1596 data = get_printer_notify_pid_list( tdb, printername, True );
1598 /* Add ourselves and increase the refcount. */
1600 for (i = 0; i < data.dsize; i += 8) {
1601 if (IVAL(data.dptr,i) == mypid) {
1602 uint32 new_refcount = IVAL(data.dptr, i+4) + 1;
1603 SIVAL(data.dptr, i+4, new_refcount);
1608 if (i == data.dsize) {
1609 /* We weren't in the list. Realloc. */
1610 data.dptr = SMB_REALLOC(data.dptr, data.dsize + 8);
1612 DEBUG(0,("print_notify_register_pid: Relloc fail for printer %s\n",
1617 SIVAL(data.dptr,data.dsize - 8,mypid);
1618 SIVAL(data.dptr,data.dsize - 4,1); /* Refcount. */
1621 /* Store back the record. */
1622 if (tdb_store_bystring(tdb, NOTIFY_PID_LIST_KEY, data, TDB_REPLACE) == -1) {
1623 DEBUG(0,("print_notify_register_pid: Failed to update pid \
1624 list for printer %s\n", printername));
1632 tdb_unlock_bystring(tdb, NOTIFY_PID_LIST_KEY);
1634 release_print_db(pdb);
1635 SAFE_FREE(data.dptr);
1639 /****************************************************************************
1640 Update an entry in the print tdb that will allow us to send notify
1641 updates only to interested smbd's.
1642 ****************************************************************************/
1644 BOOL print_notify_deregister_pid(int snum)
1647 struct tdb_print_db *pdb = NULL;
1648 TDB_CONTEXT *tdb = NULL;
1649 const char *printername;
1650 uint32 mypid = (uint32)sys_getpid();
1654 /* if ( snum == -1 ), we are deregister a print server handle
1655 which means to deregister on all print queues */
1659 int num_services = lp_numservices();
1662 for ( idx=0; idx<num_services; idx++ ) {
1663 if ( lp_snum_ok(idx) && lp_print_ok(idx) )
1664 print_notify_deregister_pid(idx);
1669 else /* deregister a specific printer */
1671 printername = lp_const_servicename(snum);
1672 pdb = get_print_db_byname(printername);
1678 if (tdb_lock_bystring_with_timeout(tdb, NOTIFY_PID_LIST_KEY, 10) == -1) {
1679 DEBUG(0,("print_notify_register_pid: Failed to lock \
1680 printer %s database\n", printername));
1682 release_print_db(pdb);
1686 data = get_printer_notify_pid_list( tdb, printername, True );
1688 /* Reduce refcount. Remove ourselves if zero. */
1690 for (i = 0; i < data.dsize; ) {
1691 if (IVAL(data.dptr,i) == mypid) {
1692 uint32 refcount = IVAL(data.dptr, i+4);
1696 if (refcount == 0) {
1697 if (data.dsize - i > 8)
1698 memmove( &data.dptr[i], &data.dptr[i+8], data.dsize - i - 8);
1702 SIVAL(data.dptr, i+4, refcount);
1708 if (data.dsize == 0)
1709 SAFE_FREE(data.dptr);
1711 /* Store back the record. */
1712 if (tdb_store_bystring(tdb, NOTIFY_PID_LIST_KEY, data, TDB_REPLACE) == -1) {
1713 DEBUG(0,("print_notify_register_pid: Failed to update pid \
1714 list for printer %s\n", printername));
1722 tdb_unlock_bystring(tdb, NOTIFY_PID_LIST_KEY);
1724 release_print_db(pdb);
1725 SAFE_FREE(data.dptr);
1729 /****************************************************************************
1730 Check if a jobid is valid. It is valid if it exists in the database.
1731 ****************************************************************************/
1733 BOOL print_job_exists(const char* sharename, uint32 jobid)
1735 struct tdb_print_db *pdb = get_print_db_byname(sharename);
1740 ret = tdb_exists(pdb->tdb, print_key(jobid));
1741 release_print_db(pdb);
1745 /****************************************************************************
1746 Give the fd used for a jobid.
1747 ****************************************************************************/
1749 int print_job_fd(const char* sharename, uint32 jobid)
1751 struct printjob *pjob = print_job_find(sharename, jobid);
1754 /* don't allow another process to get this info - it is meaningless */
1755 if (pjob->pid != sys_getpid())
1760 /****************************************************************************
1761 Give the filename used for a jobid.
1762 Only valid for the process doing the spooling and when the job
1763 has not been spooled.
1764 ****************************************************************************/
1766 char *print_job_fname(const char* sharename, uint32 jobid)
1768 struct printjob *pjob = print_job_find(sharename, jobid);
1769 if (!pjob || pjob->spooled || pjob->pid != sys_getpid())
1771 return pjob->filename;
1775 /****************************************************************************
1776 Give the filename used for a jobid.
1777 Only valid for the process doing the spooling and when the job
1778 has not been spooled.
1779 ****************************************************************************/
1781 NT_DEVICEMODE *print_job_devmode(const char* sharename, uint32 jobid)
1783 struct printjob *pjob = print_job_find(sharename, jobid);
1788 return pjob->nt_devmode;
1791 /****************************************************************************
1792 Set the place in the queue for a job.
1793 ****************************************************************************/
1795 BOOL print_job_set_place(const char *sharename, uint32 jobid, int place)
1797 DEBUG(2,("print_job_set_place not implemented yet\n"));
1801 /****************************************************************************
1802 Set the name of a job. Only possible for owner.
1803 ****************************************************************************/
1805 BOOL print_job_set_name(const char *sharename, uint32 jobid, char *name)
1807 struct printjob *pjob;
1809 pjob = print_job_find(sharename, jobid);
1810 if (!pjob || pjob->pid != sys_getpid())
1813 fstrcpy(pjob->jobname, name);
1814 return pjob_store(sharename, jobid, pjob);
1817 /***************************************************************************
1818 Remove a jobid from the 'jobs changed' list.
1819 ***************************************************************************/
1821 static BOOL remove_from_jobs_changed(const char* sharename, uint32 jobid)
1823 struct tdb_print_db *pdb = get_print_db_byname(sharename);
1825 size_t job_count, i;
1827 BOOL gotlock = False;
1835 key = string_tdb_data("INFO/jobs_changed");
1837 if (tdb_chainlock_with_timeout(pdb->tdb, key, 5) == -1)
1842 data = tdb_fetch(pdb->tdb, key);
1844 if (data.dptr == NULL || data.dsize == 0 || (data.dsize % 4 != 0))
1847 job_count = data.dsize / 4;
1848 for (i = 0; i < job_count; i++) {
1851 ch_jobid = IVAL(data.dptr, i*4);
1852 if (ch_jobid == jobid) {
1853 if (i < job_count -1 )
1854 memmove(data.dptr + (i*4), data.dptr + (i*4) + 4, (job_count - i - 1)*4 );
1856 if (tdb_store(pdb->tdb, key, data, TDB_REPLACE) == -1)
1866 tdb_chainunlock(pdb->tdb, key);
1867 SAFE_FREE(data.dptr);
1868 release_print_db(pdb);
1870 DEBUG(10,("remove_from_jobs_changed: removed jobid %u\n", (unsigned int)jobid ));
1872 DEBUG(10,("remove_from_jobs_changed: Failed to remove jobid %u\n", (unsigned int)jobid ));
1876 /****************************************************************************
1877 Delete a print job - don't update queue.
1878 ****************************************************************************/
1880 static BOOL print_job_delete1(int snum, uint32 jobid)
1882 const char* sharename = lp_const_servicename(snum);
1883 struct printjob *pjob = print_job_find(sharename, jobid);
1885 struct printif *current_printif = get_printer_fns( snum );
1891 * If already deleting just return.
1894 if (pjob->status == LPQ_DELETING)
1897 /* Hrm - we need to be able to cope with deleting a job before it
1898 has reached the spooler. Just mark it as LPQ_DELETING and
1899 let the print_queue_update() code rmeove the record */
1902 if (pjob->sysjob == -1) {
1903 DEBUG(5, ("attempt to delete job %u not seen by lpr\n", (unsigned int)jobid));
1906 /* Set the tdb entry to be deleting. */
1908 pjob->status = LPQ_DELETING;
1909 pjob_store(sharename, jobid, pjob);
1911 if (pjob->spooled && pjob->sysjob != -1)
1913 result = (*(current_printif->job_delete))(
1915 lp_lprmcommand(snum),
1918 /* Delete the tdb entry if the delete succeeded or the job hasn't
1922 struct tdb_print_db *pdb = get_print_db_byname(sharename);
1927 pjob_delete(sharename, jobid);
1928 /* Ensure we keep a rough count of the number of total jobs... */
1929 tdb_change_int32_atomic(pdb->tdb, "INFO/total_jobs", &njobs, -1);
1930 release_print_db(pdb);
1934 remove_from_jobs_changed( sharename, jobid );
1936 return (result == 0);
1939 /****************************************************************************
1940 Return true if the current user owns the print job.
1941 ****************************************************************************/
1943 static BOOL is_owner(struct current_user *user, const char *servicename,
1946 struct printjob *pjob = print_job_find(servicename, jobid);
1952 if ((vuser = get_valid_user_struct(user->vuid)) != NULL) {
1953 return strequal(pjob->user, vuser->user.smb_name);
1955 return strequal(pjob->user, uidtoname(user->ut.uid));
1959 /****************************************************************************
1961 ****************************************************************************/
1963 BOOL print_job_delete(struct current_user *user, int snum, uint32 jobid, WERROR *errcode)
1965 const char* sharename = lp_const_servicename( snum );
1966 struct printjob *pjob;
1972 owner = is_owner(user, lp_const_servicename(snum), jobid);
1974 /* Check access against security descriptor or whether the user
1978 !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
1979 DEBUG(3, ("delete denied by security descriptor\n"));
1980 *errcode = WERR_ACCESS_DENIED;
1982 /* BEGIN_ADMIN_LOG */
1983 sys_adminlog( LOG_ERR,
1984 "Permission denied-- user not allowed to delete, \
1985 pause, or resume print job. User name: %s. Printer name: %s.",
1986 uidtoname(user->ut.uid), PRINTERNAME(snum) );
1993 * get the spooled filename of the print job
1994 * if this works, then the file has not been spooled
1995 * to the underlying print system. Just delete the
1996 * spool file & return.
1999 if ( (fname = print_job_fname( sharename, jobid )) != NULL )
2001 /* remove the spool file */
2002 DEBUG(10,("print_job_delete: Removing spool file [%s]\n", fname ));
2003 if ( unlink( fname ) == -1 ) {
2004 *errcode = map_werror_from_unix(errno);
2009 if (!print_job_delete1(snum, jobid)) {
2010 *errcode = WERR_ACCESS_DENIED;
2014 /* force update the database and say the delete failed if the
2017 print_queue_update(snum, True);
2019 pjob = print_job_find(sharename, jobid);
2020 if ( pjob && (pjob->status != LPQ_DELETING) )
2021 *errcode = WERR_ACCESS_DENIED;
2023 return (pjob == NULL );
2026 /****************************************************************************
2028 ****************************************************************************/
2030 BOOL print_job_pause(struct current_user *user, int snum, uint32 jobid, WERROR *errcode)
2032 const char* sharename = lp_const_servicename(snum);
2033 struct printjob *pjob;
2035 struct printif *current_printif = get_printer_fns( snum );
2037 pjob = print_job_find(sharename, jobid);
2039 if (!pjob || !user) {
2040 DEBUG(10, ("print_job_pause: no pjob or user for jobid %u\n",
2041 (unsigned int)jobid ));
2045 if (!pjob->spooled || pjob->sysjob == -1) {
2046 DEBUG(10, ("print_job_pause: not spooled or bad sysjob = %d for jobid %u\n",
2047 (int)pjob->sysjob, (unsigned int)jobid ));
2051 if (!is_owner(user, lp_const_servicename(snum), jobid) &&
2052 !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
2053 DEBUG(3, ("pause denied by security descriptor\n"));
2055 /* BEGIN_ADMIN_LOG */
2056 sys_adminlog( LOG_ERR,
2057 "Permission denied-- user not allowed to delete, \
2058 pause, or resume print job. User name: %s. Printer name: %s.",
2059 uidtoname(user->ut.uid), PRINTERNAME(snum) );
2062 *errcode = WERR_ACCESS_DENIED;
2066 /* need to pause the spooled entry */
2067 ret = (*(current_printif->job_pause))(snum, pjob);
2070 *errcode = WERR_INVALID_PARAM;
2074 /* force update the database */
2075 print_cache_flush(lp_const_servicename(snum));
2077 /* Send a printer notify message */
2079 notify_job_status(sharename, jobid, JOB_STATUS_PAUSED);
2081 /* how do we tell if this succeeded? */
2086 /****************************************************************************
2088 ****************************************************************************/
2090 BOOL print_job_resume(struct current_user *user, int snum, uint32 jobid, WERROR *errcode)
2092 const char *sharename = lp_const_servicename(snum);
2093 struct printjob *pjob;
2095 struct printif *current_printif = get_printer_fns( snum );
2097 pjob = print_job_find(sharename, jobid);
2099 if (!pjob || !user) {
2100 DEBUG(10, ("print_job_resume: no pjob or user for jobid %u\n",
2101 (unsigned int)jobid ));
2105 if (!pjob->spooled || pjob->sysjob == -1) {
2106 DEBUG(10, ("print_job_resume: not spooled or bad sysjob = %d for jobid %u\n",
2107 (int)pjob->sysjob, (unsigned int)jobid ));
2111 if (!is_owner(user, lp_const_servicename(snum), jobid) &&
2112 !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
2113 DEBUG(3, ("resume denied by security descriptor\n"));
2114 *errcode = WERR_ACCESS_DENIED;
2116 /* BEGIN_ADMIN_LOG */
2117 sys_adminlog( LOG_ERR,
2118 "Permission denied-- user not allowed to delete, \
2119 pause, or resume print job. User name: %s. Printer name: %s.",
2120 uidtoname(user->ut.uid), PRINTERNAME(snum) );
2125 ret = (*(current_printif->job_resume))(snum, pjob);
2128 *errcode = WERR_INVALID_PARAM;
2132 /* force update the database */
2133 print_cache_flush(lp_const_servicename(snum));
2135 /* Send a printer notify message */
2137 notify_job_status(sharename, jobid, JOB_STATUS_QUEUED);
2142 /****************************************************************************
2143 Write to a print file.
2144 ****************************************************************************/
2146 ssize_t print_job_write(int snum, uint32 jobid, const char *buf, SMB_OFF_T pos, size_t size)
2148 const char* sharename = lp_const_servicename(snum);
2150 struct printjob *pjob;
2152 pjob = print_job_find(sharename, jobid);
2156 /* don't allow another process to get this info - it is meaningless */
2157 if (pjob->pid != sys_getpid())
2160 return_code = write_data_at_offset(pjob->fd, buf, size, pos);
2162 if (return_code>0) {
2164 pjob_store(sharename, jobid, pjob);
2169 /****************************************************************************
2170 Get the queue status - do not update if db is out of date.
2171 ****************************************************************************/
2173 static int get_queue_status(const char* sharename, print_status_struct *status)
2177 struct tdb_print_db *pdb = get_print_db_byname(sharename);
2181 ZERO_STRUCTP(status);
2188 fstr_sprintf(keystr, "STATUS/%s", sharename);
2189 data = tdb_fetch(pdb->tdb, string_tdb_data(keystr));
2191 if (data.dsize == sizeof(print_status_struct))
2192 /* this memcpy is ok since the status struct was
2193 not packed before storing it in the tdb */
2194 memcpy(status, data.dptr, sizeof(print_status_struct));
2195 SAFE_FREE(data.dptr);
2198 len = tdb_fetch_int32(pdb->tdb, "INFO/total_jobs");
2199 release_print_db(pdb);
2200 return (len == -1 ? 0 : len);
2203 /****************************************************************************
2204 Determine the number of jobs in a queue.
2205 ****************************************************************************/
2207 int print_queue_length(int snum, print_status_struct *pstatus)
2209 const char* sharename = lp_const_servicename( snum );
2210 print_status_struct status;
2213 ZERO_STRUCT( status );
2215 /* make sure the database is up to date */
2216 if (print_cache_expired(lp_const_servicename(snum), True))
2217 print_queue_update(snum, False);
2219 /* also fetch the queue status */
2220 memset(&status, 0, sizeof(status));
2221 len = get_queue_status(sharename, &status);
2229 /***************************************************************************
2230 Allocate a jobid. Hold the lock for as short a time as possible.
2231 ***************************************************************************/
2233 static BOOL allocate_print_jobid(struct tdb_print_db *pdb, int snum, const char *sharename, uint32 *pjobid)
2238 *pjobid = (uint32)-1;
2240 for (i = 0; i < 3; i++) {
2241 /* Lock the database - only wait 20 seconds. */
2242 if (tdb_lock_bystring_with_timeout(pdb->tdb, "INFO/nextjob", 20) == -1) {
2243 DEBUG(0,("allocate_print_jobid: failed to lock printing database %s\n", sharename));
2247 if (!tdb_fetch_uint32(pdb->tdb, "INFO/nextjob", &jobid)) {
2248 if (tdb_error(pdb->tdb) != TDB_ERR_NOEXIST) {
2249 DEBUG(0, ("allocate_print_jobid: failed to fetch INFO/nextjob for print queue %s\n",
2256 jobid = NEXT_JOBID(jobid);
2258 if (tdb_store_int32(pdb->tdb, "INFO/nextjob", jobid)==-1) {
2259 DEBUG(3, ("allocate_print_jobid: failed to store INFO/nextjob.\n"));
2260 tdb_unlock_bystring(pdb->tdb, "INFO/nextjob");
2264 /* We've finished with the INFO/nextjob lock. */
2265 tdb_unlock_bystring(pdb->tdb, "INFO/nextjob");
2267 if (!print_job_exists(sharename, jobid))
2272 DEBUG(0, ("allocate_print_jobid: failed to allocate a print job for queue %s\n",
2274 /* Probably full... */
2279 /* Store a dummy placeholder. */
2284 if (tdb_store(pdb->tdb, print_key(jobid), dum, TDB_INSERT) == -1) {
2285 DEBUG(3, ("allocate_print_jobid: jobid (%d) failed to store placeholder.\n",
2295 /***************************************************************************
2296 Append a jobid to the 'jobs changed' list.
2297 ***************************************************************************/
2299 static BOOL add_to_jobs_changed(struct tdb_print_db *pdb, uint32 jobid)
2304 SIVAL(&store_jobid, 0, jobid);
2305 data.dptr = (char *)&store_jobid;
2308 DEBUG(10,("add_to_jobs_changed: Added jobid %u\n", (unsigned int)jobid ));
2310 return (tdb_append(pdb->tdb, string_tdb_data("INFO/jobs_changed"),
2314 /***************************************************************************
2315 Start spooling a job - return the jobid.
2316 ***************************************************************************/
2318 uint32 print_job_start(struct current_user *user, int snum, char *jobname, NT_DEVICEMODE *nt_devmode )
2322 struct printjob pjob;
2324 const char *sharename = lp_const_servicename(snum);
2325 struct tdb_print_db *pdb = get_print_db_byname(sharename);
2333 if (!print_access_check(user, snum, PRINTER_ACCESS_USE)) {
2334 DEBUG(3, ("print_job_start: job start denied by security descriptor\n"));
2335 release_print_db(pdb);
2339 if (!print_time_access_check(lp_servicename(snum))) {
2340 DEBUG(3, ("print_job_start: job start denied by time check\n"));
2341 release_print_db(pdb);
2345 path = lp_pathname(snum);
2347 /* see if we have sufficient disk space */
2348 if (lp_minprintspace(snum)) {
2349 SMB_BIG_UINT dspace, dsize;
2350 if (sys_fsusage(path, &dspace, &dsize) == 0 &&
2351 dspace < 2*(SMB_BIG_UINT)lp_minprintspace(snum)) {
2352 DEBUG(3, ("print_job_start: disk space check failed.\n"));
2353 release_print_db(pdb);
2359 /* for autoloaded printers, check that the printcap entry still exists */
2360 if (lp_autoloaded(snum) && !pcap_printername_ok(lp_const_servicename(snum))) {
2361 DEBUG(3, ("print_job_start: printer name %s check failed.\n", lp_const_servicename(snum) ));
2362 release_print_db(pdb);
2367 /* Insure the maximum queue size is not violated */
2368 if ((njobs = print_queue_length(snum,NULL)) > lp_maxprintjobs(snum)) {
2369 DEBUG(3, ("print_job_start: Queue %s number of jobs (%d) larger than max printjobs per queue (%d).\n",
2370 sharename, njobs, lp_maxprintjobs(snum) ));
2371 release_print_db(pdb);
2376 DEBUG(10,("print_job_start: Queue %s number of jobs (%d), max printjobs = %d\n",
2377 sharename, njobs, lp_maxprintjobs(snum) ));
2379 if (!allocate_print_jobid(pdb, snum, sharename, &jobid))
2382 /* create the database entry */
2386 pjob.pid = sys_getpid();
2389 pjob.starttime = time(NULL);
2390 pjob.status = LPQ_SPOOLING;
2392 pjob.spooled = False;
2394 pjob.nt_devmode = nt_devmode;
2396 fstrcpy(pjob.jobname, jobname);
2398 if ((vuser = get_valid_user_struct(user->vuid)) != NULL) {
2399 fstrcpy(pjob.user, vuser->user.smb_name);
2401 fstrcpy(pjob.user, uidtoname(user->ut.uid));
2404 fstrcpy(pjob.queuename, lp_const_servicename(snum));
2406 /* we have a job entry - now create the spool file */
2407 slprintf(pjob.filename, sizeof(pjob.filename)-1, "%s/%s%.8u.XXXXXX",
2408 path, PRINT_SPOOL_PREFIX, (unsigned int)jobid);
2409 pjob.fd = smb_mkstemp(pjob.filename);
2411 if (pjob.fd == -1) {
2412 if (errno == EACCES) {
2413 /* Common setup error, force a report. */
2414 DEBUG(0, ("print_job_start: insufficient permissions \
2415 to open spool file %s.\n", pjob.filename));
2417 /* Normal case, report at level 3 and above. */
2418 DEBUG(3, ("print_job_start: can't open spool file %s,\n", pjob.filename));
2419 DEBUGADD(3, ("errno = %d (%s).\n", errno, strerror(errno)));
2424 pjob_store(sharename, jobid, &pjob);
2426 /* Update the 'jobs changed' entry used by print_queue_status. */
2427 add_to_jobs_changed(pdb, jobid);
2429 /* Ensure we keep a rough count of the number of total jobs... */
2430 tdb_change_int32_atomic(pdb->tdb, "INFO/total_jobs", &njobs, 1);
2432 release_print_db(pdb);
2438 pjob_delete(sharename, jobid);
2440 release_print_db(pdb);
2442 DEBUG(3, ("print_job_start: returning fail. Error = %s\n", strerror(errno) ));
2446 /****************************************************************************
2447 Update the number of pages spooled to jobid
2448 ****************************************************************************/
2450 void print_job_endpage(int snum, uint32 jobid)
2452 const char* sharename = lp_const_servicename(snum);
2453 struct printjob *pjob;
2455 pjob = print_job_find(sharename, jobid);
2458 /* don't allow another process to get this info - it is meaningless */
2459 if (pjob->pid != sys_getpid())
2463 pjob_store(sharename, jobid, pjob);
2466 /****************************************************************************
2467 Print a file - called on closing the file. This spools the job.
2468 If normal close is false then we're tearing down the jobs - treat as an
2470 ****************************************************************************/
2472 BOOL print_job_end(int snum, uint32 jobid, enum file_close_type close_type)
2474 const char* sharename = lp_const_servicename(snum);
2475 struct printjob *pjob;
2477 SMB_STRUCT_STAT sbuf;
2478 struct printif *current_printif = get_printer_fns( snum );
2480 pjob = print_job_find(sharename, jobid);
2485 if (pjob->spooled || pjob->pid != sys_getpid())
2488 if ((close_type == NORMAL_CLOSE || close_type == SHUTDOWN_CLOSE) &&
2489 (sys_fstat(pjob->fd, &sbuf) == 0)) {
2490 pjob->size = sbuf.st_size;
2496 * Not a normal close or we couldn't stat the job file,
2497 * so something has gone wrong. Cleanup.
2501 DEBUG(3,("print_job_end: failed to stat file for jobid %d\n", jobid ));
2505 /* Technically, this is not quite right. If the printer has a separator
2506 * page turned on, the NT spooler prints the separator page even if the
2507 * print job is 0 bytes. 010215 JRR */
2508 if (pjob->size == 0 || pjob->status == LPQ_DELETING) {
2509 /* don't bother spooling empty files or something being deleted. */
2510 DEBUG(5,("print_job_end: canceling spool of %s (%s)\n",
2511 pjob->filename, pjob->size ? "deleted" : "zero length" ));
2512 unlink(pjob->filename);
2513 pjob_delete(sharename, jobid);
2517 pjob->smbjob = jobid;
2519 ret = (*(current_printif->job_submit))(snum, pjob);
2524 /* The print job has been sucessfully handed over to the back-end */
2526 pjob->spooled = True;
2527 pjob->status = LPQ_QUEUED;
2528 pjob_store(sharename, jobid, pjob);
2530 /* make sure the database is up to date */
2531 if (print_cache_expired(lp_const_servicename(snum), True))
2532 print_queue_update(snum, False);
2538 /* The print job was not succesfully started. Cleanup */
2539 /* Still need to add proper error return propagation! 010122:JRR */
2540 unlink(pjob->filename);
2541 pjob_delete(sharename, jobid);
2545 /****************************************************************************
2546 Get a snapshot of jobs in the system without traversing.
2547 ****************************************************************************/
2549 static BOOL get_stored_queue_info(struct tdb_print_db *pdb, int snum, int *pcount, print_queue_struct **ppqueue)
2551 TDB_DATA data, cgdata;
2552 print_queue_struct *queue = NULL;
2554 uint32 extra_count = 0;
2555 int total_count = 0;
2558 int max_reported_jobs = lp_max_reported_jobs(snum);
2560 const char* sharename = lp_servicename(snum);
2562 /* make sure the database is up to date */
2563 if (print_cache_expired(lp_const_servicename(snum), True))
2564 print_queue_update(snum, False);
2570 ZERO_STRUCT(cgdata);
2572 /* Get the stored queue data. */
2573 data = tdb_fetch(pdb->tdb, string_tdb_data("INFO/linear_queue_array"));
2575 if (data.dptr && data.dsize >= sizeof(qcount))
2576 len += tdb_unpack(data.dptr + len, data.dsize - len, "d", &qcount);
2578 /* Get the changed jobs list. */
2579 cgdata = tdb_fetch(pdb->tdb, string_tdb_data("INFO/jobs_changed"));
2580 if (cgdata.dptr != NULL && (cgdata.dsize % 4 == 0))
2581 extra_count = cgdata.dsize/4;
2583 DEBUG(5,("get_stored_queue_info: qcount = %u, extra_count = %u\n", (unsigned int)qcount, (unsigned int)extra_count));
2585 /* Allocate the queue size. */
2586 if (qcount == 0 && extra_count == 0)
2589 if ((queue = SMB_MALLOC_ARRAY(print_queue_struct, qcount + extra_count)) == NULL)
2592 /* Retrieve the linearised queue data. */
2594 for( i = 0; i < qcount; i++) {
2595 uint32 qjob, qsize, qpage_count, qstatus, qpriority, qtime;
2596 len += tdb_unpack(data.dptr + len, data.dsize - len, "ddddddff",
2605 queue[i].job = qjob;
2606 queue[i].size = qsize;
2607 queue[i].page_count = qpage_count;
2608 queue[i].status = qstatus;
2609 queue[i].priority = qpriority;
2610 queue[i].time = qtime;
2613 total_count = qcount;
2615 /* Add in the changed jobids. */
2616 for( i = 0; i < extra_count; i++) {
2618 struct printjob *pjob;
2620 jobid = IVAL(cgdata.dptr, i*4);
2621 DEBUG(5,("get_stored_queue_info: changed job = %u\n", (unsigned int)jobid));
2622 pjob = print_job_find(lp_const_servicename(snum), jobid);
2624 DEBUG(5,("get_stored_queue_info: failed to find changed job = %u\n", (unsigned int)jobid));
2625 remove_from_jobs_changed(sharename, jobid);
2629 queue[total_count].job = jobid;
2630 queue[total_count].size = pjob->size;
2631 queue[total_count].page_count = pjob->page_count;
2632 queue[total_count].status = pjob->status;
2633 queue[total_count].priority = 1;
2634 queue[total_count].time = pjob->starttime;
2635 fstrcpy(queue[total_count].fs_user, pjob->user);
2636 fstrcpy(queue[total_count].fs_file, pjob->jobname);
2640 /* Sort the queue by submission time otherwise they are displayed
2643 qsort(queue, total_count, sizeof(print_queue_struct), QSORT_CAST(printjob_comp));
2645 DEBUG(5,("get_stored_queue_info: total_count = %u\n", (unsigned int)total_count));
2647 if (max_reported_jobs && total_count > max_reported_jobs)
2648 total_count = max_reported_jobs;
2651 *pcount = total_count;
2657 SAFE_FREE(data.dptr);
2658 SAFE_FREE(cgdata.dptr);
2662 /****************************************************************************
2663 Get a printer queue listing.
2664 set queue = NULL and status = NULL if you just want to update the cache
2665 ****************************************************************************/
2667 int print_queue_status(int snum,
2668 print_queue_struct **ppqueue,
2669 print_status_struct *status)
2673 const char *sharename;
2674 struct tdb_print_db *pdb;
2677 /* make sure the database is up to date */
2679 if (print_cache_expired(lp_const_servicename(snum), True))
2680 print_queue_update(snum, False);
2682 /* return if we are done */
2683 if ( !ppqueue || !status )
2687 sharename = lp_const_servicename(snum);
2688 pdb = get_print_db_byname(sharename);
2694 * Fetch the queue status. We must do this first, as there may
2695 * be no jobs in the queue.
2698 ZERO_STRUCTP(status);
2699 slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", sharename);
2701 key.dsize = strlen(keystr);
2702 data = tdb_fetch(pdb->tdb, key);
2704 if (data.dsize == sizeof(*status)) {
2705 /* this memcpy is ok since the status struct was
2706 not packed before storing it in the tdb */
2707 memcpy(status, data.dptr, sizeof(*status));
2709 SAFE_FREE(data.dptr);
2713 * Now, fetch the print queue information. We first count the number
2714 * of entries, and then only retrieve the queue if necessary.
2717 if (!get_stored_queue_info(pdb, snum, &count, ppqueue)) {
2718 release_print_db(pdb);
2722 release_print_db(pdb);
2726 /****************************************************************************
2728 ****************************************************************************/
2730 BOOL print_queue_pause(struct current_user *user, int snum, WERROR *errcode)
2733 struct printif *current_printif = get_printer_fns( snum );
2735 if (!print_access_check(user, snum, PRINTER_ACCESS_ADMINISTER)) {
2736 *errcode = WERR_ACCESS_DENIED;
2743 ret = (*(current_printif->queue_pause))(snum);
2748 *errcode = WERR_INVALID_PARAM;
2752 /* force update the database */
2753 print_cache_flush(lp_const_servicename(snum));
2755 /* Send a printer notify message */
2757 notify_printer_status(snum, PRINTER_STATUS_PAUSED);
2762 /****************************************************************************
2764 ****************************************************************************/
2766 BOOL print_queue_resume(struct current_user *user, int snum, WERROR *errcode)
2769 struct printif *current_printif = get_printer_fns( snum );
2771 if (!print_access_check(user, snum, PRINTER_ACCESS_ADMINISTER)) {
2772 *errcode = WERR_ACCESS_DENIED;
2778 ret = (*(current_printif->queue_resume))(snum);
2783 *errcode = WERR_INVALID_PARAM;
2787 /* make sure the database is up to date */
2788 if (print_cache_expired(lp_const_servicename(snum), True))
2789 print_queue_update(snum, True);
2791 /* Send a printer notify message */
2793 notify_printer_status(snum, PRINTER_STATUS_OK);
2798 /****************************************************************************
2799 Purge a queue - implemented by deleting all jobs that we can delete.
2800 ****************************************************************************/
2802 BOOL print_queue_purge(struct current_user *user, int snum, WERROR *errcode)
2804 print_queue_struct *queue;
2805 print_status_struct status;
2809 /* Force and update so the count is accurate (i.e. not a cached count) */
2810 print_queue_update(snum, True);
2812 can_job_admin = print_access_check(user, snum, JOB_ACCESS_ADMINISTER);
2813 njobs = print_queue_status(snum, &queue, &status);
2815 if ( can_job_admin )
2818 for (i=0;i<njobs;i++) {
2819 BOOL owner = is_owner(user, lp_const_servicename(snum), queue[i].job);
2821 if (owner || can_job_admin) {
2822 print_job_delete1(snum, queue[i].job);
2826 if ( can_job_admin )
2829 /* update the cache */
2830 print_queue_update( snum, True );