jobids are assigned when a job starts spooling.
*/
-static pid_t local_pid;
-
-/* Mapping between printer names and queue id's in job id's. */
-struct printer_queueid_map {
- struct printer_queueid_map *next, *prev;
- char *printername;
- uint32 queueid;
-};
-
-static struct printer_queueid_map *printer_queueid_map_head;
-static uint32 last_queueid;
-
-#define QUEUEID_BITS 12
-#define QUEUEID_MASK ((1<<(QUEUEID_BITS))-1)
-#define QUEUEID_TO_JOBID(queueid) (((queueid) & QUEUEID_MASK) << 20 )
+/***************************************************************************
+ Nightmare. LANMAN jobid's are 16 bit numbers..... We must map them to 32
+ bit RPC jobids.... JRA.
+***************************************************************************/
-/****************************************************************************
- Create an association between a printer name and a queueid. Used to encode
- the printer queueid in jobid's.
- This could be converted to use an internal tdb if searching the list is
- too slow. JRA.
-****************************************************************************/
+static TDB_CONTEXT *rap_tdb;
+static uint16 next_rap_jobid;
-BOOL create_printer_queueid(const char *printername)
+uint16 pjobid_to_rap(int snum, uint32 jobid)
{
- struct printer_queueid_map *p;
+ uint16 rap_jobid;
+ TDB_DATA data, key;
+ char jinfo[8];
- for (p = printer_queueid_map_head; p; p = p->next) {
- if (strequal(p->printername, printername))
- return True;
+ if (!rap_tdb) {
+ /* Create the in-memory tdb. */
+ rap_tdb = tdb_open_log(NULL, 0, TDB_INTERNAL, (O_RDWR|O_CREAT), 0644);
+ if (!rap_tdb)
+ return 0;
}
- p = (struct printer_queueid_map *)malloc(sizeof(*p));
- if (!p) {
- DEBUG(0,("create_printer_queueid: malloc fail !\n"));
- return False;
- }
- ZERO_STRUCTP(p);
- p->printername = strdup(printername);
- if (!p->printername) {
- DEBUG(0,("create_printer_queueid: malloc fail !\n"));
- SAFE_FREE(p);
- return False;
- }
- p->queueid = (++last_queueid);
- if (p->queueid > QUEUEID_MASK) {
- DEBUG(0,("create_printer_queueid: malloc fail !\n"));
- SAFE_FREE(p->printername);
- SAFE_FREE(p);
- return False;
+ SIVAL(&jinfo,0,(int32)snum);
+ SIVAL(&jinfo,4,jobid);
+
+ key.dptr = (char *)&jinfo;
+ key.dsize = sizeof(jinfo);
+ data = tdb_fetch(rap_tdb, key);
+ if (data.dptr && data.dsize == sizeof(uint16)) {
+ memcpy(&rap_jobid, data.dptr, sizeof(uint16));
+ SAFE_FREE(data.dptr);
+ return rap_jobid;
}
- DLIST_ADD(printer_queueid_map_head, p);
- return True;
+ /* Not found - create and store mapping. */
+ rap_jobid = ++next_rap_jobid;
+ if (rap_jobid == 0)
+ rap_jobid = ++next_rap_jobid;
+ data.dptr = (char *)&rap_jobid;
+ data.dsize = sizeof(rap_jobid);
+ tdb_store(rap_tdb, key, data, TDB_REPLACE);
+ tdb_store(rap_tdb, data, key, TDB_REPLACE);
+ return rap_jobid;
}
-void set_register_printer_fn(void)
+BOOL rap_to_pjobid(uint16 rap_jobid, int *psnum, uint32 *pjobid)
{
- extern BOOL (*register_printer_fn)(const char *);
- register_printer_fn = create_printer_queueid;
-}
-
-/****************************************************************************
- Lookups.
-****************************************************************************/
+ TDB_DATA data, key;
+ char jinfo[8];
-static uint32 get_printer_queueid_byname(const char *printername)
-{
- struct printer_queueid_map *p;
+ if (!rap_tdb)
+ return False;
- for (p = printer_queueid_map_head; p; p = p->next) {
- if (strequal(p->printername, printername))
- return p->queueid;
+ key.dptr = (char *)&rap_jobid;
+ key.dsize = sizeof(rap_jobid);
+ data = tdb_fetch(rap_tdb, key);
+ if (data.dptr && data.dsize == sizeof(jinfo)) {
+ *psnum = IVAL(&jinfo,0);
+ *pjobid = IVAL(&jinfo,4);
+ SAFE_FREE(data.dptr);
+ return True;
}
- return 0;
+ return False;
}
-/****************************************************************************
- Lookups.
-****************************************************************************/
-
-static const char *get_printer_name_byjobid(uint32 jobid)
+static void rap_jobid_delete(int snum, uint32 jobid)
{
- struct printer_queueid_map *p;
- uint32 queueid = (((jobid)>>20) & QUEUEID_MASK);
+ TDB_DATA key, data;
+ uint16 rap_jobid;
+ char jinfo[8];
- for (p = printer_queueid_map_head; p; p = p->next) {
- if (p->queueid == queueid)
- return p->printername;
- }
- return NULL;
+ if (!rap_tdb)
+ return;
+
+ SIVAL(&jinfo,0,(int32)snum);
+ SIVAL(&jinfo,4,jobid);
+
+ key.dptr = (char *)&jinfo;
+ key.dsize = sizeof(jinfo);
+ data = tdb_fetch(rap_tdb, key);
+ if (!data.dptr || (data.dsize != sizeof(uint16)))
+ return;
+
+ memcpy(&rap_jobid, data.dptr, sizeof(uint16));
+ SAFE_FREE(data.dptr);
+ data.dptr = (char *)&rap_jobid;
+ data.dsize = sizeof(rap_jobid);
+ tdb_delete(rap_tdb, key);
+ tdb_delete(rap_tdb, data);
}
+static pid_t local_pid;
+
static int get_queue_status(int, print_status_struct *);
+/* There can be this many printing tdb's open, plus any locked ones. */
#define MAX_PRINT_DBS_OPEN 1
struct tdb_print_db {
struct tdb_print_db *next, *prev;
TDB_CONTEXT *tdb;
+ int ref_count;
fstring printer_name;
};
static struct tdb_print_db *get_print_db_byname(const char *printername)
{
- struct tdb_print_db *p, *last_entry;
+ struct tdb_print_db *p = NULL, *last_entry = NULL;
int num_open = 0;
pstring printdb_path;
+ BOOL done_become_root = False;
for (p = print_db_head, last_entry = print_db_head; p; p = p->next) {
+ /* Ensure the list terminates... JRA. */
+ SMB_ASSERT(p->next != print_db_head);
+
if (p->tdb && strequal(p->printer_name, printername)) {
DLIST_PROMOTE(print_db_head, p);
+ p->ref_count++;
return p;
}
num_open++;
last_entry = p;
}
+
/* Not found. */
if (num_open >= MAX_PRINT_DBS_OPEN) {
- /* Recycle the last entry. */
+ /* Try and recycle the last entry. */
DLIST_PROMOTE(print_db_head, last_entry);
- if (print_db_head->tdb) {
- if (tdb_close(print_db_head->tdb)) {
- DEBUG(0,("get_print_db: Failed to close tdb for printer %s\n",
- print_db_head->printer_name ));
- return NULL;
+
+ for (p = print_db_head; p; p = p->next) {
+ if (p->ref_count)
+ continue;
+ if (p->tdb) {
+ if (tdb_close(print_db_head->tdb)) {
+ DEBUG(0,("get_print_db: Failed to close tdb for printer %s\n",
+ print_db_head->printer_name ));
+ return NULL;
+ }
}
+ p->tdb = NULL;
+ p->ref_count = 0;
+ memset(p->printer_name, '\0', sizeof(p->printer_name));
+ break;
}
- p = print_db_head;
- ZERO_STRUCTP(p);
- } else {
+ if (p) {
+ DLIST_PROMOTE(print_db_head, p);
+ p = print_db_head;
+ }
+ }
+
+ if (!p) {
/* Create one. */
p = (struct tdb_print_db *)malloc(sizeof(struct tdb_print_db));
if (!p) {
pstrcpy(printdb_path, lock_path("printing/"));
pstrcat(printdb_path, printername);
pstrcat(printdb_path, ".tdb");
+
+ if (geteuid() != 0) {
+ become_root();
+ done_become_root = True;
+ }
+
p->tdb = tdb_open_log(printdb_path, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
+
+ if (done_become_root)
+ unbecome_root();
+
if (!p->tdb) {
DEBUG(0,("get_print_db: Failed to open printer backend database %s.\n",
printdb_path ));
return NULL;
}
fstrcpy(p->printer_name, printername);
+ p->ref_count++;
return p;
}
-static struct tdb_print_db *get_print_db_byjobid( uint32 jobid)
+/***************************************************************************
+ Remove a reference count.
+****************************************************************************/
+
+static void release_print_db( struct tdb_print_db *pdb)
{
- const char *printername = get_printer_name_byjobid(jobid);
- if (!printername)
- return NULL;
- return get_print_db_byname(printername);
+ pdb->ref_count--;
+ SMB_ASSERT(pdb->ref_count >= 0);
+}
+
+/***************************************************************************
+ Close all open print db entries.
+****************************************************************************/
+
+static void close_all_print_db(void)
+{
+ struct tdb_print_db *p = NULL, *next_p = NULL;
+
+ for (p = print_db_head; p; p = next_p) {
+ next_p = p->next;
+
+ if (p->tdb)
+ tdb_close(p->tdb);
+ DLIST_REMOVE(print_db_head, p);
+ ZERO_STRUCTP(p);
+ SAFE_FREE(p);
+ }
}
/****************************************************************************
- Initialise the printing backend. Called once at startup.
- Does not survive a fork
+ Initialise the printing backend. Called once at startup before the fork().
****************************************************************************/
BOOL print_backend_init(void)
{
- struct printer_queueid_map *p;
char *sversion = "INFO/version";
pstring printing_path;
+ int services = lp_numservices();
+ int snum;
if (local_pid == sys_getpid())
return True;
/* handle a Samba upgrade */
- for (p = printer_queueid_map_head; p; p = p->next) {
- struct tdb_print_db *pdb = get_print_db_byname(p->printername);
+ for (snum = 0; snum < services; snum++) {
+ struct tdb_print_db *pdb;
+ if (!lp_print_ok(snum))
+ continue;
+ pdb = get_print_db_byname(lp_const_servicename(snum));
if (!pdb)
continue;
- tdb_lock_bystring(pdb->tdb, sversion);
+ if (tdb_lock_bystring(pdb->tdb, sversion, 0) == -1) {
+ DEBUG(0,("print_backend_init: Failed to open printer %s database\n", lp_const_servicename(snum) ));
+ release_print_db(pdb);
+ return False;
+ }
if (tdb_fetch_int32(pdb->tdb, sversion) != PRINT_DATABASE_VERSION) {
tdb_traverse(pdb->tdb, tdb_traverse_delete_fn, NULL);
tdb_store_int32(pdb->tdb, sversion, PRINT_DATABASE_VERSION);
}
tdb_unlock_bystring(pdb->tdb, sversion);
+ release_print_db(pdb);
}
+ close_all_print_db(); /* Don't leave any open. */
+
/* select the appropriate printing interface... */
#ifdef HAVE_CUPS
if (strcmp(lp_printcapname(), "cups") == 0)
void printing_end(void)
{
- struct tdb_print_db *p;
-
- for (p = print_db_head; p; ) {
- struct tdb_print_db *next_p = p->next;
- if (p->tdb)
- tdb_close(p->tdb);
- DLIST_REMOVE(print_db_head, p);
- SAFE_FREE(p);
- p = next_p;
- }
+ close_all_print_db(); /* Don't leave any open. */
}
/****************************************************************************
return ret;
}
+/***********************************************************************
+ unpack a pjob from a tdb buffer
+***********************************************************************/
+
+int unpack_pjob( char* buf, int buflen, struct printjob *pjob )
+{
+ int len = 0;
+ int used;
+
+ if ( !buf || !pjob )
+ return -1;
+
+ len += tdb_unpack(buf+len, buflen-len, "dddddddddffff",
+ &pjob->pid,
+ &pjob->sysjob,
+ &pjob->fd,
+ &pjob->starttime,
+ &pjob->status,
+ &pjob->size,
+ &pjob->page_count,
+ &pjob->spooled,
+ &pjob->smbjob,
+ pjob->filename,
+ pjob->jobname,
+ pjob->user,
+ pjob->queuename);
+
+ if ( len == -1 )
+ return -1;
+
+ if ( (used = unpack_devicemode(&pjob->nt_devmode, buf+len, buflen-len)) == -1 )
+ return -1;
+
+ len += used;
+
+ return len;
+
+}
+
/****************************************************************************
Useful function to find a print job in the database.
****************************************************************************/
-static struct printjob *print_job_find(uint32 jobid)
+static struct printjob *print_job_find(int snum, uint32 jobid)
{
- static struct printjob pjob;
- TDB_DATA ret;
- struct tdb_print_db *pdb = get_print_db_byjobid(jobid);
+ static struct printjob pjob;
+ TDB_DATA ret;
+ struct tdb_print_db *pdb = get_print_db_byname(lp_const_servicename(snum));
+
if (!pdb)
return NULL;
ret = tdb_fetch(pdb->tdb, print_key(jobid));
- if (!ret.dptr || ret.dsize != sizeof(pjob))
- return NULL;
+ release_print_db(pdb);
- memcpy(&pjob, ret.dptr, sizeof(pjob));
- SAFE_FREE(ret.dptr);
+ if (!ret.dptr)
+ return NULL;
+
+ if ( pjob.nt_devmode )
+ free_nt_devicemode( &pjob.nt_devmode );
+
+ ZERO_STRUCT( pjob );
+
+ if ( unpack_pjob( ret.dptr, ret.dsize, &pjob ) == -1 )
+ return NULL;
+
+ SAFE_FREE(ret.dptr);
return &pjob;
}
static int unixjob_traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA key,
TDB_DATA data, void *state)
{
- struct printjob *pjob = (struct printjob *)data.dptr;
+ struct printjob *pjob;
int *sysjob = (int *)state;
+ if (!data.dptr || data.dsize == 0)
+ return 0;
+
+ pjob = (struct printjob *)data.dptr;
if (key.dsize != sizeof(uint32))
return 0;
uint32 sysjob_to_jobid(int unix_jobid)
{
- struct printer_queueid_map *p;
+ int services = lp_numservices();
+ int snum;
+
sysjob_to_jobid_value = (uint32)-1;
- for (p = printer_queueid_map_head; p; p = p->next) {
- struct tdb_print_db *pdb = get_print_db_byname(p->printername);
+ for (snum = 0; snum < services; snum++) {
+ struct tdb_print_db *pdb;
+ if (!lp_print_ok(snum))
+ continue;
+ pdb = get_print_db_byname(lp_const_servicename(snum));
if (pdb)
tdb_traverse(pdb->tdb, unixjob_traverse_fn, &unix_jobid);
+ release_print_db(pdb);
if (sysjob_to_jobid_value != (uint32)-1)
return sysjob_to_jobid_value;
}
return 0;
}
-static void pjob_store_notify(uint32 jobid, struct printjob *old_data,
+static void pjob_store_notify(int snum, uint32 jobid, struct printjob *old_data,
struct printjob *new_data)
{
BOOL new_job = False;
- int snum = print_job_snum(jobid);
-
- if (snum == -1)
- return;
if (!old_data)
new_job = True;
Store a job structure back to the database.
****************************************************************************/
-static BOOL pjob_store(uint32 jobid, struct printjob *pjob)
+static BOOL pjob_store(int snum, uint32 jobid, struct printjob *pjob)
{
- TDB_DATA old_data, new_data;
- BOOL ret;
- struct tdb_print_db *pdb = get_print_db_byjobid(jobid);
+ TDB_DATA old_data, new_data;
+ BOOL ret = False;
+ struct tdb_print_db *pdb = get_print_db_byname(lp_const_servicename(snum));
+ char *buf = NULL;
+ int len, newlen, buflen;
+
if (!pdb)
return False;
old_data = tdb_fetch(pdb->tdb, print_key(jobid));
+ /* Doh! Now we have to pack/unpack data since the NT_DEVICEMODE was added */
+
+ newlen = 0;
+
+ do {
+ len = 0;
+ buflen = newlen;
+ len += tdb_pack(buf+len, buflen-len, "dddddddddffff",
+ pjob->pid,
+ pjob->sysjob,
+ pjob->fd,
+ pjob->starttime,
+ pjob->status,
+ pjob->size,
+ pjob->page_count,
+ pjob->spooled,
+ pjob->smbjob,
+ pjob->filename,
+ pjob->jobname,
+ pjob->user,
+ pjob->queuename);
+
+ len += pack_devicemode(pjob->nt_devmode, buf+len, buflen-len);
+
+ if (buflen != len)
+ {
+ char *tb;
+
+ tb = (char *)Realloc(buf, len);
+ if (!tb) {
+ DEBUG(0,("pjob_store: failed to enlarge buffer!\n"));
+ goto done;
+ }
+ else
+ buf = tb;
+ newlen = len;
+ }
+ }
+ while ( buflen != len );
+
+
/* Store new data */
- new_data.dptr = (void *)pjob;
- new_data.dsize = sizeof(*pjob);
+ new_data.dptr = buf;
+ new_data.dsize = len;
ret = (tdb_store(pdb->tdb, print_key(jobid), new_data, TDB_REPLACE) == 0);
+ release_print_db(pdb);
+
/* Send notify updates for what has changed */
- if (ret && (old_data.dsize == 0 || old_data.dsize == sizeof(*pjob))) {
- pjob_store_notify(
- jobid, (struct printjob *)old_data.dptr,
- (struct printjob *)new_data.dptr);
- free(old_data.dptr);
- }
+ if ( ret && (old_data.dsize == 0 || old_data.dsize == sizeof(*pjob)) )
+ pjob_store_notify( snum, jobid, (struct printjob *)old_data.dptr, pjob );
+
+done:
+ SAFE_FREE( old_data.dptr );
+ SAFE_FREE( buf );
return ret;
}
Remove a job structure from the database.
****************************************************************************/
-static void pjob_delete(uint32 jobid)
+static void pjob_delete(int snum, uint32 jobid)
{
- int snum;
- struct printjob *pjob = print_job_find(jobid);
+ struct printjob *pjob = print_job_find(snum, jobid);
uint32 job_status = 0;
- struct tdb_print_db *pdb = get_print_db_byjobid(jobid);
+ struct tdb_print_db *pdb = get_print_db_byname(lp_const_servicename(snum));
if (!pdb)
return;
if (!pjob) {
DEBUG(5, ("pjob_delete(): we were asked to delete nonexistent job %u\n",
(unsigned int)jobid));
+ release_print_db(pdb);
return;
}
JOB_STATUS_DELETED for the port monitor to delete the job
properly. */
- snum = print_job_snum(jobid);
job_status |= JOB_STATUS_DELETING;
notify_job_status(snum, jobid, job_status);
/* Remove from printing.tdb */
tdb_delete(pdb->tdb, print_key(jobid));
+ release_print_db(pdb);
+ rap_jobid_delete(snum, jobid);
}
/****************************************************************************
static void print_unix_job(int snum, print_queue_struct *q)
{
- uint32 queueid = get_printer_queueid_byname(PRINTERNAME(snum));
- uint32 jobid = (q->job + UNIX_JOB_START) | QUEUEID_TO_JOBID(queueid);
+ uint32 jobid = q->job + UNIX_JOB_START;
struct printjob pj, *old_pj;
/* Preserve the timestamp on an existing unix print job */
- old_pj = print_job_find(jobid);
+ old_pj = print_job_find(snum, jobid);
ZERO_STRUCT(pj);
fstrcpy(pj.user, q->fs_user);
fstrcpy(pj.queuename, lp_const_servicename(snum));
- pjob_store(jobid, &pj);
+ pjob_store(snum, jobid, &pj);
}
static int traverse_fn_delete(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
{
- uint32 queueid;
struct traverse_struct *ts = (struct traverse_struct *)state;
struct printjob pjob;
uint32 jobid;
int i;
- if (data.dsize != sizeof(pjob) || key.dsize != sizeof(jobid))
+ if ( key.dsize != sizeof(jobid) )
return 0;
+
memcpy(&jobid, key.dptr, sizeof(jobid));
- memcpy(&pjob, data.dptr, sizeof(pjob));
+ if ( unpack_pjob( data.dptr, data.dsize, &pjob ) == -1 )
+ return 0;
+ free_nt_devicemode( &pjob.nt_devmode );
+
if (ts->snum != lp_servicenumber(pjob.queuename)) {
/* this isn't for the queue we are looking at - this cannot happen with the split tdb's. JRA */
return 0;
}
- queueid = get_printer_queueid_byname(pjob.queuename);
-
if (!pjob.smbjob) {
/* remove a unix job if it isn't in the system queue any more */
for (i=0;i<ts->qcount;i++) {
- uint32 u_jobid = ((ts->queue[i].job + UNIX_JOB_START) | QUEUEID_TO_JOBID(queueid));
+ uint32 u_jobid = (ts->queue[i].job + UNIX_JOB_START);
if (jobid == u_jobid)
break;
}
if (i == ts->qcount)
- pjob_delete(jobid);
+ pjob_delete(ts->snum, jobid);
else
ts->total_jobs++;
return 0;
exist then kill it. This cleans up after smbd
deaths */
if (!process_exists(pjob.pid))
- pjob_delete(jobid);
+ pjob_delete(ts->snum, jobid);
else
ts->total_jobs++;
return 0;
}
for (i=0;i<ts->qcount;i++) {
- uint32 curr_jobid = print_parse_jobid(ts->queue[i].fs_file) | QUEUEID_TO_JOBID(queueid);
+ uint32 curr_jobid = print_parse_jobid(ts->queue[i].fs_file);
if (jobid == curr_jobid)
break;
}
submitted less than lp_lpqcachetime() seconds ago. */
if ((cur_t - pjob.starttime) > lp_lpqcachetime())
- pjob_delete(jobid);
+ pjob_delete(ts->snum, jobid);
else
ts->total_jobs++;
}
return;
slprintf(key, sizeof(key)-1, "CACHE/%s", printername);
tdb_store_int32(pdb->tdb, key, -1);
+ release_print_db(pdb);
}
/****************************************************************************
key.dsize = strlen(keystr);
data = tdb_fetch(pdb->tdb, key);
+ release_print_db(pdb);
if (!data.dptr || data.dsize != sizeof(pid_t))
return (pid_t)-1;
in the tdb.
****************************************************************************/
-static void set_updating_pid(fstring printer_name, BOOL delete)
+static void set_updating_pid(const fstring printer_name, BOOL delete)
{
fstring keystr;
TDB_DATA key;
if (delete) {
tdb_delete(pdb->tdb, key);
+ release_print_db(pdb);
return;
}
data.dsize = sizeof(pid_t);
tdb_store(pdb->tdb, key, data, TDB_REPLACE);
+ release_print_db(pdb);
}
/****************************************************************************
* This is essentially a mutex on the update.
*/
- if (get_updating_pid(printer_name) != -1)
+ if (get_updating_pid(printer_name) != -1) {
+ release_print_db(pdb);
return;
+ }
/* Lock the queue for the database update */
slprintf(keystr, sizeof(keystr) - 1, "LOCK/%s", printer_name);
- tdb_lock_bystring(pdb->tdb, keystr);
+ /* Only wait 10 seconds for this. */
+ if (tdb_lock_bystring(pdb->tdb, keystr, 10) == -1) {
+ DEBUG(0,("print_queue_update: Failed to lock printer %s database\n", printer_name));
+ release_print_db(pdb);
+ return;
+ }
/*
* Ensure that no one else got in here.
* Someone else is doing the update, exit.
*/
tdb_unlock_bystring(pdb->tdb, keystr);
+ release_print_db(pdb);
return;
}
}
/* we have an active SMB print job - update its status */
- pjob = print_job_find(jobid);
+ pjob = print_job_find(snum, jobid);
if (!pjob) {
/* err, somethings wrong. Probably smbd was restarted
with jobs in the queue. All we can do is treat them
pjob->sysjob = queue[i].job;
pjob->status = queue[i].status;
- pjob_store(jobid, pjob);
+ pjob_store(snum, jobid, pjob);
}
/* now delete any queued entries that don't appear in the
/* Delete our pid from the db. */
set_updating_pid(printer_name, True);
+ release_print_db(pdb);
}
/****************************************************************************
- Check if a jobid is valid. It is valid if it exists in the database.
+ Fetch and clean the pid_t record list for all pids interested in notify
+ messages. data needs freeing on exit.
****************************************************************************/
-BOOL print_job_exists(uint32 jobid)
+#define NOTIFY_PID_LIST_KEY "NOTIFY_PID_LIST"
+
+static TDB_DATA get_printer_notify_pid_list(struct tdb_print_db *pdb, BOOL cleanlist)
{
- struct tdb_print_db *pdb = get_print_db_byjobid(jobid);
+ TDB_DATA data;
+ size_t i;
+
+ ZERO_STRUCT(data);
+
+ data = tdb_fetch_by_string( pdb->tdb, NOTIFY_PID_LIST_KEY );
+
+ if (!data.dptr) {
+ ZERO_STRUCT(data);
+ return data;
+ }
+
+ if (data.dsize % 8) {
+ DEBUG(0,("get_printer_notify_pid_list: Size of record for printer %s not a multiple of 8 !\n",
+ pdb->printer_name ));
+ tdb_delete_by_string(pdb->tdb, NOTIFY_PID_LIST_KEY );
+ ZERO_STRUCT(data);
+ return data;
+ }
+
+ if (!cleanlist)
+ return data;
+
+ /*
+ * Weed out all dead entries.
+ */
+
+ for( i = 0; i < data.dsize; i += 8) {
+ pid_t pid = (pid_t)IVAL(data.dptr, i);
+
+ if (pid == sys_getpid())
+ continue;
+
+ /* Entry is dead if process doesn't exist or refcount is zero. */
+
+ while ((i < data.dsize) && ((IVAL(data.dptr, i + 4) == 0) || !process_exists(pid))) {
+
+ /* Refcount == zero is a logic error and should never happen. */
+ if (IVAL(data.dptr, i + 4) == 0) {
+ DEBUG(0,("get_printer_notify_pid_list: Refcount == 0 for pid = %u printer %s !\n",
+ (unsigned int)pid, pdb->printer_name ));
+ }
+
+ if (data.dsize - i > 8)
+ memmove( &data.dptr[i], &data.dptr[i+8], data.dsize - i - 8);
+ data.dsize -= 8;
+ }
+ }
+
+ return data;
+}
+
+/****************************************************************************
+ Return a malloced list of pid_t's that are interested in getting update
+ messages on this print queue. Used in printing/notify to send the messages.
+****************************************************************************/
+
+BOOL print_notify_pid_list(const char *printername, TALLOC_CTX *mem_ctx, size_t *p_num_pids, pid_t **pp_pid_list)
+{
+ struct tdb_print_db *pdb;
+ TDB_DATA data;
+ BOOL ret = True;
+ size_t i, num_pids, offset;
+ pid_t *pid_list;
+
+ *p_num_pids = 0;
+ *pp_pid_list = NULL;
+
+ pdb = get_print_db_byname(printername);
if (!pdb)
return False;
- return tdb_exists(pdb->tdb, print_key(jobid));
+
+ if (tdb_read_lock_bystring(pdb->tdb, NOTIFY_PID_LIST_KEY, 10) == -1) {
+ DEBUG(0,("print_notify_pid_list: Failed to lock printer %s database\n", printername));
+ release_print_db(pdb);
+ return False;
+ }
+
+ data = get_printer_notify_pid_list( pdb, True );
+
+ if (!data.dptr) {
+ ret = True;
+ goto done;
+ }
+
+ num_pids = data.dsize / 8;
+
+ if ((pid_list = (pid_t *)talloc(mem_ctx, sizeof(pid_t) * num_pids)) == NULL) {
+ ret = False;
+ goto done;
+ }
+
+ for( i = 0, offset = 0; offset < data.dsize; offset += 8, i++)
+ pid_list[i] = (pid_t)IVAL(data.dptr, offset);
+
+ *pp_pid_list = pid_list;
+ *p_num_pids = num_pids;
+
+ ret = True;
+
+ done:
+
+ tdb_read_unlock_bystring(pdb->tdb, NOTIFY_PID_LIST_KEY);
+ release_print_db(pdb);
+ SAFE_FREE(data.dptr);
+ return ret;
}
/****************************************************************************
- Work out which service a jobid is for.
- Note that we have to look up by queue name to ensure that it works for
- other than the process that started the job.
+ Create/Update an entry in the print tdb that will allow us to send notify
+ updates only to interested smbd's.
****************************************************************************/
-int print_job_snum(uint32 jobid)
+BOOL print_notify_register_pid(int snum)
{
- struct printjob *pjob = print_job_find(jobid);
- if (!pjob)
- return -1;
+ TDB_DATA data;
+ struct tdb_print_db *pdb;
+ const char *printername = lp_const_servicename(snum);
+ uint32 mypid = (uint32)sys_getpid();
+ BOOL ret = False;
+ size_t i;
+
+ pdb = get_print_db_byname(printername);
+ if (!pdb)
+ return False;
+
+ if (tdb_lock_bystring(pdb->tdb, NOTIFY_PID_LIST_KEY, 10) == -1) {
+ DEBUG(0,("print_notify_register_pid: Failed to lock printer %s\n", printername));
+ release_print_db(pdb);
+ return False;
+ }
+
+ data = get_printer_notify_pid_list( pdb, True );
+
+ /* Add ourselves and increase the refcount. */
+
+ for (i = 0; i < data.dsize; i += 8) {
+ if (IVAL(data.dptr,i) == mypid) {
+ uint32 new_refcount = IVAL(data.dptr, i+4) + 1;
+ SIVAL(data.dptr, i+4, new_refcount);
+ break;
+ }
+ }
+
+ if (i == data.dsize) {
+ /* We weren't in the list. Realloc. */
+ data.dptr = Realloc(data.dptr, data.dsize + 8);
+ if (!data.dptr) {
+ DEBUG(0,("print_notify_register_pid: Relloc fail for printer %s\n", printername));
+ goto done;
+ }
+ data.dsize += 8;
+ SIVAL(data.dptr,data.dsize - 8,mypid);
+ SIVAL(data.dptr,data.dsize - 4,1); /* Refcount. */
+ }
+
+ /* Store back the record. */
+ if (tdb_store_by_string(pdb->tdb, NOTIFY_PID_LIST_KEY, data, TDB_REPLACE) == -1) {
+ DEBUG(0,("print_notify_register_pid: Failed to update pid list for printer %s\n", printername));
+ goto done;
+ }
- return find_service(pjob->queuename);
+ ret = True;
+
+ done:
+
+ tdb_unlock_bystring(pdb->tdb, NOTIFY_PID_LIST_KEY);
+ release_print_db(pdb);
+ SAFE_FREE(data.dptr);
+ return ret;
+}
+
+/****************************************************************************
+ Update an entry in the print tdb that will allow us to send notify
+ updates only to interested smbd's.
+****************************************************************************/
+
+BOOL print_notify_deregister_pid(int snum)
+{
+ TDB_DATA data;
+ struct tdb_print_db *pdb;
+ const char *printername = lp_const_servicename(snum);
+ uint32 mypid = (uint32)sys_getpid();
+ size_t i;
+ BOOL ret = False;
+
+ pdb = get_print_db_byname(printername);
+ if (!pdb)
+ return False;
+
+ if (tdb_lock_bystring(pdb->tdb, NOTIFY_PID_LIST_KEY, 10) == -1) {
+ DEBUG(0,("print_notify_register_pid: Failed to lock printer %s database\n", printername));
+ release_print_db(pdb);
+ return False;
+ }
+
+ data = get_printer_notify_pid_list( pdb, True );
+
+ /* Reduce refcount. Remove ourselves if zero. */
+
+ for (i = 0; i < data.dsize; ) {
+ if (IVAL(data.dptr,i) == mypid) {
+ uint32 refcount = IVAL(data.dptr, i+4);
+
+ refcount--;
+
+ if (refcount == 0) {
+ if (data.dsize - i > 8)
+ memmove( &data.dptr[i], &data.dptr[i+8], data.dsize - i - 8);
+ data.dsize -= 8;
+ continue;
+ }
+ SIVAL(data.dptr, i+4, refcount);
+ }
+
+ i += 8;
+ }
+
+ if (data.dsize == 0)
+ SAFE_FREE(data.dptr);
+
+ /* Store back the record. */
+ if (tdb_store_by_string(pdb->tdb, NOTIFY_PID_LIST_KEY, data, TDB_REPLACE) == -1) {
+ DEBUG(0,("print_notify_register_pid: Failed to update pid list for printer %s\n", printername));
+ goto done;
+ }
+
+ ret = True;
+
+ done:
+
+ tdb_unlock_bystring(pdb->tdb, NOTIFY_PID_LIST_KEY);
+ release_print_db(pdb);
+ SAFE_FREE(data.dptr);
+ return ret;
+}
+
+/****************************************************************************
+ Check if a jobid is valid. It is valid if it exists in the database.
+****************************************************************************/
+
+BOOL print_job_exists(int snum, uint32 jobid)
+{
+ struct tdb_print_db *pdb = get_print_db_byname(lp_const_servicename(snum));
+ BOOL ret;
+
+ if (!pdb)
+ return False;
+ ret = tdb_exists(pdb->tdb, print_key(jobid));
+ release_print_db(pdb);
+ return ret;
}
/****************************************************************************
Give the fd used for a jobid.
****************************************************************************/
-int print_job_fd(uint32 jobid)
+int print_job_fd(int snum, uint32 jobid)
{
- struct printjob *pjob = print_job_find(jobid);
+ struct printjob *pjob = print_job_find(snum, jobid);
if (!pjob)
return -1;
/* don't allow another process to get this info - it is meaningless */
has not been spooled.
****************************************************************************/
-char *print_job_fname(uint32 jobid)
+char *print_job_fname(int snum, uint32 jobid)
{
- struct printjob *pjob = print_job_find(jobid);
+ struct printjob *pjob = print_job_find(snum, jobid);
if (!pjob || pjob->spooled || pjob->pid != local_pid)
return NULL;
return pjob->filename;
}
+
+/****************************************************************************
+ Give the filename used for a jobid.
+ Only valid for the process doing the spooling and when the job
+ has not been spooled.
+****************************************************************************/
+
+NT_DEVICEMODE *print_job_devmode(int snum, uint32 jobid)
+{
+ struct printjob *pjob = print_job_find(snum, jobid);
+
+ if ( !pjob )
+ return NULL;
+
+ return pjob->nt_devmode;
+}
+
/****************************************************************************
Set the place in the queue for a job.
****************************************************************************/
-BOOL print_job_set_place(uint32 jobid, int place)
+BOOL print_job_set_place(int snum, uint32 jobid, int place)
{
DEBUG(2,("print_job_set_place not implemented yet\n"));
return False;
Set the name of a job. Only possible for owner.
****************************************************************************/
-BOOL print_job_set_name(uint32 jobid, char *name)
+BOOL print_job_set_name(int snum, uint32 jobid, char *name)
{
- struct printjob *pjob = print_job_find(jobid);
+ struct printjob *pjob = print_job_find(snum, jobid);
if (!pjob || pjob->pid != local_pid)
return False;
fstrcpy(pjob->jobname, name);
- return pjob_store(jobid, pjob);
+ return pjob_store(snum, jobid, pjob);
}
/****************************************************************************
Delete a print job - don't update queue.
****************************************************************************/
-static BOOL print_job_delete1(uint32 jobid)
+static BOOL print_job_delete1(int snum, uint32 jobid)
{
- struct printjob *pjob = print_job_find(jobid);
- int snum, result = 0;
+ struct printjob *pjob = print_job_find(snum, jobid);
+ int result = 0;
if (!pjob)
return False;
if (pjob->status == LPQ_DELETING)
return True;
- snum = print_job_snum(jobid);
- if (snum == -1) {
- DEBUG(5,("print_job_delete1: unknown service number for jobid %u\n", (unsigned int)jobid));
- return False;
- }
-
/* Hrm - we need to be able to cope with deleting a job before it
has reached the spooler. */
/* Set the tdb entry to be deleting. */
pjob->status = LPQ_DELETING;
- pjob_store(jobid, pjob);
+ pjob_store(snum, jobid, pjob);
if (pjob->spooled && pjob->sysjob != -1)
result = (*(current_printif->job_delete))(snum, pjob);
been spooled. */
if (result == 0)
- pjob_delete(jobid);
+ pjob_delete(snum, jobid);
return (result == 0);
}
Return true if the current user owns the print job.
****************************************************************************/
-static BOOL is_owner(struct current_user *user, uint32 jobid)
+static BOOL is_owner(struct current_user *user, int snum, uint32 jobid)
{
- struct printjob *pjob = print_job_find(jobid);
+ struct printjob *pjob = print_job_find(snum, jobid);
user_struct *vuser;
if (!pjob || !user)
Delete a print job.
****************************************************************************/
-BOOL print_job_delete(struct current_user *user, uint32 jobid, WERROR *errcode)
+BOOL print_job_delete(struct current_user *user, int snum, uint32 jobid, WERROR *errcode)
{
- int snum = print_job_snum(jobid);
- BOOL owner;
+ BOOL owner, deleted;
+ char *fname;
- if (snum == -1) {
- DEBUG(5,("print_job_delete: unknown service number for jobid %d\n", jobid));
- return False;
- }
-
- owner = is_owner(user, jobid);
+ *errcode = WERR_OK;
+
+ owner = is_owner(user, snum, jobid);
/* Check access against security descriptor or whether the user
owns their job. */
!print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
DEBUG(3, ("delete denied by security descriptor\n"));
*errcode = WERR_ACCESS_DENIED;
+
+ /* BEGIN_ADMIN_LOG */
+ sys_adminlog( LOG_ERR, (char *)
+ "Permission denied-- user not allowed to delete, \
+pause, or resume print job. User name: %s. Printer name: %s.",
+ uidtoname(user->uid), PRINTERNAME(snum) );
+ /* END_ADMIN_LOG */
+
return False;
}
- if (!print_job_delete1(jobid))
+ /*
+ * get the spooled filename of the print job
+ * if this works, then the file has not been spooled
+ * to the underlying print system. Just delete the
+ * spool file & return.
+ */
+
+ if ( (fname = print_job_fname( snum, jobid )) != NULL )
+ {
+ /* remove the spool file */
+ DEBUG(10,("print_job_delete: Removing spool file [%s]\n", fname ));
+ if ( unlink( fname ) == -1 ) {
+ *errcode = map_werror_from_unix(errno);
+ return False;
+ }
+
+ return True;
+ }
+
+ if (!print_job_delete1(snum, jobid)) {
+ *errcode = WERR_ACCESS_DENIED;
return False;
+ }
/* force update the database and say the delete failed if the
job still exists */
print_queue_update(snum);
+
+ deleted = !print_job_exists(snum, jobid);
+ if ( !deleted )
+ *errcode = WERR_ACCESS_DENIED;
- return !print_job_exists(jobid);
+ return deleted;
}
/****************************************************************************
Pause a job.
****************************************************************************/
-BOOL print_job_pause(struct current_user *user, uint32 jobid, WERROR *errcode)
+BOOL print_job_pause(struct current_user *user, int snum, uint32 jobid, WERROR *errcode)
{
- struct printjob *pjob = print_job_find(jobid);
- int snum, ret = -1;
+ struct printjob *pjob = print_job_find(snum, jobid);
+ int ret = -1;
if (!pjob || !user)
return False;
if (!pjob->spooled || pjob->sysjob == -1)
return False;
- snum = print_job_snum(jobid);
- if (snum == -1) {
- DEBUG(5,("print_job_pause: unknown service number for jobid %u\n", (unsigned int)jobid));
- return False;
- }
-
- if (!is_owner(user, jobid) &&
+ if (!is_owner(user, snum, jobid) &&
!print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
DEBUG(3, ("pause denied by security descriptor\n"));
+
+ /* BEGIN_ADMIN_LOG */
+ sys_adminlog( LOG_ERR, (char *)
+ "Permission denied-- user not allowed to delete, \
+pause, or resume print job. User name: %s. Printer name: %s.",
+ uidtoname(user->uid), PRINTERNAME(snum) );
+ /* END_ADMIN_LOG */
+
*errcode = WERR_ACCESS_DENIED;
return False;
}
Resume a job.
****************************************************************************/
-BOOL print_job_resume(struct current_user *user, uint32 jobid, WERROR *errcode)
+BOOL print_job_resume(struct current_user *user, int snum, uint32 jobid, WERROR *errcode)
{
- struct printjob *pjob = print_job_find(jobid);
- int snum, ret;
+ struct printjob *pjob = print_job_find(snum, jobid);
+ int ret;
if (!pjob || !user)
return False;
if (!pjob->spooled || pjob->sysjob == -1)
return False;
- snum = print_job_snum(jobid);
- if (snum == -1) {
- DEBUG(5,("print_job_resume: unknown service number for jobid %u\n", (unsigned int)jobid));
- return False;
- }
-
- if (!is_owner(user, jobid) &&
+ if (!is_owner(user, snum, jobid) &&
!print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
DEBUG(3, ("resume denied by security descriptor\n"));
*errcode = WERR_ACCESS_DENIED;
+
+ /* BEGIN_ADMIN_LOG */
+ sys_adminlog( LOG_ERR, (char *)
+ "Permission denied-- user not allowed to delete, \
+pause, or resume print job. User name: %s. Printer name: %s.",
+ uidtoname(user->uid), PRINTERNAME(snum) );
+ /* END_ADMIN_LOG */
return False;
}
Write to a print file.
****************************************************************************/
-int print_job_write(uint32 jobid, const char *buf, int size)
+int print_job_write(int snum, uint32 jobid, const char *buf, int size)
{
int return_code;
- struct printjob *pjob = print_job_find(jobid);
+ struct printjob *pjob = print_job_find(snum, jobid);
if (!pjob)
return -1;
return_code = write(pjob->fd, buf, size);
if (return_code>0) {
pjob->size += size;
- pjob_store(jobid, pjob);
+ pjob_store(snum, jobid, pjob);
}
return return_code;
}
DEBUG(3, ("print cache expired for queue %s \
(last_qscan_time = %d, time now = %d, qcachetime = %d)\n", printername,
(int)last_qscan_time, (int)time_now, (int)lp_lpqcachetime() ));
+ release_print_db(pdb);
return True;
}
+ release_print_db(pdb);
return False;
}
key.dptr = keystr;
key.dsize = strlen(keystr);
data = tdb_fetch(pdb->tdb, key);
+ release_print_db(pdb);
if (data.dptr) {
- if (data.dsize == sizeof(print_status_struct)) {
+ if (data.dsize == sizeof(print_status_struct))
memcpy(status, data.dptr, sizeof(print_status_struct));
- }
SAFE_FREE(data.dptr);
}
return status->qcount;
return len;
}
-/****************************************************************************
- Determine the number of jobs in all queues. This is very expensive. Don't
- call ! JRA.
-****************************************************************************/
-
-static int get_total_jobs(void)
-{
- int total_jobs;
- struct printer_queueid_map *p;
-
- for (p = printer_queueid_map_head; p; p = p->next) {
- int jobs;
- struct tdb_print_db *pdb = get_print_db_byname(p->printername);
- if (!pdb)
- continue;
-
- /* make sure the database is up to date */
- if (print_cache_expired(lp_servicenumber(p->printername)))
- print_queue_update(lp_servicenumber(p->printername));
-
- jobs = tdb_fetch_int32(pdb->tdb, "INFO/total_jobs");
- if (jobs > 0)
- total_jobs += jobs;
- }
- return total_jobs;
-}
-
/***************************************************************************
Start spooling a job - return the jobid.
***************************************************************************/
-uint32 print_job_start(struct current_user *user, int snum, char *jobname)
+uint32 print_job_start(struct current_user *user, int snum, char *jobname, NT_DEVICEMODE *nt_devmode )
{
uint32 jobid;
char *path;
int njobs = 0;
const char *printername = lp_const_servicename(snum);
struct tdb_print_db *pdb = get_print_db_byname(printername);
- uint32 queueid = queueid = get_printer_queueid_byname(printername);
+ BOOL pdb_locked = False;
errno = 0;
if (!print_access_check(user, snum, PRINTER_ACCESS_USE)) {
DEBUG(3, ("print_job_start: job start denied by security descriptor\n"));
+ release_print_db(pdb);
return (uint32)-1;
}
if (!print_time_access_check(snum)) {
DEBUG(3, ("print_job_start: job start denied by time check\n"));
+ release_print_db(pdb);
return (uint32)-1;
}
if (sys_fsusage(path, &dspace, &dsize) == 0 &&
dspace < 2*(SMB_BIG_UINT)lp_minprintspace(snum)) {
DEBUG(3, ("print_job_start: disk space check failed.\n"));
+ release_print_db(pdb);
errno = ENOSPC;
return (uint32)-1;
}
/* for autoloaded printers, check that the printcap entry still exists */
if (lp_autoloaded(snum) && !pcap_printername_ok(lp_const_servicename(snum), NULL)) {
DEBUG(3, ("print_job_start: printer name %s check failed.\n", lp_const_servicename(snum) ));
+ release_print_db(pdb);
errno = ENOENT;
return (uint32)-1;
}
/* Insure the maximum queue size is not violated */
- if (lp_maxprintjobs(snum) && (njobs = print_queue_length(snum,NULL)) > lp_maxprintjobs(snum)) {
+ if ((njobs = print_queue_length(snum,NULL)) > lp_maxprintjobs(snum)) {
DEBUG(3, ("print_job_start: number of jobs (%d) larger than max printjobs per queue (%d).\n",
njobs, lp_maxprintjobs(snum) ));
+ release_print_db(pdb);
errno = ENOSPC;
return (uint32)-1;
}
- /* Insure the maximum print jobs in the system is not violated */
- if (lp_totalprintjobs() && get_total_jobs() > lp_totalprintjobs()) {
- DEBUG(3, ("print_job_start: number of jobs (%d) larger than max printjobs per system (%d).\n",
- njobs, lp_totalprintjobs() ));
- errno = ENOSPC;
+ /* Lock the database - only wait 20 seconds. */
+ if (tdb_lock_bystring(pdb->tdb, "INFO/nextjob", 20) == -1) {
+ DEBUG(0,("print_job_start: failed to lock printing database %s\n", printername ));
+ release_print_db(pdb);
return (uint32)-1;
}
- /* create the database entry */
- ZERO_STRUCT(pjob);
- pjob.pid = local_pid;
- pjob.sysjob = -1;
- pjob.fd = -1;
- pjob.starttime = time(NULL);
- pjob.status = LPQ_SPOOLING;
- pjob.size = 0;
- pjob.spooled = False;
- pjob.smbjob = True;
-
- fstrcpy(pjob.jobname, jobname);
-
- if ((vuser = get_valid_user_struct(user->vuid)) != NULL) {
- fstrcpy(pjob.user, vuser->user.smb_name);
- } else {
- fstrcpy(pjob.user, uidtoname(user->uid));
- }
-
- fstrcpy(pjob.queuename, lp_const_servicename(snum));
-
- /* lock the database */
- tdb_lock_bystring(pdb->tdb, "INFO/nextjob");
+ pdb_locked = True;
next_jobid = tdb_fetch_int32(pdb->tdb, "INFO/nextjob");
if (next_jobid == -1)
next_jobid = 1;
for (jobid = NEXT_JOBID(next_jobid); jobid != next_jobid; jobid = NEXT_JOBID(jobid)) {
- if (!print_job_exists(jobid | QUEUEID_TO_JOBID(queueid)))
+ if (!print_job_exists(snum, jobid))
break;
}
- if (jobid == next_jobid || !pjob_store(jobid | QUEUEID_TO_JOBID(queueid), &pjob)) {
- DEBUG(3, ("print_job_start: either jobid (%d)==next_jobid(%d) or pjob_store failed.\n",
+
+ if (jobid == next_jobid) {
+ DEBUG(3, ("print_job_start: jobid (%d)==next_jobid(%d).\n",
jobid, next_jobid ));
jobid = -1;
goto fail;
}
+ /* Store a dummy placeholder. This must be quick as we have the lock. */
+ {
+ TDB_DATA dum;
+ dum.dptr = NULL;
+ dum.dsize = 0;
+ if (tdb_store(pdb->tdb, print_key(jobid), dum, TDB_INSERT) == -1) {
+ DEBUG(3, ("print_job_start: jobid (%d) failed to store placeholder.\n",
+ jobid ));
+ jobid = -1;
+ goto fail;
+ }
+ }
+
if (tdb_store_int32(pdb->tdb, "INFO/nextjob", jobid)==-1) {
DEBUG(3, ("print_job_start: failed to store INFO/nextjob.\n"));
jobid = -1;
goto fail;
}
- /* Ensure the queuid is added to the jobid. */
- jobid |= QUEUEID_TO_JOBID(queueid);
+ /* We've finished with the INFO/nextjob lock. */
+ tdb_unlock_bystring(pdb->tdb, "INFO/nextjob");
+ pdb_locked = False;
+
+ /* create the database entry */
+
+ ZERO_STRUCT(pjob);
+
+ pjob.pid = local_pid;
+ pjob.sysjob = -1;
+ pjob.fd = -1;
+ pjob.starttime = time(NULL);
+ pjob.status = LPQ_SPOOLING;
+ pjob.size = 0;
+ pjob.spooled = False;
+ pjob.smbjob = True;
+ pjob.nt_devmode = nt_devmode;
+
+ fstrcpy(pjob.jobname, jobname);
+
+ if ((vuser = get_valid_user_struct(user->vuid)) != NULL) {
+ fstrcpy(pjob.user, vuser->user.smb_name);
+ } else {
+ fstrcpy(pjob.user, uidtoname(user->uid));
+ }
+
+ fstrcpy(pjob.queuename, lp_const_servicename(snum));
/* we have a job entry - now create the spool file */
slprintf(pjob.filename, sizeof(pjob.filename)-1, "%s/%s%.8u.XXXXXX",
goto fail;
}
- pjob_store(jobid, &pjob);
-
- tdb_unlock_bystring(pdb->tdb, "INFO/nextjob");
+ pjob_store(snum, jobid, &pjob);
- /*
- * If the printer is marked as postscript output a leading
- * file identifier to ensure the file is treated as a raw
- * postscript file.
- * This has a similar effect as CtrlD=0 in WIN.INI file.
- * tim@fsg.com 09/06/94
- */
- if (lp_postscript(snum)) {
- print_job_write(jobid, "%!\n",3);
- }
+ release_print_db(pdb);
return jobid;
fail:
if (jobid != -1)
- pjob_delete(jobid);
+ pjob_delete(snum, jobid);
- tdb_unlock_bystring(pdb->tdb, "INFO/nextjob");
+ if (pdb_locked)
+ tdb_unlock_bystring(pdb->tdb, "INFO/nextjob");
+ release_print_db(pdb);
DEBUG(3, ("print_job_start: returning fail. Error = %s\n", strerror(errno) ));
return -1;
Update the number of pages spooled to jobid
****************************************************************************/
-void print_job_endpage(uint32 jobid)
+void print_job_endpage(int snum, uint32 jobid)
{
- struct printjob *pjob = print_job_find(jobid);
+ struct printjob *pjob = print_job_find(snum, jobid);
if (!pjob)
return;
/* don't allow another process to get this info - it is meaningless */
return;
pjob->page_count++;
- pjob_store(jobid, pjob);
+ pjob_store(snum, jobid, pjob);
}
/****************************************************************************
error.
****************************************************************************/
-BOOL print_job_end(uint32 jobid, BOOL normal_close)
+BOOL print_job_end(int snum, uint32 jobid, BOOL normal_close)
{
- struct printjob *pjob = print_job_find(jobid);
- int snum, ret;
+ struct printjob *pjob = print_job_find(snum, jobid);
+ int ret;
SMB_STRUCT_STAT sbuf;
if (!pjob)
if (pjob->spooled || pjob->pid != local_pid)
return False;
- snum = print_job_snum(jobid);
- if (snum == -1) {
- DEBUG(5,("print_job_end: unknown service number for jobid %u\n", (unsigned int)jobid));
- return False;
- }
-
if (normal_close && (sys_fstat(pjob->fd, &sbuf) == 0)) {
pjob->size = sbuf.st_size;
close(pjob->fd);
DEBUG(5,("print_job_end: canceling spool of %s (%s)\n",
pjob->filename, pjob->size ? "deleted" : "zero length" ));
unlink(pjob->filename);
- pjob_delete(jobid);
+ pjob_delete(snum, jobid);
return True;
}
pjob->spooled = True;
pjob->status = LPQ_QUEUED;
- pjob_store(jobid, pjob);
+ pjob_store(snum, jobid, pjob);
/* make sure the database is up to date */
if (print_cache_expired(snum))
/* The print job was not succesfully started. Cleanup */
/* Still need to add proper error return propagation! 010122:JRR */
unlink(pjob->filename);
- pjob_delete(jobid);
+ pjob_delete(snum, jobid);
return False;
}
int i;
uint32 jobid;
- if (data.dsize != sizeof(pjob) || key.dsize != sizeof(int))
+ /* sanity checks */
+
+ if ( key.dsize != sizeof(jobid) )
return 0;
+
memcpy(&jobid, key.dptr, sizeof(jobid));
- memcpy(&pjob, data.dptr, sizeof(pjob));
+
+ if ( unpack_pjob( data.dptr, data.dsize, &pjob ) == -1 )
+ return 0;
+ free_nt_devicemode( &pjob.nt_devmode );
/* maybe it isn't for this queue */
if (ts->snum != lp_servicenumber(pjob.queuename))
struct printjob pjob;
uint32 jobid;
- if (data.dsize != sizeof(pjob) || key.dsize != sizeof(int))
+ /* sanity checks */
+
+ if ( key.dsize != sizeof(jobid) )
return 0;
+
memcpy(&jobid, key.dptr, sizeof(jobid));
- memcpy(&pjob, data.dptr, sizeof(pjob));
+
+ if ( unpack_pjob( data.dptr, data.dsize, &pjob ) == -1 )
+ return 0;
+
+ free_nt_devicemode( &pjob.nt_devmode );
/* maybe it isn't for this queue - this cannot happen with the tdb/printer code. JRA */
if (ts->snum != lp_servicenumber(pjob.queuename))
tdb_traverse(pdb->tdb, traverse_count_fn_queue, (void *)&tsc);
- if (tsc.count == 0)
+ if (tsc.count == 0) {
+ release_print_db(pdb);
return 0;
+ }
/* Allocate the queue size. */
if ((tstruct.queue = (print_queue_struct *)
- malloc(sizeof(print_queue_struct)*tsc.count)) == NULL)
+ malloc(sizeof(print_queue_struct)*tsc.count)) == NULL) {
+ release_print_db(pdb);
return 0;
+ }
/*
* Fill in the queue.
tstruct.snum = snum;
tdb_traverse(pdb->tdb, traverse_fn_queue, (void *)&tstruct);
+ release_print_db(pdb);
/* Sort the queue by submission time otherwise they are displayed
in hash order. */
njobs = print_queue_status(snum, &queue, &status);
for (i=0;i<njobs;i++) {
- BOOL owner = is_owner(user, queue[i].job);
+ BOOL owner = is_owner(user, snum, queue[i].job);
if (owner || can_job_admin) {
- print_job_delete1(queue[i].job);
+ print_job_delete1(snum, queue[i].job);
}
}