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/>.
24 #include "system/syslog.h"
25 #include "system/capability.h"
26 #include "system/passwd.h"
27 #include "system/filesys.h"
29 #ifdef HAVE_SYS_PRCTL_H
30 #include <sys/prctl.h>
34 The idea is that this file will eventually have wrappers around all
35 important system calls in samba. The aims are:
37 - to enable easier porting by putting OS dependent stuff in here
39 - to allow for hooks into other "pseudo-filesystems"
41 - to allow easier integration of things like the japanese extensions
43 - to support the philosophy of Samba to expose the features of
44 the OS within the SMB model. In general whatever file/printer/variable
45 expansions/etc make sense to the OS should be acceptable to Samba.
50 /*******************************************************************
51 A wrapper for memalign
52 ********************************************************************/
54 void *sys_memalign( size_t align
, size_t size
)
56 #if defined(HAVE_POSIX_MEMALIGN)
58 int ret
= posix_memalign( &p
, align
, size
);
63 #elif defined(HAVE_MEMALIGN)
64 return memalign( align
, size
);
66 /* On *BSD systems memaligns doesn't exist, but memory will
67 * be aligned on allocations of > pagesize. */
68 #if defined(SYSCONF_SC_PAGESIZE)
69 size_t pagesize
= (size_t)sysconf(_SC_PAGESIZE
);
70 #elif defined(HAVE_GETPAGESIZE)
71 size_t pagesize
= (size_t)getpagesize();
73 size_t pagesize
= (size_t)-1;
75 if (pagesize
== (size_t)-1) {
76 DEBUG(0,("memalign functionalaity not available on this platform!\n"));
79 if (size
< pagesize
) {
82 return SMB_MALLOC(size
);
86 /*******************************************************************
87 A wrapper for usleep in case we don't have one.
88 ********************************************************************/
90 int sys_usleep(long usecs
)
97 * We need this braindamage as the glibc usleep
98 * is not SPEC1170 complient... grumble... JRA.
101 if(usecs
< 0 || usecs
> 999999) {
109 #else /* HAVE_USLEEP */
111 * Fake it with select...
114 tval
.tv_usec
= usecs
/1000;
115 select(0,NULL
,NULL
,NULL
,&tval
);
117 #endif /* HAVE_USLEEP */
120 /*******************************************************************
121 A read wrapper that will deal with EINTR.
122 ********************************************************************/
124 ssize_t
sys_read(int fd
, void *buf
, size_t count
)
129 ret
= read(fd
, buf
, count
);
130 #if defined(EWOULDBLOCK)
131 } while (ret
== -1 && (errno
== EINTR
|| errno
== EAGAIN
|| errno
== EWOULDBLOCK
));
133 } while (ret
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
138 /*******************************************************************
139 A write wrapper that will deal with EINTR.
140 ********************************************************************/
142 ssize_t
sys_write(int fd
, const void *buf
, size_t count
)
147 ret
= write(fd
, buf
, count
);
148 #if defined(EWOULDBLOCK)
149 } while (ret
== -1 && (errno
== EINTR
|| errno
== EAGAIN
|| errno
== EWOULDBLOCK
));
151 } while (ret
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
156 /*******************************************************************
157 A writev wrapper that will deal with EINTR.
158 ********************************************************************/
160 ssize_t
sys_writev(int fd
, const struct iovec
*iov
, int iovcnt
)
165 /* Try to confuse write_data_iov a bit */
166 if ((random() % 5) == 0) {
167 return sys_write(fd
, iov
[0].iov_base
, iov
[0].iov_len
);
169 if (iov
[0].iov_len
> 1) {
170 return sys_write(fd
, iov
[0].iov_base
,
171 (random() % (iov
[0].iov_len
-1)) + 1);
176 ret
= writev(fd
, iov
, iovcnt
);
177 #if defined(EWOULDBLOCK)
178 } while (ret
== -1 && (errno
== EINTR
|| errno
== EAGAIN
|| errno
== EWOULDBLOCK
));
180 } while (ret
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
185 /*******************************************************************
186 A pread wrapper that will deal with EINTR and 64-bit file offsets.
187 ********************************************************************/
189 #if defined(HAVE_PREAD) || defined(HAVE_PREAD64)
190 ssize_t
sys_pread(int fd
, void *buf
, size_t count
, SMB_OFF_T off
)
195 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_PREAD64)
196 ret
= pread64(fd
, buf
, count
, off
);
198 ret
= pread(fd
, buf
, count
, off
);
200 } while (ret
== -1 && errno
== EINTR
);
205 /*******************************************************************
206 A write wrapper that will deal with EINTR and 64-bit file offsets.
207 ********************************************************************/
209 #if defined(HAVE_PWRITE) || defined(HAVE_PWRITE64)
210 ssize_t
sys_pwrite(int fd
, const void *buf
, size_t count
, SMB_OFF_T off
)
215 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_PWRITE64)
216 ret
= pwrite64(fd
, buf
, count
, off
);
218 ret
= pwrite(fd
, buf
, count
, off
);
220 } while (ret
== -1 && errno
== EINTR
);
225 /*******************************************************************
226 A send wrapper that will deal with EINTR or EAGAIN or EWOULDBLOCK.
227 ********************************************************************/
229 ssize_t
sys_send(int s
, const void *msg
, size_t len
, int flags
)
234 ret
= send(s
, msg
, len
, flags
);
235 #if defined(EWOULDBLOCK)
236 } while (ret
== -1 && (errno
== EINTR
|| errno
== EAGAIN
|| errno
== EWOULDBLOCK
));
238 } while (ret
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
243 /*******************************************************************
244 A sendto wrapper that will deal with EINTR or EAGAIN or EWOULDBLOCK.
245 ********************************************************************/
247 ssize_t
sys_sendto(int s
, const void *msg
, size_t len
, int flags
, const struct sockaddr
*to
, socklen_t tolen
)
252 ret
= sendto(s
, msg
, len
, flags
, to
, tolen
);
253 #if defined(EWOULDBLOCK)
254 } while (ret
== -1 && (errno
== EINTR
|| errno
== EAGAIN
|| errno
== EWOULDBLOCK
));
256 } while (ret
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
261 /*******************************************************************
262 A recv wrapper that will deal with EINTR or EAGAIN or EWOULDBLOCK.
263 ********************************************************************/
265 ssize_t
sys_recv(int fd
, void *buf
, size_t count
, int flags
)
270 ret
= recv(fd
, buf
, count
, flags
);
271 #if defined(EWOULDBLOCK)
272 } while (ret
== -1 && (errno
== EINTR
|| errno
== EAGAIN
|| errno
== EWOULDBLOCK
));
274 } while (ret
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
279 /*******************************************************************
280 A recvfrom wrapper that will deal with EINTR.
281 ********************************************************************/
283 ssize_t
sys_recvfrom(int s
, void *buf
, size_t len
, int flags
, struct sockaddr
*from
, socklen_t
*fromlen
)
288 ret
= recvfrom(s
, buf
, len
, flags
, from
, fromlen
);
289 #if defined(EWOULDBLOCK)
290 } while (ret
== -1 && (errno
== EINTR
|| errno
== EAGAIN
|| errno
== EWOULDBLOCK
));
292 } while (ret
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
297 /*******************************************************************
298 A fcntl wrapper that will deal with EINTR.
299 ********************************************************************/
301 int sys_fcntl_ptr(int fd
, int cmd
, void *arg
)
306 ret
= fcntl(fd
, cmd
, arg
);
307 } while (ret
== -1 && errno
== EINTR
);
311 /*******************************************************************
312 A fcntl wrapper that will deal with EINTR.
313 ********************************************************************/
315 int sys_fcntl_long(int fd
, int cmd
, long arg
)
320 ret
= fcntl(fd
, cmd
, arg
);
321 } while (ret
== -1 && errno
== EINTR
);
325 /****************************************************************************
326 Get/Set all the possible time fields from a stat struct as a timespec.
327 ****************************************************************************/
329 static struct timespec
get_atimespec(const struct stat
*pst
)
331 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
334 /* Old system - no ns timestamp. */
335 ret
.tv_sec
= pst
->st_atime
;
339 #if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
341 #elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
343 ret
.tv_sec
= pst
->st_atime
;
344 ret
.tv_nsec
= pst
->st_atimensec
;
346 #elif defined(HAVE_STRUCT_STAT_ST_MTIME_N)
348 ret
.tv_sec
= pst
->st_atime
;
349 ret
.tv_nsec
= pst
->st_atime_n
;
351 #elif defined(HAVE_STRUCT_STAT_ST_UMTIME)
353 ret
.tv_sec
= pst
->st_atime
;
354 ret
.tv_nsec
= pst
->st_uatime
* 1000;
356 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC)
357 return pst
->st_atimespec
;
359 #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
364 static struct timespec
get_mtimespec(const struct stat
*pst
)
366 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
369 /* Old system - no ns timestamp. */
370 ret
.tv_sec
= pst
->st_mtime
;
374 #if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
376 #elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
378 ret
.tv_sec
= pst
->st_mtime
;
379 ret
.tv_nsec
= pst
->st_mtimensec
;
381 #elif defined(HAVE_STRUCT_STAT_ST_MTIME_N)
383 ret
.tv_sec
= pst
->st_mtime
;
384 ret
.tv_nsec
= pst
->st_mtime_n
;
386 #elif defined(HAVE_STRUCT_STAT_ST_UMTIME)
388 ret
.tv_sec
= pst
->st_mtime
;
389 ret
.tv_nsec
= pst
->st_umtime
* 1000;
391 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC)
392 return pst
->st_mtimespec
;
394 #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
399 static struct timespec
get_ctimespec(const struct stat
*pst
)
401 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
404 /* Old system - no ns timestamp. */
405 ret
.tv_sec
= pst
->st_ctime
;
409 #if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
411 #elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
413 ret
.tv_sec
= pst
->st_ctime
;
414 ret
.tv_nsec
= pst
->st_ctimensec
;
416 #elif defined(HAVE_STRUCT_STAT_ST_MTIME_N)
418 ret
.tv_sec
= pst
->st_ctime
;
419 ret
.tv_nsec
= pst
->st_ctime_n
;
421 #elif defined(HAVE_STRUCT_STAT_ST_UMTIME)
423 ret
.tv_sec
= pst
->st_ctime
;
424 ret
.tv_nsec
= pst
->st_uctime
* 1000;
426 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC)
427 return pst
->st_ctimespec
;
429 #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
434 /****************************************************************************
435 Return the best approximation to a 'create time' under UNIX from a stat
437 ****************************************************************************/
439 static struct timespec
calc_create_time_stat(const struct stat
*st
)
441 struct timespec ret
, ret1
;
442 struct timespec c_time
= get_ctimespec(st
);
443 struct timespec m_time
= get_mtimespec(st
);
444 struct timespec a_time
= get_atimespec(st
);
446 ret
= timespec_compare(&c_time
, &m_time
) < 0 ? c_time
: m_time
;
447 ret1
= timespec_compare(&ret
, &a_time
) < 0 ? ret
: a_time
;
449 if(!null_timespec(ret1
)) {
454 * One of ctime, mtime or atime was zero (probably atime).
455 * Just return MIN(ctime, mtime).
460 /****************************************************************************
461 Return the best approximation to a 'create time' under UNIX from a stat_ex
463 ****************************************************************************/
465 static struct timespec
calc_create_time_stat_ex(const struct stat_ex
*st
)
467 struct timespec ret
, ret1
;
468 struct timespec c_time
= st
->st_ex_ctime
;
469 struct timespec m_time
= st
->st_ex_mtime
;
470 struct timespec a_time
= st
->st_ex_atime
;
472 ret
= timespec_compare(&c_time
, &m_time
) < 0 ? c_time
: m_time
;
473 ret1
= timespec_compare(&ret
, &a_time
) < 0 ? ret
: a_time
;
475 if(!null_timespec(ret1
)) {
480 * One of ctime, mtime or atime was zero (probably atime).
481 * Just return MIN(ctime, mtime).
486 /****************************************************************************
487 Return the 'create time' from a stat struct if it exists (birthtime) or else
488 use the best approximation.
489 ****************************************************************************/
491 static void make_create_timespec(const struct stat
*pst
, struct stat_ex
*dst
,
492 bool fake_dir_create_times
)
494 if (S_ISDIR(pst
->st_mode
) && fake_dir_create_times
) {
495 dst
->st_ex_btime
.tv_sec
= 315493200L; /* 1/1/1980 */
496 dst
->st_ex_btime
.tv_nsec
= 0;
499 dst
->st_ex_calculated_birthtime
= false;
501 #if defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC)
502 dst
->st_ex_btime
= pst
->st_birthtimespec
;
503 #elif defined(HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC)
504 dst
->st_ex_btime
.tv_sec
= pst
->st_birthtime
;
505 dst
->st_ex_btime
.tv_nsec
= pst
->st_birthtimenspec
;
506 #elif defined(HAVE_STRUCT_STAT_ST_BIRTHTIME)
507 dst
->st_ex_btime
.tv_sec
= pst
->st_birthtime
;
508 dst
->st_ex_btime
.tv_nsec
= 0;
510 dst
->st_ex_btime
= calc_create_time_stat(pst
);
511 dst
->st_ex_calculated_birthtime
= true;
514 /* Deal with systems that don't initialize birthtime correctly.
515 * Pointed out by SATOH Fumiyasu <fumiyas@osstech.jp>.
517 if (null_timespec(dst
->st_ex_btime
)) {
518 dst
->st_ex_btime
= calc_create_time_stat(pst
);
519 dst
->st_ex_calculated_birthtime
= true;
523 /****************************************************************************
524 If we update a timestamp in a stat_ex struct we may have to recalculate
525 the birthtime. For now only implement this for write time, but we may
526 also need to do it for atime and ctime. JRA.
527 ****************************************************************************/
529 void update_stat_ex_mtime(struct stat_ex
*dst
,
530 struct timespec write_ts
)
532 dst
->st_ex_mtime
= write_ts
;
534 /* We may have to recalculate btime. */
535 if (dst
->st_ex_calculated_birthtime
) {
536 dst
->st_ex_btime
= calc_create_time_stat_ex(dst
);
540 void update_stat_ex_create_time(struct stat_ex
*dst
,
541 struct timespec create_time
)
543 dst
->st_ex_btime
= create_time
;
544 dst
->st_ex_calculated_birthtime
= false;
547 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_STAT64)
548 static void init_stat_ex_from_stat (struct stat_ex
*dst
,
549 const struct stat64
*src
,
550 bool fake_dir_create_times
)
552 static void init_stat_ex_from_stat (struct stat_ex
*dst
,
553 const struct stat
*src
,
554 bool fake_dir_create_times
)
557 dst
->st_ex_dev
= src
->st_dev
;
558 dst
->st_ex_ino
= src
->st_ino
;
559 dst
->st_ex_mode
= src
->st_mode
;
560 dst
->st_ex_nlink
= src
->st_nlink
;
561 dst
->st_ex_uid
= src
->st_uid
;
562 dst
->st_ex_gid
= src
->st_gid
;
563 dst
->st_ex_rdev
= src
->st_rdev
;
564 dst
->st_ex_size
= src
->st_size
;
565 dst
->st_ex_atime
= get_atimespec(src
);
566 dst
->st_ex_mtime
= get_mtimespec(src
);
567 dst
->st_ex_ctime
= get_ctimespec(src
);
568 make_create_timespec(src
, dst
, fake_dir_create_times
);
569 #ifdef HAVE_STAT_ST_BLKSIZE
570 dst
->st_ex_blksize
= src
->st_blksize
;
572 dst
->st_ex_blksize
= STAT_ST_BLOCKSIZE
;
575 #ifdef HAVE_STAT_ST_BLOCKS
576 dst
->st_ex_blocks
= src
->st_blocks
;
578 dst
->st_ex_blocks
= src
->st_size
/ dst
->st_ex_blksize
+ 1;
581 #ifdef HAVE_STAT_ST_FLAGS
582 dst
->st_ex_flags
= src
->st_flags
;
584 dst
->st_ex_flags
= 0;
588 /*******************************************************************
589 A stat() wrapper that will deal with 64 bit filesizes.
590 ********************************************************************/
592 int sys_stat(const char *fname
, SMB_STRUCT_STAT
*sbuf
,
593 bool fake_dir_create_times
)
596 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_STAT64)
597 struct stat64 statbuf
;
598 ret
= stat64(fname
, &statbuf
);
601 ret
= stat(fname
, &statbuf
);
604 /* we always want directories to appear zero size */
605 if (S_ISDIR(statbuf
.st_mode
)) {
608 init_stat_ex_from_stat(sbuf
, &statbuf
, fake_dir_create_times
);
613 /*******************************************************************
614 An fstat() wrapper that will deal with 64 bit filesizes.
615 ********************************************************************/
617 int sys_fstat(int fd
, SMB_STRUCT_STAT
*sbuf
, bool fake_dir_create_times
)
620 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_FSTAT64)
621 struct stat64 statbuf
;
622 ret
= fstat64(fd
, &statbuf
);
625 ret
= fstat(fd
, &statbuf
);
628 /* we always want directories to appear zero size */
629 if (S_ISDIR(statbuf
.st_mode
)) {
632 init_stat_ex_from_stat(sbuf
, &statbuf
, fake_dir_create_times
);
637 /*******************************************************************
638 An lstat() wrapper that will deal with 64 bit filesizes.
639 ********************************************************************/
641 int sys_lstat(const char *fname
,SMB_STRUCT_STAT
*sbuf
,
642 bool fake_dir_create_times
)
645 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_LSTAT64)
646 struct stat64 statbuf
;
647 ret
= lstat64(fname
, &statbuf
);
650 ret
= lstat(fname
, &statbuf
);
653 /* we always want directories to appear zero size */
654 if (S_ISDIR(statbuf
.st_mode
)) {
657 init_stat_ex_from_stat(sbuf
, &statbuf
, fake_dir_create_times
);
662 /*******************************************************************
663 An posix_fallocate() wrapper that will deal with 64 bit filesizes.
664 ********************************************************************/
665 int sys_posix_fallocate(int fd
, SMB_OFF_T offset
, SMB_OFF_T len
)
667 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_POSIX_FALLOCATE64) && !defined(HAVE_BROKEN_POSIX_FALLOCATE)
668 return posix_fallocate64(fd
, offset
, len
);
669 #elif defined(HAVE_POSIX_FALLOCATE) && !defined(HAVE_BROKEN_POSIX_FALLOCATE)
670 return posix_fallocate(fd
, offset
, len
);
671 #elif defined(F_RESVSP64)
672 /* this handles XFS on IRIX */
674 SMB_OFF_T new_len
= offset
+ len
;
678 /* unlikely to get a too large file on a 64bit system but ... */
682 fl
.l_whence
= SEEK_SET
;
686 ret
=fcntl(fd
, F_RESVSP64
, &fl
);
691 /* Make sure the file gets enlarged after we allocated space: */
693 if (new_len
> sbuf
.st_size
)
694 ftruncate64(fd
, new_len
);
701 /*******************************************************************
702 An fallocate() function that matches the semantics of the Linux one.
703 ********************************************************************/
705 #ifdef HAVE_LINUX_FALLOC_H
706 #include <linux/falloc.h>
709 int sys_fallocate(int fd
, enum vfs_fallocate_mode mode
, SMB_OFF_T offset
, SMB_OFF_T len
)
711 #if defined(HAVE_LINUX_FALLOCATE64) || defined(HAVE_LINUX_FALLOCATE)
714 case VFS_FALLOCATE_EXTEND_SIZE
:
717 case VFS_FALLOCATE_KEEP_SIZE
:
718 lmode
= FALLOC_FL_KEEP_SIZE
;
724 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_LINUX_FALLOCATE64)
725 return fallocate64(fd
, lmode
, offset
, len
);
726 #elif defined(HAVE_LINUX_FALLOCATE)
727 return fallocate(fd
, lmode
, offset
, len
);
730 /* TODO - plumb in fallocate from other filesysetms like VXFS etc. JRA. */
736 /*******************************************************************
737 An ftruncate() wrapper that will deal with 64 bit filesizes.
738 ********************************************************************/
740 int sys_ftruncate(int fd
, SMB_OFF_T offset
)
742 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_FTRUNCATE64)
743 return ftruncate64(fd
, offset
);
745 return ftruncate(fd
, offset
);
749 /*******************************************************************
750 An lseek() wrapper that will deal with 64 bit filesizes.
751 ********************************************************************/
753 SMB_OFF_T
sys_lseek(int fd
, SMB_OFF_T offset
, int whence
)
755 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_LSEEK64)
756 return lseek64(fd
, offset
, whence
);
758 return lseek(fd
, offset
, whence
);
762 /*******************************************************************
763 An fseek() wrapper that will deal with 64 bit filesizes.
764 ********************************************************************/
766 int sys_fseek(FILE *fp
, SMB_OFF_T offset
, int whence
)
768 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(LARGE_SMB_OFF_T) && defined(HAVE_FSEEK64)
769 return fseek64(fp
, offset
, whence
);
770 #elif defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(LARGE_SMB_OFF_T) && defined(HAVE_FSEEKO64)
771 return fseeko64(fp
, offset
, whence
);
773 return fseek(fp
, offset
, whence
);
777 /*******************************************************************
778 An ftell() wrapper that will deal with 64 bit filesizes.
779 ********************************************************************/
781 SMB_OFF_T
sys_ftell(FILE *fp
)
783 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(LARGE_SMB_OFF_T) && defined(HAVE_FTELL64)
784 return (SMB_OFF_T
)ftell64(fp
);
785 #elif defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(LARGE_SMB_OFF_T) && defined(HAVE_FTELLO64)
786 return (SMB_OFF_T
)ftello64(fp
);
788 return (SMB_OFF_T
)ftell(fp
);
792 /*******************************************************************
793 A creat() wrapper that will deal with 64 bit filesizes.
794 ********************************************************************/
796 int sys_creat(const char *path
, mode_t mode
)
798 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_CREAT64)
799 return creat64(path
, mode
);
802 * If creat64 isn't defined then ensure we call a potential open64.
805 return sys_open(path
, O_WRONLY
| O_CREAT
| O_TRUNC
, mode
);
809 /*******************************************************************
810 An open() wrapper that will deal with 64 bit filesizes.
811 ********************************************************************/
813 int sys_open(const char *path
, int oflag
, mode_t mode
)
815 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OPEN64)
816 return open64(path
, oflag
, mode
);
818 return open(path
, oflag
, mode
);
822 /*******************************************************************
823 An fopen() wrapper that will deal with 64 bit filesizes.
824 ********************************************************************/
826 FILE *sys_fopen(const char *path
, const char *type
)
828 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_FOPEN64)
829 return fopen64(path
, type
);
831 return fopen(path
, type
);
836 /*******************************************************************
837 A flock() wrapper that will perform the kernel flock.
838 ********************************************************************/
840 void kernel_flock(int fd
, uint32 share_mode
, uint32 access_mask
)
842 #if HAVE_KERNEL_SHARE_MODES
844 if (share_mode
== FILE_SHARE_WRITE
) {
845 kernel_mode
= LOCK_MAND
|LOCK_WRITE
;
846 } else if (share_mode
== FILE_SHARE_READ
) {
847 kernel_mode
= LOCK_MAND
|LOCK_READ
;
848 } else if (share_mode
== FILE_SHARE_NONE
) {
849 kernel_mode
= LOCK_MAND
;
852 flock(fd
, kernel_mode
);
860 /*******************************************************************
861 An opendir wrapper that will deal with 64 bit filesizes.
862 ********************************************************************/
864 SMB_STRUCT_DIR
*sys_opendir(const char *name
)
866 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OPENDIR64)
867 return opendir64(name
);
869 return opendir(name
);
873 /*******************************************************************
874 An fdopendir wrapper that will deal with 64 bit filesizes.
875 Ugly hack - we need dirfd for this to work correctly in the
877 ********************************************************************/
879 SMB_STRUCT_DIR
*sys_fdopendir(int fd
)
881 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_FDOPENDIR64) && defined(HAVE_DIRFD)
882 return fdopendir64(fd
);
883 #elif defined(HAVE_FDOPENDIR) && defined(HAVE_DIRFD)
884 return fdopendir(fd
);
891 /*******************************************************************
892 A readdir wrapper that will deal with 64 bit filesizes.
893 ********************************************************************/
895 SMB_STRUCT_DIRENT
*sys_readdir(SMB_STRUCT_DIR
*dirp
)
897 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_READDIR64)
898 return readdir64(dirp
);
900 return readdir(dirp
);
904 /*******************************************************************
905 A seekdir wrapper that will deal with 64 bit filesizes.
906 ********************************************************************/
908 void sys_seekdir(SMB_STRUCT_DIR
*dirp
, long offset
)
910 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_SEEKDIR64)
911 seekdir64(dirp
, offset
);
913 seekdir(dirp
, offset
);
917 /*******************************************************************
918 A telldir wrapper that will deal with 64 bit filesizes.
919 ********************************************************************/
921 long sys_telldir(SMB_STRUCT_DIR
*dirp
)
923 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_TELLDIR64)
924 return (long)telldir64(dirp
);
926 return (long)telldir(dirp
);
930 /*******************************************************************
931 A rewinddir wrapper that will deal with 64 bit filesizes.
932 ********************************************************************/
934 void sys_rewinddir(SMB_STRUCT_DIR
*dirp
)
936 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_REWINDDIR64)
943 /*******************************************************************
944 A close wrapper that will deal with 64 bit filesizes.
945 ********************************************************************/
947 int sys_closedir(SMB_STRUCT_DIR
*dirp
)
949 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_CLOSEDIR64)
950 return closedir64(dirp
);
952 return closedir(dirp
);
956 /*******************************************************************
957 An mknod() wrapper that will deal with 64 bit filesizes.
958 ********************************************************************/
960 int sys_mknod(const char *path
, mode_t mode
, SMB_DEV_T dev
)
962 #if defined(HAVE_MKNOD) || defined(HAVE_MKNOD64)
963 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_MKNOD64) && defined(HAVE_DEV64_T)
964 return mknod64(path
, mode
, dev
);
966 return mknod(path
, mode
, dev
);
969 /* No mknod system call. */
975 /*******************************************************************
976 The wait() calls vary between systems
977 ********************************************************************/
979 int sys_waitpid(pid_t pid
,int *status
,int options
)
982 return waitpid(pid
,status
,options
);
983 #else /* HAVE_WAITPID */
984 return wait4(pid
, status
, options
, NULL
);
985 #endif /* HAVE_WAITPID */
988 /*******************************************************************
989 System wrapper for getwd
990 ********************************************************************/
992 char *sys_getwd(char *s
)
996 wd
= (char *)getcwd(s
, PATH_MAX
);
998 wd
= (char *)getwd(s
);
1003 #if defined(HAVE_POSIX_CAPABILITIES)
1005 /**************************************************************************
1006 Try and abstract process capabilities (for systems that have them).
1007 ****************************************************************************/
1009 /* Set the POSIX capabilities needed for the given purpose into the effective
1010 * capability set of the current process. Make sure they are always removed
1011 * from the inheritable set, because there is no circumstance in which our
1012 * children should inherit our elevated privileges.
1014 static bool set_process_capability(enum smbd_capability capability
,
1017 cap_value_t cap_vals
[2] = {0};
1018 int num_cap_vals
= 0;
1022 #if defined(HAVE_PRCTL) && defined(PR_GET_KEEPCAPS) && defined(PR_SET_KEEPCAPS)
1023 /* On Linux, make sure that any capabilities we grab are sticky
1024 * across UID changes. We expect that this would allow us to keep both
1025 * the effective and permitted capability sets, but as of circa 2.6.16,
1026 * only the permitted set is kept. It is a bug (which we work around)
1027 * that the effective set is lost, but we still require the effective
1030 if (!prctl(PR_GET_KEEPCAPS
)) {
1031 prctl(PR_SET_KEEPCAPS
, 1);
1035 cap
= cap_get_proc();
1037 DEBUG(0,("set_process_capability: cap_get_proc failed: %s\n",
1042 switch (capability
) {
1043 case KERNEL_OPLOCK_CAPABILITY
:
1044 #ifdef CAP_NETWORK_MGT
1045 /* IRIX has CAP_NETWORK_MGT for oplocks. */
1046 cap_vals
[num_cap_vals
++] = CAP_NETWORK_MGT
;
1049 case DMAPI_ACCESS_CAPABILITY
:
1050 #ifdef CAP_DEVICE_MGT
1051 /* IRIX has CAP_DEVICE_MGT for DMAPI access. */
1052 cap_vals
[num_cap_vals
++] = CAP_DEVICE_MGT
;
1054 /* Linux has CAP_MKNOD for DMAPI access. */
1055 cap_vals
[num_cap_vals
++] = CAP_MKNOD
;
1058 case LEASE_CAPABILITY
:
1060 cap_vals
[num_cap_vals
++] = CAP_LEASE
;
1065 SMB_ASSERT(num_cap_vals
<= ARRAY_SIZE(cap_vals
));
1067 if (num_cap_vals
== 0) {
1072 cap_set_flag(cap
, CAP_EFFECTIVE
, num_cap_vals
, cap_vals
,
1073 enable
? CAP_SET
: CAP_CLEAR
);
1075 /* We never want to pass capabilities down to our children, so make
1076 * sure they are not inherited.
1078 cap_set_flag(cap
, CAP_INHERITABLE
, num_cap_vals
, cap_vals
, CAP_CLEAR
);
1080 if (cap_set_proc(cap
) == -1) {
1081 DEBUG(0, ("set_process_capability: cap_set_proc failed: %s\n",
1091 #endif /* HAVE_POSIX_CAPABILITIES */
1093 /****************************************************************************
1094 Gain the oplock capability from the kernel if possible.
1095 ****************************************************************************/
1097 void set_effective_capability(enum smbd_capability capability
)
1099 #if defined(HAVE_POSIX_CAPABILITIES)
1100 set_process_capability(capability
, True
);
1101 #endif /* HAVE_POSIX_CAPABILITIES */
1104 void drop_effective_capability(enum smbd_capability capability
)
1106 #if defined(HAVE_POSIX_CAPABILITIES)
1107 set_process_capability(capability
, False
);
1108 #endif /* HAVE_POSIX_CAPABILITIES */
1111 /**************************************************************************
1112 Wrapper for random().
1113 ****************************************************************************/
1115 long sys_random(void)
1117 #if defined(HAVE_RANDOM)
1118 return (long)random();
1119 #elif defined(HAVE_RAND)
1120 return (long)rand();
1122 DEBUG(0,("Error - no random function available !\n"));
1127 /**************************************************************************
1128 Wrapper for srandom().
1129 ****************************************************************************/
1131 void sys_srandom(unsigned int seed
)
1133 #if defined(HAVE_SRANDOM)
1135 #elif defined(HAVE_SRAND)
1138 DEBUG(0,("Error - no srandom function available !\n"));
1144 #define NGROUPS_MAX 32 /* Guess... */
1147 /**************************************************************************
1148 Returns equivalent to NGROUPS_MAX - using sysconf if needed.
1149 ****************************************************************************/
1151 int groups_max(void)
1153 #if defined(SYSCONF_SC_NGROUPS_MAX)
1154 int ret
= sysconf(_SC_NGROUPS_MAX
);
1155 return (ret
== -1) ? NGROUPS_MAX
: ret
;
1161 /**************************************************************************
1162 Wrap setgroups and getgroups for systems that declare getgroups() as
1163 returning an array of gid_t, but actuall return an array of int.
1164 ****************************************************************************/
1166 #if defined(HAVE_BROKEN_GETGROUPS)
1168 #ifdef HAVE_BROKEN_GETGROUPS
1174 static int sys_broken_getgroups(int setlen
, gid_t
*gidset
)
1181 return getgroups(setlen
, &gid
);
1185 * Broken case. We need to allocate a
1186 * GID_T array of size setlen.
1195 setlen
= groups_max();
1197 if((group_list
= SMB_MALLOC_ARRAY(GID_T
, setlen
)) == NULL
) {
1198 DEBUG(0,("sys_getgroups: Malloc fail.\n"));
1202 if((ngroups
= getgroups(setlen
, group_list
)) < 0) {
1203 int saved_errno
= errno
;
1204 SAFE_FREE(group_list
);
1205 errno
= saved_errno
;
1209 for(i
= 0; i
< ngroups
; i
++)
1210 gidset
[i
] = (gid_t
)group_list
[i
];
1212 SAFE_FREE(group_list
);
1216 static int sys_broken_setgroups(int setlen
, gid_t
*gidset
)
1224 if (setlen
< 0 || setlen
> groups_max()) {
1230 * Broken case. We need to allocate a
1231 * GID_T array of size setlen.
1234 if((group_list
= SMB_MALLOC_ARRAY(GID_T
, setlen
)) == NULL
) {
1235 DEBUG(0,("sys_setgroups: Malloc fail.\n"));
1239 for(i
= 0; i
< setlen
; i
++)
1240 group_list
[i
] = (GID_T
) gidset
[i
];
1242 if(setgroups(setlen
, group_list
) != 0) {
1243 int saved_errno
= errno
;
1244 SAFE_FREE(group_list
);
1245 errno
= saved_errno
;
1249 SAFE_FREE(group_list
);
1253 #endif /* HAVE_BROKEN_GETGROUPS */
1255 /* This is a list of systems that require the first GID passed to setgroups(2)
1256 * to be the effective GID. If your system is one of these, add it here.
1258 #if defined (FREEBSD) || defined (DARWINOS)
1259 #define USE_BSD_SETGROUPS
1262 #if defined(USE_BSD_SETGROUPS)
1263 /* Depending on the particular BSD implementation, the first GID that is
1264 * passed to setgroups(2) will either be ignored or will set the credential's
1265 * effective GID. In either case, the right thing to do is to guarantee that
1266 * gidset[0] is the effective GID.
1268 static int sys_bsd_setgroups(gid_t primary_gid
, int setlen
, const gid_t
*gidset
)
1270 gid_t
*new_gidset
= NULL
;
1274 /* setgroups(2) will fail with EINVAL if we pass too many groups. */
1277 /* No group list, just make sure we are setting the efective GID. */
1279 return setgroups(1, &primary_gid
);
1282 /* If the primary gid is not the first array element, grow the array
1283 * and insert it at the front.
1285 if (gidset
[0] != primary_gid
) {
1286 new_gidset
= SMB_MALLOC_ARRAY(gid_t
, setlen
+ 1);
1287 if (new_gidset
== NULL
) {
1291 memcpy(new_gidset
+ 1, gidset
, (setlen
* sizeof(gid_t
)));
1292 new_gidset
[0] = primary_gid
;
1297 DEBUG(3, ("forced to truncate group list from %d to %d\n",
1302 #if defined(HAVE_BROKEN_GETGROUPS)
1303 ret
= sys_broken_setgroups(setlen
, new_gidset
? new_gidset
: gidset
);
1305 ret
= setgroups(setlen
, new_gidset
? new_gidset
: gidset
);
1310 SAFE_FREE(new_gidset
);
1317 #endif /* USE_BSD_SETGROUPS */
1319 /**************************************************************************
1320 Wrapper for getgroups. Deals with broken (int) case.
1321 ****************************************************************************/
1323 int sys_getgroups(int setlen
, gid_t
*gidset
)
1325 #if defined(HAVE_BROKEN_GETGROUPS)
1326 return sys_broken_getgroups(setlen
, gidset
);
1328 return getgroups(setlen
, gidset
);
1332 /**************************************************************************
1333 Wrapper for setgroups. Deals with broken (int) case and BSD case.
1334 ****************************************************************************/
1336 int sys_setgroups(gid_t
UNUSED(primary_gid
), int setlen
, gid_t
*gidset
)
1338 #if !defined(HAVE_SETGROUPS)
1341 #endif /* HAVE_SETGROUPS */
1343 #if defined(USE_BSD_SETGROUPS)
1344 return sys_bsd_setgroups(primary_gid
, setlen
, gidset
);
1345 #elif defined(HAVE_BROKEN_GETGROUPS)
1346 return sys_broken_setgroups(setlen
, gidset
);
1348 return setgroups(setlen
, gidset
);
1352 /**************************************************************************
1353 Extract a command into an arg list.
1354 ****************************************************************************/
1356 static char **extract_args(TALLOC_CTX
*mem_ctx
, const char *command
)
1365 if (!(trunc_cmd
= talloc_strdup(mem_ctx
, command
))) {
1366 DEBUG(0, ("talloc failed\n"));
1370 if(!(ptr
= strtok_r(trunc_cmd
, " \t", &saveptr
))) {
1371 TALLOC_FREE(trunc_cmd
);
1380 for( argcl
= 1; ptr
; ptr
= strtok_r(NULL
, " \t", &saveptr
))
1383 TALLOC_FREE(trunc_cmd
);
1385 if (!(argl
= TALLOC_ARRAY(mem_ctx
, char *, argcl
+ 1))) {
1390 * Now do the extraction.
1393 if (!(trunc_cmd
= talloc_strdup(mem_ctx
, command
))) {
1397 ptr
= strtok_r(trunc_cmd
, " \t", &saveptr
);
1400 if (!(argl
[i
++] = talloc_strdup(argl
, ptr
))) {
1404 while((ptr
= strtok_r(NULL
, " \t", &saveptr
)) != NULL
) {
1406 if (!(argl
[i
++] = talloc_strdup(argl
, ptr
))) {
1412 TALLOC_FREE(trunc_cmd
);
1416 DEBUG(0, ("talloc failed\n"));
1417 TALLOC_FREE(trunc_cmd
);
1423 /**************************************************************************
1424 Wrapper for popen. Safer as it doesn't search a path.
1425 Modified from the glibc sources.
1426 modified by tridge to return a file descriptor. We must kick our FILE* habit
1427 ****************************************************************************/
1429 typedef struct _popen_list
1433 struct _popen_list
*next
;
1436 static popen_list
*popen_chain
;
1438 int sys_popen(const char *command
)
1440 int parent_end
, child_end
;
1442 popen_list
*entry
= NULL
;
1445 if (pipe(pipe_fds
) < 0)
1448 parent_end
= pipe_fds
[0];
1449 child_end
= pipe_fds
[1];
1456 if((entry
= SMB_MALLOC_P(popen_list
)) == NULL
)
1459 ZERO_STRUCTP(entry
);
1462 * Extract the command and args into a NULL terminated array.
1465 if(!(argl
= extract_args(NULL
, command
)))
1468 entry
->child_pid
= sys_fork();
1470 if (entry
->child_pid
== -1) {
1474 if (entry
->child_pid
== 0) {
1480 int child_std_end
= STDOUT_FILENO
;
1484 if (child_end
!= child_std_end
) {
1485 dup2 (child_end
, child_std_end
);
1490 * POSIX.2: "popen() shall ensure that any streams from previous
1491 * popen() calls that remain open in the parent process are closed
1492 * in the new child process."
1495 for (p
= popen_chain
; p
; p
= p
->next
)
1498 execv(argl
[0], argl
);
1509 /* Link into popen_chain. */
1510 entry
->next
= popen_chain
;
1511 popen_chain
= entry
;
1512 entry
->fd
= parent_end
;
1525 /**************************************************************************
1526 Wrapper for pclose. Modified from the glibc sources.
1527 ****************************************************************************/
1529 int sys_pclose(int fd
)
1532 popen_list
**ptr
= &popen_chain
;
1533 popen_list
*entry
= NULL
;
1537 /* Unlink from popen_chain. */
1538 for ( ; *ptr
!= NULL
; ptr
= &(*ptr
)->next
) {
1539 if ((*ptr
)->fd
== fd
) {
1541 *ptr
= (*ptr
)->next
;
1547 if (status
< 0 || close(entry
->fd
) < 0)
1551 * As Samba is catching and eating child process
1552 * exits we don't really care about the child exit
1553 * code, a -1 with errno = ECHILD will do fine for us.
1557 wait_pid
= sys_waitpid (entry
->child_pid
, &wstatus
, 0);
1558 } while (wait_pid
== -1 && errno
== EINTR
);
1567 /**************************************************************************
1568 Wrapper for Admin Logs.
1569 ****************************************************************************/
1571 void sys_adminlog(int priority
, const char *format_str
, ...)
1575 char *msgbuf
= NULL
;
1577 va_start( ap
, format_str
);
1578 ret
= vasprintf( &msgbuf
, format_str
, ap
);
1584 #if defined(HAVE_SYSLOG)
1585 syslog( priority
, "%s", msgbuf
);
1587 DEBUG(0,("%s", msgbuf
));
1592 /******** Solaris EA helper function prototypes ********/
1593 #ifdef HAVE_ATTROPEN
1594 #define SOLARIS_ATTRMODE S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP
1595 static int solaris_write_xattr(int attrfd
, const char *value
, size_t size
);
1596 static ssize_t
solaris_read_xattr(int attrfd
, void *value
, size_t size
);
1597 static ssize_t
solaris_list_xattr(int attrdirfd
, char *list
, size_t size
);
1598 static int solaris_unlinkat(int attrdirfd
, const char *name
);
1599 static int solaris_attropen(const char *path
, const char *attrpath
, int oflag
, mode_t mode
);
1600 static int solaris_openat(int fildes
, const char *path
, int oflag
, mode_t mode
);
1603 /**************************************************************************
1604 Wrappers for extented attribute calls. Based on the Linux package with
1605 support for IRIX and (Net|Free)BSD also. Expand as other systems have them.
1606 ****************************************************************************/
1608 ssize_t
sys_getxattr (const char *path
, const char *name
, void *value
, size_t size
)
1610 #if defined(HAVE_GETXATTR)
1611 #ifndef XATTR_ADD_OPT
1612 return getxattr(path
, name
, value
, size
);
1615 return getxattr(path
, name
, value
, size
, 0, options
);
1617 #elif defined(HAVE_GETEA)
1618 return getea(path
, name
, value
, size
);
1619 #elif defined(HAVE_EXTATTR_GET_FILE)
1622 int attrnamespace
= (strncmp(name
, "system", 6) == 0) ?
1623 EXTATTR_NAMESPACE_SYSTEM
: EXTATTR_NAMESPACE_USER
;
1624 const char *attrname
= ((s
=strchr_m(name
, '.')) == NULL
) ? name
: s
+ 1;
1626 * The BSD implementation has a nasty habit of silently truncating
1627 * the returned value to the size of the buffer, so we have to check
1628 * that the buffer is large enough to fit the returned value.
1630 if((retval
=extattr_get_file(path
, attrnamespace
, attrname
, NULL
, 0)) >= 0) {
1635 if((retval
=extattr_get_file(path
, attrnamespace
, attrname
, value
, size
)) >= 0)
1639 DEBUG(10,("sys_getxattr: extattr_get_file() failed with: %s\n", strerror(errno
)));
1641 #elif defined(HAVE_ATTR_GET)
1642 int retval
, flags
= 0;
1643 int valuelength
= (int)size
;
1644 char *attrname
= strchr(name
,'.') + 1;
1646 if (strncmp(name
, "system", 6) == 0) flags
|= ATTR_ROOT
;
1648 retval
= attr_get(path
, attrname
, (char *)value
, &valuelength
, flags
);
1650 return retval
? retval
: valuelength
;
1651 #elif defined(HAVE_ATTROPEN)
1653 int attrfd
= solaris_attropen(path
, name
, O_RDONLY
, 0);
1655 ret
= solaris_read_xattr(attrfd
, value
, size
);
1665 ssize_t
sys_lgetxattr (const char *path
, const char *name
, void *value
, size_t size
)
1667 #if defined(HAVE_LGETXATTR)
1668 return lgetxattr(path
, name
, value
, size
);
1669 #elif defined(HAVE_GETXATTR) && defined(XATTR_ADD_OPT)
1670 int options
= XATTR_NOFOLLOW
;
1671 return getxattr(path
, name
, value
, size
, 0, options
);
1672 #elif defined(HAVE_LGETEA)
1673 return lgetea(path
, name
, value
, size
);
1674 #elif defined(HAVE_EXTATTR_GET_LINK)
1677 int attrnamespace
= (strncmp(name
, "system", 6) == 0) ?
1678 EXTATTR_NAMESPACE_SYSTEM
: EXTATTR_NAMESPACE_USER
;
1679 const char *attrname
= ((s
=strchr_m(name
, '.')) == NULL
) ? name
: s
+ 1;
1681 if((retval
=extattr_get_link(path
, attrnamespace
, attrname
, NULL
, 0)) >= 0) {
1686 if((retval
=extattr_get_link(path
, attrnamespace
, attrname
, value
, size
)) >= 0)
1690 DEBUG(10,("sys_lgetxattr: extattr_get_link() failed with: %s\n", strerror(errno
)));
1692 #elif defined(HAVE_ATTR_GET)
1693 int retval
, flags
= ATTR_DONTFOLLOW
;
1694 int valuelength
= (int)size
;
1695 char *attrname
= strchr(name
,'.') + 1;
1697 if (strncmp(name
, "system", 6) == 0) flags
|= ATTR_ROOT
;
1699 retval
= attr_get(path
, attrname
, (char *)value
, &valuelength
, flags
);
1701 return retval
? retval
: valuelength
;
1702 #elif defined(HAVE_ATTROPEN)
1704 int attrfd
= solaris_attropen(path
, name
, O_RDONLY
|AT_SYMLINK_NOFOLLOW
, 0);
1706 ret
= solaris_read_xattr(attrfd
, value
, size
);
1716 ssize_t
sys_fgetxattr (int filedes
, const char *name
, void *value
, size_t size
)
1718 #if defined(HAVE_FGETXATTR)
1719 #ifndef XATTR_ADD_OPT
1720 return fgetxattr(filedes
, name
, value
, size
);
1723 return fgetxattr(filedes
, name
, value
, size
, 0, options
);
1725 #elif defined(HAVE_FGETEA)
1726 return fgetea(filedes
, name
, value
, size
);
1727 #elif defined(HAVE_EXTATTR_GET_FD)
1730 int attrnamespace
= (strncmp(name
, "system", 6) == 0) ?
1731 EXTATTR_NAMESPACE_SYSTEM
: EXTATTR_NAMESPACE_USER
;
1732 const char *attrname
= ((s
=strchr_m(name
, '.')) == NULL
) ? name
: s
+ 1;
1734 if((retval
=extattr_get_fd(filedes
, attrnamespace
, attrname
, NULL
, 0)) >= 0) {
1739 if((retval
=extattr_get_fd(filedes
, attrnamespace
, attrname
, value
, size
)) >= 0)
1743 DEBUG(10,("sys_fgetxattr: extattr_get_fd() failed with: %s\n", strerror(errno
)));
1745 #elif defined(HAVE_ATTR_GETF)
1746 int retval
, flags
= 0;
1747 int valuelength
= (int)size
;
1748 char *attrname
= strchr(name
,'.') + 1;
1750 if (strncmp(name
, "system", 6) == 0) flags
|= ATTR_ROOT
;
1752 retval
= attr_getf(filedes
, attrname
, (char *)value
, &valuelength
, flags
);
1754 return retval
? retval
: valuelength
;
1755 #elif defined(HAVE_ATTROPEN)
1757 int attrfd
= solaris_openat(filedes
, name
, O_RDONLY
|O_XATTR
, 0);
1759 ret
= solaris_read_xattr(attrfd
, value
, size
);
1769 #if defined(HAVE_EXTATTR_LIST_FILE)
1771 #define EXTATTR_PREFIX(s) (s), (sizeof((s))-1)
1779 { EXTATTR_NAMESPACE_SYSTEM
, EXTATTR_PREFIX("system.") },
1780 { EXTATTR_NAMESPACE_USER
, EXTATTR_PREFIX("user.") },
1788 static ssize_t
bsd_attr_list (int type
, extattr_arg arg
, char *list
, size_t size
)
1790 ssize_t list_size
, total_size
= 0;
1793 /* Iterate through extattr(2) namespaces */
1794 for(t
= 0; t
< (sizeof(extattr
)/sizeof(extattr
[0])); t
++) {
1796 #if defined(HAVE_EXTATTR_LIST_FILE)
1798 list_size
= extattr_list_file(arg
.path
, extattr
[t
].space
, list
, size
);
1801 #if defined(HAVE_EXTATTR_LIST_LINK)
1803 list_size
= extattr_list_link(arg
.path
, extattr
[t
].space
, list
, size
);
1806 #if defined(HAVE_EXTATTR_LIST_FD)
1808 list_size
= extattr_list_fd(arg
.filedes
, extattr
[t
].space
, list
, size
);
1815 /* Some error happend. Errno should be set by the previous call */
1821 /* XXX: Call with an empty buffer may be used to calculate
1822 necessary buffer size. Unfortunately, we can't say, how
1823 many attributes were returned, so here is the potential
1824 problem with the emulation.
1827 /* Take the worse case of one char attribute names -
1828 two bytes per name plus one more for sanity.
1830 total_size
+= list_size
+ (list_size
/2 + 1)*extattr
[t
].len
;
1833 /* Count necessary offset to fit namespace prefixes */
1835 for(i
= 0; i
< list_size
; i
+= list
[i
] + 1)
1836 len
+= extattr
[t
].len
;
1838 total_size
+= list_size
+ len
;
1839 /* Buffer is too small to fit the results */
1840 if(total_size
> size
) {
1844 /* Shift results back, so we can prepend prefixes */
1845 buf
= (char *)memmove(list
+ len
, list
, list_size
);
1847 for(i
= 0; i
< list_size
; i
+= len
+ 1) {
1849 strncpy(list
, extattr
[t
].name
, extattr
[t
].len
+ 1);
1850 list
+= extattr
[t
].len
;
1851 strncpy(list
, buf
+ i
+ 1, len
);
1862 #if defined(HAVE_ATTR_LIST) && defined(HAVE_SYS_ATTRIBUTES_H)
1863 static char attr_buffer
[ATTR_MAX_VALUELEN
];
1865 static ssize_t
irix_attr_list(const char *path
, int filedes
, char *list
, size_t size
, int flags
)
1867 int retval
= 0, index
;
1868 attrlist_cursor_t
*cursor
= 0;
1870 attrlist_t
* al
= (attrlist_t
*)attr_buffer
;
1872 size_t ent_size
, left
= size
;
1877 retval
= attr_listf(filedes
, attr_buffer
, ATTR_MAX_VALUELEN
, flags
, cursor
);
1879 retval
= attr_list(path
, attr_buffer
, ATTR_MAX_VALUELEN
, flags
, cursor
);
1881 for (index
= 0; index
< al
->al_count
; index
++) {
1882 ae
= ATTR_ENTRY(attr_buffer
, index
);
1883 ent_size
= strlen(ae
->a_name
) + sizeof("user.");
1884 if (left
>= ent_size
) {
1885 strncpy(bp
, "user.", sizeof("user."));
1886 strncat(bp
, ae
->a_name
, ent_size
- sizeof("user."));
1894 total_size
+= ent_size
;
1896 if (al
->al_more
== 0) break;
1903 retval
= attr_listf(filedes
, attr_buffer
, ATTR_MAX_VALUELEN
, flags
, cursor
);
1905 retval
= attr_list(path
, attr_buffer
, ATTR_MAX_VALUELEN
, flags
, cursor
);
1907 for (index
= 0; index
< al
->al_count
; index
++) {
1908 ae
= ATTR_ENTRY(attr_buffer
, index
);
1909 ent_size
= strlen(ae
->a_name
) + sizeof("system.");
1910 if (left
>= ent_size
) {
1911 strncpy(bp
, "system.", sizeof("system."));
1912 strncat(bp
, ae
->a_name
, ent_size
- sizeof("system."));
1920 total_size
+= ent_size
;
1922 if (al
->al_more
== 0) break;
1925 return (ssize_t
)(retval
? retval
: total_size
);
1930 ssize_t
sys_listxattr (const char *path
, char *list
, size_t size
)
1932 #if defined(HAVE_LISTXATTR)
1933 #ifndef XATTR_ADD_OPT
1934 return listxattr(path
, list
, size
);
1937 return listxattr(path
, list
, size
, options
);
1939 #elif defined(HAVE_LISTEA)
1940 return listea(path
, list
, size
);
1941 #elif defined(HAVE_EXTATTR_LIST_FILE)
1944 return bsd_attr_list(0, arg
, list
, size
);
1945 #elif defined(HAVE_ATTR_LIST) && defined(HAVE_SYS_ATTRIBUTES_H)
1946 return irix_attr_list(path
, 0, list
, size
, 0);
1947 #elif defined(HAVE_ATTROPEN)
1949 int attrdirfd
= solaris_attropen(path
, ".", O_RDONLY
, 0);
1950 if (attrdirfd
>= 0) {
1951 ret
= solaris_list_xattr(attrdirfd
, list
, size
);
1961 ssize_t
sys_llistxattr (const char *path
, char *list
, size_t size
)
1963 #if defined(HAVE_LLISTXATTR)
1964 return llistxattr(path
, list
, size
);
1965 #elif defined(HAVE_LISTXATTR) && defined(XATTR_ADD_OPT)
1966 int options
= XATTR_NOFOLLOW
;
1967 return listxattr(path
, list
, size
, options
);
1968 #elif defined(HAVE_LLISTEA)
1969 return llistea(path
, list
, size
);
1970 #elif defined(HAVE_EXTATTR_LIST_LINK)
1973 return bsd_attr_list(1, arg
, list
, size
);
1974 #elif defined(HAVE_ATTR_LIST) && defined(HAVE_SYS_ATTRIBUTES_H)
1975 return irix_attr_list(path
, 0, list
, size
, ATTR_DONTFOLLOW
);
1976 #elif defined(HAVE_ATTROPEN)
1978 int attrdirfd
= solaris_attropen(path
, ".", O_RDONLY
|AT_SYMLINK_NOFOLLOW
, 0);
1979 if (attrdirfd
>= 0) {
1980 ret
= solaris_list_xattr(attrdirfd
, list
, size
);
1990 ssize_t
sys_flistxattr (int filedes
, char *list
, size_t size
)
1992 #if defined(HAVE_FLISTXATTR)
1993 #ifndef XATTR_ADD_OPT
1994 return flistxattr(filedes
, list
, size
);
1997 return flistxattr(filedes
, list
, size
, options
);
1999 #elif defined(HAVE_FLISTEA)
2000 return flistea(filedes
, list
, size
);
2001 #elif defined(HAVE_EXTATTR_LIST_FD)
2003 arg
.filedes
= filedes
;
2004 return bsd_attr_list(2, arg
, list
, size
);
2005 #elif defined(HAVE_ATTR_LISTF)
2006 return irix_attr_list(NULL
, filedes
, list
, size
, 0);
2007 #elif defined(HAVE_ATTROPEN)
2009 int attrdirfd
= solaris_openat(filedes
, ".", O_RDONLY
|O_XATTR
, 0);
2010 if (attrdirfd
>= 0) {
2011 ret
= solaris_list_xattr(attrdirfd
, list
, size
);
2021 int sys_removexattr (const char *path
, const char *name
)
2023 #if defined(HAVE_REMOVEXATTR)
2024 #ifndef XATTR_ADD_OPT
2025 return removexattr(path
, name
);
2028 return removexattr(path
, name
, options
);
2030 #elif defined(HAVE_REMOVEEA)
2031 return removeea(path
, name
);
2032 #elif defined(HAVE_EXTATTR_DELETE_FILE)
2034 int attrnamespace
= (strncmp(name
, "system", 6) == 0) ?
2035 EXTATTR_NAMESPACE_SYSTEM
: EXTATTR_NAMESPACE_USER
;
2036 const char *attrname
= ((s
=strchr_m(name
, '.')) == NULL
) ? name
: s
+ 1;
2038 return extattr_delete_file(path
, attrnamespace
, attrname
);
2039 #elif defined(HAVE_ATTR_REMOVE)
2041 char *attrname
= strchr(name
,'.') + 1;
2043 if (strncmp(name
, "system", 6) == 0) flags
|= ATTR_ROOT
;
2045 return attr_remove(path
, attrname
, flags
);
2046 #elif defined(HAVE_ATTROPEN)
2048 int attrdirfd
= solaris_attropen(path
, ".", O_RDONLY
, 0);
2049 if (attrdirfd
>= 0) {
2050 ret
= solaris_unlinkat(attrdirfd
, name
);
2060 int sys_lremovexattr (const char *path
, const char *name
)
2062 #if defined(HAVE_LREMOVEXATTR)
2063 return lremovexattr(path
, name
);
2064 #elif defined(HAVE_REMOVEXATTR) && defined(XATTR_ADD_OPT)
2065 int options
= XATTR_NOFOLLOW
;
2066 return removexattr(path
, name
, options
);
2067 #elif defined(HAVE_LREMOVEEA)
2068 return lremoveea(path
, name
);
2069 #elif defined(HAVE_EXTATTR_DELETE_LINK)
2071 int attrnamespace
= (strncmp(name
, "system", 6) == 0) ?
2072 EXTATTR_NAMESPACE_SYSTEM
: EXTATTR_NAMESPACE_USER
;
2073 const char *attrname
= ((s
=strchr_m(name
, '.')) == NULL
) ? name
: s
+ 1;
2075 return extattr_delete_link(path
, attrnamespace
, attrname
);
2076 #elif defined(HAVE_ATTR_REMOVE)
2077 int flags
= ATTR_DONTFOLLOW
;
2078 char *attrname
= strchr(name
,'.') + 1;
2080 if (strncmp(name
, "system", 6) == 0) flags
|= ATTR_ROOT
;
2082 return attr_remove(path
, attrname
, flags
);
2083 #elif defined(HAVE_ATTROPEN)
2085 int attrdirfd
= solaris_attropen(path
, ".", O_RDONLY
|AT_SYMLINK_NOFOLLOW
, 0);
2086 if (attrdirfd
>= 0) {
2087 ret
= solaris_unlinkat(attrdirfd
, name
);
2097 int sys_fremovexattr (int filedes
, const char *name
)
2099 #if defined(HAVE_FREMOVEXATTR)
2100 #ifndef XATTR_ADD_OPT
2101 return fremovexattr(filedes
, name
);
2104 return fremovexattr(filedes
, name
, options
);
2106 #elif defined(HAVE_FREMOVEEA)
2107 return fremoveea(filedes
, name
);
2108 #elif defined(HAVE_EXTATTR_DELETE_FD)
2110 int attrnamespace
= (strncmp(name
, "system", 6) == 0) ?
2111 EXTATTR_NAMESPACE_SYSTEM
: EXTATTR_NAMESPACE_USER
;
2112 const char *attrname
= ((s
=strchr_m(name
, '.')) == NULL
) ? name
: s
+ 1;
2114 return extattr_delete_fd(filedes
, attrnamespace
, attrname
);
2115 #elif defined(HAVE_ATTR_REMOVEF)
2117 char *attrname
= strchr(name
,'.') + 1;
2119 if (strncmp(name
, "system", 6) == 0) flags
|= ATTR_ROOT
;
2121 return attr_removef(filedes
, attrname
, flags
);
2122 #elif defined(HAVE_ATTROPEN)
2124 int attrdirfd
= solaris_openat(filedes
, ".", O_RDONLY
|O_XATTR
, 0);
2125 if (attrdirfd
>= 0) {
2126 ret
= solaris_unlinkat(attrdirfd
, name
);
2136 int sys_setxattr (const char *path
, const char *name
, const void *value
, size_t size
, int flags
)
2138 #if defined(HAVE_SETXATTR)
2139 #ifndef XATTR_ADD_OPT
2140 return setxattr(path
, name
, value
, size
, flags
);
2143 return setxattr(path
, name
, value
, size
, 0, options
);
2145 #elif defined(HAVE_SETEA)
2146 return setea(path
, name
, value
, size
, flags
);
2147 #elif defined(HAVE_EXTATTR_SET_FILE)
2150 int attrnamespace
= (strncmp(name
, "system", 6) == 0) ?
2151 EXTATTR_NAMESPACE_SYSTEM
: EXTATTR_NAMESPACE_USER
;
2152 const char *attrname
= ((s
=strchr_m(name
, '.')) == NULL
) ? name
: s
+ 1;
2154 /* Check attribute existence */
2155 retval
= extattr_get_file(path
, attrnamespace
, attrname
, NULL
, 0);
2157 /* REPLACE attribute, that doesn't exist */
2158 if (flags
& XATTR_REPLACE
&& errno
== ENOATTR
) {
2162 /* Ignore other errors */
2165 /* CREATE attribute, that already exists */
2166 if (flags
& XATTR_CREATE
) {
2172 retval
= extattr_set_file(path
, attrnamespace
, attrname
, value
, size
);
2173 return (retval
< 0) ? -1 : 0;
2174 #elif defined(HAVE_ATTR_SET)
2176 char *attrname
= strchr(name
,'.') + 1;
2178 if (strncmp(name
, "system", 6) == 0) myflags
|= ATTR_ROOT
;
2179 if (flags
& XATTR_CREATE
) myflags
|= ATTR_CREATE
;
2180 if (flags
& XATTR_REPLACE
) myflags
|= ATTR_REPLACE
;
2182 return attr_set(path
, attrname
, (const char *)value
, size
, myflags
);
2183 #elif defined(HAVE_ATTROPEN)
2185 int myflags
= O_RDWR
;
2187 if (flags
& XATTR_CREATE
) myflags
|= O_EXCL
;
2188 if (!(flags
& XATTR_REPLACE
)) myflags
|= O_CREAT
;
2189 attrfd
= solaris_attropen(path
, name
, myflags
, (mode_t
) SOLARIS_ATTRMODE
);
2191 ret
= solaris_write_xattr(attrfd
, value
, size
);
2201 int sys_lsetxattr (const char *path
, const char *name
, const void *value
, size_t size
, int flags
)
2203 #if defined(HAVE_LSETXATTR)
2204 return lsetxattr(path
, name
, value
, size
, flags
);
2205 #elif defined(HAVE_SETXATTR) && defined(XATTR_ADD_OPT)
2206 int options
= XATTR_NOFOLLOW
;
2207 return setxattr(path
, name
, value
, size
, 0, options
);
2208 #elif defined(LSETEA)
2209 return lsetea(path
, name
, value
, size
, flags
);
2210 #elif defined(HAVE_EXTATTR_SET_LINK)
2213 int attrnamespace
= (strncmp(name
, "system", 6) == 0) ?
2214 EXTATTR_NAMESPACE_SYSTEM
: EXTATTR_NAMESPACE_USER
;
2215 const char *attrname
= ((s
=strchr_m(name
, '.')) == NULL
) ? name
: s
+ 1;
2217 /* Check attribute existence */
2218 retval
= extattr_get_link(path
, attrnamespace
, attrname
, NULL
, 0);
2220 /* REPLACE attribute, that doesn't exist */
2221 if (flags
& XATTR_REPLACE
&& errno
== ENOATTR
) {
2225 /* Ignore other errors */
2228 /* CREATE attribute, that already exists */
2229 if (flags
& XATTR_CREATE
) {
2236 retval
= extattr_set_link(path
, attrnamespace
, attrname
, value
, size
);
2237 return (retval
< 0) ? -1 : 0;
2238 #elif defined(HAVE_ATTR_SET)
2239 int myflags
= ATTR_DONTFOLLOW
;
2240 char *attrname
= strchr(name
,'.') + 1;
2242 if (strncmp(name
, "system", 6) == 0) myflags
|= ATTR_ROOT
;
2243 if (flags
& XATTR_CREATE
) myflags
|= ATTR_CREATE
;
2244 if (flags
& XATTR_REPLACE
) myflags
|= ATTR_REPLACE
;
2246 return attr_set(path
, attrname
, (const char *)value
, size
, myflags
);
2247 #elif defined(HAVE_ATTROPEN)
2249 int myflags
= O_RDWR
| AT_SYMLINK_NOFOLLOW
;
2251 if (flags
& XATTR_CREATE
) myflags
|= O_EXCL
;
2252 if (!(flags
& XATTR_REPLACE
)) myflags
|= O_CREAT
;
2253 attrfd
= solaris_attropen(path
, name
, myflags
, (mode_t
) SOLARIS_ATTRMODE
);
2255 ret
= solaris_write_xattr(attrfd
, value
, size
);
2265 int sys_fsetxattr (int filedes
, const char *name
, const void *value
, size_t size
, int flags
)
2267 #if defined(HAVE_FSETXATTR)
2268 #ifndef XATTR_ADD_OPT
2269 return fsetxattr(filedes
, name
, value
, size
, flags
);
2272 return fsetxattr(filedes
, name
, value
, size
, 0, options
);
2274 #elif defined(HAVE_FSETEA)
2275 return fsetea(filedes
, name
, value
, size
, flags
);
2276 #elif defined(HAVE_EXTATTR_SET_FD)
2279 int attrnamespace
= (strncmp(name
, "system", 6) == 0) ?
2280 EXTATTR_NAMESPACE_SYSTEM
: EXTATTR_NAMESPACE_USER
;
2281 const char *attrname
= ((s
=strchr_m(name
, '.')) == NULL
) ? name
: s
+ 1;
2283 /* Check attribute existence */
2284 retval
= extattr_get_fd(filedes
, attrnamespace
, attrname
, NULL
, 0);
2286 /* REPLACE attribute, that doesn't exist */
2287 if (flags
& XATTR_REPLACE
&& errno
== ENOATTR
) {
2291 /* Ignore other errors */
2294 /* CREATE attribute, that already exists */
2295 if (flags
& XATTR_CREATE
) {
2301 retval
= extattr_set_fd(filedes
, attrnamespace
, attrname
, value
, size
);
2302 return (retval
< 0) ? -1 : 0;
2303 #elif defined(HAVE_ATTR_SETF)
2305 char *attrname
= strchr(name
,'.') + 1;
2307 if (strncmp(name
, "system", 6) == 0) myflags
|= ATTR_ROOT
;
2308 if (flags
& XATTR_CREATE
) myflags
|= ATTR_CREATE
;
2309 if (flags
& XATTR_REPLACE
) myflags
|= ATTR_REPLACE
;
2311 return attr_setf(filedes
, attrname
, (const char *)value
, size
, myflags
);
2312 #elif defined(HAVE_ATTROPEN)
2314 int myflags
= O_RDWR
| O_XATTR
;
2316 if (flags
& XATTR_CREATE
) myflags
|= O_EXCL
;
2317 if (!(flags
& XATTR_REPLACE
)) myflags
|= O_CREAT
;
2318 attrfd
= solaris_openat(filedes
, name
, myflags
, (mode_t
) SOLARIS_ATTRMODE
);
2320 ret
= solaris_write_xattr(attrfd
, value
, size
);
2330 /**************************************************************************
2331 helper functions for Solaris' EA support
2332 ****************************************************************************/
2333 #ifdef HAVE_ATTROPEN
2334 static ssize_t
solaris_read_xattr(int attrfd
, void *value
, size_t size
)
2338 if (fstat(attrfd
, &sbuf
) == -1) {
2343 /* This is to return the current size of the named extended attribute */
2345 return sbuf
.st_size
;
2348 /* check size and read xattr */
2349 if (sbuf
.st_size
> size
) {
2354 return read(attrfd
, value
, sbuf
.st_size
);
2357 static ssize_t
solaris_list_xattr(int attrdirfd
, char *list
, size_t size
)
2362 int newfd
= dup(attrdirfd
);
2363 /* CAUTION: The originating file descriptor should not be
2364 used again following the call to fdopendir().
2365 For that reason we dup() the file descriptor
2366 here to make things more clear. */
2367 dirp
= fdopendir(newfd
);
2369 while ((de
= readdir(dirp
))) {
2370 size_t listlen
= strlen(de
->d_name
);
2371 if (!strcmp(de
->d_name
, ".") || !strcmp(de
->d_name
, "..")) {
2372 /* we don't want "." and ".." here: */
2373 DEBUG(10,("skipped EA %s\n",de
->d_name
));
2378 /* return the current size of the list of extended attribute names*/
2381 /* check size and copy entrieѕ + nul into list. */
2382 if ((len
+ listlen
+ 1) > size
) {
2387 safe_strcpy(list
+ len
, de
->d_name
, listlen
);
2395 if (closedir(dirp
) == -1) {
2396 DEBUG(0,("closedir dirp failed: %s\n",strerror(errno
)));
2402 static int solaris_unlinkat(int attrdirfd
, const char *name
)
2404 if (unlinkat(attrdirfd
, name
, 0) == -1) {
2405 if (errno
== ENOENT
) {
2413 static int solaris_attropen(const char *path
, const char *attrpath
, int oflag
, mode_t mode
)
2415 int filedes
= attropen(path
, attrpath
, oflag
, mode
);
2416 if (filedes
== -1) {
2417 DEBUG(10,("attropen FAILED: path: %s, name: %s, errno: %s\n",path
,attrpath
,strerror(errno
)));
2418 if (errno
== EINVAL
) {
2427 static int solaris_openat(int fildes
, const char *path
, int oflag
, mode_t mode
)
2429 int filedes
= openat(fildes
, path
, oflag
, mode
);
2430 if (filedes
== -1) {
2431 DEBUG(10,("openat FAILED: fd: %d, path: %s, errno: %s\n",filedes
,path
,strerror(errno
)));
2432 if (errno
== EINVAL
) {
2441 static int solaris_write_xattr(int attrfd
, const char *value
, size_t size
)
2443 if ((ftruncate(attrfd
, 0) == 0) && (write(attrfd
, value
, size
) == size
)) {
2446 DEBUG(10,("solaris_write_xattr FAILED!\n"));
2450 #endif /*HAVE_ATTROPEN*/
2453 /****************************************************************************
2454 Return the major devicenumber for UNIX extensions.
2455 ****************************************************************************/
2457 uint32
unix_dev_major(SMB_DEV_T dev
)
2459 #if defined(HAVE_DEVICE_MAJOR_FN)
2460 return (uint32
)major(dev
);
2462 return (uint32
)(dev
>> 8);
2466 /****************************************************************************
2467 Return the minor devicenumber for UNIX extensions.
2468 ****************************************************************************/
2470 uint32
unix_dev_minor(SMB_DEV_T dev
)
2472 #if defined(HAVE_DEVICE_MINOR_FN)
2473 return (uint32
)minor(dev
);
2475 return (uint32
)(dev
& 0xff);
2479 #if defined(WITH_AIO)
2481 /*******************************************************************
2482 An aio_read wrapper that will deal with 64-bit sizes.
2483 ********************************************************************/
2485 int sys_aio_read(SMB_STRUCT_AIOCB
*aiocb
)
2487 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_READ64)
2488 return aio_read64(aiocb
);
2489 #elif defined(HAVE_AIO_READ)
2490 return aio_read(aiocb
);
2497 /*******************************************************************
2498 An aio_write wrapper that will deal with 64-bit sizes.
2499 ********************************************************************/
2501 int sys_aio_write(SMB_STRUCT_AIOCB
*aiocb
)
2503 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_WRITE64)
2504 return aio_write64(aiocb
);
2505 #elif defined(HAVE_AIO_WRITE)
2506 return aio_write(aiocb
);
2513 /*******************************************************************
2514 An aio_return wrapper that will deal with 64-bit sizes.
2515 ********************************************************************/
2517 ssize_t
sys_aio_return(SMB_STRUCT_AIOCB
*aiocb
)
2519 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_RETURN64)
2520 return aio_return64(aiocb
);
2521 #elif defined(HAVE_AIO_RETURN)
2522 return aio_return(aiocb
);
2529 /*******************************************************************
2530 An aio_cancel wrapper that will deal with 64-bit sizes.
2531 ********************************************************************/
2533 int sys_aio_cancel(int fd
, SMB_STRUCT_AIOCB
*aiocb
)
2535 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_CANCEL64)
2536 return aio_cancel64(fd
, aiocb
);
2537 #elif defined(HAVE_AIO_CANCEL)
2538 return aio_cancel(fd
, aiocb
);
2545 /*******************************************************************
2546 An aio_error wrapper that will deal with 64-bit sizes.
2547 ********************************************************************/
2549 int sys_aio_error(const SMB_STRUCT_AIOCB
*aiocb
)
2551 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_ERROR64)
2552 return aio_error64(aiocb
);
2553 #elif defined(HAVE_AIO_ERROR)
2554 return aio_error(aiocb
);
2561 /*******************************************************************
2562 An aio_fsync wrapper that will deal with 64-bit sizes.
2563 ********************************************************************/
2565 int sys_aio_fsync(int op
, SMB_STRUCT_AIOCB
*aiocb
)
2567 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_FSYNC64)
2568 return aio_fsync64(op
, aiocb
);
2569 #elif defined(HAVE_AIO_FSYNC)
2570 return aio_fsync(op
, aiocb
);
2577 /*******************************************************************
2578 An aio_fsync wrapper that will deal with 64-bit sizes.
2579 ********************************************************************/
2581 int sys_aio_suspend(const SMB_STRUCT_AIOCB
* const cblist
[], int n
, const struct timespec
*timeout
)
2583 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_SUSPEND64)
2584 return aio_suspend64(cblist
, n
, timeout
);
2585 #elif defined(HAVE_AIO_FSYNC)
2586 return aio_suspend(cblist
, n
, timeout
);
2592 #else /* !WITH_AIO */
2594 int sys_aio_read(SMB_STRUCT_AIOCB
*aiocb
)
2600 int sys_aio_write(SMB_STRUCT_AIOCB
*aiocb
)
2606 ssize_t
sys_aio_return(SMB_STRUCT_AIOCB
*aiocb
)
2612 int sys_aio_cancel(int fd
, SMB_STRUCT_AIOCB
*aiocb
)
2618 int sys_aio_error(const SMB_STRUCT_AIOCB
*aiocb
)
2624 int sys_aio_fsync(int op
, SMB_STRUCT_AIOCB
*aiocb
)
2630 int sys_aio_suspend(const SMB_STRUCT_AIOCB
* const cblist
[], int n
, const struct timespec
*timeout
)
2635 #endif /* WITH_AIO */
2637 int sys_getpeereid( int s
, uid_t
*uid
)
2639 #if defined(HAVE_PEERCRED)
2641 socklen_t cred_len
= sizeof(struct ucred
);
2644 ret
= getsockopt(s
, SOL_SOCKET
, SO_PEERCRED
, (void *)&cred
, &cred_len
);
2649 if (cred_len
!= sizeof(struct ucred
)) {
2657 #if defined(HAVE_GETPEEREID)
2659 return getpeereid(s
, uid
, &gid
);
2666 int sys_getnameinfo(const struct sockaddr
*psa
,
2675 * For Solaris we must make sure salen is the
2676 * correct length for the incoming sa_family.
2679 if (salen
== sizeof(struct sockaddr_storage
)) {
2680 salen
= sizeof(struct sockaddr_in
);
2681 #if defined(HAVE_IPV6)
2682 if (psa
->sa_family
== AF_INET6
) {
2683 salen
= sizeof(struct sockaddr_in6
);
2687 return getnameinfo(psa
, salen
, host
, hostlen
, service
, servlen
, flags
);
2690 int sys_connect(int fd
, const struct sockaddr
* addr
)
2692 socklen_t salen
= (socklen_t
)-1;
2694 if (addr
->sa_family
== AF_INET
) {
2695 salen
= sizeof(struct sockaddr_in
);
2696 } else if (addr
->sa_family
== AF_UNIX
) {
2697 salen
= sizeof(struct sockaddr_un
);
2699 #if defined(HAVE_IPV6)
2700 else if (addr
->sa_family
== AF_INET6
) {
2701 salen
= sizeof(struct sockaddr_in6
);
2705 return connect(fd
, addr
, salen
);