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 3 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, see <http://www.gnu.org/licenses/>.
25 extern SIG_ATOMIC_T got_sig_term;
26 extern SIG_ATOMIC_T reload_after_sighup;
27 extern struct current_user current_user;
28 extern userdom_struct current_user_info;
30 /* Current printer interface */
31 static bool remove_from_jobs_changed(const char* sharename, uint32 jobid);
34 the printing backend revolves around a tdb database that stores the
35 SMB view of the print queue
37 The key for this database is a jobid - a internally generated number that
38 uniquely identifies a print job
40 reading the print queue involves two steps:
41 - possibly running lpq and updating the internal database from that
42 - reading entries from the database
44 jobids are assigned when a job starts spooling.
47 static TDB_CONTEXT *rap_tdb;
48 static uint16 next_rap_jobid;
49 struct rap_jobid_key {
54 /***************************************************************************
55 Nightmare. LANMAN jobid's are 16 bit numbers..... We must map them to 32
56 bit RPC jobids.... JRA.
57 ***************************************************************************/
59 uint16 pjobid_to_rap(const char* sharename, uint32 jobid)
63 struct rap_jobid_key jinfo;
66 DEBUG(10,("pjobid_to_rap: called.\n"));
69 /* Create the in-memory tdb. */
70 rap_tdb = tdb_open_log(NULL, 0, TDB_INTERNAL, (O_RDWR|O_CREAT), 0644);
76 fstrcpy( jinfo.sharename, sharename );
78 key.dptr = (uint8 *)&jinfo;
79 key.dsize = sizeof(jinfo);
81 data = tdb_fetch(rap_tdb, key);
82 if (data.dptr && data.dsize == sizeof(uint16)) {
83 rap_jobid = SVAL(data.dptr, 0);
85 DEBUG(10,("pjobid_to_rap: jobid %u maps to RAP jobid %u\n",
86 (unsigned int)jobid, (unsigned int)rap_jobid));
90 /* Not found - create and store mapping. */
91 rap_jobid = ++next_rap_jobid;
93 rap_jobid = ++next_rap_jobid;
94 SSVAL(buf,0,rap_jobid);
96 data.dsize = sizeof(rap_jobid);
97 tdb_store(rap_tdb, key, data, TDB_REPLACE);
98 tdb_store(rap_tdb, data, key, TDB_REPLACE);
100 DEBUG(10,("pjobid_to_rap: created jobid %u maps to RAP jobid %u\n",
101 (unsigned int)jobid, (unsigned int)rap_jobid));
105 bool rap_to_pjobid(uint16 rap_jobid, fstring sharename, uint32 *pjobid)
110 DEBUG(10,("rap_to_pjobid called.\n"));
115 SSVAL(buf,0,rap_jobid);
117 key.dsize = sizeof(rap_jobid);
118 data = tdb_fetch(rap_tdb, key);
119 if ( data.dptr && data.dsize == sizeof(struct rap_jobid_key) )
121 struct rap_jobid_key *jinfo = (struct rap_jobid_key*)data.dptr;
122 fstrcpy( sharename, jinfo->sharename );
123 *pjobid = jinfo->jobid;
124 DEBUG(10,("rap_to_pjobid: jobid %u maps to RAP jobid %u\n",
125 (unsigned int)*pjobid, (unsigned int)rap_jobid));
126 SAFE_FREE(data.dptr);
130 DEBUG(10,("rap_to_pjobid: Failed to lookup RAP jobid %u\n",
131 (unsigned int)rap_jobid));
132 SAFE_FREE(data.dptr);
136 static void rap_jobid_delete(const char* sharename, uint32 jobid)
140 struct rap_jobid_key jinfo;
143 DEBUG(10,("rap_jobid_delete: called.\n"));
148 ZERO_STRUCT( jinfo );
149 fstrcpy( jinfo.sharename, sharename );
151 key.dptr = (uint8 *)&jinfo;
152 key.dsize = sizeof(jinfo);
154 data = tdb_fetch(rap_tdb, key);
155 if (!data.dptr || (data.dsize != sizeof(uint16))) {
156 DEBUG(10,("rap_jobid_delete: cannot find jobid %u\n",
157 (unsigned int)jobid ));
158 SAFE_FREE(data.dptr);
162 DEBUG(10,("rap_jobid_delete: deleting jobid %u\n",
163 (unsigned int)jobid ));
165 rap_jobid = SVAL(data.dptr, 0);
166 SAFE_FREE(data.dptr);
167 SSVAL(buf,0,rap_jobid);
169 data.dsize = sizeof(rap_jobid);
170 tdb_delete(rap_tdb, key);
171 tdb_delete(rap_tdb, data);
174 static int get_queue_status(const char* sharename, print_status_struct *);
176 /****************************************************************************
177 Initialise the printing backend. Called once at startup before the fork().
178 ****************************************************************************/
180 bool print_backend_init(struct messaging_context *msg_ctx)
182 const char *sversion = "INFO/version";
183 int services = lp_numservices();
186 unlink(lock_path("printing.tdb"));
187 mkdir(lock_path("printing"),0755);
189 /* handle a Samba upgrade */
191 for (snum = 0; snum < services; snum++) {
192 struct tdb_print_db *pdb;
193 if (!lp_print_ok(snum))
196 pdb = get_print_db_byname(lp_const_servicename(snum));
199 if (tdb_lock_bystring(pdb->tdb, sversion) == -1) {
200 DEBUG(0,("print_backend_init: Failed to open printer %s database\n", lp_const_servicename(snum) ));
201 release_print_db(pdb);
204 if (tdb_fetch_int32(pdb->tdb, sversion) != PRINT_DATABASE_VERSION) {
205 tdb_traverse(pdb->tdb, tdb_traverse_delete_fn, NULL);
206 tdb_store_int32(pdb->tdb, sversion, PRINT_DATABASE_VERSION);
208 tdb_unlock_bystring(pdb->tdb, sversion);
209 release_print_db(pdb);
212 close_all_print_db(); /* Don't leave any open. */
214 /* do NT print initialization... */
215 return nt_printing_init(msg_ctx);
218 /****************************************************************************
219 Shut down printing backend. Called once at shutdown to close the tdb.
220 ****************************************************************************/
222 void printing_end(void)
224 close_all_print_db(); /* Don't leave any open. */
227 /****************************************************************************
228 Retrieve the set of printing functions for a given service. This allows
229 us to set the printer function table based on the value of the 'printing'
232 Use the generic interface as the default and only use cups interface only
233 when asked for (and only when supported)
234 ****************************************************************************/
236 static struct printif *get_printer_fns_from_type( enum printing_types type )
238 struct printif *printer_fns = &generic_printif;
241 if ( type == PRINT_CUPS ) {
242 printer_fns = &cups_printif;
244 #endif /* HAVE_CUPS */
247 if ( type == PRINT_IPRINT ) {
248 printer_fns = &iprint_printif;
250 #endif /* HAVE_IPRINT */
252 printer_fns->type = type;
257 static struct printif *get_printer_fns( int snum )
259 return get_printer_fns_from_type( (enum printing_types)lp_printing(snum) );
263 /****************************************************************************
264 Useful function to generate a tdb key.
265 ****************************************************************************/
267 static TDB_DATA print_key(uint32 jobid)
273 ret.dptr = (uint8 *)&j;
274 ret.dsize = sizeof(j);
278 /***********************************************************************
279 unpack a pjob from a tdb buffer
280 ***********************************************************************/
282 int unpack_pjob( uint8 *buf, int buflen, struct printjob *pjob )
286 uint32 pjpid, pjsysjob, pjfd, pjstarttime, pjstatus;
287 uint32 pjsize, pjpage_count, pjspooled, pjsmbjob;
292 len += tdb_unpack(buf+len, buflen-len, "dddddddddffff",
310 if ( (used = unpack_devicemode(&pjob->nt_devmode, buf+len, buflen-len)) == -1 )
316 pjob->sysjob = pjsysjob;
318 pjob->starttime = pjstarttime;
319 pjob->status = pjstatus;
321 pjob->page_count = pjpage_count;
322 pjob->spooled = pjspooled;
323 pjob->smbjob = pjsmbjob;
329 /****************************************************************************
330 Useful function to find a print job in the database.
331 ****************************************************************************/
333 static struct printjob *print_job_find(const char *sharename, uint32 jobid)
335 static struct printjob pjob;
337 struct tdb_print_db *pdb = get_print_db_byname(sharename);
339 DEBUG(10,("print_job_find: looking up job %u for share %s\n",
340 (unsigned int)jobid, sharename ));
346 ret = tdb_fetch(pdb->tdb, print_key(jobid));
347 release_print_db(pdb);
350 DEBUG(10,("print_job_find: failed to find jobid %u.\n", (unsigned int)jobid ));
354 if ( pjob.nt_devmode ) {
355 free_nt_devicemode( &pjob.nt_devmode );
360 if ( unpack_pjob( ret.dptr, ret.dsize, &pjob ) == -1 ) {
361 DEBUG(10,("print_job_find: failed to unpack jobid %u.\n", (unsigned int)jobid ));
368 DEBUG(10,("print_job_find: returning system job %d for jobid %u.\n",
369 (int)pjob.sysjob, (unsigned int)jobid ));
374 /* Convert a unix jobid to a smb jobid */
376 static uint32 sysjob_to_jobid_value;
378 static int unixjob_traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA key,
379 TDB_DATA data, void *state)
381 struct printjob *pjob;
382 int *sysjob = (int *)state;
384 if (!data.dptr || data.dsize == 0)
387 pjob = (struct printjob *)data.dptr;
388 if (key.dsize != sizeof(uint32))
391 if (*sysjob == pjob->sysjob) {
392 uint32 jobid = IVAL(key.dptr,0);
394 sysjob_to_jobid_value = jobid;
401 /****************************************************************************
402 This is a *horribly expensive call as we have to iterate through all the
403 current printer tdb's. Don't do this often ! JRA.
404 ****************************************************************************/
406 uint32 sysjob_to_jobid(int unix_jobid)
408 int services = lp_numservices();
411 sysjob_to_jobid_value = (uint32)-1;
413 for (snum = 0; snum < services; snum++) {
414 struct tdb_print_db *pdb;
415 if (!lp_print_ok(snum))
417 pdb = get_print_db_byname(lp_const_servicename(snum));
421 tdb_traverse(pdb->tdb, unixjob_traverse_fn, &unix_jobid);
422 release_print_db(pdb);
423 if (sysjob_to_jobid_value != (uint32)-1)
424 return sysjob_to_jobid_value;
429 /****************************************************************************
430 Send notifications based on what has changed after a pjob_store.
431 ****************************************************************************/
435 uint32 spoolss_status;
436 } lpq_to_spoolss_status_map[] = {
437 { LPQ_QUEUED, JOB_STATUS_QUEUED },
438 { LPQ_PAUSED, JOB_STATUS_PAUSED },
439 { LPQ_SPOOLING, JOB_STATUS_SPOOLING },
440 { LPQ_PRINTING, JOB_STATUS_PRINTING },
441 { LPQ_DELETING, JOB_STATUS_DELETING },
442 { LPQ_OFFLINE, JOB_STATUS_OFFLINE },
443 { LPQ_PAPEROUT, JOB_STATUS_PAPEROUT },
444 { LPQ_PRINTED, JOB_STATUS_PRINTED },
445 { LPQ_DELETED, JOB_STATUS_DELETED },
446 { LPQ_BLOCKED, JOB_STATUS_BLOCKED },
447 { LPQ_USER_INTERVENTION, JOB_STATUS_USER_INTERVENTION },
451 /* Convert a lpq status value stored in printing.tdb into the
452 appropriate win32 API constant. */
454 static uint32 map_to_spoolss_status(uint32 lpq_status)
458 while (lpq_to_spoolss_status_map[i].lpq_status != -1) {
459 if (lpq_to_spoolss_status_map[i].lpq_status == lpq_status)
460 return lpq_to_spoolss_status_map[i].spoolss_status;
467 static void pjob_store_notify(const char* sharename, uint32 jobid, struct printjob *old_data,
468 struct printjob *new_data)
470 bool new_job = False;
475 /* Job attributes that can't be changed. We only send
476 notification for these on a new job. */
478 /* ACHTUNG! Due to a bug in Samba's spoolss parsing of the
479 NOTIFY_INFO_DATA buffer, we *have* to send the job submission
480 time first or else we'll end up with potential alignment
481 errors. I don't think the systemtime should be spooled as
482 a string, but this gets us around that error.
483 --jerry (i'll feel dirty for this) */
486 notify_job_submitted(sharename, jobid, new_data->starttime);
487 notify_job_username(sharename, jobid, new_data->user);
490 if (new_job || !strequal(old_data->jobname, new_data->jobname))
491 notify_job_name(sharename, jobid, new_data->jobname);
493 /* Job attributes of a new job or attributes that can be
496 if (new_job || !strequal(old_data->jobname, new_data->jobname))
497 notify_job_name(sharename, jobid, new_data->jobname);
499 if (new_job || old_data->status != new_data->status)
500 notify_job_status(sharename, jobid, map_to_spoolss_status(new_data->status));
502 if (new_job || old_data->size != new_data->size)
503 notify_job_total_bytes(sharename, jobid, new_data->size);
505 if (new_job || old_data->page_count != new_data->page_count)
506 notify_job_total_pages(sharename, jobid, new_data->page_count);
509 /****************************************************************************
510 Store a job structure back to the database.
511 ****************************************************************************/
513 static bool pjob_store(const char* sharename, uint32 jobid, struct printjob *pjob)
515 TDB_DATA old_data, new_data;
517 struct tdb_print_db *pdb = get_print_db_byname(sharename);
519 int len, newlen, buflen;
527 old_data = tdb_fetch(pdb->tdb, print_key(jobid));
529 /* Doh! Now we have to pack/unpack data since the NT_DEVICEMODE was added */
536 len += tdb_pack(buf+len, buflen-len, "dddddddddffff",
538 (uint32)pjob->sysjob,
540 (uint32)pjob->starttime,
541 (uint32)pjob->status,
543 (uint32)pjob->page_count,
544 (uint32)pjob->spooled,
545 (uint32)pjob->smbjob,
551 len += pack_devicemode(pjob->nt_devmode, buf+len, buflen-len);
554 buf = (uint8 *)SMB_REALLOC(buf, len);
556 DEBUG(0,("pjob_store: failed to enlarge buffer!\n"));
561 } while ( buflen != len );
567 new_data.dsize = len;
568 ret = (tdb_store(pdb->tdb, print_key(jobid), new_data, TDB_REPLACE) == 0);
570 release_print_db(pdb);
572 /* Send notify updates for what has changed */
575 struct printjob old_pjob;
577 if ( old_data.dsize )
579 if ( unpack_pjob( old_data.dptr, old_data.dsize, &old_pjob ) != -1 )
581 pjob_store_notify( sharename, jobid, &old_pjob , pjob );
582 free_nt_devicemode( &old_pjob.nt_devmode );
587 pjob_store_notify( sharename, jobid, NULL, pjob );
592 SAFE_FREE( old_data.dptr );
598 /****************************************************************************
599 Remove a job structure from the database.
600 ****************************************************************************/
602 void pjob_delete(const char* sharename, uint32 jobid)
604 struct printjob *pjob;
605 uint32 job_status = 0;
606 struct tdb_print_db *pdb;
608 pdb = get_print_db_byname( sharename );
613 pjob = print_job_find( sharename, jobid );
616 DEBUG(5, ("pjob_delete: we were asked to delete nonexistent job %u\n",
617 (unsigned int)jobid));
618 release_print_db(pdb);
622 /* We must cycle through JOB_STATUS_DELETING and
623 JOB_STATUS_DELETED for the port monitor to delete the job
626 job_status = JOB_STATUS_DELETING|JOB_STATUS_DELETED;
627 notify_job_status(sharename, jobid, job_status);
629 /* Remove from printing.tdb */
631 tdb_delete(pdb->tdb, print_key(jobid));
632 remove_from_jobs_changed(sharename, jobid);
633 release_print_db( pdb );
634 rap_jobid_delete(sharename, jobid);
637 /****************************************************************************
638 Parse a file name from the system spooler to generate a jobid.
639 ****************************************************************************/
641 static uint32 print_parse_jobid(char *fname)
645 if (strncmp(fname,PRINT_SPOOL_PREFIX,strlen(PRINT_SPOOL_PREFIX)) != 0)
647 fname += strlen(PRINT_SPOOL_PREFIX);
653 return (uint32)jobid;
656 /****************************************************************************
657 List a unix job in the print database.
658 ****************************************************************************/
660 static void print_unix_job(const char *sharename, print_queue_struct *q, uint32 jobid)
662 struct printjob pj, *old_pj;
664 if (jobid == (uint32)-1)
665 jobid = q->job + UNIX_JOB_START;
667 /* Preserve the timestamp on an existing unix print job */
669 old_pj = print_job_find(sharename, jobid);
676 pj.starttime = old_pj ? old_pj->starttime : q->time;
677 pj.status = q->status;
680 fstrcpy(pj.filename, old_pj ? old_pj->filename : "");
681 if (jobid < UNIX_JOB_START) {
683 fstrcpy(pj.jobname, old_pj ? old_pj->jobname : "Remote Downlevel Document");
686 fstrcpy(pj.jobname, old_pj ? old_pj->jobname : q->fs_file);
688 fstrcpy(pj.user, old_pj ? old_pj->user : q->fs_user);
689 fstrcpy(pj.queuename, old_pj ? old_pj->queuename : sharename );
691 pjob_store(sharename, jobid, &pj);
695 struct traverse_struct {
696 print_queue_struct *queue;
697 int qcount, snum, maxcount, total_jobs;
698 const char *sharename;
700 const char *lprm_command;
701 struct printif *print_if;
704 /****************************************************************************
705 Utility fn to delete any jobs that are no longer active.
706 ****************************************************************************/
708 static int traverse_fn_delete(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
710 struct traverse_struct *ts = (struct traverse_struct *)state;
711 struct printjob pjob;
715 if ( key.dsize != sizeof(jobid) )
718 jobid = IVAL(key.dptr, 0);
719 if ( unpack_pjob( data.dptr, data.dsize, &pjob ) == -1 )
721 free_nt_devicemode( &pjob.nt_devmode );
725 /* remove a unix job if it isn't in the system queue any more */
727 for (i=0;i<ts->qcount;i++) {
728 uint32 u_jobid = (ts->queue[i].job + UNIX_JOB_START);
729 if (jobid == u_jobid)
732 if (i == ts->qcount) {
733 DEBUG(10,("traverse_fn_delete: pjob %u deleted due to !smbjob\n",
734 (unsigned int)jobid ));
735 pjob_delete(ts->sharename, jobid);
739 /* need to continue the the bottom of the function to
740 save the correct attributes */
743 /* maybe it hasn't been spooled yet */
745 /* if a job is not spooled and the process doesn't
746 exist then kill it. This cleans up after smbd
748 if (!process_exists_by_pid(pjob.pid)) {
749 DEBUG(10,("traverse_fn_delete: pjob %u deleted due to !process_exists (%u)\n",
750 (unsigned int)jobid, (unsigned int)pjob.pid ));
751 pjob_delete(ts->sharename, jobid);
757 /* this check only makes sense for jobs submitted from Windows clients */
760 for (i=0;i<ts->qcount;i++) {
763 if ( pjob.status == LPQ_DELETED )
766 curr_jobid = print_parse_jobid(ts->queue[i].fs_file);
768 if (jobid == curr_jobid) {
770 /* try to clean up any jobs that need to be deleted */
772 if ( pjob.status == LPQ_DELETING ) {
775 result = (*(ts->print_if->job_delete))(
776 ts->sharename, ts->lprm_command, &pjob );
779 /* if we can't delete, then reset the job status */
780 pjob.status = LPQ_QUEUED;
781 pjob_store(ts->sharename, jobid, &pjob);
784 /* if we deleted the job, the remove the tdb record */
785 pjob_delete(ts->sharename, jobid);
786 pjob.status = LPQ_DELETED;
796 /* The job isn't in the system queue - we have to assume it has
797 completed, so delete the database entry. */
799 if (i == ts->qcount) {
801 /* A race can occur between the time a job is spooled and
802 when it appears in the lpq output. This happens when
803 the job is added to printing.tdb when another smbd
804 running print_queue_update() has completed a lpq and
805 is currently traversing the printing tdb and deleting jobs.
806 Don't delete the job if it was submitted after the lpq_time. */
808 if (pjob.starttime < ts->lpq_time) {
809 DEBUG(10,("traverse_fn_delete: pjob %u deleted due to pjob.starttime (%u) < ts->lpq_time (%u)\n",
811 (unsigned int)pjob.starttime,
812 (unsigned int)ts->lpq_time ));
813 pjob_delete(ts->sharename, jobid);
819 /* Save the pjob attributes we will store.
820 FIXME!!! This is the only place where queue->job
821 represents the SMB jobid --jerry */
823 ts->queue[i].job = jobid;
824 ts->queue[i].size = pjob.size;
825 ts->queue[i].page_count = pjob.page_count;
826 ts->queue[i].status = pjob.status;
827 ts->queue[i].priority = 1;
828 ts->queue[i].time = pjob.starttime;
829 fstrcpy(ts->queue[i].fs_user, pjob.user);
830 fstrcpy(ts->queue[i].fs_file, pjob.jobname);
837 /****************************************************************************
838 Check if the print queue has been updated recently enough.
839 ****************************************************************************/
841 static void print_cache_flush(const char *sharename)
844 struct tdb_print_db *pdb = get_print_db_byname(sharename);
848 slprintf(key, sizeof(key)-1, "CACHE/%s", sharename);
849 tdb_store_int32(pdb->tdb, key, -1);
850 release_print_db(pdb);
853 /****************************************************************************
854 Check if someone already thinks they are doing the update.
855 ****************************************************************************/
857 static pid_t get_updating_pid(const char *sharename)
862 struct tdb_print_db *pdb = get_print_db_byname(sharename);
866 slprintf(keystr, sizeof(keystr)-1, "UPDATING/%s", sharename);
867 key = string_tdb_data(keystr);
869 data = tdb_fetch(pdb->tdb, key);
870 release_print_db(pdb);
871 if (!data.dptr || data.dsize != sizeof(pid_t)) {
872 SAFE_FREE(data.dptr);
876 updating_pid = IVAL(data.dptr, 0);
877 SAFE_FREE(data.dptr);
879 if (process_exists_by_pid(updating_pid))
885 /****************************************************************************
886 Set the fact that we're doing the update, or have finished doing the update
888 ****************************************************************************/
890 static void set_updating_pid(const fstring sharename, bool updating)
895 pid_t updating_pid = sys_getpid();
898 struct tdb_print_db *pdb = get_print_db_byname(sharename);
903 slprintf(keystr, sizeof(keystr)-1, "UPDATING/%s", sharename);
904 key = string_tdb_data(keystr);
906 DEBUG(5, ("set_updating_pid: %s updating lpq cache for print share %s\n",
907 updating ? "" : "not ",
911 tdb_delete(pdb->tdb, key);
912 release_print_db(pdb);
916 SIVAL( buffer, 0, updating_pid);
918 data.dsize = 4; /* we always assume this is a 4 byte value */
920 tdb_store(pdb->tdb, key, data, TDB_REPLACE);
921 release_print_db(pdb);
924 /****************************************************************************
925 Sort print jobs by submittal time.
926 ****************************************************************************/
928 static int printjob_comp(print_queue_struct *j1, print_queue_struct *j2)
939 /* Sort on job start time */
941 if (j1->time == j2->time)
943 return (j1->time > j2->time) ? 1 : -1;
946 /****************************************************************************
947 Store the sorted queue representation for later portmon retrieval.
949 ****************************************************************************/
951 static void store_queue_struct(struct tdb_print_db *pdb, struct traverse_struct *pts)
954 int max_reported_jobs = lp_max_reported_jobs(pts->snum);
955 print_queue_struct *queue = pts->queue;
960 if (max_reported_jobs && (max_reported_jobs < pts->qcount))
961 pts->qcount = max_reported_jobs;
964 /* Work out the size. */
966 data.dsize += tdb_pack(NULL, 0, "d", qcount);
968 for (i = 0; i < pts->qcount; i++) {
969 if ( queue[i].status == LPQ_DELETED )
973 data.dsize += tdb_pack(NULL, 0, "ddddddff",
974 (uint32)queue[i].job,
975 (uint32)queue[i].size,
976 (uint32)queue[i].page_count,
977 (uint32)queue[i].status,
978 (uint32)queue[i].priority,
979 (uint32)queue[i].time,
984 if ((data.dptr = (uint8 *)SMB_MALLOC(data.dsize)) == NULL)
988 len += tdb_pack(data.dptr + len, data.dsize - len, "d", qcount);
989 for (i = 0; i < pts->qcount; i++) {
990 if ( queue[i].status == LPQ_DELETED )
993 len += tdb_pack(data.dptr + len, data.dsize - len, "ddddddff",
994 (uint32)queue[i].job,
995 (uint32)queue[i].size,
996 (uint32)queue[i].page_count,
997 (uint32)queue[i].status,
998 (uint32)queue[i].priority,
999 (uint32)queue[i].time,
1004 tdb_store(pdb->tdb, string_tdb_data("INFO/linear_queue_array"), data,
1006 SAFE_FREE(data.dptr);
1010 static TDB_DATA get_jobs_changed_data(struct tdb_print_db *pdb)
1016 data = tdb_fetch(pdb->tdb, string_tdb_data("INFO/jobs_changed"));
1017 if (data.dptr == NULL || data.dsize == 0 || (data.dsize % 4 != 0)) {
1018 SAFE_FREE(data.dptr);
1025 static void check_job_changed(const char *sharename, TDB_DATA data, uint32 jobid)
1028 unsigned int job_count = data.dsize / 4;
1030 for (i = 0; i < job_count; i++) {
1033 ch_jobid = IVAL(data.dptr, i*4);
1034 if (ch_jobid == jobid)
1035 remove_from_jobs_changed(sharename, jobid);
1039 /****************************************************************************
1040 Check if the print queue has been updated recently enough.
1041 ****************************************************************************/
1043 static bool print_cache_expired(const char *sharename, bool check_pending)
1046 time_t last_qscan_time, time_now = time(NULL);
1047 struct tdb_print_db *pdb = get_print_db_byname(sharename);
1048 bool result = False;
1053 snprintf(key, sizeof(key), "CACHE/%s", sharename);
1054 last_qscan_time = (time_t)tdb_fetch_int32(pdb->tdb, key);
1057 * Invalidate the queue for 3 reasons.
1058 * (1). last queue scan time == -1.
1059 * (2). Current time - last queue scan time > allowed cache time.
1060 * (3). last queue scan time > current time + MAX_CACHE_VALID_TIME (1 hour by default).
1061 * This last test picks up machines for which the clock has been moved
1062 * forward, an lpq scan done and then the clock moved back. Otherwise
1063 * that last lpq scan would stay around for a loooong loooong time... :-). JRA.
1066 if (last_qscan_time == ((time_t)-1)
1067 || (time_now - last_qscan_time) >= lp_lpqcachetime()
1068 || last_qscan_time > (time_now + MAX_CACHE_VALID_TIME))
1071 time_t msg_pending_time;
1073 DEBUG(4, ("print_cache_expired: cache expired for queue %s "
1074 "(last_qscan_time = %d, time now = %d, qcachetime = %d)\n",
1075 sharename, (int)last_qscan_time, (int)time_now,
1076 (int)lp_lpqcachetime() ));
1078 /* check if another smbd has already sent a message to update the
1079 queue. Give the pending message one minute to clear and
1080 then send another message anyways. Make sure to check for
1081 clocks that have been run forward and then back again. */
1083 snprintf(key, sizeof(key), "MSG_PENDING/%s", sharename);
1086 && tdb_fetch_uint32( pdb->tdb, key, &u )
1087 && (msg_pending_time=u) > 0
1088 && msg_pending_time <= time_now
1089 && (time_now - msg_pending_time) < 60 )
1091 DEBUG(4,("print_cache_expired: message already pending for %s. Accepting cache\n",
1100 release_print_db(pdb);
1104 /****************************************************************************
1105 main work for updating the lpq cahe for a printer queue
1106 ****************************************************************************/
1108 static void print_queue_update_internal( const char *sharename,
1109 struct printif *current_printif,
1110 char *lpq_command, char *lprm_command )
1113 print_queue_struct *queue = NULL;
1114 print_status_struct status;
1115 print_status_struct old_status;
1116 struct printjob *pjob;
1117 struct traverse_struct tstruct;
1120 fstring keystr, cachestr;
1121 struct tdb_print_db *pdb = get_print_db_byname(sharename);
1127 DEBUG(5,("print_queue_update_internal: printer = %s, type = %d, lpq command = [%s]\n",
1128 sharename, current_printif->type, lpq_command));
1131 * Update the cache time FIRST ! Stops others even
1132 * attempting to get the lock and doing this
1133 * if the lpq takes a long time.
1136 slprintf(cachestr, sizeof(cachestr)-1, "CACHE/%s", sharename);
1137 tdb_store_int32(pdb->tdb, cachestr, (int)time(NULL));
1139 /* get the current queue using the appropriate interface */
1140 ZERO_STRUCT(status);
1142 qcount = (*(current_printif->queue_get))(sharename,
1143 current_printif->type,
1144 lpq_command, &queue, &status);
1146 DEBUG(3, ("print_queue_update_internal: %d job%s in queue for %s\n",
1147 qcount, (qcount != 1) ? "s" : "", sharename));
1149 /* Sort the queue by submission time otherwise they are displayed
1152 qsort(queue, qcount, sizeof(print_queue_struct),
1153 QSORT_CAST(printjob_comp));
1156 any job in the internal database that is marked as spooled
1157 and doesn't exist in the system queue is considered finished
1158 and removed from the database
1160 any job in the system database but not in the internal database
1161 is added as a unix job
1163 fill in any system job numbers as we go
1166 jcdata = get_jobs_changed_data(pdb);
1168 for (i=0; i<qcount; i++) {
1169 uint32 jobid = print_parse_jobid(queue[i].fs_file);
1171 if (jobid == (uint32)-1) {
1172 /* assume its a unix print job */
1173 print_unix_job(sharename, &queue[i], jobid);
1177 /* we have an active SMB print job - update its status */
1178 pjob = print_job_find(sharename, jobid);
1180 /* err, somethings wrong. Probably smbd was restarted
1181 with jobs in the queue. All we can do is treat them
1182 like unix jobs. Pity. */
1183 print_unix_job(sharename, &queue[i], jobid);
1187 pjob->sysjob = queue[i].job;
1189 /* don't reset the status on jobs to be deleted */
1191 if ( pjob->status != LPQ_DELETING )
1192 pjob->status = queue[i].status;
1194 pjob_store(sharename, jobid, pjob);
1196 check_job_changed(sharename, jcdata, jobid);
1199 SAFE_FREE(jcdata.dptr);
1201 /* now delete any queued entries that don't appear in the
1203 tstruct.queue = queue;
1204 tstruct.qcount = qcount;
1206 tstruct.total_jobs = 0;
1207 tstruct.lpq_time = time(NULL);
1208 tstruct.sharename = sharename;
1209 tstruct.lprm_command = lprm_command;
1210 tstruct.print_if = current_printif;
1212 tdb_traverse(pdb->tdb, traverse_fn_delete, (void *)&tstruct);
1214 /* Store the linearised queue, max jobs only. */
1215 store_queue_struct(pdb, &tstruct);
1217 SAFE_FREE(tstruct.queue);
1219 DEBUG(10,("print_queue_update_internal: printer %s INFO/total_jobs = %d\n",
1220 sharename, tstruct.total_jobs ));
1222 tdb_store_int32(pdb->tdb, "INFO/total_jobs", tstruct.total_jobs);
1224 get_queue_status(sharename, &old_status);
1225 if (old_status.qcount != qcount)
1226 DEBUG(10,("print_queue_update_internal: queue status change %d jobs -> %d jobs for printer %s\n",
1227 old_status.qcount, qcount, sharename));
1229 /* store the new queue status structure */
1230 slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", sharename);
1231 key = string_tdb_data(keystr);
1233 status.qcount = qcount;
1234 data.dptr = (uint8 *)&status;
1235 data.dsize = sizeof(status);
1236 tdb_store(pdb->tdb, key, data, TDB_REPLACE);
1239 * Update the cache time again. We want to do this call
1240 * as little as possible...
1243 slprintf(keystr, sizeof(keystr)-1, "CACHE/%s", sharename);
1244 tdb_store_int32(pdb->tdb, keystr, (int32)time(NULL));
1246 /* clear the msg pending record for this queue */
1248 snprintf(keystr, sizeof(keystr), "MSG_PENDING/%s", sharename);
1250 if ( !tdb_store_uint32( pdb->tdb, keystr, 0 ) ) {
1251 /* log a message but continue on */
1253 DEBUG(0,("print_queue_update: failed to store MSG_PENDING flag for [%s]!\n",
1257 release_print_db( pdb );
1262 /****************************************************************************
1263 Update the internal database from the system print queue for a queue.
1264 obtain a lock on the print queue before proceeding (needed when mutiple
1265 smbd processes maytry to update the lpq cache concurrently).
1266 ****************************************************************************/
1268 static void print_queue_update_with_lock( const char *sharename,
1269 struct printif *current_printif,
1270 char *lpq_command, char *lprm_command )
1273 struct tdb_print_db *pdb;
1275 DEBUG(5,("print_queue_update_with_lock: printer share = %s\n", sharename));
1276 pdb = get_print_db_byname(sharename);
1280 if ( !print_cache_expired(sharename, False) ) {
1281 DEBUG(5,("print_queue_update_with_lock: print cache for %s is still ok\n", sharename));
1282 release_print_db(pdb);
1287 * Check to see if someone else is doing this update.
1288 * This is essentially a mutex on the update.
1291 if (get_updating_pid(sharename) != -1) {
1292 release_print_db(pdb);
1296 /* Lock the queue for the database update */
1298 slprintf(keystr, sizeof(keystr) - 1, "LOCK/%s", sharename);
1299 /* Only wait 10 seconds for this. */
1300 if (tdb_lock_bystring_with_timeout(pdb->tdb, keystr, 10) == -1) {
1301 DEBUG(0,("print_queue_update_with_lock: Failed to lock printer %s database\n", sharename));
1302 release_print_db(pdb);
1307 * Ensure that no one else got in here.
1308 * If the updating pid is still -1 then we are
1312 if (get_updating_pid(sharename) != -1) {
1314 * Someone else is doing the update, exit.
1316 tdb_unlock_bystring(pdb->tdb, keystr);
1317 release_print_db(pdb);
1322 * We're going to do the update ourselves.
1325 /* Tell others we're doing the update. */
1326 set_updating_pid(sharename, True);
1329 * Allow others to enter and notice we're doing
1333 tdb_unlock_bystring(pdb->tdb, keystr);
1335 /* do the main work now */
1337 print_queue_update_internal( sharename, current_printif,
1338 lpq_command, lprm_command );
1340 /* Delete our pid from the db. */
1341 set_updating_pid(sharename, False);
1342 release_print_db(pdb);
1345 /****************************************************************************
1346 this is the receive function of the background lpq updater
1347 ****************************************************************************/
1348 static void print_queue_receive(struct messaging_context *msg,
1351 struct server_id server_id,
1355 pstring lpqcommand, lprmcommand;
1359 len = tdb_unpack( (uint8 *)data->data, data->length, "fdPP",
1366 DEBUG(0,("print_queue_receive: Got invalid print queue update message\n"));
1370 print_queue_update_with_lock(sharename,
1371 get_printer_fns_from_type((enum printing_types)printing_type),
1372 lpqcommand, lprmcommand );
1377 static pid_t background_lpq_updater_pid = -1;
1379 /****************************************************************************
1380 main thread of the background lpq updater
1381 ****************************************************************************/
1382 void start_background_queue(void)
1384 DEBUG(3,("start_background_queue: Starting background LPQ thread\n"));
1385 background_lpq_updater_pid = sys_fork();
1387 if (background_lpq_updater_pid == -1) {
1388 DEBUG(5,("start_background_queue: background LPQ thread failed to start. %s\n", strerror(errno) ));
1392 if(background_lpq_updater_pid == 0) {
1394 DEBUG(5,("start_background_queue: background LPQ thread started\n"));
1396 claim_connection( NULL, "smbd lpq backend",
1397 FLAG_MSG_GENERAL|FLAG_MSG_SMBD|FLAG_MSG_PRINT_GENERAL);
1399 if (!locking_init(0)) {
1403 messaging_register(smbd_messaging_context(), NULL,
1404 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"));
1426 message_dispatch(smbd_messaging_context());
1428 /* process any pending print change notify messages */
1430 print_notify_send_messages(smbd_messaging_context(),
1436 /****************************************************************************
1437 update the internal database from the system print queue for a queue
1438 ****************************************************************************/
1440 static void print_queue_update(int snum, bool force)
1444 char *lpqcommand = NULL;
1445 char *lprmcommand = NULL;
1446 uint8 *buffer = NULL;
1449 struct tdb_print_db *pdb;
1451 struct printif *current_printif;
1452 TALLOC_CTX *ctx = talloc_tos();
1454 fstrcpy( sharename, lp_const_servicename(snum));
1456 /* don't strip out characters like '$' from the printername */
1458 lpqcommand = talloc_string_sub2(ctx,
1459 lp_lpqcommand(snum),
1462 false, false, false);
1466 lpqcommand = talloc_sub_advanced(ctx,
1467 lp_servicename(snum),
1468 current_user_info.unix_name,
1470 current_user.ut.gid,
1471 get_current_username(),
1472 current_user_info.domain,
1478 pstrcpy( lprmcommand, lp_lprmcommand(snum));
1479 lprmcommand = talloc_string_sub2(ctx,
1480 lp_lprmcommand(snum),
1483 false, false, false);
1487 lprmcommand = talloc_sub_advanced(ctx,
1488 lp_servicename(snum),
1489 current_user_info.unix_name,
1491 current_user.ut.gid,
1492 get_current_username(),
1493 current_user_info.domain,
1500 * Make sure that the background queue process exists.
1501 * Otherwise just do the update ourselves
1504 if ( force || background_lpq_updater_pid == -1 ) {
1505 DEBUG(4,("print_queue_update: updating queue [%s] myself\n", sharename));
1506 current_printif = get_printer_fns( snum );
1507 print_queue_update_with_lock( sharename, current_printif, lpqcommand, lprmcommand );
1512 type = lp_printing(snum);
1514 /* get the length */
1516 len = tdb_pack( NULL, 0, "fdPP",
1522 buffer = SMB_XMALLOC_ARRAY( uint8, len );
1524 /* now pack the buffer */
1525 newlen = tdb_pack( buffer, len, "fdPP",
1531 SMB_ASSERT( newlen == len );
1533 DEBUG(10,("print_queue_update: Sending message -> printer = %s, "
1534 "type = %d, lpq command = [%s] lprm command = [%s]\n",
1535 sharename, type, lpqcommand, lprmcommand ));
1537 /* here we set a msg pending record for other smbd processes
1538 to throttle the number of duplicate print_queue_update msgs
1541 pdb = get_print_db_byname(sharename);
1547 snprintf(key, sizeof(key), "MSG_PENDING/%s", sharename);
1549 if ( !tdb_store_uint32( pdb->tdb, key, time(NULL) ) ) {
1550 /* log a message but continue on */
1552 DEBUG(0,("print_queue_update: failed to store MSG_PENDING flag for [%s]!\n",
1556 release_print_db( pdb );
1558 /* finally send the message */
1560 messaging_send_buf(smbd_messaging_context(),
1561 pid_to_procid(background_lpq_updater_pid),
1562 MSG_PRINTER_UPDATE, (uint8 *)buffer, len);
1564 SAFE_FREE( buffer );
1569 /****************************************************************************
1570 Create/Update an entry in the print tdb that will allow us to send notify
1571 updates only to interested smbd's.
1572 ****************************************************************************/
1574 bool print_notify_register_pid(int snum)
1577 struct tdb_print_db *pdb = NULL;
1578 TDB_CONTEXT *tdb = NULL;
1579 const char *printername;
1580 uint32 mypid = (uint32)sys_getpid();
1584 /* if (snum == -1), then the change notify request was
1585 on a print server handle and we need to register on
1590 int num_services = lp_numservices();
1593 for ( idx=0; idx<num_services; idx++ ) {
1594 if (lp_snum_ok(idx) && lp_print_ok(idx) )
1595 print_notify_register_pid(idx);
1600 else /* register for a specific printer */
1602 printername = lp_const_servicename(snum);
1603 pdb = get_print_db_byname(printername);
1609 if (tdb_lock_bystring_with_timeout(tdb, NOTIFY_PID_LIST_KEY, 10) == -1) {
1610 DEBUG(0,("print_notify_register_pid: Failed to lock printer %s\n",
1613 release_print_db(pdb);
1617 data = get_printer_notify_pid_list( tdb, printername, True );
1619 /* Add ourselves and increase the refcount. */
1621 for (i = 0; i < data.dsize; i += 8) {
1622 if (IVAL(data.dptr,i) == mypid) {
1623 uint32 new_refcount = IVAL(data.dptr, i+4) + 1;
1624 SIVAL(data.dptr, i+4, new_refcount);
1629 if (i == data.dsize) {
1630 /* We weren't in the list. Realloc. */
1631 data.dptr = (uint8 *)SMB_REALLOC(data.dptr, data.dsize + 8);
1633 DEBUG(0,("print_notify_register_pid: Relloc fail for printer %s\n",
1638 SIVAL(data.dptr,data.dsize - 8,mypid);
1639 SIVAL(data.dptr,data.dsize - 4,1); /* Refcount. */
1642 /* Store back the record. */
1643 if (tdb_store_bystring(tdb, NOTIFY_PID_LIST_KEY, data, TDB_REPLACE) == -1) {
1644 DEBUG(0,("print_notify_register_pid: Failed to update pid \
1645 list for printer %s\n", printername));
1653 tdb_unlock_bystring(tdb, NOTIFY_PID_LIST_KEY);
1655 release_print_db(pdb);
1656 SAFE_FREE(data.dptr);
1660 /****************************************************************************
1661 Update an entry in the print tdb that will allow us to send notify
1662 updates only to interested smbd's.
1663 ****************************************************************************/
1665 bool print_notify_deregister_pid(int snum)
1668 struct tdb_print_db *pdb = NULL;
1669 TDB_CONTEXT *tdb = NULL;
1670 const char *printername;
1671 uint32 mypid = (uint32)sys_getpid();
1675 /* if ( snum == -1 ), we are deregister a print server handle
1676 which means to deregister on all print queues */
1680 int num_services = lp_numservices();
1683 for ( idx=0; idx<num_services; idx++ ) {
1684 if ( lp_snum_ok(idx) && lp_print_ok(idx) )
1685 print_notify_deregister_pid(idx);
1690 else /* deregister a specific printer */
1692 printername = lp_const_servicename(snum);
1693 pdb = get_print_db_byname(printername);
1699 if (tdb_lock_bystring_with_timeout(tdb, NOTIFY_PID_LIST_KEY, 10) == -1) {
1700 DEBUG(0,("print_notify_register_pid: Failed to lock \
1701 printer %s database\n", printername));
1703 release_print_db(pdb);
1707 data = get_printer_notify_pid_list( tdb, printername, True );
1709 /* Reduce refcount. Remove ourselves if zero. */
1711 for (i = 0; i < data.dsize; ) {
1712 if (IVAL(data.dptr,i) == mypid) {
1713 uint32 refcount = IVAL(data.dptr, i+4);
1717 if (refcount == 0) {
1718 if (data.dsize - i > 8)
1719 memmove( &data.dptr[i], &data.dptr[i+8], data.dsize - i - 8);
1723 SIVAL(data.dptr, i+4, refcount);
1729 if (data.dsize == 0)
1730 SAFE_FREE(data.dptr);
1732 /* Store back the record. */
1733 if (tdb_store_bystring(tdb, NOTIFY_PID_LIST_KEY, data, TDB_REPLACE) == -1) {
1734 DEBUG(0,("print_notify_register_pid: Failed to update pid \
1735 list for printer %s\n", printername));
1743 tdb_unlock_bystring(tdb, NOTIFY_PID_LIST_KEY);
1745 release_print_db(pdb);
1746 SAFE_FREE(data.dptr);
1750 /****************************************************************************
1751 Check if a jobid is valid. It is valid if it exists in the database.
1752 ****************************************************************************/
1754 bool print_job_exists(const char* sharename, uint32 jobid)
1756 struct tdb_print_db *pdb = get_print_db_byname(sharename);
1761 ret = tdb_exists(pdb->tdb, print_key(jobid));
1762 release_print_db(pdb);
1766 /****************************************************************************
1767 Give the fd used for a jobid.
1768 ****************************************************************************/
1770 int print_job_fd(const char* sharename, uint32 jobid)
1772 struct printjob *pjob = print_job_find(sharename, jobid);
1775 /* don't allow another process to get this info - it is meaningless */
1776 if (pjob->pid != sys_getpid())
1781 /****************************************************************************
1782 Give the filename used for a jobid.
1783 Only valid for the process doing the spooling and when the job
1784 has not been spooled.
1785 ****************************************************************************/
1787 char *print_job_fname(const char* sharename, uint32 jobid)
1789 struct printjob *pjob = print_job_find(sharename, jobid);
1790 if (!pjob || pjob->spooled || pjob->pid != sys_getpid())
1792 return pjob->filename;
1796 /****************************************************************************
1797 Give the filename used for a jobid.
1798 Only valid for the process doing the spooling and when the job
1799 has not been spooled.
1800 ****************************************************************************/
1802 NT_DEVICEMODE *print_job_devmode(const char* sharename, uint32 jobid)
1804 struct printjob *pjob = print_job_find(sharename, jobid);
1809 return pjob->nt_devmode;
1812 /****************************************************************************
1813 Set the place in the queue for a job.
1814 ****************************************************************************/
1816 bool print_job_set_place(const char *sharename, uint32 jobid, int place)
1818 DEBUG(2,("print_job_set_place not implemented yet\n"));
1822 /****************************************************************************
1823 Set the name of a job. Only possible for owner.
1824 ****************************************************************************/
1826 bool print_job_set_name(const char *sharename, uint32 jobid, char *name)
1828 struct printjob *pjob;
1830 pjob = print_job_find(sharename, jobid);
1831 if (!pjob || pjob->pid != sys_getpid())
1834 fstrcpy(pjob->jobname, name);
1835 return pjob_store(sharename, jobid, pjob);
1838 /***************************************************************************
1839 Remove a jobid from the 'jobs changed' list.
1840 ***************************************************************************/
1842 static bool remove_from_jobs_changed(const char* sharename, uint32 jobid)
1844 struct tdb_print_db *pdb = get_print_db_byname(sharename);
1846 size_t job_count, i;
1848 bool gotlock = False;
1856 key = string_tdb_data("INFO/jobs_changed");
1858 if (tdb_chainlock_with_timeout(pdb->tdb, key, 5) == -1)
1863 data = tdb_fetch(pdb->tdb, key);
1865 if (data.dptr == NULL || data.dsize == 0 || (data.dsize % 4 != 0))
1868 job_count = data.dsize / 4;
1869 for (i = 0; i < job_count; i++) {
1872 ch_jobid = IVAL(data.dptr, i*4);
1873 if (ch_jobid == jobid) {
1874 if (i < job_count -1 )
1875 memmove(data.dptr + (i*4), data.dptr + (i*4) + 4, (job_count - i - 1)*4 );
1877 if (tdb_store(pdb->tdb, key, data, TDB_REPLACE) == -1)
1887 tdb_chainunlock(pdb->tdb, key);
1888 SAFE_FREE(data.dptr);
1889 release_print_db(pdb);
1891 DEBUG(10,("remove_from_jobs_changed: removed jobid %u\n", (unsigned int)jobid ));
1893 DEBUG(10,("remove_from_jobs_changed: Failed to remove jobid %u\n", (unsigned int)jobid ));
1897 /****************************************************************************
1898 Delete a print job - don't update queue.
1899 ****************************************************************************/
1901 static bool print_job_delete1(int snum, uint32 jobid)
1903 const char* sharename = lp_const_servicename(snum);
1904 struct printjob *pjob = print_job_find(sharename, jobid);
1906 struct printif *current_printif = get_printer_fns( snum );
1912 * If already deleting just return.
1915 if (pjob->status == LPQ_DELETING)
1918 /* Hrm - we need to be able to cope with deleting a job before it
1919 has reached the spooler. Just mark it as LPQ_DELETING and
1920 let the print_queue_update() code rmeove the record */
1923 if (pjob->sysjob == -1) {
1924 DEBUG(5, ("attempt to delete job %u not seen by lpr\n", (unsigned int)jobid));
1927 /* Set the tdb entry to be deleting. */
1929 pjob->status = LPQ_DELETING;
1930 pjob_store(sharename, jobid, pjob);
1932 if (pjob->spooled && pjob->sysjob != -1)
1934 result = (*(current_printif->job_delete))(
1936 lp_lprmcommand(snum),
1939 /* Delete the tdb entry if the delete succeeded or the job hasn't
1943 struct tdb_print_db *pdb = get_print_db_byname(sharename);
1948 pjob_delete(sharename, jobid);
1949 /* Ensure we keep a rough count of the number of total jobs... */
1950 tdb_change_int32_atomic(pdb->tdb, "INFO/total_jobs", &njobs, -1);
1951 release_print_db(pdb);
1955 remove_from_jobs_changed( sharename, jobid );
1957 return (result == 0);
1960 /****************************************************************************
1961 Return true if the current user owns the print job.
1962 ****************************************************************************/
1964 static bool is_owner(struct current_user *user, const char *servicename,
1967 struct printjob *pjob = print_job_find(servicename, jobid);
1973 if ((vuser = get_valid_user_struct(user->vuid)) != NULL) {
1974 return strequal(pjob->user, vuser->user.smb_name);
1976 return strequal(pjob->user, uidtoname(user->ut.uid));
1980 /****************************************************************************
1982 ****************************************************************************/
1984 bool print_job_delete(struct current_user *user, int snum, uint32 jobid, WERROR *errcode)
1986 const char* sharename = lp_const_servicename( snum );
1987 struct printjob *pjob;
1993 owner = is_owner(user, lp_const_servicename(snum), jobid);
1995 /* Check access against security descriptor or whether the user
1999 !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
2000 DEBUG(3, ("delete denied by security descriptor\n"));
2001 *errcode = WERR_ACCESS_DENIED;
2003 /* BEGIN_ADMIN_LOG */
2004 sys_adminlog( LOG_ERR,
2005 "Permission denied-- user not allowed to delete, \
2006 pause, or resume print job. User name: %s. Printer name: %s.",
2007 uidtoname(user->ut.uid), PRINTERNAME(snum) );
2014 * get the spooled filename of the print job
2015 * if this works, then the file has not been spooled
2016 * to the underlying print system. Just delete the
2017 * spool file & return.
2020 if ( (fname = print_job_fname( sharename, jobid )) != NULL )
2022 /* remove the spool file */
2023 DEBUG(10,("print_job_delete: Removing spool file [%s]\n", fname ));
2024 if ( unlink( fname ) == -1 ) {
2025 *errcode = map_werror_from_unix(errno);
2030 if (!print_job_delete1(snum, jobid)) {
2031 *errcode = WERR_ACCESS_DENIED;
2035 /* force update the database and say the delete failed if the
2038 print_queue_update(snum, True);
2040 pjob = print_job_find(sharename, jobid);
2041 if ( pjob && (pjob->status != LPQ_DELETING) )
2042 *errcode = WERR_ACCESS_DENIED;
2044 return (pjob == NULL );
2047 /****************************************************************************
2049 ****************************************************************************/
2051 bool print_job_pause(struct current_user *user, int snum, uint32 jobid, WERROR *errcode)
2053 const char* sharename = lp_const_servicename(snum);
2054 struct printjob *pjob;
2056 struct printif *current_printif = get_printer_fns( snum );
2058 pjob = print_job_find(sharename, jobid);
2060 if (!pjob || !user) {
2061 DEBUG(10, ("print_job_pause: no pjob or user for jobid %u\n",
2062 (unsigned int)jobid ));
2066 if (!pjob->spooled || pjob->sysjob == -1) {
2067 DEBUG(10, ("print_job_pause: not spooled or bad sysjob = %d for jobid %u\n",
2068 (int)pjob->sysjob, (unsigned int)jobid ));
2072 if (!is_owner(user, lp_const_servicename(snum), jobid) &&
2073 !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
2074 DEBUG(3, ("pause denied by security descriptor\n"));
2076 /* BEGIN_ADMIN_LOG */
2077 sys_adminlog( LOG_ERR,
2078 "Permission denied-- user not allowed to delete, \
2079 pause, or resume print job. User name: %s. Printer name: %s.",
2080 uidtoname(user->ut.uid), PRINTERNAME(snum) );
2083 *errcode = WERR_ACCESS_DENIED;
2087 /* need to pause the spooled entry */
2088 ret = (*(current_printif->job_pause))(snum, pjob);
2091 *errcode = WERR_INVALID_PARAM;
2095 /* force update the database */
2096 print_cache_flush(lp_const_servicename(snum));
2098 /* Send a printer notify message */
2100 notify_job_status(sharename, jobid, JOB_STATUS_PAUSED);
2102 /* how do we tell if this succeeded? */
2107 /****************************************************************************
2109 ****************************************************************************/
2111 bool print_job_resume(struct current_user *user, int snum, uint32 jobid, WERROR *errcode)
2113 const char *sharename = lp_const_servicename(snum);
2114 struct printjob *pjob;
2116 struct printif *current_printif = get_printer_fns( snum );
2118 pjob = print_job_find(sharename, jobid);
2120 if (!pjob || !user) {
2121 DEBUG(10, ("print_job_resume: no pjob or user for jobid %u\n",
2122 (unsigned int)jobid ));
2126 if (!pjob->spooled || pjob->sysjob == -1) {
2127 DEBUG(10, ("print_job_resume: not spooled or bad sysjob = %d for jobid %u\n",
2128 (int)pjob->sysjob, (unsigned int)jobid ));
2132 if (!is_owner(user, lp_const_servicename(snum), jobid) &&
2133 !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
2134 DEBUG(3, ("resume denied by security descriptor\n"));
2135 *errcode = WERR_ACCESS_DENIED;
2137 /* BEGIN_ADMIN_LOG */
2138 sys_adminlog( LOG_ERR,
2139 "Permission denied-- user not allowed to delete, \
2140 pause, or resume print job. User name: %s. Printer name: %s.",
2141 uidtoname(user->ut.uid), PRINTERNAME(snum) );
2146 ret = (*(current_printif->job_resume))(snum, pjob);
2149 *errcode = WERR_INVALID_PARAM;
2153 /* force update the database */
2154 print_cache_flush(lp_const_servicename(snum));
2156 /* Send a printer notify message */
2158 notify_job_status(sharename, jobid, JOB_STATUS_QUEUED);
2163 /****************************************************************************
2164 Write to a print file.
2165 ****************************************************************************/
2167 ssize_t print_job_write(int snum, uint32 jobid, const char *buf, SMB_OFF_T pos, size_t size)
2169 const char* sharename = lp_const_servicename(snum);
2171 struct printjob *pjob;
2173 pjob = print_job_find(sharename, jobid);
2177 /* don't allow another process to get this info - it is meaningless */
2178 if (pjob->pid != sys_getpid())
2181 return_code = write_data_at_offset(pjob->fd, buf, size, pos);
2183 if (return_code>0) {
2185 pjob_store(sharename, jobid, pjob);
2190 /****************************************************************************
2191 Get the queue status - do not update if db is out of date.
2192 ****************************************************************************/
2194 static int get_queue_status(const char* sharename, print_status_struct *status)
2198 struct tdb_print_db *pdb = get_print_db_byname(sharename);
2202 ZERO_STRUCTP(status);
2209 fstr_sprintf(keystr, "STATUS/%s", sharename);
2210 data = tdb_fetch(pdb->tdb, string_tdb_data(keystr));
2212 if (data.dsize == sizeof(print_status_struct))
2213 /* this memcpy is ok since the status struct was
2214 not packed before storing it in the tdb */
2215 memcpy(status, data.dptr, sizeof(print_status_struct));
2216 SAFE_FREE(data.dptr);
2219 len = tdb_fetch_int32(pdb->tdb, "INFO/total_jobs");
2220 release_print_db(pdb);
2221 return (len == -1 ? 0 : len);
2224 /****************************************************************************
2225 Determine the number of jobs in a queue.
2226 ****************************************************************************/
2228 int print_queue_length(int snum, print_status_struct *pstatus)
2230 const char* sharename = lp_const_servicename( snum );
2231 print_status_struct status;
2234 ZERO_STRUCT( status );
2236 /* make sure the database is up to date */
2237 if (print_cache_expired(lp_const_servicename(snum), True))
2238 print_queue_update(snum, False);
2240 /* also fetch the queue status */
2241 memset(&status, 0, sizeof(status));
2242 len = get_queue_status(sharename, &status);
2250 /***************************************************************************
2251 Allocate a jobid. Hold the lock for as short a time as possible.
2252 ***************************************************************************/
2254 static bool allocate_print_jobid(struct tdb_print_db *pdb, int snum, const char *sharename, uint32 *pjobid)
2259 *pjobid = (uint32)-1;
2261 for (i = 0; i < 3; i++) {
2262 /* Lock the database - only wait 20 seconds. */
2263 if (tdb_lock_bystring_with_timeout(pdb->tdb, "INFO/nextjob", 20) == -1) {
2264 DEBUG(0,("allocate_print_jobid: failed to lock printing database %s\n", sharename));
2268 if (!tdb_fetch_uint32(pdb->tdb, "INFO/nextjob", &jobid)) {
2269 if (tdb_error(pdb->tdb) != TDB_ERR_NOEXIST) {
2270 DEBUG(0, ("allocate_print_jobid: failed to fetch INFO/nextjob for print queue %s\n",
2277 jobid = NEXT_JOBID(jobid);
2279 if (tdb_store_int32(pdb->tdb, "INFO/nextjob", jobid)==-1) {
2280 DEBUG(3, ("allocate_print_jobid: failed to store INFO/nextjob.\n"));
2281 tdb_unlock_bystring(pdb->tdb, "INFO/nextjob");
2285 /* We've finished with the INFO/nextjob lock. */
2286 tdb_unlock_bystring(pdb->tdb, "INFO/nextjob");
2288 if (!print_job_exists(sharename, jobid))
2293 DEBUG(0, ("allocate_print_jobid: failed to allocate a print job for queue %s\n",
2295 /* Probably full... */
2300 /* Store a dummy placeholder. */
2305 if (tdb_store(pdb->tdb, print_key(jobid), dum, TDB_INSERT) == -1) {
2306 DEBUG(3, ("allocate_print_jobid: jobid (%d) failed to store placeholder.\n",
2316 /***************************************************************************
2317 Append a jobid to the 'jobs changed' list.
2318 ***************************************************************************/
2320 static bool add_to_jobs_changed(struct tdb_print_db *pdb, uint32 jobid)
2325 SIVAL(&store_jobid, 0, jobid);
2326 data.dptr = (uint8 *)&store_jobid;
2329 DEBUG(10,("add_to_jobs_changed: Added jobid %u\n", (unsigned int)jobid ));
2331 return (tdb_append(pdb->tdb, string_tdb_data("INFO/jobs_changed"),
2335 /***************************************************************************
2336 Start spooling a job - return the jobid.
2337 ***************************************************************************/
2339 uint32 print_job_start(struct current_user *user, int snum, char *jobname, NT_DEVICEMODE *nt_devmode )
2343 struct printjob pjob;
2345 const char *sharename = lp_const_servicename(snum);
2346 struct tdb_print_db *pdb = get_print_db_byname(sharename);
2354 if (!print_access_check(user, snum, PRINTER_ACCESS_USE)) {
2355 DEBUG(3, ("print_job_start: job start denied by security descriptor\n"));
2356 release_print_db(pdb);
2360 if (!print_time_access_check(lp_servicename(snum))) {
2361 DEBUG(3, ("print_job_start: job start denied by time check\n"));
2362 release_print_db(pdb);
2366 path = lp_pathname(snum);
2368 /* see if we have sufficient disk space */
2369 if (lp_minprintspace(snum)) {
2370 SMB_BIG_UINT dspace, dsize;
2371 if (sys_fsusage(path, &dspace, &dsize) == 0 &&
2372 dspace < 2*(SMB_BIG_UINT)lp_minprintspace(snum)) {
2373 DEBUG(3, ("print_job_start: disk space check failed.\n"));
2374 release_print_db(pdb);
2380 /* for autoloaded printers, check that the printcap entry still exists */
2381 if (lp_autoloaded(snum) && !pcap_printername_ok(lp_const_servicename(snum))) {
2382 DEBUG(3, ("print_job_start: printer name %s check failed.\n", lp_const_servicename(snum) ));
2383 release_print_db(pdb);
2388 /* Insure the maximum queue size is not violated */
2389 if ((njobs = print_queue_length(snum,NULL)) > lp_maxprintjobs(snum)) {
2390 DEBUG(3, ("print_job_start: Queue %s number of jobs (%d) larger than max printjobs per queue (%d).\n",
2391 sharename, njobs, lp_maxprintjobs(snum) ));
2392 release_print_db(pdb);
2397 DEBUG(10,("print_job_start: Queue %s number of jobs (%d), max printjobs = %d\n",
2398 sharename, njobs, lp_maxprintjobs(snum) ));
2400 if (!allocate_print_jobid(pdb, snum, sharename, &jobid))
2403 /* create the database entry */
2407 pjob.pid = sys_getpid();
2410 pjob.starttime = time(NULL);
2411 pjob.status = LPQ_SPOOLING;
2413 pjob.spooled = False;
2415 pjob.nt_devmode = nt_devmode;
2417 fstrcpy(pjob.jobname, jobname);
2419 if ((vuser = get_valid_user_struct(user->vuid)) != NULL) {
2420 fstrcpy(pjob.user, lp_printjob_username(snum));
2421 standard_sub_basic(vuser->user.smb_name, vuser->user.domain,
2422 pjob.user, sizeof(pjob.user)-1);
2423 /* ensure NULL termination */
2424 pjob.user[sizeof(pjob.user)-1] = '\0';
2426 fstrcpy(pjob.user, uidtoname(user->ut.uid));
2429 fstrcpy(pjob.queuename, lp_const_servicename(snum));
2431 /* we have a job entry - now create the spool file */
2432 slprintf(pjob.filename, sizeof(pjob.filename)-1, "%s/%s%.8u.XXXXXX",
2433 path, PRINT_SPOOL_PREFIX, (unsigned int)jobid);
2434 pjob.fd = smb_mkstemp(pjob.filename);
2436 if (pjob.fd == -1) {
2437 if (errno == EACCES) {
2438 /* Common setup error, force a report. */
2439 DEBUG(0, ("print_job_start: insufficient permissions \
2440 to open spool file %s.\n", pjob.filename));
2442 /* Normal case, report at level 3 and above. */
2443 DEBUG(3, ("print_job_start: can't open spool file %s,\n", pjob.filename));
2444 DEBUGADD(3, ("errno = %d (%s).\n", errno, strerror(errno)));
2449 pjob_store(sharename, jobid, &pjob);
2451 /* Update the 'jobs changed' entry used by print_queue_status. */
2452 add_to_jobs_changed(pdb, jobid);
2454 /* Ensure we keep a rough count of the number of total jobs... */
2455 tdb_change_int32_atomic(pdb->tdb, "INFO/total_jobs", &njobs, 1);
2457 release_print_db(pdb);
2463 pjob_delete(sharename, jobid);
2465 release_print_db(pdb);
2467 DEBUG(3, ("print_job_start: returning fail. Error = %s\n", strerror(errno) ));
2471 /****************************************************************************
2472 Update the number of pages spooled to jobid
2473 ****************************************************************************/
2475 void print_job_endpage(int snum, uint32 jobid)
2477 const char* sharename = lp_const_servicename(snum);
2478 struct printjob *pjob;
2480 pjob = print_job_find(sharename, jobid);
2483 /* don't allow another process to get this info - it is meaningless */
2484 if (pjob->pid != sys_getpid())
2488 pjob_store(sharename, jobid, pjob);
2491 /****************************************************************************
2492 Print a file - called on closing the file. This spools the job.
2493 If normal close is false then we're tearing down the jobs - treat as an
2495 ****************************************************************************/
2497 bool print_job_end(int snum, uint32 jobid, enum file_close_type close_type)
2499 const char* sharename = lp_const_servicename(snum);
2500 struct printjob *pjob;
2502 SMB_STRUCT_STAT sbuf;
2503 struct printif *current_printif = get_printer_fns( snum );
2505 pjob = print_job_find(sharename, jobid);
2510 if (pjob->spooled || pjob->pid != sys_getpid())
2513 if ((close_type == NORMAL_CLOSE || close_type == SHUTDOWN_CLOSE) &&
2514 (sys_fstat(pjob->fd, &sbuf) == 0)) {
2515 pjob->size = sbuf.st_size;
2521 * Not a normal close or we couldn't stat the job file,
2522 * so something has gone wrong. Cleanup.
2526 DEBUG(3,("print_job_end: failed to stat file for jobid %d\n", jobid ));
2530 /* Technically, this is not quite right. If the printer has a separator
2531 * page turned on, the NT spooler prints the separator page even if the
2532 * print job is 0 bytes. 010215 JRR */
2533 if (pjob->size == 0 || pjob->status == LPQ_DELETING) {
2534 /* don't bother spooling empty files or something being deleted. */
2535 DEBUG(5,("print_job_end: canceling spool of %s (%s)\n",
2536 pjob->filename, pjob->size ? "deleted" : "zero length" ));
2537 unlink(pjob->filename);
2538 pjob_delete(sharename, jobid);
2542 pjob->smbjob = jobid;
2544 ret = (*(current_printif->job_submit))(snum, pjob);
2549 /* The print job has been sucessfully handed over to the back-end */
2551 pjob->spooled = True;
2552 pjob->status = LPQ_QUEUED;
2553 pjob_store(sharename, jobid, pjob);
2555 /* make sure the database is up to date */
2556 if (print_cache_expired(lp_const_servicename(snum), True))
2557 print_queue_update(snum, False);
2563 /* The print job was not succesfully started. Cleanup */
2564 /* Still need to add proper error return propagation! 010122:JRR */
2565 unlink(pjob->filename);
2566 pjob_delete(sharename, jobid);
2570 /****************************************************************************
2571 Get a snapshot of jobs in the system without traversing.
2572 ****************************************************************************/
2574 static bool get_stored_queue_info(struct tdb_print_db *pdb, int snum, int *pcount, print_queue_struct **ppqueue)
2576 TDB_DATA data, cgdata;
2577 print_queue_struct *queue = NULL;
2579 uint32 extra_count = 0;
2580 int total_count = 0;
2583 int max_reported_jobs = lp_max_reported_jobs(snum);
2585 const char* sharename = lp_servicename(snum);
2587 /* make sure the database is up to date */
2588 if (print_cache_expired(lp_const_servicename(snum), True))
2589 print_queue_update(snum, False);
2595 ZERO_STRUCT(cgdata);
2597 /* Get the stored queue data. */
2598 data = tdb_fetch(pdb->tdb, string_tdb_data("INFO/linear_queue_array"));
2600 if (data.dptr && data.dsize >= sizeof(qcount))
2601 len += tdb_unpack(data.dptr + len, data.dsize - len, "d", &qcount);
2603 /* Get the changed jobs list. */
2604 cgdata = tdb_fetch(pdb->tdb, string_tdb_data("INFO/jobs_changed"));
2605 if (cgdata.dptr != NULL && (cgdata.dsize % 4 == 0))
2606 extra_count = cgdata.dsize/4;
2608 DEBUG(5,("get_stored_queue_info: qcount = %u, extra_count = %u\n", (unsigned int)qcount, (unsigned int)extra_count));
2610 /* Allocate the queue size. */
2611 if (qcount == 0 && extra_count == 0)
2614 if ((queue = SMB_MALLOC_ARRAY(print_queue_struct, qcount + extra_count)) == NULL)
2617 /* Retrieve the linearised queue data. */
2619 for( i = 0; i < qcount; i++) {
2620 uint32 qjob, qsize, qpage_count, qstatus, qpriority, qtime;
2621 len += tdb_unpack(data.dptr + len, data.dsize - len, "ddddddff",
2630 queue[i].job = qjob;
2631 queue[i].size = qsize;
2632 queue[i].page_count = qpage_count;
2633 queue[i].status = qstatus;
2634 queue[i].priority = qpriority;
2635 queue[i].time = qtime;
2638 total_count = qcount;
2640 /* Add in the changed jobids. */
2641 for( i = 0; i < extra_count; i++) {
2643 struct printjob *pjob;
2645 jobid = IVAL(cgdata.dptr, i*4);
2646 DEBUG(5,("get_stored_queue_info: changed job = %u\n", (unsigned int)jobid));
2647 pjob = print_job_find(lp_const_servicename(snum), jobid);
2649 DEBUG(5,("get_stored_queue_info: failed to find changed job = %u\n", (unsigned int)jobid));
2650 remove_from_jobs_changed(sharename, jobid);
2654 queue[total_count].job = jobid;
2655 queue[total_count].size = pjob->size;
2656 queue[total_count].page_count = pjob->page_count;
2657 queue[total_count].status = pjob->status;
2658 queue[total_count].priority = 1;
2659 queue[total_count].time = pjob->starttime;
2660 fstrcpy(queue[total_count].fs_user, pjob->user);
2661 fstrcpy(queue[total_count].fs_file, pjob->jobname);
2665 /* Sort the queue by submission time otherwise they are displayed
2668 qsort(queue, total_count, sizeof(print_queue_struct), QSORT_CAST(printjob_comp));
2670 DEBUG(5,("get_stored_queue_info: total_count = %u\n", (unsigned int)total_count));
2672 if (max_reported_jobs && total_count > max_reported_jobs)
2673 total_count = max_reported_jobs;
2676 *pcount = total_count;
2682 SAFE_FREE(data.dptr);
2683 SAFE_FREE(cgdata.dptr);
2687 /****************************************************************************
2688 Get a printer queue listing.
2689 set queue = NULL and status = NULL if you just want to update the cache
2690 ****************************************************************************/
2692 int print_queue_status(int snum,
2693 print_queue_struct **ppqueue,
2694 print_status_struct *status)
2698 const char *sharename;
2699 struct tdb_print_db *pdb;
2702 /* make sure the database is up to date */
2704 if (print_cache_expired(lp_const_servicename(snum), True))
2705 print_queue_update(snum, False);
2707 /* return if we are done */
2708 if ( !ppqueue || !status )
2712 sharename = lp_const_servicename(snum);
2713 pdb = get_print_db_byname(sharename);
2719 * Fetch the queue status. We must do this first, as there may
2720 * be no jobs in the queue.
2723 ZERO_STRUCTP(status);
2724 slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", sharename);
2725 key = string_tdb_data(keystr);
2727 data = tdb_fetch(pdb->tdb, key);
2729 if (data.dsize == sizeof(*status)) {
2730 /* this memcpy is ok since the status struct was
2731 not packed before storing it in the tdb */
2732 memcpy(status, data.dptr, sizeof(*status));
2734 SAFE_FREE(data.dptr);
2738 * Now, fetch the print queue information. We first count the number
2739 * of entries, and then only retrieve the queue if necessary.
2742 if (!get_stored_queue_info(pdb, snum, &count, ppqueue)) {
2743 release_print_db(pdb);
2747 release_print_db(pdb);
2751 /****************************************************************************
2753 ****************************************************************************/
2755 bool print_queue_pause(struct current_user *user, int snum, WERROR *errcode)
2758 struct printif *current_printif = get_printer_fns( snum );
2760 if (!print_access_check(user, snum, PRINTER_ACCESS_ADMINISTER)) {
2761 *errcode = WERR_ACCESS_DENIED;
2768 ret = (*(current_printif->queue_pause))(snum);
2773 *errcode = WERR_INVALID_PARAM;
2777 /* force update the database */
2778 print_cache_flush(lp_const_servicename(snum));
2780 /* Send a printer notify message */
2782 notify_printer_status(snum, PRINTER_STATUS_PAUSED);
2787 /****************************************************************************
2789 ****************************************************************************/
2791 bool print_queue_resume(struct current_user *user, int snum, WERROR *errcode)
2794 struct printif *current_printif = get_printer_fns( snum );
2796 if (!print_access_check(user, snum, PRINTER_ACCESS_ADMINISTER)) {
2797 *errcode = WERR_ACCESS_DENIED;
2803 ret = (*(current_printif->queue_resume))(snum);
2808 *errcode = WERR_INVALID_PARAM;
2812 /* make sure the database is up to date */
2813 if (print_cache_expired(lp_const_servicename(snum), True))
2814 print_queue_update(snum, True);
2816 /* Send a printer notify message */
2818 notify_printer_status(snum, PRINTER_STATUS_OK);
2823 /****************************************************************************
2824 Purge a queue - implemented by deleting all jobs that we can delete.
2825 ****************************************************************************/
2827 bool print_queue_purge(struct current_user *user, int snum, WERROR *errcode)
2829 print_queue_struct *queue;
2830 print_status_struct status;
2834 /* Force and update so the count is accurate (i.e. not a cached count) */
2835 print_queue_update(snum, True);
2837 can_job_admin = print_access_check(user, snum, JOB_ACCESS_ADMINISTER);
2838 njobs = print_queue_status(snum, &queue, &status);
2840 if ( can_job_admin )
2843 for (i=0;i<njobs;i++) {
2844 bool owner = is_owner(user, lp_const_servicename(snum), queue[i].job);
2846 if (owner || can_job_admin) {
2847 print_job_delete1(snum, queue[i].job);
2851 if ( can_job_admin )
2854 /* update the cache */
2855 print_queue_update( snum, True );