b23529f5fe0d5f409306beefea5b357b2978bc8f
[samba.git] / source4 / lib / 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 2 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, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23 #include "system/network.h"
24 #include "system/wait.h"
25 #include "system/filesys.h"
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
44 /*******************************************************************
45  A wrapper for usleep in case we don't have one.
46 ********************************************************************/
47
48 int sys_usleep(long usecs)
49 {
50 #ifndef HAVE_USLEEP
51         struct timeval tval;
52 #endif
53
54         /*
55          * We need this braindamage as the glibc usleep
56          * is not SPEC1170 complient... grumble... JRA.
57          */
58
59         if(usecs < 0 || usecs > 1000000) {
60                 errno = EINVAL;
61                 return -1;
62         }
63
64 #if HAVE_USLEEP
65         usleep(usecs);
66         return 0;
67 #else /* HAVE_USLEEP */
68         /*
69          * Fake it with select...
70          */
71         tval.tv_sec = 0;
72         tval.tv_usec = usecs/1000;
73         select(0,NULL,NULL,NULL,&tval);
74         return 0;
75 #endif /* HAVE_USLEEP */
76 }
77
78
79 /*******************************************************************
80  System wrapper for getwd
81 ********************************************************************/
82 char *sys_getwd(char *s)
83 {
84         char *wd;
85 #ifdef HAVE_GETCWD
86         wd = (char *)getcwd(s, sizeof (pstring));
87 #else
88         wd = (char *)getwd(s);
89 #endif
90         return wd;
91 }
92
93 /*******************************************************************
94 A read wrapper that will deal with EINTR.
95 ********************************************************************/
96
97 ssize_t sys_read(int fd, void *buf, size_t count)
98 {
99         ssize_t ret;
100
101         do {
102                 ret = read(fd, buf, count);
103         } while (ret == -1 && errno == EINTR);
104         return ret;
105 }
106
107 /*******************************************************************
108 A write wrapper that will deal with EINTR.
109 ********************************************************************/
110
111 ssize_t sys_write(int fd, const void *buf, size_t count)
112 {
113         ssize_t ret;
114
115         do {
116                 ret = write(fd, buf, count);
117         } while (ret == -1 && errno == EINTR);
118         return ret;
119 }
120
121
122
123 /*******************************************************************
124 os/2 also doesn't have chroot
125 ********************************************************************/
126 int sys_chroot(const char *dname)
127 {
128 #ifndef HAVE_CHROOT
129         static int done;
130         if (!done) {
131                 DEBUG(1,("WARNING: no chroot!\n"));
132                 done=1;
133         }
134         errno = ENOSYS;
135         return -1;
136 #else
137         return(chroot(dname));
138 #endif
139 }
140
141 /**************************************************************************
142 A wrapper for gethostbyname() that tries avoids looking up hostnames 
143 in the root domain, which can cause dial-on-demand links to come up for no
144 apparent reason.
145 ****************************************************************************/
146
147 struct hostent *sys_gethostbyname(const char *name)
148 {
149 #ifdef REDUCE_ROOT_DNS_LOOKUPS
150         char query[256], hostname[256];
151         char *domain;
152
153         /* Does this name have any dots in it? If so, make no change */
154
155         if (strchr_m(name, '.'))
156                 return(gethostbyname(name));
157
158         /* Get my hostname, which should have domain name 
159                 attached. If not, just do the gethostname on the
160                 original string. 
161         */
162
163         gethostname(hostname, sizeof(hostname) - 1);
164         hostname[sizeof(hostname) - 1] = 0;
165         if ((domain = strchr_m(hostname, '.')) == NULL)
166                 return(gethostbyname(name));
167
168         /* Attach domain name to query and do modified query.
169                 If names too large, just do gethostname on the
170                 original string.
171         */
172
173         if((strlen(name) + strlen(domain)) >= sizeof(query))
174                 return(gethostbyname(name));
175
176         slprintf(query, sizeof(query)-1, "%s%s", name, domain);
177         return(gethostbyname(query));
178 #else /* REDUCE_ROOT_DNS_LOOKUPS */
179         return(gethostbyname(name));
180 #endif /* REDUCE_ROOT_DNS_LOOKUPS */
181 }
182
183
184
185 /**************************************************************************
186  Wrappers for dlopen, dlsym, dlclose.
187 ****************************************************************************/
188
189 void *sys_dlopen(const char *name, int flags)
190 {
191 #if defined(HAVE_DLOPEN)
192         return dlopen(name, flags);
193 #else
194         return NULL;
195 #endif
196 }
197
198 void *sys_dlsym(void *handle, const char *symbol)
199 {
200 #if defined(HAVE_DLSYM)
201     return dlsym(handle, symbol);
202 #else
203     return NULL;
204 #endif
205 }
206
207 const char *sys_dlerror(void)
208 {
209 #if defined(HAVE_DLERROR)
210         return dlerror();
211 #else
212         return NULL;
213 #endif
214 }
215
216 const char *sys_inet_ntoa(struct ipv4_addr in)
217 {
218         struct in_addr in2;
219         in2.s_addr = in.addr;
220         return inet_ntoa(in2);
221 }
222
223 uint32_t sys_inet_addr(const char *s)
224 {
225         return inet_addr(s);
226 }
227
228 struct ipv4_addr sys_inet_makeaddr(int net, int host)
229 {
230         struct in_addr in;
231         struct ipv4_addr in2;
232         in = inet_makeaddr(net, host);
233         in2.addr = in.s_addr;
234         return in2;
235 }
236