r9702: r9680@blu: tridge | 2005-08-27 18:45:08 +1000
[bbaumbach/samba-autobuild/.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 #include "pstring.h"
27
28 /*
29    The idea is that this file will eventually have wrappers around all
30    important system calls in samba. The aims are:
31
32    - to enable easier porting by putting OS dependent stuff in here
33
34    - to allow for hooks into other "pseudo-filesystems"
35
36    - to allow easier integration of things like the japanese extensions
37
38    - to support the philosophy of Samba to expose the features of
39      the OS within the SMB model. In general whatever file/printer/variable
40      expansions/etc make sense to the OS should be acceptable to Samba.
41 */
42
43
44
45 /*******************************************************************
46  A wrapper for usleep in case we don't have one.
47 ********************************************************************/
48
49 int sys_usleep(long usecs)
50 {
51 #ifndef HAVE_USLEEP
52         struct timeval tval;
53 #endif
54
55         /*
56          * We need this braindamage as the glibc usleep
57          * is not SPEC1170 complient... grumble... JRA.
58          */
59
60         if(usecs < 0 || usecs > 1000000) {
61                 errno = EINVAL;
62                 return -1;
63         }
64
65 #if HAVE_USLEEP
66         usleep(usecs);
67         return 0;
68 #else /* HAVE_USLEEP */
69         /*
70          * Fake it with select...
71          */
72         tval.tv_sec = 0;
73         tval.tv_usec = usecs/1000;
74         select(0,NULL,NULL,NULL,&tval);
75         return 0;
76 #endif /* HAVE_USLEEP */
77 }
78
79
80 /*******************************************************************
81  System wrapper for getwd
82 ********************************************************************/
83 char *sys_getwd(char *s)
84 {
85         char *wd;
86 #ifdef HAVE_GETCWD
87         wd = (char *)getcwd(s, sizeof (pstring));
88 #else
89         wd = (char *)getwd(s);
90 #endif
91         return wd;
92 }
93
94 /*******************************************************************
95 A read wrapper that will deal with EINTR.
96 ********************************************************************/
97
98 ssize_t sys_read(int fd, void *buf, size_t count)
99 {
100         ssize_t ret;
101
102         do {
103                 ret = read(fd, buf, count);
104         } while (ret == -1 && errno == EINTR);
105         return ret;
106 }
107
108 /*******************************************************************
109 A write wrapper that will deal with EINTR.
110 ********************************************************************/
111
112 ssize_t sys_write(int fd, const void *buf, size_t count)
113 {
114         ssize_t ret;
115
116         do {
117                 ret = write(fd, buf, count);
118         } while (ret == -1 && errno == EINTR);
119         return ret;
120 }
121
122
123
124 /*******************************************************************
125 os/2 also doesn't have chroot
126 ********************************************************************/
127 int sys_chroot(const char *dname)
128 {
129 #ifndef HAVE_CHROOT
130         static int done;
131         if (!done) {
132                 DEBUG(1,("WARNING: no chroot!\n"));
133                 done=1;
134         }
135         errno = ENOSYS;
136         return -1;
137 #else
138         return(chroot(dname));
139 #endif
140 }
141
142 /**************************************************************************
143 A wrapper for gethostbyname() that tries avoids looking up hostnames 
144 in the root domain, which can cause dial-on-demand links to come up for no
145 apparent reason.
146 ****************************************************************************/
147
148 struct hostent *sys_gethostbyname(const char *name)
149 {
150 #ifdef REDUCE_ROOT_DNS_LOOKUPS
151         char query[256], hostname[256];
152         char *domain;
153
154         /* Does this name have any dots in it? If so, make no change */
155
156         if (strchr_m(name, '.'))
157                 return(gethostbyname(name));
158
159         /* Get my hostname, which should have domain name 
160                 attached. If not, just do the gethostname on the
161                 original string. 
162         */
163
164         gethostname(hostname, sizeof(hostname) - 1);
165         hostname[sizeof(hostname) - 1] = 0;
166         if ((domain = strchr_m(hostname, '.')) == NULL)
167                 return(gethostbyname(name));
168
169         /* Attach domain name to query and do modified query.
170                 If names too large, just do gethostname on the
171                 original string.
172         */
173
174         if((strlen(name) + strlen(domain)) >= sizeof(query))
175                 return(gethostbyname(name));
176
177         slprintf(query, sizeof(query)-1, "%s%s", name, domain);
178         return(gethostbyname(query));
179 #else /* REDUCE_ROOT_DNS_LOOKUPS */
180         return(gethostbyname(name));
181 #endif /* REDUCE_ROOT_DNS_LOOKUPS */
182 }
183
184
185
186 /**************************************************************************
187  Wrappers for dlopen, dlsym, dlclose.
188 ****************************************************************************/
189
190 void *sys_dlopen(const char *name, int flags)
191 {
192 #if defined(HAVE_DLOPEN)
193         return dlopen(name, flags);
194 #else
195         return NULL;
196 #endif
197 }
198
199 void *sys_dlsym(void *handle, const char *symbol)
200 {
201 #if defined(HAVE_DLSYM)
202     return dlsym(handle, symbol);
203 #else
204     return NULL;
205 #endif
206 }
207
208 const char *sys_dlerror(void)
209 {
210 #if defined(HAVE_DLERROR)
211         return dlerror();
212 #else
213         return NULL;
214 #endif
215 }
216
217 const char *sys_inet_ntoa(struct ipv4_addr in)
218 {
219         struct in_addr in2;
220         in2.s_addr = in.addr;
221         return inet_ntoa(in2);
222 }
223
224 uint32_t sys_inet_addr(const char *s)
225 {
226         return inet_addr(s);
227 }
228
229 struct ipv4_addr sys_inet_makeaddr(int net, int host)
230 {
231         struct in_addr in;
232         struct ipv4_addr in2;
233         in = inet_makeaddr(net, host);
234         in2.addr = in.s_addr;
235         return in2;
236 }
237