2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Jeremy Allison 1998-2002
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.
23 #include "system/network.h"
26 The idea is that this file will eventually have wrappers around all
27 important system calls in samba. The aims are:
29 - to enable easier porting by putting OS dependent stuff in here
31 - to allow for hooks into other "pseudo-filesystems"
33 - to allow easier integration of things like the japanese extensions
35 - to support the philosophy of Samba to expose the features of
36 the OS within the SMB model. In general whatever file/printer/variable
37 expansions/etc make sense to the OS should be acceptable to Samba.
42 /*******************************************************************
43 A wrapper for usleep in case we don't have one.
44 ********************************************************************/
46 int sys_usleep(long usecs)
53 * We need this braindamage as the glibc usleep
54 * is not SPEC1170 complient... grumble... JRA.
57 if(usecs < 0 || usecs > 1000000) {
65 #else /* HAVE_USLEEP */
67 * Fake it with select...
70 tval.tv_usec = usecs/1000;
71 select(0,NULL,NULL,NULL,&tval);
73 #endif /* HAVE_USLEEP */
76 /*******************************************************************
77 A read wrapper that will deal with EINTR.
78 ********************************************************************/
80 ssize_t sys_read(int fd, void *buf, size_t count)
85 ret = read(fd, buf, count);
86 } while (ret == -1 && errno == EINTR);
90 /*******************************************************************
91 A write wrapper that will deal with EINTR.
92 ********************************************************************/
94 ssize_t sys_write(int fd, const void *buf, size_t count)
99 ret = write(fd, buf, count);
100 } while (ret == -1 && errno == EINTR);
104 /*******************************************************************
105 A send wrapper that will deal with EINTR.
106 ********************************************************************/
108 ssize_t sys_send(int s, const void *msg, size_t len, int flags)
113 ret = send(s, msg, len, flags);
114 } while (ret == -1 && errno == EINTR);
118 /*******************************************************************
119 A sendto wrapper that will deal with EINTR.
120 ********************************************************************/
122 ssize_t sys_sendto(int s, const void *msg, size_t len, int flags, const struct sockaddr *to, socklen_t tolen)
127 ret = sendto(s, msg, len, flags, to, tolen);
128 } while (ret == -1 && errno == EINTR);
133 /*******************************************************************
134 System wrapper for getwd
135 ********************************************************************/
137 char *sys_getwd(char *s)
141 wd = (char *)getcwd(s, sizeof (pstring));
143 wd = (char *)getwd(s);
148 /*******************************************************************
149 system wrapper for link
150 ********************************************************************/
152 int sys_link(const char *oldpath, const char *newpath)
158 return link(oldpath, newpath);
162 /*******************************************************************
163 os/2 also doesn't have chroot
164 ********************************************************************/
165 int sys_chroot(const char *dname)
170 DEBUG(1,("WARNING: no chroot!\n"));
176 return(chroot(dname));
180 /**************************************************************************
181 A wrapper for gethostbyname() that tries avoids looking up hostnames
182 in the root domain, which can cause dial-on-demand links to come up for no
184 ****************************************************************************/
186 struct hostent *sys_gethostbyname(const char *name)
188 #ifdef REDUCE_ROOT_DNS_LOOKUPS
189 char query[256], hostname[256];
192 /* Does this name have any dots in it? If so, make no change */
194 if (strchr_m(name, '.'))
195 return(gethostbyname(name));
197 /* Get my hostname, which should have domain name
198 attached. If not, just do the gethostname on the
202 gethostname(hostname, sizeof(hostname) - 1);
203 hostname[sizeof(hostname) - 1] = 0;
204 if ((domain = strchr_m(hostname, '.')) == NULL)
205 return(gethostbyname(name));
207 /* Attach domain name to query and do modified query.
208 If names too large, just do gethostname on the
212 if((strlen(name) + strlen(domain)) >= sizeof(query))
213 return(gethostbyname(name));
215 slprintf(query, sizeof(query)-1, "%s%s", name, domain);
216 return(gethostbyname(query));
217 #else /* REDUCE_ROOT_DNS_LOOKUPS */
218 return(gethostbyname(name));
219 #endif /* REDUCE_ROOT_DNS_LOOKUPS */
223 #if defined(HAVE_IRIX_SPECIFIC_CAPABILITIES)
224 /**************************************************************************
225 Try and abstract process capabilities (for systems that have them).
226 ****************************************************************************/
227 static BOOL set_process_capability( uint32_t cap_flag, BOOL enable )
229 if(cap_flag == KERNEL_OPLOCK_CAPABILITY) {
230 cap_t cap = cap_get_proc();
233 DEBUG(0,("set_process_capability: cap_get_proc failed. Error was %s\n",
239 cap->cap_effective |= CAP_NETWORK_MGT;
241 cap->cap_effective &= ~CAP_NETWORK_MGT;
243 if (cap_set_proc(cap) == -1) {
244 DEBUG(0,("set_process_capability: cap_set_proc failed. Error was %s\n",
252 DEBUG(10,("set_process_capability: Set KERNEL_OPLOCK_CAPABILITY.\n"));
257 /**************************************************************************
258 Try and abstract inherited process capabilities (for systems that have them).
259 ****************************************************************************/
261 static BOOL set_inherited_process_capability( uint32_t cap_flag, BOOL enable )
263 if(cap_flag == KERNEL_OPLOCK_CAPABILITY) {
264 cap_t cap = cap_get_proc();
267 DEBUG(0,("set_inherited_process_capability: cap_get_proc failed. Error was %s\n",
273 cap->cap_inheritable |= CAP_NETWORK_MGT;
275 cap->cap_inheritable &= ~CAP_NETWORK_MGT;
277 if (cap_set_proc(cap) == -1) {
278 DEBUG(0,("set_inherited_process_capability: cap_set_proc failed. Error was %s\n",
286 DEBUG(10,("set_inherited_process_capability: Set KERNEL_OPLOCK_CAPABILITY.\n"));
292 /****************************************************************************
293 Gain the oplock capability from the kernel if possible.
294 ****************************************************************************/
296 void oplock_set_capability(BOOL this_process, BOOL inherit)
298 #if HAVE_KERNEL_OPLOCKS_IRIX
299 set_process_capability(KERNEL_OPLOCK_CAPABILITY,this_process);
300 set_inherited_process_capability(KERNEL_OPLOCK_CAPABILITY,inherit);
304 /**************************************************************************
305 Wrapper for random().
306 ****************************************************************************/
308 long sys_random(void)
310 #if defined(HAVE_RANDOM)
311 return (long)random();
312 #elif defined(HAVE_RAND)
315 DEBUG(0,("Error - no random function available !\n"));
320 /**************************************************************************
321 Wrapper for srandom().
322 ****************************************************************************/
324 void sys_srandom(uint_t seed)
326 #if defined(HAVE_SRANDOM)
328 #elif defined(HAVE_SRAND)
331 DEBUG(0,("Error - no srandom function available !\n"));
336 /**************************************************************************
337 Returns equivalent to NGROUPS_MAX - using sysconf if needed.
338 ****************************************************************************/
342 #if defined(SYSCONF_SC_NGROUPS_MAX)
343 int ret = sysconf(_SC_NGROUPS_MAX);
344 return (ret == -1) ? NGROUPS_MAX : ret;
350 /**************************************************************************
351 Wrapper for getgroups. Deals with broken (int) case.
352 ****************************************************************************/
354 int sys_getgroups(int setlen, gid_t *gidset)
356 #if !defined(HAVE_BROKEN_GETGROUPS)
357 return getgroups(setlen, gidset);
365 return getgroups(setlen, &gid);
369 * Broken case. We need to allocate a
370 * GID_T array of size setlen.
379 setlen = groups_max();
381 if((group_list = (GID_T *)malloc(setlen * sizeof(GID_T))) == NULL) {
382 DEBUG(0,("sys_getgroups: Malloc fail.\n"));
386 if((ngroups = getgroups(setlen, group_list)) < 0) {
387 int saved_errno = errno;
388 SAFE_FREE(group_list);
393 for(i = 0; i < ngroups; i++)
394 gidset[i] = (gid_t)group_list[i];
396 SAFE_FREE(group_list);
398 #endif /* HAVE_BROKEN_GETGROUPS */
401 #ifdef HAVE_SETGROUPS
403 /**************************************************************************
404 Wrapper for setgroups. Deals with broken (int) case. Automatically used
405 if we have broken getgroups.
406 ****************************************************************************/
408 int sys_setgroups(int setlen, gid_t *gidset)
410 #if !defined(HAVE_BROKEN_GETGROUPS)
411 return setgroups(setlen, gidset);
420 if (setlen < 0 || setlen > groups_max()) {
426 * Broken case. We need to allocate a
427 * GID_T array of size setlen.
430 if((group_list = (GID_T *)malloc(setlen * sizeof(GID_T))) == NULL) {
431 DEBUG(0,("sys_setgroups: Malloc fail.\n"));
435 for(i = 0; i < setlen; i++)
436 group_list[i] = (GID_T) gidset[i];
438 if(setgroups(setlen, group_list) != 0) {
439 int saved_errno = errno;
440 SAFE_FREE(group_list);
445 SAFE_FREE(group_list);
447 #endif /* HAVE_BROKEN_GETGROUPS */
450 #endif /* HAVE_SETGROUPS */
452 struct passwd *sys_getpwent(void)
457 void sys_endpwent(void)
462 /**************************************************************************
463 Wrappers for getpwnam(), getpwuid(), getgrnam(), getgrgid()
464 ****************************************************************************/
466 struct passwd *sys_getpwnam(const char *name)
468 return getpwnam(name);
471 struct passwd *sys_getpwuid(uid_t uid)
473 return getpwuid(uid);
476 struct group *sys_getgrnam(const char *name)
478 return getgrnam(name);
481 struct group *sys_getgrgid(gid_t gid)
483 return getgrgid(gid);
487 /**************************************************************************
488 Wrappers for dlopen, dlsym, dlclose.
489 ****************************************************************************/
491 void *sys_dlopen(const char *name, int flags)
493 #if defined(HAVE_DLOPEN)
494 return dlopen(name, flags);
500 void *sys_dlsym(void *handle, const char *symbol)
502 #if defined(HAVE_DLSYM)
503 return dlsym(handle, symbol);
509 int sys_dlclose (void *handle)
511 #if defined(HAVE_DLCLOSE)
512 return dlclose(handle);
518 const char *sys_dlerror(void)
520 #if defined(HAVE_DLERROR)
527 int sys_dup2(int oldfd, int newfd)
529 #if defined(HAVE_DUP2)
530 return dup2(oldfd, newfd);
538 const char *sys_inet_ntoa(struct ipv4_addr in)
541 in2.s_addr = in.s_addr;
542 return inet_ntoa(in2);
545 uint32_t sys_inet_addr(const char *s)
550 struct ipv4_addr sys_inet_makeaddr(int net, int host)
553 struct ipv4_addr in2;
554 in = inet_makeaddr(net, host);
555 in2.s_addr = in.s_addr;