2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Jeremy Allison 1998-2005
6 Copyright (C) Timur Bakeyev 2005
7 Copyright (C) Bjoern Jacke 2006-2007
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #ifdef HAVE_SYS_PRCTL_H
26 #include <sys/prctl.h>
30 The idea is that this file will eventually have wrappers around all
31 important system calls in samba. The aims are:
33 - to enable easier porting by putting OS dependent stuff in here
35 - to allow for hooks into other "pseudo-filesystems"
37 - to allow easier integration of things like the japanese extensions
39 - to support the philosophy of Samba to expose the features of
40 the OS within the SMB model. In general whatever file/printer/variable
41 expansions/etc make sense to the OS should be acceptable to Samba.
46 /*******************************************************************
47 A wrapper for memalign
48 ********************************************************************/
50 void *sys_memalign( size_t align
, size_t size
)
52 #if defined(HAVE_POSIX_MEMALIGN)
54 int ret
= posix_memalign( &p
, align
, size
);
59 #elif defined(HAVE_MEMALIGN)
60 return memalign( align
, size
);
62 /* On *BSD systems memaligns doesn't exist, but memory will
63 * be aligned on allocations of > pagesize. */
64 #if defined(SYSCONF_SC_PAGESIZE)
65 size_t pagesize
= (size_t)sysconf(_SC_PAGESIZE
);
66 #elif defined(HAVE_GETPAGESIZE)
67 size_t pagesize
= (size_t)getpagesize();
69 size_t pagesize
= (size_t)-1;
71 if (pagesize
== (size_t)-1) {
72 DEBUG(0,("memalign functionalaity not available on this platform!\n"));
75 if (size
< pagesize
) {
78 return SMB_MALLOC(size
);
82 /*******************************************************************
83 A wrapper for usleep in case we don't have one.
84 ********************************************************************/
86 int sys_usleep(long usecs
)
93 * We need this braindamage as the glibc usleep
94 * is not SPEC1170 complient... grumble... JRA.
97 if(usecs
< 0 || usecs
> 999999) {
105 #else /* HAVE_USLEEP */
107 * Fake it with select...
110 tval
.tv_usec
= usecs
/1000;
111 select(0,NULL
,NULL
,NULL
,&tval
);
113 #endif /* HAVE_USLEEP */
116 /*******************************************************************
117 A read wrapper that will deal with EINTR.
118 ********************************************************************/
120 ssize_t
sys_read(int fd
, void *buf
, size_t count
)
125 ret
= read(fd
, buf
, count
);
126 } while (ret
== -1 && errno
== EINTR
);
130 /*******************************************************************
131 A write wrapper that will deal with EINTR.
132 ********************************************************************/
134 ssize_t
sys_write(int fd
, const void *buf
, size_t count
)
139 ret
= write(fd
, buf
, count
);
140 } while (ret
== -1 && errno
== EINTR
);
144 /*******************************************************************
145 A writev wrapper that will deal with EINTR.
146 ********************************************************************/
148 ssize_t
sys_writev(int fd
, const struct iovec
*iov
, int iovcnt
)
153 /* Try to confuse write_data_iov a bit */
154 if ((random() % 5) == 0) {
155 return sys_write(fd
, iov
[0].iov_base
, iov
[0].iov_len
);
157 if (iov
[0].iov_len
> 1) {
158 return sys_write(fd
, iov
[0].iov_base
,
159 (random() % (iov
[0].iov_len
-1)) + 1);
164 ret
= writev(fd
, iov
, iovcnt
);
165 } while (ret
== -1 && errno
== EINTR
);
169 /*******************************************************************
170 A pread wrapper that will deal with EINTR and 64-bit file offsets.
171 ********************************************************************/
173 #if defined(HAVE_PREAD) || defined(HAVE_PREAD64)
174 ssize_t
sys_pread(int fd
, void *buf
, size_t count
, SMB_OFF_T off
)
179 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_PREAD64)
180 ret
= pread64(fd
, buf
, count
, off
);
182 ret
= pread(fd
, buf
, count
, off
);
184 } while (ret
== -1 && errno
== EINTR
);
189 /*******************************************************************
190 A write wrapper that will deal with EINTR and 64-bit file offsets.
191 ********************************************************************/
193 #if defined(HAVE_PWRITE) || defined(HAVE_PWRITE64)
194 ssize_t
sys_pwrite(int fd
, const void *buf
, size_t count
, SMB_OFF_T off
)
199 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_PWRITE64)
200 ret
= pwrite64(fd
, buf
, count
, off
);
202 ret
= pwrite(fd
, buf
, count
, off
);
204 } while (ret
== -1 && errno
== EINTR
);
209 /*******************************************************************
210 A send wrapper that will deal with EINTR.
211 ********************************************************************/
213 ssize_t
sys_send(int s
, const void *msg
, size_t len
, int flags
)
218 ret
= send(s
, msg
, len
, flags
);
219 } while (ret
== -1 && errno
== EINTR
);
223 /*******************************************************************
224 A sendto wrapper that will deal with EINTR.
225 ********************************************************************/
227 ssize_t
sys_sendto(int s
, const void *msg
, size_t len
, int flags
, const struct sockaddr
*to
, socklen_t tolen
)
232 ret
= sendto(s
, msg
, len
, flags
, to
, tolen
);
233 } while (ret
== -1 && errno
== EINTR
);
237 /*******************************************************************
238 A recv wrapper that will deal with EINTR.
239 ********************************************************************/
241 ssize_t
sys_recv(int fd
, void *buf
, size_t count
, int flags
)
246 ret
= recv(fd
, buf
, count
, flags
);
247 } while (ret
== -1 && errno
== EINTR
);
251 /*******************************************************************
252 A recvfrom wrapper that will deal with EINTR.
253 ********************************************************************/
255 ssize_t
sys_recvfrom(int s
, void *buf
, size_t len
, int flags
, struct sockaddr
*from
, socklen_t
*fromlen
)
260 ret
= recvfrom(s
, buf
, len
, flags
, from
, fromlen
);
261 } while (ret
== -1 && errno
== EINTR
);
265 /*******************************************************************
266 A fcntl wrapper that will deal with EINTR.
267 ********************************************************************/
269 int sys_fcntl_ptr(int fd
, int cmd
, void *arg
)
274 ret
= fcntl(fd
, cmd
, arg
);
275 } while (ret
== -1 && errno
== EINTR
);
279 /*******************************************************************
280 A fcntl wrapper that will deal with EINTR.
281 ********************************************************************/
283 int sys_fcntl_long(int fd
, int cmd
, long arg
)
288 ret
= fcntl(fd
, cmd
, arg
);
289 } while (ret
== -1 && errno
== EINTR
);
293 /*******************************************************************
294 A stat() wrapper that will deal with 64 bit filesizes.
295 ********************************************************************/
297 int sys_stat(const char *fname
,SMB_STRUCT_STAT
*sbuf
)
300 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_STAT64)
301 ret
= stat64(fname
, sbuf
);
303 ret
= stat(fname
, sbuf
);
305 /* we always want directories to appear zero size */
306 if (ret
== 0 && S_ISDIR(sbuf
->st_mode
)) sbuf
->st_size
= 0;
310 /*******************************************************************
311 An fstat() wrapper that will deal with 64 bit filesizes.
312 ********************************************************************/
314 int sys_fstat(int fd
,SMB_STRUCT_STAT
*sbuf
)
317 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_FSTAT64)
318 ret
= fstat64(fd
, sbuf
);
320 ret
= fstat(fd
, sbuf
);
322 /* we always want directories to appear zero size */
323 if (ret
== 0 && S_ISDIR(sbuf
->st_mode
)) sbuf
->st_size
= 0;
327 /*******************************************************************
328 An lstat() wrapper that will deal with 64 bit filesizes.
329 ********************************************************************/
331 int sys_lstat(const char *fname
,SMB_STRUCT_STAT
*sbuf
)
334 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_LSTAT64)
335 ret
= lstat64(fname
, sbuf
);
337 ret
= lstat(fname
, sbuf
);
339 /* we always want directories to appear zero size */
340 if (ret
== 0 && S_ISDIR(sbuf
->st_mode
)) sbuf
->st_size
= 0;
344 /*******************************************************************
345 An ftruncate() wrapper that will deal with 64 bit filesizes.
346 ********************************************************************/
348 int sys_ftruncate(int fd
, SMB_OFF_T offset
)
350 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_FTRUNCATE64)
351 return ftruncate64(fd
, offset
);
353 return ftruncate(fd
, offset
);
357 /*******************************************************************
358 An lseek() wrapper that will deal with 64 bit filesizes.
359 ********************************************************************/
361 SMB_OFF_T
sys_lseek(int fd
, SMB_OFF_T offset
, int whence
)
363 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_LSEEK64)
364 return lseek64(fd
, offset
, whence
);
366 return lseek(fd
, offset
, whence
);
370 /*******************************************************************
371 An fseek() wrapper that will deal with 64 bit filesizes.
372 ********************************************************************/
374 int sys_fseek(FILE *fp
, SMB_OFF_T offset
, int whence
)
376 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(LARGE_SMB_OFF_T) && defined(HAVE_FSEEK64)
377 return fseek64(fp
, offset
, whence
);
378 #elif defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(LARGE_SMB_OFF_T) && defined(HAVE_FSEEKO64)
379 return fseeko64(fp
, offset
, whence
);
381 return fseek(fp
, offset
, whence
);
385 /*******************************************************************
386 An ftell() wrapper that will deal with 64 bit filesizes.
387 ********************************************************************/
389 SMB_OFF_T
sys_ftell(FILE *fp
)
391 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(LARGE_SMB_OFF_T) && defined(HAVE_FTELL64)
392 return (SMB_OFF_T
)ftell64(fp
);
393 #elif defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(LARGE_SMB_OFF_T) && defined(HAVE_FTELLO64)
394 return (SMB_OFF_T
)ftello64(fp
);
396 return (SMB_OFF_T
)ftell(fp
);
400 /*******************************************************************
401 A creat() wrapper that will deal with 64 bit filesizes.
402 ********************************************************************/
404 int sys_creat(const char *path
, mode_t mode
)
406 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_CREAT64)
407 return creat64(path
, mode
);
410 * If creat64 isn't defined then ensure we call a potential open64.
413 return sys_open(path
, O_WRONLY
| O_CREAT
| O_TRUNC
, mode
);
417 /*******************************************************************
418 An open() wrapper that will deal with 64 bit filesizes.
419 ********************************************************************/
421 int sys_open(const char *path
, int oflag
, mode_t mode
)
423 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OPEN64)
424 return open64(path
, oflag
, mode
);
426 return open(path
, oflag
, mode
);
430 /*******************************************************************
431 An fopen() wrapper that will deal with 64 bit filesizes.
432 ********************************************************************/
434 FILE *sys_fopen(const char *path
, const char *type
)
436 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_FOPEN64)
437 return fopen64(path
, type
);
439 return fopen(path
, type
);
444 /*******************************************************************
445 A flock() wrapper that will perform the kernel flock.
446 ********************************************************************/
448 void kernel_flock(int fd
, uint32 share_mode
)
450 #if HAVE_KERNEL_SHARE_MODES
452 if (share_mode
== FILE_SHARE_WRITE
) {
453 kernel_mode
= LOCK_MAND
|LOCK_WRITE
;
454 } else if (share_mode
== FILE_SHARE_READ
) {
455 kernel_mode
= LOCK_MAND
|LOCK_READ
;
456 } else if (share_mode
== FILE_SHARE_NONE
) {
457 kernel_mode
= LOCK_MAND
;
460 flock(fd
, kernel_mode
);
468 /*******************************************************************
469 An opendir wrapper that will deal with 64 bit filesizes.
470 ********************************************************************/
472 SMB_STRUCT_DIR
*sys_opendir(const char *name
)
474 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OPENDIR64)
475 return opendir64(name
);
477 return opendir(name
);
481 /*******************************************************************
482 A readdir wrapper that will deal with 64 bit filesizes.
483 ********************************************************************/
485 SMB_STRUCT_DIRENT
*sys_readdir(SMB_STRUCT_DIR
*dirp
)
487 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_READDIR64)
488 return readdir64(dirp
);
490 return readdir(dirp
);
494 /*******************************************************************
495 A seekdir wrapper that will deal with 64 bit filesizes.
496 ********************************************************************/
498 void sys_seekdir(SMB_STRUCT_DIR
*dirp
, long offset
)
500 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_SEEKDIR64)
501 seekdir64(dirp
, offset
);
503 seekdir(dirp
, offset
);
507 /*******************************************************************
508 A telldir wrapper that will deal with 64 bit filesizes.
509 ********************************************************************/
511 long sys_telldir(SMB_STRUCT_DIR
*dirp
)
513 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_TELLDIR64)
514 return (long)telldir64(dirp
);
516 return (long)telldir(dirp
);
520 /*******************************************************************
521 A rewinddir wrapper that will deal with 64 bit filesizes.
522 ********************************************************************/
524 void sys_rewinddir(SMB_STRUCT_DIR
*dirp
)
526 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_REWINDDIR64)
533 /*******************************************************************
534 A close wrapper that will deal with 64 bit filesizes.
535 ********************************************************************/
537 int sys_closedir(SMB_STRUCT_DIR
*dirp
)
539 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_CLOSEDIR64)
540 return closedir64(dirp
);
542 return closedir(dirp
);
546 /*******************************************************************
547 An mknod() wrapper that will deal with 64 bit filesizes.
548 ********************************************************************/
550 int sys_mknod(const char *path
, mode_t mode
, SMB_DEV_T dev
)
552 #if defined(HAVE_MKNOD) || defined(HAVE_MKNOD64)
553 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_MKNOD64) && defined(HAVE_DEV64_T)
554 return mknod64(path
, mode
, dev
);
556 return mknod(path
, mode
, dev
);
559 /* No mknod system call. */
565 /*******************************************************************
566 The wait() calls vary between systems
567 ********************************************************************/
569 int sys_waitpid(pid_t pid
,int *status
,int options
)
572 return waitpid(pid
,status
,options
);
573 #else /* HAVE_WAITPID */
574 return wait4(pid
, status
, options
, NULL
);
575 #endif /* HAVE_WAITPID */
578 /*******************************************************************
579 System wrapper for getwd
580 ********************************************************************/
582 char *sys_getwd(char *s
)
586 wd
= (char *)getcwd(s
, PATH_MAX
);
588 wd
= (char *)getwd(s
);
593 #if defined(HAVE_POSIX_CAPABILITIES)
595 /**************************************************************************
596 Try and abstract process capabilities (for systems that have them).
597 ****************************************************************************/
599 /* Set the POSIX capabilities needed for the given purpose into the effective
600 * capability set of the current process. Make sure they are always removed
601 * from the inheritable set, because there is no circumstance in which our
602 * children should inherit our elevated privileges.
604 static bool set_process_capability(enum smbd_capability capability
,
607 cap_value_t cap_vals
[2] = {0};
608 int num_cap_vals
= 0;
612 #if defined(HAVE_PRCTL) && defined(PR_GET_KEEPCAPS) && defined(PR_SET_KEEPCAPS)
613 /* On Linux, make sure that any capabilities we grab are sticky
614 * across UID changes. We expect that this would allow us to keep both
615 * the effective and permitted capability sets, but as of circa 2.6.16,
616 * only the permitted set is kept. It is a bug (which we work around)
617 * that the effective set is lost, but we still require the effective
620 if (!prctl(PR_GET_KEEPCAPS
)) {
621 prctl(PR_SET_KEEPCAPS
, 1);
625 cap
= cap_get_proc();
627 DEBUG(0,("set_process_capability: cap_get_proc failed: %s\n",
632 switch (capability
) {
633 case KERNEL_OPLOCK_CAPABILITY
:
634 #ifdef CAP_NETWORK_MGT
635 /* IRIX has CAP_NETWORK_MGT for oplocks. */
636 cap_vals
[num_cap_vals
++] = CAP_NETWORK_MGT
;
639 case DMAPI_ACCESS_CAPABILITY
:
640 #ifdef CAP_DEVICE_MGT
641 /* IRIX has CAP_DEVICE_MGT for DMAPI access. */
642 cap_vals
[num_cap_vals
++] = CAP_DEVICE_MGT
;
644 /* Linux has CAP_MKNOD for DMAPI access. */
645 cap_vals
[num_cap_vals
++] = CAP_MKNOD
;
648 case LEASE_CAPABILITY
:
650 cap_vals
[num_cap_vals
++] = CAP_LEASE
;
655 SMB_ASSERT(num_cap_vals
<= ARRAY_SIZE(cap_vals
));
657 if (num_cap_vals
== 0) {
662 cap_set_flag(cap
, CAP_EFFECTIVE
, num_cap_vals
, cap_vals
,
663 enable
? CAP_SET
: CAP_CLEAR
);
665 /* We never want to pass capabilities down to our children, so make
666 * sure they are not inherited.
668 cap_set_flag(cap
, CAP_INHERITABLE
, num_cap_vals
, cap_vals
, CAP_CLEAR
);
670 if (cap_set_proc(cap
) == -1) {
671 DEBUG(0, ("set_process_capability: cap_set_proc failed: %s\n",
681 #endif /* HAVE_POSIX_CAPABILITIES */
683 /****************************************************************************
684 Gain the oplock capability from the kernel if possible.
685 ****************************************************************************/
687 void set_effective_capability(enum smbd_capability capability
)
689 #if defined(HAVE_POSIX_CAPABILITIES)
690 set_process_capability(capability
, True
);
691 #endif /* HAVE_POSIX_CAPABILITIES */
694 void drop_effective_capability(enum smbd_capability capability
)
696 #if defined(HAVE_POSIX_CAPABILITIES)
697 set_process_capability(capability
, False
);
698 #endif /* HAVE_POSIX_CAPABILITIES */
701 /**************************************************************************
702 Wrapper for random().
703 ****************************************************************************/
705 long sys_random(void)
707 #if defined(HAVE_RANDOM)
708 return (long)random();
709 #elif defined(HAVE_RAND)
712 DEBUG(0,("Error - no random function available !\n"));
717 /**************************************************************************
718 Wrapper for srandom().
719 ****************************************************************************/
721 void sys_srandom(unsigned int seed
)
723 #if defined(HAVE_SRANDOM)
725 #elif defined(HAVE_SRAND)
728 DEBUG(0,("Error - no srandom function available !\n"));
733 /**************************************************************************
734 Returns equivalent to NGROUPS_MAX - using sysconf if needed.
735 ****************************************************************************/
739 #if defined(SYSCONF_SC_NGROUPS_MAX)
740 int ret
= sysconf(_SC_NGROUPS_MAX
);
741 return (ret
== -1) ? NGROUPS_MAX
: ret
;
747 /**************************************************************************
748 Wrap setgroups and getgroups for systems that declare getgroups() as
749 returning an array of gid_t, but actuall return an array of int.
750 ****************************************************************************/
752 #if defined(HAVE_BROKEN_GETGROUPS)
753 static int sys_broken_getgroups(int setlen
, gid_t
*gidset
)
760 return getgroups(setlen
, &gid
);
764 * Broken case. We need to allocate a
765 * GID_T array of size setlen.
774 setlen
= groups_max();
776 if((group_list
= SMB_MALLOC_ARRAY(GID_T
, setlen
)) == NULL
) {
777 DEBUG(0,("sys_getgroups: Malloc fail.\n"));
781 if((ngroups
= getgroups(setlen
, group_list
)) < 0) {
782 int saved_errno
= errno
;
783 SAFE_FREE(group_list
);
788 for(i
= 0; i
< ngroups
; i
++)
789 gidset
[i
] = (gid_t
)group_list
[i
];
791 SAFE_FREE(group_list
);
795 static int sys_broken_setgroups(int setlen
, gid_t
*gidset
)
803 if (setlen
< 0 || setlen
> groups_max()) {
809 * Broken case. We need to allocate a
810 * GID_T array of size setlen.
813 if((group_list
= SMB_MALLOC_ARRAY(GID_T
, setlen
)) == NULL
) {
814 DEBUG(0,("sys_setgroups: Malloc fail.\n"));
818 for(i
= 0; i
< setlen
; i
++)
819 group_list
[i
] = (GID_T
) gidset
[i
];
821 if(setgroups(setlen
, group_list
) != 0) {
822 int saved_errno
= errno
;
823 SAFE_FREE(group_list
);
828 SAFE_FREE(group_list
);
832 #endif /* HAVE_BROKEN_GETGROUPS */
834 /* This is a list of systems that require the first GID passed to setgroups(2)
835 * to be the effective GID. If your system is one of these, add it here.
837 #if defined (FREEBSD) || defined (DARWINOS)
838 #define USE_BSD_SETGROUPS
841 #if defined(USE_BSD_SETGROUPS)
842 /* Depending on the particular BSD implementation, the first GID that is
843 * passed to setgroups(2) will either be ignored or will set the credential's
844 * effective GID. In either case, the right thing to do is to guarantee that
845 * gidset[0] is the effective GID.
847 static int sys_bsd_setgroups(gid_t primary_gid
, int setlen
, const gid_t
*gidset
)
849 gid_t
*new_gidset
= NULL
;
853 /* setgroups(2) will fail with EINVAL if we pass too many groups. */
856 /* No group list, just make sure we are setting the efective GID. */
858 return setgroups(1, &primary_gid
);
861 /* If the primary gid is not the first array element, grow the array
862 * and insert it at the front.
864 if (gidset
[0] != primary_gid
) {
865 new_gidset
= SMB_MALLOC_ARRAY(gid_t
, setlen
+ 1);
866 if (new_gidset
== NULL
) {
870 memcpy(new_gidset
+ 1, gidset
, (setlen
* sizeof(gid_t
)));
871 new_gidset
[0] = primary_gid
;
876 DEBUG(3, ("forced to truncate group list from %d to %d\n",
881 #if defined(HAVE_BROKEN_GETGROUPS)
882 ret
= sys_broken_setgroups(setlen
, new_gidset
? new_gidset
: gidset
);
884 ret
= setgroups(setlen
, new_gidset
? new_gidset
: gidset
);
889 SAFE_FREE(new_gidset
);
896 #endif /* USE_BSD_SETGROUPS */
898 /**************************************************************************
899 Wrapper for getgroups. Deals with broken (int) case.
900 ****************************************************************************/
902 int sys_getgroups(int setlen
, gid_t
*gidset
)
904 #if defined(HAVE_BROKEN_GETGROUPS)
905 return sys_broken_getgroups(setlen
, gidset
);
907 return getgroups(setlen
, gidset
);
911 /**************************************************************************
912 Wrapper for setgroups. Deals with broken (int) case and BSD case.
913 ****************************************************************************/
915 int sys_setgroups(gid_t
UNUSED(primary_gid
), int setlen
, gid_t
*gidset
)
917 #if !defined(HAVE_SETGROUPS)
920 #endif /* HAVE_SETGROUPS */
922 #if defined(USE_BSD_SETGROUPS)
923 return sys_bsd_setgroups(primary_gid
, setlen
, gidset
);
924 #elif defined(HAVE_BROKEN_GETGROUPS)
925 return sys_broken_setgroups(setlen
, gidset
);
927 return setgroups(setlen
, gidset
);
931 /**************************************************************************
932 Wrappers for setpwent(), getpwent() and endpwent()
933 ****************************************************************************/
935 void sys_setpwent(void)
940 struct passwd
*sys_getpwent(void)
945 void sys_endpwent(void)
950 /**************************************************************************
951 Wrappers for getpwnam(), getpwuid(), getgrnam(), getgrgid()
952 ****************************************************************************/
955 struct passwd
*sys_getpwnam(const char *name
)
957 return getpwnam(name
);
960 struct passwd
*sys_getpwuid(uid_t uid
)
962 return getpwuid(uid
);
965 struct group
*sys_getgrnam(const char *name
)
967 return getgrnam(name
);
970 struct group
*sys_getgrgid(gid_t gid
)
972 return getgrgid(gid
);
975 /**************************************************************************
976 Extract a command into an arg list.
977 ****************************************************************************/
979 static char **extract_args(TALLOC_CTX
*mem_ctx
, const char *command
)
988 if (!(trunc_cmd
= talloc_strdup(mem_ctx
, command
))) {
989 DEBUG(0, ("talloc failed\n"));
993 if(!(ptr
= strtok_r(trunc_cmd
, " \t", &saveptr
))) {
994 TALLOC_FREE(trunc_cmd
);
1003 for( argcl
= 1; ptr
; ptr
= strtok_r(NULL
, " \t", &saveptr
))
1006 TALLOC_FREE(trunc_cmd
);
1008 if (!(argl
= TALLOC_ARRAY(mem_ctx
, char *, argcl
+ 1))) {
1013 * Now do the extraction.
1016 if (!(trunc_cmd
= talloc_strdup(mem_ctx
, command
))) {
1020 ptr
= strtok_r(trunc_cmd
, " \t", &saveptr
);
1023 if (!(argl
[i
++] = talloc_strdup(argl
, ptr
))) {
1027 while((ptr
= strtok_r(NULL
, " \t", &saveptr
)) != NULL
) {
1029 if (!(argl
[i
++] = talloc_strdup(argl
, ptr
))) {
1038 DEBUG(0, ("talloc failed\n"));
1039 TALLOC_FREE(trunc_cmd
);
1045 /**************************************************************************
1046 Wrapper for popen. Safer as it doesn't search a path.
1047 Modified from the glibc sources.
1048 modified by tridge to return a file descriptor. We must kick our FILE* habit
1049 ****************************************************************************/
1051 typedef struct _popen_list
1055 struct _popen_list
*next
;
1058 static popen_list
*popen_chain
;
1060 int sys_popen(const char *command
)
1062 int parent_end
, child_end
;
1064 popen_list
*entry
= NULL
;
1067 if (pipe(pipe_fds
) < 0)
1070 parent_end
= pipe_fds
[0];
1071 child_end
= pipe_fds
[1];
1078 if((entry
= SMB_MALLOC_P(popen_list
)) == NULL
)
1081 ZERO_STRUCTP(entry
);
1084 * Extract the command and args into a NULL terminated array.
1087 if(!(argl
= extract_args(NULL
, command
)))
1090 entry
->child_pid
= sys_fork();
1092 if (entry
->child_pid
== -1) {
1096 if (entry
->child_pid
== 0) {
1102 int child_std_end
= STDOUT_FILENO
;
1106 if (child_end
!= child_std_end
) {
1107 dup2 (child_end
, child_std_end
);
1112 * POSIX.2: "popen() shall ensure that any streams from previous
1113 * popen() calls that remain open in the parent process are closed
1114 * in the new child process."
1117 for (p
= popen_chain
; p
; p
= p
->next
)
1120 execv(argl
[0], argl
);
1131 /* Link into popen_chain. */
1132 entry
->next
= popen_chain
;
1133 popen_chain
= entry
;
1134 entry
->fd
= parent_end
;
1147 /**************************************************************************
1148 Wrapper for pclose. Modified from the glibc sources.
1149 ****************************************************************************/
1151 int sys_pclose(int fd
)
1154 popen_list
**ptr
= &popen_chain
;
1155 popen_list
*entry
= NULL
;
1159 /* Unlink from popen_chain. */
1160 for ( ; *ptr
!= NULL
; ptr
= &(*ptr
)->next
) {
1161 if ((*ptr
)->fd
== fd
) {
1163 *ptr
= (*ptr
)->next
;
1169 if (status
< 0 || close(entry
->fd
) < 0)
1173 * As Samba is catching and eating child process
1174 * exits we don't really care about the child exit
1175 * code, a -1 with errno = ECHILD will do fine for us.
1179 wait_pid
= sys_waitpid (entry
->child_pid
, &wstatus
, 0);
1180 } while (wait_pid
== -1 && errno
== EINTR
);
1189 /**************************************************************************
1190 Wrapper for Admin Logs.
1191 ****************************************************************************/
1193 void sys_adminlog(int priority
, const char *format_str
, ...)
1197 char *msgbuf
= NULL
;
1199 va_start( ap
, format_str
);
1200 ret
= vasprintf( &msgbuf
, format_str
, ap
);
1206 #if defined(HAVE_SYSLOG)
1207 syslog( priority
, "%s", msgbuf
);
1209 DEBUG(0,("%s", msgbuf
));
1214 /******** Solaris EA helper function prototypes ********/
1215 #ifdef HAVE_ATTROPEN
1216 #define SOLARIS_ATTRMODE S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP
1217 static int solaris_write_xattr(int attrfd
, const char *value
, size_t size
);
1218 static ssize_t
solaris_read_xattr(int attrfd
, void *value
, size_t size
);
1219 static ssize_t
solaris_list_xattr(int attrdirfd
, char *list
, size_t size
);
1220 static int solaris_unlinkat(int attrdirfd
, const char *name
);
1221 static int solaris_attropen(const char *path
, const char *attrpath
, int oflag
, mode_t mode
);
1222 static int solaris_openat(int fildes
, const char *path
, int oflag
, mode_t mode
);
1225 /**************************************************************************
1226 Wrappers for extented attribute calls. Based on the Linux package with
1227 support for IRIX and (Net|Free)BSD also. Expand as other systems have them.
1228 ****************************************************************************/
1230 ssize_t
sys_getxattr (const char *path
, const char *name
, void *value
, size_t size
)
1232 #if defined(HAVE_GETXATTR)
1233 #ifndef XATTR_ADD_OPT
1234 return getxattr(path
, name
, value
, size
);
1237 return getxattr(path
, name
, value
, size
, 0, options
);
1239 #elif defined(HAVE_GETEA)
1240 return getea(path
, name
, value
, size
);
1241 #elif defined(HAVE_EXTATTR_GET_FILE)
1244 int attrnamespace
= (strncmp(name
, "system", 6) == 0) ?
1245 EXTATTR_NAMESPACE_SYSTEM
: EXTATTR_NAMESPACE_USER
;
1246 const char *attrname
= ((s
=strchr_m(name
, '.')) == NULL
) ? name
: s
+ 1;
1248 * The BSD implementation has a nasty habit of silently truncating
1249 * the returned value to the size of the buffer, so we have to check
1250 * that the buffer is large enough to fit the returned value.
1252 if((retval
=extattr_get_file(path
, attrnamespace
, attrname
, NULL
, 0)) >= 0) {
1257 if((retval
=extattr_get_file(path
, attrnamespace
, attrname
, value
, size
)) >= 0)
1261 DEBUG(10,("sys_getxattr: extattr_get_file() failed with: %s\n", strerror(errno
)));
1263 #elif defined(HAVE_ATTR_GET)
1264 int retval
, flags
= 0;
1265 int valuelength
= (int)size
;
1266 char *attrname
= strchr(name
,'.') + 1;
1268 if (strncmp(name
, "system", 6) == 0) flags
|= ATTR_ROOT
;
1270 retval
= attr_get(path
, attrname
, (char *)value
, &valuelength
, flags
);
1272 return retval
? retval
: valuelength
;
1273 #elif defined(HAVE_ATTROPEN)
1275 int attrfd
= solaris_attropen(path
, name
, O_RDONLY
, 0);
1277 ret
= solaris_read_xattr(attrfd
, value
, size
);
1287 ssize_t
sys_lgetxattr (const char *path
, const char *name
, void *value
, size_t size
)
1289 #if defined(HAVE_LGETXATTR)
1290 return lgetxattr(path
, name
, value
, size
);
1291 #elif defined(HAVE_GETXATTR) && defined(XATTR_ADD_OPT)
1292 int options
= XATTR_NOFOLLOW
;
1293 return getxattr(path
, name
, value
, size
, 0, options
);
1294 #elif defined(HAVE_LGETEA)
1295 return lgetea(path
, name
, value
, size
);
1296 #elif defined(HAVE_EXTATTR_GET_LINK)
1299 int attrnamespace
= (strncmp(name
, "system", 6) == 0) ?
1300 EXTATTR_NAMESPACE_SYSTEM
: EXTATTR_NAMESPACE_USER
;
1301 const char *attrname
= ((s
=strchr_m(name
, '.')) == NULL
) ? name
: s
+ 1;
1303 if((retval
=extattr_get_link(path
, attrnamespace
, attrname
, NULL
, 0)) >= 0) {
1308 if((retval
=extattr_get_link(path
, attrnamespace
, attrname
, value
, size
)) >= 0)
1312 DEBUG(10,("sys_lgetxattr: extattr_get_link() failed with: %s\n", strerror(errno
)));
1314 #elif defined(HAVE_ATTR_GET)
1315 int retval
, flags
= ATTR_DONTFOLLOW
;
1316 int valuelength
= (int)size
;
1317 char *attrname
= strchr(name
,'.') + 1;
1319 if (strncmp(name
, "system", 6) == 0) flags
|= ATTR_ROOT
;
1321 retval
= attr_get(path
, attrname
, (char *)value
, &valuelength
, flags
);
1323 return retval
? retval
: valuelength
;
1324 #elif defined(HAVE_ATTROPEN)
1326 int attrfd
= solaris_attropen(path
, name
, O_RDONLY
|AT_SYMLINK_NOFOLLOW
, 0);
1328 ret
= solaris_read_xattr(attrfd
, value
, size
);
1338 ssize_t
sys_fgetxattr (int filedes
, const char *name
, void *value
, size_t size
)
1340 #if defined(HAVE_FGETXATTR)
1341 #ifndef XATTR_ADD_OPT
1342 return fgetxattr(filedes
, name
, value
, size
);
1345 return fgetxattr(filedes
, name
, value
, size
, 0, options
);
1347 #elif defined(HAVE_FGETEA)
1348 return fgetea(filedes
, name
, value
, size
);
1349 #elif defined(HAVE_EXTATTR_GET_FD)
1352 int attrnamespace
= (strncmp(name
, "system", 6) == 0) ?
1353 EXTATTR_NAMESPACE_SYSTEM
: EXTATTR_NAMESPACE_USER
;
1354 const char *attrname
= ((s
=strchr_m(name
, '.')) == NULL
) ? name
: s
+ 1;
1356 if((retval
=extattr_get_fd(filedes
, attrnamespace
, attrname
, NULL
, 0)) >= 0) {
1361 if((retval
=extattr_get_fd(filedes
, attrnamespace
, attrname
, value
, size
)) >= 0)
1365 DEBUG(10,("sys_fgetxattr: extattr_get_fd() failed with: %s\n", strerror(errno
)));
1367 #elif defined(HAVE_ATTR_GETF)
1368 int retval
, flags
= 0;
1369 int valuelength
= (int)size
;
1370 char *attrname
= strchr(name
,'.') + 1;
1372 if (strncmp(name
, "system", 6) == 0) flags
|= ATTR_ROOT
;
1374 retval
= attr_getf(filedes
, attrname
, (char *)value
, &valuelength
, flags
);
1376 return retval
? retval
: valuelength
;
1377 #elif defined(HAVE_ATTROPEN)
1379 int attrfd
= solaris_openat(filedes
, name
, O_RDONLY
|O_XATTR
, 0);
1381 ret
= solaris_read_xattr(attrfd
, value
, size
);
1391 #if defined(HAVE_EXTATTR_LIST_FILE)
1393 #define EXTATTR_PREFIX(s) (s), (sizeof((s))-1)
1401 { EXTATTR_NAMESPACE_SYSTEM
, EXTATTR_PREFIX("system.") },
1402 { EXTATTR_NAMESPACE_USER
, EXTATTR_PREFIX("user.") },
1410 static ssize_t
bsd_attr_list (int type
, extattr_arg arg
, char *list
, size_t size
)
1412 ssize_t list_size
, total_size
= 0;
1415 /* Iterate through extattr(2) namespaces */
1416 for(t
= 0; t
< (sizeof(extattr
)/sizeof(extattr
[0])); t
++) {
1418 #if defined(HAVE_EXTATTR_LIST_FILE)
1420 list_size
= extattr_list_file(arg
.path
, extattr
[t
].space
, list
, size
);
1423 #if defined(HAVE_EXTATTR_LIST_LINK)
1425 list_size
= extattr_list_link(arg
.path
, extattr
[t
].space
, list
, size
);
1428 #if defined(HAVE_EXTATTR_LIST_FD)
1430 list_size
= extattr_list_fd(arg
.filedes
, extattr
[t
].space
, list
, size
);
1437 /* Some error happend. Errno should be set by the previous call */
1443 /* XXX: Call with an empty buffer may be used to calculate
1444 necessary buffer size. Unfortunately, we can't say, how
1445 many attributes were returned, so here is the potential
1446 problem with the emulation.
1449 /* Take the worse case of one char attribute names -
1450 two bytes per name plus one more for sanity.
1452 total_size
+= list_size
+ (list_size
/2 + 1)*extattr
[t
].len
;
1455 /* Count necessary offset to fit namespace prefixes */
1457 for(i
= 0; i
< list_size
; i
+= list
[i
] + 1)
1458 len
+= extattr
[t
].len
;
1460 total_size
+= list_size
+ len
;
1461 /* Buffer is too small to fit the results */
1462 if(total_size
> size
) {
1466 /* Shift results back, so we can prepend prefixes */
1467 buf
= memmove(list
+ len
, list
, list_size
);
1469 for(i
= 0; i
< list_size
; i
+= len
+ 1) {
1471 strncpy(list
, extattr
[t
].name
, extattr
[t
].len
+ 1);
1472 list
+= extattr
[t
].len
;
1473 strncpy(list
, buf
+ i
+ 1, len
);
1484 #if defined(HAVE_ATTR_LIST) && defined(HAVE_SYS_ATTRIBUTES_H)
1485 static char attr_buffer
[ATTR_MAX_VALUELEN
];
1487 static ssize_t
irix_attr_list(const char *path
, int filedes
, char *list
, size_t size
, int flags
)
1489 int retval
= 0, index
;
1490 attrlist_cursor_t
*cursor
= 0;
1492 attrlist_t
* al
= (attrlist_t
*)attr_buffer
;
1494 size_t ent_size
, left
= size
;
1499 retval
= attr_listf(filedes
, attr_buffer
, ATTR_MAX_VALUELEN
, flags
, cursor
);
1501 retval
= attr_list(path
, attr_buffer
, ATTR_MAX_VALUELEN
, flags
, cursor
);
1503 for (index
= 0; index
< al
->al_count
; index
++) {
1504 ae
= ATTR_ENTRY(attr_buffer
, index
);
1505 ent_size
= strlen(ae
->a_name
) + sizeof("user.");
1506 if (left
>= ent_size
) {
1507 strncpy(bp
, "user.", sizeof("user."));
1508 strncat(bp
, ae
->a_name
, ent_size
- sizeof("user."));
1516 total_size
+= ent_size
;
1518 if (al
->al_more
== 0) break;
1525 retval
= attr_listf(filedes
, attr_buffer
, ATTR_MAX_VALUELEN
, flags
, cursor
);
1527 retval
= attr_list(path
, attr_buffer
, ATTR_MAX_VALUELEN
, flags
, cursor
);
1529 for (index
= 0; index
< al
->al_count
; index
++) {
1530 ae
= ATTR_ENTRY(attr_buffer
, index
);
1531 ent_size
= strlen(ae
->a_name
) + sizeof("system.");
1532 if (left
>= ent_size
) {
1533 strncpy(bp
, "system.", sizeof("system."));
1534 strncat(bp
, ae
->a_name
, ent_size
- sizeof("system."));
1542 total_size
+= ent_size
;
1544 if (al
->al_more
== 0) break;
1547 return (ssize_t
)(retval
? retval
: total_size
);
1552 ssize_t
sys_listxattr (const char *path
, char *list
, size_t size
)
1554 #if defined(HAVE_LISTXATTR)
1555 #ifndef XATTR_ADD_OPT
1556 return listxattr(path
, list
, size
);
1559 return listxattr(path
, list
, size
, options
);
1561 #elif defined(HAVE_LISTEA)
1562 return listea(path
, list
, size
);
1563 #elif defined(HAVE_EXTATTR_LIST_FILE)
1566 return bsd_attr_list(0, arg
, list
, size
);
1567 #elif defined(HAVE_ATTR_LIST) && defined(HAVE_SYS_ATTRIBUTES_H)
1568 return irix_attr_list(path
, 0, list
, size
, 0);
1569 #elif defined(HAVE_ATTROPEN)
1571 int attrdirfd
= solaris_attropen(path
, ".", O_RDONLY
, 0);
1572 if (attrdirfd
>= 0) {
1573 ret
= solaris_list_xattr(attrdirfd
, list
, size
);
1583 ssize_t
sys_llistxattr (const char *path
, char *list
, size_t size
)
1585 #if defined(HAVE_LLISTXATTR)
1586 return llistxattr(path
, list
, size
);
1587 #elif defined(HAVE_LISTXATTR) && defined(XATTR_ADD_OPT)
1588 int options
= XATTR_NOFOLLOW
;
1589 return listxattr(path
, list
, size
, options
);
1590 #elif defined(HAVE_LLISTEA)
1591 return llistea(path
, list
, size
);
1592 #elif defined(HAVE_EXTATTR_LIST_LINK)
1595 return bsd_attr_list(1, arg
, list
, size
);
1596 #elif defined(HAVE_ATTR_LIST) && defined(HAVE_SYS_ATTRIBUTES_H)
1597 return irix_attr_list(path
, 0, list
, size
, ATTR_DONTFOLLOW
);
1598 #elif defined(HAVE_ATTROPEN)
1600 int attrdirfd
= solaris_attropen(path
, ".", O_RDONLY
|AT_SYMLINK_NOFOLLOW
, 0);
1601 if (attrdirfd
>= 0) {
1602 ret
= solaris_list_xattr(attrdirfd
, list
, size
);
1612 ssize_t
sys_flistxattr (int filedes
, char *list
, size_t size
)
1614 #if defined(HAVE_FLISTXATTR)
1615 #ifndef XATTR_ADD_OPT
1616 return flistxattr(filedes
, list
, size
);
1619 return flistxattr(filedes
, list
, size
, options
);
1621 #elif defined(HAVE_FLISTEA)
1622 return flistea(filedes
, list
, size
);
1623 #elif defined(HAVE_EXTATTR_LIST_FD)
1625 arg
.filedes
= filedes
;
1626 return bsd_attr_list(2, arg
, list
, size
);
1627 #elif defined(HAVE_ATTR_LISTF)
1628 return irix_attr_list(NULL
, filedes
, list
, size
, 0);
1629 #elif defined(HAVE_ATTROPEN)
1631 int attrdirfd
= solaris_openat(filedes
, ".", O_RDONLY
|O_XATTR
, 0);
1632 if (attrdirfd
>= 0) {
1633 ret
= solaris_list_xattr(attrdirfd
, list
, size
);
1643 int sys_removexattr (const char *path
, const char *name
)
1645 #if defined(HAVE_REMOVEXATTR)
1646 #ifndef XATTR_ADD_OPT
1647 return removexattr(path
, name
);
1650 return removexattr(path
, name
, options
);
1652 #elif defined(HAVE_REMOVEEA)
1653 return removeea(path
, name
);
1654 #elif defined(HAVE_EXTATTR_DELETE_FILE)
1656 int attrnamespace
= (strncmp(name
, "system", 6) == 0) ?
1657 EXTATTR_NAMESPACE_SYSTEM
: EXTATTR_NAMESPACE_USER
;
1658 const char *attrname
= ((s
=strchr_m(name
, '.')) == NULL
) ? name
: s
+ 1;
1660 return extattr_delete_file(path
, attrnamespace
, attrname
);
1661 #elif defined(HAVE_ATTR_REMOVE)
1663 char *attrname
= strchr(name
,'.') + 1;
1665 if (strncmp(name
, "system", 6) == 0) flags
|= ATTR_ROOT
;
1667 return attr_remove(path
, attrname
, flags
);
1668 #elif defined(HAVE_ATTROPEN)
1670 int attrdirfd
= solaris_attropen(path
, ".", O_RDONLY
, 0);
1671 if (attrdirfd
>= 0) {
1672 ret
= solaris_unlinkat(attrdirfd
, name
);
1682 int sys_lremovexattr (const char *path
, const char *name
)
1684 #if defined(HAVE_LREMOVEXATTR)
1685 return lremovexattr(path
, name
);
1686 #elif defined(HAVE_REMOVEXATTR) && defined(XATTR_ADD_OPT)
1687 int options
= XATTR_NOFOLLOW
;
1688 return removexattr(path
, name
, options
);
1689 #elif defined(HAVE_LREMOVEEA)
1690 return lremoveea(path
, name
);
1691 #elif defined(HAVE_EXTATTR_DELETE_LINK)
1693 int attrnamespace
= (strncmp(name
, "system", 6) == 0) ?
1694 EXTATTR_NAMESPACE_SYSTEM
: EXTATTR_NAMESPACE_USER
;
1695 const char *attrname
= ((s
=strchr_m(name
, '.')) == NULL
) ? name
: s
+ 1;
1697 return extattr_delete_link(path
, attrnamespace
, attrname
);
1698 #elif defined(HAVE_ATTR_REMOVE)
1699 int flags
= ATTR_DONTFOLLOW
;
1700 char *attrname
= strchr(name
,'.') + 1;
1702 if (strncmp(name
, "system", 6) == 0) flags
|= ATTR_ROOT
;
1704 return attr_remove(path
, attrname
, flags
);
1705 #elif defined(HAVE_ATTROPEN)
1707 int attrdirfd
= solaris_attropen(path
, ".", O_RDONLY
|AT_SYMLINK_NOFOLLOW
, 0);
1708 if (attrdirfd
>= 0) {
1709 ret
= solaris_unlinkat(attrdirfd
, name
);
1719 int sys_fremovexattr (int filedes
, const char *name
)
1721 #if defined(HAVE_FREMOVEXATTR)
1722 #ifndef XATTR_ADD_OPT
1723 return fremovexattr(filedes
, name
);
1726 return fremovexattr(filedes
, name
, options
);
1728 #elif defined(HAVE_FREMOVEEA)
1729 return fremoveea(filedes
, name
);
1730 #elif defined(HAVE_EXTATTR_DELETE_FD)
1732 int attrnamespace
= (strncmp(name
, "system", 6) == 0) ?
1733 EXTATTR_NAMESPACE_SYSTEM
: EXTATTR_NAMESPACE_USER
;
1734 const char *attrname
= ((s
=strchr_m(name
, '.')) == NULL
) ? name
: s
+ 1;
1736 return extattr_delete_fd(filedes
, attrnamespace
, attrname
);
1737 #elif defined(HAVE_ATTR_REMOVEF)
1739 char *attrname
= strchr(name
,'.') + 1;
1741 if (strncmp(name
, "system", 6) == 0) flags
|= ATTR_ROOT
;
1743 return attr_removef(filedes
, attrname
, flags
);
1744 #elif defined(HAVE_ATTROPEN)
1746 int attrdirfd
= solaris_openat(filedes
, ".", O_RDONLY
|O_XATTR
, 0);
1747 if (attrdirfd
>= 0) {
1748 ret
= solaris_unlinkat(attrdirfd
, name
);
1758 int sys_setxattr (const char *path
, const char *name
, const void *value
, size_t size
, int flags
)
1760 #if defined(HAVE_SETXATTR)
1761 #ifndef XATTR_ADD_OPT
1762 return setxattr(path
, name
, value
, size
, flags
);
1765 return setxattr(path
, name
, value
, size
, 0, options
);
1767 #elif defined(HAVE_SETEA)
1768 return setea(path
, name
, value
, size
, flags
);
1769 #elif defined(HAVE_EXTATTR_SET_FILE)
1772 int attrnamespace
= (strncmp(name
, "system", 6) == 0) ?
1773 EXTATTR_NAMESPACE_SYSTEM
: EXTATTR_NAMESPACE_USER
;
1774 const char *attrname
= ((s
=strchr_m(name
, '.')) == NULL
) ? name
: s
+ 1;
1776 /* Check attribute existence */
1777 retval
= extattr_get_file(path
, attrnamespace
, attrname
, NULL
, 0);
1779 /* REPLACE attribute, that doesn't exist */
1780 if (flags
& XATTR_REPLACE
&& errno
== ENOATTR
) {
1784 /* Ignore other errors */
1787 /* CREATE attribute, that already exists */
1788 if (flags
& XATTR_CREATE
) {
1794 retval
= extattr_set_file(path
, attrnamespace
, attrname
, value
, size
);
1795 return (retval
< 0) ? -1 : 0;
1796 #elif defined(HAVE_ATTR_SET)
1798 char *attrname
= strchr(name
,'.') + 1;
1800 if (strncmp(name
, "system", 6) == 0) myflags
|= ATTR_ROOT
;
1801 if (flags
& XATTR_CREATE
) myflags
|= ATTR_CREATE
;
1802 if (flags
& XATTR_REPLACE
) myflags
|= ATTR_REPLACE
;
1804 return attr_set(path
, attrname
, (const char *)value
, size
, myflags
);
1805 #elif defined(HAVE_ATTROPEN)
1807 int myflags
= O_RDWR
;
1809 if (flags
& XATTR_CREATE
) myflags
|= O_EXCL
;
1810 if (!(flags
& XATTR_REPLACE
)) myflags
|= O_CREAT
;
1811 attrfd
= solaris_attropen(path
, name
, myflags
, (mode_t
) SOLARIS_ATTRMODE
);
1813 ret
= solaris_write_xattr(attrfd
, value
, size
);
1823 int sys_lsetxattr (const char *path
, const char *name
, const void *value
, size_t size
, int flags
)
1825 #if defined(HAVE_LSETXATTR)
1826 return lsetxattr(path
, name
, value
, size
, flags
);
1827 #elif defined(HAVE_SETXATTR) && defined(XATTR_ADD_OPT)
1828 int options
= XATTR_NOFOLLOW
;
1829 return setxattr(path
, name
, value
, size
, 0, options
);
1830 #elif defined(LSETEA)
1831 return lsetea(path
, name
, value
, size
, flags
);
1832 #elif defined(HAVE_EXTATTR_SET_LINK)
1835 int attrnamespace
= (strncmp(name
, "system", 6) == 0) ?
1836 EXTATTR_NAMESPACE_SYSTEM
: EXTATTR_NAMESPACE_USER
;
1837 const char *attrname
= ((s
=strchr_m(name
, '.')) == NULL
) ? name
: s
+ 1;
1839 /* Check attribute existence */
1840 retval
= extattr_get_link(path
, attrnamespace
, attrname
, NULL
, 0);
1842 /* REPLACE attribute, that doesn't exist */
1843 if (flags
& XATTR_REPLACE
&& errno
== ENOATTR
) {
1847 /* Ignore other errors */
1850 /* CREATE attribute, that already exists */
1851 if (flags
& XATTR_CREATE
) {
1858 retval
= extattr_set_link(path
, attrnamespace
, attrname
, value
, size
);
1859 return (retval
< 0) ? -1 : 0;
1860 #elif defined(HAVE_ATTR_SET)
1861 int myflags
= ATTR_DONTFOLLOW
;
1862 char *attrname
= strchr(name
,'.') + 1;
1864 if (strncmp(name
, "system", 6) == 0) myflags
|= ATTR_ROOT
;
1865 if (flags
& XATTR_CREATE
) myflags
|= ATTR_CREATE
;
1866 if (flags
& XATTR_REPLACE
) myflags
|= ATTR_REPLACE
;
1868 return attr_set(path
, attrname
, (const char *)value
, size
, myflags
);
1869 #elif defined(HAVE_ATTROPEN)
1871 int myflags
= O_RDWR
| AT_SYMLINK_NOFOLLOW
;
1873 if (flags
& XATTR_CREATE
) myflags
|= O_EXCL
;
1874 if (!(flags
& XATTR_REPLACE
)) myflags
|= O_CREAT
;
1875 attrfd
= solaris_attropen(path
, name
, myflags
, (mode_t
) SOLARIS_ATTRMODE
);
1877 ret
= solaris_write_xattr(attrfd
, value
, size
);
1887 int sys_fsetxattr (int filedes
, const char *name
, const void *value
, size_t size
, int flags
)
1889 #if defined(HAVE_FSETXATTR)
1890 #ifndef XATTR_ADD_OPT
1891 return fsetxattr(filedes
, name
, value
, size
, flags
);
1894 return fsetxattr(filedes
, name
, value
, size
, 0, options
);
1896 #elif defined(HAVE_FSETEA)
1897 return fsetea(filedes
, name
, value
, size
, flags
);
1898 #elif defined(HAVE_EXTATTR_SET_FD)
1901 int attrnamespace
= (strncmp(name
, "system", 6) == 0) ?
1902 EXTATTR_NAMESPACE_SYSTEM
: EXTATTR_NAMESPACE_USER
;
1903 const char *attrname
= ((s
=strchr_m(name
, '.')) == NULL
) ? name
: s
+ 1;
1905 /* Check attribute existence */
1906 retval
= extattr_get_fd(filedes
, attrnamespace
, attrname
, NULL
, 0);
1908 /* REPLACE attribute, that doesn't exist */
1909 if (flags
& XATTR_REPLACE
&& errno
== ENOATTR
) {
1913 /* Ignore other errors */
1916 /* CREATE attribute, that already exists */
1917 if (flags
& XATTR_CREATE
) {
1923 retval
= extattr_set_fd(filedes
, attrnamespace
, attrname
, value
, size
);
1924 return (retval
< 0) ? -1 : 0;
1925 #elif defined(HAVE_ATTR_SETF)
1927 char *attrname
= strchr(name
,'.') + 1;
1929 if (strncmp(name
, "system", 6) == 0) myflags
|= ATTR_ROOT
;
1930 if (flags
& XATTR_CREATE
) myflags
|= ATTR_CREATE
;
1931 if (flags
& XATTR_REPLACE
) myflags
|= ATTR_REPLACE
;
1933 return attr_setf(filedes
, attrname
, (const char *)value
, size
, myflags
);
1934 #elif defined(HAVE_ATTROPEN)
1936 int myflags
= O_RDWR
| O_XATTR
;
1938 if (flags
& XATTR_CREATE
) myflags
|= O_EXCL
;
1939 if (!(flags
& XATTR_REPLACE
)) myflags
|= O_CREAT
;
1940 attrfd
= solaris_openat(filedes
, name
, myflags
, (mode_t
) SOLARIS_ATTRMODE
);
1942 ret
= solaris_write_xattr(attrfd
, value
, size
);
1952 /**************************************************************************
1953 helper functions for Solaris' EA support
1954 ****************************************************************************/
1955 #ifdef HAVE_ATTROPEN
1956 static ssize_t
solaris_read_xattr(int attrfd
, void *value
, size_t size
)
1960 if (fstat(attrfd
, &sbuf
) == -1) {
1965 /* This is to return the current size of the named extended attribute */
1967 return sbuf
.st_size
;
1970 /* check size and read xattr */
1971 if (sbuf
.st_size
> size
) {
1976 return read(attrfd
, value
, sbuf
.st_size
);
1979 static ssize_t
solaris_list_xattr(int attrdirfd
, char *list
, size_t size
)
1984 int newfd
= dup(attrdirfd
);
1985 /* CAUTION: The originating file descriptor should not be
1986 used again following the call to fdopendir().
1987 For that reason we dup() the file descriptor
1988 here to make things more clear. */
1989 dirp
= fdopendir(newfd
);
1991 while ((de
= readdir(dirp
))) {
1992 size_t listlen
= strlen(de
->d_name
);
1993 if (!strcmp(de
->d_name
, ".") || !strcmp(de
->d_name
, "..")) {
1994 /* we don't want "." and ".." here: */
1995 DEBUG(10,("skipped EA %s\n",de
->d_name
));
2000 /* return the current size of the list of extended attribute names*/
2003 /* check size and copy entrieѕ + nul into list. */
2004 if ((len
+ listlen
+ 1) > size
) {
2009 safe_strcpy(list
+ len
, de
->d_name
, listlen
);
2017 if (closedir(dirp
) == -1) {
2018 DEBUG(0,("closedir dirp failed: %s\n",strerror(errno
)));
2024 static int solaris_unlinkat(int attrdirfd
, const char *name
)
2026 if (unlinkat(attrdirfd
, name
, 0) == -1) {
2027 if (errno
== ENOENT
) {
2035 static int solaris_attropen(const char *path
, const char *attrpath
, int oflag
, mode_t mode
)
2037 int filedes
= attropen(path
, attrpath
, oflag
, mode
);
2038 if (filedes
== -1) {
2039 DEBUG(10,("attropen FAILED: path: %s, name: %s, errno: %s\n",path
,attrpath
,strerror(errno
)));
2040 if (errno
== EINVAL
) {
2049 static int solaris_openat(int fildes
, const char *path
, int oflag
, mode_t mode
)
2051 int filedes
= openat(fildes
, path
, oflag
, mode
);
2052 if (filedes
== -1) {
2053 DEBUG(10,("openat FAILED: fd: %d, path: %s, errno: %s\n",filedes
,path
,strerror(errno
)));
2054 if (errno
== EINVAL
) {
2063 static int solaris_write_xattr(int attrfd
, const char *value
, size_t size
)
2065 if ((ftruncate(attrfd
, 0) == 0) && (write(attrfd
, value
, size
) == size
)) {
2068 DEBUG(10,("solaris_write_xattr FAILED!\n"));
2072 #endif /*HAVE_ATTROPEN*/
2075 /****************************************************************************
2076 Return the major devicenumber for UNIX extensions.
2077 ****************************************************************************/
2079 uint32
unix_dev_major(SMB_DEV_T dev
)
2081 #if defined(HAVE_DEVICE_MAJOR_FN)
2082 return (uint32
)major(dev
);
2084 return (uint32
)(dev
>> 8);
2088 /****************************************************************************
2089 Return the minor devicenumber for UNIX extensions.
2090 ****************************************************************************/
2092 uint32
unix_dev_minor(SMB_DEV_T dev
)
2094 #if defined(HAVE_DEVICE_MINOR_FN)
2095 return (uint32
)minor(dev
);
2097 return (uint32
)(dev
& 0xff);
2101 #if defined(WITH_AIO)
2103 /*******************************************************************
2104 An aio_read wrapper that will deal with 64-bit sizes.
2105 ********************************************************************/
2107 int sys_aio_read(SMB_STRUCT_AIOCB
*aiocb
)
2109 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_READ64)
2110 return aio_read64(aiocb
);
2111 #elif defined(HAVE_AIO_READ)
2112 return aio_read(aiocb
);
2119 /*******************************************************************
2120 An aio_write wrapper that will deal with 64-bit sizes.
2121 ********************************************************************/
2123 int sys_aio_write(SMB_STRUCT_AIOCB
*aiocb
)
2125 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_WRITE64)
2126 return aio_write64(aiocb
);
2127 #elif defined(HAVE_AIO_WRITE)
2128 return aio_write(aiocb
);
2135 /*******************************************************************
2136 An aio_return wrapper that will deal with 64-bit sizes.
2137 ********************************************************************/
2139 ssize_t
sys_aio_return(SMB_STRUCT_AIOCB
*aiocb
)
2141 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_RETURN64)
2142 return aio_return64(aiocb
);
2143 #elif defined(HAVE_AIO_RETURN)
2144 return aio_return(aiocb
);
2151 /*******************************************************************
2152 An aio_cancel wrapper that will deal with 64-bit sizes.
2153 ********************************************************************/
2155 int sys_aio_cancel(int fd
, SMB_STRUCT_AIOCB
*aiocb
)
2157 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_CANCEL64)
2158 return aio_cancel64(fd
, aiocb
);
2159 #elif defined(HAVE_AIO_CANCEL)
2160 return aio_cancel(fd
, aiocb
);
2167 /*******************************************************************
2168 An aio_error wrapper that will deal with 64-bit sizes.
2169 ********************************************************************/
2171 int sys_aio_error(const SMB_STRUCT_AIOCB
*aiocb
)
2173 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_ERROR64)
2174 return aio_error64(aiocb
);
2175 #elif defined(HAVE_AIO_ERROR)
2176 return aio_error(aiocb
);
2183 /*******************************************************************
2184 An aio_fsync wrapper that will deal with 64-bit sizes.
2185 ********************************************************************/
2187 int sys_aio_fsync(int op
, SMB_STRUCT_AIOCB
*aiocb
)
2189 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_FSYNC64)
2190 return aio_fsync64(op
, aiocb
);
2191 #elif defined(HAVE_AIO_FSYNC)
2192 return aio_fsync(op
, aiocb
);
2199 /*******************************************************************
2200 An aio_fsync wrapper that will deal with 64-bit sizes.
2201 ********************************************************************/
2203 int sys_aio_suspend(const SMB_STRUCT_AIOCB
* const cblist
[], int n
, const struct timespec
*timeout
)
2205 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_SUSPEND64)
2206 return aio_suspend64(cblist
, n
, timeout
);
2207 #elif defined(HAVE_AIO_FSYNC)
2208 return aio_suspend(cblist
, n
, timeout
);
2214 #else /* !WITH_AIO */
2216 int sys_aio_read(SMB_STRUCT_AIOCB
*aiocb
)
2222 int sys_aio_write(SMB_STRUCT_AIOCB
*aiocb
)
2228 ssize_t
sys_aio_return(SMB_STRUCT_AIOCB
*aiocb
)
2234 int sys_aio_cancel(int fd
, SMB_STRUCT_AIOCB
*aiocb
)
2240 int sys_aio_error(const SMB_STRUCT_AIOCB
*aiocb
)
2246 int sys_aio_fsync(int op
, SMB_STRUCT_AIOCB
*aiocb
)
2252 int sys_aio_suspend(const SMB_STRUCT_AIOCB
* const cblist
[], int n
, const struct timespec
*timeout
)
2257 #endif /* WITH_AIO */
2259 int sys_getpeereid( int s
, uid_t
*uid
)
2261 #if defined(HAVE_PEERCRED)
2263 socklen_t cred_len
= sizeof(struct ucred
);
2266 ret
= getsockopt(s
, SOL_SOCKET
, SO_PEERCRED
, (void *)&cred
, &cred_len
);
2271 if (cred_len
!= sizeof(struct ucred
)) {
2284 int sys_getnameinfo(const struct sockaddr
*psa
,
2293 * For Solaris we must make sure salen is the
2294 * correct length for the incoming sa_family.
2297 if (salen
== sizeof(struct sockaddr_storage
)) {
2298 salen
= sizeof(struct sockaddr_in
);
2299 #if defined(HAVE_IPV6)
2300 if (psa
->sa_family
== AF_INET6
) {
2301 salen
= sizeof(struct sockaddr_in6
);
2305 return getnameinfo(psa
, salen
, host
, hostlen
, service
, servlen
, flags
);
2308 int sys_connect(int fd
, const struct sockaddr
* addr
)
2310 socklen_t salen
= -1;
2312 if (addr
->sa_family
== AF_INET
) {
2313 salen
= sizeof(struct sockaddr_in
);
2314 } else if (addr
->sa_family
== AF_UNIX
) {
2315 salen
= sizeof(struct sockaddr_un
);
2317 #if defined(HAVE_IPV6)
2318 else if (addr
->sa_family
== AF_INET6
) {
2319 salen
= sizeof(struct sockaddr_in6
);
2323 return connect(fd
, addr
, salen
);