This commit was manufactured by cvs2svn to create branch 'SAMBA_3_0'.(This used to...
[ira/wip.git] / source3 / smbwrapper / shared.c
index 4637248dcec3443c46bfd73e1aa69722795be714..ca8df5841d1e3b9a18e7e8792277aa0e66a4abf4 100644 (file)
@@ -1,6 +1,5 @@
 /* 
-   Unix SMB/Netbios implementation.
-   Version 2.0
+   Unix SMB/CIFS implementation.
    SMB wrapper functions - shared variables
    Copyright (C) Andrew Tridgell 1998
    
@@ -21,8 +20,6 @@
 
 #include "includes.h"
 
-extern int DEBUGLEVEL;
-
 static int shared_fd;
 static char *variables;
 static int shared_size;
@@ -33,16 +30,16 @@ setup the shared area
 void smbw_setup_shared(void)
 {
        int fd;
-       pstring s, name;
+       pstring name, s;
 
-       slprintf(s,sizeof(s)-1, "%s/msg.XXXXXX",tmpdir());
+       slprintf(name,sizeof(name)-1, "%s/smbw.XXXXXX",tmpdir());
 
-       fstrcpy(name,(char *)mktemp(s));
+       fd = smb_mkstemp(name);
 
-       /* note zero permissions! don't change this */
-       fd = open(name,O_RDWR|O_CREAT|O_TRUNC|O_EXCL,0); 
        if (fd == -1) goto failed;
 
+       unlink(name);
+
        shared_fd = set_maxfiles(SMBW_MAX_OPEN);
        
        while (shared_fd && dup2(fd, shared_fd) != shared_fd) shared_fd--;
@@ -64,6 +61,7 @@ void smbw_setup_shared(void)
        exit(1);
 }
 
+static int locked;
 
 /***************************************************** 
 lock the shared variable area
@@ -78,10 +76,12 @@ static void lockit(void)
                }
                shared_fd = atoi(p);
        }
-       if (fcntl_lock(shared_fd,SMB_F_SETLKW,0,1,F_WRLCK)==False) {
-               DEBUG(0,("ERROR: can't get smbw shared lock\n"));
+       if (locked==0 && 
+           fcntl_lock(shared_fd,SMB_F_SETLKW,0,1,F_WRLCK)==False) {
+               DEBUG(0,("ERROR: can't get smbw shared lock (%s)\n", strerror(errno)));
                exit(1);
        }
+       locked++;
 }
 
 /***************************************************** 
@@ -89,7 +89,10 @@ unlock the shared variable area
 *******************************************************/
 static void unlockit(void)
 {
-       fcntl_lock(shared_fd,SMB_F_SETLK,0,1,F_UNLCK);
+       locked--;
+       if (locked == 0) {
+               fcntl_lock(shared_fd,SMB_F_SETLK,0,1,F_UNLCK);
+       }
 }
 
 
@@ -100,6 +103,7 @@ char *smbw_getshared(const char *name)
 {
        int i;
        struct stat st;
+       char *var;
 
        lockit();
 
@@ -107,8 +111,9 @@ char *smbw_getshared(const char *name)
        if (fstat(shared_fd, &st)) goto failed;
 
        if (st.st_size != shared_size) {
-               variables = (char *)Realloc(variables, st.st_size);
-               if (!variables) goto failed;
+               var = (char *)Realloc(variables, st.st_size);
+               if (!var) goto failed;
+               else variables = var;
                shared_size = st.st_size;
                lseek(shared_fd, 0, SEEK_SET);
                if (read(shared_fd, variables, shared_size) != shared_size) {
@@ -120,13 +125,15 @@ char *smbw_getshared(const char *name)
 
        i=0;
        while (i < shared_size) {
-               int len;
                char *n, *v;
+               int l1, l2;
 
-               n = &variables[i];
-               i += strlen(n)+1;
-               v = &variables[i];
-               i += strlen(v)+1;
+               l1 = SVAL(&variables[i], 0);
+               l2 = SVAL(&variables[i], 2);
+
+               n = &variables[i+4];
+               v = &variables[i+4+l1];
+               i += 4+l1+l2;
 
                if (strcmp(name,n)) {
                        continue;
@@ -139,6 +146,7 @@ char *smbw_getshared(const char *name)
  failed:
        DEBUG(0,("smbw: shared variables corrupt (%s)\n", strerror(errno)));
        exit(1);
+       return NULL;
 }
 
 
@@ -148,26 +156,33 @@ set a variable in the shared area
 *******************************************************/
 void smbw_setshared(const char *name, const char *val)
 {
-       int len;
+       int l1, l2;
+       char *var;
 
        /* we don't allow variable overwrite */
        if (smbw_getshared(name)) return;
 
        lockit();
 
-       len = strlen(name) + strlen(val) + 2;
+       l1 = strlen(name)+1;
+       l2 = strlen(val)+1;
 
-       variables = (char *)Realloc(variables, shared_size + len);
+       var = (char *)Realloc(variables, shared_size + l1+l2+4);
 
-       if (!variables) {
+       if (!var) {
                DEBUG(0,("out of memory in smbw_setshared\n"));
                exit(1);
        }
+       
+       variables = var;
 
-       pstrcpy(&variables[shared_size], name);
-       shared_size += strlen(name)+1;
-       pstrcpy(&variables[shared_size], val);
-       shared_size += strlen(val)+1;
+       SSVAL(&variables[shared_size], 0, l1);
+       SSVAL(&variables[shared_size], 2, l2);
+
+       safe_strcpy(&variables[shared_size] + 4, name, l1-1);
+       safe_strcpy(&variables[shared_size] + 4 + l1, val, l2-1);
+
+       shared_size += l1+l2+4;
 
        lseek(shared_fd, 0, SEEK_SET);
        if (write(shared_fd, variables, shared_size) != shared_size) {
@@ -177,3 +192,12 @@ void smbw_setshared(const char *name, const char *val)
 
        unlockit();
 }
+
+
+/*****************************************************************
+return true if the passed fd is the SMBW_HANDLE
+*****************************************************************/  
+int smbw_shared_fd(int fd)
+{
+       return (shared_fd && shared_fd == fd);
+}