2 Unix SMB/Netbios implementation.
5 Copyright (C) Andrew Tridgell 1998
6 Copyright (C) Derrell Lipman 2002-2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 * This is a rewrite of the original wrapped.c file, using libdl to obtain
24 * pointers into the C library rather than attempting to find undocumented
25 * functions in the C library to call for native file access. The problem
26 * with the original implementation's paradigm is that samba manipulates
27 * defines such that it gets the sizes of structures that it wants
28 * (e.g. mapping 32-bit functions to 64-bit functions with their associated
29 * 64-bit structure fields), but programs run under smbsh or using
30 * smbwrapper.so were not necessarily compiled with the same flags. As an
31 * example of the problem, a program calling stat() passes a pointer to a
32 * "struct stat" but the fields in that structure are different in samba than
33 * they are in the calling program if the calling program was not compiled to
34 * force stat() to be mapped to stat64().
36 * In this version, we provide an interface to each of the native functions,
37 * not just the ones that samba is compiled to map to. We obtain the function
38 * pointers from the C library using dlsym(), and for native file operations,
39 * directly call the same function that the calling application was
40 * requesting. Since the size of the calling application's structures vary
41 * depending on what function was called, we use our own internal structures
42 * for passing information to/from the SMB equivalent functions, and map them
43 * back to the native structures before returning the result to the caller.
45 * This implementation was completed 25 December 2002.
49 /* We do not want auto munging of 32->64 bit names in this file (only) */
50 #undef _FILE_OFFSET_BITS
53 #include <sys/types.h>
67 #define __USE_GNU /* need this to have RTLD_NEXT defined */
73 #include "libsmbclient.h"
74 #include "bsd-strlfunc.h"
80 * 0x1 = display symbol definitions not found in C library
81 * 0x2 = show wrapper functions being called
82 * 0x4 = log to file SMBW_DEBUG_FILE instead of stderr
84 #define SMBW_DEBUG 0x0
85 #define SMBW_DEBUG_FILE "/tmp/smbw.log"
90 static int debugFd = 2;
94 #define ENOTSUP EOPNOTSUPP
98 * None of the methods of having the initialization function called
99 * automatically upon shared library startup are effective in all situations.
100 * We provide the "-init" parameter to the linker which is effective most of
101 * the time, but fails for applications that provide their own shared
102 * libraries with _init() functions (e.g. ps). We can't use "-z initfirst"
103 * because the environment isn't yet set up at that point, so we can't find
104 * our shared memory identifier (see shared.c). We therefore must resort to
105 * this tried-and-true method of keeping an "initialized" flag. We check it
106 * prior to calling the initialize() function to save a function call (a slow
110 # define check_init(buf) \
112 int saved_errno = errno; \
113 if (! initialized) initialize(); \
114 (* smbw_libc.write)(debugFd, "["buf"]", sizeof(buf)+1); \
115 errno = saved_errno; \
118 # define check_init(buf) \
120 if (! initialized) smbw_initialize(); \
124 static void initialize(void);
126 static int initialized = 0;
128 SMBW_libc_pointers smbw_libc;
131 * A public entry point used by the "-init" option to the linker.
133 void smbw_initialize(void)
138 static void initialize(void)
153 # define GETSYM(symname, symstring) \
154 if ((smbw_libc.symname = dlsym(RTLD_NEXT, symstring)) == NULL) { \
155 if (smbw_libc.write != NULL && \
156 (error = dlerror()) != NULL) { \
157 (* smbw_libc.write)(1, error, strlen(error)); \
158 (* smbw_libc.write)(1, "\n", 1); \
162 # define GETSYM(symname, symstring) \
163 smbw_libc.symname = dlsym(RTLD_NEXT, symstring);
167 * Get pointers to each of the symbols we'll need, from the C library
169 * Some of these symbols may not be found in the C library. That's
170 * fine. We declare all of them here, and if the C library supports
171 * them, they may be called so we have the wrappers for them. If the
172 * C library doesn't support them, then the wrapper function will
173 * never be called, and the null pointer will never be dereferenced.
175 GETSYM(write, "write"); /* first, to allow debugging */
176 GETSYM(open, "open");
177 GETSYM(_open, "_open");
178 GETSYM(__open, "__open");
179 GETSYM(open64, "open64");
180 GETSYM(_open64, "_open64");
181 GETSYM(__open64, "__open64");
182 GETSYM(pread, "pread");
183 GETSYM(pread64, "pread64");
184 GETSYM(pwrite, "pwrite");
185 GETSYM(pwrite64, "pwrite64");
186 GETSYM(close, "close");
187 GETSYM(__close, "__close");
188 GETSYM(_close, "_close");
189 GETSYM(fcntl, "fcntl");
190 GETSYM(__fcntl, "__fcntl");
191 GETSYM(_fcntl, "_fcntl");
192 GETSYM(getdents, "getdents");
193 GETSYM(__getdents, "__getdents");
194 GETSYM(_getdents, "_getdents");
195 GETSYM(getdents64, "getdents64");
196 GETSYM(lseek, "lseek");
197 GETSYM(__lseek, "__lseek");
198 GETSYM(_lseek, "_lseek");
199 GETSYM(lseek64, "lseek64");
200 GETSYM(__lseek64, "__lseek64");
201 GETSYM(_lseek64, "_lseek64");
202 GETSYM(read, "read");
203 GETSYM(__read, "__read");
204 GETSYM(_read, "_read");
205 GETSYM(__write, "__write");
206 GETSYM(_write, "_write");
207 GETSYM(access, "access");
208 GETSYM(chmod, "chmod");
209 GETSYM(fchmod, "fchmod");
210 GETSYM(chown, "chown");
211 GETSYM(fchown, "fchown");
212 GETSYM(__xstat, "__xstat");
213 GETSYM(getcwd, "getcwd");
214 GETSYM(mkdir, "mkdir");
215 GETSYM(__fxstat, "__fxstat");
216 GETSYM(__lxstat, "__lxstat");
217 GETSYM(stat, "stat");
218 GETSYM(lstat, "lstat");
219 GETSYM(fstat, "fstat");
220 GETSYM(unlink, "unlink");
221 GETSYM(utime, "utime");
222 GETSYM(utimes, "utimes");
223 GETSYM(readlink, "readlink");
224 GETSYM(rename, "rename");
225 GETSYM(rmdir, "rmdir");
226 GETSYM(symlink, "symlink");
228 GETSYM(dup2, "dup2");
229 GETSYM(opendir, "opendir");
230 GETSYM(readdir, "readdir");
231 GETSYM(closedir, "closedir");
232 GETSYM(telldir, "telldir");
233 GETSYM(seekdir, "seekdir");
234 GETSYM(creat, "creat");
235 GETSYM(creat64, "creat64");
236 GETSYM(__xstat64, "__xstat64");
237 GETSYM(stat64, "stat64");
238 GETSYM(__fxstat64, "__fxstat64");
239 GETSYM(fstat64, "fstat64");
240 GETSYM(__lxstat64, "__lxstat64");
241 GETSYM(lstat64, "lstat64");
242 GETSYM(_llseek, "_llseek");
243 GETSYM(readdir64, "readdir64");
244 GETSYM(readdir_r, "readdir_r");
245 GETSYM(readdir64_r, "readdir64_r");
246 GETSYM(setxattr, "setxattr");
247 GETSYM(lsetxattr, "lsetxattr");
248 GETSYM(fsetxattr, "fsetxattr");
249 GETSYM(getxattr, "getxattr");
250 GETSYM(lgetxattr, "lgetxattr");
251 GETSYM(fgetxattr, "fgetxattr");
252 GETSYM(removexattr, "removexattr");
253 GETSYM(lremovexattr, "lremovexattr");
254 GETSYM(fremovexattr, "fremovexattr");
255 GETSYM(listxattr, "listxattr");
256 GETSYM(llistxattr, "llistxattr");
257 GETSYM(flistxattr, "flistxattr");
258 GETSYM(chdir, "chdir");
259 GETSYM(fchdir, "fchdir");
260 GETSYM(fork, "fork");
261 GETSYM(select, "select");
262 GETSYM(_select, "_select");
263 GETSYM(__select, "__select");
268 open(SMBW_DEBUG_FILE, O_WRONLY | O_CREAT | O_APPEND)) < 0)
270 # define SMBW_MESSAGE "Could not create " SMBW_DEBUG_FILE "\n"
271 (* smbw_libc.write)(1, SMBW_MESSAGE, sizeof(SMBW_MESSAGE));
285 static void stat_convert(struct SMBW_stat *src, struct stat *dest)
287 memset(dest, '\0', sizeof(*dest));
288 dest->st_size = src->s_size;
289 dest->st_mode = src->s_mode;
290 dest->st_ino = src->s_ino;
291 dest->st_dev = src->s_dev;
292 dest->st_rdev = src->s_rdev;
293 dest->st_nlink = src->s_nlink;
294 dest->st_uid = src->s_uid;
295 dest->st_gid = src->s_gid;
296 dest->st_atime = src->s_atime;
297 dest->st_mtime = src->s_mtime;
298 dest->st_ctime = src->s_ctime;
299 dest->st_blksize = src->s_blksize;
300 dest->st_blocks = src->s_blocks;
303 static void stat64_convert(struct SMBW_stat *src, struct stat64 *dest)
305 memset(dest, '\0', sizeof(*dest));
306 dest->st_size = src->s_size;
307 dest->st_mode = src->s_mode;
308 dest->st_ino = src->s_ino;
309 dest->st_dev = src->s_dev;
310 dest->st_rdev = src->s_rdev;
311 dest->st_nlink = src->s_nlink;
312 dest->st_uid = src->s_uid;
313 dest->st_gid = src->s_gid;
314 dest->st_atime = src->s_atime;
315 dest->st_mtime = src->s_mtime;
316 dest->st_ctime = src->s_ctime;
317 dest->st_blksize = src->s_blksize;
318 dest->st_blocks = src->s_blocks;
321 static void dirent_convert(struct SMBW_dirent *src, struct dirent *dest)
325 memset(dest, '\0', sizeof(*dest));
326 dest->d_ino = src->d_ino;
327 dest->d_off = src->d_off;
333 case SMBC_FILE_SHARE:
335 dest->d_type = DT_DIR;
339 dest->d_type = DT_REG;
342 case SMBC_PRINTER_SHARE:
343 dest->d_type = DT_CHR;
346 case SMBC_COMMS_SHARE:
347 dest->d_type = DT_SOCK;
351 dest->d_type = DT_FIFO;
355 dest->d_type = DT_LNK;
359 dest->d_reclen = src->d_reclen;
360 smbw_strlcpy(dest->d_name, src->d_name, sizeof(dest->d_name));
361 p = dest->d_name + strlen(dest->d_name) + 1;
364 sizeof(dest->d_name) - (p - dest->d_name));
367 static void dirent64_convert(struct SMBW_dirent *src, struct dirent64 *dest)
371 memset(dest, '\0', sizeof(*dest));
372 dest->d_ino = src->d_ino;
373 dest->d_off = src->d_off;
379 case SMBC_FILE_SHARE:
381 dest->d_type = DT_DIR;
385 dest->d_type = DT_REG;
388 case SMBC_PRINTER_SHARE:
389 dest->d_type = DT_CHR;
392 case SMBC_COMMS_SHARE:
393 dest->d_type = DT_SOCK;
397 dest->d_type = DT_FIFO;
401 dest->d_type = DT_LNK;
405 dest->d_reclen = src->d_reclen;
406 smbw_strlcpy(dest->d_name, src->d_name, sizeof(dest->d_name));
407 p = dest->d_name + strlen(dest->d_name) + 1;
410 sizeof(dest->d_name) - (p - dest->d_name));
413 static int openx(char *name, int flags, mode_t mode, int (* f)(char *, int, mode_t))
415 if (smbw_path(name)) {
416 return smbw_open(name, flags, mode);
419 return (* f)(name, flags, mode);
422 static int closex(int fd, int (* f)(int fd))
425 return smbw_close(fd);
431 static int fcntlx(int fd, int cmd, long arg, int (* f)(int, int, long))
434 return smbw_fcntl(fd, cmd, arg);
437 return (* f)(fd, cmd, arg);
440 static int getdentsx(int fd, struct dirent *external, unsigned int count, int (* f)(int, struct dirent *, unsigned int))
445 struct SMBW_dirent *internal;
450 * LIMITATION: If they pass a count which is not a multiple of
451 * the size of struct dirent, they will not get a partial
452 * structure; we ignore the excess count.
454 n = (count / sizeof(struct dirent));
456 internal_count = sizeof(struct SMBW_dirent) * n;
457 internal = malloc(internal_count);
458 if (internal == NULL) {
462 ret = smbw_getdents(fd, internal, internal_count);
466 ret = sizeof(struct dirent) * n;
468 for (i = 0; i < n; i++)
469 dirent_convert(&internal[i], &external[i]);
474 return (* f)(fd, external, count);
477 static off_t lseekx(int fd,
480 off_t (* f)(int, off_t, int))
485 * We have left the definitions of the smbw_ functions undefined,
486 * because types such as off_t can differ in meaning betweent his
487 * function and smbw.c et al. Functions that return other than an
488 * integer value, however, MUST have their return value defined.
490 off64_t smbw_lseek();
493 return (off_t) smbw_lseek(fd, offset, whence);
496 ret = (* f)(fd, offset, whence);
499 printf("lseekx(%d, 0x%llx) returned 0x%llx\n",
501 (unsigned long long) offset,
502 (unsigned long long) ret);
507 static off64_t lseek64x(int fd,
510 off64_t (* f)(int, off64_t, int))
515 * We have left the definitions of the smbw_ functions undefined,
516 * because types such as off_t can differ in meaning betweent his
517 * function and smbw.c et al. Functions that return other than an
518 * integer value, however, MUST have their return value defined.
520 off64_t smbw_lseek();
523 ret = smbw_lseek(fd, offset, whence);
525 ret = (* f)(fd, offset, whence);
528 printf("lseek64x(%d, 0x%llx) returned 0x%llx\n",
530 (unsigned long long) offset,
531 (unsigned long long) ret);
536 static ssize_t readx(int fd, void *buf, size_t count, ssize_t (* f)(int, void *, size_t))
539 return smbw_read(fd, buf, count);
542 return (* f)(fd, buf, count);
545 static ssize_t writex(int fd, void *buf, size_t count, ssize_t (* f)(int, void *, size_t))
548 return smbw_write(fd, buf, count);
551 return (* f)(fd, buf, count);
559 int open(__const char *name, int flags, ...)
565 mode = va_arg(ap, mode_t);
570 return openx((char *) name, flags, mode, smbw_libc.open);
573 int _open(char *name, int flags, mode_t mode)
577 return openx(name, flags, mode, smbw_libc._open);
580 int __open(char *name, int flags, mode_t mode)
584 return openx(name, flags, mode, smbw_libc.__open);
587 int open64 (__const char *name, int flags, ...)
593 mode = va_arg(ap, mode_t);
596 check_init("open64");
597 return openx((char *) name, flags, mode, smbw_libc.open64);
600 int _open64(char *name, int flags, mode_t mode)
602 check_init("_open64");
603 return openx(name, flags, mode, smbw_libc._open64);
606 int __open64(char *name, int flags, mode_t mode)
608 check_init("__open64");
609 return openx(name, flags, mode, smbw_libc.__open64);
612 ssize_t pread(int fd, void *buf, size_t size, off_t ofs)
617 return smbw_pread(fd, buf, size, ofs);
620 return (* smbw_libc.pread)(fd, buf, size, ofs);
623 ssize_t pread64(int fd, void *buf, size_t size, off64_t ofs)
625 check_init("pread64");
628 return smbw_pread(fd, buf, size, (off_t) ofs);
631 return (* smbw_libc.pread64)(fd, buf, size, ofs);
634 ssize_t pwrite(int fd, const void *buf, size_t size, off_t ofs)
636 check_init("pwrite");
639 return smbw_pwrite(fd, (void *) buf, size, ofs);
642 return (* smbw_libc.pwrite)(fd, (void *) buf, size, ofs);
645 ssize_t pwrite64(int fd, const void *buf, size_t size, off64_t ofs)
647 check_init("pwrite64");
650 return smbw_pwrite(fd, (void *) buf, size, (off_t) ofs);
653 return (* smbw_libc.pwrite64)(fd, (void *) buf, size, ofs);
656 int chdir(const char *name)
659 return smbw_chdir((char *) name);;
662 int __chdir(char *name)
664 check_init("__chdir");
665 return smbw_chdir(name);
668 int _chdir(char *name)
670 check_init("_chdir");
671 return smbw_chdir(name);
677 return closex(fd, smbw_libc.close);
682 check_init("__close");
683 return closex(fd, smbw_libc.__close);
688 check_init("_close");
689 return closex(fd, smbw_libc._close);
694 check_init("fchdir");
695 return smbw_fchdir(fd);
700 check_init("__fchdir");
706 check_init("_fchdir");
710 int fcntl (int fd, int cmd, ...)
716 arg = va_arg(ap, long);
720 return fcntlx(fd, cmd, arg, smbw_libc.fcntl);
723 int __fcntl(int fd, int cmd, ...)
729 arg = va_arg(ap, long);
732 check_init("__fcntl");
733 return fcntlx(fd, cmd, arg, smbw_libc.__fcntl);
736 int _fcntl(int fd, int cmd, ...)
742 arg = va_arg(ap, long);
745 check_init("_fcntl");
746 return fcntlx(fd, cmd, arg, smbw_libc._fcntl);
749 int getdents(int fd, struct dirent *dirp, unsigned int count)
751 check_init("getdents");
752 return getdentsx(fd, dirp, count, smbw_libc.getdents);
755 int __getdents(int fd, struct dirent *dirp, unsigned int count)
757 check_init("__getdents");
758 return getdentsx(fd, dirp, count, smbw_libc.__getdents);
761 int _getdents(int fd, struct dirent *dirp, unsigned int count)
763 check_init("_getdents");
764 return getdentsx(fd, dirp, count, smbw_libc._getdents);
767 int getdents64(int fd, struct dirent64 *external, unsigned int count)
769 check_init("getdents64");
772 struct SMBW_dirent *internal;
777 * LIMITATION: If they pass a count which is not a multiple of
778 * the size of struct dirent, they will not get a partial
779 * structure; we ignore the excess count.
781 n = (count / sizeof(struct dirent64));
783 internal = malloc(sizeof(struct SMBW_dirent) * n);
784 if (internal == NULL) {
788 ret = smbw_getdents(fd, internal, count);
792 ret = sizeof(struct dirent) * count;
794 for (i = 0; count; i++, count--)
795 dirent64_convert(&internal[i], &external[i]);
800 return (* smbw_libc.getdents64)(fd, external, count);
803 off_t lseek(int fd, off_t offset, int whence)
807 ret = lseekx(fd, offset, whence, smbw_libc.lseek);
810 printf("lseek(%d, 0x%llx) returned 0x%llx\n",
812 (unsigned long long) offset,
813 (unsigned long long) ret);
818 off_t __lseek(int fd, off_t offset, int whence)
821 check_init("__lseek");
822 ret = lseekx(fd, offset, whence, smbw_libc.__lseek);
825 printf("__lseek(%d, 0x%llx) returned 0x%llx\n",
827 (unsigned long long) offset,
828 (unsigned long long) ret);
833 off_t _lseek(int fd, off_t offset, int whence)
836 check_init("_lseek");
837 ret = lseekx(fd, offset, whence, smbw_libc._lseek);
840 printf("_lseek(%d, 0x%llx) returned 0x%llx\n",
842 (unsigned long long) offset,
843 (unsigned long long) ret);
848 off64_t lseek64(int fd, off64_t offset, int whence)
851 check_init("lseek64");
852 ret = lseek64x(fd, offset, whence, smbw_libc.lseek64);
855 printf("lseek64(%d, 0x%llx) returned 0x%llx\n",
857 (unsigned long long) offset,
858 (unsigned long long) ret);
863 off64_t __lseek64(int fd, off64_t offset, int whence)
865 check_init("__lseek64");
866 return lseek64x(fd, offset, whence, smbw_libc.__lseek64);
869 off64_t _lseek64(int fd, off64_t offset, int whence)
872 check_init("_lseek64");
873 ret = lseek64x(fd, offset, whence, smbw_libc._lseek64);
876 printf("_lseek64(%d, 0x%llx) returned 0x%llx\n",
878 (unsigned long long) offset,
879 (unsigned long long) ret);
884 ssize_t read(int fd, void *buf, size_t count)
887 return readx(fd, buf, count, smbw_libc.read);
890 ssize_t __read(int fd, void *buf, size_t count)
892 check_init("__read");
893 return readx(fd, buf, count, smbw_libc.__read);
896 ssize_t _read(int fd, void *buf, size_t count)
899 return readx(fd, buf, count, smbw_libc._read);
902 ssize_t write(int fd, const void *buf, size_t count)
905 return writex(fd, (void *) buf, count, smbw_libc.write);
908 ssize_t __write(int fd, const void *buf, size_t count)
910 check_init("__write");
911 return writex(fd, (void *) buf, count, smbw_libc.__write);
914 ssize_t _write(int fd, const void *buf, size_t count)
916 check_init("_write");
917 return writex(fd, (void *) buf, count, smbw_libc._write);
920 int access(const char *name, int mode)
922 check_init("access");
924 if (smbw_path((char *) name)) {
925 return smbw_access((char *) name, mode);
928 return (* smbw_libc.access)((char *) name, mode);
931 int chmod(const char *name, mode_t mode)
935 if (smbw_path((char *) name)) {
936 return smbw_chmod((char *) name, mode);
939 return (* smbw_libc.chmod)((char *) name, mode);
942 int fchmod(int fd, mode_t mode)
944 check_init("fchmod");
947 /* Not yet implemented in libsmbclient */
951 return (* smbw_libc.fchmod)(fd, mode);
954 int chown(const char *name, uid_t owner, gid_t group)
958 if (smbw_path((char *) name)) {
959 return smbw_chown((char *) name, owner, group);
962 return (* smbw_libc.chown)((char *) name, owner, group);
965 int fchown(int fd, uid_t owner, gid_t group)
967 check_init("fchown");
970 /* Not yet implemented in libsmbclient */
974 return (* smbw_libc.fchown)(fd, owner, group);
977 char *getcwd(char *buf, size_t size)
979 check_init("getcwd");
980 return (char *)smbw_getcwd(buf, size);
983 int mkdir(const char *name, mode_t mode)
987 if (smbw_path((char *) name)) {
988 return smbw_mkdir((char *) name, mode);
991 return (* smbw_libc.mkdir)((char *) name, mode);
994 int __fxstat(int vers, int fd, struct stat *st)
996 check_init("__fxstat");
999 struct SMBW_stat statbuf;
1000 int ret = smbw_fstat(fd, &statbuf);
1001 stat_convert(&statbuf, st);
1005 return (* smbw_libc.__fxstat)(vers, fd, st);
1008 int __xstat(int vers, const char *name, struct stat *st)
1010 check_init("__xstat");
1012 if (smbw_path((char *) name)) {
1013 struct SMBW_stat statbuf;
1014 int ret = smbw_stat((char *) name, &statbuf);
1015 stat_convert(&statbuf, st);
1019 return (* smbw_libc.__xstat)(vers, (char *) name, st);
1022 int __lxstat(int vers, const char *name, struct stat *st)
1024 check_init("__lxstat");
1026 if (smbw_path((char *) name)) {
1027 struct SMBW_stat statbuf;
1028 int ret = smbw_stat((char *) name, &statbuf);
1029 stat_convert(&statbuf, st);
1033 return (* smbw_libc.__lxstat)(vers, (char *) name, st);
1036 int stat(const char *name, struct stat *st)
1040 if (smbw_path((char *) name)) {
1041 struct SMBW_stat statbuf;
1042 int ret = smbw_stat((char *) name, &statbuf);
1043 stat_convert(&statbuf, st);
1047 return (* smbw_libc.stat)((char *) name, st);
1050 int lstat(const char *name, struct stat *st)
1052 check_init("lstat");
1054 if (smbw_path((char *) name)) {
1055 struct SMBW_stat statbuf;
1056 int ret = smbw_stat((char *) name, &statbuf);
1057 stat_convert(&statbuf, st);
1061 return (* smbw_libc.lstat)((char *) name, st);
1064 int fstat(int fd, struct stat *st)
1066 check_init("fstat");
1069 struct SMBW_stat statbuf;
1070 int ret = smbw_fstat(fd, &statbuf);
1071 stat_convert(&statbuf, st);
1075 return (* smbw_libc.fstat)(fd, st);
1078 int unlink(const char *name)
1080 check_init("unlink");
1082 if (smbw_path((char *) name)) {
1083 return smbw_unlink((char *) name);
1086 return (* smbw_libc.unlink)((char *) name);
1089 int utime(const char *name, const struct utimbuf *tvp)
1091 check_init("utime");
1093 if (smbw_path(name)) {
1094 return smbw_utime(name, (struct utimbuf *) tvp);
1097 return (* smbw_libc.utime)((char *) name, (struct utimbuf *) tvp);
1100 int utimes(const char *name, const struct timeval *tvp)
1102 check_init("utimes");
1104 if (smbw_path(name)) {
1105 return smbw_utimes(name, (struct timeval *) tvp);
1108 return (* smbw_libc.utimes)((char *) name, (struct timeval *) tvp);
1111 int readlink(const char *path, char *buf, size_t bufsize)
1113 check_init("readlink");
1115 if (smbw_path((char *) path)) {
1116 return smbw_readlink(path, (char *) buf, bufsize);
1119 return (* smbw_libc.readlink)((char *) path, buf, bufsize);
1122 int rename(const char *oldname, const char *newname)
1126 check_init("rename");
1128 p1 = smbw_path((char *) oldname);
1129 p2 = smbw_path((char *) newname);
1131 /* can't cross filesystem boundaries */
1136 return smbw_rename((char *) oldname, (char *) newname);
1139 return (* smbw_libc.rename)((char *) oldname, (char *) newname);
1142 int rmdir(const char *name)
1144 check_init("rmdir");
1146 if (smbw_path((char *) name)) {
1147 return smbw_rmdir((char *) name);
1150 return (* smbw_libc.rmdir)((char *) name);
1153 int symlink(const char *topath, const char *frompath)
1157 check_init("symlink");
1159 p1 = smbw_path((char *) topath);
1160 p2 = smbw_path((char *) frompath);
1162 /* can't handle symlinks */
1167 return (* smbw_libc.symlink)((char *) topath, (char *) frompath);
1175 return smbw_dup(fd);
1178 return (* smbw_libc.dup)(fd);
1181 int dup2(int oldfd, int newfd)
1185 if (smbw_fd(newfd)) {
1186 (* smbw_libc.close)(newfd);
1189 if (smbw_fd(oldfd)) {
1190 return smbw_dup2(oldfd, newfd);
1193 return (* smbw_libc.dup2)(oldfd, newfd);
1197 DIR *opendir(const char *name)
1199 check_init("opendir");
1201 if (smbw_path((char *) name)) {
1202 return (void *)smbw_opendir((char *) name);
1205 return (* smbw_libc.opendir)((char *) name);
1208 struct dirent *readdir(DIR *dir)
1210 check_init("readdir");
1212 if (smbw_dirp(dir)) {
1213 static struct dirent external;
1214 struct SMBW_dirent * internal = (void *)smbw_readdir(dir);
1215 if (internal != NULL) {
1216 dirent_convert(internal, &external);
1221 return (* smbw_libc.readdir)(dir);
1224 int closedir(DIR *dir)
1226 check_init("closedir");
1228 if (smbw_dirp(dir)) {
1229 return smbw_closedir(dir);
1232 return (* smbw_libc.closedir)(dir);
1235 long telldir(DIR *dir)
1237 check_init("telldir");
1239 if (smbw_dirp(dir)) {
1240 return (long) smbw_telldir(dir);
1243 return (* smbw_libc.telldir)(dir);
1246 void seekdir(DIR *dir, long offset)
1248 check_init("seekdir");
1250 if (smbw_dirp(dir)) {
1251 smbw_seekdir(dir, (long long) offset);
1255 (* smbw_libc.seekdir)(dir, offset);
1258 int creat(const char *path, mode_t mode)
1260 extern int creat_bits;
1262 check_init("creat");
1263 return openx((char *) path, creat_bits, mode, smbw_libc.open);
1266 int creat64(const char *path, mode_t mode)
1268 extern int creat_bits;
1270 check_init("creat64");
1271 return openx((char *) path, creat_bits, mode, smbw_libc.open64);
1274 int __xstat64 (int ver, const char *name, struct stat64 *st64)
1276 check_init("__xstat64");
1278 if (smbw_path((char *) name)) {
1279 struct SMBW_stat statbuf;
1280 int ret = smbw_stat((char *) name, &statbuf);
1281 stat64_convert(&statbuf, st64);
1285 return (* smbw_libc.__xstat64)(ver, (char *) name, st64);
1288 int stat64(const char *name, struct stat64 *st64)
1290 check_init("stat64");
1292 if (smbw_path((char *) name)) {
1293 struct SMBW_stat statbuf;
1294 int ret = smbw_stat((char *) name, &statbuf);
1295 stat64_convert(&statbuf, st64);
1299 return (* smbw_libc.stat64)((char *) name, st64);
1302 int __fxstat64(int ver, int fd, struct stat64 *st64)
1304 check_init("__fxstat64");
1307 struct SMBW_stat statbuf;
1308 int ret = smbw_fstat(fd, &statbuf);
1309 stat64_convert(&statbuf, st64);
1313 return (* smbw_libc.__fxstat64)(ver, fd, st64);
1316 int fstat64(int fd, struct stat64 *st64)
1318 check_init("fstat64");
1321 struct SMBW_stat statbuf;
1322 int ret = smbw_fstat(fd, &statbuf);
1323 stat64_convert(&statbuf, st64);
1327 return (* smbw_libc.fstat64)(fd, st64);
1330 int __lxstat64(int ver, const char *name, struct stat64 *st64)
1332 check_init("__lxstat64");
1334 if (smbw_path((char *) name)) {
1335 struct SMBW_stat statbuf;
1336 int ret = smbw_stat(name, &statbuf);
1337 stat64_convert(&statbuf, st64);
1341 return (* smbw_libc.__lxstat64)(ver, (char *) name, st64);
1344 int lstat64(const char *name, struct stat64 *st64)
1346 check_init("lstat64");
1348 if (smbw_path((char *) name)) {
1349 struct SMBW_stat statbuf;
1350 int ret = smbw_stat((char *) name, &statbuf);
1351 stat64_convert(&statbuf, st64);
1355 return (* smbw_libc.lstat64)((char *) name, st64);
1358 int _llseek(unsigned int fd, unsigned long offset_high, unsigned long offset_low, loff_t *result, unsigned int whence)
1360 check_init("_llseek");
1363 *result = lseek(fd, offset_low, whence);
1364 return (*result < 0 ? -1 : 0);
1367 return (* smbw_libc._llseek)(fd, offset_high, offset_low, result, whence);
1370 struct dirent64 *readdir64(DIR *dir)
1372 check_init("readdir64");
1374 if (smbw_dirp(dir)) {
1375 static struct dirent64 external;
1376 struct SMBW_dirent * internal = (void *)smbw_readdir(dir);
1377 if (internal != NULL) {
1378 dirent64_convert(internal, &external);
1384 return (* smbw_libc.readdir64)(dir);
1387 int readdir_r(DIR *dir, struct dirent *external, struct dirent **result)
1389 check_init("readdir_r");
1391 if (smbw_dirp(dir)) {
1392 struct SMBW_dirent internal;
1393 int ret = smbw_readdir_r(dir, &internal, NULL);
1395 dirent_convert(&internal, external);
1401 return (* smbw_libc.readdir_r)(dir, external, result);
1404 int readdir64_r(DIR *dir, struct dirent64 *external, struct dirent64 **result)
1406 check_init("readdir64_r");
1408 if (smbw_dirp(dir)) {
1409 struct SMBW_dirent internal;
1410 int ret = smbw_readdir_r(dir, &internal, NULL);
1412 dirent64_convert(&internal, external);
1418 return (* smbw_libc.readdir64_r)(dir, external, result);
1427 int setxattr(const char *fname,
1433 if (smbw_path(fname)) {
1434 return smbw_setxattr(fname, name, value, size, flags);
1437 return (* smbw_libc.setxattr)(fname, name, value, size, flags);
1440 int lsetxattr(const char *fname,
1446 if (smbw_path(fname)) {
1447 return smbw_lsetxattr(fname, name, value, size, flags);
1450 return (* smbw_libc.lsetxattr)(fname, name, value, size, flags);
1453 int fsetxattr(int fd,
1460 return smbw_fsetxattr(fd, name, value, size, flags);
1463 return (* smbw_libc.fsetxattr)(fd, name, value, size, flags);
1466 int getxattr(const char *fname,
1471 if (smbw_path(fname)) {
1472 return smbw_getxattr(fname, name, value, size);
1475 return (* smbw_libc.getxattr)(fname, name, value, size);
1478 int lgetxattr(const char *fname,
1483 if (smbw_path(fname)) {
1484 return smbw_lgetxattr(fname, name, value, size);
1487 return (* smbw_libc.lgetxattr)(fname, name, value, size);
1490 int fgetxattr(int fd,
1496 return smbw_fgetxattr(fd, name, value, size);
1499 return (* smbw_libc.fgetxattr)(fd, name, value, size);
1502 int removexattr(const char *fname,
1505 if (smbw_path(fname)) {
1506 return smbw_removexattr(fname, name);
1509 return (* smbw_libc.removexattr)(fname, name);
1512 int lremovexattr(const char *fname,
1515 if (smbw_path(fname)) {
1516 return smbw_lremovexattr(fname, name);
1519 return (* smbw_libc.lremovexattr)(fname, name);
1522 int fremovexattr(int fd,
1526 return smbw_fremovexattr(fd, name);
1529 return (* smbw_libc.fremovexattr)(fd, name);
1532 int listxattr(const char *fname,
1536 if (smbw_path(fname)) {
1537 return smbw_listxattr(fname, list, size);
1540 return (* smbw_libc.listxattr)(fname, list, size);
1543 int llistxattr(const char *fname,
1547 if (smbw_path(fname)) {
1548 return smbw_llistxattr(fname, list, size);
1551 return (* smbw_libc.llistxattr)(fname, list, size);
1554 int flistxattr(int fd,
1559 return smbw_flistxattr(fd, list, size);
1562 return (* smbw_libc.flistxattr)(fd, list, size);
1567 * We're ending up with a different implementation of malloc() with smbwrapper
1568 * than without it. The one with it does not support returning a non-NULL
1569 * pointer from a call to malloc(0), and many apps depend on getting a valid
1570 * pointer when requesting zero length (e.g. df, emacs).
1572 * Unfortunately, initializing the smbw_libc[] array via the dynamic link
1573 * library (libdl) requires malloc so we can't just do the same type of
1574 * mapping to the C library as we do with everything else. We need to
1575 * implement a different way of allocating memory that ensures that the C
1576 * library version of malloc() gets used. This is the only place where we
1577 * kludge the code to use an undocumented interface to the C library.
1579 * If anyone can come up with a way to dynamically link to the C library
1580 * rather than using this undocumented interface, I'd sure love to hear about
1581 * it. Better yet, if you can figure out where the alternate malloc()
1582 * functions are coming from and arrange for them not to be called, that would
1583 * be even better. We should try to avoid wrapping functions that don't
1584 * really require it.
1587 void *malloc(size_t size)
1589 void *__libc_malloc(size_t size);
1590 return __libc_malloc(size);
1593 void *calloc(size_t nmemb, size_t size)
1595 void *__libc_calloc(size_t nmemb, size_t size);
1596 return __libc_calloc(nmemb, size);
1599 void *realloc(void *ptr, size_t size)
1601 void *__libc_realloc(void *ptr, size_t size);
1602 return __libc_realloc(ptr, size);
1605 void free(void *ptr)
1607 static int in_progress = 0;
1608 void __libc_free(void *ptr);
1610 if (in_progress) return;
1619 static struct sigaction user_action[_NSIG];
1622 smbw_sigaction_handler(int signum,
1626 /* Our entire purpose for trapping signals is to call this! */
1627 sys_select_signal();
1629 /* Call the user's handler */
1630 if (user_action[signum].sa_handler != SIG_IGN &&
1631 user_action[signum].sa_handler != SIG_DFL &&
1632 user_action[signum].sa_handler != SIG_ERR) {
1633 (* user_action[signum].sa_sigaction)(signum, info, context);
1639 * Every Samba signal handler must call sys_select_signal() to avoid a race
1640 * condition, so we'll take whatever signal handler is currently assigned,
1641 * call call sys_select_signal() in addition to their call.
1648 struct timeval *timeout,
1649 int (* select_fn)(int n,
1653 struct timeval *timeout))
1659 struct sigaction new_action;
1661 saved_errno = errno;
1662 for (i=1; i<_NSIG; i++) {
1663 sigemptyset(&sigset);
1664 new_action.sa_mask = sigset;
1665 new_action.sa_flags = SA_SIGINFO;
1666 new_action.sa_sigaction = smbw_sigaction_handler;
1668 if (sigaction(i, &new_action, &user_action[i]) < 0) {
1669 if (errno != EINVAL) {
1674 errno = saved_errno;
1676 ret = (* select_fn)(n, readfds, writefds, exceptfds, timeout);
1677 saved_errno = errno;
1679 for (i=0; i<_NSIG; i++) {
1680 (void) sigaction(i, &user_action[i], NULL);
1683 errno = saved_errno;
1692 struct timeval *timeout)
1694 check_init("select");
1696 return do_select(n, readfds, writefds, exceptfds,
1697 timeout, smbw_libc.select);
1705 struct timeval *timeout)
1707 check_init("_select");
1709 return do_select(n, readfds, writefds, exceptfds,
1710 timeout, smbw_libc._select);
1718 struct timeval *timeout)
1720 check_init("__select");
1722 return do_select(n, readfds, writefds, exceptfds,
1723 timeout, smbw_libc.__select);