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 The wait() calls vary between systems
542 ********************************************************************/
544 int sys_waitpid(pid_t pid,int *status,int options)
547 return waitpid(pid,status,options);
548 #else /* HAVE_WAITPID */
549 return wait4(pid, status, options, NULL);
550 #endif /* HAVE_WAITPID */
553 /*******************************************************************
554 System wrapper for getwd
555 ********************************************************************/
557 char *sys_getwd(char *s)
561 wd = (char *)getcwd(s, PATH_MAX);
563 wd = (char *)getwd(s);
568 #if defined(HAVE_POSIX_CAPABILITIES)
570 /**************************************************************************
571 Try and abstract process capabilities (for systems that have them).
572 ****************************************************************************/
574 /* Set the POSIX capabilities needed for the given purpose into the effective
575 * capability set of the current process. Make sure they are always removed
576 * from the inheritable set, because there is no circumstance in which our
577 * children should inherit our elevated privileges.
579 static bool set_process_capability(enum smbd_capability capability,
582 cap_value_t cap_vals[2] = {0};
583 int num_cap_vals = 0;
587 #if defined(HAVE_PRCTL) && defined(PR_GET_KEEPCAPS) && defined(PR_SET_KEEPCAPS)
588 /* On Linux, make sure that any capabilities we grab are sticky
589 * across UID changes. We expect that this would allow us to keep both
590 * the effective and permitted capability sets, but as of circa 2.6.16,
591 * only the permitted set is kept. It is a bug (which we work around)
592 * that the effective set is lost, but we still require the effective
595 if (!prctl(PR_GET_KEEPCAPS)) {
596 prctl(PR_SET_KEEPCAPS, 1);
600 cap = cap_get_proc();
602 DEBUG(0,("set_process_capability: cap_get_proc failed: %s\n",
607 switch (capability) {
608 case KERNEL_OPLOCK_CAPABILITY:
609 #ifdef CAP_NETWORK_MGT
610 /* IRIX has CAP_NETWORK_MGT for oplocks. */
611 cap_vals[num_cap_vals++] = CAP_NETWORK_MGT;
614 case DMAPI_ACCESS_CAPABILITY:
615 #ifdef CAP_DEVICE_MGT
616 /* IRIX has CAP_DEVICE_MGT for DMAPI access. */
617 cap_vals[num_cap_vals++] = CAP_DEVICE_MGT;
619 /* Linux has CAP_MKNOD for DMAPI access. */
620 cap_vals[num_cap_vals++] = CAP_MKNOD;
623 case LEASE_CAPABILITY:
625 cap_vals[num_cap_vals++] = CAP_LEASE;
630 SMB_ASSERT(num_cap_vals <= ARRAY_SIZE(cap_vals));
632 if (num_cap_vals == 0) {
637 cap_set_flag(cap, CAP_EFFECTIVE, num_cap_vals, cap_vals,
638 enable ? CAP_SET : CAP_CLEAR);
640 /* We never want to pass capabilities down to our children, so make
641 * sure they are not inherited.
643 cap_set_flag(cap, CAP_INHERITABLE, num_cap_vals, cap_vals, CAP_CLEAR);
645 if (cap_set_proc(cap) == -1) {
646 DEBUG(0, ("set_process_capability: cap_set_proc failed: %s\n",
656 #endif /* HAVE_POSIX_CAPABILITIES */
658 /****************************************************************************
659 Gain the oplock capability from the kernel if possible.
660 ****************************************************************************/
662 void set_effective_capability(enum smbd_capability capability)
664 #if defined(HAVE_POSIX_CAPABILITIES)
665 set_process_capability(capability, True);
666 #endif /* HAVE_POSIX_CAPABILITIES */
669 void drop_effective_capability(enum smbd_capability capability)
671 #if defined(HAVE_POSIX_CAPABILITIES)
672 set_process_capability(capability, False);
673 #endif /* HAVE_POSIX_CAPABILITIES */
676 /**************************************************************************
677 Wrapper for random().
678 ****************************************************************************/
680 long sys_random(void)
682 #if defined(HAVE_RANDOM)
683 return (long)random();
684 #elif defined(HAVE_RAND)
687 DEBUG(0,("Error - no random function available !\n"));
692 /**************************************************************************
693 Wrapper for srandom().
694 ****************************************************************************/
696 void sys_srandom(unsigned int seed)
698 #if defined(HAVE_SRANDOM)
700 #elif defined(HAVE_SRAND)
703 DEBUG(0,("Error - no srandom function available !\n"));
708 /**************************************************************************
709 Returns equivalent to NGROUPS_MAX - using sysconf if needed.
710 ****************************************************************************/
714 #if defined(SYSCONF_SC_NGROUPS_MAX)
715 int ret = sysconf(_SC_NGROUPS_MAX);
716 return (ret == -1) ? NGROUPS_MAX : ret;
722 /**************************************************************************
723 Wrap setgroups and getgroups for systems that declare getgroups() as
724 returning an array of gid_t, but actuall return an array of int.
725 ****************************************************************************/
727 #if defined(HAVE_BROKEN_GETGROUPS)
728 static int sys_broken_getgroups(int setlen, gid_t *gidset)
735 return getgroups(setlen, &gid);
739 * Broken case. We need to allocate a
740 * GID_T array of size setlen.
749 setlen = groups_max();
751 if((group_list = SMB_MALLOC_ARRAY(GID_T, setlen)) == NULL) {
752 DEBUG(0,("sys_getgroups: Malloc fail.\n"));
756 if((ngroups = getgroups(setlen, group_list)) < 0) {
757 int saved_errno = errno;
758 SAFE_FREE(group_list);
763 for(i = 0; i < ngroups; i++)
764 gidset[i] = (gid_t)group_list[i];
766 SAFE_FREE(group_list);
770 static int sys_broken_setgroups(int setlen, gid_t *gidset)
778 if (setlen < 0 || setlen > groups_max()) {
784 * Broken case. We need to allocate a
785 * GID_T array of size setlen.
788 if((group_list = SMB_MALLOC_ARRAY(GID_T, setlen)) == NULL) {
789 DEBUG(0,("sys_setgroups: Malloc fail.\n"));
793 for(i = 0; i < setlen; i++)
794 group_list[i] = (GID_T) gidset[i];
796 if(setgroups(setlen, group_list) != 0) {
797 int saved_errno = errno;
798 SAFE_FREE(group_list);
803 SAFE_FREE(group_list);
807 #endif /* HAVE_BROKEN_GETGROUPS */
809 /* This is a list of systems that require the first GID passed to setgroups(2)
810 * to be the effective GID. If your system is one of these, add it here.
812 #if defined (FREEBSD) || defined (DARWINOS)
813 #define USE_BSD_SETGROUPS
816 #if defined(USE_BSD_SETGROUPS)
817 /* Depending on the particular BSD implementation, the first GID that is
818 * passed to setgroups(2) will either be ignored or will set the credential's
819 * effective GID. In either case, the right thing to do is to guarantee that
820 * gidset[0] is the effective GID.
822 static int sys_bsd_setgroups(gid_t primary_gid, int setlen, const gid_t *gidset)
824 gid_t *new_gidset = NULL;
828 /* setgroups(2) will fail with EINVAL if we pass too many groups. */
831 /* No group list, just make sure we are setting the efective GID. */
833 return setgroups(1, &primary_gid);
836 /* If the primary gid is not the first array element, grow the array
837 * and insert it at the front.
839 if (gidset[0] != primary_gid) {
840 new_gidset = SMB_MALLOC_ARRAY(gid_t, setlen + 1);
841 if (new_gidset == NULL) {
845 memcpy(new_gidset + 1, gidset, (setlen * sizeof(gid_t)));
846 new_gidset[0] = primary_gid;
851 DEBUG(3, ("forced to truncate group list from %d to %d\n",
856 #if defined(HAVE_BROKEN_GETGROUPS)
857 ret = sys_broken_setgroups(setlen, new_gidset ? new_gidset : gidset);
859 ret = setgroups(setlen, new_gidset ? new_gidset : gidset);
864 SAFE_FREE(new_gidset);
871 #endif /* USE_BSD_SETGROUPS */
873 /**************************************************************************
874 Wrapper for getgroups. Deals with broken (int) case.
875 ****************************************************************************/
877 int sys_getgroups(int setlen, gid_t *gidset)
879 #if defined(HAVE_BROKEN_GETGROUPS)
880 return sys_broken_getgroups(setlen, gidset);
882 return getgroups(setlen, gidset);
886 /**************************************************************************
887 Wrapper for setgroups. Deals with broken (int) case and BSD case.
888 ****************************************************************************/
890 int sys_setgroups(gid_t UNUSED(primary_gid), int setlen, gid_t *gidset)
892 #if !defined(HAVE_SETGROUPS)
895 #endif /* HAVE_SETGROUPS */
897 #if defined(USE_BSD_SETGROUPS)
898 return sys_bsd_setgroups(primary_gid, setlen, gidset);
899 #elif defined(HAVE_BROKEN_GETGROUPS)
900 return sys_broken_setgroups(setlen, gidset);
902 return setgroups(setlen, gidset);
906 /**************************************************************************
907 Wrappers for setpwent(), getpwent() and endpwent()
908 ****************************************************************************/
910 void sys_setpwent(void)
915 struct passwd *sys_getpwent(void)
920 void sys_endpwent(void)
925 /**************************************************************************
926 Wrappers for getpwnam(), getpwuid(), getgrnam(), getgrgid()
927 ****************************************************************************/
930 struct passwd *sys_getpwnam(const char *name)
932 return getpwnam(name);
935 struct passwd *sys_getpwuid(uid_t uid)
937 return getpwuid(uid);
940 struct group *sys_getgrnam(const char *name)
942 return getgrnam(name);
945 struct group *sys_getgrgid(gid_t gid)
947 return getgrgid(gid);
950 /**************************************************************************
951 Extract a command into an arg list.
952 ****************************************************************************/
954 static char **extract_args(TALLOC_CTX *mem_ctx, const char *command)
963 if (!(trunc_cmd = talloc_strdup(mem_ctx, command))) {
964 DEBUG(0, ("talloc failed\n"));
968 if(!(ptr = strtok_r(trunc_cmd, " \t", &saveptr))) {
969 TALLOC_FREE(trunc_cmd);
978 for( argcl = 1; ptr; ptr = strtok_r(NULL, " \t", &saveptr))
981 TALLOC_FREE(trunc_cmd);
983 if (!(argl = TALLOC_ARRAY(mem_ctx, char *, argcl + 1))) {
988 * Now do the extraction.
991 if (!(trunc_cmd = talloc_strdup(mem_ctx, command))) {
995 ptr = strtok_r(trunc_cmd, " \t", &saveptr);
998 if (!(argl[i++] = talloc_strdup(argl, ptr))) {
1002 while((ptr = strtok_r(NULL, " \t", &saveptr)) != NULL) {
1004 if (!(argl[i++] = talloc_strdup(argl, ptr))) {
1013 DEBUG(0, ("talloc failed\n"));
1014 TALLOC_FREE(trunc_cmd);
1020 /**************************************************************************
1021 Wrapper for fork. Ensures that mypid is reset. Used so we can write
1022 a sys_getpid() that only does a system call *once*.
1023 ****************************************************************************/
1025 static pid_t mypid = (pid_t)-1;
1027 pid_t sys_fork(void)
1029 pid_t forkret = fork();
1031 if (forkret == (pid_t)0) /* Child - reset mypid so sys_getpid does a system call. */
1037 /**************************************************************************
1038 Wrapper for getpid. Ensures we only do a system call *once*.
1039 ****************************************************************************/
1041 pid_t sys_getpid(void)
1043 if (mypid == (pid_t)-1)
1049 /**************************************************************************
1050 Wrapper for popen. Safer as it doesn't search a path.
1051 Modified from the glibc sources.
1052 modified by tridge to return a file descriptor. We must kick our FILE* habit
1053 ****************************************************************************/
1055 typedef struct _popen_list
1059 struct _popen_list *next;
1062 static popen_list *popen_chain;
1064 int sys_popen(const char *command)
1066 int parent_end, child_end;
1068 popen_list *entry = NULL;
1071 if (pipe(pipe_fds) < 0)
1074 parent_end = pipe_fds[0];
1075 child_end = pipe_fds[1];
1082 if((entry = SMB_MALLOC_P(popen_list)) == NULL)
1085 ZERO_STRUCTP(entry);
1088 * Extract the command and args into a NULL terminated array.
1091 if(!(argl = extract_args(NULL, command)))
1094 entry->child_pid = sys_fork();
1096 if (entry->child_pid == -1) {
1100 if (entry->child_pid == 0) {
1106 int child_std_end = STDOUT_FILENO;
1110 if (child_end != child_std_end) {
1111 dup2 (child_end, child_std_end);
1116 * POSIX.2: "popen() shall ensure that any streams from previous
1117 * popen() calls that remain open in the parent process are closed
1118 * in the new child process."
1121 for (p = popen_chain; p; p = p->next)
1124 execv(argl[0], argl);
1135 /* Link into popen_chain. */
1136 entry->next = popen_chain;
1137 popen_chain = entry;
1138 entry->fd = parent_end;
1151 /**************************************************************************
1152 Wrapper for pclose. Modified from the glibc sources.
1153 ****************************************************************************/
1155 int sys_pclose(int fd)
1158 popen_list **ptr = &popen_chain;
1159 popen_list *entry = NULL;
1163 /* Unlink from popen_chain. */
1164 for ( ; *ptr != NULL; ptr = &(*ptr)->next) {
1165 if ((*ptr)->fd == fd) {
1167 *ptr = (*ptr)->next;
1173 if (status < 0 || close(entry->fd) < 0)
1177 * As Samba is catching and eating child process
1178 * exits we don't really care about the child exit
1179 * code, a -1 with errno = ECHILD will do fine for us.
1183 wait_pid = sys_waitpid (entry->child_pid, &wstatus, 0);
1184 } while (wait_pid == -1 && errno == EINTR);
1193 /**************************************************************************
1194 Wrappers for dlopen, dlsym, dlclose.
1195 ****************************************************************************/
1197 void *sys_dlopen(const char *name, int flags)
1199 #if defined(HAVE_DLOPEN)
1200 return dlopen(name, flags);
1206 void *sys_dlsym(void *handle, const char *symbol)
1208 #if defined(HAVE_DLSYM)
1209 return dlsym(handle, symbol);
1215 int sys_dlclose (void *handle)
1217 #if defined(HAVE_DLCLOSE)
1218 return dlclose(handle);
1224 const char *sys_dlerror(void)
1226 #if defined(HAVE_DLERROR)
1233 int sys_dup2(int oldfd, int newfd)
1235 #if defined(HAVE_DUP2)
1236 return dup2(oldfd, newfd);
1243 /**************************************************************************
1244 Wrapper for Admin Logs.
1245 ****************************************************************************/
1247 void sys_adminlog(int priority, const char *format_str, ...)
1251 char *msgbuf = NULL;
1253 va_start( ap, format_str );
1254 ret = vasprintf( &msgbuf, format_str, ap );
1260 #if defined(HAVE_SYSLOG)
1261 syslog( priority, "%s", msgbuf );
1263 DEBUG(0,("%s", msgbuf ));
1268 /******** Solaris EA helper function prototypes ********/
1269 #ifdef HAVE_ATTROPEN
1270 #define SOLARIS_ATTRMODE S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP
1271 static int solaris_write_xattr(int attrfd, const char *value, size_t size);
1272 static ssize_t solaris_read_xattr(int attrfd, void *value, size_t size);
1273 static ssize_t solaris_list_xattr(int attrdirfd, char *list, size_t size);
1274 static int solaris_unlinkat(int attrdirfd, const char *name);
1275 static int solaris_attropen(const char *path, const char *attrpath, int oflag, mode_t mode);
1276 static int solaris_openat(int fildes, const char *path, int oflag, mode_t mode);
1279 /**************************************************************************
1280 Wrappers for extented attribute calls. Based on the Linux package with
1281 support for IRIX and (Net|Free)BSD also. Expand as other systems have them.
1282 ****************************************************************************/
1284 ssize_t sys_getxattr (const char *path, const char *name, void *value, size_t size)
1286 #if defined(HAVE_GETXATTR)
1287 #ifndef XATTR_ADD_OPT
1288 return getxattr(path, name, value, size);
1291 return getxattr(path, name, value, size, 0, options);
1293 #elif defined(HAVE_GETEA)
1294 return getea(path, name, value, size);
1295 #elif defined(HAVE_EXTATTR_GET_FILE)
1298 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
1299 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
1300 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
1302 * The BSD implementation has a nasty habit of silently truncating
1303 * the returned value to the size of the buffer, so we have to check
1304 * that the buffer is large enough to fit the returned value.
1306 if((retval=extattr_get_file(path, attrnamespace, attrname, NULL, 0)) >= 0) {
1311 if((retval=extattr_get_file(path, attrnamespace, attrname, value, size)) >= 0)
1315 DEBUG(10,("sys_getxattr: extattr_get_file() failed with: %s\n", strerror(errno)));
1317 #elif defined(HAVE_ATTR_GET)
1318 int retval, flags = 0;
1319 int valuelength = (int)size;
1320 char *attrname = strchr(name,'.') + 1;
1322 if (strncmp(name, "system", 6) == 0) flags |= ATTR_ROOT;
1324 retval = attr_get(path, attrname, (char *)value, &valuelength, flags);
1326 return retval ? retval : valuelength;
1327 #elif defined(HAVE_ATTROPEN)
1329 int attrfd = solaris_attropen(path, name, O_RDONLY, 0);
1331 ret = solaris_read_xattr(attrfd, value, size);
1341 ssize_t sys_lgetxattr (const char *path, const char *name, void *value, size_t size)
1343 #if defined(HAVE_LGETXATTR)
1344 return lgetxattr(path, name, value, size);
1345 #elif defined(HAVE_GETXATTR) && defined(XATTR_ADD_OPT)
1346 int options = XATTR_NOFOLLOW;
1347 return getxattr(path, name, value, size, 0, options);
1348 #elif defined(HAVE_LGETEA)
1349 return lgetea(path, name, value, size);
1350 #elif defined(HAVE_EXTATTR_GET_LINK)
1353 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
1354 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
1355 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
1357 if((retval=extattr_get_link(path, attrnamespace, attrname, NULL, 0)) >= 0) {
1362 if((retval=extattr_get_link(path, attrnamespace, attrname, value, size)) >= 0)
1366 DEBUG(10,("sys_lgetxattr: extattr_get_link() failed with: %s\n", strerror(errno)));
1368 #elif defined(HAVE_ATTR_GET)
1369 int retval, flags = ATTR_DONTFOLLOW;
1370 int valuelength = (int)size;
1371 char *attrname = strchr(name,'.') + 1;
1373 if (strncmp(name, "system", 6) == 0) flags |= ATTR_ROOT;
1375 retval = attr_get(path, attrname, (char *)value, &valuelength, flags);
1377 return retval ? retval : valuelength;
1378 #elif defined(HAVE_ATTROPEN)
1380 int attrfd = solaris_attropen(path, name, O_RDONLY|AT_SYMLINK_NOFOLLOW, 0);
1382 ret = solaris_read_xattr(attrfd, value, size);
1392 ssize_t sys_fgetxattr (int filedes, const char *name, void *value, size_t size)
1394 #if defined(HAVE_FGETXATTR)
1395 #ifndef XATTR_ADD_OPT
1396 return fgetxattr(filedes, name, value, size);
1399 return fgetxattr(filedes, name, value, size, 0, options);
1401 #elif defined(HAVE_FGETEA)
1402 return fgetea(filedes, name, value, size);
1403 #elif defined(HAVE_EXTATTR_GET_FD)
1406 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
1407 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
1408 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
1410 if((retval=extattr_get_fd(filedes, attrnamespace, attrname, NULL, 0)) >= 0) {
1415 if((retval=extattr_get_fd(filedes, attrnamespace, attrname, value, size)) >= 0)
1419 DEBUG(10,("sys_fgetxattr: extattr_get_fd() failed with: %s\n", strerror(errno)));
1421 #elif defined(HAVE_ATTR_GETF)
1422 int retval, flags = 0;
1423 int valuelength = (int)size;
1424 char *attrname = strchr(name,'.') + 1;
1426 if (strncmp(name, "system", 6) == 0) flags |= ATTR_ROOT;
1428 retval = attr_getf(filedes, attrname, (char *)value, &valuelength, flags);
1430 return retval ? retval : valuelength;
1431 #elif defined(HAVE_ATTROPEN)
1433 int attrfd = solaris_openat(filedes, name, O_RDONLY|O_XATTR, 0);
1435 ret = solaris_read_xattr(attrfd, value, size);
1445 #if defined(HAVE_EXTATTR_LIST_FILE)
1447 #define EXTATTR_PREFIX(s) (s), (sizeof((s))-1)
1455 { EXTATTR_NAMESPACE_SYSTEM, EXTATTR_PREFIX("system.") },
1456 { EXTATTR_NAMESPACE_USER, EXTATTR_PREFIX("user.") },
1464 static ssize_t bsd_attr_list (int type, extattr_arg arg, char *list, size_t size)
1466 ssize_t list_size, total_size = 0;
1469 /* Iterate through extattr(2) namespaces */
1470 for(t = 0; t < (sizeof(extattr)/sizeof(extattr[0])); t++) {
1472 #if defined(HAVE_EXTATTR_LIST_FILE)
1474 list_size = extattr_list_file(arg.path, extattr[t].space, list, size);
1477 #if defined(HAVE_EXTATTR_LIST_LINK)
1479 list_size = extattr_list_link(arg.path, extattr[t].space, list, size);
1482 #if defined(HAVE_EXTATTR_LIST_FD)
1484 list_size = extattr_list_fd(arg.filedes, extattr[t].space, list, size);
1491 /* Some error happend. Errno should be set by the previous call */
1497 /* XXX: Call with an empty buffer may be used to calculate
1498 necessary buffer size. Unfortunately, we can't say, how
1499 many attributes were returned, so here is the potential
1500 problem with the emulation.
1503 /* Take the worse case of one char attribute names -
1504 two bytes per name plus one more for sanity.
1506 total_size += list_size + (list_size/2 + 1)*extattr[t].len;
1509 /* Count necessary offset to fit namespace prefixes */
1511 for(i = 0; i < list_size; i += list[i] + 1)
1512 len += extattr[t].len;
1514 total_size += list_size + len;
1515 /* Buffer is too small to fit the results */
1516 if(total_size > size) {
1520 /* Shift results back, so we can prepend prefixes */
1521 buf = memmove(list + len, list, list_size);
1523 for(i = 0; i < list_size; i += len + 1) {
1525 strncpy(list, extattr[t].name, extattr[t].len + 1);
1526 list += extattr[t].len;
1527 strncpy(list, buf + i + 1, len);
1538 #if defined(HAVE_ATTR_LIST) && defined(HAVE_SYS_ATTRIBUTES_H)
1539 static char attr_buffer[ATTR_MAX_VALUELEN];
1541 static ssize_t irix_attr_list(const char *path, int filedes, char *list, size_t size, int flags)
1543 int retval = 0, index;
1544 attrlist_cursor_t *cursor = 0;
1546 attrlist_t * al = (attrlist_t *)attr_buffer;
1548 size_t ent_size, left = size;
1553 retval = attr_listf(filedes, attr_buffer, ATTR_MAX_VALUELEN, flags, cursor);
1555 retval = attr_list(path, attr_buffer, ATTR_MAX_VALUELEN, flags, cursor);
1557 for (index = 0; index < al->al_count; index++) {
1558 ae = ATTR_ENTRY(attr_buffer, index);
1559 ent_size = strlen(ae->a_name) + sizeof("user.");
1560 if (left >= ent_size) {
1561 strncpy(bp, "user.", sizeof("user."));
1562 strncat(bp, ae->a_name, ent_size - sizeof("user."));
1570 total_size += ent_size;
1572 if (al->al_more == 0) break;
1579 retval = attr_listf(filedes, attr_buffer, ATTR_MAX_VALUELEN, flags, cursor);
1581 retval = attr_list(path, attr_buffer, ATTR_MAX_VALUELEN, flags, cursor);
1583 for (index = 0; index < al->al_count; index++) {
1584 ae = ATTR_ENTRY(attr_buffer, index);
1585 ent_size = strlen(ae->a_name) + sizeof("system.");
1586 if (left >= ent_size) {
1587 strncpy(bp, "system.", sizeof("system."));
1588 strncat(bp, ae->a_name, ent_size - sizeof("system."));
1596 total_size += ent_size;
1598 if (al->al_more == 0) break;
1601 return (ssize_t)(retval ? retval : total_size);
1606 ssize_t sys_listxattr (const char *path, char *list, size_t size)
1608 #if defined(HAVE_LISTXATTR)
1609 #ifndef XATTR_ADD_OPT
1610 return listxattr(path, list, size);
1613 return listxattr(path, list, size, options);
1615 #elif defined(HAVE_LISTEA)
1616 return listea(path, list, size);
1617 #elif defined(HAVE_EXTATTR_LIST_FILE)
1620 return bsd_attr_list(0, arg, list, size);
1621 #elif defined(HAVE_ATTR_LIST) && defined(HAVE_SYS_ATTRIBUTES_H)
1622 return irix_attr_list(path, 0, list, size, 0);
1623 #elif defined(HAVE_ATTROPEN)
1625 int attrdirfd = solaris_attropen(path, ".", O_RDONLY, 0);
1626 if (attrdirfd >= 0) {
1627 ret = solaris_list_xattr(attrdirfd, list, size);
1637 ssize_t sys_llistxattr (const char *path, char *list, size_t size)
1639 #if defined(HAVE_LLISTXATTR)
1640 return llistxattr(path, list, size);
1641 #elif defined(HAVE_LISTXATTR) && defined(XATTR_ADD_OPT)
1642 int options = XATTR_NOFOLLOW;
1643 return listxattr(path, list, size, options);
1644 #elif defined(HAVE_LLISTEA)
1645 return llistea(path, list, size);
1646 #elif defined(HAVE_EXTATTR_LIST_LINK)
1649 return bsd_attr_list(1, arg, list, size);
1650 #elif defined(HAVE_ATTR_LIST) && defined(HAVE_SYS_ATTRIBUTES_H)
1651 return irix_attr_list(path, 0, list, size, ATTR_DONTFOLLOW);
1652 #elif defined(HAVE_ATTROPEN)
1654 int attrdirfd = solaris_attropen(path, ".", O_RDONLY|AT_SYMLINK_NOFOLLOW, 0);
1655 if (attrdirfd >= 0) {
1656 ret = solaris_list_xattr(attrdirfd, list, size);
1666 ssize_t sys_flistxattr (int filedes, char *list, size_t size)
1668 #if defined(HAVE_FLISTXATTR)
1669 #ifndef XATTR_ADD_OPT
1670 return flistxattr(filedes, list, size);
1673 return flistxattr(filedes, list, size, options);
1675 #elif defined(HAVE_FLISTEA)
1676 return flistea(filedes, list, size);
1677 #elif defined(HAVE_EXTATTR_LIST_FD)
1679 arg.filedes = filedes;
1680 return bsd_attr_list(2, arg, list, size);
1681 #elif defined(HAVE_ATTR_LISTF)
1682 return irix_attr_list(NULL, filedes, list, size, 0);
1683 #elif defined(HAVE_ATTROPEN)
1685 int attrdirfd = solaris_openat(filedes, ".", O_RDONLY|O_XATTR, 0);
1686 if (attrdirfd >= 0) {
1687 ret = solaris_list_xattr(attrdirfd, list, size);
1697 int sys_removexattr (const char *path, const char *name)
1699 #if defined(HAVE_REMOVEXATTR)
1700 #ifndef XATTR_ADD_OPT
1701 return removexattr(path, name);
1704 return removexattr(path, name, options);
1706 #elif defined(HAVE_REMOVEEA)
1707 return removeea(path, name);
1708 #elif defined(HAVE_EXTATTR_DELETE_FILE)
1710 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
1711 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
1712 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
1714 return extattr_delete_file(path, attrnamespace, attrname);
1715 #elif defined(HAVE_ATTR_REMOVE)
1717 char *attrname = strchr(name,'.') + 1;
1719 if (strncmp(name, "system", 6) == 0) flags |= ATTR_ROOT;
1721 return attr_remove(path, attrname, flags);
1722 #elif defined(HAVE_ATTROPEN)
1724 int attrdirfd = solaris_attropen(path, ".", O_RDONLY, 0);
1725 if (attrdirfd >= 0) {
1726 ret = solaris_unlinkat(attrdirfd, name);
1736 int sys_lremovexattr (const char *path, const char *name)
1738 #if defined(HAVE_LREMOVEXATTR)
1739 return lremovexattr(path, name);
1740 #elif defined(HAVE_REMOVEXATTR) && defined(XATTR_ADD_OPT)
1741 int options = XATTR_NOFOLLOW;
1742 return removexattr(path, name, options);
1743 #elif defined(HAVE_LREMOVEEA)
1744 return lremoveea(path, name);
1745 #elif defined(HAVE_EXTATTR_DELETE_LINK)
1747 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
1748 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
1749 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
1751 return extattr_delete_link(path, attrnamespace, attrname);
1752 #elif defined(HAVE_ATTR_REMOVE)
1753 int flags = ATTR_DONTFOLLOW;
1754 char *attrname = strchr(name,'.') + 1;
1756 if (strncmp(name, "system", 6) == 0) flags |= ATTR_ROOT;
1758 return attr_remove(path, attrname, flags);
1759 #elif defined(HAVE_ATTROPEN)
1761 int attrdirfd = solaris_attropen(path, ".", O_RDONLY|AT_SYMLINK_NOFOLLOW, 0);
1762 if (attrdirfd >= 0) {
1763 ret = solaris_unlinkat(attrdirfd, name);
1773 int sys_fremovexattr (int filedes, const char *name)
1775 #if defined(HAVE_FREMOVEXATTR)
1776 #ifndef XATTR_ADD_OPT
1777 return fremovexattr(filedes, name);
1780 return fremovexattr(filedes, name, options);
1782 #elif defined(HAVE_FREMOVEEA)
1783 return fremoveea(filedes, name);
1784 #elif defined(HAVE_EXTATTR_DELETE_FD)
1786 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
1787 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
1788 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
1790 return extattr_delete_fd(filedes, attrnamespace, attrname);
1791 #elif defined(HAVE_ATTR_REMOVEF)
1793 char *attrname = strchr(name,'.') + 1;
1795 if (strncmp(name, "system", 6) == 0) flags |= ATTR_ROOT;
1797 return attr_removef(filedes, attrname, flags);
1798 #elif defined(HAVE_ATTROPEN)
1800 int attrdirfd = solaris_openat(filedes, ".", O_RDONLY|O_XATTR, 0);
1801 if (attrdirfd >= 0) {
1802 ret = solaris_unlinkat(attrdirfd, name);
1812 int sys_setxattr (const char *path, const char *name, const void *value, size_t size, int flags)
1814 #if defined(HAVE_SETXATTR)
1815 #ifndef XATTR_ADD_OPT
1816 return setxattr(path, name, value, size, flags);
1819 return setxattr(path, name, value, size, 0, options);
1821 #elif defined(HAVE_SETEA)
1822 return setea(path, name, value, size, flags);
1823 #elif defined(HAVE_EXTATTR_SET_FILE)
1826 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
1827 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
1828 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
1830 /* Check attribute existence */
1831 retval = extattr_get_file(path, attrnamespace, attrname, NULL, 0);
1833 /* REPLACE attribute, that doesn't exist */
1834 if (flags & XATTR_REPLACE && errno == ENOATTR) {
1838 /* Ignore other errors */
1841 /* CREATE attribute, that already exists */
1842 if (flags & XATTR_CREATE) {
1848 retval = extattr_set_file(path, attrnamespace, attrname, value, size);
1849 return (retval < 0) ? -1 : 0;
1850 #elif defined(HAVE_ATTR_SET)
1852 char *attrname = strchr(name,'.') + 1;
1854 if (strncmp(name, "system", 6) == 0) myflags |= ATTR_ROOT;
1855 if (flags & XATTR_CREATE) myflags |= ATTR_CREATE;
1856 if (flags & XATTR_REPLACE) myflags |= ATTR_REPLACE;
1858 return attr_set(path, attrname, (const char *)value, size, myflags);
1859 #elif defined(HAVE_ATTROPEN)
1861 int myflags = O_RDWR;
1863 if (flags & XATTR_CREATE) myflags |= O_EXCL;
1864 if (!(flags & XATTR_REPLACE)) myflags |= O_CREAT;
1865 attrfd = solaris_attropen(path, name, myflags, (mode_t) SOLARIS_ATTRMODE);
1867 ret = solaris_write_xattr(attrfd, value, size);
1877 int sys_lsetxattr (const char *path, const char *name, const void *value, size_t size, int flags)
1879 #if defined(HAVE_LSETXATTR)
1880 return lsetxattr(path, name, value, size, flags);
1881 #elif defined(HAVE_SETXATTR) && defined(XATTR_ADD_OPT)
1882 int options = XATTR_NOFOLLOW;
1883 return setxattr(path, name, value, size, 0, options);
1884 #elif defined(LSETEA)
1885 return lsetea(path, name, value, size, flags);
1886 #elif defined(HAVE_EXTATTR_SET_LINK)
1889 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
1890 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
1891 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
1893 /* Check attribute existence */
1894 retval = extattr_get_link(path, attrnamespace, attrname, NULL, 0);
1896 /* REPLACE attribute, that doesn't exist */
1897 if (flags & XATTR_REPLACE && errno == ENOATTR) {
1901 /* Ignore other errors */
1904 /* CREATE attribute, that already exists */
1905 if (flags & XATTR_CREATE) {
1912 retval = extattr_set_link(path, attrnamespace, attrname, value, size);
1913 return (retval < 0) ? -1 : 0;
1914 #elif defined(HAVE_ATTR_SET)
1915 int myflags = ATTR_DONTFOLLOW;
1916 char *attrname = strchr(name,'.') + 1;
1918 if (strncmp(name, "system", 6) == 0) myflags |= ATTR_ROOT;
1919 if (flags & XATTR_CREATE) myflags |= ATTR_CREATE;
1920 if (flags & XATTR_REPLACE) myflags |= ATTR_REPLACE;
1922 return attr_set(path, attrname, (const char *)value, size, myflags);
1923 #elif defined(HAVE_ATTROPEN)
1925 int myflags = O_RDWR | AT_SYMLINK_NOFOLLOW;
1927 if (flags & XATTR_CREATE) myflags |= O_EXCL;
1928 if (!(flags & XATTR_REPLACE)) myflags |= O_CREAT;
1929 attrfd = solaris_attropen(path, name, myflags, (mode_t) SOLARIS_ATTRMODE);
1931 ret = solaris_write_xattr(attrfd, value, size);
1941 int sys_fsetxattr (int filedes, const char *name, const void *value, size_t size, int flags)
1943 #if defined(HAVE_FSETXATTR)
1944 #ifndef XATTR_ADD_OPT
1945 return fsetxattr(filedes, name, value, size, flags);
1948 return fsetxattr(filedes, name, value, size, 0, options);
1950 #elif defined(HAVE_FSETEA)
1951 return fsetea(filedes, name, value, size, flags);
1952 #elif defined(HAVE_EXTATTR_SET_FD)
1955 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
1956 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
1957 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
1959 /* Check attribute existence */
1960 retval = extattr_get_fd(filedes, attrnamespace, attrname, NULL, 0);
1962 /* REPLACE attribute, that doesn't exist */
1963 if (flags & XATTR_REPLACE && errno == ENOATTR) {
1967 /* Ignore other errors */
1970 /* CREATE attribute, that already exists */
1971 if (flags & XATTR_CREATE) {
1977 retval = extattr_set_fd(filedes, attrnamespace, attrname, value, size);
1978 return (retval < 0) ? -1 : 0;
1979 #elif defined(HAVE_ATTR_SETF)
1981 char *attrname = strchr(name,'.') + 1;
1983 if (strncmp(name, "system", 6) == 0) myflags |= ATTR_ROOT;
1984 if (flags & XATTR_CREATE) myflags |= ATTR_CREATE;
1985 if (flags & XATTR_REPLACE) myflags |= ATTR_REPLACE;
1987 return attr_setf(filedes, attrname, (const char *)value, size, myflags);
1988 #elif defined(HAVE_ATTROPEN)
1990 int myflags = O_RDWR | O_XATTR;
1992 if (flags & XATTR_CREATE) myflags |= O_EXCL;
1993 if (!(flags & XATTR_REPLACE)) myflags |= O_CREAT;
1994 attrfd = solaris_openat(filedes, name, myflags, (mode_t) SOLARIS_ATTRMODE);
1996 ret = solaris_write_xattr(attrfd, value, size);
2006 /**************************************************************************
2007 helper functions for Solaris' EA support
2008 ****************************************************************************/
2009 #ifdef HAVE_ATTROPEN
2010 static ssize_t solaris_read_xattr(int attrfd, void *value, size_t size)
2014 if (fstat(attrfd, &sbuf) == -1) {
2019 /* This is to return the current size of the named extended attribute */
2021 return sbuf.st_size;
2024 /* check size and read xattr */
2025 if (sbuf.st_size > size) {
2030 return read(attrfd, value, sbuf.st_size);
2033 static ssize_t solaris_list_xattr(int attrdirfd, char *list, size_t size)
2039 int newfd = dup(attrdirfd);
2040 /* CAUTION: The originating file descriptor should not be
2041 used again following the call to fdopendir().
2042 For that reason we dup() the file descriptor
2043 here to make things more clear. */
2044 dirp = fdopendir(newfd);
2046 while ((de = readdir(dirp))) {
2047 size_t listlen = strlen(de->d_name);
2048 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
2049 /* we don't want "." and ".." here: */
2050 DEBUG(10,("skipped EA %s\n",de->d_name));
2055 /* return the current size of the list of extended attribute names*/
2058 /* check size and copy entrieѕ + nul into list. */
2059 if ((len + listlen + 1) > size) {
2064 safe_strcpy(list + len, de->d_name, listlen);
2072 if (closedir(dirp) == -1) {
2073 DEBUG(0,("closedir dirp failed: %s\n",strerror(errno)));
2079 static int solaris_unlinkat(int attrdirfd, const char *name)
2081 if (unlinkat(attrdirfd, name, 0) == -1) {
2082 if (errno == ENOENT) {
2090 static int solaris_attropen(const char *path, const char *attrpath, int oflag, mode_t mode)
2092 int filedes = attropen(path, attrpath, oflag, mode);
2093 if (filedes == -1) {
2094 DEBUG(10,("attropen FAILED: path: %s, name: %s, errno: %s\n",path,attrpath,strerror(errno)));
2095 if (errno == EINVAL) {
2104 static int solaris_openat(int fildes, const char *path, int oflag, mode_t mode)
2106 int filedes = openat(fildes, path, oflag, mode);
2107 if (filedes == -1) {
2108 DEBUG(10,("openat FAILED: fd: %s, path: %s, errno: %s\n",filedes,path,strerror(errno)));
2109 if (errno == EINVAL) {
2118 static int solaris_write_xattr(int attrfd, const char *value, size_t size)
2120 if ((ftruncate(attrfd, 0) == 0) && (write(attrfd, value, size) == size)) {
2123 DEBUG(10,("solaris_write_xattr FAILED!\n"));
2127 #endif /*HAVE_ATTROPEN*/
2130 /****************************************************************************
2131 Return the major devicenumber for UNIX extensions.
2132 ****************************************************************************/
2134 uint32 unix_dev_major(SMB_DEV_T dev)
2136 #if defined(HAVE_DEVICE_MAJOR_FN)
2137 return (uint32)major(dev);
2139 return (uint32)(dev >> 8);
2143 /****************************************************************************
2144 Return the minor devicenumber for UNIX extensions.
2145 ****************************************************************************/
2147 uint32 unix_dev_minor(SMB_DEV_T dev)
2149 #if defined(HAVE_DEVICE_MINOR_FN)
2150 return (uint32)minor(dev);
2152 return (uint32)(dev & 0xff);
2156 #if defined(WITH_AIO)
2158 /*******************************************************************
2159 An aio_read wrapper that will deal with 64-bit sizes.
2160 ********************************************************************/
2162 int sys_aio_read(SMB_STRUCT_AIOCB *aiocb)
2164 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_READ64)
2165 return aio_read64(aiocb);
2166 #elif defined(HAVE_AIO_READ)
2167 return aio_read(aiocb);
2174 /*******************************************************************
2175 An aio_write wrapper that will deal with 64-bit sizes.
2176 ********************************************************************/
2178 int sys_aio_write(SMB_STRUCT_AIOCB *aiocb)
2180 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_WRITE64)
2181 return aio_write64(aiocb);
2182 #elif defined(HAVE_AIO_WRITE)
2183 return aio_write(aiocb);
2190 /*******************************************************************
2191 An aio_return wrapper that will deal with 64-bit sizes.
2192 ********************************************************************/
2194 ssize_t sys_aio_return(SMB_STRUCT_AIOCB *aiocb)
2196 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_RETURN64)
2197 return aio_return64(aiocb);
2198 #elif defined(HAVE_AIO_RETURN)
2199 return aio_return(aiocb);
2206 /*******************************************************************
2207 An aio_cancel wrapper that will deal with 64-bit sizes.
2208 ********************************************************************/
2210 int sys_aio_cancel(int fd, SMB_STRUCT_AIOCB *aiocb)
2212 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_CANCEL64)
2213 return aio_cancel64(fd, aiocb);
2214 #elif defined(HAVE_AIO_CANCEL)
2215 return aio_cancel(fd, aiocb);
2222 /*******************************************************************
2223 An aio_error wrapper that will deal with 64-bit sizes.
2224 ********************************************************************/
2226 int sys_aio_error(const SMB_STRUCT_AIOCB *aiocb)
2228 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_ERROR64)
2229 return aio_error64(aiocb);
2230 #elif defined(HAVE_AIO_ERROR)
2231 return aio_error(aiocb);
2238 /*******************************************************************
2239 An aio_fsync wrapper that will deal with 64-bit sizes.
2240 ********************************************************************/
2242 int sys_aio_fsync(int op, SMB_STRUCT_AIOCB *aiocb)
2244 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_FSYNC64)
2245 return aio_fsync64(op, aiocb);
2246 #elif defined(HAVE_AIO_FSYNC)
2247 return aio_fsync(op, aiocb);
2254 /*******************************************************************
2255 An aio_fsync wrapper that will deal with 64-bit sizes.
2256 ********************************************************************/
2258 int sys_aio_suspend(const SMB_STRUCT_AIOCB * const cblist[], int n, const struct timespec *timeout)
2260 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_SUSPEND64)
2261 return aio_suspend64(cblist, n, timeout);
2262 #elif defined(HAVE_AIO_FSYNC)
2263 return aio_suspend(cblist, n, timeout);
2269 #else /* !WITH_AIO */
2271 int sys_aio_read(SMB_STRUCT_AIOCB *aiocb)
2277 int sys_aio_write(SMB_STRUCT_AIOCB *aiocb)
2283 ssize_t sys_aio_return(SMB_STRUCT_AIOCB *aiocb)
2289 int sys_aio_cancel(int fd, SMB_STRUCT_AIOCB *aiocb)
2295 int sys_aio_error(const SMB_STRUCT_AIOCB *aiocb)
2301 int sys_aio_fsync(int op, SMB_STRUCT_AIOCB *aiocb)
2307 int sys_aio_suspend(const SMB_STRUCT_AIOCB * const cblist[], int n, const struct timespec *timeout)
2312 #endif /* WITH_AIO */
2314 int sys_getpeereid( int s, uid_t *uid)
2316 #if defined(HAVE_PEERCRED)
2318 socklen_t cred_len = sizeof(struct ucred);
2321 ret = getsockopt(s, SOL_SOCKET, SO_PEERCRED, (void *)&cred, &cred_len);
2326 if (cred_len != sizeof(struct ucred)) {
2339 int sys_getnameinfo(const struct sockaddr *psa,
2348 * For Solaris we must make sure salen is the
2349 * correct length for the incoming sa_family.
2352 if (salen == sizeof(struct sockaddr_storage)) {
2353 salen = sizeof(struct sockaddr_in);
2354 #if defined(HAVE_IPV6)
2355 if (psa->sa_family == AF_INET6) {
2356 salen = sizeof(struct sockaddr_in6);
2360 return getnameinfo(psa, salen, host, hostlen, service, servlen, flags);
2363 int sys_connect(int fd, const struct sockaddr * addr)
2365 socklen_t salen = -1;
2367 if (addr->sa_family == AF_INET) {
2368 salen = sizeof(struct sockaddr_in);
2369 } else if (addr->sa_family == AF_UNIX) {
2370 salen = sizeof(struct sockaddr_un);
2372 #if defined(HAVE_IPV6)
2373 else if (addr->sa_family == AF_INET6) {
2374 salen = sizeof(struct sockaddr_in6);
2378 return connect(fd, addr, salen);