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 struct current_user current_user;
26 extern userdom_struct current_user_info;
28 /* Current printer interface */
29 static bool remove_from_jobs_changed(const char* sharename, uint32 jobid);
32 the printing backend revolves around a tdb database that stores the
33 SMB view of the print queue
35 The key for this database is a jobid - a internally generated number that
36 uniquely identifies a print job
38 reading the print queue involves two steps:
39 - possibly running lpq and updating the internal database from that
40 - reading entries from the database
42 jobids are assigned when a job starts spooling.
45 static TDB_CONTEXT *rap_tdb;
46 static uint16 next_rap_jobid;
47 struct rap_jobid_key {
52 /***************************************************************************
53 Nightmare. LANMAN jobid's are 16 bit numbers..... We must map them to 32
54 bit RPC jobids.... JRA.
55 ***************************************************************************/
57 uint16 pjobid_to_rap(const char* sharename, uint32 jobid)
61 struct rap_jobid_key jinfo;
64 DEBUG(10,("pjobid_to_rap: called.\n"));
67 /* Create the in-memory tdb. */
68 rap_tdb = tdb_open_log(NULL, 0, TDB_INTERNAL, (O_RDWR|O_CREAT), 0644);
74 fstrcpy( jinfo.sharename, sharename );
76 key.dptr = (uint8 *)&jinfo;
77 key.dsize = sizeof(jinfo);
79 data = tdb_fetch(rap_tdb, key);
80 if (data.dptr && data.dsize == sizeof(uint16)) {
81 rap_jobid = SVAL(data.dptr, 0);
83 DEBUG(10,("pjobid_to_rap: jobid %u maps to RAP jobid %u\n",
84 (unsigned int)jobid, (unsigned int)rap_jobid));
88 /* Not found - create and store mapping. */
89 rap_jobid = ++next_rap_jobid;
91 rap_jobid = ++next_rap_jobid;
92 SSVAL(buf,0,rap_jobid);
94 data.dsize = sizeof(rap_jobid);
95 tdb_store(rap_tdb, key, data, TDB_REPLACE);
96 tdb_store(rap_tdb, data, key, TDB_REPLACE);
98 DEBUG(10,("pjobid_to_rap: created jobid %u maps to RAP jobid %u\n",
99 (unsigned int)jobid, (unsigned int)rap_jobid));
103 bool rap_to_pjobid(uint16 rap_jobid, fstring sharename, uint32 *pjobid)
108 DEBUG(10,("rap_to_pjobid called.\n"));
113 SSVAL(buf,0,rap_jobid);
115 key.dsize = sizeof(rap_jobid);
116 data = tdb_fetch(rap_tdb, key);
117 if ( data.dptr && data.dsize == sizeof(struct rap_jobid_key) )
119 struct rap_jobid_key *jinfo = (struct rap_jobid_key*)data.dptr;
120 fstrcpy( sharename, jinfo->sharename );
121 *pjobid = jinfo->jobid;
122 DEBUG(10,("rap_to_pjobid: jobid %u maps to RAP jobid %u\n",
123 (unsigned int)*pjobid, (unsigned int)rap_jobid));
124 SAFE_FREE(data.dptr);
128 DEBUG(10,("rap_to_pjobid: Failed to lookup RAP jobid %u\n",
129 (unsigned int)rap_jobid));
130 SAFE_FREE(data.dptr);
134 static void rap_jobid_delete(const char* sharename, uint32 jobid)
138 struct rap_jobid_key jinfo;
141 DEBUG(10,("rap_jobid_delete: called.\n"));
146 ZERO_STRUCT( jinfo );
147 fstrcpy( jinfo.sharename, sharename );
149 key.dptr = (uint8 *)&jinfo;
150 key.dsize = sizeof(jinfo);
152 data = tdb_fetch(rap_tdb, key);
153 if (!data.dptr || (data.dsize != sizeof(uint16))) {
154 DEBUG(10,("rap_jobid_delete: cannot find jobid %u\n",
155 (unsigned int)jobid ));
156 SAFE_FREE(data.dptr);
160 DEBUG(10,("rap_jobid_delete: deleting jobid %u\n",
161 (unsigned int)jobid ));
163 rap_jobid = SVAL(data.dptr, 0);
164 SAFE_FREE(data.dptr);
165 SSVAL(buf,0,rap_jobid);
167 data.dsize = sizeof(rap_jobid);
168 tdb_delete(rap_tdb, key);
169 tdb_delete(rap_tdb, data);
172 static int get_queue_status(const char* sharename, print_status_struct *);
174 /****************************************************************************
175 Initialise the printing backend. Called once at startup before the fork().
176 ****************************************************************************/
178 bool print_backend_init(struct messaging_context *msg_ctx)
180 const char *sversion = "INFO/version";
181 int services = lp_numservices();
184 unlink(cache_path("printing.tdb"));
185 mkdir(cache_path("printing"),0755);
187 /* handle a Samba upgrade */
189 for (snum = 0; snum < services; snum++) {
190 struct tdb_print_db *pdb;
191 if (!lp_print_ok(snum))
194 pdb = get_print_db_byname(lp_const_servicename(snum));
197 if (tdb_lock_bystring(pdb->tdb, sversion) == -1) {
198 DEBUG(0,("print_backend_init: Failed to open printer %s database\n", lp_const_servicename(snum) ));
199 release_print_db(pdb);
202 if (tdb_fetch_int32(pdb->tdb, sversion) != PRINT_DATABASE_VERSION) {
203 tdb_wipe_all(pdb->tdb);
204 tdb_store_int32(pdb->tdb, sversion, PRINT_DATABASE_VERSION);
206 tdb_unlock_bystring(pdb->tdb, sversion);
207 release_print_db(pdb);
210 close_all_print_db(); /* Don't leave any open. */
212 /* do NT print initialization... */
213 return nt_printing_init(msg_ctx);
216 /****************************************************************************
217 Shut down printing backend. Called once at shutdown to close the tdb.
218 ****************************************************************************/
220 void printing_end(void)
222 close_all_print_db(); /* Don't leave any open. */
225 /****************************************************************************
226 Retrieve the set of printing functions for a given service. This allows
227 us to set the printer function table based on the value of the 'printing'
230 Use the generic interface as the default and only use cups interface only
231 when asked for (and only when supported)
232 ****************************************************************************/
234 static struct printif *get_printer_fns_from_type( enum printing_types type )
236 struct printif *printer_fns = &generic_printif;
239 if ( type == PRINT_CUPS ) {
240 printer_fns = &cups_printif;
242 #endif /* HAVE_CUPS */
245 if ( type == PRINT_IPRINT ) {
246 printer_fns = &iprint_printif;
248 #endif /* HAVE_IPRINT */
250 printer_fns->type = type;
255 static struct printif *get_printer_fns( int snum )
257 return get_printer_fns_from_type( (enum printing_types)lp_printing(snum) );
261 /****************************************************************************
262 Useful function to generate a tdb key.
263 ****************************************************************************/
265 static TDB_DATA print_key(uint32 jobid, uint32 *tmp)
269 SIVAL(tmp, 0, jobid);
270 ret.dptr = (uint8 *)tmp;
271 ret.dsize = sizeof(*tmp);
275 /***********************************************************************
276 unpack a pjob from a tdb buffer
277 ***********************************************************************/
279 int unpack_pjob( uint8 *buf, int buflen, struct printjob *pjob )
283 uint32 pjpid, pjsysjob, pjfd, pjstarttime, pjstatus;
284 uint32 pjsize, pjpage_count, pjspooled, pjsmbjob;
289 len += tdb_unpack(buf+len, buflen-len, "dddddddddffff",
307 if ( (used = unpack_devicemode(&pjob->nt_devmode, buf+len, buflen-len)) == -1 )
313 pjob->sysjob = pjsysjob;
315 pjob->starttime = pjstarttime;
316 pjob->status = pjstatus;
318 pjob->page_count = pjpage_count;
319 pjob->spooled = pjspooled;
320 pjob->smbjob = pjsmbjob;
326 /****************************************************************************
327 Useful function to find a print job in the database.
328 ****************************************************************************/
330 static struct printjob *print_job_find(const char *sharename, uint32 jobid)
332 static struct printjob pjob;
335 struct tdb_print_db *pdb = get_print_db_byname(sharename);
337 DEBUG(10,("print_job_find: looking up job %u for share %s\n",
338 (unsigned int)jobid, sharename ));
344 ret = tdb_fetch(pdb->tdb, print_key(jobid, &tmp));
345 release_print_db(pdb);
348 DEBUG(10,("print_job_find: failed to find jobid %u.\n", (unsigned int)jobid ));
352 if ( pjob.nt_devmode ) {
353 free_nt_devicemode( &pjob.nt_devmode );
358 if ( unpack_pjob( ret.dptr, ret.dsize, &pjob ) == -1 ) {
359 DEBUG(10,("print_job_find: failed to unpack jobid %u.\n", (unsigned int)jobid ));
366 DEBUG(10,("print_job_find: returning system job %d for jobid %u.\n",
367 (int)pjob.sysjob, (unsigned int)jobid ));
372 /* Convert a unix jobid to a smb jobid */
374 struct unixjob_traverse_state {
376 uint32 sysjob_to_jobid_value;
379 static int unixjob_traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA key,
380 TDB_DATA data, void *private_data)
382 struct printjob *pjob;
383 struct unixjob_traverse_state *state =
384 (struct unixjob_traverse_state *)private_data;
386 if (!data.dptr || data.dsize == 0)
389 pjob = (struct printjob *)data.dptr;
390 if (key.dsize != sizeof(uint32))
393 if (state->sysjob == pjob->sysjob) {
394 uint32 jobid = IVAL(key.dptr,0);
396 state->sysjob_to_jobid_value = jobid;
403 /****************************************************************************
404 This is a *horribly expensive call as we have to iterate through all the
405 current printer tdb's. Don't do this often ! JRA.
406 ****************************************************************************/
408 uint32 sysjob_to_jobid(int unix_jobid)
410 int services = lp_numservices();
412 struct unixjob_traverse_state state;
414 state.sysjob = unix_jobid;
415 state.sysjob_to_jobid_value = (uint32)-1;
417 for (snum = 0; snum < services; snum++) {
418 struct tdb_print_db *pdb;
419 if (!lp_print_ok(snum))
421 pdb = get_print_db_byname(lp_const_servicename(snum));
425 tdb_traverse(pdb->tdb, unixjob_traverse_fn, &state);
426 release_print_db(pdb);
427 if (state.sysjob_to_jobid_value != (uint32)-1)
428 return state.sysjob_to_jobid_value;
433 /****************************************************************************
434 Send notifications based on what has changed after a pjob_store.
435 ****************************************************************************/
437 static const struct {
439 uint32 spoolss_status;
440 } lpq_to_spoolss_status_map[] = {
441 { LPQ_QUEUED, JOB_STATUS_QUEUED },
442 { LPQ_PAUSED, JOB_STATUS_PAUSED },
443 { LPQ_SPOOLING, JOB_STATUS_SPOOLING },
444 { LPQ_PRINTING, JOB_STATUS_PRINTING },
445 { LPQ_DELETING, JOB_STATUS_DELETING },
446 { LPQ_OFFLINE, JOB_STATUS_OFFLINE },
447 { LPQ_PAPEROUT, JOB_STATUS_PAPEROUT },
448 { LPQ_PRINTED, JOB_STATUS_PRINTED },
449 { LPQ_DELETED, JOB_STATUS_DELETED },
450 { LPQ_BLOCKED, JOB_STATUS_BLOCKED_DEVQ },
451 { LPQ_USER_INTERVENTION, JOB_STATUS_USER_INTERVENTION },
455 /* Convert a lpq status value stored in printing.tdb into the
456 appropriate win32 API constant. */
458 static uint32 map_to_spoolss_status(uint32 lpq_status)
462 while (lpq_to_spoolss_status_map[i].lpq_status != -1) {
463 if (lpq_to_spoolss_status_map[i].lpq_status == lpq_status)
464 return lpq_to_spoolss_status_map[i].spoolss_status;
471 static void pjob_store_notify(const char* sharename, uint32 jobid, struct printjob *old_data,
472 struct printjob *new_data)
474 bool new_job = False;
479 /* Job attributes that can't be changed. We only send
480 notification for these on a new job. */
482 /* ACHTUNG! Due to a bug in Samba's spoolss parsing of the
483 NOTIFY_INFO_DATA buffer, we *have* to send the job submission
484 time first or else we'll end up with potential alignment
485 errors. I don't think the systemtime should be spooled as
486 a string, but this gets us around that error.
487 --jerry (i'll feel dirty for this) */
490 notify_job_submitted(sharename, jobid, new_data->starttime);
491 notify_job_username(sharename, jobid, new_data->user);
494 if (new_job || !strequal(old_data->jobname, new_data->jobname))
495 notify_job_name(sharename, jobid, new_data->jobname);
497 /* Job attributes of a new job or attributes that can be
500 if (new_job || !strequal(old_data->jobname, new_data->jobname))
501 notify_job_name(sharename, jobid, new_data->jobname);
503 if (new_job || old_data->status != new_data->status)
504 notify_job_status(sharename, jobid, map_to_spoolss_status(new_data->status));
506 if (new_job || old_data->size != new_data->size)
507 notify_job_total_bytes(sharename, jobid, new_data->size);
509 if (new_job || old_data->page_count != new_data->page_count)
510 notify_job_total_pages(sharename, jobid, new_data->page_count);
513 /****************************************************************************
514 Store a job structure back to the database.
515 ****************************************************************************/
517 static bool pjob_store(const char* sharename, uint32 jobid, struct printjob *pjob)
520 TDB_DATA old_data, new_data;
522 struct tdb_print_db *pdb = get_print_db_byname(sharename);
524 int len, newlen, buflen;
532 old_data = tdb_fetch(pdb->tdb, print_key(jobid, &tmp));
534 /* Doh! Now we have to pack/unpack data since the NT_DEVICEMODE was added */
541 len += tdb_pack(buf+len, buflen-len, "dddddddddffff",
543 (uint32)pjob->sysjob,
545 (uint32)pjob->starttime,
546 (uint32)pjob->status,
548 (uint32)pjob->page_count,
549 (uint32)pjob->spooled,
550 (uint32)pjob->smbjob,
556 len += pack_devicemode(pjob->nt_devmode, buf+len, buflen-len);
559 buf = (uint8 *)SMB_REALLOC(buf, len);
561 DEBUG(0,("pjob_store: failed to enlarge buffer!\n"));
566 } while ( buflen != len );
572 new_data.dsize = len;
573 ret = (tdb_store(pdb->tdb, print_key(jobid, &tmp), new_data,
576 release_print_db(pdb);
578 /* Send notify updates for what has changed */
581 struct printjob old_pjob;
583 if ( old_data.dsize )
585 if ( unpack_pjob( old_data.dptr, old_data.dsize, &old_pjob ) != -1 )
587 pjob_store_notify( sharename, jobid, &old_pjob , pjob );
588 free_nt_devicemode( &old_pjob.nt_devmode );
593 pjob_store_notify( sharename, jobid, NULL, pjob );
598 SAFE_FREE( old_data.dptr );
604 /****************************************************************************
605 Remove a job structure from the database.
606 ****************************************************************************/
608 void pjob_delete(const char* sharename, uint32 jobid)
611 struct printjob *pjob;
612 uint32 job_status = 0;
613 struct tdb_print_db *pdb;
615 pdb = get_print_db_byname( sharename );
620 pjob = print_job_find( sharename, jobid );
623 DEBUG(5, ("pjob_delete: we were asked to delete nonexistent job %u\n",
624 (unsigned int)jobid));
625 release_print_db(pdb);
629 /* We must cycle through JOB_STATUS_DELETING and
630 JOB_STATUS_DELETED for the port monitor to delete the job
633 job_status = JOB_STATUS_DELETING|JOB_STATUS_DELETED;
634 notify_job_status(sharename, jobid, job_status);
636 /* Remove from printing.tdb */
638 tdb_delete(pdb->tdb, print_key(jobid, &tmp));
639 remove_from_jobs_changed(sharename, jobid);
640 release_print_db( pdb );
641 rap_jobid_delete(sharename, jobid);
644 /****************************************************************************
645 Parse a file name from the system spooler to generate a jobid.
646 ****************************************************************************/
648 static uint32 print_parse_jobid(char *fname)
652 if (strncmp(fname,PRINT_SPOOL_PREFIX,strlen(PRINT_SPOOL_PREFIX)) != 0)
654 fname += strlen(PRINT_SPOOL_PREFIX);
660 return (uint32)jobid;
663 /****************************************************************************
664 List a unix job in the print database.
665 ****************************************************************************/
667 static void print_unix_job(const char *sharename, print_queue_struct *q, uint32 jobid)
669 struct printjob pj, *old_pj;
671 if (jobid == (uint32)-1)
672 jobid = q->job + UNIX_JOB_START;
674 /* Preserve the timestamp on an existing unix print job */
676 old_pj = print_job_find(sharename, jobid);
683 pj.starttime = old_pj ? old_pj->starttime : q->time;
684 pj.status = q->status;
687 fstrcpy(pj.filename, old_pj ? old_pj->filename : "");
688 if (jobid < UNIX_JOB_START) {
690 fstrcpy(pj.jobname, old_pj ? old_pj->jobname : "Remote Downlevel Document");
693 fstrcpy(pj.jobname, old_pj ? old_pj->jobname : q->fs_file);
695 fstrcpy(pj.user, old_pj ? old_pj->user : q->fs_user);
696 fstrcpy(pj.queuename, old_pj ? old_pj->queuename : sharename );
698 pjob_store(sharename, jobid, &pj);
702 struct traverse_struct {
703 print_queue_struct *queue;
704 int qcount, snum, maxcount, total_jobs;
705 const char *sharename;
707 const char *lprm_command;
708 struct printif *print_if;
711 /****************************************************************************
712 Utility fn to delete any jobs that are no longer active.
713 ****************************************************************************/
715 static int traverse_fn_delete(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
717 struct traverse_struct *ts = (struct traverse_struct *)state;
718 struct printjob pjob;
722 if ( key.dsize != sizeof(jobid) )
725 jobid = IVAL(key.dptr, 0);
726 if ( unpack_pjob( data.dptr, data.dsize, &pjob ) == -1 )
728 free_nt_devicemode( &pjob.nt_devmode );
732 /* remove a unix job if it isn't in the system queue any more */
734 for (i=0;i<ts->qcount;i++) {
735 uint32 u_jobid = (ts->queue[i].job + UNIX_JOB_START);
736 if (jobid == u_jobid)
739 if (i == ts->qcount) {
740 DEBUG(10,("traverse_fn_delete: pjob %u deleted due to !smbjob\n",
741 (unsigned int)jobid ));
742 pjob_delete(ts->sharename, jobid);
746 /* need to continue the the bottom of the function to
747 save the correct attributes */
750 /* maybe it hasn't been spooled yet */
752 /* if a job is not spooled and the process doesn't
753 exist then kill it. This cleans up after smbd
755 if (!process_exists_by_pid(pjob.pid)) {
756 DEBUG(10,("traverse_fn_delete: pjob %u deleted due to !process_exists (%u)\n",
757 (unsigned int)jobid, (unsigned int)pjob.pid ));
758 pjob_delete(ts->sharename, jobid);
764 /* this check only makes sense for jobs submitted from Windows clients */
767 for (i=0;i<ts->qcount;i++) {
770 if ( pjob.status == LPQ_DELETED )
773 curr_jobid = print_parse_jobid(ts->queue[i].fs_file);
775 if (jobid == curr_jobid) {
777 /* try to clean up any jobs that need to be deleted */
779 if ( pjob.status == LPQ_DELETING ) {
782 result = (*(ts->print_if->job_delete))(
783 ts->sharename, ts->lprm_command, &pjob );
786 /* if we can't delete, then reset the job status */
787 pjob.status = LPQ_QUEUED;
788 pjob_store(ts->sharename, jobid, &pjob);
791 /* if we deleted the job, the remove the tdb record */
792 pjob_delete(ts->sharename, jobid);
793 pjob.status = LPQ_DELETED;
803 /* The job isn't in the system queue - we have to assume it has
804 completed, so delete the database entry. */
806 if (i == ts->qcount) {
808 /* A race can occur between the time a job is spooled and
809 when it appears in the lpq output. This happens when
810 the job is added to printing.tdb when another smbd
811 running print_queue_update() has completed a lpq and
812 is currently traversing the printing tdb and deleting jobs.
813 Don't delete the job if it was submitted after the lpq_time. */
815 if (pjob.starttime < ts->lpq_time) {
816 DEBUG(10,("traverse_fn_delete: pjob %u deleted due to pjob.starttime (%u) < ts->lpq_time (%u)\n",
818 (unsigned int)pjob.starttime,
819 (unsigned int)ts->lpq_time ));
820 pjob_delete(ts->sharename, jobid);
826 /* Save the pjob attributes we will store.
827 FIXME!!! This is the only place where queue->job
828 represents the SMB jobid --jerry */
830 ts->queue[i].job = jobid;
831 ts->queue[i].size = pjob.size;
832 ts->queue[i].page_count = pjob.page_count;
833 ts->queue[i].status = pjob.status;
834 ts->queue[i].priority = 1;
835 ts->queue[i].time = pjob.starttime;
836 fstrcpy(ts->queue[i].fs_user, pjob.user);
837 fstrcpy(ts->queue[i].fs_file, pjob.jobname);
844 /****************************************************************************
845 Check if the print queue has been updated recently enough.
846 ****************************************************************************/
848 static void print_cache_flush(const char *sharename)
851 struct tdb_print_db *pdb = get_print_db_byname(sharename);
855 slprintf(key, sizeof(key)-1, "CACHE/%s", sharename);
856 tdb_store_int32(pdb->tdb, key, -1);
857 release_print_db(pdb);
860 /****************************************************************************
861 Check if someone already thinks they are doing the update.
862 ****************************************************************************/
864 static pid_t get_updating_pid(const char *sharename)
869 struct tdb_print_db *pdb = get_print_db_byname(sharename);
873 slprintf(keystr, sizeof(keystr)-1, "UPDATING/%s", sharename);
874 key = string_tdb_data(keystr);
876 data = tdb_fetch(pdb->tdb, key);
877 release_print_db(pdb);
878 if (!data.dptr || data.dsize != sizeof(pid_t)) {
879 SAFE_FREE(data.dptr);
883 updating_pid = IVAL(data.dptr, 0);
884 SAFE_FREE(data.dptr);
886 if (process_exists_by_pid(updating_pid))
892 /****************************************************************************
893 Set the fact that we're doing the update, or have finished doing the update
895 ****************************************************************************/
897 static void set_updating_pid(const fstring sharename, bool updating)
902 pid_t updating_pid = sys_getpid();
905 struct tdb_print_db *pdb = get_print_db_byname(sharename);
910 slprintf(keystr, sizeof(keystr)-1, "UPDATING/%s", sharename);
911 key = string_tdb_data(keystr);
913 DEBUG(5, ("set_updating_pid: %s updating lpq cache for print share %s\n",
914 updating ? "" : "not ",
918 tdb_delete(pdb->tdb, key);
919 release_print_db(pdb);
923 SIVAL( buffer, 0, updating_pid);
925 data.dsize = 4; /* we always assume this is a 4 byte value */
927 tdb_store(pdb->tdb, key, data, TDB_REPLACE);
928 release_print_db(pdb);
931 /****************************************************************************
932 Sort print jobs by submittal time.
933 ****************************************************************************/
935 static int printjob_comp(print_queue_struct *j1, print_queue_struct *j2)
946 /* Sort on job start time */
948 if (j1->time == j2->time)
950 return (j1->time > j2->time) ? 1 : -1;
953 /****************************************************************************
954 Store the sorted queue representation for later portmon retrieval.
956 ****************************************************************************/
958 static void store_queue_struct(struct tdb_print_db *pdb, struct traverse_struct *pts)
961 int max_reported_jobs = lp_max_reported_jobs(pts->snum);
962 print_queue_struct *queue = pts->queue;
967 if (max_reported_jobs && (max_reported_jobs < pts->qcount))
968 pts->qcount = max_reported_jobs;
971 /* Work out the size. */
973 data.dsize += tdb_pack(NULL, 0, "d", qcount);
975 for (i = 0; i < pts->qcount; i++) {
976 if ( queue[i].status == LPQ_DELETED )
980 data.dsize += tdb_pack(NULL, 0, "ddddddff",
981 (uint32)queue[i].job,
982 (uint32)queue[i].size,
983 (uint32)queue[i].page_count,
984 (uint32)queue[i].status,
985 (uint32)queue[i].priority,
986 (uint32)queue[i].time,
991 if ((data.dptr = (uint8 *)SMB_MALLOC(data.dsize)) == NULL)
995 len += tdb_pack(data.dptr + len, data.dsize - len, "d", qcount);
996 for (i = 0; i < pts->qcount; i++) {
997 if ( queue[i].status == LPQ_DELETED )
1000 len += tdb_pack(data.dptr + len, data.dsize - len, "ddddddff",
1001 (uint32)queue[i].job,
1002 (uint32)queue[i].size,
1003 (uint32)queue[i].page_count,
1004 (uint32)queue[i].status,
1005 (uint32)queue[i].priority,
1006 (uint32)queue[i].time,
1011 tdb_store(pdb->tdb, string_tdb_data("INFO/linear_queue_array"), data,
1013 SAFE_FREE(data.dptr);
1017 static TDB_DATA get_jobs_changed_data(struct tdb_print_db *pdb)
1023 data = tdb_fetch(pdb->tdb, string_tdb_data("INFO/jobs_changed"));
1024 if (data.dptr == NULL || data.dsize == 0 || (data.dsize % 4 != 0)) {
1025 SAFE_FREE(data.dptr);
1032 static void check_job_changed(const char *sharename, TDB_DATA data, uint32 jobid)
1035 unsigned int job_count = data.dsize / 4;
1037 for (i = 0; i < job_count; i++) {
1040 ch_jobid = IVAL(data.dptr, i*4);
1041 if (ch_jobid == jobid)
1042 remove_from_jobs_changed(sharename, jobid);
1046 /****************************************************************************
1047 Check if the print queue has been updated recently enough.
1048 ****************************************************************************/
1050 static bool print_cache_expired(const char *sharename, bool check_pending)
1053 time_t last_qscan_time, time_now = time(NULL);
1054 struct tdb_print_db *pdb = get_print_db_byname(sharename);
1055 bool result = False;
1060 snprintf(key, sizeof(key), "CACHE/%s", sharename);
1061 last_qscan_time = (time_t)tdb_fetch_int32(pdb->tdb, key);
1064 * Invalidate the queue for 3 reasons.
1065 * (1). last queue scan time == -1.
1066 * (2). Current time - last queue scan time > allowed cache time.
1067 * (3). last queue scan time > current time + MAX_CACHE_VALID_TIME (1 hour by default).
1068 * This last test picks up machines for which the clock has been moved
1069 * forward, an lpq scan done and then the clock moved back. Otherwise
1070 * that last lpq scan would stay around for a loooong loooong time... :-). JRA.
1073 if (last_qscan_time == ((time_t)-1)
1074 || (time_now - last_qscan_time) >= lp_lpqcachetime()
1075 || last_qscan_time > (time_now + MAX_CACHE_VALID_TIME))
1078 time_t msg_pending_time;
1080 DEBUG(4, ("print_cache_expired: cache expired for queue %s "
1081 "(last_qscan_time = %d, time now = %d, qcachetime = %d)\n",
1082 sharename, (int)last_qscan_time, (int)time_now,
1083 (int)lp_lpqcachetime() ));
1085 /* check if another smbd has already sent a message to update the
1086 queue. Give the pending message one minute to clear and
1087 then send another message anyways. Make sure to check for
1088 clocks that have been run forward and then back again. */
1090 snprintf(key, sizeof(key), "MSG_PENDING/%s", sharename);
1093 && tdb_fetch_uint32( pdb->tdb, key, &u )
1094 && (msg_pending_time=u) > 0
1095 && msg_pending_time <= time_now
1096 && (time_now - msg_pending_time) < 60 )
1098 DEBUG(4,("print_cache_expired: message already pending for %s. Accepting cache\n",
1107 release_print_db(pdb);
1111 /****************************************************************************
1112 main work for updating the lpq cahe for a printer queue
1113 ****************************************************************************/
1115 static void print_queue_update_internal( const char *sharename,
1116 struct printif *current_printif,
1117 char *lpq_command, char *lprm_command )
1120 print_queue_struct *queue = NULL;
1121 print_status_struct status;
1122 print_status_struct old_status;
1123 struct printjob *pjob;
1124 struct traverse_struct tstruct;
1127 fstring keystr, cachestr;
1128 struct tdb_print_db *pdb = get_print_db_byname(sharename);
1134 DEBUG(5,("print_queue_update_internal: printer = %s, type = %d, lpq command = [%s]\n",
1135 sharename, current_printif->type, lpq_command));
1138 * Update the cache time FIRST ! Stops others even
1139 * attempting to get the lock and doing this
1140 * if the lpq takes a long time.
1143 slprintf(cachestr, sizeof(cachestr)-1, "CACHE/%s", sharename);
1144 tdb_store_int32(pdb->tdb, cachestr, (int)time(NULL));
1146 /* get the current queue using the appropriate interface */
1147 ZERO_STRUCT(status);
1149 qcount = (*(current_printif->queue_get))(sharename,
1150 current_printif->type,
1151 lpq_command, &queue, &status);
1153 DEBUG(3, ("print_queue_update_internal: %d job%s in queue for %s\n",
1154 qcount, (qcount != 1) ? "s" : "", sharename));
1156 /* Sort the queue by submission time otherwise they are displayed
1159 qsort(queue, qcount, sizeof(print_queue_struct),
1160 QSORT_CAST(printjob_comp));
1163 any job in the internal database that is marked as spooled
1164 and doesn't exist in the system queue is considered finished
1165 and removed from the database
1167 any job in the system database but not in the internal database
1168 is added as a unix job
1170 fill in any system job numbers as we go
1173 jcdata = get_jobs_changed_data(pdb);
1175 for (i=0; i<qcount; i++) {
1176 uint32 jobid = print_parse_jobid(queue[i].fs_file);
1178 if (jobid == (uint32)-1) {
1179 /* assume its a unix print job */
1180 print_unix_job(sharename, &queue[i], jobid);
1184 /* we have an active SMB print job - update its status */
1185 pjob = print_job_find(sharename, jobid);
1187 /* err, somethings wrong. Probably smbd was restarted
1188 with jobs in the queue. All we can do is treat them
1189 like unix jobs. Pity. */
1190 print_unix_job(sharename, &queue[i], jobid);
1194 pjob->sysjob = queue[i].job;
1196 /* don't reset the status on jobs to be deleted */
1198 if ( pjob->status != LPQ_DELETING )
1199 pjob->status = queue[i].status;
1201 pjob_store(sharename, jobid, pjob);
1203 check_job_changed(sharename, jcdata, jobid);
1206 SAFE_FREE(jcdata.dptr);
1208 /* now delete any queued entries that don't appear in the
1210 tstruct.queue = queue;
1211 tstruct.qcount = qcount;
1213 tstruct.total_jobs = 0;
1214 tstruct.lpq_time = time(NULL);
1215 tstruct.sharename = sharename;
1216 tstruct.lprm_command = lprm_command;
1217 tstruct.print_if = current_printif;
1219 tdb_traverse(pdb->tdb, traverse_fn_delete, (void *)&tstruct);
1221 /* Store the linearised queue, max jobs only. */
1222 store_queue_struct(pdb, &tstruct);
1224 SAFE_FREE(tstruct.queue);
1226 DEBUG(10,("print_queue_update_internal: printer %s INFO/total_jobs = %d\n",
1227 sharename, tstruct.total_jobs ));
1229 tdb_store_int32(pdb->tdb, "INFO/total_jobs", tstruct.total_jobs);
1231 get_queue_status(sharename, &old_status);
1232 if (old_status.qcount != qcount)
1233 DEBUG(10,("print_queue_update_internal: queue status change %d jobs -> %d jobs for printer %s\n",
1234 old_status.qcount, qcount, sharename));
1236 /* store the new queue status structure */
1237 slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", sharename);
1238 key = string_tdb_data(keystr);
1240 status.qcount = qcount;
1241 data.dptr = (uint8 *)&status;
1242 data.dsize = sizeof(status);
1243 tdb_store(pdb->tdb, key, data, TDB_REPLACE);
1246 * Update the cache time again. We want to do this call
1247 * as little as possible...
1250 slprintf(keystr, sizeof(keystr)-1, "CACHE/%s", sharename);
1251 tdb_store_int32(pdb->tdb, keystr, (int32)time(NULL));
1253 /* clear the msg pending record for this queue */
1255 snprintf(keystr, sizeof(keystr), "MSG_PENDING/%s", sharename);
1257 if ( !tdb_store_uint32( pdb->tdb, keystr, 0 ) ) {
1258 /* log a message but continue on */
1260 DEBUG(0,("print_queue_update: failed to store MSG_PENDING flag for [%s]!\n",
1264 release_print_db( pdb );
1269 /****************************************************************************
1270 Update the internal database from the system print queue for a queue.
1271 obtain a lock on the print queue before proceeding (needed when mutiple
1272 smbd processes maytry to update the lpq cache concurrently).
1273 ****************************************************************************/
1275 static void print_queue_update_with_lock( const char *sharename,
1276 struct printif *current_printif,
1277 char *lpq_command, char *lprm_command )
1280 struct tdb_print_db *pdb;
1282 DEBUG(5,("print_queue_update_with_lock: printer share = %s\n", sharename));
1283 pdb = get_print_db_byname(sharename);
1287 if ( !print_cache_expired(sharename, False) ) {
1288 DEBUG(5,("print_queue_update_with_lock: print cache for %s is still ok\n", sharename));
1289 release_print_db(pdb);
1294 * Check to see if someone else is doing this update.
1295 * This is essentially a mutex on the update.
1298 if (get_updating_pid(sharename) != -1) {
1299 release_print_db(pdb);
1303 /* Lock the queue for the database update */
1305 slprintf(keystr, sizeof(keystr) - 1, "LOCK/%s", sharename);
1306 /* Only wait 10 seconds for this. */
1307 if (tdb_lock_bystring_with_timeout(pdb->tdb, keystr, 10) == -1) {
1308 DEBUG(0,("print_queue_update_with_lock: Failed to lock printer %s database\n", sharename));
1309 release_print_db(pdb);
1314 * Ensure that no one else got in here.
1315 * If the updating pid is still -1 then we are
1319 if (get_updating_pid(sharename) != -1) {
1321 * Someone else is doing the update, exit.
1323 tdb_unlock_bystring(pdb->tdb, keystr);
1324 release_print_db(pdb);
1329 * We're going to do the update ourselves.
1332 /* Tell others we're doing the update. */
1333 set_updating_pid(sharename, True);
1336 * Allow others to enter and notice we're doing
1340 tdb_unlock_bystring(pdb->tdb, keystr);
1342 /* do the main work now */
1344 print_queue_update_internal( sharename, current_printif,
1345 lpq_command, lprm_command );
1347 /* Delete our pid from the db. */
1348 set_updating_pid(sharename, False);
1349 release_print_db(pdb);
1352 /****************************************************************************
1353 this is the receive function of the background lpq updater
1354 ****************************************************************************/
1355 static void print_queue_receive(struct messaging_context *msg,
1358 struct server_id server_id,
1362 char *lpqcommand = NULL, *lprmcommand = NULL;
1366 len = tdb_unpack( (uint8 *)data->data, data->length, "fdPP",
1373 SAFE_FREE(lpqcommand);
1374 SAFE_FREE(lprmcommand);
1375 DEBUG(0,("print_queue_receive: Got invalid print queue update message\n"));
1379 print_queue_update_with_lock(sharename,
1380 get_printer_fns_from_type((enum printing_types)printing_type),
1381 lpqcommand, lprmcommand );
1383 SAFE_FREE(lpqcommand);
1384 SAFE_FREE(lprmcommand);
1388 static pid_t background_lpq_updater_pid = -1;
1390 /****************************************************************************
1391 main thread of the background lpq updater
1392 ****************************************************************************/
1393 void start_background_queue(void)
1395 /* Use local variables for this as we don't
1396 * need to save the parent side of this, just
1397 * ensure it closes when the process exits.
1401 DEBUG(3,("start_background_queue: Starting background LPQ thread\n"));
1403 if (pipe(pause_pipe) == -1) {
1404 DEBUG(5,("start_background_queue: cannot create pipe. %s\n", strerror(errno) ));
1408 background_lpq_updater_pid = sys_fork();
1410 if (background_lpq_updater_pid == -1) {
1411 DEBUG(5,("start_background_queue: background LPQ thread failed to start. %s\n", strerror(errno) ));
1415 if(background_lpq_updater_pid == 0) {
1417 DEBUG(5,("start_background_queue: background LPQ thread started\n"));
1419 close(pause_pipe[0]);
1422 if (!reinit_after_fork(smbd_messaging_context(),
1423 smbd_event_context(), true)) {
1424 DEBUG(0,("reinit_after_fork() failed\n"));
1425 smb_panic("reinit_after_fork() failed");
1428 smbd_setup_sig_term_handler();
1429 smbd_setup_sig_hup_handler();
1431 claim_connection( NULL, "smbd lpq backend",
1432 FLAG_MSG_GENERAL|FLAG_MSG_SMBD|FLAG_MSG_PRINT_GENERAL);
1434 if (!locking_init()) {
1438 messaging_register(smbd_messaging_context(), NULL,
1439 MSG_PRINTER_UPDATE, print_queue_receive);
1441 DEBUG(5,("start_background_queue: background LPQ thread waiting for messages\n"));
1443 fd_set r_fds, w_fds;
1448 /* Process a signal and timed events now... */
1449 if (run_events(smbd_event_context(), 0, NULL, NULL)) {
1453 to.tv_sec = SMBD_SELECT_TIMEOUT;
1457 * Setup the select fd sets.
1464 * Are there any timed events waiting ? If so, ensure we don't
1465 * select for longer than it would take to wait for them.
1472 event_add_to_select_args(smbd_event_context(), &now,
1473 &r_fds, &w_fds, &to, &maxfd);
1476 FD_SET(pause_pipe[1], &r_fds);
1477 maxfd = MAX(pause_pipe[1], maxfd);
1479 ret = sys_select(maxfd, &r_fds, &w_fds, NULL, &to);
1482 * If pause_pipe[1] is closed it means the parent smbd
1483 * and children exited or aborted. If sys_select()
1484 * failed, then something more sinister is wrong
1487 (ret == 1 && FD_ISSET(pause_pipe[1], &r_fds))) {
1488 exit_server_cleanly(NULL);
1491 if (run_events(smbd_event_context(), ret, &r_fds, &w_fds)) {
1497 close(pause_pipe[1]);
1500 /****************************************************************************
1501 update the internal database from the system print queue for a queue
1502 ****************************************************************************/
1504 static void print_queue_update(int snum, bool force)
1508 char *lpqcommand = NULL;
1509 char *lprmcommand = NULL;
1510 uint8 *buffer = NULL;
1513 struct tdb_print_db *pdb;
1515 struct printif *current_printif;
1516 TALLOC_CTX *ctx = talloc_tos();
1518 fstrcpy( sharename, lp_const_servicename(snum));
1520 /* don't strip out characters like '$' from the printername */
1522 lpqcommand = talloc_string_sub2(ctx,
1523 lp_lpqcommand(snum),
1526 false, false, false);
1530 lpqcommand = talloc_sub_advanced(ctx,
1531 lp_servicename(snum),
1532 current_user_info.unix_name,
1534 current_user.ut.gid,
1535 get_current_username(),
1536 current_user_info.domain,
1542 lprmcommand = talloc_string_sub2(ctx,
1543 lp_lprmcommand(snum),
1546 false, false, false);
1550 lprmcommand = talloc_sub_advanced(ctx,
1551 lp_servicename(snum),
1552 current_user_info.unix_name,
1554 current_user.ut.gid,
1555 get_current_username(),
1556 current_user_info.domain,
1563 * Make sure that the background queue process exists.
1564 * Otherwise just do the update ourselves
1567 if ( force || background_lpq_updater_pid == -1 ) {
1568 DEBUG(4,("print_queue_update: updating queue [%s] myself\n", sharename));
1569 current_printif = get_printer_fns( snum );
1570 print_queue_update_with_lock( sharename, current_printif, lpqcommand, lprmcommand );
1575 type = lp_printing(snum);
1577 /* get the length */
1579 len = tdb_pack( NULL, 0, "fdPP",
1585 buffer = SMB_XMALLOC_ARRAY( uint8, len );
1587 /* now pack the buffer */
1588 newlen = tdb_pack( buffer, len, "fdPP",
1594 SMB_ASSERT( newlen == len );
1596 DEBUG(10,("print_queue_update: Sending message -> printer = %s, "
1597 "type = %d, lpq command = [%s] lprm command = [%s]\n",
1598 sharename, type, lpqcommand, lprmcommand ));
1600 /* here we set a msg pending record for other smbd processes
1601 to throttle the number of duplicate print_queue_update msgs
1604 pdb = get_print_db_byname(sharename);
1610 snprintf(key, sizeof(key), "MSG_PENDING/%s", sharename);
1612 if ( !tdb_store_uint32( pdb->tdb, key, time(NULL) ) ) {
1613 /* log a message but continue on */
1615 DEBUG(0,("print_queue_update: failed to store MSG_PENDING flag for [%s]!\n",
1619 release_print_db( pdb );
1621 /* finally send the message */
1623 messaging_send_buf(smbd_messaging_context(),
1624 pid_to_procid(background_lpq_updater_pid),
1625 MSG_PRINTER_UPDATE, (uint8 *)buffer, len);
1627 SAFE_FREE( buffer );
1632 /****************************************************************************
1633 Create/Update an entry in the print tdb that will allow us to send notify
1634 updates only to interested smbd's.
1635 ****************************************************************************/
1637 bool print_notify_register_pid(int snum)
1640 struct tdb_print_db *pdb = NULL;
1641 TDB_CONTEXT *tdb = NULL;
1642 const char *printername;
1643 uint32 mypid = (uint32)sys_getpid();
1647 /* if (snum == -1), then the change notify request was
1648 on a print server handle and we need to register on
1653 int num_services = lp_numservices();
1656 for ( idx=0; idx<num_services; idx++ ) {
1657 if (lp_snum_ok(idx) && lp_print_ok(idx) )
1658 print_notify_register_pid(idx);
1663 else /* register for a specific printer */
1665 printername = lp_const_servicename(snum);
1666 pdb = get_print_db_byname(printername);
1672 if (tdb_lock_bystring_with_timeout(tdb, NOTIFY_PID_LIST_KEY, 10) == -1) {
1673 DEBUG(0,("print_notify_register_pid: Failed to lock printer %s\n",
1676 release_print_db(pdb);
1680 data = get_printer_notify_pid_list( tdb, printername, True );
1682 /* Add ourselves and increase the refcount. */
1684 for (i = 0; i < data.dsize; i += 8) {
1685 if (IVAL(data.dptr,i) == mypid) {
1686 uint32 new_refcount = IVAL(data.dptr, i+4) + 1;
1687 SIVAL(data.dptr, i+4, new_refcount);
1692 if (i == data.dsize) {
1693 /* We weren't in the list. Realloc. */
1694 data.dptr = (uint8 *)SMB_REALLOC(data.dptr, data.dsize + 8);
1696 DEBUG(0,("print_notify_register_pid: Relloc fail for printer %s\n",
1701 SIVAL(data.dptr,data.dsize - 8,mypid);
1702 SIVAL(data.dptr,data.dsize - 4,1); /* Refcount. */
1705 /* Store back the record. */
1706 if (tdb_store_bystring(tdb, NOTIFY_PID_LIST_KEY, data, TDB_REPLACE) == -1) {
1707 DEBUG(0,("print_notify_register_pid: Failed to update pid \
1708 list for printer %s\n", printername));
1716 tdb_unlock_bystring(tdb, NOTIFY_PID_LIST_KEY);
1718 release_print_db(pdb);
1719 SAFE_FREE(data.dptr);
1723 /****************************************************************************
1724 Update an entry in the print tdb that will allow us to send notify
1725 updates only to interested smbd's.
1726 ****************************************************************************/
1728 bool print_notify_deregister_pid(int snum)
1731 struct tdb_print_db *pdb = NULL;
1732 TDB_CONTEXT *tdb = NULL;
1733 const char *printername;
1734 uint32 mypid = (uint32)sys_getpid();
1738 /* if ( snum == -1 ), we are deregister a print server handle
1739 which means to deregister on all print queues */
1743 int num_services = lp_numservices();
1746 for ( idx=0; idx<num_services; idx++ ) {
1747 if ( lp_snum_ok(idx) && lp_print_ok(idx) )
1748 print_notify_deregister_pid(idx);
1753 else /* deregister a specific printer */
1755 printername = lp_const_servicename(snum);
1756 pdb = get_print_db_byname(printername);
1762 if (tdb_lock_bystring_with_timeout(tdb, NOTIFY_PID_LIST_KEY, 10) == -1) {
1763 DEBUG(0,("print_notify_register_pid: Failed to lock \
1764 printer %s database\n", printername));
1766 release_print_db(pdb);
1770 data = get_printer_notify_pid_list( tdb, printername, True );
1772 /* Reduce refcount. Remove ourselves if zero. */
1774 for (i = 0; i < data.dsize; ) {
1775 if (IVAL(data.dptr,i) == mypid) {
1776 uint32 refcount = IVAL(data.dptr, i+4);
1780 if (refcount == 0) {
1781 if (data.dsize - i > 8)
1782 memmove( &data.dptr[i], &data.dptr[i+8], data.dsize - i - 8);
1786 SIVAL(data.dptr, i+4, refcount);
1792 if (data.dsize == 0)
1793 SAFE_FREE(data.dptr);
1795 /* Store back the record. */
1796 if (tdb_store_bystring(tdb, NOTIFY_PID_LIST_KEY, data, TDB_REPLACE) == -1) {
1797 DEBUG(0,("print_notify_register_pid: Failed to update pid \
1798 list for printer %s\n", printername));
1806 tdb_unlock_bystring(tdb, NOTIFY_PID_LIST_KEY);
1808 release_print_db(pdb);
1809 SAFE_FREE(data.dptr);
1813 /****************************************************************************
1814 Check if a jobid is valid. It is valid if it exists in the database.
1815 ****************************************************************************/
1817 bool print_job_exists(const char* sharename, uint32 jobid)
1819 struct tdb_print_db *pdb = get_print_db_byname(sharename);
1825 ret = tdb_exists(pdb->tdb, print_key(jobid, &tmp));
1826 release_print_db(pdb);
1830 /****************************************************************************
1831 Give the fd used for a jobid.
1832 ****************************************************************************/
1834 int print_job_fd(const char* sharename, uint32 jobid)
1836 struct printjob *pjob = print_job_find(sharename, jobid);
1839 /* don't allow another process to get this info - it is meaningless */
1840 if (pjob->pid != sys_getpid())
1845 /****************************************************************************
1846 Give the filename used for a jobid.
1847 Only valid for the process doing the spooling and when the job
1848 has not been spooled.
1849 ****************************************************************************/
1851 char *print_job_fname(const char* sharename, uint32 jobid)
1853 struct printjob *pjob = print_job_find(sharename, jobid);
1854 if (!pjob || pjob->spooled || pjob->pid != sys_getpid())
1856 return pjob->filename;
1860 /****************************************************************************
1861 Give the filename used for a jobid.
1862 Only valid for the process doing the spooling and when the job
1863 has not been spooled.
1864 ****************************************************************************/
1866 NT_DEVICEMODE *print_job_devmode(const char* sharename, uint32 jobid)
1868 struct printjob *pjob = print_job_find(sharename, jobid);
1873 return pjob->nt_devmode;
1876 /****************************************************************************
1877 Set the place in the queue for a job.
1878 ****************************************************************************/
1880 bool print_job_set_place(const char *sharename, uint32 jobid, int place)
1882 DEBUG(2,("print_job_set_place not implemented yet\n"));
1886 /****************************************************************************
1887 Set the name of a job. Only possible for owner.
1888 ****************************************************************************/
1890 bool print_job_set_name(const char *sharename, uint32 jobid, char *name)
1892 struct printjob *pjob;
1894 pjob = print_job_find(sharename, jobid);
1895 if (!pjob || pjob->pid != sys_getpid())
1898 fstrcpy(pjob->jobname, name);
1899 return pjob_store(sharename, jobid, pjob);
1902 /***************************************************************************
1903 Remove a jobid from the 'jobs changed' list.
1904 ***************************************************************************/
1906 static bool remove_from_jobs_changed(const char* sharename, uint32 jobid)
1908 struct tdb_print_db *pdb = get_print_db_byname(sharename);
1910 size_t job_count, i;
1912 bool gotlock = False;
1920 key = string_tdb_data("INFO/jobs_changed");
1922 if (tdb_chainlock_with_timeout(pdb->tdb, key, 5) == -1)
1927 data = tdb_fetch(pdb->tdb, key);
1929 if (data.dptr == NULL || data.dsize == 0 || (data.dsize % 4 != 0))
1932 job_count = data.dsize / 4;
1933 for (i = 0; i < job_count; i++) {
1936 ch_jobid = IVAL(data.dptr, i*4);
1937 if (ch_jobid == jobid) {
1938 if (i < job_count -1 )
1939 memmove(data.dptr + (i*4), data.dptr + (i*4) + 4, (job_count - i - 1)*4 );
1941 if (tdb_store(pdb->tdb, key, data, TDB_REPLACE) == -1)
1951 tdb_chainunlock(pdb->tdb, key);
1952 SAFE_FREE(data.dptr);
1953 release_print_db(pdb);
1955 DEBUG(10,("remove_from_jobs_changed: removed jobid %u\n", (unsigned int)jobid ));
1957 DEBUG(10,("remove_from_jobs_changed: Failed to remove jobid %u\n", (unsigned int)jobid ));
1961 /****************************************************************************
1962 Delete a print job - don't update queue.
1963 ****************************************************************************/
1965 static bool print_job_delete1(int snum, uint32 jobid)
1967 const char* sharename = lp_const_servicename(snum);
1968 struct printjob *pjob = print_job_find(sharename, jobid);
1970 struct printif *current_printif = get_printer_fns( snum );
1976 * If already deleting just return.
1979 if (pjob->status == LPQ_DELETING)
1982 /* Hrm - we need to be able to cope with deleting a job before it
1983 has reached the spooler. Just mark it as LPQ_DELETING and
1984 let the print_queue_update() code rmeove the record */
1987 if (pjob->sysjob == -1) {
1988 DEBUG(5, ("attempt to delete job %u not seen by lpr\n", (unsigned int)jobid));
1991 /* Set the tdb entry to be deleting. */
1993 pjob->status = LPQ_DELETING;
1994 pjob_store(sharename, jobid, pjob);
1996 if (pjob->spooled && pjob->sysjob != -1)
1998 result = (*(current_printif->job_delete))(
2000 lp_lprmcommand(snum),
2003 /* Delete the tdb entry if the delete succeeded or the job hasn't
2007 struct tdb_print_db *pdb = get_print_db_byname(sharename);
2012 pjob_delete(sharename, jobid);
2013 /* Ensure we keep a rough count of the number of total jobs... */
2014 tdb_change_int32_atomic(pdb->tdb, "INFO/total_jobs", &njobs, -1);
2015 release_print_db(pdb);
2019 remove_from_jobs_changed( sharename, jobid );
2021 return (result == 0);
2024 /****************************************************************************
2025 Return true if the current user owns the print job.
2026 ****************************************************************************/
2028 static bool is_owner(struct auth_serversupplied_info *server_info,
2029 const char *servicename,
2032 struct printjob *pjob = print_job_find(servicename, jobid);
2034 if (!pjob || !server_info)
2037 return strequal(pjob->user, server_info->sanitized_username);
2040 /****************************************************************************
2042 ****************************************************************************/
2044 bool print_job_delete(struct auth_serversupplied_info *server_info, int snum,
2045 uint32 jobid, WERROR *errcode)
2047 const char* sharename = lp_const_servicename( snum );
2048 struct printjob *pjob;
2054 owner = is_owner(server_info, lp_const_servicename(snum), jobid);
2056 /* Check access against security descriptor or whether the user
2060 !print_access_check(server_info, snum, JOB_ACCESS_ADMINISTER)) {
2061 DEBUG(3, ("delete denied by security descriptor\n"));
2062 *errcode = WERR_ACCESS_DENIED;
2064 /* BEGIN_ADMIN_LOG */
2065 sys_adminlog( LOG_ERR,
2066 "Permission denied-- user not allowed to delete, \
2067 pause, or resume print job. User name: %s. Printer name: %s.",
2068 uidtoname(server_info->utok.uid),
2069 PRINTERNAME(snum) );
2076 * get the spooled filename of the print job
2077 * if this works, then the file has not been spooled
2078 * to the underlying print system. Just delete the
2079 * spool file & return.
2082 if ( (fname = print_job_fname( sharename, jobid )) != NULL )
2084 /* remove the spool file */
2085 DEBUG(10,("print_job_delete: Removing spool file [%s]\n", fname ));
2086 if ( unlink( fname ) == -1 ) {
2087 *errcode = map_werror_from_unix(errno);
2092 if (!print_job_delete1(snum, jobid)) {
2093 *errcode = WERR_ACCESS_DENIED;
2097 /* force update the database and say the delete failed if the
2100 print_queue_update(snum, True);
2102 pjob = print_job_find(sharename, jobid);
2103 if ( pjob && (pjob->status != LPQ_DELETING) )
2104 *errcode = WERR_ACCESS_DENIED;
2106 return (pjob == NULL );
2109 /****************************************************************************
2111 ****************************************************************************/
2113 bool print_job_pause(struct auth_serversupplied_info *server_info, int snum,
2114 uint32 jobid, WERROR *errcode)
2116 const char* sharename = lp_const_servicename(snum);
2117 struct printjob *pjob;
2119 struct printif *current_printif = get_printer_fns( snum );
2121 pjob = print_job_find(sharename, jobid);
2123 if (!pjob || !server_info) {
2124 DEBUG(10, ("print_job_pause: no pjob or user for jobid %u\n",
2125 (unsigned int)jobid ));
2129 if (!pjob->spooled || pjob->sysjob == -1) {
2130 DEBUG(10, ("print_job_pause: not spooled or bad sysjob = %d for jobid %u\n",
2131 (int)pjob->sysjob, (unsigned int)jobid ));
2135 if (!is_owner(server_info, lp_const_servicename(snum), jobid) &&
2136 !print_access_check(server_info, snum, JOB_ACCESS_ADMINISTER)) {
2137 DEBUG(3, ("pause denied by security descriptor\n"));
2139 /* BEGIN_ADMIN_LOG */
2140 sys_adminlog( LOG_ERR,
2141 "Permission denied-- user not allowed to delete, \
2142 pause, or resume print job. User name: %s. Printer name: %s.",
2143 uidtoname(server_info->utok.uid),
2144 PRINTERNAME(snum) );
2147 *errcode = WERR_ACCESS_DENIED;
2151 /* need to pause the spooled entry */
2152 ret = (*(current_printif->job_pause))(snum, pjob);
2155 *errcode = WERR_INVALID_PARAM;
2159 /* force update the database */
2160 print_cache_flush(lp_const_servicename(snum));
2162 /* Send a printer notify message */
2164 notify_job_status(sharename, jobid, JOB_STATUS_PAUSED);
2166 /* how do we tell if this succeeded? */
2171 /****************************************************************************
2173 ****************************************************************************/
2175 bool print_job_resume(struct auth_serversupplied_info *server_info, int snum,
2176 uint32 jobid, WERROR *errcode)
2178 const char *sharename = lp_const_servicename(snum);
2179 struct printjob *pjob;
2181 struct printif *current_printif = get_printer_fns( snum );
2183 pjob = print_job_find(sharename, jobid);
2185 if (!pjob || !server_info) {
2186 DEBUG(10, ("print_job_resume: no pjob or user for jobid %u\n",
2187 (unsigned int)jobid ));
2191 if (!pjob->spooled || pjob->sysjob == -1) {
2192 DEBUG(10, ("print_job_resume: not spooled or bad sysjob = %d for jobid %u\n",
2193 (int)pjob->sysjob, (unsigned int)jobid ));
2197 if (!is_owner(server_info, lp_const_servicename(snum), jobid) &&
2198 !print_access_check(server_info, snum, JOB_ACCESS_ADMINISTER)) {
2199 DEBUG(3, ("resume denied by security descriptor\n"));
2200 *errcode = WERR_ACCESS_DENIED;
2202 /* BEGIN_ADMIN_LOG */
2203 sys_adminlog( LOG_ERR,
2204 "Permission denied-- user not allowed to delete, \
2205 pause, or resume print job. User name: %s. Printer name: %s.",
2206 uidtoname(server_info->utok.uid),
2207 PRINTERNAME(snum) );
2212 ret = (*(current_printif->job_resume))(snum, pjob);
2215 *errcode = WERR_INVALID_PARAM;
2219 /* force update the database */
2220 print_cache_flush(lp_const_servicename(snum));
2222 /* Send a printer notify message */
2224 notify_job_status(sharename, jobid, JOB_STATUS_QUEUED);
2229 /****************************************************************************
2230 Write to a print file.
2231 ****************************************************************************/
2233 ssize_t print_job_write(int snum, uint32 jobid, const char *buf, SMB_OFF_T pos, size_t size)
2235 const char* sharename = lp_const_servicename(snum);
2237 struct printjob *pjob;
2239 pjob = print_job_find(sharename, jobid);
2243 /* don't allow another process to get this info - it is meaningless */
2244 if (pjob->pid != sys_getpid())
2247 return_code = write_data_at_offset(pjob->fd, buf, size, pos);
2249 if (return_code>0) {
2251 pjob_store(sharename, jobid, pjob);
2256 /****************************************************************************
2257 Get the queue status - do not update if db is out of date.
2258 ****************************************************************************/
2260 static int get_queue_status(const char* sharename, print_status_struct *status)
2264 struct tdb_print_db *pdb = get_print_db_byname(sharename);
2268 ZERO_STRUCTP(status);
2275 fstr_sprintf(keystr, "STATUS/%s", sharename);
2276 data = tdb_fetch(pdb->tdb, string_tdb_data(keystr));
2278 if (data.dsize == sizeof(print_status_struct))
2279 /* this memcpy is ok since the status struct was
2280 not packed before storing it in the tdb */
2281 memcpy(status, data.dptr, sizeof(print_status_struct));
2282 SAFE_FREE(data.dptr);
2285 len = tdb_fetch_int32(pdb->tdb, "INFO/total_jobs");
2286 release_print_db(pdb);
2287 return (len == -1 ? 0 : len);
2290 /****************************************************************************
2291 Determine the number of jobs in a queue.
2292 ****************************************************************************/
2294 int print_queue_length(int snum, print_status_struct *pstatus)
2296 const char* sharename = lp_const_servicename( snum );
2297 print_status_struct status;
2300 ZERO_STRUCT( status );
2302 /* make sure the database is up to date */
2303 if (print_cache_expired(lp_const_servicename(snum), True))
2304 print_queue_update(snum, False);
2306 /* also fetch the queue status */
2307 memset(&status, 0, sizeof(status));
2308 len = get_queue_status(sharename, &status);
2316 /***************************************************************************
2317 Allocate a jobid. Hold the lock for as short a time as possible.
2318 ***************************************************************************/
2320 static bool allocate_print_jobid(struct tdb_print_db *pdb, int snum, const char *sharename, uint32 *pjobid)
2325 *pjobid = (uint32)-1;
2327 for (i = 0; i < 3; i++) {
2328 /* Lock the database - only wait 20 seconds. */
2329 if (tdb_lock_bystring_with_timeout(pdb->tdb, "INFO/nextjob", 20) == -1) {
2330 DEBUG(0,("allocate_print_jobid: failed to lock printing database %s\n", sharename));
2334 if (!tdb_fetch_uint32(pdb->tdb, "INFO/nextjob", &jobid)) {
2335 if (tdb_error(pdb->tdb) != TDB_ERR_NOEXIST) {
2336 DEBUG(0, ("allocate_print_jobid: failed to fetch INFO/nextjob for print queue %s\n",
2343 jobid = NEXT_JOBID(jobid);
2345 if (tdb_store_int32(pdb->tdb, "INFO/nextjob", jobid)==-1) {
2346 DEBUG(3, ("allocate_print_jobid: failed to store INFO/nextjob.\n"));
2347 tdb_unlock_bystring(pdb->tdb, "INFO/nextjob");
2351 /* We've finished with the INFO/nextjob lock. */
2352 tdb_unlock_bystring(pdb->tdb, "INFO/nextjob");
2354 if (!print_job_exists(sharename, jobid))
2359 DEBUG(0, ("allocate_print_jobid: failed to allocate a print job for queue %s\n",
2361 /* Probably full... */
2366 /* Store a dummy placeholder. */
2372 if (tdb_store(pdb->tdb, print_key(jobid, &tmp), dum,
2373 TDB_INSERT) == -1) {
2374 DEBUG(3, ("allocate_print_jobid: jobid (%d) failed to store placeholder.\n",
2384 /***************************************************************************
2385 Append a jobid to the 'jobs changed' list.
2386 ***************************************************************************/
2388 static bool add_to_jobs_changed(struct tdb_print_db *pdb, uint32 jobid)
2393 SIVAL(&store_jobid, 0, jobid);
2394 data.dptr = (uint8 *)&store_jobid;
2397 DEBUG(10,("add_to_jobs_changed: Added jobid %u\n", (unsigned int)jobid ));
2399 return (tdb_append(pdb->tdb, string_tdb_data("INFO/jobs_changed"),
2403 /***************************************************************************
2404 Start spooling a job - return the jobid.
2405 ***************************************************************************/
2407 uint32 print_job_start(struct auth_serversupplied_info *server_info, int snum,
2408 char *jobname, NT_DEVICEMODE *nt_devmode )
2412 struct printjob pjob;
2413 const char *sharename = lp_const_servicename(snum);
2414 struct tdb_print_db *pdb = get_print_db_byname(sharename);
2422 if (!print_access_check(server_info, snum, PRINTER_ACCESS_USE)) {
2423 DEBUG(3, ("print_job_start: job start denied by security descriptor\n"));
2424 release_print_db(pdb);
2428 if (!print_time_access_check(lp_servicename(snum))) {
2429 DEBUG(3, ("print_job_start: job start denied by time check\n"));
2430 release_print_db(pdb);
2434 path = lp_pathname(snum);
2436 /* see if we have sufficient disk space */
2437 if (lp_minprintspace(snum)) {
2438 uint64_t dspace, dsize;
2439 if (sys_fsusage(path, &dspace, &dsize) == 0 &&
2440 dspace < 2*(uint64_t)lp_minprintspace(snum)) {
2441 DEBUG(3, ("print_job_start: disk space check failed.\n"));
2442 release_print_db(pdb);
2448 /* for autoloaded printers, check that the printcap entry still exists */
2449 if (lp_autoloaded(snum) && !pcap_printername_ok(lp_const_servicename(snum))) {
2450 DEBUG(3, ("print_job_start: printer name %s check failed.\n", lp_const_servicename(snum) ));
2451 release_print_db(pdb);
2456 /* Insure the maximum queue size is not violated */
2457 if ((njobs = print_queue_length(snum,NULL)) > lp_maxprintjobs(snum)) {
2458 DEBUG(3, ("print_job_start: Queue %s number of jobs (%d) larger than max printjobs per queue (%d).\n",
2459 sharename, njobs, lp_maxprintjobs(snum) ));
2460 release_print_db(pdb);
2465 DEBUG(10,("print_job_start: Queue %s number of jobs (%d), max printjobs = %d\n",
2466 sharename, njobs, lp_maxprintjobs(snum) ));
2468 if (!allocate_print_jobid(pdb, snum, sharename, &jobid))
2471 /* create the database entry */
2475 pjob.pid = sys_getpid();
2478 pjob.starttime = time(NULL);
2479 pjob.status = LPQ_SPOOLING;
2481 pjob.spooled = False;
2483 pjob.nt_devmode = nt_devmode;
2485 fstrcpy(pjob.jobname, jobname);
2487 fstrcpy(pjob.user, lp_printjob_username(snum));
2488 standard_sub_advanced(sharename, server_info->sanitized_username,
2489 path, server_info->utok.gid,
2490 server_info->sanitized_username,
2491 pdb_get_domain(server_info->sam_account),
2492 pjob.user, sizeof(pjob.user)-1);
2493 /* ensure NULL termination */
2494 pjob.user[sizeof(pjob.user)-1] = '\0';
2496 fstrcpy(pjob.queuename, lp_const_servicename(snum));
2498 /* we have a job entry - now create the spool file */
2499 slprintf(pjob.filename, sizeof(pjob.filename)-1, "%s/%s%.8u.XXXXXX",
2500 path, PRINT_SPOOL_PREFIX, (unsigned int)jobid);
2501 pjob.fd = smb_mkstemp(pjob.filename);
2503 if (pjob.fd == -1) {
2504 if (errno == EACCES) {
2505 /* Common setup error, force a report. */
2506 DEBUG(0, ("print_job_start: insufficient permissions \
2507 to open spool file %s.\n", pjob.filename));
2509 /* Normal case, report at level 3 and above. */
2510 DEBUG(3, ("print_job_start: can't open spool file %s,\n", pjob.filename));
2511 DEBUGADD(3, ("errno = %d (%s).\n", errno, strerror(errno)));
2516 pjob_store(sharename, jobid, &pjob);
2518 /* Update the 'jobs changed' entry used by print_queue_status. */
2519 add_to_jobs_changed(pdb, jobid);
2521 /* Ensure we keep a rough count of the number of total jobs... */
2522 tdb_change_int32_atomic(pdb->tdb, "INFO/total_jobs", &njobs, 1);
2524 release_print_db(pdb);
2530 pjob_delete(sharename, jobid);
2532 release_print_db(pdb);
2534 DEBUG(3, ("print_job_start: returning fail. Error = %s\n", strerror(errno) ));
2538 /****************************************************************************
2539 Update the number of pages spooled to jobid
2540 ****************************************************************************/
2542 void print_job_endpage(int snum, uint32 jobid)
2544 const char* sharename = lp_const_servicename(snum);
2545 struct printjob *pjob;
2547 pjob = print_job_find(sharename, jobid);
2550 /* don't allow another process to get this info - it is meaningless */
2551 if (pjob->pid != sys_getpid())
2555 pjob_store(sharename, jobid, pjob);
2558 /****************************************************************************
2559 Print a file - called on closing the file. This spools the job.
2560 If normal close is false then we're tearing down the jobs - treat as an
2562 ****************************************************************************/
2564 bool print_job_end(int snum, uint32 jobid, enum file_close_type close_type)
2566 const char* sharename = lp_const_servicename(snum);
2567 struct printjob *pjob;
2569 SMB_STRUCT_STAT sbuf;
2570 struct printif *current_printif = get_printer_fns( snum );
2572 pjob = print_job_find(sharename, jobid);
2577 if (pjob->spooled || pjob->pid != sys_getpid())
2580 if ((close_type == NORMAL_CLOSE || close_type == SHUTDOWN_CLOSE) &&
2581 (sys_fstat(pjob->fd, &sbuf) == 0)) {
2582 pjob->size = sbuf.st_size;
2588 * Not a normal close or we couldn't stat the job file,
2589 * so something has gone wrong. Cleanup.
2593 DEBUG(3,("print_job_end: failed to stat file for jobid %d\n", jobid ));
2597 /* Technically, this is not quite right. If the printer has a separator
2598 * page turned on, the NT spooler prints the separator page even if the
2599 * print job is 0 bytes. 010215 JRR */
2600 if (pjob->size == 0 || pjob->status == LPQ_DELETING) {
2601 /* don't bother spooling empty files or something being deleted. */
2602 DEBUG(5,("print_job_end: canceling spool of %s (%s)\n",
2603 pjob->filename, pjob->size ? "deleted" : "zero length" ));
2604 unlink(pjob->filename);
2605 pjob_delete(sharename, jobid);
2609 pjob->smbjob = jobid;
2611 ret = (*(current_printif->job_submit))(snum, pjob);
2616 /* The print job has been successfully handed over to the back-end */
2618 pjob->spooled = True;
2619 pjob->status = LPQ_QUEUED;
2620 pjob_store(sharename, jobid, pjob);
2622 /* make sure the database is up to date */
2623 if (print_cache_expired(lp_const_servicename(snum), True))
2624 print_queue_update(snum, False);
2630 /* The print job was not successfully started. Cleanup */
2631 /* Still need to add proper error return propagation! 010122:JRR */
2632 unlink(pjob->filename);
2633 pjob_delete(sharename, jobid);
2637 /****************************************************************************
2638 Get a snapshot of jobs in the system without traversing.
2639 ****************************************************************************/
2641 static bool get_stored_queue_info(struct tdb_print_db *pdb, int snum, int *pcount, print_queue_struct **ppqueue)
2643 TDB_DATA data, cgdata;
2644 print_queue_struct *queue = NULL;
2646 uint32 extra_count = 0;
2647 int total_count = 0;
2650 int max_reported_jobs = lp_max_reported_jobs(snum);
2652 const char* sharename = lp_servicename(snum);
2654 /* make sure the database is up to date */
2655 if (print_cache_expired(lp_const_servicename(snum), True))
2656 print_queue_update(snum, False);
2662 ZERO_STRUCT(cgdata);
2664 /* Get the stored queue data. */
2665 data = tdb_fetch(pdb->tdb, string_tdb_data("INFO/linear_queue_array"));
2667 if (data.dptr && data.dsize >= sizeof(qcount))
2668 len += tdb_unpack(data.dptr + len, data.dsize - len, "d", &qcount);
2670 /* Get the changed jobs list. */
2671 cgdata = tdb_fetch(pdb->tdb, string_tdb_data("INFO/jobs_changed"));
2672 if (cgdata.dptr != NULL && (cgdata.dsize % 4 == 0))
2673 extra_count = cgdata.dsize/4;
2675 DEBUG(5,("get_stored_queue_info: qcount = %u, extra_count = %u\n", (unsigned int)qcount, (unsigned int)extra_count));
2677 /* Allocate the queue size. */
2678 if (qcount == 0 && extra_count == 0)
2681 if ((queue = SMB_MALLOC_ARRAY(print_queue_struct, qcount + extra_count)) == NULL)
2684 /* Retrieve the linearised queue data. */
2686 for( i = 0; i < qcount; i++) {
2687 uint32 qjob, qsize, qpage_count, qstatus, qpriority, qtime;
2688 len += tdb_unpack(data.dptr + len, data.dsize - len, "ddddddff",
2697 queue[i].job = qjob;
2698 queue[i].size = qsize;
2699 queue[i].page_count = qpage_count;
2700 queue[i].status = qstatus;
2701 queue[i].priority = qpriority;
2702 queue[i].time = qtime;
2705 total_count = qcount;
2707 /* Add in the changed jobids. */
2708 for( i = 0; i < extra_count; i++) {
2710 struct printjob *pjob;
2712 jobid = IVAL(cgdata.dptr, i*4);
2713 DEBUG(5,("get_stored_queue_info: changed job = %u\n", (unsigned int)jobid));
2714 pjob = print_job_find(lp_const_servicename(snum), jobid);
2716 DEBUG(5,("get_stored_queue_info: failed to find changed job = %u\n", (unsigned int)jobid));
2717 remove_from_jobs_changed(sharename, jobid);
2721 queue[total_count].job = jobid;
2722 queue[total_count].size = pjob->size;
2723 queue[total_count].page_count = pjob->page_count;
2724 queue[total_count].status = pjob->status;
2725 queue[total_count].priority = 1;
2726 queue[total_count].time = pjob->starttime;
2727 fstrcpy(queue[total_count].fs_user, pjob->user);
2728 fstrcpy(queue[total_count].fs_file, pjob->jobname);
2732 /* Sort the queue by submission time otherwise they are displayed
2735 qsort(queue, total_count, sizeof(print_queue_struct), QSORT_CAST(printjob_comp));
2737 DEBUG(5,("get_stored_queue_info: total_count = %u\n", (unsigned int)total_count));
2739 if (max_reported_jobs && total_count > max_reported_jobs)
2740 total_count = max_reported_jobs;
2743 *pcount = total_count;
2749 SAFE_FREE(data.dptr);
2750 SAFE_FREE(cgdata.dptr);
2754 /****************************************************************************
2755 Get a printer queue listing.
2756 set queue = NULL and status = NULL if you just want to update the cache
2757 ****************************************************************************/
2759 int print_queue_status(int snum,
2760 print_queue_struct **ppqueue,
2761 print_status_struct *status)
2765 const char *sharename;
2766 struct tdb_print_db *pdb;
2769 /* make sure the database is up to date */
2771 if (print_cache_expired(lp_const_servicename(snum), True))
2772 print_queue_update(snum, False);
2774 /* return if we are done */
2775 if ( !ppqueue || !status )
2779 sharename = lp_const_servicename(snum);
2780 pdb = get_print_db_byname(sharename);
2786 * Fetch the queue status. We must do this first, as there may
2787 * be no jobs in the queue.
2790 ZERO_STRUCTP(status);
2791 slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", sharename);
2792 key = string_tdb_data(keystr);
2794 data = tdb_fetch(pdb->tdb, key);
2796 if (data.dsize == sizeof(*status)) {
2797 /* this memcpy is ok since the status struct was
2798 not packed before storing it in the tdb */
2799 memcpy(status, data.dptr, sizeof(*status));
2801 SAFE_FREE(data.dptr);
2805 * Now, fetch the print queue information. We first count the number
2806 * of entries, and then only retrieve the queue if necessary.
2809 if (!get_stored_queue_info(pdb, snum, &count, ppqueue)) {
2810 release_print_db(pdb);
2814 release_print_db(pdb);
2818 /****************************************************************************
2820 ****************************************************************************/
2822 bool print_queue_pause(struct auth_serversupplied_info *server_info, int snum,
2826 struct printif *current_printif = get_printer_fns( snum );
2828 if (!print_access_check(server_info, snum,
2829 PRINTER_ACCESS_ADMINISTER)) {
2830 *errcode = WERR_ACCESS_DENIED;
2837 ret = (*(current_printif->queue_pause))(snum);
2842 *errcode = WERR_INVALID_PARAM;
2846 /* force update the database */
2847 print_cache_flush(lp_const_servicename(snum));
2849 /* Send a printer notify message */
2851 notify_printer_status(snum, PRINTER_STATUS_PAUSED);
2856 /****************************************************************************
2858 ****************************************************************************/
2860 bool print_queue_resume(struct auth_serversupplied_info *server_info, int snum,
2864 struct printif *current_printif = get_printer_fns( snum );
2866 if (!print_access_check(server_info, snum,
2867 PRINTER_ACCESS_ADMINISTER)) {
2868 *errcode = WERR_ACCESS_DENIED;
2874 ret = (*(current_printif->queue_resume))(snum);
2879 *errcode = WERR_INVALID_PARAM;
2883 /* make sure the database is up to date */
2884 if (print_cache_expired(lp_const_servicename(snum), True))
2885 print_queue_update(snum, True);
2887 /* Send a printer notify message */
2889 notify_printer_status(snum, PRINTER_STATUS_OK);
2894 /****************************************************************************
2895 Purge a queue - implemented by deleting all jobs that we can delete.
2896 ****************************************************************************/
2898 bool print_queue_purge(struct auth_serversupplied_info *server_info, int snum,
2901 print_queue_struct *queue;
2902 print_status_struct status;
2906 /* Force and update so the count is accurate (i.e. not a cached count) */
2907 print_queue_update(snum, True);
2909 can_job_admin = print_access_check(server_info, snum,
2910 JOB_ACCESS_ADMINISTER);
2911 njobs = print_queue_status(snum, &queue, &status);
2913 if ( can_job_admin )
2916 for (i=0;i<njobs;i++) {
2917 bool owner = is_owner(server_info, lp_const_servicename(snum),
2920 if (owner || can_job_admin) {
2921 print_job_delete1(snum, queue[i].job);
2925 if ( can_job_admin )
2928 /* update the cache */
2929 print_queue_update( snum, True );