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 3 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, see <http://www.gnu.org/licenses/>.
22 #include "system/network.h"
23 #include "system/filesys.h"
28 The idea is that this file will eventually have wrappers around all
29 important system calls in samba. The aims are:
31 - to enable easier porting by putting OS dependent stuff in here
33 - to allow for hooks into other "pseudo-filesystems"
35 - to allow easier integration of things like the japanese extensions
37 - to support the philosophy of Samba to expose the features of
38 the OS within the SMB model. In general whatever file/printer/variable
39 expansions/etc make sense to the OS should be acceptable to Samba.
42 /*******************************************************************
43 A wrapper for memalign
44 ********************************************************************/
46 void *sys_memalign( size_t align, size_t size )
48 #if defined(HAVE_POSIX_MEMALIGN)
50 int ret = posix_memalign( &p, align, size );
55 #elif defined(HAVE_MEMALIGN)
56 return memalign( align, size );
58 /* On *BSD systems memaligns doesn't exist, but memory will
59 * be aligned on allocations of > pagesize. */
60 #if defined(SYSCONF_SC_PAGESIZE)
61 size_t pagesize = (size_t)sysconf(_SC_PAGESIZE);
62 #elif defined(HAVE_GETPAGESIZE)
63 size_t pagesize = (size_t)getpagesize();
65 size_t pagesize = (size_t)-1;
67 if (pagesize == (size_t)-1) {
68 DEBUG(0,("memalign functionalaity not available on this platform!\n"));
71 if (size < pagesize) {
78 /**************************************************************************
79 A wrapper for gethostbyname() that tries avoids looking up hostnames
80 in the root domain, which can cause dial-on-demand links to come up for no
82 ****************************************************************************/
84 _PUBLIC_ struct hostent *sys_gethostbyname(const char *name)
86 #ifdef REDUCE_ROOT_DNS_LOOKUPS
87 char query[256], hostname[256];
90 /* Does this name have any dots in it? If so, make no change */
92 if (strchr(name, '.'))
93 return(gethostbyname(name));
95 /* Get my hostname, which should have domain name
96 attached. If not, just do the gethostname on the
100 gethostname(hostname, sizeof(hostname) - 1);
101 hostname[sizeof(hostname) - 1] = 0;
102 if ((domain = strchr(hostname, '.')) == NULL)
103 return(gethostbyname(name));
105 /* Attach domain name to query and do modified query.
106 If names too large, just do gethostname on the
110 if((strlen(name) + strlen(domain)) >= sizeof(query))
111 return(gethostbyname(name));
113 slprintf(query, sizeof(query)-1, "%s%s", name, domain);
114 return(gethostbyname(query));
115 #else /* REDUCE_ROOT_DNS_LOOKUPS */
116 return(gethostbyname(name));
117 #endif /* REDUCE_ROOT_DNS_LOOKUPS */
120 _PUBLIC_ struct in_addr sys_inet_makeaddr(int net, int host)
124 in = inet_makeaddr(net, host);
125 in2.s_addr = in.s_addr;
129 /**************************************************************************
130 Wrapper for fork. Ensures we clear our pid cache.
131 ****************************************************************************/
133 static pid_t mypid = (pid_t)-1;
135 _PUBLIC_ pid_t sys_fork(void)
137 pid_t forkret = fork();
139 if (forkret == (pid_t)0) {
140 /* Child - reset mypid so sys_getpid does a system call. */
147 /**************************************************************************
148 Wrapper for getpid. Ensures we only do a system call *once*.
149 ****************************************************************************/
151 _PUBLIC_ pid_t sys_getpid(void)
153 if (mypid == (pid_t)-1)
160 _PUBLIC_ int sys_getpeereid( int s, uid_t *uid)
162 #if defined(HAVE_PEERCRED)
164 socklen_t cred_len = sizeof(struct ucred);
167 ret = getsockopt(s, SOL_SOCKET, SO_PEERCRED, (void *)&cred, &cred_len);
172 if (cred_len != sizeof(struct ucred)) {
180 #if defined(HAVE_GETPEEREID)
182 return getpeereid(s, uid, &gid);
189 _PUBLIC_ int sys_getnameinfo(const struct sockaddr *psa,
198 * For Solaris we must make sure salen is the
199 * correct length for the incoming sa_family.
202 if (salen == sizeof(struct sockaddr_storage)) {
203 salen = sizeof(struct sockaddr_in);
204 #if defined(HAVE_IPV6)
205 if (psa->sa_family == AF_INET6) {
206 salen = sizeof(struct sockaddr_in6);
210 return getnameinfo(psa, salen, host, hostlen, service, servlen, flags);
213 _PUBLIC_ int sys_connect(int fd, const struct sockaddr * addr)
215 socklen_t salen = (socklen_t)-1;
217 if (addr->sa_family == AF_INET) {
218 salen = sizeof(struct sockaddr_in);
219 } else if (addr->sa_family == AF_UNIX) {
220 salen = sizeof(struct sockaddr_un);
222 #if defined(HAVE_IPV6)
223 else if (addr->sa_family == AF_INET6) {
224 salen = sizeof(struct sockaddr_in6);
228 return connect(fd, addr, salen);