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,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 *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 fds,NULL,NULL,tval?&t2:NULL);
135 } while (selrtn<0 && errno == EINTR);
139 #endif /* USE_POLL */
140 #endif /* NO_SELECT */
142 /*******************************************************************
143 A stat() wrapper that will deal with 64 bit filesizes.
144 ********************************************************************/
146 int sys_stat(const char *fname,SMB_STRUCT_STAT *sbuf)
148 #if defined(HAVE_OFF64_T) && defined(HAVE_STAT64)
149 return stat64(fname, sbuf);
151 return stat(fname, sbuf);
155 /*******************************************************************
156 An fstat() wrapper that will deal with 64 bit filesizes.
157 ********************************************************************/
159 int sys_fstat(int fd,SMB_STRUCT_STAT *sbuf)
161 #if defined(HAVE_OFF64_T) && defined(HAVE_FSTAT64)
162 return fstat64(fd, sbuf);
164 return fstat(fd, sbuf);
168 /*******************************************************************
169 An lstat() wrapper that will deal with 64 bit filesizes.
170 ********************************************************************/
172 int sys_lstat(const char *fname,SMB_STRUCT_STAT *sbuf)
174 #if defined(HAVE_OFF64_T) && defined(HAVE_LSTAT64)
175 return lstat64(fname, sbuf);
177 return lstat(fname, sbuf);
181 /*******************************************************************
182 An ftruncate() wrapper that will deal with 64 bit filesizes.
183 ********************************************************************/
185 int sys_ftruncate(int fd, SMB_OFF_T offset)
187 #if defined(HAVE_OFF64_T) && defined(HAVE_FTRUNCATE64)
188 return ftruncate64(fd, offset);
190 return ftruncate(fd, offset);
194 /*******************************************************************
195 An lseek() wrapper that will deal with 64 bit filesizes.
196 ********************************************************************/
198 SMB_OFF_T sys_lseek(int fd, SMB_OFF_T offset, int whence)
200 #if defined(HAVE_OFF64_T) && defined(HAVE_LSEEK64)
201 return lseek64(fd, offset, whence);
203 return lseek(fd, offset, whence);
207 /*******************************************************************
208 An fseek() wrapper that will deal with 64 bit filesizes.
209 ********************************************************************/
211 int sys_fseek(FILE *fp, SMB_OFF_T offset, int whence)
213 #if defined(LARGE_SMB_OFF_T) && defined(HAVE_FSEEK64)
214 return fseek64(fp, offset, whence);
216 return fseek(fp, offset, whence);
220 /*******************************************************************
221 An ftell() wrapper that will deal with 64 bit filesizes.
222 ********************************************************************/
224 SMB_OFF_T sys_ftell(FILE *fp)
226 #if defined(LARGE_SMB_OFF_T) && defined(HAVE_FTELL64)
227 return (SMB_OFF_T)ftell64(fp);
229 return (SMB_OFF_T)ftell(fp);
233 /*******************************************************************
234 A creat() wrapper that will deal with 64 bit filesizes.
235 ********************************************************************/
237 int sys_creat(const char *path, mode_t mode)
239 #if defined(HAVE_CREAT64)
240 return creat64(path, mode);
243 * If creat64 isn't defined then ensure we call a potential open64.
246 return sys_open(path, O_WRONLY | O_CREAT | O_TRUNC, mode);
250 /*******************************************************************
251 An open() wrapper that will deal with 64 bit filesizes.
252 ********************************************************************/
254 int sys_open(const char *path, int oflag, mode_t mode)
256 #if defined(HAVE_OPEN64)
257 return open64(path, oflag, mode);
259 return open(path, oflag, mode);
263 /*******************************************************************
264 An fopen() wrapper that will deal with 64 bit filesizes.
265 ********************************************************************/
267 FILE *sys_fopen(const char *path, const char *type)
269 #if defined(HAVE_FOPEN64)
270 return fopen64(path, type);
272 return fopen(path, type);
276 /*******************************************************************
277 An mmap() wrapper that will deal with 64 bit filesizes.
278 ********************************************************************/
280 void *sys_mmap(void *addr, size_t len, int prot, int flags, int fd, SMB_OFF_T offset)
282 #if defined(LARGE_SMB_OFF_T) && defined(HAVE_MMAP64)
283 return mmap64(addr, len, prot, flags, fd, offset);
285 return mmap(addr, len, prot, flags, fd, offset);
289 /*******************************************************************
290 The wait() calls vary between systems
291 ********************************************************************/
293 int sys_waitpid(pid_t pid,int *status,int options)
296 return waitpid(pid,status,options);
297 #else /* HAVE_WAITPID */
298 return wait4(pid, status, options, NULL);
299 #endif /* HAVE_WAITPID */
302 /*******************************************************************
303 system wrapper for getwd
304 ********************************************************************/
305 char *sys_getwd(char *s)
309 wd = (char *)getcwd(s, sizeof (pstring));
311 wd = (char *)getwd(s);
316 /*******************************************************************
317 chown isn't used much but OS/2 doesn't have it
318 ********************************************************************/
319 int sys_chown(const char *fname,uid_t uid,gid_t gid)
324 DEBUG(1,("WARNING: no chown!\n"));
328 return(chown(fname,uid,gid));
332 /*******************************************************************
333 os/2 also doesn't have chroot
334 ********************************************************************/
335 int sys_chroot(const char *dname)
340 DEBUG(1,("WARNING: no chroot!\n"));
344 return(chroot(dname));
348 /**************************************************************************
349 A wrapper for gethostbyname() that tries avoids looking up hostnames
350 in the root domain, which can cause dial-on-demand links to come up for no
352 ****************************************************************************/
353 struct hostent *sys_gethostbyname(const char *name)
355 #ifdef REDUCE_ROOT_DNS_LOOKUPS
356 char query[256], hostname[256];
359 /* Does this name have any dots in it? If so, make no change */
361 if (strchr(name, '.'))
362 return(gethostbyname(name));
364 /* Get my hostname, which should have domain name
365 attached. If not, just do the gethostname on the
369 gethostname(hostname, sizeof(hostname) - 1);
370 hostname[sizeof(hostname) - 1] = 0;
371 if ((domain = strchr(hostname, '.')) == NULL)
372 return(gethostbyname(name));
374 /* Attach domain name to query and do modified query.
375 If names too large, just do gethostname on the
379 if((strlen(name) + strlen(domain)) >= sizeof(query))
380 return(gethostbyname(name));
382 slprintf(query, sizeof(query)-1, "%s%s", name, domain);
383 return(gethostbyname(query));
384 #else /* REDUCE_ROOT_DNS_LOOKUPS */
385 return(gethostbyname(name));
386 #endif /* REDUCE_ROOT_DNS_LOOKUPS */
390 /**************************************************************************
391 Try and abstract process capabilities (for systems that have them).
392 ****************************************************************************/
394 BOOL set_process_capability( uint32 cap_flag, BOOL enable )
396 #if defined(HAVE_IRIX_SPECIFIC_CAPABILITIES)
397 if(cap_flag == KERNEL_OPLOCK_CAPABILITY)
399 cap_t cap = cap_get_proc();
402 DEBUG(0,("set_process_capability: cap_get_proc failed. Error was %s\n",
408 cap->cap_effective |= CAP_NETWORK_MGT;
410 cap->cap_effective &= ~CAP_NETWORK_MGT;
412 if (cap_set_proc(cap) == -1) {
413 DEBUG(0,("set_process_capability: cap_set_proc failed. Error was %s\n",
418 DEBUG(10,("set_process_capability: Set KERNEL_OPLOCK_CAPABILITY.\n"));
424 /**************************************************************************
425 Try and abstract inherited process capabilities (for systems that have them).
426 ****************************************************************************/
428 BOOL set_inherited_process_capability( uint32 cap_flag, BOOL enable )
430 #if defined(HAVE_IRIX_SPECIFIC_CAPABILITIES)
431 if(cap_flag == KERNEL_OPLOCK_CAPABILITY)
433 cap_t cap = cap_get_proc();
436 DEBUG(0,("set_inherited_process_capability: cap_get_proc failed. Error was %s\n",
442 cap->cap_inheritable |= CAP_NETWORK_MGT;
444 cap->cap_inheritable &= ~CAP_NETWORK_MGT;
446 if (cap_set_proc(cap) == -1) {
447 DEBUG(0,("set_inherited_process_capability: cap_set_proc failed. Error was %s\n",
452 DEBUG(10,("set_inherited_process_capability: Set KERNEL_OPLOCK_CAPABILITY.\n"));
458 /**************************************************************************
459 Wrapper for random().
460 ****************************************************************************/
462 long sys_random(void)
464 #if defined(HAVE_RANDOM)
465 return (long)random();
466 #elif defined(HAVE_RAND)
469 DEBUG(0,("Error - no random function available !\n"));
474 /**************************************************************************
475 Wrapper for srandom().
476 ****************************************************************************/
478 void sys_srandom(unsigned int seed)
480 #if defined(HAVE_SRANDOM)
482 #elif defined(HAVE_SRAND)
485 DEBUG(0,("Error - no srandom function available !\n"));
490 /**************************************************************************
491 Wrapper for getgroups. Deals with broken (int) case.
492 ****************************************************************************/
494 int sys_getgroups(int setlen, gid_t *gidset)
496 #if !defined(HAVE_BROKEN_GETGROUPS)
497 return getgroups(setlen, gidset);
505 return getgroups(setlen, &gid);
509 * Broken case. We need to allocate a
510 * GID_T array of size setlen.
518 if (setlen == 0) setlen = 1;
520 if((group_list = (GID_T *)malloc(setlen * sizeof(GID_T))) == NULL) {
521 DEBUG(0,("sys_getgroups: Malloc fail.\n"));
525 if((ngroups = getgroups(setlen, group_list)) < 0) {
526 int saved_errno = errno;
527 free((char *)group_list);
532 for(i = 0; i < ngroups; i++)
533 gidset[i] = (gid_t)group_list[i];
535 free((char *)group_list);
537 #endif /* HAVE_BROKEN_GETGROUPS */
541 * We only wrap pw_name and pw_passwd for now as these
542 * are the only potentially modified fields.
545 /**************************************************************************
546 Helper function for getpwnam/getpwuid wrappers.
547 ****************************************************************************/
549 static struct passwd *setup_pwret(struct passwd *pass)
551 static pstring pw_name;
552 static pstring pw_passwd;
553 static struct passwd pw_ret;
560 memcpy((char *)&pw_ret, pass, sizeof(struct passwd));
565 pw_ret.pw_name = pw_name;
566 pstrcpy(pw_ret.pw_name, pass->pw_name);
572 pw_ret.pw_passwd = pw_passwd;
573 pstrcpy(pw_ret.pw_passwd, pass->pw_passwd);
579 /**************************************************************************
580 Wrapper for getpwnam(). Always returns a static that can be modified.
581 ****************************************************************************/
583 struct passwd *sys_getpwnam(const char *name)
585 return setup_pwret(getpwnam(name));
588 /**************************************************************************
589 Wrapper for getpwuid(). Always returns a static that can be modified.
590 ****************************************************************************/
592 struct passwd *sys_getpwuid(uid_t uid)
594 return setup_pwret(getpwuid(uid));