2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Jeremy Allison 1998-2005
6 Copyright (C) Timur Bakeyev 2005
7 Copyright (C) Bjoern Jacke 2006-2007
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #ifdef HAVE_SYS_PRCTL_H
26 #include <sys/prctl.h>
30 The idea is that this file will eventually have wrappers around all
31 important system calls in samba. The aims are:
33 - to enable easier porting by putting OS dependent stuff in here
35 - to allow for hooks into other "pseudo-filesystems"
37 - to allow easier integration of things like the japanese extensions
39 - to support the philosophy of Samba to expose the features of
40 the OS within the SMB model. In general whatever file/printer/variable
41 expansions/etc make sense to the OS should be acceptable to Samba.
46 /*******************************************************************
47 A wrapper for memalign
48 ********************************************************************/
50 void *sys_memalign( size_t align, size_t size )
52 #if defined(HAVE_POSIX_MEMALIGN)
54 int ret = posix_memalign( &p, align, size );
59 #elif defined(HAVE_MEMALIGN)
60 return memalign( align, size );
62 /* On *BSD systems memaligns doesn't exist, but memory will
63 * be aligned on allocations of > pagesize. */
64 #if defined(SYSCONF_SC_PAGESIZE)
65 size_t pagesize = (size_t)sysconf(_SC_PAGESIZE);
66 #elif defined(HAVE_GETPAGESIZE)
67 size_t pagesize = (size_t)getpagesize();
69 size_t pagesize = (size_t)-1;
71 if (pagesize == (size_t)-1) {
72 DEBUG(0,("memalign functionalaity not available on this platform!\n"));
75 if (size < pagesize) {
78 return SMB_MALLOC(size);
82 /*******************************************************************
83 A wrapper for usleep in case we don't have one.
84 ********************************************************************/
86 int sys_usleep(long usecs)
93 * We need this braindamage as the glibc usleep
94 * is not SPEC1170 complient... grumble... JRA.
97 if(usecs < 0 || usecs > 1000000) {
105 #else /* HAVE_USLEEP */
107 * Fake it with select...
110 tval.tv_usec = usecs/1000;
111 select(0,NULL,NULL,NULL,&tval);
113 #endif /* HAVE_USLEEP */
116 /*******************************************************************
117 A read wrapper that will deal with EINTR.
118 ********************************************************************/
120 ssize_t sys_read(int fd, void *buf, size_t count)
125 ret = read(fd, buf, count);
126 } while (ret == -1 && errno == EINTR);
130 /*******************************************************************
131 A write wrapper that will deal with EINTR.
132 ********************************************************************/
134 ssize_t sys_write(int fd, const void *buf, size_t count)
139 ret = write(fd, buf, count);
140 } while (ret == -1 && errno == EINTR);
144 /*******************************************************************
145 A pread wrapper that will deal with EINTR and 64-bit file offsets.
146 ********************************************************************/
148 #if defined(HAVE_PREAD) || defined(HAVE_PREAD64)
149 ssize_t sys_pread(int fd, void *buf, size_t count, SMB_OFF_T off)
154 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_PREAD64)
155 ret = pread64(fd, buf, count, off);
157 ret = pread(fd, buf, count, off);
159 } while (ret == -1 && errno == EINTR);
164 /*******************************************************************
165 A write wrapper that will deal with EINTR and 64-bit file offsets.
166 ********************************************************************/
168 #if defined(HAVE_PWRITE) || defined(HAVE_PWRITE64)
169 ssize_t sys_pwrite(int fd, const void *buf, size_t count, SMB_OFF_T off)
174 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_PWRITE64)
175 ret = pwrite64(fd, buf, count, off);
177 ret = pwrite(fd, buf, count, off);
179 } while (ret == -1 && errno == EINTR);
184 /*******************************************************************
185 A send wrapper that will deal with EINTR.
186 ********************************************************************/
188 ssize_t sys_send(int s, const void *msg, size_t len, int flags)
193 ret = send(s, msg, len, flags);
194 } while (ret == -1 && errno == EINTR);
198 /*******************************************************************
199 A sendto wrapper that will deal with EINTR.
200 ********************************************************************/
202 ssize_t sys_sendto(int s, const void *msg, size_t len, int flags, const struct sockaddr *to, socklen_t tolen)
207 ret = sendto(s, msg, len, flags, to, tolen);
208 } while (ret == -1 && errno == EINTR);
212 /*******************************************************************
213 A recv wrapper that will deal with EINTR.
214 ********************************************************************/
216 ssize_t sys_recv(int fd, void *buf, size_t count, int flags)
221 ret = recv(fd, buf, count, flags);
222 } while (ret == -1 && errno == EINTR);
226 /*******************************************************************
227 A recvfrom wrapper that will deal with EINTR.
228 ********************************************************************/
230 ssize_t sys_recvfrom(int s, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen)
235 ret = recvfrom(s, buf, len, flags, from, fromlen);
236 } while (ret == -1 && errno == EINTR);
240 /*******************************************************************
241 A fcntl wrapper that will deal with EINTR.
242 ********************************************************************/
244 int sys_fcntl_ptr(int fd, int cmd, void *arg)
249 ret = fcntl(fd, cmd, arg);
250 } while (ret == -1 && errno == EINTR);
254 /*******************************************************************
255 A fcntl wrapper that will deal with EINTR.
256 ********************************************************************/
258 int sys_fcntl_long(int fd, int cmd, long arg)
263 ret = fcntl(fd, cmd, arg);
264 } while (ret == -1 && errno == EINTR);
268 /*******************************************************************
269 A stat() wrapper that will deal with 64 bit filesizes.
270 ********************************************************************/
272 int sys_stat(const char *fname,SMB_STRUCT_STAT *sbuf)
275 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_STAT64)
276 ret = stat64(fname, sbuf);
278 ret = stat(fname, sbuf);
280 /* we always want directories to appear zero size */
281 if (ret == 0 && S_ISDIR(sbuf->st_mode)) sbuf->st_size = 0;
285 /*******************************************************************
286 An fstat() wrapper that will deal with 64 bit filesizes.
287 ********************************************************************/
289 int sys_fstat(int fd,SMB_STRUCT_STAT *sbuf)
292 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_FSTAT64)
293 ret = fstat64(fd, sbuf);
295 ret = fstat(fd, sbuf);
297 /* we always want directories to appear zero size */
298 if (ret == 0 && S_ISDIR(sbuf->st_mode)) sbuf->st_size = 0;
302 /*******************************************************************
303 An lstat() wrapper that will deal with 64 bit filesizes.
304 ********************************************************************/
306 int sys_lstat(const char *fname,SMB_STRUCT_STAT *sbuf)
309 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_LSTAT64)
310 ret = lstat64(fname, sbuf);
312 ret = lstat(fname, sbuf);
314 /* we always want directories to appear zero size */
315 if (ret == 0 && S_ISDIR(sbuf->st_mode)) sbuf->st_size = 0;
319 /*******************************************************************
320 An ftruncate() wrapper that will deal with 64 bit filesizes.
321 ********************************************************************/
323 int sys_ftruncate(int fd, SMB_OFF_T offset)
325 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_FTRUNCATE64)
326 return ftruncate64(fd, offset);
328 return ftruncate(fd, offset);
332 /*******************************************************************
333 An lseek() wrapper that will deal with 64 bit filesizes.
334 ********************************************************************/
336 SMB_OFF_T sys_lseek(int fd, SMB_OFF_T offset, int whence)
338 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_LSEEK64)
339 return lseek64(fd, offset, whence);
341 return lseek(fd, offset, whence);
345 /*******************************************************************
346 An fseek() wrapper that will deal with 64 bit filesizes.
347 ********************************************************************/
349 int sys_fseek(FILE *fp, SMB_OFF_T offset, int whence)
351 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(LARGE_SMB_OFF_T) && defined(HAVE_FSEEK64)
352 return fseek64(fp, offset, whence);
353 #elif defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(LARGE_SMB_OFF_T) && defined(HAVE_FSEEKO64)
354 return fseeko64(fp, offset, whence);
356 return fseek(fp, offset, whence);
360 /*******************************************************************
361 An ftell() wrapper that will deal with 64 bit filesizes.
362 ********************************************************************/
364 SMB_OFF_T sys_ftell(FILE *fp)
366 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(LARGE_SMB_OFF_T) && defined(HAVE_FTELL64)
367 return (SMB_OFF_T)ftell64(fp);
368 #elif defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(LARGE_SMB_OFF_T) && defined(HAVE_FTELLO64)
369 return (SMB_OFF_T)ftello64(fp);
371 return (SMB_OFF_T)ftell(fp);
375 /*******************************************************************
376 A creat() wrapper that will deal with 64 bit filesizes.
377 ********************************************************************/
379 int sys_creat(const char *path, mode_t mode)
381 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_CREAT64)
382 return creat64(path, mode);
385 * If creat64 isn't defined then ensure we call a potential open64.
388 return sys_open(path, O_WRONLY | O_CREAT | O_TRUNC, mode);
392 /*******************************************************************
393 An open() wrapper that will deal with 64 bit filesizes.
394 ********************************************************************/
396 int sys_open(const char *path, int oflag, mode_t mode)
398 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OPEN64)
399 return open64(path, oflag, mode);
401 return open(path, oflag, mode);
405 /*******************************************************************
406 An fopen() wrapper that will deal with 64 bit filesizes.
407 ********************************************************************/
409 FILE *sys_fopen(const char *path, const char *type)
411 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_FOPEN64)
412 return fopen64(path, type);
414 return fopen(path, type);
419 /*******************************************************************
420 A flock() wrapper that will perform the kernel flock.
421 ********************************************************************/
423 void kernel_flock(int fd, uint32 share_mode)
425 #if HAVE_KERNEL_SHARE_MODES
427 if (share_mode == FILE_SHARE_WRITE) {
428 kernel_mode = LOCK_MAND|LOCK_WRITE;
429 } else if (share_mode == FILE_SHARE_READ) {
430 kernel_mode = LOCK_MAND|LOCK_READ;
431 } else if (share_mode == FILE_SHARE_NONE) {
432 kernel_mode = LOCK_MAND;
435 flock(fd, kernel_mode);
443 /*******************************************************************
444 An opendir wrapper that will deal with 64 bit filesizes.
445 ********************************************************************/
447 SMB_STRUCT_DIR *sys_opendir(const char *name)
449 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OPENDIR64)
450 return opendir64(name);
452 return opendir(name);
456 /*******************************************************************
457 A readdir wrapper that will deal with 64 bit filesizes.
458 ********************************************************************/
460 SMB_STRUCT_DIRENT *sys_readdir(SMB_STRUCT_DIR *dirp)
462 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_READDIR64)
463 return readdir64(dirp);
465 return readdir(dirp);
469 /*******************************************************************
470 A seekdir wrapper that will deal with 64 bit filesizes.
471 ********************************************************************/
473 void sys_seekdir(SMB_STRUCT_DIR *dirp, long offset)
475 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_SEEKDIR64)
476 seekdir64(dirp, offset);
478 seekdir(dirp, offset);
482 /*******************************************************************
483 A telldir wrapper that will deal with 64 bit filesizes.
484 ********************************************************************/
486 long sys_telldir(SMB_STRUCT_DIR *dirp)
488 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_TELLDIR64)
489 return (long)telldir64(dirp);
491 return (long)telldir(dirp);
495 /*******************************************************************
496 A rewinddir wrapper that will deal with 64 bit filesizes.
497 ********************************************************************/
499 void sys_rewinddir(SMB_STRUCT_DIR *dirp)
501 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_REWINDDIR64)
508 /*******************************************************************
509 A close wrapper that will deal with 64 bit filesizes.
510 ********************************************************************/
512 int sys_closedir(SMB_STRUCT_DIR *dirp)
514 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_CLOSEDIR64)
515 return closedir64(dirp);
517 return closedir(dirp);
521 /*******************************************************************
522 An mknod() wrapper that will deal with 64 bit filesizes.
523 ********************************************************************/
525 int sys_mknod(const char *path, mode_t mode, SMB_DEV_T dev)
527 #if defined(HAVE_MKNOD) || defined(HAVE_MKNOD64)
528 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_MKNOD64) && defined(HAVE_DEV64_T)
529 return mknod64(path, mode, dev);
531 return mknod(path, mode, dev);
534 /* No mknod system call. */
540 /*******************************************************************
541 Wrapper for realpath.
542 ********************************************************************/
544 char *sys_realpath(const char *path, char *resolved_path)
546 #if defined(HAVE_REALPATH)
547 return realpath(path, resolved_path);
549 /* As realpath is not a system call we can't return ENOSYS. */
555 /*******************************************************************
556 The wait() calls vary between systems
557 ********************************************************************/
559 int sys_waitpid(pid_t pid,int *status,int options)
562 return waitpid(pid,status,options);
563 #else /* HAVE_WAITPID */
564 return wait4(pid, status, options, NULL);
565 #endif /* HAVE_WAITPID */
568 /*******************************************************************
569 System wrapper for getwd
570 ********************************************************************/
572 char *sys_getwd(char *s)
576 wd = (char *)getcwd(s, PATH_MAX);
578 wd = (char *)getwd(s);
583 /*******************************************************************
584 system wrapper for symlink
585 ********************************************************************/
587 int sys_symlink(const char *oldpath, const char *newpath)
593 return symlink(oldpath, newpath);
597 /*******************************************************************
598 system wrapper for readlink
599 ********************************************************************/
601 int sys_readlink(const char *path, char *buf, size_t bufsiz)
603 #ifndef HAVE_READLINK
607 return readlink(path, buf, bufsiz);
611 /*******************************************************************
613 ********************************************************************/
615 int sys_lchown(const char *fname,uid_t uid,gid_t gid)
620 DEBUG(1,("WARNING: no lchown!\n"));
626 return(lchown(fname,uid,gid));
630 /*******************************************************************
631 os/2 also doesn't have chroot
632 ********************************************************************/
633 int sys_chroot(const char *dname)
638 DEBUG(1,("WARNING: no chroot!\n"));
644 return(chroot(dname));
648 #if defined(HAVE_POSIX_CAPABILITIES)
650 /**************************************************************************
651 Try and abstract process capabilities (for systems that have them).
652 ****************************************************************************/
654 /* Set the POSIX capabilities needed for the given purpose into the effective
655 * capability set of the current process. Make sure they are always removed
656 * from the inheritable set, because there is no circumstance in which our
657 * children should inherit our elevated privileges.
659 static bool set_process_capability(enum smbd_capability capability,
662 cap_value_t cap_vals[2] = {0};
663 int num_cap_vals = 0;
667 #if defined(HAVE_PRCTL) && defined(PR_GET_KEEPCAPS) && defined(PR_SET_KEEPCAPS)
668 /* On Linux, make sure that any capabilities we grab are sticky
669 * across UID changes. We expect that this would allow us to keep both
670 * the effective and permitted capability sets, but as of circa 2.6.16,
671 * only the permitted set is kept. It is a bug (which we work around)
672 * that the effective set is lost, but we still require the effective
675 if (!prctl(PR_GET_KEEPCAPS)) {
676 prctl(PR_SET_KEEPCAPS, 1);
680 cap = cap_get_proc();
682 DEBUG(0,("set_process_capability: cap_get_proc failed: %s\n",
687 switch (capability) {
688 case KERNEL_OPLOCK_CAPABILITY:
689 #ifdef CAP_NETWORK_MGT
690 /* IRIX has CAP_NETWORK_MGT for oplocks. */
691 cap_vals[num_cap_vals++] = CAP_NETWORK_MGT;
694 case DMAPI_ACCESS_CAPABILITY:
695 #ifdef CAP_DEVICE_MGT
696 /* IRIX has CAP_DEVICE_MGT for DMAPI access. */
697 cap_vals[num_cap_vals++] = CAP_DEVICE_MGT;
699 /* Linux has CAP_MKNOD for DMAPI access. */
700 cap_vals[num_cap_vals++] = CAP_MKNOD;
703 case LEASE_CAPABILITY:
705 cap_vals[num_cap_vals++] = CAP_LEASE;
710 SMB_ASSERT(num_cap_vals <= ARRAY_SIZE(cap_vals));
712 if (num_cap_vals == 0) {
717 cap_set_flag(cap, CAP_EFFECTIVE, num_cap_vals, cap_vals,
718 enable ? CAP_SET : CAP_CLEAR);
720 /* We never want to pass capabilities down to our children, so make
721 * sure they are not inherited.
723 cap_set_flag(cap, CAP_INHERITABLE, num_cap_vals, cap_vals, CAP_CLEAR);
725 if (cap_set_proc(cap) == -1) {
726 DEBUG(0, ("set_process_capability: cap_set_proc failed: %s\n",
736 #endif /* HAVE_POSIX_CAPABILITIES */
738 /****************************************************************************
739 Gain the oplock capability from the kernel if possible.
740 ****************************************************************************/
742 void set_effective_capability(enum smbd_capability capability)
744 #if defined(HAVE_POSIX_CAPABILITIES)
745 set_process_capability(capability, True);
746 #endif /* HAVE_POSIX_CAPABILITIES */
749 void drop_effective_capability(enum smbd_capability capability)
751 #if defined(HAVE_POSIX_CAPABILITIES)
752 set_process_capability(capability, False);
753 #endif /* HAVE_POSIX_CAPABILITIES */
756 /**************************************************************************
757 Wrapper for random().
758 ****************************************************************************/
760 long sys_random(void)
762 #if defined(HAVE_RANDOM)
763 return (long)random();
764 #elif defined(HAVE_RAND)
767 DEBUG(0,("Error - no random function available !\n"));
772 /**************************************************************************
773 Wrapper for srandom().
774 ****************************************************************************/
776 void sys_srandom(unsigned int seed)
778 #if defined(HAVE_SRANDOM)
780 #elif defined(HAVE_SRAND)
783 DEBUG(0,("Error - no srandom function available !\n"));
788 /**************************************************************************
789 Returns equivalent to NGROUPS_MAX - using sysconf if needed.
790 ****************************************************************************/
794 #if defined(SYSCONF_SC_NGROUPS_MAX)
795 int ret = sysconf(_SC_NGROUPS_MAX);
796 return (ret == -1) ? NGROUPS_MAX : ret;
802 /**************************************************************************
803 Wrap setgroups and getgroups for systems that declare getgroups() as
804 returning an array of gid_t, but actuall return an array of int.
805 ****************************************************************************/
807 #if defined(HAVE_BROKEN_GETGROUPS)
808 static int sys_broken_getgroups(int setlen, gid_t *gidset)
815 return getgroups(setlen, &gid);
819 * Broken case. We need to allocate a
820 * GID_T array of size setlen.
829 setlen = groups_max();
831 if((group_list = SMB_MALLOC_ARRAY(GID_T, setlen)) == NULL) {
832 DEBUG(0,("sys_getgroups: Malloc fail.\n"));
836 if((ngroups = getgroups(setlen, group_list)) < 0) {
837 int saved_errno = errno;
838 SAFE_FREE(group_list);
843 for(i = 0; i < ngroups; i++)
844 gidset[i] = (gid_t)group_list[i];
846 SAFE_FREE(group_list);
850 static int sys_broken_setgroups(int setlen, gid_t *gidset)
858 if (setlen < 0 || setlen > groups_max()) {
864 * Broken case. We need to allocate a
865 * GID_T array of size setlen.
868 if((group_list = SMB_MALLOC_ARRAY(GID_T, setlen)) == NULL) {
869 DEBUG(0,("sys_setgroups: Malloc fail.\n"));
873 for(i = 0; i < setlen; i++)
874 group_list[i] = (GID_T) gidset[i];
876 if(setgroups(setlen, group_list) != 0) {
877 int saved_errno = errno;
878 SAFE_FREE(group_list);
883 SAFE_FREE(group_list);
887 #endif /* HAVE_BROKEN_GETGROUPS */
889 /* This is a list of systems that require the first GID passed to setgroups(2)
890 * to be the effective GID. If your system is one of these, add it here.
892 #if defined (FREEBSD) || defined (DARWINOS)
893 #define USE_BSD_SETGROUPS
896 #if defined(USE_BSD_SETGROUPS)
897 /* Depending on the particular BSD implementation, the first GID that is
898 * passed to setgroups(2) will either be ignored or will set the credential's
899 * effective GID. In either case, the right thing to do is to guarantee that
900 * gidset[0] is the effective GID.
902 static int sys_bsd_setgroups(gid_t primary_gid, int setlen, const gid_t *gidset)
904 gid_t *new_gidset = NULL;
908 /* setgroups(2) will fail with EINVAL if we pass too many groups. */
911 /* No group list, just make sure we are setting the efective GID. */
913 return setgroups(1, &primary_gid);
916 /* If the primary gid is not the first array element, grow the array
917 * and insert it at the front.
919 if (gidset[0] != primary_gid) {
920 new_gidset = SMB_MALLOC_ARRAY(gid_t, setlen + 1);
921 if (new_gidset == NULL) {
925 memcpy(new_gidset + 1, gidset, (setlen * sizeof(gid_t)));
926 new_gidset[0] = primary_gid;
931 DEBUG(3, ("forced to truncate group list from %d to %d\n",
936 #if defined(HAVE_BROKEN_GETGROUPS)
937 ret = sys_broken_setgroups(setlen, new_gidset ? new_gidset : gidset);
939 ret = setgroups(setlen, new_gidset ? new_gidset : gidset);
944 SAFE_FREE(new_gidset);
951 #endif /* USE_BSD_SETGROUPS */
953 /**************************************************************************
954 Wrapper for getgroups. Deals with broken (int) case.
955 ****************************************************************************/
957 int sys_getgroups(int setlen, gid_t *gidset)
959 #if defined(HAVE_BROKEN_GETGROUPS)
960 return sys_broken_getgroups(setlen, gidset);
962 return getgroups(setlen, gidset);
966 /**************************************************************************
967 Wrapper for setgroups. Deals with broken (int) case and BSD case.
968 ****************************************************************************/
970 int sys_setgroups(gid_t UNUSED(primary_gid), int setlen, gid_t *gidset)
972 #if !defined(HAVE_SETGROUPS)
975 #endif /* HAVE_SETGROUPS */
977 #if defined(USE_BSD_SETGROUPS)
978 return sys_bsd_setgroups(primary_gid, setlen, gidset);
979 #elif defined(HAVE_BROKEN_GETGROUPS)
980 return sys_broken_setgroups(setlen, gidset);
982 return setgroups(setlen, gidset);
986 /**************************************************************************
987 Wrappers for setpwent(), getpwent() and endpwent()
988 ****************************************************************************/
990 void sys_setpwent(void)
995 struct passwd *sys_getpwent(void)
1000 void sys_endpwent(void)
1005 /**************************************************************************
1006 Wrappers for getpwnam(), getpwuid(), getgrnam(), getgrgid()
1007 ****************************************************************************/
1010 struct passwd *sys_getpwnam(const char *name)
1012 return getpwnam(name);
1015 struct passwd *sys_getpwuid(uid_t uid)
1017 return getpwuid(uid);
1020 struct group *sys_getgrnam(const char *name)
1022 return getgrnam(name);
1025 struct group *sys_getgrgid(gid_t gid)
1027 return getgrgid(gid);
1030 /**************************************************************************
1031 Extract a command into an arg list.
1032 ****************************************************************************/
1034 static char **extract_args(TALLOC_CTX *mem_ctx, const char *command)
1043 if (!(trunc_cmd = talloc_strdup(mem_ctx, command))) {
1044 DEBUG(0, ("talloc failed\n"));
1048 if(!(ptr = strtok_r(trunc_cmd, " \t", &saveptr))) {
1049 TALLOC_FREE(trunc_cmd);
1058 for( argcl = 1; ptr; ptr = strtok_r(NULL, " \t", &saveptr))
1061 TALLOC_FREE(trunc_cmd);
1063 if (!(argl = TALLOC_ARRAY(mem_ctx, char *, argcl + 1))) {
1068 * Now do the extraction.
1071 if (!(trunc_cmd = talloc_strdup(mem_ctx, command))) {
1075 ptr = strtok_r(trunc_cmd, " \t", &saveptr);
1078 if (!(argl[i++] = talloc_strdup(argl, ptr))) {
1082 while((ptr = strtok_r(NULL, " \t", &saveptr)) != NULL) {
1084 if (!(argl[i++] = talloc_strdup(argl, ptr))) {
1093 DEBUG(0, ("talloc failed\n"));
1094 TALLOC_FREE(trunc_cmd);
1100 /**************************************************************************
1101 Wrapper for fork. Ensures that mypid is reset. Used so we can write
1102 a sys_getpid() that only does a system call *once*.
1103 ****************************************************************************/
1105 static pid_t mypid = (pid_t)-1;
1107 pid_t sys_fork(void)
1109 pid_t forkret = fork();
1111 if (forkret == (pid_t)0) /* Child - reset mypid so sys_getpid does a system call. */
1117 /**************************************************************************
1118 Wrapper for getpid. Ensures we only do a system call *once*.
1119 ****************************************************************************/
1121 pid_t sys_getpid(void)
1123 if (mypid == (pid_t)-1)
1129 /**************************************************************************
1130 Wrapper for popen. Safer as it doesn't search a path.
1131 Modified from the glibc sources.
1132 modified by tridge to return a file descriptor. We must kick our FILE* habit
1133 ****************************************************************************/
1135 typedef struct _popen_list
1139 struct _popen_list *next;
1142 static popen_list *popen_chain;
1144 int sys_popen(const char *command)
1146 int parent_end, child_end;
1148 popen_list *entry = NULL;
1151 if (pipe(pipe_fds) < 0)
1154 parent_end = pipe_fds[0];
1155 child_end = pipe_fds[1];
1162 if((entry = SMB_MALLOC_P(popen_list)) == NULL)
1165 ZERO_STRUCTP(entry);
1168 * Extract the command and args into a NULL terminated array.
1171 if(!(argl = extract_args(NULL, command)))
1174 entry->child_pid = sys_fork();
1176 if (entry->child_pid == -1) {
1180 if (entry->child_pid == 0) {
1186 int child_std_end = STDOUT_FILENO;
1190 if (child_end != child_std_end) {
1191 dup2 (child_end, child_std_end);
1196 * POSIX.2: "popen() shall ensure that any streams from previous
1197 * popen() calls that remain open in the parent process are closed
1198 * in the new child process."
1201 for (p = popen_chain; p; p = p->next)
1204 execv(argl[0], argl);
1215 /* Link into popen_chain. */
1216 entry->next = popen_chain;
1217 popen_chain = entry;
1218 entry->fd = parent_end;
1231 /**************************************************************************
1232 Wrapper for pclose. Modified from the glibc sources.
1233 ****************************************************************************/
1235 int sys_pclose(int fd)
1238 popen_list **ptr = &popen_chain;
1239 popen_list *entry = NULL;
1243 /* Unlink from popen_chain. */
1244 for ( ; *ptr != NULL; ptr = &(*ptr)->next) {
1245 if ((*ptr)->fd == fd) {
1247 *ptr = (*ptr)->next;
1253 if (status < 0 || close(entry->fd) < 0)
1257 * As Samba is catching and eating child process
1258 * exits we don't really care about the child exit
1259 * code, a -1 with errno = ECHILD will do fine for us.
1263 wait_pid = sys_waitpid (entry->child_pid, &wstatus, 0);
1264 } while (wait_pid == -1 && errno == EINTR);
1273 /**************************************************************************
1274 Wrappers for dlopen, dlsym, dlclose.
1275 ****************************************************************************/
1277 void *sys_dlopen(const char *name, int flags)
1279 #if defined(HAVE_DLOPEN)
1280 return dlopen(name, flags);
1286 void *sys_dlsym(void *handle, const char *symbol)
1288 #if defined(HAVE_DLSYM)
1289 return dlsym(handle, symbol);
1295 int sys_dlclose (void *handle)
1297 #if defined(HAVE_DLCLOSE)
1298 return dlclose(handle);
1304 const char *sys_dlerror(void)
1306 #if defined(HAVE_DLERROR)
1313 int sys_dup2(int oldfd, int newfd)
1315 #if defined(HAVE_DUP2)
1316 return dup2(oldfd, newfd);
1323 /**************************************************************************
1324 Wrapper for Admin Logs.
1325 ****************************************************************************/
1327 void sys_adminlog(int priority, const char *format_str, ...)
1331 char *msgbuf = NULL;
1333 va_start( ap, format_str );
1334 ret = vasprintf( &msgbuf, format_str, ap );
1340 #if defined(HAVE_SYSLOG)
1341 syslog( priority, "%s", msgbuf );
1343 DEBUG(0,("%s", msgbuf ));
1348 /******** Solaris EA helper function prototypes ********/
1349 #ifdef HAVE_ATTROPEN
1350 #define SOLARIS_ATTRMODE S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP
1351 static int solaris_write_xattr(int attrfd, const char *value, size_t size);
1352 static ssize_t solaris_read_xattr(int attrfd, void *value, size_t size);
1353 static ssize_t solaris_list_xattr(int attrdirfd, char *list, size_t size);
1354 static int solaris_unlinkat(int attrdirfd, const char *name);
1355 static int solaris_attropen(const char *path, const char *attrpath, int oflag, mode_t mode);
1356 static int solaris_openat(int fildes, const char *path, int oflag, mode_t mode);
1359 /**************************************************************************
1360 Wrappers for extented attribute calls. Based on the Linux package with
1361 support for IRIX and (Net|Free)BSD also. Expand as other systems have them.
1362 ****************************************************************************/
1364 ssize_t sys_getxattr (const char *path, const char *name, void *value, size_t size)
1366 #if defined(HAVE_GETXATTR)
1367 #ifndef XATTR_ADD_OPT
1368 return getxattr(path, name, value, size);
1371 return getxattr(path, name, value, size, 0, options);
1373 #elif defined(HAVE_GETEA)
1374 return getea(path, name, value, size);
1375 #elif defined(HAVE_EXTATTR_GET_FILE)
1378 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
1379 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
1380 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
1382 * The BSD implementation has a nasty habit of silently truncating
1383 * the returned value to the size of the buffer, so we have to check
1384 * that the buffer is large enough to fit the returned value.
1386 if((retval=extattr_get_file(path, attrnamespace, attrname, NULL, 0)) >= 0) {
1391 if((retval=extattr_get_file(path, attrnamespace, attrname, value, size)) >= 0)
1395 DEBUG(10,("sys_getxattr: extattr_get_file() failed with: %s\n", strerror(errno)));
1397 #elif defined(HAVE_ATTR_GET)
1398 int retval, flags = 0;
1399 int valuelength = (int)size;
1400 char *attrname = strchr(name,'.') + 1;
1402 if (strncmp(name, "system", 6) == 0) flags |= ATTR_ROOT;
1404 retval = attr_get(path, attrname, (char *)value, &valuelength, flags);
1406 return retval ? retval : valuelength;
1407 #elif defined(HAVE_ATTROPEN)
1409 int attrfd = solaris_attropen(path, name, O_RDONLY, 0);
1411 ret = solaris_read_xattr(attrfd, value, size);
1421 ssize_t sys_lgetxattr (const char *path, const char *name, void *value, size_t size)
1423 #if defined(HAVE_LGETXATTR)
1424 return lgetxattr(path, name, value, size);
1425 #elif defined(HAVE_GETXATTR) && defined(XATTR_ADD_OPT)
1426 int options = XATTR_NOFOLLOW;
1427 return getxattr(path, name, value, size, 0, options);
1428 #elif defined(HAVE_LGETEA)
1429 return lgetea(path, name, value, size);
1430 #elif defined(HAVE_EXTATTR_GET_LINK)
1433 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
1434 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
1435 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
1437 if((retval=extattr_get_link(path, attrnamespace, attrname, NULL, 0)) >= 0) {
1442 if((retval=extattr_get_link(path, attrnamespace, attrname, value, size)) >= 0)
1446 DEBUG(10,("sys_lgetxattr: extattr_get_link() failed with: %s\n", strerror(errno)));
1448 #elif defined(HAVE_ATTR_GET)
1449 int retval, flags = ATTR_DONTFOLLOW;
1450 int valuelength = (int)size;
1451 char *attrname = strchr(name,'.') + 1;
1453 if (strncmp(name, "system", 6) == 0) flags |= ATTR_ROOT;
1455 retval = attr_get(path, attrname, (char *)value, &valuelength, flags);
1457 return retval ? retval : valuelength;
1458 #elif defined(HAVE_ATTROPEN)
1460 int attrfd = solaris_attropen(path, name, O_RDONLY|AT_SYMLINK_NOFOLLOW, 0);
1462 ret = solaris_read_xattr(attrfd, value, size);
1472 ssize_t sys_fgetxattr (int filedes, const char *name, void *value, size_t size)
1474 #if defined(HAVE_FGETXATTR)
1475 #ifndef XATTR_ADD_OPT
1476 return fgetxattr(filedes, name, value, size);
1479 return fgetxattr(filedes, name, value, size, 0, options);
1481 #elif defined(HAVE_FGETEA)
1482 return fgetea(filedes, name, value, size);
1483 #elif defined(HAVE_EXTATTR_GET_FD)
1486 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
1487 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
1488 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
1490 if((retval=extattr_get_fd(filedes, attrnamespace, attrname, NULL, 0)) >= 0) {
1495 if((retval=extattr_get_fd(filedes, attrnamespace, attrname, value, size)) >= 0)
1499 DEBUG(10,("sys_fgetxattr: extattr_get_fd() failed with: %s\n", strerror(errno)));
1501 #elif defined(HAVE_ATTR_GETF)
1502 int retval, flags = 0;
1503 int valuelength = (int)size;
1504 char *attrname = strchr(name,'.') + 1;
1506 if (strncmp(name, "system", 6) == 0) flags |= ATTR_ROOT;
1508 retval = attr_getf(filedes, attrname, (char *)value, &valuelength, flags);
1510 return retval ? retval : valuelength;
1511 #elif defined(HAVE_ATTROPEN)
1513 int attrfd = solaris_openat(filedes, name, O_RDONLY|O_XATTR, 0);
1515 ret = solaris_read_xattr(attrfd, value, size);
1525 #if defined(HAVE_EXTATTR_LIST_FILE)
1527 #define EXTATTR_PREFIX(s) (s), (sizeof((s))-1)
1535 { EXTATTR_NAMESPACE_SYSTEM, EXTATTR_PREFIX("system.") },
1536 { EXTATTR_NAMESPACE_USER, EXTATTR_PREFIX("user.") },
1544 static ssize_t bsd_attr_list (int type, extattr_arg arg, char *list, size_t size)
1546 ssize_t list_size, total_size = 0;
1549 /* Iterate through extattr(2) namespaces */
1550 for(t = 0; t < (sizeof(extattr)/sizeof(extattr[0])); t++) {
1552 #if defined(HAVE_EXTATTR_LIST_FILE)
1554 list_size = extattr_list_file(arg.path, extattr[t].space, list, size);
1557 #if defined(HAVE_EXTATTR_LIST_LINK)
1559 list_size = extattr_list_link(arg.path, extattr[t].space, list, size);
1562 #if defined(HAVE_EXTATTR_LIST_FD)
1564 list_size = extattr_list_fd(arg.filedes, extattr[t].space, list, size);
1571 /* Some error happend. Errno should be set by the previous call */
1577 /* XXX: Call with an empty buffer may be used to calculate
1578 necessary buffer size. Unfortunately, we can't say, how
1579 many attributes were returned, so here is the potential
1580 problem with the emulation.
1583 /* Take the worse case of one char attribute names -
1584 two bytes per name plus one more for sanity.
1586 total_size += list_size + (list_size/2 + 1)*extattr[t].len;
1589 /* Count necessary offset to fit namespace prefixes */
1591 for(i = 0; i < list_size; i += list[i] + 1)
1592 len += extattr[t].len;
1594 total_size += list_size + len;
1595 /* Buffer is too small to fit the results */
1596 if(total_size > size) {
1600 /* Shift results back, so we can prepend prefixes */
1601 buf = memmove(list + len, list, list_size);
1603 for(i = 0; i < list_size; i += len + 1) {
1605 strncpy(list, extattr[t].name, extattr[t].len + 1);
1606 list += extattr[t].len;
1607 strncpy(list, buf + i + 1, len);
1618 #if defined(HAVE_ATTR_LIST) && defined(HAVE_SYS_ATTRIBUTES_H)
1619 static char attr_buffer[ATTR_MAX_VALUELEN];
1621 static ssize_t irix_attr_list(const char *path, int filedes, char *list, size_t size, int flags)
1623 int retval = 0, index;
1624 attrlist_cursor_t *cursor = 0;
1626 attrlist_t * al = (attrlist_t *)attr_buffer;
1628 size_t ent_size, left = size;
1633 retval = attr_listf(filedes, attr_buffer, ATTR_MAX_VALUELEN, flags, cursor);
1635 retval = attr_list(path, attr_buffer, ATTR_MAX_VALUELEN, flags, cursor);
1637 for (index = 0; index < al->al_count; index++) {
1638 ae = ATTR_ENTRY(attr_buffer, index);
1639 ent_size = strlen(ae->a_name) + sizeof("user.");
1640 if (left >= ent_size) {
1641 strncpy(bp, "user.", sizeof("user."));
1642 strncat(bp, ae->a_name, ent_size - sizeof("user."));
1650 total_size += ent_size;
1652 if (al->al_more == 0) break;
1659 retval = attr_listf(filedes, attr_buffer, ATTR_MAX_VALUELEN, flags, cursor);
1661 retval = attr_list(path, attr_buffer, ATTR_MAX_VALUELEN, flags, cursor);
1663 for (index = 0; index < al->al_count; index++) {
1664 ae = ATTR_ENTRY(attr_buffer, index);
1665 ent_size = strlen(ae->a_name) + sizeof("system.");
1666 if (left >= ent_size) {
1667 strncpy(bp, "system.", sizeof("system."));
1668 strncat(bp, ae->a_name, ent_size - sizeof("system."));
1676 total_size += ent_size;
1678 if (al->al_more == 0) break;
1681 return (ssize_t)(retval ? retval : total_size);
1686 ssize_t sys_listxattr (const char *path, char *list, size_t size)
1688 #if defined(HAVE_LISTXATTR)
1689 #ifndef XATTR_ADD_OPT
1690 return listxattr(path, list, size);
1693 return listxattr(path, list, size, options);
1695 #elif defined(HAVE_LISTEA)
1696 return listea(path, list, size);
1697 #elif defined(HAVE_EXTATTR_LIST_FILE)
1700 return bsd_attr_list(0, arg, list, size);
1701 #elif defined(HAVE_ATTR_LIST) && defined(HAVE_SYS_ATTRIBUTES_H)
1702 return irix_attr_list(path, 0, list, size, 0);
1703 #elif defined(HAVE_ATTROPEN)
1705 int attrdirfd = solaris_attropen(path, ".", O_RDONLY, 0);
1706 if (attrdirfd >= 0) {
1707 ret = solaris_list_xattr(attrdirfd, list, size);
1717 ssize_t sys_llistxattr (const char *path, char *list, size_t size)
1719 #if defined(HAVE_LLISTXATTR)
1720 return llistxattr(path, list, size);
1721 #elif defined(HAVE_LISTXATTR) && defined(XATTR_ADD_OPT)
1722 int options = XATTR_NOFOLLOW;
1723 return listxattr(path, list, size, options);
1724 #elif defined(HAVE_LLISTEA)
1725 return llistea(path, list, size);
1726 #elif defined(HAVE_EXTATTR_LIST_LINK)
1729 return bsd_attr_list(1, arg, list, size);
1730 #elif defined(HAVE_ATTR_LIST) && defined(HAVE_SYS_ATTRIBUTES_H)
1731 return irix_attr_list(path, 0, list, size, ATTR_DONTFOLLOW);
1732 #elif defined(HAVE_ATTROPEN)
1734 int attrdirfd = solaris_attropen(path, ".", O_RDONLY|AT_SYMLINK_NOFOLLOW, 0);
1735 if (attrdirfd >= 0) {
1736 ret = solaris_list_xattr(attrdirfd, list, size);
1746 ssize_t sys_flistxattr (int filedes, char *list, size_t size)
1748 #if defined(HAVE_FLISTXATTR)
1749 #ifndef XATTR_ADD_OPT
1750 return flistxattr(filedes, list, size);
1753 return flistxattr(filedes, list, size, options);
1755 #elif defined(HAVE_FLISTEA)
1756 return flistea(filedes, list, size);
1757 #elif defined(HAVE_EXTATTR_LIST_FD)
1759 arg.filedes = filedes;
1760 return bsd_attr_list(2, arg, list, size);
1761 #elif defined(HAVE_ATTR_LISTF)
1762 return irix_attr_list(NULL, filedes, list, size, 0);
1763 #elif defined(HAVE_ATTROPEN)
1765 int attrdirfd = solaris_openat(filedes, ".", O_RDONLY|O_XATTR, 0);
1766 if (attrdirfd >= 0) {
1767 ret = solaris_list_xattr(attrdirfd, list, size);
1777 int sys_removexattr (const char *path, const char *name)
1779 #if defined(HAVE_REMOVEXATTR)
1780 #ifndef XATTR_ADD_OPT
1781 return removexattr(path, name);
1784 return removexattr(path, name, options);
1786 #elif defined(HAVE_REMOVEEA)
1787 return removeea(path, name);
1788 #elif defined(HAVE_EXTATTR_DELETE_FILE)
1790 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
1791 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
1792 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
1794 return extattr_delete_file(path, attrnamespace, attrname);
1795 #elif defined(HAVE_ATTR_REMOVE)
1797 char *attrname = strchr(name,'.') + 1;
1799 if (strncmp(name, "system", 6) == 0) flags |= ATTR_ROOT;
1801 return attr_remove(path, attrname, flags);
1802 #elif defined(HAVE_ATTROPEN)
1804 int attrdirfd = solaris_attropen(path, ".", O_RDONLY, 0);
1805 if (attrdirfd >= 0) {
1806 ret = solaris_unlinkat(attrdirfd, name);
1816 int sys_lremovexattr (const char *path, const char *name)
1818 #if defined(HAVE_LREMOVEXATTR)
1819 return lremovexattr(path, name);
1820 #elif defined(HAVE_REMOVEXATTR) && defined(XATTR_ADD_OPT)
1821 int options = XATTR_NOFOLLOW;
1822 return removexattr(path, name, options);
1823 #elif defined(HAVE_LREMOVEEA)
1824 return lremoveea(path, name);
1825 #elif defined(HAVE_EXTATTR_DELETE_LINK)
1827 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
1828 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
1829 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
1831 return extattr_delete_link(path, attrnamespace, attrname);
1832 #elif defined(HAVE_ATTR_REMOVE)
1833 int flags = ATTR_DONTFOLLOW;
1834 char *attrname = strchr(name,'.') + 1;
1836 if (strncmp(name, "system", 6) == 0) flags |= ATTR_ROOT;
1838 return attr_remove(path, attrname, flags);
1839 #elif defined(HAVE_ATTROPEN)
1841 int attrdirfd = solaris_attropen(path, ".", O_RDONLY|AT_SYMLINK_NOFOLLOW, 0);
1842 if (attrdirfd >= 0) {
1843 ret = solaris_unlinkat(attrdirfd, name);
1853 int sys_fremovexattr (int filedes, const char *name)
1855 #if defined(HAVE_FREMOVEXATTR)
1856 #ifndef XATTR_ADD_OPT
1857 return fremovexattr(filedes, name);
1860 return fremovexattr(filedes, name, options);
1862 #elif defined(HAVE_FREMOVEEA)
1863 return fremoveea(filedes, name);
1864 #elif defined(HAVE_EXTATTR_DELETE_FD)
1866 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
1867 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
1868 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
1870 return extattr_delete_fd(filedes, attrnamespace, attrname);
1871 #elif defined(HAVE_ATTR_REMOVEF)
1873 char *attrname = strchr(name,'.') + 1;
1875 if (strncmp(name, "system", 6) == 0) flags |= ATTR_ROOT;
1877 return attr_removef(filedes, attrname, flags);
1878 #elif defined(HAVE_ATTROPEN)
1880 int attrdirfd = solaris_openat(filedes, ".", O_RDONLY|O_XATTR, 0);
1881 if (attrdirfd >= 0) {
1882 ret = solaris_unlinkat(attrdirfd, name);
1892 int sys_setxattr (const char *path, const char *name, const void *value, size_t size, int flags)
1894 #if defined(HAVE_SETXATTR)
1895 #ifndef XATTR_ADD_OPT
1896 return setxattr(path, name, value, size, flags);
1899 return setxattr(path, name, value, size, 0, options);
1901 #elif defined(HAVE_SETEA)
1902 return setea(path, name, value, size, flags);
1903 #elif defined(HAVE_EXTATTR_SET_FILE)
1906 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
1907 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
1908 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
1910 /* Check attribute existence */
1911 retval = extattr_get_file(path, attrnamespace, attrname, NULL, 0);
1913 /* REPLACE attribute, that doesn't exist */
1914 if (flags & XATTR_REPLACE && errno == ENOATTR) {
1918 /* Ignore other errors */
1921 /* CREATE attribute, that already exists */
1922 if (flags & XATTR_CREATE) {
1928 retval = extattr_set_file(path, attrnamespace, attrname, value, size);
1929 return (retval < 0) ? -1 : 0;
1930 #elif defined(HAVE_ATTR_SET)
1932 char *attrname = strchr(name,'.') + 1;
1934 if (strncmp(name, "system", 6) == 0) myflags |= ATTR_ROOT;
1935 if (flags & XATTR_CREATE) myflags |= ATTR_CREATE;
1936 if (flags & XATTR_REPLACE) myflags |= ATTR_REPLACE;
1938 return attr_set(path, attrname, (const char *)value, size, myflags);
1939 #elif defined(HAVE_ATTROPEN)
1941 int myflags = O_RDWR;
1943 if (flags & XATTR_CREATE) myflags |= O_EXCL;
1944 if (!(flags & XATTR_REPLACE)) myflags |= O_CREAT;
1945 attrfd = solaris_attropen(path, name, myflags, (mode_t) SOLARIS_ATTRMODE);
1947 ret = solaris_write_xattr(attrfd, value, size);
1957 int sys_lsetxattr (const char *path, const char *name, const void *value, size_t size, int flags)
1959 #if defined(HAVE_LSETXATTR)
1960 return lsetxattr(path, name, value, size, flags);
1961 #elif defined(HAVE_SETXATTR) && defined(XATTR_ADD_OPT)
1962 int options = XATTR_NOFOLLOW;
1963 return setxattr(path, name, value, size, 0, options);
1964 #elif defined(LSETEA)
1965 return lsetea(path, name, value, size, flags);
1966 #elif defined(HAVE_EXTATTR_SET_LINK)
1969 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
1970 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
1971 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
1973 /* Check attribute existence */
1974 retval = extattr_get_link(path, attrnamespace, attrname, NULL, 0);
1976 /* REPLACE attribute, that doesn't exist */
1977 if (flags & XATTR_REPLACE && errno == ENOATTR) {
1981 /* Ignore other errors */
1984 /* CREATE attribute, that already exists */
1985 if (flags & XATTR_CREATE) {
1992 retval = extattr_set_link(path, attrnamespace, attrname, value, size);
1993 return (retval < 0) ? -1 : 0;
1994 #elif defined(HAVE_ATTR_SET)
1995 int myflags = ATTR_DONTFOLLOW;
1996 char *attrname = strchr(name,'.') + 1;
1998 if (strncmp(name, "system", 6) == 0) myflags |= ATTR_ROOT;
1999 if (flags & XATTR_CREATE) myflags |= ATTR_CREATE;
2000 if (flags & XATTR_REPLACE) myflags |= ATTR_REPLACE;
2002 return attr_set(path, attrname, (const char *)value, size, myflags);
2003 #elif defined(HAVE_ATTROPEN)
2005 int myflags = O_RDWR | AT_SYMLINK_NOFOLLOW;
2007 if (flags & XATTR_CREATE) myflags |= O_EXCL;
2008 if (!(flags & XATTR_REPLACE)) myflags |= O_CREAT;
2009 attrfd = solaris_attropen(path, name, myflags, (mode_t) SOLARIS_ATTRMODE);
2011 ret = solaris_write_xattr(attrfd, value, size);
2021 int sys_fsetxattr (int filedes, const char *name, const void *value, size_t size, int flags)
2023 #if defined(HAVE_FSETXATTR)
2024 #ifndef XATTR_ADD_OPT
2025 return fsetxattr(filedes, name, value, size, flags);
2028 return fsetxattr(filedes, name, value, size, 0, options);
2030 #elif defined(HAVE_FSETEA)
2031 return fsetea(filedes, name, value, size, flags);
2032 #elif defined(HAVE_EXTATTR_SET_FD)
2035 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
2036 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
2037 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
2039 /* Check attribute existence */
2040 retval = extattr_get_fd(filedes, attrnamespace, attrname, NULL, 0);
2042 /* REPLACE attribute, that doesn't exist */
2043 if (flags & XATTR_REPLACE && errno == ENOATTR) {
2047 /* Ignore other errors */
2050 /* CREATE attribute, that already exists */
2051 if (flags & XATTR_CREATE) {
2057 retval = extattr_set_fd(filedes, attrnamespace, attrname, value, size);
2058 return (retval < 0) ? -1 : 0;
2059 #elif defined(HAVE_ATTR_SETF)
2061 char *attrname = strchr(name,'.') + 1;
2063 if (strncmp(name, "system", 6) == 0) myflags |= ATTR_ROOT;
2064 if (flags & XATTR_CREATE) myflags |= ATTR_CREATE;
2065 if (flags & XATTR_REPLACE) myflags |= ATTR_REPLACE;
2067 return attr_setf(filedes, attrname, (const char *)value, size, myflags);
2068 #elif defined(HAVE_ATTROPEN)
2070 int myflags = O_RDWR | O_XATTR;
2072 if (flags & XATTR_CREATE) myflags |= O_EXCL;
2073 if (!(flags & XATTR_REPLACE)) myflags |= O_CREAT;
2074 attrfd = solaris_openat(filedes, name, myflags, (mode_t) SOLARIS_ATTRMODE);
2076 ret = solaris_write_xattr(attrfd, value, size);
2086 /**************************************************************************
2087 helper functions for Solaris' EA support
2088 ****************************************************************************/
2089 #ifdef HAVE_ATTROPEN
2090 static ssize_t solaris_read_xattr(int attrfd, void *value, size_t size)
2094 if (fstat(attrfd, &sbuf) == -1) {
2099 /* This is to return the current size of the named extended attribute */
2101 return sbuf.st_size;
2104 /* check size and read xattr */
2105 if (sbuf.st_size > size) {
2110 return read(attrfd, value, sbuf.st_size);
2113 static ssize_t solaris_list_xattr(int attrdirfd, char *list, size_t size)
2119 int newfd = dup(attrdirfd);
2120 /* CAUTION: The originating file descriptor should not be
2121 used again following the call to fdopendir().
2122 For that reason we dup() the file descriptor
2123 here to make things more clear. */
2124 dirp = fdopendir(newfd);
2126 while ((de = readdir(dirp))) {
2127 size_t listlen = strlen(de->d_name);
2128 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
2129 /* we don't want "." and ".." here: */
2130 DEBUG(10,("skipped EA %s\n",de->d_name));
2135 /* return the current size of the list of extended attribute names*/
2138 /* check size and copy entrieѕ + nul into list. */
2139 if ((len + listlen + 1) > size) {
2144 safe_strcpy(list + len, de->d_name, listlen);
2152 if (closedir(dirp) == -1) {
2153 DEBUG(0,("closedir dirp failed: %s\n",strerror(errno)));
2159 static int solaris_unlinkat(int attrdirfd, const char *name)
2161 if (unlinkat(attrdirfd, name, 0) == -1) {
2162 if (errno == ENOENT) {
2170 static int solaris_attropen(const char *path, const char *attrpath, int oflag, mode_t mode)
2172 int filedes = attropen(path, attrpath, oflag, mode);
2173 if (filedes == -1) {
2174 DEBUG(10,("attropen FAILED: path: %s, name: %s, errno: %s\n",path,attrpath,strerror(errno)));
2175 if (errno == EINVAL) {
2184 static int solaris_openat(int fildes, const char *path, int oflag, mode_t mode)
2186 int filedes = openat(fildes, path, oflag, mode);
2187 if (filedes == -1) {
2188 DEBUG(10,("openat FAILED: fd: %s, path: %s, errno: %s\n",filedes,path,strerror(errno)));
2189 if (errno == EINVAL) {
2198 static int solaris_write_xattr(int attrfd, const char *value, size_t size)
2200 if ((ftruncate(attrfd, 0) == 0) && (write(attrfd, value, size) == size)) {
2203 DEBUG(10,("solaris_write_xattr FAILED!\n"));
2207 #endif /*HAVE_ATTROPEN*/
2210 /****************************************************************************
2211 Return the major devicenumber for UNIX extensions.
2212 ****************************************************************************/
2214 uint32 unix_dev_major(SMB_DEV_T dev)
2216 #if defined(HAVE_DEVICE_MAJOR_FN)
2217 return (uint32)major(dev);
2219 return (uint32)(dev >> 8);
2223 /****************************************************************************
2224 Return the minor devicenumber for UNIX extensions.
2225 ****************************************************************************/
2227 uint32 unix_dev_minor(SMB_DEV_T dev)
2229 #if defined(HAVE_DEVICE_MINOR_FN)
2230 return (uint32)minor(dev);
2232 return (uint32)(dev & 0xff);
2236 #if defined(WITH_AIO)
2238 /*******************************************************************
2239 An aio_read wrapper that will deal with 64-bit sizes.
2240 ********************************************************************/
2242 int sys_aio_read(SMB_STRUCT_AIOCB *aiocb)
2244 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_READ64)
2245 return aio_read64(aiocb);
2246 #elif defined(HAVE_AIO_READ)
2247 return aio_read(aiocb);
2254 /*******************************************************************
2255 An aio_write wrapper that will deal with 64-bit sizes.
2256 ********************************************************************/
2258 int sys_aio_write(SMB_STRUCT_AIOCB *aiocb)
2260 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_WRITE64)
2261 return aio_write64(aiocb);
2262 #elif defined(HAVE_AIO_WRITE)
2263 return aio_write(aiocb);
2270 /*******************************************************************
2271 An aio_return wrapper that will deal with 64-bit sizes.
2272 ********************************************************************/
2274 ssize_t sys_aio_return(SMB_STRUCT_AIOCB *aiocb)
2276 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_RETURN64)
2277 return aio_return64(aiocb);
2278 #elif defined(HAVE_AIO_RETURN)
2279 return aio_return(aiocb);
2286 /*******************************************************************
2287 An aio_cancel wrapper that will deal with 64-bit sizes.
2288 ********************************************************************/
2290 int sys_aio_cancel(int fd, SMB_STRUCT_AIOCB *aiocb)
2292 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_CANCEL64)
2293 return aio_cancel64(fd, aiocb);
2294 #elif defined(HAVE_AIO_CANCEL)
2295 return aio_cancel(fd, aiocb);
2302 /*******************************************************************
2303 An aio_error wrapper that will deal with 64-bit sizes.
2304 ********************************************************************/
2306 int sys_aio_error(const SMB_STRUCT_AIOCB *aiocb)
2308 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_ERROR64)
2309 return aio_error64(aiocb);
2310 #elif defined(HAVE_AIO_ERROR)
2311 return aio_error(aiocb);
2318 /*******************************************************************
2319 An aio_fsync wrapper that will deal with 64-bit sizes.
2320 ********************************************************************/
2322 int sys_aio_fsync(int op, SMB_STRUCT_AIOCB *aiocb)
2324 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_FSYNC64)
2325 return aio_fsync64(op, aiocb);
2326 #elif defined(HAVE_AIO_FSYNC)
2327 return aio_fsync(op, aiocb);
2334 /*******************************************************************
2335 An aio_fsync wrapper that will deal with 64-bit sizes.
2336 ********************************************************************/
2338 int sys_aio_suspend(const SMB_STRUCT_AIOCB * const cblist[], int n, const struct timespec *timeout)
2340 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_SUSPEND64)
2341 return aio_suspend64(cblist, n, timeout);
2342 #elif defined(HAVE_AIO_FSYNC)
2343 return aio_suspend(cblist, n, timeout);
2349 #else /* !WITH_AIO */
2351 int sys_aio_read(SMB_STRUCT_AIOCB *aiocb)
2357 int sys_aio_write(SMB_STRUCT_AIOCB *aiocb)
2363 ssize_t sys_aio_return(SMB_STRUCT_AIOCB *aiocb)
2369 int sys_aio_cancel(int fd, SMB_STRUCT_AIOCB *aiocb)
2375 int sys_aio_error(const SMB_STRUCT_AIOCB *aiocb)
2381 int sys_aio_fsync(int op, SMB_STRUCT_AIOCB *aiocb)
2387 int sys_aio_suspend(const SMB_STRUCT_AIOCB * const cblist[], int n, const struct timespec *timeout)
2392 #endif /* WITH_AIO */
2394 int sys_getpeereid( int s, uid_t *uid)
2396 #if defined(HAVE_PEERCRED)
2398 socklen_t cred_len = sizeof(struct ucred);
2401 ret = getsockopt(s, SOL_SOCKET, SO_PEERCRED, (void *)&cred, &cred_len);
2406 if (cred_len != sizeof(struct ucred)) {
2419 int sys_getnameinfo(const struct sockaddr *psa,
2428 * For Solaris we must make sure salen is the
2429 * correct length for the incoming sa_family.
2432 if (salen == sizeof(struct sockaddr_storage)) {
2433 salen = sizeof(struct sockaddr_in);
2434 #if defined(HAVE_IPV6)
2435 if (psa->sa_family == AF_INET6) {
2436 salen = sizeof(struct sockaddr_in6);
2440 return getnameinfo(psa, salen, host, hostlen, service, servlen, flags);
2443 int sys_connect(int fd, const struct sockaddr * addr)
2445 socklen_t salen = -1;
2447 if (addr->sa_family == AF_INET) {
2448 salen = sizeof(struct sockaddr_in);
2449 } else if (addr->sa_family == AF_UNIX) {
2450 salen = sizeof(struct sockaddr_un);
2452 #if defined(HAVE_IPV6)
2453 else if (addr->sa_family == AF_INET6) {
2454 salen = sizeof(struct sockaddr_in6);
2458 return connect(fd, addr, salen);