lib/util Move sys_memalign into lib/util/system.c
[ira/wip.git] / lib / util / system.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Samba system utilities
4    Copyright (C) Andrew Tridgell 1992-1998
5    Copyright (C) Jeremy Allison 1998-2002
6    
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.
11    
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.
16    
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/>.
19 */
20
21 #include "includes.h"
22 #include "system/network.h"
23 #include "system/filesys.h"
24
25 #undef malloc
26
27 /*
28    The idea is that this file will eventually have wrappers around all
29    important system calls in samba. The aims are:
30
31    - to enable easier porting by putting OS dependent stuff in here
32
33    - to allow for hooks into other "pseudo-filesystems"
34
35    - to allow easier integration of things like the japanese extensions
36
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.
40 */
41
42 /*******************************************************************
43  A wrapper for memalign
44 ********************************************************************/
45
46 void *sys_memalign( size_t align, size_t size )
47 {
48 #if defined(HAVE_POSIX_MEMALIGN)
49         void *p = NULL;
50         int ret = posix_memalign( &p, align, size );
51         if ( ret == 0 )
52                 return p;
53
54         return NULL;
55 #elif defined(HAVE_MEMALIGN)
56         return memalign( align, size );
57 #else
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();
64 #else
65         size_t pagesize = (size_t)-1;
66 #endif
67         if (pagesize == (size_t)-1) {
68                 DEBUG(0,("memalign functionalaity not available on this platform!\n"));
69                 return NULL;
70         }
71         if (size < pagesize) {
72                 size = pagesize;
73         }
74         return malloc(size);
75 #endif
76 }
77
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
81 apparent reason.
82 ****************************************************************************/
83
84 _PUBLIC_ struct hostent *sys_gethostbyname(const char *name)
85 {
86 #ifdef REDUCE_ROOT_DNS_LOOKUPS
87         char query[256], hostname[256];
88         char *domain;
89
90         /* Does this name have any dots in it? If so, make no change */
91
92         if (strchr(name, '.'))
93                 return(gethostbyname(name));
94
95         /* Get my hostname, which should have domain name 
96                 attached. If not, just do the gethostname on the
97                 original string. 
98         */
99
100         gethostname(hostname, sizeof(hostname) - 1);
101         hostname[sizeof(hostname) - 1] = 0;
102         if ((domain = strchr(hostname, '.')) == NULL)
103                 return(gethostbyname(name));
104
105         /* Attach domain name to query and do modified query.
106                 If names too large, just do gethostname on the
107                 original string.
108         */
109
110         if((strlen(name) + strlen(domain)) >= sizeof(query))
111                 return(gethostbyname(name));
112
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 */
118 }
119
120 _PUBLIC_ struct in_addr sys_inet_makeaddr(int net, int host)
121 {
122         struct in_addr in;
123         struct in_addr in2;
124         in = inet_makeaddr(net, host);
125         in2.s_addr = in.s_addr;
126         return in2;
127 }
128
129 /**************************************************************************
130  Wrapper for fork. Ensures we clear our pid cache.
131 ****************************************************************************/
132
133 static pid_t mypid = (pid_t)-1;
134
135 _PUBLIC_ pid_t sys_fork(void)
136 {
137         pid_t forkret = fork();
138
139         if (forkret == (pid_t)0) {
140                 /* Child - reset mypid so sys_getpid does a system call. */
141                 mypid = (pid_t) -1;
142         }
143
144         return forkret;
145 }
146
147 /**************************************************************************
148  Wrapper for getpid. Ensures we only do a system call *once*.
149 ****************************************************************************/
150
151 _PUBLIC_ pid_t sys_getpid(void)
152 {
153         if (mypid == (pid_t)-1)
154                 mypid = getpid();
155
156         return mypid;
157 }
158
159
160 _PUBLIC_ int sys_getpeereid( int s, uid_t *uid)
161 {
162 #if defined(HAVE_PEERCRED)
163         struct ucred cred;
164         socklen_t cred_len = sizeof(struct ucred);
165         int ret;
166
167         ret = getsockopt(s, SOL_SOCKET, SO_PEERCRED, (void *)&cred, &cred_len);
168         if (ret != 0) {
169                 return -1;
170         }
171
172         if (cred_len != sizeof(struct ucred)) {
173                 errno = EINVAL;
174                 return -1;
175         }
176
177         *uid = cred.uid;
178         return 0;
179 #else
180 #if defined(HAVE_GETPEEREID)
181         gid_t gid;
182         return getpeereid(s, uid, &gid);
183 #endif
184         errno = ENOSYS;
185         return -1;
186 #endif
187 }
188
189 _PUBLIC_ int sys_getnameinfo(const struct sockaddr *psa,
190                              int salen,
191                              char *host,
192                              size_t hostlen,
193                              char *service,
194                              size_t servlen,
195                              int flags)
196 {
197         /*
198          * For Solaris we must make sure salen is the
199          * correct length for the incoming sa_family.
200          */
201
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);
207                 }
208 #endif
209         }
210         return getnameinfo(psa, salen, host, hostlen, service, servlen, flags);
211 }
212
213 _PUBLIC_ int sys_connect(int fd, const struct sockaddr * addr)
214 {
215         socklen_t salen = (socklen_t)-1;
216
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);
221         }
222 #if defined(HAVE_IPV6)
223         else if (addr->sa_family == AF_INET6) {
224             salen = sizeof(struct sockaddr_in6);
225         }
226 #endif
227
228         return connect(fd, addr, salen);
229 }
230