2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Jeremy Allison 1998-2005
6 Copyright (C) Timur Bakeyev 2005
7 Copyright (C) Bjoern Jacke 2006-2007
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #ifdef HAVE_SYS_PRCTL_H
26 #include <sys/prctl.h>
30 The idea is that this file will eventually have wrappers around all
31 important system calls in samba. The aims are:
33 - to enable easier porting by putting OS dependent stuff in here
35 - to allow for hooks into other "pseudo-filesystems"
37 - to allow easier integration of things like the japanese extensions
39 - to support the philosophy of Samba to expose the features of
40 the OS within the SMB model. In general whatever file/printer/variable
41 expansions/etc make sense to the OS should be acceptable to Samba.
46 /*******************************************************************
47 A wrapper for memalign
48 ********************************************************************/
50 void *sys_memalign( size_t align, size_t size )
52 #if defined(HAVE_POSIX_MEMALIGN)
54 int ret = posix_memalign( &p, align, size );
59 #elif defined(HAVE_MEMALIGN)
60 return memalign( align, size );
62 /* On *BSD systems memaligns doesn't exist, but memory will
63 * be aligned on allocations of > pagesize. */
64 #if defined(SYSCONF_SC_PAGESIZE)
65 size_t pagesize = (size_t)sysconf(_SC_PAGESIZE);
66 #elif defined(HAVE_GETPAGESIZE)
67 size_t pagesize = (size_t)getpagesize();
69 size_t pagesize = (size_t)-1;
71 if (pagesize == (size_t)-1) {
72 DEBUG(0,("memalign functionalaity not available on this platform!\n"));
75 if (size < pagesize) {
78 return SMB_MALLOC(size);
82 /*******************************************************************
83 A wrapper for usleep in case we don't have one.
84 ********************************************************************/
86 int sys_usleep(long usecs)
93 * We need this braindamage as the glibc usleep
94 * is not SPEC1170 complient... grumble... JRA.
97 if(usecs < 0 || usecs > 1000000) {
105 #else /* HAVE_USLEEP */
107 * Fake it with select...
110 tval.tv_usec = usecs/1000;
111 select(0,NULL,NULL,NULL,&tval);
113 #endif /* HAVE_USLEEP */
116 /*******************************************************************
117 A read wrapper that will deal with EINTR.
118 ********************************************************************/
120 ssize_t sys_read(int fd, void *buf, size_t count)
125 ret = read(fd, buf, count);
126 } while (ret == -1 && errno == EINTR);
130 /*******************************************************************
131 A write wrapper that will deal with EINTR.
132 ********************************************************************/
134 ssize_t sys_write(int fd, const void *buf, size_t count)
139 ret = write(fd, buf, count);
140 } while (ret == -1 && errno == EINTR);
144 /*******************************************************************
145 A pread wrapper that will deal with EINTR and 64-bit file offsets.
146 ********************************************************************/
148 #if defined(HAVE_PREAD) || defined(HAVE_PREAD64)
149 ssize_t sys_pread(int fd, void *buf, size_t count, SMB_OFF_T off)
154 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_PREAD64)
155 ret = pread64(fd, buf, count, off);
157 ret = pread(fd, buf, count, off);
159 } while (ret == -1 && errno == EINTR);
164 /*******************************************************************
165 A write wrapper that will deal with EINTR and 64-bit file offsets.
166 ********************************************************************/
168 #if defined(HAVE_PWRITE) || defined(HAVE_PWRITE64)
169 ssize_t sys_pwrite(int fd, const void *buf, size_t count, SMB_OFF_T off)
174 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_PWRITE64)
175 ret = pwrite64(fd, buf, count, off);
177 ret = pwrite(fd, buf, count, off);
179 } while (ret == -1 && errno == EINTR);
184 /*******************************************************************
185 A send wrapper that will deal with EINTR.
186 ********************************************************************/
188 ssize_t sys_send(int s, const void *msg, size_t len, int flags)
193 ret = send(s, msg, len, flags);
194 } while (ret == -1 && errno == EINTR);
198 /*******************************************************************
199 A sendto wrapper that will deal with EINTR.
200 ********************************************************************/
202 ssize_t sys_sendto(int s, const void *msg, size_t len, int flags, const struct sockaddr *to, socklen_t tolen)
207 ret = sendto(s, msg, len, flags, to, tolen);
208 } while (ret == -1 && errno == EINTR);
212 /*******************************************************************
213 A recv wrapper that will deal with EINTR.
214 ********************************************************************/
216 ssize_t sys_recv(int fd, void *buf, size_t count, int flags)
221 ret = recv(fd, buf, count, flags);
222 } while (ret == -1 && errno == EINTR);
226 /*******************************************************************
227 A recvfrom wrapper that will deal with EINTR.
228 ********************************************************************/
230 ssize_t sys_recvfrom(int s, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen)
235 ret = recvfrom(s, buf, len, flags, from, fromlen);
236 } while (ret == -1 && errno == EINTR);
240 /*******************************************************************
241 A fcntl wrapper that will deal with EINTR.
242 ********************************************************************/
244 int sys_fcntl_ptr(int fd, int cmd, void *arg)
249 ret = fcntl(fd, cmd, arg);
250 } while (ret == -1 && errno == EINTR);
254 /*******************************************************************
255 A fcntl wrapper that will deal with EINTR.
256 ********************************************************************/
258 int sys_fcntl_long(int fd, int cmd, long arg)
263 ret = fcntl(fd, cmd, arg);
264 } while (ret == -1 && errno == EINTR);
268 /*******************************************************************
269 A stat() wrapper that will deal with 64 bit filesizes.
270 ********************************************************************/
272 int sys_stat(const char *fname,SMB_STRUCT_STAT *sbuf)
275 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_STAT64)
276 ret = stat64(fname, sbuf);
278 ret = stat(fname, sbuf);
280 /* we always want directories to appear zero size */
281 if (ret == 0 && S_ISDIR(sbuf->st_mode)) sbuf->st_size = 0;
285 /*******************************************************************
286 An fstat() wrapper that will deal with 64 bit filesizes.
287 ********************************************************************/
289 int sys_fstat(int fd,SMB_STRUCT_STAT *sbuf)
292 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_FSTAT64)
293 ret = fstat64(fd, sbuf);
295 ret = fstat(fd, sbuf);
297 /* we always want directories to appear zero size */
298 if (ret == 0 && S_ISDIR(sbuf->st_mode)) sbuf->st_size = 0;
302 /*******************************************************************
303 An lstat() wrapper that will deal with 64 bit filesizes.
304 ********************************************************************/
306 int sys_lstat(const char *fname,SMB_STRUCT_STAT *sbuf)
309 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_LSTAT64)
310 ret = lstat64(fname, sbuf);
312 ret = lstat(fname, sbuf);
314 /* we always want directories to appear zero size */
315 if (ret == 0 && S_ISDIR(sbuf->st_mode)) sbuf->st_size = 0;
319 /*******************************************************************
320 An ftruncate() wrapper that will deal with 64 bit filesizes.
321 ********************************************************************/
323 int sys_ftruncate(int fd, SMB_OFF_T offset)
325 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_FTRUNCATE64)
326 return ftruncate64(fd, offset);
328 return ftruncate(fd, offset);
332 /*******************************************************************
333 An lseek() wrapper that will deal with 64 bit filesizes.
334 ********************************************************************/
336 SMB_OFF_T sys_lseek(int fd, SMB_OFF_T offset, int whence)
338 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_LSEEK64)
339 return lseek64(fd, offset, whence);
341 return lseek(fd, offset, whence);
345 /*******************************************************************
346 An fseek() wrapper that will deal with 64 bit filesizes.
347 ********************************************************************/
349 int sys_fseek(FILE *fp, SMB_OFF_T offset, int whence)
351 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(LARGE_SMB_OFF_T) && defined(HAVE_FSEEK64)
352 return fseek64(fp, offset, whence);
353 #elif defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(LARGE_SMB_OFF_T) && defined(HAVE_FSEEKO64)
354 return fseeko64(fp, offset, whence);
356 return fseek(fp, offset, whence);
360 /*******************************************************************
361 An ftell() wrapper that will deal with 64 bit filesizes.
362 ********************************************************************/
364 SMB_OFF_T sys_ftell(FILE *fp)
366 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(LARGE_SMB_OFF_T) && defined(HAVE_FTELL64)
367 return (SMB_OFF_T)ftell64(fp);
368 #elif defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(LARGE_SMB_OFF_T) && defined(HAVE_FTELLO64)
369 return (SMB_OFF_T)ftello64(fp);
371 return (SMB_OFF_T)ftell(fp);
375 /*******************************************************************
376 A creat() wrapper that will deal with 64 bit filesizes.
377 ********************************************************************/
379 int sys_creat(const char *path, mode_t mode)
381 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_CREAT64)
382 return creat64(path, mode);
385 * If creat64 isn't defined then ensure we call a potential open64.
388 return sys_open(path, O_WRONLY | O_CREAT | O_TRUNC, mode);
392 /*******************************************************************
393 An open() wrapper that will deal with 64 bit filesizes.
394 ********************************************************************/
396 int sys_open(const char *path, int oflag, mode_t mode)
398 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OPEN64)
399 return open64(path, oflag, mode);
401 return open(path, oflag, mode);
405 /*******************************************************************
406 An fopen() wrapper that will deal with 64 bit filesizes.
407 ********************************************************************/
409 FILE *sys_fopen(const char *path, const char *type)
411 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_FOPEN64)
412 return fopen64(path, type);
414 return fopen(path, type);
419 /*******************************************************************
420 A flock() wrapper that will perform the kernel flock.
421 ********************************************************************/
423 void kernel_flock(int fd, uint32 share_mode)
425 #if HAVE_KERNEL_SHARE_MODES
427 if (share_mode == FILE_SHARE_WRITE) {
428 kernel_mode = LOCK_MAND|LOCK_WRITE;
429 } else if (share_mode == FILE_SHARE_READ) {
430 kernel_mode = LOCK_MAND|LOCK_READ;
431 } else if (share_mode == FILE_SHARE_NONE) {
432 kernel_mode = LOCK_MAND;
435 flock(fd, kernel_mode);
443 /*******************************************************************
444 An opendir wrapper that will deal with 64 bit filesizes.
445 ********************************************************************/
447 SMB_STRUCT_DIR *sys_opendir(const char *name)
449 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OPENDIR64)
450 return opendir64(name);
452 return opendir(name);
456 /*******************************************************************
457 A readdir wrapper that will deal with 64 bit filesizes.
458 ********************************************************************/
460 SMB_STRUCT_DIRENT *sys_readdir(SMB_STRUCT_DIR *dirp)
462 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_READDIR64)
463 return readdir64(dirp);
465 return readdir(dirp);
469 /*******************************************************************
470 A seekdir wrapper that will deal with 64 bit filesizes.
471 ********************************************************************/
473 void sys_seekdir(SMB_STRUCT_DIR *dirp, long offset)
475 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_SEEKDIR64)
476 seekdir64(dirp, offset);
478 seekdir(dirp, offset);
482 /*******************************************************************
483 A telldir wrapper that will deal with 64 bit filesizes.
484 ********************************************************************/
486 long sys_telldir(SMB_STRUCT_DIR *dirp)
488 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_TELLDIR64)
489 return (long)telldir64(dirp);
491 return (long)telldir(dirp);
495 /*******************************************************************
496 A rewinddir wrapper that will deal with 64 bit filesizes.
497 ********************************************************************/
499 void sys_rewinddir(SMB_STRUCT_DIR *dirp)
501 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_REWINDDIR64)
508 /*******************************************************************
509 A close wrapper that will deal with 64 bit filesizes.
510 ********************************************************************/
512 int sys_closedir(SMB_STRUCT_DIR *dirp)
514 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_CLOSEDIR64)
515 return closedir64(dirp);
517 return closedir(dirp);
521 /*******************************************************************
522 An mknod() wrapper that will deal with 64 bit filesizes.
523 ********************************************************************/
525 int sys_mknod(const char *path, mode_t mode, SMB_DEV_T dev)
527 #if defined(HAVE_MKNOD) || defined(HAVE_MKNOD64)
528 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_MKNOD64) && defined(HAVE_DEV64_T)
529 return mknod64(path, mode, dev);
531 return mknod(path, mode, dev);
534 /* No mknod system call. */
540 /*******************************************************************
541 Wrapper for realpath.
542 ********************************************************************/
544 char *sys_realpath(const char *path, char *resolved_path)
546 #if defined(HAVE_REALPATH)
547 return realpath(path, resolved_path);
549 /* As realpath is not a system call we can't return ENOSYS. */
555 /*******************************************************************
556 The wait() calls vary between systems
557 ********************************************************************/
559 int sys_waitpid(pid_t pid,int *status,int options)
562 return waitpid(pid,status,options);
563 #else /* HAVE_WAITPID */
564 return wait4(pid, status, options, NULL);
565 #endif /* HAVE_WAITPID */
568 /*******************************************************************
569 System wrapper for getwd
570 ********************************************************************/
572 char *sys_getwd(char *s)
576 wd = (char *)getcwd(s, PATH_MAX);
578 wd = (char *)getwd(s);
583 /*******************************************************************
584 os/2 also doesn't have chroot
585 ********************************************************************/
586 int sys_chroot(const char *dname)
591 DEBUG(1,("WARNING: no chroot!\n"));
597 return(chroot(dname));
601 #if defined(HAVE_POSIX_CAPABILITIES)
603 /**************************************************************************
604 Try and abstract process capabilities (for systems that have them).
605 ****************************************************************************/
607 /* Set the POSIX capabilities needed for the given purpose into the effective
608 * capability set of the current process. Make sure they are always removed
609 * from the inheritable set, because there is no circumstance in which our
610 * children should inherit our elevated privileges.
612 static bool set_process_capability(enum smbd_capability capability,
615 cap_value_t cap_vals[2] = {0};
616 int num_cap_vals = 0;
620 #if defined(HAVE_PRCTL) && defined(PR_GET_KEEPCAPS) && defined(PR_SET_KEEPCAPS)
621 /* On Linux, make sure that any capabilities we grab are sticky
622 * across UID changes. We expect that this would allow us to keep both
623 * the effective and permitted capability sets, but as of circa 2.6.16,
624 * only the permitted set is kept. It is a bug (which we work around)
625 * that the effective set is lost, but we still require the effective
628 if (!prctl(PR_GET_KEEPCAPS)) {
629 prctl(PR_SET_KEEPCAPS, 1);
633 cap = cap_get_proc();
635 DEBUG(0,("set_process_capability: cap_get_proc failed: %s\n",
640 switch (capability) {
641 case KERNEL_OPLOCK_CAPABILITY:
642 #ifdef CAP_NETWORK_MGT
643 /* IRIX has CAP_NETWORK_MGT for oplocks. */
644 cap_vals[num_cap_vals++] = CAP_NETWORK_MGT;
647 case DMAPI_ACCESS_CAPABILITY:
648 #ifdef CAP_DEVICE_MGT
649 /* IRIX has CAP_DEVICE_MGT for DMAPI access. */
650 cap_vals[num_cap_vals++] = CAP_DEVICE_MGT;
652 /* Linux has CAP_MKNOD for DMAPI access. */
653 cap_vals[num_cap_vals++] = CAP_MKNOD;
656 case LEASE_CAPABILITY:
658 cap_vals[num_cap_vals++] = CAP_LEASE;
663 SMB_ASSERT(num_cap_vals <= ARRAY_SIZE(cap_vals));
665 if (num_cap_vals == 0) {
670 cap_set_flag(cap, CAP_EFFECTIVE, num_cap_vals, cap_vals,
671 enable ? CAP_SET : CAP_CLEAR);
673 /* We never want to pass capabilities down to our children, so make
674 * sure they are not inherited.
676 cap_set_flag(cap, CAP_INHERITABLE, num_cap_vals, cap_vals, CAP_CLEAR);
678 if (cap_set_proc(cap) == -1) {
679 DEBUG(0, ("set_process_capability: cap_set_proc failed: %s\n",
689 #endif /* HAVE_POSIX_CAPABILITIES */
691 /****************************************************************************
692 Gain the oplock capability from the kernel if possible.
693 ****************************************************************************/
695 void set_effective_capability(enum smbd_capability capability)
697 #if defined(HAVE_POSIX_CAPABILITIES)
698 set_process_capability(capability, True);
699 #endif /* HAVE_POSIX_CAPABILITIES */
702 void drop_effective_capability(enum smbd_capability capability)
704 #if defined(HAVE_POSIX_CAPABILITIES)
705 set_process_capability(capability, False);
706 #endif /* HAVE_POSIX_CAPABILITIES */
709 /**************************************************************************
710 Wrapper for random().
711 ****************************************************************************/
713 long sys_random(void)
715 #if defined(HAVE_RANDOM)
716 return (long)random();
717 #elif defined(HAVE_RAND)
720 DEBUG(0,("Error - no random function available !\n"));
725 /**************************************************************************
726 Wrapper for srandom().
727 ****************************************************************************/
729 void sys_srandom(unsigned int seed)
731 #if defined(HAVE_SRANDOM)
733 #elif defined(HAVE_SRAND)
736 DEBUG(0,("Error - no srandom function available !\n"));
741 /**************************************************************************
742 Returns equivalent to NGROUPS_MAX - using sysconf if needed.
743 ****************************************************************************/
747 #if defined(SYSCONF_SC_NGROUPS_MAX)
748 int ret = sysconf(_SC_NGROUPS_MAX);
749 return (ret == -1) ? NGROUPS_MAX : ret;
755 /**************************************************************************
756 Wrap setgroups and getgroups for systems that declare getgroups() as
757 returning an array of gid_t, but actuall return an array of int.
758 ****************************************************************************/
760 #if defined(HAVE_BROKEN_GETGROUPS)
761 static int sys_broken_getgroups(int setlen, gid_t *gidset)
768 return getgroups(setlen, &gid);
772 * Broken case. We need to allocate a
773 * GID_T array of size setlen.
782 setlen = groups_max();
784 if((group_list = SMB_MALLOC_ARRAY(GID_T, setlen)) == NULL) {
785 DEBUG(0,("sys_getgroups: Malloc fail.\n"));
789 if((ngroups = getgroups(setlen, group_list)) < 0) {
790 int saved_errno = errno;
791 SAFE_FREE(group_list);
796 for(i = 0; i < ngroups; i++)
797 gidset[i] = (gid_t)group_list[i];
799 SAFE_FREE(group_list);
803 static int sys_broken_setgroups(int setlen, gid_t *gidset)
811 if (setlen < 0 || setlen > groups_max()) {
817 * Broken case. We need to allocate a
818 * GID_T array of size setlen.
821 if((group_list = SMB_MALLOC_ARRAY(GID_T, setlen)) == NULL) {
822 DEBUG(0,("sys_setgroups: Malloc fail.\n"));
826 for(i = 0; i < setlen; i++)
827 group_list[i] = (GID_T) gidset[i];
829 if(setgroups(setlen, group_list) != 0) {
830 int saved_errno = errno;
831 SAFE_FREE(group_list);
836 SAFE_FREE(group_list);
840 #endif /* HAVE_BROKEN_GETGROUPS */
842 /* This is a list of systems that require the first GID passed to setgroups(2)
843 * to be the effective GID. If your system is one of these, add it here.
845 #if defined (FREEBSD) || defined (DARWINOS)
846 #define USE_BSD_SETGROUPS
849 #if defined(USE_BSD_SETGROUPS)
850 /* Depending on the particular BSD implementation, the first GID that is
851 * passed to setgroups(2) will either be ignored or will set the credential's
852 * effective GID. In either case, the right thing to do is to guarantee that
853 * gidset[0] is the effective GID.
855 static int sys_bsd_setgroups(gid_t primary_gid, int setlen, const gid_t *gidset)
857 gid_t *new_gidset = NULL;
861 /* setgroups(2) will fail with EINVAL if we pass too many groups. */
864 /* No group list, just make sure we are setting the efective GID. */
866 return setgroups(1, &primary_gid);
869 /* If the primary gid is not the first array element, grow the array
870 * and insert it at the front.
872 if (gidset[0] != primary_gid) {
873 new_gidset = SMB_MALLOC_ARRAY(gid_t, setlen + 1);
874 if (new_gidset == NULL) {
878 memcpy(new_gidset + 1, gidset, (setlen * sizeof(gid_t)));
879 new_gidset[0] = primary_gid;
884 DEBUG(3, ("forced to truncate group list from %d to %d\n",
889 #if defined(HAVE_BROKEN_GETGROUPS)
890 ret = sys_broken_setgroups(setlen, new_gidset ? new_gidset : gidset);
892 ret = setgroups(setlen, new_gidset ? new_gidset : gidset);
897 SAFE_FREE(new_gidset);
904 #endif /* USE_BSD_SETGROUPS */
906 /**************************************************************************
907 Wrapper for getgroups. Deals with broken (int) case.
908 ****************************************************************************/
910 int sys_getgroups(int setlen, gid_t *gidset)
912 #if defined(HAVE_BROKEN_GETGROUPS)
913 return sys_broken_getgroups(setlen, gidset);
915 return getgroups(setlen, gidset);
919 /**************************************************************************
920 Wrapper for setgroups. Deals with broken (int) case and BSD case.
921 ****************************************************************************/
923 int sys_setgroups(gid_t UNUSED(primary_gid), int setlen, gid_t *gidset)
925 #if !defined(HAVE_SETGROUPS)
928 #endif /* HAVE_SETGROUPS */
930 #if defined(USE_BSD_SETGROUPS)
931 return sys_bsd_setgroups(primary_gid, setlen, gidset);
932 #elif defined(HAVE_BROKEN_GETGROUPS)
933 return sys_broken_setgroups(setlen, gidset);
935 return setgroups(setlen, gidset);
939 /**************************************************************************
940 Wrappers for setpwent(), getpwent() and endpwent()
941 ****************************************************************************/
943 void sys_setpwent(void)
948 struct passwd *sys_getpwent(void)
953 void sys_endpwent(void)
958 /**************************************************************************
959 Wrappers for getpwnam(), getpwuid(), getgrnam(), getgrgid()
960 ****************************************************************************/
963 struct passwd *sys_getpwnam(const char *name)
965 return getpwnam(name);
968 struct passwd *sys_getpwuid(uid_t uid)
970 return getpwuid(uid);
973 struct group *sys_getgrnam(const char *name)
975 return getgrnam(name);
978 struct group *sys_getgrgid(gid_t gid)
980 return getgrgid(gid);
983 /**************************************************************************
984 Extract a command into an arg list.
985 ****************************************************************************/
987 static char **extract_args(TALLOC_CTX *mem_ctx, const char *command)
996 if (!(trunc_cmd = talloc_strdup(mem_ctx, command))) {
997 DEBUG(0, ("talloc failed\n"));
1001 if(!(ptr = strtok_r(trunc_cmd, " \t", &saveptr))) {
1002 TALLOC_FREE(trunc_cmd);
1011 for( argcl = 1; ptr; ptr = strtok_r(NULL, " \t", &saveptr))
1014 TALLOC_FREE(trunc_cmd);
1016 if (!(argl = TALLOC_ARRAY(mem_ctx, char *, argcl + 1))) {
1021 * Now do the extraction.
1024 if (!(trunc_cmd = talloc_strdup(mem_ctx, command))) {
1028 ptr = strtok_r(trunc_cmd, " \t", &saveptr);
1031 if (!(argl[i++] = talloc_strdup(argl, ptr))) {
1035 while((ptr = strtok_r(NULL, " \t", &saveptr)) != NULL) {
1037 if (!(argl[i++] = talloc_strdup(argl, ptr))) {
1046 DEBUG(0, ("talloc failed\n"));
1047 TALLOC_FREE(trunc_cmd);
1053 /**************************************************************************
1054 Wrapper for fork. Ensures that mypid is reset. Used so we can write
1055 a sys_getpid() that only does a system call *once*.
1056 ****************************************************************************/
1058 static pid_t mypid = (pid_t)-1;
1060 pid_t sys_fork(void)
1062 pid_t forkret = fork();
1064 if (forkret == (pid_t)0) /* Child - reset mypid so sys_getpid does a system call. */
1070 /**************************************************************************
1071 Wrapper for getpid. Ensures we only do a system call *once*.
1072 ****************************************************************************/
1074 pid_t sys_getpid(void)
1076 if (mypid == (pid_t)-1)
1082 /**************************************************************************
1083 Wrapper for popen. Safer as it doesn't search a path.
1084 Modified from the glibc sources.
1085 modified by tridge to return a file descriptor. We must kick our FILE* habit
1086 ****************************************************************************/
1088 typedef struct _popen_list
1092 struct _popen_list *next;
1095 static popen_list *popen_chain;
1097 int sys_popen(const char *command)
1099 int parent_end, child_end;
1101 popen_list *entry = NULL;
1104 if (pipe(pipe_fds) < 0)
1107 parent_end = pipe_fds[0];
1108 child_end = pipe_fds[1];
1115 if((entry = SMB_MALLOC_P(popen_list)) == NULL)
1118 ZERO_STRUCTP(entry);
1121 * Extract the command and args into a NULL terminated array.
1124 if(!(argl = extract_args(NULL, command)))
1127 entry->child_pid = sys_fork();
1129 if (entry->child_pid == -1) {
1133 if (entry->child_pid == 0) {
1139 int child_std_end = STDOUT_FILENO;
1143 if (child_end != child_std_end) {
1144 dup2 (child_end, child_std_end);
1149 * POSIX.2: "popen() shall ensure that any streams from previous
1150 * popen() calls that remain open in the parent process are closed
1151 * in the new child process."
1154 for (p = popen_chain; p; p = p->next)
1157 execv(argl[0], argl);
1168 /* Link into popen_chain. */
1169 entry->next = popen_chain;
1170 popen_chain = entry;
1171 entry->fd = parent_end;
1184 /**************************************************************************
1185 Wrapper for pclose. Modified from the glibc sources.
1186 ****************************************************************************/
1188 int sys_pclose(int fd)
1191 popen_list **ptr = &popen_chain;
1192 popen_list *entry = NULL;
1196 /* Unlink from popen_chain. */
1197 for ( ; *ptr != NULL; ptr = &(*ptr)->next) {
1198 if ((*ptr)->fd == fd) {
1200 *ptr = (*ptr)->next;
1206 if (status < 0 || close(entry->fd) < 0)
1210 * As Samba is catching and eating child process
1211 * exits we don't really care about the child exit
1212 * code, a -1 with errno = ECHILD will do fine for us.
1216 wait_pid = sys_waitpid (entry->child_pid, &wstatus, 0);
1217 } while (wait_pid == -1 && errno == EINTR);
1226 /**************************************************************************
1227 Wrappers for dlopen, dlsym, dlclose.
1228 ****************************************************************************/
1230 void *sys_dlopen(const char *name, int flags)
1232 #if defined(HAVE_DLOPEN)
1233 return dlopen(name, flags);
1239 void *sys_dlsym(void *handle, const char *symbol)
1241 #if defined(HAVE_DLSYM)
1242 return dlsym(handle, symbol);
1248 int sys_dlclose (void *handle)
1250 #if defined(HAVE_DLCLOSE)
1251 return dlclose(handle);
1257 const char *sys_dlerror(void)
1259 #if defined(HAVE_DLERROR)
1266 int sys_dup2(int oldfd, int newfd)
1268 #if defined(HAVE_DUP2)
1269 return dup2(oldfd, newfd);
1276 /**************************************************************************
1277 Wrapper for Admin Logs.
1278 ****************************************************************************/
1280 void sys_adminlog(int priority, const char *format_str, ...)
1284 char *msgbuf = NULL;
1286 va_start( ap, format_str );
1287 ret = vasprintf( &msgbuf, format_str, ap );
1293 #if defined(HAVE_SYSLOG)
1294 syslog( priority, "%s", msgbuf );
1296 DEBUG(0,("%s", msgbuf ));
1301 /******** Solaris EA helper function prototypes ********/
1302 #ifdef HAVE_ATTROPEN
1303 #define SOLARIS_ATTRMODE S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP
1304 static int solaris_write_xattr(int attrfd, const char *value, size_t size);
1305 static ssize_t solaris_read_xattr(int attrfd, void *value, size_t size);
1306 static ssize_t solaris_list_xattr(int attrdirfd, char *list, size_t size);
1307 static int solaris_unlinkat(int attrdirfd, const char *name);
1308 static int solaris_attropen(const char *path, const char *attrpath, int oflag, mode_t mode);
1309 static int solaris_openat(int fildes, const char *path, int oflag, mode_t mode);
1312 /**************************************************************************
1313 Wrappers for extented attribute calls. Based on the Linux package with
1314 support for IRIX and (Net|Free)BSD also. Expand as other systems have them.
1315 ****************************************************************************/
1317 ssize_t sys_getxattr (const char *path, const char *name, void *value, size_t size)
1319 #if defined(HAVE_GETXATTR)
1320 #ifndef XATTR_ADD_OPT
1321 return getxattr(path, name, value, size);
1324 return getxattr(path, name, value, size, 0, options);
1326 #elif defined(HAVE_GETEA)
1327 return getea(path, name, value, size);
1328 #elif defined(HAVE_EXTATTR_GET_FILE)
1331 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
1332 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
1333 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
1335 * The BSD implementation has a nasty habit of silently truncating
1336 * the returned value to the size of the buffer, so we have to check
1337 * that the buffer is large enough to fit the returned value.
1339 if((retval=extattr_get_file(path, attrnamespace, attrname, NULL, 0)) >= 0) {
1344 if((retval=extattr_get_file(path, attrnamespace, attrname, value, size)) >= 0)
1348 DEBUG(10,("sys_getxattr: extattr_get_file() failed with: %s\n", strerror(errno)));
1350 #elif defined(HAVE_ATTR_GET)
1351 int retval, flags = 0;
1352 int valuelength = (int)size;
1353 char *attrname = strchr(name,'.') + 1;
1355 if (strncmp(name, "system", 6) == 0) flags |= ATTR_ROOT;
1357 retval = attr_get(path, attrname, (char *)value, &valuelength, flags);
1359 return retval ? retval : valuelength;
1360 #elif defined(HAVE_ATTROPEN)
1362 int attrfd = solaris_attropen(path, name, O_RDONLY, 0);
1364 ret = solaris_read_xattr(attrfd, value, size);
1374 ssize_t sys_lgetxattr (const char *path, const char *name, void *value, size_t size)
1376 #if defined(HAVE_LGETXATTR)
1377 return lgetxattr(path, name, value, size);
1378 #elif defined(HAVE_GETXATTR) && defined(XATTR_ADD_OPT)
1379 int options = XATTR_NOFOLLOW;
1380 return getxattr(path, name, value, size, 0, options);
1381 #elif defined(HAVE_LGETEA)
1382 return lgetea(path, name, value, size);
1383 #elif defined(HAVE_EXTATTR_GET_LINK)
1386 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
1387 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
1388 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
1390 if((retval=extattr_get_link(path, attrnamespace, attrname, NULL, 0)) >= 0) {
1395 if((retval=extattr_get_link(path, attrnamespace, attrname, value, size)) >= 0)
1399 DEBUG(10,("sys_lgetxattr: extattr_get_link() failed with: %s\n", strerror(errno)));
1401 #elif defined(HAVE_ATTR_GET)
1402 int retval, flags = ATTR_DONTFOLLOW;
1403 int valuelength = (int)size;
1404 char *attrname = strchr(name,'.') + 1;
1406 if (strncmp(name, "system", 6) == 0) flags |= ATTR_ROOT;
1408 retval = attr_get(path, attrname, (char *)value, &valuelength, flags);
1410 return retval ? retval : valuelength;
1411 #elif defined(HAVE_ATTROPEN)
1413 int attrfd = solaris_attropen(path, name, O_RDONLY|AT_SYMLINK_NOFOLLOW, 0);
1415 ret = solaris_read_xattr(attrfd, value, size);
1425 ssize_t sys_fgetxattr (int filedes, const char *name, void *value, size_t size)
1427 #if defined(HAVE_FGETXATTR)
1428 #ifndef XATTR_ADD_OPT
1429 return fgetxattr(filedes, name, value, size);
1432 return fgetxattr(filedes, name, value, size, 0, options);
1434 #elif defined(HAVE_FGETEA)
1435 return fgetea(filedes, name, value, size);
1436 #elif defined(HAVE_EXTATTR_GET_FD)
1439 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
1440 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
1441 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
1443 if((retval=extattr_get_fd(filedes, attrnamespace, attrname, NULL, 0)) >= 0) {
1448 if((retval=extattr_get_fd(filedes, attrnamespace, attrname, value, size)) >= 0)
1452 DEBUG(10,("sys_fgetxattr: extattr_get_fd() failed with: %s\n", strerror(errno)));
1454 #elif defined(HAVE_ATTR_GETF)
1455 int retval, flags = 0;
1456 int valuelength = (int)size;
1457 char *attrname = strchr(name,'.') + 1;
1459 if (strncmp(name, "system", 6) == 0) flags |= ATTR_ROOT;
1461 retval = attr_getf(filedes, attrname, (char *)value, &valuelength, flags);
1463 return retval ? retval : valuelength;
1464 #elif defined(HAVE_ATTROPEN)
1466 int attrfd = solaris_openat(filedes, name, O_RDONLY|O_XATTR, 0);
1468 ret = solaris_read_xattr(attrfd, value, size);
1478 #if defined(HAVE_EXTATTR_LIST_FILE)
1480 #define EXTATTR_PREFIX(s) (s), (sizeof((s))-1)
1488 { EXTATTR_NAMESPACE_SYSTEM, EXTATTR_PREFIX("system.") },
1489 { EXTATTR_NAMESPACE_USER, EXTATTR_PREFIX("user.") },
1497 static ssize_t bsd_attr_list (int type, extattr_arg arg, char *list, size_t size)
1499 ssize_t list_size, total_size = 0;
1502 /* Iterate through extattr(2) namespaces */
1503 for(t = 0; t < (sizeof(extattr)/sizeof(extattr[0])); t++) {
1505 #if defined(HAVE_EXTATTR_LIST_FILE)
1507 list_size = extattr_list_file(arg.path, extattr[t].space, list, size);
1510 #if defined(HAVE_EXTATTR_LIST_LINK)
1512 list_size = extattr_list_link(arg.path, extattr[t].space, list, size);
1515 #if defined(HAVE_EXTATTR_LIST_FD)
1517 list_size = extattr_list_fd(arg.filedes, extattr[t].space, list, size);
1524 /* Some error happend. Errno should be set by the previous call */
1530 /* XXX: Call with an empty buffer may be used to calculate
1531 necessary buffer size. Unfortunately, we can't say, how
1532 many attributes were returned, so here is the potential
1533 problem with the emulation.
1536 /* Take the worse case of one char attribute names -
1537 two bytes per name plus one more for sanity.
1539 total_size += list_size + (list_size/2 + 1)*extattr[t].len;
1542 /* Count necessary offset to fit namespace prefixes */
1544 for(i = 0; i < list_size; i += list[i] + 1)
1545 len += extattr[t].len;
1547 total_size += list_size + len;
1548 /* Buffer is too small to fit the results */
1549 if(total_size > size) {
1553 /* Shift results back, so we can prepend prefixes */
1554 buf = memmove(list + len, list, list_size);
1556 for(i = 0; i < list_size; i += len + 1) {
1558 strncpy(list, extattr[t].name, extattr[t].len + 1);
1559 list += extattr[t].len;
1560 strncpy(list, buf + i + 1, len);
1571 #if defined(HAVE_ATTR_LIST) && defined(HAVE_SYS_ATTRIBUTES_H)
1572 static char attr_buffer[ATTR_MAX_VALUELEN];
1574 static ssize_t irix_attr_list(const char *path, int filedes, char *list, size_t size, int flags)
1576 int retval = 0, index;
1577 attrlist_cursor_t *cursor = 0;
1579 attrlist_t * al = (attrlist_t *)attr_buffer;
1581 size_t ent_size, left = size;
1586 retval = attr_listf(filedes, attr_buffer, ATTR_MAX_VALUELEN, flags, cursor);
1588 retval = attr_list(path, attr_buffer, ATTR_MAX_VALUELEN, flags, cursor);
1590 for (index = 0; index < al->al_count; index++) {
1591 ae = ATTR_ENTRY(attr_buffer, index);
1592 ent_size = strlen(ae->a_name) + sizeof("user.");
1593 if (left >= ent_size) {
1594 strncpy(bp, "user.", sizeof("user."));
1595 strncat(bp, ae->a_name, ent_size - sizeof("user."));
1603 total_size += ent_size;
1605 if (al->al_more == 0) break;
1612 retval = attr_listf(filedes, attr_buffer, ATTR_MAX_VALUELEN, flags, cursor);
1614 retval = attr_list(path, attr_buffer, ATTR_MAX_VALUELEN, flags, cursor);
1616 for (index = 0; index < al->al_count; index++) {
1617 ae = ATTR_ENTRY(attr_buffer, index);
1618 ent_size = strlen(ae->a_name) + sizeof("system.");
1619 if (left >= ent_size) {
1620 strncpy(bp, "system.", sizeof("system."));
1621 strncat(bp, ae->a_name, ent_size - sizeof("system."));
1629 total_size += ent_size;
1631 if (al->al_more == 0) break;
1634 return (ssize_t)(retval ? retval : total_size);
1639 ssize_t sys_listxattr (const char *path, char *list, size_t size)
1641 #if defined(HAVE_LISTXATTR)
1642 #ifndef XATTR_ADD_OPT
1643 return listxattr(path, list, size);
1646 return listxattr(path, list, size, options);
1648 #elif defined(HAVE_LISTEA)
1649 return listea(path, list, size);
1650 #elif defined(HAVE_EXTATTR_LIST_FILE)
1653 return bsd_attr_list(0, arg, list, size);
1654 #elif defined(HAVE_ATTR_LIST) && defined(HAVE_SYS_ATTRIBUTES_H)
1655 return irix_attr_list(path, 0, list, size, 0);
1656 #elif defined(HAVE_ATTROPEN)
1658 int attrdirfd = solaris_attropen(path, ".", O_RDONLY, 0);
1659 if (attrdirfd >= 0) {
1660 ret = solaris_list_xattr(attrdirfd, list, size);
1670 ssize_t sys_llistxattr (const char *path, char *list, size_t size)
1672 #if defined(HAVE_LLISTXATTR)
1673 return llistxattr(path, list, size);
1674 #elif defined(HAVE_LISTXATTR) && defined(XATTR_ADD_OPT)
1675 int options = XATTR_NOFOLLOW;
1676 return listxattr(path, list, size, options);
1677 #elif defined(HAVE_LLISTEA)
1678 return llistea(path, list, size);
1679 #elif defined(HAVE_EXTATTR_LIST_LINK)
1682 return bsd_attr_list(1, arg, list, size);
1683 #elif defined(HAVE_ATTR_LIST) && defined(HAVE_SYS_ATTRIBUTES_H)
1684 return irix_attr_list(path, 0, list, size, ATTR_DONTFOLLOW);
1685 #elif defined(HAVE_ATTROPEN)
1687 int attrdirfd = solaris_attropen(path, ".", O_RDONLY|AT_SYMLINK_NOFOLLOW, 0);
1688 if (attrdirfd >= 0) {
1689 ret = solaris_list_xattr(attrdirfd, list, size);
1699 ssize_t sys_flistxattr (int filedes, char *list, size_t size)
1701 #if defined(HAVE_FLISTXATTR)
1702 #ifndef XATTR_ADD_OPT
1703 return flistxattr(filedes, list, size);
1706 return flistxattr(filedes, list, size, options);
1708 #elif defined(HAVE_FLISTEA)
1709 return flistea(filedes, list, size);
1710 #elif defined(HAVE_EXTATTR_LIST_FD)
1712 arg.filedes = filedes;
1713 return bsd_attr_list(2, arg, list, size);
1714 #elif defined(HAVE_ATTR_LISTF)
1715 return irix_attr_list(NULL, filedes, list, size, 0);
1716 #elif defined(HAVE_ATTROPEN)
1718 int attrdirfd = solaris_openat(filedes, ".", O_RDONLY|O_XATTR, 0);
1719 if (attrdirfd >= 0) {
1720 ret = solaris_list_xattr(attrdirfd, list, size);
1730 int sys_removexattr (const char *path, const char *name)
1732 #if defined(HAVE_REMOVEXATTR)
1733 #ifndef XATTR_ADD_OPT
1734 return removexattr(path, name);
1737 return removexattr(path, name, options);
1739 #elif defined(HAVE_REMOVEEA)
1740 return removeea(path, name);
1741 #elif defined(HAVE_EXTATTR_DELETE_FILE)
1743 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
1744 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
1745 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
1747 return extattr_delete_file(path, attrnamespace, attrname);
1748 #elif defined(HAVE_ATTR_REMOVE)
1750 char *attrname = strchr(name,'.') + 1;
1752 if (strncmp(name, "system", 6) == 0) flags |= ATTR_ROOT;
1754 return attr_remove(path, attrname, flags);
1755 #elif defined(HAVE_ATTROPEN)
1757 int attrdirfd = solaris_attropen(path, ".", O_RDONLY, 0);
1758 if (attrdirfd >= 0) {
1759 ret = solaris_unlinkat(attrdirfd, name);
1769 int sys_lremovexattr (const char *path, const char *name)
1771 #if defined(HAVE_LREMOVEXATTR)
1772 return lremovexattr(path, name);
1773 #elif defined(HAVE_REMOVEXATTR) && defined(XATTR_ADD_OPT)
1774 int options = XATTR_NOFOLLOW;
1775 return removexattr(path, name, options);
1776 #elif defined(HAVE_LREMOVEEA)
1777 return lremoveea(path, name);
1778 #elif defined(HAVE_EXTATTR_DELETE_LINK)
1780 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
1781 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
1782 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
1784 return extattr_delete_link(path, attrnamespace, attrname);
1785 #elif defined(HAVE_ATTR_REMOVE)
1786 int flags = ATTR_DONTFOLLOW;
1787 char *attrname = strchr(name,'.') + 1;
1789 if (strncmp(name, "system", 6) == 0) flags |= ATTR_ROOT;
1791 return attr_remove(path, attrname, flags);
1792 #elif defined(HAVE_ATTROPEN)
1794 int attrdirfd = solaris_attropen(path, ".", O_RDONLY|AT_SYMLINK_NOFOLLOW, 0);
1795 if (attrdirfd >= 0) {
1796 ret = solaris_unlinkat(attrdirfd, name);
1806 int sys_fremovexattr (int filedes, const char *name)
1808 #if defined(HAVE_FREMOVEXATTR)
1809 #ifndef XATTR_ADD_OPT
1810 return fremovexattr(filedes, name);
1813 return fremovexattr(filedes, name, options);
1815 #elif defined(HAVE_FREMOVEEA)
1816 return fremoveea(filedes, name);
1817 #elif defined(HAVE_EXTATTR_DELETE_FD)
1819 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
1820 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
1821 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
1823 return extattr_delete_fd(filedes, attrnamespace, attrname);
1824 #elif defined(HAVE_ATTR_REMOVEF)
1826 char *attrname = strchr(name,'.') + 1;
1828 if (strncmp(name, "system", 6) == 0) flags |= ATTR_ROOT;
1830 return attr_removef(filedes, attrname, flags);
1831 #elif defined(HAVE_ATTROPEN)
1833 int attrdirfd = solaris_openat(filedes, ".", O_RDONLY|O_XATTR, 0);
1834 if (attrdirfd >= 0) {
1835 ret = solaris_unlinkat(attrdirfd, name);
1845 int sys_setxattr (const char *path, const char *name, const void *value, size_t size, int flags)
1847 #if defined(HAVE_SETXATTR)
1848 #ifndef XATTR_ADD_OPT
1849 return setxattr(path, name, value, size, flags);
1852 return setxattr(path, name, value, size, 0, options);
1854 #elif defined(HAVE_SETEA)
1855 return setea(path, name, value, size, flags);
1856 #elif defined(HAVE_EXTATTR_SET_FILE)
1859 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
1860 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
1861 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
1863 /* Check attribute existence */
1864 retval = extattr_get_file(path, attrnamespace, attrname, NULL, 0);
1866 /* REPLACE attribute, that doesn't exist */
1867 if (flags & XATTR_REPLACE && errno == ENOATTR) {
1871 /* Ignore other errors */
1874 /* CREATE attribute, that already exists */
1875 if (flags & XATTR_CREATE) {
1881 retval = extattr_set_file(path, attrnamespace, attrname, value, size);
1882 return (retval < 0) ? -1 : 0;
1883 #elif defined(HAVE_ATTR_SET)
1885 char *attrname = strchr(name,'.') + 1;
1887 if (strncmp(name, "system", 6) == 0) myflags |= ATTR_ROOT;
1888 if (flags & XATTR_CREATE) myflags |= ATTR_CREATE;
1889 if (flags & XATTR_REPLACE) myflags |= ATTR_REPLACE;
1891 return attr_set(path, attrname, (const char *)value, size, myflags);
1892 #elif defined(HAVE_ATTROPEN)
1894 int myflags = O_RDWR;
1896 if (flags & XATTR_CREATE) myflags |= O_EXCL;
1897 if (!(flags & XATTR_REPLACE)) myflags |= O_CREAT;
1898 attrfd = solaris_attropen(path, name, myflags, (mode_t) SOLARIS_ATTRMODE);
1900 ret = solaris_write_xattr(attrfd, value, size);
1910 int sys_lsetxattr (const char *path, const char *name, const void *value, size_t size, int flags)
1912 #if defined(HAVE_LSETXATTR)
1913 return lsetxattr(path, name, value, size, flags);
1914 #elif defined(HAVE_SETXATTR) && defined(XATTR_ADD_OPT)
1915 int options = XATTR_NOFOLLOW;
1916 return setxattr(path, name, value, size, 0, options);
1917 #elif defined(LSETEA)
1918 return lsetea(path, name, value, size, flags);
1919 #elif defined(HAVE_EXTATTR_SET_LINK)
1922 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
1923 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
1924 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
1926 /* Check attribute existence */
1927 retval = extattr_get_link(path, attrnamespace, attrname, NULL, 0);
1929 /* REPLACE attribute, that doesn't exist */
1930 if (flags & XATTR_REPLACE && errno == ENOATTR) {
1934 /* Ignore other errors */
1937 /* CREATE attribute, that already exists */
1938 if (flags & XATTR_CREATE) {
1945 retval = extattr_set_link(path, attrnamespace, attrname, value, size);
1946 return (retval < 0) ? -1 : 0;
1947 #elif defined(HAVE_ATTR_SET)
1948 int myflags = ATTR_DONTFOLLOW;
1949 char *attrname = strchr(name,'.') + 1;
1951 if (strncmp(name, "system", 6) == 0) myflags |= ATTR_ROOT;
1952 if (flags & XATTR_CREATE) myflags |= ATTR_CREATE;
1953 if (flags & XATTR_REPLACE) myflags |= ATTR_REPLACE;
1955 return attr_set(path, attrname, (const char *)value, size, myflags);
1956 #elif defined(HAVE_ATTROPEN)
1958 int myflags = O_RDWR | AT_SYMLINK_NOFOLLOW;
1960 if (flags & XATTR_CREATE) myflags |= O_EXCL;
1961 if (!(flags & XATTR_REPLACE)) myflags |= O_CREAT;
1962 attrfd = solaris_attropen(path, name, myflags, (mode_t) SOLARIS_ATTRMODE);
1964 ret = solaris_write_xattr(attrfd, value, size);
1974 int sys_fsetxattr (int filedes, const char *name, const void *value, size_t size, int flags)
1976 #if defined(HAVE_FSETXATTR)
1977 #ifndef XATTR_ADD_OPT
1978 return fsetxattr(filedes, name, value, size, flags);
1981 return fsetxattr(filedes, name, value, size, 0, options);
1983 #elif defined(HAVE_FSETEA)
1984 return fsetea(filedes, name, value, size, flags);
1985 #elif defined(HAVE_EXTATTR_SET_FD)
1988 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
1989 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
1990 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
1992 /* Check attribute existence */
1993 retval = extattr_get_fd(filedes, attrnamespace, attrname, NULL, 0);
1995 /* REPLACE attribute, that doesn't exist */
1996 if (flags & XATTR_REPLACE && errno == ENOATTR) {
2000 /* Ignore other errors */
2003 /* CREATE attribute, that already exists */
2004 if (flags & XATTR_CREATE) {
2010 retval = extattr_set_fd(filedes, attrnamespace, attrname, value, size);
2011 return (retval < 0) ? -1 : 0;
2012 #elif defined(HAVE_ATTR_SETF)
2014 char *attrname = strchr(name,'.') + 1;
2016 if (strncmp(name, "system", 6) == 0) myflags |= ATTR_ROOT;
2017 if (flags & XATTR_CREATE) myflags |= ATTR_CREATE;
2018 if (flags & XATTR_REPLACE) myflags |= ATTR_REPLACE;
2020 return attr_setf(filedes, attrname, (const char *)value, size, myflags);
2021 #elif defined(HAVE_ATTROPEN)
2023 int myflags = O_RDWR | O_XATTR;
2025 if (flags & XATTR_CREATE) myflags |= O_EXCL;
2026 if (!(flags & XATTR_REPLACE)) myflags |= O_CREAT;
2027 attrfd = solaris_openat(filedes, name, myflags, (mode_t) SOLARIS_ATTRMODE);
2029 ret = solaris_write_xattr(attrfd, value, size);
2039 /**************************************************************************
2040 helper functions for Solaris' EA support
2041 ****************************************************************************/
2042 #ifdef HAVE_ATTROPEN
2043 static ssize_t solaris_read_xattr(int attrfd, void *value, size_t size)
2047 if (fstat(attrfd, &sbuf) == -1) {
2052 /* This is to return the current size of the named extended attribute */
2054 return sbuf.st_size;
2057 /* check size and read xattr */
2058 if (sbuf.st_size > size) {
2063 return read(attrfd, value, sbuf.st_size);
2066 static ssize_t solaris_list_xattr(int attrdirfd, char *list, size_t size)
2072 int newfd = dup(attrdirfd);
2073 /* CAUTION: The originating file descriptor should not be
2074 used again following the call to fdopendir().
2075 For that reason we dup() the file descriptor
2076 here to make things more clear. */
2077 dirp = fdopendir(newfd);
2079 while ((de = readdir(dirp))) {
2080 size_t listlen = strlen(de->d_name);
2081 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
2082 /* we don't want "." and ".." here: */
2083 DEBUG(10,("skipped EA %s\n",de->d_name));
2088 /* return the current size of the list of extended attribute names*/
2091 /* check size and copy entrieѕ + nul into list. */
2092 if ((len + listlen + 1) > size) {
2097 safe_strcpy(list + len, de->d_name, listlen);
2105 if (closedir(dirp) == -1) {
2106 DEBUG(0,("closedir dirp failed: %s\n",strerror(errno)));
2112 static int solaris_unlinkat(int attrdirfd, const char *name)
2114 if (unlinkat(attrdirfd, name, 0) == -1) {
2115 if (errno == ENOENT) {
2123 static int solaris_attropen(const char *path, const char *attrpath, int oflag, mode_t mode)
2125 int filedes = attropen(path, attrpath, oflag, mode);
2126 if (filedes == -1) {
2127 DEBUG(10,("attropen FAILED: path: %s, name: %s, errno: %s\n",path,attrpath,strerror(errno)));
2128 if (errno == EINVAL) {
2137 static int solaris_openat(int fildes, const char *path, int oflag, mode_t mode)
2139 int filedes = openat(fildes, path, oflag, mode);
2140 if (filedes == -1) {
2141 DEBUG(10,("openat FAILED: fd: %s, path: %s, errno: %s\n",filedes,path,strerror(errno)));
2142 if (errno == EINVAL) {
2151 static int solaris_write_xattr(int attrfd, const char *value, size_t size)
2153 if ((ftruncate(attrfd, 0) == 0) && (write(attrfd, value, size) == size)) {
2156 DEBUG(10,("solaris_write_xattr FAILED!\n"));
2160 #endif /*HAVE_ATTROPEN*/
2163 /****************************************************************************
2164 Return the major devicenumber for UNIX extensions.
2165 ****************************************************************************/
2167 uint32 unix_dev_major(SMB_DEV_T dev)
2169 #if defined(HAVE_DEVICE_MAJOR_FN)
2170 return (uint32)major(dev);
2172 return (uint32)(dev >> 8);
2176 /****************************************************************************
2177 Return the minor devicenumber for UNIX extensions.
2178 ****************************************************************************/
2180 uint32 unix_dev_minor(SMB_DEV_T dev)
2182 #if defined(HAVE_DEVICE_MINOR_FN)
2183 return (uint32)minor(dev);
2185 return (uint32)(dev & 0xff);
2189 #if defined(WITH_AIO)
2191 /*******************************************************************
2192 An aio_read wrapper that will deal with 64-bit sizes.
2193 ********************************************************************/
2195 int sys_aio_read(SMB_STRUCT_AIOCB *aiocb)
2197 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_READ64)
2198 return aio_read64(aiocb);
2199 #elif defined(HAVE_AIO_READ)
2200 return aio_read(aiocb);
2207 /*******************************************************************
2208 An aio_write wrapper that will deal with 64-bit sizes.
2209 ********************************************************************/
2211 int sys_aio_write(SMB_STRUCT_AIOCB *aiocb)
2213 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_WRITE64)
2214 return aio_write64(aiocb);
2215 #elif defined(HAVE_AIO_WRITE)
2216 return aio_write(aiocb);
2223 /*******************************************************************
2224 An aio_return wrapper that will deal with 64-bit sizes.
2225 ********************************************************************/
2227 ssize_t sys_aio_return(SMB_STRUCT_AIOCB *aiocb)
2229 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_RETURN64)
2230 return aio_return64(aiocb);
2231 #elif defined(HAVE_AIO_RETURN)
2232 return aio_return(aiocb);
2239 /*******************************************************************
2240 An aio_cancel wrapper that will deal with 64-bit sizes.
2241 ********************************************************************/
2243 int sys_aio_cancel(int fd, SMB_STRUCT_AIOCB *aiocb)
2245 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_CANCEL64)
2246 return aio_cancel64(fd, aiocb);
2247 #elif defined(HAVE_AIO_CANCEL)
2248 return aio_cancel(fd, aiocb);
2255 /*******************************************************************
2256 An aio_error wrapper that will deal with 64-bit sizes.
2257 ********************************************************************/
2259 int sys_aio_error(const SMB_STRUCT_AIOCB *aiocb)
2261 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_ERROR64)
2262 return aio_error64(aiocb);
2263 #elif defined(HAVE_AIO_ERROR)
2264 return aio_error(aiocb);
2271 /*******************************************************************
2272 An aio_fsync wrapper that will deal with 64-bit sizes.
2273 ********************************************************************/
2275 int sys_aio_fsync(int op, SMB_STRUCT_AIOCB *aiocb)
2277 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_FSYNC64)
2278 return aio_fsync64(op, aiocb);
2279 #elif defined(HAVE_AIO_FSYNC)
2280 return aio_fsync(op, aiocb);
2287 /*******************************************************************
2288 An aio_fsync wrapper that will deal with 64-bit sizes.
2289 ********************************************************************/
2291 int sys_aio_suspend(const SMB_STRUCT_AIOCB * const cblist[], int n, const struct timespec *timeout)
2293 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_SUSPEND64)
2294 return aio_suspend64(cblist, n, timeout);
2295 #elif defined(HAVE_AIO_FSYNC)
2296 return aio_suspend(cblist, n, timeout);
2302 #else /* !WITH_AIO */
2304 int sys_aio_read(SMB_STRUCT_AIOCB *aiocb)
2310 int sys_aio_write(SMB_STRUCT_AIOCB *aiocb)
2316 ssize_t sys_aio_return(SMB_STRUCT_AIOCB *aiocb)
2322 int sys_aio_cancel(int fd, SMB_STRUCT_AIOCB *aiocb)
2328 int sys_aio_error(const SMB_STRUCT_AIOCB *aiocb)
2334 int sys_aio_fsync(int op, SMB_STRUCT_AIOCB *aiocb)
2340 int sys_aio_suspend(const SMB_STRUCT_AIOCB * const cblist[], int n, const struct timespec *timeout)
2345 #endif /* WITH_AIO */
2347 int sys_getpeereid( int s, uid_t *uid)
2349 #if defined(HAVE_PEERCRED)
2351 socklen_t cred_len = sizeof(struct ucred);
2354 ret = getsockopt(s, SOL_SOCKET, SO_PEERCRED, (void *)&cred, &cred_len);
2359 if (cred_len != sizeof(struct ucred)) {
2372 int sys_getnameinfo(const struct sockaddr *psa,
2381 * For Solaris we must make sure salen is the
2382 * correct length for the incoming sa_family.
2385 if (salen == sizeof(struct sockaddr_storage)) {
2386 salen = sizeof(struct sockaddr_in);
2387 #if defined(HAVE_IPV6)
2388 if (psa->sa_family == AF_INET6) {
2389 salen = sizeof(struct sockaddr_in6);
2393 return getnameinfo(psa, salen, host, hostlen, service, servlen, flags);
2396 int sys_connect(int fd, const struct sockaddr * addr)
2398 socklen_t salen = -1;
2400 if (addr->sa_family == AF_INET) {
2401 salen = sizeof(struct sockaddr_in);
2402 } else if (addr->sa_family == AF_UNIX) {
2403 salen = sizeof(struct sockaddr_un);
2405 #if defined(HAVE_IPV6)
2406 else if (addr->sa_family == AF_INET6) {
2407 salen = sizeof(struct sockaddr_in6);
2411 return connect(fd, addr, salen);