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 */
143 /*******************************************************************
144 just a unlink wrapper that calls dos_to_unix.
145 ********************************************************************/
146 int dos_unlink(char *fname)
148 return(unlink(dos_to_unix(fname,False)));
152 /*******************************************************************
153 a simple open() wrapper that calls dos_to_unix.
154 ********************************************************************/
155 int dos_open(char *fname,int flags,int mode)
157 return(open(dos_to_unix(fname,False),flags,mode));
161 /*******************************************************************
162 a simple opendir() wrapper that calls dos_to_unix
163 ********************************************************************/
164 DIR *dos_opendir(char *dname)
166 return(opendir(dos_to_unix(dname,False)));
170 /*******************************************************************
171 and a stat() wrapper that calls dos_to_unix.
172 ********************************************************************/
173 int dos_stat(char *fname,SMB_STRUCT_STAT *sbuf)
175 return(stat(dos_to_unix(fname,False),sbuf));
178 /*******************************************************************
179 The wait() calls vary between systems
180 ********************************************************************/
181 int sys_waitpid(pid_t pid,int *status,int options)
184 return waitpid(pid,status,options);
185 #else /* HAVE_WAITPID */
186 return wait4(pid, status, options, NULL);
187 #endif /* HAVE_WAITPID */
190 /*******************************************************************
191 don't forget lstat() that calls dos_to_unix.
192 ********************************************************************/
193 int dos_lstat(char *fname,struct stat *sbuf)
195 return(lstat(dos_to_unix(fname,False),sbuf));
199 /*******************************************************************
200 mkdir() gets a wrapper that calls dos_to_unix.
201 ********************************************************************/
202 int dos_mkdir(char *dname,int mode)
204 return(mkdir(dos_to_unix(dname,False),mode));
208 /*******************************************************************
209 do does rmdir() - call dos_to_unix
210 ********************************************************************/
211 int dos_rmdir(char *dname)
213 return(rmdir(dos_to_unix(dname,False)));
217 /*******************************************************************
218 I almost forgot chdir() - call dos_to_unix.
219 ********************************************************************/
220 int dos_chdir(char *dname)
222 return(chdir(dos_to_unix(dname,False)));
226 /*******************************************************************
227 now for utime() - call dos_to_unix.
228 ********************************************************************/
229 int dos_utime(char *fname,struct utimbuf *times)
231 /* if the modtime is 0 or -1 then ignore the call and
233 if (times->modtime == (time_t)0 || times->modtime == (time_t)-1)
236 /* if the access time is 0 or -1 then set it to the modtime */
237 if (times->actime == (time_t)0 || times->actime == (time_t)-1)
238 times->actime = times->modtime;
240 return(utime(dos_to_unix(fname,False),times));
243 /*********************************************************
244 for rename across filesystems Patch from Warren Birnbaum
245 <warrenb@hpcvscdp.cv.hp.com>
246 **********************************************************/
248 static int copy_reg(char *source, const char *dest)
250 struct stat source_stats;
254 int len; /* Number of bytes read into `buf'. */
256 lstat (source, &source_stats);
257 if (!S_ISREG (source_stats.st_mode))
262 if (unlink (dest) && errno != ENOENT)
267 if((ifd = open (source, O_RDONLY, 0)) < 0)
271 if((ofd = open (dest, O_WRONLY | O_CREAT | O_TRUNC, 0600)) < 0 )
277 if((buf = malloc( COPYBUF_SIZE )) == NULL)
285 while ((len = read(ifd, buf, COPYBUF_SIZE)) > 0)
287 if (write_data(ofd, buf, len) < 0)
315 /* chown turns off set[ug]id bits for non-root,
316 so do the chmod last. */
318 /* Try to copy the old file's modtime and access time. */
322 tv.actime = source_stats.st_atime;
323 tv.modtime = source_stats.st_mtime;
324 if (utime (dest, &tv))
330 /* Try to preserve ownership. For non-root it might fail, but that's ok.
331 But root probably wants to know, e.g. if NFS disallows it. */
332 if (chown (dest, source_stats.st_uid, source_stats.st_gid)
338 if (chmod (dest, source_stats.st_mode & 07777))
346 /*******************************************************************
347 for rename() - call dos_to_unix.
348 ********************************************************************/
349 int dos_rename(char *from, char *to)
354 pstrcpy (zfrom, dos_to_unix (from, False));
355 pstrcpy (zto, dos_to_unix (to, False));
356 rcode = rename (zfrom, zto);
360 /* Rename across filesystems needed. */
361 rcode = copy_reg (zfrom, zto);
366 /*******************************************************************
367 for chmod - call dos_to_unix.
368 ********************************************************************/
369 int dos_chmod(char *fname,int mode)
371 return(chmod(dos_to_unix(fname,False),mode));
374 /*******************************************************************
376 ********************************************************************/
377 char *sys_getwd(char *s)
381 wd = (char *)getcwd(s, sizeof (pstring));
383 wd = (char *)getwd(s);
386 unix_to_dos(wd, True);
390 /*******************************************************************
391 chown isn't used much but OS/2 doesn't have it
392 ********************************************************************/
393 int sys_chown(char *fname,int uid,int gid)
398 DEBUG(1,("WARNING: no chown!\n"));
402 return(chown(fname,uid,gid));
406 /*******************************************************************
407 os/2 also doesn't have chroot
408 ********************************************************************/
409 int sys_chroot(char *dname)
414 DEBUG(1,("WARNING: no chroot!\n"));
418 return(chroot(dname));
422 /**************************************************************************
423 A wrapper for gethostbyname() that tries avoids looking up hostnames
424 in the root domain, which can cause dial-on-demand links to come up for no
426 ****************************************************************************/
427 struct hostent *sys_gethostbyname(char *name)
429 #ifdef REDUCE_ROOT_DNS_LOOKUPS
430 char query[256], hostname[256];
433 /* Does this name have any dots in it? If so, make no change */
435 if (strchr(name, '.'))
436 return(gethostbyname(name));
438 /* Get my hostname, which should have domain name
439 attached. If not, just do the gethostname on the
443 gethostname(hostname, sizeof(hostname) - 1);
444 hostname[sizeof(hostname) - 1] = 0;
445 if ((domain = strchr(hostname, '.')) == NULL)
446 return(gethostbyname(name));
448 /* Attach domain name to query and do modified query.
449 If names too large, just do gethostname on the
453 if((strlen(name) + strlen(domain)) >= sizeof(query))
454 return(gethostbyname(name));
456 slprintf(query, sizeof(query)-1, "%s%s", name, domain);
457 return(gethostbyname(query));
458 #else /* REDUCE_ROOT_DNS_LOOKUPS */
459 return(gethostbyname(name));
460 #endif /* REDUCE_ROOT_DNS_LOOKUPS */