s3:smb2_server: add supports_multicredit to sconn
[Samba/gbeck.git] / lib / util / system.c
blob8625229404aa4a98e901faa622f31e1b46db9f0a
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 Wrapper for fork. Ensures we clear our pid cache.
80 ****************************************************************************/
82 static pid_t mypid = (pid_t)-1;
84 _PUBLIC_ pid_t sys_fork(void)
86 pid_t forkret = fork();
88 if (forkret == (pid_t)0) {
89 /* Child - reset mypid so sys_getpid does a system call. */
90 mypid = (pid_t) -1;
93 return forkret;
96 /**************************************************************************
97 Wrapper for getpid. Ensures we only do a system call *once*.
98 ****************************************************************************/
100 _PUBLIC_ pid_t sys_getpid(void)
102 if (mypid == (pid_t)-1)
103 mypid = getpid();
105 return mypid;
109 _PUBLIC_ int sys_getpeereid( int s, uid_t *uid)
111 #if defined(HAVE_PEERCRED)
112 struct ucred cred;
113 socklen_t cred_len = sizeof(struct ucred);
114 int ret;
116 ret = getsockopt(s, SOL_SOCKET, SO_PEERCRED, (void *)&cred, &cred_len);
117 if (ret != 0) {
118 return -1;
121 if (cred_len != sizeof(struct ucred)) {
122 errno = EINVAL;
123 return -1;
126 *uid = cred.uid;
127 return 0;
128 #else
129 #if defined(HAVE_GETPEEREID)
130 gid_t gid;
131 return getpeereid(s, uid, &gid);
132 #endif
133 errno = ENOSYS;
134 return -1;
135 #endif
138 _PUBLIC_ int sys_getnameinfo(const struct sockaddr *psa,
139 int salen,
140 char *host,
141 size_t hostlen,
142 char *service,
143 size_t servlen,
144 int flags)
147 * For Solaris we must make sure salen is the
148 * correct length for the incoming sa_family.
151 if (salen == sizeof(struct sockaddr_storage)) {
152 salen = sizeof(struct sockaddr_in);
153 #if defined(HAVE_IPV6)
154 if (psa->sa_family == AF_INET6) {
155 salen = sizeof(struct sockaddr_in6);
157 #endif
159 return getnameinfo(psa, salen, host, hostlen, service, servlen, flags);
162 _PUBLIC_ int sys_connect(int fd, const struct sockaddr * addr)
164 socklen_t salen = (socklen_t)-1;
166 if (addr->sa_family == AF_INET) {
167 salen = sizeof(struct sockaddr_in);
168 } else if (addr->sa_family == AF_UNIX) {
169 salen = sizeof(struct sockaddr_un);
171 #if defined(HAVE_IPV6)
172 else if (addr->sa_family == AF_INET6) {
173 salen = sizeof(struct sockaddr_in6);
175 #endif
177 return connect(fd, addr, salen);