2 Unix SMB/Netbios implementation.
5 Copyright (C) Andrew Tridgell 1992-1998
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.
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, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 extern int DEBUGLEVEL;
27 The idea is that this file will eventually have wrappers around all
28 important system calls in samba. The aims are:
30 - to enable easier porting by putting OS dependent stuff in here
32 - to allow for hooks into other "pseudo-filesystems"
34 - to allow easier integration of things like the japanese extensions
36 - to support the philosophy of Samba to expose the features of
37 the OS within the SMB model. In general whatever file/printer/variable
38 expansions/etc make sense to the OS should be acceptable to Samba.
42 /*******************************************************************
43 this replaces the normal select() system call
44 return if some data has arrived on one of the file descriptors
46 ********************************************************************/
48 static int pollfd(int fd)
54 #elif defined(TCRDCHK)
55 (void)ioctl(fd, TCRDCHK, &r);
57 (void)ioctl(fd, FIONREAD, &r);
63 int sys_select(int maxfd, fd_set *fds, fd_set *w_fds, struct timeval *tval)
74 for (i=0;i<maxfd;i++) {
75 if (FD_ISSET(i,fds) && pollfd(i)>0) {
82 memcpy((void *)fds,(void *)&fds2,sizeof(fds2));
86 if (tval && tval->tv_sec < counter) return(0);
92 #else /* !NO_SELECT */
93 int sys_select(int maxfd, fd_set *r_fds, fd_set *w_fds, struct timeval *tval)
96 struct pollfd pfd[256];
103 for( i = 0; i < maxfd; i++) {
104 if(FD_ISSET(i,fds)) {
105 struct pollfd *pfdp = &pfd[maxpoll++];
107 pfdp->events = POLLIN;
112 timeout = (tval != NULL) ? (tval->tv_sec * 1000) + (tval->tv_usec/1000) :
116 pollrtn = poll( &pfd[0], maxpoll, timeout);
117 } while (pollrtn<0 && errno == EINTR);
121 for( i = 0; i < maxpoll; i++)
122 if( pfd[i].revents & POLLIN )
123 FD_SET(pfd[i].fd,fds);
132 if (tval) memcpy((void *)&t2,(void *)tval,sizeof(t2));
134 selrtn = select(maxfd,SELECT_CAST r_fds,SELECT_CAST w_fds,
136 } while (selrtn<0 && errno == EINTR);
140 #endif /* USE_POLL */
141 #endif /* NO_SELECT */
143 /*******************************************************************
144 A stat() wrapper that will deal with 64 bit filesizes.
145 ********************************************************************/
147 int sys_stat(const char *fname,SMB_STRUCT_STAT *sbuf)
149 #if defined(HAVE_OFF64_T) && defined(HAVE_STAT64)
150 return stat64(fname, sbuf);
152 return stat(fname, sbuf);
156 /*******************************************************************
157 An fstat() wrapper that will deal with 64 bit filesizes.
158 ********************************************************************/
160 int sys_fstat(int fd,SMB_STRUCT_STAT *sbuf)
162 #if defined(HAVE_OFF64_T) && defined(HAVE_FSTAT64)
163 return fstat64(fd, sbuf);
165 return fstat(fd, sbuf);
169 /*******************************************************************
170 An lstat() wrapper that will deal with 64 bit filesizes.
171 ********************************************************************/
173 int sys_lstat(const char *fname,SMB_STRUCT_STAT *sbuf)
175 #if defined(HAVE_OFF64_T) && defined(HAVE_LSTAT64)
176 return lstat64(fname, sbuf);
178 return lstat(fname, sbuf);
182 /*******************************************************************
183 An ftruncate() wrapper that will deal with 64 bit filesizes.
184 ********************************************************************/
186 int sys_ftruncate(int fd, SMB_OFF_T offset)
188 #if defined(HAVE_OFF64_T) && defined(HAVE_FTRUNCATE64)
189 return ftruncate64(fd, offset);
191 return ftruncate(fd, offset);
195 /*******************************************************************
196 An lseek() wrapper that will deal with 64 bit filesizes.
197 ********************************************************************/
199 SMB_OFF_T sys_lseek(int fd, SMB_OFF_T offset, int whence)
201 #if defined(HAVE_OFF64_T) && defined(HAVE_LSEEK64)
202 return lseek64(fd, offset, whence);
204 return lseek(fd, offset, whence);
208 /*******************************************************************
209 An fseek() wrapper that will deal with 64 bit filesizes.
210 ********************************************************************/
212 int sys_fseek(FILE *fp, SMB_OFF_T offset, int whence)
214 #if defined(LARGE_SMB_OFF_T) && defined(HAVE_FSEEK64)
215 return fseek64(fp, offset, whence);
217 return fseek(fp, offset, whence);
221 /*******************************************************************
222 An ftell() wrapper that will deal with 64 bit filesizes.
223 ********************************************************************/
225 SMB_OFF_T sys_ftell(FILE *fp)
227 #if defined(LARGE_SMB_OFF_T) && defined(HAVE_FTELL64)
228 return (SMB_OFF_T)ftell64(fp);
230 return (SMB_OFF_T)ftell(fp);
234 /*******************************************************************
235 A creat() wrapper that will deal with 64 bit filesizes.
236 ********************************************************************/
238 int sys_creat(const char *path, mode_t mode)
240 #if defined(HAVE_CREAT64)
241 return creat64(path, mode);
244 * If creat64 isn't defined then ensure we call a potential open64.
247 return sys_open(path, O_WRONLY | O_CREAT | O_TRUNC, mode);
251 /*******************************************************************
252 An open() wrapper that will deal with 64 bit filesizes.
253 ********************************************************************/
255 int sys_open(const char *path, int oflag, mode_t mode)
257 #if defined(HAVE_OPEN64)
258 return open64(path, oflag, mode);
260 return open(path, oflag, mode);
264 /*******************************************************************
265 An fopen() wrapper that will deal with 64 bit filesizes.
266 ********************************************************************/
268 FILE *sys_fopen(const char *path, const char *type)
270 #if defined(HAVE_FOPEN64)
271 return fopen64(path, type);
273 return fopen(path, type);
277 /*******************************************************************
278 An mmap() wrapper that will deal with 64 bit filesizes.
279 ********************************************************************/
281 void *sys_mmap(void *addr, size_t len, int prot, int flags, int fd, SMB_OFF_T offset)
283 #if defined(LARGE_SMB_OFF_T) && defined(HAVE_MMAP64)
284 return mmap64(addr, len, prot, flags, fd, offset);
286 return mmap(addr, len, prot, flags, fd, offset);
290 /*******************************************************************
291 The wait() calls vary between systems
292 ********************************************************************/
294 int sys_waitpid(pid_t pid,int *status,int options)
297 return waitpid(pid,status,options);
298 #else /* HAVE_WAITPID */
299 return wait4(pid, status, options, NULL);
300 #endif /* HAVE_WAITPID */
303 /*******************************************************************
304 system wrapper for getwd
305 ********************************************************************/
306 char *sys_getwd(char *s)
310 wd = (char *)getcwd(s, sizeof (pstring));
312 wd = (char *)getwd(s);
317 /*******************************************************************
318 chown isn't used much but OS/2 doesn't have it
319 ********************************************************************/
320 int sys_chown(const char *fname,uid_t uid,gid_t gid)
325 DEBUG(1,("WARNING: no chown!\n"));
329 return(chown(fname,uid,gid));
333 /*******************************************************************
334 os/2 also doesn't have chroot
335 ********************************************************************/
336 int sys_chroot(const char *dname)
341 DEBUG(1,("WARNING: no chroot!\n"));
345 return(chroot(dname));
349 /**************************************************************************
350 A wrapper for gethostbyname() that tries avoids looking up hostnames
351 in the root domain, which can cause dial-on-demand links to come up for no
353 ****************************************************************************/
354 struct hostent *sys_gethostbyname(const char *name)
356 #ifdef REDUCE_ROOT_DNS_LOOKUPS
357 char query[256], hostname[256];
360 /* Does this name have any dots in it? If so, make no change */
362 if (strchr(name, '.'))
363 return(gethostbyname(name));
365 /* Get my hostname, which should have domain name
366 attached. If not, just do the gethostname on the
370 gethostname(hostname, sizeof(hostname) - 1);
371 hostname[sizeof(hostname) - 1] = 0;
372 if ((domain = strchr(hostname, '.')) == NULL)
373 return(gethostbyname(name));
375 /* Attach domain name to query and do modified query.
376 If names too large, just do gethostname on the
380 if((strlen(name) + strlen(domain)) >= sizeof(query))
381 return(gethostbyname(name));
383 slprintf(query, sizeof(query)-1, "%s%s", name, domain);
384 return(gethostbyname(query));
385 #else /* REDUCE_ROOT_DNS_LOOKUPS */
386 return(gethostbyname(name));
387 #endif /* REDUCE_ROOT_DNS_LOOKUPS */
391 /**************************************************************************
392 Try and abstract process capabilities (for systems that have them).
393 ****************************************************************************/
395 BOOL set_process_capability( uint32 cap_flag, BOOL enable )
397 #if defined(HAVE_IRIX_SPECIFIC_CAPABILITIES)
398 if(cap_flag == KERNEL_OPLOCK_CAPABILITY)
400 cap_t cap = cap_get_proc();
403 DEBUG(0,("set_process_capability: cap_get_proc failed. Error was %s\n",
409 cap->cap_effective |= CAP_NETWORK_MGT;
411 cap->cap_effective &= ~CAP_NETWORK_MGT;
413 if (cap_set_proc(cap) == -1) {
414 DEBUG(0,("set_process_capability: cap_set_proc failed. Error was %s\n",
419 DEBUG(10,("set_process_capability: Set KERNEL_OPLOCK_CAPABILITY.\n"));
425 /**************************************************************************
426 Try and abstract inherited process capabilities (for systems that have them).
427 ****************************************************************************/
429 BOOL set_inherited_process_capability( uint32 cap_flag, BOOL enable )
431 #if defined(HAVE_IRIX_SPECIFIC_CAPABILITIES)
432 if(cap_flag == KERNEL_OPLOCK_CAPABILITY)
434 cap_t cap = cap_get_proc();
437 DEBUG(0,("set_inherited_process_capability: cap_get_proc failed. Error was %s\n",
443 cap->cap_inheritable |= CAP_NETWORK_MGT;
445 cap->cap_inheritable &= ~CAP_NETWORK_MGT;
447 if (cap_set_proc(cap) == -1) {
448 DEBUG(0,("set_inherited_process_capability: cap_set_proc failed. Error was %s\n",
453 DEBUG(10,("set_inherited_process_capability: Set KERNEL_OPLOCK_CAPABILITY.\n"));
459 /**************************************************************************
460 Wrapper for random().
461 ****************************************************************************/
463 long sys_random(void)
465 #if defined(HAVE_RANDOM)
466 return (long)random();
467 #elif defined(HAVE_RAND)
470 DEBUG(0,("Error - no random function available !\n"));
475 /**************************************************************************
476 Wrapper for srandom().
477 ****************************************************************************/
479 void sys_srandom(unsigned int seed)
481 #if defined(HAVE_SRANDOM)
483 #elif defined(HAVE_SRAND)
486 DEBUG(0,("Error - no srandom function available !\n"));
491 /**************************************************************************
492 Wrapper for getgroups. Deals with broken (int) case.
493 ****************************************************************************/
495 int sys_getgroups(int setlen, gid_t *gidset)
497 #if !defined(HAVE_BROKEN_GETGROUPS)
498 return getgroups(setlen, gidset);
506 return getgroups(setlen, &gid);
510 * Broken case. We need to allocate a
511 * GID_T array of size setlen.
519 if (setlen == 0) setlen = 1;
521 if((group_list = (GID_T *)malloc(setlen * sizeof(GID_T))) == NULL) {
522 DEBUG(0,("sys_getgroups: Malloc fail.\n"));
526 if((ngroups = getgroups(setlen, group_list)) < 0) {
527 int saved_errno = errno;
528 free((char *)group_list);
533 for(i = 0; i < ngroups; i++)
534 gidset[i] = (gid_t)group_list[i];
536 free((char *)group_list);
538 #endif /* HAVE_BROKEN_GETGROUPS */
542 * We only wrap pw_name and pw_passwd for now as these
543 * are the only potentially modified fields.
546 /**************************************************************************
547 Helper function for getpwnam/getpwuid wrappers.
548 ****************************************************************************/
550 struct passwd *copy_passwd_struct(struct passwd *pass)
552 static pstring pw_name;
553 static pstring pw_passwd;
554 static struct passwd pw_ret;
563 /* catch silly error where buffer was already copied */
564 DEBUG(0,("copy_passwd_struct: can't copy internal buffer!\n"));
568 memcpy((char *)&pw_ret, pass, sizeof(struct passwd));
573 pw_ret.pw_name = pw_name;
574 pstrcpy(pw_ret.pw_name, pass->pw_name);
580 pw_ret.pw_passwd = pw_passwd;
581 pstrcpy(pw_ret.pw_passwd, pass->pw_passwd);
587 /**************************************************************************
588 Wrapper for getpwnam(). Always returns a static that can be modified.
589 ****************************************************************************/
591 struct passwd *sys_getpwnam(const char *name)
593 return copy_passwd_struct(getpwnam(name));
596 /**************************************************************************
597 Wrapper for getpwuid(). Always returns a static that can be modified.
598 ****************************************************************************/
600 struct passwd *sys_getpwuid(uid_t uid)
602 return copy_passwd_struct(getpwuid(uid));