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 > 999999) {
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 writev wrapper that will deal with EINTR.
146 ********************************************************************/
148 ssize_t sys_writev(int fd, const struct iovec *iov, int iovcnt)
153 /* Try to confuse write_data_iov a bit */
154 if ((random() % 5) == 0) {
155 return sys_write(fd, iov[0].iov_base, iov[0].iov_len);
157 if (iov[0].iov_len > 1) {
158 return sys_write(fd, iov[0].iov_base,
159 (random() % (iov[0].iov_len-1)) + 1);
164 ret = writev(fd, iov, iovcnt);
165 } while (ret == -1 && errno == EINTR);
169 /*******************************************************************
170 A pread wrapper that will deal with EINTR and 64-bit file offsets.
171 ********************************************************************/
173 #if defined(HAVE_PREAD) || defined(HAVE_PREAD64)
174 ssize_t sys_pread(int fd, void *buf, size_t count, SMB_OFF_T off)
179 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_PREAD64)
180 ret = pread64(fd, buf, count, off);
182 ret = pread(fd, buf, count, off);
184 } while (ret == -1 && errno == EINTR);
189 /*******************************************************************
190 A write wrapper that will deal with EINTR and 64-bit file offsets.
191 ********************************************************************/
193 #if defined(HAVE_PWRITE) || defined(HAVE_PWRITE64)
194 ssize_t sys_pwrite(int fd, const void *buf, size_t count, SMB_OFF_T off)
199 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_PWRITE64)
200 ret = pwrite64(fd, buf, count, off);
202 ret = pwrite(fd, buf, count, off);
204 } while (ret == -1 && errno == EINTR);
209 /*******************************************************************
210 A send wrapper that will deal with EINTR.
211 ********************************************************************/
213 ssize_t sys_send(int s, const void *msg, size_t len, int flags)
218 ret = send(s, msg, len, flags);
219 } while (ret == -1 && errno == EINTR);
223 /*******************************************************************
224 A sendto wrapper that will deal with EINTR.
225 ********************************************************************/
227 ssize_t sys_sendto(int s, const void *msg, size_t len, int flags, const struct sockaddr *to, socklen_t tolen)
232 ret = sendto(s, msg, len, flags, to, tolen);
233 } while (ret == -1 && errno == EINTR);
237 /*******************************************************************
238 A recv wrapper that will deal with EINTR.
239 ********************************************************************/
241 ssize_t sys_recv(int fd, void *buf, size_t count, int flags)
246 ret = recv(fd, buf, count, flags);
247 } while (ret == -1 && errno == EINTR);
251 /*******************************************************************
252 A recvfrom wrapper that will deal with EINTR.
253 ********************************************************************/
255 ssize_t sys_recvfrom(int s, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen)
260 ret = recvfrom(s, buf, len, flags, from, fromlen);
261 } while (ret == -1 && errno == EINTR);
265 /*******************************************************************
266 A fcntl wrapper that will deal with EINTR.
267 ********************************************************************/
269 int sys_fcntl_ptr(int fd, int cmd, void *arg)
274 ret = fcntl(fd, cmd, arg);
275 } while (ret == -1 && errno == EINTR);
279 /*******************************************************************
280 A fcntl wrapper that will deal with EINTR.
281 ********************************************************************/
283 int sys_fcntl_long(int fd, int cmd, long arg)
288 ret = fcntl(fd, cmd, arg);
289 } while (ret == -1 && errno == EINTR);
293 /****************************************************************************
294 Return the best approximation to a 'create time' under UNIX from a stat
296 ****************************************************************************/
298 static time_t calc_create_time(const struct stat *st)
302 ret = MIN(st->st_ctime, st->st_mtime);
303 ret1 = MIN(ret, st->st_atime);
305 if(ret1 != (time_t)0) {
310 * One of ctime, mtime or atime was zero (probably atime).
311 * Just return MIN(ctime, mtime).
316 /****************************************************************************
317 Return the 'create time' from a stat struct if it exists (birthtime) or else
318 use the best approximation.
319 ****************************************************************************/
321 static struct timespec get_create_timespec(const struct stat *pst)
325 if (S_ISDIR(pst->st_mode) && lp_fake_dir_create_times()) {
326 ret.tv_sec = 315493200L; /* 1/1/1980 */
331 #if defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC)
332 ret = pst->st_birthtimespec;
333 #elif defined(HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC)
334 ret.tv_sec = pst->st_birthtime;
335 ret.tv_nsec = pst->st_birthtimenspec;
336 #elif defined(HAVE_STRUCT_STAT_ST_BIRTHTIME)
337 ret.tv_sec = pst->st_birthtime;
340 ret.tv_sec = calc_create_time(pst);
344 /* Deal with systems that don't initialize birthtime correctly.
345 * Pointed out by SATOH Fumiyasu <fumiyas@osstech.jp>.
347 if (null_timespec(ret)) {
348 ret.tv_sec = calc_create_time(pst);
354 /****************************************************************************
355 Get/Set all the possible time fields from a stat struct as a timespec.
356 ****************************************************************************/
358 static struct timespec get_atimespec(const struct stat *pst)
360 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
363 /* Old system - no ns timestamp. */
364 ret.tv_sec = pst->st_atime;
368 #if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
370 #elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
372 ret.tv_sec = pst->st_atime;
373 ret.tv_nsec = pst->st_atimensec;
375 #elif defined(HAVE_STRUCT_STAT_ST_MTIME_N)
377 ret.tv_sec = pst->st_atime;
378 ret.tv_nsec = pst->st_atime_n;
380 #elif defined(HAVE_STRUCT_STAT_ST_UMTIME)
382 ret.tv_sec = pst->st_atime;
383 ret.tv_nsec = pst->st_uatime * 1000;
385 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC)
386 return pst->st_atimespec;
388 #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
393 static struct timespec get_mtimespec(const struct stat *pst)
395 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
398 /* Old system - no ns timestamp. */
399 ret.tv_sec = pst->st_mtime;
403 #if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
405 #elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
407 ret.tv_sec = pst->st_mtime;
408 ret.tv_nsec = pst->st_mtimensec;
410 #elif defined(HAVE_STRUCT_STAT_ST_MTIME_N)
412 ret.tv_sec = pst->st_mtime;
413 ret.tv_nsec = pst->st_mtime_n;
415 #elif defined(HAVE_STRUCT_STAT_ST_UMTIME)
417 ret.tv_sec = pst->st_mtime;
418 ret.tv_nsec = pst->st_umtime * 1000;
420 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC)
421 return pst->st_mtimespec;
423 #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
428 static struct timespec get_ctimespec(const struct stat *pst)
430 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
433 /* Old system - no ns timestamp. */
434 ret.tv_sec = pst->st_ctime;
438 #if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
440 #elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
442 ret.tv_sec = pst->st_ctime;
443 ret.tv_nsec = pst->st_ctimensec;
445 #elif defined(HAVE_STRUCT_STAT_ST_MTIME_N)
447 ret.tv_sec = pst->st_ctime;
448 ret.tv_nsec = pst->st_ctime_n;
450 #elif defined(HAVE_STRUCT_STAT_ST_UMTIME)
452 ret.tv_sec = pst->st_ctime;
453 ret.tv_nsec = pst->st_uctime * 1000;
455 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC)
456 return pst->st_ctimespec;
458 #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
463 static void init_stat_ex_from_stat (struct stat_ex *dst,
464 const struct stat *src)
466 dst->st_ex_dev = src->st_dev;
467 dst->st_ex_ino = src->st_ino;
468 dst->st_ex_mode = src->st_mode;
469 dst->st_ex_nlink = src->st_nlink;
470 dst->st_ex_uid = src->st_uid;
471 dst->st_ex_gid = src->st_gid;
472 dst->st_ex_rdev = src->st_rdev;
473 dst->st_ex_size = src->st_size;
474 dst->st_ex_atime = get_atimespec(src);
475 dst->st_ex_mtime = get_mtimespec(src);
476 dst->st_ex_ctime = get_ctimespec(src);
477 dst->st_ex_btime = get_create_timespec(src);
478 dst->st_ex_blksize = src->st_blksize;
479 dst->st_ex_blocks = src->st_blocks;
481 #ifdef HAVE_STAT_ST_FLAGS
482 dst->st_ex_flags = src->st_flags;
484 dst->st_ex_flags = 0;
488 /*******************************************************************
489 A stat() wrapper that will deal with 64 bit filesizes.
490 ********************************************************************/
492 int sys_stat(const char *fname,SMB_STRUCT_STAT *sbuf)
495 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_STAT64)
496 ret = stat64(fname, sbuf);
499 ret = stat(fname, &statbuf);
502 /* we always want directories to appear zero size */
503 if (S_ISDIR(statbuf.st_mode)) {
506 init_stat_ex_from_stat(sbuf, &statbuf);
511 /*******************************************************************
512 An fstat() wrapper that will deal with 64 bit filesizes.
513 ********************************************************************/
515 int sys_fstat(int fd,SMB_STRUCT_STAT *sbuf)
518 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_FSTAT64)
519 ret = fstat64(fd, sbuf);
522 ret = fstat(fd, &statbuf);
525 /* we always want directories to appear zero size */
526 if (S_ISDIR(statbuf.st_mode)) {
529 init_stat_ex_from_stat(sbuf, &statbuf);
534 /*******************************************************************
535 An lstat() wrapper that will deal with 64 bit filesizes.
536 ********************************************************************/
538 int sys_lstat(const char *fname,SMB_STRUCT_STAT *sbuf)
541 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_LSTAT64)
542 ret = lstat64(fname, sbuf);
545 ret = lstat(fname, &statbuf);
548 /* we always want directories to appear zero size */
549 if (S_ISDIR(statbuf.st_mode)) {
552 init_stat_ex_from_stat(sbuf, &statbuf);
557 /*******************************************************************
558 An ftruncate() wrapper that will deal with 64 bit filesizes.
559 ********************************************************************/
561 int sys_ftruncate(int fd, SMB_OFF_T offset)
563 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_FTRUNCATE64)
564 return ftruncate64(fd, offset);
566 return ftruncate(fd, offset);
570 /*******************************************************************
571 An lseek() wrapper that will deal with 64 bit filesizes.
572 ********************************************************************/
574 SMB_OFF_T sys_lseek(int fd, SMB_OFF_T offset, int whence)
576 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_LSEEK64)
577 return lseek64(fd, offset, whence);
579 return lseek(fd, offset, whence);
583 /*******************************************************************
584 An fseek() wrapper that will deal with 64 bit filesizes.
585 ********************************************************************/
587 int sys_fseek(FILE *fp, SMB_OFF_T offset, int whence)
589 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(LARGE_SMB_OFF_T) && defined(HAVE_FSEEK64)
590 return fseek64(fp, offset, whence);
591 #elif defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(LARGE_SMB_OFF_T) && defined(HAVE_FSEEKO64)
592 return fseeko64(fp, offset, whence);
594 return fseek(fp, offset, whence);
598 /*******************************************************************
599 An ftell() wrapper that will deal with 64 bit filesizes.
600 ********************************************************************/
602 SMB_OFF_T sys_ftell(FILE *fp)
604 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(LARGE_SMB_OFF_T) && defined(HAVE_FTELL64)
605 return (SMB_OFF_T)ftell64(fp);
606 #elif defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(LARGE_SMB_OFF_T) && defined(HAVE_FTELLO64)
607 return (SMB_OFF_T)ftello64(fp);
609 return (SMB_OFF_T)ftell(fp);
613 /*******************************************************************
614 A creat() wrapper that will deal with 64 bit filesizes.
615 ********************************************************************/
617 int sys_creat(const char *path, mode_t mode)
619 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_CREAT64)
620 return creat64(path, mode);
623 * If creat64 isn't defined then ensure we call a potential open64.
626 return sys_open(path, O_WRONLY | O_CREAT | O_TRUNC, mode);
630 /*******************************************************************
631 An open() wrapper that will deal with 64 bit filesizes.
632 ********************************************************************/
634 int sys_open(const char *path, int oflag, mode_t mode)
636 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OPEN64)
637 return open64(path, oflag, mode);
639 return open(path, oflag, mode);
643 /*******************************************************************
644 An fopen() wrapper that will deal with 64 bit filesizes.
645 ********************************************************************/
647 FILE *sys_fopen(const char *path, const char *type)
649 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_FOPEN64)
650 return fopen64(path, type);
652 return fopen(path, type);
657 /*******************************************************************
658 A flock() wrapper that will perform the kernel flock.
659 ********************************************************************/
661 void kernel_flock(int fd, uint32 share_mode)
663 #if HAVE_KERNEL_SHARE_MODES
665 if (share_mode == FILE_SHARE_WRITE) {
666 kernel_mode = LOCK_MAND|LOCK_WRITE;
667 } else if (share_mode == FILE_SHARE_READ) {
668 kernel_mode = LOCK_MAND|LOCK_READ;
669 } else if (share_mode == FILE_SHARE_NONE) {
670 kernel_mode = LOCK_MAND;
673 flock(fd, kernel_mode);
681 /*******************************************************************
682 An opendir wrapper that will deal with 64 bit filesizes.
683 ********************************************************************/
685 SMB_STRUCT_DIR *sys_opendir(const char *name)
687 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OPENDIR64)
688 return opendir64(name);
690 return opendir(name);
694 /*******************************************************************
695 A readdir wrapper that will deal with 64 bit filesizes.
696 ********************************************************************/
698 SMB_STRUCT_DIRENT *sys_readdir(SMB_STRUCT_DIR *dirp)
700 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_READDIR64)
701 return readdir64(dirp);
703 return readdir(dirp);
707 /*******************************************************************
708 A seekdir wrapper that will deal with 64 bit filesizes.
709 ********************************************************************/
711 void sys_seekdir(SMB_STRUCT_DIR *dirp, long offset)
713 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_SEEKDIR64)
714 seekdir64(dirp, offset);
716 seekdir(dirp, offset);
720 /*******************************************************************
721 A telldir wrapper that will deal with 64 bit filesizes.
722 ********************************************************************/
724 long sys_telldir(SMB_STRUCT_DIR *dirp)
726 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_TELLDIR64)
727 return (long)telldir64(dirp);
729 return (long)telldir(dirp);
733 /*******************************************************************
734 A rewinddir wrapper that will deal with 64 bit filesizes.
735 ********************************************************************/
737 void sys_rewinddir(SMB_STRUCT_DIR *dirp)
739 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_REWINDDIR64)
746 /*******************************************************************
747 A close wrapper that will deal with 64 bit filesizes.
748 ********************************************************************/
750 int sys_closedir(SMB_STRUCT_DIR *dirp)
752 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_CLOSEDIR64)
753 return closedir64(dirp);
755 return closedir(dirp);
759 /*******************************************************************
760 An mknod() wrapper that will deal with 64 bit filesizes.
761 ********************************************************************/
763 int sys_mknod(const char *path, mode_t mode, SMB_DEV_T dev)
765 #if defined(HAVE_MKNOD) || defined(HAVE_MKNOD64)
766 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_MKNOD64) && defined(HAVE_DEV64_T)
767 return mknod64(path, mode, dev);
769 return mknod(path, mode, dev);
772 /* No mknod system call. */
778 /*******************************************************************
779 The wait() calls vary between systems
780 ********************************************************************/
782 int sys_waitpid(pid_t pid,int *status,int options)
785 return waitpid(pid,status,options);
786 #else /* HAVE_WAITPID */
787 return wait4(pid, status, options, NULL);
788 #endif /* HAVE_WAITPID */
791 /*******************************************************************
792 System wrapper for getwd
793 ********************************************************************/
795 char *sys_getwd(char *s)
799 wd = (char *)getcwd(s, PATH_MAX);
801 wd = (char *)getwd(s);
806 #if defined(HAVE_POSIX_CAPABILITIES)
808 /**************************************************************************
809 Try and abstract process capabilities (for systems that have them).
810 ****************************************************************************/
812 /* Set the POSIX capabilities needed for the given purpose into the effective
813 * capability set of the current process. Make sure they are always removed
814 * from the inheritable set, because there is no circumstance in which our
815 * children should inherit our elevated privileges.
817 static bool set_process_capability(enum smbd_capability capability,
820 cap_value_t cap_vals[2] = {0};
821 int num_cap_vals = 0;
825 #if defined(HAVE_PRCTL) && defined(PR_GET_KEEPCAPS) && defined(PR_SET_KEEPCAPS)
826 /* On Linux, make sure that any capabilities we grab are sticky
827 * across UID changes. We expect that this would allow us to keep both
828 * the effective and permitted capability sets, but as of circa 2.6.16,
829 * only the permitted set is kept. It is a bug (which we work around)
830 * that the effective set is lost, but we still require the effective
833 if (!prctl(PR_GET_KEEPCAPS)) {
834 prctl(PR_SET_KEEPCAPS, 1);
838 cap = cap_get_proc();
840 DEBUG(0,("set_process_capability: cap_get_proc failed: %s\n",
845 switch (capability) {
846 case KERNEL_OPLOCK_CAPABILITY:
847 #ifdef CAP_NETWORK_MGT
848 /* IRIX has CAP_NETWORK_MGT for oplocks. */
849 cap_vals[num_cap_vals++] = CAP_NETWORK_MGT;
852 case DMAPI_ACCESS_CAPABILITY:
853 #ifdef CAP_DEVICE_MGT
854 /* IRIX has CAP_DEVICE_MGT for DMAPI access. */
855 cap_vals[num_cap_vals++] = CAP_DEVICE_MGT;
857 /* Linux has CAP_MKNOD for DMAPI access. */
858 cap_vals[num_cap_vals++] = CAP_MKNOD;
861 case LEASE_CAPABILITY:
863 cap_vals[num_cap_vals++] = CAP_LEASE;
868 SMB_ASSERT(num_cap_vals <= ARRAY_SIZE(cap_vals));
870 if (num_cap_vals == 0) {
875 cap_set_flag(cap, CAP_EFFECTIVE, num_cap_vals, cap_vals,
876 enable ? CAP_SET : CAP_CLEAR);
878 /* We never want to pass capabilities down to our children, so make
879 * sure they are not inherited.
881 cap_set_flag(cap, CAP_INHERITABLE, num_cap_vals, cap_vals, CAP_CLEAR);
883 if (cap_set_proc(cap) == -1) {
884 DEBUG(0, ("set_process_capability: cap_set_proc failed: %s\n",
894 #endif /* HAVE_POSIX_CAPABILITIES */
896 /****************************************************************************
897 Gain the oplock capability from the kernel if possible.
898 ****************************************************************************/
900 void set_effective_capability(enum smbd_capability capability)
902 #if defined(HAVE_POSIX_CAPABILITIES)
903 set_process_capability(capability, True);
904 #endif /* HAVE_POSIX_CAPABILITIES */
907 void drop_effective_capability(enum smbd_capability capability)
909 #if defined(HAVE_POSIX_CAPABILITIES)
910 set_process_capability(capability, False);
911 #endif /* HAVE_POSIX_CAPABILITIES */
914 /**************************************************************************
915 Wrapper for random().
916 ****************************************************************************/
918 long sys_random(void)
920 #if defined(HAVE_RANDOM)
921 return (long)random();
922 #elif defined(HAVE_RAND)
925 DEBUG(0,("Error - no random function available !\n"));
930 /**************************************************************************
931 Wrapper for srandom().
932 ****************************************************************************/
934 void sys_srandom(unsigned int seed)
936 #if defined(HAVE_SRANDOM)
938 #elif defined(HAVE_SRAND)
941 DEBUG(0,("Error - no srandom function available !\n"));
946 /**************************************************************************
947 Returns equivalent to NGROUPS_MAX - using sysconf if needed.
948 ****************************************************************************/
952 #if defined(SYSCONF_SC_NGROUPS_MAX)
953 int ret = sysconf(_SC_NGROUPS_MAX);
954 return (ret == -1) ? NGROUPS_MAX : ret;
960 /**************************************************************************
961 Wrap setgroups and getgroups for systems that declare getgroups() as
962 returning an array of gid_t, but actuall return an array of int.
963 ****************************************************************************/
965 #if defined(HAVE_BROKEN_GETGROUPS)
966 static int sys_broken_getgroups(int setlen, gid_t *gidset)
973 return getgroups(setlen, &gid);
977 * Broken case. We need to allocate a
978 * GID_T array of size setlen.
987 setlen = groups_max();
989 if((group_list = SMB_MALLOC_ARRAY(GID_T, setlen)) == NULL) {
990 DEBUG(0,("sys_getgroups: Malloc fail.\n"));
994 if((ngroups = getgroups(setlen, group_list)) < 0) {
995 int saved_errno = errno;
996 SAFE_FREE(group_list);
1001 for(i = 0; i < ngroups; i++)
1002 gidset[i] = (gid_t)group_list[i];
1004 SAFE_FREE(group_list);
1008 static int sys_broken_setgroups(int setlen, gid_t *gidset)
1016 if (setlen < 0 || setlen > groups_max()) {
1022 * Broken case. We need to allocate a
1023 * GID_T array of size setlen.
1026 if((group_list = SMB_MALLOC_ARRAY(GID_T, setlen)) == NULL) {
1027 DEBUG(0,("sys_setgroups: Malloc fail.\n"));
1031 for(i = 0; i < setlen; i++)
1032 group_list[i] = (GID_T) gidset[i];
1034 if(setgroups(setlen, group_list) != 0) {
1035 int saved_errno = errno;
1036 SAFE_FREE(group_list);
1037 errno = saved_errno;
1041 SAFE_FREE(group_list);
1045 #endif /* HAVE_BROKEN_GETGROUPS */
1047 /* This is a list of systems that require the first GID passed to setgroups(2)
1048 * to be the effective GID. If your system is one of these, add it here.
1050 #if defined (FREEBSD) || defined (DARWINOS)
1051 #define USE_BSD_SETGROUPS
1054 #if defined(USE_BSD_SETGROUPS)
1055 /* Depending on the particular BSD implementation, the first GID that is
1056 * passed to setgroups(2) will either be ignored or will set the credential's
1057 * effective GID. In either case, the right thing to do is to guarantee that
1058 * gidset[0] is the effective GID.
1060 static int sys_bsd_setgroups(gid_t primary_gid, int setlen, const gid_t *gidset)
1062 gid_t *new_gidset = NULL;
1066 /* setgroups(2) will fail with EINVAL if we pass too many groups. */
1069 /* No group list, just make sure we are setting the efective GID. */
1071 return setgroups(1, &primary_gid);
1074 /* If the primary gid is not the first array element, grow the array
1075 * and insert it at the front.
1077 if (gidset[0] != primary_gid) {
1078 new_gidset = SMB_MALLOC_ARRAY(gid_t, setlen + 1);
1079 if (new_gidset == NULL) {
1083 memcpy(new_gidset + 1, gidset, (setlen * sizeof(gid_t)));
1084 new_gidset[0] = primary_gid;
1089 DEBUG(3, ("forced to truncate group list from %d to %d\n",
1094 #if defined(HAVE_BROKEN_GETGROUPS)
1095 ret = sys_broken_setgroups(setlen, new_gidset ? new_gidset : gidset);
1097 ret = setgroups(setlen, new_gidset ? new_gidset : gidset);
1102 SAFE_FREE(new_gidset);
1109 #endif /* USE_BSD_SETGROUPS */
1111 /**************************************************************************
1112 Wrapper for getgroups. Deals with broken (int) case.
1113 ****************************************************************************/
1115 int sys_getgroups(int setlen, gid_t *gidset)
1117 #if defined(HAVE_BROKEN_GETGROUPS)
1118 return sys_broken_getgroups(setlen, gidset);
1120 return getgroups(setlen, gidset);
1124 /**************************************************************************
1125 Wrapper for setgroups. Deals with broken (int) case and BSD case.
1126 ****************************************************************************/
1128 int sys_setgroups(gid_t UNUSED(primary_gid), int setlen, gid_t *gidset)
1130 #if !defined(HAVE_SETGROUPS)
1133 #endif /* HAVE_SETGROUPS */
1135 #if defined(USE_BSD_SETGROUPS)
1136 return sys_bsd_setgroups(primary_gid, setlen, gidset);
1137 #elif defined(HAVE_BROKEN_GETGROUPS)
1138 return sys_broken_setgroups(setlen, gidset);
1140 return setgroups(setlen, gidset);
1144 /**************************************************************************
1145 Wrappers for setpwent(), getpwent() and endpwent()
1146 ****************************************************************************/
1148 void sys_setpwent(void)
1153 struct passwd *sys_getpwent(void)
1158 void sys_endpwent(void)
1163 /**************************************************************************
1164 Wrappers for getpwnam(), getpwuid(), getgrnam(), getgrgid()
1165 ****************************************************************************/
1168 struct passwd *sys_getpwnam(const char *name)
1170 return getpwnam(name);
1173 struct passwd *sys_getpwuid(uid_t uid)
1175 return getpwuid(uid);
1178 struct group *sys_getgrnam(const char *name)
1180 return getgrnam(name);
1183 struct group *sys_getgrgid(gid_t gid)
1185 return getgrgid(gid);
1188 /**************************************************************************
1189 Extract a command into an arg list.
1190 ****************************************************************************/
1192 static char **extract_args(TALLOC_CTX *mem_ctx, const char *command)
1201 if (!(trunc_cmd = talloc_strdup(mem_ctx, command))) {
1202 DEBUG(0, ("talloc failed\n"));
1206 if(!(ptr = strtok_r(trunc_cmd, " \t", &saveptr))) {
1207 TALLOC_FREE(trunc_cmd);
1216 for( argcl = 1; ptr; ptr = strtok_r(NULL, " \t", &saveptr))
1219 TALLOC_FREE(trunc_cmd);
1221 if (!(argl = TALLOC_ARRAY(mem_ctx, char *, argcl + 1))) {
1226 * Now do the extraction.
1229 if (!(trunc_cmd = talloc_strdup(mem_ctx, command))) {
1233 ptr = strtok_r(trunc_cmd, " \t", &saveptr);
1236 if (!(argl[i++] = talloc_strdup(argl, ptr))) {
1240 while((ptr = strtok_r(NULL, " \t", &saveptr)) != NULL) {
1242 if (!(argl[i++] = talloc_strdup(argl, ptr))) {
1251 DEBUG(0, ("talloc failed\n"));
1252 TALLOC_FREE(trunc_cmd);
1258 /**************************************************************************
1259 Wrapper for popen. Safer as it doesn't search a path.
1260 Modified from the glibc sources.
1261 modified by tridge to return a file descriptor. We must kick our FILE* habit
1262 ****************************************************************************/
1264 typedef struct _popen_list
1268 struct _popen_list *next;
1271 static popen_list *popen_chain;
1273 int sys_popen(const char *command)
1275 int parent_end, child_end;
1277 popen_list *entry = NULL;
1280 if (pipe(pipe_fds) < 0)
1283 parent_end = pipe_fds[0];
1284 child_end = pipe_fds[1];
1291 if((entry = SMB_MALLOC_P(popen_list)) == NULL)
1294 ZERO_STRUCTP(entry);
1297 * Extract the command and args into a NULL terminated array.
1300 if(!(argl = extract_args(NULL, command)))
1303 entry->child_pid = sys_fork();
1305 if (entry->child_pid == -1) {
1309 if (entry->child_pid == 0) {
1315 int child_std_end = STDOUT_FILENO;
1319 if (child_end != child_std_end) {
1320 dup2 (child_end, child_std_end);
1325 * POSIX.2: "popen() shall ensure that any streams from previous
1326 * popen() calls that remain open in the parent process are closed
1327 * in the new child process."
1330 for (p = popen_chain; p; p = p->next)
1333 execv(argl[0], argl);
1344 /* Link into popen_chain. */
1345 entry->next = popen_chain;
1346 popen_chain = entry;
1347 entry->fd = parent_end;
1360 /**************************************************************************
1361 Wrapper for pclose. Modified from the glibc sources.
1362 ****************************************************************************/
1364 int sys_pclose(int fd)
1367 popen_list **ptr = &popen_chain;
1368 popen_list *entry = NULL;
1372 /* Unlink from popen_chain. */
1373 for ( ; *ptr != NULL; ptr = &(*ptr)->next) {
1374 if ((*ptr)->fd == fd) {
1376 *ptr = (*ptr)->next;
1382 if (status < 0 || close(entry->fd) < 0)
1386 * As Samba is catching and eating child process
1387 * exits we don't really care about the child exit
1388 * code, a -1 with errno = ECHILD will do fine for us.
1392 wait_pid = sys_waitpid (entry->child_pid, &wstatus, 0);
1393 } while (wait_pid == -1 && errno == EINTR);
1402 /**************************************************************************
1403 Wrapper for Admin Logs.
1404 ****************************************************************************/
1406 void sys_adminlog(int priority, const char *format_str, ...)
1410 char *msgbuf = NULL;
1412 va_start( ap, format_str );
1413 ret = vasprintf( &msgbuf, format_str, ap );
1419 #if defined(HAVE_SYSLOG)
1420 syslog( priority, "%s", msgbuf );
1422 DEBUG(0,("%s", msgbuf ));
1427 /******** Solaris EA helper function prototypes ********/
1428 #ifdef HAVE_ATTROPEN
1429 #define SOLARIS_ATTRMODE S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP
1430 static int solaris_write_xattr(int attrfd, const char *value, size_t size);
1431 static ssize_t solaris_read_xattr(int attrfd, void *value, size_t size);
1432 static ssize_t solaris_list_xattr(int attrdirfd, char *list, size_t size);
1433 static int solaris_unlinkat(int attrdirfd, const char *name);
1434 static int solaris_attropen(const char *path, const char *attrpath, int oflag, mode_t mode);
1435 static int solaris_openat(int fildes, const char *path, int oflag, mode_t mode);
1438 /**************************************************************************
1439 Wrappers for extented attribute calls. Based on the Linux package with
1440 support for IRIX and (Net|Free)BSD also. Expand as other systems have them.
1441 ****************************************************************************/
1443 ssize_t sys_getxattr (const char *path, const char *name, void *value, size_t size)
1445 #if defined(HAVE_GETXATTR)
1446 #ifndef XATTR_ADD_OPT
1447 return getxattr(path, name, value, size);
1450 return getxattr(path, name, value, size, 0, options);
1452 #elif defined(HAVE_GETEA)
1453 return getea(path, name, value, size);
1454 #elif defined(HAVE_EXTATTR_GET_FILE)
1457 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
1458 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
1459 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
1461 * The BSD implementation has a nasty habit of silently truncating
1462 * the returned value to the size of the buffer, so we have to check
1463 * that the buffer is large enough to fit the returned value.
1465 if((retval=extattr_get_file(path, attrnamespace, attrname, NULL, 0)) >= 0) {
1470 if((retval=extattr_get_file(path, attrnamespace, attrname, value, size)) >= 0)
1474 DEBUG(10,("sys_getxattr: extattr_get_file() failed with: %s\n", strerror(errno)));
1476 #elif defined(HAVE_ATTR_GET)
1477 int retval, flags = 0;
1478 int valuelength = (int)size;
1479 char *attrname = strchr(name,'.') + 1;
1481 if (strncmp(name, "system", 6) == 0) flags |= ATTR_ROOT;
1483 retval = attr_get(path, attrname, (char *)value, &valuelength, flags);
1485 return retval ? retval : valuelength;
1486 #elif defined(HAVE_ATTROPEN)
1488 int attrfd = solaris_attropen(path, name, O_RDONLY, 0);
1490 ret = solaris_read_xattr(attrfd, value, size);
1500 ssize_t sys_lgetxattr (const char *path, const char *name, void *value, size_t size)
1502 #if defined(HAVE_LGETXATTR)
1503 return lgetxattr(path, name, value, size);
1504 #elif defined(HAVE_GETXATTR) && defined(XATTR_ADD_OPT)
1505 int options = XATTR_NOFOLLOW;
1506 return getxattr(path, name, value, size, 0, options);
1507 #elif defined(HAVE_LGETEA)
1508 return lgetea(path, name, value, size);
1509 #elif defined(HAVE_EXTATTR_GET_LINK)
1512 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
1513 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
1514 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
1516 if((retval=extattr_get_link(path, attrnamespace, attrname, NULL, 0)) >= 0) {
1521 if((retval=extattr_get_link(path, attrnamespace, attrname, value, size)) >= 0)
1525 DEBUG(10,("sys_lgetxattr: extattr_get_link() failed with: %s\n", strerror(errno)));
1527 #elif defined(HAVE_ATTR_GET)
1528 int retval, flags = ATTR_DONTFOLLOW;
1529 int valuelength = (int)size;
1530 char *attrname = strchr(name,'.') + 1;
1532 if (strncmp(name, "system", 6) == 0) flags |= ATTR_ROOT;
1534 retval = attr_get(path, attrname, (char *)value, &valuelength, flags);
1536 return retval ? retval : valuelength;
1537 #elif defined(HAVE_ATTROPEN)
1539 int attrfd = solaris_attropen(path, name, O_RDONLY|AT_SYMLINK_NOFOLLOW, 0);
1541 ret = solaris_read_xattr(attrfd, value, size);
1551 ssize_t sys_fgetxattr (int filedes, const char *name, void *value, size_t size)
1553 #if defined(HAVE_FGETXATTR)
1554 #ifndef XATTR_ADD_OPT
1555 return fgetxattr(filedes, name, value, size);
1558 return fgetxattr(filedes, name, value, size, 0, options);
1560 #elif defined(HAVE_FGETEA)
1561 return fgetea(filedes, name, value, size);
1562 #elif defined(HAVE_EXTATTR_GET_FD)
1565 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
1566 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
1567 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
1569 if((retval=extattr_get_fd(filedes, attrnamespace, attrname, NULL, 0)) >= 0) {
1574 if((retval=extattr_get_fd(filedes, attrnamespace, attrname, value, size)) >= 0)
1578 DEBUG(10,("sys_fgetxattr: extattr_get_fd() failed with: %s\n", strerror(errno)));
1580 #elif defined(HAVE_ATTR_GETF)
1581 int retval, flags = 0;
1582 int valuelength = (int)size;
1583 char *attrname = strchr(name,'.') + 1;
1585 if (strncmp(name, "system", 6) == 0) flags |= ATTR_ROOT;
1587 retval = attr_getf(filedes, attrname, (char *)value, &valuelength, flags);
1589 return retval ? retval : valuelength;
1590 #elif defined(HAVE_ATTROPEN)
1592 int attrfd = solaris_openat(filedes, name, O_RDONLY|O_XATTR, 0);
1594 ret = solaris_read_xattr(attrfd, value, size);
1604 #if defined(HAVE_EXTATTR_LIST_FILE)
1606 #define EXTATTR_PREFIX(s) (s), (sizeof((s))-1)
1614 { EXTATTR_NAMESPACE_SYSTEM, EXTATTR_PREFIX("system.") },
1615 { EXTATTR_NAMESPACE_USER, EXTATTR_PREFIX("user.") },
1623 static ssize_t bsd_attr_list (int type, extattr_arg arg, char *list, size_t size)
1625 ssize_t list_size, total_size = 0;
1628 /* Iterate through extattr(2) namespaces */
1629 for(t = 0; t < (sizeof(extattr)/sizeof(extattr[0])); t++) {
1631 #if defined(HAVE_EXTATTR_LIST_FILE)
1633 list_size = extattr_list_file(arg.path, extattr[t].space, list, size);
1636 #if defined(HAVE_EXTATTR_LIST_LINK)
1638 list_size = extattr_list_link(arg.path, extattr[t].space, list, size);
1641 #if defined(HAVE_EXTATTR_LIST_FD)
1643 list_size = extattr_list_fd(arg.filedes, extattr[t].space, list, size);
1650 /* Some error happend. Errno should be set by the previous call */
1656 /* XXX: Call with an empty buffer may be used to calculate
1657 necessary buffer size. Unfortunately, we can't say, how
1658 many attributes were returned, so here is the potential
1659 problem with the emulation.
1662 /* Take the worse case of one char attribute names -
1663 two bytes per name plus one more for sanity.
1665 total_size += list_size + (list_size/2 + 1)*extattr[t].len;
1668 /* Count necessary offset to fit namespace prefixes */
1670 for(i = 0; i < list_size; i += list[i] + 1)
1671 len += extattr[t].len;
1673 total_size += list_size + len;
1674 /* Buffer is too small to fit the results */
1675 if(total_size > size) {
1679 /* Shift results back, so we can prepend prefixes */
1680 buf = memmove(list + len, list, list_size);
1682 for(i = 0; i < list_size; i += len + 1) {
1684 strncpy(list, extattr[t].name, extattr[t].len + 1);
1685 list += extattr[t].len;
1686 strncpy(list, buf + i + 1, len);
1697 #if defined(HAVE_ATTR_LIST) && defined(HAVE_SYS_ATTRIBUTES_H)
1698 static char attr_buffer[ATTR_MAX_VALUELEN];
1700 static ssize_t irix_attr_list(const char *path, int filedes, char *list, size_t size, int flags)
1702 int retval = 0, index;
1703 attrlist_cursor_t *cursor = 0;
1705 attrlist_t * al = (attrlist_t *)attr_buffer;
1707 size_t ent_size, left = size;
1712 retval = attr_listf(filedes, attr_buffer, ATTR_MAX_VALUELEN, flags, cursor);
1714 retval = attr_list(path, attr_buffer, ATTR_MAX_VALUELEN, flags, cursor);
1716 for (index = 0; index < al->al_count; index++) {
1717 ae = ATTR_ENTRY(attr_buffer, index);
1718 ent_size = strlen(ae->a_name) + sizeof("user.");
1719 if (left >= ent_size) {
1720 strncpy(bp, "user.", sizeof("user."));
1721 strncat(bp, ae->a_name, ent_size - sizeof("user."));
1729 total_size += ent_size;
1731 if (al->al_more == 0) break;
1738 retval = attr_listf(filedes, attr_buffer, ATTR_MAX_VALUELEN, flags, cursor);
1740 retval = attr_list(path, attr_buffer, ATTR_MAX_VALUELEN, flags, cursor);
1742 for (index = 0; index < al->al_count; index++) {
1743 ae = ATTR_ENTRY(attr_buffer, index);
1744 ent_size = strlen(ae->a_name) + sizeof("system.");
1745 if (left >= ent_size) {
1746 strncpy(bp, "system.", sizeof("system."));
1747 strncat(bp, ae->a_name, ent_size - sizeof("system."));
1755 total_size += ent_size;
1757 if (al->al_more == 0) break;
1760 return (ssize_t)(retval ? retval : total_size);
1765 ssize_t sys_listxattr (const char *path, char *list, size_t size)
1767 #if defined(HAVE_LISTXATTR)
1768 #ifndef XATTR_ADD_OPT
1769 return listxattr(path, list, size);
1772 return listxattr(path, list, size, options);
1774 #elif defined(HAVE_LISTEA)
1775 return listea(path, list, size);
1776 #elif defined(HAVE_EXTATTR_LIST_FILE)
1779 return bsd_attr_list(0, arg, list, size);
1780 #elif defined(HAVE_ATTR_LIST) && defined(HAVE_SYS_ATTRIBUTES_H)
1781 return irix_attr_list(path, 0, list, size, 0);
1782 #elif defined(HAVE_ATTROPEN)
1784 int attrdirfd = solaris_attropen(path, ".", O_RDONLY, 0);
1785 if (attrdirfd >= 0) {
1786 ret = solaris_list_xattr(attrdirfd, list, size);
1796 ssize_t sys_llistxattr (const char *path, char *list, size_t size)
1798 #if defined(HAVE_LLISTXATTR)
1799 return llistxattr(path, list, size);
1800 #elif defined(HAVE_LISTXATTR) && defined(XATTR_ADD_OPT)
1801 int options = XATTR_NOFOLLOW;
1802 return listxattr(path, list, size, options);
1803 #elif defined(HAVE_LLISTEA)
1804 return llistea(path, list, size);
1805 #elif defined(HAVE_EXTATTR_LIST_LINK)
1808 return bsd_attr_list(1, arg, list, size);
1809 #elif defined(HAVE_ATTR_LIST) && defined(HAVE_SYS_ATTRIBUTES_H)
1810 return irix_attr_list(path, 0, list, size, ATTR_DONTFOLLOW);
1811 #elif defined(HAVE_ATTROPEN)
1813 int attrdirfd = solaris_attropen(path, ".", O_RDONLY|AT_SYMLINK_NOFOLLOW, 0);
1814 if (attrdirfd >= 0) {
1815 ret = solaris_list_xattr(attrdirfd, list, size);
1825 ssize_t sys_flistxattr (int filedes, char *list, size_t size)
1827 #if defined(HAVE_FLISTXATTR)
1828 #ifndef XATTR_ADD_OPT
1829 return flistxattr(filedes, list, size);
1832 return flistxattr(filedes, list, size, options);
1834 #elif defined(HAVE_FLISTEA)
1835 return flistea(filedes, list, size);
1836 #elif defined(HAVE_EXTATTR_LIST_FD)
1838 arg.filedes = filedes;
1839 return bsd_attr_list(2, arg, list, size);
1840 #elif defined(HAVE_ATTR_LISTF)
1841 return irix_attr_list(NULL, filedes, list, size, 0);
1842 #elif defined(HAVE_ATTROPEN)
1844 int attrdirfd = solaris_openat(filedes, ".", O_RDONLY|O_XATTR, 0);
1845 if (attrdirfd >= 0) {
1846 ret = solaris_list_xattr(attrdirfd, list, size);
1856 int sys_removexattr (const char *path, const char *name)
1858 #if defined(HAVE_REMOVEXATTR)
1859 #ifndef XATTR_ADD_OPT
1860 return removexattr(path, name);
1863 return removexattr(path, name, options);
1865 #elif defined(HAVE_REMOVEEA)
1866 return removeea(path, name);
1867 #elif defined(HAVE_EXTATTR_DELETE_FILE)
1869 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
1870 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
1871 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
1873 return extattr_delete_file(path, attrnamespace, attrname);
1874 #elif defined(HAVE_ATTR_REMOVE)
1876 char *attrname = strchr(name,'.') + 1;
1878 if (strncmp(name, "system", 6) == 0) flags |= ATTR_ROOT;
1880 return attr_remove(path, attrname, flags);
1881 #elif defined(HAVE_ATTROPEN)
1883 int attrdirfd = solaris_attropen(path, ".", O_RDONLY, 0);
1884 if (attrdirfd >= 0) {
1885 ret = solaris_unlinkat(attrdirfd, name);
1895 int sys_lremovexattr (const char *path, const char *name)
1897 #if defined(HAVE_LREMOVEXATTR)
1898 return lremovexattr(path, name);
1899 #elif defined(HAVE_REMOVEXATTR) && defined(XATTR_ADD_OPT)
1900 int options = XATTR_NOFOLLOW;
1901 return removexattr(path, name, options);
1902 #elif defined(HAVE_LREMOVEEA)
1903 return lremoveea(path, name);
1904 #elif defined(HAVE_EXTATTR_DELETE_LINK)
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 return extattr_delete_link(path, attrnamespace, attrname);
1911 #elif defined(HAVE_ATTR_REMOVE)
1912 int flags = ATTR_DONTFOLLOW;
1913 char *attrname = strchr(name,'.') + 1;
1915 if (strncmp(name, "system", 6) == 0) flags |= ATTR_ROOT;
1917 return attr_remove(path, attrname, flags);
1918 #elif defined(HAVE_ATTROPEN)
1920 int attrdirfd = solaris_attropen(path, ".", O_RDONLY|AT_SYMLINK_NOFOLLOW, 0);
1921 if (attrdirfd >= 0) {
1922 ret = solaris_unlinkat(attrdirfd, name);
1932 int sys_fremovexattr (int filedes, const char *name)
1934 #if defined(HAVE_FREMOVEXATTR)
1935 #ifndef XATTR_ADD_OPT
1936 return fremovexattr(filedes, name);
1939 return fremovexattr(filedes, name, options);
1941 #elif defined(HAVE_FREMOVEEA)
1942 return fremoveea(filedes, name);
1943 #elif defined(HAVE_EXTATTR_DELETE_FD)
1945 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
1946 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
1947 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
1949 return extattr_delete_fd(filedes, attrnamespace, attrname);
1950 #elif defined(HAVE_ATTR_REMOVEF)
1952 char *attrname = strchr(name,'.') + 1;
1954 if (strncmp(name, "system", 6) == 0) flags |= ATTR_ROOT;
1956 return attr_removef(filedes, attrname, flags);
1957 #elif defined(HAVE_ATTROPEN)
1959 int attrdirfd = solaris_openat(filedes, ".", O_RDONLY|O_XATTR, 0);
1960 if (attrdirfd >= 0) {
1961 ret = solaris_unlinkat(attrdirfd, name);
1971 int sys_setxattr (const char *path, const char *name, const void *value, size_t size, int flags)
1973 #if defined(HAVE_SETXATTR)
1974 #ifndef XATTR_ADD_OPT
1975 return setxattr(path, name, value, size, flags);
1978 return setxattr(path, name, value, size, 0, options);
1980 #elif defined(HAVE_SETEA)
1981 return setea(path, name, value, size, flags);
1982 #elif defined(HAVE_EXTATTR_SET_FILE)
1985 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
1986 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
1987 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
1989 /* Check attribute existence */
1990 retval = extattr_get_file(path, attrnamespace, attrname, NULL, 0);
1992 /* REPLACE attribute, that doesn't exist */
1993 if (flags & XATTR_REPLACE && errno == ENOATTR) {
1997 /* Ignore other errors */
2000 /* CREATE attribute, that already exists */
2001 if (flags & XATTR_CREATE) {
2007 retval = extattr_set_file(path, attrnamespace, attrname, value, size);
2008 return (retval < 0) ? -1 : 0;
2009 #elif defined(HAVE_ATTR_SET)
2011 char *attrname = strchr(name,'.') + 1;
2013 if (strncmp(name, "system", 6) == 0) myflags |= ATTR_ROOT;
2014 if (flags & XATTR_CREATE) myflags |= ATTR_CREATE;
2015 if (flags & XATTR_REPLACE) myflags |= ATTR_REPLACE;
2017 return attr_set(path, attrname, (const char *)value, size, myflags);
2018 #elif defined(HAVE_ATTROPEN)
2020 int myflags = O_RDWR;
2022 if (flags & XATTR_CREATE) myflags |= O_EXCL;
2023 if (!(flags & XATTR_REPLACE)) myflags |= O_CREAT;
2024 attrfd = solaris_attropen(path, name, myflags, (mode_t) SOLARIS_ATTRMODE);
2026 ret = solaris_write_xattr(attrfd, value, size);
2036 int sys_lsetxattr (const char *path, const char *name, const void *value, size_t size, int flags)
2038 #if defined(HAVE_LSETXATTR)
2039 return lsetxattr(path, name, value, size, flags);
2040 #elif defined(HAVE_SETXATTR) && defined(XATTR_ADD_OPT)
2041 int options = XATTR_NOFOLLOW;
2042 return setxattr(path, name, value, size, 0, options);
2043 #elif defined(LSETEA)
2044 return lsetea(path, name, value, size, flags);
2045 #elif defined(HAVE_EXTATTR_SET_LINK)
2048 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
2049 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
2050 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
2052 /* Check attribute existence */
2053 retval = extattr_get_link(path, attrnamespace, attrname, NULL, 0);
2055 /* REPLACE attribute, that doesn't exist */
2056 if (flags & XATTR_REPLACE && errno == ENOATTR) {
2060 /* Ignore other errors */
2063 /* CREATE attribute, that already exists */
2064 if (flags & XATTR_CREATE) {
2071 retval = extattr_set_link(path, attrnamespace, attrname, value, size);
2072 return (retval < 0) ? -1 : 0;
2073 #elif defined(HAVE_ATTR_SET)
2074 int myflags = ATTR_DONTFOLLOW;
2075 char *attrname = strchr(name,'.') + 1;
2077 if (strncmp(name, "system", 6) == 0) myflags |= ATTR_ROOT;
2078 if (flags & XATTR_CREATE) myflags |= ATTR_CREATE;
2079 if (flags & XATTR_REPLACE) myflags |= ATTR_REPLACE;
2081 return attr_set(path, attrname, (const char *)value, size, myflags);
2082 #elif defined(HAVE_ATTROPEN)
2084 int myflags = O_RDWR | AT_SYMLINK_NOFOLLOW;
2086 if (flags & XATTR_CREATE) myflags |= O_EXCL;
2087 if (!(flags & XATTR_REPLACE)) myflags |= O_CREAT;
2088 attrfd = solaris_attropen(path, name, myflags, (mode_t) SOLARIS_ATTRMODE);
2090 ret = solaris_write_xattr(attrfd, value, size);
2100 int sys_fsetxattr (int filedes, const char *name, const void *value, size_t size, int flags)
2102 #if defined(HAVE_FSETXATTR)
2103 #ifndef XATTR_ADD_OPT
2104 return fsetxattr(filedes, name, value, size, flags);
2107 return fsetxattr(filedes, name, value, size, 0, options);
2109 #elif defined(HAVE_FSETEA)
2110 return fsetea(filedes, name, value, size, flags);
2111 #elif defined(HAVE_EXTATTR_SET_FD)
2114 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
2115 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
2116 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
2118 /* Check attribute existence */
2119 retval = extattr_get_fd(filedes, attrnamespace, attrname, NULL, 0);
2121 /* REPLACE attribute, that doesn't exist */
2122 if (flags & XATTR_REPLACE && errno == ENOATTR) {
2126 /* Ignore other errors */
2129 /* CREATE attribute, that already exists */
2130 if (flags & XATTR_CREATE) {
2136 retval = extattr_set_fd(filedes, attrnamespace, attrname, value, size);
2137 return (retval < 0) ? -1 : 0;
2138 #elif defined(HAVE_ATTR_SETF)
2140 char *attrname = strchr(name,'.') + 1;
2142 if (strncmp(name, "system", 6) == 0) myflags |= ATTR_ROOT;
2143 if (flags & XATTR_CREATE) myflags |= ATTR_CREATE;
2144 if (flags & XATTR_REPLACE) myflags |= ATTR_REPLACE;
2146 return attr_setf(filedes, attrname, (const char *)value, size, myflags);
2147 #elif defined(HAVE_ATTROPEN)
2149 int myflags = O_RDWR | O_XATTR;
2151 if (flags & XATTR_CREATE) myflags |= O_EXCL;
2152 if (!(flags & XATTR_REPLACE)) myflags |= O_CREAT;
2153 attrfd = solaris_openat(filedes, name, myflags, (mode_t) SOLARIS_ATTRMODE);
2155 ret = solaris_write_xattr(attrfd, value, size);
2165 /**************************************************************************
2166 helper functions for Solaris' EA support
2167 ****************************************************************************/
2168 #ifdef HAVE_ATTROPEN
2169 static ssize_t solaris_read_xattr(int attrfd, void *value, size_t size)
2173 if (fstat(attrfd, &sbuf) == -1) {
2178 /* This is to return the current size of the named extended attribute */
2180 return sbuf.st_size;
2183 /* check size and read xattr */
2184 if (sbuf.st_size > size) {
2189 return read(attrfd, value, sbuf.st_size);
2192 static ssize_t solaris_list_xattr(int attrdirfd, char *list, size_t size)
2197 int newfd = dup(attrdirfd);
2198 /* CAUTION: The originating file descriptor should not be
2199 used again following the call to fdopendir().
2200 For that reason we dup() the file descriptor
2201 here to make things more clear. */
2202 dirp = fdopendir(newfd);
2204 while ((de = readdir(dirp))) {
2205 size_t listlen = strlen(de->d_name);
2206 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
2207 /* we don't want "." and ".." here: */
2208 DEBUG(10,("skipped EA %s\n",de->d_name));
2213 /* return the current size of the list of extended attribute names*/
2216 /* check size and copy entrieѕ + nul into list. */
2217 if ((len + listlen + 1) > size) {
2222 safe_strcpy(list + len, de->d_name, listlen);
2230 if (closedir(dirp) == -1) {
2231 DEBUG(0,("closedir dirp failed: %s\n",strerror(errno)));
2237 static int solaris_unlinkat(int attrdirfd, const char *name)
2239 if (unlinkat(attrdirfd, name, 0) == -1) {
2240 if (errno == ENOENT) {
2248 static int solaris_attropen(const char *path, const char *attrpath, int oflag, mode_t mode)
2250 int filedes = attropen(path, attrpath, oflag, mode);
2251 if (filedes == -1) {
2252 DEBUG(10,("attropen FAILED: path: %s, name: %s, errno: %s\n",path,attrpath,strerror(errno)));
2253 if (errno == EINVAL) {
2262 static int solaris_openat(int fildes, const char *path, int oflag, mode_t mode)
2264 int filedes = openat(fildes, path, oflag, mode);
2265 if (filedes == -1) {
2266 DEBUG(10,("openat FAILED: fd: %d, path: %s, errno: %s\n",filedes,path,strerror(errno)));
2267 if (errno == EINVAL) {
2276 static int solaris_write_xattr(int attrfd, const char *value, size_t size)
2278 if ((ftruncate(attrfd, 0) == 0) && (write(attrfd, value, size) == size)) {
2281 DEBUG(10,("solaris_write_xattr FAILED!\n"));
2285 #endif /*HAVE_ATTROPEN*/
2288 /****************************************************************************
2289 Return the major devicenumber for UNIX extensions.
2290 ****************************************************************************/
2292 uint32 unix_dev_major(SMB_DEV_T dev)
2294 #if defined(HAVE_DEVICE_MAJOR_FN)
2295 return (uint32)major(dev);
2297 return (uint32)(dev >> 8);
2301 /****************************************************************************
2302 Return the minor devicenumber for UNIX extensions.
2303 ****************************************************************************/
2305 uint32 unix_dev_minor(SMB_DEV_T dev)
2307 #if defined(HAVE_DEVICE_MINOR_FN)
2308 return (uint32)minor(dev);
2310 return (uint32)(dev & 0xff);
2314 #if defined(WITH_AIO)
2316 /*******************************************************************
2317 An aio_read wrapper that will deal with 64-bit sizes.
2318 ********************************************************************/
2320 int sys_aio_read(SMB_STRUCT_AIOCB *aiocb)
2322 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_READ64)
2323 return aio_read64(aiocb);
2324 #elif defined(HAVE_AIO_READ)
2325 return aio_read(aiocb);
2332 /*******************************************************************
2333 An aio_write wrapper that will deal with 64-bit sizes.
2334 ********************************************************************/
2336 int sys_aio_write(SMB_STRUCT_AIOCB *aiocb)
2338 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_WRITE64)
2339 return aio_write64(aiocb);
2340 #elif defined(HAVE_AIO_WRITE)
2341 return aio_write(aiocb);
2348 /*******************************************************************
2349 An aio_return wrapper that will deal with 64-bit sizes.
2350 ********************************************************************/
2352 ssize_t sys_aio_return(SMB_STRUCT_AIOCB *aiocb)
2354 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_RETURN64)
2355 return aio_return64(aiocb);
2356 #elif defined(HAVE_AIO_RETURN)
2357 return aio_return(aiocb);
2364 /*******************************************************************
2365 An aio_cancel wrapper that will deal with 64-bit sizes.
2366 ********************************************************************/
2368 int sys_aio_cancel(int fd, SMB_STRUCT_AIOCB *aiocb)
2370 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_CANCEL64)
2371 return aio_cancel64(fd, aiocb);
2372 #elif defined(HAVE_AIO_CANCEL)
2373 return aio_cancel(fd, aiocb);
2380 /*******************************************************************
2381 An aio_error wrapper that will deal with 64-bit sizes.
2382 ********************************************************************/
2384 int sys_aio_error(const SMB_STRUCT_AIOCB *aiocb)
2386 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_ERROR64)
2387 return aio_error64(aiocb);
2388 #elif defined(HAVE_AIO_ERROR)
2389 return aio_error(aiocb);
2396 /*******************************************************************
2397 An aio_fsync wrapper that will deal with 64-bit sizes.
2398 ********************************************************************/
2400 int sys_aio_fsync(int op, SMB_STRUCT_AIOCB *aiocb)
2402 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_FSYNC64)
2403 return aio_fsync64(op, aiocb);
2404 #elif defined(HAVE_AIO_FSYNC)
2405 return aio_fsync(op, aiocb);
2412 /*******************************************************************
2413 An aio_fsync wrapper that will deal with 64-bit sizes.
2414 ********************************************************************/
2416 int sys_aio_suspend(const SMB_STRUCT_AIOCB * const cblist[], int n, const struct timespec *timeout)
2418 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_SUSPEND64)
2419 return aio_suspend64(cblist, n, timeout);
2420 #elif defined(HAVE_AIO_FSYNC)
2421 return aio_suspend(cblist, n, timeout);
2427 #else /* !WITH_AIO */
2429 int sys_aio_read(SMB_STRUCT_AIOCB *aiocb)
2435 int sys_aio_write(SMB_STRUCT_AIOCB *aiocb)
2441 ssize_t sys_aio_return(SMB_STRUCT_AIOCB *aiocb)
2447 int sys_aio_cancel(int fd, SMB_STRUCT_AIOCB *aiocb)
2453 int sys_aio_error(const SMB_STRUCT_AIOCB *aiocb)
2459 int sys_aio_fsync(int op, SMB_STRUCT_AIOCB *aiocb)
2465 int sys_aio_suspend(const SMB_STRUCT_AIOCB * const cblist[], int n, const struct timespec *timeout)
2470 #endif /* WITH_AIO */
2472 int sys_getpeereid( int s, uid_t *uid)
2474 #if defined(HAVE_PEERCRED)
2476 socklen_t cred_len = sizeof(struct ucred);
2479 ret = getsockopt(s, SOL_SOCKET, SO_PEERCRED, (void *)&cred, &cred_len);
2484 if (cred_len != sizeof(struct ucred)) {
2497 int sys_getnameinfo(const struct sockaddr *psa,
2506 * For Solaris we must make sure salen is the
2507 * correct length for the incoming sa_family.
2510 if (salen == sizeof(struct sockaddr_storage)) {
2511 salen = sizeof(struct sockaddr_in);
2512 #if defined(HAVE_IPV6)
2513 if (psa->sa_family == AF_INET6) {
2514 salen = sizeof(struct sockaddr_in6);
2518 return getnameinfo(psa, salen, host, hostlen, service, servlen, flags);
2521 int sys_connect(int fd, const struct sockaddr * addr)
2523 socklen_t salen = -1;
2525 if (addr->sa_family == AF_INET) {
2526 salen = sizeof(struct sockaddr_in);
2527 } else if (addr->sa_family == AF_UNIX) {
2528 salen = sizeof(struct sockaddr_un);
2530 #if defined(HAVE_IPV6)
2531 else if (addr->sa_family == AF_INET6) {
2532 salen = sizeof(struct sockaddr_in6);
2536 return connect(fd, addr, salen);