2 Unix SMB/Netbios implementation.
4 SMB wrapper functions - shared variables
5 Copyright (C) Andrew Tridgell 1998
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 static char *variables;
26 static int shared_size;
28 /*****************************************************
30 *******************************************************/
31 void smbw_setup_shared(void)
36 slprintf(name,sizeof(name)-1, "%s/smbw.XXXXXX",tmpdir());
38 fd = smb_mkstemp(name);
40 if (fd == -1) goto failed;
44 shared_fd = set_maxfiles(SMBW_MAX_OPEN);
46 while (shared_fd && dup2(fd, shared_fd) != shared_fd) shared_fd--;
48 if (shared_fd == 0) goto failed;
52 DEBUG(4,("created shared_fd=%d\n", shared_fd));
54 slprintf(s,sizeof(s)-1,"%d", shared_fd);
56 smbw_setenv("SMBW_HANDLE", s);
61 perror("Failed to setup shared variable area ");
67 /*****************************************************
68 lock the shared variable area
69 *******************************************************/
70 static void lockit(void)
73 char *p = getenv("SMBW_HANDLE");
75 DEBUG(0,("ERROR: can't get smbw shared handle\n"));
81 fcntl_lock(shared_fd,SMB_F_SETLKW,0,1,F_WRLCK)==False) {
82 DEBUG(0,("ERROR: can't get smbw shared lock (%s)\n", strerror(errno)));
88 /*****************************************************
89 unlock the shared variable area
90 *******************************************************/
91 static void unlockit(void)
95 fcntl_lock(shared_fd,SMB_F_SETLK,0,1,F_UNLCK);
100 /*****************************************************
101 get a variable from the shared area
102 *******************************************************/
103 char *smbw_getshared(const char *name)
111 /* maybe the area has changed */
112 if (fstat(shared_fd, &st)) goto failed;
114 if (st.st_size != shared_size) {
115 var = (char *)Realloc(variables, st.st_size);
116 if (!var) goto failed;
117 else variables = var;
118 shared_size = st.st_size;
119 lseek(shared_fd, 0, SEEK_SET);
120 if (read(shared_fd, variables, shared_size) != shared_size) {
128 while (i < shared_size) {
132 l1 = SVAL(&variables[i], 0);
133 l2 = SVAL(&variables[i], 2);
136 v = &variables[i+4+l1];
139 if (strcmp(name,n)) {
148 DEBUG(0,("smbw: shared variables corrupt (%s)\n", strerror(errno)));
155 /*****************************************************
156 set a variable in the shared area
157 *******************************************************/
158 void smbw_setshared(const char *name, const char *val)
163 /* we don't allow variable overwrite */
164 if (smbw_getshared(name)) return;
171 var = (char *)Realloc(variables, shared_size + l1+l2+4);
174 DEBUG(0,("out of memory in smbw_setshared\n"));
180 SSVAL(&variables[shared_size], 0, l1);
181 SSVAL(&variables[shared_size], 2, l2);
183 pstrcpy(&variables[shared_size] + 4, name);
184 pstrcpy(&variables[shared_size] + 4 + l1, val);
186 shared_size += l1+l2+4;
188 lseek(shared_fd, 0, SEEK_SET);
189 if (write(shared_fd, variables, shared_size) != shared_size) {
190 DEBUG(0,("smbw_setshared failed (%s)\n", strerror(errno)));
198 /*****************************************************************
199 set an env variable - some systems don't have this
200 *****************************************************************/
201 int smbw_setenv(const char *name, const char *value)
207 slprintf(s,sizeof(s)-1,"%s=%s", name, value);
211 if (p) ret = putenv(p);
216 /*****************************************************************
217 return true if the passed fd is the SMBW_HANDLE
218 *****************************************************************/
219 int smbw_shared_fd(int fd)
221 return (shared_fd && shared_fd == fd);