libcli/smb: add SMB2_DIALECT_REVISION_224
[Samba.git] / lib / util / system.c
blob1e80f1a88a301828913c724b0485e6f77a93e473
1 /*
2 Unix SMB/CIFS implementation.
3 Samba system utilities
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/>.
21 #include "includes.h"
22 #include "system/network.h"
23 #include "system/filesys.h"
25 #undef malloc
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)
49 void *p = NULL;
50 int ret = posix_memalign( &p, align, size );
51 if ( ret == 0 )
52 return p;
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;
71 if (size < pagesize) {
72 size = pagesize;
74 return malloc(size);
75 #endif
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 ****************************************************************************/
84 _PUBLIC_ struct hostent *sys_gethostbyname(const char *name)
86 #ifdef REDUCE_ROOT_DNS_LOOKUPS
87 char query[256], hostname[256];
88 char *domain;
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
97 original string.
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
107 original string.
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)
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;
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. */
141 mypid = (pid_t) -1;
144 return forkret;
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)
154 mypid = getpid();
156 return mypid;
160 _PUBLIC_ int sys_getpeereid( int s, uid_t *uid)
162 #if defined(HAVE_PEERCRED)
163 struct ucred cred;
164 socklen_t cred_len = sizeof(struct ucred);
165 int ret;
167 ret = getsockopt(s, SOL_SOCKET, SO_PEERCRED, (void *)&cred, &cred_len);
168 if (ret != 0) {
169 return -1;
172 if (cred_len != sizeof(struct ucred)) {
173 errno = EINVAL;
174 return -1;
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
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)
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);
208 #endif
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);
226 #endif
228 return connect(fd, addr, salen);