RHEL packaging: Try to fix makerpms.sh on RHEL.
[Samba.git] / lib / util / system.c
blob17c0553102b5be3af6df8593354299a135bbd1b9
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"
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.
40 /**************************************************************************
41 A wrapper for gethostbyname() that tries avoids looking up hostnames
42 in the root domain, which can cause dial-on-demand links to come up for no
43 apparent reason.
44 ****************************************************************************/
46 _PUBLIC_ struct hostent *sys_gethostbyname(const char *name)
48 #ifdef REDUCE_ROOT_DNS_LOOKUPS
49 char query[256], hostname[256];
50 char *domain;
52 /* Does this name have any dots in it? If so, make no change */
54 if (strchr(name, '.'))
55 return(gethostbyname(name));
57 /* Get my hostname, which should have domain name
58 attached. If not, just do the gethostname on the
59 original string.
62 gethostname(hostname, sizeof(hostname) - 1);
63 hostname[sizeof(hostname) - 1] = 0;
64 if ((domain = strchr(hostname, '.')) == NULL)
65 return(gethostbyname(name));
67 /* Attach domain name to query and do modified query.
68 If names too large, just do gethostname on the
69 original string.
72 if((strlen(name) + strlen(domain)) >= sizeof(query))
73 return(gethostbyname(name));
75 slprintf(query, sizeof(query)-1, "%s%s", name, domain);
76 return(gethostbyname(query));
77 #else /* REDUCE_ROOT_DNS_LOOKUPS */
78 return(gethostbyname(name));
79 #endif /* REDUCE_ROOT_DNS_LOOKUPS */
82 _PUBLIC_ struct in_addr sys_inet_makeaddr(int net, int host)
84 struct in_addr in;
85 struct in_addr in2;
86 in = inet_makeaddr(net, host);
87 in2.s_addr = in.s_addr;
88 return in2;
91 /**************************************************************************
92 Wrapper for fork. Ensures we clear our pid cache.
93 ****************************************************************************/
95 static pid_t mypid = (pid_t)-1;
97 _PUBLIC_ pid_t sys_fork(void)
99 pid_t forkret = fork();
101 if (forkret == (pid_t)0) {
102 /* Child - reset mypid so sys_getpid does a system call. */
103 mypid = (pid_t) -1;
106 return forkret;
109 /**************************************************************************
110 Wrapper for getpid. Ensures we only do a system call *once*.
111 ****************************************************************************/
113 _PUBLIC_ pid_t sys_getpid(void)
115 if (mypid == (pid_t)-1)
116 mypid = getpid();
118 return mypid;
122 _PUBLIC_ int sys_getpeereid( int s, uid_t *uid)
124 #if defined(HAVE_PEERCRED)
125 struct ucred cred;
126 socklen_t cred_len = sizeof(struct ucred);
127 int ret;
129 ret = getsockopt(s, SOL_SOCKET, SO_PEERCRED, (void *)&cred, &cred_len);
130 if (ret != 0) {
131 return -1;
134 if (cred_len != sizeof(struct ucred)) {
135 errno = EINVAL;
136 return -1;
139 *uid = cred.uid;
140 return 0;
141 #else
142 #if defined(HAVE_GETPEEREID)
143 gid_t gid;
144 return getpeereid(s, uid, &gid);
145 #endif
146 errno = ENOSYS;
147 return -1;
148 #endif
151 _PUBLIC_ int sys_getnameinfo(const struct sockaddr *psa,
152 int salen,
153 char *host,
154 size_t hostlen,
155 char *service,
156 size_t servlen,
157 int flags)
160 * For Solaris we must make sure salen is the
161 * correct length for the incoming sa_family.
164 if (salen == sizeof(struct sockaddr_storage)) {
165 salen = sizeof(struct sockaddr_in);
166 #if defined(HAVE_IPV6)
167 if (psa->sa_family == AF_INET6) {
168 salen = sizeof(struct sockaddr_in6);
170 #endif
172 return getnameinfo(psa, salen, host, hostlen, service, servlen, flags);
175 _PUBLIC_ int sys_connect(int fd, const struct sockaddr * addr)
177 socklen_t salen = (socklen_t)-1;
179 if (addr->sa_family == AF_INET) {
180 salen = sizeof(struct sockaddr_in);
181 } else if (addr->sa_family == AF_UNIX) {
182 salen = sizeof(struct sockaddr_un);
184 #if defined(HAVE_IPV6)
185 else if (addr->sa_family == AF_INET6) {
186 salen = sizeof(struct sockaddr_in6);
188 #endif
190 return connect(fd, addr, salen);