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(fd_set *fds,struct timeval *tval)
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);
93 int sys_select(fd_set *fds,struct timeval *tval)
99 if (tval) memcpy((void *)&t2,(void *)tval,sizeof(t2));
101 selrtn = select(255,SELECT_CAST fds,NULL,NULL,tval?&t2:NULL);
102 } while (selrtn<0 && errno == EINTR);
109 /*******************************************************************
110 just a unlink wrapper
111 ********************************************************************/
112 int sys_unlink(char *fname)
114 return(unlink(dos_to_unix(fname,False)));
118 /*******************************************************************
119 a simple open() wrapper
120 ********************************************************************/
121 int sys_open(char *fname,int flags,int mode)
123 return(open(dos_to_unix(fname,False),flags,mode));
127 /*******************************************************************
128 a simple opendir() wrapper
129 ********************************************************************/
130 DIR *sys_opendir(char *dname)
132 return(opendir(dos_to_unix(dname,False)));
136 /*******************************************************************
138 ********************************************************************/
139 int sys_stat(char *fname,struct stat *sbuf)
141 return(stat(dos_to_unix(fname,False),sbuf));
144 /*******************************************************************
145 The wait() calls vary between systems
146 ********************************************************************/
147 int sys_waitpid(pid_t pid,int *status,int options)
150 return waitpid(pid,status,options);
151 #else /* USE_WAITPID */
152 return wait4(pid, status, options, NULL);
153 #endif /* USE_WAITPID */
156 /*******************************************************************
158 ********************************************************************/
159 int sys_lstat(char *fname,struct stat *sbuf)
161 return(lstat(dos_to_unix(fname,False),sbuf));
165 /*******************************************************************
166 mkdir() gets a wrapper
167 ********************************************************************/
168 int sys_mkdir(char *dname,int mode)
170 return(mkdir(dos_to_unix(dname,False),mode));
174 /*******************************************************************
176 ********************************************************************/
177 int sys_rmdir(char *dname)
179 return(rmdir(dos_to_unix(dname,False)));
183 /*******************************************************************
184 I almost forgot chdir()
185 ********************************************************************/
186 int sys_chdir(char *dname)
188 return(chdir(dos_to_unix(dname,False)));
192 /*******************************************************************
194 ********************************************************************/
195 int sys_utime(char *fname,struct utimbuf *times)
197 /* if the modtime is 0 or -1 then ignore the call and
199 if (times->modtime == (time_t)0 || times->modtime == (time_t)-1)
202 /* if the access time is 0 or -1 then set it to the modtime */
203 if (times->actime == (time_t)0 || times->actime == (time_t)-1)
204 times->actime = times->modtime;
206 return(utime(dos_to_unix(fname,False),times));
209 /*********************************************************
210 for rename across filesystems Patch from Warren Birnbaum
211 <warrenb@hpcvscdp.cv.hp.com>
212 **********************************************************/
214 static int copy_reg(char *source, const char *dest)
216 struct stat source_stats;
220 int len; /* Number of bytes read into `buf'. */
222 lstat (source, &source_stats);
223 if (!S_ISREG (source_stats.st_mode))
228 if (unlink (dest) && errno != ENOENT)
233 if((ifd = open (source, O_RDONLY, 0)) < 0)
237 if((ofd = open (dest, O_WRONLY | O_CREAT | O_TRUNC, 0600)) < 0 )
243 if((buf = malloc( COPYBUF_SIZE )) == NULL)
251 while ((len = read(ifd, buf, COPYBUF_SIZE)) > 0)
253 if (write_data(ofd, buf, len) < 0)
281 /* chown turns off set[ug]id bits for non-root,
282 so do the chmod last. */
284 /* Try to copy the old file's modtime and access time. */
288 tv.actime = source_stats.st_atime;
289 tv.modtime = source_stats.st_mtime;
290 if (utime (dest, &tv))
296 /* Try to preserve ownership. For non-root it might fail, but that's ok.
297 But root probably wants to know, e.g. if NFS disallows it. */
298 if (chown (dest, source_stats.st_uid, source_stats.st_gid)
304 if (chmod (dest, source_stats.st_mode & 07777))
312 /*******************************************************************
314 ********************************************************************/
315 int sys_rename(char *from, char *to)
320 pstrcpy (zfrom, dos_to_unix (from, False));
321 pstrcpy (zto, dos_to_unix (to, False));
322 rcode = rename (zfrom, zto);
326 /* Rename across filesystems needed. */
327 rcode = copy_reg (zfrom, zto);
332 /*******************************************************************
334 ********************************************************************/
335 int sys_chmod(char *fname,int mode)
337 return(chmod(dos_to_unix(fname,False),mode));
340 /*******************************************************************
342 ********************************************************************/
343 char *sys_getwd(char *s)
347 wd = (char *) getcwd (s, sizeof (pstring));
349 wd = (char *) getwd (s);
352 unix_to_dos (wd, True);
356 /*******************************************************************
357 chown isn't used much but OS/2 doesn't have it
358 ********************************************************************/
359 int sys_chown(char *fname,int uid,int gid)
362 DEBUG(1,("Warning - chown(%s,%d,%d) not done\n",fname,uid,gid));
364 return(chown(fname,uid,gid));
368 /*******************************************************************
369 os/2 also doesn't have chroot
370 ********************************************************************/
371 int sys_chroot(char *dname)
374 DEBUG(1,("Warning - chroot(%s) not done\n",dname));
376 return(chroot(dname));
380 /**************************************************************************
381 A wrapper for gethostbyname() that tries avoids looking up hostnames
382 in the root domain, which can cause dial-on-demand links to come up for no
384 ****************************************************************************/
385 struct hostent *sys_gethostbyname(char *name)
387 #ifdef REDUCE_ROOT_DNS_LOOKUPS
388 char query[256], hostname[256];
391 /* Does this name have any dots in it? If so, make no change */
393 if (strchr(name, '.'))
394 return(gethostbyname(name));
396 /* Get my hostname, which should have domain name
397 attached. If not, just do the gethostname on the
401 gethostname(hostname, sizeof(hostname) - 1);
402 hostname[sizeof(hostname) - 1] = 0;
403 if ((domain = strchr(hostname, '.')) == NULL)
404 return(gethostbyname(name));
406 /* Attach domain name to query and do modified query.
407 If names too large, just do gethostname on the
411 if((strlen(name) + strlen(domain)) >= sizeof(query))
412 return(gethostbyname(name));
414 slprintf(query, sizeof(query)-1, "%s%s", name, domain);
415 return(gethostbyname(query));
416 #else /* REDUCE_ROOT_DNS_LOOKUPS */
417 return(gethostbyname(name));
418 #endif /* REDUCE_ROOT_DNS_LOOKUPS */